Nikhilesh Reddy | 759832b | 2017-07-26 09:41:57 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include "fuse_passthrough.h" |
| 16 | |
| 17 | #include <linux/aio.h> |
| 18 | #include <linux/fs_stack.h> |
| 19 | |
| 20 | void fuse_setup_passthrough(struct fuse_conn *fc, struct fuse_req *req) |
| 21 | { |
| 22 | int daemon_fd, fs_stack_depth; |
| 23 | unsigned int open_out_index; |
| 24 | struct file *passthrough_filp; |
| 25 | struct inode *passthrough_inode; |
| 26 | struct super_block *passthrough_sb; |
| 27 | struct fuse_open_out *open_out; |
| 28 | |
| 29 | req->passthrough_filp = NULL; |
| 30 | |
| 31 | if (!(fc->passthrough)) |
| 32 | return; |
| 33 | |
| 34 | if ((req->in.h.opcode != FUSE_OPEN) && |
| 35 | (req->in.h.opcode != FUSE_CREATE)) |
| 36 | return; |
| 37 | |
| 38 | open_out_index = req->in.numargs - 1; |
| 39 | |
| 40 | WARN_ON(open_out_index != 0 && open_out_index != 1); |
| 41 | WARN_ON(req->out.args[open_out_index].size != sizeof(*open_out)); |
| 42 | |
| 43 | open_out = req->out.args[open_out_index].value; |
| 44 | |
| 45 | daemon_fd = (int)open_out->passthrough_fd; |
| 46 | if (daemon_fd < 0) |
| 47 | return; |
| 48 | |
| 49 | passthrough_filp = fget_raw(daemon_fd); |
| 50 | if (!passthrough_filp) |
| 51 | return; |
| 52 | |
| 53 | passthrough_inode = file_inode(passthrough_filp); |
| 54 | passthrough_sb = passthrough_inode->i_sb; |
| 55 | fs_stack_depth = passthrough_sb->s_stack_depth + 1; |
| 56 | |
| 57 | /* If we reached the stacking limit go through regular io */ |
| 58 | if (fs_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { |
| 59 | /* Release the passthrough file. */ |
| 60 | fput(passthrough_filp); |
| 61 | pr_err("FUSE: maximum fs stacking depth exceeded, cannot use passthrough for this file\n"); |
| 62 | return; |
| 63 | } |
| 64 | req->passthrough_filp = passthrough_filp; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | static ssize_t fuse_passthrough_read_write_iter(struct kiocb *iocb, |
| 69 | struct iov_iter *iter, int do_write) |
| 70 | { |
| 71 | ssize_t ret_val; |
| 72 | struct fuse_file *ff; |
| 73 | struct file *fuse_file, *passthrough_filp; |
| 74 | struct inode *fuse_inode, *passthrough_inode; |
| 75 | struct fuse_conn *fc; |
| 76 | |
| 77 | ff = iocb->ki_filp->private_data; |
| 78 | fuse_file = iocb->ki_filp; |
| 79 | passthrough_filp = ff->passthrough_filp; |
| 80 | fc = ff->fc; |
| 81 | |
| 82 | /* lock passthrough file to prevent it from being released */ |
| 83 | get_file(passthrough_filp); |
| 84 | iocb->ki_filp = passthrough_filp; |
| 85 | fuse_inode = fuse_file->f_path.dentry->d_inode; |
| 86 | passthrough_inode = file_inode(passthrough_filp); |
| 87 | |
| 88 | if (do_write) { |
| 89 | if (!passthrough_filp->f_op->write_iter) |
| 90 | return -EIO; |
| 91 | |
| 92 | ret_val = passthrough_filp->f_op->write_iter(iocb, iter); |
| 93 | |
| 94 | if (ret_val >= 0 || ret_val == -EIOCBQUEUED) { |
| 95 | spin_lock(&fc->lock); |
| 96 | fsstack_copy_inode_size(fuse_inode, passthrough_inode); |
| 97 | spin_unlock(&fc->lock); |
| 98 | fsstack_copy_attr_times(fuse_inode, passthrough_inode); |
| 99 | } |
| 100 | } else { |
| 101 | if (!passthrough_filp->f_op->read_iter) |
| 102 | return -EIO; |
| 103 | |
| 104 | ret_val = passthrough_filp->f_op->read_iter(iocb, iter); |
| 105 | if (ret_val >= 0 || ret_val == -EIOCBQUEUED) |
| 106 | fsstack_copy_attr_atime(fuse_inode, passthrough_inode); |
| 107 | } |
| 108 | |
| 109 | iocb->ki_filp = fuse_file; |
| 110 | |
| 111 | /* unlock passthrough file */ |
| 112 | fput(passthrough_filp); |
| 113 | |
| 114 | return ret_val; |
| 115 | } |
| 116 | |
| 117 | ssize_t fuse_passthrough_read_iter(struct kiocb *iocb, struct iov_iter *to) |
| 118 | { |
| 119 | return fuse_passthrough_read_write_iter(iocb, to, 0); |
| 120 | } |
| 121 | |
| 122 | ssize_t fuse_passthrough_write_iter(struct kiocb *iocb, struct iov_iter *from) |
| 123 | { |
| 124 | return fuse_passthrough_read_write_iter(iocb, from, 1); |
| 125 | } |
| 126 | |
| 127 | void fuse_passthrough_release(struct fuse_file *ff) |
| 128 | { |
| 129 | if (!(ff->passthrough_filp)) |
| 130 | return; |
| 131 | |
| 132 | /* Release the passthrough file. */ |
| 133 | fput(ff->passthrough_filp); |
| 134 | ff->passthrough_filp = NULL; |
| 135 | } |