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