1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* dir.c: AFS filesystem directory handling 3 * 4 * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 */ 7 8 #include <linux/kernel.h> 9 #include <linux/fs.h> 10 #include <linux/namei.h> 11 #include <linux/pagemap.h> 12 #include <linux/swap.h> 13 #include <linux/ctype.h> 14 #include <linux/sched.h> 15 #include <linux/task_io_accounting_ops.h> 16 #include "internal.h" 17 #include "afs_fs.h" 18 #include "xdr_fs.h" 19 20 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry, 21 unsigned int flags); 22 static int afs_dir_open(struct inode *inode, struct file *file); 23 static int afs_readdir(struct file *file, struct dir_context *ctx); 24 static int afs_d_revalidate(struct dentry *dentry, unsigned int flags); 25 static int afs_d_delete(const struct dentry *dentry); 26 static void afs_d_iput(struct dentry *dentry, struct inode *inode); 27 static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen, 28 loff_t fpos, u64 ino, unsigned dtype); 29 static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen, 30 loff_t fpos, u64 ino, unsigned dtype); 31 static int afs_create(struct user_namespace *mnt_userns, struct inode *dir, 32 struct dentry *dentry, umode_t mode, bool excl); 33 static int afs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 34 struct dentry *dentry, umode_t mode); 35 static int afs_rmdir(struct inode *dir, struct dentry *dentry); 36 static int afs_unlink(struct inode *dir, struct dentry *dentry); 37 static int afs_link(struct dentry *from, struct inode *dir, 38 struct dentry *dentry); 39 static int afs_symlink(struct user_namespace *mnt_userns, struct inode *dir, 40 struct dentry *dentry, const char *content); 41 static int afs_rename(struct user_namespace *mnt_userns, struct inode *old_dir, 42 struct dentry *old_dentry, struct inode *new_dir, 43 struct dentry *new_dentry, unsigned int flags); 44 static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags); 45 static void afs_dir_invalidatepage(struct page *page, unsigned int offset, 46 unsigned int length); 47 48 static int afs_dir_set_page_dirty(struct page *page) 49 { 50 BUG(); /* This should never happen. */ 51 } 52 53 const struct file_operations afs_dir_file_operations = { 54 .open = afs_dir_open, 55 .release = afs_release, 56 .iterate_shared = afs_readdir, 57 .lock = afs_lock, 58 .llseek = generic_file_llseek, 59 }; 60 61 const struct inode_operations afs_dir_inode_operations = { 62 .create = afs_create, 63 .lookup = afs_lookup, 64 .link = afs_link, 65 .unlink = afs_unlink, 66 .symlink = afs_symlink, 67 .mkdir = afs_mkdir, 68 .rmdir = afs_rmdir, 69 .rename = afs_rename, 70 .permission = afs_permission, 71 .getattr = afs_getattr, 72 .setattr = afs_setattr, 73 }; 74 75 const struct address_space_operations afs_dir_aops = { 76 .set_page_dirty = afs_dir_set_page_dirty, 77 .releasepage = afs_dir_releasepage, 78 .invalidatepage = afs_dir_invalidatepage, 79 }; 80 81 const struct dentry_operations afs_fs_dentry_operations = { 82 .d_revalidate = afs_d_revalidate, 83 .d_delete = afs_d_delete, 84 .d_release = afs_d_release, 85 .d_automount = afs_d_automount, 86 .d_iput = afs_d_iput, 87 }; 88 89 struct afs_lookup_one_cookie { 90 struct dir_context ctx; 91 struct qstr name; 92 bool found; 93 struct afs_fid fid; 94 }; 95 96 struct afs_lookup_cookie { 97 struct dir_context ctx; 98 struct qstr name; 99 bool found; 100 bool one_only; 101 unsigned short nr_fids; 102 struct afs_fid fids[50]; 103 }; 104 105 /* 106 * Drop the refs that we're holding on the pages we were reading into. We've 107 * got refs on the first nr_pages pages. 108 */ 109 static void afs_dir_read_cleanup(struct afs_read *req) 110 { 111 struct address_space *mapping = req->vnode->vfs_inode.i_mapping; 112 struct page *page; 113 pgoff_t last = req->nr_pages - 1; 114 115 XA_STATE(xas, &mapping->i_pages, 0); 116 117 if (unlikely(!req->nr_pages)) 118 return; 119 120 rcu_read_lock(); 121 xas_for_each(&xas, page, last) { 122 if (xas_retry(&xas, page)) 123 continue; 124 BUG_ON(xa_is_value(page)); 125 BUG_ON(PageCompound(page)); 126 ASSERTCMP(page->mapping, ==, mapping); 127 128 put_page(page); 129 } 130 131 rcu_read_unlock(); 132 } 133 134 /* 135 * check that a directory page is valid 136 */ 137 static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page, 138 loff_t i_size) 139 { 140 struct afs_xdr_dir_page *dbuf; 141 loff_t latter, off; 142 int tmp, qty; 143 144 /* Determine how many magic numbers there should be in this page, but 145 * we must take care because the directory may change size under us. 146 */ 147 off = page_offset(page); 148 if (i_size <= off) 149 goto checked; 150 151 latter = i_size - off; 152 if (latter >= PAGE_SIZE) 153 qty = PAGE_SIZE; 154 else 155 qty = latter; 156 qty /= sizeof(union afs_xdr_dir_block); 157 158 /* check them */ 159 dbuf = kmap_atomic(page); 160 for (tmp = 0; tmp < qty; tmp++) { 161 if (dbuf->blocks[tmp].hdr.magic != AFS_DIR_MAGIC) { 162 printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n", 163 __func__, dvnode->vfs_inode.i_ino, tmp, qty, 164 ntohs(dbuf->blocks[tmp].hdr.magic)); 165 trace_afs_dir_check_failed(dvnode, off, i_size); 166 kunmap(page); 167 trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic); 168 goto error; 169 } 170 171 /* Make sure each block is NUL terminated so we can reasonably 172 * use string functions on it. The filenames in the page 173 * *should* be NUL-terminated anyway. 174 */ 175 ((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0; 176 } 177 178 kunmap_atomic(dbuf); 179 180 checked: 181 afs_stat_v(dvnode, n_read_dir); 182 return true; 183 184 error: 185 return false; 186 } 187 188 /* 189 * Dump the contents of a directory. 190 */ 191 static void afs_dir_dump(struct afs_vnode *dvnode, struct afs_read *req) 192 { 193 struct afs_xdr_dir_page *dbuf; 194 struct address_space *mapping = dvnode->vfs_inode.i_mapping; 195 struct page *page; 196 unsigned int i, qty = PAGE_SIZE / sizeof(union afs_xdr_dir_block); 197 pgoff_t last = req->nr_pages - 1; 198 199 XA_STATE(xas, &mapping->i_pages, 0); 200 201 pr_warn("DIR %llx:%llx f=%llx l=%llx al=%llx\n", 202 dvnode->fid.vid, dvnode->fid.vnode, 203 req->file_size, req->len, req->actual_len); 204 pr_warn("DIR %llx %x %zx %zx\n", 205 req->pos, req->nr_pages, 206 req->iter->iov_offset, iov_iter_count(req->iter)); 207 208 xas_for_each(&xas, page, last) { 209 if (xas_retry(&xas, page)) 210 continue; 211 212 BUG_ON(PageCompound(page)); 213 BUG_ON(page->mapping != mapping); 214 215 dbuf = kmap_atomic(page); 216 for (i = 0; i < qty; i++) { 217 union afs_xdr_dir_block *block = &dbuf->blocks[i]; 218 219 pr_warn("[%02lx] %32phN\n", page->index * qty + i, block); 220 } 221 kunmap_atomic(dbuf); 222 } 223 } 224 225 /* 226 * Check all the pages in a directory. All the pages are held pinned. 227 */ 228 static int afs_dir_check(struct afs_vnode *dvnode, struct afs_read *req) 229 { 230 struct address_space *mapping = dvnode->vfs_inode.i_mapping; 231 struct page *page; 232 pgoff_t last = req->nr_pages - 1; 233 int ret = 0; 234 235 XA_STATE(xas, &mapping->i_pages, 0); 236 237 if (unlikely(!req->nr_pages)) 238 return 0; 239 240 rcu_read_lock(); 241 xas_for_each(&xas, page, last) { 242 if (xas_retry(&xas, page)) 243 continue; 244 245 BUG_ON(PageCompound(page)); 246 BUG_ON(page->mapping != mapping); 247 248 if (!afs_dir_check_page(dvnode, page, req->file_size)) { 249 afs_dir_dump(dvnode, req); 250 ret = -EIO; 251 break; 252 } 253 } 254 255 rcu_read_unlock(); 256 return ret; 257 } 258 259 /* 260 * open an AFS directory file 261 */ 262 static int afs_dir_open(struct inode *inode, struct file *file) 263 { 264 _enter("{%lu}", inode->i_ino); 265 266 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048); 267 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32); 268 269 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags)) 270 return -ENOENT; 271 272 return afs_open(inode, file); 273 } 274 275 /* 276 * Read the directory into the pagecache in one go, scrubbing the previous 277 * contents. The list of pages is returned, pinning them so that they don't 278 * get reclaimed during the iteration. 279 */ 280 static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key) 281 __acquires(&dvnode->validate_lock) 282 { 283 struct afs_read *req; 284 loff_t i_size; 285 int nr_pages, i, n; 286 int ret; 287 288 _enter(""); 289 290 req = kzalloc(sizeof(*req), GFP_KERNEL); 291 if (!req) 292 return ERR_PTR(-ENOMEM); 293 294 refcount_set(&req->usage, 1); 295 req->vnode = dvnode; 296 req->key = key_get(key); 297 req->cleanup = afs_dir_read_cleanup; 298 299 expand: 300 i_size = i_size_read(&dvnode->vfs_inode); 301 if (i_size < 2048) { 302 ret = afs_bad(dvnode, afs_file_error_dir_small); 303 goto error; 304 } 305 if (i_size > 2048 * 1024) { 306 trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big); 307 ret = -EFBIG; 308 goto error; 309 } 310 311 _enter("%llu", i_size); 312 313 nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE; 314 315 req->actual_len = i_size; /* May change */ 316 req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */ 317 req->data_version = dvnode->status.data_version; /* May change */ 318 iov_iter_xarray(&req->def_iter, READ, &dvnode->vfs_inode.i_mapping->i_pages, 319 0, i_size); 320 req->iter = &req->def_iter; 321 322 /* Fill in any gaps that we might find where the memory reclaimer has 323 * been at work and pin all the pages. If there are any gaps, we will 324 * need to reread the entire directory contents. 325 */ 326 i = req->nr_pages; 327 while (i < nr_pages) { 328 struct page *pages[8], *page; 329 330 n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i, 331 min_t(unsigned int, nr_pages - i, 332 ARRAY_SIZE(pages)), 333 pages); 334 _debug("find %u at %u/%u", n, i, nr_pages); 335 336 if (n == 0) { 337 gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask; 338 339 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) 340 afs_stat_v(dvnode, n_inval); 341 342 ret = -ENOMEM; 343 page = __page_cache_alloc(gfp); 344 if (!page) 345 goto error; 346 ret = add_to_page_cache_lru(page, 347 dvnode->vfs_inode.i_mapping, 348 i, gfp); 349 if (ret < 0) 350 goto error; 351 352 attach_page_private(page, (void *)1); 353 unlock_page(page); 354 req->nr_pages++; 355 i++; 356 } else { 357 req->nr_pages += n; 358 i += n; 359 } 360 } 361 362 /* If we're going to reload, we need to lock all the pages to prevent 363 * races. 364 */ 365 ret = -ERESTARTSYS; 366 if (down_read_killable(&dvnode->validate_lock) < 0) 367 goto error; 368 369 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) 370 goto success; 371 372 up_read(&dvnode->validate_lock); 373 if (down_write_killable(&dvnode->validate_lock) < 0) 374 goto error; 375 376 if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) { 377 trace_afs_reload_dir(dvnode); 378 ret = afs_fetch_data(dvnode, req); 379 if (ret < 0) 380 goto error_unlock; 381 382 task_io_account_read(PAGE_SIZE * req->nr_pages); 383 384 if (req->len < req->file_size) { 385 /* The content has grown, so we need to expand the 386 * buffer. 387 */ 388 up_write(&dvnode->validate_lock); 389 goto expand; 390 } 391 392 /* Validate the data we just read. */ 393 ret = afs_dir_check(dvnode, req); 394 if (ret < 0) 395 goto error_unlock; 396 397 // TODO: Trim excess pages 398 399 set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags); 400 } 401 402 downgrade_write(&dvnode->validate_lock); 403 success: 404 return req; 405 406 error_unlock: 407 up_write(&dvnode->validate_lock); 408 error: 409 afs_put_read(req); 410 _leave(" = %d", ret); 411 return ERR_PTR(ret); 412 } 413 414 /* 415 * deal with one block in an AFS directory 416 */ 417 static int afs_dir_iterate_block(struct afs_vnode *dvnode, 418 struct dir_context *ctx, 419 union afs_xdr_dir_block *block, 420 unsigned blkoff) 421 { 422 union afs_xdr_dirent *dire; 423 unsigned offset, next, curr, nr_slots; 424 size_t nlen; 425 int tmp; 426 427 _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block); 428 429 curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent); 430 431 /* walk through the block, an entry at a time */ 432 for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS); 433 offset < AFS_DIR_SLOTS_PER_BLOCK; 434 offset = next 435 ) { 436 /* skip entries marked unused in the bitmap */ 437 if (!(block->hdr.bitmap[offset / 8] & 438 (1 << (offset % 8)))) { 439 _debug("ENT[%zu.%u]: unused", 440 blkoff / sizeof(union afs_xdr_dir_block), offset); 441 next = offset + 1; 442 if (offset >= curr) 443 ctx->pos = blkoff + 444 next * sizeof(union afs_xdr_dirent); 445 continue; 446 } 447 448 /* got a valid entry */ 449 dire = &block->dirents[offset]; 450 nlen = strnlen(dire->u.name, 451 sizeof(*block) - 452 offset * sizeof(union afs_xdr_dirent)); 453 if (nlen > AFSNAMEMAX - 1) { 454 _debug("ENT[%zu]: name too long (len %u/%zu)", 455 blkoff / sizeof(union afs_xdr_dir_block), 456 offset, nlen); 457 return afs_bad(dvnode, afs_file_error_dir_name_too_long); 458 } 459 460 _debug("ENT[%zu.%u]: %s %zu \"%s\"", 461 blkoff / sizeof(union afs_xdr_dir_block), offset, 462 (offset < curr ? "skip" : "fill"), 463 nlen, dire->u.name); 464 465 nr_slots = afs_dir_calc_slots(nlen); 466 next = offset + nr_slots; 467 if (next > AFS_DIR_SLOTS_PER_BLOCK) { 468 _debug("ENT[%zu.%u]:" 469 " %u extends beyond end dir block" 470 " (len %zu)", 471 blkoff / sizeof(union afs_xdr_dir_block), 472 offset, next, nlen); 473 return afs_bad(dvnode, afs_file_error_dir_over_end); 474 } 475 476 /* Check that the name-extension dirents are all allocated */ 477 for (tmp = 1; tmp < nr_slots; tmp++) { 478 unsigned int ix = offset + tmp; 479 if (!(block->hdr.bitmap[ix / 8] & (1 << (ix % 8)))) { 480 _debug("ENT[%zu.u]:" 481 " %u unmarked extension (%u/%u)", 482 blkoff / sizeof(union afs_xdr_dir_block), 483 offset, tmp, nr_slots); 484 return afs_bad(dvnode, afs_file_error_dir_unmarked_ext); 485 } 486 } 487 488 /* skip if starts before the current position */ 489 if (offset < curr) 490 continue; 491 492 /* found the next entry */ 493 if (!dir_emit(ctx, dire->u.name, nlen, 494 ntohl(dire->u.vnode), 495 (ctx->actor == afs_lookup_filldir || 496 ctx->actor == afs_lookup_one_filldir)? 497 ntohl(dire->u.unique) : DT_UNKNOWN)) { 498 _leave(" = 0 [full]"); 499 return 0; 500 } 501 502 ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent); 503 } 504 505 _leave(" = 1 [more]"); 506 return 1; 507 } 508 509 /* 510 * iterate through the data blob that lists the contents of an AFS directory 511 */ 512 static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx, 513 struct key *key, afs_dataversion_t *_dir_version) 514 { 515 struct afs_vnode *dvnode = AFS_FS_I(dir); 516 struct afs_xdr_dir_page *dbuf; 517 union afs_xdr_dir_block *dblock; 518 struct afs_read *req; 519 struct page *page; 520 unsigned blkoff, limit; 521 void __rcu **slot; 522 int ret; 523 524 _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos); 525 526 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) { 527 _leave(" = -ESTALE"); 528 return -ESTALE; 529 } 530 531 req = afs_read_dir(dvnode, key); 532 if (IS_ERR(req)) 533 return PTR_ERR(req); 534 *_dir_version = req->data_version; 535 536 /* round the file position up to the next entry boundary */ 537 ctx->pos += sizeof(union afs_xdr_dirent) - 1; 538 ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1); 539 540 /* walk through the blocks in sequence */ 541 ret = 0; 542 while (ctx->pos < req->actual_len) { 543 blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1); 544 545 /* Fetch the appropriate page from the directory and re-add it 546 * to the LRU. We have all the pages pinned with an extra ref. 547 */ 548 rcu_read_lock(); 549 page = NULL; 550 slot = radix_tree_lookup_slot(&dvnode->vfs_inode.i_mapping->i_pages, 551 blkoff / PAGE_SIZE); 552 if (slot) 553 page = radix_tree_deref_slot(slot); 554 rcu_read_unlock(); 555 if (!page) { 556 ret = afs_bad(dvnode, afs_file_error_dir_missing_page); 557 break; 558 } 559 mark_page_accessed(page); 560 561 limit = blkoff & ~(PAGE_SIZE - 1); 562 563 dbuf = kmap(page); 564 565 /* deal with the individual blocks stashed on this page */ 566 do { 567 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) / 568 sizeof(union afs_xdr_dir_block)]; 569 ret = afs_dir_iterate_block(dvnode, ctx, dblock, blkoff); 570 if (ret != 1) { 571 kunmap(page); 572 goto out; 573 } 574 575 blkoff += sizeof(union afs_xdr_dir_block); 576 577 } while (ctx->pos < dir->i_size && blkoff < limit); 578 579 kunmap(page); 580 ret = 0; 581 } 582 583 out: 584 up_read(&dvnode->validate_lock); 585 afs_put_read(req); 586 _leave(" = %d", ret); 587 return ret; 588 } 589 590 /* 591 * read an AFS directory 592 */ 593 static int afs_readdir(struct file *file, struct dir_context *ctx) 594 { 595 afs_dataversion_t dir_version; 596 597 return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file), 598 &dir_version); 599 } 600 601 /* 602 * Search the directory for a single name 603 * - if afs_dir_iterate_block() spots this function, it'll pass the FID 604 * uniquifier through dtype 605 */ 606 static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, 607 int nlen, loff_t fpos, u64 ino, unsigned dtype) 608 { 609 struct afs_lookup_one_cookie *cookie = 610 container_of(ctx, struct afs_lookup_one_cookie, ctx); 611 612 _enter("{%s,%u},%s,%u,,%llu,%u", 613 cookie->name.name, cookie->name.len, name, nlen, 614 (unsigned long long) ino, dtype); 615 616 /* insanity checks first */ 617 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048); 618 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32); 619 620 if (cookie->name.len != nlen || 621 memcmp(cookie->name.name, name, nlen) != 0) { 622 _leave(" = 0 [no]"); 623 return 0; 624 } 625 626 cookie->fid.vnode = ino; 627 cookie->fid.unique = dtype; 628 cookie->found = 1; 629 630 _leave(" = -1 [found]"); 631 return -1; 632 } 633 634 /* 635 * Do a lookup of a single name in a directory 636 * - just returns the FID the dentry name maps to if found 637 */ 638 static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry, 639 struct afs_fid *fid, struct key *key, 640 afs_dataversion_t *_dir_version) 641 { 642 struct afs_super_info *as = dir->i_sb->s_fs_info; 643 struct afs_lookup_one_cookie cookie = { 644 .ctx.actor = afs_lookup_one_filldir, 645 .name = dentry->d_name, 646 .fid.vid = as->volume->vid 647 }; 648 int ret; 649 650 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry); 651 652 /* search the directory */ 653 ret = afs_dir_iterate(dir, &cookie.ctx, key, _dir_version); 654 if (ret < 0) { 655 _leave(" = %d [iter]", ret); 656 return ret; 657 } 658 659 ret = -ENOENT; 660 if (!cookie.found) { 661 _leave(" = -ENOENT [not found]"); 662 return -ENOENT; 663 } 664 665 *fid = cookie.fid; 666 _leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique); 667 return 0; 668 } 669 670 /* 671 * search the directory for a name 672 * - if afs_dir_iterate_block() spots this function, it'll pass the FID 673 * uniquifier through dtype 674 */ 675 static int afs_lookup_filldir(struct dir_context *ctx, const char *name, 676 int nlen, loff_t fpos, u64 ino, unsigned dtype) 677 { 678 struct afs_lookup_cookie *cookie = 679 container_of(ctx, struct afs_lookup_cookie, ctx); 680 int ret; 681 682 _enter("{%s,%u},%s,%u,,%llu,%u", 683 cookie->name.name, cookie->name.len, name, nlen, 684 (unsigned long long) ino, dtype); 685 686 /* insanity checks first */ 687 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048); 688 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32); 689 690 if (cookie->found) { 691 if (cookie->nr_fids < 50) { 692 cookie->fids[cookie->nr_fids].vnode = ino; 693 cookie->fids[cookie->nr_fids].unique = dtype; 694 cookie->nr_fids++; 695 } 696 } else if (cookie->name.len == nlen && 697 memcmp(cookie->name.name, name, nlen) == 0) { 698 cookie->fids[1].vnode = ino; 699 cookie->fids[1].unique = dtype; 700 cookie->found = 1; 701 if (cookie->one_only) 702 return -1; 703 } 704 705 ret = cookie->nr_fids >= 50 ? -1 : 0; 706 _leave(" = %d", ret); 707 return ret; 708 } 709 710 /* 711 * Deal with the result of a successful lookup operation. Turn all the files 712 * into inodes and save the first one - which is the one we actually want. 713 */ 714 static void afs_do_lookup_success(struct afs_operation *op) 715 { 716 struct afs_vnode_param *vp; 717 struct afs_vnode *vnode; 718 struct inode *inode; 719 u32 abort_code; 720 int i; 721 722 _enter(""); 723 724 for (i = 0; i < op->nr_files; i++) { 725 switch (i) { 726 case 0: 727 vp = &op->file[0]; 728 abort_code = vp->scb.status.abort_code; 729 if (abort_code != 0) { 730 op->ac.abort_code = abort_code; 731 op->error = afs_abort_to_error(abort_code); 732 } 733 break; 734 735 case 1: 736 vp = &op->file[1]; 737 break; 738 739 default: 740 vp = &op->more_files[i - 2]; 741 break; 742 } 743 744 if (!vp->scb.have_status && !vp->scb.have_error) 745 continue; 746 747 _debug("do [%u]", i); 748 if (vp->vnode) { 749 if (!test_bit(AFS_VNODE_UNSET, &vp->vnode->flags)) 750 afs_vnode_commit_status(op, vp); 751 } else if (vp->scb.status.abort_code == 0) { 752 inode = afs_iget(op, vp); 753 if (!IS_ERR(inode)) { 754 vnode = AFS_FS_I(inode); 755 afs_cache_permit(vnode, op->key, 756 0 /* Assume vnode->cb_break is 0 */ + 757 op->cb_v_break, 758 &vp->scb); 759 vp->vnode = vnode; 760 vp->put_vnode = true; 761 } 762 } else { 763 _debug("- abort %d %llx:%llx.%x", 764 vp->scb.status.abort_code, 765 vp->fid.vid, vp->fid.vnode, vp->fid.unique); 766 } 767 } 768 769 _leave(""); 770 } 771 772 static const struct afs_operation_ops afs_inline_bulk_status_operation = { 773 .issue_afs_rpc = afs_fs_inline_bulk_status, 774 .issue_yfs_rpc = yfs_fs_inline_bulk_status, 775 .success = afs_do_lookup_success, 776 }; 777 778 static const struct afs_operation_ops afs_lookup_fetch_status_operation = { 779 .issue_afs_rpc = afs_fs_fetch_status, 780 .issue_yfs_rpc = yfs_fs_fetch_status, 781 .success = afs_do_lookup_success, 782 .aborted = afs_check_for_remote_deletion, 783 }; 784 785 /* 786 * See if we know that the server we expect to use doesn't support 787 * FS.InlineBulkStatus. 788 */ 789 static bool afs_server_supports_ibulk(struct afs_vnode *dvnode) 790 { 791 struct afs_server_list *slist; 792 struct afs_volume *volume = dvnode->volume; 793 struct afs_server *server; 794 bool ret = true; 795 int i; 796 797 if (!test_bit(AFS_VOLUME_MAYBE_NO_IBULK, &volume->flags)) 798 return true; 799 800 rcu_read_lock(); 801 slist = rcu_dereference(volume->servers); 802 803 for (i = 0; i < slist->nr_servers; i++) { 804 server = slist->servers[i].server; 805 if (server == dvnode->cb_server) { 806 if (test_bit(AFS_SERVER_FL_NO_IBULK, &server->flags)) 807 ret = false; 808 break; 809 } 810 } 811 812 rcu_read_unlock(); 813 return ret; 814 } 815 816 /* 817 * Do a lookup in a directory. We make use of bulk lookup to query a slew of 818 * files in one go and create inodes for them. The inode of the file we were 819 * asked for is returned. 820 */ 821 static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry, 822 struct key *key) 823 { 824 struct afs_lookup_cookie *cookie; 825 struct afs_vnode_param *vp; 826 struct afs_operation *op; 827 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode; 828 struct inode *inode = NULL, *ti; 829 afs_dataversion_t data_version = READ_ONCE(dvnode->status.data_version); 830 long ret; 831 int i; 832 833 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry); 834 835 cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL); 836 if (!cookie) 837 return ERR_PTR(-ENOMEM); 838 839 for (i = 0; i < ARRAY_SIZE(cookie->fids); i++) 840 cookie->fids[i].vid = dvnode->fid.vid; 841 cookie->ctx.actor = afs_lookup_filldir; 842 cookie->name = dentry->d_name; 843 cookie->nr_fids = 2; /* slot 0 is saved for the fid we actually want 844 * and slot 1 for the directory */ 845 846 if (!afs_server_supports_ibulk(dvnode)) 847 cookie->one_only = true; 848 849 /* search the directory */ 850 ret = afs_dir_iterate(dir, &cookie->ctx, key, &data_version); 851 if (ret < 0) 852 goto out; 853 854 dentry->d_fsdata = (void *)(unsigned long)data_version; 855 856 ret = -ENOENT; 857 if (!cookie->found) 858 goto out; 859 860 /* Check to see if we already have an inode for the primary fid. */ 861 inode = ilookup5(dir->i_sb, cookie->fids[1].vnode, 862 afs_ilookup5_test_by_fid, &cookie->fids[1]); 863 if (inode) 864 goto out; /* We do */ 865 866 /* Okay, we didn't find it. We need to query the server - and whilst 867 * we're doing that, we're going to attempt to look up a bunch of other 868 * vnodes also. 869 */ 870 op = afs_alloc_operation(NULL, dvnode->volume); 871 if (IS_ERR(op)) { 872 ret = PTR_ERR(op); 873 goto out; 874 } 875 876 afs_op_set_vnode(op, 0, dvnode); 877 afs_op_set_fid(op, 1, &cookie->fids[1]); 878 879 op->nr_files = cookie->nr_fids; 880 _debug("nr_files %u", op->nr_files); 881 882 /* Need space for examining all the selected files */ 883 op->error = -ENOMEM; 884 if (op->nr_files > 2) { 885 op->more_files = kvcalloc(op->nr_files - 2, 886 sizeof(struct afs_vnode_param), 887 GFP_KERNEL); 888 if (!op->more_files) 889 goto out_op; 890 891 for (i = 2; i < op->nr_files; i++) { 892 vp = &op->more_files[i - 2]; 893 vp->fid = cookie->fids[i]; 894 895 /* Find any inodes that already exist and get their 896 * callback counters. 897 */ 898 ti = ilookup5_nowait(dir->i_sb, vp->fid.vnode, 899 afs_ilookup5_test_by_fid, &vp->fid); 900 if (!IS_ERR_OR_NULL(ti)) { 901 vnode = AFS_FS_I(ti); 902 vp->dv_before = vnode->status.data_version; 903 vp->cb_break_before = afs_calc_vnode_cb_break(vnode); 904 vp->vnode = vnode; 905 vp->put_vnode = true; 906 vp->speculative = true; /* vnode not locked */ 907 } 908 } 909 } 910 911 /* Try FS.InlineBulkStatus first. Abort codes for the individual 912 * lookups contained therein are stored in the reply without aborting 913 * the whole operation. 914 */ 915 op->error = -ENOTSUPP; 916 if (!cookie->one_only) { 917 op->ops = &afs_inline_bulk_status_operation; 918 afs_begin_vnode_operation(op); 919 afs_wait_for_operation(op); 920 } 921 922 if (op->error == -ENOTSUPP) { 923 /* We could try FS.BulkStatus next, but this aborts the entire 924 * op if any of the lookups fails - so, for the moment, revert 925 * to FS.FetchStatus for op->file[1]. 926 */ 927 op->fetch_status.which = 1; 928 op->ops = &afs_lookup_fetch_status_operation; 929 afs_begin_vnode_operation(op); 930 afs_wait_for_operation(op); 931 } 932 inode = ERR_PTR(op->error); 933 934 out_op: 935 if (op->error == 0) { 936 inode = &op->file[1].vnode->vfs_inode; 937 op->file[1].vnode = NULL; 938 } 939 940 if (op->file[0].scb.have_status) 941 dentry->d_fsdata = (void *)(unsigned long)op->file[0].scb.status.data_version; 942 else 943 dentry->d_fsdata = (void *)(unsigned long)op->file[0].dv_before; 944 ret = afs_put_operation(op); 945 out: 946 kfree(cookie); 947 _leave(""); 948 return inode ?: ERR_PTR(ret); 949 } 950 951 /* 952 * Look up an entry in a directory with @sys substitution. 953 */ 954 static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry, 955 struct key *key) 956 { 957 struct afs_sysnames *subs; 958 struct afs_net *net = afs_i2net(dir); 959 struct dentry *ret; 960 char *buf, *p, *name; 961 int len, i; 962 963 _enter(""); 964 965 ret = ERR_PTR(-ENOMEM); 966 p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL); 967 if (!buf) 968 goto out_p; 969 if (dentry->d_name.len > 4) { 970 memcpy(p, dentry->d_name.name, dentry->d_name.len - 4); 971 p += dentry->d_name.len - 4; 972 } 973 974 /* There is an ordered list of substitutes that we have to try. */ 975 read_lock(&net->sysnames_lock); 976 subs = net->sysnames; 977 refcount_inc(&subs->usage); 978 read_unlock(&net->sysnames_lock); 979 980 for (i = 0; i < subs->nr; i++) { 981 name = subs->subs[i]; 982 len = dentry->d_name.len - 4 + strlen(name); 983 if (len >= AFSNAMEMAX) { 984 ret = ERR_PTR(-ENAMETOOLONG); 985 goto out_s; 986 } 987 988 strcpy(p, name); 989 ret = lookup_one_len(buf, dentry->d_parent, len); 990 if (IS_ERR(ret) || d_is_positive(ret)) 991 goto out_s; 992 dput(ret); 993 } 994 995 /* We don't want to d_add() the @sys dentry here as we don't want to 996 * the cached dentry to hide changes to the sysnames list. 997 */ 998 ret = NULL; 999 out_s: 1000 afs_put_sysnames(subs); 1001 kfree(buf); 1002 out_p: 1003 key_put(key); 1004 return ret; 1005 } 1006 1007 /* 1008 * look up an entry in a directory 1009 */ 1010 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry, 1011 unsigned int flags) 1012 { 1013 struct afs_vnode *dvnode = AFS_FS_I(dir); 1014 struct afs_fid fid = {}; 1015 struct inode *inode; 1016 struct dentry *d; 1017 struct key *key; 1018 int ret; 1019 1020 _enter("{%llx:%llu},%p{%pd},", 1021 dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry); 1022 1023 ASSERTCMP(d_inode(dentry), ==, NULL); 1024 1025 if (dentry->d_name.len >= AFSNAMEMAX) { 1026 _leave(" = -ENAMETOOLONG"); 1027 return ERR_PTR(-ENAMETOOLONG); 1028 } 1029 1030 if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) { 1031 _leave(" = -ESTALE"); 1032 return ERR_PTR(-ESTALE); 1033 } 1034 1035 key = afs_request_key(dvnode->volume->cell); 1036 if (IS_ERR(key)) { 1037 _leave(" = %ld [key]", PTR_ERR(key)); 1038 return ERR_CAST(key); 1039 } 1040 1041 ret = afs_validate(dvnode, key); 1042 if (ret < 0) { 1043 key_put(key); 1044 _leave(" = %d [val]", ret); 1045 return ERR_PTR(ret); 1046 } 1047 1048 if (dentry->d_name.len >= 4 && 1049 dentry->d_name.name[dentry->d_name.len - 4] == '@' && 1050 dentry->d_name.name[dentry->d_name.len - 3] == 's' && 1051 dentry->d_name.name[dentry->d_name.len - 2] == 'y' && 1052 dentry->d_name.name[dentry->d_name.len - 1] == 's') 1053 return afs_lookup_atsys(dir, dentry, key); 1054 1055 afs_stat_v(dvnode, n_lookup); 1056 inode = afs_do_lookup(dir, dentry, key); 1057 key_put(key); 1058 if (inode == ERR_PTR(-ENOENT)) 1059 inode = afs_try_auto_mntpt(dentry, dir); 1060 1061 if (!IS_ERR_OR_NULL(inode)) 1062 fid = AFS_FS_I(inode)->fid; 1063 1064 _debug("splice %p", dentry->d_inode); 1065 d = d_splice_alias(inode, dentry); 1066 if (!IS_ERR_OR_NULL(d)) { 1067 d->d_fsdata = dentry->d_fsdata; 1068 trace_afs_lookup(dvnode, &d->d_name, &fid); 1069 } else { 1070 trace_afs_lookup(dvnode, &dentry->d_name, &fid); 1071 } 1072 _leave(""); 1073 return d; 1074 } 1075 1076 /* 1077 * Check the validity of a dentry under RCU conditions. 1078 */ 1079 static int afs_d_revalidate_rcu(struct dentry *dentry) 1080 { 1081 struct afs_vnode *dvnode, *vnode; 1082 struct dentry *parent; 1083 struct inode *dir, *inode; 1084 long dir_version, de_version; 1085 1086 _enter("%p", dentry); 1087 1088 /* Check the parent directory is still valid first. */ 1089 parent = READ_ONCE(dentry->d_parent); 1090 dir = d_inode_rcu(parent); 1091 if (!dir) 1092 return -ECHILD; 1093 dvnode = AFS_FS_I(dir); 1094 if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) 1095 return -ECHILD; 1096 1097 if (!afs_check_validity(dvnode)) 1098 return -ECHILD; 1099 1100 /* We only need to invalidate a dentry if the server's copy changed 1101 * behind our back. If we made the change, it's no problem. Note that 1102 * on a 32-bit system, we only have 32 bits in the dentry to store the 1103 * version. 1104 */ 1105 dir_version = (long)READ_ONCE(dvnode->status.data_version); 1106 de_version = (long)READ_ONCE(dentry->d_fsdata); 1107 if (de_version != dir_version) { 1108 dir_version = (long)READ_ONCE(dvnode->invalid_before); 1109 if (de_version - dir_version < 0) 1110 return -ECHILD; 1111 } 1112 1113 /* Check to see if the vnode referred to by the dentry still 1114 * has a callback. 1115 */ 1116 if (d_really_is_positive(dentry)) { 1117 inode = d_inode_rcu(dentry); 1118 if (inode) { 1119 vnode = AFS_FS_I(inode); 1120 if (!afs_check_validity(vnode)) 1121 return -ECHILD; 1122 } 1123 } 1124 1125 return 1; /* Still valid */ 1126 } 1127 1128 /* 1129 * check that a dentry lookup hit has found a valid entry 1130 * - NOTE! the hit can be a negative hit too, so we can't assume we have an 1131 * inode 1132 */ 1133 static int afs_d_revalidate(struct dentry *dentry, unsigned int flags) 1134 { 1135 struct afs_vnode *vnode, *dir; 1136 struct afs_fid fid; 1137 struct dentry *parent; 1138 struct inode *inode; 1139 struct key *key; 1140 afs_dataversion_t dir_version, invalid_before; 1141 long de_version; 1142 int ret; 1143 1144 if (flags & LOOKUP_RCU) 1145 return afs_d_revalidate_rcu(dentry); 1146 1147 if (d_really_is_positive(dentry)) { 1148 vnode = AFS_FS_I(d_inode(dentry)); 1149 _enter("{v={%llx:%llu} n=%pd fl=%lx},", 1150 vnode->fid.vid, vnode->fid.vnode, dentry, 1151 vnode->flags); 1152 } else { 1153 _enter("{neg n=%pd}", dentry); 1154 } 1155 1156 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell); 1157 if (IS_ERR(key)) 1158 key = NULL; 1159 1160 if (d_really_is_positive(dentry)) { 1161 inode = d_inode(dentry); 1162 if (inode) { 1163 vnode = AFS_FS_I(inode); 1164 afs_validate(vnode, key); 1165 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) 1166 goto out_bad; 1167 } 1168 } 1169 1170 /* lock down the parent dentry so we can peer at it */ 1171 parent = dget_parent(dentry); 1172 dir = AFS_FS_I(d_inode(parent)); 1173 1174 /* validate the parent directory */ 1175 afs_validate(dir, key); 1176 1177 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) { 1178 _debug("%pd: parent dir deleted", dentry); 1179 goto out_bad_parent; 1180 } 1181 1182 /* We only need to invalidate a dentry if the server's copy changed 1183 * behind our back. If we made the change, it's no problem. Note that 1184 * on a 32-bit system, we only have 32 bits in the dentry to store the 1185 * version. 1186 */ 1187 dir_version = dir->status.data_version; 1188 de_version = (long)dentry->d_fsdata; 1189 if (de_version == (long)dir_version) 1190 goto out_valid_noupdate; 1191 1192 invalid_before = dir->invalid_before; 1193 if (de_version - (long)invalid_before >= 0) 1194 goto out_valid; 1195 1196 _debug("dir modified"); 1197 afs_stat_v(dir, n_reval); 1198 1199 /* search the directory for this vnode */ 1200 ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key, &dir_version); 1201 switch (ret) { 1202 case 0: 1203 /* the filename maps to something */ 1204 if (d_really_is_negative(dentry)) 1205 goto out_bad_parent; 1206 inode = d_inode(dentry); 1207 if (is_bad_inode(inode)) { 1208 printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n", 1209 dentry); 1210 goto out_bad_parent; 1211 } 1212 1213 vnode = AFS_FS_I(inode); 1214 1215 /* if the vnode ID has changed, then the dirent points to a 1216 * different file */ 1217 if (fid.vnode != vnode->fid.vnode) { 1218 _debug("%pd: dirent changed [%llu != %llu]", 1219 dentry, fid.vnode, 1220 vnode->fid.vnode); 1221 goto not_found; 1222 } 1223 1224 /* if the vnode ID uniqifier has changed, then the file has 1225 * been deleted and replaced, and the original vnode ID has 1226 * been reused */ 1227 if (fid.unique != vnode->fid.unique) { 1228 _debug("%pd: file deleted (uq %u -> %u I:%u)", 1229 dentry, fid.unique, 1230 vnode->fid.unique, 1231 vnode->vfs_inode.i_generation); 1232 write_seqlock(&vnode->cb_lock); 1233 set_bit(AFS_VNODE_DELETED, &vnode->flags); 1234 write_sequnlock(&vnode->cb_lock); 1235 goto not_found; 1236 } 1237 goto out_valid; 1238 1239 case -ENOENT: 1240 /* the filename is unknown */ 1241 _debug("%pd: dirent not found", dentry); 1242 if (d_really_is_positive(dentry)) 1243 goto not_found; 1244 goto out_valid; 1245 1246 default: 1247 _debug("failed to iterate dir %pd: %d", 1248 parent, ret); 1249 goto out_bad_parent; 1250 } 1251 1252 out_valid: 1253 dentry->d_fsdata = (void *)(unsigned long)dir_version; 1254 out_valid_noupdate: 1255 dput(parent); 1256 key_put(key); 1257 _leave(" = 1 [valid]"); 1258 return 1; 1259 1260 /* the dirent, if it exists, now points to a different vnode */ 1261 not_found: 1262 spin_lock(&dentry->d_lock); 1263 dentry->d_flags |= DCACHE_NFSFS_RENAMED; 1264 spin_unlock(&dentry->d_lock); 1265 1266 out_bad_parent: 1267 _debug("dropping dentry %pd2", dentry); 1268 dput(parent); 1269 out_bad: 1270 key_put(key); 1271 1272 _leave(" = 0 [bad]"); 1273 return 0; 1274 } 1275 1276 /* 1277 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't 1278 * sleep) 1279 * - called from dput() when d_count is going to 0. 1280 * - return 1 to request dentry be unhashed, 0 otherwise 1281 */ 1282 static int afs_d_delete(const struct dentry *dentry) 1283 { 1284 _enter("%pd", dentry); 1285 1286 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1287 goto zap; 1288 1289 if (d_really_is_positive(dentry) && 1290 (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) || 1291 test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags))) 1292 goto zap; 1293 1294 _leave(" = 0 [keep]"); 1295 return 0; 1296 1297 zap: 1298 _leave(" = 1 [zap]"); 1299 return 1; 1300 } 1301 1302 /* 1303 * Clean up sillyrename files on dentry removal. 1304 */ 1305 static void afs_d_iput(struct dentry *dentry, struct inode *inode) 1306 { 1307 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1308 afs_silly_iput(dentry, inode); 1309 iput(inode); 1310 } 1311 1312 /* 1313 * handle dentry release 1314 */ 1315 void afs_d_release(struct dentry *dentry) 1316 { 1317 _enter("%pd", dentry); 1318 } 1319 1320 void afs_check_for_remote_deletion(struct afs_operation *op) 1321 { 1322 struct afs_vnode *vnode = op->file[0].vnode; 1323 1324 switch (op->ac.abort_code) { 1325 case VNOVNODE: 1326 set_bit(AFS_VNODE_DELETED, &vnode->flags); 1327 afs_break_callback(vnode, afs_cb_break_for_deleted); 1328 } 1329 } 1330 1331 /* 1332 * Create a new inode for create/mkdir/symlink 1333 */ 1334 static void afs_vnode_new_inode(struct afs_operation *op) 1335 { 1336 struct afs_vnode_param *vp = &op->file[1]; 1337 struct afs_vnode *vnode; 1338 struct inode *inode; 1339 1340 _enter(""); 1341 1342 ASSERTCMP(op->error, ==, 0); 1343 1344 inode = afs_iget(op, vp); 1345 if (IS_ERR(inode)) { 1346 /* ENOMEM or EINTR at a really inconvenient time - just abandon 1347 * the new directory on the server. 1348 */ 1349 op->error = PTR_ERR(inode); 1350 return; 1351 } 1352 1353 vnode = AFS_FS_I(inode); 1354 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); 1355 if (!op->error) 1356 afs_cache_permit(vnode, op->key, vnode->cb_break, &vp->scb); 1357 d_instantiate(op->dentry, inode); 1358 } 1359 1360 static void afs_create_success(struct afs_operation *op) 1361 { 1362 _enter("op=%08x", op->debug_id); 1363 op->ctime = op->file[0].scb.status.mtime_client; 1364 afs_vnode_commit_status(op, &op->file[0]); 1365 afs_update_dentry_version(op, &op->file[0], op->dentry); 1366 afs_vnode_new_inode(op); 1367 } 1368 1369 static void afs_create_edit_dir(struct afs_operation *op) 1370 { 1371 struct afs_vnode_param *dvp = &op->file[0]; 1372 struct afs_vnode_param *vp = &op->file[1]; 1373 struct afs_vnode *dvnode = dvp->vnode; 1374 1375 _enter("op=%08x", op->debug_id); 1376 1377 down_write(&dvnode->validate_lock); 1378 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) && 1379 dvnode->status.data_version == dvp->dv_before + dvp->dv_delta) 1380 afs_edit_dir_add(dvnode, &op->dentry->d_name, &vp->fid, 1381 op->create.reason); 1382 up_write(&dvnode->validate_lock); 1383 } 1384 1385 static void afs_create_put(struct afs_operation *op) 1386 { 1387 _enter("op=%08x", op->debug_id); 1388 1389 if (op->error) 1390 d_drop(op->dentry); 1391 } 1392 1393 static const struct afs_operation_ops afs_mkdir_operation = { 1394 .issue_afs_rpc = afs_fs_make_dir, 1395 .issue_yfs_rpc = yfs_fs_make_dir, 1396 .success = afs_create_success, 1397 .aborted = afs_check_for_remote_deletion, 1398 .edit_dir = afs_create_edit_dir, 1399 .put = afs_create_put, 1400 }; 1401 1402 /* 1403 * create a directory on an AFS filesystem 1404 */ 1405 static int afs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 1406 struct dentry *dentry, umode_t mode) 1407 { 1408 struct afs_operation *op; 1409 struct afs_vnode *dvnode = AFS_FS_I(dir); 1410 1411 _enter("{%llx:%llu},{%pd},%ho", 1412 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode); 1413 1414 op = afs_alloc_operation(NULL, dvnode->volume); 1415 if (IS_ERR(op)) { 1416 d_drop(dentry); 1417 return PTR_ERR(op); 1418 } 1419 1420 afs_op_set_vnode(op, 0, dvnode); 1421 op->file[0].dv_delta = 1; 1422 op->file[0].modification = true; 1423 op->file[0].update_ctime = true; 1424 op->dentry = dentry; 1425 op->create.mode = S_IFDIR | mode; 1426 op->create.reason = afs_edit_dir_for_mkdir; 1427 op->ops = &afs_mkdir_operation; 1428 return afs_do_sync_operation(op); 1429 } 1430 1431 /* 1432 * Remove a subdir from a directory. 1433 */ 1434 static void afs_dir_remove_subdir(struct dentry *dentry) 1435 { 1436 if (d_really_is_positive(dentry)) { 1437 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); 1438 1439 clear_nlink(&vnode->vfs_inode); 1440 set_bit(AFS_VNODE_DELETED, &vnode->flags); 1441 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags); 1442 clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags); 1443 } 1444 } 1445 1446 static void afs_rmdir_success(struct afs_operation *op) 1447 { 1448 _enter("op=%08x", op->debug_id); 1449 op->ctime = op->file[0].scb.status.mtime_client; 1450 afs_vnode_commit_status(op, &op->file[0]); 1451 afs_update_dentry_version(op, &op->file[0], op->dentry); 1452 } 1453 1454 static void afs_rmdir_edit_dir(struct afs_operation *op) 1455 { 1456 struct afs_vnode_param *dvp = &op->file[0]; 1457 struct afs_vnode *dvnode = dvp->vnode; 1458 1459 _enter("op=%08x", op->debug_id); 1460 afs_dir_remove_subdir(op->dentry); 1461 1462 down_write(&dvnode->validate_lock); 1463 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) && 1464 dvnode->status.data_version == dvp->dv_before + dvp->dv_delta) 1465 afs_edit_dir_remove(dvnode, &op->dentry->d_name, 1466 afs_edit_dir_for_rmdir); 1467 up_write(&dvnode->validate_lock); 1468 } 1469 1470 static void afs_rmdir_put(struct afs_operation *op) 1471 { 1472 _enter("op=%08x", op->debug_id); 1473 if (op->file[1].vnode) 1474 up_write(&op->file[1].vnode->rmdir_lock); 1475 } 1476 1477 static const struct afs_operation_ops afs_rmdir_operation = { 1478 .issue_afs_rpc = afs_fs_remove_dir, 1479 .issue_yfs_rpc = yfs_fs_remove_dir, 1480 .success = afs_rmdir_success, 1481 .aborted = afs_check_for_remote_deletion, 1482 .edit_dir = afs_rmdir_edit_dir, 1483 .put = afs_rmdir_put, 1484 }; 1485 1486 /* 1487 * remove a directory from an AFS filesystem 1488 */ 1489 static int afs_rmdir(struct inode *dir, struct dentry *dentry) 1490 { 1491 struct afs_operation *op; 1492 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL; 1493 int ret; 1494 1495 _enter("{%llx:%llu},{%pd}", 1496 dvnode->fid.vid, dvnode->fid.vnode, dentry); 1497 1498 op = afs_alloc_operation(NULL, dvnode->volume); 1499 if (IS_ERR(op)) 1500 return PTR_ERR(op); 1501 1502 afs_op_set_vnode(op, 0, dvnode); 1503 op->file[0].dv_delta = 1; 1504 op->file[0].modification = true; 1505 op->file[0].update_ctime = true; 1506 1507 op->dentry = dentry; 1508 op->ops = &afs_rmdir_operation; 1509 1510 /* Try to make sure we have a callback promise on the victim. */ 1511 if (d_really_is_positive(dentry)) { 1512 vnode = AFS_FS_I(d_inode(dentry)); 1513 ret = afs_validate(vnode, op->key); 1514 if (ret < 0) 1515 goto error; 1516 } 1517 1518 if (vnode) { 1519 ret = down_write_killable(&vnode->rmdir_lock); 1520 if (ret < 0) 1521 goto error; 1522 op->file[1].vnode = vnode; 1523 } 1524 1525 return afs_do_sync_operation(op); 1526 1527 error: 1528 return afs_put_operation(op); 1529 } 1530 1531 /* 1532 * Remove a link to a file or symlink from a directory. 1533 * 1534 * If the file was not deleted due to excess hard links, the fileserver will 1535 * break the callback promise on the file - if it had one - before it returns 1536 * to us, and if it was deleted, it won't 1537 * 1538 * However, if we didn't have a callback promise outstanding, or it was 1539 * outstanding on a different server, then it won't break it either... 1540 */ 1541 static void afs_dir_remove_link(struct afs_operation *op) 1542 { 1543 struct afs_vnode *dvnode = op->file[0].vnode; 1544 struct afs_vnode *vnode = op->file[1].vnode; 1545 struct dentry *dentry = op->dentry; 1546 int ret; 1547 1548 if (op->error != 0 || 1549 (op->file[1].scb.have_status && op->file[1].scb.have_error)) 1550 return; 1551 if (d_really_is_positive(dentry)) 1552 return; 1553 1554 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) { 1555 /* Already done */ 1556 } else if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) { 1557 write_seqlock(&vnode->cb_lock); 1558 drop_nlink(&vnode->vfs_inode); 1559 if (vnode->vfs_inode.i_nlink == 0) { 1560 set_bit(AFS_VNODE_DELETED, &vnode->flags); 1561 __afs_break_callback(vnode, afs_cb_break_for_unlink); 1562 } 1563 write_sequnlock(&vnode->cb_lock); 1564 } else { 1565 afs_break_callback(vnode, afs_cb_break_for_unlink); 1566 1567 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) 1568 _debug("AFS_VNODE_DELETED"); 1569 1570 ret = afs_validate(vnode, op->key); 1571 if (ret != -ESTALE) 1572 op->error = ret; 1573 } 1574 1575 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, op->error); 1576 } 1577 1578 static void afs_unlink_success(struct afs_operation *op) 1579 { 1580 _enter("op=%08x", op->debug_id); 1581 op->ctime = op->file[0].scb.status.mtime_client; 1582 afs_check_dir_conflict(op, &op->file[0]); 1583 afs_vnode_commit_status(op, &op->file[0]); 1584 afs_vnode_commit_status(op, &op->file[1]); 1585 afs_update_dentry_version(op, &op->file[0], op->dentry); 1586 afs_dir_remove_link(op); 1587 } 1588 1589 static void afs_unlink_edit_dir(struct afs_operation *op) 1590 { 1591 struct afs_vnode_param *dvp = &op->file[0]; 1592 struct afs_vnode *dvnode = dvp->vnode; 1593 1594 _enter("op=%08x", op->debug_id); 1595 down_write(&dvnode->validate_lock); 1596 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) && 1597 dvnode->status.data_version == dvp->dv_before + dvp->dv_delta) 1598 afs_edit_dir_remove(dvnode, &op->dentry->d_name, 1599 afs_edit_dir_for_unlink); 1600 up_write(&dvnode->validate_lock); 1601 } 1602 1603 static void afs_unlink_put(struct afs_operation *op) 1604 { 1605 _enter("op=%08x", op->debug_id); 1606 if (op->unlink.need_rehash && op->error < 0 && op->error != -ENOENT) 1607 d_rehash(op->dentry); 1608 } 1609 1610 static const struct afs_operation_ops afs_unlink_operation = { 1611 .issue_afs_rpc = afs_fs_remove_file, 1612 .issue_yfs_rpc = yfs_fs_remove_file, 1613 .success = afs_unlink_success, 1614 .aborted = afs_check_for_remote_deletion, 1615 .edit_dir = afs_unlink_edit_dir, 1616 .put = afs_unlink_put, 1617 }; 1618 1619 /* 1620 * Remove a file or symlink from an AFS filesystem. 1621 */ 1622 static int afs_unlink(struct inode *dir, struct dentry *dentry) 1623 { 1624 struct afs_operation *op; 1625 struct afs_vnode *dvnode = AFS_FS_I(dir); 1626 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); 1627 int ret; 1628 1629 _enter("{%llx:%llu},{%pd}", 1630 dvnode->fid.vid, dvnode->fid.vnode, dentry); 1631 1632 if (dentry->d_name.len >= AFSNAMEMAX) 1633 return -ENAMETOOLONG; 1634 1635 op = afs_alloc_operation(NULL, dvnode->volume); 1636 if (IS_ERR(op)) 1637 return PTR_ERR(op); 1638 1639 afs_op_set_vnode(op, 0, dvnode); 1640 op->file[0].dv_delta = 1; 1641 op->file[0].modification = true; 1642 op->file[0].update_ctime = true; 1643 1644 /* Try to make sure we have a callback promise on the victim. */ 1645 ret = afs_validate(vnode, op->key); 1646 if (ret < 0) { 1647 op->error = ret; 1648 goto error; 1649 } 1650 1651 spin_lock(&dentry->d_lock); 1652 if (d_count(dentry) > 1) { 1653 spin_unlock(&dentry->d_lock); 1654 /* Start asynchronous writeout of the inode */ 1655 write_inode_now(d_inode(dentry), 0); 1656 op->error = afs_sillyrename(dvnode, vnode, dentry, op->key); 1657 goto error; 1658 } 1659 if (!d_unhashed(dentry)) { 1660 /* Prevent a race with RCU lookup. */ 1661 __d_drop(dentry); 1662 op->unlink.need_rehash = true; 1663 } 1664 spin_unlock(&dentry->d_lock); 1665 1666 op->file[1].vnode = vnode; 1667 op->file[1].update_ctime = true; 1668 op->file[1].op_unlinked = true; 1669 op->dentry = dentry; 1670 op->ops = &afs_unlink_operation; 1671 afs_begin_vnode_operation(op); 1672 afs_wait_for_operation(op); 1673 1674 /* If there was a conflict with a third party, check the status of the 1675 * unlinked vnode. 1676 */ 1677 if (op->error == 0 && (op->flags & AFS_OPERATION_DIR_CONFLICT)) { 1678 op->file[1].update_ctime = false; 1679 op->fetch_status.which = 1; 1680 op->ops = &afs_fetch_status_operation; 1681 afs_begin_vnode_operation(op); 1682 afs_wait_for_operation(op); 1683 } 1684 1685 return afs_put_operation(op); 1686 1687 error: 1688 return afs_put_operation(op); 1689 } 1690 1691 static const struct afs_operation_ops afs_create_operation = { 1692 .issue_afs_rpc = afs_fs_create_file, 1693 .issue_yfs_rpc = yfs_fs_create_file, 1694 .success = afs_create_success, 1695 .aborted = afs_check_for_remote_deletion, 1696 .edit_dir = afs_create_edit_dir, 1697 .put = afs_create_put, 1698 }; 1699 1700 /* 1701 * create a regular file on an AFS filesystem 1702 */ 1703 static int afs_create(struct user_namespace *mnt_userns, struct inode *dir, 1704 struct dentry *dentry, umode_t mode, bool excl) 1705 { 1706 struct afs_operation *op; 1707 struct afs_vnode *dvnode = AFS_FS_I(dir); 1708 int ret = -ENAMETOOLONG; 1709 1710 _enter("{%llx:%llu},{%pd},%ho", 1711 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode); 1712 1713 if (dentry->d_name.len >= AFSNAMEMAX) 1714 goto error; 1715 1716 op = afs_alloc_operation(NULL, dvnode->volume); 1717 if (IS_ERR(op)) { 1718 ret = PTR_ERR(op); 1719 goto error; 1720 } 1721 1722 afs_op_set_vnode(op, 0, dvnode); 1723 op->file[0].dv_delta = 1; 1724 op->file[0].modification = true; 1725 op->file[0].update_ctime = true; 1726 1727 op->dentry = dentry; 1728 op->create.mode = S_IFREG | mode; 1729 op->create.reason = afs_edit_dir_for_create; 1730 op->ops = &afs_create_operation; 1731 return afs_do_sync_operation(op); 1732 1733 error: 1734 d_drop(dentry); 1735 _leave(" = %d", ret); 1736 return ret; 1737 } 1738 1739 static void afs_link_success(struct afs_operation *op) 1740 { 1741 struct afs_vnode_param *dvp = &op->file[0]; 1742 struct afs_vnode_param *vp = &op->file[1]; 1743 1744 _enter("op=%08x", op->debug_id); 1745 op->ctime = dvp->scb.status.mtime_client; 1746 afs_vnode_commit_status(op, dvp); 1747 afs_vnode_commit_status(op, vp); 1748 afs_update_dentry_version(op, dvp, op->dentry); 1749 if (op->dentry_2->d_parent == op->dentry->d_parent) 1750 afs_update_dentry_version(op, dvp, op->dentry_2); 1751 ihold(&vp->vnode->vfs_inode); 1752 d_instantiate(op->dentry, &vp->vnode->vfs_inode); 1753 } 1754 1755 static void afs_link_put(struct afs_operation *op) 1756 { 1757 _enter("op=%08x", op->debug_id); 1758 if (op->error) 1759 d_drop(op->dentry); 1760 } 1761 1762 static const struct afs_operation_ops afs_link_operation = { 1763 .issue_afs_rpc = afs_fs_link, 1764 .issue_yfs_rpc = yfs_fs_link, 1765 .success = afs_link_success, 1766 .aborted = afs_check_for_remote_deletion, 1767 .edit_dir = afs_create_edit_dir, 1768 .put = afs_link_put, 1769 }; 1770 1771 /* 1772 * create a hard link between files in an AFS filesystem 1773 */ 1774 static int afs_link(struct dentry *from, struct inode *dir, 1775 struct dentry *dentry) 1776 { 1777 struct afs_operation *op; 1778 struct afs_vnode *dvnode = AFS_FS_I(dir); 1779 struct afs_vnode *vnode = AFS_FS_I(d_inode(from)); 1780 int ret = -ENAMETOOLONG; 1781 1782 _enter("{%llx:%llu},{%llx:%llu},{%pd}", 1783 vnode->fid.vid, vnode->fid.vnode, 1784 dvnode->fid.vid, dvnode->fid.vnode, 1785 dentry); 1786 1787 if (dentry->d_name.len >= AFSNAMEMAX) 1788 goto error; 1789 1790 op = afs_alloc_operation(NULL, dvnode->volume); 1791 if (IS_ERR(op)) { 1792 ret = PTR_ERR(op); 1793 goto error; 1794 } 1795 1796 afs_op_set_vnode(op, 0, dvnode); 1797 afs_op_set_vnode(op, 1, vnode); 1798 op->file[0].dv_delta = 1; 1799 op->file[0].modification = true; 1800 op->file[0].update_ctime = true; 1801 op->file[1].update_ctime = true; 1802 1803 op->dentry = dentry; 1804 op->dentry_2 = from; 1805 op->ops = &afs_link_operation; 1806 op->create.reason = afs_edit_dir_for_link; 1807 return afs_do_sync_operation(op); 1808 1809 error: 1810 d_drop(dentry); 1811 _leave(" = %d", ret); 1812 return ret; 1813 } 1814 1815 static const struct afs_operation_ops afs_symlink_operation = { 1816 .issue_afs_rpc = afs_fs_symlink, 1817 .issue_yfs_rpc = yfs_fs_symlink, 1818 .success = afs_create_success, 1819 .aborted = afs_check_for_remote_deletion, 1820 .edit_dir = afs_create_edit_dir, 1821 .put = afs_create_put, 1822 }; 1823 1824 /* 1825 * create a symlink in an AFS filesystem 1826 */ 1827 static int afs_symlink(struct user_namespace *mnt_userns, struct inode *dir, 1828 struct dentry *dentry, const char *content) 1829 { 1830 struct afs_operation *op; 1831 struct afs_vnode *dvnode = AFS_FS_I(dir); 1832 int ret; 1833 1834 _enter("{%llx:%llu},{%pd},%s", 1835 dvnode->fid.vid, dvnode->fid.vnode, dentry, 1836 content); 1837 1838 ret = -ENAMETOOLONG; 1839 if (dentry->d_name.len >= AFSNAMEMAX) 1840 goto error; 1841 1842 ret = -EINVAL; 1843 if (strlen(content) >= AFSPATHMAX) 1844 goto error; 1845 1846 op = afs_alloc_operation(NULL, dvnode->volume); 1847 if (IS_ERR(op)) { 1848 ret = PTR_ERR(op); 1849 goto error; 1850 } 1851 1852 afs_op_set_vnode(op, 0, dvnode); 1853 op->file[0].dv_delta = 1; 1854 1855 op->dentry = dentry; 1856 op->ops = &afs_symlink_operation; 1857 op->create.reason = afs_edit_dir_for_symlink; 1858 op->create.symlink = content; 1859 return afs_do_sync_operation(op); 1860 1861 error: 1862 d_drop(dentry); 1863 _leave(" = %d", ret); 1864 return ret; 1865 } 1866 1867 static void afs_rename_success(struct afs_operation *op) 1868 { 1869 _enter("op=%08x", op->debug_id); 1870 1871 op->ctime = op->file[0].scb.status.mtime_client; 1872 afs_check_dir_conflict(op, &op->file[1]); 1873 afs_vnode_commit_status(op, &op->file[0]); 1874 if (op->file[1].vnode != op->file[0].vnode) { 1875 op->ctime = op->file[1].scb.status.mtime_client; 1876 afs_vnode_commit_status(op, &op->file[1]); 1877 } 1878 } 1879 1880 static void afs_rename_edit_dir(struct afs_operation *op) 1881 { 1882 struct afs_vnode_param *orig_dvp = &op->file[0]; 1883 struct afs_vnode_param *new_dvp = &op->file[1]; 1884 struct afs_vnode *orig_dvnode = orig_dvp->vnode; 1885 struct afs_vnode *new_dvnode = new_dvp->vnode; 1886 struct afs_vnode *vnode = AFS_FS_I(d_inode(op->dentry)); 1887 struct dentry *old_dentry = op->dentry; 1888 struct dentry *new_dentry = op->dentry_2; 1889 struct inode *new_inode; 1890 1891 _enter("op=%08x", op->debug_id); 1892 1893 if (op->rename.rehash) { 1894 d_rehash(op->rename.rehash); 1895 op->rename.rehash = NULL; 1896 } 1897 1898 down_write(&orig_dvnode->validate_lock); 1899 if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags) && 1900 orig_dvnode->status.data_version == orig_dvp->dv_before + orig_dvp->dv_delta) 1901 afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name, 1902 afs_edit_dir_for_rename_0); 1903 1904 if (new_dvnode != orig_dvnode) { 1905 up_write(&orig_dvnode->validate_lock); 1906 down_write(&new_dvnode->validate_lock); 1907 } 1908 1909 if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags) && 1910 new_dvnode->status.data_version == new_dvp->dv_before + new_dvp->dv_delta) { 1911 if (!op->rename.new_negative) 1912 afs_edit_dir_remove(new_dvnode, &new_dentry->d_name, 1913 afs_edit_dir_for_rename_1); 1914 1915 afs_edit_dir_add(new_dvnode, &new_dentry->d_name, 1916 &vnode->fid, afs_edit_dir_for_rename_2); 1917 } 1918 1919 new_inode = d_inode(new_dentry); 1920 if (new_inode) { 1921 spin_lock(&new_inode->i_lock); 1922 if (S_ISDIR(new_inode->i_mode)) 1923 clear_nlink(new_inode); 1924 else if (new_inode->i_nlink > 0) 1925 drop_nlink(new_inode); 1926 spin_unlock(&new_inode->i_lock); 1927 } 1928 1929 /* Now we can update d_fsdata on the dentries to reflect their 1930 * new parent's data_version. 1931 * 1932 * Note that if we ever implement RENAME_EXCHANGE, we'll have 1933 * to update both dentries with opposing dir versions. 1934 */ 1935 afs_update_dentry_version(op, new_dvp, op->dentry); 1936 afs_update_dentry_version(op, new_dvp, op->dentry_2); 1937 1938 d_move(old_dentry, new_dentry); 1939 1940 up_write(&new_dvnode->validate_lock); 1941 } 1942 1943 static void afs_rename_put(struct afs_operation *op) 1944 { 1945 _enter("op=%08x", op->debug_id); 1946 if (op->rename.rehash) 1947 d_rehash(op->rename.rehash); 1948 dput(op->rename.tmp); 1949 if (op->error) 1950 d_rehash(op->dentry); 1951 } 1952 1953 static const struct afs_operation_ops afs_rename_operation = { 1954 .issue_afs_rpc = afs_fs_rename, 1955 .issue_yfs_rpc = yfs_fs_rename, 1956 .success = afs_rename_success, 1957 .edit_dir = afs_rename_edit_dir, 1958 .put = afs_rename_put, 1959 }; 1960 1961 /* 1962 * rename a file in an AFS filesystem and/or move it between directories 1963 */ 1964 static int afs_rename(struct user_namespace *mnt_userns, struct inode *old_dir, 1965 struct dentry *old_dentry, struct inode *new_dir, 1966 struct dentry *new_dentry, unsigned int flags) 1967 { 1968 struct afs_operation *op; 1969 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode; 1970 int ret; 1971 1972 if (flags) 1973 return -EINVAL; 1974 1975 /* Don't allow silly-rename files be moved around. */ 1976 if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED) 1977 return -EINVAL; 1978 1979 vnode = AFS_FS_I(d_inode(old_dentry)); 1980 orig_dvnode = AFS_FS_I(old_dir); 1981 new_dvnode = AFS_FS_I(new_dir); 1982 1983 _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}", 1984 orig_dvnode->fid.vid, orig_dvnode->fid.vnode, 1985 vnode->fid.vid, vnode->fid.vnode, 1986 new_dvnode->fid.vid, new_dvnode->fid.vnode, 1987 new_dentry); 1988 1989 op = afs_alloc_operation(NULL, orig_dvnode->volume); 1990 if (IS_ERR(op)) 1991 return PTR_ERR(op); 1992 1993 afs_op_set_vnode(op, 0, orig_dvnode); 1994 afs_op_set_vnode(op, 1, new_dvnode); /* May be same as orig_dvnode */ 1995 op->file[0].dv_delta = 1; 1996 op->file[1].dv_delta = 1; 1997 op->file[0].modification = true; 1998 op->file[1].modification = true; 1999 op->file[0].update_ctime = true; 2000 op->file[1].update_ctime = true; 2001 2002 op->dentry = old_dentry; 2003 op->dentry_2 = new_dentry; 2004 op->rename.new_negative = d_is_negative(new_dentry); 2005 op->ops = &afs_rename_operation; 2006 2007 /* For non-directories, check whether the target is busy and if so, 2008 * make a copy of the dentry and then do a silly-rename. If the 2009 * silly-rename succeeds, the copied dentry is hashed and becomes the 2010 * new target. 2011 */ 2012 if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) { 2013 /* To prevent any new references to the target during the 2014 * rename, we unhash the dentry in advance. 2015 */ 2016 if (!d_unhashed(new_dentry)) { 2017 d_drop(new_dentry); 2018 op->rename.rehash = new_dentry; 2019 } 2020 2021 if (d_count(new_dentry) > 2) { 2022 /* copy the target dentry's name */ 2023 ret = -ENOMEM; 2024 op->rename.tmp = d_alloc(new_dentry->d_parent, 2025 &new_dentry->d_name); 2026 if (!op->rename.tmp) 2027 goto error; 2028 2029 ret = afs_sillyrename(new_dvnode, 2030 AFS_FS_I(d_inode(new_dentry)), 2031 new_dentry, op->key); 2032 if (ret) 2033 goto error; 2034 2035 op->dentry_2 = op->rename.tmp; 2036 op->rename.rehash = NULL; 2037 op->rename.new_negative = true; 2038 } 2039 } 2040 2041 /* This bit is potentially nasty as there's a potential race with 2042 * afs_d_revalidate{,_rcu}(). We have to change d_fsdata on the dentry 2043 * to reflect it's new parent's new data_version after the op, but 2044 * d_revalidate may see old_dentry between the op having taken place 2045 * and the version being updated. 2046 * 2047 * So drop the old_dentry for now to make other threads go through 2048 * lookup instead - which we hold a lock against. 2049 */ 2050 d_drop(old_dentry); 2051 2052 return afs_do_sync_operation(op); 2053 2054 error: 2055 return afs_put_operation(op); 2056 } 2057 2058 /* 2059 * Release a directory page and clean up its private state if it's not busy 2060 * - return true if the page can now be released, false if not 2061 */ 2062 static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags) 2063 { 2064 struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host); 2065 2066 _enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index); 2067 2068 detach_page_private(page); 2069 2070 /* The directory will need reloading. */ 2071 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) 2072 afs_stat_v(dvnode, n_relpg); 2073 return 1; 2074 } 2075 2076 /* 2077 * invalidate part or all of a page 2078 * - release a page and clean up its private data if offset is 0 (indicating 2079 * the entire page) 2080 */ 2081 static void afs_dir_invalidatepage(struct page *page, unsigned int offset, 2082 unsigned int length) 2083 { 2084 struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host); 2085 2086 _enter("{%lu},%u,%u", page->index, offset, length); 2087 2088 BUG_ON(!PageLocked(page)); 2089 2090 /* The directory will need reloading. */ 2091 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) 2092 afs_stat_v(dvnode, n_inval); 2093 2094 /* we clean up only if the entire page is being invalidated */ 2095 if (offset == 0 && length == thp_size(page)) 2096 detach_page_private(page); 2097 } 2098