| b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | #include "fuse_i.h" |
| 4 | |
| 5 | #include <linux/fuse.h> |
| 6 | #include <linux/idr.h> |
| 7 | #include <linux/uio.h> |
| 8 | |
| 9 | #define PASSTHROUGH_IOCB_MASK \ |
| 10 | (IOCB_APPEND | IOCB_DSYNC | IOCB_HIPRI | IOCB_NOWAIT | IOCB_SYNC) |
| 11 | |
| 12 | struct fuse_aio_req { |
| 13 | struct kiocb iocb; |
| 14 | struct kiocb *iocb_fuse; |
| 15 | }; |
| 16 | |
| 17 | static inline void kiocb_clone(struct kiocb *kiocb, struct kiocb *kiocb_src, |
| 18 | struct file *filp) |
| 19 | { |
| 20 | *kiocb = (struct kiocb){ |
| 21 | .ki_filp = filp, |
| 22 | .ki_flags = kiocb_src->ki_flags, |
| 23 | .ki_hint = kiocb_src->ki_hint, |
| 24 | .ki_ioprio = kiocb_src->ki_ioprio, |
| 25 | .ki_pos = kiocb_src->ki_pos, |
| 26 | }; |
| 27 | } |
| 28 | |
| 29 | static void fuse_file_accessed(struct file *dst_file, struct file *src_file) |
| 30 | { |
| 31 | struct inode *dst_inode; |
| 32 | struct inode *src_inode; |
| 33 | |
| 34 | if (dst_file->f_flags & O_NOATIME) |
| 35 | return; |
| 36 | |
| 37 | dst_inode = file_inode(dst_file); |
| 38 | src_inode = file_inode(src_file); |
| 39 | |
| 40 | if ((!timespec64_equal(&dst_inode->i_mtime, &src_inode->i_mtime) || |
| 41 | !timespec64_equal(&dst_inode->i_ctime, &src_inode->i_ctime))) { |
| 42 | dst_inode->i_mtime = src_inode->i_mtime; |
| 43 | dst_inode->i_ctime = src_inode->i_ctime; |
| 44 | } |
| 45 | |
| 46 | touch_atime(&dst_file->f_path); |
| 47 | } |
| 48 | |
| 49 | static void fuse_copyattr(struct file *dst_file, struct file *src_file) |
| 50 | { |
| 51 | struct inode *dst = file_inode(dst_file); |
| 52 | struct inode *src = file_inode(src_file); |
| 53 | |
| 54 | dst->i_atime = src->i_atime; |
| 55 | dst->i_mtime = src->i_mtime; |
| 56 | dst->i_ctime = src->i_ctime; |
| 57 | i_size_write(dst, i_size_read(src)); |
| 58 | } |
| 59 | |
| 60 | static void fuse_aio_cleanup_handler(struct fuse_aio_req *aio_req) |
| 61 | { |
| 62 | struct kiocb *iocb = &aio_req->iocb; |
| 63 | struct kiocb *iocb_fuse = aio_req->iocb_fuse; |
| 64 | |
| 65 | if (iocb->ki_flags & IOCB_WRITE) { |
| 66 | __sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb, |
| 67 | SB_FREEZE_WRITE); |
| 68 | file_end_write(iocb->ki_filp); |
| 69 | fuse_copyattr(iocb_fuse->ki_filp, iocb->ki_filp); |
| 70 | } |
| 71 | |
| 72 | iocb_fuse->ki_pos = iocb->ki_pos; |
| 73 | kfree(aio_req); |
| 74 | } |
| 75 | |
| 76 | static void fuse_aio_rw_complete(struct kiocb *iocb, long res, long res2) |
| 77 | { |
| 78 | struct fuse_aio_req *aio_req = |
| 79 | container_of(iocb, struct fuse_aio_req, iocb); |
| 80 | struct kiocb *iocb_fuse = aio_req->iocb_fuse; |
| 81 | |
| 82 | fuse_aio_cleanup_handler(aio_req); |
| 83 | iocb_fuse->ki_complete(iocb_fuse, res, res2); |
| 84 | } |
| 85 | |
| 86 | ssize_t fuse_passthrough_read_iter(struct kiocb *iocb_fuse, |
| 87 | struct iov_iter *iter) |
| 88 | { |
| 89 | ssize_t ret; |
| 90 | const struct cred *old_cred; |
| 91 | struct file *fuse_filp = iocb_fuse->ki_filp; |
| 92 | struct fuse_file *ff = fuse_filp->private_data; |
| 93 | struct file *passthrough_filp = ff->passthrough.filp; |
| 94 | |
| 95 | if (!iov_iter_count(iter)) |
| 96 | return 0; |
| 97 | |
| 98 | old_cred = override_creds(ff->passthrough.cred); |
| 99 | if (is_sync_kiocb(iocb_fuse)) { |
| 100 | ret = vfs_iter_read(passthrough_filp, iter, &iocb_fuse->ki_pos, |
| 101 | iocb_to_rw_flags(iocb_fuse->ki_flags, |
| 102 | PASSTHROUGH_IOCB_MASK)); |
| 103 | } else { |
| 104 | struct fuse_aio_req *aio_req; |
| 105 | |
| 106 | aio_req = kmalloc(sizeof(struct fuse_aio_req), GFP_KERNEL); |
| 107 | if (!aio_req) { |
| 108 | ret = -ENOMEM; |
| 109 | goto out; |
| 110 | } |
| 111 | |
| 112 | aio_req->iocb_fuse = iocb_fuse; |
| 113 | kiocb_clone(&aio_req->iocb, iocb_fuse, passthrough_filp); |
| 114 | aio_req->iocb.ki_complete = fuse_aio_rw_complete; |
| 115 | ret = call_read_iter(passthrough_filp, &aio_req->iocb, iter); |
| 116 | if (ret != -EIOCBQUEUED) |
| 117 | fuse_aio_cleanup_handler(aio_req); |
| 118 | } |
| 119 | out: |
| 120 | revert_creds(old_cred); |
| 121 | |
| 122 | fuse_file_accessed(fuse_filp, passthrough_filp); |
| 123 | |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | ssize_t fuse_passthrough_write_iter(struct kiocb *iocb_fuse, |
| 128 | struct iov_iter *iter) |
| 129 | { |
| 130 | ssize_t ret; |
| 131 | const struct cred *old_cred; |
| 132 | struct file *fuse_filp = iocb_fuse->ki_filp; |
| 133 | struct fuse_file *ff = fuse_filp->private_data; |
| 134 | struct inode *fuse_inode = file_inode(fuse_filp); |
| 135 | struct file *passthrough_filp = ff->passthrough.filp; |
| 136 | struct inode *passthrough_inode = file_inode(passthrough_filp); |
| 137 | |
| 138 | if (!iov_iter_count(iter)) |
| 139 | return 0; |
| 140 | |
| 141 | inode_lock(fuse_inode); |
| 142 | |
| 143 | fuse_copyattr(fuse_filp, passthrough_filp); |
| 144 | |
| 145 | old_cred = override_creds(ff->passthrough.cred); |
| 146 | if (is_sync_kiocb(iocb_fuse)) { |
| 147 | file_start_write(passthrough_filp); |
| 148 | ret = vfs_iter_write(passthrough_filp, iter, &iocb_fuse->ki_pos, |
| 149 | iocb_to_rw_flags(iocb_fuse->ki_flags, |
| 150 | PASSTHROUGH_IOCB_MASK)); |
| 151 | file_end_write(passthrough_filp); |
| 152 | if (ret > 0) |
| 153 | fuse_copyattr(fuse_filp, passthrough_filp); |
| 154 | } else { |
| 155 | struct fuse_aio_req *aio_req; |
| 156 | |
| 157 | aio_req = kmalloc(sizeof(struct fuse_aio_req), GFP_KERNEL); |
| 158 | if (!aio_req) { |
| 159 | ret = -ENOMEM; |
| 160 | goto out; |
| 161 | } |
| 162 | |
| 163 | file_start_write(passthrough_filp); |
| 164 | __sb_writers_release(passthrough_inode->i_sb, SB_FREEZE_WRITE); |
| 165 | |
| 166 | aio_req->iocb_fuse = iocb_fuse; |
| 167 | kiocb_clone(&aio_req->iocb, iocb_fuse, passthrough_filp); |
| 168 | aio_req->iocb.ki_complete = fuse_aio_rw_complete; |
| 169 | ret = call_write_iter(passthrough_filp, &aio_req->iocb, iter); |
| 170 | if (ret != -EIOCBQUEUED) |
| 171 | fuse_aio_cleanup_handler(aio_req); |
| 172 | } |
| 173 | out: |
| 174 | revert_creds(old_cred); |
| 175 | inode_unlock(fuse_inode); |
| 176 | |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma) |
| 181 | { |
| 182 | int ret; |
| 183 | const struct cred *old_cred; |
| 184 | struct fuse_file *ff = file->private_data; |
| 185 | struct file *passthrough_filp = ff->passthrough.filp; |
| 186 | |
| 187 | if (!passthrough_filp->f_op->mmap) |
| 188 | return -ENODEV; |
| 189 | |
| 190 | if (WARN_ON(file != vma->vm_file)) |
| 191 | return -EIO; |
| 192 | |
| 193 | vma->vm_file = get_file(passthrough_filp); |
| 194 | |
| 195 | old_cred = override_creds(ff->passthrough.cred); |
| 196 | ret = call_mmap(vma->vm_file, vma); |
| 197 | revert_creds(old_cred); |
| 198 | |
| 199 | if (ret) |
| 200 | fput(passthrough_filp); |
| 201 | else |
| 202 | fput(file); |
| 203 | |
| 204 | fuse_file_accessed(file, passthrough_filp); |
| 205 | |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | int fuse_passthrough_open(struct fuse_dev *fud, u32 lower_fd) |
| 210 | { |
| 211 | int res; |
| 212 | struct file *passthrough_filp; |
| 213 | struct fuse_conn *fc = fud->fc; |
| 214 | struct inode *passthrough_inode; |
| 215 | struct super_block *passthrough_sb; |
| 216 | struct fuse_passthrough *passthrough; |
| 217 | |
| 218 | if (!fc->passthrough) |
| 219 | return -EPERM; |
| 220 | |
| 221 | passthrough_filp = fget(lower_fd); |
| 222 | if (!passthrough_filp) { |
| 223 | pr_err("FUSE: invalid file descriptor for passthrough.\n"); |
| 224 | return -EBADF; |
| 225 | } |
| 226 | |
| 227 | if (!passthrough_filp->f_op->read_iter || |
| 228 | !passthrough_filp->f_op->write_iter) { |
| 229 | pr_err("FUSE: passthrough file misses file operations.\n"); |
| 230 | res = -EBADF; |
| 231 | goto err_free_file; |
| 232 | } |
| 233 | |
| 234 | passthrough_inode = file_inode(passthrough_filp); |
| 235 | passthrough_sb = passthrough_inode->i_sb; |
| 236 | if (passthrough_sb->s_stack_depth >= FILESYSTEM_MAX_STACK_DEPTH) { |
| 237 | pr_err("FUSE: fs stacking depth exceeded for passthrough\n"); |
| 238 | res = -EINVAL; |
| 239 | goto err_free_file; |
| 240 | } |
| 241 | |
| 242 | passthrough = kmalloc(sizeof(struct fuse_passthrough), GFP_KERNEL); |
| 243 | if (!passthrough) { |
| 244 | res = -ENOMEM; |
| 245 | goto err_free_file; |
| 246 | } |
| 247 | |
| 248 | passthrough->filp = passthrough_filp; |
| 249 | passthrough->cred = prepare_creds(); |
| 250 | |
| 251 | idr_preload(GFP_KERNEL); |
| 252 | spin_lock(&fc->passthrough_req_lock); |
| 253 | res = idr_alloc(&fc->passthrough_req, passthrough, 1, 0, GFP_ATOMIC); |
| 254 | spin_unlock(&fc->passthrough_req_lock); |
| 255 | idr_preload_end(); |
| 256 | |
| 257 | if (res > 0) |
| 258 | return res; |
| 259 | |
| 260 | fuse_passthrough_release(passthrough); |
| 261 | kfree(passthrough); |
| 262 | |
| 263 | err_free_file: |
| 264 | fput(passthrough_filp); |
| 265 | |
| 266 | return res; |
| 267 | } |
| 268 | |
| 269 | int fuse_passthrough_setup(struct fuse_conn *fc, struct fuse_file *ff, |
| 270 | struct fuse_open_out *openarg) |
| 271 | { |
| 272 | struct fuse_passthrough *passthrough; |
| 273 | int passthrough_fh = openarg->passthrough_fh; |
| 274 | |
| 275 | if (!fc->passthrough) |
| 276 | return -EPERM; |
| 277 | |
| 278 | /* Default case, passthrough is not requested */ |
| 279 | if (passthrough_fh <= 0) |
| 280 | return -EINVAL; |
| 281 | |
| 282 | spin_lock(&fc->passthrough_req_lock); |
| 283 | passthrough = idr_remove(&fc->passthrough_req, passthrough_fh); |
| 284 | spin_unlock(&fc->passthrough_req_lock); |
| 285 | |
| 286 | if (!passthrough) |
| 287 | return -EINVAL; |
| 288 | |
| 289 | ff->passthrough = *passthrough; |
| 290 | kfree(passthrough); |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | void fuse_passthrough_release(struct fuse_passthrough *passthrough) |
| 296 | { |
| 297 | if (passthrough->filp) { |
| 298 | fput(passthrough->filp); |
| 299 | passthrough->filp = NULL; |
| 300 | } |
| 301 | if (passthrough->cred) { |
| 302 | put_cred(passthrough->cred); |
| 303 | passthrough->cred = NULL; |
| 304 | } |
| 305 | } |