xref: /linux/fs/bcachefs/io_misc.c (revision 72bea132f3680ee51e7ed2cee62892b6f5121909)
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(trans);
129 		closure_sync(&cl);
130 	}
131 
132 	return ret;
133 }
134 
135 /*
136  * Returns -BCH_ERR_transacton_restart if we had to drop locks:
137  */
138 int bch2_fpunch_at(struct btree_trans *trans, struct btree_iter *iter,
139 		   subvol_inum inum, u64 end,
140 		   s64 *i_sectors_delta)
141 {
142 	struct bch_fs *c	= trans->c;
143 	unsigned max_sectors	= KEY_SIZE_MAX & (~0 << c->block_bits);
144 	struct bpos end_pos = POS(inum.inum, end);
145 	struct bkey_s_c k;
146 	int ret = 0, ret2 = 0;
147 	u32 snapshot;
148 
149 	while (!ret ||
150 	       bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
151 		struct disk_reservation disk_res =
152 			bch2_disk_reservation_init(c, 0);
153 		struct bkey_i delete;
154 
155 		if (ret)
156 			ret2 = ret;
157 
158 		bch2_trans_begin(trans);
159 
160 		ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
161 		if (ret)
162 			continue;
163 
164 		bch2_btree_iter_set_snapshot(iter, snapshot);
165 
166 		/*
167 		 * peek_upto() doesn't have ideal semantics for extents:
168 		 */
169 		k = bch2_btree_iter_peek_upto(iter, end_pos);
170 		if (!k.k)
171 			break;
172 
173 		ret = bkey_err(k);
174 		if (ret)
175 			continue;
176 
177 		bkey_init(&delete.k);
178 		delete.k.p = iter->pos;
179 
180 		/* create the biggest key we can */
181 		bch2_key_resize(&delete.k, max_sectors);
182 		bch2_cut_back(end_pos, &delete);
183 
184 		ret = bch2_extent_update(trans, inum, iter, &delete,
185 				&disk_res, 0, i_sectors_delta, false);
186 		bch2_disk_reservation_put(c, &disk_res);
187 	}
188 
189 	return ret ?: ret2;
190 }
191 
192 int bch2_fpunch(struct bch_fs *c, subvol_inum inum, u64 start, u64 end,
193 		s64 *i_sectors_delta)
194 {
195 	struct btree_trans *trans = bch2_trans_get(c);
196 	struct btree_iter iter;
197 	int ret;
198 
199 	bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
200 			     POS(inum.inum, start),
201 			     BTREE_ITER_INTENT);
202 
203 	ret = bch2_fpunch_at(trans, &iter, inum, end, i_sectors_delta);
204 
205 	bch2_trans_iter_exit(trans, &iter);
206 	bch2_trans_put(trans);
207 
208 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
209 		ret = 0;
210 
211 	return ret;
212 }
213 
214 /* truncate: */
215 
216 void bch2_logged_op_truncate_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
217 {
218 	struct bkey_s_c_logged_op_truncate op = bkey_s_c_to_logged_op_truncate(k);
219 
220 	prt_printf(out, "subvol=%u", le32_to_cpu(op.v->subvol));
221 	prt_printf(out, " inum=%llu", le64_to_cpu(op.v->inum));
222 	prt_printf(out, " new_i_size=%llu", le64_to_cpu(op.v->new_i_size));
223 }
224 
225 static int truncate_set_isize(struct btree_trans *trans,
226 			      subvol_inum inum,
227 			      u64 new_i_size)
228 {
229 	struct btree_iter iter = { NULL };
230 	struct bch_inode_unpacked inode_u;
231 	int ret;
232 
233 	ret   = bch2_inode_peek(trans, &iter, &inode_u, inum, BTREE_ITER_INTENT) ?:
234 		(inode_u.bi_size = new_i_size, 0) ?:
235 		bch2_inode_write(trans, &iter, &inode_u);
236 
237 	bch2_trans_iter_exit(trans, &iter);
238 	return ret;
239 }
240 
241 static int __bch2_resume_logged_op_truncate(struct btree_trans *trans,
242 					    struct bkey_i *op_k,
243 					    u64 *i_sectors_delta)
244 {
245 	struct bch_fs *c = trans->c;
246 	struct btree_iter fpunch_iter;
247 	struct bkey_i_logged_op_truncate *op = bkey_i_to_logged_op_truncate(op_k);
248 	subvol_inum inum = { le32_to_cpu(op->v.subvol), le64_to_cpu(op->v.inum) };
249 	u64 new_i_size = le64_to_cpu(op->v.new_i_size);
250 	int ret;
251 
252 	ret = commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
253 			truncate_set_isize(trans, inum, new_i_size));
254 	if (ret)
255 		goto err;
256 
257 	bch2_trans_iter_init(trans, &fpunch_iter, BTREE_ID_extents,
258 			     POS(inum.inum, round_up(new_i_size, block_bytes(c)) >> 9),
259 			     BTREE_ITER_INTENT);
260 	ret = bch2_fpunch_at(trans, &fpunch_iter, inum, U64_MAX, i_sectors_delta);
261 	bch2_trans_iter_exit(trans, &fpunch_iter);
262 
263 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
264 		ret = 0;
265 err:
266 	bch2_logged_op_finish(trans, op_k);
267 	return ret;
268 }
269 
270 int bch2_resume_logged_op_truncate(struct btree_trans *trans, struct bkey_i *op_k)
271 {
272 	return __bch2_resume_logged_op_truncate(trans, op_k, NULL);
273 }
274 
275 int bch2_truncate(struct bch_fs *c, subvol_inum inum, u64 new_i_size, u64 *i_sectors_delta)
276 {
277 	struct bkey_i_logged_op_truncate op;
278 
279 	bkey_logged_op_truncate_init(&op.k_i);
280 	op.v.subvol	= cpu_to_le32(inum.subvol);
281 	op.v.inum	= cpu_to_le64(inum.inum);
282 	op.v.new_i_size	= cpu_to_le64(new_i_size);
283 
284 	/*
285 	 * Logged ops aren't atomic w.r.t. snapshot creation: creating a
286 	 * snapshot while they're in progress, then crashing, will result in the
287 	 * resume only proceeding in one of the snapshots
288 	 */
289 	down_read(&c->snapshot_create_lock);
290 	int ret = bch2_trans_run(c,
291 		bch2_logged_op_start(trans, &op.k_i) ?:
292 		__bch2_resume_logged_op_truncate(trans, &op.k_i, i_sectors_delta));
293 	up_read(&c->snapshot_create_lock);
294 
295 	return ret;
296 }
297 
298 /* finsert/fcollapse: */
299 
300 void bch2_logged_op_finsert_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
301 {
302 	struct bkey_s_c_logged_op_finsert op = bkey_s_c_to_logged_op_finsert(k);
303 
304 	prt_printf(out, "subvol=%u",		le32_to_cpu(op.v->subvol));
305 	prt_printf(out, " inum=%llu",		le64_to_cpu(op.v->inum));
306 	prt_printf(out, " dst_offset=%lli",	le64_to_cpu(op.v->dst_offset));
307 	prt_printf(out, " src_offset=%llu",	le64_to_cpu(op.v->src_offset));
308 }
309 
310 static int adjust_i_size(struct btree_trans *trans, subvol_inum inum, u64 offset, s64 len)
311 {
312 	struct btree_iter iter;
313 	struct bch_inode_unpacked inode_u;
314 	int ret;
315 
316 	offset	<<= 9;
317 	len	<<= 9;
318 
319 	ret = bch2_inode_peek(trans, &iter, &inode_u, inum, BTREE_ITER_INTENT);
320 	if (ret)
321 		return ret;
322 
323 	if (len > 0) {
324 		if (MAX_LFS_FILESIZE - inode_u.bi_size < len) {
325 			ret = -EFBIG;
326 			goto err;
327 		}
328 
329 		if (offset >= inode_u.bi_size) {
330 			ret = -EINVAL;
331 			goto err;
332 		}
333 	}
334 
335 	inode_u.bi_size += len;
336 	inode_u.bi_mtime = inode_u.bi_ctime = bch2_current_time(trans->c);
337 
338 	ret = bch2_inode_write(trans, &iter, &inode_u);
339 err:
340 	bch2_trans_iter_exit(trans, &iter);
341 	return ret;
342 }
343 
344 static int __bch2_resume_logged_op_finsert(struct btree_trans *trans,
345 					   struct bkey_i *op_k,
346 					   u64 *i_sectors_delta)
347 {
348 	struct bch_fs *c = trans->c;
349 	struct btree_iter iter;
350 	struct bkey_i_logged_op_finsert *op = bkey_i_to_logged_op_finsert(op_k);
351 	subvol_inum inum = { le32_to_cpu(op->v.subvol), le64_to_cpu(op->v.inum) };
352 	struct bch_io_opts opts;
353 	u64 dst_offset = le64_to_cpu(op->v.dst_offset);
354 	u64 src_offset = le64_to_cpu(op->v.src_offset);
355 	s64 shift = dst_offset - src_offset;
356 	u64 len = abs(shift);
357 	u64 pos = le64_to_cpu(op->v.pos);
358 	bool insert = shift > 0;
359 	int ret = 0;
360 
361 	ret = bch2_inum_opts_get(trans, inum, &opts);
362 	if (ret)
363 		return ret;
364 
365 	bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
366 			     POS(inum.inum, 0),
367 			     BTREE_ITER_INTENT);
368 
369 	switch (op->v.state) {
370 case LOGGED_OP_FINSERT_start:
371 	op->v.state = LOGGED_OP_FINSERT_shift_extents;
372 
373 	if (insert) {
374 		ret = commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
375 				adjust_i_size(trans, inum, src_offset, len) ?:
376 				bch2_logged_op_update(trans, &op->k_i));
377 		if (ret)
378 			goto err;
379 	} else {
380 		bch2_btree_iter_set_pos(&iter, POS(inum.inum, src_offset));
381 
382 		ret = bch2_fpunch_at(trans, &iter, inum, src_offset + len, i_sectors_delta);
383 		if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
384 			goto err;
385 
386 		ret = commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
387 				bch2_logged_op_update(trans, &op->k_i));
388 	}
389 
390 	fallthrough;
391 case LOGGED_OP_FINSERT_shift_extents:
392 	while (1) {
393 		struct disk_reservation disk_res =
394 			bch2_disk_reservation_init(c, 0);
395 		struct bkey_i delete, *copy;
396 		struct bkey_s_c k;
397 		struct bpos src_pos = POS(inum.inum, src_offset);
398 		u32 snapshot;
399 
400 		bch2_trans_begin(trans);
401 
402 		ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
403 		if (ret)
404 			goto btree_err;
405 
406 		bch2_btree_iter_set_snapshot(&iter, snapshot);
407 		bch2_btree_iter_set_pos(&iter, SPOS(inum.inum, pos, snapshot));
408 
409 		k = insert
410 			? bch2_btree_iter_peek_prev(&iter)
411 			: bch2_btree_iter_peek_upto(&iter, POS(inum.inum, U64_MAX));
412 		if ((ret = bkey_err(k)))
413 			goto btree_err;
414 
415 		if (!k.k ||
416 		    k.k->p.inode != inum.inum ||
417 		    bkey_le(k.k->p, POS(inum.inum, src_offset)))
418 			break;
419 
420 		copy = bch2_bkey_make_mut_noupdate(trans, k);
421 		if ((ret = PTR_ERR_OR_ZERO(copy)))
422 			goto btree_err;
423 
424 		if (insert &&
425 		    bkey_lt(bkey_start_pos(k.k), src_pos)) {
426 			bch2_cut_front(src_pos, copy);
427 
428 			/* Splitting compressed extent? */
429 			bch2_disk_reservation_add(c, &disk_res,
430 					copy->k.size *
431 					bch2_bkey_nr_ptrs_allocated(bkey_i_to_s_c(copy)),
432 					BCH_DISK_RESERVATION_NOFAIL);
433 		}
434 
435 		bkey_init(&delete.k);
436 		delete.k.p = copy->k.p;
437 		delete.k.p.snapshot = snapshot;
438 		delete.k.size = copy->k.size;
439 
440 		copy->k.p.offset += shift;
441 		copy->k.p.snapshot = snapshot;
442 
443 		op->v.pos = cpu_to_le64(insert ? bkey_start_offset(&delete.k) : delete.k.p.offset);
444 
445 		ret =   bch2_bkey_set_needs_rebalance(c, copy, &opts) ?:
446 			bch2_btree_insert_trans(trans, BTREE_ID_extents, &delete, 0) ?:
447 			bch2_btree_insert_trans(trans, BTREE_ID_extents, copy, 0) ?:
448 			bch2_logged_op_update(trans, &op->k_i) ?:
449 			bch2_trans_commit(trans, &disk_res, NULL, BCH_TRANS_COMMIT_no_enospc);
450 btree_err:
451 		bch2_disk_reservation_put(c, &disk_res);
452 
453 		if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
454 			continue;
455 		if (ret)
456 			goto err;
457 
458 		pos = le64_to_cpu(op->v.pos);
459 	}
460 
461 	op->v.state = LOGGED_OP_FINSERT_finish;
462 
463 	if (!insert) {
464 		ret = commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
465 				adjust_i_size(trans, inum, src_offset, shift) ?:
466 				bch2_logged_op_update(trans, &op->k_i));
467 	} else {
468 		/* We need an inode update to update bi_journal_seq for fsync: */
469 		ret = commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
470 				adjust_i_size(trans, inum, 0, 0) ?:
471 				bch2_logged_op_update(trans, &op->k_i));
472 	}
473 
474 	break;
475 case LOGGED_OP_FINSERT_finish:
476 	break;
477 	}
478 err:
479 	bch2_logged_op_finish(trans, op_k);
480 	bch2_trans_iter_exit(trans, &iter);
481 	return ret;
482 }
483 
484 int bch2_resume_logged_op_finsert(struct btree_trans *trans, struct bkey_i *op_k)
485 {
486 	return __bch2_resume_logged_op_finsert(trans, op_k, NULL);
487 }
488 
489 int bch2_fcollapse_finsert(struct bch_fs *c, subvol_inum inum,
490 			   u64 offset, u64 len, bool insert,
491 			   s64 *i_sectors_delta)
492 {
493 	struct bkey_i_logged_op_finsert op;
494 	s64 shift = insert ? len : -len;
495 
496 	bkey_logged_op_finsert_init(&op.k_i);
497 	op.v.subvol	= cpu_to_le32(inum.subvol);
498 	op.v.inum	= cpu_to_le64(inum.inum);
499 	op.v.dst_offset	= cpu_to_le64(offset + shift);
500 	op.v.src_offset	= cpu_to_le64(offset);
501 	op.v.pos	= cpu_to_le64(insert ? U64_MAX : offset);
502 
503 	/*
504 	 * Logged ops aren't atomic w.r.t. snapshot creation: creating a
505 	 * snapshot while they're in progress, then crashing, will result in the
506 	 * resume only proceeding in one of the snapshots
507 	 */
508 	down_read(&c->snapshot_create_lock);
509 	int ret = bch2_trans_run(c,
510 		bch2_logged_op_start(trans, &op.k_i) ?:
511 		__bch2_resume_logged_op_finsert(trans, &op.k_i, i_sectors_delta));
512 	up_read(&c->snapshot_create_lock);
513 
514 	return ret;
515 }
516