xref: /linux/fs/bcachefs/reflink.c (revision 8b8eed05a1c650c27e78bc47d07f7d6c9ba779e8)
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 	return bch2_trans_mark_extent(trans, btree_id, level, old, new, flags);
125 }
126 
127 /* indirect inline data */
128 
129 int bch2_indirect_inline_data_invalid(struct bch_fs *c, struct bkey_s_c k,
130 				      enum bkey_invalid_flags flags,
131 				      struct printbuf *err)
132 {
133 	return 0;
134 }
135 
136 void bch2_indirect_inline_data_to_text(struct printbuf *out,
137 				       struct bch_fs *c, struct bkey_s_c k)
138 {
139 	struct bkey_s_c_indirect_inline_data d = bkey_s_c_to_indirect_inline_data(k);
140 	unsigned datalen = bkey_inline_data_bytes(k.k);
141 
142 	prt_printf(out, "refcount %llu datalen %u: %*phN",
143 	       le64_to_cpu(d.v->refcount), datalen,
144 	       min(datalen, 32U), d.v->data);
145 }
146 
147 int bch2_trans_mark_indirect_inline_data(struct btree_trans *trans,
148 			      enum btree_id btree_id, unsigned level,
149 			      struct bkey_s_c old, struct bkey_i *new,
150 			      unsigned flags)
151 {
152 	check_indirect_extent_deleting(new, &flags);
153 
154 	return 0;
155 }
156 
157 static int bch2_make_extent_indirect(struct btree_trans *trans,
158 				     struct btree_iter *extent_iter,
159 				     struct bkey_i *orig)
160 {
161 	struct bch_fs *c = trans->c;
162 	struct btree_iter reflink_iter = { NULL };
163 	struct bkey_s_c k;
164 	struct bkey_i *r_v;
165 	struct bkey_i_reflink_p *r_p;
166 	__le64 *refcount;
167 	int ret;
168 
169 	if (orig->k.type == KEY_TYPE_inline_data)
170 		bch2_check_set_feature(c, BCH_FEATURE_reflink_inline_data);
171 
172 	bch2_trans_iter_init(trans, &reflink_iter, BTREE_ID_reflink, POS_MAX,
173 			     BTREE_ITER_INTENT);
174 	k = bch2_btree_iter_peek_prev(&reflink_iter);
175 	ret = bkey_err(k);
176 	if (ret)
177 		goto err;
178 
179 	r_v = bch2_trans_kmalloc(trans, sizeof(__le64) + bkey_bytes(&orig->k));
180 	ret = PTR_ERR_OR_ZERO(r_v);
181 	if (ret)
182 		goto err;
183 
184 	bkey_init(&r_v->k);
185 	r_v->k.type	= bkey_type_to_indirect(&orig->k);
186 	r_v->k.p	= reflink_iter.pos;
187 	bch2_key_resize(&r_v->k, orig->k.size);
188 	r_v->k.version	= orig->k.version;
189 
190 	set_bkey_val_bytes(&r_v->k, sizeof(__le64) + bkey_val_bytes(&orig->k));
191 
192 	refcount	= bkey_refcount(r_v);
193 	*refcount	= 0;
194 	memcpy(refcount + 1, &orig->v, bkey_val_bytes(&orig->k));
195 
196 	ret = bch2_trans_update(trans, &reflink_iter, r_v, 0);
197 	if (ret)
198 		goto err;
199 
200 	/*
201 	 * orig is in a bkey_buf which statically allocates 5 64s for the val,
202 	 * so we know it will be big enough:
203 	 */
204 	orig->k.type = KEY_TYPE_reflink_p;
205 	r_p = bkey_i_to_reflink_p(orig);
206 	set_bkey_val_bytes(&r_p->k, sizeof(r_p->v));
207 
208 	/* FORTIFY_SOURCE is broken here, and doesn't provide unsafe_memset() */
209 #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
210 	__underlying_memset(&r_p->v, 0, sizeof(r_p->v));
211 #else
212 	memset(&r_p->v, 0, sizeof(r_p->v));
213 #endif
214 
215 	r_p->v.idx = cpu_to_le64(bkey_start_offset(&r_v->k));
216 
217 	ret = bch2_trans_update(trans, extent_iter, &r_p->k_i,
218 				BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
219 err:
220 	bch2_trans_iter_exit(trans, &reflink_iter);
221 
222 	return ret;
223 }
224 
225 static struct bkey_s_c get_next_src(struct btree_iter *iter, struct bpos end)
226 {
227 	struct bkey_s_c k;
228 	int ret;
229 
230 	for_each_btree_key_upto_continue_norestart(*iter, end, 0, k, ret) {
231 		if (bkey_extent_is_unwritten(k))
232 			continue;
233 
234 		if (bkey_extent_is_data(k.k))
235 			return k;
236 	}
237 
238 	if (bkey_ge(iter->pos, end))
239 		bch2_btree_iter_set_pos(iter, end);
240 	return ret ? bkey_s_c_err(ret) : bkey_s_c_null;
241 }
242 
243 s64 bch2_remap_range(struct bch_fs *c,
244 		     subvol_inum dst_inum, u64 dst_offset,
245 		     subvol_inum src_inum, u64 src_offset,
246 		     u64 remap_sectors,
247 		     u64 new_i_size, s64 *i_sectors_delta)
248 {
249 	struct btree_trans *trans;
250 	struct btree_iter dst_iter, src_iter;
251 	struct bkey_s_c src_k;
252 	struct bkey_buf new_dst, new_src;
253 	struct bpos dst_start = POS(dst_inum.inum, dst_offset);
254 	struct bpos src_start = POS(src_inum.inum, src_offset);
255 	struct bpos dst_end = dst_start, src_end = src_start;
256 	struct bch_io_opts opts;
257 	struct bpos src_want;
258 	u64 dst_done = 0;
259 	u32 dst_snapshot, src_snapshot;
260 	int ret = 0, ret2 = 0;
261 
262 	if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_reflink))
263 		return -BCH_ERR_erofs_no_writes;
264 
265 	bch2_check_set_feature(c, BCH_FEATURE_reflink);
266 
267 	dst_end.offset += remap_sectors;
268 	src_end.offset += remap_sectors;
269 
270 	bch2_bkey_buf_init(&new_dst);
271 	bch2_bkey_buf_init(&new_src);
272 	trans = bch2_trans_get(c);
273 
274 	ret = bch2_inum_opts_get(trans, src_inum, &opts);
275 	if (ret)
276 		goto err;
277 
278 	bch2_trans_iter_init(trans, &src_iter, BTREE_ID_extents, src_start,
279 			     BTREE_ITER_INTENT);
280 	bch2_trans_iter_init(trans, &dst_iter, BTREE_ID_extents, dst_start,
281 			     BTREE_ITER_INTENT);
282 
283 	while ((ret == 0 ||
284 		bch2_err_matches(ret, BCH_ERR_transaction_restart)) &&
285 	       bkey_lt(dst_iter.pos, dst_end)) {
286 		struct disk_reservation disk_res = { 0 };
287 
288 		bch2_trans_begin(trans);
289 
290 		if (fatal_signal_pending(current)) {
291 			ret = -EINTR;
292 			break;
293 		}
294 
295 		ret = bch2_subvolume_get_snapshot(trans, src_inum.subvol,
296 						  &src_snapshot);
297 		if (ret)
298 			continue;
299 
300 		bch2_btree_iter_set_snapshot(&src_iter, src_snapshot);
301 
302 		ret = bch2_subvolume_get_snapshot(trans, dst_inum.subvol,
303 						  &dst_snapshot);
304 		if (ret)
305 			continue;
306 
307 		bch2_btree_iter_set_snapshot(&dst_iter, dst_snapshot);
308 
309 		dst_done = dst_iter.pos.offset - dst_start.offset;
310 		src_want = POS(src_start.inode, src_start.offset + dst_done);
311 		bch2_btree_iter_set_pos(&src_iter, src_want);
312 
313 		src_k = get_next_src(&src_iter, src_end);
314 		ret = bkey_err(src_k);
315 		if (ret)
316 			continue;
317 
318 		if (bkey_lt(src_want, src_iter.pos)) {
319 			ret = bch2_fpunch_at(trans, &dst_iter, dst_inum,
320 					min(dst_end.offset,
321 					    dst_iter.pos.offset +
322 					    src_iter.pos.offset - src_want.offset),
323 					i_sectors_delta);
324 			continue;
325 		}
326 
327 		if (src_k.k->type != KEY_TYPE_reflink_p) {
328 			bch2_btree_iter_set_pos_to_extent_start(&src_iter);
329 
330 			bch2_bkey_buf_reassemble(&new_src, c, src_k);
331 			src_k = bkey_i_to_s_c(new_src.k);
332 
333 			ret = bch2_make_extent_indirect(trans, &src_iter,
334 						new_src.k);
335 			if (ret)
336 				continue;
337 
338 			BUG_ON(src_k.k->type != KEY_TYPE_reflink_p);
339 		}
340 
341 		if (src_k.k->type == KEY_TYPE_reflink_p) {
342 			struct bkey_s_c_reflink_p src_p =
343 				bkey_s_c_to_reflink_p(src_k);
344 			struct bkey_i_reflink_p *dst_p =
345 				bkey_reflink_p_init(new_dst.k);
346 
347 			u64 offset = le64_to_cpu(src_p.v->idx) +
348 				(src_want.offset -
349 				 bkey_start_offset(src_k.k));
350 
351 			dst_p->v.idx = cpu_to_le64(offset);
352 		} else {
353 			BUG();
354 		}
355 
356 		new_dst.k->k.p = dst_iter.pos;
357 		bch2_key_resize(&new_dst.k->k,
358 				min(src_k.k->p.offset - src_want.offset,
359 				    dst_end.offset - dst_iter.pos.offset));
360 
361 		ret =   bch2_bkey_set_needs_rebalance(c, new_dst.k,
362 					opts.background_target,
363 					opts.background_compression) ?:
364 			bch2_extent_update(trans, dst_inum, &dst_iter,
365 					new_dst.k, &disk_res,
366 					new_i_size, i_sectors_delta,
367 					true);
368 		bch2_disk_reservation_put(c, &disk_res);
369 	}
370 	bch2_trans_iter_exit(trans, &dst_iter);
371 	bch2_trans_iter_exit(trans, &src_iter);
372 
373 	BUG_ON(!ret && !bkey_eq(dst_iter.pos, dst_end));
374 	BUG_ON(bkey_gt(dst_iter.pos, dst_end));
375 
376 	dst_done = dst_iter.pos.offset - dst_start.offset;
377 	new_i_size = min(dst_iter.pos.offset << 9, new_i_size);
378 
379 	do {
380 		struct bch_inode_unpacked inode_u;
381 		struct btree_iter inode_iter = { NULL };
382 
383 		bch2_trans_begin(trans);
384 
385 		ret2 = bch2_inode_peek(trans, &inode_iter, &inode_u,
386 				       dst_inum, BTREE_ITER_INTENT);
387 
388 		if (!ret2 &&
389 		    inode_u.bi_size < new_i_size) {
390 			inode_u.bi_size = new_i_size;
391 			ret2  = bch2_inode_write(trans, &inode_iter, &inode_u) ?:
392 				bch2_trans_commit(trans, NULL, NULL,
393 						  BTREE_INSERT_NOFAIL);
394 		}
395 
396 		bch2_trans_iter_exit(trans, &inode_iter);
397 	} while (bch2_err_matches(ret2, BCH_ERR_transaction_restart));
398 err:
399 	bch2_trans_put(trans);
400 	bch2_bkey_buf_exit(&new_src, c);
401 	bch2_bkey_buf_exit(&new_dst, c);
402 
403 	bch2_write_ref_put(c, BCH_WRITE_REF_reflink);
404 
405 	return dst_done ?: ret ?: ret2;
406 }
407