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