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