1 /* dir.c: AFS filesystem directory handling 2 * 3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved. 4 * Written by David Howells (dhowells@redhat.com) 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 9 * 2 of the License, or (at your option) any later version. 10 */ 11 12 #include <linux/kernel.h> 13 #include <linux/module.h> 14 #include <linux/init.h> 15 #include <linux/fs.h> 16 #include <linux/namei.h> 17 #include <linux/pagemap.h> 18 #include <linux/ctype.h> 19 #include <linux/sched.h> 20 #include <linux/dns_resolver.h> 21 #include "internal.h" 22 23 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry, 24 unsigned int flags); 25 static struct dentry *afs_dynroot_lookup(struct inode *dir, struct dentry *dentry, 26 unsigned int flags); 27 static int afs_dir_open(struct inode *inode, struct file *file); 28 static int afs_readdir(struct file *file, struct dir_context *ctx); 29 static int afs_d_revalidate(struct dentry *dentry, unsigned int flags); 30 static int afs_d_delete(const struct dentry *dentry); 31 static void afs_d_release(struct dentry *dentry); 32 static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen, 33 loff_t fpos, u64 ino, unsigned dtype); 34 static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 35 bool excl); 36 static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode); 37 static int afs_rmdir(struct inode *dir, struct dentry *dentry); 38 static int afs_unlink(struct inode *dir, struct dentry *dentry); 39 static int afs_link(struct dentry *from, struct inode *dir, 40 struct dentry *dentry); 41 static int afs_symlink(struct inode *dir, struct dentry *dentry, 42 const char *content); 43 static int afs_rename(struct inode *old_dir, struct dentry *old_dentry, 44 struct inode *new_dir, struct dentry *new_dentry, 45 unsigned int flags); 46 47 const struct file_operations afs_dir_file_operations = { 48 .open = afs_dir_open, 49 .release = afs_release, 50 .iterate_shared = afs_readdir, 51 .lock = afs_lock, 52 .llseek = generic_file_llseek, 53 }; 54 55 const struct inode_operations afs_dir_inode_operations = { 56 .create = afs_create, 57 .lookup = afs_lookup, 58 .link = afs_link, 59 .unlink = afs_unlink, 60 .symlink = afs_symlink, 61 .mkdir = afs_mkdir, 62 .rmdir = afs_rmdir, 63 .rename = afs_rename, 64 .permission = afs_permission, 65 .getattr = afs_getattr, 66 .setattr = afs_setattr, 67 .listxattr = afs_listxattr, 68 }; 69 70 const struct file_operations afs_dynroot_file_operations = { 71 .open = dcache_dir_open, 72 .release = dcache_dir_close, 73 .iterate_shared = dcache_readdir, 74 .llseek = dcache_dir_lseek, 75 }; 76 77 const struct inode_operations afs_dynroot_inode_operations = { 78 .lookup = afs_dynroot_lookup, 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 }; 87 88 #define AFS_DIR_HASHTBL_SIZE 128 89 #define AFS_DIR_DIRENT_SIZE 32 90 #define AFS_DIRENT_PER_BLOCK 64 91 92 union afs_dirent { 93 struct { 94 uint8_t valid; 95 uint8_t unused[1]; 96 __be16 hash_next; 97 __be32 vnode; 98 __be32 unique; 99 uint8_t name[16]; 100 uint8_t overflow[4]; /* if any char of the name (inc 101 * NUL) reaches here, consume 102 * the next dirent too */ 103 } u; 104 uint8_t extended_name[32]; 105 }; 106 107 /* AFS directory page header (one at the beginning of every 2048-byte chunk) */ 108 struct afs_dir_pagehdr { 109 __be16 npages; 110 __be16 magic; 111 #define AFS_DIR_MAGIC htons(1234) 112 uint8_t nentries; 113 uint8_t bitmap[8]; 114 uint8_t pad[19]; 115 }; 116 117 /* directory block layout */ 118 union afs_dir_block { 119 120 struct afs_dir_pagehdr pagehdr; 121 122 struct { 123 struct afs_dir_pagehdr pagehdr; 124 uint8_t alloc_ctrs[128]; 125 /* dir hash table */ 126 uint16_t hashtable[AFS_DIR_HASHTBL_SIZE]; 127 } hdr; 128 129 union afs_dirent dirents[AFS_DIRENT_PER_BLOCK]; 130 }; 131 132 /* layout on a linux VM page */ 133 struct afs_dir_page { 134 union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)]; 135 }; 136 137 struct afs_lookup_cookie { 138 struct dir_context ctx; 139 struct afs_fid fid; 140 struct qstr name; 141 int found; 142 }; 143 144 /* 145 * check that a directory page is valid 146 */ 147 bool afs_dir_check_page(struct inode *dir, struct page *page) 148 { 149 struct afs_dir_page *dbuf; 150 struct afs_vnode *vnode = AFS_FS_I(dir); 151 loff_t latter, i_size, off; 152 int tmp, qty; 153 154 #if 0 155 /* check the page count */ 156 qty = desc.size / sizeof(dbuf->blocks[0]); 157 if (qty == 0) 158 goto error; 159 160 if (page->index == 0 && qty != ntohs(dbuf->blocks[0].pagehdr.npages)) { 161 printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n", 162 __func__, dir->i_ino, qty, 163 ntohs(dbuf->blocks[0].pagehdr.npages)); 164 goto error; 165 } 166 #endif 167 168 /* Determine how many magic numbers there should be in this page, but 169 * we must take care because the directory may change size under us. 170 */ 171 off = page_offset(page); 172 i_size = i_size_read(dir); 173 if (i_size <= off) 174 goto checked; 175 176 latter = i_size - off; 177 if (latter >= PAGE_SIZE) 178 qty = PAGE_SIZE; 179 else 180 qty = latter; 181 qty /= sizeof(union afs_dir_block); 182 183 /* check them */ 184 dbuf = page_address(page); 185 for (tmp = 0; tmp < qty; tmp++) { 186 if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) { 187 printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n", 188 __func__, dir->i_ino, tmp, qty, 189 ntohs(dbuf->blocks[tmp].pagehdr.magic)); 190 trace_afs_dir_check_failed(vnode, off, i_size); 191 goto error; 192 } 193 } 194 195 checked: 196 SetPageChecked(page); 197 return true; 198 199 error: 200 SetPageError(page); 201 return false; 202 } 203 204 /* 205 * discard a page cached in the pagecache 206 */ 207 static inline void afs_dir_put_page(struct page *page) 208 { 209 kunmap(page); 210 unlock_page(page); 211 put_page(page); 212 } 213 214 /* 215 * get a page into the pagecache 216 */ 217 static struct page *afs_dir_get_page(struct inode *dir, unsigned long index, 218 struct key *key) 219 { 220 struct page *page; 221 _enter("{%lu},%lu", dir->i_ino, index); 222 223 page = read_cache_page(dir->i_mapping, index, afs_page_filler, key); 224 if (!IS_ERR(page)) { 225 lock_page(page); 226 kmap(page); 227 if (unlikely(!PageChecked(page))) { 228 if (PageError(page)) 229 goto fail; 230 } 231 } 232 return page; 233 234 fail: 235 afs_dir_put_page(page); 236 _leave(" = -EIO"); 237 return ERR_PTR(-EIO); 238 } 239 240 /* 241 * open an AFS directory file 242 */ 243 static int afs_dir_open(struct inode *inode, struct file *file) 244 { 245 _enter("{%lu}", inode->i_ino); 246 247 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048); 248 BUILD_BUG_ON(sizeof(union afs_dirent) != 32); 249 250 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags)) 251 return -ENOENT; 252 253 return afs_open(inode, file); 254 } 255 256 /* 257 * deal with one block in an AFS directory 258 */ 259 static int afs_dir_iterate_block(struct dir_context *ctx, 260 union afs_dir_block *block, 261 unsigned blkoff) 262 { 263 union afs_dirent *dire; 264 unsigned offset, next, curr; 265 size_t nlen; 266 int tmp; 267 268 _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block); 269 270 curr = (ctx->pos - blkoff) / sizeof(union afs_dirent); 271 272 /* walk through the block, an entry at a time */ 273 for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries; 274 offset < AFS_DIRENT_PER_BLOCK; 275 offset = next 276 ) { 277 next = offset + 1; 278 279 /* skip entries marked unused in the bitmap */ 280 if (!(block->pagehdr.bitmap[offset / 8] & 281 (1 << (offset % 8)))) { 282 _debug("ENT[%zu.%u]: unused", 283 blkoff / sizeof(union afs_dir_block), offset); 284 if (offset >= curr) 285 ctx->pos = blkoff + 286 next * sizeof(union afs_dirent); 287 continue; 288 } 289 290 /* got a valid entry */ 291 dire = &block->dirents[offset]; 292 nlen = strnlen(dire->u.name, 293 sizeof(*block) - 294 offset * sizeof(union afs_dirent)); 295 296 _debug("ENT[%zu.%u]: %s %zu \"%s\"", 297 blkoff / sizeof(union afs_dir_block), offset, 298 (offset < curr ? "skip" : "fill"), 299 nlen, dire->u.name); 300 301 /* work out where the next possible entry is */ 302 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) { 303 if (next >= AFS_DIRENT_PER_BLOCK) { 304 _debug("ENT[%zu.%u]:" 305 " %u travelled beyond end dir block" 306 " (len %u/%zu)", 307 blkoff / sizeof(union afs_dir_block), 308 offset, next, tmp, nlen); 309 return -EIO; 310 } 311 if (!(block->pagehdr.bitmap[next / 8] & 312 (1 << (next % 8)))) { 313 _debug("ENT[%zu.%u]:" 314 " %u unmarked extension (len %u/%zu)", 315 blkoff / sizeof(union afs_dir_block), 316 offset, next, tmp, nlen); 317 return -EIO; 318 } 319 320 _debug("ENT[%zu.%u]: ext %u/%zu", 321 blkoff / sizeof(union afs_dir_block), 322 next, tmp, nlen); 323 next++; 324 } 325 326 /* skip if starts before the current position */ 327 if (offset < curr) 328 continue; 329 330 /* found the next entry */ 331 if (!dir_emit(ctx, dire->u.name, nlen, 332 ntohl(dire->u.vnode), 333 ctx->actor == afs_lookup_filldir ? 334 ntohl(dire->u.unique) : DT_UNKNOWN)) { 335 _leave(" = 0 [full]"); 336 return 0; 337 } 338 339 ctx->pos = blkoff + next * sizeof(union afs_dirent); 340 } 341 342 _leave(" = 1 [more]"); 343 return 1; 344 } 345 346 /* 347 * iterate through the data blob that lists the contents of an AFS directory 348 */ 349 static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx, 350 struct key *key) 351 { 352 union afs_dir_block *dblock; 353 struct afs_dir_page *dbuf; 354 struct page *page; 355 unsigned blkoff, limit; 356 int ret; 357 358 _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos); 359 360 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) { 361 _leave(" = -ESTALE"); 362 return -ESTALE; 363 } 364 365 /* round the file position up to the next entry boundary */ 366 ctx->pos += sizeof(union afs_dirent) - 1; 367 ctx->pos &= ~(sizeof(union afs_dirent) - 1); 368 369 /* walk through the blocks in sequence */ 370 ret = 0; 371 while (ctx->pos < dir->i_size) { 372 blkoff = ctx->pos & ~(sizeof(union afs_dir_block) - 1); 373 374 /* fetch the appropriate page from the directory */ 375 page = afs_dir_get_page(dir, blkoff / PAGE_SIZE, key); 376 if (IS_ERR(page)) { 377 ret = PTR_ERR(page); 378 break; 379 } 380 381 limit = blkoff & ~(PAGE_SIZE - 1); 382 383 dbuf = page_address(page); 384 385 /* deal with the individual blocks stashed on this page */ 386 do { 387 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) / 388 sizeof(union afs_dir_block)]; 389 ret = afs_dir_iterate_block(ctx, dblock, blkoff); 390 if (ret != 1) { 391 afs_dir_put_page(page); 392 goto out; 393 } 394 395 blkoff += sizeof(union afs_dir_block); 396 397 } while (ctx->pos < dir->i_size && blkoff < limit); 398 399 afs_dir_put_page(page); 400 ret = 0; 401 } 402 403 out: 404 _leave(" = %d", ret); 405 return ret; 406 } 407 408 /* 409 * read an AFS directory 410 */ 411 static int afs_readdir(struct file *file, struct dir_context *ctx) 412 { 413 return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file)); 414 } 415 416 /* 417 * search the directory for a name 418 * - if afs_dir_iterate_block() spots this function, it'll pass the FID 419 * uniquifier through dtype 420 */ 421 static int afs_lookup_filldir(struct dir_context *ctx, const char *name, 422 int nlen, loff_t fpos, u64 ino, unsigned dtype) 423 { 424 struct afs_lookup_cookie *cookie = 425 container_of(ctx, struct afs_lookup_cookie, ctx); 426 427 _enter("{%s,%u},%s,%u,,%llu,%u", 428 cookie->name.name, cookie->name.len, name, nlen, 429 (unsigned long long) ino, dtype); 430 431 /* insanity checks first */ 432 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048); 433 BUILD_BUG_ON(sizeof(union afs_dirent) != 32); 434 435 if (cookie->name.len != nlen || 436 memcmp(cookie->name.name, name, nlen) != 0) { 437 _leave(" = 0 [no]"); 438 return 0; 439 } 440 441 cookie->fid.vnode = ino; 442 cookie->fid.unique = dtype; 443 cookie->found = 1; 444 445 _leave(" = -1 [found]"); 446 return -1; 447 } 448 449 /* 450 * do a lookup in a directory 451 * - just returns the FID the dentry name maps to if found 452 */ 453 static int afs_do_lookup(struct inode *dir, struct dentry *dentry, 454 struct afs_fid *fid, struct key *key) 455 { 456 struct afs_super_info *as = dir->i_sb->s_fs_info; 457 struct afs_lookup_cookie cookie = { 458 .ctx.actor = afs_lookup_filldir, 459 .name = dentry->d_name, 460 .fid.vid = as->volume->vid 461 }; 462 int ret; 463 464 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry); 465 466 /* search the directory */ 467 ret = afs_dir_iterate(dir, &cookie.ctx, key); 468 if (ret < 0) { 469 _leave(" = %d [iter]", ret); 470 return ret; 471 } 472 473 ret = -ENOENT; 474 if (!cookie.found) { 475 _leave(" = -ENOENT [not found]"); 476 return -ENOENT; 477 } 478 479 *fid = cookie.fid; 480 _leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique); 481 return 0; 482 } 483 484 /* 485 * Probe to see if a cell may exist. This prevents positive dentries from 486 * being created unnecessarily. 487 */ 488 static int afs_probe_cell_name(struct dentry *dentry) 489 { 490 struct afs_cell *cell; 491 const char *name = dentry->d_name.name; 492 size_t len = dentry->d_name.len; 493 int ret; 494 495 /* Names prefixed with a dot are R/W mounts. */ 496 if (name[0] == '.') { 497 if (len == 1) 498 return -EINVAL; 499 name++; 500 len--; 501 } 502 503 cell = afs_lookup_cell_rcu(afs_d2net(dentry), name, len); 504 if (!IS_ERR(cell)) { 505 afs_put_cell(afs_d2net(dentry), cell); 506 return 0; 507 } 508 509 ret = dns_query("afsdb", name, len, "ipv4", NULL, NULL); 510 if (ret == -ENODATA) 511 ret = -EDESTADDRREQ; 512 return ret; 513 } 514 515 /* 516 * Try to auto mount the mountpoint with pseudo directory, if the autocell 517 * operation is setted. 518 */ 519 static struct inode *afs_try_auto_mntpt(struct dentry *dentry, 520 struct inode *dir, struct afs_fid *fid) 521 { 522 struct afs_vnode *vnode = AFS_FS_I(dir); 523 struct inode *inode; 524 int ret = -ENOENT; 525 526 _enter("%p{%pd}, {%x:%u}", 527 dentry, dentry, vnode->fid.vid, vnode->fid.vnode); 528 529 if (!test_bit(AFS_VNODE_AUTOCELL, &vnode->flags)) 530 goto out; 531 532 ret = afs_probe_cell_name(dentry); 533 if (ret < 0) 534 goto out; 535 536 inode = afs_iget_pseudo_dir(dir->i_sb, false); 537 if (IS_ERR(inode)) { 538 ret = PTR_ERR(inode); 539 goto out; 540 } 541 542 *fid = AFS_FS_I(inode)->fid; 543 _leave("= %p", inode); 544 return inode; 545 546 out: 547 _leave("= %d", ret); 548 return ERR_PTR(ret); 549 } 550 551 /* 552 * look up an entry in a directory 553 */ 554 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry, 555 unsigned int flags) 556 { 557 struct afs_vnode *vnode; 558 struct afs_fid fid; 559 struct inode *inode; 560 struct key *key; 561 int ret; 562 563 vnode = AFS_FS_I(dir); 564 565 _enter("{%x:%u},%p{%pd},", 566 vnode->fid.vid, vnode->fid.vnode, dentry, dentry); 567 568 ASSERTCMP(d_inode(dentry), ==, NULL); 569 570 if (dentry->d_name.len >= AFSNAMEMAX) { 571 _leave(" = -ENAMETOOLONG"); 572 return ERR_PTR(-ENAMETOOLONG); 573 } 574 575 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) { 576 _leave(" = -ESTALE"); 577 return ERR_PTR(-ESTALE); 578 } 579 580 key = afs_request_key(vnode->volume->cell); 581 if (IS_ERR(key)) { 582 _leave(" = %ld [key]", PTR_ERR(key)); 583 return ERR_CAST(key); 584 } 585 586 ret = afs_validate(vnode, key); 587 if (ret < 0) { 588 key_put(key); 589 _leave(" = %d [val]", ret); 590 return ERR_PTR(ret); 591 } 592 593 ret = afs_do_lookup(dir, dentry, &fid, key); 594 if (ret < 0) { 595 if (ret == -ENOENT) { 596 inode = afs_try_auto_mntpt(dentry, dir, &fid); 597 if (!IS_ERR(inode)) { 598 key_put(key); 599 goto success; 600 } 601 602 ret = PTR_ERR(inode); 603 } 604 605 key_put(key); 606 if (ret == -ENOENT) { 607 d_add(dentry, NULL); 608 _leave(" = NULL [negative]"); 609 return NULL; 610 } 611 _leave(" = %d [do]", ret); 612 return ERR_PTR(ret); 613 } 614 dentry->d_fsdata = (void *)(unsigned long) vnode->status.data_version; 615 616 /* instantiate the dentry */ 617 inode = afs_iget(dir->i_sb, key, &fid, NULL, NULL, NULL); 618 key_put(key); 619 if (IS_ERR(inode)) { 620 _leave(" = %ld", PTR_ERR(inode)); 621 return ERR_CAST(inode); 622 } 623 624 success: 625 d_add(dentry, inode); 626 _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%u }", 627 fid.vnode, 628 fid.unique, 629 d_inode(dentry)->i_ino, 630 d_inode(dentry)->i_generation); 631 632 return NULL; 633 } 634 635 /* 636 * Look up an entry in a dynroot directory. 637 */ 638 static struct dentry *afs_dynroot_lookup(struct inode *dir, struct dentry *dentry, 639 unsigned int flags) 640 { 641 struct afs_vnode *vnode; 642 struct afs_fid fid; 643 struct inode *inode; 644 int ret; 645 646 vnode = AFS_FS_I(dir); 647 648 _enter("%pd", dentry); 649 650 ASSERTCMP(d_inode(dentry), ==, NULL); 651 652 if (dentry->d_name.len >= AFSNAMEMAX) { 653 _leave(" = -ENAMETOOLONG"); 654 return ERR_PTR(-ENAMETOOLONG); 655 } 656 657 inode = afs_try_auto_mntpt(dentry, dir, &fid); 658 if (IS_ERR(inode)) { 659 ret = PTR_ERR(inode); 660 if (ret == -ENOENT) { 661 d_add(dentry, NULL); 662 _leave(" = NULL [negative]"); 663 return NULL; 664 } 665 _leave(" = %d [do]", ret); 666 return ERR_PTR(ret); 667 } 668 669 d_add(dentry, inode); 670 _leave(" = 0 { ino=%lu v=%u }", 671 d_inode(dentry)->i_ino, d_inode(dentry)->i_generation); 672 return NULL; 673 } 674 675 /* 676 * check that a dentry lookup hit has found a valid entry 677 * - NOTE! the hit can be a negative hit too, so we can't assume we have an 678 * inode 679 */ 680 static int afs_d_revalidate(struct dentry *dentry, unsigned int flags) 681 { 682 struct afs_super_info *as = dentry->d_sb->s_fs_info; 683 struct afs_vnode *vnode, *dir; 684 struct afs_fid uninitialized_var(fid); 685 struct dentry *parent; 686 struct inode *inode; 687 struct key *key; 688 void *dir_version; 689 int ret; 690 691 if (flags & LOOKUP_RCU) 692 return -ECHILD; 693 694 if (as->dyn_root) 695 return 1; 696 697 if (d_really_is_positive(dentry)) { 698 vnode = AFS_FS_I(d_inode(dentry)); 699 _enter("{v={%x:%u} n=%pd fl=%lx},", 700 vnode->fid.vid, vnode->fid.vnode, dentry, 701 vnode->flags); 702 } else { 703 _enter("{neg n=%pd}", dentry); 704 } 705 706 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell); 707 if (IS_ERR(key)) 708 key = NULL; 709 710 if (d_really_is_positive(dentry)) { 711 inode = d_inode(dentry); 712 if (inode) { 713 vnode = AFS_FS_I(inode); 714 afs_validate(vnode, key); 715 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) 716 goto out_bad; 717 } 718 } 719 720 /* lock down the parent dentry so we can peer at it */ 721 parent = dget_parent(dentry); 722 dir = AFS_FS_I(d_inode(parent)); 723 724 /* validate the parent directory */ 725 afs_validate(dir, key); 726 727 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) { 728 _debug("%pd: parent dir deleted", dentry); 729 goto out_bad_parent; 730 } 731 732 dir_version = (void *) (unsigned long) dir->status.data_version; 733 if (dentry->d_fsdata == dir_version) 734 goto out_valid; /* the dir contents are unchanged */ 735 736 _debug("dir modified"); 737 738 /* search the directory for this vnode */ 739 ret = afs_do_lookup(&dir->vfs_inode, dentry, &fid, key); 740 switch (ret) { 741 case 0: 742 /* the filename maps to something */ 743 if (d_really_is_negative(dentry)) 744 goto out_bad_parent; 745 inode = d_inode(dentry); 746 if (is_bad_inode(inode)) { 747 printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n", 748 dentry); 749 goto out_bad_parent; 750 } 751 752 vnode = AFS_FS_I(inode); 753 754 /* if the vnode ID has changed, then the dirent points to a 755 * different file */ 756 if (fid.vnode != vnode->fid.vnode) { 757 _debug("%pd: dirent changed [%u != %u]", 758 dentry, fid.vnode, 759 vnode->fid.vnode); 760 goto not_found; 761 } 762 763 /* if the vnode ID uniqifier has changed, then the file has 764 * been deleted and replaced, and the original vnode ID has 765 * been reused */ 766 if (fid.unique != vnode->fid.unique) { 767 _debug("%pd: file deleted (uq %u -> %u I:%u)", 768 dentry, fid.unique, 769 vnode->fid.unique, 770 vnode->vfs_inode.i_generation); 771 write_seqlock(&vnode->cb_lock); 772 set_bit(AFS_VNODE_DELETED, &vnode->flags); 773 write_sequnlock(&vnode->cb_lock); 774 goto not_found; 775 } 776 goto out_valid; 777 778 case -ENOENT: 779 /* the filename is unknown */ 780 _debug("%pd: dirent not found", dentry); 781 if (d_really_is_positive(dentry)) 782 goto not_found; 783 goto out_valid; 784 785 default: 786 _debug("failed to iterate dir %pd: %d", 787 parent, ret); 788 goto out_bad_parent; 789 } 790 791 out_valid: 792 dentry->d_fsdata = dir_version; 793 dput(parent); 794 key_put(key); 795 _leave(" = 1 [valid]"); 796 return 1; 797 798 /* the dirent, if it exists, now points to a different vnode */ 799 not_found: 800 spin_lock(&dentry->d_lock); 801 dentry->d_flags |= DCACHE_NFSFS_RENAMED; 802 spin_unlock(&dentry->d_lock); 803 804 out_bad_parent: 805 _debug("dropping dentry %pd2", dentry); 806 dput(parent); 807 out_bad: 808 key_put(key); 809 810 _leave(" = 0 [bad]"); 811 return 0; 812 } 813 814 /* 815 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't 816 * sleep) 817 * - called from dput() when d_count is going to 0. 818 * - return 1 to request dentry be unhashed, 0 otherwise 819 */ 820 static int afs_d_delete(const struct dentry *dentry) 821 { 822 _enter("%pd", dentry); 823 824 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 825 goto zap; 826 827 if (d_really_is_positive(dentry) && 828 (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) || 829 test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags))) 830 goto zap; 831 832 _leave(" = 0 [keep]"); 833 return 0; 834 835 zap: 836 _leave(" = 1 [zap]"); 837 return 1; 838 } 839 840 /* 841 * handle dentry release 842 */ 843 static void afs_d_release(struct dentry *dentry) 844 { 845 _enter("%pd", dentry); 846 } 847 848 /* 849 * Create a new inode for create/mkdir/symlink 850 */ 851 static void afs_vnode_new_inode(struct afs_fs_cursor *fc, 852 struct dentry *new_dentry, 853 struct afs_fid *newfid, 854 struct afs_file_status *newstatus, 855 struct afs_callback *newcb) 856 { 857 struct inode *inode; 858 859 if (fc->ac.error < 0) 860 return; 861 862 d_drop(new_dentry); 863 864 inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key, 865 newfid, newstatus, newcb, fc->cbi); 866 if (IS_ERR(inode)) { 867 /* ENOMEM or EINTR at a really inconvenient time - just abandon 868 * the new directory on the server. 869 */ 870 fc->ac.error = PTR_ERR(inode); 871 return; 872 } 873 874 d_add(new_dentry, inode); 875 } 876 877 /* 878 * create a directory on an AFS filesystem 879 */ 880 static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 881 { 882 struct afs_file_status newstatus; 883 struct afs_fs_cursor fc; 884 struct afs_callback newcb; 885 struct afs_vnode *dvnode = AFS_FS_I(dir); 886 struct afs_fid newfid; 887 struct key *key; 888 int ret; 889 890 mode |= S_IFDIR; 891 892 _enter("{%x:%u},{%pd},%ho", 893 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode); 894 895 key = afs_request_key(dvnode->volume->cell); 896 if (IS_ERR(key)) { 897 ret = PTR_ERR(key); 898 goto error; 899 } 900 901 ret = -ERESTARTSYS; 902 if (afs_begin_vnode_operation(&fc, dvnode, key)) { 903 while (afs_select_fileserver(&fc)) { 904 fc.cb_break = dvnode->cb_break + dvnode->cb_s_break; 905 afs_fs_create(&fc, dentry->d_name.name, mode, 906 &newfid, &newstatus, &newcb); 907 } 908 909 afs_check_for_remote_deletion(&fc, fc.vnode); 910 afs_vnode_commit_status(&fc, dvnode, fc.cb_break); 911 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb); 912 ret = afs_end_vnode_operation(&fc); 913 if (ret < 0) 914 goto error_key; 915 } else { 916 goto error_key; 917 } 918 919 key_put(key); 920 _leave(" = 0"); 921 return 0; 922 923 error_key: 924 key_put(key); 925 error: 926 d_drop(dentry); 927 _leave(" = %d", ret); 928 return ret; 929 } 930 931 /* 932 * Remove a subdir from a directory. 933 */ 934 static void afs_dir_remove_subdir(struct dentry *dentry) 935 { 936 if (d_really_is_positive(dentry)) { 937 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); 938 939 clear_nlink(&vnode->vfs_inode); 940 set_bit(AFS_VNODE_DELETED, &vnode->flags); 941 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags); 942 } 943 } 944 945 /* 946 * remove a directory from an AFS filesystem 947 */ 948 static int afs_rmdir(struct inode *dir, struct dentry *dentry) 949 { 950 struct afs_fs_cursor fc; 951 struct afs_vnode *dvnode = AFS_FS_I(dir); 952 struct key *key; 953 int ret; 954 955 _enter("{%x:%u},{%pd}", 956 dvnode->fid.vid, dvnode->fid.vnode, dentry); 957 958 key = afs_request_key(dvnode->volume->cell); 959 if (IS_ERR(key)) { 960 ret = PTR_ERR(key); 961 goto error; 962 } 963 964 ret = -ERESTARTSYS; 965 if (afs_begin_vnode_operation(&fc, dvnode, key)) { 966 while (afs_select_fileserver(&fc)) { 967 fc.cb_break = dvnode->cb_break + dvnode->cb_s_break; 968 afs_fs_remove(&fc, dentry->d_name.name, true); 969 } 970 971 afs_vnode_commit_status(&fc, dvnode, fc.cb_break); 972 ret = afs_end_vnode_operation(&fc); 973 if (ret == 0) 974 afs_dir_remove_subdir(dentry); 975 } 976 977 key_put(key); 978 error: 979 return ret; 980 } 981 982 /* 983 * Remove a link to a file or symlink from a directory. 984 * 985 * If the file was not deleted due to excess hard links, the fileserver will 986 * break the callback promise on the file - if it had one - before it returns 987 * to us, and if it was deleted, it won't 988 * 989 * However, if we didn't have a callback promise outstanding, or it was 990 * outstanding on a different server, then it won't break it either... 991 */ 992 static int afs_dir_remove_link(struct dentry *dentry, struct key *key, 993 unsigned long d_version_before, 994 unsigned long d_version_after) 995 { 996 bool dir_valid; 997 int ret = 0; 998 999 /* There were no intervening changes on the server if the version 1000 * number we got back was incremented by exactly 1. 1001 */ 1002 dir_valid = (d_version_after == d_version_before + 1); 1003 1004 if (d_really_is_positive(dentry)) { 1005 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); 1006 1007 if (dir_valid) { 1008 drop_nlink(&vnode->vfs_inode); 1009 if (vnode->vfs_inode.i_nlink == 0) { 1010 set_bit(AFS_VNODE_DELETED, &vnode->flags); 1011 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags); 1012 } 1013 ret = 0; 1014 } else { 1015 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags); 1016 1017 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) 1018 kdebug("AFS_VNODE_DELETED"); 1019 1020 ret = afs_validate(vnode, key); 1021 if (ret == -ESTALE) 1022 ret = 0; 1023 } 1024 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret); 1025 } 1026 1027 return ret; 1028 } 1029 1030 /* 1031 * Remove a file or symlink from an AFS filesystem. 1032 */ 1033 static int afs_unlink(struct inode *dir, struct dentry *dentry) 1034 { 1035 struct afs_fs_cursor fc; 1036 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode; 1037 struct key *key; 1038 unsigned long d_version = (unsigned long)dentry->d_fsdata; 1039 int ret; 1040 1041 _enter("{%x:%u},{%pd}", 1042 dvnode->fid.vid, dvnode->fid.vnode, dentry); 1043 1044 if (dentry->d_name.len >= AFSNAMEMAX) 1045 return -ENAMETOOLONG; 1046 1047 key = afs_request_key(dvnode->volume->cell); 1048 if (IS_ERR(key)) { 1049 ret = PTR_ERR(key); 1050 goto error; 1051 } 1052 1053 /* Try to make sure we have a callback promise on the victim. */ 1054 if (d_really_is_positive(dentry)) { 1055 vnode = AFS_FS_I(d_inode(dentry)); 1056 ret = afs_validate(vnode, key); 1057 if (ret < 0) 1058 goto error_key; 1059 } 1060 1061 ret = -ERESTARTSYS; 1062 if (afs_begin_vnode_operation(&fc, dvnode, key)) { 1063 while (afs_select_fileserver(&fc)) { 1064 fc.cb_break = dvnode->cb_break + dvnode->cb_s_break; 1065 afs_fs_remove(&fc, dentry->d_name.name, false); 1066 } 1067 1068 afs_vnode_commit_status(&fc, dvnode, fc.cb_break); 1069 ret = afs_end_vnode_operation(&fc); 1070 if (ret == 0) 1071 ret = afs_dir_remove_link( 1072 dentry, key, d_version, 1073 (unsigned long)dvnode->status.data_version); 1074 } 1075 1076 error_key: 1077 key_put(key); 1078 error: 1079 _leave(" = %d", ret); 1080 return ret; 1081 } 1082 1083 /* 1084 * create a regular file on an AFS filesystem 1085 */ 1086 static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 1087 bool excl) 1088 { 1089 struct afs_fs_cursor fc; 1090 struct afs_file_status newstatus; 1091 struct afs_callback newcb; 1092 struct afs_vnode *dvnode = AFS_FS_I(dir); 1093 struct afs_fid newfid; 1094 struct key *key; 1095 int ret; 1096 1097 mode |= S_IFREG; 1098 1099 _enter("{%x:%u},{%pd},%ho,", 1100 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode); 1101 1102 ret = -ENAMETOOLONG; 1103 if (dentry->d_name.len >= AFSNAMEMAX) 1104 goto error; 1105 1106 key = afs_request_key(dvnode->volume->cell); 1107 if (IS_ERR(key)) { 1108 ret = PTR_ERR(key); 1109 goto error; 1110 } 1111 1112 ret = -ERESTARTSYS; 1113 if (afs_begin_vnode_operation(&fc, dvnode, key)) { 1114 while (afs_select_fileserver(&fc)) { 1115 fc.cb_break = dvnode->cb_break + dvnode->cb_s_break; 1116 afs_fs_create(&fc, dentry->d_name.name, mode, 1117 &newfid, &newstatus, &newcb); 1118 } 1119 1120 afs_check_for_remote_deletion(&fc, fc.vnode); 1121 afs_vnode_commit_status(&fc, dvnode, fc.cb_break); 1122 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb); 1123 ret = afs_end_vnode_operation(&fc); 1124 if (ret < 0) 1125 goto error_key; 1126 } else { 1127 goto error_key; 1128 } 1129 1130 key_put(key); 1131 _leave(" = 0"); 1132 return 0; 1133 1134 error_key: 1135 key_put(key); 1136 error: 1137 d_drop(dentry); 1138 _leave(" = %d", ret); 1139 return ret; 1140 } 1141 1142 /* 1143 * create a hard link between files in an AFS filesystem 1144 */ 1145 static int afs_link(struct dentry *from, struct inode *dir, 1146 struct dentry *dentry) 1147 { 1148 struct afs_fs_cursor fc; 1149 struct afs_vnode *dvnode, *vnode; 1150 struct key *key; 1151 int ret; 1152 1153 vnode = AFS_FS_I(d_inode(from)); 1154 dvnode = AFS_FS_I(dir); 1155 1156 _enter("{%x:%u},{%x:%u},{%pd}", 1157 vnode->fid.vid, vnode->fid.vnode, 1158 dvnode->fid.vid, dvnode->fid.vnode, 1159 dentry); 1160 1161 ret = -ENAMETOOLONG; 1162 if (dentry->d_name.len >= AFSNAMEMAX) 1163 goto error; 1164 1165 key = afs_request_key(dvnode->volume->cell); 1166 if (IS_ERR(key)) { 1167 ret = PTR_ERR(key); 1168 goto error; 1169 } 1170 1171 ret = -ERESTARTSYS; 1172 if (afs_begin_vnode_operation(&fc, dvnode, key)) { 1173 if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) { 1174 afs_end_vnode_operation(&fc); 1175 goto error_key; 1176 } 1177 1178 while (afs_select_fileserver(&fc)) { 1179 fc.cb_break = dvnode->cb_break + dvnode->cb_s_break; 1180 fc.cb_break_2 = vnode->cb_break + vnode->cb_s_break; 1181 afs_fs_link(&fc, vnode, dentry->d_name.name); 1182 } 1183 1184 afs_vnode_commit_status(&fc, dvnode, fc.cb_break); 1185 afs_vnode_commit_status(&fc, vnode, fc.cb_break_2); 1186 ihold(&vnode->vfs_inode); 1187 d_instantiate(dentry, &vnode->vfs_inode); 1188 1189 mutex_unlock(&vnode->io_lock); 1190 ret = afs_end_vnode_operation(&fc); 1191 if (ret < 0) 1192 goto error_key; 1193 } else { 1194 goto error_key; 1195 } 1196 1197 key_put(key); 1198 _leave(" = 0"); 1199 return 0; 1200 1201 error_key: 1202 key_put(key); 1203 error: 1204 d_drop(dentry); 1205 _leave(" = %d", ret); 1206 return ret; 1207 } 1208 1209 /* 1210 * create a symlink in an AFS filesystem 1211 */ 1212 static int afs_symlink(struct inode *dir, struct dentry *dentry, 1213 const char *content) 1214 { 1215 struct afs_fs_cursor fc; 1216 struct afs_file_status newstatus; 1217 struct afs_vnode *dvnode = AFS_FS_I(dir); 1218 struct afs_fid newfid; 1219 struct key *key; 1220 int ret; 1221 1222 _enter("{%x:%u},{%pd},%s", 1223 dvnode->fid.vid, dvnode->fid.vnode, dentry, 1224 content); 1225 1226 ret = -ENAMETOOLONG; 1227 if (dentry->d_name.len >= AFSNAMEMAX) 1228 goto error; 1229 1230 ret = -EINVAL; 1231 if (strlen(content) >= AFSPATHMAX) 1232 goto error; 1233 1234 key = afs_request_key(dvnode->volume->cell); 1235 if (IS_ERR(key)) { 1236 ret = PTR_ERR(key); 1237 goto error; 1238 } 1239 1240 ret = -ERESTARTSYS; 1241 if (afs_begin_vnode_operation(&fc, dvnode, key)) { 1242 while (afs_select_fileserver(&fc)) { 1243 fc.cb_break = dvnode->cb_break + dvnode->cb_s_break; 1244 afs_fs_symlink(&fc, dentry->d_name.name, content, 1245 &newfid, &newstatus); 1246 } 1247 1248 afs_check_for_remote_deletion(&fc, fc.vnode); 1249 afs_vnode_commit_status(&fc, dvnode, fc.cb_break); 1250 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, NULL); 1251 ret = afs_end_vnode_operation(&fc); 1252 if (ret < 0) 1253 goto error_key; 1254 } else { 1255 goto error_key; 1256 } 1257 1258 key_put(key); 1259 _leave(" = 0"); 1260 return 0; 1261 1262 error_key: 1263 key_put(key); 1264 error: 1265 d_drop(dentry); 1266 _leave(" = %d", ret); 1267 return ret; 1268 } 1269 1270 /* 1271 * rename a file in an AFS filesystem and/or move it between directories 1272 */ 1273 static int afs_rename(struct inode *old_dir, struct dentry *old_dentry, 1274 struct inode *new_dir, struct dentry *new_dentry, 1275 unsigned int flags) 1276 { 1277 struct afs_fs_cursor fc; 1278 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode; 1279 struct key *key; 1280 int ret; 1281 1282 if (flags) 1283 return -EINVAL; 1284 1285 vnode = AFS_FS_I(d_inode(old_dentry)); 1286 orig_dvnode = AFS_FS_I(old_dir); 1287 new_dvnode = AFS_FS_I(new_dir); 1288 1289 _enter("{%x:%u},{%x:%u},{%x:%u},{%pd}", 1290 orig_dvnode->fid.vid, orig_dvnode->fid.vnode, 1291 vnode->fid.vid, vnode->fid.vnode, 1292 new_dvnode->fid.vid, new_dvnode->fid.vnode, 1293 new_dentry); 1294 1295 key = afs_request_key(orig_dvnode->volume->cell); 1296 if (IS_ERR(key)) { 1297 ret = PTR_ERR(key); 1298 goto error; 1299 } 1300 1301 ret = -ERESTARTSYS; 1302 if (afs_begin_vnode_operation(&fc, orig_dvnode, key)) { 1303 if (orig_dvnode != new_dvnode) { 1304 if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) { 1305 afs_end_vnode_operation(&fc); 1306 goto error_key; 1307 } 1308 } 1309 while (afs_select_fileserver(&fc)) { 1310 fc.cb_break = orig_dvnode->cb_break + orig_dvnode->cb_s_break; 1311 fc.cb_break_2 = new_dvnode->cb_break + new_dvnode->cb_s_break; 1312 afs_fs_rename(&fc, old_dentry->d_name.name, 1313 new_dvnode, new_dentry->d_name.name); 1314 } 1315 1316 afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break); 1317 afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2); 1318 if (orig_dvnode != new_dvnode) 1319 mutex_unlock(&new_dvnode->io_lock); 1320 ret = afs_end_vnode_operation(&fc); 1321 if (ret < 0) 1322 goto error_key; 1323 } 1324 1325 error_key: 1326 key_put(key); 1327 error: 1328 _leave(" = %d", ret); 1329 return ret; 1330 } 1331