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