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