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