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