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