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