1 // SPDX-License-Identifier: GPL-2.0 2 #include "bcachefs.h" 3 #include "bkey_buf.h" 4 #include "btree_update.h" 5 #include "buckets.h" 6 #include "extents.h" 7 #include "inode.h" 8 #include "io_misc.h" 9 #include "io_write.h" 10 #include "rebalance.h" 11 #include "reflink.h" 12 #include "subvolume.h" 13 #include "super-io.h" 14 15 #include <linux/sched/signal.h> 16 17 static inline unsigned bkey_type_to_indirect(const struct bkey *k) 18 { 19 switch (k->type) { 20 case KEY_TYPE_extent: 21 return KEY_TYPE_reflink_v; 22 case KEY_TYPE_inline_data: 23 return KEY_TYPE_indirect_inline_data; 24 default: 25 return 0; 26 } 27 } 28 29 /* reflink pointers */ 30 31 int bch2_reflink_p_invalid(struct bch_fs *c, struct bkey_s_c k, 32 enum bkey_invalid_flags flags, 33 struct printbuf *err) 34 { 35 struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k); 36 37 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix && 38 le64_to_cpu(p.v->idx) < le32_to_cpu(p.v->front_pad)) { 39 prt_printf(err, "idx < front_pad (%llu < %u)", 40 le64_to_cpu(p.v->idx), le32_to_cpu(p.v->front_pad)); 41 return -EINVAL; 42 } 43 44 return 0; 45 } 46 47 void bch2_reflink_p_to_text(struct printbuf *out, struct bch_fs *c, 48 struct bkey_s_c k) 49 { 50 struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k); 51 52 prt_printf(out, "idx %llu front_pad %u back_pad %u", 53 le64_to_cpu(p.v->idx), 54 le32_to_cpu(p.v->front_pad), 55 le32_to_cpu(p.v->back_pad)); 56 } 57 58 bool bch2_reflink_p_merge(struct bch_fs *c, struct bkey_s _l, struct bkey_s_c _r) 59 { 60 struct bkey_s_reflink_p l = bkey_s_to_reflink_p(_l); 61 struct bkey_s_c_reflink_p r = bkey_s_c_to_reflink_p(_r); 62 63 /* 64 * Disabled for now, the triggers code needs to be reworked for merging 65 * of reflink pointers to work: 66 */ 67 return false; 68 69 if (le64_to_cpu(l.v->idx) + l.k->size != le64_to_cpu(r.v->idx)) 70 return false; 71 72 bch2_key_resize(l.k, l.k->size + r.k->size); 73 return true; 74 } 75 76 /* indirect extents */ 77 78 int bch2_reflink_v_invalid(struct bch_fs *c, struct bkey_s_c k, 79 enum bkey_invalid_flags flags, 80 struct printbuf *err) 81 { 82 return bch2_bkey_ptrs_invalid(c, k, flags, err); 83 } 84 85 void bch2_reflink_v_to_text(struct printbuf *out, struct bch_fs *c, 86 struct bkey_s_c k) 87 { 88 struct bkey_s_c_reflink_v r = bkey_s_c_to_reflink_v(k); 89 90 prt_printf(out, "refcount: %llu ", le64_to_cpu(r.v->refcount)); 91 92 bch2_bkey_ptrs_to_text(out, c, k); 93 } 94 95 #if 0 96 Currently disabled, needs to be debugged: 97 98 bool bch2_reflink_v_merge(struct bch_fs *c, struct bkey_s _l, struct bkey_s_c _r) 99 { 100 struct bkey_s_reflink_v l = bkey_s_to_reflink_v(_l); 101 struct bkey_s_c_reflink_v r = bkey_s_c_to_reflink_v(_r); 102 103 return l.v->refcount == r.v->refcount && bch2_extent_merge(c, _l, _r); 104 } 105 #endif 106 107 static inline void check_indirect_extent_deleting(struct bkey_i *new, unsigned *flags) 108 { 109 if ((*flags & BTREE_TRIGGER_INSERT) && !*bkey_refcount(new)) { 110 new->k.type = KEY_TYPE_deleted; 111 new->k.size = 0; 112 set_bkey_val_u64s(&new->k, 0);; 113 *flags &= ~BTREE_TRIGGER_INSERT; 114 } 115 } 116 117 int bch2_trans_mark_reflink_v(struct btree_trans *trans, 118 enum btree_id btree_id, unsigned level, 119 struct bkey_s_c old, struct bkey_i *new, 120 unsigned flags) 121 { 122 check_indirect_extent_deleting(new, &flags); 123 124 if (old.k->type == KEY_TYPE_reflink_v && 125 new->k.type == KEY_TYPE_reflink_v && 126 old.k->u64s == new->k.u64s && 127 !memcmp(bkey_s_c_to_reflink_v(old).v->start, 128 bkey_i_to_reflink_v(new)->v.start, 129 bkey_val_bytes(&new->k) - 8)) 130 return 0; 131 132 return bch2_trans_mark_extent(trans, btree_id, level, old, new, flags); 133 } 134 135 /* indirect inline data */ 136 137 int bch2_indirect_inline_data_invalid(struct bch_fs *c, struct bkey_s_c k, 138 enum bkey_invalid_flags flags, 139 struct printbuf *err) 140 { 141 return 0; 142 } 143 144 void bch2_indirect_inline_data_to_text(struct printbuf *out, 145 struct bch_fs *c, struct bkey_s_c k) 146 { 147 struct bkey_s_c_indirect_inline_data d = bkey_s_c_to_indirect_inline_data(k); 148 unsigned datalen = bkey_inline_data_bytes(k.k); 149 150 prt_printf(out, "refcount %llu datalen %u: %*phN", 151 le64_to_cpu(d.v->refcount), datalen, 152 min(datalen, 32U), d.v->data); 153 } 154 155 int bch2_trans_mark_indirect_inline_data(struct btree_trans *trans, 156 enum btree_id btree_id, unsigned level, 157 struct bkey_s_c old, struct bkey_i *new, 158 unsigned flags) 159 { 160 check_indirect_extent_deleting(new, &flags); 161 162 return 0; 163 } 164 165 static int bch2_make_extent_indirect(struct btree_trans *trans, 166 struct btree_iter *extent_iter, 167 struct bkey_i *orig) 168 { 169 struct bch_fs *c = trans->c; 170 struct btree_iter reflink_iter = { NULL }; 171 struct bkey_s_c k; 172 struct bkey_i *r_v; 173 struct bkey_i_reflink_p *r_p; 174 __le64 *refcount; 175 int ret; 176 177 if (orig->k.type == KEY_TYPE_inline_data) 178 bch2_check_set_feature(c, BCH_FEATURE_reflink_inline_data); 179 180 bch2_trans_iter_init(trans, &reflink_iter, BTREE_ID_reflink, POS_MAX, 181 BTREE_ITER_INTENT); 182 k = bch2_btree_iter_peek_prev(&reflink_iter); 183 ret = bkey_err(k); 184 if (ret) 185 goto err; 186 187 r_v = bch2_trans_kmalloc(trans, sizeof(__le64) + bkey_bytes(&orig->k)); 188 ret = PTR_ERR_OR_ZERO(r_v); 189 if (ret) 190 goto err; 191 192 bkey_init(&r_v->k); 193 r_v->k.type = bkey_type_to_indirect(&orig->k); 194 r_v->k.p = reflink_iter.pos; 195 bch2_key_resize(&r_v->k, orig->k.size); 196 r_v->k.version = orig->k.version; 197 198 set_bkey_val_bytes(&r_v->k, sizeof(__le64) + bkey_val_bytes(&orig->k)); 199 200 refcount = bkey_refcount(r_v); 201 *refcount = 0; 202 memcpy(refcount + 1, &orig->v, bkey_val_bytes(&orig->k)); 203 204 ret = bch2_trans_update(trans, &reflink_iter, r_v, 0); 205 if (ret) 206 goto err; 207 208 /* 209 * orig is in a bkey_buf which statically allocates 5 64s for the val, 210 * so we know it will be big enough: 211 */ 212 orig->k.type = KEY_TYPE_reflink_p; 213 r_p = bkey_i_to_reflink_p(orig); 214 set_bkey_val_bytes(&r_p->k, sizeof(r_p->v)); 215 216 /* FORTIFY_SOURCE is broken here, and doesn't provide unsafe_memset() */ 217 #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE) 218 __underlying_memset(&r_p->v, 0, sizeof(r_p->v)); 219 #else 220 memset(&r_p->v, 0, sizeof(r_p->v)); 221 #endif 222 223 r_p->v.idx = cpu_to_le64(bkey_start_offset(&r_v->k)); 224 225 ret = bch2_trans_update(trans, extent_iter, &r_p->k_i, 226 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE); 227 err: 228 bch2_trans_iter_exit(trans, &reflink_iter); 229 230 return ret; 231 } 232 233 static struct bkey_s_c get_next_src(struct btree_iter *iter, struct bpos end) 234 { 235 struct bkey_s_c k; 236 int ret; 237 238 for_each_btree_key_upto_continue_norestart(*iter, end, 0, k, ret) { 239 if (bkey_extent_is_unwritten(k)) 240 continue; 241 242 if (bkey_extent_is_data(k.k)) 243 return k; 244 } 245 246 if (bkey_ge(iter->pos, end)) 247 bch2_btree_iter_set_pos(iter, end); 248 return ret ? bkey_s_c_err(ret) : bkey_s_c_null; 249 } 250 251 s64 bch2_remap_range(struct bch_fs *c, 252 subvol_inum dst_inum, u64 dst_offset, 253 subvol_inum src_inum, u64 src_offset, 254 u64 remap_sectors, 255 u64 new_i_size, s64 *i_sectors_delta) 256 { 257 struct btree_trans *trans; 258 struct btree_iter dst_iter, src_iter; 259 struct bkey_s_c src_k; 260 struct bkey_buf new_dst, new_src; 261 struct bpos dst_start = POS(dst_inum.inum, dst_offset); 262 struct bpos src_start = POS(src_inum.inum, src_offset); 263 struct bpos dst_end = dst_start, src_end = src_start; 264 struct bch_io_opts opts; 265 struct bpos src_want; 266 u64 dst_done = 0; 267 u32 dst_snapshot, src_snapshot; 268 int ret = 0, ret2 = 0; 269 270 if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_reflink)) 271 return -BCH_ERR_erofs_no_writes; 272 273 bch2_check_set_feature(c, BCH_FEATURE_reflink); 274 275 dst_end.offset += remap_sectors; 276 src_end.offset += remap_sectors; 277 278 bch2_bkey_buf_init(&new_dst); 279 bch2_bkey_buf_init(&new_src); 280 trans = bch2_trans_get(c); 281 282 ret = bch2_inum_opts_get(trans, src_inum, &opts); 283 if (ret) 284 goto err; 285 286 bch2_trans_iter_init(trans, &src_iter, BTREE_ID_extents, src_start, 287 BTREE_ITER_INTENT); 288 bch2_trans_iter_init(trans, &dst_iter, BTREE_ID_extents, dst_start, 289 BTREE_ITER_INTENT); 290 291 while ((ret == 0 || 292 bch2_err_matches(ret, BCH_ERR_transaction_restart)) && 293 bkey_lt(dst_iter.pos, dst_end)) { 294 struct disk_reservation disk_res = { 0 }; 295 296 bch2_trans_begin(trans); 297 298 if (fatal_signal_pending(current)) { 299 ret = -EINTR; 300 break; 301 } 302 303 ret = bch2_subvolume_get_snapshot(trans, src_inum.subvol, 304 &src_snapshot); 305 if (ret) 306 continue; 307 308 bch2_btree_iter_set_snapshot(&src_iter, src_snapshot); 309 310 ret = bch2_subvolume_get_snapshot(trans, dst_inum.subvol, 311 &dst_snapshot); 312 if (ret) 313 continue; 314 315 bch2_btree_iter_set_snapshot(&dst_iter, dst_snapshot); 316 317 dst_done = dst_iter.pos.offset - dst_start.offset; 318 src_want = POS(src_start.inode, src_start.offset + dst_done); 319 bch2_btree_iter_set_pos(&src_iter, src_want); 320 321 src_k = get_next_src(&src_iter, src_end); 322 ret = bkey_err(src_k); 323 if (ret) 324 continue; 325 326 if (bkey_lt(src_want, src_iter.pos)) { 327 ret = bch2_fpunch_at(trans, &dst_iter, dst_inum, 328 min(dst_end.offset, 329 dst_iter.pos.offset + 330 src_iter.pos.offset - src_want.offset), 331 i_sectors_delta); 332 continue; 333 } 334 335 if (src_k.k->type != KEY_TYPE_reflink_p) { 336 bch2_btree_iter_set_pos_to_extent_start(&src_iter); 337 338 bch2_bkey_buf_reassemble(&new_src, c, src_k); 339 src_k = bkey_i_to_s_c(new_src.k); 340 341 ret = bch2_make_extent_indirect(trans, &src_iter, 342 new_src.k); 343 if (ret) 344 continue; 345 346 BUG_ON(src_k.k->type != KEY_TYPE_reflink_p); 347 } 348 349 if (src_k.k->type == KEY_TYPE_reflink_p) { 350 struct bkey_s_c_reflink_p src_p = 351 bkey_s_c_to_reflink_p(src_k); 352 struct bkey_i_reflink_p *dst_p = 353 bkey_reflink_p_init(new_dst.k); 354 355 u64 offset = le64_to_cpu(src_p.v->idx) + 356 (src_want.offset - 357 bkey_start_offset(src_k.k)); 358 359 dst_p->v.idx = cpu_to_le64(offset); 360 } else { 361 BUG(); 362 } 363 364 new_dst.k->k.p = dst_iter.pos; 365 bch2_key_resize(&new_dst.k->k, 366 min(src_k.k->p.offset - src_want.offset, 367 dst_end.offset - dst_iter.pos.offset)); 368 369 ret = bch2_bkey_set_needs_rebalance(c, new_dst.k, 370 opts.background_target, 371 opts.background_compression) ?: 372 bch2_extent_update(trans, dst_inum, &dst_iter, 373 new_dst.k, &disk_res, 374 new_i_size, i_sectors_delta, 375 true); 376 bch2_disk_reservation_put(c, &disk_res); 377 } 378 bch2_trans_iter_exit(trans, &dst_iter); 379 bch2_trans_iter_exit(trans, &src_iter); 380 381 BUG_ON(!ret && !bkey_eq(dst_iter.pos, dst_end)); 382 BUG_ON(bkey_gt(dst_iter.pos, dst_end)); 383 384 dst_done = dst_iter.pos.offset - dst_start.offset; 385 new_i_size = min(dst_iter.pos.offset << 9, new_i_size); 386 387 do { 388 struct bch_inode_unpacked inode_u; 389 struct btree_iter inode_iter = { NULL }; 390 391 bch2_trans_begin(trans); 392 393 ret2 = bch2_inode_peek(trans, &inode_iter, &inode_u, 394 dst_inum, BTREE_ITER_INTENT); 395 396 if (!ret2 && 397 inode_u.bi_size < new_i_size) { 398 inode_u.bi_size = new_i_size; 399 ret2 = bch2_inode_write(trans, &inode_iter, &inode_u) ?: 400 bch2_trans_commit(trans, NULL, NULL, 401 BTREE_INSERT_NOFAIL); 402 } 403 404 bch2_trans_iter_exit(trans, &inode_iter); 405 } while (bch2_err_matches(ret2, BCH_ERR_transaction_restart)); 406 err: 407 bch2_trans_put(trans); 408 bch2_bkey_buf_exit(&new_src, c); 409 bch2_bkey_buf_exit(&new_dst, c); 410 411 bch2_write_ref_put(c, BCH_WRITE_REF_reflink); 412 413 return dst_done ?: ret ?: ret2; 414 } 415