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