Commit | Line | Data |
---|---|---|
02a1520d | 1 | // SPDX-License-Identifier: GPL-2.0 |
a4bd217b JG |
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 | ||
3e03f632 IK |
21 | void pblk_write_to_cache(struct pblk *pblk, struct bio *bio, |
22 | unsigned long flags) | |
a4bd217b | 23 | { |
998ba629 | 24 | struct request_queue *q = pblk->dev->q; |
a4bd217b JG |
25 | struct pblk_w_ctx w_ctx; |
26 | sector_t lba = pblk_get_lba(bio); | |
998ba629 | 27 | unsigned long start_time = jiffies; |
a4bd217b JG |
28 | unsigned int bpos, pos; |
29 | int nr_entries = pblk_get_secs(bio); | |
30 | int i, ret; | |
31 | ||
ddcf35d3 MC |
32 | generic_start_io_acct(q, REQ_OP_WRITE, bio_sectors(bio), |
33 | &pblk->disk->part0); | |
998ba629 | 34 | |
a4bd217b JG |
35 | /* Update the write buffer head (mem) with the entries that we can |
36 | * write. The write in itself cannot fail, so there is no need to | |
37 | * rollback from here on. | |
38 | */ | |
39 | retry: | |
40 | ret = pblk_rb_may_write_user(&pblk->rwb, bio, nr_entries, &bpos); | |
588726d3 JG |
41 | switch (ret) { |
42 | case NVM_IO_REQUEUE: | |
a4bd217b JG |
43 | io_schedule(); |
44 | goto retry; | |
588726d3 JG |
45 | case NVM_IO_ERR: |
46 | pblk_pipeline_stop(pblk); | |
3e03f632 | 47 | bio_io_error(bio); |
588726d3 | 48 | goto out; |
a4bd217b JG |
49 | } |
50 | ||
a4bd217b | 51 | pblk_ppa_set_empty(&w_ctx.ppa); |
6ca2f71f | 52 | w_ctx.flags = flags; |
cc9c9a00 | 53 | if (bio->bi_opf & REQ_PREFLUSH) { |
6ca2f71f | 54 | w_ctx.flags |= PBLK_FLUSH_ENTRY; |
cc9c9a00 HH |
55 | pblk_write_kick(pblk); |
56 | } | |
57 | ||
58 | if (unlikely(!bio_has_data(bio))) | |
59 | goto out; | |
a4bd217b JG |
60 | |
61 | for (i = 0; i < nr_entries; i++) { | |
62 | void *data = bio_data(bio); | |
63 | ||
64 | w_ctx.lba = lba + i; | |
65 | ||
66 | pos = pblk_rb_wrap_pos(&pblk->rwb, bpos + i); | |
67 | pblk_rb_write_entry_user(&pblk->rwb, data, w_ctx, pos); | |
68 | ||
69 | bio_advance(bio, PBLK_EXPOSED_PAGE_SIZE); | |
70 | } | |
71 | ||
76758390 HH |
72 | atomic64_add(nr_entries, &pblk->user_wa); |
73 | ||
880eda54 | 74 | #ifdef CONFIG_NVM_PBLK_DEBUG |
a4bd217b JG |
75 | atomic_long_add(nr_entries, &pblk->inflight_writes); |
76 | atomic_long_add(nr_entries, &pblk->req_writes); | |
77 | #endif | |
78 | ||
588726d3 JG |
79 | pblk_rl_inserted(&pblk->rl, nr_entries); |
80 | ||
a4bd217b | 81 | out: |
ddcf35d3 | 82 | generic_end_io_acct(q, REQ_OP_WRITE, &pblk->disk->part0, start_time); |
a4bd217b | 83 | pblk_write_should_kick(pblk); |
3e03f632 IK |
84 | |
85 | if (ret == NVM_IO_DONE) | |
86 | bio_endio(bio); | |
a4bd217b JG |
87 | } |
88 | ||
89 | /* | |
90 | * On GC the incoming lbas are not necessarily sequential. Also, some of the | |
91 | * lbas might not be valid entries, which are marked as empty by the GC thread | |
92 | */ | |
d340121e | 93 | int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq) |
a4bd217b JG |
94 | { |
95 | struct pblk_w_ctx w_ctx; | |
96 | unsigned int bpos, pos; | |
d340121e | 97 | void *data = gc_rq->data; |
a4bd217b JG |
98 | int i, valid_entries; |
99 | ||
100 | /* Update the write buffer head (mem) with the entries that we can | |
101 | * write. The write in itself cannot fail, so there is no need to | |
102 | * rollback from here on. | |
103 | */ | |
104 | retry: | |
d340121e | 105 | if (!pblk_rb_may_write_gc(&pblk->rwb, gc_rq->secs_to_gc, &bpos)) { |
a4bd217b JG |
106 | io_schedule(); |
107 | goto retry; | |
108 | } | |
109 | ||
d340121e | 110 | w_ctx.flags = PBLK_IOTYPE_GC; |
a4bd217b JG |
111 | pblk_ppa_set_empty(&w_ctx.ppa); |
112 | ||
d340121e JG |
113 | for (i = 0, valid_entries = 0; i < gc_rq->nr_secs; i++) { |
114 | if (gc_rq->lba_list[i] == ADDR_EMPTY) | |
a4bd217b JG |
115 | continue; |
116 | ||
d340121e | 117 | w_ctx.lba = gc_rq->lba_list[i]; |
a4bd217b JG |
118 | |
119 | pos = pblk_rb_wrap_pos(&pblk->rwb, bpos + valid_entries); | |
d340121e JG |
120 | pblk_rb_write_entry_gc(&pblk->rwb, data, w_ctx, gc_rq->line, |
121 | gc_rq->paddr_list[i], pos); | |
a4bd217b JG |
122 | |
123 | data += PBLK_EXPOSED_PAGE_SIZE; | |
124 | valid_entries++; | |
125 | } | |
126 | ||
d340121e | 127 | WARN_ONCE(gc_rq->secs_to_gc != valid_entries, |
a4bd217b JG |
128 | "pblk: inconsistent GC write\n"); |
129 | ||
76758390 HH |
130 | atomic64_add(valid_entries, &pblk->gc_wa); |
131 | ||
880eda54 | 132 | #ifdef CONFIG_NVM_PBLK_DEBUG |
a4bd217b JG |
133 | atomic_long_add(valid_entries, &pblk->inflight_writes); |
134 | atomic_long_add(valid_entries, &pblk->recov_gc_writes); | |
135 | #endif | |
136 | ||
137 | pblk_write_should_kick(pblk); | |
138 | return NVM_IO_OK; | |
139 | } |