pblk-map.c 5.47 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (C) 2016 CNEX Labs
 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
 *                  Matias Bjorling <matias@cnexlabs.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * pblk-map.c - pblk's lba-ppa mapping strategy
 *
 */

#include "pblk.h"

22 23 24 25 26
static int pblk_map_page_data(struct pblk *pblk, unsigned int sentry,
			      struct ppa_addr *ppa_list,
			      unsigned long *lun_bitmap,
			      struct pblk_sec_meta *meta_list,
			      unsigned int valid_secs)
27 28
{
	struct pblk_line *line = pblk_line_get_data(pblk);
29
	struct pblk_emeta *emeta;
30
	struct pblk_w_ctx *w_ctx;
31
	__le64 *lba_list;
32 33 34 35
	u64 paddr;
	int nr_secs = pblk->min_write_pgs;
	int i;

36 37 38
	if (pblk_line_is_full(line)) {
		struct pblk_line *prev_line = line;

39 40 41
		/* If we cannot allocate a new line, make sure to store metadata
		 * on current line and then fail
		 */
42 43
		line = pblk_line_replace_data(pblk);
		pblk_line_close_meta(pblk, prev_line);
44 45 46

		if (!line)
			return -EINTR;
47 48 49 50 51
	}

	emeta = line->emeta;
	lba_list = emeta_to_lbas(pblk, emeta->buf);

52 53 54
	paddr = pblk_alloc_page(pblk, line, nr_secs);

	for (i = 0; i < nr_secs; i++, paddr++) {
55 56
		__le64 addr_empty = cpu_to_le64(ADDR_EMPTY);

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
		/* ppa to be sent to the device */
		ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id);

		/* Write context for target bio completion on write buffer. Note
		 * that the write buffer is protected by the sync backpointer,
		 * and a single writer thread have access to each specific entry
		 * at a time. Thus, it is safe to modify the context for the
		 * entry we are setting up for submission without taking any
		 * lock or memory barrier.
		 */
		if (i < valid_secs) {
			kref_get(&line->ref);
			w_ctx = pblk_rb_w_ctx(&pblk->rwb, sentry + i);
			w_ctx->ppa = ppa_list[i];
			meta_list[i].lba = cpu_to_le64(w_ctx->lba);
			lba_list[paddr] = cpu_to_le64(w_ctx->lba);
73 74
			if (lba_list[paddr] != addr_empty)
				line->nr_valid_lbas++;
75 76
			else
				atomic64_inc(&pblk->pad_wa);
77
		} else {
78
			lba_list[paddr] = meta_list[i].lba = addr_empty;
79
			__pblk_map_invalidate(pblk, line, paddr);
80 81 82
		}
	}

83
	pblk_down_rq(pblk, ppa_list[0], lun_bitmap);
84
	return 0;
85 86 87 88 89 90 91
}

void pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry,
		 unsigned long *lun_bitmap, unsigned int valid_secs,
		 unsigned int off)
{
	struct pblk_sec_meta *meta_list = rqd->meta_list;
92
	struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
93 94 95 96 97 98
	unsigned int map_secs;
	int min = pblk->min_write_pgs;
	int i;

	for (i = off; i < rqd->nr_ppas; i += min) {
		map_secs = (i + min > valid_secs) ? (valid_secs % min) : min;
99
		if (pblk_map_page_data(pblk, sentry + i, &ppa_list[i],
100 101 102 103 104
					lun_bitmap, &meta_list[i], map_secs)) {
			bio_put(rqd->bio);
			pblk_free_rqd(pblk, rqd, PBLK_WRITE);
			pblk_pipeline_stop(pblk);
		}
105 106 107 108 109 110 111 112 113 114
	}
}

/* only if erase_ppa is set, acquire erase semaphore */
void pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd,
		       unsigned int sentry, unsigned long *lun_bitmap,
		       unsigned int valid_secs, struct ppa_addr *erase_ppa)
{
	struct nvm_tgt_dev *dev = pblk->dev;
	struct nvm_geo *geo = &dev->geo;
115
	struct pblk_line_meta *lm = &pblk->lm;
116
	struct pblk_sec_meta *meta_list = rqd->meta_list;
117
	struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
118
	struct pblk_line *e_line, *d_line;
119 120 121 122 123 124
	unsigned int map_secs;
	int min = pblk->min_write_pgs;
	int i, erase_lun;

	for (i = 0; i < rqd->nr_ppas; i += min) {
		map_secs = (i + min > valid_secs) ? (valid_secs % min) : min;
125
		if (pblk_map_page_data(pblk, sentry + i, &ppa_list[i],
126 127 128 129 130
					lun_bitmap, &meta_list[i], map_secs)) {
			bio_put(rqd->bio);
			pblk_free_rqd(pblk, rqd, PBLK_WRITE);
			pblk_pipeline_stop(pblk);
		}
131

132
		erase_lun = pblk_ppa_to_pos(geo, ppa_list[i]);
133

134 135 136 137 138 139 140 141
		/* line can change after page map. We might also be writing the
		 * last line.
		 */
		e_line = pblk_line_get_erase(pblk);
		if (!e_line)
			return pblk_map_rq(pblk, rqd, sentry, lun_bitmap,
							valid_secs, i + min);

142
		spin_lock(&e_line->lock);
143 144
		if (!test_bit(erase_lun, e_line->erase_bitmap)) {
			set_bit(erase_lun, e_line->erase_bitmap);
145
			atomic_dec(&e_line->left_eblks);
146

147
			*erase_ppa = ppa_list[i];
148
			erase_ppa->a.blk = e_line->id;
149

150 151
			spin_unlock(&e_line->lock);

152 153 154 155
			/* Avoid evaluating e_line->left_eblks */
			return pblk_map_rq(pblk, rqd, sentry, lun_bitmap,
							valid_secs, i + min);
		}
156
		spin_unlock(&e_line->lock);
157 158
	}

159
	d_line = pblk_line_get_data(pblk);
160

161 162 163 164 165 166 167
	/* line can change after page map. We might also be writing the
	 * last line.
	 */
	e_line = pblk_line_get_erase(pblk);
	if (!e_line)
		return;

168
	/* Erase blocks that are bad in this line but might not be in next */
169
	if (unlikely(pblk_ppa_empty(*erase_ppa)) &&
170 171 172 173 174 175 176
			bitmap_weight(d_line->blk_bitmap, lm->blk_per_line)) {
		int bit = -1;

retry:
		bit = find_next_bit(d_line->blk_bitmap,
						lm->blk_per_line, bit + 1);
		if (bit >= lm->blk_per_line)
177 178
			return;

179 180 181 182 183 184 185 186
		spin_lock(&e_line->lock);
		if (test_bit(bit, e_line->erase_bitmap)) {
			spin_unlock(&e_line->lock);
			goto retry;
		}
		spin_unlock(&e_line->lock);

		set_bit(bit, e_line->erase_bitmap);
187
		atomic_dec(&e_line->left_eblks);
188
		*erase_ppa = pblk->luns[bit].bppa; /* set ch and lun */
189
		erase_ppa->a.blk = e_line->id;
190 191
	}
}