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