1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/fs/nfs/file.c 4 * 5 * Copyright (C) 1992 Rick Sladkey 6 * 7 * Changes Copyright (C) 1994 by Florian La Roche 8 * - Do not copy data too often around in the kernel. 9 * - In nfs_file_read the return value of kmalloc wasn't checked. 10 * - Put in a better version of read look-ahead buffering. Original idea 11 * and implementation by Wai S Kok elekokws@ee.nus.sg. 12 * 13 * Expire cache on write to a file by Wai S Kok (Oct 1994). 14 * 15 * Total rewrite of read side for new NFS buffer cache.. Linus. 16 * 17 * nfs regular file handling functions 18 */ 19 20 #include <linux/module.h> 21 #include <linux/time.h> 22 #include <linux/kernel.h> 23 #include <linux/errno.h> 24 #include <linux/fcntl.h> 25 #include <linux/stat.h> 26 #include <linux/nfs_fs.h> 27 #include <linux/nfs_mount.h> 28 #include <linux/mm.h> 29 #include <linux/pagemap.h> 30 #include <linux/gfp.h> 31 #include <linux/rmap.h> 32 #include <linux/swap.h> 33 #include <linux/compaction.h> 34 35 #include <linux/uaccess.h> 36 #include <linux/filelock.h> 37 38 #include "delegation.h" 39 #include "internal.h" 40 #include "iostat.h" 41 #include "fscache.h" 42 #include "pnfs.h" 43 44 #include "nfstrace.h" 45 46 #define NFSDBG_FACILITY NFSDBG_FILE 47 48 static const struct vm_operations_struct nfs_file_vm_ops; 49 50 int nfs_check_flags(int flags) 51 { 52 if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT)) 53 return -EINVAL; 54 55 return 0; 56 } 57 EXPORT_SYMBOL_GPL(nfs_check_flags); 58 59 /* 60 * Open file 61 */ 62 static int 63 nfs_file_open(struct inode *inode, struct file *filp) 64 { 65 int res; 66 67 dprintk("NFS: open file(%pD2)\n", filp); 68 69 nfs_inc_stats(inode, NFSIOS_VFSOPEN); 70 res = nfs_check_flags(filp->f_flags); 71 if (res) 72 return res; 73 74 res = nfs_open(inode, filp); 75 if (res == 0) 76 filp->f_mode |= FMODE_CAN_ODIRECT; 77 return res; 78 } 79 80 int 81 nfs_file_release(struct inode *inode, struct file *filp) 82 { 83 dprintk("NFS: release(%pD2)\n", filp); 84 85 nfs_inc_stats(inode, NFSIOS_VFSRELEASE); 86 nfs_file_clear_open_context(filp); 87 nfs_fscache_release_file(inode, filp); 88 return 0; 89 } 90 EXPORT_SYMBOL_GPL(nfs_file_release); 91 92 /** 93 * nfs_revalidate_file_size - Revalidate the file size 94 * @inode: pointer to inode struct 95 * @filp: pointer to struct file 96 * 97 * Revalidates the file length. This is basically a wrapper around 98 * nfs_revalidate_inode() that takes into account the fact that we may 99 * have cached writes (in which case we don't care about the server's 100 * idea of what the file length is), or O_DIRECT (in which case we 101 * shouldn't trust the cache). 102 */ 103 static int nfs_revalidate_file_size(struct inode *inode, struct file *filp) 104 { 105 struct nfs_server *server = NFS_SERVER(inode); 106 107 if (filp->f_flags & O_DIRECT) 108 goto force_reval; 109 if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_SIZE)) 110 goto force_reval; 111 return 0; 112 force_reval: 113 return __nfs_revalidate_inode(server, inode); 114 } 115 116 loff_t nfs_file_llseek(struct file *filp, loff_t offset, int whence) 117 { 118 dprintk("NFS: llseek file(%pD2, %lld, %d)\n", 119 filp, offset, whence); 120 121 /* 122 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate 123 * the cached file length 124 */ 125 if (whence != SEEK_SET && whence != SEEK_CUR) { 126 struct inode *inode = filp->f_mapping->host; 127 128 int retval = nfs_revalidate_file_size(inode, filp); 129 if (retval < 0) 130 return (loff_t)retval; 131 } 132 133 return generic_file_llseek(filp, offset, whence); 134 } 135 EXPORT_SYMBOL_GPL(nfs_file_llseek); 136 137 /* 138 * Flush all dirty pages, and check for write errors. 139 */ 140 static int 141 nfs_file_flush(struct file *file, fl_owner_t id) 142 { 143 struct inode *inode = file_inode(file); 144 errseq_t since; 145 146 dprintk("NFS: flush(%pD2)\n", file); 147 148 nfs_inc_stats(inode, NFSIOS_VFSFLUSH); 149 if ((file->f_mode & FMODE_WRITE) == 0) 150 return 0; 151 152 /* Flush writes to the server and return any errors */ 153 since = filemap_sample_wb_err(file->f_mapping); 154 nfs_wb_all(inode); 155 return filemap_check_wb_err(file->f_mapping, since); 156 } 157 158 ssize_t 159 nfs_file_read(struct kiocb *iocb, struct iov_iter *to) 160 { 161 struct inode *inode = file_inode(iocb->ki_filp); 162 ssize_t result; 163 164 if (iocb->ki_flags & IOCB_DIRECT) 165 return nfs_file_direct_read(iocb, to, false); 166 167 dprintk("NFS: read(%pD2, %zu@%lu)\n", 168 iocb->ki_filp, 169 iov_iter_count(to), (unsigned long) iocb->ki_pos); 170 171 result = nfs_start_io_read(inode); 172 if (result) 173 return result; 174 175 result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping); 176 if (!result) { 177 result = generic_file_read_iter(iocb, to); 178 if (result > 0) 179 nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result); 180 } 181 nfs_end_io_read(inode); 182 return result; 183 } 184 EXPORT_SYMBOL_GPL(nfs_file_read); 185 186 ssize_t 187 nfs_file_splice_read(struct file *in, loff_t *ppos, struct pipe_inode_info *pipe, 188 size_t len, unsigned int flags) 189 { 190 struct inode *inode = file_inode(in); 191 ssize_t result; 192 193 dprintk("NFS: splice_read(%pD2, %zu@%llu)\n", in, len, *ppos); 194 195 result = nfs_start_io_read(inode); 196 if (result) 197 return result; 198 199 result = nfs_revalidate_mapping(inode, in->f_mapping); 200 if (!result) { 201 result = filemap_splice_read(in, ppos, pipe, len, flags); 202 if (result > 0) 203 nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result); 204 } 205 nfs_end_io_read(inode); 206 return result; 207 } 208 EXPORT_SYMBOL_GPL(nfs_file_splice_read); 209 210 int 211 nfs_file_mmap_prepare(struct vm_area_desc *desc) 212 { 213 struct file *file = desc->file; 214 struct inode *inode = file_inode(file); 215 int status; 216 217 dprintk("NFS: mmap(%pD2)\n", file); 218 219 /* Note: generic_file_mmap_prepare() returns ENOSYS on nommu systems 220 * so we call that before revalidating the mapping 221 */ 222 status = generic_file_mmap_prepare(desc); 223 if (!status) { 224 desc->vm_ops = &nfs_file_vm_ops; 225 status = nfs_revalidate_mapping(inode, file->f_mapping); 226 } 227 return status; 228 } 229 EXPORT_SYMBOL_GPL(nfs_file_mmap_prepare); 230 231 /* 232 * Flush any dirty pages for this process, and check for write errors. 233 * The return status from this call provides a reliable indication of 234 * whether any write errors occurred for this process. 235 */ 236 static int 237 nfs_file_fsync_commit(struct file *file, int datasync) 238 { 239 struct inode *inode = file_inode(file); 240 int ret, ret2; 241 242 dprintk("NFS: fsync file(%pD2) datasync %d\n", file, datasync); 243 244 nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 245 ret = nfs_commit_inode(inode, FLUSH_SYNC); 246 ret2 = file_check_and_advance_wb_err(file); 247 if (ret2 < 0) 248 return ret2; 249 return ret; 250 } 251 252 int 253 nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync) 254 { 255 struct inode *inode = file_inode(file); 256 struct nfs_inode *nfsi = NFS_I(inode); 257 long save_nredirtied = atomic_long_read(&nfsi->redirtied_pages); 258 long nredirtied; 259 int ret; 260 261 trace_nfs_fsync_enter(inode); 262 263 for (;;) { 264 ret = file_write_and_wait_range(file, start, end); 265 if (ret != 0) 266 break; 267 ret = nfs_file_fsync_commit(file, datasync); 268 if (ret != 0) 269 break; 270 ret = pnfs_sync_inode(inode, !!datasync); 271 if (ret != 0) 272 break; 273 nredirtied = atomic_long_read(&nfsi->redirtied_pages); 274 if (nredirtied == save_nredirtied) 275 break; 276 save_nredirtied = nredirtied; 277 } 278 279 trace_nfs_fsync_exit(inode, ret); 280 return ret; 281 } 282 EXPORT_SYMBOL_GPL(nfs_file_fsync); 283 284 void nfs_truncate_last_folio(struct address_space *mapping, loff_t from, 285 loff_t to) 286 { 287 struct folio *folio; 288 289 if (from >= to) 290 return; 291 292 folio = filemap_lock_folio(mapping, from >> PAGE_SHIFT); 293 if (IS_ERR(folio)) 294 return; 295 296 if (folio_mkclean(folio)) 297 folio_mark_dirty(folio); 298 299 if (folio_test_uptodate(folio)) { 300 loff_t fpos = folio_pos(folio); 301 size_t offset = from - fpos; 302 size_t end = folio_size(folio); 303 304 if (to - fpos < end) 305 end = to - fpos; 306 folio_zero_segment(folio, offset, end); 307 trace_nfs_size_truncate_folio(mapping->host, to); 308 } 309 310 folio_unlock(folio); 311 folio_put(folio); 312 } 313 EXPORT_SYMBOL_GPL(nfs_truncate_last_folio); 314 315 /* 316 * Decide whether a read/modify/write cycle may be more efficient 317 * then a modify/write/read cycle when writing to a page in the 318 * page cache. 319 * 320 * Some pNFS layout drivers can only read/write at a certain block 321 * granularity like all block devices and therefore we must perform 322 * read/modify/write whenever a page hasn't read yet and the data 323 * to be written there is not aligned to a block boundary and/or 324 * smaller than the block size. 325 * 326 * The modify/write/read cycle may occur if a page is read before 327 * being completely filled by the writer. In this situation, the 328 * page must be completely written to stable storage on the server 329 * before it can be refilled by reading in the page from the server. 330 * This can lead to expensive, small, FILE_SYNC mode writes being 331 * done. 332 * 333 * It may be more efficient to read the page first if the file is 334 * open for reading in addition to writing, the page is not marked 335 * as Uptodate, it is not dirty or waiting to be committed, 336 * indicating that it was previously allocated and then modified, 337 * that there were valid bytes of data in that range of the file, 338 * and that the new data won't completely replace the old data in 339 * that range of the file. 340 */ 341 static bool nfs_folio_is_full_write(struct folio *folio, loff_t pos, 342 unsigned int len) 343 { 344 unsigned int pglen = nfs_folio_length(folio); 345 unsigned int offset = offset_in_folio(folio, pos); 346 unsigned int end = offset + len; 347 348 return !pglen || (end >= pglen && !offset); 349 } 350 351 static bool nfs_want_read_modify_write(struct file *file, struct folio *folio, 352 loff_t pos, unsigned int len) 353 { 354 /* 355 * Up-to-date pages, those with ongoing or full-page write 356 * don't need read/modify/write 357 */ 358 if (folio_test_uptodate(folio) || folio_test_private(folio) || 359 nfs_folio_is_full_write(folio, pos, len)) 360 return false; 361 362 if (pnfs_ld_read_whole_page(file_inode(file))) 363 return true; 364 /* Open for reading too? */ 365 if (file->f_mode & FMODE_READ) 366 return true; 367 return false; 368 } 369 370 /* 371 * This does the "real" work of the write. We must allocate and lock the 372 * page to be sent back to the generic routine, which then copies the 373 * data from user space. 374 * 375 * If the writer ends up delaying the write, the writer needs to 376 * increment the page use counts until he is done with the page. 377 */ 378 static int nfs_write_begin(const struct kiocb *iocb, 379 struct address_space *mapping, 380 loff_t pos, unsigned len, struct folio **foliop, 381 void **fsdata) 382 { 383 fgf_t fgp = FGP_WRITEBEGIN; 384 struct folio *folio; 385 struct file *file = iocb->ki_filp; 386 int once_thru = 0; 387 int ret; 388 389 dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%lu), %u@%lld)\n", 390 file, mapping->host->i_ino, len, (long long) pos); 391 nfs_truncate_last_folio(mapping, i_size_read(mapping->host), pos); 392 393 fgp |= fgf_set_order(len); 394 start: 395 folio = __filemap_get_folio(mapping, pos >> PAGE_SHIFT, fgp, 396 mapping_gfp_mask(mapping)); 397 if (IS_ERR(folio)) 398 return PTR_ERR(folio); 399 *foliop = folio; 400 401 ret = nfs_flush_incompatible(file, folio); 402 if (ret) { 403 folio_unlock(folio); 404 folio_put(folio); 405 } else if (!once_thru && 406 nfs_want_read_modify_write(file, folio, pos, len)) { 407 once_thru = 1; 408 ret = nfs_read_folio(file, folio); 409 folio_put(folio); 410 if (!ret) 411 goto start; 412 } 413 return ret; 414 } 415 416 static int nfs_write_end(const struct kiocb *iocb, 417 struct address_space *mapping, 418 loff_t pos, unsigned len, unsigned copied, 419 struct folio *folio, void *fsdata) 420 { 421 struct file *file = iocb->ki_filp; 422 struct nfs_open_context *ctx = nfs_file_open_context(file); 423 unsigned offset = offset_in_folio(folio, pos); 424 int status; 425 426 dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)\n", 427 file, mapping->host->i_ino, len, (long long) pos); 428 429 /* 430 * Zero any uninitialised parts of the page, and then mark the page 431 * as up to date if it turns out that we're extending the file. 432 */ 433 if (!folio_test_uptodate(folio)) { 434 size_t fsize = folio_size(folio); 435 unsigned pglen = nfs_folio_length(folio); 436 unsigned end = offset + copied; 437 438 if (pglen == 0) { 439 folio_zero_segments(folio, 0, offset, end, fsize); 440 folio_mark_uptodate(folio); 441 } else if (end >= pglen) { 442 folio_zero_segment(folio, end, fsize); 443 if (offset == 0) 444 folio_mark_uptodate(folio); 445 } else 446 folio_zero_segment(folio, pglen, fsize); 447 } 448 449 status = nfs_update_folio(file, folio, offset, copied); 450 451 folio_unlock(folio); 452 folio_put(folio); 453 454 if (status < 0) 455 return status; 456 NFS_I(mapping->host)->write_io += copied; 457 458 if (nfs_ctx_key_to_expire(ctx, mapping->host)) 459 nfs_wb_all(mapping->host); 460 461 return copied; 462 } 463 464 /* 465 * Partially or wholly invalidate a page 466 * - Release the private state associated with a page if undergoing complete 467 * page invalidation 468 * - Called if either PG_private or PG_fscache is set on the page 469 * - Caller holds page lock 470 */ 471 static void nfs_invalidate_folio(struct folio *folio, size_t offset, 472 size_t length) 473 { 474 struct inode *inode = folio->mapping->host; 475 dfprintk(PAGECACHE, "NFS: invalidate_folio(%lu, %zu, %zu)\n", 476 folio->index, offset, length); 477 478 /* Cancel any unstarted writes on this page */ 479 if (offset != 0 || length < folio_size(folio)) 480 nfs_wb_folio(inode, folio); 481 else 482 nfs_wb_folio_cancel(inode, folio); 483 folio_wait_private_2(folio); /* [DEPRECATED] */ 484 trace_nfs_invalidate_folio(inode, folio_pos(folio) + offset, length); 485 } 486 487 /* 488 * Attempt to release the private state associated with a folio 489 * - Called if either private or fscache flags are set on the folio 490 * - Caller holds folio lock 491 * - Return true (may release folio) or false (may not) 492 */ 493 static bool nfs_release_folio(struct folio *folio, gfp_t gfp) 494 { 495 dfprintk(PAGECACHE, "NFS: release_folio(%p)\n", folio); 496 497 /* If the private flag is set, then the folio is not freeable */ 498 if (folio_test_private(folio)) { 499 if ((current_gfp_context(gfp) & GFP_KERNEL) != GFP_KERNEL || 500 current_is_kswapd() || current_is_kcompactd()) 501 return false; 502 if (nfs_wb_folio(folio->mapping->host, folio) < 0) 503 return false; 504 } 505 return nfs_fscache_release_folio(folio, gfp); 506 } 507 508 static void nfs_check_dirty_writeback(struct folio *folio, 509 bool *dirty, bool *writeback) 510 { 511 struct nfs_inode *nfsi; 512 struct address_space *mapping = folio->mapping; 513 514 /* 515 * Check if an unstable folio is currently being committed and 516 * if so, have the VM treat it as if the folio is under writeback 517 * so it will not block due to folios that will shortly be freeable. 518 */ 519 nfsi = NFS_I(mapping->host); 520 if (atomic_read(&nfsi->commit_info.rpcs_out)) { 521 *writeback = true; 522 return; 523 } 524 525 /* 526 * If the private flag is set, then the folio is not freeable 527 * and as the inode is not being committed, it's not going to 528 * be cleaned in the near future so treat it as dirty 529 */ 530 if (folio_test_private(folio)) 531 *dirty = true; 532 } 533 534 /* 535 * Attempt to clear the private state associated with a page when an error 536 * occurs that requires the cached contents of an inode to be written back or 537 * destroyed 538 * - Called if either PG_private or fscache is set on the page 539 * - Caller holds page lock 540 * - Return 0 if successful, -error otherwise 541 */ 542 static int nfs_launder_folio(struct folio *folio) 543 { 544 struct inode *inode = folio->mapping->host; 545 int ret; 546 547 dfprintk(PAGECACHE, "NFS: launder_folio(%ld, %llu)\n", 548 inode->i_ino, folio_pos(folio)); 549 550 folio_wait_private_2(folio); /* [DEPRECATED] */ 551 ret = nfs_wb_folio(inode, folio); 552 trace_nfs_launder_folio_done(inode, folio_pos(folio), 553 folio_size(folio), ret); 554 return ret; 555 } 556 557 static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file, 558 sector_t *span) 559 { 560 unsigned long blocks; 561 long long isize; 562 int ret; 563 struct inode *inode = file_inode(file); 564 struct rpc_clnt *clnt = NFS_CLIENT(inode); 565 struct nfs_client *cl = NFS_SERVER(inode)->nfs_client; 566 567 spin_lock(&inode->i_lock); 568 blocks = inode->i_blocks; 569 isize = inode->i_size; 570 spin_unlock(&inode->i_lock); 571 if (blocks*512 < isize) { 572 pr_warn("swap activate: swapfile has holes\n"); 573 return -EINVAL; 574 } 575 576 ret = rpc_clnt_swap_activate(clnt); 577 if (ret) 578 return ret; 579 ret = add_swap_extent(sis, 0, sis->max, 0); 580 if (ret < 0) { 581 rpc_clnt_swap_deactivate(clnt); 582 return ret; 583 } 584 585 *span = sis->pages; 586 587 if (cl->rpc_ops->enable_swap) 588 cl->rpc_ops->enable_swap(inode); 589 590 sis->flags |= SWP_FS_OPS; 591 return ret; 592 } 593 594 static void nfs_swap_deactivate(struct file *file) 595 { 596 struct inode *inode = file_inode(file); 597 struct rpc_clnt *clnt = NFS_CLIENT(inode); 598 struct nfs_client *cl = NFS_SERVER(inode)->nfs_client; 599 600 rpc_clnt_swap_deactivate(clnt); 601 if (cl->rpc_ops->disable_swap) 602 cl->rpc_ops->disable_swap(file_inode(file)); 603 } 604 605 const struct address_space_operations nfs_file_aops = { 606 .read_folio = nfs_read_folio, 607 .readahead = nfs_readahead, 608 .dirty_folio = filemap_dirty_folio, 609 .writepages = nfs_writepages, 610 .write_begin = nfs_write_begin, 611 .write_end = nfs_write_end, 612 .invalidate_folio = nfs_invalidate_folio, 613 .release_folio = nfs_release_folio, 614 .migrate_folio = nfs_migrate_folio, 615 .launder_folio = nfs_launder_folio, 616 .is_dirty_writeback = nfs_check_dirty_writeback, 617 .error_remove_folio = generic_error_remove_folio, 618 .swap_activate = nfs_swap_activate, 619 .swap_deactivate = nfs_swap_deactivate, 620 .swap_rw = nfs_swap_rw, 621 }; 622 623 /* 624 * Notification that a PTE pointing to an NFS page is about to be made 625 * writable, implying that someone is about to modify the page through a 626 * shared-writable mapping 627 */ 628 static vm_fault_t nfs_vm_page_mkwrite(struct vm_fault *vmf) 629 { 630 struct file *filp = vmf->vma->vm_file; 631 struct inode *inode = file_inode(filp); 632 unsigned pagelen; 633 vm_fault_t ret = VM_FAULT_NOPAGE; 634 struct address_space *mapping; 635 struct folio *folio = page_folio(vmf->page); 636 637 dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%pD2(%lu), offset %lld)\n", 638 filp, filp->f_mapping->host->i_ino, 639 (long long)folio_pos(folio)); 640 641 sb_start_pagefault(inode->i_sb); 642 643 /* make sure the cache has finished storing the page */ 644 if (folio_test_private_2(folio) && /* [DEPRECATED] */ 645 folio_wait_private_2_killable(folio) < 0) { 646 ret = VM_FAULT_RETRY; 647 goto out; 648 } 649 650 wait_on_bit_action(&NFS_I(inode)->flags, NFS_INO_INVALIDATING, 651 nfs_wait_bit_killable, 652 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE); 653 654 folio_lock(folio); 655 mapping = folio->mapping; 656 if (mapping != inode->i_mapping) 657 goto out_unlock; 658 659 folio_wait_writeback(folio); 660 661 pagelen = nfs_folio_length(folio); 662 if (pagelen == 0) 663 goto out_unlock; 664 665 ret = VM_FAULT_LOCKED; 666 if (nfs_flush_incompatible(filp, folio) == 0 && 667 nfs_update_folio(filp, folio, 0, pagelen) == 0) 668 goto out; 669 670 ret = VM_FAULT_SIGBUS; 671 out_unlock: 672 folio_unlock(folio); 673 out: 674 sb_end_pagefault(inode->i_sb); 675 return ret; 676 } 677 678 static const struct vm_operations_struct nfs_file_vm_ops = { 679 .fault = filemap_fault, 680 .map_pages = filemap_map_pages, 681 .page_mkwrite = nfs_vm_page_mkwrite, 682 }; 683 684 ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from) 685 { 686 struct file *file = iocb->ki_filp; 687 struct inode *inode = file_inode(file); 688 unsigned int mntflags = NFS_SERVER(inode)->flags; 689 ssize_t result, written; 690 errseq_t since; 691 int error; 692 693 result = nfs_key_timeout_notify(file, inode); 694 if (result) 695 return result; 696 697 if (iocb->ki_flags & IOCB_DIRECT) 698 return nfs_file_direct_write(iocb, from, false); 699 700 dprintk("NFS: write(%pD2, %zu@%Ld)\n", 701 file, iov_iter_count(from), (long long) iocb->ki_pos); 702 703 if (IS_SWAPFILE(inode)) 704 goto out_swapfile; 705 /* 706 * O_APPEND implies that we must revalidate the file length. 707 */ 708 if (iocb->ki_flags & IOCB_APPEND || iocb->ki_pos > i_size_read(inode)) { 709 result = nfs_revalidate_file_size(inode, file); 710 if (result) 711 return result; 712 } 713 714 nfs_clear_invalid_mapping(file->f_mapping); 715 716 since = filemap_sample_wb_err(file->f_mapping); 717 error = nfs_start_io_write(inode); 718 if (error) 719 return error; 720 result = generic_write_checks(iocb, from); 721 if (result > 0) 722 result = generic_perform_write(iocb, from); 723 nfs_end_io_write(inode); 724 if (result <= 0) 725 goto out; 726 727 written = result; 728 nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written); 729 730 if (mntflags & NFS_MOUNT_WRITE_EAGER) { 731 result = filemap_fdatawrite_range(file->f_mapping, 732 iocb->ki_pos - written, 733 iocb->ki_pos - 1); 734 if (result < 0) 735 goto out; 736 } 737 if (mntflags & NFS_MOUNT_WRITE_WAIT) { 738 filemap_fdatawait_range(file->f_mapping, 739 iocb->ki_pos - written, 740 iocb->ki_pos - 1); 741 } 742 result = generic_write_sync(iocb, written); 743 if (result < 0) 744 return result; 745 746 out: 747 /* Return error values */ 748 error = filemap_check_wb_err(file->f_mapping, since); 749 switch (error) { 750 default: 751 break; 752 case -EDQUOT: 753 case -EFBIG: 754 case -ENOSPC: 755 nfs_wb_all(inode); 756 error = file_check_and_advance_wb_err(file); 757 if (error < 0) 758 result = error; 759 } 760 return result; 761 762 out_swapfile: 763 printk(KERN_INFO "NFS: attempt to write to active swap file!\n"); 764 return -ETXTBSY; 765 } 766 EXPORT_SYMBOL_GPL(nfs_file_write); 767 768 static int 769 do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 770 { 771 struct inode *inode = filp->f_mapping->host; 772 int status = 0; 773 unsigned int saved_type = fl->c.flc_type; 774 775 /* Try local locking first */ 776 posix_test_lock(filp, fl); 777 if (fl->c.flc_type != F_UNLCK) { 778 /* found a conflict */ 779 goto out; 780 } 781 fl->c.flc_type = saved_type; 782 783 if (nfs_have_read_or_write_delegation(inode)) 784 goto out_noconflict; 785 786 if (is_local) 787 goto out_noconflict; 788 789 status = NFS_PROTO(inode)->lock(filp, cmd, fl); 790 out: 791 return status; 792 out_noconflict: 793 fl->c.flc_type = F_UNLCK; 794 goto out; 795 } 796 797 static int 798 do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 799 { 800 struct inode *inode = filp->f_mapping->host; 801 struct nfs_lock_context *l_ctx; 802 int status; 803 804 /* 805 * Flush all pending writes before doing anything 806 * with locks.. 807 */ 808 nfs_wb_all(inode); 809 810 l_ctx = nfs_get_lock_context(nfs_file_open_context(filp)); 811 if (!IS_ERR(l_ctx)) { 812 status = nfs_iocounter_wait(l_ctx); 813 nfs_put_lock_context(l_ctx); 814 /* NOTE: special case 815 * If we're signalled while cleaning up locks on process exit, we 816 * still need to complete the unlock. 817 */ 818 if (status < 0 && !(fl->c.flc_flags & FL_CLOSE)) 819 return status; 820 } 821 822 /* 823 * Use local locking if mounted with "-onolock" or with appropriate 824 * "-olocal_lock=" 825 */ 826 if (!is_local) 827 status = NFS_PROTO(inode)->lock(filp, cmd, fl); 828 else 829 status = locks_lock_file_wait(filp, fl); 830 return status; 831 } 832 833 static int 834 do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local) 835 { 836 struct inode *inode = filp->f_mapping->host; 837 int status; 838 839 /* 840 * Flush all pending writes before doing anything 841 * with locks.. 842 */ 843 status = nfs_sync_mapping(filp->f_mapping); 844 if (status != 0) 845 goto out; 846 847 /* 848 * Use local locking if mounted with "-onolock" or with appropriate 849 * "-olocal_lock=" 850 */ 851 if (!is_local) 852 status = NFS_PROTO(inode)->lock(filp, cmd, fl); 853 else 854 status = locks_lock_file_wait(filp, fl); 855 if (status < 0) 856 goto out; 857 858 /* 859 * Invalidate cache to prevent missing any changes. If 860 * the file is mapped, clear the page cache as well so 861 * those mappings will be loaded. 862 * 863 * This makes locking act as a cache coherency point. 864 */ 865 nfs_sync_mapping(filp->f_mapping); 866 if (!nfs_have_read_or_write_delegation(inode)) { 867 nfs_zap_caches(inode); 868 if (mapping_mapped(filp->f_mapping)) 869 nfs_revalidate_mapping(inode, filp->f_mapping); 870 } 871 out: 872 return status; 873 } 874 875 /* 876 * Lock a (portion of) a file 877 */ 878 int nfs_lock(struct file *filp, int cmd, struct file_lock *fl) 879 { 880 struct inode *inode = filp->f_mapping->host; 881 int ret = -ENOLCK; 882 int is_local = 0; 883 884 dprintk("NFS: lock(%pD2, t=%x, fl=%x, r=%lld:%lld)\n", 885 filp, fl->c.flc_type, fl->c.flc_flags, 886 (long long)fl->fl_start, (long long)fl->fl_end); 887 888 nfs_inc_stats(inode, NFSIOS_VFSLOCK); 889 890 if (fl->c.flc_flags & FL_RECLAIM) 891 return -ENOGRACE; 892 893 if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL) 894 is_local = 1; 895 896 if (NFS_PROTO(inode)->lock_check_bounds != NULL) { 897 ret = NFS_PROTO(inode)->lock_check_bounds(fl); 898 if (ret < 0) 899 goto out_err; 900 } 901 902 if (IS_GETLK(cmd)) 903 ret = do_getlk(filp, cmd, fl, is_local); 904 else if (lock_is_unlock(fl)) 905 ret = do_unlk(filp, cmd, fl, is_local); 906 else 907 ret = do_setlk(filp, cmd, fl, is_local); 908 out_err: 909 return ret; 910 } 911 EXPORT_SYMBOL_GPL(nfs_lock); 912 913 /* 914 * Lock a (portion of) a file 915 */ 916 int nfs_flock(struct file *filp, int cmd, struct file_lock *fl) 917 { 918 struct inode *inode = filp->f_mapping->host; 919 int is_local = 0; 920 921 dprintk("NFS: flock(%pD2, t=%x, fl=%x)\n", 922 filp, fl->c.flc_type, fl->c.flc_flags); 923 924 if (!(fl->c.flc_flags & FL_FLOCK)) 925 return -ENOLCK; 926 927 if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK) 928 is_local = 1; 929 930 /* We're simulating flock() locks using posix locks on the server */ 931 if (lock_is_unlock(fl)) 932 return do_unlk(filp, cmd, fl, is_local); 933 return do_setlk(filp, cmd, fl, is_local); 934 } 935 EXPORT_SYMBOL_GPL(nfs_flock); 936 937 const struct file_operations nfs_file_operations = { 938 .llseek = nfs_file_llseek, 939 .read_iter = nfs_file_read, 940 .write_iter = nfs_file_write, 941 .mmap_prepare = nfs_file_mmap_prepare, 942 .open = nfs_file_open, 943 .flush = nfs_file_flush, 944 .release = nfs_file_release, 945 .fsync = nfs_file_fsync, 946 .lock = nfs_lock, 947 .flock = nfs_flock, 948 .splice_read = nfs_file_splice_read, 949 .splice_write = iter_file_splice_write, 950 .check_flags = nfs_check_flags, 951 .setlease = simple_nosetlease, 952 }; 953 EXPORT_SYMBOL_GPL(nfs_file_operations); 954