]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/io.c
706f18bc4238373a758998b30064ad7d702a478e
[bcachefs-tools-debian] / libbcachefs / io.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Some low level IO code, and hacks for various block layer limitations
4  *
5  * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6  * Copyright 2012 Google, Inc.
7  */
8
9 #include "bcachefs.h"
10 #include "alloc_background.h"
11 #include "alloc_foreground.h"
12 #include "bkey_buf.h"
13 #include "bset.h"
14 #include "btree_update.h"
15 #include "buckets.h"
16 #include "checksum.h"
17 #include "compress.h"
18 #include "clock.h"
19 #include "data_update.h"
20 #include "debug.h"
21 #include "disk_groups.h"
22 #include "ec.h"
23 #include "error.h"
24 #include "extent_update.h"
25 #include "inode.h"
26 #include "io.h"
27 #include "journal.h"
28 #include "keylist.h"
29 #include "move.h"
30 #include "rebalance.h"
31 #include "subvolume.h"
32 #include "super.h"
33 #include "super-io.h"
34
35 #include <linux/blkdev.h>
36 #include <linux/random.h>
37 #include <linux/sched/mm.h>
38
39 #include <trace/events/bcachefs.h>
40
41 const char *bch2_blk_status_to_str(blk_status_t status)
42 {
43         if (status == BLK_STS_REMOVED)
44                 return "device removed";
45         return blk_status_to_str(status);
46 }
47
48 static bool bch2_target_congested(struct bch_fs *c, u16 target)
49 {
50         const struct bch_devs_mask *devs;
51         unsigned d, nr = 0, total = 0;
52         u64 now = local_clock(), last;
53         s64 congested;
54         struct bch_dev *ca;
55
56         if (!target)
57                 return false;
58
59         rcu_read_lock();
60         devs = bch2_target_to_mask(c, target) ?:
61                 &c->rw_devs[BCH_DATA_user];
62
63         for_each_set_bit(d, devs->d, BCH_SB_MEMBERS_MAX) {
64                 ca = rcu_dereference(c->devs[d]);
65                 if (!ca)
66                         continue;
67
68                 congested = atomic_read(&ca->congested);
69                 last = READ_ONCE(ca->congested_last);
70                 if (time_after64(now, last))
71                         congested -= (now - last) >> 12;
72
73                 total += max(congested, 0LL);
74                 nr++;
75         }
76         rcu_read_unlock();
77
78         return bch2_rand_range(nr * CONGESTED_MAX) < total;
79 }
80
81 static inline void bch2_congested_acct(struct bch_dev *ca, u64 io_latency,
82                                        u64 now, int rw)
83 {
84         u64 latency_capable =
85                 ca->io_latency[rw].quantiles.entries[QUANTILE_IDX(1)].m;
86         /* ideally we'd be taking into account the device's variance here: */
87         u64 latency_threshold = latency_capable << (rw == READ ? 2 : 3);
88         s64 latency_over = io_latency - latency_threshold;
89
90         if (latency_threshold && latency_over > 0) {
91                 /*
92                  * bump up congested by approximately latency_over * 4 /
93                  * latency_threshold - we don't need much accuracy here so don't
94                  * bother with the divide:
95                  */
96                 if (atomic_read(&ca->congested) < CONGESTED_MAX)
97                         atomic_add(latency_over >>
98                                    max_t(int, ilog2(latency_threshold) - 2, 0),
99                                    &ca->congested);
100
101                 ca->congested_last = now;
102         } else if (atomic_read(&ca->congested) > 0) {
103                 atomic_dec(&ca->congested);
104         }
105 }
106
107 void bch2_latency_acct(struct bch_dev *ca, u64 submit_time, int rw)
108 {
109         atomic64_t *latency = &ca->cur_latency[rw];
110         u64 now = local_clock();
111         u64 io_latency = time_after64(now, submit_time)
112                 ? now - submit_time
113                 : 0;
114         u64 old, new, v = atomic64_read(latency);
115
116         do {
117                 old = v;
118
119                 /*
120                  * If the io latency was reasonably close to the current
121                  * latency, skip doing the update and atomic operation - most of
122                  * the time:
123                  */
124                 if (abs((int) (old - io_latency)) < (old >> 1) &&
125                     now & ~(~0U << 5))
126                         break;
127
128                 new = ewma_add(old, io_latency, 5);
129         } while ((v = atomic64_cmpxchg(latency, old, new)) != old);
130
131         bch2_congested_acct(ca, io_latency, now, rw);
132
133         __bch2_time_stats_update(&ca->io_latency[rw], submit_time, now);
134 }
135
136 /* Allocate, free from mempool: */
137
138 void bch2_bio_free_pages_pool(struct bch_fs *c, struct bio *bio)
139 {
140         struct bvec_iter_all iter;
141         struct bio_vec *bv;
142
143         bio_for_each_segment_all(bv, bio, iter)
144                 if (bv->bv_page != ZERO_PAGE(0))
145                         mempool_free(bv->bv_page, &c->bio_bounce_pages);
146         bio->bi_vcnt = 0;
147 }
148
149 static struct page *__bio_alloc_page_pool(struct bch_fs *c, bool *using_mempool)
150 {
151         struct page *page;
152
153         if (likely(!*using_mempool)) {
154                 page = alloc_page(GFP_NOIO);
155                 if (unlikely(!page)) {
156                         mutex_lock(&c->bio_bounce_pages_lock);
157                         *using_mempool = true;
158                         goto pool_alloc;
159
160                 }
161         } else {
162 pool_alloc:
163                 page = mempool_alloc(&c->bio_bounce_pages, GFP_NOIO);
164         }
165
166         return page;
167 }
168
169 void bch2_bio_alloc_pages_pool(struct bch_fs *c, struct bio *bio,
170                                size_t size)
171 {
172         bool using_mempool = false;
173
174         while (size) {
175                 struct page *page = __bio_alloc_page_pool(c, &using_mempool);
176                 unsigned len = min_t(size_t, PAGE_SIZE, size);
177
178                 BUG_ON(!bio_add_page(bio, page, len, 0));
179                 size -= len;
180         }
181
182         if (using_mempool)
183                 mutex_unlock(&c->bio_bounce_pages_lock);
184 }
185
186 /* Extent update path: */
187
188 int bch2_sum_sector_overwrites(struct btree_trans *trans,
189                                struct btree_iter *extent_iter,
190                                struct bkey_i *new,
191                                bool *usage_increasing,
192                                s64 *i_sectors_delta,
193                                s64 *disk_sectors_delta)
194 {
195         struct bch_fs *c = trans->c;
196         struct btree_iter iter;
197         struct bkey_s_c old;
198         unsigned new_replicas = bch2_bkey_replicas(c, bkey_i_to_s_c(new));
199         bool new_compressed = bch2_bkey_sectors_compressed(bkey_i_to_s_c(new));
200         int ret = 0;
201
202         *usage_increasing       = false;
203         *i_sectors_delta        = 0;
204         *disk_sectors_delta     = 0;
205
206         bch2_trans_copy_iter(&iter, extent_iter);
207
208         for_each_btree_key_continue_norestart(iter, BTREE_ITER_SLOTS, old, ret) {
209                 s64 sectors = min(new->k.p.offset, old.k->p.offset) -
210                         max(bkey_start_offset(&new->k),
211                             bkey_start_offset(old.k));
212
213                 *i_sectors_delta += sectors *
214                         (bkey_extent_is_allocation(&new->k) -
215                          bkey_extent_is_allocation(old.k));
216
217                 *disk_sectors_delta += sectors * bch2_bkey_nr_ptrs_allocated(bkey_i_to_s_c(new));
218                 *disk_sectors_delta -= new->k.p.snapshot == old.k->p.snapshot
219                         ? sectors * bch2_bkey_nr_ptrs_fully_allocated(old)
220                         : 0;
221
222                 if (!*usage_increasing &&
223                     (new->k.p.snapshot != old.k->p.snapshot ||
224                      new_replicas > bch2_bkey_replicas(c, old) ||
225                      (!new_compressed && bch2_bkey_sectors_compressed(old))))
226                         *usage_increasing = true;
227
228                 if (bkey_ge(old.k->p, new->k.p))
229                         break;
230         }
231
232         bch2_trans_iter_exit(trans, &iter);
233         return ret;
234 }
235
236 static inline int bch2_extent_update_i_size_sectors(struct btree_trans *trans,
237                                                     struct btree_iter *extent_iter,
238                                                     u64 new_i_size,
239                                                     s64 i_sectors_delta)
240 {
241         struct btree_iter iter;
242         struct bkey_i *k;
243         struct bkey_i_inode_v3 *inode;
244         int ret;
245
246         bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
247                              SPOS(0,
248                                   extent_iter->pos.inode,
249                                   extent_iter->snapshot),
250                              BTREE_ITER_INTENT|BTREE_ITER_CACHED);
251         k = bch2_bkey_get_mut(trans, &iter);
252         ret = PTR_ERR_OR_ZERO(k);
253         if (unlikely(ret))
254                 goto err;
255
256         if (unlikely(k->k.type != KEY_TYPE_inode_v3)) {
257                 k = bch2_inode_to_v3(trans, k);
258                 ret = PTR_ERR_OR_ZERO(k);
259                 if (unlikely(ret))
260                         goto err;
261         }
262
263         inode = bkey_i_to_inode_v3(k);
264
265         if (!(le64_to_cpu(inode->v.bi_flags) & BCH_INODE_I_SIZE_DIRTY) &&
266             new_i_size > le64_to_cpu(inode->v.bi_size))
267                 inode->v.bi_size = cpu_to_le64(new_i_size);
268
269         le64_add_cpu(&inode->v.bi_sectors, i_sectors_delta);
270
271         inode->k.p.snapshot = iter.snapshot;
272
273         ret = bch2_trans_update(trans, &iter, &inode->k_i,
274                                 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
275 err:
276         bch2_trans_iter_exit(trans, &iter);
277         return ret;
278 }
279
280 int bch2_extent_update(struct btree_trans *trans,
281                        subvol_inum inum,
282                        struct btree_iter *iter,
283                        struct bkey_i *k,
284                        struct disk_reservation *disk_res,
285                        u64 new_i_size,
286                        s64 *i_sectors_delta_total,
287                        bool check_enospc)
288 {
289         struct bpos next_pos;
290         bool usage_increasing;
291         s64 i_sectors_delta = 0, disk_sectors_delta = 0;
292         int ret;
293
294         /*
295          * This traverses us the iterator without changing iter->path->pos to
296          * search_key() (which is pos + 1 for extents): we want there to be a
297          * path already traversed at iter->pos because
298          * bch2_trans_extent_update() will use it to attempt extent merging
299          */
300         ret = __bch2_btree_iter_traverse(iter);
301         if (ret)
302                 return ret;
303
304         ret = bch2_extent_trim_atomic(trans, iter, k);
305         if (ret)
306                 return ret;
307
308         next_pos = k->k.p;
309
310         ret = bch2_sum_sector_overwrites(trans, iter, k,
311                         &usage_increasing,
312                         &i_sectors_delta,
313                         &disk_sectors_delta);
314         if (ret)
315                 return ret;
316
317         if (disk_res &&
318             disk_sectors_delta > (s64) disk_res->sectors) {
319                 ret = bch2_disk_reservation_add(trans->c, disk_res,
320                                         disk_sectors_delta - disk_res->sectors,
321                                         !check_enospc || !usage_increasing
322                                         ? BCH_DISK_RESERVATION_NOFAIL : 0);
323                 if (ret)
324                         return ret;
325         }
326
327         /*
328          * Note:
329          * We always have to do an inode update - even when i_size/i_sectors
330          * aren't changing - for fsync to work properly; fsync relies on
331          * inode->bi_journal_seq which is updated by the trigger code:
332          */
333         ret =   bch2_extent_update_i_size_sectors(trans, iter,
334                                                   min(k->k.p.offset << 9, new_i_size),
335                                                   i_sectors_delta) ?:
336                 bch2_trans_update(trans, iter, k, 0) ?:
337                 bch2_trans_commit(trans, disk_res, NULL,
338                                 BTREE_INSERT_NOCHECK_RW|
339                                 BTREE_INSERT_NOFAIL);
340         if (unlikely(ret))
341                 return ret;
342
343         if (i_sectors_delta_total)
344                 *i_sectors_delta_total += i_sectors_delta;
345         bch2_btree_iter_set_pos(iter, next_pos);
346         return 0;
347 }
348
349 /* Overwrites whatever was present with zeroes: */
350 int bch2_extent_fallocate(struct btree_trans *trans,
351                           subvol_inum inum,
352                           struct btree_iter *iter,
353                           unsigned sectors,
354                           struct bch_io_opts opts,
355                           s64 *i_sectors_delta,
356                           struct write_point_specifier write_point)
357 {
358         struct bch_fs *c = trans->c;
359         struct disk_reservation disk_res = { 0 };
360         struct closure cl;
361         struct open_buckets open_buckets;
362         struct bkey_s_c k;
363         struct bkey_buf old, new;
364         bool have_reservation = false;
365         bool unwritten = opts.nocow &&
366             c->sb.version >= bcachefs_metadata_version_unwritten_extents;
367         int ret;
368
369         bch2_bkey_buf_init(&old);
370         bch2_bkey_buf_init(&new);
371         closure_init_stack(&cl);
372         open_buckets.nr = 0;
373 retry:
374         k = bch2_btree_iter_peek_slot(iter);
375         ret = bkey_err(k);
376         if (ret)
377                 return ret;
378
379         sectors = min_t(u64, sectors, k.k->p.offset - iter->pos.offset);
380
381         if (!have_reservation) {
382                 unsigned new_replicas =
383                         max(0, (int) opts.data_replicas -
384                             (int) bch2_bkey_nr_ptrs_fully_allocated(k));
385                 /*
386                  * Get a disk reservation before (in the nocow case) calling
387                  * into the allocator:
388                  */
389                 ret = bch2_disk_reservation_get(c, &disk_res, sectors, new_replicas, 0);
390                 if (unlikely(ret))
391                         goto out;
392
393                 bch2_bkey_buf_reassemble(&old, c, k);
394         }
395
396         if (have_reservation) {
397                 if (!bch2_extents_match(k, bkey_i_to_s_c(old.k)))
398                         goto out;
399
400                 bch2_key_resize(&new.k->k, sectors);
401         } else if (!unwritten) {
402                 struct bkey_i_reservation *reservation;
403
404                 bch2_bkey_buf_realloc(&new, c, sizeof(*reservation) / sizeof(u64));
405                 reservation = bkey_reservation_init(new.k);
406                 reservation->k.p = iter->pos;
407                 bch2_key_resize(&reservation->k, sectors);
408                 reservation->v.nr_replicas = opts.data_replicas;
409         } else {
410                 struct bkey_i_extent *e;
411                 struct bch_devs_list devs_have;
412                 struct write_point *wp;
413                 struct bch_extent_ptr *ptr;
414
415                 devs_have.nr = 0;
416
417                 bch2_bkey_buf_realloc(&new, c, BKEY_EXTENT_U64s_MAX);
418
419                 e = bkey_extent_init(new.k);
420                 e->k.p = iter->pos;
421
422                 ret = bch2_alloc_sectors_start_trans(trans,
423                                 opts.foreground_target,
424                                 false,
425                                 write_point,
426                                 &devs_have,
427                                 opts.data_replicas,
428                                 opts.data_replicas,
429                                 RESERVE_none, 0, &cl, &wp);
430                 if (bch2_err_matches(ret, BCH_ERR_operation_blocked)) {
431                         bch2_trans_unlock(trans);
432                         closure_sync(&cl);
433                         goto retry;
434                 }
435                 if (ret)
436                         return ret;
437
438                 sectors = min(sectors, wp->sectors_free);
439
440                 bch2_key_resize(&e->k, sectors);
441
442                 bch2_open_bucket_get(c, wp, &open_buckets);
443                 bch2_alloc_sectors_append_ptrs(c, wp, &e->k_i, sectors, false);
444                 bch2_alloc_sectors_done(c, wp);
445
446                 extent_for_each_ptr(extent_i_to_s(e), ptr)
447                         ptr->unwritten = true;
448         }
449
450         have_reservation = true;
451
452         ret = bch2_extent_update(trans, inum, iter, new.k, &disk_res,
453                                  0, i_sectors_delta, true);
454 out:
455         if ((atomic_read(&cl.remaining) & CLOSURE_REMAINING_MASK) != 1) {
456                 bch2_trans_unlock(trans);
457                 closure_sync(&cl);
458         }
459
460         if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
461                 bch2_trans_begin(trans);
462                 goto retry;
463         }
464
465         bch2_open_buckets_put(c, &open_buckets);
466         bch2_disk_reservation_put(c, &disk_res);
467         bch2_bkey_buf_exit(&new, c);
468         bch2_bkey_buf_exit(&old, c);
469
470         return ret;
471 }
472
473 /*
474  * Returns -BCH_ERR_transacton_restart if we had to drop locks:
475  */
476 int bch2_fpunch_at(struct btree_trans *trans, struct btree_iter *iter,
477                    subvol_inum inum, u64 end,
478                    s64 *i_sectors_delta)
479 {
480         struct bch_fs *c        = trans->c;
481         unsigned max_sectors    = KEY_SIZE_MAX & (~0 << c->block_bits);
482         struct bpos end_pos = POS(inum.inum, end);
483         struct bkey_s_c k;
484         int ret = 0, ret2 = 0;
485         u32 snapshot;
486
487         while (!ret ||
488                bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
489                 struct disk_reservation disk_res =
490                         bch2_disk_reservation_init(c, 0);
491                 struct bkey_i delete;
492
493                 if (ret)
494                         ret2 = ret;
495
496                 bch2_trans_begin(trans);
497
498                 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
499                 if (ret)
500                         continue;
501
502                 bch2_btree_iter_set_snapshot(iter, snapshot);
503
504                 /*
505                  * peek_upto() doesn't have ideal semantics for extents:
506                  */
507                 k = bch2_btree_iter_peek_upto(iter, end_pos);
508                 if (!k.k)
509                         break;
510
511                 ret = bkey_err(k);
512                 if (ret)
513                         continue;
514
515                 BUG_ON(bkey_ge(iter->pos, end_pos));
516
517                 bkey_init(&delete.k);
518                 delete.k.p = iter->pos;
519
520                 /* create the biggest key we can */
521                 bch2_key_resize(&delete.k, max_sectors);
522                 bch2_cut_back(end_pos, &delete);
523
524                 ret = bch2_extent_update(trans, inum, iter, &delete,
525                                 &disk_res, 0, i_sectors_delta, false);
526                 bch2_disk_reservation_put(c, &disk_res);
527         }
528
529         BUG_ON(bkey_gt(iter->pos, end_pos));
530
531         return ret ?: ret2;
532 }
533
534 int bch2_fpunch(struct bch_fs *c, subvol_inum inum, u64 start, u64 end,
535                 s64 *i_sectors_delta)
536 {
537         struct btree_trans trans;
538         struct btree_iter iter;
539         int ret;
540
541         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 1024);
542         bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
543                              POS(inum.inum, start),
544                              BTREE_ITER_INTENT);
545
546         ret = bch2_fpunch_at(&trans, &iter, inum, end, i_sectors_delta);
547
548         bch2_trans_iter_exit(&trans, &iter);
549         bch2_trans_exit(&trans);
550
551         if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
552                 ret = 0;
553
554         return ret;
555 }
556
557 static int bch2_write_index_default(struct bch_write_op *op)
558 {
559         struct bch_fs *c = op->c;
560         struct bkey_buf sk;
561         struct keylist *keys = &op->insert_keys;
562         struct bkey_i *k = bch2_keylist_front(keys);
563         struct btree_trans trans;
564         struct btree_iter iter;
565         subvol_inum inum = {
566                 .subvol = op->subvol,
567                 .inum   = k->k.p.inode,
568         };
569         int ret;
570
571         BUG_ON(!inum.subvol);
572
573         bch2_bkey_buf_init(&sk);
574         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 1024);
575
576         do {
577                 bch2_trans_begin(&trans);
578
579                 k = bch2_keylist_front(keys);
580                 bch2_bkey_buf_copy(&sk, c, k);
581
582                 ret = bch2_subvolume_get_snapshot(&trans, inum.subvol,
583                                                   &sk.k->k.p.snapshot);
584                 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
585                         continue;
586                 if (ret)
587                         break;
588
589                 bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
590                                      bkey_start_pos(&sk.k->k),
591                                      BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
592
593                 ret = bch2_extent_update(&trans, inum, &iter, sk.k,
594                                          &op->res,
595                                          op->new_i_size, &op->i_sectors_delta,
596                                          op->flags & BCH_WRITE_CHECK_ENOSPC);
597                 bch2_trans_iter_exit(&trans, &iter);
598
599                 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
600                         continue;
601                 if (ret)
602                         break;
603
604                 if (bkey_ge(iter.pos, k->k.p))
605                         bch2_keylist_pop_front(&op->insert_keys);
606                 else
607                         bch2_cut_front(iter.pos, k);
608         } while (!bch2_keylist_empty(keys));
609
610         bch2_trans_exit(&trans);
611         bch2_bkey_buf_exit(&sk, c);
612
613         return ret;
614 }
615
616 /* Writes */
617
618 void bch2_submit_wbio_replicas(struct bch_write_bio *wbio, struct bch_fs *c,
619                                enum bch_data_type type,
620                                const struct bkey_i *k,
621                                bool nocow)
622 {
623         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(bkey_i_to_s_c(k));
624         const struct bch_extent_ptr *ptr;
625         struct bch_write_bio *n;
626         struct bch_dev *ca;
627
628         BUG_ON(c->opts.nochanges);
629
630         bkey_for_each_ptr(ptrs, ptr) {
631                 BUG_ON(ptr->dev >= BCH_SB_MEMBERS_MAX ||
632                        !c->devs[ptr->dev]);
633
634                 ca = bch_dev_bkey_exists(c, ptr->dev);
635
636                 if (to_entry(ptr + 1) < ptrs.end) {
637                         n = to_wbio(bio_alloc_clone(NULL, &wbio->bio,
638                                                 GFP_NOIO, &ca->replica_set));
639
640                         n->bio.bi_end_io        = wbio->bio.bi_end_io;
641                         n->bio.bi_private       = wbio->bio.bi_private;
642                         n->parent               = wbio;
643                         n->split                = true;
644                         n->bounce               = false;
645                         n->put_bio              = true;
646                         n->bio.bi_opf           = wbio->bio.bi_opf;
647                         bio_inc_remaining(&wbio->bio);
648                 } else {
649                         n = wbio;
650                         n->split                = false;
651                 }
652
653                 n->c                    = c;
654                 n->dev                  = ptr->dev;
655                 n->have_ioref           = nocow || bch2_dev_get_ioref(ca,
656                                         type == BCH_DATA_btree ? READ : WRITE);
657                 n->nocow                = nocow;
658                 n->submit_time          = local_clock();
659                 n->inode_offset         = bkey_start_offset(&k->k);
660                 n->bio.bi_iter.bi_sector = ptr->offset;
661
662                 if (likely(n->have_ioref)) {
663                         this_cpu_add(ca->io_done->sectors[WRITE][type],
664                                      bio_sectors(&n->bio));
665
666                         bio_set_dev(&n->bio, ca->disk_sb.bdev);
667                         submit_bio(&n->bio);
668                 } else {
669                         n->bio.bi_status        = BLK_STS_REMOVED;
670                         bio_endio(&n->bio);
671                 }
672         }
673 }
674
675 static void __bch2_write(struct bch_write_op *);
676
677 static void bch2_write_done(struct closure *cl)
678 {
679         struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
680         struct bch_fs *c = op->c;
681
682         bch2_disk_reservation_put(c, &op->res);
683         percpu_ref_put(&c->writes);
684         bch2_keylist_free(&op->insert_keys, op->inline_keys);
685
686         bch2_time_stats_update(&c->times[BCH_TIME_data_write], op->start_time);
687
688         closure_debug_destroy(cl);
689         if (op->end_io)
690                 op->end_io(op);
691 }
692
693 static noinline int bch2_write_drop_io_error_ptrs(struct bch_write_op *op)
694 {
695         struct keylist *keys = &op->insert_keys;
696         struct bch_extent_ptr *ptr;
697         struct bkey_i *src, *dst = keys->keys, *n;
698
699         for (src = keys->keys; src != keys->top; src = n) {
700                 n = bkey_next(src);
701
702                 if (bkey_extent_is_direct_data(&src->k)) {
703                         bch2_bkey_drop_ptrs(bkey_i_to_s(src), ptr,
704                                             test_bit(ptr->dev, op->failed.d));
705
706                         if (!bch2_bkey_nr_ptrs(bkey_i_to_s_c(src)))
707                                 return -EIO;
708                 }
709
710                 if (dst != src)
711                         memmove_u64s_down(dst, src, src->u64s);
712                 dst = bkey_next(dst);
713         }
714
715         keys->top = dst;
716         return 0;
717 }
718
719 /**
720  * bch_write_index - after a write, update index to point to new data
721  */
722 static void __bch2_write_index(struct bch_write_op *op)
723 {
724         struct bch_fs *c = op->c;
725         struct keylist *keys = &op->insert_keys;
726         struct bkey_i *k;
727         unsigned dev;
728         int ret = 0;
729
730         if (unlikely(op->flags & BCH_WRITE_IO_ERROR)) {
731                 ret = bch2_write_drop_io_error_ptrs(op);
732                 if (ret)
733                         goto err;
734         }
735
736         /*
737          * probably not the ideal place to hook this in, but I don't
738          * particularly want to plumb io_opts all the way through the btree
739          * update stack right now
740          */
741         for_each_keylist_key(keys, k)
742                 bch2_rebalance_add_key(c, bkey_i_to_s_c(k), &op->opts);
743
744         if (!bch2_keylist_empty(keys)) {
745                 u64 sectors_start = keylist_sectors(keys);
746
747                 ret = !(op->flags & BCH_WRITE_MOVE)
748                         ? bch2_write_index_default(op)
749                         : bch2_data_update_index_update(op);
750
751                 BUG_ON(bch2_err_matches(ret, BCH_ERR_transaction_restart));
752                 BUG_ON(keylist_sectors(keys) && !ret);
753
754                 op->written += sectors_start - keylist_sectors(keys);
755
756                 if (ret) {
757                         struct bkey_i *k = bch2_keylist_front(&op->insert_keys);
758
759                         bch_err_inum_offset_ratelimited(c,
760                                 k->k.p.inode, k->k.p.offset << 9,
761                                 "write error while doing btree update: %s",
762                                 bch2_err_str(ret));
763                         goto err;
764                 }
765         }
766 out:
767         /* If some a bucket wasn't written, we can't erasure code it: */
768         for_each_set_bit(dev, op->failed.d, BCH_SB_MEMBERS_MAX)
769                 bch2_open_bucket_write_error(c, &op->open_buckets, dev);
770
771         bch2_open_buckets_put(c, &op->open_buckets);
772         return;
773 err:
774         keys->top = keys->keys;
775         op->error = ret;
776         op->flags |= BCH_WRITE_DONE;
777         goto out;
778 }
779
780 static void bch2_write_index(struct closure *cl)
781 {
782         struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
783         struct write_point *wp = op->wp;
784         struct workqueue_struct *wq = index_update_wq(op);
785
786         barrier();
787         op->btree_update_ready = true;
788         queue_work(wq, &wp->index_update_work);
789 }
790
791 void bch2_write_point_do_index_updates(struct work_struct *work)
792 {
793         struct write_point *wp =
794                 container_of(work, struct write_point, index_update_work);
795         struct bch_write_op *op;
796
797         while (1) {
798                 spin_lock(&wp->writes_lock);
799                 op = list_first_entry_or_null(&wp->writes, struct bch_write_op, wp_list);
800                 if (op && !op->btree_update_ready)
801                         op = NULL;
802                 if (op)
803                         list_del(&op->wp_list);
804                 spin_unlock(&wp->writes_lock);
805
806                 if (!op)
807                         break;
808
809                 __bch2_write_index(op);
810
811                 if (!(op->flags & BCH_WRITE_DONE))
812                         __bch2_write(op);
813                 else
814                         bch2_write_done(&op->cl);
815         }
816 }
817
818 static void bch2_write_endio(struct bio *bio)
819 {
820         struct closure *cl              = bio->bi_private;
821         struct bch_write_op *op         = container_of(cl, struct bch_write_op, cl);
822         struct bch_write_bio *wbio      = to_wbio(bio);
823         struct bch_write_bio *parent    = wbio->split ? wbio->parent : NULL;
824         struct bch_fs *c                = wbio->c;
825         struct bch_dev *ca              = bch_dev_bkey_exists(c, wbio->dev);
826
827         if (bch2_dev_inum_io_err_on(bio->bi_status, ca,
828                                     op->pos.inode,
829                                     wbio->inode_offset << 9,
830                                     "data write error: %s",
831                                     bch2_blk_status_to_str(bio->bi_status))) {
832                 set_bit(wbio->dev, op->failed.d);
833                 op->flags |= BCH_WRITE_IO_ERROR;
834         }
835
836         if (wbio->nocow)
837                 set_bit(wbio->dev, op->devs_need_flush->d);
838
839         if (wbio->have_ioref) {
840                 bch2_latency_acct(ca, wbio->submit_time, WRITE);
841                 percpu_ref_put(&ca->io_ref);
842         }
843
844         if (wbio->bounce)
845                 bch2_bio_free_pages_pool(c, bio);
846
847         if (wbio->put_bio)
848                 bio_put(bio);
849
850         if (parent) {
851                 bio_endio(&parent->bio);
852                 return;
853         }
854
855         closure_put(cl);
856 }
857
858 static void init_append_extent(struct bch_write_op *op,
859                                struct write_point *wp,
860                                struct bversion version,
861                                struct bch_extent_crc_unpacked crc)
862 {
863         struct bch_fs *c = op->c;
864         struct bkey_i_extent *e;
865
866         op->pos.offset += crc.uncompressed_size;
867
868         e = bkey_extent_init(op->insert_keys.top);
869         e->k.p          = op->pos;
870         e->k.size       = crc.uncompressed_size;
871         e->k.version    = version;
872
873         if (crc.csum_type ||
874             crc.compression_type ||
875             crc.nonce)
876                 bch2_extent_crc_append(&e->k_i, crc);
877
878         bch2_alloc_sectors_append_ptrs_inlined(c, wp, &e->k_i, crc.compressed_size,
879                                        op->flags & BCH_WRITE_CACHED);
880
881         bch2_keylist_push(&op->insert_keys);
882 }
883
884 static struct bio *bch2_write_bio_alloc(struct bch_fs *c,
885                                         struct write_point *wp,
886                                         struct bio *src,
887                                         bool *page_alloc_failed,
888                                         void *buf)
889 {
890         struct bch_write_bio *wbio;
891         struct bio *bio;
892         unsigned output_available =
893                 min(wp->sectors_free << 9, src->bi_iter.bi_size);
894         unsigned pages = DIV_ROUND_UP(output_available +
895                                       (buf
896                                        ? ((unsigned long) buf & (PAGE_SIZE - 1))
897                                        : 0), PAGE_SIZE);
898
899         pages = min(pages, BIO_MAX_VECS);
900
901         bio = bio_alloc_bioset(NULL, pages, 0,
902                                GFP_NOIO, &c->bio_write);
903         wbio                    = wbio_init(bio);
904         wbio->put_bio           = true;
905         /* copy WRITE_SYNC flag */
906         wbio->bio.bi_opf        = src->bi_opf;
907
908         if (buf) {
909                 bch2_bio_map(bio, buf, output_available);
910                 return bio;
911         }
912
913         wbio->bounce            = true;
914
915         /*
916          * We can't use mempool for more than c->sb.encoded_extent_max
917          * worth of pages, but we'd like to allocate more if we can:
918          */
919         bch2_bio_alloc_pages_pool(c, bio,
920                                   min_t(unsigned, output_available,
921                                         c->opts.encoded_extent_max));
922
923         if (bio->bi_iter.bi_size < output_available)
924                 *page_alloc_failed =
925                         bch2_bio_alloc_pages(bio,
926                                              output_available -
927                                              bio->bi_iter.bi_size,
928                                              GFP_NOFS) != 0;
929
930         return bio;
931 }
932
933 static int bch2_write_rechecksum(struct bch_fs *c,
934                                  struct bch_write_op *op,
935                                  unsigned new_csum_type)
936 {
937         struct bio *bio = &op->wbio.bio;
938         struct bch_extent_crc_unpacked new_crc;
939         int ret;
940
941         /* bch2_rechecksum_bio() can't encrypt or decrypt data: */
942
943         if (bch2_csum_type_is_encryption(op->crc.csum_type) !=
944             bch2_csum_type_is_encryption(new_csum_type))
945                 new_csum_type = op->crc.csum_type;
946
947         ret = bch2_rechecksum_bio(c, bio, op->version, op->crc,
948                                   NULL, &new_crc,
949                                   op->crc.offset, op->crc.live_size,
950                                   new_csum_type);
951         if (ret)
952                 return ret;
953
954         bio_advance(bio, op->crc.offset << 9);
955         bio->bi_iter.bi_size = op->crc.live_size << 9;
956         op->crc = new_crc;
957         return 0;
958 }
959
960 static int bch2_write_decrypt(struct bch_write_op *op)
961 {
962         struct bch_fs *c = op->c;
963         struct nonce nonce = extent_nonce(op->version, op->crc);
964         struct bch_csum csum;
965         int ret;
966
967         if (!bch2_csum_type_is_encryption(op->crc.csum_type))
968                 return 0;
969
970         /*
971          * If we need to decrypt data in the write path, we'll no longer be able
972          * to verify the existing checksum (poly1305 mac, in this case) after
973          * it's decrypted - this is the last point we'll be able to reverify the
974          * checksum:
975          */
976         csum = bch2_checksum_bio(c, op->crc.csum_type, nonce, &op->wbio.bio);
977         if (bch2_crc_cmp(op->crc.csum, csum))
978                 return -EIO;
979
980         ret = bch2_encrypt_bio(c, op->crc.csum_type, nonce, &op->wbio.bio);
981         op->crc.csum_type = 0;
982         op->crc.csum = (struct bch_csum) { 0, 0 };
983         return ret;
984 }
985
986 static enum prep_encoded_ret {
987         PREP_ENCODED_OK,
988         PREP_ENCODED_ERR,
989         PREP_ENCODED_CHECKSUM_ERR,
990         PREP_ENCODED_DO_WRITE,
991 } bch2_write_prep_encoded_data(struct bch_write_op *op, struct write_point *wp)
992 {
993         struct bch_fs *c = op->c;
994         struct bio *bio = &op->wbio.bio;
995
996         if (!(op->flags & BCH_WRITE_DATA_ENCODED))
997                 return PREP_ENCODED_OK;
998
999         BUG_ON(bio_sectors(bio) != op->crc.compressed_size);
1000
1001         /* Can we just write the entire extent as is? */
1002         if (op->crc.uncompressed_size == op->crc.live_size &&
1003             op->crc.compressed_size <= wp->sectors_free &&
1004             (op->crc.compression_type == op->compression_type ||
1005              op->incompressible)) {
1006                 if (!crc_is_compressed(op->crc) &&
1007                     op->csum_type != op->crc.csum_type &&
1008                     bch2_write_rechecksum(c, op, op->csum_type))
1009                         return PREP_ENCODED_CHECKSUM_ERR;
1010
1011                 return PREP_ENCODED_DO_WRITE;
1012         }
1013
1014         /*
1015          * If the data is compressed and we couldn't write the entire extent as
1016          * is, we have to decompress it:
1017          */
1018         if (crc_is_compressed(op->crc)) {
1019                 struct bch_csum csum;
1020
1021                 if (bch2_write_decrypt(op))
1022                         return PREP_ENCODED_CHECKSUM_ERR;
1023
1024                 /* Last point we can still verify checksum: */
1025                 csum = bch2_checksum_bio(c, op->crc.csum_type,
1026                                          extent_nonce(op->version, op->crc),
1027                                          bio);
1028                 if (bch2_crc_cmp(op->crc.csum, csum))
1029                         return PREP_ENCODED_CHECKSUM_ERR;
1030
1031                 if (bch2_bio_uncompress_inplace(c, bio, &op->crc))
1032                         return PREP_ENCODED_ERR;
1033         }
1034
1035         /*
1036          * No longer have compressed data after this point - data might be
1037          * encrypted:
1038          */
1039
1040         /*
1041          * If the data is checksummed and we're only writing a subset,
1042          * rechecksum and adjust bio to point to currently live data:
1043          */
1044         if ((op->crc.live_size != op->crc.uncompressed_size ||
1045              op->crc.csum_type != op->csum_type) &&
1046             bch2_write_rechecksum(c, op, op->csum_type))
1047                 return PREP_ENCODED_CHECKSUM_ERR;
1048
1049         /*
1050          * If we want to compress the data, it has to be decrypted:
1051          */
1052         if ((op->compression_type ||
1053              bch2_csum_type_is_encryption(op->crc.csum_type) !=
1054              bch2_csum_type_is_encryption(op->csum_type)) &&
1055             bch2_write_decrypt(op))
1056                 return PREP_ENCODED_CHECKSUM_ERR;
1057
1058         return PREP_ENCODED_OK;
1059 }
1060
1061 static int bch2_write_extent(struct bch_write_op *op, struct write_point *wp,
1062                              struct bio **_dst)
1063 {
1064         struct bch_fs *c = op->c;
1065         struct bio *src = &op->wbio.bio, *dst = src;
1066         struct bvec_iter saved_iter;
1067         void *ec_buf;
1068         unsigned total_output = 0, total_input = 0;
1069         bool bounce = false;
1070         bool page_alloc_failed = false;
1071         int ret, more = 0;
1072
1073         BUG_ON(!bio_sectors(src));
1074
1075         ec_buf = bch2_writepoint_ec_buf(c, wp);
1076
1077         switch (bch2_write_prep_encoded_data(op, wp)) {
1078         case PREP_ENCODED_OK:
1079                 break;
1080         case PREP_ENCODED_ERR:
1081                 ret = -EIO;
1082                 goto err;
1083         case PREP_ENCODED_CHECKSUM_ERR:
1084                 goto csum_err;
1085         case PREP_ENCODED_DO_WRITE:
1086                 /* XXX look for bug here */
1087                 if (ec_buf) {
1088                         dst = bch2_write_bio_alloc(c, wp, src,
1089                                                    &page_alloc_failed,
1090                                                    ec_buf);
1091                         bio_copy_data(dst, src);
1092                         bounce = true;
1093                 }
1094                 init_append_extent(op, wp, op->version, op->crc);
1095                 goto do_write;
1096         }
1097
1098         if (ec_buf ||
1099             op->compression_type ||
1100             (op->csum_type &&
1101              !(op->flags & BCH_WRITE_PAGES_STABLE)) ||
1102             (bch2_csum_type_is_encryption(op->csum_type) &&
1103              !(op->flags & BCH_WRITE_PAGES_OWNED))) {
1104                 dst = bch2_write_bio_alloc(c, wp, src,
1105                                            &page_alloc_failed,
1106                                            ec_buf);
1107                 bounce = true;
1108         }
1109
1110         saved_iter = dst->bi_iter;
1111
1112         do {
1113                 struct bch_extent_crc_unpacked crc = { 0 };
1114                 struct bversion version = op->version;
1115                 size_t dst_len, src_len;
1116
1117                 if (page_alloc_failed &&
1118                     dst->bi_iter.bi_size  < (wp->sectors_free << 9) &&
1119                     dst->bi_iter.bi_size < c->opts.encoded_extent_max)
1120                         break;
1121
1122                 BUG_ON(op->compression_type &&
1123                        (op->flags & BCH_WRITE_DATA_ENCODED) &&
1124                        bch2_csum_type_is_encryption(op->crc.csum_type));
1125                 BUG_ON(op->compression_type && !bounce);
1126
1127                 crc.compression_type = op->incompressible
1128                         ? BCH_COMPRESSION_TYPE_incompressible
1129                         : op->compression_type
1130                         ? bch2_bio_compress(c, dst, &dst_len, src, &src_len,
1131                                             op->compression_type)
1132                         : 0;
1133                 if (!crc_is_compressed(crc)) {
1134                         dst_len = min(dst->bi_iter.bi_size, src->bi_iter.bi_size);
1135                         dst_len = min_t(unsigned, dst_len, wp->sectors_free << 9);
1136
1137                         if (op->csum_type)
1138                                 dst_len = min_t(unsigned, dst_len,
1139                                                 c->opts.encoded_extent_max);
1140
1141                         if (bounce) {
1142                                 swap(dst->bi_iter.bi_size, dst_len);
1143                                 bio_copy_data(dst, src);
1144                                 swap(dst->bi_iter.bi_size, dst_len);
1145                         }
1146
1147                         src_len = dst_len;
1148                 }
1149
1150                 BUG_ON(!src_len || !dst_len);
1151
1152                 if (bch2_csum_type_is_encryption(op->csum_type)) {
1153                         if (bversion_zero(version)) {
1154                                 version.lo = atomic64_inc_return(&c->key_version);
1155                         } else {
1156                                 crc.nonce = op->nonce;
1157                                 op->nonce += src_len >> 9;
1158                         }
1159                 }
1160
1161                 if ((op->flags & BCH_WRITE_DATA_ENCODED) &&
1162                     !crc_is_compressed(crc) &&
1163                     bch2_csum_type_is_encryption(op->crc.csum_type) ==
1164                     bch2_csum_type_is_encryption(op->csum_type)) {
1165                         u8 compression_type = crc.compression_type;
1166                         u16 nonce = crc.nonce;
1167                         /*
1168                          * Note: when we're using rechecksum(), we need to be
1169                          * checksumming @src because it has all the data our
1170                          * existing checksum covers - if we bounced (because we
1171                          * were trying to compress), @dst will only have the
1172                          * part of the data the new checksum will cover.
1173                          *
1174                          * But normally we want to be checksumming post bounce,
1175                          * because part of the reason for bouncing is so the
1176                          * data can't be modified (by userspace) while it's in
1177                          * flight.
1178                          */
1179                         if (bch2_rechecksum_bio(c, src, version, op->crc,
1180                                         &crc, &op->crc,
1181                                         src_len >> 9,
1182                                         bio_sectors(src) - (src_len >> 9),
1183                                         op->csum_type))
1184                                 goto csum_err;
1185                         /*
1186                          * rchecksum_bio sets compression_type on crc from op->crc,
1187                          * this isn't always correct as sometimes we're changing
1188                          * an extent from uncompressed to incompressible.
1189                          */
1190                         crc.compression_type = compression_type;
1191                         crc.nonce = nonce;
1192                 } else {
1193                         if ((op->flags & BCH_WRITE_DATA_ENCODED) &&
1194                             bch2_rechecksum_bio(c, src, version, op->crc,
1195                                         NULL, &op->crc,
1196                                         src_len >> 9,
1197                                         bio_sectors(src) - (src_len >> 9),
1198                                         op->crc.csum_type))
1199                                 goto csum_err;
1200
1201                         crc.compressed_size     = dst_len >> 9;
1202                         crc.uncompressed_size   = src_len >> 9;
1203                         crc.live_size           = src_len >> 9;
1204
1205                         swap(dst->bi_iter.bi_size, dst_len);
1206                         ret = bch2_encrypt_bio(c, op->csum_type,
1207                                                extent_nonce(version, crc), dst);
1208                         if (ret)
1209                                 goto err;
1210
1211                         crc.csum = bch2_checksum_bio(c, op->csum_type,
1212                                          extent_nonce(version, crc), dst);
1213                         crc.csum_type = op->csum_type;
1214                         swap(dst->bi_iter.bi_size, dst_len);
1215                 }
1216
1217                 init_append_extent(op, wp, version, crc);
1218
1219                 if (dst != src)
1220                         bio_advance(dst, dst_len);
1221                 bio_advance(src, src_len);
1222                 total_output    += dst_len;
1223                 total_input     += src_len;
1224         } while (dst->bi_iter.bi_size &&
1225                  src->bi_iter.bi_size &&
1226                  wp->sectors_free &&
1227                  !bch2_keylist_realloc(&op->insert_keys,
1228                                       op->inline_keys,
1229                                       ARRAY_SIZE(op->inline_keys),
1230                                       BKEY_EXTENT_U64s_MAX));
1231
1232         more = src->bi_iter.bi_size != 0;
1233
1234         dst->bi_iter = saved_iter;
1235
1236         if (dst == src && more) {
1237                 BUG_ON(total_output != total_input);
1238
1239                 dst = bio_split(src, total_input >> 9,
1240                                 GFP_NOIO, &c->bio_write);
1241                 wbio_init(dst)->put_bio = true;
1242                 /* copy WRITE_SYNC flag */
1243                 dst->bi_opf             = src->bi_opf;
1244         }
1245
1246         dst->bi_iter.bi_size = total_output;
1247 do_write:
1248         *_dst = dst;
1249         return more;
1250 csum_err:
1251         bch_err(c, "error verifying existing checksum while rewriting existing data (memory corruption?)");
1252         ret = -EIO;
1253 err:
1254         if (to_wbio(dst)->bounce)
1255                 bch2_bio_free_pages_pool(c, dst);
1256         if (to_wbio(dst)->put_bio)
1257                 bio_put(dst);
1258
1259         return ret;
1260 }
1261
1262 static bool bch2_extent_is_writeable(struct bch_write_op *op,
1263                                      struct bkey_s_c k)
1264 {
1265         struct bch_fs *c = op->c;
1266         struct bkey_s_c_extent e;
1267         struct extent_ptr_decoded p;
1268         const union bch_extent_entry *entry;
1269         unsigned replicas = 0;
1270
1271         if (k.k->type != KEY_TYPE_extent)
1272                 return false;
1273
1274         e = bkey_s_c_to_extent(k);
1275         extent_for_each_ptr_decode(e, p, entry) {
1276                 if (p.crc.csum_type ||
1277                     crc_is_compressed(p.crc) ||
1278                     p.has_ec)
1279                         return false;
1280
1281                 replicas += bch2_extent_ptr_durability(c, &p);
1282         }
1283
1284         return replicas >= op->opts.data_replicas;
1285 }
1286
1287 static inline void bch2_nocow_write_unlock(struct bch_write_op *op)
1288 {
1289         struct bch_fs *c = op->c;
1290         const struct bch_extent_ptr *ptr;
1291         struct bkey_i *k;
1292
1293         for_each_keylist_key(&op->insert_keys, k) {
1294                 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(bkey_i_to_s_c(k));
1295
1296                 bkey_for_each_ptr(ptrs, ptr)
1297                         bch2_bucket_nocow_unlock(&c->nocow_locks,
1298                                                PTR_BUCKET_POS(c, ptr),
1299                                                BUCKET_NOCOW_LOCK_UPDATE);
1300         }
1301 }
1302
1303 static int bch2_nocow_write_convert_one_unwritten(struct btree_trans *trans,
1304                                                   struct btree_iter *iter,
1305                                                   struct bkey_i *orig,
1306                                                   struct bkey_s_c k,
1307                                                   u64 new_i_size)
1308 {
1309         struct bkey_i *new;
1310         struct bkey_ptrs ptrs;
1311         struct bch_extent_ptr *ptr;
1312         int ret;
1313
1314         if (!bch2_extents_match(bkey_i_to_s_c(orig), k)) {
1315                 /* trace this */
1316                 return 0;
1317         }
1318
1319         new = bch2_bkey_make_mut(trans, k);
1320         ret = PTR_ERR_OR_ZERO(new);
1321         if (ret)
1322                 return ret;
1323
1324         bch2_cut_front(bkey_start_pos(&orig->k), new);
1325         bch2_cut_back(orig->k.p, new);
1326
1327         ptrs = bch2_bkey_ptrs(bkey_i_to_s(new));
1328         bkey_for_each_ptr(ptrs, ptr)
1329                 ptr->unwritten = 0;
1330
1331         /*
1332          * Note that we're not calling bch2_subvol_get_snapshot() in this path -
1333          * that was done when we kicked off the write, and here it's important
1334          * that we update the extent that we wrote to - even if a snapshot has
1335          * since been created. The write is still outstanding, so we're ok
1336          * w.r.t. snapshot atomicity:
1337          */
1338         return  bch2_extent_update_i_size_sectors(trans, iter,
1339                                         min(new->k.p.offset << 9, new_i_size), 0) ?:
1340                 bch2_trans_update(trans, iter, new,
1341                                   BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1342 }
1343
1344 static void bch2_nocow_write_convert_unwritten(struct bch_write_op *op)
1345 {
1346         struct bch_fs *c = op->c;
1347         struct btree_trans trans;
1348         struct btree_iter iter;
1349         struct bkey_i *orig;
1350         struct bkey_s_c k;
1351         int ret;
1352
1353         bch2_trans_init(&trans, c, 0, 0);
1354
1355         for_each_keylist_key(&op->insert_keys, orig) {
1356                 ret = for_each_btree_key_upto_commit(&trans, iter, BTREE_ID_extents,
1357                                      bkey_start_pos(&orig->k), orig->k.p,
1358                                      BTREE_ITER_INTENT, k,
1359                                      NULL, NULL, BTREE_INSERT_NOFAIL, ({
1360                         BUG_ON(bkey_ge(bkey_start_pos(k.k), orig->k.p));
1361
1362                         bch2_nocow_write_convert_one_unwritten(&trans, &iter, orig, k, op->new_i_size);
1363                 }));
1364
1365                 if (ret) {
1366                         struct bkey_i *k = bch2_keylist_front(&op->insert_keys);
1367
1368                         bch_err_inum_offset_ratelimited(c,
1369                                 k->k.p.inode, k->k.p.offset << 9,
1370                                 "write error while doing btree update: %s",
1371                                 bch2_err_str(ret));
1372                         op->error = ret;
1373                         break;
1374                 }
1375         }
1376
1377         bch2_trans_exit(&trans);
1378 }
1379
1380 static void __bch2_nocow_write_done(struct bch_write_op *op)
1381 {
1382         bch2_nocow_write_unlock(op);
1383
1384         if (unlikely(op->flags & BCH_WRITE_IO_ERROR)) {
1385                 op->error = -EIO;
1386         } else if (unlikely(op->flags & BCH_WRITE_CONVERT_UNWRITTEN))
1387                 bch2_nocow_write_convert_unwritten(op);
1388 }
1389
1390 static void bch2_nocow_write_done(struct closure *cl)
1391 {
1392         struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
1393
1394         __bch2_nocow_write_done(op);
1395         bch2_write_done(cl);
1396 }
1397
1398 static void bch2_nocow_write(struct bch_write_op *op)
1399 {
1400         struct bch_fs *c = op->c;
1401         struct btree_trans trans;
1402         struct btree_iter iter;
1403         struct bkey_s_c k;
1404         struct bkey_ptrs_c ptrs;
1405         const struct bch_extent_ptr *ptr, *ptr2;
1406         struct {
1407                 struct bpos     b;
1408                 unsigned        gen;
1409                 two_state_lock_t *l;
1410         } buckets[BCH_REPLICAS_MAX];
1411         unsigned nr_buckets = 0;
1412         u32 snapshot;
1413         int ret, i;
1414
1415         if (op->flags & BCH_WRITE_MOVE)
1416                 return;
1417
1418         bch2_trans_init(&trans, c, 0, 0);
1419 retry:
1420         bch2_trans_begin(&trans);
1421
1422         ret = bch2_subvolume_get_snapshot(&trans, op->subvol, &snapshot);
1423         if (unlikely(ret))
1424                 goto err;
1425
1426         bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
1427                              SPOS(op->pos.inode, op->pos.offset, snapshot),
1428                              BTREE_ITER_SLOTS);
1429         while (1) {
1430                 struct bio *bio = &op->wbio.bio;
1431
1432                 nr_buckets = 0;
1433
1434                 k = bch2_btree_iter_peek_slot(&iter);
1435                 ret = bkey_err(k);
1436                 if (ret)
1437                         break;
1438
1439                 /* fall back to normal cow write path? */
1440                 if (unlikely(k.k->p.snapshot != snapshot ||
1441                              !bch2_extent_is_writeable(op, k)))
1442                         break;
1443
1444                 if (bch2_keylist_realloc(&op->insert_keys,
1445                                         op->inline_keys,
1446                                         ARRAY_SIZE(op->inline_keys),
1447                                         k.k->u64s))
1448                         break;
1449
1450                 /* Get iorefs before dropping btree locks: */
1451                 ptrs = bch2_bkey_ptrs_c(k);
1452                 bkey_for_each_ptr(ptrs, ptr) {
1453                         buckets[nr_buckets].b = PTR_BUCKET_POS(c, ptr);
1454                         buckets[nr_buckets].gen = ptr->gen;
1455                         buckets[nr_buckets].l =
1456                                 bucket_nocow_lock(&c->nocow_locks, buckets[nr_buckets].b);
1457
1458                         prefetch(buckets[nr_buckets].l);
1459                         nr_buckets++;
1460
1461                         if (unlikely(!bch2_dev_get_ioref(bch_dev_bkey_exists(c, ptr->dev), WRITE)))
1462                                 goto err_get_ioref;
1463
1464                         if (ptr->unwritten)
1465                                 op->flags |= BCH_WRITE_CONVERT_UNWRITTEN;
1466                 }
1467
1468                 /* Unlock before taking nocow locks, doing IO: */
1469                 bkey_reassemble(op->insert_keys.top, k);
1470                 bch2_trans_unlock(&trans);
1471
1472                 bch2_cut_front(op->pos, op->insert_keys.top);
1473                 if (op->flags & BCH_WRITE_CONVERT_UNWRITTEN)
1474                         bch2_cut_back(POS(op->pos.inode, op->pos.offset + bio_sectors(bio)), op->insert_keys.top);
1475
1476                 for (i = 0; i < nr_buckets; i++) {
1477                         struct bch_dev *ca = bch_dev_bkey_exists(c, buckets[i].b.inode);
1478                         two_state_lock_t *l = buckets[i].l;
1479                         bool stale;
1480
1481                         if (!bch2_two_state_trylock(l, BUCKET_NOCOW_LOCK_UPDATE))
1482                                 __bch2_bucket_nocow_lock(&c->nocow_locks, l, BUCKET_NOCOW_LOCK_UPDATE);
1483
1484                         rcu_read_lock();
1485                         stale = gen_after(*bucket_gen(ca, buckets[i].b.offset), buckets[i].gen);
1486                         rcu_read_unlock();
1487
1488                         if (unlikely(stale))
1489                                 goto err_bucket_stale;
1490                 }
1491
1492                 bio = &op->wbio.bio;
1493                 if (k.k->p.offset < op->pos.offset + bio_sectors(bio)) {
1494                         bio = bio_split(bio, k.k->p.offset - op->pos.offset,
1495                                         GFP_KERNEL, &c->bio_write);
1496                         wbio_init(bio)->put_bio = true;
1497                         bio->bi_opf = op->wbio.bio.bi_opf;
1498                 } else {
1499                         op->flags |= BCH_WRITE_DONE;
1500                 }
1501
1502                 op->pos.offset += bio_sectors(bio);
1503                 op->written += bio_sectors(bio);
1504
1505                 bio->bi_end_io  = bch2_write_endio;
1506                 bio->bi_private = &op->cl;
1507                 bio->bi_opf |= REQ_OP_WRITE;
1508                 closure_get(&op->cl);
1509                 bch2_submit_wbio_replicas(to_wbio(bio), c, BCH_DATA_user,
1510                                           op->insert_keys.top, true);
1511
1512                 bch2_keylist_push(&op->insert_keys);
1513                 if (op->flags & BCH_WRITE_DONE)
1514                         break;
1515                 bch2_btree_iter_advance(&iter);
1516         }
1517 out:
1518         bch2_trans_iter_exit(&trans, &iter);
1519 err:
1520         if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
1521                 goto retry;
1522
1523         if (ret) {
1524                 bch_err_inum_offset_ratelimited(c,
1525                                 op->pos.inode,
1526                                 op->pos.offset << 9,
1527                                 "%s: btree lookup error %s",
1528                                 __func__, bch2_err_str(ret));
1529                 op->error = ret;
1530                 op->flags |= BCH_WRITE_DONE;
1531         }
1532
1533         bch2_trans_exit(&trans);
1534
1535         /* fallback to cow write path? */
1536         if (!(op->flags & BCH_WRITE_DONE)) {
1537                 closure_sync(&op->cl);
1538                 __bch2_nocow_write_done(op);
1539                 op->insert_keys.top = op->insert_keys.keys;
1540         } else if (op->flags & BCH_WRITE_SYNC) {
1541                 closure_sync(&op->cl);
1542                 bch2_nocow_write_done(&op->cl);
1543         } else {
1544                 /*
1545                  * XXX
1546                  * needs to run out of process context because ei_quota_lock is
1547                  * a mutex
1548                  */
1549                 continue_at(&op->cl, bch2_nocow_write_done, index_update_wq(op));
1550         }
1551         return;
1552 err_get_ioref:
1553         bkey_for_each_ptr(ptrs, ptr2) {
1554                 if (ptr2 == ptr)
1555                         break;
1556
1557                 percpu_ref_put(&bch_dev_bkey_exists(c, ptr2->dev)->io_ref);
1558         }
1559
1560         /* Fall back to COW path: */
1561         goto out;
1562 err_bucket_stale:
1563         while (--i >= 0)
1564                 bch2_bucket_nocow_unlock(&c->nocow_locks,
1565                                          buckets[i].b,
1566                                          BUCKET_NOCOW_LOCK_UPDATE);
1567
1568         bkey_for_each_ptr(ptrs, ptr2)
1569                 percpu_ref_put(&bch_dev_bkey_exists(c, ptr2->dev)->io_ref);
1570
1571         /* We can retry this: */
1572         ret = BCH_ERR_transaction_restart;
1573         goto out;
1574 }
1575
1576 static void __bch2_write(struct bch_write_op *op)
1577 {
1578         struct bch_fs *c = op->c;
1579         struct write_point *wp = NULL;
1580         struct bio *bio = NULL;
1581         unsigned nofs_flags;
1582         int ret;
1583
1584         nofs_flags = memalloc_nofs_save();
1585
1586         if (unlikely(op->opts.nocow)) {
1587                 bch2_nocow_write(op);
1588                 if (op->flags & BCH_WRITE_DONE)
1589                         goto out_nofs_restore;
1590         }
1591 again:
1592         memset(&op->failed, 0, sizeof(op->failed));
1593         op->btree_update_ready = false;
1594
1595         do {
1596                 struct bkey_i *key_to_write;
1597                 unsigned key_to_write_offset = op->insert_keys.top_p -
1598                         op->insert_keys.keys_p;
1599
1600                 /* +1 for possible cache device: */
1601                 if (op->open_buckets.nr + op->nr_replicas + 1 >
1602                     ARRAY_SIZE(op->open_buckets.v))
1603                         break;
1604
1605                 if (bch2_keylist_realloc(&op->insert_keys,
1606                                         op->inline_keys,
1607                                         ARRAY_SIZE(op->inline_keys),
1608                                         BKEY_EXTENT_U64s_MAX))
1609                         break;
1610
1611                 /*
1612                  * The copygc thread is now global, which means it's no longer
1613                  * freeing up space on specific disks, which means that
1614                  * allocations for specific disks may hang arbitrarily long:
1615                  */
1616                 ret = bch2_trans_do(c, NULL, NULL, 0,
1617                         bch2_alloc_sectors_start_trans(&trans,
1618                                 op->target,
1619                                 op->opts.erasure_code && !(op->flags & BCH_WRITE_CACHED),
1620                                 op->write_point,
1621                                 &op->devs_have,
1622                                 op->nr_replicas,
1623                                 op->nr_replicas_required,
1624                                 op->alloc_reserve,
1625                                 op->flags,
1626                                 (op->flags & (BCH_WRITE_ALLOC_NOWAIT|
1627                                               BCH_WRITE_ONLY_SPECIFIED_DEVS))
1628                                 ? NULL : &op->cl, &wp));
1629                 if (unlikely(ret)) {
1630                         if (bch2_err_matches(ret, BCH_ERR_operation_blocked))
1631                                 break;
1632
1633                         goto err;
1634                 }
1635
1636                 ret = bch2_write_extent(op, wp, &bio);
1637
1638                 if (ret >= 0)
1639                         bch2_open_bucket_get(c, wp, &op->open_buckets);
1640                 bch2_alloc_sectors_done_inlined(c, wp);
1641 err:
1642                 if (ret <= 0) {
1643                         if (!(op->flags & BCH_WRITE_SYNC)) {
1644                                 spin_lock(&wp->writes_lock);
1645                                 op->wp = wp;
1646                                 list_add_tail(&op->wp_list, &wp->writes);
1647                                 spin_unlock(&wp->writes_lock);
1648                         }
1649
1650                         op->flags |= BCH_WRITE_DONE;
1651
1652                         if (ret < 0) {
1653                                 op->error = ret;
1654                                 break;
1655                         }
1656                 }
1657
1658                 bio->bi_end_io  = bch2_write_endio;
1659                 bio->bi_private = &op->cl;
1660                 bio->bi_opf |= REQ_OP_WRITE;
1661
1662                 closure_get(bio->bi_private);
1663
1664                 key_to_write = (void *) (op->insert_keys.keys_p +
1665                                          key_to_write_offset);
1666
1667                 bch2_submit_wbio_replicas(to_wbio(bio), c, BCH_DATA_user,
1668                                           key_to_write, false);
1669         } while (ret);
1670
1671         /*
1672          * Sync or no?
1673          *
1674          * If we're running asynchronously, wne may still want to block
1675          * synchronously here if we weren't able to submit all of the IO at
1676          * once, as that signals backpressure to the caller.
1677          */
1678         if ((op->flags & BCH_WRITE_SYNC) || !(op->flags & BCH_WRITE_DONE)) {
1679                 closure_sync(&op->cl);
1680                 __bch2_write_index(op);
1681
1682                 if (!(op->flags & BCH_WRITE_DONE))
1683                         goto again;
1684                 bch2_write_done(&op->cl);
1685         } else {
1686                 continue_at(&op->cl, bch2_write_index, NULL);
1687         }
1688 out_nofs_restore:
1689         memalloc_nofs_restore(nofs_flags);
1690 }
1691
1692 static void bch2_write_data_inline(struct bch_write_op *op, unsigned data_len)
1693 {
1694         struct bio *bio = &op->wbio.bio;
1695         struct bvec_iter iter;
1696         struct bkey_i_inline_data *id;
1697         unsigned sectors;
1698         int ret;
1699
1700         bch2_check_set_feature(op->c, BCH_FEATURE_inline_data);
1701
1702         ret = bch2_keylist_realloc(&op->insert_keys, op->inline_keys,
1703                                    ARRAY_SIZE(op->inline_keys),
1704                                    BKEY_U64s + DIV_ROUND_UP(data_len, 8));
1705         if (ret) {
1706                 op->error = ret;
1707                 goto err;
1708         }
1709
1710         sectors = bio_sectors(bio);
1711         op->pos.offset += sectors;
1712
1713         id = bkey_inline_data_init(op->insert_keys.top);
1714         id->k.p         = op->pos;
1715         id->k.version   = op->version;
1716         id->k.size      = sectors;
1717
1718         iter = bio->bi_iter;
1719         iter.bi_size = data_len;
1720         memcpy_from_bio(id->v.data, bio, iter);
1721
1722         while (data_len & 7)
1723                 id->v.data[data_len++] = '\0';
1724         set_bkey_val_bytes(&id->k, data_len);
1725         bch2_keylist_push(&op->insert_keys);
1726
1727         op->flags |= BCH_WRITE_WROTE_DATA_INLINE;
1728         op->flags |= BCH_WRITE_DONE;
1729
1730         __bch2_write_index(op);
1731 err:
1732         bch2_write_done(&op->cl);
1733 }
1734
1735 /**
1736  * bch_write - handle a write to a cache device or flash only volume
1737  *
1738  * This is the starting point for any data to end up in a cache device; it could
1739  * be from a normal write, or a writeback write, or a write to a flash only
1740  * volume - it's also used by the moving garbage collector to compact data in
1741  * mostly empty buckets.
1742  *
1743  * It first writes the data to the cache, creating a list of keys to be inserted
1744  * (if the data won't fit in a single open bucket, there will be multiple keys);
1745  * after the data is written it calls bch_journal, and after the keys have been
1746  * added to the next journal write they're inserted into the btree.
1747  *
1748  * If op->discard is true, instead of inserting the data it invalidates the
1749  * region of the cache represented by op->bio and op->inode.
1750  */
1751 void bch2_write(struct closure *cl)
1752 {
1753         struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
1754         struct bio *bio = &op->wbio.bio;
1755         struct bch_fs *c = op->c;
1756         unsigned data_len;
1757
1758         EBUG_ON(op->cl.parent);
1759         BUG_ON(!op->nr_replicas);
1760         BUG_ON(!op->write_point.v);
1761         BUG_ON(bkey_eq(op->pos, POS_MAX));
1762
1763         op->start_time = local_clock();
1764         bch2_keylist_init(&op->insert_keys, op->inline_keys);
1765         wbio_init(bio)->put_bio = false;
1766
1767         if (bio->bi_iter.bi_size & (c->opts.block_size - 1)) {
1768                 bch_err_inum_offset_ratelimited(c,
1769                         op->pos.inode,
1770                         op->pos.offset << 9,
1771                         "misaligned write");
1772                 op->error = -EIO;
1773                 goto err;
1774         }
1775
1776         if (c->opts.nochanges ||
1777             !percpu_ref_tryget_live(&c->writes)) {
1778                 op->error = -BCH_ERR_erofs_no_writes;
1779                 goto err;
1780         }
1781
1782         this_cpu_add(c->counters[BCH_COUNTER_io_write], bio_sectors(bio));
1783         bch2_increment_clock(c, bio_sectors(bio), WRITE);
1784
1785         data_len = min_t(u64, bio->bi_iter.bi_size,
1786                          op->new_i_size - (op->pos.offset << 9));
1787
1788         if (c->opts.inline_data &&
1789             data_len <= min(block_bytes(c) / 2, 1024U)) {
1790                 bch2_write_data_inline(op, data_len);
1791                 return;
1792         }
1793
1794         __bch2_write(op);
1795         return;
1796 err:
1797         bch2_disk_reservation_put(c, &op->res);
1798
1799         closure_debug_destroy(&op->cl);
1800         if (op->end_io)
1801                 op->end_io(op);
1802 }
1803
1804 /* Cache promotion on read */
1805
1806 struct promote_op {
1807         struct rcu_head         rcu;
1808         u64                     start_time;
1809
1810         struct rhash_head       hash;
1811         struct bpos             pos;
1812
1813         struct data_update      write;
1814         struct bio_vec          bi_inline_vecs[0]; /* must be last */
1815 };
1816
1817 static const struct rhashtable_params bch_promote_params = {
1818         .head_offset    = offsetof(struct promote_op, hash),
1819         .key_offset     = offsetof(struct promote_op, pos),
1820         .key_len        = sizeof(struct bpos),
1821 };
1822
1823 static inline bool should_promote(struct bch_fs *c, struct bkey_s_c k,
1824                                   struct bpos pos,
1825                                   struct bch_io_opts opts,
1826                                   unsigned flags)
1827 {
1828         if (!(flags & BCH_READ_MAY_PROMOTE))
1829                 return false;
1830
1831         if (!opts.promote_target)
1832                 return false;
1833
1834         if (bch2_bkey_has_target(c, k, opts.promote_target))
1835                 return false;
1836
1837         if (bkey_extent_is_unwritten(k))
1838                 return false;
1839
1840         if (bch2_target_congested(c, opts.promote_target)) {
1841                 /* XXX trace this */
1842                 return false;
1843         }
1844
1845         if (rhashtable_lookup_fast(&c->promote_table, &pos,
1846                                    bch_promote_params))
1847                 return false;
1848
1849         return true;
1850 }
1851
1852 static void promote_free(struct bch_fs *c, struct promote_op *op)
1853 {
1854         int ret;
1855
1856         ret = rhashtable_remove_fast(&c->promote_table, &op->hash,
1857                                      bch_promote_params);
1858         BUG_ON(ret);
1859         percpu_ref_put(&c->writes);
1860         kfree_rcu(op, rcu);
1861 }
1862
1863 static void promote_done(struct bch_write_op *wop)
1864 {
1865         struct promote_op *op =
1866                 container_of(wop, struct promote_op, write.op);
1867         struct bch_fs *c = op->write.op.c;
1868
1869         bch2_time_stats_update(&c->times[BCH_TIME_data_promote],
1870                                op->start_time);
1871
1872         bch2_data_update_exit(&op->write);
1873         promote_free(c, op);
1874 }
1875
1876 static void promote_start(struct promote_op *op, struct bch_read_bio *rbio)
1877 {
1878         struct bio *bio = &op->write.op.wbio.bio;
1879
1880         trace_and_count(op->write.op.c, read_promote, &rbio->bio);
1881
1882         /* we now own pages: */
1883         BUG_ON(!rbio->bounce);
1884         BUG_ON(rbio->bio.bi_vcnt > bio->bi_max_vecs);
1885
1886         memcpy(bio->bi_io_vec, rbio->bio.bi_io_vec,
1887                sizeof(struct bio_vec) * rbio->bio.bi_vcnt);
1888         swap(bio->bi_vcnt, rbio->bio.bi_vcnt);
1889
1890         bch2_data_update_read_done(&op->write, rbio->pick.crc);
1891 }
1892
1893 static struct promote_op *__promote_alloc(struct bch_fs *c,
1894                                           enum btree_id btree_id,
1895                                           struct bkey_s_c k,
1896                                           struct bpos pos,
1897                                           struct extent_ptr_decoded *pick,
1898                                           struct bch_io_opts opts,
1899                                           unsigned sectors,
1900                                           struct bch_read_bio **rbio)
1901 {
1902         struct promote_op *op = NULL;
1903         struct bio *bio;
1904         unsigned pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
1905         int ret;
1906
1907         if (!percpu_ref_tryget_live(&c->writes))
1908                 return NULL;
1909
1910         op = kzalloc(sizeof(*op) + sizeof(struct bio_vec) * pages, GFP_NOIO);
1911         if (!op)
1912                 goto err;
1913
1914         op->start_time = local_clock();
1915         op->pos = pos;
1916
1917         /*
1918          * We don't use the mempool here because extents that aren't
1919          * checksummed or compressed can be too big for the mempool:
1920          */
1921         *rbio = kzalloc(sizeof(struct bch_read_bio) +
1922                         sizeof(struct bio_vec) * pages,
1923                         GFP_NOIO);
1924         if (!*rbio)
1925                 goto err;
1926
1927         rbio_init(&(*rbio)->bio, opts);
1928         bio_init(&(*rbio)->bio, NULL, (*rbio)->bio.bi_inline_vecs, pages, 0);
1929
1930         if (bch2_bio_alloc_pages(&(*rbio)->bio, sectors << 9,
1931                                  GFP_NOIO))
1932                 goto err;
1933
1934         (*rbio)->bounce         = true;
1935         (*rbio)->split          = true;
1936         (*rbio)->kmalloc        = true;
1937
1938         if (rhashtable_lookup_insert_fast(&c->promote_table, &op->hash,
1939                                           bch_promote_params))
1940                 goto err;
1941
1942         bio = &op->write.op.wbio.bio;
1943         bio_init(bio, NULL, bio->bi_inline_vecs, pages, 0);
1944
1945         ret = bch2_data_update_init(c, &op->write,
1946                         writepoint_hashed((unsigned long) current),
1947                         opts,
1948                         (struct data_update_opts) {
1949                                 .target         = opts.promote_target,
1950                                 .extra_replicas = 1,
1951                                 .write_flags    = BCH_WRITE_ALLOC_NOWAIT|BCH_WRITE_CACHED,
1952                         },
1953                         btree_id, k);
1954         BUG_ON(ret);
1955         op->write.op.end_io = promote_done;
1956
1957         return op;
1958 err:
1959         if (*rbio)
1960                 bio_free_pages(&(*rbio)->bio);
1961         kfree(*rbio);
1962         *rbio = NULL;
1963         kfree(op);
1964         percpu_ref_put(&c->writes);
1965         return NULL;
1966 }
1967
1968 noinline
1969 static struct promote_op *promote_alloc(struct bch_fs *c,
1970                                                struct bvec_iter iter,
1971                                                struct bkey_s_c k,
1972                                                struct extent_ptr_decoded *pick,
1973                                                struct bch_io_opts opts,
1974                                                unsigned flags,
1975                                                struct bch_read_bio **rbio,
1976                                                bool *bounce,
1977                                                bool *read_full)
1978 {
1979         bool promote_full = *read_full || READ_ONCE(c->promote_whole_extents);
1980         /* data might have to be decompressed in the write path: */
1981         unsigned sectors = promote_full
1982                 ? max(pick->crc.compressed_size, pick->crc.live_size)
1983                 : bvec_iter_sectors(iter);
1984         struct bpos pos = promote_full
1985                 ? bkey_start_pos(k.k)
1986                 : POS(k.k->p.inode, iter.bi_sector);
1987         struct promote_op *promote;
1988
1989         if (!should_promote(c, k, pos, opts, flags))
1990                 return NULL;
1991
1992         promote = __promote_alloc(c,
1993                                   k.k->type == KEY_TYPE_reflink_v
1994                                   ? BTREE_ID_reflink
1995                                   : BTREE_ID_extents,
1996                                   k, pos, pick, opts, sectors, rbio);
1997         if (!promote)
1998                 return NULL;
1999
2000         *bounce         = true;
2001         *read_full      = promote_full;
2002         return promote;
2003 }
2004
2005 /* Read */
2006
2007 #define READ_RETRY_AVOID        1
2008 #define READ_RETRY              2
2009 #define READ_ERR                3
2010
2011 enum rbio_context {
2012         RBIO_CONTEXT_NULL,
2013         RBIO_CONTEXT_HIGHPRI,
2014         RBIO_CONTEXT_UNBOUND,
2015 };
2016
2017 static inline struct bch_read_bio *
2018 bch2_rbio_parent(struct bch_read_bio *rbio)
2019 {
2020         return rbio->split ? rbio->parent : rbio;
2021 }
2022
2023 __always_inline
2024 static void bch2_rbio_punt(struct bch_read_bio *rbio, work_func_t fn,
2025                            enum rbio_context context,
2026                            struct workqueue_struct *wq)
2027 {
2028         if (context <= rbio->context) {
2029                 fn(&rbio->work);
2030         } else {
2031                 rbio->work.func         = fn;
2032                 rbio->context           = context;
2033                 queue_work(wq, &rbio->work);
2034         }
2035 }
2036
2037 static inline struct bch_read_bio *bch2_rbio_free(struct bch_read_bio *rbio)
2038 {
2039         BUG_ON(rbio->bounce && !rbio->split);
2040
2041         if (rbio->promote)
2042                 promote_free(rbio->c, rbio->promote);
2043         rbio->promote = NULL;
2044
2045         if (rbio->bounce)
2046                 bch2_bio_free_pages_pool(rbio->c, &rbio->bio);
2047
2048         if (rbio->split) {
2049                 struct bch_read_bio *parent = rbio->parent;
2050
2051                 if (rbio->kmalloc)
2052                         kfree(rbio);
2053                 else
2054                         bio_put(&rbio->bio);
2055
2056                 rbio = parent;
2057         }
2058
2059         return rbio;
2060 }
2061
2062 /*
2063  * Only called on a top level bch_read_bio to complete an entire read request,
2064  * not a split:
2065  */
2066 static void bch2_rbio_done(struct bch_read_bio *rbio)
2067 {
2068         if (rbio->start_time)
2069                 bch2_time_stats_update(&rbio->c->times[BCH_TIME_data_read],
2070                                        rbio->start_time);
2071         bio_endio(&rbio->bio);
2072 }
2073
2074 static void bch2_read_retry_nodecode(struct bch_fs *c, struct bch_read_bio *rbio,
2075                                      struct bvec_iter bvec_iter,
2076                                      struct bch_io_failures *failed,
2077                                      unsigned flags)
2078 {
2079         struct btree_trans trans;
2080         struct btree_iter iter;
2081         struct bkey_buf sk;
2082         struct bkey_s_c k;
2083         int ret;
2084
2085         flags &= ~BCH_READ_LAST_FRAGMENT;
2086         flags |= BCH_READ_MUST_CLONE;
2087
2088         bch2_bkey_buf_init(&sk);
2089         bch2_trans_init(&trans, c, 0, 0);
2090
2091         bch2_trans_iter_init(&trans, &iter, rbio->data_btree,
2092                              rbio->read_pos, BTREE_ITER_SLOTS);
2093 retry:
2094         rbio->bio.bi_status = 0;
2095
2096         k = bch2_btree_iter_peek_slot(&iter);
2097         if (bkey_err(k))
2098                 goto err;
2099
2100         bch2_bkey_buf_reassemble(&sk, c, k);
2101         k = bkey_i_to_s_c(sk.k);
2102         bch2_trans_unlock(&trans);
2103
2104         if (!bch2_bkey_matches_ptr(c, k,
2105                                    rbio->pick.ptr,
2106                                    rbio->data_pos.offset -
2107                                    rbio->pick.crc.offset)) {
2108                 /* extent we wanted to read no longer exists: */
2109                 rbio->hole = true;
2110                 goto out;
2111         }
2112
2113         ret = __bch2_read_extent(&trans, rbio, bvec_iter,
2114                                  rbio->read_pos,
2115                                  rbio->data_btree,
2116                                  k, 0, failed, flags);
2117         if (ret == READ_RETRY)
2118                 goto retry;
2119         if (ret)
2120                 goto err;
2121 out:
2122         bch2_rbio_done(rbio);
2123         bch2_trans_iter_exit(&trans, &iter);
2124         bch2_trans_exit(&trans);
2125         bch2_bkey_buf_exit(&sk, c);
2126         return;
2127 err:
2128         rbio->bio.bi_status = BLK_STS_IOERR;
2129         goto out;
2130 }
2131
2132 static void bch2_rbio_retry(struct work_struct *work)
2133 {
2134         struct bch_read_bio *rbio =
2135                 container_of(work, struct bch_read_bio, work);
2136         struct bch_fs *c        = rbio->c;
2137         struct bvec_iter iter   = rbio->bvec_iter;
2138         unsigned flags          = rbio->flags;
2139         subvol_inum inum = {
2140                 .subvol = rbio->subvol,
2141                 .inum   = rbio->read_pos.inode,
2142         };
2143         struct bch_io_failures failed = { .nr = 0 };
2144
2145         trace_and_count(c, read_retry, &rbio->bio);
2146
2147         if (rbio->retry == READ_RETRY_AVOID)
2148                 bch2_mark_io_failure(&failed, &rbio->pick);
2149
2150         rbio->bio.bi_status = 0;
2151
2152         rbio = bch2_rbio_free(rbio);
2153
2154         flags |= BCH_READ_IN_RETRY;
2155         flags &= ~BCH_READ_MAY_PROMOTE;
2156
2157         if (flags & BCH_READ_NODECODE) {
2158                 bch2_read_retry_nodecode(c, rbio, iter, &failed, flags);
2159         } else {
2160                 flags &= ~BCH_READ_LAST_FRAGMENT;
2161                 flags |= BCH_READ_MUST_CLONE;
2162
2163                 __bch2_read(c, rbio, iter, inum, &failed, flags);
2164         }
2165 }
2166
2167 static void bch2_rbio_error(struct bch_read_bio *rbio, int retry,
2168                             blk_status_t error)
2169 {
2170         rbio->retry = retry;
2171
2172         if (rbio->flags & BCH_READ_IN_RETRY)
2173                 return;
2174
2175         if (retry == READ_ERR) {
2176                 rbio = bch2_rbio_free(rbio);
2177
2178                 rbio->bio.bi_status = error;
2179                 bch2_rbio_done(rbio);
2180         } else {
2181                 bch2_rbio_punt(rbio, bch2_rbio_retry,
2182                                RBIO_CONTEXT_UNBOUND, system_unbound_wq);
2183         }
2184 }
2185
2186 static int __bch2_rbio_narrow_crcs(struct btree_trans *trans,
2187                                    struct bch_read_bio *rbio)
2188 {
2189         struct bch_fs *c = rbio->c;
2190         u64 data_offset = rbio->data_pos.offset - rbio->pick.crc.offset;
2191         struct bch_extent_crc_unpacked new_crc;
2192         struct btree_iter iter;
2193         struct bkey_i *new;
2194         struct bkey_s_c k;
2195         int ret = 0;
2196
2197         if (crc_is_compressed(rbio->pick.crc))
2198                 return 0;
2199
2200         bch2_trans_iter_init(trans, &iter, rbio->data_btree, rbio->data_pos,
2201                              BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
2202         k = bch2_btree_iter_peek_slot(&iter);
2203         if ((ret = bkey_err(k)))
2204                 goto out;
2205
2206         if (bversion_cmp(k.k->version, rbio->version) ||
2207             !bch2_bkey_matches_ptr(c, k, rbio->pick.ptr, data_offset))
2208                 goto out;
2209
2210         /* Extent was merged? */
2211         if (bkey_start_offset(k.k) < data_offset ||
2212             k.k->p.offset > data_offset + rbio->pick.crc.uncompressed_size)
2213                 goto out;
2214
2215         if (bch2_rechecksum_bio(c, &rbio->bio, rbio->version,
2216                         rbio->pick.crc, NULL, &new_crc,
2217                         bkey_start_offset(k.k) - data_offset, k.k->size,
2218                         rbio->pick.crc.csum_type)) {
2219                 bch_err(c, "error verifying existing checksum while narrowing checksum (memory corruption?)");
2220                 ret = 0;
2221                 goto out;
2222         }
2223
2224         /*
2225          * going to be temporarily appending another checksum entry:
2226          */
2227         new = bch2_trans_kmalloc(trans, bkey_bytes(k.k) +
2228                                  sizeof(struct bch_extent_crc128));
2229         if ((ret = PTR_ERR_OR_ZERO(new)))
2230                 goto out;
2231
2232         bkey_reassemble(new, k);
2233
2234         if (!bch2_bkey_narrow_crcs(new, new_crc))
2235                 goto out;
2236
2237         ret = bch2_trans_update(trans, &iter, new,
2238                                 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
2239 out:
2240         bch2_trans_iter_exit(trans, &iter);
2241         return ret;
2242 }
2243
2244 static noinline void bch2_rbio_narrow_crcs(struct bch_read_bio *rbio)
2245 {
2246         bch2_trans_do(rbio->c, NULL, NULL, BTREE_INSERT_NOFAIL,
2247                       __bch2_rbio_narrow_crcs(&trans, rbio));
2248 }
2249
2250 /* Inner part that may run in process context */
2251 static void __bch2_read_endio(struct work_struct *work)
2252 {
2253         struct bch_read_bio *rbio =
2254                 container_of(work, struct bch_read_bio, work);
2255         struct bch_fs *c        = rbio->c;
2256         struct bch_dev *ca      = bch_dev_bkey_exists(c, rbio->pick.ptr.dev);
2257         struct bio *src         = &rbio->bio;
2258         struct bio *dst         = &bch2_rbio_parent(rbio)->bio;
2259         struct bvec_iter dst_iter = rbio->bvec_iter;
2260         struct bch_extent_crc_unpacked crc = rbio->pick.crc;
2261         struct nonce nonce = extent_nonce(rbio->version, crc);
2262         unsigned nofs_flags;
2263         struct bch_csum csum;
2264         int ret;
2265
2266         nofs_flags = memalloc_nofs_save();
2267
2268         /* Reset iterator for checksumming and copying bounced data: */
2269         if (rbio->bounce) {
2270                 src->bi_iter.bi_size            = crc.compressed_size << 9;
2271                 src->bi_iter.bi_idx             = 0;
2272                 src->bi_iter.bi_bvec_done       = 0;
2273         } else {
2274                 src->bi_iter                    = rbio->bvec_iter;
2275         }
2276
2277         csum = bch2_checksum_bio(c, crc.csum_type, nonce, src);
2278         if (bch2_crc_cmp(csum, rbio->pick.crc.csum))
2279                 goto csum_err;
2280
2281         /*
2282          * XXX
2283          * We need to rework the narrow_crcs path to deliver the read completion
2284          * first, and then punt to a different workqueue, otherwise we're
2285          * holding up reads while doing btree updates which is bad for memory
2286          * reclaim.
2287          */
2288         if (unlikely(rbio->narrow_crcs))
2289                 bch2_rbio_narrow_crcs(rbio);
2290
2291         if (rbio->flags & BCH_READ_NODECODE)
2292                 goto nodecode;
2293
2294         /* Adjust crc to point to subset of data we want: */
2295         crc.offset     += rbio->offset_into_extent;
2296         crc.live_size   = bvec_iter_sectors(rbio->bvec_iter);
2297
2298         if (crc_is_compressed(crc)) {
2299                 ret = bch2_encrypt_bio(c, crc.csum_type, nonce, src);
2300                 if (ret)
2301                         goto decrypt_err;
2302
2303                 if (bch2_bio_uncompress(c, src, dst, dst_iter, crc))
2304                         goto decompression_err;
2305         } else {
2306                 /* don't need to decrypt the entire bio: */
2307                 nonce = nonce_add(nonce, crc.offset << 9);
2308                 bio_advance(src, crc.offset << 9);
2309
2310                 BUG_ON(src->bi_iter.bi_size < dst_iter.bi_size);
2311                 src->bi_iter.bi_size = dst_iter.bi_size;
2312
2313                 ret = bch2_encrypt_bio(c, crc.csum_type, nonce, src);
2314                 if (ret)
2315                         goto decrypt_err;
2316
2317                 if (rbio->bounce) {
2318                         struct bvec_iter src_iter = src->bi_iter;
2319                         bio_copy_data_iter(dst, &dst_iter, src, &src_iter);
2320                 }
2321         }
2322
2323         if (rbio->promote) {
2324                 /*
2325                  * Re encrypt data we decrypted, so it's consistent with
2326                  * rbio->crc:
2327                  */
2328                 ret = bch2_encrypt_bio(c, crc.csum_type, nonce, src);
2329                 if (ret)
2330                         goto decrypt_err;
2331
2332                 promote_start(rbio->promote, rbio);
2333                 rbio->promote = NULL;
2334         }
2335 nodecode:
2336         if (likely(!(rbio->flags & BCH_READ_IN_RETRY))) {
2337                 rbio = bch2_rbio_free(rbio);
2338                 bch2_rbio_done(rbio);
2339         }
2340 out:
2341         memalloc_nofs_restore(nofs_flags);
2342         return;
2343 csum_err:
2344         /*
2345          * Checksum error: if the bio wasn't bounced, we may have been
2346          * reading into buffers owned by userspace (that userspace can
2347          * scribble over) - retry the read, bouncing it this time:
2348          */
2349         if (!rbio->bounce && (rbio->flags & BCH_READ_USER_MAPPED)) {
2350                 rbio->flags |= BCH_READ_MUST_BOUNCE;
2351                 bch2_rbio_error(rbio, READ_RETRY, BLK_STS_IOERR);
2352                 goto out;
2353         }
2354
2355         bch_err_inum_offset_ratelimited(ca,
2356                 rbio->read_pos.inode,
2357                 rbio->read_pos.offset << 9,
2358                 "data checksum error: expected %0llx:%0llx got %0llx:%0llx (type %s)",
2359                 rbio->pick.crc.csum.hi, rbio->pick.crc.csum.lo,
2360                 csum.hi, csum.lo, bch2_csum_types[crc.csum_type]);
2361         bch2_io_error(ca);
2362         bch2_rbio_error(rbio, READ_RETRY_AVOID, BLK_STS_IOERR);
2363         goto out;
2364 decompression_err:
2365         bch_err_inum_offset_ratelimited(c, rbio->read_pos.inode,
2366                                         rbio->read_pos.offset << 9,
2367                                         "decompression error");
2368         bch2_rbio_error(rbio, READ_ERR, BLK_STS_IOERR);
2369         goto out;
2370 decrypt_err:
2371         bch_err_inum_offset_ratelimited(c, rbio->read_pos.inode,
2372                                         rbio->read_pos.offset << 9,
2373                                         "decrypt error");
2374         bch2_rbio_error(rbio, READ_ERR, BLK_STS_IOERR);
2375         goto out;
2376 }
2377
2378 static void bch2_read_endio(struct bio *bio)
2379 {
2380         struct bch_read_bio *rbio =
2381                 container_of(bio, struct bch_read_bio, bio);
2382         struct bch_fs *c        = rbio->c;
2383         struct bch_dev *ca      = bch_dev_bkey_exists(c, rbio->pick.ptr.dev);
2384         struct workqueue_struct *wq = NULL;
2385         enum rbio_context context = RBIO_CONTEXT_NULL;
2386
2387         if (rbio->have_ioref) {
2388                 bch2_latency_acct(ca, rbio->submit_time, READ);
2389                 percpu_ref_put(&ca->io_ref);
2390         }
2391
2392         if (!rbio->split)
2393                 rbio->bio.bi_end_io = rbio->end_io;
2394
2395         if (bch2_dev_inum_io_err_on(bio->bi_status, ca,
2396                                     rbio->read_pos.inode,
2397                                     rbio->read_pos.offset,
2398                                     "data read error: %s",
2399                                bch2_blk_status_to_str(bio->bi_status))) {
2400                 bch2_rbio_error(rbio, READ_RETRY_AVOID, bio->bi_status);
2401                 return;
2402         }
2403
2404         if (((rbio->flags & BCH_READ_RETRY_IF_STALE) && race_fault()) ||
2405             ptr_stale(ca, &rbio->pick.ptr)) {
2406                 trace_and_count(c, read_reuse_race, &rbio->bio);
2407
2408                 if (rbio->flags & BCH_READ_RETRY_IF_STALE)
2409                         bch2_rbio_error(rbio, READ_RETRY, BLK_STS_AGAIN);
2410                 else
2411                         bch2_rbio_error(rbio, READ_ERR, BLK_STS_AGAIN);
2412                 return;
2413         }
2414
2415         if (rbio->narrow_crcs ||
2416             rbio->promote ||
2417             crc_is_compressed(rbio->pick.crc) ||
2418             bch2_csum_type_is_encryption(rbio->pick.crc.csum_type))
2419                 context = RBIO_CONTEXT_UNBOUND, wq = system_unbound_wq;
2420         else if (rbio->pick.crc.csum_type)
2421                 context = RBIO_CONTEXT_HIGHPRI, wq = system_highpri_wq;
2422
2423         bch2_rbio_punt(rbio, __bch2_read_endio, context, wq);
2424 }
2425
2426 int __bch2_read_indirect_extent(struct btree_trans *trans,
2427                                 unsigned *offset_into_extent,
2428                                 struct bkey_buf *orig_k)
2429 {
2430         struct btree_iter iter;
2431         struct bkey_s_c k;
2432         u64 reflink_offset;
2433         int ret;
2434
2435         reflink_offset = le64_to_cpu(bkey_i_to_reflink_p(orig_k->k)->v.idx) +
2436                 *offset_into_extent;
2437
2438         bch2_trans_iter_init(trans, &iter, BTREE_ID_reflink,
2439                              POS(0, reflink_offset),
2440                              BTREE_ITER_SLOTS);
2441         k = bch2_btree_iter_peek_slot(&iter);
2442         ret = bkey_err(k);
2443         if (ret)
2444                 goto err;
2445
2446         if (k.k->type != KEY_TYPE_reflink_v &&
2447             k.k->type != KEY_TYPE_indirect_inline_data) {
2448                 bch_err_inum_offset_ratelimited(trans->c,
2449                         orig_k->k->k.p.inode,
2450                         orig_k->k->k.p.offset << 9,
2451                         "%llu len %u points to nonexistent indirect extent %llu",
2452                         orig_k->k->k.p.offset,
2453                         orig_k->k->k.size,
2454                         reflink_offset);
2455                 bch2_inconsistent_error(trans->c);
2456                 ret = -EIO;
2457                 goto err;
2458         }
2459
2460         *offset_into_extent = iter.pos.offset - bkey_start_offset(k.k);
2461         bch2_bkey_buf_reassemble(orig_k, trans->c, k);
2462 err:
2463         bch2_trans_iter_exit(trans, &iter);
2464         return ret;
2465 }
2466
2467 static noinline void read_from_stale_dirty_pointer(struct btree_trans *trans,
2468                                                    struct bkey_s_c k,
2469                                                    struct bch_extent_ptr ptr)
2470 {
2471         struct bch_fs *c = trans->c;
2472         struct bch_dev *ca = bch_dev_bkey_exists(c, ptr.dev);
2473         struct btree_iter iter;
2474         struct printbuf buf = PRINTBUF;
2475         int ret;
2476
2477         bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
2478                              PTR_BUCKET_POS(c, &ptr),
2479                              BTREE_ITER_CACHED);
2480
2481         prt_printf(&buf, "Attempting to read from stale dirty pointer:");
2482         printbuf_indent_add(&buf, 2);
2483         prt_newline(&buf);
2484
2485         bch2_bkey_val_to_text(&buf, c, k);
2486         prt_newline(&buf);
2487
2488         prt_printf(&buf, "memory gen: %u", *bucket_gen(ca, iter.pos.offset));
2489
2490         ret = lockrestart_do(trans, bkey_err(k = bch2_btree_iter_peek_slot(&iter)));
2491         if (!ret) {
2492                 prt_newline(&buf);
2493                 bch2_bkey_val_to_text(&buf, c, k);
2494         }
2495
2496         bch2_fs_inconsistent(c, "%s", buf.buf);
2497
2498         bch2_trans_iter_exit(trans, &iter);
2499         printbuf_exit(&buf);
2500 }
2501
2502 int __bch2_read_extent(struct btree_trans *trans, struct bch_read_bio *orig,
2503                        struct bvec_iter iter, struct bpos read_pos,
2504                        enum btree_id data_btree, struct bkey_s_c k,
2505                        unsigned offset_into_extent,
2506                        struct bch_io_failures *failed, unsigned flags)
2507 {
2508         struct bch_fs *c = trans->c;
2509         struct extent_ptr_decoded pick;
2510         struct bch_read_bio *rbio = NULL;
2511         struct bch_dev *ca = NULL;
2512         struct promote_op *promote = NULL;
2513         bool bounce = false, read_full = false, narrow_crcs = false;
2514         struct bpos data_pos = bkey_start_pos(k.k);
2515         int pick_ret;
2516
2517         if (bkey_extent_is_inline_data(k.k)) {
2518                 unsigned bytes = min_t(unsigned, iter.bi_size,
2519                                        bkey_inline_data_bytes(k.k));
2520
2521                 swap(iter.bi_size, bytes);
2522                 memcpy_to_bio(&orig->bio, iter, bkey_inline_data_p(k));
2523                 swap(iter.bi_size, bytes);
2524                 bio_advance_iter(&orig->bio, &iter, bytes);
2525                 zero_fill_bio_iter(&orig->bio, iter);
2526                 goto out_read_done;
2527         }
2528 retry_pick:
2529         pick_ret = bch2_bkey_pick_read_device(c, k, failed, &pick);
2530
2531         /* hole or reservation - just zero fill: */
2532         if (!pick_ret)
2533                 goto hole;
2534
2535         if (pick_ret < 0) {
2536                 bch_err_inum_offset_ratelimited(c,
2537                                 read_pos.inode, read_pos.offset << 9,
2538                                 "no device to read from");
2539                 goto err;
2540         }
2541
2542         ca = bch_dev_bkey_exists(c, pick.ptr.dev);
2543
2544         /*
2545          * Stale dirty pointers are treated as IO errors, but @failed isn't
2546          * allocated unless we're in the retry path - so if we're not in the
2547          * retry path, don't check here, it'll be caught in bch2_read_endio()
2548          * and we'll end up in the retry path:
2549          */
2550         if ((flags & BCH_READ_IN_RETRY) &&
2551             !pick.ptr.cached &&
2552             unlikely(ptr_stale(ca, &pick.ptr))) {
2553                 read_from_stale_dirty_pointer(trans, k, pick.ptr);
2554                 bch2_mark_io_failure(failed, &pick);
2555                 goto retry_pick;
2556         }
2557
2558         /*
2559          * Unlock the iterator while the btree node's lock is still in
2560          * cache, before doing the IO:
2561          */
2562         bch2_trans_unlock(trans);
2563
2564         if (flags & BCH_READ_NODECODE) {
2565                 /*
2566                  * can happen if we retry, and the extent we were going to read
2567                  * has been merged in the meantime:
2568                  */
2569                 if (pick.crc.compressed_size > orig->bio.bi_vcnt * PAGE_SECTORS)
2570                         goto hole;
2571
2572                 iter.bi_size    = pick.crc.compressed_size << 9;
2573                 goto get_bio;
2574         }
2575
2576         if (!(flags & BCH_READ_LAST_FRAGMENT) ||
2577             bio_flagged(&orig->bio, BIO_CHAIN))
2578                 flags |= BCH_READ_MUST_CLONE;
2579
2580         narrow_crcs = !(flags & BCH_READ_IN_RETRY) &&
2581                 bch2_can_narrow_extent_crcs(k, pick.crc);
2582
2583         if (narrow_crcs && (flags & BCH_READ_USER_MAPPED))
2584                 flags |= BCH_READ_MUST_BOUNCE;
2585
2586         EBUG_ON(offset_into_extent + bvec_iter_sectors(iter) > k.k->size);
2587
2588         if (crc_is_compressed(pick.crc) ||
2589             (pick.crc.csum_type != BCH_CSUM_none &&
2590              (bvec_iter_sectors(iter) != pick.crc.uncompressed_size ||
2591               (bch2_csum_type_is_encryption(pick.crc.csum_type) &&
2592                (flags & BCH_READ_USER_MAPPED)) ||
2593               (flags & BCH_READ_MUST_BOUNCE)))) {
2594                 read_full = true;
2595                 bounce = true;
2596         }
2597
2598         if (orig->opts.promote_target)
2599                 promote = promote_alloc(c, iter, k, &pick, orig->opts, flags,
2600                                         &rbio, &bounce, &read_full);
2601
2602         if (!read_full) {
2603                 EBUG_ON(crc_is_compressed(pick.crc));
2604                 EBUG_ON(pick.crc.csum_type &&
2605                         (bvec_iter_sectors(iter) != pick.crc.uncompressed_size ||
2606                          bvec_iter_sectors(iter) != pick.crc.live_size ||
2607                          pick.crc.offset ||
2608                          offset_into_extent));
2609
2610                 data_pos.offset += offset_into_extent;
2611                 pick.ptr.offset += pick.crc.offset +
2612                         offset_into_extent;
2613                 offset_into_extent              = 0;
2614                 pick.crc.compressed_size        = bvec_iter_sectors(iter);
2615                 pick.crc.uncompressed_size      = bvec_iter_sectors(iter);
2616                 pick.crc.offset                 = 0;
2617                 pick.crc.live_size              = bvec_iter_sectors(iter);
2618                 offset_into_extent              = 0;
2619         }
2620 get_bio:
2621         if (rbio) {
2622                 /*
2623                  * promote already allocated bounce rbio:
2624                  * promote needs to allocate a bio big enough for uncompressing
2625                  * data in the write path, but we're not going to use it all
2626                  * here:
2627                  */
2628                 EBUG_ON(rbio->bio.bi_iter.bi_size <
2629                        pick.crc.compressed_size << 9);
2630                 rbio->bio.bi_iter.bi_size =
2631                         pick.crc.compressed_size << 9;
2632         } else if (bounce) {
2633                 unsigned sectors = pick.crc.compressed_size;
2634
2635                 rbio = rbio_init(bio_alloc_bioset(NULL,
2636                                                   DIV_ROUND_UP(sectors, PAGE_SECTORS),
2637                                                   0,
2638                                                   GFP_NOIO,
2639                                                   &c->bio_read_split),
2640                                  orig->opts);
2641
2642                 bch2_bio_alloc_pages_pool(c, &rbio->bio, sectors << 9);
2643                 rbio->bounce    = true;
2644                 rbio->split     = true;
2645         } else if (flags & BCH_READ_MUST_CLONE) {
2646                 /*
2647                  * Have to clone if there were any splits, due to error
2648                  * reporting issues (if a split errored, and retrying didn't
2649                  * work, when it reports the error to its parent (us) we don't
2650                  * know if the error was from our bio, and we should retry, or
2651                  * from the whole bio, in which case we don't want to retry and
2652                  * lose the error)
2653                  */
2654                 rbio = rbio_init(bio_alloc_clone(NULL, &orig->bio, GFP_NOIO,
2655                                                  &c->bio_read_split),
2656                                  orig->opts);
2657                 rbio->bio.bi_iter = iter;
2658                 rbio->split     = true;
2659         } else {
2660                 rbio = orig;
2661                 rbio->bio.bi_iter = iter;
2662                 EBUG_ON(bio_flagged(&rbio->bio, BIO_CHAIN));
2663         }
2664
2665         EBUG_ON(bio_sectors(&rbio->bio) != pick.crc.compressed_size);
2666
2667         rbio->c                 = c;
2668         rbio->submit_time       = local_clock();
2669         if (rbio->split)
2670                 rbio->parent    = orig;
2671         else
2672                 rbio->end_io    = orig->bio.bi_end_io;
2673         rbio->bvec_iter         = iter;
2674         rbio->offset_into_extent= offset_into_extent;
2675         rbio->flags             = flags;
2676         rbio->have_ioref        = pick_ret > 0 && bch2_dev_get_ioref(ca, READ);
2677         rbio->narrow_crcs       = narrow_crcs;
2678         rbio->hole              = 0;
2679         rbio->retry             = 0;
2680         rbio->context           = 0;
2681         /* XXX: only initialize this if needed */
2682         rbio->devs_have         = bch2_bkey_devs(k);
2683         rbio->pick              = pick;
2684         rbio->subvol            = orig->subvol;
2685         rbio->read_pos          = read_pos;
2686         rbio->data_btree        = data_btree;
2687         rbio->data_pos          = data_pos;
2688         rbio->version           = k.k->version;
2689         rbio->promote           = promote;
2690         INIT_WORK(&rbio->work, NULL);
2691
2692         rbio->bio.bi_opf        = orig->bio.bi_opf;
2693         rbio->bio.bi_iter.bi_sector = pick.ptr.offset;
2694         rbio->bio.bi_end_io     = bch2_read_endio;
2695
2696         if (rbio->bounce)
2697                 trace_and_count(c, read_bounce, &rbio->bio);
2698
2699         this_cpu_add(c->counters[BCH_COUNTER_io_read], bio_sectors(&rbio->bio));
2700         bch2_increment_clock(c, bio_sectors(&rbio->bio), READ);
2701
2702         /*
2703          * If it's being moved internally, we don't want to flag it as a cache
2704          * hit:
2705          */
2706         if (pick.ptr.cached && !(flags & BCH_READ_NODECODE))
2707                 bch2_bucket_io_time_reset(trans, pick.ptr.dev,
2708                         PTR_BUCKET_NR(ca, &pick.ptr), READ);
2709
2710         if (!(flags & (BCH_READ_IN_RETRY|BCH_READ_LAST_FRAGMENT))) {
2711                 bio_inc_remaining(&orig->bio);
2712                 trace_and_count(c, read_split, &orig->bio);
2713         }
2714
2715         if (!rbio->pick.idx) {
2716                 if (!rbio->have_ioref) {
2717                         bch_err_inum_offset_ratelimited(c,
2718                                         read_pos.inode,
2719                                         read_pos.offset << 9,
2720                                         "no device to read from");
2721                         bch2_rbio_error(rbio, READ_RETRY_AVOID, BLK_STS_IOERR);
2722                         goto out;
2723                 }
2724
2725                 this_cpu_add(ca->io_done->sectors[READ][BCH_DATA_user],
2726                              bio_sectors(&rbio->bio));
2727                 bio_set_dev(&rbio->bio, ca->disk_sb.bdev);
2728
2729                 if (likely(!(flags & BCH_READ_IN_RETRY)))
2730                         submit_bio(&rbio->bio);
2731                 else
2732                         submit_bio_wait(&rbio->bio);
2733         } else {
2734                 /* Attempting reconstruct read: */
2735                 if (bch2_ec_read_extent(c, rbio)) {
2736                         bch2_rbio_error(rbio, READ_RETRY_AVOID, BLK_STS_IOERR);
2737                         goto out;
2738                 }
2739
2740                 if (likely(!(flags & BCH_READ_IN_RETRY)))
2741                         bio_endio(&rbio->bio);
2742         }
2743 out:
2744         if (likely(!(flags & BCH_READ_IN_RETRY))) {
2745                 return 0;
2746         } else {
2747                 int ret;
2748
2749                 rbio->context = RBIO_CONTEXT_UNBOUND;
2750                 bch2_read_endio(&rbio->bio);
2751
2752                 ret = rbio->retry;
2753                 rbio = bch2_rbio_free(rbio);
2754
2755                 if (ret == READ_RETRY_AVOID) {
2756                         bch2_mark_io_failure(failed, &pick);
2757                         ret = READ_RETRY;
2758                 }
2759
2760                 if (!ret)
2761                         goto out_read_done;
2762
2763                 return ret;
2764         }
2765
2766 err:
2767         if (flags & BCH_READ_IN_RETRY)
2768                 return READ_ERR;
2769
2770         orig->bio.bi_status = BLK_STS_IOERR;
2771         goto out_read_done;
2772
2773 hole:
2774         /*
2775          * won't normally happen in the BCH_READ_NODECODE
2776          * (bch2_move_extent()) path, but if we retry and the extent we wanted
2777          * to read no longer exists we have to signal that:
2778          */
2779         if (flags & BCH_READ_NODECODE)
2780                 orig->hole = true;
2781
2782         zero_fill_bio_iter(&orig->bio, iter);
2783 out_read_done:
2784         if (flags & BCH_READ_LAST_FRAGMENT)
2785                 bch2_rbio_done(orig);
2786         return 0;
2787 }
2788
2789 void __bch2_read(struct bch_fs *c, struct bch_read_bio *rbio,
2790                  struct bvec_iter bvec_iter, subvol_inum inum,
2791                  struct bch_io_failures *failed, unsigned flags)
2792 {
2793         struct btree_trans trans;
2794         struct btree_iter iter;
2795         struct bkey_buf sk;
2796         struct bkey_s_c k;
2797         u32 snapshot;
2798         int ret;
2799
2800         BUG_ON(flags & BCH_READ_NODECODE);
2801
2802         bch2_bkey_buf_init(&sk);
2803         bch2_trans_init(&trans, c, 0, 0);
2804 retry:
2805         bch2_trans_begin(&trans);
2806         iter = (struct btree_iter) { NULL };
2807
2808         ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
2809         if (ret)
2810                 goto err;
2811
2812         bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
2813                              SPOS(inum.inum, bvec_iter.bi_sector, snapshot),
2814                              BTREE_ITER_SLOTS);
2815         while (1) {
2816                 unsigned bytes, sectors, offset_into_extent;
2817                 enum btree_id data_btree = BTREE_ID_extents;
2818
2819                 /*
2820                  * read_extent -> io_time_reset may cause a transaction restart
2821                  * without returning an error, we need to check for that here:
2822                  */
2823                 ret = bch2_trans_relock(&trans);
2824                 if (ret)
2825                         break;
2826
2827                 bch2_btree_iter_set_pos(&iter,
2828                                 POS(inum.inum, bvec_iter.bi_sector));
2829
2830                 k = bch2_btree_iter_peek_slot(&iter);
2831                 ret = bkey_err(k);
2832                 if (ret)
2833                         break;
2834
2835                 offset_into_extent = iter.pos.offset -
2836                         bkey_start_offset(k.k);
2837                 sectors = k.k->size - offset_into_extent;
2838
2839                 bch2_bkey_buf_reassemble(&sk, c, k);
2840
2841                 ret = bch2_read_indirect_extent(&trans, &data_btree,
2842                                         &offset_into_extent, &sk);
2843                 if (ret)
2844                         break;
2845
2846                 k = bkey_i_to_s_c(sk.k);
2847
2848                 /*
2849                  * With indirect extents, the amount of data to read is the min
2850                  * of the original extent and the indirect extent:
2851                  */
2852                 sectors = min(sectors, k.k->size - offset_into_extent);
2853
2854                 bytes = min(sectors, bvec_iter_sectors(bvec_iter)) << 9;
2855                 swap(bvec_iter.bi_size, bytes);
2856
2857                 if (bvec_iter.bi_size == bytes)
2858                         flags |= BCH_READ_LAST_FRAGMENT;
2859
2860                 ret = __bch2_read_extent(&trans, rbio, bvec_iter, iter.pos,
2861                                          data_btree, k,
2862                                          offset_into_extent, failed, flags);
2863                 if (ret)
2864                         break;
2865
2866                 if (flags & BCH_READ_LAST_FRAGMENT)
2867                         break;
2868
2869                 swap(bvec_iter.bi_size, bytes);
2870                 bio_advance_iter(&rbio->bio, &bvec_iter, bytes);
2871
2872                 ret = btree_trans_too_many_iters(&trans);
2873                 if (ret)
2874                         break;
2875         }
2876 err:
2877         bch2_trans_iter_exit(&trans, &iter);
2878
2879         if (bch2_err_matches(ret, BCH_ERR_transaction_restart) ||
2880             ret == READ_RETRY ||
2881             ret == READ_RETRY_AVOID)
2882                 goto retry;
2883
2884         bch2_trans_exit(&trans);
2885         bch2_bkey_buf_exit(&sk, c);
2886
2887         if (ret) {
2888                 bch_err_inum_offset_ratelimited(c, inum.inum,
2889                                                 bvec_iter.bi_sector << 9,
2890                                                 "read error %i from btree lookup", ret);
2891                 rbio->bio.bi_status = BLK_STS_IOERR;
2892                 bch2_rbio_done(rbio);
2893         }
2894 }
2895
2896 void bch2_fs_io_exit(struct bch_fs *c)
2897 {
2898         if (c->promote_table.tbl)
2899                 rhashtable_destroy(&c->promote_table);
2900         mempool_exit(&c->bio_bounce_pages);
2901         bioset_exit(&c->bio_write);
2902         bioset_exit(&c->bio_read_split);
2903         bioset_exit(&c->bio_read);
2904 }
2905
2906 int bch2_fs_io_init(struct bch_fs *c)
2907 {
2908         unsigned i;
2909
2910         for (i = 0; i < ARRAY_SIZE(c->nocow_locks.l); i++)
2911                 two_state_lock_init(&c->nocow_locks.l[i]);
2912
2913         if (bioset_init(&c->bio_read, 1, offsetof(struct bch_read_bio, bio),
2914                         BIOSET_NEED_BVECS) ||
2915             bioset_init(&c->bio_read_split, 1, offsetof(struct bch_read_bio, bio),
2916                         BIOSET_NEED_BVECS) ||
2917             bioset_init(&c->bio_write, 1, offsetof(struct bch_write_bio, bio),
2918                         BIOSET_NEED_BVECS) ||
2919             mempool_init_page_pool(&c->bio_bounce_pages,
2920                                    max_t(unsigned,
2921                                          c->opts.btree_node_size,
2922                                          c->opts.encoded_extent_max) /
2923                                    PAGE_SIZE, 0) ||
2924             rhashtable_init(&c->promote_table, &bch_promote_params))
2925                 return -ENOMEM;
2926
2927         return 0;
2928 }