1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
4 * Copyright 2012 Google, Inc.
5 */
6
7#include "bcachefs.h"
8#include "alloc_foreground.h"
9#include "bkey_buf.h"
10#include "bset.h"
11#include "btree_update.h"
12#include "buckets.h"
13#include "checksum.h"
14#include "clock.h"
15#include "compress.h"
16#include "debug.h"
17#include "ec.h"
18#include "error.h"
19#include "extent_update.h"
20#include "inode.h"
21#include "io_write.h"
22#include "journal.h"
23#include "keylist.h"
24#include "move.h"
25#include "nocow_locking.h"
26#include "rebalance.h"
27#include "subvolume.h"
28#include "super.h"
29#include "super-io.h"
30#include "trace.h"
31
32#include <linux/blkdev.h>
33#include <linux/prefetch.h>
34#include <linux/random.h>
35#include <linux/sched/mm.h>
36
37#ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT
38
39static inline void bch2_congested_acct(struct bch_dev *ca, u64 io_latency,
40				       u64 now, int rw)
41{
42	u64 latency_capable =
43		ca->io_latency[rw].quantiles.entries[QUANTILE_IDX(1)].m;
44	/* ideally we'd be taking into account the device's variance here: */
45	u64 latency_threshold = latency_capable << (rw == READ ? 2 : 3);
46	s64 latency_over = io_latency - latency_threshold;
47
48	if (latency_threshold && latency_over > 0) {
49		/*
50		 * bump up congested by approximately latency_over * 4 /
51		 * latency_threshold - we don't need much accuracy here so don't
52		 * bother with the divide:
53		 */
54		if (atomic_read(&ca->congested) < CONGESTED_MAX)
55			atomic_add(latency_over >>
56				   max_t(int, ilog2(latency_threshold) - 2, 0),
57				   &ca->congested);
58
59		ca->congested_last = now;
60	} else if (atomic_read(&ca->congested) > 0) {
61		atomic_dec(&ca->congested);
62	}
63}
64
65void bch2_latency_acct(struct bch_dev *ca, u64 submit_time, int rw)
66{
67	atomic64_t *latency = &ca->cur_latency[rw];
68	u64 now = local_clock();
69	u64 io_latency = time_after64(now, submit_time)
70		? now - submit_time
71		: 0;
72	u64 old, new, v = atomic64_read(latency);
73
74	do {
75		old = v;
76
77		/*
78		 * If the io latency was reasonably close to the current
79		 * latency, skip doing the update and atomic operation - most of
80		 * the time:
81		 */
82		if (abs((int) (old - io_latency)) < (old >> 1) &&
83		    now & ~(~0U << 5))
84			break;
85
86		new = ewma_add(old, io_latency, 5);
87	} while ((v = atomic64_cmpxchg(latency, old, new)) != old);
88
89	bch2_congested_acct(ca, io_latency, now, rw);
90
91	__bch2_time_stats_update(&ca->io_latency[rw].stats, submit_time, now);
92}
93
94#endif
95
96/* Allocate, free from mempool: */
97
98void bch2_bio_free_pages_pool(struct bch_fs *c, struct bio *bio)
99{
100	struct bvec_iter_all iter;
101	struct bio_vec *bv;
102
103	bio_for_each_segment_all(bv, bio, iter)
104		if (bv->bv_page != ZERO_PAGE(0))
105			mempool_free(bv->bv_page, &c->bio_bounce_pages);
106	bio->bi_vcnt = 0;
107}
108
109static struct page *__bio_alloc_page_pool(struct bch_fs *c, bool *using_mempool)
110{
111	struct page *page;
112
113	if (likely(!*using_mempool)) {
114		page = alloc_page(GFP_NOFS);
115		if (unlikely(!page)) {
116			mutex_lock(&c->bio_bounce_pages_lock);
117			*using_mempool = true;
118			goto pool_alloc;
119
120		}
121	} else {
122pool_alloc:
123		page = mempool_alloc(&c->bio_bounce_pages, GFP_NOFS);
124	}
125
126	return page;
127}
128
129void bch2_bio_alloc_pages_pool(struct bch_fs *c, struct bio *bio,
130			       size_t size)
131{
132	bool using_mempool = false;
133
134	while (size) {
135		struct page *page = __bio_alloc_page_pool(c, &using_mempool);
136		unsigned len = min_t(size_t, PAGE_SIZE, size);
137
138		BUG_ON(!bio_add_page(bio, page, len, 0));
139		size -= len;
140	}
141
142	if (using_mempool)
143		mutex_unlock(&c->bio_bounce_pages_lock);
144}
145
146/* Extent update path: */
147
148int bch2_sum_sector_overwrites(struct btree_trans *trans,
149			       struct btree_iter *extent_iter,
150			       struct bkey_i *new,
151			       bool *usage_increasing,
152			       s64 *i_sectors_delta,
153			       s64 *disk_sectors_delta)
154{
155	struct bch_fs *c = trans->c;
156	struct btree_iter iter;
157	struct bkey_s_c old;
158	unsigned new_replicas = bch2_bkey_replicas(c, bkey_i_to_s_c(new));
159	bool new_compressed = bch2_bkey_sectors_compressed(bkey_i_to_s_c(new));
160	int ret = 0;
161
162	*usage_increasing	= false;
163	*i_sectors_delta	= 0;
164	*disk_sectors_delta	= 0;
165
166	bch2_trans_copy_iter(&iter, extent_iter);
167
168	for_each_btree_key_upto_continue_norestart(iter,
169				new->k.p, BTREE_ITER_slots, old, ret) {
170		s64 sectors = min(new->k.p.offset, old.k->p.offset) -
171			max(bkey_start_offset(&new->k),
172			    bkey_start_offset(old.k));
173
174		*i_sectors_delta += sectors *
175			(bkey_extent_is_allocation(&new->k) -
176			 bkey_extent_is_allocation(old.k));
177
178		*disk_sectors_delta += sectors * bch2_bkey_nr_ptrs_allocated(bkey_i_to_s_c(new));
179		*disk_sectors_delta -= new->k.p.snapshot == old.k->p.snapshot
180			? sectors * bch2_bkey_nr_ptrs_fully_allocated(old)
181			: 0;
182
183		if (!*usage_increasing &&
184		    (new->k.p.snapshot != old.k->p.snapshot ||
185		     new_replicas > bch2_bkey_replicas(c, old) ||
186		     (!new_compressed && bch2_bkey_sectors_compressed(old))))
187			*usage_increasing = true;
188
189		if (bkey_ge(old.k->p, new->k.p))
190			break;
191	}
192
193	bch2_trans_iter_exit(trans, &iter);
194	return ret;
195}
196
197static inline int bch2_extent_update_i_size_sectors(struct btree_trans *trans,
198						    struct btree_iter *extent_iter,
199						    u64 new_i_size,
200						    s64 i_sectors_delta)
201{
202	/*
203	 * Crazy performance optimization:
204	 * Every extent update needs to also update the inode: the inode trigger
205	 * will set bi->journal_seq to the journal sequence number of this
206	 * transaction - for fsync.
207	 *
208	 * But if that's the only reason we're updating the inode (we're not
209	 * updating bi_size or bi_sectors), then we don't need the inode update
210	 * to be journalled - if we crash, the bi_journal_seq update will be
211	 * lost, but that's fine.
212	 */
213	unsigned inode_update_flags = BTREE_UPDATE_nojournal;
214
215	struct btree_iter iter;
216	struct bkey_s_c k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
217			      SPOS(0,
218				   extent_iter->pos.inode,
219				   extent_iter->snapshot),
220			      BTREE_ITER_cached);
221	int ret = bkey_err(k);
222	if (unlikely(ret))
223		return ret;
224
225	/*
226	 * varint_decode_fast(), in the inode .invalid method, reads up to 7
227	 * bytes past the end of the buffer:
228	 */
229	struct bkey_i *k_mut = bch2_trans_kmalloc_nomemzero(trans, bkey_bytes(k.k) + 8);
230	ret = PTR_ERR_OR_ZERO(k_mut);
231	if (unlikely(ret))
232		goto err;
233
234	bkey_reassemble(k_mut, k);
235
236	if (unlikely(k_mut->k.type != KEY_TYPE_inode_v3)) {
237		k_mut = bch2_inode_to_v3(trans, k_mut);
238		ret = PTR_ERR_OR_ZERO(k_mut);
239		if (unlikely(ret))
240			goto err;
241	}
242
243	struct bkey_i_inode_v3 *inode = bkey_i_to_inode_v3(k_mut);
244
245	if (!(le64_to_cpu(inode->v.bi_flags) & BCH_INODE_i_size_dirty) &&
246	    new_i_size > le64_to_cpu(inode->v.bi_size)) {
247		inode->v.bi_size = cpu_to_le64(new_i_size);
248		inode_update_flags = 0;
249	}
250
251	if (i_sectors_delta) {
252		le64_add_cpu(&inode->v.bi_sectors, i_sectors_delta);
253		inode_update_flags = 0;
254	}
255
256	if (inode->k.p.snapshot != iter.snapshot) {
257		inode->k.p.snapshot = iter.snapshot;
258		inode_update_flags = 0;
259	}
260
261	ret = bch2_trans_update(trans, &iter, &inode->k_i,
262				BTREE_UPDATE_internal_snapshot_node|
263				inode_update_flags);
264err:
265	bch2_trans_iter_exit(trans, &iter);
266	return ret;
267}
268
269int bch2_extent_update(struct btree_trans *trans,
270		       subvol_inum inum,
271		       struct btree_iter *iter,
272		       struct bkey_i *k,
273		       struct disk_reservation *disk_res,
274		       u64 new_i_size,
275		       s64 *i_sectors_delta_total,
276		       bool check_enospc)
277{
278	struct bpos next_pos;
279	bool usage_increasing;
280	s64 i_sectors_delta = 0, disk_sectors_delta = 0;
281	int ret;
282
283	/*
284	 * This traverses us the iterator without changing iter->path->pos to
285	 * search_key() (which is pos + 1 for extents): we want there to be a
286	 * path already traversed at iter->pos because
287	 * bch2_trans_extent_update() will use it to attempt extent merging
288	 */
289	ret = __bch2_btree_iter_traverse(iter);
290	if (ret)
291		return ret;
292
293	ret = bch2_extent_trim_atomic(trans, iter, k);
294	if (ret)
295		return ret;
296
297	next_pos = k->k.p;
298
299	ret = bch2_sum_sector_overwrites(trans, iter, k,
300			&usage_increasing,
301			&i_sectors_delta,
302			&disk_sectors_delta);
303	if (ret)
304		return ret;
305
306	if (disk_res &&
307	    disk_sectors_delta > (s64) disk_res->sectors) {
308		ret = bch2_disk_reservation_add(trans->c, disk_res,
309					disk_sectors_delta - disk_res->sectors,
310					!check_enospc || !usage_increasing
311					? BCH_DISK_RESERVATION_NOFAIL : 0);
312		if (ret)
313			return ret;
314	}
315
316	/*
317	 * Note:
318	 * We always have to do an inode update - even when i_size/i_sectors
319	 * aren't changing - for fsync to work properly; fsync relies on
320	 * inode->bi_journal_seq which is updated by the trigger code:
321	 */
322	ret =   bch2_extent_update_i_size_sectors(trans, iter,
323						  min(k->k.p.offset << 9, new_i_size),
324						  i_sectors_delta) ?:
325		bch2_trans_update(trans, iter, k, 0) ?:
326		bch2_trans_commit(trans, disk_res, NULL,
327				BCH_TRANS_COMMIT_no_check_rw|
328				BCH_TRANS_COMMIT_no_enospc);
329	if (unlikely(ret))
330		return ret;
331
332	if (i_sectors_delta_total)
333		*i_sectors_delta_total += i_sectors_delta;
334	bch2_btree_iter_set_pos(iter, next_pos);
335	return 0;
336}
337
338static int bch2_write_index_default(struct bch_write_op *op)
339{
340	struct bch_fs *c = op->c;
341	struct bkey_buf sk;
342	struct keylist *keys = &op->insert_keys;
343	struct bkey_i *k = bch2_keylist_front(keys);
344	struct btree_trans *trans = bch2_trans_get(c);
345	struct btree_iter iter;
346	subvol_inum inum = {
347		.subvol = op->subvol,
348		.inum	= k->k.p.inode,
349	};
350	int ret;
351
352	BUG_ON(!inum.subvol);
353
354	bch2_bkey_buf_init(&sk);
355
356	do {
357		bch2_trans_begin(trans);
358
359		k = bch2_keylist_front(keys);
360		bch2_bkey_buf_copy(&sk, c, k);
361
362		ret = bch2_subvolume_get_snapshot(trans, inum.subvol,
363						  &sk.k->k.p.snapshot);
364		if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
365			continue;
366		if (ret)
367			break;
368
369		bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
370				     bkey_start_pos(&sk.k->k),
371				     BTREE_ITER_slots|BTREE_ITER_intent);
372
373		ret =   bch2_bkey_set_needs_rebalance(c, sk.k, &op->opts) ?:
374			bch2_extent_update(trans, inum, &iter, sk.k,
375					&op->res,
376					op->new_i_size, &op->i_sectors_delta,
377					op->flags & BCH_WRITE_CHECK_ENOSPC);
378		bch2_trans_iter_exit(trans, &iter);
379
380		if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
381			continue;
382		if (ret)
383			break;
384
385		if (bkey_ge(iter.pos, k->k.p))
386			bch2_keylist_pop_front(&op->insert_keys);
387		else
388			bch2_cut_front(iter.pos, k);
389	} while (!bch2_keylist_empty(keys));
390
391	bch2_trans_put(trans);
392	bch2_bkey_buf_exit(&sk, c);
393
394	return ret;
395}
396
397/* Writes */
398
399void bch2_submit_wbio_replicas(struct bch_write_bio *wbio, struct bch_fs *c,
400			       enum bch_data_type type,
401			       const struct bkey_i *k,
402			       bool nocow)
403{
404	struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(bkey_i_to_s_c(k));
405	struct bch_write_bio *n;
406
407	BUG_ON(c->opts.nochanges);
408
409	bkey_for_each_ptr(ptrs, ptr) {
410		struct bch_dev *ca = nocow
411			? bch2_dev_have_ref(c, ptr->dev)
412			: bch2_dev_get_ioref(c, ptr->dev, type == BCH_DATA_btree ? READ : WRITE);
413
414		if (to_entry(ptr + 1) < ptrs.end) {
415			n = to_wbio(bio_alloc_clone(NULL, &wbio->bio, GFP_NOFS, &c->replica_set));
416
417			n->bio.bi_end_io	= wbio->bio.bi_end_io;
418			n->bio.bi_private	= wbio->bio.bi_private;
419			n->parent		= wbio;
420			n->split		= true;
421			n->bounce		= false;
422			n->put_bio		= true;
423			n->bio.bi_opf		= wbio->bio.bi_opf;
424			bio_inc_remaining(&wbio->bio);
425		} else {
426			n = wbio;
427			n->split		= false;
428		}
429
430		n->c			= c;
431		n->dev			= ptr->dev;
432		n->have_ioref		= ca != NULL;
433		n->nocow		= nocow;
434		n->submit_time		= local_clock();
435		n->inode_offset		= bkey_start_offset(&k->k);
436		if (nocow)
437			n->nocow_bucket	= PTR_BUCKET_NR(ca, ptr);
438		n->bio.bi_iter.bi_sector = ptr->offset;
439
440		if (likely(n->have_ioref)) {
441			this_cpu_add(ca->io_done->sectors[WRITE][type],
442				     bio_sectors(&n->bio));
443
444			bio_set_dev(&n->bio, ca->disk_sb.bdev);
445
446			if (type != BCH_DATA_btree && unlikely(c->opts.no_data_io)) {
447				bio_endio(&n->bio);
448				continue;
449			}
450
451			submit_bio(&n->bio);
452		} else {
453			n->bio.bi_status	= BLK_STS_REMOVED;
454			bio_endio(&n->bio);
455		}
456	}
457}
458
459static void __bch2_write(struct bch_write_op *);
460
461static void bch2_write_done(struct closure *cl)
462{
463	struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
464	struct bch_fs *c = op->c;
465
466	EBUG_ON(op->open_buckets.nr);
467
468	bch2_time_stats_update(&c->times[BCH_TIME_data_write], op->start_time);
469	bch2_disk_reservation_put(c, &op->res);
470
471	if (!(op->flags & BCH_WRITE_MOVE))
472		bch2_write_ref_put(c, BCH_WRITE_REF_write);
473	bch2_keylist_free(&op->insert_keys, op->inline_keys);
474
475	EBUG_ON(cl->parent);
476	closure_debug_destroy(cl);
477	if (op->end_io)
478		op->end_io(op);
479}
480
481static noinline int bch2_write_drop_io_error_ptrs(struct bch_write_op *op)
482{
483	struct keylist *keys = &op->insert_keys;
484	struct bkey_i *src, *dst = keys->keys, *n;
485
486	for (src = keys->keys; src != keys->top; src = n) {
487		n = bkey_next(src);
488
489		if (bkey_extent_is_direct_data(&src->k)) {
490			bch2_bkey_drop_ptrs(bkey_i_to_s(src), ptr,
491					    test_bit(ptr->dev, op->failed.d));
492
493			if (!bch2_bkey_nr_ptrs(bkey_i_to_s_c(src)))
494				return -EIO;
495		}
496
497		if (dst != src)
498			memmove_u64s_down(dst, src, src->k.u64s);
499		dst = bkey_next(dst);
500	}
501
502	keys->top = dst;
503	return 0;
504}
505
506/**
507 * __bch2_write_index - after a write, update index to point to new data
508 * @op:		bch_write_op to process
509 */
510static void __bch2_write_index(struct bch_write_op *op)
511{
512	struct bch_fs *c = op->c;
513	struct keylist *keys = &op->insert_keys;
514	unsigned dev;
515	int ret = 0;
516
517	if (unlikely(op->flags & BCH_WRITE_IO_ERROR)) {
518		ret = bch2_write_drop_io_error_ptrs(op);
519		if (ret)
520			goto err;
521	}
522
523	if (!bch2_keylist_empty(keys)) {
524		u64 sectors_start = keylist_sectors(keys);
525
526		ret = !(op->flags & BCH_WRITE_MOVE)
527			? bch2_write_index_default(op)
528			: bch2_data_update_index_update(op);
529
530		BUG_ON(bch2_err_matches(ret, BCH_ERR_transaction_restart));
531		BUG_ON(keylist_sectors(keys) && !ret);
532
533		op->written += sectors_start - keylist_sectors(keys);
534
535		if (ret && !bch2_err_matches(ret, EROFS)) {
536			struct bkey_i *insert = bch2_keylist_front(&op->insert_keys);
537
538			bch_err_inum_offset_ratelimited(c,
539				insert->k.p.inode, insert->k.p.offset << 9,
540				"%s write error while doing btree update: %s",
541				op->flags & BCH_WRITE_MOVE ? "move" : "user",
542				bch2_err_str(ret));
543		}
544
545		if (ret)
546			goto err;
547	}
548out:
549	/* If some a bucket wasn't written, we can't erasure code it: */
550	for_each_set_bit(dev, op->failed.d, BCH_SB_MEMBERS_MAX)
551		bch2_open_bucket_write_error(c, &op->open_buckets, dev);
552
553	bch2_open_buckets_put(c, &op->open_buckets);
554	return;
555err:
556	keys->top = keys->keys;
557	op->error = ret;
558	op->flags |= BCH_WRITE_DONE;
559	goto out;
560}
561
562static inline void __wp_update_state(struct write_point *wp, enum write_point_state state)
563{
564	if (state != wp->state) {
565		u64 now = ktime_get_ns();
566
567		if (wp->last_state_change &&
568		    time_after64(now, wp->last_state_change))
569			wp->time[wp->state] += now - wp->last_state_change;
570		wp->state = state;
571		wp->last_state_change = now;
572	}
573}
574
575static inline void wp_update_state(struct write_point *wp, bool running)
576{
577	enum write_point_state state;
578
579	state = running			 ? WRITE_POINT_running :
580		!list_empty(&wp->writes) ? WRITE_POINT_waiting_io
581					 : WRITE_POINT_stopped;
582
583	__wp_update_state(wp, state);
584}
585
586static CLOSURE_CALLBACK(bch2_write_index)
587{
588	closure_type(op, struct bch_write_op, cl);
589	struct write_point *wp = op->wp;
590	struct workqueue_struct *wq = index_update_wq(op);
591	unsigned long flags;
592
593	if ((op->flags & BCH_WRITE_DONE) &&
594	    (op->flags & BCH_WRITE_MOVE))
595		bch2_bio_free_pages_pool(op->c, &op->wbio.bio);
596
597	spin_lock_irqsave(&wp->writes_lock, flags);
598	if (wp->state == WRITE_POINT_waiting_io)
599		__wp_update_state(wp, WRITE_POINT_waiting_work);
600	list_add_tail(&op->wp_list, &wp->writes);
601	spin_unlock_irqrestore (&wp->writes_lock, flags);
602
603	queue_work(wq, &wp->index_update_work);
604}
605
606static inline void bch2_write_queue(struct bch_write_op *op, struct write_point *wp)
607{
608	op->wp = wp;
609
610	if (wp->state == WRITE_POINT_stopped) {
611		spin_lock_irq(&wp->writes_lock);
612		__wp_update_state(wp, WRITE_POINT_waiting_io);
613		spin_unlock_irq(&wp->writes_lock);
614	}
615}
616
617void bch2_write_point_do_index_updates(struct work_struct *work)
618{
619	struct write_point *wp =
620		container_of(work, struct write_point, index_update_work);
621	struct bch_write_op *op;
622
623	while (1) {
624		spin_lock_irq(&wp->writes_lock);
625		op = list_first_entry_or_null(&wp->writes, struct bch_write_op, wp_list);
626		if (op)
627			list_del(&op->wp_list);
628		wp_update_state(wp, op != NULL);
629		spin_unlock_irq(&wp->writes_lock);
630
631		if (!op)
632			break;
633
634		op->flags |= BCH_WRITE_IN_WORKER;
635
636		__bch2_write_index(op);
637
638		if (!(op->flags & BCH_WRITE_DONE))
639			__bch2_write(op);
640		else
641			bch2_write_done(&op->cl);
642	}
643}
644
645static void bch2_write_endio(struct bio *bio)
646{
647	struct closure *cl		= bio->bi_private;
648	struct bch_write_op *op		= container_of(cl, struct bch_write_op, cl);
649	struct bch_write_bio *wbio	= to_wbio(bio);
650	struct bch_write_bio *parent	= wbio->split ? wbio->parent : NULL;
651	struct bch_fs *c		= wbio->c;
652	struct bch_dev *ca		= wbio->have_ioref
653		? bch2_dev_have_ref(c, wbio->dev)
654		: NULL;
655
656	if (bch2_dev_inum_io_err_on(bio->bi_status, ca, BCH_MEMBER_ERROR_write,
657				    op->pos.inode,
658				    wbio->inode_offset << 9,
659				    "data write error: %s",
660				    bch2_blk_status_to_str(bio->bi_status))) {
661		set_bit(wbio->dev, op->failed.d);
662		op->flags |= BCH_WRITE_IO_ERROR;
663	}
664
665	if (wbio->nocow) {
666		bch2_bucket_nocow_unlock(&c->nocow_locks,
667					 POS(ca->dev_idx, wbio->nocow_bucket),
668					 BUCKET_NOCOW_LOCK_UPDATE);
669		set_bit(wbio->dev, op->devs_need_flush->d);
670	}
671
672	if (wbio->have_ioref) {
673		bch2_latency_acct(ca, wbio->submit_time, WRITE);
674		percpu_ref_put(&ca->io_ref);
675	}
676
677	if (wbio->bounce)
678		bch2_bio_free_pages_pool(c, bio);
679
680	if (wbio->put_bio)
681		bio_put(bio);
682
683	if (parent)
684		bio_endio(&parent->bio);
685	else
686		closure_put(cl);
687}
688
689static void init_append_extent(struct bch_write_op *op,
690			       struct write_point *wp,
691			       struct bversion version,
692			       struct bch_extent_crc_unpacked crc)
693{
694	struct bkey_i_extent *e;
695
696	op->pos.offset += crc.uncompressed_size;
697
698	e = bkey_extent_init(op->insert_keys.top);
699	e->k.p		= op->pos;
700	e->k.size	= crc.uncompressed_size;
701	e->k.version	= version;
702
703	if (crc.csum_type ||
704	    crc.compression_type ||
705	    crc.nonce)
706		bch2_extent_crc_append(&e->k_i, crc);
707
708	bch2_alloc_sectors_append_ptrs_inlined(op->c, wp, &e->k_i, crc.compressed_size,
709				       op->flags & BCH_WRITE_CACHED);
710
711	bch2_keylist_push(&op->insert_keys);
712}
713
714static struct bio *bch2_write_bio_alloc(struct bch_fs *c,
715					struct write_point *wp,
716					struct bio *src,
717					bool *page_alloc_failed,
718					void *buf)
719{
720	struct bch_write_bio *wbio;
721	struct bio *bio;
722	unsigned output_available =
723		min(wp->sectors_free << 9, src->bi_iter.bi_size);
724	unsigned pages = DIV_ROUND_UP(output_available +
725				      (buf
726				       ? ((unsigned long) buf & (PAGE_SIZE - 1))
727				       : 0), PAGE_SIZE);
728
729	pages = min(pages, BIO_MAX_VECS);
730
731	bio = bio_alloc_bioset(NULL, pages, 0,
732			       GFP_NOFS, &c->bio_write);
733	wbio			= wbio_init(bio);
734	wbio->put_bio		= true;
735	/* copy WRITE_SYNC flag */
736	wbio->bio.bi_opf	= src->bi_opf;
737
738	if (buf) {
739		bch2_bio_map(bio, buf, output_available);
740		return bio;
741	}
742
743	wbio->bounce		= true;
744
745	/*
746	 * We can't use mempool for more than c->sb.encoded_extent_max
747	 * worth of pages, but we'd like to allocate more if we can:
748	 */
749	bch2_bio_alloc_pages_pool(c, bio,
750				  min_t(unsigned, output_available,
751					c->opts.encoded_extent_max));
752
753	if (bio->bi_iter.bi_size < output_available)
754		*page_alloc_failed =
755			bch2_bio_alloc_pages(bio,
756					     output_available -
757					     bio->bi_iter.bi_size,
758					     GFP_NOFS) != 0;
759
760	return bio;
761}
762
763static int bch2_write_rechecksum(struct bch_fs *c,
764				 struct bch_write_op *op,
765				 unsigned new_csum_type)
766{
767	struct bio *bio = &op->wbio.bio;
768	struct bch_extent_crc_unpacked new_crc;
769	int ret;
770
771	/* bch2_rechecksum_bio() can't encrypt or decrypt data: */
772
773	if (bch2_csum_type_is_encryption(op->crc.csum_type) !=
774	    bch2_csum_type_is_encryption(new_csum_type))
775		new_csum_type = op->crc.csum_type;
776
777	ret = bch2_rechecksum_bio(c, bio, op->version, op->crc,
778				  NULL, &new_crc,
779				  op->crc.offset, op->crc.live_size,
780				  new_csum_type);
781	if (ret)
782		return ret;
783
784	bio_advance(bio, op->crc.offset << 9);
785	bio->bi_iter.bi_size = op->crc.live_size << 9;
786	op->crc = new_crc;
787	return 0;
788}
789
790static int bch2_write_decrypt(struct bch_write_op *op)
791{
792	struct bch_fs *c = op->c;
793	struct nonce nonce = extent_nonce(op->version, op->crc);
794	struct bch_csum csum;
795	int ret;
796
797	if (!bch2_csum_type_is_encryption(op->crc.csum_type))
798		return 0;
799
800	/*
801	 * If we need to decrypt data in the write path, we'll no longer be able
802	 * to verify the existing checksum (poly1305 mac, in this case) after
803	 * it's decrypted - this is the last point we'll be able to reverify the
804	 * checksum:
805	 */
806	csum = bch2_checksum_bio(c, op->crc.csum_type, nonce, &op->wbio.bio);
807	if (bch2_crc_cmp(op->crc.csum, csum) && !c->opts.no_data_io)
808		return -EIO;
809
810	ret = bch2_encrypt_bio(c, op->crc.csum_type, nonce, &op->wbio.bio);
811	op->crc.csum_type = 0;
812	op->crc.csum = (struct bch_csum) { 0, 0 };
813	return ret;
814}
815
816static enum prep_encoded_ret {
817	PREP_ENCODED_OK,
818	PREP_ENCODED_ERR,
819	PREP_ENCODED_CHECKSUM_ERR,
820	PREP_ENCODED_DO_WRITE,
821} bch2_write_prep_encoded_data(struct bch_write_op *op, struct write_point *wp)
822{
823	struct bch_fs *c = op->c;
824	struct bio *bio = &op->wbio.bio;
825
826	if (!(op->flags & BCH_WRITE_DATA_ENCODED))
827		return PREP_ENCODED_OK;
828
829	BUG_ON(bio_sectors(bio) != op->crc.compressed_size);
830
831	/* Can we just write the entire extent as is? */
832	if (op->crc.uncompressed_size == op->crc.live_size &&
833	    op->crc.uncompressed_size <= c->opts.encoded_extent_max >> 9 &&
834	    op->crc.compressed_size <= wp->sectors_free &&
835	    (op->crc.compression_type == bch2_compression_opt_to_type(op->compression_opt) ||
836	     op->incompressible)) {
837		if (!crc_is_compressed(op->crc) &&
838		    op->csum_type != op->crc.csum_type &&
839		    bch2_write_rechecksum(c, op, op->csum_type) &&
840		    !c->opts.no_data_io)
841			return PREP_ENCODED_CHECKSUM_ERR;
842
843		return PREP_ENCODED_DO_WRITE;
844	}
845
846	/*
847	 * If the data is compressed and we couldn't write the entire extent as
848	 * is, we have to decompress it:
849	 */
850	if (crc_is_compressed(op->crc)) {
851		struct bch_csum csum;
852
853		if (bch2_write_decrypt(op))
854			return PREP_ENCODED_CHECKSUM_ERR;
855
856		/* Last point we can still verify checksum: */
857		csum = bch2_checksum_bio(c, op->crc.csum_type,
858					 extent_nonce(op->version, op->crc),
859					 bio);
860		if (bch2_crc_cmp(op->crc.csum, csum) && !c->opts.no_data_io)
861			return PREP_ENCODED_CHECKSUM_ERR;
862
863		if (bch2_bio_uncompress_inplace(c, bio, &op->crc))
864			return PREP_ENCODED_ERR;
865	}
866
867	/*
868	 * No longer have compressed data after this point - data might be
869	 * encrypted:
870	 */
871
872	/*
873	 * If the data is checksummed and we're only writing a subset,
874	 * rechecksum and adjust bio to point to currently live data:
875	 */
876	if ((op->crc.live_size != op->crc.uncompressed_size ||
877	     op->crc.csum_type != op->csum_type) &&
878	    bch2_write_rechecksum(c, op, op->csum_type) &&
879	    !c->opts.no_data_io)
880		return PREP_ENCODED_CHECKSUM_ERR;
881
882	/*
883	 * If we want to compress the data, it has to be decrypted:
884	 */
885	if ((op->compression_opt ||
886	     bch2_csum_type_is_encryption(op->crc.csum_type) !=
887	     bch2_csum_type_is_encryption(op->csum_type)) &&
888	    bch2_write_decrypt(op))
889		return PREP_ENCODED_CHECKSUM_ERR;
890
891	return PREP_ENCODED_OK;
892}
893
894static int bch2_write_extent(struct bch_write_op *op, struct write_point *wp,
895			     struct bio **_dst)
896{
897	struct bch_fs *c = op->c;
898	struct bio *src = &op->wbio.bio, *dst = src;
899	struct bvec_iter saved_iter;
900	void *ec_buf;
901	unsigned total_output = 0, total_input = 0;
902	bool bounce = false;
903	bool page_alloc_failed = false;
904	int ret, more = 0;
905
906	BUG_ON(!bio_sectors(src));
907
908	ec_buf = bch2_writepoint_ec_buf(c, wp);
909
910	switch (bch2_write_prep_encoded_data(op, wp)) {
911	case PREP_ENCODED_OK:
912		break;
913	case PREP_ENCODED_ERR:
914		ret = -EIO;
915		goto err;
916	case PREP_ENCODED_CHECKSUM_ERR:
917		goto csum_err;
918	case PREP_ENCODED_DO_WRITE:
919		/* XXX look for bug here */
920		if (ec_buf) {
921			dst = bch2_write_bio_alloc(c, wp, src,
922						   &page_alloc_failed,
923						   ec_buf);
924			bio_copy_data(dst, src);
925			bounce = true;
926		}
927		init_append_extent(op, wp, op->version, op->crc);
928		goto do_write;
929	}
930
931	if (ec_buf ||
932	    op->compression_opt ||
933	    (op->csum_type &&
934	     !(op->flags & BCH_WRITE_PAGES_STABLE)) ||
935	    (bch2_csum_type_is_encryption(op->csum_type) &&
936	     !(op->flags & BCH_WRITE_PAGES_OWNED))) {
937		dst = bch2_write_bio_alloc(c, wp, src,
938					   &page_alloc_failed,
939					   ec_buf);
940		bounce = true;
941	}
942
943	saved_iter = dst->bi_iter;
944
945	do {
946		struct bch_extent_crc_unpacked crc = { 0 };
947		struct bversion version = op->version;
948		size_t dst_len = 0, src_len = 0;
949
950		if (page_alloc_failed &&
951		    dst->bi_iter.bi_size  < (wp->sectors_free << 9) &&
952		    dst->bi_iter.bi_size < c->opts.encoded_extent_max)
953			break;
954
955		BUG_ON(op->compression_opt &&
956		       (op->flags & BCH_WRITE_DATA_ENCODED) &&
957		       bch2_csum_type_is_encryption(op->crc.csum_type));
958		BUG_ON(op->compression_opt && !bounce);
959
960		crc.compression_type = op->incompressible
961			? BCH_COMPRESSION_TYPE_incompressible
962			: op->compression_opt
963			? bch2_bio_compress(c, dst, &dst_len, src, &src_len,
964					    op->compression_opt)
965			: 0;
966		if (!crc_is_compressed(crc)) {
967			dst_len = min(dst->bi_iter.bi_size, src->bi_iter.bi_size);
968			dst_len = min_t(unsigned, dst_len, wp->sectors_free << 9);
969
970			if (op->csum_type)
971				dst_len = min_t(unsigned, dst_len,
972						c->opts.encoded_extent_max);
973
974			if (bounce) {
975				swap(dst->bi_iter.bi_size, dst_len);
976				bio_copy_data(dst, src);
977				swap(dst->bi_iter.bi_size, dst_len);
978			}
979
980			src_len = dst_len;
981		}
982
983		BUG_ON(!src_len || !dst_len);
984
985		if (bch2_csum_type_is_encryption(op->csum_type)) {
986			if (bversion_zero(version)) {
987				version.lo = atomic64_inc_return(&c->key_version);
988			} else {
989				crc.nonce = op->nonce;
990				op->nonce += src_len >> 9;
991			}
992		}
993
994		if ((op->flags & BCH_WRITE_DATA_ENCODED) &&
995		    !crc_is_compressed(crc) &&
996		    bch2_csum_type_is_encryption(op->crc.csum_type) ==
997		    bch2_csum_type_is_encryption(op->csum_type)) {
998			u8 compression_type = crc.compression_type;
999			u16 nonce = crc.nonce;
1000			/*
1001			 * Note: when we're using rechecksum(), we need to be
1002			 * checksumming @src because it has all the data our
1003			 * existing checksum covers - if we bounced (because we
1004			 * were trying to compress), @dst will only have the
1005			 * part of the data the new checksum will cover.
1006			 *
1007			 * But normally we want to be checksumming post bounce,
1008			 * because part of the reason for bouncing is so the
1009			 * data can't be modified (by userspace) while it's in
1010			 * flight.
1011			 */
1012			if (bch2_rechecksum_bio(c, src, version, op->crc,
1013					&crc, &op->crc,
1014					src_len >> 9,
1015					bio_sectors(src) - (src_len >> 9),
1016					op->csum_type))
1017				goto csum_err;
1018			/*
1019			 * rchecksum_bio sets compression_type on crc from op->crc,
1020			 * this isn't always correct as sometimes we're changing
1021			 * an extent from uncompressed to incompressible.
1022			 */
1023			crc.compression_type = compression_type;
1024			crc.nonce = nonce;
1025		} else {
1026			if ((op->flags & BCH_WRITE_DATA_ENCODED) &&
1027			    bch2_rechecksum_bio(c, src, version, op->crc,
1028					NULL, &op->crc,
1029					src_len >> 9,
1030					bio_sectors(src) - (src_len >> 9),
1031					op->crc.csum_type))
1032				goto csum_err;
1033
1034			crc.compressed_size	= dst_len >> 9;
1035			crc.uncompressed_size	= src_len >> 9;
1036			crc.live_size		= src_len >> 9;
1037
1038			swap(dst->bi_iter.bi_size, dst_len);
1039			ret = bch2_encrypt_bio(c, op->csum_type,
1040					       extent_nonce(version, crc), dst);
1041			if (ret)
1042				goto err;
1043
1044			crc.csum = bch2_checksum_bio(c, op->csum_type,
1045					 extent_nonce(version, crc), dst);
1046			crc.csum_type = op->csum_type;
1047			swap(dst->bi_iter.bi_size, dst_len);
1048		}
1049
1050		init_append_extent(op, wp, version, crc);
1051
1052		if (dst != src)
1053			bio_advance(dst, dst_len);
1054		bio_advance(src, src_len);
1055		total_output	+= dst_len;
1056		total_input	+= src_len;
1057	} while (dst->bi_iter.bi_size &&
1058		 src->bi_iter.bi_size &&
1059		 wp->sectors_free &&
1060		 !bch2_keylist_realloc(&op->insert_keys,
1061				      op->inline_keys,
1062				      ARRAY_SIZE(op->inline_keys),
1063				      BKEY_EXTENT_U64s_MAX));
1064
1065	more = src->bi_iter.bi_size != 0;
1066
1067	dst->bi_iter = saved_iter;
1068
1069	if (dst == src && more) {
1070		BUG_ON(total_output != total_input);
1071
1072		dst = bio_split(src, total_input >> 9,
1073				GFP_NOFS, &c->bio_write);
1074		wbio_init(dst)->put_bio	= true;
1075		/* copy WRITE_SYNC flag */
1076		dst->bi_opf		= src->bi_opf;
1077	}
1078
1079	dst->bi_iter.bi_size = total_output;
1080do_write:
1081	*_dst = dst;
1082	return more;
1083csum_err:
1084	bch_err(c, "%s writ error: error verifying existing checksum while rewriting existing data (memory corruption?)",
1085		op->flags & BCH_WRITE_MOVE ? "move" : "user");
1086	ret = -EIO;
1087err:
1088	if (to_wbio(dst)->bounce)
1089		bch2_bio_free_pages_pool(c, dst);
1090	if (to_wbio(dst)->put_bio)
1091		bio_put(dst);
1092
1093	return ret;
1094}
1095
1096static bool bch2_extent_is_writeable(struct bch_write_op *op,
1097				     struct bkey_s_c k)
1098{
1099	struct bch_fs *c = op->c;
1100	struct bkey_s_c_extent e;
1101	struct extent_ptr_decoded p;
1102	const union bch_extent_entry *entry;
1103	unsigned replicas = 0;
1104
1105	if (k.k->type != KEY_TYPE_extent)
1106		return false;
1107
1108	e = bkey_s_c_to_extent(k);
1109
1110	rcu_read_lock();
1111	extent_for_each_ptr_decode(e, p, entry) {
1112		if (crc_is_encoded(p.crc) || p.has_ec) {
1113			rcu_read_unlock();
1114			return false;
1115		}
1116
1117		replicas += bch2_extent_ptr_durability(c, &p);
1118	}
1119	rcu_read_unlock();
1120
1121	return replicas >= op->opts.data_replicas;
1122}
1123
1124static int bch2_nocow_write_convert_one_unwritten(struct btree_trans *trans,
1125						  struct btree_iter *iter,
1126						  struct bkey_i *orig,
1127						  struct bkey_s_c k,
1128						  u64 new_i_size)
1129{
1130	if (!bch2_extents_match(bkey_i_to_s_c(orig), k)) {
1131		/* trace this */
1132		return 0;
1133	}
1134
1135	struct bkey_i *new = bch2_bkey_make_mut_noupdate(trans, k);
1136	int ret = PTR_ERR_OR_ZERO(new);
1137	if (ret)
1138		return ret;
1139
1140	bch2_cut_front(bkey_start_pos(&orig->k), new);
1141	bch2_cut_back(orig->k.p, new);
1142
1143	struct bkey_ptrs ptrs = bch2_bkey_ptrs(bkey_i_to_s(new));
1144	bkey_for_each_ptr(ptrs, ptr)
1145		ptr->unwritten = 0;
1146
1147	/*
1148	 * Note that we're not calling bch2_subvol_get_snapshot() in this path -
1149	 * that was done when we kicked off the write, and here it's important
1150	 * that we update the extent that we wrote to - even if a snapshot has
1151	 * since been created. The write is still outstanding, so we're ok
1152	 * w.r.t. snapshot atomicity:
1153	 */
1154	return  bch2_extent_update_i_size_sectors(trans, iter,
1155					min(new->k.p.offset << 9, new_i_size), 0) ?:
1156		bch2_trans_update(trans, iter, new,
1157				  BTREE_UPDATE_internal_snapshot_node);
1158}
1159
1160static void bch2_nocow_write_convert_unwritten(struct bch_write_op *op)
1161{
1162	struct bch_fs *c = op->c;
1163	struct btree_trans *trans = bch2_trans_get(c);
1164
1165	for_each_keylist_key(&op->insert_keys, orig) {
1166		int ret = for_each_btree_key_upto_commit(trans, iter, BTREE_ID_extents,
1167				     bkey_start_pos(&orig->k), orig->k.p,
1168				     BTREE_ITER_intent, k,
1169				     NULL, NULL, BCH_TRANS_COMMIT_no_enospc, ({
1170			bch2_nocow_write_convert_one_unwritten(trans, &iter, orig, k, op->new_i_size);
1171		}));
1172
1173		if (ret && !bch2_err_matches(ret, EROFS)) {
1174			struct bkey_i *insert = bch2_keylist_front(&op->insert_keys);
1175
1176			bch_err_inum_offset_ratelimited(c,
1177				insert->k.p.inode, insert->k.p.offset << 9,
1178				"%s write error while doing btree update: %s",
1179				op->flags & BCH_WRITE_MOVE ? "move" : "user",
1180				bch2_err_str(ret));
1181		}
1182
1183		if (ret) {
1184			op->error = ret;
1185			break;
1186		}
1187	}
1188
1189	bch2_trans_put(trans);
1190}
1191
1192static void __bch2_nocow_write_done(struct bch_write_op *op)
1193{
1194	if (unlikely(op->flags & BCH_WRITE_IO_ERROR)) {
1195		op->error = -EIO;
1196	} else if (unlikely(op->flags & BCH_WRITE_CONVERT_UNWRITTEN))
1197		bch2_nocow_write_convert_unwritten(op);
1198}
1199
1200static CLOSURE_CALLBACK(bch2_nocow_write_done)
1201{
1202	closure_type(op, struct bch_write_op, cl);
1203
1204	__bch2_nocow_write_done(op);
1205	bch2_write_done(cl);
1206}
1207
1208struct bucket_to_lock {
1209	struct bpos		b;
1210	unsigned		gen;
1211	struct nocow_lock_bucket *l;
1212};
1213
1214static void bch2_nocow_write(struct bch_write_op *op)
1215{
1216	struct bch_fs *c = op->c;
1217	struct btree_trans *trans;
1218	struct btree_iter iter;
1219	struct bkey_s_c k;
1220	DARRAY_PREALLOCATED(struct bucket_to_lock, 3) buckets;
1221	u32 snapshot;
1222	struct bucket_to_lock *stale_at;
1223	int ret;
1224
1225	if (op->flags & BCH_WRITE_MOVE)
1226		return;
1227
1228	darray_init(&buckets);
1229	trans = bch2_trans_get(c);
1230retry:
1231	bch2_trans_begin(trans);
1232
1233	ret = bch2_subvolume_get_snapshot(trans, op->subvol, &snapshot);
1234	if (unlikely(ret))
1235		goto err;
1236
1237	bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
1238			     SPOS(op->pos.inode, op->pos.offset, snapshot),
1239			     BTREE_ITER_slots);
1240	while (1) {
1241		struct bio *bio = &op->wbio.bio;
1242
1243		buckets.nr = 0;
1244
1245		ret = bch2_trans_relock(trans);
1246		if (ret)
1247			break;
1248
1249		k = bch2_btree_iter_peek_slot(&iter);
1250		ret = bkey_err(k);
1251		if (ret)
1252			break;
1253
1254		/* fall back to normal cow write path? */
1255		if (unlikely(k.k->p.snapshot != snapshot ||
1256			     !bch2_extent_is_writeable(op, k)))
1257			break;
1258
1259		if (bch2_keylist_realloc(&op->insert_keys,
1260					 op->inline_keys,
1261					 ARRAY_SIZE(op->inline_keys),
1262					 k.k->u64s))
1263			break;
1264
1265		/* Get iorefs before dropping btree locks: */
1266		struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1267		bkey_for_each_ptr(ptrs, ptr) {
1268			struct bch_dev *ca = bch2_dev_get_ioref(c, ptr->dev, WRITE);
1269			if (unlikely(!ca))
1270				goto err_get_ioref;
1271
1272			struct bpos b = PTR_BUCKET_POS(ca, ptr);
1273			struct nocow_lock_bucket *l =
1274				bucket_nocow_lock(&c->nocow_locks, bucket_to_u64(b));
1275			prefetch(l);
1276
1277			/* XXX allocating memory with btree locks held - rare */
1278			darray_push_gfp(&buckets, ((struct bucket_to_lock) {
1279						   .b = b, .gen = ptr->gen, .l = l,
1280						   }), GFP_KERNEL|__GFP_NOFAIL);
1281
1282			if (ptr->unwritten)
1283				op->flags |= BCH_WRITE_CONVERT_UNWRITTEN;
1284		}
1285
1286		/* Unlock before taking nocow locks, doing IO: */
1287		bkey_reassemble(op->insert_keys.top, k);
1288		bch2_trans_unlock(trans);
1289
1290		bch2_cut_front(op->pos, op->insert_keys.top);
1291		if (op->flags & BCH_WRITE_CONVERT_UNWRITTEN)
1292			bch2_cut_back(POS(op->pos.inode, op->pos.offset + bio_sectors(bio)), op->insert_keys.top);
1293
1294		darray_for_each(buckets, i) {
1295			struct bch_dev *ca = bch2_dev_have_ref(c, i->b.inode);
1296
1297			__bch2_bucket_nocow_lock(&c->nocow_locks, i->l,
1298						 bucket_to_u64(i->b),
1299						 BUCKET_NOCOW_LOCK_UPDATE);
1300
1301			rcu_read_lock();
1302			bool stale = gen_after(*bucket_gen(ca, i->b.offset), i->gen);
1303			rcu_read_unlock();
1304
1305			if (unlikely(stale)) {
1306				stale_at = i;
1307				goto err_bucket_stale;
1308			}
1309		}
1310
1311		bio = &op->wbio.bio;
1312		if (k.k->p.offset < op->pos.offset + bio_sectors(bio)) {
1313			bio = bio_split(bio, k.k->p.offset - op->pos.offset,
1314					GFP_KERNEL, &c->bio_write);
1315			wbio_init(bio)->put_bio = true;
1316			bio->bi_opf = op->wbio.bio.bi_opf;
1317		} else {
1318			op->flags |= BCH_WRITE_DONE;
1319		}
1320
1321		op->pos.offset += bio_sectors(bio);
1322		op->written += bio_sectors(bio);
1323
1324		bio->bi_end_io	= bch2_write_endio;
1325		bio->bi_private	= &op->cl;
1326		bio->bi_opf |= REQ_OP_WRITE;
1327		closure_get(&op->cl);
1328		bch2_submit_wbio_replicas(to_wbio(bio), c, BCH_DATA_user,
1329					  op->insert_keys.top, true);
1330
1331		bch2_keylist_push(&op->insert_keys);
1332		if (op->flags & BCH_WRITE_DONE)
1333			break;
1334		bch2_btree_iter_advance(&iter);
1335	}
1336out:
1337	bch2_trans_iter_exit(trans, &iter);
1338err:
1339	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
1340		goto retry;
1341
1342	if (ret) {
1343		bch_err_inum_offset_ratelimited(c,
1344			op->pos.inode, op->pos.offset << 9,
1345			"%s: btree lookup error %s", __func__, bch2_err_str(ret));
1346		op->error = ret;
1347		op->flags |= BCH_WRITE_DONE;
1348	}
1349
1350	bch2_trans_put(trans);
1351	darray_exit(&buckets);
1352
1353	/* fallback to cow write path? */
1354	if (!(op->flags & BCH_WRITE_DONE)) {
1355		closure_sync(&op->cl);
1356		__bch2_nocow_write_done(op);
1357		op->insert_keys.top = op->insert_keys.keys;
1358	} else if (op->flags & BCH_WRITE_SYNC) {
1359		closure_sync(&op->cl);
1360		bch2_nocow_write_done(&op->cl.work);
1361	} else {
1362		/*
1363		 * XXX
1364		 * needs to run out of process context because ei_quota_lock is
1365		 * a mutex
1366		 */
1367		continue_at(&op->cl, bch2_nocow_write_done, index_update_wq(op));
1368	}
1369	return;
1370err_get_ioref:
1371	darray_for_each(buckets, i)
1372		percpu_ref_put(&bch2_dev_have_ref(c, i->b.inode)->io_ref);
1373
1374	/* Fall back to COW path: */
1375	goto out;
1376err_bucket_stale:
1377	darray_for_each(buckets, i) {
1378		bch2_bucket_nocow_unlock(&c->nocow_locks, i->b, BUCKET_NOCOW_LOCK_UPDATE);
1379		if (i == stale_at)
1380			break;
1381	}
1382
1383	/* We can retry this: */
1384	ret = -BCH_ERR_transaction_restart;
1385	goto err_get_ioref;
1386}
1387
1388static void __bch2_write(struct bch_write_op *op)
1389{
1390	struct bch_fs *c = op->c;
1391	struct write_point *wp = NULL;
1392	struct bio *bio = NULL;
1393	unsigned nofs_flags;
1394	int ret;
1395
1396	nofs_flags = memalloc_nofs_save();
1397
1398	if (unlikely(op->opts.nocow && c->opts.nocow_enabled)) {
1399		bch2_nocow_write(op);
1400		if (op->flags & BCH_WRITE_DONE)
1401			goto out_nofs_restore;
1402	}
1403again:
1404	memset(&op->failed, 0, sizeof(op->failed));
1405
1406	do {
1407		struct bkey_i *key_to_write;
1408		unsigned key_to_write_offset = op->insert_keys.top_p -
1409			op->insert_keys.keys_p;
1410
1411		/* +1 for possible cache device: */
1412		if (op->open_buckets.nr + op->nr_replicas + 1 >
1413		    ARRAY_SIZE(op->open_buckets.v))
1414			break;
1415
1416		if (bch2_keylist_realloc(&op->insert_keys,
1417					op->inline_keys,
1418					ARRAY_SIZE(op->inline_keys),
1419					BKEY_EXTENT_U64s_MAX))
1420			break;
1421
1422		/*
1423		 * The copygc thread is now global, which means it's no longer
1424		 * freeing up space on specific disks, which means that
1425		 * allocations for specific disks may hang arbitrarily long:
1426		 */
1427		ret = bch2_trans_do(c, NULL, NULL, 0,
1428			bch2_alloc_sectors_start_trans(trans,
1429				op->target,
1430				op->opts.erasure_code && !(op->flags & BCH_WRITE_CACHED),
1431				op->write_point,
1432				&op->devs_have,
1433				op->nr_replicas,
1434				op->nr_replicas_required,
1435				op->watermark,
1436				op->flags,
1437				(op->flags & (BCH_WRITE_ALLOC_NOWAIT|
1438					      BCH_WRITE_ONLY_SPECIFIED_DEVS))
1439				? NULL : &op->cl, &wp));
1440		if (unlikely(ret)) {
1441			if (bch2_err_matches(ret, BCH_ERR_operation_blocked))
1442				break;
1443
1444			goto err;
1445		}
1446
1447		EBUG_ON(!wp);
1448
1449		bch2_open_bucket_get(c, wp, &op->open_buckets);
1450		ret = bch2_write_extent(op, wp, &bio);
1451
1452		bch2_alloc_sectors_done_inlined(c, wp);
1453err:
1454		if (ret <= 0) {
1455			op->flags |= BCH_WRITE_DONE;
1456
1457			if (ret < 0) {
1458				if (!(op->flags & BCH_WRITE_ALLOC_NOWAIT))
1459					bch_err_inum_offset_ratelimited(c,
1460						op->pos.inode,
1461						op->pos.offset << 9,
1462						"%s(): %s error: %s", __func__,
1463						op->flags & BCH_WRITE_MOVE ? "move" : "user",
1464						bch2_err_str(ret));
1465				op->error = ret;
1466				break;
1467			}
1468		}
1469
1470		bio->bi_end_io	= bch2_write_endio;
1471		bio->bi_private	= &op->cl;
1472		bio->bi_opf |= REQ_OP_WRITE;
1473
1474		closure_get(bio->bi_private);
1475
1476		key_to_write = (void *) (op->insert_keys.keys_p +
1477					 key_to_write_offset);
1478
1479		bch2_submit_wbio_replicas(to_wbio(bio), c, BCH_DATA_user,
1480					  key_to_write, false);
1481	} while (ret);
1482
1483	/*
1484	 * Sync or no?
1485	 *
1486	 * If we're running asynchronously, wne may still want to block
1487	 * synchronously here if we weren't able to submit all of the IO at
1488	 * once, as that signals backpressure to the caller.
1489	 */
1490	if ((op->flags & BCH_WRITE_SYNC) ||
1491	    (!(op->flags & BCH_WRITE_DONE) &&
1492	     !(op->flags & BCH_WRITE_IN_WORKER))) {
1493		if (closure_sync_timeout(&op->cl, HZ * 10)) {
1494			bch2_print_allocator_stuck(c);
1495			closure_sync(&op->cl);
1496		}
1497
1498		__bch2_write_index(op);
1499
1500		if (!(op->flags & BCH_WRITE_DONE))
1501			goto again;
1502		bch2_write_done(&op->cl);
1503	} else {
1504		bch2_write_queue(op, wp);
1505		continue_at(&op->cl, bch2_write_index, NULL);
1506	}
1507out_nofs_restore:
1508	memalloc_nofs_restore(nofs_flags);
1509}
1510
1511static void bch2_write_data_inline(struct bch_write_op *op, unsigned data_len)
1512{
1513	struct bio *bio = &op->wbio.bio;
1514	struct bvec_iter iter;
1515	struct bkey_i_inline_data *id;
1516	unsigned sectors;
1517	int ret;
1518
1519	memset(&op->failed, 0, sizeof(op->failed));
1520
1521	op->flags |= BCH_WRITE_WROTE_DATA_INLINE;
1522	op->flags |= BCH_WRITE_DONE;
1523
1524	bch2_check_set_feature(op->c, BCH_FEATURE_inline_data);
1525
1526	ret = bch2_keylist_realloc(&op->insert_keys, op->inline_keys,
1527				   ARRAY_SIZE(op->inline_keys),
1528				   BKEY_U64s + DIV_ROUND_UP(data_len, 8));
1529	if (ret) {
1530		op->error = ret;
1531		goto err;
1532	}
1533
1534	sectors = bio_sectors(bio);
1535	op->pos.offset += sectors;
1536
1537	id = bkey_inline_data_init(op->insert_keys.top);
1538	id->k.p		= op->pos;
1539	id->k.version	= op->version;
1540	id->k.size	= sectors;
1541
1542	iter = bio->bi_iter;
1543	iter.bi_size = data_len;
1544	memcpy_from_bio(id->v.data, bio, iter);
1545
1546	while (data_len & 7)
1547		id->v.data[data_len++] = '\0';
1548	set_bkey_val_bytes(&id->k, data_len);
1549	bch2_keylist_push(&op->insert_keys);
1550
1551	__bch2_write_index(op);
1552err:
1553	bch2_write_done(&op->cl);
1554}
1555
1556/**
1557 * bch2_write() - handle a write to a cache device or flash only volume
1558 * @cl:		&bch_write_op->cl
1559 *
1560 * This is the starting point for any data to end up in a cache device; it could
1561 * be from a normal write, or a writeback write, or a write to a flash only
1562 * volume - it's also used by the moving garbage collector to compact data in
1563 * mostly empty buckets.
1564 *
1565 * It first writes the data to the cache, creating a list of keys to be inserted
1566 * (if the data won't fit in a single open bucket, there will be multiple keys);
1567 * after the data is written it calls bch_journal, and after the keys have been
1568 * added to the next journal write they're inserted into the btree.
1569 *
1570 * If op->discard is true, instead of inserting the data it invalidates the
1571 * region of the cache represented by op->bio and op->inode.
1572 */
1573CLOSURE_CALLBACK(bch2_write)
1574{
1575	closure_type(op, struct bch_write_op, cl);
1576	struct bio *bio = &op->wbio.bio;
1577	struct bch_fs *c = op->c;
1578	unsigned data_len;
1579
1580	EBUG_ON(op->cl.parent);
1581	BUG_ON(!op->nr_replicas);
1582	BUG_ON(!op->write_point.v);
1583	BUG_ON(bkey_eq(op->pos, POS_MAX));
1584
1585	op->nr_replicas_required = min_t(unsigned, op->nr_replicas_required, op->nr_replicas);
1586	op->start_time = local_clock();
1587	bch2_keylist_init(&op->insert_keys, op->inline_keys);
1588	wbio_init(bio)->put_bio = false;
1589
1590	if (bio->bi_iter.bi_size & (c->opts.block_size - 1)) {
1591		bch_err_inum_offset_ratelimited(c,
1592			op->pos.inode,
1593			op->pos.offset << 9,
1594			"%s write error: misaligned write",
1595			op->flags & BCH_WRITE_MOVE ? "move" : "user");
1596		op->error = -EIO;
1597		goto err;
1598	}
1599
1600	if (c->opts.nochanges) {
1601		op->error = -BCH_ERR_erofs_no_writes;
1602		goto err;
1603	}
1604
1605	if (!(op->flags & BCH_WRITE_MOVE) &&
1606	    !bch2_write_ref_tryget(c, BCH_WRITE_REF_write)) {
1607		op->error = -BCH_ERR_erofs_no_writes;
1608		goto err;
1609	}
1610
1611	this_cpu_add(c->counters[BCH_COUNTER_io_write], bio_sectors(bio));
1612	bch2_increment_clock(c, bio_sectors(bio), WRITE);
1613
1614	data_len = min_t(u64, bio->bi_iter.bi_size,
1615			 op->new_i_size - (op->pos.offset << 9));
1616
1617	if (c->opts.inline_data &&
1618	    data_len <= min(block_bytes(c) / 2, 1024U)) {
1619		bch2_write_data_inline(op, data_len);
1620		return;
1621	}
1622
1623	__bch2_write(op);
1624	return;
1625err:
1626	bch2_disk_reservation_put(c, &op->res);
1627
1628	closure_debug_destroy(&op->cl);
1629	if (op->end_io)
1630		op->end_io(op);
1631}
1632
1633static const char * const bch2_write_flags[] = {
1634#define x(f)	#f,
1635	BCH_WRITE_FLAGS()
1636#undef x
1637	NULL
1638};
1639
1640void bch2_write_op_to_text(struct printbuf *out, struct bch_write_op *op)
1641{
1642	prt_str(out, "pos: ");
1643	bch2_bpos_to_text(out, op->pos);
1644	prt_newline(out);
1645	printbuf_indent_add(out, 2);
1646
1647	prt_str(out, "started: ");
1648	bch2_pr_time_units(out, local_clock() - op->start_time);
1649	prt_newline(out);
1650
1651	prt_str(out, "flags: ");
1652	prt_bitflags(out, bch2_write_flags, op->flags);
1653	prt_newline(out);
1654
1655	prt_printf(out, "ref: %u\n", closure_nr_remaining(&op->cl));
1656
1657	printbuf_indent_sub(out, 2);
1658}
1659
1660void bch2_fs_io_write_exit(struct bch_fs *c)
1661{
1662	mempool_exit(&c->bio_bounce_pages);
1663	bioset_exit(&c->replica_set);
1664	bioset_exit(&c->bio_write);
1665}
1666
1667int bch2_fs_io_write_init(struct bch_fs *c)
1668{
1669	if (bioset_init(&c->bio_write,   1, offsetof(struct bch_write_bio, bio), BIOSET_NEED_BVECS) ||
1670	    bioset_init(&c->replica_set, 4, offsetof(struct bch_write_bio, bio), 0))
1671		return -BCH_ERR_ENOMEM_bio_write_init;
1672
1673	if (mempool_init_page_pool(&c->bio_bounce_pages,
1674				   max_t(unsigned,
1675					 c->opts.btree_node_size,
1676					 c->opts.encoded_extent_max) /
1677				   PAGE_SIZE, 0))
1678		return -BCH_ERR_ENOMEM_bio_bounce_pages_init;
1679
1680	return 0;
1681}
1682