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