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