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