]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/lightnvm/pblk-cache.c
nfp: flower: ensure dead neighbour entries are not offloaded
[linux.git] / drivers / lightnvm / pblk-cache.c
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 {
22         struct request_queue *q = pblk->dev->q;
23         struct pblk_w_ctx w_ctx;
24         sector_t lba = pblk_get_lba(bio);
25         unsigned long start_time = jiffies;
26         unsigned int bpos, pos;
27         int nr_entries = pblk_get_secs(bio);
28         int i, ret;
29
30         generic_start_io_acct(q, WRITE, bio_sectors(bio), &pblk->disk->part0);
31
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);
38         switch (ret) {
39         case NVM_IO_REQUEUE:
40                 io_schedule();
41                 goto retry;
42         case NVM_IO_ERR:
43                 pblk_pipeline_stop(pblk);
44                 goto out;
45         }
46
47         pblk_ppa_set_empty(&w_ctx.ppa);
48         w_ctx.flags = flags;
49         if (bio->bi_opf & REQ_PREFLUSH) {
50                 w_ctx.flags |= PBLK_FLUSH_ENTRY;
51                 pblk_write_kick(pblk);
52         }
53
54         if (unlikely(!bio_has_data(bio)))
55                 goto out;
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
68         atomic64_add(nr_entries, &pblk->user_wa);
69
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
75         pblk_rl_inserted(&pblk->rl, nr_entries);
76
77 out:
78         generic_end_io_acct(q, WRITE, &pblk->disk->part0, start_time);
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  */
87 int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq)
88 {
89         struct pblk_w_ctx w_ctx;
90         unsigned int bpos, pos;
91         void *data = gc_rq->data;
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:
99         if (!pblk_rb_may_write_gc(&pblk->rwb, gc_rq->secs_to_gc, &bpos)) {
100                 io_schedule();
101                 goto retry;
102         }
103
104         w_ctx.flags = PBLK_IOTYPE_GC;
105         pblk_ppa_set_empty(&w_ctx.ppa);
106
107         for (i = 0, valid_entries = 0; i < gc_rq->nr_secs; i++) {
108                 if (gc_rq->lba_list[i] == ADDR_EMPTY)
109                         continue;
110
111                 w_ctx.lba = gc_rq->lba_list[i];
112
113                 pos = pblk_rb_wrap_pos(&pblk->rwb, bpos + valid_entries);
114                 pblk_rb_write_entry_gc(&pblk->rwb, data, w_ctx, gc_rq->line,
115                                                 gc_rq->paddr_list[i], pos);
116
117                 data += PBLK_EXPOSED_PAGE_SIZE;
118                 valid_entries++;
119         }
120
121         WARN_ONCE(gc_rq->secs_to_gc != valid_entries,
122                                         "pblk: inconsistent GC write\n");
123
124         atomic64_add(valid_entries, &pblk->gc_wa);
125
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 }