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