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