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