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