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