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