1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* dir.c: AFS filesystem directory handling 3 * 4 * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 */ 7 8 #include <linux/kernel.h> 9 #include <linux/fs.h> 10 #include <linux/namei.h> 11 #include <linux/pagemap.h> 12 #include <linux/swap.h> 13 #include <linux/ctype.h> 14 #include <linux/sched.h> 15 #include <linux/iversion.h> 16 #include <linux/iov_iter.h> 17 #include <linux/task_io_accounting_ops.h> 18 #include "internal.h" 19 #include "afs_fs.h" 20 #include "xdr_fs.h" 21 22 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry, 23 unsigned int flags); 24 static int afs_dir_open(struct inode *inode, struct file *file); 25 static int afs_readdir(struct file *file, struct dir_context *ctx); 26 static int afs_d_revalidate(struct inode *dir, const struct qstr *name, 27 struct dentry *dentry, unsigned int flags); 28 static int afs_d_delete(const struct dentry *dentry); 29 static void afs_d_iput(struct dentry *dentry, struct inode *inode); 30 static bool afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen, 31 u64 ino, u32 uniquifier); 32 #define AFS_LOOKUP_ONE ((filldir_t)0x123UL) 33 static bool afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen, 34 u64 ino, u32 uniquifier); 35 #define AFS_LOOKUP ((filldir_t)0x137UL) 36 static int afs_create(struct mnt_idmap *idmap, struct inode *dir, 37 struct dentry *dentry, umode_t mode, bool excl); 38 static struct dentry *afs_mkdir(struct mnt_idmap *idmap, struct inode *dir, 39 struct dentry *dentry, umode_t mode); 40 static int afs_rmdir(struct inode *dir, struct dentry *dentry); 41 static int afs_unlink(struct inode *dir, struct dentry *dentry); 42 static int afs_link(struct dentry *from, struct inode *dir, 43 struct dentry *dentry); 44 static int afs_symlink(struct mnt_idmap *idmap, struct inode *dir, 45 struct dentry *dentry, const char *content); 46 static int afs_rename(struct mnt_idmap *idmap, struct inode *old_dir, 47 struct dentry *old_dentry, struct inode *new_dir, 48 struct dentry *new_dentry, unsigned int flags); 49 static int afs_dir_writepages(struct address_space *mapping, 50 struct writeback_control *wbc); 51 52 const struct file_operations afs_dir_file_operations = { 53 .open = afs_dir_open, 54 .release = afs_release, 55 .iterate_shared = afs_readdir, 56 .lock = afs_lock, 57 .llseek = generic_file_llseek, 58 }; 59 60 const struct inode_operations afs_dir_inode_operations = { 61 .create = afs_create, 62 .lookup = afs_lookup, 63 .link = afs_link, 64 .unlink = afs_unlink, 65 .symlink = afs_symlink, 66 .mkdir = afs_mkdir, 67 .rmdir = afs_rmdir, 68 .rename = afs_rename, 69 .permission = afs_permission, 70 .getattr = afs_getattr, 71 .setattr = afs_setattr, 72 }; 73 74 const struct address_space_operations afs_dir_aops = { 75 .writepages = afs_dir_writepages, 76 }; 77 78 const struct dentry_operations afs_fs_dentry_operations = { 79 .d_revalidate = afs_d_revalidate, 80 .d_delete = afs_d_delete, 81 .d_release = afs_d_release, 82 .d_automount = afs_d_automount, 83 .d_iput = afs_d_iput, 84 }; 85 86 struct afs_lookup_one_cookie { 87 struct dir_context ctx; 88 struct qstr name; 89 bool found; 90 struct afs_fid fid; 91 }; 92 93 struct afs_lookup_cookie { 94 struct dir_context ctx; 95 struct qstr name; 96 unsigned short nr_fids; 97 struct afs_fid fids[50]; 98 }; 99 100 static void afs_dir_unuse_cookie(struct afs_vnode *dvnode, int ret) 101 { 102 if (ret == 0) { 103 struct afs_vnode_cache_aux aux; 104 loff_t i_size = i_size_read(&dvnode->netfs.inode); 105 106 afs_set_cache_aux(dvnode, &aux); 107 fscache_unuse_cookie(afs_vnode_cache(dvnode), &aux, &i_size); 108 } else { 109 fscache_unuse_cookie(afs_vnode_cache(dvnode), NULL, NULL); 110 } 111 } 112 113 /* 114 * Iterate through a kmapped directory segment, dumping a summary of 115 * the contents. 116 */ 117 static size_t afs_dir_dump_step(void *iter_base, size_t progress, size_t len, 118 void *priv, void *priv2) 119 { 120 do { 121 union afs_xdr_dir_block *block = iter_base; 122 123 pr_warn("[%05zx] %32phN\n", progress, block); 124 iter_base += AFS_DIR_BLOCK_SIZE; 125 progress += AFS_DIR_BLOCK_SIZE; 126 len -= AFS_DIR_BLOCK_SIZE; 127 } while (len > 0); 128 129 return len; 130 } 131 132 /* 133 * Dump the contents of a directory. 134 */ 135 static void afs_dir_dump(struct afs_vnode *dvnode) 136 { 137 struct iov_iter iter; 138 unsigned long long i_size = i_size_read(&dvnode->netfs.inode); 139 140 pr_warn("DIR %llx:%llx is=%llx\n", 141 dvnode->fid.vid, dvnode->fid.vnode, i_size); 142 143 iov_iter_folio_queue(&iter, ITER_SOURCE, dvnode->directory, 0, 0, i_size); 144 iterate_folioq(&iter, iov_iter_count(&iter), NULL, NULL, 145 afs_dir_dump_step); 146 } 147 148 /* 149 * check that a directory folio is valid 150 */ 151 static bool afs_dir_check_block(struct afs_vnode *dvnode, size_t progress, 152 union afs_xdr_dir_block *block) 153 { 154 if (block->hdr.magic != AFS_DIR_MAGIC) { 155 pr_warn("%s(%llx): [%zx] bad magic %04x\n", 156 __func__, dvnode->netfs.inode.i_ino, 157 progress, ntohs(block->hdr.magic)); 158 trace_afs_dir_check_failed(dvnode, progress); 159 trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic); 160 return false; 161 } 162 163 /* Make sure each block is NUL terminated so we can reasonably 164 * use string functions on it. The filenames in the folio 165 * *should* be NUL-terminated anyway. 166 */ 167 ((u8 *)block)[AFS_DIR_BLOCK_SIZE - 1] = 0; 168 afs_stat_v(dvnode, n_read_dir); 169 return true; 170 } 171 172 /* 173 * Iterate through a kmapped directory segment, checking the content. 174 */ 175 static size_t afs_dir_check_step(void *iter_base, size_t progress, size_t len, 176 void *priv, void *priv2) 177 { 178 struct afs_vnode *dvnode = priv; 179 180 if (WARN_ON_ONCE(progress % AFS_DIR_BLOCK_SIZE || 181 len % AFS_DIR_BLOCK_SIZE)) 182 return len; 183 184 do { 185 if (!afs_dir_check_block(dvnode, progress, iter_base)) 186 break; 187 iter_base += AFS_DIR_BLOCK_SIZE; 188 len -= AFS_DIR_BLOCK_SIZE; 189 } while (len > 0); 190 191 return len; 192 } 193 194 /* 195 * Check all the blocks in a directory. 196 */ 197 static int afs_dir_check(struct afs_vnode *dvnode) 198 { 199 struct iov_iter iter; 200 unsigned long long i_size = i_size_read(&dvnode->netfs.inode); 201 size_t checked = 0; 202 203 if (unlikely(!i_size)) 204 return 0; 205 206 iov_iter_folio_queue(&iter, ITER_SOURCE, dvnode->directory, 0, 0, i_size); 207 checked = iterate_folioq(&iter, iov_iter_count(&iter), dvnode, NULL, 208 afs_dir_check_step); 209 if (checked != i_size) { 210 afs_dir_dump(dvnode); 211 return -EIO; 212 } 213 return 0; 214 } 215 216 /* 217 * open an AFS directory file 218 */ 219 static int afs_dir_open(struct inode *inode, struct file *file) 220 { 221 _enter("{%llu}", inode->i_ino); 222 223 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048); 224 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32); 225 226 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags)) 227 return -ENOENT; 228 229 return afs_open(inode, file); 230 } 231 232 /* 233 * Read a file in a single download. 234 */ 235 static ssize_t afs_do_read_single(struct afs_vnode *dvnode, struct file *file) 236 { 237 struct iov_iter iter; 238 ssize_t ret; 239 loff_t i_size; 240 241 i_size = i_size_read(&dvnode->netfs.inode); 242 if (i_size < AFS_DIR_BLOCK_SIZE) 243 return afs_bad(dvnode, afs_file_error_dir_small); 244 if (i_size > AFS_DIR_BLOCK_SIZE * 1024) { 245 trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big); 246 return -EFBIG; 247 } 248 249 /* Expand the storage. TODO: Shrink the storage too. */ 250 if (dvnode->directory_size < i_size) { 251 size_t cur_size = dvnode->directory_size; 252 253 ret = netfs_alloc_folioq_buffer(NULL, 254 &dvnode->directory, &cur_size, i_size, 255 mapping_gfp_mask(dvnode->netfs.inode.i_mapping)); 256 dvnode->directory_size = cur_size; 257 if (ret < 0) 258 return ret; 259 } 260 261 iov_iter_folio_queue(&iter, ITER_DEST, dvnode->directory, 0, 0, dvnode->directory_size); 262 263 /* AFS requires us to perform the read of a directory synchronously as 264 * a single unit to avoid issues with the directory contents being 265 * changed between reads. 266 */ 267 ret = netfs_read_single(&dvnode->netfs.inode, file, &iter); 268 if (ret >= 0) { 269 i_size = i_size_read(&dvnode->netfs.inode); 270 if (i_size > ret) { 271 /* The content has grown, so we need to expand the 272 * buffer. 273 */ 274 ret = -ESTALE; 275 } else { 276 int ret2 = afs_dir_check(dvnode); 277 278 if (ret2 < 0) 279 ret = ret2; 280 } 281 } 282 283 return ret; 284 } 285 286 static ssize_t afs_read_single(struct afs_vnode *dvnode, struct file *file) 287 { 288 ssize_t ret; 289 290 fscache_use_cookie(afs_vnode_cache(dvnode), false); 291 ret = afs_do_read_single(dvnode, file); 292 fscache_unuse_cookie(afs_vnode_cache(dvnode), NULL, NULL); 293 return ret; 294 } 295 296 /* 297 * Read the directory into a folio_queue buffer in one go, scrubbing the 298 * previous contents. We return -ESTALE if the caller needs to call us again. 299 */ 300 ssize_t afs_read_dir(struct afs_vnode *dvnode, struct file *file) 301 __acquires(&dvnode->validate_lock) 302 { 303 ssize_t ret; 304 loff_t i_size; 305 306 i_size = i_size_read(&dvnode->netfs.inode); 307 308 ret = -ERESTARTSYS; 309 if (down_read_killable(&dvnode->validate_lock) < 0) 310 goto error; 311 312 /* We only need to reread the data if it became invalid - or if we 313 * haven't read it yet. 314 */ 315 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) && 316 test_bit(AFS_VNODE_DIR_READ, &dvnode->flags)) { 317 ret = i_size; 318 goto valid; 319 } 320 321 up_read(&dvnode->validate_lock); 322 if (down_write_killable(&dvnode->validate_lock) < 0) 323 goto error; 324 325 if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) 326 afs_invalidate_cache(dvnode, 0); 327 328 if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) || 329 !test_bit(AFS_VNODE_DIR_READ, &dvnode->flags)) { 330 trace_afs_reload_dir(dvnode); 331 ret = afs_read_single(dvnode, file); 332 if (ret < 0) 333 goto error_unlock; 334 335 // TODO: Trim excess pages 336 337 set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags); 338 set_bit(AFS_VNODE_DIR_READ, &dvnode->flags); 339 } else { 340 ret = i_size; 341 } 342 343 downgrade_write(&dvnode->validate_lock); 344 valid: 345 return ret; 346 347 error_unlock: 348 up_write(&dvnode->validate_lock); 349 error: 350 _leave(" = %zd", ret); 351 return ret; 352 } 353 354 /* 355 * deal with one block in an AFS directory 356 */ 357 static int afs_dir_iterate_block(struct afs_vnode *dvnode, 358 struct dir_context *ctx, 359 union afs_xdr_dir_block *block) 360 { 361 union afs_xdr_dirent *dire; 362 unsigned int blknum, base, hdr, pos, next, nr_slots; 363 size_t nlen; 364 int tmp; 365 366 blknum = ctx->pos / AFS_DIR_BLOCK_SIZE; 367 base = blknum * AFS_DIR_SLOTS_PER_BLOCK; 368 hdr = (blknum == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS); 369 pos = DIV_ROUND_UP(ctx->pos, AFS_DIR_DIRENT_SIZE) - base; 370 371 _enter("%llx,%x", ctx->pos, blknum); 372 373 /* walk through the block, an entry at a time */ 374 for (unsigned int slot = hdr; slot < AFS_DIR_SLOTS_PER_BLOCK; slot = next) { 375 /* skip entries marked unused in the bitmap */ 376 if (!(block->hdr.bitmap[slot / 8] & 377 (1 << (slot % 8)))) { 378 _debug("ENT[%x]: Unused", base + slot); 379 next = slot + 1; 380 if (next >= pos) 381 ctx->pos = (base + next) * sizeof(union afs_xdr_dirent); 382 continue; 383 } 384 385 /* got a valid entry */ 386 dire = &block->dirents[slot]; 387 nlen = strnlen(dire->u.name, 388 (unsigned long)(block + 1) - (unsigned long)dire->u.name - 1); 389 if (nlen > AFSNAMEMAX - 1) { 390 _debug("ENT[%x]: Name too long (len %zx)", 391 base + slot, nlen); 392 return afs_bad(dvnode, afs_file_error_dir_name_too_long); 393 } 394 395 _debug("ENT[%x]: %s %zx \"%s\"", 396 base + slot, (slot < pos ? "skip" : "fill"), 397 nlen, dire->u.name); 398 399 nr_slots = afs_dir_calc_slots(nlen); 400 next = slot + nr_slots; 401 if (next > AFS_DIR_SLOTS_PER_BLOCK) { 402 _debug("ENT[%x]: extends beyond end dir block (len %zx)", 403 base + slot, nlen); 404 return afs_bad(dvnode, afs_file_error_dir_over_end); 405 } 406 407 /* Check that the name-extension dirents are all allocated */ 408 for (tmp = 1; tmp < nr_slots; tmp++) { 409 unsigned int xslot = slot + tmp; 410 411 if (!(block->hdr.bitmap[xslot / 8] & (1 << (xslot % 8)))) { 412 _debug("ENT[%x]: Unmarked extension (%x/%x)", 413 base + slot, tmp, nr_slots); 414 return afs_bad(dvnode, afs_file_error_dir_unmarked_ext); 415 } 416 } 417 418 /* skip if starts before the current position */ 419 if (slot < pos) { 420 if (next > pos) 421 ctx->pos = (base + next) * sizeof(union afs_xdr_dirent); 422 continue; 423 } 424 425 /* found the next entry */ 426 if (ctx->actor == AFS_LOOKUP) { 427 if (!afs_lookup_filldir(ctx, dire->u.name, nlen, 428 ntohl(dire->u.vnode), 429 ntohl(dire->u.unique))) 430 return 0; 431 } else if (ctx->actor == AFS_LOOKUP_ONE) { 432 if (!afs_lookup_one_filldir(ctx, dire->u.name, nlen, 433 ntohl(dire->u.vnode), 434 ntohl(dire->u.unique))) 435 return 0; 436 } else if (!dir_emit(ctx, dire->u.name, nlen, 437 ntohl(dire->u.vnode), DT_UNKNOWN)) { 438 _leave(" = 0 [full]"); 439 return 0; 440 } 441 442 ctx->pos = (base + next) * sizeof(union afs_xdr_dirent); 443 } 444 445 _leave(" = 1 [more]"); 446 return 1; 447 } 448 449 struct afs_dir_iteration_ctx { 450 struct dir_context *dir_ctx; 451 int error; 452 }; 453 454 /* 455 * Iterate through a kmapped directory segment. 456 */ 457 static size_t afs_dir_iterate_step(void *iter_base, size_t progress, size_t len, 458 void *priv, void *priv2) 459 { 460 struct afs_dir_iteration_ctx *ctx = priv2; 461 struct afs_vnode *dvnode = priv; 462 int ret; 463 464 if (WARN_ON_ONCE(progress % AFS_DIR_BLOCK_SIZE || 465 len % AFS_DIR_BLOCK_SIZE)) { 466 pr_err("Mis-iteration prog=%zx len=%zx\n", 467 progress % AFS_DIR_BLOCK_SIZE, 468 len % AFS_DIR_BLOCK_SIZE); 469 return len; 470 } 471 472 do { 473 ret = afs_dir_iterate_block(dvnode, ctx->dir_ctx, iter_base); 474 if (ret != 1) 475 break; 476 477 ctx->dir_ctx->pos = round_up(ctx->dir_ctx->pos, AFS_DIR_BLOCK_SIZE); 478 iter_base += AFS_DIR_BLOCK_SIZE; 479 len -= AFS_DIR_BLOCK_SIZE; 480 } while (len > 0); 481 482 return len; 483 } 484 485 /* 486 * Iterate through the directory folios. 487 */ 488 static int afs_dir_iterate_contents(struct inode *dir, struct dir_context *dir_ctx) 489 { 490 struct afs_dir_iteration_ctx ctx = { .dir_ctx = dir_ctx }; 491 struct afs_vnode *dvnode = AFS_FS_I(dir); 492 struct iov_iter iter; 493 unsigned long long i_size = i_size_read(dir); 494 495 /* Round the file position up to the next entry boundary */ 496 dir_ctx->pos = round_up(dir_ctx->pos, sizeof(union afs_xdr_dirent)); 497 498 if (i_size <= 0 || dir_ctx->pos >= i_size) 499 return 0; 500 501 iov_iter_folio_queue(&iter, ITER_SOURCE, dvnode->directory, 0, 0, i_size); 502 iov_iter_advance(&iter, round_down(dir_ctx->pos, AFS_DIR_BLOCK_SIZE)); 503 504 iterate_folioq(&iter, iov_iter_count(&iter), dvnode, &ctx, 505 afs_dir_iterate_step); 506 507 if (ctx.error == -ESTALE) 508 afs_invalidate_dir(dvnode, afs_dir_invalid_iter_stale); 509 return ctx.error; 510 } 511 512 /* 513 * iterate through the data blob that lists the contents of an AFS directory 514 */ 515 static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx, 516 struct file *file, afs_dataversion_t *_dir_version) 517 { 518 struct afs_vnode *dvnode = AFS_FS_I(dir); 519 int retry_limit = 100; 520 int ret; 521 522 _enter("{%llu},%llx,,", dir->i_ino, ctx->pos); 523 524 do { 525 if (--retry_limit < 0) { 526 pr_warn("afs_read_dir(): Too many retries\n"); 527 ret = -ESTALE; 528 break; 529 } 530 ret = afs_read_dir(dvnode, file); 531 if (ret < 0) { 532 if (ret != -ESTALE) 533 break; 534 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) { 535 ret = -ESTALE; 536 break; 537 } 538 continue; 539 } 540 *_dir_version = inode_peek_iversion_raw(dir); 541 542 ret = afs_dir_iterate_contents(dir, ctx); 543 up_read(&dvnode->validate_lock); 544 } while (ret == -ESTALE); 545 546 _leave(" = %d", ret); 547 return ret; 548 } 549 550 /* 551 * read an AFS directory 552 */ 553 static int afs_readdir(struct file *file, struct dir_context *ctx) 554 { 555 afs_dataversion_t dir_version; 556 557 ctx->dt_flags_mask = UINT_MAX; 558 return afs_dir_iterate(file_inode(file), ctx, file, &dir_version); 559 } 560 561 /* 562 * Search the directory for a single name 563 * - if afs_dir_iterate_block() spots this function, it'll pass the FID 564 * uniquifier through dtype 565 */ 566 static bool afs_lookup_one_filldir(struct dir_context *ctx, const char *name, 567 int nlen, u64 ino, u32 uniquifier) 568 { 569 struct afs_lookup_one_cookie *cookie = 570 container_of(ctx, struct afs_lookup_one_cookie, ctx); 571 572 _enter("{%s,%u},%s,%u,,%llu,%u", 573 cookie->name.name, cookie->name.len, name, nlen, 574 (unsigned long long) ino, uniquifier); 575 576 /* insanity checks first */ 577 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048); 578 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32); 579 580 if (cookie->name.len != nlen || 581 memcmp(cookie->name.name, name, nlen) != 0) { 582 _leave(" = true [keep looking]"); 583 return true; 584 } 585 586 cookie->fid.vnode = ino; 587 cookie->fid.unique = uniquifier; 588 cookie->found = 1; 589 590 _leave(" = false [found]"); 591 return false; 592 } 593 594 /* 595 * Do a lookup of a single name in a directory 596 * - just returns the FID the dentry name maps to if found 597 */ 598 static int afs_do_lookup_one(struct inode *dir, const struct qstr *name, 599 struct afs_fid *fid, 600 afs_dataversion_t *_dir_version) 601 { 602 struct afs_super_info *as = dir->i_sb->s_fs_info; 603 struct afs_lookup_one_cookie cookie = { 604 .ctx.actor = AFS_LOOKUP_ONE, 605 .name = *name, 606 .fid.vid = as->volume->vid 607 }; 608 int ret; 609 610 _enter("{%llu},{%.*s},", dir->i_ino, name->len, name->name); 611 612 /* search the directory */ 613 ret = afs_dir_iterate(dir, &cookie.ctx, NULL, _dir_version); 614 if (ret < 0) { 615 _leave(" = %d [iter]", ret); 616 return ret; 617 } 618 619 if (!cookie.found) { 620 _leave(" = -ENOENT [not found]"); 621 return -ENOENT; 622 } 623 624 *fid = cookie.fid; 625 _leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique); 626 return 0; 627 } 628 629 /* 630 * search the directory for a name 631 * - if afs_dir_iterate_block() spots this function, it'll pass the FID 632 * uniquifier through dtype 633 */ 634 static bool afs_lookup_filldir(struct dir_context *ctx, const char *name, 635 int nlen, u64 ino, u32 uniquifier) 636 { 637 struct afs_lookup_cookie *cookie = 638 container_of(ctx, struct afs_lookup_cookie, ctx); 639 640 _enter("{%s,%u},%s,%u,,%llu,%u", 641 cookie->name.name, cookie->name.len, name, nlen, 642 (unsigned long long) ino, uniquifier); 643 644 /* insanity checks first */ 645 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048); 646 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32); 647 648 if (cookie->nr_fids < 50) { 649 cookie->fids[cookie->nr_fids].vnode = ino; 650 cookie->fids[cookie->nr_fids].unique = uniquifier; 651 cookie->nr_fids++; 652 } 653 654 return cookie->nr_fids < 50; 655 } 656 657 /* 658 * Deal with the result of a successful lookup operation. Turn all the files 659 * into inodes and save the first one - which is the one we actually want. 660 */ 661 static void afs_do_lookup_success(struct afs_operation *op) 662 { 663 struct afs_vnode_param *vp; 664 struct afs_vnode *vnode; 665 struct inode *inode; 666 u32 abort_code; 667 int i; 668 669 _enter(""); 670 671 for (i = 0; i < op->nr_files; i++) { 672 switch (i) { 673 case 0: 674 vp = &op->file[0]; 675 abort_code = vp->scb.status.abort_code; 676 if (abort_code != 0) { 677 op->call_abort_code = abort_code; 678 afs_op_set_error(op, afs_abort_to_error(abort_code)); 679 op->cumul_error.abort_code = abort_code; 680 } 681 break; 682 683 case 1: 684 vp = &op->file[1]; 685 break; 686 687 default: 688 vp = &op->more_files[i - 2]; 689 break; 690 } 691 692 if (vp->scb.status.abort_code) 693 trace_afs_bulkstat_error(op, &vp->fid, i, vp->scb.status.abort_code); 694 if (!vp->scb.have_status && !vp->scb.have_error) 695 continue; 696 697 _debug("do [%u]", i); 698 if (vp->vnode) { 699 if (!test_bit(AFS_VNODE_UNSET, &vp->vnode->flags)) 700 afs_vnode_commit_status(op, vp); 701 } else if (vp->scb.status.abort_code == 0) { 702 inode = afs_iget(op, vp); 703 if (!IS_ERR(inode)) { 704 vnode = AFS_FS_I(inode); 705 afs_cache_permit(vnode, op->key, 706 0 /* Assume vnode->cb_break is 0 */ + 707 op->cb_v_break, 708 &vp->scb); 709 vp->vnode = vnode; 710 vp->put_vnode = true; 711 } 712 } else { 713 _debug("- abort %d %llx:%llx.%x", 714 vp->scb.status.abort_code, 715 vp->fid.vid, vp->fid.vnode, vp->fid.unique); 716 } 717 } 718 719 _leave(""); 720 } 721 722 static const struct afs_operation_ops afs_inline_bulk_status_operation = { 723 .issue_afs_rpc = afs_fs_inline_bulk_status, 724 .issue_yfs_rpc = yfs_fs_inline_bulk_status, 725 .success = afs_do_lookup_success, 726 }; 727 728 static const struct afs_operation_ops afs_lookup_fetch_status_operation = { 729 .issue_afs_rpc = afs_fs_fetch_status, 730 .issue_yfs_rpc = yfs_fs_fetch_status, 731 .success = afs_do_lookup_success, 732 .aborted = afs_check_for_remote_deletion, 733 }; 734 735 /* 736 * See if we know that the server we expect to use doesn't support 737 * FS.InlineBulkStatus. 738 */ 739 static bool afs_server_supports_ibulk(struct afs_vnode *dvnode) 740 { 741 struct afs_server_list *slist; 742 struct afs_volume *volume = dvnode->volume; 743 struct afs_server *server; 744 bool ret = true; 745 int i; 746 747 if (!test_bit(AFS_VOLUME_MAYBE_NO_IBULK, &volume->flags)) 748 return true; 749 750 rcu_read_lock(); 751 slist = rcu_dereference(volume->servers); 752 753 for (i = 0; i < slist->nr_servers; i++) { 754 server = slist->servers[i].server; 755 if (server == dvnode->cb_server) { 756 if (test_bit(AFS_SERVER_FL_NO_IBULK, &server->flags)) 757 ret = false; 758 break; 759 } 760 } 761 762 rcu_read_unlock(); 763 return ret; 764 } 765 766 /* 767 * Do a lookup in a directory. We make use of bulk lookup to query a slew of 768 * files in one go and create inodes for them. The inode of the file we were 769 * asked for is returned. 770 */ 771 static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry) 772 { 773 struct afs_lookup_cookie *cookie; 774 struct afs_vnode_param *vp; 775 struct afs_operation *op; 776 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode; 777 struct inode *inode = NULL, *ti; 778 afs_dataversion_t data_version = READ_ONCE(dvnode->status.data_version); 779 bool supports_ibulk, isnew; 780 long ret; 781 int i; 782 783 _enter("{%llu},%p{%pd},", dir->i_ino, dentry, dentry); 784 785 cookie = kzalloc_obj(struct afs_lookup_cookie); 786 if (!cookie) 787 return ERR_PTR(-ENOMEM); 788 789 for (i = 0; i < ARRAY_SIZE(cookie->fids); i++) 790 cookie->fids[i].vid = dvnode->fid.vid; 791 cookie->ctx.actor = AFS_LOOKUP; 792 cookie->name = dentry->d_name; 793 cookie->nr_fids = 2; /* slot 1 is saved for the fid we actually want 794 * and slot 0 for the directory */ 795 796 /* Search the directory for the named entry using the hash table... */ 797 ret = afs_dir_search(dvnode, &dentry->d_name, &cookie->fids[1], &data_version); 798 if (ret < 0) 799 goto out; 800 801 supports_ibulk = afs_server_supports_ibulk(dvnode); 802 if (supports_ibulk) { 803 /* ...then scan linearly from that point for entries to lookup-ahead. */ 804 cookie->ctx.pos = (ret + 1) * AFS_DIR_DIRENT_SIZE; 805 afs_dir_iterate(dir, &cookie->ctx, NULL, &data_version); 806 } 807 808 dentry->d_fsdata = (void *)(unsigned long)data_version; 809 810 /* Check to see if we already have an inode for the primary fid. */ 811 inode = ilookup5(dir->i_sb, cookie->fids[1].vnode, 812 afs_ilookup5_test_by_fid, &cookie->fids[1]); 813 if (inode) 814 goto out; /* We do */ 815 816 /* Okay, we didn't find it. We need to query the server - and whilst 817 * we're doing that, we're going to attempt to look up a bunch of other 818 * vnodes also. 819 */ 820 op = afs_alloc_operation(NULL, dvnode->volume); 821 if (IS_ERR(op)) { 822 ret = PTR_ERR(op); 823 goto out; 824 } 825 826 afs_op_set_vnode(op, 0, dvnode); 827 afs_op_set_fid(op, 1, &cookie->fids[1]); 828 829 op->nr_files = cookie->nr_fids; 830 _debug("nr_files %u", op->nr_files); 831 832 /* Need space for examining all the selected files */ 833 if (op->nr_files > 2) { 834 op->more_files = kvzalloc_objs(struct afs_vnode_param, 835 op->nr_files - 2); 836 if (!op->more_files) { 837 afs_op_nomem(op); 838 goto out_op; 839 } 840 841 for (i = 2; i < op->nr_files; i++) { 842 vp = &op->more_files[i - 2]; 843 vp->fid = cookie->fids[i]; 844 845 /* Find any inodes that already exist and get their 846 * callback counters. 847 */ 848 ti = ilookup5_nowait(dir->i_sb, vp->fid.vnode, 849 afs_ilookup5_test_by_fid, &vp->fid, &isnew); 850 if (!IS_ERR_OR_NULL(ti)) { 851 vnode = AFS_FS_I(ti); 852 vp->dv_before = vnode->status.data_version; 853 vp->cb_break_before = afs_calc_vnode_cb_break(vnode); 854 vp->vnode = vnode; 855 vp->put_vnode = true; 856 vp->speculative = true; /* vnode not locked */ 857 } 858 } 859 } 860 861 /* Try FS.InlineBulkStatus first. Abort codes for the individual 862 * lookups contained therein are stored in the reply without aborting 863 * the whole operation. 864 */ 865 afs_op_set_error(op, -ENOTSUPP); 866 if (supports_ibulk) { 867 op->ops = &afs_inline_bulk_status_operation; 868 afs_begin_vnode_operation(op); 869 afs_wait_for_operation(op); 870 } 871 872 if (afs_op_error(op) == -ENOTSUPP) { 873 /* We could try FS.BulkStatus next, but this aborts the entire 874 * op if any of the lookups fails - so, for the moment, revert 875 * to FS.FetchStatus for op->file[1]. 876 */ 877 op->fetch_status.which = 1; 878 op->ops = &afs_lookup_fetch_status_operation; 879 afs_begin_vnode_operation(op); 880 afs_wait_for_operation(op); 881 } 882 883 out_op: 884 if (!afs_op_error(op)) { 885 if (op->file[1].scb.status.abort_code) { 886 afs_op_accumulate_error(op, -ECONNABORTED, 887 op->file[1].scb.status.abort_code); 888 } else { 889 inode = &op->file[1].vnode->netfs.inode; 890 op->file[1].vnode = NULL; 891 } 892 } 893 894 if (op->file[0].scb.have_status) 895 dentry->d_fsdata = (void *)(unsigned long)op->file[0].scb.status.data_version; 896 else 897 dentry->d_fsdata = (void *)(unsigned long)op->file[0].dv_before; 898 ret = afs_put_operation(op); 899 out: 900 kfree(cookie); 901 _leave(""); 902 return inode ?: ERR_PTR(ret); 903 } 904 905 /* 906 * Look up an entry in a directory with @sys substitution. 907 */ 908 static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry) 909 { 910 struct afs_sysnames *subs; 911 struct afs_net *net = afs_i2net(dir); 912 struct dentry *ret; 913 char *buf, *p, *name; 914 int len, i; 915 916 _enter(""); 917 918 ret = ERR_PTR(-ENOMEM); 919 p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL); 920 if (!buf) 921 goto out_p; 922 if (dentry->d_name.len > 4) { 923 memcpy(p, dentry->d_name.name, dentry->d_name.len - 4); 924 p += dentry->d_name.len - 4; 925 } 926 927 /* There is an ordered list of substitutes that we have to try. */ 928 read_lock(&net->sysnames_lock); 929 subs = net->sysnames; 930 refcount_inc(&subs->usage); 931 read_unlock(&net->sysnames_lock); 932 933 for (i = 0; i < subs->nr; i++) { 934 name = subs->subs[i]; 935 len = dentry->d_name.len - 4 + strlen(name); 936 if (len >= AFSNAMEMAX) { 937 ret = ERR_PTR(-ENAMETOOLONG); 938 goto out_s; 939 } 940 941 strcpy(p, name); 942 ret = lookup_noperm(&QSTR(buf), dentry->d_parent); 943 if (IS_ERR(ret) || d_is_positive(ret)) 944 goto out_s; 945 dput(ret); 946 } 947 948 /* We don't want to d_add() the @sys dentry here as we don't want to 949 * the cached dentry to hide changes to the sysnames list. 950 */ 951 ret = NULL; 952 out_s: 953 afs_put_sysnames(subs); 954 kfree(buf); 955 out_p: 956 return ret; 957 } 958 959 /* 960 * look up an entry in a directory 961 */ 962 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry, 963 unsigned int flags) 964 { 965 struct afs_vnode *dvnode = AFS_FS_I(dir); 966 struct afs_fid fid = {}; 967 struct inode *inode; 968 struct dentry *d; 969 int ret; 970 971 _enter("{%llx:%llu},%p{%pd},", 972 dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry); 973 974 ASSERTCMP(d_inode(dentry), ==, NULL); 975 976 if (dentry->d_name.len >= AFSNAMEMAX) { 977 _leave(" = -ENAMETOOLONG"); 978 return ERR_PTR(-ENAMETOOLONG); 979 } 980 981 if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) { 982 _leave(" = -ESTALE"); 983 return ERR_PTR(-ESTALE); 984 } 985 986 ret = afs_validate(dvnode, NULL); 987 if (ret < 0) { 988 afs_dir_unuse_cookie(dvnode, ret); 989 _leave(" = %d [val]", ret); 990 return ERR_PTR(ret); 991 } 992 993 if (dentry->d_name.len >= 4 && 994 dentry->d_name.name[dentry->d_name.len - 4] == '@' && 995 dentry->d_name.name[dentry->d_name.len - 3] == 's' && 996 dentry->d_name.name[dentry->d_name.len - 2] == 'y' && 997 dentry->d_name.name[dentry->d_name.len - 1] == 's') 998 return afs_lookup_atsys(dir, dentry); 999 1000 afs_stat_v(dvnode, n_lookup); 1001 inode = afs_do_lookup(dir, dentry); 1002 if (inode == ERR_PTR(-ENOENT)) 1003 inode = NULL; 1004 else if (!IS_ERR_OR_NULL(inode)) 1005 fid = AFS_FS_I(inode)->fid; 1006 1007 _debug("splice %p", dentry->d_inode); 1008 d = d_splice_alias(inode, dentry); 1009 if (!IS_ERR_OR_NULL(d)) { 1010 d->d_fsdata = dentry->d_fsdata; 1011 trace_afs_lookup(dvnode, &d->d_name, &fid); 1012 } else { 1013 trace_afs_lookup(dvnode, &dentry->d_name, &fid); 1014 } 1015 _leave(""); 1016 return d; 1017 } 1018 1019 /* 1020 * Check the validity of a dentry under RCU conditions. 1021 */ 1022 static int afs_d_revalidate_rcu(struct afs_vnode *dvnode, struct dentry *dentry) 1023 { 1024 long dir_version, de_version; 1025 1026 _enter("%p", dentry); 1027 1028 if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) 1029 return -ECHILD; 1030 1031 if (!afs_check_validity(dvnode)) 1032 return -ECHILD; 1033 1034 /* We only need to invalidate a dentry if the server's copy changed 1035 * behind our back. If we made the change, it's no problem. Note that 1036 * on a 32-bit system, we only have 32 bits in the dentry to store the 1037 * version. 1038 */ 1039 dir_version = (long)READ_ONCE(dvnode->status.data_version); 1040 de_version = (long)READ_ONCE(dentry->d_fsdata); 1041 if (de_version != dir_version) { 1042 dir_version = (long)READ_ONCE(dvnode->invalid_before); 1043 if (de_version - dir_version < 0) 1044 return -ECHILD; 1045 } 1046 1047 return 1; /* Still valid */ 1048 } 1049 1050 /* 1051 * check that a dentry lookup hit has found a valid entry 1052 * - NOTE! the hit can be a negative hit too, so we can't assume we have an 1053 * inode 1054 */ 1055 static int afs_d_revalidate(struct inode *parent_dir, const struct qstr *name, 1056 struct dentry *dentry, unsigned int flags) 1057 { 1058 struct afs_vnode *vnode, *dir = AFS_FS_I(parent_dir); 1059 struct afs_fid fid; 1060 struct inode *inode; 1061 struct key *key; 1062 afs_dataversion_t dir_version, invalid_before; 1063 long de_version; 1064 int ret; 1065 1066 if (flags & LOOKUP_RCU) 1067 return afs_d_revalidate_rcu(dir, dentry); 1068 1069 if (d_really_is_positive(dentry)) { 1070 vnode = AFS_FS_I(d_inode(dentry)); 1071 _enter("{v={%llx:%llu} n=%pd fl=%lx},", 1072 vnode->fid.vid, vnode->fid.vnode, dentry, 1073 vnode->flags); 1074 } else { 1075 _enter("{neg n=%pd}", dentry); 1076 } 1077 1078 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell); 1079 if (IS_ERR(key)) 1080 key = NULL; 1081 1082 /* validate the parent directory */ 1083 ret = afs_validate(dir, key); 1084 if (ret == -ERESTARTSYS) { 1085 key_put(key); 1086 return ret; 1087 } 1088 1089 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) { 1090 _debug("%pd: parent dir deleted", dentry); 1091 goto not_found; 1092 } 1093 1094 /* We only need to invalidate a dentry if the server's copy changed 1095 * behind our back. If we made the change, it's no problem. Note that 1096 * on a 32-bit system, we only have 32 bits in the dentry to store the 1097 * version. 1098 */ 1099 dir_version = dir->status.data_version; 1100 de_version = (long)dentry->d_fsdata; 1101 if (de_version == (long)dir_version) 1102 goto out_valid_noupdate; 1103 1104 invalid_before = dir->invalid_before; 1105 if (de_version - (long)invalid_before >= 0) 1106 goto out_valid; 1107 1108 _debug("dir modified"); 1109 afs_stat_v(dir, n_reval); 1110 1111 /* search the directory for this vnode */ 1112 ret = afs_do_lookup_one(&dir->netfs.inode, name, &fid, &dir_version); 1113 switch (ret) { 1114 case 0: 1115 /* the filename maps to something */ 1116 if (d_really_is_negative(dentry)) 1117 goto not_found; 1118 inode = d_inode(dentry); 1119 if (is_bad_inode(inode)) { 1120 printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n", 1121 dentry); 1122 goto not_found; 1123 } 1124 1125 vnode = AFS_FS_I(inode); 1126 1127 /* if the vnode ID has changed, then the dirent points to a 1128 * different file */ 1129 if (fid.vnode != vnode->fid.vnode) { 1130 _debug("%pd: dirent changed [%llu != %llu]", 1131 dentry, fid.vnode, 1132 vnode->fid.vnode); 1133 goto not_found; 1134 } 1135 1136 /* if the vnode ID uniqifier has changed, then the file has 1137 * been deleted and replaced, and the original vnode ID has 1138 * been reused */ 1139 if (fid.unique != vnode->fid.unique) { 1140 _debug("%pd: file deleted (uq %u -> %u I:%u)", 1141 dentry, fid.unique, 1142 vnode->fid.unique, 1143 vnode->netfs.inode.i_generation); 1144 goto not_found; 1145 } 1146 goto out_valid; 1147 1148 case -ENOENT: 1149 /* the filename is unknown */ 1150 _debug("%pd: dirent not found", dentry); 1151 if (d_really_is_positive(dentry)) 1152 goto not_found; 1153 goto out_valid; 1154 1155 default: 1156 _debug("failed to iterate parent %pd2: %d", dentry, ret); 1157 goto not_found; 1158 } 1159 1160 out_valid: 1161 dentry->d_fsdata = (void *)(unsigned long)dir_version; 1162 out_valid_noupdate: 1163 key_put(key); 1164 _leave(" = 1 [valid]"); 1165 return 1; 1166 1167 not_found: 1168 _debug("dropping dentry %pd2", dentry); 1169 key_put(key); 1170 1171 _leave(" = 0 [bad]"); 1172 return 0; 1173 } 1174 1175 /* 1176 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't 1177 * sleep) 1178 * - called from dput() when d_count is going to 0. 1179 * - return 1 to request dentry be unhashed, 0 otherwise 1180 */ 1181 static int afs_d_delete(const struct dentry *dentry) 1182 { 1183 _enter("%pd", dentry); 1184 1185 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1186 goto zap; 1187 1188 if (d_really_is_positive(dentry) && 1189 (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) || 1190 test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags))) 1191 goto zap; 1192 1193 _leave(" = 0 [keep]"); 1194 return 0; 1195 1196 zap: 1197 _leave(" = 1 [zap]"); 1198 return 1; 1199 } 1200 1201 /* 1202 * Clean up sillyrename files on dentry removal. 1203 */ 1204 static void afs_d_iput(struct dentry *dentry, struct inode *inode) 1205 { 1206 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1207 afs_silly_iput(dentry, inode); 1208 iput(inode); 1209 } 1210 1211 /* 1212 * handle dentry release 1213 */ 1214 void afs_d_release(struct dentry *dentry) 1215 { 1216 _enter("%pd", dentry); 1217 } 1218 1219 void afs_check_for_remote_deletion(struct afs_operation *op) 1220 { 1221 struct afs_vnode *vnode = op->file[0].vnode; 1222 1223 switch (afs_op_abort_code(op)) { 1224 case VNOVNODE: 1225 set_bit(AFS_VNODE_DELETED, &vnode->flags); 1226 clear_nlink(&vnode->netfs.inode); 1227 afs_break_callback(vnode, afs_cb_break_for_deleted); 1228 } 1229 } 1230 1231 /* 1232 * Create a new inode for create/mkdir/symlink 1233 */ 1234 static void afs_vnode_new_inode(struct afs_operation *op) 1235 { 1236 struct afs_vnode_param *dvp = &op->file[0]; 1237 struct afs_vnode_param *vp = &op->file[1]; 1238 struct afs_vnode *vnode; 1239 struct inode *inode; 1240 1241 _enter(""); 1242 1243 ASSERTCMP(afs_op_error(op), ==, 0); 1244 1245 inode = afs_iget(op, vp); 1246 if (IS_ERR(inode)) { 1247 /* ENOMEM or EINTR at a really inconvenient time - just abandon 1248 * the new directory on the server. 1249 */ 1250 afs_op_accumulate_error(op, PTR_ERR(inode), 0); 1251 return; 1252 } 1253 1254 vnode = AFS_FS_I(inode); 1255 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); 1256 if (S_ISDIR(inode->i_mode)) 1257 afs_mkdir_init_dir(vnode, dvp->vnode); 1258 else if (S_ISLNK(inode->i_mode)) 1259 afs_init_new_symlink(vnode, op); 1260 if (!afs_op_error(op)) 1261 afs_cache_permit(vnode, op->key, vnode->cb_break, &vp->scb); 1262 d_instantiate(op->dentry, inode); 1263 } 1264 1265 static void afs_create_success(struct afs_operation *op) 1266 { 1267 _enter("op=%08x", op->debug_id); 1268 op->ctime = op->file[0].scb.status.mtime_client; 1269 afs_vnode_commit_status(op, &op->file[0]); 1270 afs_update_dentry_version(op, &op->file[0], op->dentry); 1271 afs_vnode_new_inode(op); 1272 } 1273 1274 static void afs_create_edit_dir(struct afs_operation *op) 1275 { 1276 struct netfs_cache_resources cres = {}; 1277 struct afs_vnode_param *dvp = &op->file[0]; 1278 struct afs_vnode_param *vp = &op->file[1]; 1279 struct afs_vnode *dvnode = dvp->vnode; 1280 1281 _enter("op=%08x", op->debug_id); 1282 1283 fscache_begin_write_operation(&cres, afs_vnode_cache(dvnode)); 1284 down_write(&dvnode->validate_lock); 1285 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) && 1286 dvnode->status.data_version == dvp->dv_before + dvp->dv_delta) 1287 afs_edit_dir_add(dvnode, &op->dentry->d_name, &vp->fid, 1288 op->create.reason); 1289 up_write(&dvnode->validate_lock); 1290 fscache_end_operation(&cres); 1291 } 1292 1293 static void afs_create_put(struct afs_operation *op) 1294 { 1295 _enter("op=%08x", op->debug_id); 1296 1297 if (afs_op_error(op)) 1298 d_drop(op->dentry); 1299 } 1300 1301 static const struct afs_operation_ops afs_mkdir_operation = { 1302 .issue_afs_rpc = afs_fs_make_dir, 1303 .issue_yfs_rpc = yfs_fs_make_dir, 1304 .success = afs_create_success, 1305 .aborted = afs_check_for_remote_deletion, 1306 .edit_dir = afs_create_edit_dir, 1307 .put = afs_create_put, 1308 }; 1309 1310 /* 1311 * create a directory on an AFS filesystem 1312 */ 1313 static struct dentry *afs_mkdir(struct mnt_idmap *idmap, struct inode *dir, 1314 struct dentry *dentry, umode_t mode) 1315 { 1316 struct afs_operation *op; 1317 struct afs_vnode *dvnode = AFS_FS_I(dir); 1318 int ret; 1319 1320 _enter("{%llx:%llu},{%pd},%ho", 1321 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode); 1322 1323 op = afs_alloc_operation(NULL, dvnode->volume); 1324 if (IS_ERR(op)) { 1325 d_drop(dentry); 1326 return ERR_CAST(op); 1327 } 1328 1329 fscache_use_cookie(afs_vnode_cache(dvnode), true); 1330 1331 afs_op_set_vnode(op, 0, dvnode); 1332 op->file[0].dv_delta = 1; 1333 op->file[0].modification = true; 1334 op->file[0].update_ctime = true; 1335 op->dentry = dentry; 1336 op->create.mode = S_IFDIR | mode; 1337 op->create.reason = afs_edit_dir_for_mkdir; 1338 op->mtime = current_time(dir); 1339 op->ops = &afs_mkdir_operation; 1340 ret = afs_do_sync_operation(op); 1341 afs_dir_unuse_cookie(dvnode, ret); 1342 return ERR_PTR(ret); 1343 } 1344 1345 /* 1346 * Remove a subdir from a directory. 1347 */ 1348 static void afs_dir_remove_subdir(struct dentry *dentry) 1349 { 1350 if (d_really_is_positive(dentry)) { 1351 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); 1352 1353 clear_nlink(&vnode->netfs.inode); 1354 set_bit(AFS_VNODE_DELETED, &vnode->flags); 1355 afs_clear_cb_promise(vnode, afs_cb_promise_clear_rmdir); 1356 afs_invalidate_dir(vnode, afs_dir_invalid_subdir_removed); 1357 } 1358 } 1359 1360 static void afs_rmdir_success(struct afs_operation *op) 1361 { 1362 _enter("op=%08x", op->debug_id); 1363 op->ctime = op->file[0].scb.status.mtime_client; 1364 afs_vnode_commit_status(op, &op->file[0]); 1365 afs_update_dentry_version(op, &op->file[0], op->dentry); 1366 } 1367 1368 static void afs_rmdir_edit_dir(struct afs_operation *op) 1369 { 1370 struct netfs_cache_resources cres = {}; 1371 struct afs_vnode_param *dvp = &op->file[0]; 1372 struct afs_vnode *dvnode = dvp->vnode; 1373 1374 _enter("op=%08x", op->debug_id); 1375 afs_dir_remove_subdir(op->dentry); 1376 1377 fscache_begin_write_operation(&cres, afs_vnode_cache(dvnode)); 1378 down_write(&dvnode->validate_lock); 1379 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) && 1380 dvnode->status.data_version == dvp->dv_before + dvp->dv_delta) 1381 afs_edit_dir_remove(dvnode, &op->dentry->d_name, 1382 afs_edit_dir_for_rmdir); 1383 up_write(&dvnode->validate_lock); 1384 fscache_end_operation(&cres); 1385 } 1386 1387 static void afs_rmdir_put(struct afs_operation *op) 1388 { 1389 _enter("op=%08x", op->debug_id); 1390 if (op->file[1].vnode) 1391 up_write(&op->file[1].vnode->rmdir_lock); 1392 } 1393 1394 static const struct afs_operation_ops afs_rmdir_operation = { 1395 .issue_afs_rpc = afs_fs_remove_dir, 1396 .issue_yfs_rpc = yfs_fs_remove_dir, 1397 .success = afs_rmdir_success, 1398 .aborted = afs_check_for_remote_deletion, 1399 .edit_dir = afs_rmdir_edit_dir, 1400 .put = afs_rmdir_put, 1401 }; 1402 1403 /* 1404 * remove a directory from an AFS filesystem 1405 */ 1406 static int afs_rmdir(struct inode *dir, struct dentry *dentry) 1407 { 1408 struct afs_operation *op; 1409 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL; 1410 int ret; 1411 1412 _enter("{%llx:%llu},{%pd}", 1413 dvnode->fid.vid, dvnode->fid.vnode, dentry); 1414 1415 op = afs_alloc_operation(NULL, dvnode->volume); 1416 if (IS_ERR(op)) 1417 return PTR_ERR(op); 1418 1419 fscache_use_cookie(afs_vnode_cache(dvnode), true); 1420 1421 afs_op_set_vnode(op, 0, dvnode); 1422 op->file[0].dv_delta = 1; 1423 op->file[0].modification = true; 1424 op->file[0].update_ctime = true; 1425 1426 op->dentry = dentry; 1427 op->ops = &afs_rmdir_operation; 1428 1429 /* Try to make sure we have a callback promise on the victim. */ 1430 if (d_really_is_positive(dentry)) { 1431 vnode = AFS_FS_I(d_inode(dentry)); 1432 ret = afs_validate(vnode, op->key); 1433 if (ret < 0) 1434 goto error; 1435 } 1436 1437 if (vnode) { 1438 ret = down_write_killable(&vnode->rmdir_lock); 1439 if (ret < 0) 1440 goto error; 1441 op->file[1].vnode = vnode; 1442 } 1443 1444 ret = afs_do_sync_operation(op); 1445 1446 /* Not all systems that can host afs servers have ENOTEMPTY. */ 1447 if (ret == -EEXIST) 1448 ret = -ENOTEMPTY; 1449 out: 1450 afs_dir_unuse_cookie(dvnode, ret); 1451 return ret; 1452 1453 error: 1454 ret = afs_put_operation(op); 1455 goto out; 1456 } 1457 1458 /* 1459 * Remove a link to a file or symlink from a directory. 1460 * 1461 * If the file was not deleted due to excess hard links, the fileserver will 1462 * break the callback promise on the file - if it had one - before it returns 1463 * to us, and if it was deleted, it won't 1464 * 1465 * However, if we didn't have a callback promise outstanding, or it was 1466 * outstanding on a different server, then it won't break it either... 1467 */ 1468 static void afs_dir_remove_link(struct afs_operation *op) 1469 { 1470 struct afs_vnode *dvnode = op->file[0].vnode; 1471 struct afs_vnode *vnode = op->file[1].vnode; 1472 struct dentry *dentry = op->dentry; 1473 int ret; 1474 1475 if (afs_op_error(op) || 1476 (op->file[1].scb.have_status && op->file[1].scb.have_error)) 1477 return; 1478 if (d_really_is_positive(dentry)) 1479 return; 1480 1481 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) { 1482 /* Already done */ 1483 } else if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) { 1484 write_seqlock(&vnode->cb_lock); 1485 drop_nlink(&vnode->netfs.inode); 1486 if (vnode->netfs.inode.i_nlink == 0) { 1487 set_bit(AFS_VNODE_DELETED, &vnode->flags); 1488 __afs_break_callback(vnode, afs_cb_break_for_unlink); 1489 } 1490 write_sequnlock(&vnode->cb_lock); 1491 } else { 1492 afs_break_callback(vnode, afs_cb_break_for_unlink); 1493 1494 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) 1495 _debug("AFS_VNODE_DELETED"); 1496 1497 ret = afs_validate(vnode, op->key); 1498 if (ret != -ESTALE) 1499 afs_op_set_error(op, ret); 1500 } 1501 1502 _debug("nlink %d [val %d]", vnode->netfs.inode.i_nlink, afs_op_error(op)); 1503 } 1504 1505 static void afs_unlink_success(struct afs_operation *op) 1506 { 1507 _enter("op=%08x", op->debug_id); 1508 op->ctime = op->file[0].scb.status.mtime_client; 1509 afs_check_dir_conflict(op, &op->file[0]); 1510 afs_vnode_commit_status(op, &op->file[0]); 1511 afs_vnode_commit_status(op, &op->file[1]); 1512 afs_update_dentry_version(op, &op->file[0], op->dentry); 1513 afs_dir_remove_link(op); 1514 } 1515 1516 static void afs_unlink_edit_dir(struct afs_operation *op) 1517 { 1518 struct netfs_cache_resources cres = {}; 1519 struct afs_vnode_param *dvp = &op->file[0]; 1520 struct afs_vnode *dvnode = dvp->vnode; 1521 1522 _enter("op=%08x", op->debug_id); 1523 fscache_begin_write_operation(&cres, afs_vnode_cache(dvnode)); 1524 down_write(&dvnode->validate_lock); 1525 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) && 1526 dvnode->status.data_version == dvp->dv_before + dvp->dv_delta) 1527 afs_edit_dir_remove(dvnode, &op->dentry->d_name, 1528 afs_edit_dir_for_unlink); 1529 up_write(&dvnode->validate_lock); 1530 fscache_end_operation(&cres); 1531 } 1532 1533 static void afs_unlink_put(struct afs_operation *op) 1534 { 1535 _enter("op=%08x", op->debug_id); 1536 if (op->unlink.need_rehash && afs_op_error(op) < 0 && afs_op_error(op) != -ENOENT) 1537 d_rehash(op->dentry); 1538 } 1539 1540 static const struct afs_operation_ops afs_unlink_operation = { 1541 .issue_afs_rpc = afs_fs_remove_file, 1542 .issue_yfs_rpc = yfs_fs_remove_file, 1543 .success = afs_unlink_success, 1544 .aborted = afs_check_for_remote_deletion, 1545 .edit_dir = afs_unlink_edit_dir, 1546 .put = afs_unlink_put, 1547 }; 1548 1549 /* 1550 * Remove a file or symlink from an AFS filesystem. 1551 */ 1552 static int afs_unlink(struct inode *dir, struct dentry *dentry) 1553 { 1554 struct afs_operation *op; 1555 struct afs_vnode *dvnode = AFS_FS_I(dir); 1556 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); 1557 int ret; 1558 1559 _enter("{%llx:%llu},{%pd}", 1560 dvnode->fid.vid, dvnode->fid.vnode, dentry); 1561 1562 if (dentry->d_name.len >= AFSNAMEMAX) 1563 return -ENAMETOOLONG; 1564 1565 op = afs_alloc_operation(NULL, dvnode->volume); 1566 if (IS_ERR(op)) 1567 return PTR_ERR(op); 1568 1569 fscache_use_cookie(afs_vnode_cache(dvnode), true); 1570 1571 afs_op_set_vnode(op, 0, dvnode); 1572 op->file[0].dv_delta = 1; 1573 op->file[0].modification = true; 1574 op->file[0].update_ctime = true; 1575 1576 /* Try to make sure we have a callback promise on the victim. */ 1577 ret = afs_validate(vnode, op->key); 1578 if (ret < 0) { 1579 afs_op_set_error(op, ret); 1580 goto error; 1581 } 1582 1583 spin_lock(&dentry->d_lock); 1584 if (d_count(dentry) > 1) { 1585 spin_unlock(&dentry->d_lock); 1586 /* Start asynchronous writeout of the inode */ 1587 write_inode_now(d_inode(dentry), 0); 1588 afs_op_set_error(op, afs_sillyrename(dvnode, vnode, dentry, op->key)); 1589 goto error; 1590 } 1591 if (!d_unhashed(dentry)) { 1592 /* Prevent a race with RCU lookup. */ 1593 __d_drop(dentry); 1594 op->unlink.need_rehash = true; 1595 } 1596 spin_unlock(&dentry->d_lock); 1597 1598 op->file[1].vnode = vnode; 1599 op->file[1].update_ctime = true; 1600 op->file[1].op_unlinked = true; 1601 op->dentry = dentry; 1602 op->ops = &afs_unlink_operation; 1603 afs_begin_vnode_operation(op); 1604 afs_wait_for_operation(op); 1605 1606 /* If there was a conflict with a third party, check the status of the 1607 * unlinked vnode. 1608 */ 1609 if (afs_op_error(op) == 0 && (op->flags & AFS_OPERATION_DIR_CONFLICT)) { 1610 op->file[1].update_ctime = false; 1611 op->fetch_status.which = 1; 1612 op->ops = &afs_fetch_status_operation; 1613 afs_begin_vnode_operation(op); 1614 afs_wait_for_operation(op); 1615 } 1616 1617 error: 1618 ret = afs_put_operation(op); 1619 afs_dir_unuse_cookie(dvnode, ret); 1620 return ret; 1621 } 1622 1623 static const struct afs_operation_ops afs_create_operation = { 1624 .issue_afs_rpc = afs_fs_create_file, 1625 .issue_yfs_rpc = yfs_fs_create_file, 1626 .success = afs_create_success, 1627 .aborted = afs_check_for_remote_deletion, 1628 .edit_dir = afs_create_edit_dir, 1629 .put = afs_create_put, 1630 }; 1631 1632 /* 1633 * create a regular file on an AFS filesystem 1634 */ 1635 static int afs_create(struct mnt_idmap *idmap, struct inode *dir, 1636 struct dentry *dentry, umode_t mode, bool excl) 1637 { 1638 struct afs_operation *op; 1639 struct afs_vnode *dvnode = AFS_FS_I(dir); 1640 int ret = -ENAMETOOLONG; 1641 1642 _enter("{%llx:%llu},{%pd},%ho", 1643 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode); 1644 1645 if (dentry->d_name.len >= AFSNAMEMAX) 1646 goto error; 1647 1648 op = afs_alloc_operation(NULL, dvnode->volume); 1649 if (IS_ERR(op)) { 1650 ret = PTR_ERR(op); 1651 goto error; 1652 } 1653 1654 fscache_use_cookie(afs_vnode_cache(dvnode), true); 1655 1656 afs_op_set_vnode(op, 0, dvnode); 1657 op->file[0].dv_delta = 1; 1658 op->file[0].modification = true; 1659 op->file[0].update_ctime = true; 1660 1661 op->dentry = dentry; 1662 op->create.mode = S_IFREG | mode; 1663 op->create.reason = afs_edit_dir_for_create; 1664 op->mtime = current_time(dir); 1665 op->ops = &afs_create_operation; 1666 ret = afs_do_sync_operation(op); 1667 afs_dir_unuse_cookie(dvnode, ret); 1668 return ret; 1669 1670 error: 1671 d_drop(dentry); 1672 _leave(" = %d", ret); 1673 return ret; 1674 } 1675 1676 static void afs_link_success(struct afs_operation *op) 1677 { 1678 struct afs_vnode_param *dvp = &op->file[0]; 1679 struct afs_vnode_param *vp = &op->file[1]; 1680 1681 _enter("op=%08x", op->debug_id); 1682 op->ctime = dvp->scb.status.mtime_client; 1683 afs_vnode_commit_status(op, dvp); 1684 afs_vnode_commit_status(op, vp); 1685 afs_update_dentry_version(op, dvp, op->dentry); 1686 if (op->dentry_2->d_parent == op->dentry->d_parent) 1687 afs_update_dentry_version(op, dvp, op->dentry_2); 1688 ihold(&vp->vnode->netfs.inode); 1689 d_instantiate(op->dentry, &vp->vnode->netfs.inode); 1690 } 1691 1692 static void afs_link_put(struct afs_operation *op) 1693 { 1694 _enter("op=%08x", op->debug_id); 1695 if (afs_op_error(op)) 1696 d_drop(op->dentry); 1697 } 1698 1699 static const struct afs_operation_ops afs_link_operation = { 1700 .issue_afs_rpc = afs_fs_link, 1701 .issue_yfs_rpc = yfs_fs_link, 1702 .success = afs_link_success, 1703 .aborted = afs_check_for_remote_deletion, 1704 .edit_dir = afs_create_edit_dir, 1705 .put = afs_link_put, 1706 }; 1707 1708 /* 1709 * create a hard link between files in an AFS filesystem 1710 */ 1711 static int afs_link(struct dentry *from, struct inode *dir, 1712 struct dentry *dentry) 1713 { 1714 struct afs_operation *op; 1715 struct afs_vnode *dvnode = AFS_FS_I(dir); 1716 struct afs_vnode *vnode = AFS_FS_I(d_inode(from)); 1717 int ret = -ENAMETOOLONG; 1718 1719 _enter("{%llx:%llu},{%llx:%llu},{%pd}", 1720 vnode->fid.vid, vnode->fid.vnode, 1721 dvnode->fid.vid, dvnode->fid.vnode, 1722 dentry); 1723 1724 if (dentry->d_name.len >= AFSNAMEMAX) 1725 goto error; 1726 1727 op = afs_alloc_operation(NULL, dvnode->volume); 1728 if (IS_ERR(op)) { 1729 ret = PTR_ERR(op); 1730 goto error; 1731 } 1732 1733 fscache_use_cookie(afs_vnode_cache(dvnode), true); 1734 1735 ret = afs_validate(vnode, op->key); 1736 if (ret < 0) 1737 goto error_op; 1738 1739 afs_op_set_vnode(op, 0, dvnode); 1740 afs_op_set_vnode(op, 1, vnode); 1741 op->file[0].dv_delta = 1; 1742 op->file[0].modification = true; 1743 op->file[0].update_ctime = true; 1744 op->file[1].update_ctime = true; 1745 1746 op->dentry = dentry; 1747 op->dentry_2 = from; 1748 op->ops = &afs_link_operation; 1749 op->create.reason = afs_edit_dir_for_link; 1750 ret = afs_do_sync_operation(op); 1751 afs_dir_unuse_cookie(dvnode, ret); 1752 return ret; 1753 1754 error_op: 1755 afs_put_operation(op); 1756 afs_dir_unuse_cookie(dvnode, ret); 1757 error: 1758 d_drop(dentry); 1759 _leave(" = %d", ret); 1760 return ret; 1761 } 1762 1763 static void afs_symlink_put(struct afs_operation *op) 1764 { 1765 kfree(op->create.symlink); 1766 op->create.symlink = NULL; 1767 afs_create_put(op); 1768 } 1769 1770 static const struct afs_operation_ops afs_symlink_operation = { 1771 .issue_afs_rpc = afs_fs_symlink, 1772 .issue_yfs_rpc = yfs_fs_symlink, 1773 .success = afs_create_success, 1774 .aborted = afs_check_for_remote_deletion, 1775 .edit_dir = afs_create_edit_dir, 1776 .put = afs_symlink_put, 1777 }; 1778 1779 /* 1780 * create a symlink in an AFS filesystem 1781 */ 1782 static int afs_symlink(struct mnt_idmap *idmap, struct inode *dir, 1783 struct dentry *dentry, const char *content) 1784 { 1785 struct afs_operation *op; 1786 struct afs_symlink *symlink; 1787 struct afs_vnode *dvnode = AFS_FS_I(dir); 1788 size_t clen = strlen(content); 1789 int ret; 1790 1791 _enter("{%llx:%llu},{%pd},%s", 1792 dvnode->fid.vid, dvnode->fid.vnode, dentry, 1793 content); 1794 1795 ret = -ENAMETOOLONG; 1796 if (dentry->d_name.len >= AFSNAMEMAX) 1797 goto error; 1798 1799 ret = -EINVAL; 1800 if (clen >= AFSPATHMAX) 1801 goto error; 1802 1803 ret = -ENOMEM; 1804 symlink = kmalloc_flex(struct afs_symlink, content, clen + 1, GFP_KERNEL); 1805 if (!symlink) 1806 goto error; 1807 refcount_set(&symlink->ref, 1); 1808 memcpy(symlink->content, content, clen + 1); 1809 1810 op = afs_alloc_operation(NULL, dvnode->volume); 1811 if (IS_ERR(op)) { 1812 ret = PTR_ERR(op); 1813 kfree(symlink); 1814 goto error; 1815 } 1816 1817 fscache_use_cookie(afs_vnode_cache(dvnode), true); 1818 1819 afs_op_set_vnode(op, 0, dvnode); 1820 op->file[0].dv_delta = 1; 1821 1822 op->dentry = dentry; 1823 op->ops = &afs_symlink_operation; 1824 op->create.reason = afs_edit_dir_for_symlink; 1825 op->create.symlink = symlink; 1826 op->mtime = current_time(dir); 1827 ret = afs_do_sync_operation(op); 1828 afs_dir_unuse_cookie(dvnode, ret); 1829 return ret; 1830 1831 error: 1832 d_drop(dentry); 1833 _leave(" = %d", ret); 1834 return ret; 1835 } 1836 1837 static void afs_rename_success(struct afs_operation *op) 1838 { 1839 struct afs_vnode *vnode = op->more_files[0].vnode; 1840 struct afs_vnode *new_vnode = op->more_files[1].vnode; 1841 1842 _enter("op=%08x", op->debug_id); 1843 1844 op->ctime = op->file[0].scb.status.mtime_client; 1845 afs_check_dir_conflict(op, &op->file[1]); 1846 afs_vnode_commit_status(op, &op->file[0]); 1847 if (op->file[1].vnode != op->file[0].vnode) { 1848 op->ctime = op->file[1].scb.status.mtime_client; 1849 afs_vnode_commit_status(op, &op->file[1]); 1850 } 1851 if (op->more_files[0].scb.have_status) 1852 afs_vnode_commit_status(op, &op->more_files[0]); 1853 if (op->more_files[1].scb.have_status) 1854 afs_vnode_commit_status(op, &op->more_files[1]); 1855 1856 /* If we're moving a subdir between dirs, we need to update 1857 * its DV counter too as the ".." will be altered. 1858 */ 1859 if (op->file[0].vnode != op->file[1].vnode) { 1860 if (S_ISDIR(vnode->netfs.inode.i_mode)) { 1861 u64 new_dv; 1862 1863 write_seqlock(&vnode->cb_lock); 1864 1865 new_dv = vnode->status.data_version + 1; 1866 trace_afs_set_dv(vnode, new_dv); 1867 vnode->status.data_version = new_dv; 1868 inode_set_iversion_raw(&vnode->netfs.inode, new_dv); 1869 1870 write_sequnlock(&vnode->cb_lock); 1871 } 1872 1873 if ((op->rename.rename_flags & RENAME_EXCHANGE) && 1874 S_ISDIR(new_vnode->netfs.inode.i_mode)) { 1875 u64 new_dv; 1876 1877 write_seqlock(&new_vnode->cb_lock); 1878 1879 new_dv = new_vnode->status.data_version + 1; 1880 new_vnode->status.data_version = new_dv; 1881 inode_set_iversion_raw(&new_vnode->netfs.inode, new_dv); 1882 1883 write_sequnlock(&new_vnode->cb_lock); 1884 } 1885 } 1886 } 1887 1888 static void afs_rename_edit_dir(struct afs_operation *op) 1889 { 1890 struct netfs_cache_resources orig_cres = {}, new_cres = {}; 1891 struct afs_vnode_param *orig_dvp = &op->file[0]; 1892 struct afs_vnode_param *new_dvp = &op->file[1]; 1893 struct afs_vnode *orig_dvnode = orig_dvp->vnode; 1894 struct afs_vnode *new_dvnode = new_dvp->vnode; 1895 struct afs_vnode *vnode = AFS_FS_I(d_inode(op->dentry)); 1896 struct dentry *old_dentry = op->dentry; 1897 struct dentry *new_dentry = op->dentry_2; 1898 struct inode *new_inode; 1899 1900 _enter("op=%08x", op->debug_id); 1901 1902 if (op->rename.rehash) { 1903 d_rehash(op->rename.rehash); 1904 op->rename.rehash = NULL; 1905 } 1906 1907 fscache_begin_write_operation(&orig_cres, afs_vnode_cache(orig_dvnode)); 1908 if (new_dvnode != orig_dvnode) 1909 fscache_begin_write_operation(&new_cres, afs_vnode_cache(new_dvnode)); 1910 1911 down_write(&orig_dvnode->validate_lock); 1912 if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags) && 1913 orig_dvnode->status.data_version == orig_dvp->dv_before + orig_dvp->dv_delta) 1914 afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name, 1915 afs_edit_dir_for_rename_0); 1916 1917 if (new_dvnode != orig_dvnode) { 1918 up_write(&orig_dvnode->validate_lock); 1919 down_write(&new_dvnode->validate_lock); 1920 } 1921 1922 if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags) && 1923 new_dvnode->status.data_version == new_dvp->dv_before + new_dvp->dv_delta) { 1924 if (!op->rename.new_negative) 1925 afs_edit_dir_remove(new_dvnode, &new_dentry->d_name, 1926 afs_edit_dir_for_rename_1); 1927 1928 afs_edit_dir_add(new_dvnode, &new_dentry->d_name, 1929 &vnode->fid, afs_edit_dir_for_rename_2); 1930 } 1931 1932 if (S_ISDIR(vnode->netfs.inode.i_mode) && 1933 new_dvnode != orig_dvnode && 1934 test_bit(AFS_VNODE_DIR_VALID, &vnode->flags)) 1935 afs_edit_dir_update(vnode, &dotdot_name, new_dvnode, 1936 afs_edit_dir_for_rename_sub); 1937 1938 new_inode = d_inode(new_dentry); 1939 if (new_inode) { 1940 spin_lock(&new_inode->i_lock); 1941 if (S_ISDIR(new_inode->i_mode)) 1942 clear_nlink(new_inode); 1943 else if (new_inode->i_nlink > 0) 1944 drop_nlink(new_inode); 1945 spin_unlock(&new_inode->i_lock); 1946 } 1947 1948 /* Now we can update d_fsdata on the dentries to reflect their 1949 * new parent's data_version. 1950 */ 1951 afs_update_dentry_version(op, new_dvp, op->dentry); 1952 afs_update_dentry_version(op, new_dvp, op->dentry_2); 1953 1954 d_move(old_dentry, new_dentry); 1955 1956 up_write(&new_dvnode->validate_lock); 1957 fscache_end_operation(&orig_cres); 1958 if (new_dvnode != orig_dvnode) 1959 fscache_end_operation(&new_cres); 1960 } 1961 1962 static void afs_rename_exchange_edit_dir(struct afs_operation *op) 1963 { 1964 struct afs_vnode_param *orig_dvp = &op->file[0]; 1965 struct afs_vnode_param *new_dvp = &op->file[1]; 1966 struct afs_vnode *orig_dvnode = orig_dvp->vnode; 1967 struct afs_vnode *new_dvnode = new_dvp->vnode; 1968 struct afs_vnode *old_vnode = op->more_files[0].vnode; 1969 struct afs_vnode *new_vnode = op->more_files[1].vnode; 1970 struct dentry *old_dentry = op->dentry; 1971 struct dentry *new_dentry = op->dentry_2; 1972 1973 _enter("op=%08x", op->debug_id); 1974 1975 if (new_dvnode == orig_dvnode) { 1976 down_write(&orig_dvnode->validate_lock); 1977 if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags) && 1978 orig_dvnode->status.data_version == orig_dvp->dv_before + orig_dvp->dv_delta) { 1979 afs_edit_dir_update(orig_dvnode, &old_dentry->d_name, 1980 new_vnode, afs_edit_dir_for_rename_0); 1981 afs_edit_dir_update(orig_dvnode, &new_dentry->d_name, 1982 old_vnode, afs_edit_dir_for_rename_1); 1983 } 1984 1985 d_exchange(old_dentry, new_dentry); 1986 up_write(&orig_dvnode->validate_lock); 1987 } else { 1988 down_write(&orig_dvnode->validate_lock); 1989 if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags) && 1990 orig_dvnode->status.data_version == orig_dvp->dv_before + orig_dvp->dv_delta) 1991 afs_edit_dir_update(orig_dvnode, &old_dentry->d_name, 1992 new_vnode, afs_edit_dir_for_rename_0); 1993 1994 up_write(&orig_dvnode->validate_lock); 1995 down_write(&new_dvnode->validate_lock); 1996 1997 if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags) && 1998 new_dvnode->status.data_version == new_dvp->dv_before + new_dvp->dv_delta) 1999 afs_edit_dir_update(new_dvnode, &new_dentry->d_name, 2000 old_vnode, afs_edit_dir_for_rename_1); 2001 2002 if (S_ISDIR(old_vnode->netfs.inode.i_mode) && 2003 test_bit(AFS_VNODE_DIR_VALID, &old_vnode->flags)) 2004 afs_edit_dir_update(old_vnode, &dotdot_name, new_dvnode, 2005 afs_edit_dir_for_rename_sub); 2006 2007 if (S_ISDIR(new_vnode->netfs.inode.i_mode) && 2008 test_bit(AFS_VNODE_DIR_VALID, &new_vnode->flags)) 2009 afs_edit_dir_update(new_vnode, &dotdot_name, orig_dvnode, 2010 afs_edit_dir_for_rename_sub); 2011 2012 /* Now we can update d_fsdata on the dentries to reflect their 2013 * new parents' data_version. 2014 */ 2015 afs_update_dentry_version(op, new_dvp, old_dentry); 2016 afs_update_dentry_version(op, orig_dvp, new_dentry); 2017 2018 d_exchange(old_dentry, new_dentry); 2019 up_write(&new_dvnode->validate_lock); 2020 } 2021 } 2022 2023 static void afs_rename_put(struct afs_operation *op) 2024 { 2025 _enter("op=%08x", op->debug_id); 2026 if (op->rename.rehash) 2027 d_rehash(op->rename.rehash); 2028 dput(op->rename.tmp); 2029 if (afs_op_error(op)) 2030 d_rehash(op->dentry); 2031 } 2032 2033 static const struct afs_operation_ops afs_rename_operation = { 2034 .issue_afs_rpc = afs_fs_rename, 2035 .issue_yfs_rpc = yfs_fs_rename, 2036 .success = afs_rename_success, 2037 .edit_dir = afs_rename_edit_dir, 2038 .put = afs_rename_put, 2039 }; 2040 2041 #if 0 /* Autoswitched in yfs_fs_rename_replace(). */ 2042 static const struct afs_operation_ops afs_rename_replace_operation = { 2043 .issue_afs_rpc = NULL, 2044 .issue_yfs_rpc = yfs_fs_rename_replace, 2045 .success = afs_rename_success, 2046 .edit_dir = afs_rename_edit_dir, 2047 .put = afs_rename_put, 2048 }; 2049 #endif 2050 2051 static const struct afs_operation_ops afs_rename_noreplace_operation = { 2052 .issue_afs_rpc = NULL, 2053 .issue_yfs_rpc = yfs_fs_rename_noreplace, 2054 .success = afs_rename_success, 2055 .edit_dir = afs_rename_edit_dir, 2056 .put = afs_rename_put, 2057 }; 2058 2059 static const struct afs_operation_ops afs_rename_exchange_operation = { 2060 .issue_afs_rpc = NULL, 2061 .issue_yfs_rpc = yfs_fs_rename_exchange, 2062 .success = afs_rename_success, 2063 .edit_dir = afs_rename_exchange_edit_dir, 2064 .put = afs_rename_put, 2065 }; 2066 2067 /* 2068 * rename a file in an AFS filesystem and/or move it between directories 2069 */ 2070 static int afs_rename(struct mnt_idmap *idmap, struct inode *old_dir, 2071 struct dentry *old_dentry, struct inode *new_dir, 2072 struct dentry *new_dentry, unsigned int flags) 2073 { 2074 struct afs_operation *op; 2075 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode, *new_vnode = NULL; 2076 int ret; 2077 2078 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE)) 2079 return -EINVAL; 2080 2081 /* Don't allow silly-rename files be moved around. */ 2082 if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED) 2083 return -EINVAL; 2084 2085 vnode = AFS_FS_I(d_inode(old_dentry)); 2086 orig_dvnode = AFS_FS_I(old_dir); 2087 new_dvnode = AFS_FS_I(new_dir); 2088 if (d_is_positive(new_dentry)) 2089 new_vnode = AFS_FS_I(d_inode(new_dentry)); 2090 2091 _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}", 2092 orig_dvnode->fid.vid, orig_dvnode->fid.vnode, 2093 vnode->fid.vid, vnode->fid.vnode, 2094 new_dvnode->fid.vid, new_dvnode->fid.vnode, 2095 new_dentry); 2096 2097 op = afs_alloc_operation(NULL, orig_dvnode->volume); 2098 if (IS_ERR(op)) 2099 return PTR_ERR(op); 2100 2101 fscache_use_cookie(afs_vnode_cache(orig_dvnode), true); 2102 if (new_dvnode != orig_dvnode) 2103 fscache_use_cookie(afs_vnode_cache(new_dvnode), true); 2104 2105 ret = afs_validate(vnode, op->key); 2106 afs_op_set_error(op, ret); 2107 if (ret < 0) 2108 goto error; 2109 2110 ret = -ENOMEM; 2111 op->more_files = kvzalloc_objs(struct afs_vnode_param, 2); 2112 if (!op->more_files) 2113 goto error; 2114 2115 afs_op_set_vnode(op, 0, orig_dvnode); 2116 afs_op_set_vnode(op, 1, new_dvnode); /* May be same as orig_dvnode */ 2117 op->file[0].dv_delta = 1; 2118 op->file[1].dv_delta = 1; 2119 op->file[0].modification = true; 2120 op->file[1].modification = true; 2121 op->file[0].update_ctime = true; 2122 op->file[1].update_ctime = true; 2123 op->more_files[0].vnode = vnode; 2124 op->more_files[0].speculative = true; 2125 op->more_files[1].vnode = new_vnode; 2126 op->more_files[1].speculative = true; 2127 op->nr_files = 4; 2128 2129 op->dentry = old_dentry; 2130 op->dentry_2 = new_dentry; 2131 op->rename.rename_flags = flags; 2132 op->rename.new_negative = d_is_negative(new_dentry); 2133 2134 if (flags & RENAME_NOREPLACE) { 2135 op->ops = &afs_rename_noreplace_operation; 2136 } else if (flags & RENAME_EXCHANGE) { 2137 op->ops = &afs_rename_exchange_operation; 2138 d_drop(new_dentry); 2139 } else { 2140 /* If we might displace the target, we might need to do silly 2141 * rename. 2142 */ 2143 op->ops = &afs_rename_operation; 2144 2145 /* For non-directories, check whether the target is busy and if 2146 * so, make a copy of the dentry and then do a silly-rename. 2147 * If the silly-rename succeeds, the copied dentry is hashed 2148 * and becomes the new target. 2149 */ 2150 if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) { 2151 /* To prevent any new references to the target during 2152 * the rename, we unhash the dentry in advance. 2153 */ 2154 if (!d_unhashed(new_dentry)) { 2155 d_drop(new_dentry); 2156 op->rename.rehash = new_dentry; 2157 } 2158 2159 if (d_count(new_dentry) > 2) { 2160 /* copy the target dentry's name */ 2161 op->rename.tmp = d_alloc(new_dentry->d_parent, 2162 &new_dentry->d_name); 2163 if (!op->rename.tmp) { 2164 afs_op_nomem(op); 2165 goto error; 2166 } 2167 2168 ret = afs_sillyrename(new_dvnode, 2169 AFS_FS_I(d_inode(new_dentry)), 2170 new_dentry, op->key); 2171 if (ret) { 2172 afs_op_set_error(op, ret); 2173 goto error; 2174 } 2175 2176 op->dentry_2 = op->rename.tmp; 2177 op->rename.rehash = NULL; 2178 op->rename.new_negative = true; 2179 } 2180 } 2181 } 2182 2183 /* This bit is potentially nasty as there's a potential race with 2184 * afs_d_revalidate{,_rcu}(). We have to change d_fsdata on the dentry 2185 * to reflect it's new parent's new data_version after the op, but 2186 * d_revalidate may see old_dentry between the op having taken place 2187 * and the version being updated. 2188 * 2189 * So drop the old_dentry for now to make other threads go through 2190 * lookup instead - which we hold a lock against. 2191 */ 2192 d_drop(old_dentry); 2193 2194 ret = afs_do_sync_operation(op); 2195 if (ret == -ENOTSUPP) 2196 ret = -EINVAL; 2197 out: 2198 afs_dir_unuse_cookie(orig_dvnode, ret); 2199 if (new_dvnode != orig_dvnode) 2200 afs_dir_unuse_cookie(new_dvnode, ret); 2201 return ret; 2202 2203 error: 2204 ret = afs_put_operation(op); 2205 goto out; 2206 } 2207 2208 /* 2209 * Write the directory contents to the cache as a single blob. 2210 */ 2211 static int afs_dir_writepages(struct address_space *mapping, 2212 struct writeback_control *wbc) 2213 { 2214 struct afs_vnode *dvnode = AFS_FS_I(mapping->host); 2215 struct iov_iter iter; 2216 int ret = 0; 2217 2218 /* Need to lock to prevent the folio queue and folios from being thrown 2219 * away. 2220 */ 2221 if (!down_read_trylock(&dvnode->validate_lock)) { 2222 if (wbc->sync_mode == WB_SYNC_NONE) { 2223 /* The VFS will have undirtied the inode. */ 2224 netfs_single_mark_inode_dirty(&dvnode->netfs.inode); 2225 return 0; 2226 } 2227 down_read(&dvnode->validate_lock); 2228 } 2229 2230 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) { 2231 iov_iter_folio_queue(&iter, ITER_SOURCE, dvnode->directory, 0, 0, 2232 i_size_read(&dvnode->netfs.inode)); 2233 ret = netfs_writeback_single(mapping, wbc, &iter); 2234 if (ret == 1) 2235 ret = 0; /* Skipped write due to lock conflict. */ 2236 } 2237 2238 up_read(&dvnode->validate_lock); 2239 return ret; 2240 } 2241