Merge tag 'for-gkh' of git://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma
[linux/fpc-iii.git] / drivers / lightnvm / pblk-cache.c
blobf565a56b898ab0760d36cc7999a1ef75039d70de
1 /*
2 * Copyright (C) 2016 CNEX Labs
3 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
4 * Matias Bjorling <matias@cnexlabs.com>
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.
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.
15 * pblk-cache.c - pblk's write cache
18 #include "pblk.h"
20 int pblk_write_to_cache(struct pblk *pblk, struct bio *bio, unsigned long flags)
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;
30 generic_start_io_acct(q, REQ_OP_WRITE, bio_sectors(bio),
31 &pblk->disk->part0);
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.
37 retry:
38 ret = pblk_rb_may_write_user(&pblk->rwb, bio, nr_entries, &bpos);
39 switch (ret) {
40 case NVM_IO_REQUEUE:
41 io_schedule();
42 goto retry;
43 case NVM_IO_ERR:
44 pblk_pipeline_stop(pblk);
45 goto out;
48 pblk_ppa_set_empty(&w_ctx.ppa);
49 w_ctx.flags = flags;
50 if (bio->bi_opf & REQ_PREFLUSH) {
51 w_ctx.flags |= PBLK_FLUSH_ENTRY;
52 pblk_write_kick(pblk);
55 if (unlikely(!bio_has_data(bio)))
56 goto out;
58 for (i = 0; i < nr_entries; i++) {
59 void *data = bio_data(bio);
61 w_ctx.lba = lba + i;
63 pos = pblk_rb_wrap_pos(&pblk->rwb, bpos + i);
64 pblk_rb_write_entry_user(&pblk->rwb, data, w_ctx, pos);
66 bio_advance(bio, PBLK_EXPOSED_PAGE_SIZE);
69 atomic64_add(nr_entries, &pblk->user_wa);
71 #ifdef CONFIG_NVM_PBLK_DEBUG
72 atomic_long_add(nr_entries, &pblk->inflight_writes);
73 atomic_long_add(nr_entries, &pblk->req_writes);
74 #endif
76 pblk_rl_inserted(&pblk->rl, nr_entries);
78 out:
79 generic_end_io_acct(q, REQ_OP_WRITE, &pblk->disk->part0, start_time);
80 pblk_write_should_kick(pblk);
81 return ret;
85 * On GC the incoming lbas are not necessarily sequential. Also, some of the
86 * lbas might not be valid entries, which are marked as empty by the GC thread
88 int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq)
90 struct pblk_w_ctx w_ctx;
91 unsigned int bpos, pos;
92 void *data = gc_rq->data;
93 int i, valid_entries;
95 /* Update the write buffer head (mem) with the entries that we can
96 * write. The write in itself cannot fail, so there is no need to
97 * rollback from here on.
99 retry:
100 if (!pblk_rb_may_write_gc(&pblk->rwb, gc_rq->secs_to_gc, &bpos)) {
101 io_schedule();
102 goto retry;
105 w_ctx.flags = PBLK_IOTYPE_GC;
106 pblk_ppa_set_empty(&w_ctx.ppa);
108 for (i = 0, valid_entries = 0; i < gc_rq->nr_secs; i++) {
109 if (gc_rq->lba_list[i] == ADDR_EMPTY)
110 continue;
112 w_ctx.lba = gc_rq->lba_list[i];
114 pos = pblk_rb_wrap_pos(&pblk->rwb, bpos + valid_entries);
115 pblk_rb_write_entry_gc(&pblk->rwb, data, w_ctx, gc_rq->line,
116 gc_rq->paddr_list[i], pos);
118 data += PBLK_EXPOSED_PAGE_SIZE;
119 valid_entries++;
122 WARN_ONCE(gc_rq->secs_to_gc != valid_entries,
123 "pblk: inconsistent GC write\n");
125 atomic64_add(valid_entries, &pblk->gc_wa);
127 #ifdef CONFIG_NVM_PBLK_DEBUG
128 atomic_long_add(valid_entries, &pblk->inflight_writes);
129 atomic_long_add(valid_entries, &pblk->recov_gc_writes);
130 #endif
132 pblk_write_should_kick(pblk);
133 return NVM_IO_OK;