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