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 struct dentry *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_noperm(&QSTR(buf), dentry->d_parent); 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 = NULL; 1008 else if (!IS_ERR_OR_NULL(inode)) 1009 fid = AFS_FS_I(inode)->fid; 1010 1011 _debug("splice %p", dentry->d_inode); 1012 d = d_splice_alias(inode, dentry); 1013 if (!IS_ERR_OR_NULL(d)) { 1014 d->d_fsdata = dentry->d_fsdata; 1015 trace_afs_lookup(dvnode, &d->d_name, &fid); 1016 } else { 1017 trace_afs_lookup(dvnode, &dentry->d_name, &fid); 1018 } 1019 _leave(""); 1020 return d; 1021 } 1022 1023 /* 1024 * Check the validity of a dentry under RCU conditions. 1025 */ 1026 static int afs_d_revalidate_rcu(struct afs_vnode *dvnode, struct dentry *dentry) 1027 { 1028 long dir_version, de_version; 1029 1030 _enter("%p", dentry); 1031 1032 if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) 1033 return -ECHILD; 1034 1035 if (!afs_check_validity(dvnode)) 1036 return -ECHILD; 1037 1038 /* We only need to invalidate a dentry if the server's copy changed 1039 * behind our back. If we made the change, it's no problem. Note that 1040 * on a 32-bit system, we only have 32 bits in the dentry to store the 1041 * version. 1042 */ 1043 dir_version = (long)READ_ONCE(dvnode->status.data_version); 1044 de_version = (long)READ_ONCE(dentry->d_fsdata); 1045 if (de_version != dir_version) { 1046 dir_version = (long)READ_ONCE(dvnode->invalid_before); 1047 if (de_version - dir_version < 0) 1048 return -ECHILD; 1049 } 1050 1051 return 1; /* Still valid */ 1052 } 1053 1054 /* 1055 * check that a dentry lookup hit has found a valid entry 1056 * - NOTE! the hit can be a negative hit too, so we can't assume we have an 1057 * inode 1058 */ 1059 static int afs_d_revalidate(struct inode *parent_dir, const struct qstr *name, 1060 struct dentry *dentry, unsigned int flags) 1061 { 1062 struct afs_vnode *vnode, *dir = AFS_FS_I(parent_dir); 1063 struct afs_fid fid; 1064 struct inode *inode; 1065 struct key *key; 1066 afs_dataversion_t dir_version, invalid_before; 1067 long de_version; 1068 int ret; 1069 1070 if (flags & LOOKUP_RCU) 1071 return afs_d_revalidate_rcu(dir, dentry); 1072 1073 if (d_really_is_positive(dentry)) { 1074 vnode = AFS_FS_I(d_inode(dentry)); 1075 _enter("{v={%llx:%llu} n=%pd fl=%lx},", 1076 vnode->fid.vid, vnode->fid.vnode, dentry, 1077 vnode->flags); 1078 } else { 1079 _enter("{neg n=%pd}", dentry); 1080 } 1081 1082 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell); 1083 if (IS_ERR(key)) 1084 key = NULL; 1085 1086 /* validate the parent directory */ 1087 ret = afs_validate(dir, key); 1088 if (ret == -ERESTARTSYS) { 1089 key_put(key); 1090 return ret; 1091 } 1092 1093 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) { 1094 _debug("%pd: parent dir deleted", dentry); 1095 goto not_found; 1096 } 1097 1098 /* We only need to invalidate a dentry if the server's copy changed 1099 * behind our back. If we made the change, it's no problem. Note that 1100 * on a 32-bit system, we only have 32 bits in the dentry to store the 1101 * version. 1102 */ 1103 dir_version = dir->status.data_version; 1104 de_version = (long)dentry->d_fsdata; 1105 if (de_version == (long)dir_version) 1106 goto out_valid_noupdate; 1107 1108 invalid_before = dir->invalid_before; 1109 if (de_version - (long)invalid_before >= 0) 1110 goto out_valid; 1111 1112 _debug("dir modified"); 1113 afs_stat_v(dir, n_reval); 1114 1115 /* search the directory for this vnode */ 1116 ret = afs_do_lookup_one(&dir->netfs.inode, name, &fid, &dir_version); 1117 switch (ret) { 1118 case 0: 1119 /* the filename maps to something */ 1120 if (d_really_is_negative(dentry)) 1121 goto not_found; 1122 inode = d_inode(dentry); 1123 if (is_bad_inode(inode)) { 1124 printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n", 1125 dentry); 1126 goto not_found; 1127 } 1128 1129 vnode = AFS_FS_I(inode); 1130 1131 /* if the vnode ID has changed, then the dirent points to a 1132 * different file */ 1133 if (fid.vnode != vnode->fid.vnode) { 1134 _debug("%pd: dirent changed [%llu != %llu]", 1135 dentry, fid.vnode, 1136 vnode->fid.vnode); 1137 goto not_found; 1138 } 1139 1140 /* if the vnode ID uniqifier has changed, then the file has 1141 * been deleted and replaced, and the original vnode ID has 1142 * been reused */ 1143 if (fid.unique != vnode->fid.unique) { 1144 _debug("%pd: file deleted (uq %u -> %u I:%u)", 1145 dentry, fid.unique, 1146 vnode->fid.unique, 1147 vnode->netfs.inode.i_generation); 1148 goto not_found; 1149 } 1150 goto out_valid; 1151 1152 case -ENOENT: 1153 /* the filename is unknown */ 1154 _debug("%pd: dirent not found", dentry); 1155 if (d_really_is_positive(dentry)) 1156 goto not_found; 1157 goto out_valid; 1158 1159 default: 1160 _debug("failed to iterate parent %pd2: %d", dentry, ret); 1161 goto not_found; 1162 } 1163 1164 out_valid: 1165 dentry->d_fsdata = (void *)(unsigned long)dir_version; 1166 out_valid_noupdate: 1167 key_put(key); 1168 _leave(" = 1 [valid]"); 1169 return 1; 1170 1171 not_found: 1172 _debug("dropping dentry %pd2", dentry); 1173 key_put(key); 1174 1175 _leave(" = 0 [bad]"); 1176 return 0; 1177 } 1178 1179 /* 1180 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't 1181 * sleep) 1182 * - called from dput() when d_count is going to 0. 1183 * - return 1 to request dentry be unhashed, 0 otherwise 1184 */ 1185 static int afs_d_delete(const struct dentry *dentry) 1186 { 1187 _enter("%pd", dentry); 1188 1189 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1190 goto zap; 1191 1192 if (d_really_is_positive(dentry) && 1193 (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) || 1194 test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags))) 1195 goto zap; 1196 1197 _leave(" = 0 [keep]"); 1198 return 0; 1199 1200 zap: 1201 _leave(" = 1 [zap]"); 1202 return 1; 1203 } 1204 1205 /* 1206 * Clean up sillyrename files on dentry removal. 1207 */ 1208 static void afs_d_iput(struct dentry *dentry, struct inode *inode) 1209 { 1210 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 1211 afs_silly_iput(dentry, inode); 1212 iput(inode); 1213 } 1214 1215 /* 1216 * handle dentry release 1217 */ 1218 void afs_d_release(struct dentry *dentry) 1219 { 1220 _enter("%pd", dentry); 1221 } 1222 1223 void afs_check_for_remote_deletion(struct afs_operation *op) 1224 { 1225 struct afs_vnode *vnode = op->file[0].vnode; 1226 1227 switch (afs_op_abort_code(op)) { 1228 case VNOVNODE: 1229 set_bit(AFS_VNODE_DELETED, &vnode->flags); 1230 clear_nlink(&vnode->netfs.inode); 1231 afs_break_callback(vnode, afs_cb_break_for_deleted); 1232 } 1233 } 1234 1235 /* 1236 * Create a new inode for create/mkdir/symlink 1237 */ 1238 static void afs_vnode_new_inode(struct afs_operation *op) 1239 { 1240 struct afs_vnode_param *dvp = &op->file[0]; 1241 struct afs_vnode_param *vp = &op->file[1]; 1242 struct afs_vnode *vnode; 1243 struct inode *inode; 1244 1245 _enter(""); 1246 1247 ASSERTCMP(afs_op_error(op), ==, 0); 1248 1249 inode = afs_iget(op, vp); 1250 if (IS_ERR(inode)) { 1251 /* ENOMEM or EINTR at a really inconvenient time - just abandon 1252 * the new directory on the server. 1253 */ 1254 afs_op_accumulate_error(op, PTR_ERR(inode), 0); 1255 return; 1256 } 1257 1258 vnode = AFS_FS_I(inode); 1259 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); 1260 if (S_ISDIR(inode->i_mode)) 1261 afs_mkdir_init_dir(vnode, dvp->vnode); 1262 else if (S_ISLNK(inode->i_mode)) 1263 afs_init_new_symlink(vnode, op); 1264 if (!afs_op_error(op)) 1265 afs_cache_permit(vnode, op->key, vnode->cb_break, &vp->scb); 1266 d_instantiate(op->dentry, inode); 1267 } 1268 1269 static void afs_create_success(struct afs_operation *op) 1270 { 1271 _enter("op=%08x", op->debug_id); 1272 op->ctime = op->file[0].scb.status.mtime_client; 1273 afs_vnode_commit_status(op, &op->file[0]); 1274 afs_update_dentry_version(op, &op->file[0], op->dentry); 1275 afs_vnode_new_inode(op); 1276 } 1277 1278 static void afs_create_edit_dir(struct afs_operation *op) 1279 { 1280 struct netfs_cache_resources cres = {}; 1281 struct afs_vnode_param *dvp = &op->file[0]; 1282 struct afs_vnode_param *vp = &op->file[1]; 1283 struct afs_vnode *dvnode = dvp->vnode; 1284 1285 _enter("op=%08x", op->debug_id); 1286 1287 fscache_begin_write_operation(&cres, afs_vnode_cache(dvnode)); 1288 down_write(&dvnode->validate_lock); 1289 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) && 1290 dvnode->status.data_version == dvp->dv_before + dvp->dv_delta) 1291 afs_edit_dir_add(dvnode, &op->dentry->d_name, &vp->fid, 1292 op->create.reason); 1293 up_write(&dvnode->validate_lock); 1294 fscache_end_operation(&cres); 1295 } 1296 1297 static void afs_create_put(struct afs_operation *op) 1298 { 1299 _enter("op=%08x", op->debug_id); 1300 1301 if (afs_op_error(op)) 1302 d_drop(op->dentry); 1303 } 1304 1305 static const struct afs_operation_ops afs_mkdir_operation = { 1306 .issue_afs_rpc = afs_fs_make_dir, 1307 .issue_yfs_rpc = yfs_fs_make_dir, 1308 .success = afs_create_success, 1309 .aborted = afs_check_for_remote_deletion, 1310 .edit_dir = afs_create_edit_dir, 1311 .put = afs_create_put, 1312 }; 1313 1314 /* 1315 * create a directory on an AFS filesystem 1316 */ 1317 static struct dentry *afs_mkdir(struct mnt_idmap *idmap, struct inode *dir, 1318 struct dentry *dentry, umode_t mode) 1319 { 1320 struct afs_operation *op; 1321 struct afs_vnode *dvnode = AFS_FS_I(dir); 1322 int ret; 1323 1324 _enter("{%llx:%llu},{%pd},%ho", 1325 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode); 1326 1327 op = afs_alloc_operation(NULL, dvnode->volume); 1328 if (IS_ERR(op)) { 1329 d_drop(dentry); 1330 return ERR_CAST(op); 1331 } 1332 1333 fscache_use_cookie(afs_vnode_cache(dvnode), true); 1334 1335 afs_op_set_vnode(op, 0, dvnode); 1336 op->file[0].dv_delta = 1; 1337 op->file[0].modification = true; 1338 op->file[0].update_ctime = true; 1339 op->dentry = dentry; 1340 op->create.mode = S_IFDIR | mode; 1341 op->create.reason = afs_edit_dir_for_mkdir; 1342 op->mtime = current_time(dir); 1343 op->ops = &afs_mkdir_operation; 1344 ret = afs_do_sync_operation(op); 1345 afs_dir_unuse_cookie(dvnode, ret); 1346 return ERR_PTR(ret); 1347 } 1348 1349 /* 1350 * Remove a subdir from a directory. 1351 */ 1352 static void afs_dir_remove_subdir(struct dentry *dentry) 1353 { 1354 if (d_really_is_positive(dentry)) { 1355 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); 1356 1357 clear_nlink(&vnode->netfs.inode); 1358 set_bit(AFS_VNODE_DELETED, &vnode->flags); 1359 afs_clear_cb_promise(vnode, afs_cb_promise_clear_rmdir); 1360 afs_invalidate_dir(vnode, afs_dir_invalid_subdir_removed); 1361 } 1362 } 1363 1364 static void afs_rmdir_success(struct afs_operation *op) 1365 { 1366 _enter("op=%08x", op->debug_id); 1367 op->ctime = op->file[0].scb.status.mtime_client; 1368 afs_vnode_commit_status(op, &op->file[0]); 1369 afs_update_dentry_version(op, &op->file[0], op->dentry); 1370 } 1371 1372 static void afs_rmdir_edit_dir(struct afs_operation *op) 1373 { 1374 struct netfs_cache_resources cres = {}; 1375 struct afs_vnode_param *dvp = &op->file[0]; 1376 struct afs_vnode *dvnode = dvp->vnode; 1377 1378 _enter("op=%08x", op->debug_id); 1379 afs_dir_remove_subdir(op->dentry); 1380 1381 fscache_begin_write_operation(&cres, afs_vnode_cache(dvnode)); 1382 down_write(&dvnode->validate_lock); 1383 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) && 1384 dvnode->status.data_version == dvp->dv_before + dvp->dv_delta) 1385 afs_edit_dir_remove(dvnode, &op->dentry->d_name, 1386 afs_edit_dir_for_rmdir); 1387 up_write(&dvnode->validate_lock); 1388 fscache_end_operation(&cres); 1389 } 1390 1391 static void afs_rmdir_put(struct afs_operation *op) 1392 { 1393 _enter("op=%08x", op->debug_id); 1394 if (op->file[1].vnode) 1395 up_write(&op->file[1].vnode->rmdir_lock); 1396 } 1397 1398 static const struct afs_operation_ops afs_rmdir_operation = { 1399 .issue_afs_rpc = afs_fs_remove_dir, 1400 .issue_yfs_rpc = yfs_fs_remove_dir, 1401 .success = afs_rmdir_success, 1402 .aborted = afs_check_for_remote_deletion, 1403 .edit_dir = afs_rmdir_edit_dir, 1404 .put = afs_rmdir_put, 1405 }; 1406 1407 /* 1408 * remove a directory from an AFS filesystem 1409 */ 1410 static int afs_rmdir(struct inode *dir, struct dentry *dentry) 1411 { 1412 struct afs_operation *op; 1413 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL; 1414 int ret; 1415 1416 _enter("{%llx:%llu},{%pd}", 1417 dvnode->fid.vid, dvnode->fid.vnode, dentry); 1418 1419 op = afs_alloc_operation(NULL, dvnode->volume); 1420 if (IS_ERR(op)) 1421 return PTR_ERR(op); 1422 1423 fscache_use_cookie(afs_vnode_cache(dvnode), true); 1424 1425 afs_op_set_vnode(op, 0, dvnode); 1426 op->file[0].dv_delta = 1; 1427 op->file[0].modification = true; 1428 op->file[0].update_ctime = true; 1429 1430 op->dentry = dentry; 1431 op->ops = &afs_rmdir_operation; 1432 1433 /* Try to make sure we have a callback promise on the victim. */ 1434 if (d_really_is_positive(dentry)) { 1435 vnode = AFS_FS_I(d_inode(dentry)); 1436 ret = afs_validate(vnode, op->key); 1437 if (ret < 0) 1438 goto error; 1439 } 1440 1441 if (vnode) { 1442 ret = down_write_killable(&vnode->rmdir_lock); 1443 if (ret < 0) 1444 goto error; 1445 op->file[1].vnode = vnode; 1446 } 1447 1448 ret = afs_do_sync_operation(op); 1449 1450 /* Not all systems that can host afs servers have ENOTEMPTY. */ 1451 if (ret == -EEXIST) 1452 ret = -ENOTEMPTY; 1453 out: 1454 afs_dir_unuse_cookie(dvnode, ret); 1455 return ret; 1456 1457 error: 1458 ret = afs_put_operation(op); 1459 goto out; 1460 } 1461 1462 /* 1463 * Remove a link to a file or symlink from a directory. 1464 * 1465 * If the file was not deleted due to excess hard links, the fileserver will 1466 * break the callback promise on the file - if it had one - before it returns 1467 * to us, and if it was deleted, it won't 1468 * 1469 * However, if we didn't have a callback promise outstanding, or it was 1470 * outstanding on a different server, then it won't break it either... 1471 */ 1472 static void afs_dir_remove_link(struct afs_operation *op) 1473 { 1474 struct afs_vnode *dvnode = op->file[0].vnode; 1475 struct afs_vnode *vnode = op->file[1].vnode; 1476 struct dentry *dentry = op->dentry; 1477 int ret; 1478 1479 if (afs_op_error(op) || 1480 (op->file[1].scb.have_status && op->file[1].scb.have_error)) 1481 return; 1482 if (d_really_is_positive(dentry)) 1483 return; 1484 1485 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) { 1486 /* Already done */ 1487 } else if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) { 1488 write_seqlock(&vnode->cb_lock); 1489 drop_nlink(&vnode->netfs.inode); 1490 if (vnode->netfs.inode.i_nlink == 0) { 1491 set_bit(AFS_VNODE_DELETED, &vnode->flags); 1492 __afs_break_callback(vnode, afs_cb_break_for_unlink); 1493 } 1494 write_sequnlock(&vnode->cb_lock); 1495 } else { 1496 afs_break_callback(vnode, afs_cb_break_for_unlink); 1497 1498 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) 1499 _debug("AFS_VNODE_DELETED"); 1500 1501 ret = afs_validate(vnode, op->key); 1502 if (ret != -ESTALE) 1503 afs_op_set_error(op, ret); 1504 } 1505 1506 _debug("nlink %d [val %d]", vnode->netfs.inode.i_nlink, afs_op_error(op)); 1507 } 1508 1509 static void afs_unlink_success(struct afs_operation *op) 1510 { 1511 _enter("op=%08x", op->debug_id); 1512 op->ctime = op->file[0].scb.status.mtime_client; 1513 afs_check_dir_conflict(op, &op->file[0]); 1514 afs_vnode_commit_status(op, &op->file[0]); 1515 afs_vnode_commit_status(op, &op->file[1]); 1516 afs_update_dentry_version(op, &op->file[0], op->dentry); 1517 afs_dir_remove_link(op); 1518 } 1519 1520 static void afs_unlink_edit_dir(struct afs_operation *op) 1521 { 1522 struct netfs_cache_resources cres = {}; 1523 struct afs_vnode_param *dvp = &op->file[0]; 1524 struct afs_vnode *dvnode = dvp->vnode; 1525 1526 _enter("op=%08x", op->debug_id); 1527 fscache_begin_write_operation(&cres, afs_vnode_cache(dvnode)); 1528 down_write(&dvnode->validate_lock); 1529 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) && 1530 dvnode->status.data_version == dvp->dv_before + dvp->dv_delta) 1531 afs_edit_dir_remove(dvnode, &op->dentry->d_name, 1532 afs_edit_dir_for_unlink); 1533 up_write(&dvnode->validate_lock); 1534 fscache_end_operation(&cres); 1535 } 1536 1537 static void afs_unlink_put(struct afs_operation *op) 1538 { 1539 _enter("op=%08x", op->debug_id); 1540 if (op->unlink.need_rehash && afs_op_error(op) < 0 && afs_op_error(op) != -ENOENT) 1541 d_rehash(op->dentry); 1542 } 1543 1544 static const struct afs_operation_ops afs_unlink_operation = { 1545 .issue_afs_rpc = afs_fs_remove_file, 1546 .issue_yfs_rpc = yfs_fs_remove_file, 1547 .success = afs_unlink_success, 1548 .aborted = afs_check_for_remote_deletion, 1549 .edit_dir = afs_unlink_edit_dir, 1550 .put = afs_unlink_put, 1551 }; 1552 1553 /* 1554 * Remove a file or symlink from an AFS filesystem. 1555 */ 1556 static int afs_unlink(struct inode *dir, struct dentry *dentry) 1557 { 1558 struct afs_operation *op; 1559 struct afs_vnode *dvnode = AFS_FS_I(dir); 1560 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); 1561 int ret; 1562 1563 _enter("{%llx:%llu},{%pd}", 1564 dvnode->fid.vid, dvnode->fid.vnode, dentry); 1565 1566 if (dentry->d_name.len >= AFSNAMEMAX) 1567 return -ENAMETOOLONG; 1568 1569 op = afs_alloc_operation(NULL, dvnode->volume); 1570 if (IS_ERR(op)) 1571 return PTR_ERR(op); 1572 1573 fscache_use_cookie(afs_vnode_cache(dvnode), true); 1574 1575 afs_op_set_vnode(op, 0, dvnode); 1576 op->file[0].dv_delta = 1; 1577 op->file[0].modification = true; 1578 op->file[0].update_ctime = true; 1579 1580 /* Try to make sure we have a callback promise on the victim. */ 1581 ret = afs_validate(vnode, op->key); 1582 if (ret < 0) { 1583 afs_op_set_error(op, ret); 1584 goto error; 1585 } 1586 1587 spin_lock(&dentry->d_lock); 1588 if (d_count(dentry) > 1) { 1589 spin_unlock(&dentry->d_lock); 1590 /* Start asynchronous writeout of the inode */ 1591 write_inode_now(d_inode(dentry), 0); 1592 afs_op_set_error(op, afs_sillyrename(dvnode, vnode, dentry, op->key)); 1593 goto error; 1594 } 1595 if (!d_unhashed(dentry)) { 1596 /* Prevent a race with RCU lookup. */ 1597 __d_drop(dentry); 1598 op->unlink.need_rehash = true; 1599 } 1600 spin_unlock(&dentry->d_lock); 1601 1602 op->file[1].vnode = vnode; 1603 op->file[1].update_ctime = true; 1604 op->file[1].op_unlinked = true; 1605 op->dentry = dentry; 1606 op->ops = &afs_unlink_operation; 1607 afs_begin_vnode_operation(op); 1608 afs_wait_for_operation(op); 1609 1610 /* If there was a conflict with a third party, check the status of the 1611 * unlinked vnode. 1612 */ 1613 if (afs_op_error(op) == 0 && (op->flags & AFS_OPERATION_DIR_CONFLICT)) { 1614 op->file[1].update_ctime = false; 1615 op->fetch_status.which = 1; 1616 op->ops = &afs_fetch_status_operation; 1617 afs_begin_vnode_operation(op); 1618 afs_wait_for_operation(op); 1619 } 1620 1621 error: 1622 ret = afs_put_operation(op); 1623 afs_dir_unuse_cookie(dvnode, ret); 1624 return ret; 1625 } 1626 1627 static const struct afs_operation_ops afs_create_operation = { 1628 .issue_afs_rpc = afs_fs_create_file, 1629 .issue_yfs_rpc = yfs_fs_create_file, 1630 .success = afs_create_success, 1631 .aborted = afs_check_for_remote_deletion, 1632 .edit_dir = afs_create_edit_dir, 1633 .put = afs_create_put, 1634 }; 1635 1636 /* 1637 * create a regular file on an AFS filesystem 1638 */ 1639 static int afs_create(struct mnt_idmap *idmap, struct inode *dir, 1640 struct dentry *dentry, umode_t mode, bool excl) 1641 { 1642 struct afs_operation *op; 1643 struct afs_vnode *dvnode = AFS_FS_I(dir); 1644 int ret = -ENAMETOOLONG; 1645 1646 _enter("{%llx:%llu},{%pd},%ho", 1647 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode); 1648 1649 if (dentry->d_name.len >= AFSNAMEMAX) 1650 goto error; 1651 1652 op = afs_alloc_operation(NULL, dvnode->volume); 1653 if (IS_ERR(op)) { 1654 ret = PTR_ERR(op); 1655 goto error; 1656 } 1657 1658 fscache_use_cookie(afs_vnode_cache(dvnode), true); 1659 1660 afs_op_set_vnode(op, 0, dvnode); 1661 op->file[0].dv_delta = 1; 1662 op->file[0].modification = true; 1663 op->file[0].update_ctime = true; 1664 1665 op->dentry = dentry; 1666 op->create.mode = S_IFREG | mode; 1667 op->create.reason = afs_edit_dir_for_create; 1668 op->mtime = current_time(dir); 1669 op->ops = &afs_create_operation; 1670 ret = afs_do_sync_operation(op); 1671 afs_dir_unuse_cookie(dvnode, ret); 1672 return ret; 1673 1674 error: 1675 d_drop(dentry); 1676 _leave(" = %d", ret); 1677 return ret; 1678 } 1679 1680 static void afs_link_success(struct afs_operation *op) 1681 { 1682 struct afs_vnode_param *dvp = &op->file[0]; 1683 struct afs_vnode_param *vp = &op->file[1]; 1684 1685 _enter("op=%08x", op->debug_id); 1686 op->ctime = dvp->scb.status.mtime_client; 1687 afs_vnode_commit_status(op, dvp); 1688 afs_vnode_commit_status(op, vp); 1689 afs_update_dentry_version(op, dvp, op->dentry); 1690 if (op->dentry_2->d_parent == op->dentry->d_parent) 1691 afs_update_dentry_version(op, dvp, op->dentry_2); 1692 ihold(&vp->vnode->netfs.inode); 1693 d_instantiate(op->dentry, &vp->vnode->netfs.inode); 1694 } 1695 1696 static void afs_link_put(struct afs_operation *op) 1697 { 1698 _enter("op=%08x", op->debug_id); 1699 if (afs_op_error(op)) 1700 d_drop(op->dentry); 1701 } 1702 1703 static const struct afs_operation_ops afs_link_operation = { 1704 .issue_afs_rpc = afs_fs_link, 1705 .issue_yfs_rpc = yfs_fs_link, 1706 .success = afs_link_success, 1707 .aborted = afs_check_for_remote_deletion, 1708 .edit_dir = afs_create_edit_dir, 1709 .put = afs_link_put, 1710 }; 1711 1712 /* 1713 * create a hard link between files in an AFS filesystem 1714 */ 1715 static int afs_link(struct dentry *from, struct inode *dir, 1716 struct dentry *dentry) 1717 { 1718 struct afs_operation *op; 1719 struct afs_vnode *dvnode = AFS_FS_I(dir); 1720 struct afs_vnode *vnode = AFS_FS_I(d_inode(from)); 1721 int ret = -ENAMETOOLONG; 1722 1723 _enter("{%llx:%llu},{%llx:%llu},{%pd}", 1724 vnode->fid.vid, vnode->fid.vnode, 1725 dvnode->fid.vid, dvnode->fid.vnode, 1726 dentry); 1727 1728 if (dentry->d_name.len >= AFSNAMEMAX) 1729 goto error; 1730 1731 op = afs_alloc_operation(NULL, dvnode->volume); 1732 if (IS_ERR(op)) { 1733 ret = PTR_ERR(op); 1734 goto error; 1735 } 1736 1737 fscache_use_cookie(afs_vnode_cache(dvnode), true); 1738 1739 ret = afs_validate(vnode, op->key); 1740 if (ret < 0) 1741 goto error_op; 1742 1743 afs_op_set_vnode(op, 0, dvnode); 1744 afs_op_set_vnode(op, 1, vnode); 1745 op->file[0].dv_delta = 1; 1746 op->file[0].modification = true; 1747 op->file[0].update_ctime = true; 1748 op->file[1].update_ctime = true; 1749 1750 op->dentry = dentry; 1751 op->dentry_2 = from; 1752 op->ops = &afs_link_operation; 1753 op->create.reason = afs_edit_dir_for_link; 1754 ret = afs_do_sync_operation(op); 1755 afs_dir_unuse_cookie(dvnode, ret); 1756 return ret; 1757 1758 error_op: 1759 afs_put_operation(op); 1760 afs_dir_unuse_cookie(dvnode, ret); 1761 error: 1762 d_drop(dentry); 1763 _leave(" = %d", ret); 1764 return ret; 1765 } 1766 1767 static const struct afs_operation_ops afs_symlink_operation = { 1768 .issue_afs_rpc = afs_fs_symlink, 1769 .issue_yfs_rpc = yfs_fs_symlink, 1770 .success = afs_create_success, 1771 .aborted = afs_check_for_remote_deletion, 1772 .edit_dir = afs_create_edit_dir, 1773 .put = afs_create_put, 1774 }; 1775 1776 /* 1777 * create a symlink in an AFS filesystem 1778 */ 1779 static int afs_symlink(struct mnt_idmap *idmap, struct inode *dir, 1780 struct dentry *dentry, const char *content) 1781 { 1782 struct afs_operation *op; 1783 struct afs_vnode *dvnode = AFS_FS_I(dir); 1784 int ret; 1785 1786 _enter("{%llx:%llu},{%pd},%s", 1787 dvnode->fid.vid, dvnode->fid.vnode, dentry, 1788 content); 1789 1790 ret = -ENAMETOOLONG; 1791 if (dentry->d_name.len >= AFSNAMEMAX) 1792 goto error; 1793 1794 ret = -EINVAL; 1795 if (strlen(content) >= AFSPATHMAX) 1796 goto error; 1797 1798 op = afs_alloc_operation(NULL, dvnode->volume); 1799 if (IS_ERR(op)) { 1800 ret = PTR_ERR(op); 1801 goto error; 1802 } 1803 1804 fscache_use_cookie(afs_vnode_cache(dvnode), true); 1805 1806 afs_op_set_vnode(op, 0, dvnode); 1807 op->file[0].dv_delta = 1; 1808 1809 op->dentry = dentry; 1810 op->ops = &afs_symlink_operation; 1811 op->create.reason = afs_edit_dir_for_symlink; 1812 op->create.symlink = content; 1813 op->mtime = current_time(dir); 1814 ret = afs_do_sync_operation(op); 1815 afs_dir_unuse_cookie(dvnode, ret); 1816 return ret; 1817 1818 error: 1819 d_drop(dentry); 1820 _leave(" = %d", ret); 1821 return ret; 1822 } 1823 1824 static void afs_rename_success(struct afs_operation *op) 1825 { 1826 struct afs_vnode *vnode = AFS_FS_I(d_inode(op->dentry)); 1827 1828 _enter("op=%08x", op->debug_id); 1829 1830 op->ctime = op->file[0].scb.status.mtime_client; 1831 afs_check_dir_conflict(op, &op->file[1]); 1832 afs_vnode_commit_status(op, &op->file[0]); 1833 if (op->file[1].vnode != op->file[0].vnode) { 1834 op->ctime = op->file[1].scb.status.mtime_client; 1835 afs_vnode_commit_status(op, &op->file[1]); 1836 } 1837 1838 /* If we're moving a subdir between dirs, we need to update 1839 * its DV counter too as the ".." will be altered. 1840 */ 1841 if (S_ISDIR(vnode->netfs.inode.i_mode) && 1842 op->file[0].vnode != op->file[1].vnode) { 1843 u64 new_dv; 1844 1845 write_seqlock(&vnode->cb_lock); 1846 1847 new_dv = vnode->status.data_version + 1; 1848 trace_afs_set_dv(vnode, new_dv); 1849 vnode->status.data_version = new_dv; 1850 inode_set_iversion_raw(&vnode->netfs.inode, new_dv); 1851 1852 write_sequnlock(&vnode->cb_lock); 1853 } 1854 } 1855 1856 static void afs_rename_edit_dir(struct afs_operation *op) 1857 { 1858 struct netfs_cache_resources orig_cres = {}, new_cres = {}; 1859 struct afs_vnode_param *orig_dvp = &op->file[0]; 1860 struct afs_vnode_param *new_dvp = &op->file[1]; 1861 struct afs_vnode *orig_dvnode = orig_dvp->vnode; 1862 struct afs_vnode *new_dvnode = new_dvp->vnode; 1863 struct afs_vnode *vnode = AFS_FS_I(d_inode(op->dentry)); 1864 struct dentry *old_dentry = op->dentry; 1865 struct dentry *new_dentry = op->dentry_2; 1866 struct inode *new_inode; 1867 1868 _enter("op=%08x", op->debug_id); 1869 1870 if (op->rename.rehash) { 1871 d_rehash(op->rename.rehash); 1872 op->rename.rehash = NULL; 1873 } 1874 1875 fscache_begin_write_operation(&orig_cres, afs_vnode_cache(orig_dvnode)); 1876 if (new_dvnode != orig_dvnode) 1877 fscache_begin_write_operation(&new_cres, afs_vnode_cache(new_dvnode)); 1878 1879 down_write(&orig_dvnode->validate_lock); 1880 if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags) && 1881 orig_dvnode->status.data_version == orig_dvp->dv_before + orig_dvp->dv_delta) 1882 afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name, 1883 afs_edit_dir_for_rename_0); 1884 1885 if (new_dvnode != orig_dvnode) { 1886 up_write(&orig_dvnode->validate_lock); 1887 down_write(&new_dvnode->validate_lock); 1888 } 1889 1890 if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags) && 1891 new_dvnode->status.data_version == new_dvp->dv_before + new_dvp->dv_delta) { 1892 if (!op->rename.new_negative) 1893 afs_edit_dir_remove(new_dvnode, &new_dentry->d_name, 1894 afs_edit_dir_for_rename_1); 1895 1896 afs_edit_dir_add(new_dvnode, &new_dentry->d_name, 1897 &vnode->fid, afs_edit_dir_for_rename_2); 1898 } 1899 1900 if (S_ISDIR(vnode->netfs.inode.i_mode) && 1901 new_dvnode != orig_dvnode && 1902 test_bit(AFS_VNODE_DIR_VALID, &vnode->flags)) 1903 afs_edit_dir_update_dotdot(vnode, new_dvnode, 1904 afs_edit_dir_for_rename_sub); 1905 1906 new_inode = d_inode(new_dentry); 1907 if (new_inode) { 1908 spin_lock(&new_inode->i_lock); 1909 if (S_ISDIR(new_inode->i_mode)) 1910 clear_nlink(new_inode); 1911 else if (new_inode->i_nlink > 0) 1912 drop_nlink(new_inode); 1913 spin_unlock(&new_inode->i_lock); 1914 } 1915 1916 /* Now we can update d_fsdata on the dentries to reflect their 1917 * new parent's data_version. 1918 * 1919 * Note that if we ever implement RENAME_EXCHANGE, we'll have 1920 * to update both dentries with opposing dir versions. 1921 */ 1922 afs_update_dentry_version(op, new_dvp, op->dentry); 1923 afs_update_dentry_version(op, new_dvp, op->dentry_2); 1924 1925 d_move(old_dentry, new_dentry); 1926 1927 up_write(&new_dvnode->validate_lock); 1928 fscache_end_operation(&orig_cres); 1929 if (new_dvnode != orig_dvnode) 1930 fscache_end_operation(&new_cres); 1931 } 1932 1933 static void afs_rename_put(struct afs_operation *op) 1934 { 1935 _enter("op=%08x", op->debug_id); 1936 if (op->rename.rehash) 1937 d_rehash(op->rename.rehash); 1938 dput(op->rename.tmp); 1939 if (afs_op_error(op)) 1940 d_rehash(op->dentry); 1941 } 1942 1943 static const struct afs_operation_ops afs_rename_operation = { 1944 .issue_afs_rpc = afs_fs_rename, 1945 .issue_yfs_rpc = yfs_fs_rename, 1946 .success = afs_rename_success, 1947 .edit_dir = afs_rename_edit_dir, 1948 .put = afs_rename_put, 1949 }; 1950 1951 /* 1952 * rename a file in an AFS filesystem and/or move it between directories 1953 */ 1954 static int afs_rename(struct mnt_idmap *idmap, struct inode *old_dir, 1955 struct dentry *old_dentry, struct inode *new_dir, 1956 struct dentry *new_dentry, unsigned int flags) 1957 { 1958 struct afs_operation *op; 1959 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode; 1960 int ret; 1961 1962 if (flags) 1963 return -EINVAL; 1964 1965 /* Don't allow silly-rename files be moved around. */ 1966 if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED) 1967 return -EINVAL; 1968 1969 vnode = AFS_FS_I(d_inode(old_dentry)); 1970 orig_dvnode = AFS_FS_I(old_dir); 1971 new_dvnode = AFS_FS_I(new_dir); 1972 1973 _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}", 1974 orig_dvnode->fid.vid, orig_dvnode->fid.vnode, 1975 vnode->fid.vid, vnode->fid.vnode, 1976 new_dvnode->fid.vid, new_dvnode->fid.vnode, 1977 new_dentry); 1978 1979 op = afs_alloc_operation(NULL, orig_dvnode->volume); 1980 if (IS_ERR(op)) 1981 return PTR_ERR(op); 1982 1983 fscache_use_cookie(afs_vnode_cache(orig_dvnode), true); 1984 if (new_dvnode != orig_dvnode) 1985 fscache_use_cookie(afs_vnode_cache(new_dvnode), true); 1986 1987 ret = afs_validate(vnode, op->key); 1988 afs_op_set_error(op, ret); 1989 if (ret < 0) 1990 goto error; 1991 1992 afs_op_set_vnode(op, 0, orig_dvnode); 1993 afs_op_set_vnode(op, 1, new_dvnode); /* May be same as orig_dvnode */ 1994 op->file[0].dv_delta = 1; 1995 op->file[1].dv_delta = 1; 1996 op->file[0].modification = true; 1997 op->file[1].modification = true; 1998 op->file[0].update_ctime = true; 1999 op->file[1].update_ctime = true; 2000 2001 op->dentry = old_dentry; 2002 op->dentry_2 = new_dentry; 2003 op->rename.new_negative = d_is_negative(new_dentry); 2004 op->ops = &afs_rename_operation; 2005 2006 /* For non-directories, check whether the target is busy and if so, 2007 * make a copy of the dentry and then do a silly-rename. If the 2008 * silly-rename succeeds, the copied dentry is hashed and becomes the 2009 * new target. 2010 */ 2011 if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) { 2012 /* To prevent any new references to the target during the 2013 * rename, we unhash the dentry in advance. 2014 */ 2015 if (!d_unhashed(new_dentry)) { 2016 d_drop(new_dentry); 2017 op->rename.rehash = new_dentry; 2018 } 2019 2020 if (d_count(new_dentry) > 2) { 2021 /* copy the target dentry's name */ 2022 op->rename.tmp = d_alloc(new_dentry->d_parent, 2023 &new_dentry->d_name); 2024 if (!op->rename.tmp) { 2025 afs_op_nomem(op); 2026 goto error; 2027 } 2028 2029 ret = afs_sillyrename(new_dvnode, 2030 AFS_FS_I(d_inode(new_dentry)), 2031 new_dentry, op->key); 2032 if (ret) { 2033 afs_op_set_error(op, ret); 2034 goto error; 2035 } 2036 2037 op->dentry_2 = op->rename.tmp; 2038 op->rename.rehash = NULL; 2039 op->rename.new_negative = true; 2040 } 2041 } 2042 2043 /* This bit is potentially nasty as there's a potential race with 2044 * afs_d_revalidate{,_rcu}(). We have to change d_fsdata on the dentry 2045 * to reflect it's new parent's new data_version after the op, but 2046 * d_revalidate may see old_dentry between the op having taken place 2047 * and the version being updated. 2048 * 2049 * So drop the old_dentry for now to make other threads go through 2050 * lookup instead - which we hold a lock against. 2051 */ 2052 d_drop(old_dentry); 2053 2054 ret = afs_do_sync_operation(op); 2055 out: 2056 afs_dir_unuse_cookie(orig_dvnode, ret); 2057 if (new_dvnode != orig_dvnode) 2058 afs_dir_unuse_cookie(new_dvnode, ret); 2059 return ret; 2060 2061 error: 2062 ret = afs_put_operation(op); 2063 goto out; 2064 } 2065 2066 /* 2067 * Write the file contents to the cache as a single blob. 2068 */ 2069 int afs_single_writepages(struct address_space *mapping, 2070 struct writeback_control *wbc) 2071 { 2072 struct afs_vnode *dvnode = AFS_FS_I(mapping->host); 2073 struct iov_iter iter; 2074 bool is_dir = (S_ISDIR(dvnode->netfs.inode.i_mode) && 2075 !test_bit(AFS_VNODE_MOUNTPOINT, &dvnode->flags)); 2076 int ret = 0; 2077 2078 /* Need to lock to prevent the folio queue and folios from being thrown 2079 * away. 2080 */ 2081 down_read(&dvnode->validate_lock); 2082 2083 if (is_dir ? 2084 test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) : 2085 atomic64_read(&dvnode->cb_expires_at) != AFS_NO_CB_PROMISE) { 2086 iov_iter_folio_queue(&iter, ITER_SOURCE, dvnode->directory, 0, 0, 2087 i_size_read(&dvnode->netfs.inode)); 2088 ret = netfs_writeback_single(mapping, wbc, &iter); 2089 } 2090 2091 up_read(&dvnode->validate_lock); 2092 return ret; 2093 } 2094