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.h" 9 #include "reflink.h" 10 11 #include <linux/sched/signal.h> 12 13 static inline unsigned bkey_type_to_indirect(const struct bkey *k) 14 { 15 switch (k->type) { 16 case KEY_TYPE_extent: 17 return KEY_TYPE_reflink_v; 18 case KEY_TYPE_inline_data: 19 return KEY_TYPE_indirect_inline_data; 20 default: 21 return 0; 22 } 23 } 24 25 /* reflink pointers */ 26 27 const char *bch2_reflink_p_invalid(const struct bch_fs *c, struct bkey_s_c k) 28 { 29 struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k); 30 31 if (bkey_val_bytes(p.k) != sizeof(*p.v)) 32 return "incorrect value size"; 33 34 return NULL; 35 } 36 37 void bch2_reflink_p_to_text(struct printbuf *out, struct bch_fs *c, 38 struct bkey_s_c k) 39 { 40 struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k); 41 42 pr_buf(out, "idx %llu", le64_to_cpu(p.v->idx)); 43 } 44 45 bool bch2_reflink_p_merge(struct bch_fs *c, struct bkey_s _l, struct bkey_s_c _r) 46 { 47 struct bkey_s_reflink_p l = bkey_s_to_reflink_p(_l); 48 struct bkey_s_c_reflink_p r = bkey_s_c_to_reflink_p(_r); 49 50 /* 51 * Disabled for now, the triggers code needs to be reworked for merging 52 * of reflink pointers to work: 53 */ 54 return false; 55 56 if (le64_to_cpu(l.v->idx) + l.k->size != le64_to_cpu(r.v->idx)) 57 return false; 58 59 bch2_key_resize(l.k, l.k->size + r.k->size); 60 return true; 61 } 62 63 /* indirect extents */ 64 65 const char *bch2_reflink_v_invalid(const struct bch_fs *c, struct bkey_s_c k) 66 { 67 struct bkey_s_c_reflink_v r = bkey_s_c_to_reflink_v(k); 68 69 if (bkey_val_bytes(r.k) < sizeof(*r.v)) 70 return "incorrect value size"; 71 72 return bch2_bkey_ptrs_invalid(c, k); 73 } 74 75 void bch2_reflink_v_to_text(struct printbuf *out, struct bch_fs *c, 76 struct bkey_s_c k) 77 { 78 struct bkey_s_c_reflink_v r = bkey_s_c_to_reflink_v(k); 79 80 pr_buf(out, "refcount: %llu ", le64_to_cpu(r.v->refcount)); 81 82 bch2_bkey_ptrs_to_text(out, c, k); 83 } 84 85 bool bch2_reflink_v_merge(struct bch_fs *c, struct bkey_s _l, struct bkey_s_c _r) 86 { 87 struct bkey_s_reflink_v l = bkey_s_to_reflink_v(_l); 88 struct bkey_s_c_reflink_v r = bkey_s_c_to_reflink_v(_r); 89 90 return l.v->refcount == r.v->refcount && bch2_extent_merge(c, _l, _r); 91 } 92 93 /* indirect inline data */ 94 95 const char *bch2_indirect_inline_data_invalid(const struct bch_fs *c, 96 struct bkey_s_c k) 97 { 98 if (bkey_val_bytes(k.k) < sizeof(struct bch_indirect_inline_data)) 99 return "incorrect value size"; 100 return NULL; 101 } 102 103 void bch2_indirect_inline_data_to_text(struct printbuf *out, 104 struct bch_fs *c, struct bkey_s_c k) 105 { 106 struct bkey_s_c_indirect_inline_data d = bkey_s_c_to_indirect_inline_data(k); 107 unsigned datalen = bkey_inline_data_bytes(k.k); 108 109 pr_buf(out, "refcount %llu datalen %u: %*phN", 110 le64_to_cpu(d.v->refcount), datalen, 111 min(datalen, 32U), d.v->data); 112 } 113 114 static int bch2_make_extent_indirect(struct btree_trans *trans, 115 struct btree_iter *extent_iter, 116 struct bkey_i *orig) 117 { 118 struct bch_fs *c = trans->c; 119 struct btree_iter reflink_iter = { NULL }; 120 struct bkey_s_c k; 121 struct bkey_i *r_v; 122 struct bkey_i_reflink_p *r_p; 123 __le64 *refcount; 124 int ret; 125 126 if (orig->k.type == KEY_TYPE_inline_data) 127 bch2_check_set_feature(c, BCH_FEATURE_reflink_inline_data); 128 129 for_each_btree_key(trans, reflink_iter, BTREE_ID_reflink, 130 POS(0, c->reflink_hint), 131 BTREE_ITER_INTENT|BTREE_ITER_SLOTS, k, ret) { 132 if (reflink_iter.pos.inode) { 133 bch2_btree_iter_set_pos(&reflink_iter, POS_MIN); 134 continue; 135 } 136 137 if (bkey_deleted(k.k) && orig->k.size <= k.k->size) 138 break; 139 } 140 141 if (ret) 142 goto err; 143 144 /* rewind iter to start of hole, if necessary: */ 145 bch2_btree_iter_set_pos_to_extent_start(&reflink_iter); 146 147 r_v = bch2_trans_kmalloc(trans, sizeof(__le64) + bkey_bytes(&orig->k)); 148 ret = PTR_ERR_OR_ZERO(r_v); 149 if (ret) 150 goto err; 151 152 bkey_init(&r_v->k); 153 r_v->k.type = bkey_type_to_indirect(&orig->k); 154 r_v->k.p = reflink_iter.pos; 155 bch2_key_resize(&r_v->k, orig->k.size); 156 r_v->k.version = orig->k.version; 157 158 set_bkey_val_bytes(&r_v->k, sizeof(__le64) + bkey_val_bytes(&orig->k)); 159 160 refcount = bkey_refcount(r_v); 161 *refcount = 0; 162 memcpy(refcount + 1, &orig->v, bkey_val_bytes(&orig->k)); 163 164 ret = bch2_trans_update(trans, &reflink_iter, r_v, 0); 165 if (ret) 166 goto err; 167 168 orig->k.type = KEY_TYPE_reflink_p; 169 r_p = bkey_i_to_reflink_p(orig); 170 set_bkey_val_bytes(&r_p->k, sizeof(r_p->v)); 171 r_p->v.idx = cpu_to_le64(bkey_start_offset(&r_v->k)); 172 173 ret = bch2_trans_update(trans, extent_iter, &r_p->k_i, 0); 174 err: 175 c->reflink_hint = reflink_iter.pos.offset; 176 bch2_trans_iter_exit(trans, &reflink_iter); 177 178 return ret; 179 } 180 181 static struct bkey_s_c get_next_src(struct btree_iter *iter, struct bpos end) 182 { 183 struct bkey_s_c k; 184 int ret; 185 186 for_each_btree_key_continue(*iter, 0, k, ret) { 187 if (bkey_cmp(iter->pos, end) >= 0) 188 break; 189 190 if (bkey_extent_is_data(k.k)) 191 return k; 192 } 193 194 if (bkey_cmp(iter->pos, end) >= 0) 195 bch2_btree_iter_set_pos(iter, end); 196 return ret ? bkey_s_c_err(ret) : bkey_s_c_null; 197 } 198 199 s64 bch2_remap_range(struct bch_fs *c, 200 struct bpos dst_start, struct bpos src_start, 201 u64 remap_sectors, u64 *journal_seq, 202 u64 new_i_size, s64 *i_sectors_delta) 203 { 204 struct btree_trans trans; 205 struct btree_iter dst_iter, src_iter; 206 struct bkey_s_c src_k; 207 struct bkey_buf new_dst, new_src; 208 struct bpos dst_end = dst_start, src_end = src_start; 209 struct bpos src_want; 210 u64 dst_done; 211 int ret = 0, ret2 = 0; 212 213 if (!percpu_ref_tryget(&c->writes)) 214 return -EROFS; 215 216 bch2_check_set_feature(c, BCH_FEATURE_reflink); 217 218 dst_end.offset += remap_sectors; 219 src_end.offset += remap_sectors; 220 221 bch2_bkey_buf_init(&new_dst); 222 bch2_bkey_buf_init(&new_src); 223 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 4096); 224 225 bch2_trans_iter_init(&trans, &src_iter, BTREE_ID_extents, src_start, 226 BTREE_ITER_INTENT); 227 bch2_trans_iter_init(&trans, &dst_iter, BTREE_ID_extents, dst_start, 228 BTREE_ITER_INTENT); 229 230 while ((ret == 0 || ret == -EINTR) && 231 bkey_cmp(dst_iter.pos, dst_end) < 0) { 232 struct disk_reservation disk_res = { 0 }; 233 234 bch2_trans_begin(&trans); 235 236 if (fatal_signal_pending(current)) { 237 ret = -EINTR; 238 break; 239 } 240 241 dst_done = dst_iter.pos.offset - dst_start.offset; 242 src_want = POS(src_start.inode, src_start.offset + dst_done); 243 bch2_btree_iter_set_pos(&src_iter, src_want); 244 245 src_k = get_next_src(&src_iter, src_end); 246 ret = bkey_err(src_k); 247 if (ret) 248 continue; 249 250 if (bkey_cmp(src_want, src_iter.pos) < 0) { 251 ret = bch2_fpunch_at(&trans, &dst_iter, 252 bpos_min(dst_end, 253 POS(dst_iter.pos.inode, dst_iter.pos.offset + 254 src_iter.pos.offset - src_want.offset)), 255 journal_seq, i_sectors_delta); 256 continue; 257 } 258 259 if (src_k.k->type != KEY_TYPE_reflink_p) { 260 bch2_btree_iter_set_pos_to_extent_start(&src_iter); 261 262 bch2_bkey_buf_reassemble(&new_src, c, src_k); 263 src_k = bkey_i_to_s_c(new_src.k); 264 265 ret = bch2_make_extent_indirect(&trans, &src_iter, 266 new_src.k); 267 if (ret) 268 continue; 269 270 BUG_ON(src_k.k->type != KEY_TYPE_reflink_p); 271 } 272 273 if (src_k.k->type == KEY_TYPE_reflink_p) { 274 struct bkey_s_c_reflink_p src_p = 275 bkey_s_c_to_reflink_p(src_k); 276 struct bkey_i_reflink_p *dst_p = 277 bkey_reflink_p_init(new_dst.k); 278 279 u64 offset = le64_to_cpu(src_p.v->idx) + 280 (src_want.offset - 281 bkey_start_offset(src_k.k)); 282 283 dst_p->v.idx = cpu_to_le64(offset); 284 } else { 285 BUG(); 286 } 287 288 new_dst.k->k.p = dst_iter.pos; 289 bch2_key_resize(&new_dst.k->k, 290 min(src_k.k->p.offset - src_want.offset, 291 dst_end.offset - dst_iter.pos.offset)); 292 ret = bch2_extent_update(&trans, &dst_iter, new_dst.k, 293 &disk_res, journal_seq, 294 new_i_size, i_sectors_delta, 295 true); 296 bch2_disk_reservation_put(c, &disk_res); 297 } 298 bch2_trans_iter_exit(&trans, &dst_iter); 299 bch2_trans_iter_exit(&trans, &src_iter); 300 301 BUG_ON(!ret && bkey_cmp(dst_iter.pos, dst_end)); 302 BUG_ON(bkey_cmp(dst_iter.pos, dst_end) > 0); 303 304 dst_done = dst_iter.pos.offset - dst_start.offset; 305 new_i_size = min(dst_iter.pos.offset << 9, new_i_size); 306 307 do { 308 struct bch_inode_unpacked inode_u; 309 struct btree_iter inode_iter = { NULL }; 310 311 bch2_trans_begin(&trans); 312 313 ret2 = bch2_inode_peek(&trans, &inode_iter, &inode_u, 314 dst_start.inode, BTREE_ITER_INTENT); 315 316 if (!ret2 && 317 inode_u.bi_size < new_i_size) { 318 inode_u.bi_size = new_i_size; 319 ret2 = bch2_inode_write(&trans, &inode_iter, &inode_u) ?: 320 bch2_trans_commit(&trans, NULL, journal_seq, 0); 321 } 322 323 bch2_trans_iter_exit(&trans, &inode_iter); 324 } while (ret2 == -EINTR); 325 326 ret = bch2_trans_exit(&trans) ?: ret; 327 bch2_bkey_buf_exit(&new_src, c); 328 bch2_bkey_buf_exit(&new_dst, c); 329 330 percpu_ref_put(&c->writes); 331 332 return dst_done ?: ret ?: ret2; 333 } 334