Commit | Line | Data |
---|---|---|
b6aeaded MS |
1 | /* |
2 | FUSE: Filesystem in Userspace | |
1729a16c | 3 | Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu> |
b6aeaded 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" | |
6fa3eb70 | 10 | #include "fuse.h" |
b6aeaded MS |
11 | |
12 | #include <linux/pagemap.h> | |
13 | #include <linux/slab.h> | |
14 | #include <linux/kernel.h> | |
e8edc6e0 | 15 | #include <linux/sched.h> |
08cbf542 | 16 | #include <linux/module.h> |
d9d318d3 | 17 | #include <linux/compat.h> |
478e0841 | 18 | #include <linux/swap.h> |
a27bb332 | 19 | #include <linux/aio.h> |
3634a632 | 20 | #include <linux/falloc.h> |
6fa3eb70 S |
21 | #include <asm/div64.h> |
22 | ||
23 | #ifdef FUSEIO_TRACE | |
24 | struct mutex fuse_iolog_lock; | |
25 | ||
26 | static struct fuse_proc_info fuse_iolog[FUSE_IOLOG_MAX]; | |
27 | static struct timespec fuse_iolog_time; | |
28 | static struct task_struct *fuse_iolog_thread=NULL; | |
29 | ||
30 | void fuse_time_diff( | |
31 | struct timespec *start, | |
32 | struct timespec *end, | |
33 | struct timespec *diff) | |
34 | { | |
35 | if ((end->tv_nsec-start->tv_nsec)<0) { | |
36 | diff->tv_sec = end->tv_sec-start->tv_sec-1; | |
37 | diff->tv_nsec = 1000000000+end->tv_nsec-start->tv_nsec; | |
38 | } else { | |
39 | diff->tv_sec = end->tv_sec-start->tv_sec; | |
40 | diff->tv_nsec = end->tv_nsec-start->tv_nsec; | |
41 | } | |
42 | return; | |
43 | } | |
44 | ||
45 | struct fuse_ioiog_type_map | |
46 | { | |
47 | int type; | |
48 | const char *str; | |
49 | }; | |
50 | ||
51 | #define FUSE_IOLOG_TYPE_MAX 46 | |
52 | ||
53 | static const char *fuse_iolog_type[FUSE_IOLOG_TYPE_MAX]={ | |
54 | "unknown", /*0*/ | |
55 | "lookup", /*1*/ | |
56 | "forget", /*2*/ | |
57 | "getattr", /*3*/ | |
58 | "setattr", /*4*/ | |
59 | "readlink", /*5*/ | |
60 | "symlink", /*6*/ | |
61 | "", /*7*/ | |
62 | "mknod", /*8*/ | |
63 | "mkdir", /*9*/ | |
64 | "unlink", /*10*/ | |
65 | "rmdir", /*11*/ | |
66 | "rename", /*12*/ | |
67 | "link", /*13*/ | |
68 | "open", /*14*/ | |
69 | "read", /*15*/ | |
70 | "write", /*16*/ | |
71 | "statfs", /*17*/ | |
72 | "release", /*18*/ | |
73 | "", /*19*/ | |
74 | "fsync", /*20*/ | |
75 | "setxattr", /*21*/ | |
76 | "getxattr", /*22*/ | |
77 | "listxattr", /*23*/ | |
78 | "removexattr", /*24*/ | |
79 | "flush", /*25*/ | |
80 | "init", /*26*/ | |
81 | "opendir", /*27*/ | |
82 | "readdir", /*28*/ | |
83 | "releasedir", /*29*/ | |
84 | "fsyncdir", /*30*/ | |
85 | "getlk", /*31*/ | |
86 | "setlk", /*32*/ | |
87 | "setlkw", /*33*/ | |
88 | "access", /*34*/ | |
89 | "create", /*35*/ | |
90 | "interrupt", /*36*/ | |
91 | "bmap", /*37*/ | |
92 | "destroy", /*38*/ | |
93 | "ioctl", /*39*/ | |
94 | "poll", /*40*/ | |
95 | "notify_reply", /*41*/ | |
96 | "batch_forget", /*42*/ | |
97 | "fallocate", /*43*/ | |
98 | "readdirplus", /*44*/ | |
99 | "cuse" | |
100 | }; | |
101 | ||
102 | static const char *fuse_iolog_type2str(int type) | |
103 | { | |
104 | if (type>=0 && type<FUSE_IOLOG_TYPE_MAX) | |
105 | goto out; | |
106 | else if (type==CUSE_INIT) | |
107 | type=FUSE_IOLOG_TYPE_MAX-1; | |
108 | else | |
109 | type=0; | |
110 | out: | |
111 | return fuse_iolog_type[type]; | |
112 | } | |
113 | ||
114 | int fuse_iolog_print(void) | |
115 | { | |
116 | int i, len, n; | |
117 | char buf[FUSE_IOLOG_BUFLEN], *ptr; | |
118 | ||
119 | len=FUSE_IOLOG_BUFLEN-1; | |
120 | ptr=&buf[0]; | |
121 | ||
122 | for (i=0;i<FUSE_IOLOG_MAX && fuse_iolog[i].valid;i++) { | |
123 | ||
124 | if (fuse_iolog[i].read.count || fuse_iolog[i].write.count) { | |
125 | n=snprintf(ptr, len, "{%d:R(%d,%d,%d),W(%d,%d,%d)}", | |
126 | fuse_iolog[i].pid, | |
127 | fuse_iolog[i].read.bytes, | |
128 | fuse_iolog[i].read.count, | |
129 | fuse_iolog[i].read.us, | |
130 | fuse_iolog[i].write.bytes, | |
131 | fuse_iolog[i].write.count, | |
132 | fuse_iolog[i].write.us); | |
133 | ||
134 | len -=n; | |
135 | ptr +=n; | |
136 | ||
137 | if (len<0) | |
138 | goto overflow; | |
139 | } | |
140 | ||
141 | if (fuse_iolog[i].misc_type) { | |
142 | n=snprintf(ptr, len, "{%d:%s(%d,%d,%d)}", | |
143 | fuse_iolog[i].pid, | |
144 | fuse_iolog_type2str(fuse_iolog[i].misc_type), | |
145 | fuse_iolog[i].misc.bytes, | |
146 | fuse_iolog[i].misc.count, | |
147 | fuse_iolog[i].misc.us); | |
148 | len -=n; | |
149 | ptr +=n; | |
150 | ||
151 | if (len<0) | |
152 | goto overflow; | |
153 | } | |
154 | } | |
155 | ||
156 | if (i>0) | |
157 | xlog_printk(ANDROID_LOG_DEBUG, "BLOCK_TAG", "FUSEIO %s\n", buf); | |
158 | ||
159 | return ptr - &buf[0]; | |
160 | ||
161 | overflow: | |
162 | xlog_printk(ANDROID_LOG_DEBUG, "BLOCK_TAG", | |
163 | "FUSEIO log buffer overflow \n"); | |
164 | ||
165 | return -1; | |
166 | } | |
167 | ||
168 | void fuse_iolog_proc_clear(void) | |
169 | { | |
170 | memset(&fuse_iolog[0], 0, sizeof(struct fuse_proc_info)*FUSE_IOLOG_MAX); | |
171 | get_monotonic_boottime(&fuse_iolog_time); | |
172 | } | |
173 | ||
174 | inline __u32 fuse_iolog_timeus(struct timespec *t) | |
175 | { | |
176 | __u32 _t; | |
177 | long us; | |
178 | ||
179 | us = t->tv_nsec; | |
180 | do_div(us, 1000); | |
181 | ||
182 | if (t->tv_sec > 3600) | |
183 | return 0xD693A400; /* 3600000000 */ | |
184 | else | |
185 | _t = t->tv_sec * 1000000 + us; | |
186 | ||
187 | if (_t) | |
188 | return _t; | |
189 | else | |
190 | return 1; | |
191 | } | |
192 | ||
193 | __u32 fuse_iolog_timeus_diff(struct timespec *start, struct timespec *end) | |
194 | { | |
195 | struct timespec diff; | |
196 | fuse_time_diff(start, end, &diff); | |
197 | return fuse_iolog_timeus(&diff); | |
198 | } | |
199 | ||
200 | ||
201 | inline int fuse_iolog_proc_update(struct fuse_proc_info *info, | |
202 | __u32 io_bytes, int type, struct timespec *diff) | |
203 | { | |
204 | struct fuse_rw_info *rwi; | |
205 | __u32 _t; | |
206 | ||
207 | _t = fuse_iolog_timeus(diff); | |
208 | ||
209 | if (type==FUSE_READ) | |
210 | rwi = &info->read; | |
211 | else if (type == FUSE_WRITE) | |
212 | rwi = &info->write; | |
213 | else { | |
214 | if (info->misc_type==0) | |
215 | info->misc_type=type; | |
216 | else if (info->misc_type!=type) /* misc type mismatch => continue */ | |
217 | return -1; | |
218 | rwi = &info->misc; | |
219 | } | |
220 | ||
221 | rwi->bytes += io_bytes; | |
222 | rwi->us += _t; | |
223 | rwi->count ++; | |
224 | ||
225 | return 0; | |
226 | } | |
227 | ||
228 | static int fuse_iolog_watch(void *arg) | |
229 | { | |
230 | unsigned int timeout; | |
231 | int n; | |
232 | struct timespec curr, diff; | |
233 | ||
234 | while (1) { | |
235 | if (kthread_should_stop()) break; | |
236 | ||
237 | get_monotonic_boottime(&curr); | |
238 | ||
239 | mutex_lock(&fuse_iolog_lock); | |
240 | fuse_time_diff(&fuse_iolog_time, &curr, &diff); | |
241 | ||
242 | n=fuse_iolog_print(); | |
243 | ||
244 | if (n>0) | |
245 | fuse_iolog_proc_clear(); | |
246 | ||
247 | mutex_unlock(&fuse_iolog_lock); | |
248 | ||
249 | do { | |
250 | set_current_state(TASK_INTERRUPTIBLE); | |
251 | timeout = schedule_timeout(FUSE_IOLOG_LATENCY*HZ); | |
252 | } while(timeout); | |
253 | } | |
254 | ||
255 | return 0; | |
256 | } | |
257 | ||
258 | void fuse_iolog_init(void) | |
259 | { | |
260 | int ret; | |
261 | ||
262 | mutex_init(&fuse_iolog_lock); | |
263 | mutex_lock(&fuse_iolog_lock); | |
264 | fuse_iolog_proc_clear(); | |
265 | mutex_unlock(&fuse_iolog_lock); | |
266 | ||
267 | fuse_iolog_thread=kthread_create(fuse_iolog_watch, NULL, "fuse_log"); | |
268 | if (IS_ERR(fuse_iolog_thread)) { | |
269 | ret = PTR_ERR(fuse_iolog_thread); | |
270 | xlog_printk(ANDROID_LOG_DEBUG, "BLOCK_TAG", | |
271 | "Fail to create fuse_log thread %d\n", ret); | |
272 | fuse_iolog_thread = NULL; | |
273 | goto out; | |
274 | } | |
275 | wake_up_process(fuse_iolog_thread); | |
276 | out: | |
277 | return; | |
278 | } | |
279 | ||
280 | void fuse_iolog_exit(void) | |
281 | { | |
282 | kthread_stop(fuse_iolog_thread); | |
283 | } | |
284 | void fuse_iolog_add(__u32 io_bytes, int type, | |
285 | struct timespec *start, | |
286 | struct timespec *end) | |
287 | { | |
288 | struct fuse_proc_info *info; | |
289 | struct timespec diff; | |
290 | pid_t pid; | |
291 | int i; | |
292 | pid = task_pid_nr(current); | |
293 | fuse_time_diff(start, end, &diff); | |
294 | ||
295 | mutex_lock(&fuse_iolog_lock); | |
296 | ||
297 | for (i=0;i<FUSE_IOLOG_MAX;i++) { | |
298 | info=&fuse_iolog[i]; | |
299 | ||
300 | if (info->valid) { | |
301 | if (info->pid == pid) { | |
302 | if (fuse_iolog_proc_update(info, io_bytes, type, &diff)) { | |
303 | continue; // ops mismatch | |
304 | } | |
305 | else | |
306 | goto out; | |
307 | } | |
308 | else { | |
309 | continue; | |
310 | } | |
311 | } | |
312 | else { | |
313 | info->valid=1; | |
314 | info->pid=pid; | |
315 | fuse_iolog_proc_update(info, io_bytes, type, &diff); | |
316 | if (i==0) | |
317 | get_monotonic_boottime(&fuse_iolog_time); | |
318 | goto out; | |
319 | } | |
320 | } | |
321 | ||
322 | if (i==FUSE_IOLOG_MAX) { | |
323 | fuse_iolog_print(); | |
324 | fuse_iolog_proc_clear(); | |
325 | info=&fuse_iolog[0]; | |
326 | info->valid=1; | |
327 | info->pid=pid; | |
328 | fuse_iolog_proc_update(info, io_bytes, type, &diff); | |
329 | } | |
330 | out: | |
331 | mutex_unlock(&fuse_iolog_lock); | |
332 | } | |
333 | ||
334 | #endif | |
b6aeaded | 335 | |
4b6f5d20 | 336 | static const struct file_operations fuse_direct_io_file_operations; |
45323fb7 | 337 | |
91fe96b4 MS |
338 | static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file, |
339 | int opcode, struct fuse_open_out *outargp) | |
b6aeaded | 340 | { |
b6aeaded | 341 | struct fuse_open_in inarg; |
fd72faac MS |
342 | struct fuse_req *req; |
343 | int err; | |
344 | ||
b111c8c0 | 345 | req = fuse_get_req_nopages(fc); |
ce1d5a49 MS |
346 | if (IS_ERR(req)) |
347 | return PTR_ERR(req); | |
fd72faac MS |
348 | |
349 | memset(&inarg, 0, sizeof(inarg)); | |
6ff958ed MS |
350 | inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY); |
351 | if (!fc->atomic_o_trunc) | |
352 | inarg.flags &= ~O_TRUNC; | |
91fe96b4 MS |
353 | req->in.h.opcode = opcode; |
354 | req->in.h.nodeid = nodeid; | |
fd72faac MS |
355 | req->in.numargs = 1; |
356 | req->in.args[0].size = sizeof(inarg); | |
357 | req->in.args[0].value = &inarg; | |
358 | req->out.numargs = 1; | |
359 | req->out.args[0].size = sizeof(*outargp); | |
360 | req->out.args[0].value = outargp; | |
b93f858a | 361 | fuse_request_send(fc, req); |
fd72faac MS |
362 | err = req->out.h.error; |
363 | fuse_put_request(fc, req); | |
364 | ||
365 | return err; | |
366 | } | |
367 | ||
acf99433 | 368 | struct fuse_file *fuse_file_alloc(struct fuse_conn *fc) |
fd72faac MS |
369 | { |
370 | struct fuse_file *ff; | |
6b2db28a | 371 | |
c2ace304 | 372 | ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL); |
6b2db28a TH |
373 | if (unlikely(!ff)) |
374 | return NULL; | |
375 | ||
da5e4714 | 376 | ff->fc = fc; |
4250c066 | 377 | ff->reserved_req = fuse_request_alloc(0); |
6b2db28a TH |
378 | if (unlikely(!ff->reserved_req)) { |
379 | kfree(ff); | |
380 | return NULL; | |
fd72faac | 381 | } |
6b2db28a TH |
382 | |
383 | INIT_LIST_HEAD(&ff->write_entry); | |
384 | atomic_set(&ff->count, 0); | |
385 | RB_CLEAR_NODE(&ff->polled_node); | |
386 | init_waitqueue_head(&ff->poll_wait); | |
387 | ||
388 | spin_lock(&fc->lock); | |
389 | ff->kh = ++fc->khctr; | |
390 | spin_unlock(&fc->lock); | |
391 | ||
fd72faac MS |
392 | return ff; |
393 | } | |
394 | ||
395 | void fuse_file_free(struct fuse_file *ff) | |
396 | { | |
33649c91 | 397 | fuse_request_free(ff->reserved_req); |
fd72faac MS |
398 | kfree(ff); |
399 | } | |
400 | ||
c7b7143c | 401 | struct fuse_file *fuse_file_get(struct fuse_file *ff) |
c756e0a4 MS |
402 | { |
403 | atomic_inc(&ff->count); | |
404 | return ff; | |
405 | } | |
406 | ||
5a18ec17 MS |
407 | static void fuse_release_async(struct work_struct *work) |
408 | { | |
409 | struct fuse_req *req; | |
410 | struct fuse_conn *fc; | |
411 | struct path path; | |
412 | ||
413 | req = container_of(work, struct fuse_req, misc.release.work); | |
414 | path = req->misc.release.path; | |
415 | fc = get_fuse_conn(path.dentry->d_inode); | |
416 | ||
417 | fuse_put_request(fc, req); | |
418 | path_put(&path); | |
419 | } | |
420 | ||
819c4b3b MS |
421 | static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req) |
422 | { | |
5a18ec17 MS |
423 | if (fc->destroy_req) { |
424 | /* | |
425 | * If this is a fuseblk mount, then it's possible that | |
426 | * releasing the path will result in releasing the | |
427 | * super block and sending the DESTROY request. If | |
428 | * the server is single threaded, this would hang. | |
429 | * For this reason do the path_put() in a separate | |
430 | * thread. | |
431 | */ | |
432 | atomic_inc(&req->count); | |
433 | INIT_WORK(&req->misc.release.work, fuse_release_async); | |
434 | schedule_work(&req->misc.release.work); | |
435 | } else { | |
436 | path_put(&req->misc.release.path); | |
437 | } | |
819c4b3b MS |
438 | } |
439 | ||
5a18ec17 | 440 | static void fuse_file_put(struct fuse_file *ff, bool sync) |
c756e0a4 MS |
441 | { |
442 | if (atomic_dec_and_test(&ff->count)) { | |
443 | struct fuse_req *req = ff->reserved_req; | |
8b0797a4 | 444 | |
5a18ec17 | 445 | if (sync) { |
5beea857 | 446 | req->force = 1; |
8b41e671 | 447 | req->background = 0; |
5a18ec17 MS |
448 | fuse_request_send(ff->fc, req); |
449 | path_put(&req->misc.release.path); | |
450 | fuse_put_request(ff->fc, req); | |
451 | } else { | |
452 | req->end = fuse_release_end; | |
8b41e671 | 453 | req->background = 1; |
5a18ec17 MS |
454 | fuse_request_send_background(ff->fc, req); |
455 | } | |
c756e0a4 MS |
456 | kfree(ff); |
457 | } | |
458 | } | |
459 | ||
08cbf542 TH |
460 | int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file, |
461 | bool isdir) | |
91fe96b4 MS |
462 | { |
463 | struct fuse_open_out outarg; | |
464 | struct fuse_file *ff; | |
465 | int err; | |
466 | int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN; | |
467 | ||
468 | ff = fuse_file_alloc(fc); | |
469 | if (!ff) | |
470 | return -ENOMEM; | |
471 | ||
472 | err = fuse_send_open(fc, nodeid, file, opcode, &outarg); | |
473 | if (err) { | |
474 | fuse_file_free(ff); | |
475 | return err; | |
476 | } | |
477 | ||
478 | if (isdir) | |
479 | outarg.open_flags &= ~FOPEN_DIRECT_IO; | |
480 | ||
481 | ff->fh = outarg.fh; | |
482 | ff->nodeid = nodeid; | |
483 | ff->open_flags = outarg.open_flags; | |
484 | file->private_data = fuse_file_get(ff); | |
485 | ||
486 | return 0; | |
487 | } | |
08cbf542 | 488 | EXPORT_SYMBOL_GPL(fuse_do_open); |
91fe96b4 | 489 | |
c7b7143c | 490 | void fuse_finish_open(struct inode *inode, struct file *file) |
fd72faac | 491 | { |
c7b7143c | 492 | struct fuse_file *ff = file->private_data; |
a0822c55 | 493 | struct fuse_conn *fc = get_fuse_conn(inode); |
c7b7143c MS |
494 | |
495 | if (ff->open_flags & FOPEN_DIRECT_IO) | |
fd72faac | 496 | file->f_op = &fuse_direct_io_file_operations; |
c7b7143c | 497 | if (!(ff->open_flags & FOPEN_KEEP_CACHE)) |
b1009979 | 498 | invalidate_inode_pages2(inode->i_mapping); |
c7b7143c | 499 | if (ff->open_flags & FOPEN_NONSEEKABLE) |
a7c1b990 | 500 | nonseekable_open(inode, file); |
a0822c55 KS |
501 | if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) { |
502 | struct fuse_inode *fi = get_fuse_inode(inode); | |
503 | ||
504 | spin_lock(&fc->lock); | |
505 | fi->attr_version = ++fc->attr_version; | |
506 | i_size_write(inode, 0); | |
507 | spin_unlock(&fc->lock); | |
508 | fuse_invalidate_attr(inode); | |
509 | } | |
fd72faac MS |
510 | } |
511 | ||
91fe96b4 | 512 | int fuse_open_common(struct inode *inode, struct file *file, bool isdir) |
fd72faac | 513 | { |
acf99433 | 514 | struct fuse_conn *fc = get_fuse_conn(inode); |
b6aeaded | 515 | int err; |
b6aeaded MS |
516 | |
517 | err = generic_file_open(inode, file); | |
518 | if (err) | |
519 | return err; | |
520 | ||
91fe96b4 | 521 | err = fuse_do_open(fc, get_node_id(inode), file, isdir); |
fd72faac | 522 | if (err) |
91fe96b4 | 523 | return err; |
b6aeaded | 524 | |
91fe96b4 MS |
525 | fuse_finish_open(inode, file); |
526 | ||
527 | return 0; | |
b6aeaded MS |
528 | } |
529 | ||
8b0797a4 | 530 | static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode) |
64c6d8ed | 531 | { |
8b0797a4 | 532 | struct fuse_conn *fc = ff->fc; |
33649c91 | 533 | struct fuse_req *req = ff->reserved_req; |
b57d4264 | 534 | struct fuse_release_in *inarg = &req->misc.release.in; |
b6aeaded | 535 | |
8b0797a4 MS |
536 | spin_lock(&fc->lock); |
537 | list_del(&ff->write_entry); | |
538 | if (!RB_EMPTY_NODE(&ff->polled_node)) | |
539 | rb_erase(&ff->polled_node, &fc->polled_files); | |
540 | spin_unlock(&fc->lock); | |
541 | ||
357ccf2b | 542 | wake_up_interruptible_all(&ff->poll_wait); |
8b0797a4 | 543 | |
b6aeaded | 544 | inarg->fh = ff->fh; |
fd72faac | 545 | inarg->flags = flags; |
51eb01e7 | 546 | req->in.h.opcode = opcode; |
c7b7143c | 547 | req->in.h.nodeid = ff->nodeid; |
b6aeaded MS |
548 | req->in.numargs = 1; |
549 | req->in.args[0].size = sizeof(struct fuse_release_in); | |
550 | req->in.args[0].value = inarg; | |
fd72faac MS |
551 | } |
552 | ||
8b0797a4 | 553 | void fuse_release_common(struct file *file, int opcode) |
fd72faac | 554 | { |
6b2db28a TH |
555 | struct fuse_file *ff; |
556 | struct fuse_req *req; | |
b6aeaded | 557 | |
6b2db28a TH |
558 | ff = file->private_data; |
559 | if (unlikely(!ff)) | |
8b0797a4 | 560 | return; |
6b2db28a | 561 | |
6b2db28a | 562 | req = ff->reserved_req; |
8b0797a4 | 563 | fuse_prepare_release(ff, file->f_flags, opcode); |
6b2db28a | 564 | |
37fb3a30 MS |
565 | if (ff->flock) { |
566 | struct fuse_release_in *inarg = &req->misc.release.in; | |
567 | inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK; | |
568 | inarg->lock_owner = fuse_lock_owner_id(ff->fc, | |
569 | (fl_owner_t) file); | |
570 | } | |
6b2db28a | 571 | /* Hold vfsmount and dentry until release is finished */ |
b0be46eb MS |
572 | path_get(&file->f_path); |
573 | req->misc.release.path = file->f_path; | |
6b2db28a | 574 | |
6b2db28a TH |
575 | /* |
576 | * Normally this will send the RELEASE request, however if | |
577 | * some asynchronous READ or WRITE requests are outstanding, | |
578 | * the sending will be delayed. | |
5a18ec17 MS |
579 | * |
580 | * Make the release synchronous if this is a fuseblk mount, | |
581 | * synchronous RELEASE is allowed (and desirable) in this case | |
582 | * because the server can be trusted not to screw up. | |
6b2db28a | 583 | */ |
5a18ec17 | 584 | fuse_file_put(ff, ff->fc->destroy_req != NULL); |
b6aeaded MS |
585 | } |
586 | ||
04730fef MS |
587 | static int fuse_open(struct inode *inode, struct file *file) |
588 | { | |
91fe96b4 | 589 | return fuse_open_common(inode, file, false); |
04730fef MS |
590 | } |
591 | ||
592 | static int fuse_release(struct inode *inode, struct file *file) | |
593 | { | |
8b0797a4 MS |
594 | fuse_release_common(file, FUSE_RELEASE); |
595 | ||
596 | /* return value is ignored by VFS */ | |
597 | return 0; | |
598 | } | |
599 | ||
600 | void fuse_sync_release(struct fuse_file *ff, int flags) | |
601 | { | |
602 | WARN_ON(atomic_read(&ff->count) > 1); | |
603 | fuse_prepare_release(ff, flags, FUSE_RELEASE); | |
604 | ff->reserved_req->force = 1; | |
8b41e671 | 605 | ff->reserved_req->background = 0; |
8b0797a4 MS |
606 | fuse_request_send(ff->fc, ff->reserved_req); |
607 | fuse_put_request(ff->fc, ff->reserved_req); | |
608 | kfree(ff); | |
04730fef | 609 | } |
08cbf542 | 610 | EXPORT_SYMBOL_GPL(fuse_sync_release); |
04730fef | 611 | |
71421259 | 612 | /* |
9c8ef561 MS |
613 | * Scramble the ID space with XTEA, so that the value of the files_struct |
614 | * pointer is not exposed to userspace. | |
71421259 | 615 | */ |
f3332114 | 616 | u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id) |
71421259 | 617 | { |
9c8ef561 MS |
618 | u32 *k = fc->scramble_key; |
619 | u64 v = (unsigned long) id; | |
620 | u32 v0 = v; | |
621 | u32 v1 = v >> 32; | |
622 | u32 sum = 0; | |
623 | int i; | |
624 | ||
625 | for (i = 0; i < 32; i++) { | |
626 | v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); | |
627 | sum += 0x9E3779B9; | |
628 | v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); | |
629 | } | |
630 | ||
631 | return (u64) v0 + ((u64) v1 << 32); | |
71421259 MS |
632 | } |
633 | ||
3be5a52b MS |
634 | /* |
635 | * Check if page is under writeback | |
636 | * | |
637 | * This is currently done by walking the list of writepage requests | |
638 | * for the inode, which can be pretty inefficient. | |
639 | */ | |
640 | static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index) | |
641 | { | |
642 | struct fuse_conn *fc = get_fuse_conn(inode); | |
643 | struct fuse_inode *fi = get_fuse_inode(inode); | |
644 | struct fuse_req *req; | |
645 | bool found = false; | |
646 | ||
647 | spin_lock(&fc->lock); | |
648 | list_for_each_entry(req, &fi->writepages, writepages_entry) { | |
649 | pgoff_t curr_index; | |
650 | ||
651 | BUG_ON(req->inode != inode); | |
652 | curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT; | |
653 | if (curr_index == index) { | |
654 | found = true; | |
655 | break; | |
656 | } | |
657 | } | |
658 | spin_unlock(&fc->lock); | |
659 | ||
660 | return found; | |
661 | } | |
662 | ||
663 | /* | |
664 | * Wait for page writeback to be completed. | |
665 | * | |
666 | * Since fuse doesn't rely on the VM writeback tracking, this has to | |
667 | * use some other means. | |
668 | */ | |
669 | static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index) | |
670 | { | |
671 | struct fuse_inode *fi = get_fuse_inode(inode); | |
672 | ||
673 | wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index)); | |
674 | return 0; | |
675 | } | |
676 | ||
75e1fcc0 | 677 | static int fuse_flush(struct file *file, fl_owner_t id) |
b6aeaded | 678 | { |
6131ffaa | 679 | struct inode *inode = file_inode(file); |
b6aeaded MS |
680 | struct fuse_conn *fc = get_fuse_conn(inode); |
681 | struct fuse_file *ff = file->private_data; | |
682 | struct fuse_req *req; | |
683 | struct fuse_flush_in inarg; | |
684 | int err; | |
685 | ||
248d86e8 MS |
686 | if (is_bad_inode(inode)) |
687 | return -EIO; | |
688 | ||
b6aeaded MS |
689 | if (fc->no_flush) |
690 | return 0; | |
691 | ||
b111c8c0 | 692 | req = fuse_get_req_nofail_nopages(fc, file); |
b6aeaded MS |
693 | memset(&inarg, 0, sizeof(inarg)); |
694 | inarg.fh = ff->fh; | |
9c8ef561 | 695 | inarg.lock_owner = fuse_lock_owner_id(fc, id); |
b6aeaded MS |
696 | req->in.h.opcode = FUSE_FLUSH; |
697 | req->in.h.nodeid = get_node_id(inode); | |
b6aeaded MS |
698 | req->in.numargs = 1; |
699 | req->in.args[0].size = sizeof(inarg); | |
700 | req->in.args[0].value = &inarg; | |
71421259 | 701 | req->force = 1; |
b93f858a | 702 | fuse_request_send(fc, req); |
b6aeaded MS |
703 | err = req->out.h.error; |
704 | fuse_put_request(fc, req); | |
705 | if (err == -ENOSYS) { | |
706 | fc->no_flush = 1; | |
707 | err = 0; | |
708 | } | |
709 | return err; | |
710 | } | |
711 | ||
3be5a52b MS |
712 | /* |
713 | * Wait for all pending writepages on the inode to finish. | |
714 | * | |
715 | * This is currently done by blocking further writes with FUSE_NOWRITE | |
716 | * and waiting for all sent writes to complete. | |
717 | * | |
718 | * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage | |
719 | * could conflict with truncation. | |
720 | */ | |
721 | static void fuse_sync_writes(struct inode *inode) | |
722 | { | |
723 | fuse_set_nowrite(inode); | |
724 | fuse_release_nowrite(inode); | |
725 | } | |
726 | ||
02c24a82 JB |
727 | int fuse_fsync_common(struct file *file, loff_t start, loff_t end, |
728 | int datasync, int isdir) | |
b6aeaded | 729 | { |
7ea80859 | 730 | struct inode *inode = file->f_mapping->host; |
b6aeaded MS |
731 | struct fuse_conn *fc = get_fuse_conn(inode); |
732 | struct fuse_file *ff = file->private_data; | |
733 | struct fuse_req *req; | |
734 | struct fuse_fsync_in inarg; | |
735 | int err; | |
736 | ||
248d86e8 MS |
737 | if (is_bad_inode(inode)) |
738 | return -EIO; | |
739 | ||
02c24a82 JB |
740 | err = filemap_write_and_wait_range(inode->i_mapping, start, end); |
741 | if (err) | |
742 | return err; | |
743 | ||
82547981 | 744 | if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir)) |
b6aeaded MS |
745 | return 0; |
746 | ||
02c24a82 JB |
747 | mutex_lock(&inode->i_mutex); |
748 | ||
3be5a52b MS |
749 | /* |
750 | * Start writeback against all dirty pages of the inode, then | |
751 | * wait for all outstanding writes, before sending the FSYNC | |
752 | * request. | |
753 | */ | |
754 | err = write_inode_now(inode, 0); | |
755 | if (err) | |
02c24a82 | 756 | goto out; |
3be5a52b MS |
757 | |
758 | fuse_sync_writes(inode); | |
759 | ||
b111c8c0 | 760 | req = fuse_get_req_nopages(fc); |
02c24a82 JB |
761 | if (IS_ERR(req)) { |
762 | err = PTR_ERR(req); | |
763 | goto out; | |
764 | } | |
b6aeaded MS |
765 | |
766 | memset(&inarg, 0, sizeof(inarg)); | |
767 | inarg.fh = ff->fh; | |
768 | inarg.fsync_flags = datasync ? 1 : 0; | |
82547981 | 769 | req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC; |
b6aeaded | 770 | req->in.h.nodeid = get_node_id(inode); |
b6aeaded MS |
771 | req->in.numargs = 1; |
772 | req->in.args[0].size = sizeof(inarg); | |
773 | req->in.args[0].value = &inarg; | |
b93f858a | 774 | fuse_request_send(fc, req); |
b6aeaded MS |
775 | err = req->out.h.error; |
776 | fuse_put_request(fc, req); | |
777 | if (err == -ENOSYS) { | |
82547981 MS |
778 | if (isdir) |
779 | fc->no_fsyncdir = 1; | |
780 | else | |
781 | fc->no_fsync = 1; | |
b6aeaded MS |
782 | err = 0; |
783 | } | |
02c24a82 JB |
784 | out: |
785 | mutex_unlock(&inode->i_mutex); | |
b6aeaded MS |
786 | return err; |
787 | } | |
788 | ||
02c24a82 JB |
789 | static int fuse_fsync(struct file *file, loff_t start, loff_t end, |
790 | int datasync) | |
82547981 | 791 | { |
02c24a82 | 792 | return fuse_fsync_common(file, start, end, datasync, 0); |
82547981 MS |
793 | } |
794 | ||
2106cb18 MS |
795 | void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos, |
796 | size_t count, int opcode) | |
b6aeaded | 797 | { |
5c5c5e51 | 798 | struct fuse_read_in *inarg = &req->misc.read.in; |
a6643094 | 799 | struct fuse_file *ff = file->private_data; |
b6aeaded | 800 | |
361b1eb5 MS |
801 | inarg->fh = ff->fh; |
802 | inarg->offset = pos; | |
803 | inarg->size = count; | |
a6643094 | 804 | inarg->flags = file->f_flags; |
361b1eb5 | 805 | req->in.h.opcode = opcode; |
2106cb18 | 806 | req->in.h.nodeid = ff->nodeid; |
b6aeaded MS |
807 | req->in.numargs = 1; |
808 | req->in.args[0].size = sizeof(struct fuse_read_in); | |
c1aa96a5 | 809 | req->in.args[0].value = inarg; |
b6aeaded MS |
810 | req->out.argvar = 1; |
811 | req->out.numargs = 1; | |
812 | req->out.args[0].size = count; | |
b6aeaded MS |
813 | } |
814 | ||
187c5c36 MP |
815 | static void fuse_release_user_pages(struct fuse_req *req, int write) |
816 | { | |
817 | unsigned i; | |
818 | ||
819 | for (i = 0; i < req->num_pages; i++) { | |
820 | struct page *page = req->pages[i]; | |
821 | if (write) | |
822 | set_page_dirty_lock(page); | |
823 | put_page(page); | |
824 | } | |
825 | } | |
826 | ||
01e9d11a MP |
827 | /** |
828 | * In case of short read, the caller sets 'pos' to the position of | |
829 | * actual end of fuse request in IO request. Otherwise, if bytes_requested | |
830 | * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1. | |
831 | * | |
832 | * An example: | |
833 | * User requested DIO read of 64K. It was splitted into two 32K fuse requests, | |
834 | * both submitted asynchronously. The first of them was ACKed by userspace as | |
835 | * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The | |
836 | * second request was ACKed as short, e.g. only 1K was read, resulting in | |
837 | * pos == 33K. | |
838 | * | |
839 | * Thus, when all fuse requests are completed, the minimal non-negative 'pos' | |
840 | * will be equal to the length of the longest contiguous fragment of | |
841 | * transferred data starting from the beginning of IO request. | |
842 | */ | |
843 | static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos) | |
844 | { | |
845 | int left; | |
846 | ||
847 | spin_lock(&io->lock); | |
848 | if (err) | |
849 | io->err = io->err ? : err; | |
850 | else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes)) | |
851 | io->bytes = pos; | |
852 | ||
853 | left = --io->reqs; | |
854 | spin_unlock(&io->lock); | |
855 | ||
856 | if (!left) { | |
857 | long res; | |
858 | ||
859 | if (io->err) | |
860 | res = io->err; | |
861 | else if (io->bytes >= 0 && io->write) | |
862 | res = -EIO; | |
863 | else { | |
864 | res = io->bytes < 0 ? io->size : io->bytes; | |
865 | ||
866 | if (!is_sync_kiocb(io->iocb)) { | |
867 | struct path *path = &io->iocb->ki_filp->f_path; | |
868 | struct inode *inode = path->dentry->d_inode; | |
869 | struct fuse_conn *fc = get_fuse_conn(inode); | |
870 | struct fuse_inode *fi = get_fuse_inode(inode); | |
871 | ||
872 | spin_lock(&fc->lock); | |
873 | fi->attr_version = ++fc->attr_version; | |
874 | spin_unlock(&fc->lock); | |
875 | } | |
876 | } | |
877 | ||
878 | aio_complete(io->iocb, res, 0); | |
879 | kfree(io); | |
880 | } | |
881 | } | |
882 | ||
883 | static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req) | |
884 | { | |
885 | struct fuse_io_priv *io = req->io; | |
886 | ssize_t pos = -1; | |
887 | ||
888 | fuse_release_user_pages(req, !io->write); | |
889 | ||
890 | if (io->write) { | |
891 | if (req->misc.write.in.size != req->misc.write.out.size) | |
892 | pos = req->misc.write.in.offset - io->offset + | |
893 | req->misc.write.out.size; | |
894 | } else { | |
895 | if (req->misc.read.in.size != req->out.args[0].size) | |
896 | pos = req->misc.read.in.offset - io->offset + | |
897 | req->out.args[0].size; | |
898 | } | |
899 | ||
900 | fuse_aio_complete(io, req->out.h.error, pos); | |
901 | } | |
902 | ||
903 | static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req, | |
904 | size_t num_bytes, struct fuse_io_priv *io) | |
905 | { | |
906 | spin_lock(&io->lock); | |
907 | io->size += num_bytes; | |
908 | io->reqs++; | |
909 | spin_unlock(&io->lock); | |
910 | ||
911 | req->io = io; | |
912 | req->end = fuse_aio_complete_req; | |
913 | ||
36cf66ed | 914 | __fuse_get_request(req); |
01e9d11a MP |
915 | fuse_request_send_background(fc, req); |
916 | ||
917 | return num_bytes; | |
918 | } | |
919 | ||
36cf66ed | 920 | static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io, |
2106cb18 | 921 | loff_t pos, size_t count, fl_owner_t owner) |
04730fef | 922 | { |
36cf66ed | 923 | struct file *file = io->file; |
2106cb18 MS |
924 | struct fuse_file *ff = file->private_data; |
925 | struct fuse_conn *fc = ff->fc; | |
f3332114 | 926 | |
2106cb18 | 927 | fuse_read_fill(req, file, pos, count, FUSE_READ); |
f3332114 | 928 | if (owner != NULL) { |
5c5c5e51 | 929 | struct fuse_read_in *inarg = &req->misc.read.in; |
f3332114 MS |
930 | |
931 | inarg->read_flags |= FUSE_READ_LOCKOWNER; | |
932 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); | |
933 | } | |
36cf66ed MP |
934 | |
935 | if (io->async) | |
936 | return fuse_async_req_send(fc, req, count, io); | |
937 | ||
b93f858a | 938 | fuse_request_send(fc, req); |
361b1eb5 | 939 | return req->out.args[0].size; |
04730fef MS |
940 | } |
941 | ||
5c5c5e51 MS |
942 | static void fuse_read_update_size(struct inode *inode, loff_t size, |
943 | u64 attr_ver) | |
944 | { | |
945 | struct fuse_conn *fc = get_fuse_conn(inode); | |
946 | struct fuse_inode *fi = get_fuse_inode(inode); | |
947 | ||
948 | spin_lock(&fc->lock); | |
4e208303 MP |
949 | if (attr_ver == fi->attr_version && size < inode->i_size && |
950 | !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) { | |
5c5c5e51 MS |
951 | fi->attr_version = ++fc->attr_version; |
952 | i_size_write(inode, size); | |
953 | } | |
954 | spin_unlock(&fc->lock); | |
955 | } | |
956 | ||
b6aeaded MS |
957 | static int fuse_readpage(struct file *file, struct page *page) |
958 | { | |
36cf66ed | 959 | struct fuse_io_priv io = { .async = 0, .file = file }; |
b6aeaded MS |
960 | struct inode *inode = page->mapping->host; |
961 | struct fuse_conn *fc = get_fuse_conn(inode); | |
248d86e8 | 962 | struct fuse_req *req; |
5c5c5e51 MS |
963 | size_t num_read; |
964 | loff_t pos = page_offset(page); | |
965 | size_t count = PAGE_CACHE_SIZE; | |
966 | u64 attr_ver; | |
248d86e8 MS |
967 | int err; |
968 | ||
969 | err = -EIO; | |
970 | if (is_bad_inode(inode)) | |
971 | goto out; | |
972 | ||
3be5a52b | 973 | /* |
25985edc | 974 | * Page writeback can extend beyond the lifetime of the |
3be5a52b MS |
975 | * page-cache page, so make sure we read a properly synced |
976 | * page. | |
977 | */ | |
978 | fuse_wait_on_page_writeback(inode, page->index); | |
979 | ||
b111c8c0 | 980 | req = fuse_get_req(fc, 1); |
ce1d5a49 MS |
981 | err = PTR_ERR(req); |
982 | if (IS_ERR(req)) | |
b6aeaded MS |
983 | goto out; |
984 | ||
5c5c5e51 MS |
985 | attr_ver = fuse_get_attr_version(fc); |
986 | ||
b6aeaded | 987 | req->out.page_zeroing = 1; |
f4975c67 | 988 | req->out.argpages = 1; |
b6aeaded MS |
989 | req->num_pages = 1; |
990 | req->pages[0] = page; | |
85f40aec | 991 | req->page_descs[0].length = count; |
36cf66ed | 992 | num_read = fuse_send_read(req, &io, pos, count, NULL); |
b6aeaded MS |
993 | err = req->out.h.error; |
994 | fuse_put_request(fc, req); | |
5c5c5e51 MS |
995 | |
996 | if (!err) { | |
997 | /* | |
998 | * Short read means EOF. If file size is larger, truncate it | |
999 | */ | |
1000 | if (num_read < count) | |
1001 | fuse_read_update_size(inode, pos + num_read, attr_ver); | |
1002 | ||
b6aeaded | 1003 | SetPageUptodate(page); |
5c5c5e51 MS |
1004 | } |
1005 | ||
b36c31ba | 1006 | fuse_invalidate_attr(inode); /* atime changed */ |
b6aeaded MS |
1007 | out: |
1008 | unlock_page(page); | |
1009 | return err; | |
1010 | } | |
1011 | ||
c1aa96a5 | 1012 | static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req) |
db50b96c | 1013 | { |
c1aa96a5 | 1014 | int i; |
5c5c5e51 MS |
1015 | size_t count = req->misc.read.in.size; |
1016 | size_t num_read = req->out.args[0].size; | |
ce534fb0 | 1017 | struct address_space *mapping = NULL; |
c1aa96a5 | 1018 | |
ce534fb0 MS |
1019 | for (i = 0; mapping == NULL && i < req->num_pages; i++) |
1020 | mapping = req->pages[i]->mapping; | |
5c5c5e51 | 1021 | |
ce534fb0 MS |
1022 | if (mapping) { |
1023 | struct inode *inode = mapping->host; | |
1024 | ||
1025 | /* | |
1026 | * Short read means EOF. If file size is larger, truncate it | |
1027 | */ | |
1028 | if (!req->out.h.error && num_read < count) { | |
1029 | loff_t pos; | |
1030 | ||
1031 | pos = page_offset(req->pages[0]) + num_read; | |
1032 | fuse_read_update_size(inode, pos, | |
1033 | req->misc.read.attr_ver); | |
1034 | } | |
1035 | fuse_invalidate_attr(inode); /* atime changed */ | |
1036 | } | |
c1aa96a5 | 1037 | |
db50b96c MS |
1038 | for (i = 0; i < req->num_pages; i++) { |
1039 | struct page *page = req->pages[i]; | |
1040 | if (!req->out.h.error) | |
1041 | SetPageUptodate(page); | |
c1aa96a5 MS |
1042 | else |
1043 | SetPageError(page); | |
db50b96c | 1044 | unlock_page(page); |
b5dd3285 | 1045 | page_cache_release(page); |
db50b96c | 1046 | } |
c756e0a4 | 1047 | if (req->ff) |
5a18ec17 | 1048 | fuse_file_put(req->ff, false); |
c1aa96a5 MS |
1049 | } |
1050 | ||
2106cb18 | 1051 | static void fuse_send_readpages(struct fuse_req *req, struct file *file) |
c1aa96a5 | 1052 | { |
2106cb18 MS |
1053 | struct fuse_file *ff = file->private_data; |
1054 | struct fuse_conn *fc = ff->fc; | |
c1aa96a5 MS |
1055 | loff_t pos = page_offset(req->pages[0]); |
1056 | size_t count = req->num_pages << PAGE_CACHE_SHIFT; | |
f4975c67 MS |
1057 | |
1058 | req->out.argpages = 1; | |
c1aa96a5 | 1059 | req->out.page_zeroing = 1; |
ce534fb0 | 1060 | req->out.page_replace = 1; |
2106cb18 | 1061 | fuse_read_fill(req, file, pos, count, FUSE_READ); |
5c5c5e51 | 1062 | req->misc.read.attr_ver = fuse_get_attr_version(fc); |
9cd68455 | 1063 | if (fc->async_read) { |
c756e0a4 | 1064 | req->ff = fuse_file_get(ff); |
9cd68455 | 1065 | req->end = fuse_readpages_end; |
6fa3eb70 | 1066 | fuse_request_send_background_ex(fc, req, count); |
9cd68455 | 1067 | } else { |
6fa3eb70 | 1068 | fuse_request_send_ex(fc, req, count); |
9cd68455 | 1069 | fuse_readpages_end(fc, req); |
e9bb09dd | 1070 | fuse_put_request(fc, req); |
9cd68455 | 1071 | } |
db50b96c MS |
1072 | } |
1073 | ||
c756e0a4 | 1074 | struct fuse_fill_data { |
db50b96c | 1075 | struct fuse_req *req; |
a6643094 | 1076 | struct file *file; |
db50b96c | 1077 | struct inode *inode; |
f8dbdf81 | 1078 | unsigned nr_pages; |
db50b96c MS |
1079 | }; |
1080 | ||
1081 | static int fuse_readpages_fill(void *_data, struct page *page) | |
1082 | { | |
c756e0a4 | 1083 | struct fuse_fill_data *data = _data; |
db50b96c MS |
1084 | struct fuse_req *req = data->req; |
1085 | struct inode *inode = data->inode; | |
1086 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1087 | ||
3be5a52b MS |
1088 | fuse_wait_on_page_writeback(inode, page->index); |
1089 | ||
db50b96c MS |
1090 | if (req->num_pages && |
1091 | (req->num_pages == FUSE_MAX_PAGES_PER_REQ || | |
1092 | (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read || | |
1093 | req->pages[req->num_pages - 1]->index + 1 != page->index)) { | |
f8dbdf81 MP |
1094 | int nr_alloc = min_t(unsigned, data->nr_pages, |
1095 | FUSE_MAX_PAGES_PER_REQ); | |
2106cb18 | 1096 | fuse_send_readpages(req, data->file); |
8b41e671 MP |
1097 | if (fc->async_read) |
1098 | req = fuse_get_req_for_background(fc, nr_alloc); | |
1099 | else | |
1100 | req = fuse_get_req(fc, nr_alloc); | |
1101 | ||
1102 | data->req = req; | |
ce1d5a49 | 1103 | if (IS_ERR(req)) { |
db50b96c | 1104 | unlock_page(page); |
ce1d5a49 | 1105 | return PTR_ERR(req); |
db50b96c | 1106 | } |
db50b96c | 1107 | } |
f8dbdf81 MP |
1108 | |
1109 | if (WARN_ON(req->num_pages >= req->max_pages)) { | |
1110 | fuse_put_request(fc, req); | |
1111 | return -EIO; | |
1112 | } | |
1113 | ||
b5dd3285 | 1114 | page_cache_get(page); |
db50b96c | 1115 | req->pages[req->num_pages] = page; |
85f40aec | 1116 | req->page_descs[req->num_pages].length = PAGE_SIZE; |
1729a16c | 1117 | req->num_pages++; |
f8dbdf81 | 1118 | data->nr_pages--; |
db50b96c MS |
1119 | return 0; |
1120 | } | |
1121 | ||
1122 | static int fuse_readpages(struct file *file, struct address_space *mapping, | |
1123 | struct list_head *pages, unsigned nr_pages) | |
1124 | { | |
1125 | struct inode *inode = mapping->host; | |
1126 | struct fuse_conn *fc = get_fuse_conn(inode); | |
c756e0a4 | 1127 | struct fuse_fill_data data; |
db50b96c | 1128 | int err; |
f8dbdf81 | 1129 | int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ); |
248d86e8 | 1130 | |
1d7ea732 | 1131 | err = -EIO; |
248d86e8 | 1132 | if (is_bad_inode(inode)) |
2e990021 | 1133 | goto out; |
248d86e8 | 1134 | |
a6643094 | 1135 | data.file = file; |
db50b96c | 1136 | data.inode = inode; |
8b41e671 MP |
1137 | if (fc->async_read) |
1138 | data.req = fuse_get_req_for_background(fc, nr_alloc); | |
1139 | else | |
1140 | data.req = fuse_get_req(fc, nr_alloc); | |
f8dbdf81 | 1141 | data.nr_pages = nr_pages; |
1d7ea732 | 1142 | err = PTR_ERR(data.req); |
ce1d5a49 | 1143 | if (IS_ERR(data.req)) |
2e990021 | 1144 | goto out; |
db50b96c MS |
1145 | |
1146 | err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data); | |
d3406ffa MS |
1147 | if (!err) { |
1148 | if (data.req->num_pages) | |
2106cb18 | 1149 | fuse_send_readpages(data.req, file); |
d3406ffa MS |
1150 | else |
1151 | fuse_put_request(fc, data.req); | |
1152 | } | |
2e990021 | 1153 | out: |
1d7ea732 | 1154 | return err; |
db50b96c MS |
1155 | } |
1156 | ||
bcb4be80 MS |
1157 | static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov, |
1158 | unsigned long nr_segs, loff_t pos) | |
1159 | { | |
1160 | struct inode *inode = iocb->ki_filp->f_mapping->host; | |
a8894274 | 1161 | struct fuse_conn *fc = get_fuse_conn(inode); |
bcb4be80 | 1162 | |
a8894274 BF |
1163 | /* |
1164 | * In auto invalidate mode, always update attributes on read. | |
1165 | * Otherwise, only update if we attempt to read past EOF (to ensure | |
1166 | * i_size is up to date). | |
1167 | */ | |
1168 | if (fc->auto_inval_data || | |
1169 | (pos + iov_length(iov, nr_segs) > i_size_read(inode))) { | |
bcb4be80 | 1170 | int err; |
bcb4be80 MS |
1171 | err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL); |
1172 | if (err) | |
1173 | return err; | |
1174 | } | |
1175 | ||
1176 | return generic_file_aio_read(iocb, iov, nr_segs, pos); | |
1177 | } | |
1178 | ||
2d698b07 | 1179 | static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff, |
2106cb18 | 1180 | loff_t pos, size_t count) |
b6aeaded | 1181 | { |
b25e82e5 MS |
1182 | struct fuse_write_in *inarg = &req->misc.write.in; |
1183 | struct fuse_write_out *outarg = &req->misc.write.out; | |
b6aeaded | 1184 | |
b25e82e5 MS |
1185 | inarg->fh = ff->fh; |
1186 | inarg->offset = pos; | |
1187 | inarg->size = count; | |
b6aeaded | 1188 | req->in.h.opcode = FUSE_WRITE; |
2106cb18 | 1189 | req->in.h.nodeid = ff->nodeid; |
b6aeaded | 1190 | req->in.numargs = 2; |
2106cb18 | 1191 | if (ff->fc->minor < 9) |
f3332114 MS |
1192 | req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE; |
1193 | else | |
1194 | req->in.args[0].size = sizeof(struct fuse_write_in); | |
b25e82e5 | 1195 | req->in.args[0].value = inarg; |
b6aeaded MS |
1196 | req->in.args[1].size = count; |
1197 | req->out.numargs = 1; | |
1198 | req->out.args[0].size = sizeof(struct fuse_write_out); | |
b25e82e5 MS |
1199 | req->out.args[0].value = outarg; |
1200 | } | |
1201 | ||
36cf66ed | 1202 | static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io, |
2106cb18 | 1203 | loff_t pos, size_t count, fl_owner_t owner) |
b25e82e5 | 1204 | { |
36cf66ed | 1205 | struct file *file = io->file; |
2106cb18 MS |
1206 | struct fuse_file *ff = file->private_data; |
1207 | struct fuse_conn *fc = ff->fc; | |
2d698b07 MS |
1208 | struct fuse_write_in *inarg = &req->misc.write.in; |
1209 | ||
2106cb18 | 1210 | fuse_write_fill(req, ff, pos, count); |
2d698b07 | 1211 | inarg->flags = file->f_flags; |
f3332114 | 1212 | if (owner != NULL) { |
f3332114 MS |
1213 | inarg->write_flags |= FUSE_WRITE_LOCKOWNER; |
1214 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); | |
1215 | } | |
36cf66ed MP |
1216 | |
1217 | if (io->async) | |
1218 | return fuse_async_req_send(fc, req, count, io); | |
1219 | ||
6fa3eb70 | 1220 | fuse_request_send_ex(fc, req, count); |
b25e82e5 | 1221 | return req->misc.write.out.size; |
b6aeaded MS |
1222 | } |
1223 | ||
a1d75f25 | 1224 | void fuse_write_update_size(struct inode *inode, loff_t pos) |
854512ec MS |
1225 | { |
1226 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1227 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1228 | ||
1229 | spin_lock(&fc->lock); | |
1230 | fi->attr_version = ++fc->attr_version; | |
1231 | if (pos > inode->i_size) | |
1232 | i_size_write(inode, pos); | |
1233 | spin_unlock(&fc->lock); | |
1234 | } | |
1235 | ||
ea9b9907 NP |
1236 | static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file, |
1237 | struct inode *inode, loff_t pos, | |
1238 | size_t count) | |
1239 | { | |
1240 | size_t res; | |
1241 | unsigned offset; | |
1242 | unsigned i; | |
36cf66ed | 1243 | struct fuse_io_priv io = { .async = 0, .file = file }; |
ea9b9907 NP |
1244 | |
1245 | for (i = 0; i < req->num_pages; i++) | |
1246 | fuse_wait_on_page_writeback(inode, req->pages[i]->index); | |
1247 | ||
36cf66ed | 1248 | res = fuse_send_write(req, &io, pos, count, NULL); |
ea9b9907 | 1249 | |
b2430d75 | 1250 | offset = req->page_descs[0].offset; |
ea9b9907 NP |
1251 | count = res; |
1252 | for (i = 0; i < req->num_pages; i++) { | |
1253 | struct page *page = req->pages[i]; | |
1254 | ||
1255 | if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE) | |
1256 | SetPageUptodate(page); | |
1257 | ||
1258 | if (count > PAGE_CACHE_SIZE - offset) | |
1259 | count -= PAGE_CACHE_SIZE - offset; | |
1260 | else | |
1261 | count = 0; | |
1262 | offset = 0; | |
1263 | ||
1264 | unlock_page(page); | |
1265 | page_cache_release(page); | |
1266 | } | |
1267 | ||
1268 | return res; | |
1269 | } | |
1270 | ||
1271 | static ssize_t fuse_fill_write_pages(struct fuse_req *req, | |
1272 | struct address_space *mapping, | |
1273 | struct iov_iter *ii, loff_t pos) | |
1274 | { | |
1275 | struct fuse_conn *fc = get_fuse_conn(mapping->host); | |
1276 | unsigned offset = pos & (PAGE_CACHE_SIZE - 1); | |
1277 | size_t count = 0; | |
1278 | int err; | |
1279 | ||
f4975c67 | 1280 | req->in.argpages = 1; |
b2430d75 | 1281 | req->page_descs[0].offset = offset; |
ea9b9907 NP |
1282 | |
1283 | do { | |
1284 | size_t tmp; | |
1285 | struct page *page; | |
1286 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; | |
1287 | size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset, | |
1288 | iov_iter_count(ii)); | |
1289 | ||
1290 | bytes = min_t(size_t, bytes, fc->max_write - count); | |
1291 | ||
1292 | again: | |
1293 | err = -EFAULT; | |
1294 | if (iov_iter_fault_in_readable(ii, bytes)) | |
1295 | break; | |
1296 | ||
1297 | err = -ENOMEM; | |
54566b2c | 1298 | page = grab_cache_page_write_begin(mapping, index, 0); |
ea9b9907 NP |
1299 | if (!page) |
1300 | break; | |
1301 | ||
931e80e4 | 1302 | if (mapping_writably_mapped(mapping)) |
1303 | flush_dcache_page(page); | |
1304 | ||
ea9b9907 NP |
1305 | pagefault_disable(); |
1306 | tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes); | |
1307 | pagefault_enable(); | |
1308 | flush_dcache_page(page); | |
1309 | ||
478e0841 JW |
1310 | mark_page_accessed(page); |
1311 | ||
020ef191 | 1312 | iov_iter_advance(ii, tmp); |
ea9b9907 NP |
1313 | if (!tmp) { |
1314 | unlock_page(page); | |
1315 | page_cache_release(page); | |
1316 | bytes = min(bytes, iov_iter_single_seg_count(ii)); | |
1317 | goto again; | |
1318 | } | |
1319 | ||
1320 | err = 0; | |
1321 | req->pages[req->num_pages] = page; | |
85f40aec | 1322 | req->page_descs[req->num_pages].length = tmp; |
ea9b9907 NP |
1323 | req->num_pages++; |
1324 | ||
ea9b9907 NP |
1325 | count += tmp; |
1326 | pos += tmp; | |
1327 | offset += tmp; | |
1328 | if (offset == PAGE_CACHE_SIZE) | |
1329 | offset = 0; | |
1330 | ||
78bb6cb9 MS |
1331 | if (!fc->big_writes) |
1332 | break; | |
ea9b9907 | 1333 | } while (iov_iter_count(ii) && count < fc->max_write && |
d07f09f5 | 1334 | req->num_pages < req->max_pages && offset == 0); |
ea9b9907 NP |
1335 | |
1336 | return count > 0 ? count : err; | |
1337 | } | |
1338 | ||
d07f09f5 MP |
1339 | static inline unsigned fuse_wr_pages(loff_t pos, size_t len) |
1340 | { | |
1341 | return min_t(unsigned, | |
1342 | ((pos + len - 1) >> PAGE_CACHE_SHIFT) - | |
1343 | (pos >> PAGE_CACHE_SHIFT) + 1, | |
1344 | FUSE_MAX_PAGES_PER_REQ); | |
1345 | } | |
1346 | ||
ea9b9907 NP |
1347 | static ssize_t fuse_perform_write(struct file *file, |
1348 | struct address_space *mapping, | |
1349 | struct iov_iter *ii, loff_t pos) | |
1350 | { | |
1351 | struct inode *inode = mapping->host; | |
1352 | struct fuse_conn *fc = get_fuse_conn(inode); | |
4e208303 | 1353 | struct fuse_inode *fi = get_fuse_inode(inode); |
ea9b9907 NP |
1354 | int err = 0; |
1355 | ssize_t res = 0; | |
1356 | ||
1357 | if (is_bad_inode(inode)) | |
1358 | return -EIO; | |
1359 | ||
4e208303 MP |
1360 | if (inode->i_size < pos + iov_iter_count(ii)) |
1361 | set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); | |
1362 | ||
ea9b9907 NP |
1363 | do { |
1364 | struct fuse_req *req; | |
1365 | ssize_t count; | |
d07f09f5 | 1366 | unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii)); |
ea9b9907 | 1367 | |
d07f09f5 | 1368 | req = fuse_get_req(fc, nr_pages); |
ea9b9907 NP |
1369 | if (IS_ERR(req)) { |
1370 | err = PTR_ERR(req); | |
1371 | break; | |
1372 | } | |
1373 | ||
1374 | count = fuse_fill_write_pages(req, mapping, ii, pos); | |
1375 | if (count <= 0) { | |
1376 | err = count; | |
1377 | } else { | |
1378 | size_t num_written; | |
1379 | ||
1380 | num_written = fuse_send_write_pages(req, file, inode, | |
1381 | pos, count); | |
1382 | err = req->out.h.error; | |
1383 | if (!err) { | |
1384 | res += num_written; | |
1385 | pos += num_written; | |
1386 | ||
1387 | /* break out of the loop on short write */ | |
1388 | if (num_written != count) | |
1389 | err = -EIO; | |
1390 | } | |
1391 | } | |
1392 | fuse_put_request(fc, req); | |
1393 | } while (!err && iov_iter_count(ii)); | |
1394 | ||
1395 | if (res > 0) | |
1396 | fuse_write_update_size(inode, pos); | |
1397 | ||
4e208303 | 1398 | clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); |
ea9b9907 NP |
1399 | fuse_invalidate_attr(inode); |
1400 | ||
1401 | return res > 0 ? res : err; | |
1402 | } | |
1403 | ||
1404 | static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov, | |
1405 | unsigned long nr_segs, loff_t pos) | |
1406 | { | |
1407 | struct file *file = iocb->ki_filp; | |
1408 | struct address_space *mapping = file->f_mapping; | |
1409 | size_t count = 0; | |
4273b793 | 1410 | size_t ocount = 0; |
ea9b9907 | 1411 | ssize_t written = 0; |
4273b793 | 1412 | ssize_t written_buffered = 0; |
ea9b9907 NP |
1413 | struct inode *inode = mapping->host; |
1414 | ssize_t err; | |
1415 | struct iov_iter i; | |
4273b793 | 1416 | loff_t endbyte = 0; |
ea9b9907 NP |
1417 | |
1418 | WARN_ON(iocb->ki_pos != pos); | |
1419 | ||
4273b793 AA |
1420 | ocount = 0; |
1421 | err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ); | |
ea9b9907 NP |
1422 | if (err) |
1423 | return err; | |
1424 | ||
4273b793 | 1425 | count = ocount; |
ea9b9907 | 1426 | mutex_lock(&inode->i_mutex); |
ea9b9907 NP |
1427 | |
1428 | /* We can write back this queue in page reclaim */ | |
1429 | current->backing_dev_info = mapping->backing_dev_info; | |
1430 | ||
1431 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); | |
1432 | if (err) | |
1433 | goto out; | |
1434 | ||
1435 | if (count == 0) | |
1436 | goto out; | |
1437 | ||
2f1936b8 | 1438 | err = file_remove_suid(file); |
ea9b9907 NP |
1439 | if (err) |
1440 | goto out; | |
1441 | ||
c3b2da31 JB |
1442 | err = file_update_time(file); |
1443 | if (err) | |
1444 | goto out; | |
ea9b9907 | 1445 | |
4273b793 AA |
1446 | if (file->f_flags & O_DIRECT) { |
1447 | written = generic_file_direct_write(iocb, iov, &nr_segs, | |
1448 | pos, &iocb->ki_pos, | |
1449 | count, ocount); | |
1450 | if (written < 0 || written == count) | |
1451 | goto out; | |
1452 | ||
1453 | pos += written; | |
1454 | count -= written; | |
ea9b9907 | 1455 | |
4273b793 AA |
1456 | iov_iter_init(&i, iov, nr_segs, count, written); |
1457 | written_buffered = fuse_perform_write(file, mapping, &i, pos); | |
1458 | if (written_buffered < 0) { | |
1459 | err = written_buffered; | |
1460 | goto out; | |
1461 | } | |
1462 | endbyte = pos + written_buffered - 1; | |
1463 | ||
1464 | err = filemap_write_and_wait_range(file->f_mapping, pos, | |
1465 | endbyte); | |
1466 | if (err) | |
1467 | goto out; | |
1468 | ||
1469 | invalidate_mapping_pages(file->f_mapping, | |
1470 | pos >> PAGE_CACHE_SHIFT, | |
1471 | endbyte >> PAGE_CACHE_SHIFT); | |
1472 | ||
1473 | written += written_buffered; | |
1474 | iocb->ki_pos = pos + written_buffered; | |
1475 | } else { | |
1476 | iov_iter_init(&i, iov, nr_segs, count, 0); | |
1477 | written = fuse_perform_write(file, mapping, &i, pos); | |
1478 | if (written >= 0) | |
1479 | iocb->ki_pos = pos + written; | |
1480 | } | |
ea9b9907 NP |
1481 | out: |
1482 | current->backing_dev_info = NULL; | |
1483 | mutex_unlock(&inode->i_mutex); | |
1484 | ||
1485 | return written ? written : err; | |
1486 | } | |
1487 | ||
7c190c8b MP |
1488 | static inline void fuse_page_descs_length_init(struct fuse_req *req, |
1489 | unsigned index, unsigned nr_pages) | |
85f40aec MP |
1490 | { |
1491 | int i; | |
1492 | ||
7c190c8b | 1493 | for (i = index; i < index + nr_pages; i++) |
85f40aec MP |
1494 | req->page_descs[i].length = PAGE_SIZE - |
1495 | req->page_descs[i].offset; | |
1496 | } | |
1497 | ||
7c190c8b MP |
1498 | static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii) |
1499 | { | |
1500 | return (unsigned long)ii->iov->iov_base + ii->iov_offset; | |
1501 | } | |
1502 | ||
1503 | static inline size_t fuse_get_frag_size(const struct iov_iter *ii, | |
1504 | size_t max_size) | |
1505 | { | |
1506 | return min(iov_iter_single_seg_count(ii), max_size); | |
1507 | } | |
1508 | ||
b98d023a | 1509 | static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii, |
ce60a2f1 | 1510 | size_t *nbytesp, int write) |
413ef8cb | 1511 | { |
7c190c8b | 1512 | size_t nbytes = 0; /* # bytes already packed in req */ |
b98d023a | 1513 | |
f4975c67 MS |
1514 | /* Special case for kernel I/O: can copy directly into the buffer */ |
1515 | if (segment_eq(get_fs(), KERNEL_DS)) { | |
7c190c8b MP |
1516 | unsigned long user_addr = fuse_get_user_addr(ii); |
1517 | size_t frag_size = fuse_get_frag_size(ii, *nbytesp); | |
1518 | ||
f4975c67 MS |
1519 | if (write) |
1520 | req->in.args[1].value = (void *) user_addr; | |
1521 | else | |
1522 | req->out.args[0].value = (void *) user_addr; | |
1523 | ||
b98d023a MP |
1524 | iov_iter_advance(ii, frag_size); |
1525 | *nbytesp = frag_size; | |
f4975c67 MS |
1526 | return 0; |
1527 | } | |
413ef8cb | 1528 | |
5565a9d8 | 1529 | while (nbytes < *nbytesp && req->num_pages < req->max_pages) { |
7c190c8b MP |
1530 | unsigned npages; |
1531 | unsigned long user_addr = fuse_get_user_addr(ii); | |
1532 | unsigned offset = user_addr & ~PAGE_MASK; | |
1533 | size_t frag_size = fuse_get_frag_size(ii, *nbytesp - nbytes); | |
1534 | int ret; | |
413ef8cb | 1535 | |
5565a9d8 | 1536 | unsigned n = req->max_pages - req->num_pages; |
7c190c8b MP |
1537 | frag_size = min_t(size_t, frag_size, n << PAGE_SHIFT); |
1538 | ||
1539 | npages = (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
1540 | npages = clamp(npages, 1U, n); | |
1541 | ||
1542 | ret = get_user_pages_fast(user_addr, npages, !write, | |
1543 | &req->pages[req->num_pages]); | |
1544 | if (ret < 0) | |
1545 | return ret; | |
1546 | ||
1547 | npages = ret; | |
1548 | frag_size = min_t(size_t, frag_size, | |
1549 | (npages << PAGE_SHIFT) - offset); | |
1550 | iov_iter_advance(ii, frag_size); | |
1551 | ||
1552 | req->page_descs[req->num_pages].offset = offset; | |
1553 | fuse_page_descs_length_init(req, req->num_pages, npages); | |
1554 | ||
1555 | req->num_pages += npages; | |
1556 | req->page_descs[req->num_pages - 1].length -= | |
1557 | (npages << PAGE_SHIFT) - offset - frag_size; | |
1558 | ||
1559 | nbytes += frag_size; | |
1560 | } | |
f4975c67 MS |
1561 | |
1562 | if (write) | |
1563 | req->in.argpages = 1; | |
1564 | else | |
1565 | req->out.argpages = 1; | |
1566 | ||
7c190c8b | 1567 | *nbytesp = nbytes; |
f4975c67 | 1568 | |
413ef8cb MS |
1569 | return 0; |
1570 | } | |
1571 | ||
5565a9d8 MP |
1572 | static inline int fuse_iter_npages(const struct iov_iter *ii_p) |
1573 | { | |
1574 | struct iov_iter ii = *ii_p; | |
1575 | int npages = 0; | |
1576 | ||
1577 | while (iov_iter_count(&ii) && npages < FUSE_MAX_PAGES_PER_REQ) { | |
1578 | unsigned long user_addr = fuse_get_user_addr(&ii); | |
1579 | unsigned offset = user_addr & ~PAGE_MASK; | |
1580 | size_t frag_size = iov_iter_single_seg_count(&ii); | |
1581 | ||
1582 | npages += (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
1583 | iov_iter_advance(&ii, frag_size); | |
1584 | } | |
1585 | ||
1586 | return min(npages, FUSE_MAX_PAGES_PER_REQ); | |
1587 | } | |
1588 | ||
36cf66ed | 1589 | ssize_t fuse_direct_io(struct fuse_io_priv *io, const struct iovec *iov, |
fb05f41f MS |
1590 | unsigned long nr_segs, size_t count, loff_t *ppos, |
1591 | int write) | |
413ef8cb | 1592 | { |
36cf66ed | 1593 | struct file *file = io->file; |
2106cb18 MS |
1594 | struct fuse_file *ff = file->private_data; |
1595 | struct fuse_conn *fc = ff->fc; | |
413ef8cb MS |
1596 | size_t nmax = write ? fc->max_write : fc->max_read; |
1597 | loff_t pos = *ppos; | |
1598 | ssize_t res = 0; | |
248d86e8 | 1599 | struct fuse_req *req; |
b98d023a MP |
1600 | struct iov_iter ii; |
1601 | ||
1602 | iov_iter_init(&ii, iov, nr_segs, count, 0); | |
248d86e8 | 1603 | |
de82b923 BF |
1604 | if (io->async) |
1605 | req = fuse_get_req_for_background(fc, fuse_iter_npages(&ii)); | |
1606 | else | |
1607 | req = fuse_get_req(fc, fuse_iter_npages(&ii)); | |
ce1d5a49 MS |
1608 | if (IS_ERR(req)) |
1609 | return PTR_ERR(req); | |
413ef8cb MS |
1610 | |
1611 | while (count) { | |
413ef8cb | 1612 | size_t nres; |
2106cb18 | 1613 | fl_owner_t owner = current->files; |
f4975c67 | 1614 | size_t nbytes = min(count, nmax); |
b98d023a | 1615 | int err = fuse_get_user_pages(req, &ii, &nbytes, write); |
413ef8cb MS |
1616 | if (err) { |
1617 | res = err; | |
1618 | break; | |
1619 | } | |
f4975c67 | 1620 | |
413ef8cb | 1621 | if (write) |
36cf66ed | 1622 | nres = fuse_send_write(req, io, pos, nbytes, owner); |
413ef8cb | 1623 | else |
36cf66ed | 1624 | nres = fuse_send_read(req, io, pos, nbytes, owner); |
2106cb18 | 1625 | |
36cf66ed MP |
1626 | if (!io->async) |
1627 | fuse_release_user_pages(req, !write); | |
413ef8cb MS |
1628 | if (req->out.h.error) { |
1629 | if (!res) | |
1630 | res = req->out.h.error; | |
1631 | break; | |
1632 | } else if (nres > nbytes) { | |
1633 | res = -EIO; | |
1634 | break; | |
1635 | } | |
1636 | count -= nres; | |
1637 | res += nres; | |
1638 | pos += nres; | |
413ef8cb MS |
1639 | if (nres != nbytes) |
1640 | break; | |
56cf34ff MS |
1641 | if (count) { |
1642 | fuse_put_request(fc, req); | |
de82b923 BF |
1643 | if (io->async) |
1644 | req = fuse_get_req_for_background(fc, | |
1645 | fuse_iter_npages(&ii)); | |
1646 | else | |
1647 | req = fuse_get_req(fc, fuse_iter_npages(&ii)); | |
56cf34ff MS |
1648 | if (IS_ERR(req)) |
1649 | break; | |
1650 | } | |
413ef8cb | 1651 | } |
f60311d5 AA |
1652 | if (!IS_ERR(req)) |
1653 | fuse_put_request(fc, req); | |
d09cb9d7 | 1654 | if (res > 0) |
413ef8cb | 1655 | *ppos = pos; |
413ef8cb MS |
1656 | |
1657 | return res; | |
1658 | } | |
08cbf542 | 1659 | EXPORT_SYMBOL_GPL(fuse_direct_io); |
413ef8cb | 1660 | |
36cf66ed MP |
1661 | static ssize_t __fuse_direct_read(struct fuse_io_priv *io, |
1662 | const struct iovec *iov, | |
439ee5f0 MP |
1663 | unsigned long nr_segs, loff_t *ppos, |
1664 | size_t count) | |
413ef8cb | 1665 | { |
d09cb9d7 | 1666 | ssize_t res; |
36cf66ed | 1667 | struct file *file = io->file; |
6131ffaa | 1668 | struct inode *inode = file_inode(file); |
d09cb9d7 MS |
1669 | |
1670 | if (is_bad_inode(inode)) | |
1671 | return -EIO; | |
1672 | ||
439ee5f0 | 1673 | res = fuse_direct_io(io, iov, nr_segs, count, ppos, 0); |
d09cb9d7 MS |
1674 | |
1675 | fuse_invalidate_attr(inode); | |
1676 | ||
1677 | return res; | |
413ef8cb MS |
1678 | } |
1679 | ||
b98d023a MP |
1680 | static ssize_t fuse_direct_read(struct file *file, char __user *buf, |
1681 | size_t count, loff_t *ppos) | |
1682 | { | |
36cf66ed | 1683 | struct fuse_io_priv io = { .async = 0, .file = file }; |
fb05f41f | 1684 | struct iovec iov = { .iov_base = buf, .iov_len = count }; |
439ee5f0 | 1685 | return __fuse_direct_read(&io, &iov, 1, ppos, count); |
b98d023a MP |
1686 | } |
1687 | ||
36cf66ed MP |
1688 | static ssize_t __fuse_direct_write(struct fuse_io_priv *io, |
1689 | const struct iovec *iov, | |
b98d023a | 1690 | unsigned long nr_segs, loff_t *ppos) |
413ef8cb | 1691 | { |
36cf66ed | 1692 | struct file *file = io->file; |
6131ffaa | 1693 | struct inode *inode = file_inode(file); |
b98d023a | 1694 | size_t count = iov_length(iov, nr_segs); |
413ef8cb | 1695 | ssize_t res; |
d09cb9d7 | 1696 | |
889f7848 | 1697 | res = generic_write_checks(file, ppos, &count, 0); |
bcba24cc | 1698 | if (!res) |
36cf66ed | 1699 | res = fuse_direct_io(io, iov, nr_segs, count, ppos, 1); |
d09cb9d7 MS |
1700 | |
1701 | fuse_invalidate_attr(inode); | |
1702 | ||
413ef8cb MS |
1703 | return res; |
1704 | } | |
1705 | ||
4273b793 AA |
1706 | static ssize_t fuse_direct_write(struct file *file, const char __user *buf, |
1707 | size_t count, loff_t *ppos) | |
1708 | { | |
fb05f41f | 1709 | struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count }; |
6131ffaa | 1710 | struct inode *inode = file_inode(file); |
4273b793 | 1711 | ssize_t res; |
36cf66ed | 1712 | struct fuse_io_priv io = { .async = 0, .file = file }; |
4273b793 AA |
1713 | |
1714 | if (is_bad_inode(inode)) | |
1715 | return -EIO; | |
1716 | ||
1717 | /* Don't allow parallel writes to the same file */ | |
1718 | mutex_lock(&inode->i_mutex); | |
36cf66ed | 1719 | res = __fuse_direct_write(&io, &iov, 1, ppos); |
bcba24cc MP |
1720 | if (res > 0) |
1721 | fuse_write_update_size(inode, *ppos); | |
4273b793 AA |
1722 | mutex_unlock(&inode->i_mutex); |
1723 | ||
1724 | return res; | |
1725 | } | |
1726 | ||
3be5a52b | 1727 | static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req) |
b6aeaded | 1728 | { |
3be5a52b | 1729 | __free_page(req->pages[0]); |
5a18ec17 | 1730 | fuse_file_put(req->ff, false); |
3be5a52b MS |
1731 | } |
1732 | ||
1733 | static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req) | |
1734 | { | |
1735 | struct inode *inode = req->inode; | |
1736 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1737 | struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info; | |
1738 | ||
1739 | list_del(&req->writepages_entry); | |
1740 | dec_bdi_stat(bdi, BDI_WRITEBACK); | |
1741 | dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP); | |
1742 | bdi_writeout_inc(bdi); | |
1743 | wake_up(&fi->page_waitq); | |
1744 | } | |
1745 | ||
1746 | /* Called under fc->lock, may release and reacquire it */ | |
1747 | static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req) | |
b9ca67b2 MS |
1748 | __releases(fc->lock) |
1749 | __acquires(fc->lock) | |
3be5a52b MS |
1750 | { |
1751 | struct fuse_inode *fi = get_fuse_inode(req->inode); | |
1752 | loff_t size = i_size_read(req->inode); | |
1753 | struct fuse_write_in *inarg = &req->misc.write.in; | |
1754 | ||
1755 | if (!fc->connected) | |
1756 | goto out_free; | |
1757 | ||
1758 | if (inarg->offset + PAGE_CACHE_SIZE <= size) { | |
1759 | inarg->size = PAGE_CACHE_SIZE; | |
1760 | } else if (inarg->offset < size) { | |
1761 | inarg->size = size & (PAGE_CACHE_SIZE - 1); | |
1762 | } else { | |
1763 | /* Got truncated off completely */ | |
1764 | goto out_free; | |
b6aeaded | 1765 | } |
3be5a52b MS |
1766 | |
1767 | req->in.args[1].size = inarg->size; | |
1768 | fi->writectr++; | |
b93f858a | 1769 | fuse_request_send_background_locked(fc, req); |
3be5a52b MS |
1770 | return; |
1771 | ||
1772 | out_free: | |
1773 | fuse_writepage_finish(fc, req); | |
1774 | spin_unlock(&fc->lock); | |
1775 | fuse_writepage_free(fc, req); | |
e9bb09dd | 1776 | fuse_put_request(fc, req); |
3be5a52b | 1777 | spin_lock(&fc->lock); |
b6aeaded MS |
1778 | } |
1779 | ||
3be5a52b MS |
1780 | /* |
1781 | * If fi->writectr is positive (no truncate or fsync going on) send | |
1782 | * all queued writepage requests. | |
1783 | * | |
1784 | * Called with fc->lock | |
1785 | */ | |
1786 | void fuse_flush_writepages(struct inode *inode) | |
b9ca67b2 MS |
1787 | __releases(fc->lock) |
1788 | __acquires(fc->lock) | |
b6aeaded | 1789 | { |
3be5a52b MS |
1790 | struct fuse_conn *fc = get_fuse_conn(inode); |
1791 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1792 | struct fuse_req *req; | |
1793 | ||
1794 | while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) { | |
1795 | req = list_entry(fi->queued_writes.next, struct fuse_req, list); | |
1796 | list_del_init(&req->list); | |
1797 | fuse_send_writepage(fc, req); | |
1798 | } | |
1799 | } | |
1800 | ||
1801 | static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req) | |
1802 | { | |
1803 | struct inode *inode = req->inode; | |
1804 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1805 | ||
1806 | mapping_set_error(inode->i_mapping, req->out.h.error); | |
1807 | spin_lock(&fc->lock); | |
1808 | fi->writectr--; | |
1809 | fuse_writepage_finish(fc, req); | |
1810 | spin_unlock(&fc->lock); | |
1811 | fuse_writepage_free(fc, req); | |
1812 | } | |
1813 | ||
1814 | static int fuse_writepage_locked(struct page *page) | |
1815 | { | |
1816 | struct address_space *mapping = page->mapping; | |
1817 | struct inode *inode = mapping->host; | |
1818 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1819 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1820 | struct fuse_req *req; | |
1821 | struct fuse_file *ff; | |
1822 | struct page *tmp_page; | |
1823 | ||
1824 | set_page_writeback(page); | |
1825 | ||
4250c066 | 1826 | req = fuse_request_alloc_nofs(1); |
3be5a52b MS |
1827 | if (!req) |
1828 | goto err; | |
1829 | ||
8b41e671 | 1830 | req->background = 1; /* writeback always goes to bg_queue */ |
3be5a52b MS |
1831 | tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); |
1832 | if (!tmp_page) | |
1833 | goto err_free; | |
1834 | ||
1835 | spin_lock(&fc->lock); | |
1836 | BUG_ON(list_empty(&fi->write_files)); | |
1837 | ff = list_entry(fi->write_files.next, struct fuse_file, write_entry); | |
1838 | req->ff = fuse_file_get(ff); | |
1839 | spin_unlock(&fc->lock); | |
1840 | ||
2106cb18 | 1841 | fuse_write_fill(req, ff, page_offset(page), 0); |
3be5a52b MS |
1842 | |
1843 | copy_highpage(tmp_page, page); | |
2d698b07 | 1844 | req->misc.write.in.write_flags |= FUSE_WRITE_CACHE; |
f4975c67 | 1845 | req->in.argpages = 1; |
3be5a52b MS |
1846 | req->num_pages = 1; |
1847 | req->pages[0] = tmp_page; | |
b2430d75 | 1848 | req->page_descs[0].offset = 0; |
85f40aec | 1849 | req->page_descs[0].length = PAGE_SIZE; |
3be5a52b MS |
1850 | req->end = fuse_writepage_end; |
1851 | req->inode = inode; | |
1852 | ||
1853 | inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK); | |
1854 | inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP); | |
3be5a52b MS |
1855 | |
1856 | spin_lock(&fc->lock); | |
1857 | list_add(&req->writepages_entry, &fi->writepages); | |
1858 | list_add_tail(&req->list, &fi->queued_writes); | |
1859 | fuse_flush_writepages(inode); | |
1860 | spin_unlock(&fc->lock); | |
1861 | ||
3002e63b MP |
1862 | end_page_writeback(page); |
1863 | ||
3be5a52b MS |
1864 | return 0; |
1865 | ||
1866 | err_free: | |
1867 | fuse_request_free(req); | |
1868 | err: | |
1869 | end_page_writeback(page); | |
1870 | return -ENOMEM; | |
1871 | } | |
1872 | ||
1873 | static int fuse_writepage(struct page *page, struct writeback_control *wbc) | |
1874 | { | |
1875 | int err; | |
1876 | ||
1877 | err = fuse_writepage_locked(page); | |
1878 | unlock_page(page); | |
1879 | ||
1880 | return err; | |
1881 | } | |
1882 | ||
1883 | static int fuse_launder_page(struct page *page) | |
1884 | { | |
1885 | int err = 0; | |
1886 | if (clear_page_dirty_for_io(page)) { | |
1887 | struct inode *inode = page->mapping->host; | |
1888 | err = fuse_writepage_locked(page); | |
1889 | if (!err) | |
1890 | fuse_wait_on_page_writeback(inode, page->index); | |
1891 | } | |
1892 | return err; | |
1893 | } | |
1894 | ||
1895 | /* | |
1896 | * Write back dirty pages now, because there may not be any suitable | |
1897 | * open files later | |
1898 | */ | |
1899 | static void fuse_vma_close(struct vm_area_struct *vma) | |
1900 | { | |
1901 | filemap_write_and_wait(vma->vm_file->f_mapping); | |
1902 | } | |
1903 | ||
1904 | /* | |
1905 | * Wait for writeback against this page to complete before allowing it | |
1906 | * to be marked dirty again, and hence written back again, possibly | |
1907 | * before the previous writepage completed. | |
1908 | * | |
1909 | * Block here, instead of in ->writepage(), so that the userspace fs | |
1910 | * can only block processes actually operating on the filesystem. | |
1911 | * | |
1912 | * Otherwise unprivileged userspace fs would be able to block | |
1913 | * unrelated: | |
1914 | * | |
1915 | * - page migration | |
1916 | * - sync(2) | |
1917 | * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER | |
1918 | */ | |
c2ec175c | 1919 | static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
3be5a52b | 1920 | { |
c2ec175c | 1921 | struct page *page = vmf->page; |
3be5a52b MS |
1922 | /* |
1923 | * Don't use page->mapping as it may become NULL from a | |
1924 | * concurrent truncate. | |
1925 | */ | |
1926 | struct inode *inode = vma->vm_file->f_mapping->host; | |
1927 | ||
1928 | fuse_wait_on_page_writeback(inode, page->index); | |
1929 | return 0; | |
1930 | } | |
1931 | ||
f0f37e2f | 1932 | static const struct vm_operations_struct fuse_file_vm_ops = { |
3be5a52b MS |
1933 | .close = fuse_vma_close, |
1934 | .fault = filemap_fault, | |
1935 | .page_mkwrite = fuse_page_mkwrite, | |
0b173bc4 | 1936 | .remap_pages = generic_file_remap_pages, |
3be5a52b MS |
1937 | }; |
1938 | ||
1939 | static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) | |
1940 | { | |
1941 | if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) { | |
6131ffaa | 1942 | struct inode *inode = file_inode(file); |
3be5a52b MS |
1943 | struct fuse_conn *fc = get_fuse_conn(inode); |
1944 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1945 | struct fuse_file *ff = file->private_data; | |
1946 | /* | |
1947 | * file may be written through mmap, so chain it onto the | |
1948 | * inodes's write_file list | |
1949 | */ | |
1950 | spin_lock(&fc->lock); | |
1951 | if (list_empty(&ff->write_entry)) | |
1952 | list_add(&ff->write_entry, &fi->write_files); | |
1953 | spin_unlock(&fc->lock); | |
1954 | } | |
1955 | file_accessed(file); | |
1956 | vma->vm_ops = &fuse_file_vm_ops; | |
b6aeaded MS |
1957 | return 0; |
1958 | } | |
1959 | ||
fc280c96 MS |
1960 | static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma) |
1961 | { | |
1962 | /* Can't provide the coherency needed for MAP_SHARED */ | |
1963 | if (vma->vm_flags & VM_MAYSHARE) | |
1964 | return -ENODEV; | |
1965 | ||
3121bfe7 MS |
1966 | invalidate_inode_pages2(file->f_mapping); |
1967 | ||
fc280c96 MS |
1968 | return generic_file_mmap(file, vma); |
1969 | } | |
1970 | ||
71421259 MS |
1971 | static int convert_fuse_file_lock(const struct fuse_file_lock *ffl, |
1972 | struct file_lock *fl) | |
1973 | { | |
1974 | switch (ffl->type) { | |
1975 | case F_UNLCK: | |
1976 | break; | |
1977 | ||
1978 | case F_RDLCK: | |
1979 | case F_WRLCK: | |
1980 | if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX || | |
1981 | ffl->end < ffl->start) | |
1982 | return -EIO; | |
1983 | ||
1984 | fl->fl_start = ffl->start; | |
1985 | fl->fl_end = ffl->end; | |
1986 | fl->fl_pid = ffl->pid; | |
1987 | break; | |
1988 | ||
1989 | default: | |
1990 | return -EIO; | |
1991 | } | |
1992 | fl->fl_type = ffl->type; | |
1993 | return 0; | |
1994 | } | |
1995 | ||
1996 | static void fuse_lk_fill(struct fuse_req *req, struct file *file, | |
a9ff4f87 MS |
1997 | const struct file_lock *fl, int opcode, pid_t pid, |
1998 | int flock) | |
71421259 | 1999 | { |
6131ffaa | 2000 | struct inode *inode = file_inode(file); |
9c8ef561 | 2001 | struct fuse_conn *fc = get_fuse_conn(inode); |
71421259 MS |
2002 | struct fuse_file *ff = file->private_data; |
2003 | struct fuse_lk_in *arg = &req->misc.lk_in; | |
2004 | ||
2005 | arg->fh = ff->fh; | |
9c8ef561 | 2006 | arg->owner = fuse_lock_owner_id(fc, fl->fl_owner); |
71421259 MS |
2007 | arg->lk.start = fl->fl_start; |
2008 | arg->lk.end = fl->fl_end; | |
2009 | arg->lk.type = fl->fl_type; | |
2010 | arg->lk.pid = pid; | |
a9ff4f87 MS |
2011 | if (flock) |
2012 | arg->lk_flags |= FUSE_LK_FLOCK; | |
71421259 MS |
2013 | req->in.h.opcode = opcode; |
2014 | req->in.h.nodeid = get_node_id(inode); | |
2015 | req->in.numargs = 1; | |
2016 | req->in.args[0].size = sizeof(*arg); | |
2017 | req->in.args[0].value = arg; | |
2018 | } | |
2019 | ||
2020 | static int fuse_getlk(struct file *file, struct file_lock *fl) | |
2021 | { | |
6131ffaa | 2022 | struct inode *inode = file_inode(file); |
71421259 MS |
2023 | struct fuse_conn *fc = get_fuse_conn(inode); |
2024 | struct fuse_req *req; | |
2025 | struct fuse_lk_out outarg; | |
2026 | int err; | |
2027 | ||
b111c8c0 | 2028 | req = fuse_get_req_nopages(fc); |
71421259 MS |
2029 | if (IS_ERR(req)) |
2030 | return PTR_ERR(req); | |
2031 | ||
a9ff4f87 | 2032 | fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0); |
71421259 MS |
2033 | req->out.numargs = 1; |
2034 | req->out.args[0].size = sizeof(outarg); | |
2035 | req->out.args[0].value = &outarg; | |
b93f858a | 2036 | fuse_request_send(fc, req); |
71421259 MS |
2037 | err = req->out.h.error; |
2038 | fuse_put_request(fc, req); | |
2039 | if (!err) | |
2040 | err = convert_fuse_file_lock(&outarg.lk, fl); | |
2041 | ||
2042 | return err; | |
2043 | } | |
2044 | ||
a9ff4f87 | 2045 | static int fuse_setlk(struct file *file, struct file_lock *fl, int flock) |
71421259 | 2046 | { |
6131ffaa | 2047 | struct inode *inode = file_inode(file); |
71421259 MS |
2048 | struct fuse_conn *fc = get_fuse_conn(inode); |
2049 | struct fuse_req *req; | |
2050 | int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK; | |
2051 | pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0; | |
2052 | int err; | |
2053 | ||
8fb47a4f | 2054 | if (fl->fl_lmops && fl->fl_lmops->lm_grant) { |
48e90761 MS |
2055 | /* NLM needs asynchronous locks, which we don't support yet */ |
2056 | return -ENOLCK; | |
2057 | } | |
2058 | ||
71421259 MS |
2059 | /* Unlock on close is handled by the flush method */ |
2060 | if (fl->fl_flags & FL_CLOSE) | |
2061 | return 0; | |
2062 | ||
b111c8c0 | 2063 | req = fuse_get_req_nopages(fc); |
71421259 MS |
2064 | if (IS_ERR(req)) |
2065 | return PTR_ERR(req); | |
2066 | ||
a9ff4f87 | 2067 | fuse_lk_fill(req, file, fl, opcode, pid, flock); |
b93f858a | 2068 | fuse_request_send(fc, req); |
71421259 | 2069 | err = req->out.h.error; |
a4d27e75 MS |
2070 | /* locking is restartable */ |
2071 | if (err == -EINTR) | |
2072 | err = -ERESTARTSYS; | |
71421259 MS |
2073 | fuse_put_request(fc, req); |
2074 | return err; | |
2075 | } | |
2076 | ||
2077 | static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl) | |
2078 | { | |
6131ffaa | 2079 | struct inode *inode = file_inode(file); |
71421259 MS |
2080 | struct fuse_conn *fc = get_fuse_conn(inode); |
2081 | int err; | |
2082 | ||
48e90761 MS |
2083 | if (cmd == F_CANCELLK) { |
2084 | err = 0; | |
2085 | } else if (cmd == F_GETLK) { | |
71421259 | 2086 | if (fc->no_lock) { |
9d6a8c5c | 2087 | posix_test_lock(file, fl); |
71421259 MS |
2088 | err = 0; |
2089 | } else | |
2090 | err = fuse_getlk(file, fl); | |
2091 | } else { | |
2092 | if (fc->no_lock) | |
48e90761 | 2093 | err = posix_lock_file(file, fl, NULL); |
71421259 | 2094 | else |
a9ff4f87 | 2095 | err = fuse_setlk(file, fl, 0); |
71421259 MS |
2096 | } |
2097 | return err; | |
2098 | } | |
2099 | ||
a9ff4f87 MS |
2100 | static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl) |
2101 | { | |
6131ffaa | 2102 | struct inode *inode = file_inode(file); |
a9ff4f87 MS |
2103 | struct fuse_conn *fc = get_fuse_conn(inode); |
2104 | int err; | |
2105 | ||
37fb3a30 | 2106 | if (fc->no_flock) { |
a9ff4f87 MS |
2107 | err = flock_lock_file_wait(file, fl); |
2108 | } else { | |
37fb3a30 MS |
2109 | struct fuse_file *ff = file->private_data; |
2110 | ||
a9ff4f87 MS |
2111 | /* emulate flock with POSIX locks */ |
2112 | fl->fl_owner = (fl_owner_t) file; | |
37fb3a30 | 2113 | ff->flock = true; |
a9ff4f87 MS |
2114 | err = fuse_setlk(file, fl, 1); |
2115 | } | |
2116 | ||
2117 | return err; | |
2118 | } | |
2119 | ||
b2d2272f MS |
2120 | static sector_t fuse_bmap(struct address_space *mapping, sector_t block) |
2121 | { | |
2122 | struct inode *inode = mapping->host; | |
2123 | struct fuse_conn *fc = get_fuse_conn(inode); | |
2124 | struct fuse_req *req; | |
2125 | struct fuse_bmap_in inarg; | |
2126 | struct fuse_bmap_out outarg; | |
2127 | int err; | |
2128 | ||
2129 | if (!inode->i_sb->s_bdev || fc->no_bmap) | |
2130 | return 0; | |
2131 | ||
b111c8c0 | 2132 | req = fuse_get_req_nopages(fc); |
b2d2272f MS |
2133 | if (IS_ERR(req)) |
2134 | return 0; | |
2135 | ||
2136 | memset(&inarg, 0, sizeof(inarg)); | |
2137 | inarg.block = block; | |
2138 | inarg.blocksize = inode->i_sb->s_blocksize; | |
2139 | req->in.h.opcode = FUSE_BMAP; | |
2140 | req->in.h.nodeid = get_node_id(inode); | |
2141 | req->in.numargs = 1; | |
2142 | req->in.args[0].size = sizeof(inarg); | |
2143 | req->in.args[0].value = &inarg; | |
2144 | req->out.numargs = 1; | |
2145 | req->out.args[0].size = sizeof(outarg); | |
2146 | req->out.args[0].value = &outarg; | |
b93f858a | 2147 | fuse_request_send(fc, req); |
b2d2272f MS |
2148 | err = req->out.h.error; |
2149 | fuse_put_request(fc, req); | |
2150 | if (err == -ENOSYS) | |
2151 | fc->no_bmap = 1; | |
2152 | ||
2153 | return err ? 0 : outarg.block; | |
2154 | } | |
2155 | ||
965c8e59 | 2156 | static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence) |
5559b8f4 MS |
2157 | { |
2158 | loff_t retval; | |
6131ffaa | 2159 | struct inode *inode = file_inode(file); |
5559b8f4 | 2160 | |
c07c3d19 | 2161 | /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */ |
965c8e59 AM |
2162 | if (whence == SEEK_CUR || whence == SEEK_SET) |
2163 | return generic_file_llseek(file, offset, whence); | |
06222e49 | 2164 | |
c07c3d19 MS |
2165 | mutex_lock(&inode->i_mutex); |
2166 | retval = fuse_update_attributes(inode, NULL, file, NULL); | |
2167 | if (!retval) | |
965c8e59 | 2168 | retval = generic_file_llseek(file, offset, whence); |
5559b8f4 | 2169 | mutex_unlock(&inode->i_mutex); |
c07c3d19 | 2170 | |
5559b8f4 MS |
2171 | return retval; |
2172 | } | |
2173 | ||
59efec7b TH |
2174 | static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov, |
2175 | unsigned int nr_segs, size_t bytes, bool to_user) | |
2176 | { | |
2177 | struct iov_iter ii; | |
2178 | int page_idx = 0; | |
2179 | ||
2180 | if (!bytes) | |
2181 | return 0; | |
2182 | ||
2183 | iov_iter_init(&ii, iov, nr_segs, bytes, 0); | |
2184 | ||
2185 | while (iov_iter_count(&ii)) { | |
2186 | struct page *page = pages[page_idx++]; | |
2187 | size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii)); | |
4aa0edd2 | 2188 | void *kaddr; |
59efec7b | 2189 | |
4aa0edd2 | 2190 | kaddr = kmap(page); |
59efec7b TH |
2191 | |
2192 | while (todo) { | |
2193 | char __user *uaddr = ii.iov->iov_base + ii.iov_offset; | |
2194 | size_t iov_len = ii.iov->iov_len - ii.iov_offset; | |
2195 | size_t copy = min(todo, iov_len); | |
2196 | size_t left; | |
2197 | ||
2198 | if (!to_user) | |
2199 | left = copy_from_user(kaddr, uaddr, copy); | |
2200 | else | |
2201 | left = copy_to_user(uaddr, kaddr, copy); | |
2202 | ||
2203 | if (unlikely(left)) | |
2204 | return -EFAULT; | |
2205 | ||
2206 | iov_iter_advance(&ii, copy); | |
2207 | todo -= copy; | |
2208 | kaddr += copy; | |
2209 | } | |
2210 | ||
0bd87182 | 2211 | kunmap(page); |
59efec7b TH |
2212 | } |
2213 | ||
2214 | return 0; | |
2215 | } | |
2216 | ||
d9d318d3 MS |
2217 | /* |
2218 | * CUSE servers compiled on 32bit broke on 64bit kernels because the | |
2219 | * ABI was defined to be 'struct iovec' which is different on 32bit | |
2220 | * and 64bit. Fortunately we can determine which structure the server | |
2221 | * used from the size of the reply. | |
2222 | */ | |
1baa26b2 MS |
2223 | static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src, |
2224 | size_t transferred, unsigned count, | |
2225 | bool is_compat) | |
d9d318d3 MS |
2226 | { |
2227 | #ifdef CONFIG_COMPAT | |
2228 | if (count * sizeof(struct compat_iovec) == transferred) { | |
2229 | struct compat_iovec *ciov = src; | |
2230 | unsigned i; | |
2231 | ||
2232 | /* | |
2233 | * With this interface a 32bit server cannot support | |
2234 | * non-compat (i.e. ones coming from 64bit apps) ioctl | |
2235 | * requests | |
2236 | */ | |
2237 | if (!is_compat) | |
2238 | return -EINVAL; | |
2239 | ||
2240 | for (i = 0; i < count; i++) { | |
2241 | dst[i].iov_base = compat_ptr(ciov[i].iov_base); | |
2242 | dst[i].iov_len = ciov[i].iov_len; | |
2243 | } | |
2244 | return 0; | |
2245 | } | |
2246 | #endif | |
2247 | ||
2248 | if (count * sizeof(struct iovec) != transferred) | |
2249 | return -EIO; | |
2250 | ||
2251 | memcpy(dst, src, transferred); | |
2252 | return 0; | |
2253 | } | |
2254 | ||
7572777e MS |
2255 | /* Make sure iov_length() won't overflow */ |
2256 | static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count) | |
2257 | { | |
2258 | size_t n; | |
2259 | u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT; | |
2260 | ||
fb6ccff6 | 2261 | for (n = 0; n < count; n++, iov++) { |
7572777e MS |
2262 | if (iov->iov_len > (size_t) max) |
2263 | return -ENOMEM; | |
2264 | max -= iov->iov_len; | |
2265 | } | |
2266 | return 0; | |
2267 | } | |
2268 | ||
1baa26b2 MS |
2269 | static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst, |
2270 | void *src, size_t transferred, unsigned count, | |
2271 | bool is_compat) | |
2272 | { | |
2273 | unsigned i; | |
2274 | struct fuse_ioctl_iovec *fiov = src; | |
2275 | ||
2276 | if (fc->minor < 16) { | |
2277 | return fuse_copy_ioctl_iovec_old(dst, src, transferred, | |
2278 | count, is_compat); | |
2279 | } | |
2280 | ||
2281 | if (count * sizeof(struct fuse_ioctl_iovec) != transferred) | |
2282 | return -EIO; | |
2283 | ||
2284 | for (i = 0; i < count; i++) { | |
2285 | /* Did the server supply an inappropriate value? */ | |
2286 | if (fiov[i].base != (unsigned long) fiov[i].base || | |
2287 | fiov[i].len != (unsigned long) fiov[i].len) | |
2288 | return -EIO; | |
2289 | ||
2290 | dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base; | |
2291 | dst[i].iov_len = (size_t) fiov[i].len; | |
2292 | ||
2293 | #ifdef CONFIG_COMPAT | |
2294 | if (is_compat && | |
2295 | (ptr_to_compat(dst[i].iov_base) != fiov[i].base || | |
2296 | (compat_size_t) dst[i].iov_len != fiov[i].len)) | |
2297 | return -EIO; | |
2298 | #endif | |
2299 | } | |
2300 | ||
2301 | return 0; | |
2302 | } | |
2303 | ||
2304 | ||
59efec7b TH |
2305 | /* |
2306 | * For ioctls, there is no generic way to determine how much memory | |
2307 | * needs to be read and/or written. Furthermore, ioctls are allowed | |
2308 | * to dereference the passed pointer, so the parameter requires deep | |
2309 | * copying but FUSE has no idea whatsoever about what to copy in or | |
2310 | * out. | |
2311 | * | |
2312 | * This is solved by allowing FUSE server to retry ioctl with | |
2313 | * necessary in/out iovecs. Let's assume the ioctl implementation | |
2314 | * needs to read in the following structure. | |
2315 | * | |
2316 | * struct a { | |
2317 | * char *buf; | |
2318 | * size_t buflen; | |
2319 | * } | |
2320 | * | |
2321 | * On the first callout to FUSE server, inarg->in_size and | |
2322 | * inarg->out_size will be NULL; then, the server completes the ioctl | |
2323 | * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and | |
2324 | * the actual iov array to | |
2325 | * | |
2326 | * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } } | |
2327 | * | |
2328 | * which tells FUSE to copy in the requested area and retry the ioctl. | |
2329 | * On the second round, the server has access to the structure and | |
2330 | * from that it can tell what to look for next, so on the invocation, | |
2331 | * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to | |
2332 | * | |
2333 | * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) }, | |
2334 | * { .iov_base = a.buf, .iov_len = a.buflen } } | |
2335 | * | |
2336 | * FUSE will copy both struct a and the pointed buffer from the | |
2337 | * process doing the ioctl and retry ioctl with both struct a and the | |
2338 | * buffer. | |
2339 | * | |
2340 | * This time, FUSE server has everything it needs and completes ioctl | |
2341 | * without FUSE_IOCTL_RETRY which finishes the ioctl call. | |
2342 | * | |
2343 | * Copying data out works the same way. | |
2344 | * | |
2345 | * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel | |
2346 | * automatically initializes in and out iovs by decoding @cmd with | |
2347 | * _IOC_* macros and the server is not allowed to request RETRY. This | |
2348 | * limits ioctl data transfers to well-formed ioctls and is the forced | |
2349 | * behavior for all FUSE servers. | |
2350 | */ | |
08cbf542 TH |
2351 | long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg, |
2352 | unsigned int flags) | |
59efec7b | 2353 | { |
59efec7b | 2354 | struct fuse_file *ff = file->private_data; |
d36f2487 | 2355 | struct fuse_conn *fc = ff->fc; |
59efec7b TH |
2356 | struct fuse_ioctl_in inarg = { |
2357 | .fh = ff->fh, | |
2358 | .cmd = cmd, | |
2359 | .arg = arg, | |
2360 | .flags = flags | |
2361 | }; | |
2362 | struct fuse_ioctl_out outarg; | |
2363 | struct fuse_req *req = NULL; | |
2364 | struct page **pages = NULL; | |
8ac83505 | 2365 | struct iovec *iov_page = NULL; |
59efec7b TH |
2366 | struct iovec *in_iov = NULL, *out_iov = NULL; |
2367 | unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages; | |
2368 | size_t in_size, out_size, transferred; | |
2369 | int err; | |
2370 | ||
1baa26b2 MS |
2371 | #if BITS_PER_LONG == 32 |
2372 | inarg.flags |= FUSE_IOCTL_32BIT; | |
2373 | #else | |
2374 | if (flags & FUSE_IOCTL_COMPAT) | |
2375 | inarg.flags |= FUSE_IOCTL_32BIT; | |
2376 | #endif | |
2377 | ||
59efec7b | 2378 | /* assume all the iovs returned by client always fits in a page */ |
1baa26b2 | 2379 | BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE); |
59efec7b | 2380 | |
59efec7b | 2381 | err = -ENOMEM; |
c411cc88 | 2382 | pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL); |
8ac83505 | 2383 | iov_page = (struct iovec *) __get_free_page(GFP_KERNEL); |
59efec7b TH |
2384 | if (!pages || !iov_page) |
2385 | goto out; | |
2386 | ||
2387 | /* | |
2388 | * If restricted, initialize IO parameters as encoded in @cmd. | |
2389 | * RETRY from server is not allowed. | |
2390 | */ | |
2391 | if (!(flags & FUSE_IOCTL_UNRESTRICTED)) { | |
8ac83505 | 2392 | struct iovec *iov = iov_page; |
59efec7b | 2393 | |
c9f0523d | 2394 | iov->iov_base = (void __user *)arg; |
59efec7b TH |
2395 | iov->iov_len = _IOC_SIZE(cmd); |
2396 | ||
2397 | if (_IOC_DIR(cmd) & _IOC_WRITE) { | |
2398 | in_iov = iov; | |
2399 | in_iovs = 1; | |
2400 | } | |
2401 | ||
2402 | if (_IOC_DIR(cmd) & _IOC_READ) { | |
2403 | out_iov = iov; | |
2404 | out_iovs = 1; | |
2405 | } | |
2406 | } | |
2407 | ||
2408 | retry: | |
2409 | inarg.in_size = in_size = iov_length(in_iov, in_iovs); | |
2410 | inarg.out_size = out_size = iov_length(out_iov, out_iovs); | |
2411 | ||
2412 | /* | |
2413 | * Out data can be used either for actual out data or iovs, | |
2414 | * make sure there always is at least one page. | |
2415 | */ | |
2416 | out_size = max_t(size_t, out_size, PAGE_SIZE); | |
2417 | max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE); | |
2418 | ||
2419 | /* make sure there are enough buffer pages and init request with them */ | |
2420 | err = -ENOMEM; | |
2421 | if (max_pages > FUSE_MAX_PAGES_PER_REQ) | |
2422 | goto out; | |
2423 | while (num_pages < max_pages) { | |
2424 | pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); | |
2425 | if (!pages[num_pages]) | |
2426 | goto out; | |
2427 | num_pages++; | |
2428 | } | |
2429 | ||
54b96670 | 2430 | req = fuse_get_req(fc, num_pages); |
59efec7b TH |
2431 | if (IS_ERR(req)) { |
2432 | err = PTR_ERR(req); | |
2433 | req = NULL; | |
2434 | goto out; | |
2435 | } | |
2436 | memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages); | |
2437 | req->num_pages = num_pages; | |
7c190c8b | 2438 | fuse_page_descs_length_init(req, 0, req->num_pages); |
59efec7b TH |
2439 | |
2440 | /* okay, let's send it to the client */ | |
2441 | req->in.h.opcode = FUSE_IOCTL; | |
d36f2487 | 2442 | req->in.h.nodeid = ff->nodeid; |
59efec7b TH |
2443 | req->in.numargs = 1; |
2444 | req->in.args[0].size = sizeof(inarg); | |
2445 | req->in.args[0].value = &inarg; | |
2446 | if (in_size) { | |
2447 | req->in.numargs++; | |
2448 | req->in.args[1].size = in_size; | |
2449 | req->in.argpages = 1; | |
2450 | ||
2451 | err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size, | |
2452 | false); | |
2453 | if (err) | |
2454 | goto out; | |
2455 | } | |
2456 | ||
2457 | req->out.numargs = 2; | |
2458 | req->out.args[0].size = sizeof(outarg); | |
2459 | req->out.args[0].value = &outarg; | |
2460 | req->out.args[1].size = out_size; | |
2461 | req->out.argpages = 1; | |
2462 | req->out.argvar = 1; | |
2463 | ||
b93f858a | 2464 | fuse_request_send(fc, req); |
59efec7b TH |
2465 | err = req->out.h.error; |
2466 | transferred = req->out.args[1].size; | |
2467 | fuse_put_request(fc, req); | |
2468 | req = NULL; | |
2469 | if (err) | |
2470 | goto out; | |
2471 | ||
2472 | /* did it ask for retry? */ | |
2473 | if (outarg.flags & FUSE_IOCTL_RETRY) { | |
8ac83505 | 2474 | void *vaddr; |
59efec7b TH |
2475 | |
2476 | /* no retry if in restricted mode */ | |
2477 | err = -EIO; | |
2478 | if (!(flags & FUSE_IOCTL_UNRESTRICTED)) | |
2479 | goto out; | |
2480 | ||
2481 | in_iovs = outarg.in_iovs; | |
2482 | out_iovs = outarg.out_iovs; | |
2483 | ||
2484 | /* | |
2485 | * Make sure things are in boundary, separate checks | |
2486 | * are to protect against overflow. | |
2487 | */ | |
2488 | err = -ENOMEM; | |
2489 | if (in_iovs > FUSE_IOCTL_MAX_IOV || | |
2490 | out_iovs > FUSE_IOCTL_MAX_IOV || | |
2491 | in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV) | |
2492 | goto out; | |
2493 | ||
2408f6ef | 2494 | vaddr = kmap_atomic(pages[0]); |
1baa26b2 | 2495 | err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr, |
d9d318d3 MS |
2496 | transferred, in_iovs + out_iovs, |
2497 | (flags & FUSE_IOCTL_COMPAT) != 0); | |
2408f6ef | 2498 | kunmap_atomic(vaddr); |
d9d318d3 MS |
2499 | if (err) |
2500 | goto out; | |
59efec7b | 2501 | |
8ac83505 | 2502 | in_iov = iov_page; |
59efec7b TH |
2503 | out_iov = in_iov + in_iovs; |
2504 | ||
7572777e MS |
2505 | err = fuse_verify_ioctl_iov(in_iov, in_iovs); |
2506 | if (err) | |
2507 | goto out; | |
2508 | ||
2509 | err = fuse_verify_ioctl_iov(out_iov, out_iovs); | |
2510 | if (err) | |
2511 | goto out; | |
2512 | ||
59efec7b TH |
2513 | goto retry; |
2514 | } | |
2515 | ||
2516 | err = -EIO; | |
2517 | if (transferred > inarg.out_size) | |
2518 | goto out; | |
2519 | ||
2520 | err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true); | |
2521 | out: | |
2522 | if (req) | |
2523 | fuse_put_request(fc, req); | |
8ac83505 | 2524 | free_page((unsigned long) iov_page); |
59efec7b TH |
2525 | while (num_pages) |
2526 | __free_page(pages[--num_pages]); | |
2527 | kfree(pages); | |
2528 | ||
2529 | return err ? err : outarg.result; | |
2530 | } | |
08cbf542 | 2531 | EXPORT_SYMBOL_GPL(fuse_do_ioctl); |
59efec7b | 2532 | |
b18da0c5 MS |
2533 | long fuse_ioctl_common(struct file *file, unsigned int cmd, |
2534 | unsigned long arg, unsigned int flags) | |
d36f2487 | 2535 | { |
6131ffaa | 2536 | struct inode *inode = file_inode(file); |
d36f2487 MS |
2537 | struct fuse_conn *fc = get_fuse_conn(inode); |
2538 | ||
c2132c1b | 2539 | if (!fuse_allow_current_process(fc)) |
d36f2487 MS |
2540 | return -EACCES; |
2541 | ||
2542 | if (is_bad_inode(inode)) | |
2543 | return -EIO; | |
2544 | ||
2545 | return fuse_do_ioctl(file, cmd, arg, flags); | |
2546 | } | |
2547 | ||
59efec7b TH |
2548 | static long fuse_file_ioctl(struct file *file, unsigned int cmd, |
2549 | unsigned long arg) | |
2550 | { | |
b18da0c5 | 2551 | return fuse_ioctl_common(file, cmd, arg, 0); |
59efec7b TH |
2552 | } |
2553 | ||
2554 | static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd, | |
2555 | unsigned long arg) | |
2556 | { | |
b18da0c5 | 2557 | return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT); |
59efec7b TH |
2558 | } |
2559 | ||
95668a69 TH |
2560 | /* |
2561 | * All files which have been polled are linked to RB tree | |
2562 | * fuse_conn->polled_files which is indexed by kh. Walk the tree and | |
2563 | * find the matching one. | |
2564 | */ | |
2565 | static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh, | |
2566 | struct rb_node **parent_out) | |
2567 | { | |
2568 | struct rb_node **link = &fc->polled_files.rb_node; | |
2569 | struct rb_node *last = NULL; | |
2570 | ||
2571 | while (*link) { | |
2572 | struct fuse_file *ff; | |
2573 | ||
2574 | last = *link; | |
2575 | ff = rb_entry(last, struct fuse_file, polled_node); | |
2576 | ||
2577 | if (kh < ff->kh) | |
2578 | link = &last->rb_left; | |
2579 | else if (kh > ff->kh) | |
2580 | link = &last->rb_right; | |
2581 | else | |
2582 | return link; | |
2583 | } | |
2584 | ||
2585 | if (parent_out) | |
2586 | *parent_out = last; | |
2587 | return link; | |
2588 | } | |
2589 | ||
2590 | /* | |
2591 | * The file is about to be polled. Make sure it's on the polled_files | |
2592 | * RB tree. Note that files once added to the polled_files tree are | |
2593 | * not removed before the file is released. This is because a file | |
2594 | * polled once is likely to be polled again. | |
2595 | */ | |
2596 | static void fuse_register_polled_file(struct fuse_conn *fc, | |
2597 | struct fuse_file *ff) | |
2598 | { | |
2599 | spin_lock(&fc->lock); | |
2600 | if (RB_EMPTY_NODE(&ff->polled_node)) { | |
2601 | struct rb_node **link, *parent; | |
2602 | ||
2603 | link = fuse_find_polled_node(fc, ff->kh, &parent); | |
2604 | BUG_ON(*link); | |
2605 | rb_link_node(&ff->polled_node, parent, link); | |
2606 | rb_insert_color(&ff->polled_node, &fc->polled_files); | |
2607 | } | |
2608 | spin_unlock(&fc->lock); | |
2609 | } | |
2610 | ||
08cbf542 | 2611 | unsigned fuse_file_poll(struct file *file, poll_table *wait) |
95668a69 | 2612 | { |
95668a69 | 2613 | struct fuse_file *ff = file->private_data; |
797759aa | 2614 | struct fuse_conn *fc = ff->fc; |
95668a69 TH |
2615 | struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh }; |
2616 | struct fuse_poll_out outarg; | |
2617 | struct fuse_req *req; | |
2618 | int err; | |
2619 | ||
2620 | if (fc->no_poll) | |
2621 | return DEFAULT_POLLMASK; | |
2622 | ||
2623 | poll_wait(file, &ff->poll_wait, wait); | |
0415d291 | 2624 | inarg.events = (__u32)poll_requested_events(wait); |
95668a69 TH |
2625 | |
2626 | /* | |
2627 | * Ask for notification iff there's someone waiting for it. | |
2628 | * The client may ignore the flag and always notify. | |
2629 | */ | |
2630 | if (waitqueue_active(&ff->poll_wait)) { | |
2631 | inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY; | |
2632 | fuse_register_polled_file(fc, ff); | |
2633 | } | |
2634 | ||
b111c8c0 | 2635 | req = fuse_get_req_nopages(fc); |
95668a69 | 2636 | if (IS_ERR(req)) |
201fa69a | 2637 | return POLLERR; |
95668a69 TH |
2638 | |
2639 | req->in.h.opcode = FUSE_POLL; | |
797759aa | 2640 | req->in.h.nodeid = ff->nodeid; |
95668a69 TH |
2641 | req->in.numargs = 1; |
2642 | req->in.args[0].size = sizeof(inarg); | |
2643 | req->in.args[0].value = &inarg; | |
2644 | req->out.numargs = 1; | |
2645 | req->out.args[0].size = sizeof(outarg); | |
2646 | req->out.args[0].value = &outarg; | |
b93f858a | 2647 | fuse_request_send(fc, req); |
95668a69 TH |
2648 | err = req->out.h.error; |
2649 | fuse_put_request(fc, req); | |
2650 | ||
2651 | if (!err) | |
2652 | return outarg.revents; | |
2653 | if (err == -ENOSYS) { | |
2654 | fc->no_poll = 1; | |
2655 | return DEFAULT_POLLMASK; | |
2656 | } | |
2657 | return POLLERR; | |
2658 | } | |
08cbf542 | 2659 | EXPORT_SYMBOL_GPL(fuse_file_poll); |
95668a69 TH |
2660 | |
2661 | /* | |
2662 | * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and | |
2663 | * wakes up the poll waiters. | |
2664 | */ | |
2665 | int fuse_notify_poll_wakeup(struct fuse_conn *fc, | |
2666 | struct fuse_notify_poll_wakeup_out *outarg) | |
2667 | { | |
2668 | u64 kh = outarg->kh; | |
2669 | struct rb_node **link; | |
2670 | ||
2671 | spin_lock(&fc->lock); | |
2672 | ||
2673 | link = fuse_find_polled_node(fc, kh, NULL); | |
2674 | if (*link) { | |
2675 | struct fuse_file *ff; | |
2676 | ||
2677 | ff = rb_entry(*link, struct fuse_file, polled_node); | |
2678 | wake_up_interruptible_sync(&ff->poll_wait); | |
2679 | } | |
2680 | ||
2681 | spin_unlock(&fc->lock); | |
2682 | return 0; | |
2683 | } | |
2684 | ||
efb9fa9e MP |
2685 | static void fuse_do_truncate(struct file *file) |
2686 | { | |
2687 | struct inode *inode = file->f_mapping->host; | |
2688 | struct iattr attr; | |
2689 | ||
2690 | attr.ia_valid = ATTR_SIZE; | |
2691 | attr.ia_size = i_size_read(inode); | |
2692 | ||
2693 | attr.ia_file = file; | |
2694 | attr.ia_valid |= ATTR_FILE; | |
2695 | ||
2696 | fuse_do_setattr(inode, &attr, file); | |
2697 | } | |
2698 | ||
e5c5f05d MP |
2699 | static inline loff_t fuse_round_up(loff_t off) |
2700 | { | |
2701 | return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT); | |
2702 | } | |
2703 | ||
4273b793 AA |
2704 | static ssize_t |
2705 | fuse_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, | |
2706 | loff_t offset, unsigned long nr_segs) | |
2707 | { | |
2708 | ssize_t ret = 0; | |
60b9df7a MS |
2709 | struct file *file = iocb->ki_filp; |
2710 | struct fuse_file *ff = file->private_data; | |
e5c5f05d | 2711 | bool async_dio = ff->fc->async_dio; |
4273b793 | 2712 | loff_t pos = 0; |
bcba24cc MP |
2713 | struct inode *inode; |
2714 | loff_t i_size; | |
2715 | size_t count = iov_length(iov, nr_segs); | |
36cf66ed | 2716 | struct fuse_io_priv *io; |
d3005b09 | 2717 | bool is_sync = is_sync_kiocb(iocb); |
4273b793 | 2718 | |
4273b793 | 2719 | pos = offset; |
bcba24cc MP |
2720 | inode = file->f_mapping->host; |
2721 | i_size = i_size_read(inode); | |
4273b793 | 2722 | |
439ee5f0 | 2723 | /* optimization for short read */ |
e5c5f05d | 2724 | if (async_dio && rw != WRITE && offset + count > i_size) { |
439ee5f0 MP |
2725 | if (offset >= i_size) |
2726 | return 0; | |
e5c5f05d | 2727 | count = min_t(loff_t, count, fuse_round_up(i_size - offset)); |
439ee5f0 MP |
2728 | } |
2729 | ||
bcba24cc | 2730 | io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL); |
36cf66ed MP |
2731 | if (!io) |
2732 | return -ENOMEM; | |
bcba24cc MP |
2733 | spin_lock_init(&io->lock); |
2734 | io->reqs = 1; | |
2735 | io->bytes = -1; | |
2736 | io->size = 0; | |
2737 | io->offset = offset; | |
2738 | io->write = (rw == WRITE); | |
2739 | io->err = 0; | |
36cf66ed | 2740 | io->file = file; |
bcba24cc MP |
2741 | /* |
2742 | * By default, we want to optimize all I/Os with async request | |
60b9df7a | 2743 | * submission to the client filesystem if supported. |
bcba24cc | 2744 | */ |
e5c5f05d | 2745 | io->async = async_dio; |
bcba24cc MP |
2746 | io->iocb = iocb; |
2747 | ||
2748 | /* | |
2749 | * We cannot asynchronously extend the size of a file. We have no method | |
2750 | * to wait on real async I/O requests, so we must submit this request | |
2751 | * synchronously. | |
2752 | */ | |
d3005b09 | 2753 | if (!is_sync && (offset + count > i_size) && rw == WRITE) |
bcba24cc | 2754 | io->async = false; |
4273b793 | 2755 | |
b98d023a | 2756 | if (rw == WRITE) |
36cf66ed | 2757 | ret = __fuse_direct_write(io, iov, nr_segs, &pos); |
b98d023a | 2758 | else |
439ee5f0 | 2759 | ret = __fuse_direct_read(io, iov, nr_segs, &pos, count); |
36cf66ed | 2760 | |
bcba24cc MP |
2761 | if (io->async) { |
2762 | fuse_aio_complete(io, ret < 0 ? ret : 0, -1); | |
2763 | ||
2764 | /* we have a non-extending, async request, so return */ | |
d3005b09 | 2765 | if (!is_sync) |
bcba24cc MP |
2766 | return -EIOCBQUEUED; |
2767 | ||
2768 | ret = wait_on_sync_kiocb(iocb); | |
2769 | } else { | |
2770 | kfree(io); | |
2771 | } | |
2772 | ||
efb9fa9e MP |
2773 | if (rw == WRITE) { |
2774 | if (ret > 0) | |
2775 | fuse_write_update_size(inode, pos); | |
2776 | else if (ret < 0 && offset + count > i_size) | |
2777 | fuse_do_truncate(file); | |
2778 | } | |
4273b793 AA |
2779 | |
2780 | return ret; | |
2781 | } | |
2782 | ||
cdadb11c MS |
2783 | static long fuse_file_fallocate(struct file *file, int mode, loff_t offset, |
2784 | loff_t length) | |
05ba1f08 AP |
2785 | { |
2786 | struct fuse_file *ff = file->private_data; | |
3634a632 | 2787 | struct inode *inode = file->f_inode; |
9abd30b4 | 2788 | struct fuse_inode *fi = get_fuse_inode(inode); |
05ba1f08 AP |
2789 | struct fuse_conn *fc = ff->fc; |
2790 | struct fuse_req *req; | |
2791 | struct fuse_fallocate_in inarg = { | |
2792 | .fh = ff->fh, | |
2793 | .offset = offset, | |
2794 | .length = length, | |
2795 | .mode = mode | |
2796 | }; | |
2797 | int err; | |
14c14414 MP |
2798 | bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) || |
2799 | (mode & FALLOC_FL_PUNCH_HOLE); | |
05ba1f08 | 2800 | |
519c6040 MS |
2801 | if (fc->no_fallocate) |
2802 | return -EOPNOTSUPP; | |
2803 | ||
14c14414 | 2804 | if (lock_inode) { |
3634a632 | 2805 | mutex_lock(&inode->i_mutex); |
eb12ca30 MP |
2806 | if (mode & FALLOC_FL_PUNCH_HOLE) { |
2807 | loff_t endbyte = offset + length - 1; | |
2808 | err = filemap_write_and_wait_range(inode->i_mapping, | |
2809 | offset, endbyte); | |
2810 | if (err) | |
2811 | goto out; | |
2812 | ||
2813 | fuse_sync_writes(inode); | |
2814 | } | |
3634a632 BF |
2815 | } |
2816 | ||
9abd30b4 MP |
2817 | if (!(mode & FALLOC_FL_KEEP_SIZE)) |
2818 | set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); | |
2819 | ||
b111c8c0 | 2820 | req = fuse_get_req_nopages(fc); |
3634a632 BF |
2821 | if (IS_ERR(req)) { |
2822 | err = PTR_ERR(req); | |
2823 | goto out; | |
2824 | } | |
05ba1f08 AP |
2825 | |
2826 | req->in.h.opcode = FUSE_FALLOCATE; | |
2827 | req->in.h.nodeid = ff->nodeid; | |
2828 | req->in.numargs = 1; | |
2829 | req->in.args[0].size = sizeof(inarg); | |
2830 | req->in.args[0].value = &inarg; | |
2831 | fuse_request_send(fc, req); | |
2832 | err = req->out.h.error; | |
519c6040 MS |
2833 | if (err == -ENOSYS) { |
2834 | fc->no_fallocate = 1; | |
2835 | err = -EOPNOTSUPP; | |
2836 | } | |
05ba1f08 AP |
2837 | fuse_put_request(fc, req); |
2838 | ||
bee6c307 BF |
2839 | if (err) |
2840 | goto out; | |
2841 | ||
2842 | /* we could have extended the file */ | |
2843 | if (!(mode & FALLOC_FL_KEEP_SIZE)) | |
2844 | fuse_write_update_size(inode, offset + length); | |
2845 | ||
2846 | if (mode & FALLOC_FL_PUNCH_HOLE) | |
2847 | truncate_pagecache_range(inode, offset, offset + length - 1); | |
2848 | ||
2849 | fuse_invalidate_attr(inode); | |
2850 | ||
3634a632 | 2851 | out: |
9abd30b4 MP |
2852 | if (!(mode & FALLOC_FL_KEEP_SIZE)) |
2853 | clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); | |
2854 | ||
eb12ca30 | 2855 | if (lock_inode) |
3634a632 | 2856 | mutex_unlock(&inode->i_mutex); |
3634a632 | 2857 | |
05ba1f08 AP |
2858 | return err; |
2859 | } | |
05ba1f08 | 2860 | |
4b6f5d20 | 2861 | static const struct file_operations fuse_file_operations = { |
5559b8f4 | 2862 | .llseek = fuse_file_llseek, |
543ade1f | 2863 | .read = do_sync_read, |
bcb4be80 | 2864 | .aio_read = fuse_file_aio_read, |
543ade1f | 2865 | .write = do_sync_write, |
ea9b9907 | 2866 | .aio_write = fuse_file_aio_write, |
b6aeaded MS |
2867 | .mmap = fuse_file_mmap, |
2868 | .open = fuse_open, | |
2869 | .flush = fuse_flush, | |
2870 | .release = fuse_release, | |
2871 | .fsync = fuse_fsync, | |
71421259 | 2872 | .lock = fuse_file_lock, |
a9ff4f87 | 2873 | .flock = fuse_file_flock, |
5ffc4ef4 | 2874 | .splice_read = generic_file_splice_read, |
59efec7b TH |
2875 | .unlocked_ioctl = fuse_file_ioctl, |
2876 | .compat_ioctl = fuse_file_compat_ioctl, | |
95668a69 | 2877 | .poll = fuse_file_poll, |
05ba1f08 | 2878 | .fallocate = fuse_file_fallocate, |
b6aeaded MS |
2879 | }; |
2880 | ||
4b6f5d20 | 2881 | static const struct file_operations fuse_direct_io_file_operations = { |
5559b8f4 | 2882 | .llseek = fuse_file_llseek, |
413ef8cb MS |
2883 | .read = fuse_direct_read, |
2884 | .write = fuse_direct_write, | |
fc280c96 | 2885 | .mmap = fuse_direct_mmap, |
413ef8cb MS |
2886 | .open = fuse_open, |
2887 | .flush = fuse_flush, | |
2888 | .release = fuse_release, | |
2889 | .fsync = fuse_fsync, | |
71421259 | 2890 | .lock = fuse_file_lock, |
a9ff4f87 | 2891 | .flock = fuse_file_flock, |
59efec7b TH |
2892 | .unlocked_ioctl = fuse_file_ioctl, |
2893 | .compat_ioctl = fuse_file_compat_ioctl, | |
95668a69 | 2894 | .poll = fuse_file_poll, |
05ba1f08 | 2895 | .fallocate = fuse_file_fallocate, |
fc280c96 | 2896 | /* no splice_read */ |
413ef8cb MS |
2897 | }; |
2898 | ||
f5e54d6e | 2899 | static const struct address_space_operations fuse_file_aops = { |
b6aeaded | 2900 | .readpage = fuse_readpage, |
3be5a52b MS |
2901 | .writepage = fuse_writepage, |
2902 | .launder_page = fuse_launder_page, | |
db50b96c | 2903 | .readpages = fuse_readpages, |
3be5a52b | 2904 | .set_page_dirty = __set_page_dirty_nobuffers, |
b2d2272f | 2905 | .bmap = fuse_bmap, |
4273b793 | 2906 | .direct_IO = fuse_direct_IO, |
b6aeaded MS |
2907 | }; |
2908 | ||
2909 | void fuse_init_file_inode(struct inode *inode) | |
2910 | { | |
45323fb7 MS |
2911 | inode->i_fop = &fuse_file_operations; |
2912 | inode->i_data.a_ops = &fuse_file_aops; | |
b6aeaded | 2913 | } |