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