1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * FUSE passthrough to backing file. 4 * 5 * Copyright (c) 2023 CTERA Networks. 6 */ 7 8 #include "fuse_i.h" 9 10 #include <linux/file.h> 11 #include <linux/backing-file.h> 12 #include <linux/splice.h> 13 14 static void fuse_file_accessed(struct file *file) 15 { 16 struct inode *inode = file_inode(file); 17 18 fuse_invalidate_atime(inode); 19 } 20 21 static void fuse_passthrough_end_write(struct kiocb *iocb, ssize_t ret) 22 { 23 struct inode *inode = file_inode(iocb->ki_filp); 24 25 fuse_write_update_attr(inode, iocb->ki_pos, ret); 26 } 27 28 ssize_t fuse_passthrough_read_iter(struct kiocb *iocb, struct iov_iter *iter) 29 { 30 struct file *file = iocb->ki_filp; 31 struct fuse_file *ff = file->private_data; 32 struct file *backing_file = fuse_file_passthrough(ff); 33 size_t count = iov_iter_count(iter); 34 ssize_t ret; 35 struct backing_file_ctx ctx = { 36 .cred = ff->cred, 37 .accessed = fuse_file_accessed, 38 }; 39 40 41 pr_debug("%s: backing_file=0x%p, pos=%lld, len=%zu\n", __func__, 42 backing_file, iocb->ki_pos, count); 43 44 if (!count) 45 return 0; 46 47 ret = backing_file_read_iter(backing_file, iter, iocb, iocb->ki_flags, 48 &ctx); 49 50 return ret; 51 } 52 53 ssize_t fuse_passthrough_write_iter(struct kiocb *iocb, 54 struct iov_iter *iter) 55 { 56 struct file *file = iocb->ki_filp; 57 struct inode *inode = file_inode(file); 58 struct fuse_file *ff = file->private_data; 59 struct file *backing_file = fuse_file_passthrough(ff); 60 size_t count = iov_iter_count(iter); 61 ssize_t ret; 62 struct backing_file_ctx ctx = { 63 .cred = ff->cred, 64 .end_write = fuse_passthrough_end_write, 65 }; 66 67 pr_debug("%s: backing_file=0x%p, pos=%lld, len=%zu\n", __func__, 68 backing_file, iocb->ki_pos, count); 69 70 if (!count) 71 return 0; 72 73 inode_lock(inode); 74 ret = backing_file_write_iter(backing_file, iter, iocb, iocb->ki_flags, 75 &ctx); 76 inode_unlock(inode); 77 78 return ret; 79 } 80 81 ssize_t fuse_passthrough_splice_read(struct file *in, loff_t *ppos, 82 struct pipe_inode_info *pipe, 83 size_t len, unsigned int flags) 84 { 85 struct fuse_file *ff = in->private_data; 86 struct file *backing_file = fuse_file_passthrough(ff); 87 struct backing_file_ctx ctx = { 88 .cred = ff->cred, 89 .accessed = fuse_file_accessed, 90 }; 91 struct kiocb iocb; 92 ssize_t ret; 93 94 pr_debug("%s: backing_file=0x%p, pos=%lld, len=%zu, flags=0x%x\n", __func__, 95 backing_file, *ppos, len, flags); 96 97 init_sync_kiocb(&iocb, in); 98 iocb.ki_pos = *ppos; 99 ret = backing_file_splice_read(backing_file, &iocb, pipe, len, flags, &ctx); 100 *ppos = iocb.ki_pos; 101 102 return ret; 103 } 104 105 ssize_t fuse_passthrough_splice_write(struct pipe_inode_info *pipe, 106 struct file *out, loff_t *ppos, 107 size_t len, unsigned int flags) 108 { 109 struct fuse_file *ff = out->private_data; 110 struct file *backing_file = fuse_file_passthrough(ff); 111 struct inode *inode = file_inode(out); 112 ssize_t ret; 113 struct backing_file_ctx ctx = { 114 .cred = ff->cred, 115 .end_write = fuse_passthrough_end_write, 116 }; 117 struct kiocb iocb; 118 119 pr_debug("%s: backing_file=0x%p, pos=%lld, len=%zu, flags=0x%x\n", __func__, 120 backing_file, *ppos, len, flags); 121 122 inode_lock(inode); 123 init_sync_kiocb(&iocb, out); 124 iocb.ki_pos = *ppos; 125 ret = backing_file_splice_write(pipe, backing_file, &iocb, len, flags, &ctx); 126 *ppos = iocb.ki_pos; 127 inode_unlock(inode); 128 129 return ret; 130 } 131 132 ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma) 133 { 134 struct fuse_file *ff = file->private_data; 135 struct file *backing_file = fuse_file_passthrough(ff); 136 struct backing_file_ctx ctx = { 137 .cred = ff->cred, 138 .accessed = fuse_file_accessed, 139 }; 140 141 pr_debug("%s: backing_file=0x%p, start=%lu, end=%lu\n", __func__, 142 backing_file, vma->vm_start, vma->vm_end); 143 144 return backing_file_mmap(backing_file, vma, &ctx); 145 } 146 147 struct fuse_backing *fuse_backing_get(struct fuse_backing *fb) 148 { 149 if (fb && refcount_inc_not_zero(&fb->count)) 150 return fb; 151 return NULL; 152 } 153 154 static void fuse_backing_free(struct fuse_backing *fb) 155 { 156 pr_debug("%s: fb=0x%p\n", __func__, fb); 157 158 if (fb->file) 159 fput(fb->file); 160 put_cred(fb->cred); 161 kfree_rcu(fb, rcu); 162 } 163 164 void fuse_backing_put(struct fuse_backing *fb) 165 { 166 if (fb && refcount_dec_and_test(&fb->count)) 167 fuse_backing_free(fb); 168 } 169 170 void fuse_backing_files_init(struct fuse_conn *fc) 171 { 172 idr_init(&fc->backing_files_map); 173 } 174 175 static int fuse_backing_id_alloc(struct fuse_conn *fc, struct fuse_backing *fb) 176 { 177 int id; 178 179 idr_preload(GFP_KERNEL); 180 spin_lock(&fc->lock); 181 /* FIXME: xarray might be space inefficient */ 182 id = idr_alloc_cyclic(&fc->backing_files_map, fb, 1, 0, GFP_ATOMIC); 183 spin_unlock(&fc->lock); 184 idr_preload_end(); 185 186 WARN_ON_ONCE(id == 0); 187 return id; 188 } 189 190 static struct fuse_backing *fuse_backing_id_remove(struct fuse_conn *fc, 191 int id) 192 { 193 struct fuse_backing *fb; 194 195 spin_lock(&fc->lock); 196 fb = idr_remove(&fc->backing_files_map, id); 197 spin_unlock(&fc->lock); 198 199 return fb; 200 } 201 202 static int fuse_backing_id_free(int id, void *p, void *data) 203 { 204 struct fuse_backing *fb = p; 205 206 WARN_ON_ONCE(refcount_read(&fb->count) != 1); 207 fuse_backing_free(fb); 208 return 0; 209 } 210 211 void fuse_backing_files_free(struct fuse_conn *fc) 212 { 213 idr_for_each(&fc->backing_files_map, fuse_backing_id_free, NULL); 214 idr_destroy(&fc->backing_files_map); 215 } 216 217 int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map) 218 { 219 struct file *file; 220 struct super_block *backing_sb; 221 struct fuse_backing *fb = NULL; 222 int res; 223 224 pr_debug("%s: fd=%d flags=0x%x\n", __func__, map->fd, map->flags); 225 226 /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */ 227 res = -EPERM; 228 if (!fc->passthrough || !capable(CAP_SYS_ADMIN)) 229 goto out; 230 231 res = -EINVAL; 232 if (map->flags || map->padding) 233 goto out; 234 235 file = fget_raw(map->fd); 236 res = -EBADF; 237 if (!file) 238 goto out; 239 240 /* read/write/splice/mmap passthrough only relevant for regular files */ 241 res = d_is_dir(file->f_path.dentry) ? -EISDIR : -EINVAL; 242 if (!d_is_reg(file->f_path.dentry)) 243 goto out_fput; 244 245 backing_sb = file_inode(file)->i_sb; 246 res = -ELOOP; 247 if (backing_sb->s_stack_depth >= fc->max_stack_depth) 248 goto out_fput; 249 250 fb = kmalloc(sizeof(struct fuse_backing), GFP_KERNEL); 251 res = -ENOMEM; 252 if (!fb) 253 goto out_fput; 254 255 fb->file = file; 256 fb->cred = prepare_creds(); 257 refcount_set(&fb->count, 1); 258 259 res = fuse_backing_id_alloc(fc, fb); 260 if (res < 0) { 261 fuse_backing_free(fb); 262 fb = NULL; 263 } 264 265 out: 266 pr_debug("%s: fb=0x%p, ret=%i\n", __func__, fb, res); 267 268 return res; 269 270 out_fput: 271 fput(file); 272 goto out; 273 } 274 275 int fuse_backing_close(struct fuse_conn *fc, int backing_id) 276 { 277 struct fuse_backing *fb = NULL; 278 int err; 279 280 pr_debug("%s: backing_id=%d\n", __func__, backing_id); 281 282 /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */ 283 err = -EPERM; 284 if (!fc->passthrough || !capable(CAP_SYS_ADMIN)) 285 goto out; 286 287 err = -EINVAL; 288 if (backing_id <= 0) 289 goto out; 290 291 err = -ENOENT; 292 fb = fuse_backing_id_remove(fc, backing_id); 293 if (!fb) 294 goto out; 295 296 fuse_backing_put(fb); 297 err = 0; 298 out: 299 pr_debug("%s: fb=0x%p, err=%i\n", __func__, fb, err); 300 301 return err; 302 } 303 304 /* 305 * Setup passthrough to a backing file. 306 * 307 * Returns an fb object with elevated refcount to be stored in fuse inode. 308 */ 309 struct fuse_backing *fuse_passthrough_open(struct file *file, 310 struct inode *inode, 311 int backing_id) 312 { 313 struct fuse_file *ff = file->private_data; 314 struct fuse_conn *fc = ff->fm->fc; 315 struct fuse_backing *fb = NULL; 316 struct file *backing_file; 317 int err; 318 319 err = -EINVAL; 320 if (backing_id <= 0) 321 goto out; 322 323 rcu_read_lock(); 324 fb = idr_find(&fc->backing_files_map, backing_id); 325 fb = fuse_backing_get(fb); 326 rcu_read_unlock(); 327 328 err = -ENOENT; 329 if (!fb) 330 goto out; 331 332 /* Allocate backing file per fuse file to store fuse path */ 333 backing_file = backing_file_open(&file->f_path, file->f_flags, 334 &fb->file->f_path, fb->cred); 335 err = PTR_ERR(backing_file); 336 if (IS_ERR(backing_file)) { 337 fuse_backing_put(fb); 338 goto out; 339 } 340 341 err = 0; 342 ff->passthrough = backing_file; 343 ff->cred = get_cred(fb->cred); 344 out: 345 pr_debug("%s: backing_id=%d, fb=0x%p, backing_file=0x%p, err=%i\n", __func__, 346 backing_id, fb, ff->passthrough, err); 347 348 return err ? ERR_PTR(err) : fb; 349 } 350 351 void fuse_passthrough_release(struct fuse_file *ff, struct fuse_backing *fb) 352 { 353 pr_debug("%s: fb=0x%p, backing_file=0x%p\n", __func__, 354 fb, ff->passthrough); 355 356 fput(ff->passthrough); 357 ff->passthrough = NULL; 358 put_cred(ff->cred); 359 ff->cred = NULL; 360 } 361