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/slab.h> 16 #include <linux/fs.h> 17 #include <linux/pagemap.h> 18 #include <linux/ctype.h> 19 #include "internal.h" 20 21 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry, 22 struct nameidata *nd); 23 static int afs_dir_open(struct inode *inode, struct file *file); 24 static int afs_readdir(struct file *file, void *dirent, filldir_t filldir); 25 static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd); 26 static int afs_d_delete(struct dentry *dentry); 27 static void afs_d_release(struct dentry *dentry); 28 static int afs_lookup_filldir(void *_cookie, const char *name, int nlen, 29 loff_t fpos, u64 ino, unsigned dtype); 30 static int afs_create(struct inode *dir, struct dentry *dentry, int mode, 31 struct nameidata *nd); 32 static int afs_mkdir(struct inode *dir, struct dentry *dentry, int mode); 33 static int afs_rmdir(struct inode *dir, struct dentry *dentry); 34 static int afs_unlink(struct inode *dir, struct dentry *dentry); 35 static int afs_link(struct dentry *from, struct inode *dir, 36 struct dentry *dentry); 37 static int afs_symlink(struct inode *dir, struct dentry *dentry, 38 const char *content); 39 static int afs_rename(struct inode *old_dir, struct dentry *old_dentry, 40 struct inode *new_dir, struct dentry *new_dentry); 41 42 const struct file_operations afs_dir_file_operations = { 43 .open = afs_dir_open, 44 .release = afs_release, 45 .readdir = afs_readdir, 46 }; 47 48 const struct inode_operations afs_dir_inode_operations = { 49 .create = afs_create, 50 .lookup = afs_lookup, 51 .link = afs_link, 52 .unlink = afs_unlink, 53 .symlink = afs_symlink, 54 .mkdir = afs_mkdir, 55 .rmdir = afs_rmdir, 56 .rename = afs_rename, 57 .permission = afs_permission, 58 .getattr = afs_getattr, 59 .setattr = afs_setattr, 60 }; 61 62 static struct dentry_operations afs_fs_dentry_operations = { 63 .d_revalidate = afs_d_revalidate, 64 .d_delete = afs_d_delete, 65 .d_release = afs_d_release, 66 }; 67 68 #define AFS_DIR_HASHTBL_SIZE 128 69 #define AFS_DIR_DIRENT_SIZE 32 70 #define AFS_DIRENT_PER_BLOCK 64 71 72 union afs_dirent { 73 struct { 74 uint8_t valid; 75 uint8_t unused[1]; 76 __be16 hash_next; 77 __be32 vnode; 78 __be32 unique; 79 uint8_t name[16]; 80 uint8_t overflow[4]; /* if any char of the name (inc 81 * NUL) reaches here, consume 82 * the next dirent too */ 83 } u; 84 uint8_t extended_name[32]; 85 }; 86 87 /* AFS directory page header (one at the beginning of every 2048-byte chunk) */ 88 struct afs_dir_pagehdr { 89 __be16 npages; 90 __be16 magic; 91 #define AFS_DIR_MAGIC htons(1234) 92 uint8_t nentries; 93 uint8_t bitmap[8]; 94 uint8_t pad[19]; 95 }; 96 97 /* directory block layout */ 98 union afs_dir_block { 99 100 struct afs_dir_pagehdr pagehdr; 101 102 struct { 103 struct afs_dir_pagehdr pagehdr; 104 uint8_t alloc_ctrs[128]; 105 /* dir hash table */ 106 uint16_t hashtable[AFS_DIR_HASHTBL_SIZE]; 107 } hdr; 108 109 union afs_dirent dirents[AFS_DIRENT_PER_BLOCK]; 110 }; 111 112 /* layout on a linux VM page */ 113 struct afs_dir_page { 114 union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)]; 115 }; 116 117 struct afs_lookup_cookie { 118 struct afs_fid fid; 119 const char *name; 120 size_t nlen; 121 int found; 122 }; 123 124 /* 125 * check that a directory page is valid 126 */ 127 static inline void afs_dir_check_page(struct inode *dir, struct page *page) 128 { 129 struct afs_dir_page *dbuf; 130 loff_t latter; 131 int tmp, qty; 132 133 #if 0 134 /* check the page count */ 135 qty = desc.size / sizeof(dbuf->blocks[0]); 136 if (qty == 0) 137 goto error; 138 139 if (page->index == 0 && qty != ntohs(dbuf->blocks[0].pagehdr.npages)) { 140 printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n", 141 __FUNCTION__, dir->i_ino, qty, 142 ntohs(dbuf->blocks[0].pagehdr.npages)); 143 goto error; 144 } 145 #endif 146 147 /* determine how many magic numbers there should be in this page */ 148 latter = dir->i_size - page_offset(page); 149 if (latter >= PAGE_SIZE) 150 qty = PAGE_SIZE; 151 else 152 qty = latter; 153 qty /= sizeof(union afs_dir_block); 154 155 /* check them */ 156 dbuf = page_address(page); 157 for (tmp = 0; tmp < qty; tmp++) { 158 if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) { 159 printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n", 160 __FUNCTION__, dir->i_ino, tmp, qty, 161 ntohs(dbuf->blocks[tmp].pagehdr.magic)); 162 goto error; 163 } 164 } 165 166 SetPageChecked(page); 167 return; 168 169 error: 170 SetPageChecked(page); 171 SetPageError(page); 172 } 173 174 /* 175 * discard a page cached in the pagecache 176 */ 177 static inline void afs_dir_put_page(struct page *page) 178 { 179 kunmap(page); 180 page_cache_release(page); 181 } 182 183 /* 184 * get a page into the pagecache 185 */ 186 static struct page *afs_dir_get_page(struct inode *dir, unsigned long index, 187 struct key *key) 188 { 189 struct page *page; 190 struct file file = { 191 .private_data = key, 192 }; 193 194 _enter("{%lu},%lu", dir->i_ino, index); 195 196 page = read_mapping_page(dir->i_mapping, index, &file); 197 if (!IS_ERR(page)) { 198 kmap(page); 199 if (!PageChecked(page)) 200 afs_dir_check_page(dir, page); 201 if (PageError(page)) 202 goto fail; 203 } 204 return page; 205 206 fail: 207 afs_dir_put_page(page); 208 _leave(" = -EIO"); 209 return ERR_PTR(-EIO); 210 } 211 212 /* 213 * open an AFS directory file 214 */ 215 static int afs_dir_open(struct inode *inode, struct file *file) 216 { 217 _enter("{%lu}", inode->i_ino); 218 219 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048); 220 BUILD_BUG_ON(sizeof(union afs_dirent) != 32); 221 222 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags)) 223 return -ENOENT; 224 225 return afs_open(inode, file); 226 } 227 228 /* 229 * deal with one block in an AFS directory 230 */ 231 static int afs_dir_iterate_block(unsigned *fpos, 232 union afs_dir_block *block, 233 unsigned blkoff, 234 void *cookie, 235 filldir_t filldir) 236 { 237 union afs_dirent *dire; 238 unsigned offset, next, curr; 239 size_t nlen; 240 int tmp, ret; 241 242 _enter("%u,%x,%p,,",*fpos,blkoff,block); 243 244 curr = (*fpos - blkoff) / sizeof(union afs_dirent); 245 246 /* walk through the block, an entry at a time */ 247 for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries; 248 offset < AFS_DIRENT_PER_BLOCK; 249 offset = next 250 ) { 251 next = offset + 1; 252 253 /* skip entries marked unused in the bitmap */ 254 if (!(block->pagehdr.bitmap[offset / 8] & 255 (1 << (offset % 8)))) { 256 _debug("ENT[%Zu.%u]: unused", 257 blkoff / sizeof(union afs_dir_block), offset); 258 if (offset >= curr) 259 *fpos = blkoff + 260 next * sizeof(union afs_dirent); 261 continue; 262 } 263 264 /* got a valid entry */ 265 dire = &block->dirents[offset]; 266 nlen = strnlen(dire->u.name, 267 sizeof(*block) - 268 offset * sizeof(union afs_dirent)); 269 270 _debug("ENT[%Zu.%u]: %s %Zu \"%s\"", 271 blkoff / sizeof(union afs_dir_block), offset, 272 (offset < curr ? "skip" : "fill"), 273 nlen, dire->u.name); 274 275 /* work out where the next possible entry is */ 276 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) { 277 if (next >= AFS_DIRENT_PER_BLOCK) { 278 _debug("ENT[%Zu.%u]:" 279 " %u travelled beyond end dir block" 280 " (len %u/%Zu)", 281 blkoff / sizeof(union afs_dir_block), 282 offset, next, tmp, nlen); 283 return -EIO; 284 } 285 if (!(block->pagehdr.bitmap[next / 8] & 286 (1 << (next % 8)))) { 287 _debug("ENT[%Zu.%u]:" 288 " %u unmarked extension (len %u/%Zu)", 289 blkoff / sizeof(union afs_dir_block), 290 offset, next, tmp, nlen); 291 return -EIO; 292 } 293 294 _debug("ENT[%Zu.%u]: ext %u/%Zu", 295 blkoff / sizeof(union afs_dir_block), 296 next, tmp, nlen); 297 next++; 298 } 299 300 /* skip if starts before the current position */ 301 if (offset < curr) 302 continue; 303 304 /* found the next entry */ 305 ret = filldir(cookie, 306 dire->u.name, 307 nlen, 308 blkoff + offset * sizeof(union afs_dirent), 309 ntohl(dire->u.vnode), 310 filldir == afs_lookup_filldir ? 311 ntohl(dire->u.unique) : DT_UNKNOWN); 312 if (ret < 0) { 313 _leave(" = 0 [full]"); 314 return 0; 315 } 316 317 *fpos = blkoff + next * sizeof(union afs_dirent); 318 } 319 320 _leave(" = 1 [more]"); 321 return 1; 322 } 323 324 /* 325 * iterate through the data blob that lists the contents of an AFS directory 326 */ 327 static int afs_dir_iterate(struct inode *dir, unsigned *fpos, void *cookie, 328 filldir_t filldir, struct key *key) 329 { 330 union afs_dir_block *dblock; 331 struct afs_dir_page *dbuf; 332 struct page *page; 333 unsigned blkoff, limit; 334 int ret; 335 336 _enter("{%lu},%u,,", dir->i_ino, *fpos); 337 338 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) { 339 _leave(" = -ESTALE"); 340 return -ESTALE; 341 } 342 343 /* round the file position up to the next entry boundary */ 344 *fpos += sizeof(union afs_dirent) - 1; 345 *fpos &= ~(sizeof(union afs_dirent) - 1); 346 347 /* walk through the blocks in sequence */ 348 ret = 0; 349 while (*fpos < dir->i_size) { 350 blkoff = *fpos & ~(sizeof(union afs_dir_block) - 1); 351 352 /* fetch the appropriate page from the directory */ 353 page = afs_dir_get_page(dir, blkoff / PAGE_SIZE, key); 354 if (IS_ERR(page)) { 355 ret = PTR_ERR(page); 356 break; 357 } 358 359 limit = blkoff & ~(PAGE_SIZE - 1); 360 361 dbuf = page_address(page); 362 363 /* deal with the individual blocks stashed on this page */ 364 do { 365 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) / 366 sizeof(union afs_dir_block)]; 367 ret = afs_dir_iterate_block(fpos, dblock, blkoff, 368 cookie, filldir); 369 if (ret != 1) { 370 afs_dir_put_page(page); 371 goto out; 372 } 373 374 blkoff += sizeof(union afs_dir_block); 375 376 } while (*fpos < dir->i_size && blkoff < limit); 377 378 afs_dir_put_page(page); 379 ret = 0; 380 } 381 382 out: 383 _leave(" = %d", ret); 384 return ret; 385 } 386 387 /* 388 * read an AFS directory 389 */ 390 static int afs_readdir(struct file *file, void *cookie, filldir_t filldir) 391 { 392 unsigned fpos; 393 int ret; 394 395 _enter("{%Ld,{%lu}}", 396 file->f_pos, file->f_path.dentry->d_inode->i_ino); 397 398 ASSERT(file->private_data != NULL); 399 400 fpos = file->f_pos; 401 ret = afs_dir_iterate(file->f_path.dentry->d_inode, &fpos, 402 cookie, filldir, file->private_data); 403 file->f_pos = fpos; 404 405 _leave(" = %d", ret); 406 return ret; 407 } 408 409 /* 410 * search the directory for a name 411 * - if afs_dir_iterate_block() spots this function, it'll pass the FID 412 * uniquifier through dtype 413 */ 414 static int afs_lookup_filldir(void *_cookie, const char *name, int nlen, 415 loff_t fpos, u64 ino, unsigned dtype) 416 { 417 struct afs_lookup_cookie *cookie = _cookie; 418 419 _enter("{%s,%Zu},%s,%u,,%llu,%u", 420 cookie->name, cookie->nlen, name, nlen, 421 (unsigned long long) ino, dtype); 422 423 /* insanity checks first */ 424 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048); 425 BUILD_BUG_ON(sizeof(union afs_dirent) != 32); 426 427 if (cookie->nlen != nlen || memcmp(cookie->name, name, nlen) != 0) { 428 _leave(" = 0 [no]"); 429 return 0; 430 } 431 432 cookie->fid.vnode = ino; 433 cookie->fid.unique = dtype; 434 cookie->found = 1; 435 436 _leave(" = -1 [found]"); 437 return -1; 438 } 439 440 /* 441 * do a lookup in a directory 442 * - just returns the FID the dentry name maps to if found 443 */ 444 static int afs_do_lookup(struct inode *dir, struct dentry *dentry, 445 struct afs_fid *fid, struct key *key) 446 { 447 struct afs_lookup_cookie cookie; 448 struct afs_super_info *as; 449 unsigned fpos; 450 int ret; 451 452 _enter("{%lu},%p{%s},", dir->i_ino, dentry, dentry->d_name.name); 453 454 as = dir->i_sb->s_fs_info; 455 456 /* search the directory */ 457 cookie.name = dentry->d_name.name; 458 cookie.nlen = dentry->d_name.len; 459 cookie.fid.vid = as->volume->vid; 460 cookie.found = 0; 461 462 fpos = 0; 463 ret = afs_dir_iterate(dir, &fpos, &cookie, afs_lookup_filldir, 464 key); 465 if (ret < 0) { 466 _leave(" = %d [iter]", ret); 467 return ret; 468 } 469 470 ret = -ENOENT; 471 if (!cookie.found) { 472 _leave(" = -ENOENT [not found]"); 473 return -ENOENT; 474 } 475 476 *fid = cookie.fid; 477 _leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique); 478 return 0; 479 } 480 481 /* 482 * look up an entry in a directory 483 */ 484 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry, 485 struct nameidata *nd) 486 { 487 struct afs_vnode *vnode; 488 struct afs_fid fid; 489 struct inode *inode; 490 struct key *key; 491 int ret; 492 493 vnode = AFS_FS_I(dir); 494 495 _enter("{%x:%u},%p{%s},", 496 vnode->fid.vid, vnode->fid.vnode, dentry, dentry->d_name.name); 497 498 ASSERTCMP(dentry->d_inode, ==, NULL); 499 500 if (dentry->d_name.len >= AFSNAMEMAX) { 501 _leave(" = -ENAMETOOLONG"); 502 return ERR_PTR(-ENAMETOOLONG); 503 } 504 505 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) { 506 _leave(" = -ESTALE"); 507 return ERR_PTR(-ESTALE); 508 } 509 510 key = afs_request_key(vnode->volume->cell); 511 if (IS_ERR(key)) { 512 _leave(" = %ld [key]", PTR_ERR(key)); 513 return ERR_PTR(PTR_ERR(key)); 514 } 515 516 ret = afs_validate(vnode, key); 517 if (ret < 0) { 518 key_put(key); 519 _leave(" = %d [val]", ret); 520 return ERR_PTR(ret); 521 } 522 523 ret = afs_do_lookup(dir, dentry, &fid, key); 524 if (ret < 0) { 525 key_put(key); 526 if (ret == -ENOENT) { 527 d_add(dentry, NULL); 528 _leave(" = NULL [negative]"); 529 return NULL; 530 } 531 _leave(" = %d [do]", ret); 532 return ERR_PTR(ret); 533 } 534 dentry->d_fsdata = (void *)(unsigned long) vnode->status.data_version; 535 536 /* instantiate the dentry */ 537 inode = afs_iget(dir->i_sb, key, &fid, NULL, NULL); 538 key_put(key); 539 if (IS_ERR(inode)) { 540 _leave(" = %ld", PTR_ERR(inode)); 541 return ERR_PTR(PTR_ERR(inode)); 542 } 543 544 dentry->d_op = &afs_fs_dentry_operations; 545 546 d_add(dentry, inode); 547 _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%lu }", 548 fid.vnode, 549 fid.unique, 550 dentry->d_inode->i_ino, 551 dentry->d_inode->i_version); 552 553 return NULL; 554 } 555 556 /* 557 * check that a dentry lookup hit has found a valid entry 558 * - NOTE! the hit can be a negative hit too, so we can't assume we have an 559 * inode 560 */ 561 static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd) 562 { 563 struct afs_vnode *vnode, *dir; 564 struct afs_fid fid; 565 struct dentry *parent; 566 struct key *key; 567 void *dir_version; 568 int ret; 569 570 vnode = AFS_FS_I(dentry->d_inode); 571 572 if (dentry->d_inode) 573 _enter("{v={%x:%u} n=%s fl=%lx},", 574 vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name, 575 vnode->flags); 576 else 577 _enter("{neg n=%s}", dentry->d_name.name); 578 579 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell); 580 if (IS_ERR(key)) 581 key = NULL; 582 583 /* lock down the parent dentry so we can peer at it */ 584 parent = dget_parent(dentry); 585 if (!parent->d_inode) 586 goto out_bad; 587 588 dir = AFS_FS_I(parent->d_inode); 589 590 /* validate the parent directory */ 591 if (test_bit(AFS_VNODE_MODIFIED, &dir->flags)) 592 afs_validate(dir, key); 593 594 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) { 595 _debug("%s: parent dir deleted", dentry->d_name.name); 596 goto out_bad; 597 } 598 599 dir_version = (void *) (unsigned long) dir->status.data_version; 600 if (dentry->d_fsdata == dir_version) 601 goto out_valid; /* the dir contents are unchanged */ 602 603 _debug("dir modified"); 604 605 /* search the directory for this vnode */ 606 ret = afs_do_lookup(&dir->vfs_inode, dentry, &fid, key); 607 switch (ret) { 608 case 0: 609 /* the filename maps to something */ 610 if (!dentry->d_inode) 611 goto out_bad; 612 if (is_bad_inode(dentry->d_inode)) { 613 printk("kAFS: afs_d_revalidate: %s/%s has bad inode\n", 614 parent->d_name.name, dentry->d_name.name); 615 goto out_bad; 616 } 617 618 /* if the vnode ID has changed, then the dirent points to a 619 * different file */ 620 if (fid.vnode != vnode->fid.vnode) { 621 _debug("%s: dirent changed [%u != %u]", 622 dentry->d_name.name, fid.vnode, 623 vnode->fid.vnode); 624 goto not_found; 625 } 626 627 /* if the vnode ID uniqifier has changed, then the file has 628 * been deleted and replaced, and the original vnode ID has 629 * been reused */ 630 if (fid.unique != vnode->fid.unique) { 631 _debug("%s: file deleted (uq %u -> %u I:%lu)", 632 dentry->d_name.name, fid.unique, 633 vnode->fid.unique, dentry->d_inode->i_version); 634 spin_lock(&vnode->lock); 635 set_bit(AFS_VNODE_DELETED, &vnode->flags); 636 spin_unlock(&vnode->lock); 637 goto not_found; 638 } 639 goto out_valid; 640 641 case -ENOENT: 642 /* the filename is unknown */ 643 _debug("%s: dirent not found", dentry->d_name.name); 644 if (dentry->d_inode) 645 goto not_found; 646 goto out_valid; 647 648 default: 649 _debug("failed to iterate dir %s: %d", 650 parent->d_name.name, ret); 651 goto out_bad; 652 } 653 654 out_valid: 655 dentry->d_fsdata = dir_version; 656 out_skip: 657 dput(parent); 658 key_put(key); 659 _leave(" = 1 [valid]"); 660 return 1; 661 662 /* the dirent, if it exists, now points to a different vnode */ 663 not_found: 664 spin_lock(&dentry->d_lock); 665 dentry->d_flags |= DCACHE_NFSFS_RENAMED; 666 spin_unlock(&dentry->d_lock); 667 668 out_bad: 669 if (dentry->d_inode) { 670 /* don't unhash if we have submounts */ 671 if (have_submounts(dentry)) 672 goto out_skip; 673 } 674 675 _debug("dropping dentry %s/%s", 676 parent->d_name.name, dentry->d_name.name); 677 shrink_dcache_parent(dentry); 678 d_drop(dentry); 679 dput(parent); 680 key_put(key); 681 682 _leave(" = 0 [bad]"); 683 return 0; 684 } 685 686 /* 687 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't 688 * sleep) 689 * - called from dput() when d_count is going to 0. 690 * - return 1 to request dentry be unhashed, 0 otherwise 691 */ 692 static int afs_d_delete(struct dentry *dentry) 693 { 694 _enter("%s", dentry->d_name.name); 695 696 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 697 goto zap; 698 699 if (dentry->d_inode && 700 test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dentry->d_inode)->flags)) 701 goto zap; 702 703 _leave(" = 0 [keep]"); 704 return 0; 705 706 zap: 707 _leave(" = 1 [zap]"); 708 return 1; 709 } 710 711 /* 712 * handle dentry release 713 */ 714 static void afs_d_release(struct dentry *dentry) 715 { 716 _enter("%s", dentry->d_name.name); 717 } 718 719 /* 720 * create a directory on an AFS filesystem 721 */ 722 static int afs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 723 { 724 struct afs_file_status status; 725 struct afs_callback cb; 726 struct afs_server *server; 727 struct afs_vnode *dvnode, *vnode; 728 struct afs_fid fid; 729 struct inode *inode; 730 struct key *key; 731 int ret; 732 733 dvnode = AFS_FS_I(dir); 734 735 _enter("{%x:%u},{%s},%o", 736 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode); 737 738 ret = -ENAMETOOLONG; 739 if (dentry->d_name.len >= AFSNAMEMAX) 740 goto error; 741 742 key = afs_request_key(dvnode->volume->cell); 743 if (IS_ERR(key)) { 744 ret = PTR_ERR(key); 745 goto error; 746 } 747 748 mode |= S_IFDIR; 749 ret = afs_vnode_create(dvnode, key, dentry->d_name.name, 750 mode, &fid, &status, &cb, &server); 751 if (ret < 0) 752 goto mkdir_error; 753 754 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb); 755 if (IS_ERR(inode)) { 756 /* ENOMEM at a really inconvenient time - just abandon the new 757 * directory on the server */ 758 ret = PTR_ERR(inode); 759 goto iget_error; 760 } 761 762 /* apply the status report we've got for the new vnode */ 763 vnode = AFS_FS_I(inode); 764 spin_lock(&vnode->lock); 765 vnode->update_cnt++; 766 spin_unlock(&vnode->lock); 767 afs_vnode_finalise_status_update(vnode, server); 768 afs_put_server(server); 769 770 d_instantiate(dentry, inode); 771 if (d_unhashed(dentry)) { 772 _debug("not hashed"); 773 d_rehash(dentry); 774 } 775 key_put(key); 776 _leave(" = 0"); 777 return 0; 778 779 iget_error: 780 afs_put_server(server); 781 mkdir_error: 782 key_put(key); 783 error: 784 d_drop(dentry); 785 _leave(" = %d", ret); 786 return ret; 787 } 788 789 /* 790 * remove a directory from an AFS filesystem 791 */ 792 static int afs_rmdir(struct inode *dir, struct dentry *dentry) 793 { 794 struct afs_vnode *dvnode, *vnode; 795 struct key *key; 796 int ret; 797 798 dvnode = AFS_FS_I(dir); 799 800 _enter("{%x:%u},{%s}", 801 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name); 802 803 ret = -ENAMETOOLONG; 804 if (dentry->d_name.len >= AFSNAMEMAX) 805 goto error; 806 807 key = afs_request_key(dvnode->volume->cell); 808 if (IS_ERR(key)) { 809 ret = PTR_ERR(key); 810 goto error; 811 } 812 813 ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, true); 814 if (ret < 0) 815 goto rmdir_error; 816 817 if (dentry->d_inode) { 818 vnode = AFS_FS_I(dentry->d_inode); 819 clear_nlink(&vnode->vfs_inode); 820 set_bit(AFS_VNODE_DELETED, &vnode->flags); 821 afs_discard_callback_on_delete(vnode); 822 } 823 824 key_put(key); 825 _leave(" = 0"); 826 return 0; 827 828 rmdir_error: 829 key_put(key); 830 error: 831 _leave(" = %d", ret); 832 return ret; 833 } 834 835 /* 836 * remove a file from an AFS filesystem 837 */ 838 static int afs_unlink(struct inode *dir, struct dentry *dentry) 839 { 840 struct afs_vnode *dvnode, *vnode; 841 struct key *key; 842 int ret; 843 844 dvnode = AFS_FS_I(dir); 845 846 _enter("{%x:%u},{%s}", 847 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name); 848 849 ret = -ENAMETOOLONG; 850 if (dentry->d_name.len >= AFSNAMEMAX) 851 goto error; 852 853 key = afs_request_key(dvnode->volume->cell); 854 if (IS_ERR(key)) { 855 ret = PTR_ERR(key); 856 goto error; 857 } 858 859 if (dentry->d_inode) { 860 vnode = AFS_FS_I(dentry->d_inode); 861 862 /* make sure we have a callback promise on the victim */ 863 ret = afs_validate(vnode, key); 864 if (ret < 0) 865 goto error; 866 } 867 868 ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, false); 869 if (ret < 0) 870 goto remove_error; 871 872 if (dentry->d_inode) { 873 /* if the file wasn't deleted due to excess hard links, the 874 * fileserver will break the callback promise on the file - if 875 * it had one - before it returns to us, and if it was deleted, 876 * it won't 877 * 878 * however, if we didn't have a callback promise outstanding, 879 * or it was outstanding on a different server, then it won't 880 * break it either... 881 */ 882 vnode = AFS_FS_I(dentry->d_inode); 883 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) 884 _debug("AFS_VNODE_DELETED"); 885 if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags)) 886 _debug("AFS_VNODE_CB_BROKEN"); 887 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags); 888 ret = afs_validate(vnode, key); 889 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret); 890 } 891 892 key_put(key); 893 _leave(" = 0"); 894 return 0; 895 896 remove_error: 897 key_put(key); 898 error: 899 _leave(" = %d", ret); 900 return ret; 901 } 902 903 /* 904 * create a regular file on an AFS filesystem 905 */ 906 static int afs_create(struct inode *dir, struct dentry *dentry, int mode, 907 struct nameidata *nd) 908 { 909 struct afs_file_status status; 910 struct afs_callback cb; 911 struct afs_server *server; 912 struct afs_vnode *dvnode, *vnode; 913 struct afs_fid fid; 914 struct inode *inode; 915 struct key *key; 916 int ret; 917 918 dvnode = AFS_FS_I(dir); 919 920 _enter("{%x:%u},{%s},%o,", 921 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode); 922 923 ret = -ENAMETOOLONG; 924 if (dentry->d_name.len >= AFSNAMEMAX) 925 goto error; 926 927 key = afs_request_key(dvnode->volume->cell); 928 if (IS_ERR(key)) { 929 ret = PTR_ERR(key); 930 goto error; 931 } 932 933 mode |= S_IFREG; 934 ret = afs_vnode_create(dvnode, key, dentry->d_name.name, 935 mode, &fid, &status, &cb, &server); 936 if (ret < 0) 937 goto create_error; 938 939 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb); 940 if (IS_ERR(inode)) { 941 /* ENOMEM at a really inconvenient time - just abandon the new 942 * directory on the server */ 943 ret = PTR_ERR(inode); 944 goto iget_error; 945 } 946 947 /* apply the status report we've got for the new vnode */ 948 vnode = AFS_FS_I(inode); 949 spin_lock(&vnode->lock); 950 vnode->update_cnt++; 951 spin_unlock(&vnode->lock); 952 afs_vnode_finalise_status_update(vnode, server); 953 afs_put_server(server); 954 955 d_instantiate(dentry, inode); 956 if (d_unhashed(dentry)) { 957 _debug("not hashed"); 958 d_rehash(dentry); 959 } 960 key_put(key); 961 _leave(" = 0"); 962 return 0; 963 964 iget_error: 965 afs_put_server(server); 966 create_error: 967 key_put(key); 968 error: 969 d_drop(dentry); 970 _leave(" = %d", ret); 971 return ret; 972 } 973 974 /* 975 * create a hard link between files in an AFS filesystem 976 */ 977 static int afs_link(struct dentry *from, struct inode *dir, 978 struct dentry *dentry) 979 { 980 struct afs_vnode *dvnode, *vnode; 981 struct key *key; 982 int ret; 983 984 vnode = AFS_FS_I(from->d_inode); 985 dvnode = AFS_FS_I(dir); 986 987 _enter("{%x:%u},{%x:%u},{%s}", 988 vnode->fid.vid, vnode->fid.vnode, 989 dvnode->fid.vid, dvnode->fid.vnode, 990 dentry->d_name.name); 991 992 ret = -ENAMETOOLONG; 993 if (dentry->d_name.len >= AFSNAMEMAX) 994 goto error; 995 996 key = afs_request_key(dvnode->volume->cell); 997 if (IS_ERR(key)) { 998 ret = PTR_ERR(key); 999 goto error; 1000 } 1001 1002 ret = afs_vnode_link(dvnode, vnode, key, dentry->d_name.name); 1003 if (ret < 0) 1004 goto link_error; 1005 1006 atomic_inc(&vnode->vfs_inode.i_count); 1007 d_instantiate(dentry, &vnode->vfs_inode); 1008 key_put(key); 1009 _leave(" = 0"); 1010 return 0; 1011 1012 link_error: 1013 key_put(key); 1014 error: 1015 d_drop(dentry); 1016 _leave(" = %d", ret); 1017 return ret; 1018 } 1019 1020 /* 1021 * create a symlink in an AFS filesystem 1022 */ 1023 static int afs_symlink(struct inode *dir, struct dentry *dentry, 1024 const char *content) 1025 { 1026 struct afs_file_status status; 1027 struct afs_server *server; 1028 struct afs_vnode *dvnode, *vnode; 1029 struct afs_fid fid; 1030 struct inode *inode; 1031 struct key *key; 1032 int ret; 1033 1034 dvnode = AFS_FS_I(dir); 1035 1036 _enter("{%x:%u},{%s},%s", 1037 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, 1038 content); 1039 1040 ret = -ENAMETOOLONG; 1041 if (dentry->d_name.len >= AFSNAMEMAX) 1042 goto error; 1043 1044 ret = -EINVAL; 1045 if (strlen(content) >= AFSPATHMAX) 1046 goto error; 1047 1048 key = afs_request_key(dvnode->volume->cell); 1049 if (IS_ERR(key)) { 1050 ret = PTR_ERR(key); 1051 goto error; 1052 } 1053 1054 ret = afs_vnode_symlink(dvnode, key, dentry->d_name.name, content, 1055 &fid, &status, &server); 1056 if (ret < 0) 1057 goto create_error; 1058 1059 inode = afs_iget(dir->i_sb, key, &fid, &status, NULL); 1060 if (IS_ERR(inode)) { 1061 /* ENOMEM at a really inconvenient time - just abandon the new 1062 * directory on the server */ 1063 ret = PTR_ERR(inode); 1064 goto iget_error; 1065 } 1066 1067 /* apply the status report we've got for the new vnode */ 1068 vnode = AFS_FS_I(inode); 1069 spin_lock(&vnode->lock); 1070 vnode->update_cnt++; 1071 spin_unlock(&vnode->lock); 1072 afs_vnode_finalise_status_update(vnode, server); 1073 afs_put_server(server); 1074 1075 d_instantiate(dentry, inode); 1076 if (d_unhashed(dentry)) { 1077 _debug("not hashed"); 1078 d_rehash(dentry); 1079 } 1080 key_put(key); 1081 _leave(" = 0"); 1082 return 0; 1083 1084 iget_error: 1085 afs_put_server(server); 1086 create_error: 1087 key_put(key); 1088 error: 1089 d_drop(dentry); 1090 _leave(" = %d", ret); 1091 return ret; 1092 } 1093 1094 /* 1095 * rename a file in an AFS filesystem and/or move it between directories 1096 */ 1097 static int afs_rename(struct inode *old_dir, struct dentry *old_dentry, 1098 struct inode *new_dir, struct dentry *new_dentry) 1099 { 1100 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode; 1101 struct key *key; 1102 int ret; 1103 1104 vnode = AFS_FS_I(old_dentry->d_inode); 1105 orig_dvnode = AFS_FS_I(old_dir); 1106 new_dvnode = AFS_FS_I(new_dir); 1107 1108 _enter("{%x:%u},{%x:%u},{%x:%u},{%s}", 1109 orig_dvnode->fid.vid, orig_dvnode->fid.vnode, 1110 vnode->fid.vid, vnode->fid.vnode, 1111 new_dvnode->fid.vid, new_dvnode->fid.vnode, 1112 new_dentry->d_name.name); 1113 1114 ret = -ENAMETOOLONG; 1115 if (new_dentry->d_name.len >= AFSNAMEMAX) 1116 goto error; 1117 1118 key = afs_request_key(orig_dvnode->volume->cell); 1119 if (IS_ERR(key)) { 1120 ret = PTR_ERR(key); 1121 goto error; 1122 } 1123 1124 ret = afs_vnode_rename(orig_dvnode, new_dvnode, key, 1125 old_dentry->d_name.name, 1126 new_dentry->d_name.name); 1127 if (ret < 0) 1128 goto rename_error; 1129 key_put(key); 1130 _leave(" = 0"); 1131 return 0; 1132 1133 rename_error: 1134 key_put(key); 1135 error: 1136 d_drop(new_dentry); 1137 _leave(" = %d", ret); 1138 return ret; 1139 } 1140