^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2016 CNEX Labs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Initial release: Javier Gonzalez <javier@cnexlabs.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Matias Bjorling <matias@cnexlabs.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * This program is free software; you can redistribute it and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * modify it under the terms of the GNU General Public License version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * 2 as published by the Free Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * This program is distributed in the hope that it will be useful, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * pblk-cache.c - pblk's write cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "pblk.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) void pblk_write_to_cache(struct pblk *pblk, struct bio *bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct pblk_w_ctx w_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) sector_t lba = pblk_get_lba(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) unsigned long start_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) unsigned int bpos, pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) int nr_entries = pblk_get_secs(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) start_time = bio_start_io_acct(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /* Update the write buffer head (mem) with the entries that we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * write. The write in itself cannot fail, so there is no need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * rollback from here on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) ret = pblk_rb_may_write_user(&pblk->rwb, bio, nr_entries, &bpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) case NVM_IO_REQUEUE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) io_schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) case NVM_IO_ERR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) pblk_pipeline_stop(pblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) bio_io_error(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) pblk_ppa_set_empty(&w_ctx.ppa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) w_ctx.flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (bio->bi_opf & REQ_PREFLUSH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) w_ctx.flags |= PBLK_FLUSH_ENTRY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) pblk_write_kick(pblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (unlikely(!bio_has_data(bio)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) for (i = 0; i < nr_entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) void *data = bio_data(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) w_ctx.lba = lba + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) pos = pblk_rb_wrap_pos(&pblk->rwb, bpos + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) pblk_rb_write_entry_user(&pblk->rwb, data, w_ctx, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) bio_advance(bio, PBLK_EXPOSED_PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) atomic64_add(nr_entries, &pblk->user_wa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #ifdef CONFIG_NVM_PBLK_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) atomic_long_add(nr_entries, &pblk->inflight_writes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) atomic_long_add(nr_entries, &pblk->req_writes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) pblk_rl_inserted(&pblk->rl, nr_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) bio_end_io_acct(bio, start_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) pblk_write_should_kick(pblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (ret == NVM_IO_DONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) bio_endio(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * On GC the incoming lbas are not necessarily sequential. Also, some of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * lbas might not be valid entries, which are marked as empty by the GC thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct pblk_w_ctx w_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) unsigned int bpos, pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) void *data = gc_rq->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) int i, valid_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* Update the write buffer head (mem) with the entries that we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * write. The write in itself cannot fail, so there is no need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * rollback from here on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (!pblk_rb_may_write_gc(&pblk->rwb, gc_rq->secs_to_gc, &bpos)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) io_schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) w_ctx.flags = PBLK_IOTYPE_GC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) pblk_ppa_set_empty(&w_ctx.ppa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) for (i = 0, valid_entries = 0; i < gc_rq->nr_secs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (gc_rq->lba_list[i] == ADDR_EMPTY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) w_ctx.lba = gc_rq->lba_list[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) pos = pblk_rb_wrap_pos(&pblk->rwb, bpos + valid_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) pblk_rb_write_entry_gc(&pblk->rwb, data, w_ctx, gc_rq->line,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) gc_rq->paddr_list[i], pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) data += PBLK_EXPOSED_PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) valid_entries++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) WARN_ONCE(gc_rq->secs_to_gc != valid_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) "pblk: inconsistent GC write\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) atomic64_add(valid_entries, &pblk->gc_wa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) #ifdef CONFIG_NVM_PBLK_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) atomic_long_add(valid_entries, &pblk->inflight_writes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) atomic_long_add(valid_entries, &pblk->recov_gc_writes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) pblk_write_should_kick(pblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return NVM_IO_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }