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