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