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