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