1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/fs/nfs/dir.c 4 * 5 * Copyright (C) 1992 Rick Sladkey 6 * 7 * nfs directory handling functions 8 * 9 * 10 Apr 1996 Added silly rename for unlink --okir 10 * 28 Sep 1996 Improved directory cache --okir 11 * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de 12 * Re-implemented silly rename for unlink, newly implemented 13 * silly rename for nfs_rename() following the suggestions 14 * of Olaf Kirch (okir) found in this file. 15 * Following Linus comments on my original hack, this version 16 * depends only on the dcache stuff and doesn't touch the inode 17 * layer (iput() and friends). 18 * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM 19 */ 20 21 #include <linux/module.h> 22 #include <linux/time.h> 23 #include <linux/errno.h> 24 #include <linux/stat.h> 25 #include <linux/fcntl.h> 26 #include <linux/string.h> 27 #include <linux/kernel.h> 28 #include <linux/slab.h> 29 #include <linux/mm.h> 30 #include <linux/sunrpc/clnt.h> 31 #include <linux/nfs_fs.h> 32 #include <linux/nfs_mount.h> 33 #include <linux/pagemap.h> 34 #include <linux/pagevec.h> 35 #include <linux/namei.h> 36 #include <linux/mount.h> 37 #include <linux/swap.h> 38 #include <linux/sched.h> 39 #include <linux/kmemleak.h> 40 #include <linux/xattr.h> 41 42 #include "delegation.h" 43 #include "iostat.h" 44 #include "internal.h" 45 #include "fscache.h" 46 47 #include "nfstrace.h" 48 49 /* #define NFS_DEBUG_VERBOSE 1 */ 50 51 static int nfs_opendir(struct inode *, struct file *); 52 static int nfs_closedir(struct inode *, struct file *); 53 static int nfs_readdir(struct file *, struct dir_context *); 54 static int nfs_fsync_dir(struct file *, loff_t, loff_t, int); 55 static loff_t nfs_llseek_dir(struct file *, loff_t, int); 56 static void nfs_readdir_clear_array(struct page*); 57 58 const struct file_operations nfs_dir_operations = { 59 .llseek = nfs_llseek_dir, 60 .read = generic_read_dir, 61 .iterate_shared = nfs_readdir, 62 .open = nfs_opendir, 63 .release = nfs_closedir, 64 .fsync = nfs_fsync_dir, 65 }; 66 67 const struct address_space_operations nfs_dir_aops = { 68 .freepage = nfs_readdir_clear_array, 69 }; 70 71 static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir) 72 { 73 struct nfs_inode *nfsi = NFS_I(dir); 74 struct nfs_open_dir_context *ctx; 75 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); 76 if (ctx != NULL) { 77 ctx->duped = 0; 78 ctx->attr_gencount = nfsi->attr_gencount; 79 ctx->dir_cookie = 0; 80 ctx->dup_cookie = 0; 81 ctx->page_index = 0; 82 spin_lock(&dir->i_lock); 83 if (list_empty(&nfsi->open_files) && 84 (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER)) 85 nfs_set_cache_invalid(dir, 86 NFS_INO_INVALID_DATA | 87 NFS_INO_REVAL_FORCED); 88 list_add(&ctx->list, &nfsi->open_files); 89 clear_bit(NFS_INO_FORCE_READDIR, &nfsi->flags); 90 spin_unlock(&dir->i_lock); 91 return ctx; 92 } 93 return ERR_PTR(-ENOMEM); 94 } 95 96 static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx) 97 { 98 spin_lock(&dir->i_lock); 99 list_del(&ctx->list); 100 spin_unlock(&dir->i_lock); 101 kfree(ctx); 102 } 103 104 /* 105 * Open file 106 */ 107 static int 108 nfs_opendir(struct inode *inode, struct file *filp) 109 { 110 int res = 0; 111 struct nfs_open_dir_context *ctx; 112 113 dfprintk(FILE, "NFS: open dir(%pD2)\n", filp); 114 115 nfs_inc_stats(inode, NFSIOS_VFSOPEN); 116 117 ctx = alloc_nfs_open_dir_context(inode); 118 if (IS_ERR(ctx)) { 119 res = PTR_ERR(ctx); 120 goto out; 121 } 122 filp->private_data = ctx; 123 out: 124 return res; 125 } 126 127 static int 128 nfs_closedir(struct inode *inode, struct file *filp) 129 { 130 put_nfs_open_dir_context(file_inode(filp), filp->private_data); 131 return 0; 132 } 133 134 struct nfs_cache_array_entry { 135 u64 cookie; 136 u64 ino; 137 const char *name; 138 unsigned int name_len; 139 unsigned char d_type; 140 }; 141 142 struct nfs_cache_array { 143 u64 last_cookie; 144 unsigned int size; 145 unsigned char page_full : 1, 146 page_is_eof : 1, 147 cookies_are_ordered : 1; 148 struct nfs_cache_array_entry array[]; 149 }; 150 151 struct nfs_readdir_descriptor { 152 struct file *file; 153 struct page *page; 154 struct dir_context *ctx; 155 pgoff_t page_index; 156 u64 dir_cookie; 157 u64 last_cookie; 158 u64 dup_cookie; 159 loff_t current_index; 160 loff_t prev_index; 161 162 __be32 verf[NFS_DIR_VERIFIER_SIZE]; 163 unsigned long dir_verifier; 164 unsigned long timestamp; 165 unsigned long gencount; 166 unsigned long attr_gencount; 167 unsigned int cache_entry_index; 168 signed char duped; 169 bool plus; 170 bool eof; 171 }; 172 173 static void nfs_readdir_array_init(struct nfs_cache_array *array) 174 { 175 memset(array, 0, sizeof(struct nfs_cache_array)); 176 } 177 178 static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie) 179 { 180 struct nfs_cache_array *array; 181 182 array = kmap_atomic(page); 183 nfs_readdir_array_init(array); 184 array->last_cookie = last_cookie; 185 array->cookies_are_ordered = 1; 186 kunmap_atomic(array); 187 } 188 189 /* 190 * we are freeing strings created by nfs_add_to_readdir_array() 191 */ 192 static 193 void nfs_readdir_clear_array(struct page *page) 194 { 195 struct nfs_cache_array *array; 196 int i; 197 198 array = kmap_atomic(page); 199 for (i = 0; i < array->size; i++) 200 kfree(array->array[i].name); 201 nfs_readdir_array_init(array); 202 kunmap_atomic(array); 203 } 204 205 static struct page * 206 nfs_readdir_page_array_alloc(u64 last_cookie, gfp_t gfp_flags) 207 { 208 struct page *page = alloc_page(gfp_flags); 209 if (page) 210 nfs_readdir_page_init_array(page, last_cookie); 211 return page; 212 } 213 214 static void nfs_readdir_page_array_free(struct page *page) 215 { 216 if (page) { 217 nfs_readdir_clear_array(page); 218 put_page(page); 219 } 220 } 221 222 static void nfs_readdir_array_set_eof(struct nfs_cache_array *array) 223 { 224 array->page_is_eof = 1; 225 array->page_full = 1; 226 } 227 228 static bool nfs_readdir_array_is_full(struct nfs_cache_array *array) 229 { 230 return array->page_full; 231 } 232 233 /* 234 * the caller is responsible for freeing qstr.name 235 * when called by nfs_readdir_add_to_array, the strings will be freed in 236 * nfs_clear_readdir_array() 237 */ 238 static const char *nfs_readdir_copy_name(const char *name, unsigned int len) 239 { 240 const char *ret = kmemdup_nul(name, len, GFP_KERNEL); 241 242 /* 243 * Avoid a kmemleak false positive. The pointer to the name is stored 244 * in a page cache page which kmemleak does not scan. 245 */ 246 if (ret != NULL) 247 kmemleak_not_leak(ret); 248 return ret; 249 } 250 251 /* 252 * Check that the next array entry lies entirely within the page bounds 253 */ 254 static int nfs_readdir_array_can_expand(struct nfs_cache_array *array) 255 { 256 struct nfs_cache_array_entry *cache_entry; 257 258 if (array->page_full) 259 return -ENOSPC; 260 cache_entry = &array->array[array->size + 1]; 261 if ((char *)cache_entry - (char *)array > PAGE_SIZE) { 262 array->page_full = 1; 263 return -ENOSPC; 264 } 265 return 0; 266 } 267 268 static 269 int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) 270 { 271 struct nfs_cache_array *array; 272 struct nfs_cache_array_entry *cache_entry; 273 const char *name; 274 int ret; 275 276 name = nfs_readdir_copy_name(entry->name, entry->len); 277 if (!name) 278 return -ENOMEM; 279 280 array = kmap_atomic(page); 281 ret = nfs_readdir_array_can_expand(array); 282 if (ret) { 283 kfree(name); 284 goto out; 285 } 286 287 cache_entry = &array->array[array->size]; 288 cache_entry->cookie = entry->prev_cookie; 289 cache_entry->ino = entry->ino; 290 cache_entry->d_type = entry->d_type; 291 cache_entry->name_len = entry->len; 292 cache_entry->name = name; 293 array->last_cookie = entry->cookie; 294 if (array->last_cookie <= cache_entry->cookie) 295 array->cookies_are_ordered = 0; 296 array->size++; 297 if (entry->eof != 0) 298 nfs_readdir_array_set_eof(array); 299 out: 300 kunmap_atomic(array); 301 return ret; 302 } 303 304 static struct page *nfs_readdir_page_get_locked(struct address_space *mapping, 305 pgoff_t index, u64 last_cookie) 306 { 307 struct page *page; 308 309 page = grab_cache_page(mapping, index); 310 if (page && !PageUptodate(page)) { 311 nfs_readdir_page_init_array(page, last_cookie); 312 if (invalidate_inode_pages2_range(mapping, index + 1, -1) < 0) 313 nfs_zap_mapping(mapping->host, mapping); 314 SetPageUptodate(page); 315 } 316 317 return page; 318 } 319 320 static u64 nfs_readdir_page_last_cookie(struct page *page) 321 { 322 struct nfs_cache_array *array; 323 u64 ret; 324 325 array = kmap_atomic(page); 326 ret = array->last_cookie; 327 kunmap_atomic(array); 328 return ret; 329 } 330 331 static bool nfs_readdir_page_needs_filling(struct page *page) 332 { 333 struct nfs_cache_array *array; 334 bool ret; 335 336 array = kmap_atomic(page); 337 ret = !nfs_readdir_array_is_full(array); 338 kunmap_atomic(array); 339 return ret; 340 } 341 342 static void nfs_readdir_page_set_eof(struct page *page) 343 { 344 struct nfs_cache_array *array; 345 346 array = kmap_atomic(page); 347 nfs_readdir_array_set_eof(array); 348 kunmap_atomic(array); 349 } 350 351 static void nfs_readdir_page_unlock_and_put(struct page *page) 352 { 353 unlock_page(page); 354 put_page(page); 355 } 356 357 static struct page *nfs_readdir_page_get_next(struct address_space *mapping, 358 pgoff_t index, u64 cookie) 359 { 360 struct page *page; 361 362 page = nfs_readdir_page_get_locked(mapping, index, cookie); 363 if (page) { 364 if (nfs_readdir_page_last_cookie(page) == cookie) 365 return page; 366 nfs_readdir_page_unlock_and_put(page); 367 } 368 return NULL; 369 } 370 371 static inline 372 int is_32bit_api(void) 373 { 374 #ifdef CONFIG_COMPAT 375 return in_compat_syscall(); 376 #else 377 return (BITS_PER_LONG == 32); 378 #endif 379 } 380 381 static 382 bool nfs_readdir_use_cookie(const struct file *filp) 383 { 384 if ((filp->f_mode & FMODE_32BITHASH) || 385 (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api())) 386 return false; 387 return true; 388 } 389 390 static int nfs_readdir_search_for_pos(struct nfs_cache_array *array, 391 struct nfs_readdir_descriptor *desc) 392 { 393 loff_t diff = desc->ctx->pos - desc->current_index; 394 unsigned int index; 395 396 if (diff < 0) 397 goto out_eof; 398 if (diff >= array->size) { 399 if (array->page_is_eof) 400 goto out_eof; 401 return -EAGAIN; 402 } 403 404 index = (unsigned int)diff; 405 desc->dir_cookie = array->array[index].cookie; 406 desc->cache_entry_index = index; 407 return 0; 408 out_eof: 409 desc->eof = true; 410 return -EBADCOOKIE; 411 } 412 413 static bool 414 nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi) 415 { 416 if (nfsi->cache_validity & (NFS_INO_INVALID_CHANGE | 417 NFS_INO_INVALID_DATA)) 418 return false; 419 smp_rmb(); 420 return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags); 421 } 422 423 static bool nfs_readdir_array_cookie_in_range(struct nfs_cache_array *array, 424 u64 cookie) 425 { 426 if (!array->cookies_are_ordered) 427 return true; 428 /* Optimisation for monotonically increasing cookies */ 429 if (cookie >= array->last_cookie) 430 return false; 431 if (array->size && cookie < array->array[0].cookie) 432 return false; 433 return true; 434 } 435 436 static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, 437 struct nfs_readdir_descriptor *desc) 438 { 439 int i; 440 loff_t new_pos; 441 int status = -EAGAIN; 442 443 if (!nfs_readdir_array_cookie_in_range(array, desc->dir_cookie)) 444 goto check_eof; 445 446 for (i = 0; i < array->size; i++) { 447 if (array->array[i].cookie == desc->dir_cookie) { 448 struct nfs_inode *nfsi = NFS_I(file_inode(desc->file)); 449 450 new_pos = desc->current_index + i; 451 if (desc->attr_gencount != nfsi->attr_gencount || 452 !nfs_readdir_inode_mapping_valid(nfsi)) { 453 desc->duped = 0; 454 desc->attr_gencount = nfsi->attr_gencount; 455 } else if (new_pos < desc->prev_index) { 456 if (desc->duped > 0 457 && desc->dup_cookie == desc->dir_cookie) { 458 if (printk_ratelimit()) { 459 pr_notice("NFS: directory %pD2 contains a readdir loop." 460 "Please contact your server vendor. " 461 "The file: %s has duplicate cookie %llu\n", 462 desc->file, array->array[i].name, desc->dir_cookie); 463 } 464 status = -ELOOP; 465 goto out; 466 } 467 desc->dup_cookie = desc->dir_cookie; 468 desc->duped = -1; 469 } 470 if (nfs_readdir_use_cookie(desc->file)) 471 desc->ctx->pos = desc->dir_cookie; 472 else 473 desc->ctx->pos = new_pos; 474 desc->prev_index = new_pos; 475 desc->cache_entry_index = i; 476 return 0; 477 } 478 } 479 check_eof: 480 if (array->page_is_eof) { 481 status = -EBADCOOKIE; 482 if (desc->dir_cookie == array->last_cookie) 483 desc->eof = true; 484 } 485 out: 486 return status; 487 } 488 489 static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc) 490 { 491 struct nfs_cache_array *array; 492 int status; 493 494 array = kmap_atomic(desc->page); 495 496 if (desc->dir_cookie == 0) 497 status = nfs_readdir_search_for_pos(array, desc); 498 else 499 status = nfs_readdir_search_for_cookie(array, desc); 500 501 if (status == -EAGAIN) { 502 desc->last_cookie = array->last_cookie; 503 desc->current_index += array->size; 504 desc->page_index++; 505 } 506 kunmap_atomic(array); 507 return status; 508 } 509 510 /* Fill a page with xdr information before transferring to the cache page */ 511 static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc, 512 __be32 *verf, u64 cookie, 513 struct page **pages, size_t bufsize, 514 __be32 *verf_res) 515 { 516 struct inode *inode = file_inode(desc->file); 517 struct nfs_readdir_arg arg = { 518 .dentry = file_dentry(desc->file), 519 .cred = desc->file->f_cred, 520 .verf = verf, 521 .cookie = cookie, 522 .pages = pages, 523 .page_len = bufsize, 524 .plus = desc->plus, 525 }; 526 struct nfs_readdir_res res = { 527 .verf = verf_res, 528 }; 529 unsigned long timestamp, gencount; 530 int error; 531 532 again: 533 timestamp = jiffies; 534 gencount = nfs_inc_attr_generation_counter(); 535 desc->dir_verifier = nfs_save_change_attribute(inode); 536 error = NFS_PROTO(inode)->readdir(&arg, &res); 537 if (error < 0) { 538 /* We requested READDIRPLUS, but the server doesn't grok it */ 539 if (error == -ENOTSUPP && desc->plus) { 540 NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; 541 clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); 542 desc->plus = arg.plus = false; 543 goto again; 544 } 545 goto error; 546 } 547 desc->timestamp = timestamp; 548 desc->gencount = gencount; 549 error: 550 return error; 551 } 552 553 static int xdr_decode(struct nfs_readdir_descriptor *desc, 554 struct nfs_entry *entry, struct xdr_stream *xdr) 555 { 556 struct inode *inode = file_inode(desc->file); 557 int error; 558 559 error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus); 560 if (error) 561 return error; 562 entry->fattr->time_start = desc->timestamp; 563 entry->fattr->gencount = desc->gencount; 564 return 0; 565 } 566 567 /* Match file and dirent using either filehandle or fileid 568 * Note: caller is responsible for checking the fsid 569 */ 570 static 571 int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) 572 { 573 struct inode *inode; 574 struct nfs_inode *nfsi; 575 576 if (d_really_is_negative(dentry)) 577 return 0; 578 579 inode = d_inode(dentry); 580 if (is_bad_inode(inode) || NFS_STALE(inode)) 581 return 0; 582 583 nfsi = NFS_I(inode); 584 if (entry->fattr->fileid != nfsi->fileid) 585 return 0; 586 if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0) 587 return 0; 588 return 1; 589 } 590 591 static 592 bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx) 593 { 594 if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) 595 return false; 596 if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags)) 597 return true; 598 if (ctx->pos == 0) 599 return true; 600 return false; 601 } 602 603 /* 604 * This function is called by the lookup and getattr code to request the 605 * use of readdirplus to accelerate any future lookups in the same 606 * directory. 607 */ 608 void nfs_advise_use_readdirplus(struct inode *dir) 609 { 610 struct nfs_inode *nfsi = NFS_I(dir); 611 612 if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 613 !list_empty(&nfsi->open_files)) 614 set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 615 } 616 617 /* 618 * This function is mainly for use by nfs_getattr(). 619 * 620 * If this is an 'ls -l', we want to force use of readdirplus. 621 * Do this by checking if there is an active file descriptor 622 * and calling nfs_advise_use_readdirplus, then forcing a 623 * cache flush. 624 */ 625 void nfs_force_use_readdirplus(struct inode *dir) 626 { 627 struct nfs_inode *nfsi = NFS_I(dir); 628 629 if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && 630 !list_empty(&nfsi->open_files)) { 631 set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 632 set_bit(NFS_INO_FORCE_READDIR, &nfsi->flags); 633 } 634 } 635 636 static 637 void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry, 638 unsigned long dir_verifier) 639 { 640 struct qstr filename = QSTR_INIT(entry->name, entry->len); 641 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 642 struct dentry *dentry; 643 struct dentry *alias; 644 struct inode *inode; 645 int status; 646 647 if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) 648 return; 649 if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) 650 return; 651 if (filename.len == 0) 652 return; 653 /* Validate that the name doesn't contain any illegal '\0' */ 654 if (strnlen(filename.name, filename.len) != filename.len) 655 return; 656 /* ...or '/' */ 657 if (strnchr(filename.name, filename.len, '/')) 658 return; 659 if (filename.name[0] == '.') { 660 if (filename.len == 1) 661 return; 662 if (filename.len == 2 && filename.name[1] == '.') 663 return; 664 } 665 filename.hash = full_name_hash(parent, filename.name, filename.len); 666 667 dentry = d_lookup(parent, &filename); 668 again: 669 if (!dentry) { 670 dentry = d_alloc_parallel(parent, &filename, &wq); 671 if (IS_ERR(dentry)) 672 return; 673 } 674 if (!d_in_lookup(dentry)) { 675 /* Is there a mountpoint here? If so, just exit */ 676 if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, 677 &entry->fattr->fsid)) 678 goto out; 679 if (nfs_same_file(dentry, entry)) { 680 if (!entry->fh->size) 681 goto out; 682 nfs_set_verifier(dentry, dir_verifier); 683 status = nfs_refresh_inode(d_inode(dentry), entry->fattr); 684 if (!status) 685 nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label); 686 goto out; 687 } else { 688 d_invalidate(dentry); 689 dput(dentry); 690 dentry = NULL; 691 goto again; 692 } 693 } 694 if (!entry->fh->size) { 695 d_lookup_done(dentry); 696 goto out; 697 } 698 699 inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label); 700 alias = d_splice_alias(inode, dentry); 701 d_lookup_done(dentry); 702 if (alias) { 703 if (IS_ERR(alias)) 704 goto out; 705 dput(dentry); 706 dentry = alias; 707 } 708 nfs_set_verifier(dentry, dir_verifier); 709 out: 710 dput(dentry); 711 } 712 713 /* Perform conversion from xdr to cache array */ 714 static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc, 715 struct nfs_entry *entry, 716 struct page **xdr_pages, 717 unsigned int buflen, 718 struct page **arrays, 719 size_t narrays) 720 { 721 struct address_space *mapping = desc->file->f_mapping; 722 struct xdr_stream stream; 723 struct xdr_buf buf; 724 struct page *scratch, *new, *page = *arrays; 725 int status; 726 727 scratch = alloc_page(GFP_KERNEL); 728 if (scratch == NULL) 729 return -ENOMEM; 730 731 xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); 732 xdr_set_scratch_page(&stream, scratch); 733 734 do { 735 if (entry->label) 736 entry->label->len = NFS4_MAXLABELLEN; 737 738 status = xdr_decode(desc, entry, &stream); 739 if (status != 0) 740 break; 741 742 if (desc->plus) 743 nfs_prime_dcache(file_dentry(desc->file), entry, 744 desc->dir_verifier); 745 746 status = nfs_readdir_add_to_array(entry, page); 747 if (status != -ENOSPC) 748 continue; 749 750 if (page->mapping != mapping) { 751 if (!--narrays) 752 break; 753 new = nfs_readdir_page_array_alloc(entry->prev_cookie, 754 GFP_KERNEL); 755 if (!new) 756 break; 757 arrays++; 758 *arrays = page = new; 759 } else { 760 new = nfs_readdir_page_get_next(mapping, 761 page->index + 1, 762 entry->prev_cookie); 763 if (!new) 764 break; 765 if (page != *arrays) 766 nfs_readdir_page_unlock_and_put(page); 767 page = new; 768 } 769 status = nfs_readdir_add_to_array(entry, page); 770 } while (!status && !entry->eof); 771 772 switch (status) { 773 case -EBADCOOKIE: 774 if (entry->eof) { 775 nfs_readdir_page_set_eof(page); 776 status = 0; 777 } 778 break; 779 case -ENOSPC: 780 case -EAGAIN: 781 status = 0; 782 break; 783 } 784 785 if (page != *arrays) 786 nfs_readdir_page_unlock_and_put(page); 787 788 put_page(scratch); 789 return status; 790 } 791 792 static void nfs_readdir_free_pages(struct page **pages, size_t npages) 793 { 794 while (npages--) 795 put_page(pages[npages]); 796 kfree(pages); 797 } 798 799 /* 800 * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call 801 * to nfs_readdir_free_pages() 802 */ 803 static struct page **nfs_readdir_alloc_pages(size_t npages) 804 { 805 struct page **pages; 806 size_t i; 807 808 pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL); 809 if (!pages) 810 return NULL; 811 for (i = 0; i < npages; i++) { 812 struct page *page = alloc_page(GFP_KERNEL); 813 if (page == NULL) 814 goto out_freepages; 815 pages[i] = page; 816 } 817 return pages; 818 819 out_freepages: 820 nfs_readdir_free_pages(pages, i); 821 return NULL; 822 } 823 824 static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc, 825 __be32 *verf_arg, __be32 *verf_res, 826 struct page **arrays, size_t narrays) 827 { 828 struct page **pages; 829 struct page *page = *arrays; 830 struct nfs_entry *entry; 831 size_t array_size; 832 struct inode *inode = file_inode(desc->file); 833 size_t dtsize = NFS_SERVER(inode)->dtsize; 834 int status = -ENOMEM; 835 836 entry = kzalloc(sizeof(*entry), GFP_KERNEL); 837 if (!entry) 838 return -ENOMEM; 839 entry->cookie = nfs_readdir_page_last_cookie(page); 840 entry->fh = nfs_alloc_fhandle(); 841 entry->fattr = nfs_alloc_fattr(); 842 entry->server = NFS_SERVER(inode); 843 if (entry->fh == NULL || entry->fattr == NULL) 844 goto out; 845 846 entry->label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); 847 if (IS_ERR(entry->label)) { 848 status = PTR_ERR(entry->label); 849 goto out; 850 } 851 852 array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT; 853 pages = nfs_readdir_alloc_pages(array_size); 854 if (!pages) 855 goto out_release_label; 856 857 do { 858 unsigned int pglen; 859 status = nfs_readdir_xdr_filler(desc, verf_arg, entry->cookie, 860 pages, dtsize, 861 verf_res); 862 if (status < 0) 863 break; 864 865 pglen = status; 866 if (pglen == 0) { 867 nfs_readdir_page_set_eof(page); 868 break; 869 } 870 871 verf_arg = verf_res; 872 873 status = nfs_readdir_page_filler(desc, entry, pages, pglen, 874 arrays, narrays); 875 } while (!status && nfs_readdir_page_needs_filling(page)); 876 877 nfs_readdir_free_pages(pages, array_size); 878 out_release_label: 879 nfs4_label_free(entry->label); 880 out: 881 nfs_free_fattr(entry->fattr); 882 nfs_free_fhandle(entry->fh); 883 kfree(entry); 884 return status; 885 } 886 887 static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc) 888 { 889 put_page(desc->page); 890 desc->page = NULL; 891 } 892 893 static void 894 nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc) 895 { 896 unlock_page(desc->page); 897 nfs_readdir_page_put(desc); 898 } 899 900 static struct page * 901 nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc) 902 { 903 return nfs_readdir_page_get_locked(desc->file->f_mapping, 904 desc->page_index, 905 desc->last_cookie); 906 } 907 908 /* 909 * Returns 0 if desc->dir_cookie was found on page desc->page_index 910 * and locks the page to prevent removal from the page cache. 911 */ 912 static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc) 913 { 914 struct inode *inode = file_inode(desc->file); 915 struct nfs_inode *nfsi = NFS_I(inode); 916 __be32 verf[NFS_DIR_VERIFIER_SIZE]; 917 int res; 918 919 desc->page = nfs_readdir_page_get_cached(desc); 920 if (!desc->page) 921 return -ENOMEM; 922 if (nfs_readdir_page_needs_filling(desc->page)) { 923 res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf, 924 &desc->page, 1); 925 if (res < 0) { 926 nfs_readdir_page_unlock_and_put_cached(desc); 927 if (res == -EBADCOOKIE || res == -ENOTSYNC) { 928 invalidate_inode_pages2(desc->file->f_mapping); 929 desc->page_index = 0; 930 return -EAGAIN; 931 } 932 return res; 933 } 934 /* 935 * Set the cookie verifier if the page cache was empty 936 */ 937 if (desc->page_index == 0) 938 memcpy(nfsi->cookieverf, verf, 939 sizeof(nfsi->cookieverf)); 940 } 941 res = nfs_readdir_search_array(desc); 942 if (res == 0) 943 return 0; 944 nfs_readdir_page_unlock_and_put_cached(desc); 945 return res; 946 } 947 948 static bool nfs_readdir_dont_search_cache(struct nfs_readdir_descriptor *desc) 949 { 950 struct address_space *mapping = desc->file->f_mapping; 951 struct inode *dir = file_inode(desc->file); 952 unsigned int dtsize = NFS_SERVER(dir)->dtsize; 953 loff_t size = i_size_read(dir); 954 955 /* 956 * Default to uncached readdir if the page cache is empty, and 957 * we're looking for a non-zero cookie in a large directory. 958 */ 959 return desc->dir_cookie != 0 && mapping->nrpages == 0 && size > dtsize; 960 } 961 962 /* Search for desc->dir_cookie from the beginning of the page cache */ 963 static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc) 964 { 965 int res; 966 967 if (nfs_readdir_dont_search_cache(desc)) 968 return -EBADCOOKIE; 969 970 do { 971 if (desc->page_index == 0) { 972 desc->current_index = 0; 973 desc->prev_index = 0; 974 desc->last_cookie = 0; 975 } 976 res = find_and_lock_cache_page(desc); 977 } while (res == -EAGAIN); 978 return res; 979 } 980 981 /* 982 * Once we've found the start of the dirent within a page: fill 'er up... 983 */ 984 static void nfs_do_filldir(struct nfs_readdir_descriptor *desc, 985 const __be32 *verf) 986 { 987 struct file *file = desc->file; 988 struct nfs_cache_array *array; 989 unsigned int i = 0; 990 991 array = kmap(desc->page); 992 for (i = desc->cache_entry_index; i < array->size; i++) { 993 struct nfs_cache_array_entry *ent; 994 995 ent = &array->array[i]; 996 if (!dir_emit(desc->ctx, ent->name, ent->name_len, 997 nfs_compat_user_ino64(ent->ino), ent->d_type)) { 998 desc->eof = true; 999 break; 1000 } 1001 memcpy(desc->verf, verf, sizeof(desc->verf)); 1002 if (i < (array->size-1)) 1003 desc->dir_cookie = array->array[i+1].cookie; 1004 else 1005 desc->dir_cookie = array->last_cookie; 1006 if (nfs_readdir_use_cookie(file)) 1007 desc->ctx->pos = desc->dir_cookie; 1008 else 1009 desc->ctx->pos++; 1010 if (desc->duped != 0) 1011 desc->duped = 1; 1012 } 1013 if (array->page_is_eof) 1014 desc->eof = true; 1015 1016 kunmap(desc->page); 1017 dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n", 1018 (unsigned long long)desc->dir_cookie); 1019 } 1020 1021 /* 1022 * If we cannot find a cookie in our cache, we suspect that this is 1023 * because it points to a deleted file, so we ask the server to return 1024 * whatever it thinks is the next entry. We then feed this to filldir. 1025 * If all goes well, we should then be able to find our way round the 1026 * cache on the next call to readdir_search_pagecache(); 1027 * 1028 * NOTE: we cannot add the anonymous page to the pagecache because 1029 * the data it contains might not be page aligned. Besides, 1030 * we should already have a complete representation of the 1031 * directory in the page cache by the time we get here. 1032 */ 1033 static int uncached_readdir(struct nfs_readdir_descriptor *desc) 1034 { 1035 struct page **arrays; 1036 size_t i, sz = 512; 1037 __be32 verf[NFS_DIR_VERIFIER_SIZE]; 1038 int status = -ENOMEM; 1039 1040 dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %llu\n", 1041 (unsigned long long)desc->dir_cookie); 1042 1043 arrays = kcalloc(sz, sizeof(*arrays), GFP_KERNEL); 1044 if (!arrays) 1045 goto out; 1046 arrays[0] = nfs_readdir_page_array_alloc(desc->dir_cookie, GFP_KERNEL); 1047 if (!arrays[0]) 1048 goto out; 1049 1050 desc->page_index = 0; 1051 desc->last_cookie = desc->dir_cookie; 1052 desc->duped = 0; 1053 1054 status = nfs_readdir_xdr_to_array(desc, desc->verf, verf, arrays, sz); 1055 1056 for (i = 0; !desc->eof && i < sz && arrays[i]; i++) { 1057 desc->page = arrays[i]; 1058 nfs_do_filldir(desc, verf); 1059 } 1060 desc->page = NULL; 1061 1062 1063 for (i = 0; i < sz && arrays[i]; i++) 1064 nfs_readdir_page_array_free(arrays[i]); 1065 out: 1066 kfree(arrays); 1067 dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status); 1068 return status; 1069 } 1070 1071 /* The file offset position represents the dirent entry number. A 1072 last cookie cache takes care of the common case of reading the 1073 whole directory. 1074 */ 1075 static int nfs_readdir(struct file *file, struct dir_context *ctx) 1076 { 1077 struct dentry *dentry = file_dentry(file); 1078 struct inode *inode = d_inode(dentry); 1079 struct nfs_inode *nfsi = NFS_I(inode); 1080 struct nfs_open_dir_context *dir_ctx = file->private_data; 1081 struct nfs_readdir_descriptor *desc; 1082 pgoff_t page_index; 1083 int res; 1084 1085 dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", 1086 file, (long long)ctx->pos); 1087 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 1088 1089 /* 1090 * ctx->pos points to the dirent entry number. 1091 * *desc->dir_cookie has the cookie for the next entry. We have 1092 * to either find the entry with the appropriate number or 1093 * revalidate the cookie. 1094 */ 1095 if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) { 1096 res = nfs_revalidate_mapping(inode, file->f_mapping); 1097 if (res < 0) 1098 goto out; 1099 } 1100 1101 res = -ENOMEM; 1102 desc = kzalloc(sizeof(*desc), GFP_KERNEL); 1103 if (!desc) 1104 goto out; 1105 desc->file = file; 1106 desc->ctx = ctx; 1107 desc->plus = nfs_use_readdirplus(inode, ctx); 1108 1109 spin_lock(&file->f_lock); 1110 desc->dir_cookie = dir_ctx->dir_cookie; 1111 desc->dup_cookie = dir_ctx->dup_cookie; 1112 desc->duped = dir_ctx->duped; 1113 page_index = dir_ctx->page_index; 1114 desc->attr_gencount = dir_ctx->attr_gencount; 1115 memcpy(desc->verf, dir_ctx->verf, sizeof(desc->verf)); 1116 spin_unlock(&file->f_lock); 1117 1118 if (test_and_clear_bit(NFS_INO_FORCE_READDIR, &nfsi->flags) && 1119 list_is_singular(&nfsi->open_files)) 1120 invalidate_mapping_pages(inode->i_mapping, page_index + 1, -1); 1121 1122 do { 1123 res = readdir_search_pagecache(desc); 1124 1125 if (res == -EBADCOOKIE) { 1126 res = 0; 1127 /* This means either end of directory */ 1128 if (desc->dir_cookie && !desc->eof) { 1129 /* Or that the server has 'lost' a cookie */ 1130 res = uncached_readdir(desc); 1131 if (res == 0) 1132 continue; 1133 if (res == -EBADCOOKIE || res == -ENOTSYNC) 1134 res = 0; 1135 } 1136 break; 1137 } 1138 if (res == -ETOOSMALL && desc->plus) { 1139 clear_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); 1140 nfs_zap_caches(inode); 1141 desc->page_index = 0; 1142 desc->plus = false; 1143 desc->eof = false; 1144 continue; 1145 } 1146 if (res < 0) 1147 break; 1148 1149 nfs_do_filldir(desc, nfsi->cookieverf); 1150 nfs_readdir_page_unlock_and_put_cached(desc); 1151 } while (!desc->eof); 1152 1153 spin_lock(&file->f_lock); 1154 dir_ctx->dir_cookie = desc->dir_cookie; 1155 dir_ctx->dup_cookie = desc->dup_cookie; 1156 dir_ctx->duped = desc->duped; 1157 dir_ctx->attr_gencount = desc->attr_gencount; 1158 dir_ctx->page_index = desc->page_index; 1159 memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf)); 1160 spin_unlock(&file->f_lock); 1161 1162 kfree(desc); 1163 1164 out: 1165 dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); 1166 return res; 1167 } 1168 1169 static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) 1170 { 1171 struct nfs_open_dir_context *dir_ctx = filp->private_data; 1172 1173 dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", 1174 filp, offset, whence); 1175 1176 switch (whence) { 1177 default: 1178 return -EINVAL; 1179 case SEEK_SET: 1180 if (offset < 0) 1181 return -EINVAL; 1182 spin_lock(&filp->f_lock); 1183 break; 1184 case SEEK_CUR: 1185 if (offset == 0) 1186 return filp->f_pos; 1187 spin_lock(&filp->f_lock); 1188 offset += filp->f_pos; 1189 if (offset < 0) { 1190 spin_unlock(&filp->f_lock); 1191 return -EINVAL; 1192 } 1193 } 1194 if (offset != filp->f_pos) { 1195 filp->f_pos = offset; 1196 if (nfs_readdir_use_cookie(filp)) 1197 dir_ctx->dir_cookie = offset; 1198 else 1199 dir_ctx->dir_cookie = 0; 1200 if (offset == 0) 1201 memset(dir_ctx->verf, 0, sizeof(dir_ctx->verf)); 1202 dir_ctx->duped = 0; 1203 } 1204 spin_unlock(&filp->f_lock); 1205 return offset; 1206 } 1207 1208 /* 1209 * All directory operations under NFS are synchronous, so fsync() 1210 * is a dummy operation. 1211 */ 1212 static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, 1213 int datasync) 1214 { 1215 dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); 1216 1217 nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC); 1218 return 0; 1219 } 1220 1221 /** 1222 * nfs_force_lookup_revalidate - Mark the directory as having changed 1223 * @dir: pointer to directory inode 1224 * 1225 * This forces the revalidation code in nfs_lookup_revalidate() to do a 1226 * full lookup on all child dentries of 'dir' whenever a change occurs 1227 * on the server that might have invalidated our dcache. 1228 * 1229 * Note that we reserve bit '0' as a tag to let us know when a dentry 1230 * was revalidated while holding a delegation on its inode. 1231 * 1232 * The caller should be holding dir->i_lock 1233 */ 1234 void nfs_force_lookup_revalidate(struct inode *dir) 1235 { 1236 NFS_I(dir)->cache_change_attribute += 2; 1237 } 1238 EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); 1239 1240 /** 1241 * nfs_verify_change_attribute - Detects NFS remote directory changes 1242 * @dir: pointer to parent directory inode 1243 * @verf: previously saved change attribute 1244 * 1245 * Return "false" if the verifiers doesn't match the change attribute. 1246 * This would usually indicate that the directory contents have changed on 1247 * the server, and that any dentries need revalidating. 1248 */ 1249 static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf) 1250 { 1251 return (verf & ~1UL) == nfs_save_change_attribute(dir); 1252 } 1253 1254 static void nfs_set_verifier_delegated(unsigned long *verf) 1255 { 1256 *verf |= 1UL; 1257 } 1258 1259 #if IS_ENABLED(CONFIG_NFS_V4) 1260 static void nfs_unset_verifier_delegated(unsigned long *verf) 1261 { 1262 *verf &= ~1UL; 1263 } 1264 #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1265 1266 static bool nfs_test_verifier_delegated(unsigned long verf) 1267 { 1268 return verf & 1; 1269 } 1270 1271 static bool nfs_verifier_is_delegated(struct dentry *dentry) 1272 { 1273 return nfs_test_verifier_delegated(dentry->d_time); 1274 } 1275 1276 static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf) 1277 { 1278 struct inode *inode = d_inode(dentry); 1279 1280 if (!nfs_verifier_is_delegated(dentry) && 1281 !nfs_verify_change_attribute(d_inode(dentry->d_parent), verf)) 1282 goto out; 1283 if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) 1284 nfs_set_verifier_delegated(&verf); 1285 out: 1286 dentry->d_time = verf; 1287 } 1288 1289 /** 1290 * nfs_set_verifier - save a parent directory verifier in the dentry 1291 * @dentry: pointer to dentry 1292 * @verf: verifier to save 1293 * 1294 * Saves the parent directory verifier in @dentry. If the inode has 1295 * a delegation, we also tag the dentry as having been revalidated 1296 * while holding a delegation so that we know we don't have to 1297 * look it up again after a directory change. 1298 */ 1299 void nfs_set_verifier(struct dentry *dentry, unsigned long verf) 1300 { 1301 1302 spin_lock(&dentry->d_lock); 1303 nfs_set_verifier_locked(dentry, verf); 1304 spin_unlock(&dentry->d_lock); 1305 } 1306 EXPORT_SYMBOL_GPL(nfs_set_verifier); 1307 1308 #if IS_ENABLED(CONFIG_NFS_V4) 1309 /** 1310 * nfs_clear_verifier_delegated - clear the dir verifier delegation tag 1311 * @inode: pointer to inode 1312 * 1313 * Iterates through the dentries in the inode alias list and clears 1314 * the tag used to indicate that the dentry has been revalidated 1315 * while holding a delegation. 1316 * This function is intended for use when the delegation is being 1317 * returned or revoked. 1318 */ 1319 void nfs_clear_verifier_delegated(struct inode *inode) 1320 { 1321 struct dentry *alias; 1322 1323 if (!inode) 1324 return; 1325 spin_lock(&inode->i_lock); 1326 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { 1327 spin_lock(&alias->d_lock); 1328 nfs_unset_verifier_delegated(&alias->d_time); 1329 spin_unlock(&alias->d_lock); 1330 } 1331 spin_unlock(&inode->i_lock); 1332 } 1333 EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated); 1334 #endif /* IS_ENABLED(CONFIG_NFS_V4) */ 1335 1336 /* 1337 * A check for whether or not the parent directory has changed. 1338 * In the case it has, we assume that the dentries are untrustworthy 1339 * and may need to be looked up again. 1340 * If rcu_walk prevents us from performing a full check, return 0. 1341 */ 1342 static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, 1343 int rcu_walk) 1344 { 1345 if (IS_ROOT(dentry)) 1346 return 1; 1347 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) 1348 return 0; 1349 if (!nfs_verify_change_attribute(dir, dentry->d_time)) 1350 return 0; 1351 /* Revalidate nfsi->cache_change_attribute before we declare a match */ 1352 if (nfs_mapping_need_revalidate_inode(dir)) { 1353 if (rcu_walk) 1354 return 0; 1355 if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) 1356 return 0; 1357 } 1358 if (!nfs_verify_change_attribute(dir, dentry->d_time)) 1359 return 0; 1360 return 1; 1361 } 1362 1363 /* 1364 * Use intent information to check whether or not we're going to do 1365 * an O_EXCL create using this path component. 1366 */ 1367 static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) 1368 { 1369 if (NFS_PROTO(dir)->version == 2) 1370 return 0; 1371 return flags & LOOKUP_EXCL; 1372 } 1373 1374 /* 1375 * Inode and filehandle revalidation for lookups. 1376 * 1377 * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, 1378 * or if the intent information indicates that we're about to open this 1379 * particular file and the "nocto" mount flag is not set. 1380 * 1381 */ 1382 static 1383 int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) 1384 { 1385 struct nfs_server *server = NFS_SERVER(inode); 1386 int ret; 1387 1388 if (IS_AUTOMOUNT(inode)) 1389 return 0; 1390 1391 if (flags & LOOKUP_OPEN) { 1392 switch (inode->i_mode & S_IFMT) { 1393 case S_IFREG: 1394 /* A NFSv4 OPEN will revalidate later */ 1395 if (server->caps & NFS_CAP_ATOMIC_OPEN) 1396 goto out; 1397 fallthrough; 1398 case S_IFDIR: 1399 if (server->flags & NFS_MOUNT_NOCTO) 1400 break; 1401 /* NFS close-to-open cache consistency validation */ 1402 goto out_force; 1403 } 1404 } 1405 1406 /* VFS wants an on-the-wire revalidation */ 1407 if (flags & LOOKUP_REVAL) 1408 goto out_force; 1409 out: 1410 return (inode->i_nlink == 0) ? -ESTALE : 0; 1411 out_force: 1412 if (flags & LOOKUP_RCU) 1413 return -ECHILD; 1414 ret = __nfs_revalidate_inode(server, inode); 1415 if (ret != 0) 1416 return ret; 1417 goto out; 1418 } 1419 1420 static void nfs_mark_dir_for_revalidate(struct inode *inode) 1421 { 1422 spin_lock(&inode->i_lock); 1423 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE); 1424 spin_unlock(&inode->i_lock); 1425 } 1426 1427 /* 1428 * We judge how long we want to trust negative 1429 * dentries by looking at the parent inode mtime. 1430 * 1431 * If parent mtime has changed, we revalidate, else we wait for a 1432 * period corresponding to the parent's attribute cache timeout value. 1433 * 1434 * If LOOKUP_RCU prevents us from performing a full check, return 1 1435 * suggesting a reval is needed. 1436 * 1437 * Note that when creating a new file, or looking up a rename target, 1438 * then it shouldn't be necessary to revalidate a negative dentry. 1439 */ 1440 static inline 1441 int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, 1442 unsigned int flags) 1443 { 1444 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 1445 return 0; 1446 if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) 1447 return 1; 1448 return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); 1449 } 1450 1451 static int 1452 nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, 1453 struct inode *inode, int error) 1454 { 1455 switch (error) { 1456 case 1: 1457 dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n", 1458 __func__, dentry); 1459 return 1; 1460 case 0: 1461 /* 1462 * We can't d_drop the root of a disconnected tree: 1463 * its d_hash is on the s_anon list and d_drop() would hide 1464 * it from shrink_dcache_for_unmount(), leading to busy 1465 * inodes on unmount and further oopses. 1466 */ 1467 if (inode && IS_ROOT(dentry)) 1468 return 1; 1469 dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n", 1470 __func__, dentry); 1471 return 0; 1472 } 1473 dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n", 1474 __func__, dentry, error); 1475 return error; 1476 } 1477 1478 static int 1479 nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, 1480 unsigned int flags) 1481 { 1482 int ret = 1; 1483 if (nfs_neg_need_reval(dir, dentry, flags)) { 1484 if (flags & LOOKUP_RCU) 1485 return -ECHILD; 1486 ret = 0; 1487 } 1488 return nfs_lookup_revalidate_done(dir, dentry, NULL, ret); 1489 } 1490 1491 static int 1492 nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry, 1493 struct inode *inode) 1494 { 1495 nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1496 return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 1497 } 1498 1499 static int 1500 nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry, 1501 struct inode *inode) 1502 { 1503 struct nfs_fh *fhandle; 1504 struct nfs_fattr *fattr; 1505 struct nfs4_label *label; 1506 unsigned long dir_verifier; 1507 int ret; 1508 1509 ret = -ENOMEM; 1510 fhandle = nfs_alloc_fhandle(); 1511 fattr = nfs_alloc_fattr(); 1512 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); 1513 if (fhandle == NULL || fattr == NULL || IS_ERR(label)) 1514 goto out; 1515 1516 dir_verifier = nfs_save_change_attribute(dir); 1517 ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 1518 if (ret < 0) { 1519 switch (ret) { 1520 case -ESTALE: 1521 case -ENOENT: 1522 ret = 0; 1523 break; 1524 case -ETIMEDOUT: 1525 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL) 1526 ret = 1; 1527 } 1528 goto out; 1529 } 1530 ret = 0; 1531 if (nfs_compare_fh(NFS_FH(inode), fhandle)) 1532 goto out; 1533 if (nfs_refresh_inode(inode, fattr) < 0) 1534 goto out; 1535 1536 nfs_setsecurity(inode, fattr, label); 1537 nfs_set_verifier(dentry, dir_verifier); 1538 1539 /* set a readdirplus hint that we had a cache miss */ 1540 nfs_force_use_readdirplus(dir); 1541 ret = 1; 1542 out: 1543 nfs_free_fattr(fattr); 1544 nfs_free_fhandle(fhandle); 1545 nfs4_label_free(label); 1546 1547 /* 1548 * If the lookup failed despite the dentry change attribute being 1549 * a match, then we should revalidate the directory cache. 1550 */ 1551 if (!ret && nfs_verify_change_attribute(dir, dentry->d_time)) 1552 nfs_mark_dir_for_revalidate(dir); 1553 return nfs_lookup_revalidate_done(dir, dentry, inode, ret); 1554 } 1555 1556 /* 1557 * This is called every time the dcache has a lookup hit, 1558 * and we should check whether we can really trust that 1559 * lookup. 1560 * 1561 * NOTE! The hit can be a negative hit too, don't assume 1562 * we have an inode! 1563 * 1564 * If the parent directory is seen to have changed, we throw out the 1565 * cached dentry and do a new lookup. 1566 */ 1567 static int 1568 nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 1569 unsigned int flags) 1570 { 1571 struct inode *inode; 1572 int error; 1573 1574 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 1575 inode = d_inode(dentry); 1576 1577 if (!inode) 1578 return nfs_lookup_revalidate_negative(dir, dentry, flags); 1579 1580 if (is_bad_inode(inode)) { 1581 dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 1582 __func__, dentry); 1583 goto out_bad; 1584 } 1585 1586 if (nfs_verifier_is_delegated(dentry)) 1587 return nfs_lookup_revalidate_delegated(dir, dentry, inode); 1588 1589 /* Force a full look up iff the parent directory has changed */ 1590 if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && 1591 nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { 1592 error = nfs_lookup_verify_inode(inode, flags); 1593 if (error) { 1594 if (error == -ESTALE) 1595 nfs_mark_dir_for_revalidate(dir); 1596 goto out_bad; 1597 } 1598 nfs_advise_use_readdirplus(dir); 1599 goto out_valid; 1600 } 1601 1602 if (flags & LOOKUP_RCU) 1603 return -ECHILD; 1604 1605 if (NFS_STALE(inode)) 1606 goto out_bad; 1607 1608 trace_nfs_lookup_revalidate_enter(dir, dentry, flags); 1609 error = nfs_lookup_revalidate_dentry(dir, dentry, inode); 1610 trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error); 1611 return error; 1612 out_valid: 1613 return nfs_lookup_revalidate_done(dir, dentry, inode, 1); 1614 out_bad: 1615 if (flags & LOOKUP_RCU) 1616 return -ECHILD; 1617 return nfs_lookup_revalidate_done(dir, dentry, inode, 0); 1618 } 1619 1620 static int 1621 __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags, 1622 int (*reval)(struct inode *, struct dentry *, unsigned int)) 1623 { 1624 struct dentry *parent; 1625 struct inode *dir; 1626 int ret; 1627 1628 if (flags & LOOKUP_RCU) { 1629 parent = READ_ONCE(dentry->d_parent); 1630 dir = d_inode_rcu(parent); 1631 if (!dir) 1632 return -ECHILD; 1633 ret = reval(dir, dentry, flags); 1634 if (parent != READ_ONCE(dentry->d_parent)) 1635 return -ECHILD; 1636 } else { 1637 parent = dget_parent(dentry); 1638 ret = reval(d_inode(parent), dentry, flags); 1639 dput(parent); 1640 } 1641 return ret; 1642 } 1643 1644 static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) 1645 { 1646 return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate); 1647 } 1648 1649 /* 1650 * A weaker form of d_revalidate for revalidating just the d_inode(dentry) 1651 * when we don't really care about the dentry name. This is called when a 1652 * pathwalk ends on a dentry that was not found via a normal lookup in the 1653 * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). 1654 * 1655 * In this situation, we just want to verify that the inode itself is OK 1656 * since the dentry might have changed on the server. 1657 */ 1658 static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) 1659 { 1660 struct inode *inode = d_inode(dentry); 1661 int error = 0; 1662 1663 /* 1664 * I believe we can only get a negative dentry here in the case of a 1665 * procfs-style symlink. Just assume it's correct for now, but we may 1666 * eventually need to do something more here. 1667 */ 1668 if (!inode) { 1669 dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", 1670 __func__, dentry); 1671 return 1; 1672 } 1673 1674 if (is_bad_inode(inode)) { 1675 dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", 1676 __func__, dentry); 1677 return 0; 1678 } 1679 1680 error = nfs_lookup_verify_inode(inode, flags); 1681 dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", 1682 __func__, inode->i_ino, error ? "invalid" : "valid"); 1683 return !error; 1684 } 1685 1686 /* 1687 * This is called from dput() when d_count is going to 0. 1688 */ 1689 static int nfs_dentry_delete(const struct dentry *dentry) 1690 { 1691 dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", 1692 dentry, dentry->d_flags); 1693 1694 /* Unhash any dentry with a stale inode */ 1695 if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) 1696 return 1; 1697 1698 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1699 /* Unhash it, so that ->d_iput() would be called */ 1700 return 1; 1701 } 1702 if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { 1703 /* Unhash it, so that ancestors of killed async unlink 1704 * files will be cleaned up during umount */ 1705 return 1; 1706 } 1707 return 0; 1708 1709 } 1710 1711 /* Ensure that we revalidate inode->i_nlink */ 1712 static void nfs_drop_nlink(struct inode *inode) 1713 { 1714 spin_lock(&inode->i_lock); 1715 /* drop the inode if we're reasonably sure this is the last link */ 1716 if (inode->i_nlink > 0) 1717 drop_nlink(inode); 1718 NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter(); 1719 nfs_set_cache_invalid( 1720 inode, NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME | 1721 NFS_INO_INVALID_NLINK); 1722 spin_unlock(&inode->i_lock); 1723 } 1724 1725 /* 1726 * Called when the dentry loses inode. 1727 * We use it to clean up silly-renamed files. 1728 */ 1729 static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 1730 { 1731 if (S_ISDIR(inode->i_mode)) 1732 /* drop any readdir cache as it could easily be old */ 1733 nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA); 1734 1735 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 1736 nfs_complete_unlink(dentry, inode); 1737 nfs_drop_nlink(inode); 1738 } 1739 iput(inode); 1740 } 1741 1742 static void nfs_d_release(struct dentry *dentry) 1743 { 1744 /* free cached devname value, if it survived that far */ 1745 if (unlikely(dentry->d_fsdata)) { 1746 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1747 WARN_ON(1); 1748 else 1749 kfree(dentry->d_fsdata); 1750 } 1751 } 1752 1753 const struct dentry_operations nfs_dentry_operations = { 1754 .d_revalidate = nfs_lookup_revalidate, 1755 .d_weak_revalidate = nfs_weak_revalidate, 1756 .d_delete = nfs_dentry_delete, 1757 .d_iput = nfs_dentry_iput, 1758 .d_automount = nfs_d_automount, 1759 .d_release = nfs_d_release, 1760 }; 1761 EXPORT_SYMBOL_GPL(nfs_dentry_operations); 1762 1763 struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 1764 { 1765 struct dentry *res; 1766 struct inode *inode = NULL; 1767 struct nfs_fh *fhandle = NULL; 1768 struct nfs_fattr *fattr = NULL; 1769 struct nfs4_label *label = NULL; 1770 unsigned long dir_verifier; 1771 int error; 1772 1773 dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); 1774 nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); 1775 1776 if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) 1777 return ERR_PTR(-ENAMETOOLONG); 1778 1779 /* 1780 * If we're doing an exclusive create, optimize away the lookup 1781 * but don't hash the dentry. 1782 */ 1783 if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) 1784 return NULL; 1785 1786 res = ERR_PTR(-ENOMEM); 1787 fhandle = nfs_alloc_fhandle(); 1788 fattr = nfs_alloc_fattr(); 1789 if (fhandle == NULL || fattr == NULL) 1790 goto out; 1791 1792 label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT); 1793 if (IS_ERR(label)) 1794 goto out; 1795 1796 dir_verifier = nfs_save_change_attribute(dir); 1797 trace_nfs_lookup_enter(dir, dentry, flags); 1798 error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); 1799 if (error == -ENOENT) 1800 goto no_entry; 1801 if (error < 0) { 1802 res = ERR_PTR(error); 1803 goto out_label; 1804 } 1805 inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1806 res = ERR_CAST(inode); 1807 if (IS_ERR(res)) 1808 goto out_label; 1809 1810 /* Notify readdir to use READDIRPLUS */ 1811 nfs_force_use_readdirplus(dir); 1812 1813 no_entry: 1814 res = d_splice_alias(inode, dentry); 1815 if (res != NULL) { 1816 if (IS_ERR(res)) 1817 goto out_label; 1818 dentry = res; 1819 } 1820 nfs_set_verifier(dentry, dir_verifier); 1821 out_label: 1822 trace_nfs_lookup_exit(dir, dentry, flags, error); 1823 nfs4_label_free(label); 1824 out: 1825 nfs_free_fattr(fattr); 1826 nfs_free_fhandle(fhandle); 1827 return res; 1828 } 1829 EXPORT_SYMBOL_GPL(nfs_lookup); 1830 1831 #if IS_ENABLED(CONFIG_NFS_V4) 1832 static int nfs4_lookup_revalidate(struct dentry *, unsigned int); 1833 1834 const struct dentry_operations nfs4_dentry_operations = { 1835 .d_revalidate = nfs4_lookup_revalidate, 1836 .d_weak_revalidate = nfs_weak_revalidate, 1837 .d_delete = nfs_dentry_delete, 1838 .d_iput = nfs_dentry_iput, 1839 .d_automount = nfs_d_automount, 1840 .d_release = nfs_d_release, 1841 }; 1842 EXPORT_SYMBOL_GPL(nfs4_dentry_operations); 1843 1844 static fmode_t flags_to_mode(int flags) 1845 { 1846 fmode_t res = (__force fmode_t)flags & FMODE_EXEC; 1847 if ((flags & O_ACCMODE) != O_WRONLY) 1848 res |= FMODE_READ; 1849 if ((flags & O_ACCMODE) != O_RDONLY) 1850 res |= FMODE_WRITE; 1851 return res; 1852 } 1853 1854 static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) 1855 { 1856 return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); 1857 } 1858 1859 static int do_open(struct inode *inode, struct file *filp) 1860 { 1861 nfs_fscache_open_file(inode, filp); 1862 return 0; 1863 } 1864 1865 static int nfs_finish_open(struct nfs_open_context *ctx, 1866 struct dentry *dentry, 1867 struct file *file, unsigned open_flags) 1868 { 1869 int err; 1870 1871 err = finish_open(file, dentry, do_open); 1872 if (err) 1873 goto out; 1874 if (S_ISREG(file->f_path.dentry->d_inode->i_mode)) 1875 nfs_file_set_open_context(file, ctx); 1876 else 1877 err = -EOPENSTALE; 1878 out: 1879 return err; 1880 } 1881 1882 int nfs_atomic_open(struct inode *dir, struct dentry *dentry, 1883 struct file *file, unsigned open_flags, 1884 umode_t mode) 1885 { 1886 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 1887 struct nfs_open_context *ctx; 1888 struct dentry *res; 1889 struct iattr attr = { .ia_valid = ATTR_OPEN }; 1890 struct inode *inode; 1891 unsigned int lookup_flags = 0; 1892 bool switched = false; 1893 int created = 0; 1894 int err; 1895 1896 /* Expect a negative dentry */ 1897 BUG_ON(d_inode(dentry)); 1898 1899 dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", 1900 dir->i_sb->s_id, dir->i_ino, dentry); 1901 1902 err = nfs_check_flags(open_flags); 1903 if (err) 1904 return err; 1905 1906 /* NFS only supports OPEN on regular files */ 1907 if ((open_flags & O_DIRECTORY)) { 1908 if (!d_in_lookup(dentry)) { 1909 /* 1910 * Hashed negative dentry with O_DIRECTORY: dentry was 1911 * revalidated and is fine, no need to perform lookup 1912 * again 1913 */ 1914 return -ENOENT; 1915 } 1916 lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; 1917 goto no_open; 1918 } 1919 1920 if (dentry->d_name.len > NFS_SERVER(dir)->namelen) 1921 return -ENAMETOOLONG; 1922 1923 if (open_flags & O_CREAT) { 1924 struct nfs_server *server = NFS_SERVER(dir); 1925 1926 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) 1927 mode &= ~current_umask(); 1928 1929 attr.ia_valid |= ATTR_MODE; 1930 attr.ia_mode = mode; 1931 } 1932 if (open_flags & O_TRUNC) { 1933 attr.ia_valid |= ATTR_SIZE; 1934 attr.ia_size = 0; 1935 } 1936 1937 if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { 1938 d_drop(dentry); 1939 switched = true; 1940 dentry = d_alloc_parallel(dentry->d_parent, 1941 &dentry->d_name, &wq); 1942 if (IS_ERR(dentry)) 1943 return PTR_ERR(dentry); 1944 if (unlikely(!d_in_lookup(dentry))) 1945 return finish_no_open(file, dentry); 1946 } 1947 1948 ctx = create_nfs_open_context(dentry, open_flags, file); 1949 err = PTR_ERR(ctx); 1950 if (IS_ERR(ctx)) 1951 goto out; 1952 1953 trace_nfs_atomic_open_enter(dir, ctx, open_flags); 1954 inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created); 1955 if (created) 1956 file->f_mode |= FMODE_CREATED; 1957 if (IS_ERR(inode)) { 1958 err = PTR_ERR(inode); 1959 trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 1960 put_nfs_open_context(ctx); 1961 d_drop(dentry); 1962 switch (err) { 1963 case -ENOENT: 1964 d_splice_alias(NULL, dentry); 1965 nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1966 break; 1967 case -EISDIR: 1968 case -ENOTDIR: 1969 goto no_open; 1970 case -ELOOP: 1971 if (!(open_flags & O_NOFOLLOW)) 1972 goto no_open; 1973 break; 1974 /* case -EINVAL: */ 1975 default: 1976 break; 1977 } 1978 goto out; 1979 } 1980 1981 err = nfs_finish_open(ctx, ctx->dentry, file, open_flags); 1982 trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); 1983 put_nfs_open_context(ctx); 1984 out: 1985 if (unlikely(switched)) { 1986 d_lookup_done(dentry); 1987 dput(dentry); 1988 } 1989 return err; 1990 1991 no_open: 1992 res = nfs_lookup(dir, dentry, lookup_flags); 1993 if (switched) { 1994 d_lookup_done(dentry); 1995 if (!res) 1996 res = dentry; 1997 else 1998 dput(dentry); 1999 } 2000 if (IS_ERR(res)) 2001 return PTR_ERR(res); 2002 return finish_no_open(file, res); 2003 } 2004 EXPORT_SYMBOL_GPL(nfs_atomic_open); 2005 2006 static int 2007 nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, 2008 unsigned int flags) 2009 { 2010 struct inode *inode; 2011 2012 if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) 2013 goto full_reval; 2014 if (d_mountpoint(dentry)) 2015 goto full_reval; 2016 2017 inode = d_inode(dentry); 2018 2019 /* We can't create new files in nfs_open_revalidate(), so we 2020 * optimize away revalidation of negative dentries. 2021 */ 2022 if (inode == NULL) 2023 goto full_reval; 2024 2025 if (nfs_verifier_is_delegated(dentry)) 2026 return nfs_lookup_revalidate_delegated(dir, dentry, inode); 2027 2028 /* NFS only supports OPEN on regular files */ 2029 if (!S_ISREG(inode->i_mode)) 2030 goto full_reval; 2031 2032 /* We cannot do exclusive creation on a positive dentry */ 2033 if (flags & (LOOKUP_EXCL | LOOKUP_REVAL)) 2034 goto reval_dentry; 2035 2036 /* Check if the directory changed */ 2037 if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) 2038 goto reval_dentry; 2039 2040 /* Let f_op->open() actually open (and revalidate) the file */ 2041 return 1; 2042 reval_dentry: 2043 if (flags & LOOKUP_RCU) 2044 return -ECHILD; 2045 return nfs_lookup_revalidate_dentry(dir, dentry, inode); 2046 2047 full_reval: 2048 return nfs_do_lookup_revalidate(dir, dentry, flags); 2049 } 2050 2051 static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) 2052 { 2053 return __nfs_lookup_revalidate(dentry, flags, 2054 nfs4_do_lookup_revalidate); 2055 } 2056 2057 #endif /* CONFIG_NFSV4 */ 2058 2059 struct dentry * 2060 nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle, 2061 struct nfs_fattr *fattr, 2062 struct nfs4_label *label) 2063 { 2064 struct dentry *parent = dget_parent(dentry); 2065 struct inode *dir = d_inode(parent); 2066 struct inode *inode; 2067 struct dentry *d; 2068 int error; 2069 2070 d_drop(dentry); 2071 2072 if (fhandle->size == 0) { 2073 error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL); 2074 if (error) 2075 goto out_error; 2076 } 2077 nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 2078 if (!(fattr->valid & NFS_ATTR_FATTR)) { 2079 struct nfs_server *server = NFS_SB(dentry->d_sb); 2080 error = server->nfs_client->rpc_ops->getattr(server, fhandle, 2081 fattr, NULL, NULL); 2082 if (error < 0) 2083 goto out_error; 2084 } 2085 inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 2086 d = d_splice_alias(inode, dentry); 2087 out: 2088 dput(parent); 2089 return d; 2090 out_error: 2091 d = ERR_PTR(error); 2092 goto out; 2093 } 2094 EXPORT_SYMBOL_GPL(nfs_add_or_obtain); 2095 2096 /* 2097 * Code common to create, mkdir, and mknod. 2098 */ 2099 int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, 2100 struct nfs_fattr *fattr, 2101 struct nfs4_label *label) 2102 { 2103 struct dentry *d; 2104 2105 d = nfs_add_or_obtain(dentry, fhandle, fattr, label); 2106 if (IS_ERR(d)) 2107 return PTR_ERR(d); 2108 2109 /* Callers don't care */ 2110 dput(d); 2111 return 0; 2112 } 2113 EXPORT_SYMBOL_GPL(nfs_instantiate); 2114 2115 /* 2116 * Following a failed create operation, we drop the dentry rather 2117 * than retain a negative dentry. This avoids a problem in the event 2118 * that the operation succeeded on the server, but an error in the 2119 * reply path made it appear to have failed. 2120 */ 2121 int nfs_create(struct user_namespace *mnt_userns, struct inode *dir, 2122 struct dentry *dentry, umode_t mode, bool excl) 2123 { 2124 struct iattr attr; 2125 int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; 2126 int error; 2127 2128 dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", 2129 dir->i_sb->s_id, dir->i_ino, dentry); 2130 2131 attr.ia_mode = mode; 2132 attr.ia_valid = ATTR_MODE; 2133 2134 trace_nfs_create_enter(dir, dentry, open_flags); 2135 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 2136 trace_nfs_create_exit(dir, dentry, open_flags, error); 2137 if (error != 0) 2138 goto out_err; 2139 return 0; 2140 out_err: 2141 d_drop(dentry); 2142 return error; 2143 } 2144 EXPORT_SYMBOL_GPL(nfs_create); 2145 2146 /* 2147 * See comments for nfs_proc_create regarding failed operations. 2148 */ 2149 int 2150 nfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, 2151 struct dentry *dentry, umode_t mode, dev_t rdev) 2152 { 2153 struct iattr attr; 2154 int status; 2155 2156 dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", 2157 dir->i_sb->s_id, dir->i_ino, dentry); 2158 2159 attr.ia_mode = mode; 2160 attr.ia_valid = ATTR_MODE; 2161 2162 trace_nfs_mknod_enter(dir, dentry); 2163 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 2164 trace_nfs_mknod_exit(dir, dentry, status); 2165 if (status != 0) 2166 goto out_err; 2167 return 0; 2168 out_err: 2169 d_drop(dentry); 2170 return status; 2171 } 2172 EXPORT_SYMBOL_GPL(nfs_mknod); 2173 2174 /* 2175 * See comments for nfs_proc_create regarding failed operations. 2176 */ 2177 int nfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 2178 struct dentry *dentry, umode_t mode) 2179 { 2180 struct iattr attr; 2181 int error; 2182 2183 dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", 2184 dir->i_sb->s_id, dir->i_ino, dentry); 2185 2186 attr.ia_valid = ATTR_MODE; 2187 attr.ia_mode = mode | S_IFDIR; 2188 2189 trace_nfs_mkdir_enter(dir, dentry); 2190 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 2191 trace_nfs_mkdir_exit(dir, dentry, error); 2192 if (error != 0) 2193 goto out_err; 2194 return 0; 2195 out_err: 2196 d_drop(dentry); 2197 return error; 2198 } 2199 EXPORT_SYMBOL_GPL(nfs_mkdir); 2200 2201 static void nfs_dentry_handle_enoent(struct dentry *dentry) 2202 { 2203 if (simple_positive(dentry)) 2204 d_delete(dentry); 2205 } 2206 2207 static void nfs_dentry_remove_handle_error(struct inode *dir, 2208 struct dentry *dentry, int error) 2209 { 2210 switch (error) { 2211 case -ENOENT: 2212 d_delete(dentry); 2213 fallthrough; 2214 case 0: 2215 nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 2216 } 2217 } 2218 2219 int nfs_rmdir(struct inode *dir, struct dentry *dentry) 2220 { 2221 int error; 2222 2223 dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", 2224 dir->i_sb->s_id, dir->i_ino, dentry); 2225 2226 trace_nfs_rmdir_enter(dir, dentry); 2227 if (d_really_is_positive(dentry)) { 2228 down_write(&NFS_I(d_inode(dentry))->rmdir_sem); 2229 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 2230 /* Ensure the VFS deletes this inode */ 2231 switch (error) { 2232 case 0: 2233 clear_nlink(d_inode(dentry)); 2234 break; 2235 case -ENOENT: 2236 nfs_dentry_handle_enoent(dentry); 2237 } 2238 up_write(&NFS_I(d_inode(dentry))->rmdir_sem); 2239 } else 2240 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 2241 nfs_dentry_remove_handle_error(dir, dentry, error); 2242 trace_nfs_rmdir_exit(dir, dentry, error); 2243 2244 return error; 2245 } 2246 EXPORT_SYMBOL_GPL(nfs_rmdir); 2247 2248 /* 2249 * Remove a file after making sure there are no pending writes, 2250 * and after checking that the file has only one user. 2251 * 2252 * We invalidate the attribute cache and free the inode prior to the operation 2253 * to avoid possible races if the server reuses the inode. 2254 */ 2255 static int nfs_safe_remove(struct dentry *dentry) 2256 { 2257 struct inode *dir = d_inode(dentry->d_parent); 2258 struct inode *inode = d_inode(dentry); 2259 int error = -EBUSY; 2260 2261 dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); 2262 2263 /* If the dentry was sillyrenamed, we simply call d_delete() */ 2264 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 2265 error = 0; 2266 goto out; 2267 } 2268 2269 trace_nfs_remove_enter(dir, dentry); 2270 if (inode != NULL) { 2271 error = NFS_PROTO(dir)->remove(dir, dentry); 2272 if (error == 0) 2273 nfs_drop_nlink(inode); 2274 } else 2275 error = NFS_PROTO(dir)->remove(dir, dentry); 2276 if (error == -ENOENT) 2277 nfs_dentry_handle_enoent(dentry); 2278 trace_nfs_remove_exit(dir, dentry, error); 2279 out: 2280 return error; 2281 } 2282 2283 /* We do silly rename. In case sillyrename() returns -EBUSY, the inode 2284 * belongs to an active ".nfs..." file and we return -EBUSY. 2285 * 2286 * If sillyrename() returns 0, we do nothing, otherwise we unlink. 2287 */ 2288 int nfs_unlink(struct inode *dir, struct dentry *dentry) 2289 { 2290 int error; 2291 int need_rehash = 0; 2292 2293 dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, 2294 dir->i_ino, dentry); 2295 2296 trace_nfs_unlink_enter(dir, dentry); 2297 spin_lock(&dentry->d_lock); 2298 if (d_count(dentry) > 1) { 2299 spin_unlock(&dentry->d_lock); 2300 /* Start asynchronous writeout of the inode */ 2301 write_inode_now(d_inode(dentry), 0); 2302 error = nfs_sillyrename(dir, dentry); 2303 goto out; 2304 } 2305 if (!d_unhashed(dentry)) { 2306 __d_drop(dentry); 2307 need_rehash = 1; 2308 } 2309 spin_unlock(&dentry->d_lock); 2310 error = nfs_safe_remove(dentry); 2311 nfs_dentry_remove_handle_error(dir, dentry, error); 2312 if (need_rehash) 2313 d_rehash(dentry); 2314 out: 2315 trace_nfs_unlink_exit(dir, dentry, error); 2316 return error; 2317 } 2318 EXPORT_SYMBOL_GPL(nfs_unlink); 2319 2320 /* 2321 * To create a symbolic link, most file systems instantiate a new inode, 2322 * add a page to it containing the path, then write it out to the disk 2323 * using prepare_write/commit_write. 2324 * 2325 * Unfortunately the NFS client can't create the in-core inode first 2326 * because it needs a file handle to create an in-core inode (see 2327 * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the 2328 * symlink request has completed on the server. 2329 * 2330 * So instead we allocate a raw page, copy the symname into it, then do 2331 * the SYMLINK request with the page as the buffer. If it succeeds, we 2332 * now have a new file handle and can instantiate an in-core NFS inode 2333 * and move the raw page into its mapping. 2334 */ 2335 int nfs_symlink(struct user_namespace *mnt_userns, struct inode *dir, 2336 struct dentry *dentry, const char *symname) 2337 { 2338 struct page *page; 2339 char *kaddr; 2340 struct iattr attr; 2341 unsigned int pathlen = strlen(symname); 2342 int error; 2343 2344 dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, 2345 dir->i_ino, dentry, symname); 2346 2347 if (pathlen > PAGE_SIZE) 2348 return -ENAMETOOLONG; 2349 2350 attr.ia_mode = S_IFLNK | S_IRWXUGO; 2351 attr.ia_valid = ATTR_MODE; 2352 2353 page = alloc_page(GFP_USER); 2354 if (!page) 2355 return -ENOMEM; 2356 2357 kaddr = page_address(page); 2358 memcpy(kaddr, symname, pathlen); 2359 if (pathlen < PAGE_SIZE) 2360 memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); 2361 2362 trace_nfs_symlink_enter(dir, dentry); 2363 error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); 2364 trace_nfs_symlink_exit(dir, dentry, error); 2365 if (error != 0) { 2366 dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", 2367 dir->i_sb->s_id, dir->i_ino, 2368 dentry, symname, error); 2369 d_drop(dentry); 2370 __free_page(page); 2371 return error; 2372 } 2373 2374 nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 2375 2376 /* 2377 * No big deal if we can't add this page to the page cache here. 2378 * READLINK will get the missing page from the server if needed. 2379 */ 2380 if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, 2381 GFP_KERNEL)) { 2382 SetPageUptodate(page); 2383 unlock_page(page); 2384 /* 2385 * add_to_page_cache_lru() grabs an extra page refcount. 2386 * Drop it here to avoid leaking this page later. 2387 */ 2388 put_page(page); 2389 } else 2390 __free_page(page); 2391 2392 return 0; 2393 } 2394 EXPORT_SYMBOL_GPL(nfs_symlink); 2395 2396 int 2397 nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 2398 { 2399 struct inode *inode = d_inode(old_dentry); 2400 int error; 2401 2402 dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", 2403 old_dentry, dentry); 2404 2405 trace_nfs_link_enter(inode, dir, dentry); 2406 d_drop(dentry); 2407 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 2408 if (error == 0) { 2409 nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 2410 ihold(inode); 2411 d_add(dentry, inode); 2412 } 2413 trace_nfs_link_exit(inode, dir, dentry, error); 2414 return error; 2415 } 2416 EXPORT_SYMBOL_GPL(nfs_link); 2417 2418 /* 2419 * RENAME 2420 * FIXME: Some nfsds, like the Linux user space nfsd, may generate a 2421 * different file handle for the same inode after a rename (e.g. when 2422 * moving to a different directory). A fail-safe method to do so would 2423 * be to look up old_dir/old_name, create a link to new_dir/new_name and 2424 * rename the old file using the sillyrename stuff. This way, the original 2425 * file in old_dir will go away when the last process iput()s the inode. 2426 * 2427 * FIXED. 2428 * 2429 * It actually works quite well. One needs to have the possibility for 2430 * at least one ".nfs..." file in each directory the file ever gets 2431 * moved or linked to which happens automagically with the new 2432 * implementation that only depends on the dcache stuff instead of 2433 * using the inode layer 2434 * 2435 * Unfortunately, things are a little more complicated than indicated 2436 * above. For a cross-directory move, we want to make sure we can get 2437 * rid of the old inode after the operation. This means there must be 2438 * no pending writes (if it's a file), and the use count must be 1. 2439 * If these conditions are met, we can drop the dentries before doing 2440 * the rename. 2441 */ 2442 int nfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir, 2443 struct dentry *old_dentry, struct inode *new_dir, 2444 struct dentry *new_dentry, unsigned int flags) 2445 { 2446 struct inode *old_inode = d_inode(old_dentry); 2447 struct inode *new_inode = d_inode(new_dentry); 2448 struct dentry *dentry = NULL, *rehash = NULL; 2449 struct rpc_task *task; 2450 int error = -EBUSY; 2451 2452 if (flags) 2453 return -EINVAL; 2454 2455 dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", 2456 old_dentry, new_dentry, 2457 d_count(new_dentry)); 2458 2459 trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); 2460 /* 2461 * For non-directories, check whether the target is busy and if so, 2462 * make a copy of the dentry and then do a silly-rename. If the 2463 * silly-rename succeeds, the copied dentry is hashed and becomes 2464 * the new target. 2465 */ 2466 if (new_inode && !S_ISDIR(new_inode->i_mode)) { 2467 /* 2468 * To prevent any new references to the target during the 2469 * rename, we unhash the dentry in advance. 2470 */ 2471 if (!d_unhashed(new_dentry)) { 2472 d_drop(new_dentry); 2473 rehash = new_dentry; 2474 } 2475 2476 if (d_count(new_dentry) > 2) { 2477 int err; 2478 2479 /* copy the target dentry's name */ 2480 dentry = d_alloc(new_dentry->d_parent, 2481 &new_dentry->d_name); 2482 if (!dentry) 2483 goto out; 2484 2485 /* silly-rename the existing target ... */ 2486 err = nfs_sillyrename(new_dir, new_dentry); 2487 if (err) 2488 goto out; 2489 2490 new_dentry = dentry; 2491 rehash = NULL; 2492 new_inode = NULL; 2493 } 2494 } 2495 2496 task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); 2497 if (IS_ERR(task)) { 2498 error = PTR_ERR(task); 2499 goto out; 2500 } 2501 2502 error = rpc_wait_for_completion_task(task); 2503 if (error != 0) { 2504 ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; 2505 /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */ 2506 smp_wmb(); 2507 } else 2508 error = task->tk_status; 2509 rpc_put_task(task); 2510 /* Ensure the inode attributes are revalidated */ 2511 if (error == 0) { 2512 spin_lock(&old_inode->i_lock); 2513 NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter(); 2514 nfs_set_cache_invalid(old_inode, NFS_INO_INVALID_CHANGE | 2515 NFS_INO_INVALID_CTIME | 2516 NFS_INO_REVAL_FORCED); 2517 spin_unlock(&old_inode->i_lock); 2518 } 2519 out: 2520 if (rehash) 2521 d_rehash(rehash); 2522 trace_nfs_rename_exit(old_dir, old_dentry, 2523 new_dir, new_dentry, error); 2524 if (!error) { 2525 if (new_inode != NULL) 2526 nfs_drop_nlink(new_inode); 2527 /* 2528 * The d_move() should be here instead of in an async RPC completion 2529 * handler because we need the proper locks to move the dentry. If 2530 * we're interrupted by a signal, the async RPC completion handler 2531 * should mark the directories for revalidation. 2532 */ 2533 d_move(old_dentry, new_dentry); 2534 nfs_set_verifier(old_dentry, 2535 nfs_save_change_attribute(new_dir)); 2536 } else if (error == -ENOENT) 2537 nfs_dentry_handle_enoent(old_dentry); 2538 2539 /* new dentry created? */ 2540 if (dentry) 2541 dput(dentry); 2542 return error; 2543 } 2544 EXPORT_SYMBOL_GPL(nfs_rename); 2545 2546 static DEFINE_SPINLOCK(nfs_access_lru_lock); 2547 static LIST_HEAD(nfs_access_lru_list); 2548 static atomic_long_t nfs_access_nr_entries; 2549 2550 static unsigned long nfs_access_max_cachesize = 4*1024*1024; 2551 module_param(nfs_access_max_cachesize, ulong, 0644); 2552 MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); 2553 2554 static void nfs_access_free_entry(struct nfs_access_entry *entry) 2555 { 2556 put_cred(entry->cred); 2557 kfree_rcu(entry, rcu_head); 2558 smp_mb__before_atomic(); 2559 atomic_long_dec(&nfs_access_nr_entries); 2560 smp_mb__after_atomic(); 2561 } 2562 2563 static void nfs_access_free_list(struct list_head *head) 2564 { 2565 struct nfs_access_entry *cache; 2566 2567 while (!list_empty(head)) { 2568 cache = list_entry(head->next, struct nfs_access_entry, lru); 2569 list_del(&cache->lru); 2570 nfs_access_free_entry(cache); 2571 } 2572 } 2573 2574 static unsigned long 2575 nfs_do_access_cache_scan(unsigned int nr_to_scan) 2576 { 2577 LIST_HEAD(head); 2578 struct nfs_inode *nfsi, *next; 2579 struct nfs_access_entry *cache; 2580 long freed = 0; 2581 2582 spin_lock(&nfs_access_lru_lock); 2583 list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { 2584 struct inode *inode; 2585 2586 if (nr_to_scan-- == 0) 2587 break; 2588 inode = &nfsi->vfs_inode; 2589 spin_lock(&inode->i_lock); 2590 if (list_empty(&nfsi->access_cache_entry_lru)) 2591 goto remove_lru_entry; 2592 cache = list_entry(nfsi->access_cache_entry_lru.next, 2593 struct nfs_access_entry, lru); 2594 list_move(&cache->lru, &head); 2595 rb_erase(&cache->rb_node, &nfsi->access_cache); 2596 freed++; 2597 if (!list_empty(&nfsi->access_cache_entry_lru)) 2598 list_move_tail(&nfsi->access_cache_inode_lru, 2599 &nfs_access_lru_list); 2600 else { 2601 remove_lru_entry: 2602 list_del_init(&nfsi->access_cache_inode_lru); 2603 smp_mb__before_atomic(); 2604 clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); 2605 smp_mb__after_atomic(); 2606 } 2607 spin_unlock(&inode->i_lock); 2608 } 2609 spin_unlock(&nfs_access_lru_lock); 2610 nfs_access_free_list(&head); 2611 return freed; 2612 } 2613 2614 unsigned long 2615 nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) 2616 { 2617 int nr_to_scan = sc->nr_to_scan; 2618 gfp_t gfp_mask = sc->gfp_mask; 2619 2620 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) 2621 return SHRINK_STOP; 2622 return nfs_do_access_cache_scan(nr_to_scan); 2623 } 2624 2625 2626 unsigned long 2627 nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) 2628 { 2629 return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); 2630 } 2631 2632 static void 2633 nfs_access_cache_enforce_limit(void) 2634 { 2635 long nr_entries = atomic_long_read(&nfs_access_nr_entries); 2636 unsigned long diff; 2637 unsigned int nr_to_scan; 2638 2639 if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) 2640 return; 2641 nr_to_scan = 100; 2642 diff = nr_entries - nfs_access_max_cachesize; 2643 if (diff < nr_to_scan) 2644 nr_to_scan = diff; 2645 nfs_do_access_cache_scan(nr_to_scan); 2646 } 2647 2648 static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) 2649 { 2650 struct rb_root *root_node = &nfsi->access_cache; 2651 struct rb_node *n; 2652 struct nfs_access_entry *entry; 2653 2654 /* Unhook entries from the cache */ 2655 while ((n = rb_first(root_node)) != NULL) { 2656 entry = rb_entry(n, struct nfs_access_entry, rb_node); 2657 rb_erase(n, root_node); 2658 list_move(&entry->lru, head); 2659 } 2660 nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; 2661 } 2662 2663 void nfs_access_zap_cache(struct inode *inode) 2664 { 2665 LIST_HEAD(head); 2666 2667 if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) 2668 return; 2669 /* Remove from global LRU init */ 2670 spin_lock(&nfs_access_lru_lock); 2671 if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2672 list_del_init(&NFS_I(inode)->access_cache_inode_lru); 2673 2674 spin_lock(&inode->i_lock); 2675 __nfs_access_zap_cache(NFS_I(inode), &head); 2676 spin_unlock(&inode->i_lock); 2677 spin_unlock(&nfs_access_lru_lock); 2678 nfs_access_free_list(&head); 2679 } 2680 EXPORT_SYMBOL_GPL(nfs_access_zap_cache); 2681 2682 static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred) 2683 { 2684 struct rb_node *n = NFS_I(inode)->access_cache.rb_node; 2685 2686 while (n != NULL) { 2687 struct nfs_access_entry *entry = 2688 rb_entry(n, struct nfs_access_entry, rb_node); 2689 int cmp = cred_fscmp(cred, entry->cred); 2690 2691 if (cmp < 0) 2692 n = n->rb_left; 2693 else if (cmp > 0) 2694 n = n->rb_right; 2695 else 2696 return entry; 2697 } 2698 return NULL; 2699 } 2700 2701 static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block) 2702 { 2703 struct nfs_inode *nfsi = NFS_I(inode); 2704 struct nfs_access_entry *cache; 2705 bool retry = true; 2706 int err; 2707 2708 spin_lock(&inode->i_lock); 2709 for(;;) { 2710 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2711 goto out_zap; 2712 cache = nfs_access_search_rbtree(inode, cred); 2713 err = -ENOENT; 2714 if (cache == NULL) 2715 goto out; 2716 /* Found an entry, is our attribute cache valid? */ 2717 if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2718 break; 2719 if (!retry) 2720 break; 2721 err = -ECHILD; 2722 if (!may_block) 2723 goto out; 2724 spin_unlock(&inode->i_lock); 2725 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); 2726 if (err) 2727 return err; 2728 spin_lock(&inode->i_lock); 2729 retry = false; 2730 } 2731 res->cred = cache->cred; 2732 res->mask = cache->mask; 2733 list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); 2734 err = 0; 2735 out: 2736 spin_unlock(&inode->i_lock); 2737 return err; 2738 out_zap: 2739 spin_unlock(&inode->i_lock); 2740 nfs_access_zap_cache(inode); 2741 return -ENOENT; 2742 } 2743 2744 static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res) 2745 { 2746 /* Only check the most recently returned cache entry, 2747 * but do it without locking. 2748 */ 2749 struct nfs_inode *nfsi = NFS_I(inode); 2750 struct nfs_access_entry *cache; 2751 int err = -ECHILD; 2752 struct list_head *lh; 2753 2754 rcu_read_lock(); 2755 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) 2756 goto out; 2757 lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru)); 2758 cache = list_entry(lh, struct nfs_access_entry, lru); 2759 if (lh == &nfsi->access_cache_entry_lru || 2760 cred_fscmp(cred, cache->cred) != 0) 2761 cache = NULL; 2762 if (cache == NULL) 2763 goto out; 2764 if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) 2765 goto out; 2766 res->cred = cache->cred; 2767 res->mask = cache->mask; 2768 err = 0; 2769 out: 2770 rcu_read_unlock(); 2771 return err; 2772 } 2773 2774 int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct 2775 nfs_access_entry *res, bool may_block) 2776 { 2777 int status; 2778 2779 status = nfs_access_get_cached_rcu(inode, cred, res); 2780 if (status != 0) 2781 status = nfs_access_get_cached_locked(inode, cred, res, 2782 may_block); 2783 2784 return status; 2785 } 2786 EXPORT_SYMBOL_GPL(nfs_access_get_cached); 2787 2788 static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) 2789 { 2790 struct nfs_inode *nfsi = NFS_I(inode); 2791 struct rb_root *root_node = &nfsi->access_cache; 2792 struct rb_node **p = &root_node->rb_node; 2793 struct rb_node *parent = NULL; 2794 struct nfs_access_entry *entry; 2795 int cmp; 2796 2797 spin_lock(&inode->i_lock); 2798 while (*p != NULL) { 2799 parent = *p; 2800 entry = rb_entry(parent, struct nfs_access_entry, rb_node); 2801 cmp = cred_fscmp(set->cred, entry->cred); 2802 2803 if (cmp < 0) 2804 p = &parent->rb_left; 2805 else if (cmp > 0) 2806 p = &parent->rb_right; 2807 else 2808 goto found; 2809 } 2810 rb_link_node(&set->rb_node, parent, p); 2811 rb_insert_color(&set->rb_node, root_node); 2812 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2813 spin_unlock(&inode->i_lock); 2814 return; 2815 found: 2816 rb_replace_node(parent, &set->rb_node, root_node); 2817 list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); 2818 list_del(&entry->lru); 2819 spin_unlock(&inode->i_lock); 2820 nfs_access_free_entry(entry); 2821 } 2822 2823 void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) 2824 { 2825 struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); 2826 if (cache == NULL) 2827 return; 2828 RB_CLEAR_NODE(&cache->rb_node); 2829 cache->cred = get_cred(set->cred); 2830 cache->mask = set->mask; 2831 2832 /* The above field assignments must be visible 2833 * before this item appears on the lru. We cannot easily 2834 * use rcu_assign_pointer, so just force the memory barrier. 2835 */ 2836 smp_wmb(); 2837 nfs_access_add_rbtree(inode, cache); 2838 2839 /* Update accounting */ 2840 smp_mb__before_atomic(); 2841 atomic_long_inc(&nfs_access_nr_entries); 2842 smp_mb__after_atomic(); 2843 2844 /* Add inode to global LRU list */ 2845 if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { 2846 spin_lock(&nfs_access_lru_lock); 2847 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) 2848 list_add_tail(&NFS_I(inode)->access_cache_inode_lru, 2849 &nfs_access_lru_list); 2850 spin_unlock(&nfs_access_lru_lock); 2851 } 2852 nfs_access_cache_enforce_limit(); 2853 } 2854 EXPORT_SYMBOL_GPL(nfs_access_add_cache); 2855 2856 #define NFS_MAY_READ (NFS_ACCESS_READ) 2857 #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ 2858 NFS_ACCESS_EXTEND | \ 2859 NFS_ACCESS_DELETE) 2860 #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ 2861 NFS_ACCESS_EXTEND) 2862 #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE 2863 #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP) 2864 #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) 2865 static int 2866 nfs_access_calc_mask(u32 access_result, umode_t umode) 2867 { 2868 int mask = 0; 2869 2870 if (access_result & NFS_MAY_READ) 2871 mask |= MAY_READ; 2872 if (S_ISDIR(umode)) { 2873 if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE) 2874 mask |= MAY_WRITE; 2875 if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP) 2876 mask |= MAY_EXEC; 2877 } else if (S_ISREG(umode)) { 2878 if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE) 2879 mask |= MAY_WRITE; 2880 if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE) 2881 mask |= MAY_EXEC; 2882 } else if (access_result & NFS_MAY_WRITE) 2883 mask |= MAY_WRITE; 2884 return mask; 2885 } 2886 2887 void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) 2888 { 2889 entry->mask = access_result; 2890 } 2891 EXPORT_SYMBOL_GPL(nfs_access_set_mask); 2892 2893 static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask) 2894 { 2895 struct nfs_access_entry cache; 2896 bool may_block = (mask & MAY_NOT_BLOCK) == 0; 2897 int cache_mask = -1; 2898 int status; 2899 2900 trace_nfs_access_enter(inode); 2901 2902 status = nfs_access_get_cached(inode, cred, &cache, may_block); 2903 if (status == 0) 2904 goto out_cached; 2905 2906 status = -ECHILD; 2907 if (!may_block) 2908 goto out; 2909 2910 /* 2911 * Determine which access bits we want to ask for... 2912 */ 2913 cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; 2914 if (nfs_server_capable(inode, NFS_CAP_XATTR)) { 2915 cache.mask |= NFS_ACCESS_XAREAD | NFS_ACCESS_XAWRITE | 2916 NFS_ACCESS_XALIST; 2917 } 2918 if (S_ISDIR(inode->i_mode)) 2919 cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; 2920 else 2921 cache.mask |= NFS_ACCESS_EXECUTE; 2922 cache.cred = cred; 2923 status = NFS_PROTO(inode)->access(inode, &cache); 2924 if (status != 0) { 2925 if (status == -ESTALE) { 2926 if (!S_ISDIR(inode->i_mode)) 2927 nfs_set_inode_stale(inode); 2928 else 2929 nfs_zap_caches(inode); 2930 } 2931 goto out; 2932 } 2933 nfs_access_add_cache(inode, &cache); 2934 out_cached: 2935 cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); 2936 if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) 2937 status = -EACCES; 2938 out: 2939 trace_nfs_access_exit(inode, mask, cache_mask, status); 2940 return status; 2941 } 2942 2943 static int nfs_open_permission_mask(int openflags) 2944 { 2945 int mask = 0; 2946 2947 if (openflags & __FMODE_EXEC) { 2948 /* ONLY check exec rights */ 2949 mask = MAY_EXEC; 2950 } else { 2951 if ((openflags & O_ACCMODE) != O_WRONLY) 2952 mask |= MAY_READ; 2953 if ((openflags & O_ACCMODE) != O_RDONLY) 2954 mask |= MAY_WRITE; 2955 } 2956 2957 return mask; 2958 } 2959 2960 int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags) 2961 { 2962 return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); 2963 } 2964 EXPORT_SYMBOL_GPL(nfs_may_open); 2965 2966 static int nfs_execute_ok(struct inode *inode, int mask) 2967 { 2968 struct nfs_server *server = NFS_SERVER(inode); 2969 int ret = 0; 2970 2971 if (S_ISDIR(inode->i_mode)) 2972 return 0; 2973 if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_MODE)) { 2974 if (mask & MAY_NOT_BLOCK) 2975 return -ECHILD; 2976 ret = __nfs_revalidate_inode(server, inode); 2977 } 2978 if (ret == 0 && !execute_ok(inode)) 2979 ret = -EACCES; 2980 return ret; 2981 } 2982 2983 int nfs_permission(struct user_namespace *mnt_userns, 2984 struct inode *inode, 2985 int mask) 2986 { 2987 const struct cred *cred = current_cred(); 2988 int res = 0; 2989 2990 nfs_inc_stats(inode, NFSIOS_VFSACCESS); 2991 2992 if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 2993 goto out; 2994 /* Is this sys_access() ? */ 2995 if (mask & (MAY_ACCESS | MAY_CHDIR)) 2996 goto force_lookup; 2997 2998 switch (inode->i_mode & S_IFMT) { 2999 case S_IFLNK: 3000 goto out; 3001 case S_IFREG: 3002 if ((mask & MAY_OPEN) && 3003 nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) 3004 return 0; 3005 break; 3006 case S_IFDIR: 3007 /* 3008 * Optimize away all write operations, since the server 3009 * will check permissions when we perform the op. 3010 */ 3011 if ((mask & MAY_WRITE) && !(mask & MAY_READ)) 3012 goto out; 3013 } 3014 3015 force_lookup: 3016 if (!NFS_PROTO(inode)->access) 3017 goto out_notsup; 3018 3019 res = nfs_do_access(inode, cred, mask); 3020 out: 3021 if (!res && (mask & MAY_EXEC)) 3022 res = nfs_execute_ok(inode, mask); 3023 3024 dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", 3025 inode->i_sb->s_id, inode->i_ino, mask, res); 3026 return res; 3027 out_notsup: 3028 if (mask & MAY_NOT_BLOCK) 3029 return -ECHILD; 3030 3031 res = nfs_revalidate_inode(inode, NFS_INO_INVALID_MODE | 3032 NFS_INO_INVALID_OTHER); 3033 if (res == 0) 3034 res = generic_permission(&init_user_ns, inode, mask); 3035 goto out; 3036 } 3037 EXPORT_SYMBOL_GPL(nfs_permission); 3038