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