blob: 0d227ef7d1b95ad65fc5e1bfc3700ccca201da35 [file] [log] [blame]
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001/*
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
20int pblk_write_to_cache(struct pblk *pblk, struct bio *bio, unsigned long flags)
21{
22 struct pblk_w_ctx w_ctx;
23 sector_t lba = pblk_get_lba(bio);
24 unsigned int bpos, pos;
25 int nr_entries = pblk_get_secs(bio);
26 int i, ret;
27
28 /* Update the write buffer head (mem) with the entries that we can
29 * write. The write in itself cannot fail, so there is no need to
30 * rollback from here on.
31 */
32retry:
33 ret = pblk_rb_may_write_user(&pblk->rwb, bio, nr_entries, &bpos);
Javier González588726d32017-06-26 11:57:29 +020034 switch (ret) {
35 case NVM_IO_REQUEUE:
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020036 io_schedule();
37 goto retry;
Javier González588726d32017-06-26 11:57:29 +020038 case NVM_IO_ERR:
39 pblk_pipeline_stop(pblk);
40 goto out;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020041 }
42
43 if (unlikely(!bio_has_data(bio)))
44 goto out;
45
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020046 pblk_ppa_set_empty(&w_ctx.ppa);
Javier González6ca2f712017-10-13 14:46:17 +020047 w_ctx.flags = flags;
48 if (bio->bi_opf & REQ_PREFLUSH)
49 w_ctx.flags |= PBLK_FLUSH_ENTRY;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020050
51 for (i = 0; i < nr_entries; i++) {
52 void *data = bio_data(bio);
53
54 w_ctx.lba = lba + i;
55
56 pos = pblk_rb_wrap_pos(&pblk->rwb, bpos + i);
57 pblk_rb_write_entry_user(&pblk->rwb, data, w_ctx, pos);
58
59 bio_advance(bio, PBLK_EXPOSED_PAGE_SIZE);
60 }
61
62#ifdef CONFIG_NVM_DEBUG
63 atomic_long_add(nr_entries, &pblk->inflight_writes);
64 atomic_long_add(nr_entries, &pblk->req_writes);
65#endif
66
Javier González588726d32017-06-26 11:57:29 +020067 pblk_rl_inserted(&pblk->rl, nr_entries);
68
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020069out:
70 pblk_write_should_kick(pblk);
71 return ret;
72}
73
74/*
75 * On GC the incoming lbas are not necessarily sequential. Also, some of the
76 * lbas might not be valid entries, which are marked as empty by the GC thread
77 */
Javier Gonzálezd3401212017-10-13 14:46:14 +020078int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020079{
80 struct pblk_w_ctx w_ctx;
81 unsigned int bpos, pos;
Javier Gonzálezd3401212017-10-13 14:46:14 +020082 void *data = gc_rq->data;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020083 int i, valid_entries;
84
85 /* Update the write buffer head (mem) with the entries that we can
86 * write. The write in itself cannot fail, so there is no need to
87 * rollback from here on.
88 */
89retry:
Javier Gonzálezd3401212017-10-13 14:46:14 +020090 if (!pblk_rb_may_write_gc(&pblk->rwb, gc_rq->secs_to_gc, &bpos)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020091 io_schedule();
92 goto retry;
93 }
94
Javier Gonzálezd3401212017-10-13 14:46:14 +020095 w_ctx.flags = PBLK_IOTYPE_GC;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020096 pblk_ppa_set_empty(&w_ctx.ppa);
97
Javier Gonzálezd3401212017-10-13 14:46:14 +020098 for (i = 0, valid_entries = 0; i < gc_rq->nr_secs; i++) {
99 if (gc_rq->lba_list[i] == ADDR_EMPTY)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200100 continue;
101
Javier Gonzálezd3401212017-10-13 14:46:14 +0200102 w_ctx.lba = gc_rq->lba_list[i];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200103
104 pos = pblk_rb_wrap_pos(&pblk->rwb, bpos + valid_entries);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200105 pblk_rb_write_entry_gc(&pblk->rwb, data, w_ctx, gc_rq->line,
106 gc_rq->paddr_list[i], pos);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200107
108 data += PBLK_EXPOSED_PAGE_SIZE;
109 valid_entries++;
110 }
111
Javier Gonzálezd3401212017-10-13 14:46:14 +0200112 WARN_ONCE(gc_rq->secs_to_gc != valid_entries,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200113 "pblk: inconsistent GC write\n");
114
115#ifdef CONFIG_NVM_DEBUG
116 atomic_long_add(valid_entries, &pblk->inflight_writes);
117 atomic_long_add(valid_entries, &pblk->recov_gc_writes);
118#endif
119
120 pblk_write_should_kick(pblk);
121 return NVM_IO_OK;
122}