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