xref: /linux/fs/bcachefs/io_misc.c (revision a9aaf1ff88a8cb99a1335c9eb76de637f0cf8c10)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * io_misc.c - fallocate, fpunch, truncate:
4  */
5 
6 #include "bcachefs.h"
7 #include "alloc_foreground.h"
8 #include "bkey_buf.h"
9 #include "btree_update.h"
10 #include "buckets.h"
11 #include "clock.h"
12 #include "error.h"
13 #include "extents.h"
14 #include "extent_update.h"
15 #include "inode.h"
16 #include "io_misc.h"
17 #include "io_write.h"
18 #include "logged_ops.h"
19 #include "rebalance.h"
20 #include "subvolume.h"
21 
22 /* Overwrites whatever was present with zeroes: */
23 int bch2_extent_fallocate(struct btree_trans *trans,
24 			  subvol_inum inum,
25 			  struct btree_iter *iter,
26 			  u64 sectors,
27 			  struct bch_io_opts opts,
28 			  s64 *i_sectors_delta,
29 			  struct write_point_specifier write_point)
30 {
31 	struct bch_fs *c = trans->c;
32 	struct disk_reservation disk_res = { 0 };
33 	struct closure cl;
34 	struct open_buckets open_buckets = { 0 };
35 	struct bkey_s_c k;
36 	struct bkey_buf old, new;
37 	unsigned sectors_allocated = 0, new_replicas;
38 	bool unwritten = opts.nocow &&
39 	    c->sb.version >= bcachefs_metadata_version_unwritten_extents;
40 	int ret;
41 
42 	bch2_bkey_buf_init(&old);
43 	bch2_bkey_buf_init(&new);
44 	closure_init_stack(&cl);
45 
46 	k = bch2_btree_iter_peek_slot(iter);
47 	ret = bkey_err(k);
48 	if (ret)
49 		return ret;
50 
51 	sectors = min_t(u64, sectors, k.k->p.offset - iter->pos.offset);
52 	new_replicas = max(0, (int) opts.data_replicas -
53 			   (int) bch2_bkey_nr_ptrs_fully_allocated(k));
54 
55 	/*
56 	 * Get a disk reservation before (in the nocow case) calling
57 	 * into the allocator:
58 	 */
59 	ret = bch2_disk_reservation_get(c, &disk_res, sectors, new_replicas, 0);
60 	if (unlikely(ret))
61 		goto err_noprint;
62 
63 	bch2_bkey_buf_reassemble(&old, c, k);
64 
65 	if (!unwritten) {
66 		struct bkey_i_reservation *reservation;
67 
68 		bch2_bkey_buf_realloc(&new, c, sizeof(*reservation) / sizeof(u64));
69 		reservation = bkey_reservation_init(new.k);
70 		reservation->k.p = iter->pos;
71 		bch2_key_resize(&reservation->k, sectors);
72 		reservation->v.nr_replicas = opts.data_replicas;
73 	} else {
74 		struct bkey_i_extent *e;
75 		struct bch_devs_list devs_have;
76 		struct write_point *wp;
77 
78 		devs_have.nr = 0;
79 
80 		bch2_bkey_buf_realloc(&new, c, BKEY_EXTENT_U64s_MAX);
81 
82 		e = bkey_extent_init(new.k);
83 		e->k.p = iter->pos;
84 
85 		ret = bch2_alloc_sectors_start_trans(trans,
86 				opts.foreground_target,
87 				false,
88 				write_point,
89 				&devs_have,
90 				opts.data_replicas,
91 				opts.data_replicas,
92 				BCH_WATERMARK_normal, 0, &cl, &wp);
93 		if (bch2_err_matches(ret, BCH_ERR_operation_blocked))
94 			ret = -BCH_ERR_transaction_restart_nested;
95 		if (ret)
96 			goto err;
97 
98 		sectors = min_t(u64, sectors, wp->sectors_free);
99 		sectors_allocated = sectors;
100 
101 		bch2_key_resize(&e->k, sectors);
102 
103 		bch2_open_bucket_get(c, wp, &open_buckets);
104 		bch2_alloc_sectors_append_ptrs(c, wp, &e->k_i, sectors, false);
105 		bch2_alloc_sectors_done(c, wp);
106 
107 		extent_for_each_ptr(extent_i_to_s(e), ptr)
108 			ptr->unwritten = true;
109 	}
110 
111 	ret = bch2_extent_update(trans, inum, iter, new.k, &disk_res,
112 				 0, i_sectors_delta, true);
113 err:
114 	if (!ret && sectors_allocated)
115 		bch2_increment_clock(c, sectors_allocated, WRITE);
116 	if (should_print_err(ret))
117 		bch_err_inum_offset_ratelimited(c,
118 			inum.inum,
119 			iter->pos.offset << 9,
120 			"%s(): error: %s", __func__, bch2_err_str(ret));
121 err_noprint:
122 	bch2_open_buckets_put(c, &open_buckets);
123 	bch2_disk_reservation_put(c, &disk_res);
124 	bch2_bkey_buf_exit(&new, c);
125 	bch2_bkey_buf_exit(&old, c);
126 
127 	if (closure_nr_remaining(&cl) != 1) {
128 		bch2_trans_unlock_long(trans);
129 
130 		if (closure_sync_timeout(&cl, HZ * 10)) {
131 			bch2_print_allocator_stuck(c);
132 			closure_sync(&cl);
133 		}
134 	}
135 
136 	return ret;
137 }
138 
139 /*
140  * Returns -BCH_ERR_transacton_restart if we had to drop locks:
141  */
142 int bch2_fpunch_at(struct btree_trans *trans, struct btree_iter *iter,
143 		   subvol_inum inum, u64 end,
144 		   s64 *i_sectors_delta)
145 {
146 	struct bch_fs *c	= trans->c;
147 	unsigned max_sectors	= KEY_SIZE_MAX & (~0 << c->block_bits);
148 	struct bpos end_pos = POS(inum.inum, end);
149 	struct bkey_s_c k;
150 	int ret = 0, ret2 = 0;
151 	u32 snapshot;
152 
153 	while (!ret ||
154 	       bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
155 		struct disk_reservation disk_res =
156 			bch2_disk_reservation_init(c, 0);
157 		struct bkey_i delete;
158 
159 		if (ret)
160 			ret2 = ret;
161 
162 		bch2_trans_begin(trans);
163 
164 		ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
165 		if (ret)
166 			continue;
167 
168 		bch2_btree_iter_set_snapshot(iter, snapshot);
169 
170 		/*
171 		 * peek_upto() doesn't have ideal semantics for extents:
172 		 */
173 		k = bch2_btree_iter_peek_upto(iter, end_pos);
174 		if (!k.k)
175 			break;
176 
177 		ret = bkey_err(k);
178 		if (ret)
179 			continue;
180 
181 		bkey_init(&delete.k);
182 		delete.k.p = iter->pos;
183 
184 		/* create the biggest key we can */
185 		bch2_key_resize(&delete.k, max_sectors);
186 		bch2_cut_back(end_pos, &delete);
187 
188 		ret = bch2_extent_update(trans, inum, iter, &delete,
189 				&disk_res, 0, i_sectors_delta, false);
190 		bch2_disk_reservation_put(c, &disk_res);
191 	}
192 
193 	return ret ?: ret2;
194 }
195 
196 int bch2_fpunch(struct bch_fs *c, subvol_inum inum, u64 start, u64 end,
197 		s64 *i_sectors_delta)
198 {
199 	struct btree_trans *trans = bch2_trans_get(c);
200 	struct btree_iter iter;
201 	int ret;
202 
203 	bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
204 			     POS(inum.inum, start),
205 			     BTREE_ITER_intent);
206 
207 	ret = bch2_fpunch_at(trans, &iter, inum, end, i_sectors_delta);
208 
209 	bch2_trans_iter_exit(trans, &iter);
210 	bch2_trans_put(trans);
211 
212 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
213 		ret = 0;
214 
215 	return ret;
216 }
217 
218 /* truncate: */
219 
220 void bch2_logged_op_truncate_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
221 {
222 	struct bkey_s_c_logged_op_truncate op = bkey_s_c_to_logged_op_truncate(k);
223 
224 	prt_printf(out, "subvol=%u", le32_to_cpu(op.v->subvol));
225 	prt_printf(out, " inum=%llu", le64_to_cpu(op.v->inum));
226 	prt_printf(out, " new_i_size=%llu", le64_to_cpu(op.v->new_i_size));
227 }
228 
229 static int truncate_set_isize(struct btree_trans *trans,
230 			      subvol_inum inum,
231 			      u64 new_i_size)
232 {
233 	struct btree_iter iter = { NULL };
234 	struct bch_inode_unpacked inode_u;
235 	int ret;
236 
237 	ret   = bch2_inode_peek(trans, &iter, &inode_u, inum, BTREE_ITER_intent) ?:
238 		(inode_u.bi_size = new_i_size, 0) ?:
239 		bch2_inode_write(trans, &iter, &inode_u);
240 
241 	bch2_trans_iter_exit(trans, &iter);
242 	return ret;
243 }
244 
245 static int __bch2_resume_logged_op_truncate(struct btree_trans *trans,
246 					    struct bkey_i *op_k,
247 					    u64 *i_sectors_delta)
248 {
249 	struct bch_fs *c = trans->c;
250 	struct btree_iter fpunch_iter;
251 	struct bkey_i_logged_op_truncate *op = bkey_i_to_logged_op_truncate(op_k);
252 	subvol_inum inum = { le32_to_cpu(op->v.subvol), le64_to_cpu(op->v.inum) };
253 	u64 new_i_size = le64_to_cpu(op->v.new_i_size);
254 	int ret;
255 
256 	ret = commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
257 			truncate_set_isize(trans, inum, new_i_size));
258 	if (ret)
259 		goto err;
260 
261 	bch2_trans_iter_init(trans, &fpunch_iter, BTREE_ID_extents,
262 			     POS(inum.inum, round_up(new_i_size, block_bytes(c)) >> 9),
263 			     BTREE_ITER_intent);
264 	ret = bch2_fpunch_at(trans, &fpunch_iter, inum, U64_MAX, i_sectors_delta);
265 	bch2_trans_iter_exit(trans, &fpunch_iter);
266 
267 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
268 		ret = 0;
269 err:
270 	bch2_logged_op_finish(trans, op_k);
271 	bch_err_fn(c, ret);
272 	return ret;
273 }
274 
275 int bch2_resume_logged_op_truncate(struct btree_trans *trans, struct bkey_i *op_k)
276 {
277 	return __bch2_resume_logged_op_truncate(trans, op_k, NULL);
278 }
279 
280 int bch2_truncate(struct bch_fs *c, subvol_inum inum, u64 new_i_size, u64 *i_sectors_delta)
281 {
282 	struct bkey_i_logged_op_truncate op;
283 
284 	bkey_logged_op_truncate_init(&op.k_i);
285 	op.v.subvol	= cpu_to_le32(inum.subvol);
286 	op.v.inum	= cpu_to_le64(inum.inum);
287 	op.v.new_i_size	= cpu_to_le64(new_i_size);
288 
289 	/*
290 	 * Logged ops aren't atomic w.r.t. snapshot creation: creating a
291 	 * snapshot while they're in progress, then crashing, will result in the
292 	 * resume only proceeding in one of the snapshots
293 	 */
294 	down_read(&c->snapshot_create_lock);
295 	int ret = bch2_trans_run(c,
296 		bch2_logged_op_start(trans, &op.k_i) ?:
297 		__bch2_resume_logged_op_truncate(trans, &op.k_i, i_sectors_delta));
298 	up_read(&c->snapshot_create_lock);
299 
300 	return ret;
301 }
302 
303 /* finsert/fcollapse: */
304 
305 void bch2_logged_op_finsert_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
306 {
307 	struct bkey_s_c_logged_op_finsert op = bkey_s_c_to_logged_op_finsert(k);
308 
309 	prt_printf(out, "subvol=%u",		le32_to_cpu(op.v->subvol));
310 	prt_printf(out, " inum=%llu",		le64_to_cpu(op.v->inum));
311 	prt_printf(out, " dst_offset=%lli",	le64_to_cpu(op.v->dst_offset));
312 	prt_printf(out, " src_offset=%llu",	le64_to_cpu(op.v->src_offset));
313 }
314 
315 static int adjust_i_size(struct btree_trans *trans, subvol_inum inum, u64 offset, s64 len)
316 {
317 	struct btree_iter iter;
318 	struct bch_inode_unpacked inode_u;
319 	int ret;
320 
321 	offset	<<= 9;
322 	len	<<= 9;
323 
324 	ret = bch2_inode_peek(trans, &iter, &inode_u, inum, BTREE_ITER_intent);
325 	if (ret)
326 		return ret;
327 
328 	if (len > 0) {
329 		if (MAX_LFS_FILESIZE - inode_u.bi_size < len) {
330 			ret = -EFBIG;
331 			goto err;
332 		}
333 
334 		if (offset >= inode_u.bi_size) {
335 			ret = -EINVAL;
336 			goto err;
337 		}
338 	}
339 
340 	inode_u.bi_size += len;
341 	inode_u.bi_mtime = inode_u.bi_ctime = bch2_current_time(trans->c);
342 
343 	ret = bch2_inode_write(trans, &iter, &inode_u);
344 err:
345 	bch2_trans_iter_exit(trans, &iter);
346 	return ret;
347 }
348 
349 static int __bch2_resume_logged_op_finsert(struct btree_trans *trans,
350 					   struct bkey_i *op_k,
351 					   u64 *i_sectors_delta)
352 {
353 	struct bch_fs *c = trans->c;
354 	struct btree_iter iter;
355 	struct bkey_i_logged_op_finsert *op = bkey_i_to_logged_op_finsert(op_k);
356 	subvol_inum inum = { le32_to_cpu(op->v.subvol), le64_to_cpu(op->v.inum) };
357 	struct bch_io_opts opts;
358 	u64 dst_offset = le64_to_cpu(op->v.dst_offset);
359 	u64 src_offset = le64_to_cpu(op->v.src_offset);
360 	s64 shift = dst_offset - src_offset;
361 	u64 len = abs(shift);
362 	u64 pos = le64_to_cpu(op->v.pos);
363 	bool insert = shift > 0;
364 	int ret = 0;
365 
366 	ret = bch2_inum_opts_get(trans, inum, &opts);
367 	if (ret)
368 		return ret;
369 
370 	bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
371 			     POS(inum.inum, 0),
372 			     BTREE_ITER_intent);
373 
374 	switch (op->v.state) {
375 case LOGGED_OP_FINSERT_start:
376 	op->v.state = LOGGED_OP_FINSERT_shift_extents;
377 
378 	if (insert) {
379 		ret = commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
380 				adjust_i_size(trans, inum, src_offset, len) ?:
381 				bch2_logged_op_update(trans, &op->k_i));
382 		if (ret)
383 			goto err;
384 	} else {
385 		bch2_btree_iter_set_pos(&iter, POS(inum.inum, src_offset));
386 
387 		ret = bch2_fpunch_at(trans, &iter, inum, src_offset + len, i_sectors_delta);
388 		if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
389 			goto err;
390 
391 		ret = commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
392 				bch2_logged_op_update(trans, &op->k_i));
393 	}
394 
395 	fallthrough;
396 case LOGGED_OP_FINSERT_shift_extents:
397 	while (1) {
398 		struct disk_reservation disk_res =
399 			bch2_disk_reservation_init(c, 0);
400 		struct bkey_i delete, *copy;
401 		struct bkey_s_c k;
402 		struct bpos src_pos = POS(inum.inum, src_offset);
403 		u32 snapshot;
404 
405 		bch2_trans_begin(trans);
406 
407 		ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
408 		if (ret)
409 			goto btree_err;
410 
411 		bch2_btree_iter_set_snapshot(&iter, snapshot);
412 		bch2_btree_iter_set_pos(&iter, SPOS(inum.inum, pos, snapshot));
413 
414 		k = insert
415 			? bch2_btree_iter_peek_prev(&iter)
416 			: bch2_btree_iter_peek_upto(&iter, POS(inum.inum, U64_MAX));
417 		if ((ret = bkey_err(k)))
418 			goto btree_err;
419 
420 		if (!k.k ||
421 		    k.k->p.inode != inum.inum ||
422 		    bkey_le(k.k->p, POS(inum.inum, src_offset)))
423 			break;
424 
425 		copy = bch2_bkey_make_mut_noupdate(trans, k);
426 		if ((ret = PTR_ERR_OR_ZERO(copy)))
427 			goto btree_err;
428 
429 		if (insert &&
430 		    bkey_lt(bkey_start_pos(k.k), src_pos)) {
431 			bch2_cut_front(src_pos, copy);
432 
433 			/* Splitting compressed extent? */
434 			bch2_disk_reservation_add(c, &disk_res,
435 					copy->k.size *
436 					bch2_bkey_nr_ptrs_allocated(bkey_i_to_s_c(copy)),
437 					BCH_DISK_RESERVATION_NOFAIL);
438 		}
439 
440 		bkey_init(&delete.k);
441 		delete.k.p = copy->k.p;
442 		delete.k.p.snapshot = snapshot;
443 		delete.k.size = copy->k.size;
444 
445 		copy->k.p.offset += shift;
446 		copy->k.p.snapshot = snapshot;
447 
448 		op->v.pos = cpu_to_le64(insert ? bkey_start_offset(&delete.k) : delete.k.p.offset);
449 
450 		ret =   bch2_bkey_set_needs_rebalance(c, copy, &opts) ?:
451 			bch2_btree_insert_trans(trans, BTREE_ID_extents, &delete, 0) ?:
452 			bch2_btree_insert_trans(trans, BTREE_ID_extents, copy, 0) ?:
453 			bch2_logged_op_update(trans, &op->k_i) ?:
454 			bch2_trans_commit(trans, &disk_res, NULL, BCH_TRANS_COMMIT_no_enospc);
455 btree_err:
456 		bch2_disk_reservation_put(c, &disk_res);
457 
458 		if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
459 			continue;
460 		if (ret)
461 			goto err;
462 
463 		pos = le64_to_cpu(op->v.pos);
464 	}
465 
466 	op->v.state = LOGGED_OP_FINSERT_finish;
467 
468 	if (!insert) {
469 		ret = commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
470 				adjust_i_size(trans, inum, src_offset, shift) ?:
471 				bch2_logged_op_update(trans, &op->k_i));
472 	} else {
473 		/* We need an inode update to update bi_journal_seq for fsync: */
474 		ret = commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
475 				adjust_i_size(trans, inum, 0, 0) ?:
476 				bch2_logged_op_update(trans, &op->k_i));
477 	}
478 
479 	break;
480 case LOGGED_OP_FINSERT_finish:
481 	break;
482 	}
483 err:
484 	bch_err_fn(c, ret);
485 	bch2_logged_op_finish(trans, op_k);
486 	bch2_trans_iter_exit(trans, &iter);
487 	return ret;
488 }
489 
490 int bch2_resume_logged_op_finsert(struct btree_trans *trans, struct bkey_i *op_k)
491 {
492 	return __bch2_resume_logged_op_finsert(trans, op_k, NULL);
493 }
494 
495 int bch2_fcollapse_finsert(struct bch_fs *c, subvol_inum inum,
496 			   u64 offset, u64 len, bool insert,
497 			   s64 *i_sectors_delta)
498 {
499 	struct bkey_i_logged_op_finsert op;
500 	s64 shift = insert ? len : -len;
501 
502 	bkey_logged_op_finsert_init(&op.k_i);
503 	op.v.subvol	= cpu_to_le32(inum.subvol);
504 	op.v.inum	= cpu_to_le64(inum.inum);
505 	op.v.dst_offset	= cpu_to_le64(offset + shift);
506 	op.v.src_offset	= cpu_to_le64(offset);
507 	op.v.pos	= cpu_to_le64(insert ? U64_MAX : offset);
508 
509 	/*
510 	 * Logged ops aren't atomic w.r.t. snapshot creation: creating a
511 	 * snapshot while they're in progress, then crashing, will result in the
512 	 * resume only proceeding in one of the snapshots
513 	 */
514 	down_read(&c->snapshot_create_lock);
515 	int ret = bch2_trans_run(c,
516 		bch2_logged_op_start(trans, &op.k_i) ?:
517 		__bch2_resume_logged_op_finsert(trans, &op.k_i, i_sectors_delta));
518 	up_read(&c->snapshot_create_lock);
519 
520 	return ret;
521 }
522