1 // SPDX-License-Identifier: GPL-2.0 2 3 #include "bcachefs.h" 4 #include "acl.h" 5 #include "btree_update.h" 6 #include "dirent.h" 7 #include "inode.h" 8 #include "namei.h" 9 #include "subvolume.h" 10 #include "xattr.h" 11 12 #include <linux/posix_acl.h> 13 14 static inline int is_subdir_for_nlink(struct bch_inode_unpacked *inode) 15 { 16 return S_ISDIR(inode->bi_mode) && !inode->bi_subvol; 17 } 18 19 int bch2_create_trans(struct btree_trans *trans, 20 subvol_inum dir, 21 struct bch_inode_unpacked *dir_u, 22 struct bch_inode_unpacked *new_inode, 23 const struct qstr *name, 24 uid_t uid, gid_t gid, umode_t mode, dev_t rdev, 25 struct posix_acl *default_acl, 26 struct posix_acl *acl, 27 subvol_inum snapshot_src, 28 unsigned flags) 29 { 30 struct bch_fs *c = trans->c; 31 struct btree_iter dir_iter = {}; 32 struct btree_iter inode_iter = {}; 33 subvol_inum new_inum = dir; 34 u64 now = bch2_current_time(c); 35 u64 cpu = raw_smp_processor_id(); 36 u64 dir_target; 37 u32 snapshot; 38 unsigned dir_type = mode_to_type(mode); 39 int ret; 40 41 ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &snapshot); 42 if (ret) 43 goto err; 44 45 ret = bch2_inode_peek(trans, &dir_iter, dir_u, dir, 46 BTREE_ITER_intent|BTREE_ITER_with_updates); 47 if (ret) 48 goto err; 49 50 /* Inherit casefold state from parent. */ 51 if (S_ISDIR(mode)) 52 new_inode->bi_flags |= dir_u->bi_flags & BCH_INODE_casefolded; 53 54 if (!(flags & BCH_CREATE_SNAPSHOT)) { 55 /* Normal create path - allocate a new inode: */ 56 bch2_inode_init_late(new_inode, now, uid, gid, mode, rdev, dir_u); 57 58 if (flags & BCH_CREATE_TMPFILE) 59 new_inode->bi_flags |= BCH_INODE_unlinked; 60 61 ret = bch2_inode_create(trans, &inode_iter, new_inode, snapshot, cpu); 62 if (ret) 63 goto err; 64 65 snapshot_src = (subvol_inum) { 0 }; 66 } else { 67 /* 68 * Creating a snapshot - we're not allocating a new inode, but 69 * we do have to lookup the root inode of the subvolume we're 70 * snapshotting and update it (in the new snapshot): 71 */ 72 73 if (!snapshot_src.inum) { 74 /* Inode wasn't specified, just snapshot: */ 75 struct bch_subvolume s; 76 ret = bch2_subvolume_get(trans, snapshot_src.subvol, true, &s); 77 if (ret) 78 goto err; 79 80 snapshot_src.inum = le64_to_cpu(s.inode); 81 } 82 83 ret = bch2_inode_peek(trans, &inode_iter, new_inode, snapshot_src, 84 BTREE_ITER_intent); 85 if (ret) 86 goto err; 87 88 if (new_inode->bi_subvol != snapshot_src.subvol) { 89 /* Not a subvolume root: */ 90 ret = -EINVAL; 91 goto err; 92 } 93 94 /* 95 * If we're not root, we have to own the subvolume being 96 * snapshotted: 97 */ 98 if (uid && new_inode->bi_uid != uid) { 99 ret = -EPERM; 100 goto err; 101 } 102 103 flags |= BCH_CREATE_SUBVOL; 104 } 105 106 new_inum.inum = new_inode->bi_inum; 107 dir_target = new_inode->bi_inum; 108 109 if (flags & BCH_CREATE_SUBVOL) { 110 u32 new_subvol, dir_snapshot; 111 112 ret = bch2_subvolume_create(trans, new_inode->bi_inum, 113 dir.subvol, 114 snapshot_src.subvol, 115 &new_subvol, &snapshot, 116 (flags & BCH_CREATE_SNAPSHOT_RO) != 0); 117 if (ret) 118 goto err; 119 120 new_inode->bi_parent_subvol = dir.subvol; 121 new_inode->bi_subvol = new_subvol; 122 new_inum.subvol = new_subvol; 123 dir_target = new_subvol; 124 dir_type = DT_SUBVOL; 125 126 ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &dir_snapshot); 127 if (ret) 128 goto err; 129 130 bch2_btree_iter_set_snapshot(trans, &dir_iter, dir_snapshot); 131 ret = bch2_btree_iter_traverse(trans, &dir_iter); 132 if (ret) 133 goto err; 134 } 135 136 if (!(flags & BCH_CREATE_SNAPSHOT)) { 137 if (default_acl) { 138 ret = bch2_set_acl_trans(trans, new_inum, new_inode, 139 default_acl, ACL_TYPE_DEFAULT); 140 if (ret) 141 goto err; 142 } 143 144 if (acl) { 145 ret = bch2_set_acl_trans(trans, new_inum, new_inode, 146 acl, ACL_TYPE_ACCESS); 147 if (ret) 148 goto err; 149 } 150 } 151 152 if (!(flags & BCH_CREATE_TMPFILE)) { 153 struct bch_hash_info dir_hash = bch2_hash_info_init(c, dir_u); 154 u64 dir_offset; 155 156 if (is_subdir_for_nlink(new_inode)) 157 dir_u->bi_nlink++; 158 dir_u->bi_mtime = dir_u->bi_ctime = now; 159 160 ret = bch2_dirent_create(trans, dir, &dir_hash, 161 dir_type, 162 name, 163 dir_target, 164 &dir_offset, 165 &dir_u->bi_size, 166 STR_HASH_must_create|BTREE_ITER_with_updates) ?: 167 bch2_inode_write(trans, &dir_iter, dir_u); 168 if (ret) 169 goto err; 170 171 new_inode->bi_dir = dir_u->bi_inum; 172 new_inode->bi_dir_offset = dir_offset; 173 } 174 175 if (S_ISDIR(mode) && 176 !new_inode->bi_subvol) 177 new_inode->bi_depth = dir_u->bi_depth + 1; 178 179 inode_iter.flags &= ~BTREE_ITER_all_snapshots; 180 bch2_btree_iter_set_snapshot(trans, &inode_iter, snapshot); 181 182 ret = bch2_btree_iter_traverse(trans, &inode_iter) ?: 183 bch2_inode_write(trans, &inode_iter, new_inode); 184 err: 185 bch2_trans_iter_exit(trans, &inode_iter); 186 bch2_trans_iter_exit(trans, &dir_iter); 187 return ret; 188 } 189 190 int bch2_link_trans(struct btree_trans *trans, 191 subvol_inum dir, struct bch_inode_unpacked *dir_u, 192 subvol_inum inum, struct bch_inode_unpacked *inode_u, 193 const struct qstr *name) 194 { 195 struct bch_fs *c = trans->c; 196 struct btree_iter dir_iter = {}; 197 struct btree_iter inode_iter = {}; 198 struct bch_hash_info dir_hash; 199 u64 now = bch2_current_time(c); 200 u64 dir_offset = 0; 201 int ret; 202 203 if (dir.subvol != inum.subvol) 204 return -EXDEV; 205 206 ret = bch2_inode_peek(trans, &inode_iter, inode_u, inum, BTREE_ITER_intent); 207 if (ret) 208 return ret; 209 210 inode_u->bi_ctime = now; 211 ret = bch2_inode_nlink_inc(inode_u); 212 if (ret) 213 goto err; 214 215 ret = bch2_inode_peek(trans, &dir_iter, dir_u, dir, BTREE_ITER_intent); 216 if (ret) 217 goto err; 218 219 if (bch2_reinherit_attrs(inode_u, dir_u)) { 220 ret = -EXDEV; 221 goto err; 222 } 223 224 dir_u->bi_mtime = dir_u->bi_ctime = now; 225 226 dir_hash = bch2_hash_info_init(c, dir_u); 227 228 ret = bch2_dirent_create(trans, dir, &dir_hash, 229 mode_to_type(inode_u->bi_mode), 230 name, inum.inum, 231 &dir_offset, 232 &dir_u->bi_size, 233 STR_HASH_must_create); 234 if (ret) 235 goto err; 236 237 inode_u->bi_dir = dir.inum; 238 inode_u->bi_dir_offset = dir_offset; 239 240 ret = bch2_inode_write(trans, &dir_iter, dir_u) ?: 241 bch2_inode_write(trans, &inode_iter, inode_u); 242 err: 243 bch2_trans_iter_exit(trans, &dir_iter); 244 bch2_trans_iter_exit(trans, &inode_iter); 245 return ret; 246 } 247 248 int bch2_unlink_trans(struct btree_trans *trans, 249 subvol_inum dir, 250 struct bch_inode_unpacked *dir_u, 251 struct bch_inode_unpacked *inode_u, 252 const struct qstr *name, 253 bool deleting_subvol) 254 { 255 struct bch_fs *c = trans->c; 256 struct btree_iter dir_iter = {}; 257 struct btree_iter dirent_iter = {}; 258 struct btree_iter inode_iter = {}; 259 struct bch_hash_info dir_hash; 260 subvol_inum inum; 261 u64 now = bch2_current_time(c); 262 struct bkey_s_c k; 263 int ret; 264 265 ret = bch2_inode_peek(trans, &dir_iter, dir_u, dir, BTREE_ITER_intent); 266 if (ret) 267 goto err; 268 269 dir_hash = bch2_hash_info_init(c, dir_u); 270 271 ret = bch2_dirent_lookup_trans(trans, &dirent_iter, dir, &dir_hash, 272 name, &inum, BTREE_ITER_intent); 273 if (ret) 274 goto err; 275 276 ret = bch2_inode_peek(trans, &inode_iter, inode_u, inum, 277 BTREE_ITER_intent); 278 if (ret) 279 goto err; 280 281 if (!deleting_subvol && S_ISDIR(inode_u->bi_mode)) { 282 ret = bch2_empty_dir_trans(trans, inum); 283 if (ret) 284 goto err; 285 } 286 287 if (deleting_subvol && !inode_u->bi_subvol) { 288 ret = -BCH_ERR_ENOENT_not_subvol; 289 goto err; 290 } 291 292 if (inode_u->bi_subvol) { 293 /* Recursive subvolume destroy not allowed (yet?) */ 294 ret = bch2_subvol_has_children(trans, inode_u->bi_subvol); 295 if (ret) 296 goto err; 297 } 298 299 if (deleting_subvol || inode_u->bi_subvol) { 300 ret = bch2_subvolume_unlink(trans, inode_u->bi_subvol); 301 if (ret) 302 goto err; 303 304 k = bch2_btree_iter_peek_slot(trans, &dirent_iter); 305 ret = bkey_err(k); 306 if (ret) 307 goto err; 308 309 /* 310 * If we're deleting a subvolume, we need to really delete the 311 * dirent, not just emit a whiteout in the current snapshot: 312 */ 313 bch2_btree_iter_set_snapshot(trans, &dirent_iter, k.k->p.snapshot); 314 ret = bch2_btree_iter_traverse(trans, &dirent_iter); 315 if (ret) 316 goto err; 317 } else { 318 bch2_inode_nlink_dec(trans, inode_u); 319 } 320 321 if (inode_u->bi_dir == dirent_iter.pos.inode && 322 inode_u->bi_dir_offset == dirent_iter.pos.offset) { 323 inode_u->bi_dir = 0; 324 inode_u->bi_dir_offset = 0; 325 } 326 327 dir_u->bi_mtime = dir_u->bi_ctime = inode_u->bi_ctime = now; 328 dir_u->bi_nlink -= is_subdir_for_nlink(inode_u); 329 330 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc, 331 &dir_hash, &dirent_iter, 332 BTREE_UPDATE_internal_snapshot_node) ?: 333 bch2_inode_write(trans, &dir_iter, dir_u) ?: 334 bch2_inode_write(trans, &inode_iter, inode_u); 335 err: 336 bch2_trans_iter_exit(trans, &inode_iter); 337 bch2_trans_iter_exit(trans, &dirent_iter); 338 bch2_trans_iter_exit(trans, &dir_iter); 339 return ret; 340 } 341 342 bool bch2_reinherit_attrs(struct bch_inode_unpacked *dst_u, 343 struct bch_inode_unpacked *src_u) 344 { 345 u64 src, dst; 346 unsigned id; 347 bool ret = false; 348 349 for (id = 0; id < Inode_opt_nr; id++) { 350 /* Skip attributes that were explicitly set on this inode */ 351 if (dst_u->bi_fields_set & (1 << id)) 352 continue; 353 354 src = bch2_inode_opt_get(src_u, id); 355 dst = bch2_inode_opt_get(dst_u, id); 356 357 if (src == dst) 358 continue; 359 360 bch2_inode_opt_set(dst_u, id, src); 361 ret = true; 362 } 363 364 return ret; 365 } 366 367 static int subvol_update_parent(struct btree_trans *trans, u32 subvol, u32 new_parent) 368 { 369 struct btree_iter iter; 370 struct bkey_i_subvolume *s = 371 bch2_bkey_get_mut_typed(trans, &iter, 372 BTREE_ID_subvolumes, POS(0, subvol), 373 BTREE_ITER_cached, subvolume); 374 int ret = PTR_ERR_OR_ZERO(s); 375 if (ret) 376 return ret; 377 378 s->v.fs_path_parent = cpu_to_le32(new_parent); 379 bch2_trans_iter_exit(trans, &iter); 380 return 0; 381 } 382 383 int bch2_rename_trans(struct btree_trans *trans, 384 subvol_inum src_dir, struct bch_inode_unpacked *src_dir_u, 385 subvol_inum dst_dir, struct bch_inode_unpacked *dst_dir_u, 386 struct bch_inode_unpacked *src_inode_u, 387 struct bch_inode_unpacked *dst_inode_u, 388 const struct qstr *src_name, 389 const struct qstr *dst_name, 390 enum bch_rename_mode mode) 391 { 392 struct bch_fs *c = trans->c; 393 struct btree_iter src_dir_iter = {}; 394 struct btree_iter dst_dir_iter = {}; 395 struct btree_iter src_inode_iter = {}; 396 struct btree_iter dst_inode_iter = {}; 397 struct bch_hash_info src_hash, dst_hash; 398 subvol_inum src_inum, dst_inum; 399 u64 src_offset, dst_offset; 400 u64 now = bch2_current_time(c); 401 int ret; 402 403 ret = bch2_inode_peek(trans, &src_dir_iter, src_dir_u, src_dir, 404 BTREE_ITER_intent); 405 if (ret) 406 goto err; 407 408 src_hash = bch2_hash_info_init(c, src_dir_u); 409 410 if (dst_dir.inum != src_dir.inum || 411 dst_dir.subvol != src_dir.subvol) { 412 ret = bch2_inode_peek(trans, &dst_dir_iter, dst_dir_u, dst_dir, 413 BTREE_ITER_intent); 414 if (ret) 415 goto err; 416 417 dst_hash = bch2_hash_info_init(c, dst_dir_u); 418 } else { 419 dst_dir_u = src_dir_u; 420 dst_hash = src_hash; 421 } 422 423 ret = bch2_dirent_rename(trans, 424 src_dir, &src_hash, &src_dir_u->bi_size, 425 dst_dir, &dst_hash, &dst_dir_u->bi_size, 426 src_name, &src_inum, &src_offset, 427 dst_name, &dst_inum, &dst_offset, 428 mode); 429 if (ret) 430 goto err; 431 432 ret = bch2_inode_peek(trans, &src_inode_iter, src_inode_u, src_inum, 433 BTREE_ITER_intent); 434 if (ret) 435 goto err; 436 437 if (dst_inum.inum) { 438 ret = bch2_inode_peek(trans, &dst_inode_iter, dst_inode_u, dst_inum, 439 BTREE_ITER_intent); 440 if (ret) 441 goto err; 442 } 443 444 if (src_inode_u->bi_subvol && 445 dst_dir.subvol != src_inode_u->bi_parent_subvol) { 446 ret = subvol_update_parent(trans, src_inode_u->bi_subvol, dst_dir.subvol); 447 if (ret) 448 goto err; 449 } 450 451 if (mode == BCH_RENAME_EXCHANGE && 452 dst_inode_u->bi_subvol && 453 src_dir.subvol != dst_inode_u->bi_parent_subvol) { 454 ret = subvol_update_parent(trans, dst_inode_u->bi_subvol, src_dir.subvol); 455 if (ret) 456 goto err; 457 } 458 459 /* Can't move across subvolumes, unless it's a subvolume root: */ 460 if (src_dir.subvol != dst_dir.subvol && 461 (!src_inode_u->bi_subvol || 462 (dst_inum.inum && !dst_inode_u->bi_subvol))) { 463 ret = -EXDEV; 464 goto err; 465 } 466 467 if (src_inode_u->bi_parent_subvol) 468 src_inode_u->bi_parent_subvol = dst_dir.subvol; 469 470 if ((mode == BCH_RENAME_EXCHANGE) && 471 dst_inode_u->bi_parent_subvol) 472 dst_inode_u->bi_parent_subvol = src_dir.subvol; 473 474 src_inode_u->bi_dir = dst_dir_u->bi_inum; 475 src_inode_u->bi_dir_offset = dst_offset; 476 477 if (mode == BCH_RENAME_EXCHANGE) { 478 dst_inode_u->bi_dir = src_dir_u->bi_inum; 479 dst_inode_u->bi_dir_offset = src_offset; 480 } 481 482 if (mode == BCH_RENAME_OVERWRITE && 483 dst_inode_u->bi_dir == dst_dir_u->bi_inum && 484 dst_inode_u->bi_dir_offset == src_offset) { 485 dst_inode_u->bi_dir = 0; 486 dst_inode_u->bi_dir_offset = 0; 487 } 488 489 if (mode == BCH_RENAME_OVERWRITE) { 490 if (S_ISDIR(src_inode_u->bi_mode) != 491 S_ISDIR(dst_inode_u->bi_mode)) { 492 ret = -ENOTDIR; 493 goto err; 494 } 495 496 if (S_ISDIR(dst_inode_u->bi_mode)) { 497 ret = bch2_empty_dir_trans(trans, dst_inum); 498 if (ret) 499 goto err; 500 } 501 } 502 503 if (bch2_reinherit_attrs(src_inode_u, dst_dir_u) && 504 S_ISDIR(src_inode_u->bi_mode)) { 505 ret = -EXDEV; 506 goto err; 507 } 508 509 if (mode == BCH_RENAME_EXCHANGE && 510 bch2_reinherit_attrs(dst_inode_u, src_dir_u) && 511 S_ISDIR(dst_inode_u->bi_mode)) { 512 ret = -EXDEV; 513 goto err; 514 } 515 516 if (is_subdir_for_nlink(src_inode_u)) { 517 src_dir_u->bi_nlink--; 518 dst_dir_u->bi_nlink++; 519 } 520 521 if (S_ISDIR(src_inode_u->bi_mode) && 522 !src_inode_u->bi_subvol) 523 src_inode_u->bi_depth = dst_dir_u->bi_depth + 1; 524 525 if (mode == BCH_RENAME_EXCHANGE && 526 S_ISDIR(dst_inode_u->bi_mode) && 527 !dst_inode_u->bi_subvol) 528 dst_inode_u->bi_depth = src_dir_u->bi_depth + 1; 529 530 if (dst_inum.inum && is_subdir_for_nlink(dst_inode_u)) { 531 dst_dir_u->bi_nlink--; 532 src_dir_u->bi_nlink += mode == BCH_RENAME_EXCHANGE; 533 } 534 535 if (mode == BCH_RENAME_OVERWRITE) 536 bch2_inode_nlink_dec(trans, dst_inode_u); 537 538 src_dir_u->bi_mtime = now; 539 src_dir_u->bi_ctime = now; 540 541 if (src_dir.inum != dst_dir.inum) { 542 dst_dir_u->bi_mtime = now; 543 dst_dir_u->bi_ctime = now; 544 } 545 546 src_inode_u->bi_ctime = now; 547 548 if (dst_inum.inum) 549 dst_inode_u->bi_ctime = now; 550 551 ret = bch2_inode_write(trans, &src_dir_iter, src_dir_u) ?: 552 (src_dir.inum != dst_dir.inum 553 ? bch2_inode_write(trans, &dst_dir_iter, dst_dir_u) 554 : 0) ?: 555 bch2_inode_write(trans, &src_inode_iter, src_inode_u) ?: 556 (dst_inum.inum 557 ? bch2_inode_write(trans, &dst_inode_iter, dst_inode_u) 558 : 0); 559 err: 560 bch2_trans_iter_exit(trans, &dst_inode_iter); 561 bch2_trans_iter_exit(trans, &src_inode_iter); 562 bch2_trans_iter_exit(trans, &dst_dir_iter); 563 bch2_trans_iter_exit(trans, &src_dir_iter); 564 return ret; 565 } 566 567 /* inum_to_path */ 568 569 static inline void prt_bytes_reversed(struct printbuf *out, const void *b, unsigned n) 570 { 571 bch2_printbuf_make_room(out, n); 572 573 unsigned can_print = min(n, printbuf_remaining(out)); 574 575 b += n; 576 577 for (unsigned i = 0; i < can_print; i++) 578 out->buf[out->pos++] = *((char *) --b); 579 580 printbuf_nul_terminate(out); 581 } 582 583 static inline void prt_str_reversed(struct printbuf *out, const char *s) 584 { 585 prt_bytes_reversed(out, s, strlen(s)); 586 } 587 588 static inline void reverse_bytes(void *b, size_t n) 589 { 590 char *e = b + n, *s = b; 591 592 while (s < e) { 593 --e; 594 swap(*s, *e); 595 s++; 596 } 597 } 598 599 /* XXX: we don't yet attempt to print paths when we don't know the subvol */ 600 int bch2_inum_to_path(struct btree_trans *trans, subvol_inum inum, struct printbuf *path) 601 { 602 unsigned orig_pos = path->pos; 603 int ret = 0; 604 605 while (!(inum.subvol == BCACHEFS_ROOT_SUBVOL && 606 inum.inum == BCACHEFS_ROOT_INO)) { 607 struct bch_inode_unpacked inode; 608 ret = bch2_inode_find_by_inum_trans(trans, inum, &inode); 609 if (ret) 610 goto disconnected; 611 612 if (!inode.bi_dir && !inode.bi_dir_offset) { 613 ret = -BCH_ERR_ENOENT_inode_no_backpointer; 614 goto disconnected; 615 } 616 617 inum.subvol = inode.bi_parent_subvol ?: inum.subvol; 618 inum.inum = inode.bi_dir; 619 620 u32 snapshot; 621 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot); 622 if (ret) 623 goto disconnected; 624 625 struct btree_iter d_iter; 626 struct bkey_s_c_dirent d = bch2_bkey_get_iter_typed(trans, &d_iter, 627 BTREE_ID_dirents, SPOS(inode.bi_dir, inode.bi_dir_offset, snapshot), 628 0, dirent); 629 ret = bkey_err(d.s_c); 630 if (ret) 631 goto disconnected; 632 633 struct qstr dirent_name = bch2_dirent_get_name(d); 634 prt_bytes_reversed(path, dirent_name.name, dirent_name.len); 635 636 prt_char(path, '/'); 637 638 bch2_trans_iter_exit(trans, &d_iter); 639 } 640 641 if (orig_pos == path->pos) 642 prt_char(path, '/'); 643 out: 644 ret = path->allocation_failure ? -ENOMEM : 0; 645 if (ret) 646 goto err; 647 648 reverse_bytes(path->buf + orig_pos, path->pos - orig_pos); 649 return 0; 650 err: 651 return ret; 652 disconnected: 653 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 654 goto err; 655 656 prt_str_reversed(path, "(disconnected)"); 657 goto out; 658 } 659 660 /* fsck */ 661 662 static int bch2_check_dirent_inode_dirent(struct btree_trans *trans, 663 struct bkey_s_c_dirent d, 664 struct bch_inode_unpacked *target, 665 bool in_fsck) 666 { 667 struct bch_fs *c = trans->c; 668 struct printbuf buf = PRINTBUF; 669 struct btree_iter bp_iter = {}; 670 int ret = 0; 671 672 if (inode_points_to_dirent(target, d)) 673 return 0; 674 675 if (!target->bi_dir && 676 !target->bi_dir_offset) { 677 fsck_err_on(S_ISDIR(target->bi_mode), 678 trans, inode_dir_missing_backpointer, 679 "directory with missing backpointer\n%s", 680 (printbuf_reset(&buf), 681 bch2_bkey_val_to_text(&buf, c, d.s_c), 682 prt_printf(&buf, "\n"), 683 bch2_inode_unpacked_to_text(&buf, target), 684 buf.buf)); 685 686 fsck_err_on(target->bi_flags & BCH_INODE_unlinked, 687 trans, inode_unlinked_but_has_dirent, 688 "inode unlinked but has dirent\n%s", 689 (printbuf_reset(&buf), 690 bch2_bkey_val_to_text(&buf, c, d.s_c), 691 prt_printf(&buf, "\n"), 692 bch2_inode_unpacked_to_text(&buf, target), 693 buf.buf)); 694 695 target->bi_flags &= ~BCH_INODE_unlinked; 696 target->bi_dir = d.k->p.inode; 697 target->bi_dir_offset = d.k->p.offset; 698 return __bch2_fsck_write_inode(trans, target); 699 } 700 701 if (bch2_inode_should_have_single_bp(target) && 702 !fsck_err(trans, inode_wrong_backpointer, 703 "dirent points to inode that does not point back:\n%s", 704 (bch2_bkey_val_to_text(&buf, c, d.s_c), 705 prt_newline(&buf), 706 bch2_inode_unpacked_to_text(&buf, target), 707 buf.buf))) 708 goto err; 709 710 struct bkey_s_c_dirent bp_dirent = 711 bch2_bkey_get_iter_typed(trans, &bp_iter, BTREE_ID_dirents, 712 SPOS(target->bi_dir, target->bi_dir_offset, target->bi_snapshot), 713 0, dirent); 714 ret = bkey_err(bp_dirent); 715 if (ret && !bch2_err_matches(ret, ENOENT)) 716 goto err; 717 718 bool backpointer_exists = !ret; 719 ret = 0; 720 721 if (!backpointer_exists) { 722 if (fsck_err(trans, inode_wrong_backpointer, 723 "inode %llu:%u has wrong backpointer:\n" 724 "got %llu:%llu\n" 725 "should be %llu:%llu", 726 target->bi_inum, target->bi_snapshot, 727 target->bi_dir, 728 target->bi_dir_offset, 729 d.k->p.inode, 730 d.k->p.offset)) { 731 target->bi_dir = d.k->p.inode; 732 target->bi_dir_offset = d.k->p.offset; 733 ret = __bch2_fsck_write_inode(trans, target); 734 } 735 } else { 736 bch2_bkey_val_to_text(&buf, c, d.s_c); 737 prt_newline(&buf); 738 bch2_bkey_val_to_text(&buf, c, bp_dirent.s_c); 739 740 if (S_ISDIR(target->bi_mode) || target->bi_subvol) { 741 /* 742 * XXX: verify connectivity of the other dirent 743 * up to the root before removing this one 744 * 745 * Additionally, bch2_lookup would need to cope with the 746 * dirent it found being removed - or should we remove 747 * the other one, even though the inode points to it? 748 */ 749 if (in_fsck) { 750 if (fsck_err(trans, inode_dir_multiple_links, 751 "%s %llu:%u with multiple links\n%s", 752 S_ISDIR(target->bi_mode) ? "directory" : "subvolume", 753 target->bi_inum, target->bi_snapshot, buf.buf)) 754 ret = bch2_fsck_remove_dirent(trans, d.k->p); 755 } else { 756 bch2_fs_inconsistent(c, 757 "%s %llu:%u with multiple links\n%s", 758 S_ISDIR(target->bi_mode) ? "directory" : "subvolume", 759 target->bi_inum, target->bi_snapshot, buf.buf); 760 } 761 762 goto out; 763 } else { 764 /* 765 * hardlinked file with nlink 0: 766 * We're just adjusting nlink here so check_nlinks() will pick 767 * it up, it ignores inodes with nlink 0 768 */ 769 if (fsck_err_on(!target->bi_nlink, 770 trans, inode_multiple_links_but_nlink_0, 771 "inode %llu:%u type %s has multiple links but i_nlink 0\n%s", 772 target->bi_inum, target->bi_snapshot, bch2_d_types[d.v->d_type], buf.buf)) { 773 target->bi_nlink++; 774 target->bi_flags &= ~BCH_INODE_unlinked; 775 ret = __bch2_fsck_write_inode(trans, target); 776 if (ret) 777 goto err; 778 } 779 } 780 } 781 out: 782 err: 783 fsck_err: 784 bch2_trans_iter_exit(trans, &bp_iter); 785 printbuf_exit(&buf); 786 bch_err_fn(c, ret); 787 return ret; 788 } 789 790 int __bch2_check_dirent_target(struct btree_trans *trans, 791 struct btree_iter *dirent_iter, 792 struct bkey_s_c_dirent d, 793 struct bch_inode_unpacked *target, 794 bool in_fsck) 795 { 796 struct bch_fs *c = trans->c; 797 struct printbuf buf = PRINTBUF; 798 int ret = 0; 799 800 ret = bch2_check_dirent_inode_dirent(trans, d, target, in_fsck); 801 if (ret) 802 goto err; 803 804 if (fsck_err_on(d.v->d_type != inode_d_type(target), 805 trans, dirent_d_type_wrong, 806 "incorrect d_type: got %s, should be %s:\n%s", 807 bch2_d_type_str(d.v->d_type), 808 bch2_d_type_str(inode_d_type(target)), 809 (printbuf_reset(&buf), 810 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) { 811 struct bkey_i_dirent *n = bch2_trans_kmalloc(trans, bkey_bytes(d.k)); 812 ret = PTR_ERR_OR_ZERO(n); 813 if (ret) 814 goto err; 815 816 bkey_reassemble(&n->k_i, d.s_c); 817 n->v.d_type = inode_d_type(target); 818 if (n->v.d_type == DT_SUBVOL) { 819 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol); 820 n->v.d_child_subvol = cpu_to_le32(target->bi_subvol); 821 } else { 822 n->v.d_inum = cpu_to_le64(target->bi_inum); 823 } 824 825 ret = bch2_trans_update(trans, dirent_iter, &n->k_i, 0); 826 if (ret) 827 goto err; 828 } 829 err: 830 fsck_err: 831 printbuf_exit(&buf); 832 bch_err_fn(c, ret); 833 return ret; 834 } 835