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