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