blob: 3fcf062d752cfe3cfed93102a97ba9f78bc5c2c4 [file] [log] [blame]
Javier González02a15202018-10-09 13:12:06 +02001// SPDX-License-Identifier: GPL-2.0
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002/*
3 * Copyright (C) 2016 CNEX Labs
4 * Initial: Javier Gonzalez <javier@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-recovery.c - pblk's recovery path
Javier González361d8892018-12-11 20:16:19 +010016 *
17 * The L2P recovery path is single threaded as the L2P table is updated in order
18 * following the line sequence ID.
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020019 */
20
21#include "pblk.h"
Hans Holmbergf2937232018-10-09 13:11:53 +020022#include "pblk-trace.h"
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020023
Hans Holmberg06bc0722018-01-05 14:16:08 +010024int pblk_recov_check_emeta(struct pblk *pblk, struct line_emeta *emeta_buf)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020025{
26 u32 crc;
27
Javier Gonzálezdd2a4342017-06-26 11:57:17 +020028 crc = pblk_calc_emeta_crc(pblk, emeta_buf);
29 if (le32_to_cpu(emeta_buf->crc) != crc)
Hans Holmberg06bc0722018-01-05 14:16:08 +010030 return 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020031
Javier Gonzálezdd2a4342017-06-26 11:57:17 +020032 if (le32_to_cpu(emeta_buf->header.identifier) != PBLK_MAGIC)
Hans Holmberg06bc0722018-01-05 14:16:08 +010033 return 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020034
Hans Holmberg06bc0722018-01-05 14:16:08 +010035 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020036}
37
38static int pblk_recov_l2p_from_emeta(struct pblk *pblk, struct pblk_line *line)
39{
40 struct nvm_tgt_dev *dev = pblk->dev;
41 struct nvm_geo *geo = &dev->geo;
42 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +020043 struct pblk_emeta *emeta = line->emeta;
44 struct line_emeta *emeta_buf = emeta->buf;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020045 __le64 *lba_list;
Hans Holmberg75610cd2017-10-13 14:46:38 +020046 u64 data_start, data_end;
47 u64 nr_valid_lbas, nr_lbas = 0;
48 u64 i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020049
Hans Holmberg06bc0722018-01-05 14:16:08 +010050 lba_list = emeta_to_lbas(pblk, emeta_buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020051 if (!lba_list)
52 return 1;
53
54 data_start = pblk_line_smeta_start(pblk, line) + lm->smeta_sec;
Hans Holmberg75610cd2017-10-13 14:46:38 +020055 data_end = line->emeta_ssec;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +020056 nr_valid_lbas = le64_to_cpu(emeta_buf->nr_valid_lbas);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020057
Hans Holmberg92957092017-10-13 14:46:35 +020058 for (i = data_start; i < data_end; i++) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020059 struct ppa_addr ppa;
60 int pos;
61
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +010062 ppa = addr_to_gen_ppa(pblk, i, line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020063 pos = pblk_ppa_to_pos(geo, ppa);
64
65 /* Do not update bad blocks */
66 if (test_bit(pos, line->blk_bitmap))
67 continue;
68
69 if (le64_to_cpu(lba_list[i]) == ADDR_EMPTY) {
70 spin_lock(&line->lock);
71 if (test_and_set_bit(i, line->invalid_bitmap))
Dan Carpenter2a79efd2017-04-15 20:55:52 +020072 WARN_ONCE(1, "pblk: rec. double invalidate:\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020073 else
Javier Gonzálezdd2a4342017-06-26 11:57:17 +020074 le32_add_cpu(line->vsc, -1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020075 spin_unlock(&line->lock);
76
77 continue;
78 }
79
80 pblk_update_map(pblk, le64_to_cpu(lba_list[i]), ppa);
81 nr_lbas++;
82 }
83
84 if (nr_valid_lbas != nr_lbas)
Matias Bjørling4e495a42018-07-13 10:48:42 +020085 pblk_err(pblk, "line %d - inconsistent lba list(%llu/%llu)\n",
Hans Holmberg75610cd2017-10-13 14:46:38 +020086 line->id, nr_valid_lbas, nr_lbas);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020087
88 line->left_msecs = 0;
89
90 return 0;
91}
92
Javier González6ad2f612018-10-09 13:12:05 +020093static void pblk_update_line_wp(struct pblk *pblk, struct pblk_line *line,
94 u64 written_secs)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020095{
Javier González6ad2f612018-10-09 13:12:05 +020096 int i;
97
98 for (i = 0; i < written_secs; i += pblk->min_write_pgs)
99 pblk_alloc_page(pblk, line, pblk->min_write_pgs);
100}
101
102static u64 pblk_sec_in_open_line(struct pblk *pblk, struct pblk_line *line)
103{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200104 struct pblk_line_meta *lm = &pblk->lm;
105 int nr_bb = bitmap_weight(line->blk_bitmap, lm->blk_per_line);
Javier González6ad2f612018-10-09 13:12:05 +0200106 u64 written_secs = 0;
107 int valid_chunks = 0;
108 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200109
Javier González6ad2f612018-10-09 13:12:05 +0200110 for (i = 0; i < lm->blk_per_line; i++) {
111 struct nvm_chk_meta *chunk = &line->chks[i];
112
113 if (chunk->state & NVM_CHK_ST_OFFLINE)
114 continue;
115
116 written_secs += chunk->wp;
117 valid_chunks++;
118 }
119
120 if (lm->blk_per_line - nr_bb != valid_chunks)
121 pblk_err(pblk, "recovery line %d is bad\n", line->id);
122
123 pblk_update_line_wp(pblk, line, written_secs - lm->smeta_sec);
124
125 return written_secs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200126}
127
128struct pblk_recov_alloc {
129 struct ppa_addr *ppa_list;
Igor Konopkofaa79f22018-12-11 20:16:23 +0100130 void *meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200131 struct nvm_rq *rqd;
132 void *data;
133 dma_addr_t dma_ppa_list;
134 dma_addr_t dma_meta_list;
135};
136
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200137static void pblk_recov_complete(struct kref *ref)
138{
139 struct pblk_pad_rq *pad_rq = container_of(ref, struct pblk_pad_rq, ref);
140
141 complete(&pad_rq->wait);
142}
143
144static void pblk_end_io_recov(struct nvm_rq *rqd)
145{
Hans Holmbergd68a9342018-10-09 13:11:46 +0200146 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200147 struct pblk_pad_rq *pad_rq = rqd->private;
148 struct pblk *pblk = pad_rq->pblk;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200149
Matias Bjørling43241cf2018-10-09 13:11:51 +0200150 pblk_up_chunk(pblk, ppa_list[0]);
Javier González3eaa11e2017-07-07 21:08:52 +0200151
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200152 pblk_free_rqd(pblk, rqd, PBLK_WRITE_INT);
Javier González3eaa11e2017-07-07 21:08:52 +0200153
154 atomic_dec(&pblk->inflight_io);
155 kref_put(&pad_rq->ref, pblk_recov_complete);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200156}
157
Javier González6ad2f612018-10-09 13:12:05 +0200158/* pad line using line bitmap. */
159static int pblk_recov_pad_line(struct pblk *pblk, struct pblk_line *line,
160 int left_ppas)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200161{
162 struct nvm_tgt_dev *dev = pblk->dev;
163 struct nvm_geo *geo = &dev->geo;
Igor Konopkofaa79f22018-12-11 20:16:23 +0100164 void *meta_list;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200165 struct pblk_pad_rq *pad_rq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200166 struct nvm_rq *rqd;
167 struct bio *bio;
168 void *data;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200169 __le64 *lba_list = emeta_to_lbas(pblk, line->emeta->buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200170 u64 w_ptr = line->cur_sec;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200171 int left_line_ppas, rq_ppas, rq_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200172 int i, j;
173 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200174
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200175 spin_lock(&line->lock);
176 left_line_ppas = line->left_msecs;
177 spin_unlock(&line->lock);
178
179 pad_rq = kmalloc(sizeof(struct pblk_pad_rq), GFP_KERNEL);
180 if (!pad_rq)
181 return -ENOMEM;
182
Kees Cookfad953c2018-06-12 14:27:37 -0700183 data = vzalloc(array_size(pblk->max_write_pgs, geo->csecs));
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200184 if (!data) {
185 ret = -ENOMEM;
186 goto free_rq;
187 }
188
189 pad_rq->pblk = pblk;
190 init_completion(&pad_rq->wait);
191 kref_init(&pad_rq->ref);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200192
193next_pad_rq:
Igor Konopko55d8ec32018-12-11 20:16:26 +0100194 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0, false);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200195 if (rq_ppas < pblk->min_write_pgs) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200196 pblk_err(pblk, "corrupted pad line %d\n", line->id);
Javier González3eaa11e2017-07-07 21:08:52 +0200197 goto fail_free_pad;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200198 }
199
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200200 rq_len = rq_ppas * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200201
Javier González3eaa11e2017-07-07 21:08:52 +0200202 bio = pblk_bio_map_addr(pblk, data, rq_ppas, rq_len,
203 PBLK_VMALLOC_META, GFP_KERNEL);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200204 if (IS_ERR(bio)) {
205 ret = PTR_ERR(bio);
Javier González45dcf292018-10-09 13:12:02 +0200206 goto fail_free_pad;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200207 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200208
209 bio->bi_iter.bi_sector = 0; /* internal bio */
210 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
211
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200212 rqd = pblk_alloc_rqd(pblk, PBLK_WRITE_INT);
Javier González2942f502017-10-13 14:46:10 +0200213
Javier González45dcf292018-10-09 13:12:02 +0200214 ret = pblk_alloc_rqd_meta(pblk, rqd);
215 if (ret)
216 goto fail_free_rqd;
217
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200218 rqd->bio = bio;
219 rqd->opcode = NVM_OP_PWRITE;
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200220 rqd->is_seq = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200221 rqd->nr_ppas = rq_ppas;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200222 rqd->end_io = pblk_end_io_recov;
223 rqd->private = pad_rq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200224
Javier González45dcf292018-10-09 13:12:02 +0200225 meta_list = rqd->meta_list;
226
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200227 for (i = 0; i < rqd->nr_ppas; ) {
228 struct ppa_addr ppa;
229 int pos;
230
231 w_ptr = pblk_alloc_page(pblk, line, pblk->min_write_pgs);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100232 ppa = addr_to_gen_ppa(pblk, w_ptr, line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200233 pos = pblk_ppa_to_pos(geo, ppa);
234
235 while (test_bit(pos, line->blk_bitmap)) {
236 w_ptr += pblk->min_write_pgs;
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100237 ppa = addr_to_gen_ppa(pblk, w_ptr, line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200238 pos = pblk_ppa_to_pos(geo, ppa);
239 }
240
241 for (j = 0; j < pblk->min_write_pgs; j++, i++, w_ptr++) {
242 struct ppa_addr dev_ppa;
Igor Konopkofaa79f22018-12-11 20:16:23 +0100243 struct pblk_sec_meta *meta;
Javier Gonzálezf417aa02017-06-30 17:56:34 +0200244 __le64 addr_empty = cpu_to_le64(ADDR_EMPTY);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200245
246 dev_ppa = addr_to_gen_ppa(pblk, w_ptr, line->id);
247
248 pblk_map_invalidate(pblk, dev_ppa);
Igor Konopkofaa79f22018-12-11 20:16:23 +0100249 lba_list[w_ptr] = addr_empty;
250 meta = pblk_get_meta(pblk, meta_list, i);
251 meta->lba = addr_empty;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200252 rqd->ppa_list[i] = dev_ppa;
253 }
254 }
255
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200256 kref_get(&pad_rq->ref);
Matias Bjørling43241cf2018-10-09 13:11:51 +0200257 pblk_down_chunk(pblk, rqd->ppa_list[0]);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200258
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200259 ret = pblk_submit_io(pblk, rqd);
260 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200261 pblk_err(pblk, "I/O submission failed: %d\n", ret);
Matias Bjørling43241cf2018-10-09 13:11:51 +0200262 pblk_up_chunk(pblk, rqd->ppa_list[0]);
Javier González45dcf292018-10-09 13:12:02 +0200263 goto fail_free_rqd;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200264 }
265
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200266 left_line_ppas -= rq_ppas;
267 left_ppas -= rq_ppas;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200268 if (left_ppas && left_line_ppas)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200269 goto next_pad_rq;
270
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200271 kref_put(&pad_rq->ref, pblk_recov_complete);
272
273 if (!wait_for_completion_io_timeout(&pad_rq->wait,
274 msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200275 pblk_err(pblk, "pad write timed out\n");
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200276 ret = -ETIME;
277 }
278
Javier González3eaa11e2017-07-07 21:08:52 +0200279 if (!pblk_line_is_full(line))
Matias Bjørling4e495a42018-07-13 10:48:42 +0200280 pblk_err(pblk, "corrupted padded line: %d\n", line->id);
Javier González3eaa11e2017-07-07 21:08:52 +0200281
282 vfree(data);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200283free_rq:
284 kfree(pad_rq);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200285 return ret;
286
Javier González45dcf292018-10-09 13:12:02 +0200287fail_free_rqd:
288 pblk_free_rqd(pblk, rqd, PBLK_WRITE_INT);
Javier González3eaa11e2017-07-07 21:08:52 +0200289 bio_put(bio);
Javier González3eaa11e2017-07-07 21:08:52 +0200290fail_free_pad:
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200291 kfree(pad_rq);
Javier González3eaa11e2017-07-07 21:08:52 +0200292 vfree(data);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200293 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200294}
295
Javier González63dee3a2018-10-09 13:11:57 +0200296static int pblk_pad_distance(struct pblk *pblk, struct pblk_line *line)
297{
298 struct nvm_tgt_dev *dev = pblk->dev;
299 struct nvm_geo *geo = &dev->geo;
300 int distance = geo->mw_cunits * geo->all_luns * geo->ws_opt;
301
302 return (distance > line->left_msecs) ? line->left_msecs : distance;
303}
304
Javier González6ad2f612018-10-09 13:12:05 +0200305static int pblk_line_wp_is_unbalanced(struct pblk *pblk,
306 struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200307{
308 struct nvm_tgt_dev *dev = pblk->dev;
309 struct nvm_geo *geo = &dev->geo;
Javier González6ad2f612018-10-09 13:12:05 +0200310 struct pblk_line_meta *lm = &pblk->lm;
311 struct pblk_lun *rlun;
312 struct nvm_chk_meta *chunk;
313 struct ppa_addr ppa;
314 u64 line_wp;
315 int pos, i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200316
Javier González6ad2f612018-10-09 13:12:05 +0200317 rlun = &pblk->luns[0];
318 ppa = rlun->bppa;
319 pos = pblk_ppa_to_pos(geo, ppa);
320 chunk = &line->chks[pos];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200321
Javier González6ad2f612018-10-09 13:12:05 +0200322 line_wp = chunk->wp;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200323
Javier González6ad2f612018-10-09 13:12:05 +0200324 for (i = 1; i < lm->blk_per_line; i++) {
325 rlun = &pblk->luns[i];
326 ppa = rlun->bppa;
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100327 pos = pblk_ppa_to_pos(geo, ppa);
Javier González6ad2f612018-10-09 13:12:05 +0200328 chunk = &line->chks[pos];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200329
Javier González6ad2f612018-10-09 13:12:05 +0200330 if (chunk->wp > line_wp)
331 return 1;
332 else if (chunk->wp < line_wp)
333 line_wp = chunk->wp;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200334 }
335
Javier González6ad2f612018-10-09 13:12:05 +0200336 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200337}
338
339static int pblk_recov_scan_oob(struct pblk *pblk, struct pblk_line *line,
Javier González6ad2f612018-10-09 13:12:05 +0200340 struct pblk_recov_alloc p)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200341{
342 struct nvm_tgt_dev *dev = pblk->dev;
Zhoujie Wuf40a62d2018-12-11 20:16:07 +0100343 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200344 struct nvm_geo *geo = &dev->geo;
345 struct ppa_addr *ppa_list;
Igor Konopkofaa79f22018-12-11 20:16:23 +0100346 void *meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200347 struct nvm_rq *rqd;
348 struct bio *bio;
349 void *data;
350 dma_addr_t dma_ppa_list, dma_meta_list;
Javier González6ad2f612018-10-09 13:12:05 +0200351 __le64 *lba_list;
Zhoujie Wuf40a62d2018-12-11 20:16:07 +0100352 u64 paddr = pblk_line_smeta_start(pblk, line) + lm->smeta_sec;
Javier González6ad2f612018-10-09 13:12:05 +0200353 bool padded = false;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200354 int rq_ppas, rq_len;
355 int i, j;
Javier González6ad2f612018-10-09 13:12:05 +0200356 int ret;
Zhoujie Wuf40a62d2018-12-11 20:16:07 +0100357 u64 left_ppas = pblk_sec_in_open_line(pblk, line) - lm->smeta_sec;
Javier González6ad2f612018-10-09 13:12:05 +0200358
359 if (pblk_line_wp_is_unbalanced(pblk, line))
360 pblk_warn(pblk, "recovering unbalanced line (%d)\n", line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200361
362 ppa_list = p.ppa_list;
363 meta_list = p.meta_list;
364 rqd = p.rqd;
365 data = p.data;
366 dma_ppa_list = p.dma_ppa_list;
367 dma_meta_list = p.dma_meta_list;
368
Javier González6ad2f612018-10-09 13:12:05 +0200369 lba_list = emeta_to_lbas(pblk, line->emeta->buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200370
371next_rq:
Javier González084ec9b2017-06-26 16:27:13 -0600372 memset(rqd, 0, pblk_g_rq_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200373
Igor Konopko55d8ec32018-12-11 20:16:26 +0100374 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0, false);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200375 if (!rq_ppas)
376 rq_ppas = pblk->min_write_pgs;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200377 rq_len = rq_ppas * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200378
Igor Konopko55d8ec32018-12-11 20:16:26 +0100379retry_rq:
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200380 bio = bio_map_kern(dev->q, data, rq_len, GFP_KERNEL);
381 if (IS_ERR(bio))
382 return PTR_ERR(bio);
383
384 bio->bi_iter.bi_sector = 0; /* internal bio */
385 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Igor Konopko55d8ec32018-12-11 20:16:26 +0100386 bio_get(bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200387
388 rqd->bio = bio;
389 rqd->opcode = NVM_OP_PREAD;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200390 rqd->meta_list = meta_list;
391 rqd->nr_ppas = rq_ppas;
392 rqd->ppa_list = ppa_list;
393 rqd->dma_ppa_list = dma_ppa_list;
394 rqd->dma_meta_list = dma_meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200395
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200396 if (pblk_io_aligned(pblk, rq_ppas))
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200397 rqd->is_seq = 1;
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200398
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200399 for (i = 0; i < rqd->nr_ppas; ) {
400 struct ppa_addr ppa;
401 int pos;
402
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200403 ppa = addr_to_gen_ppa(pblk, paddr, line->id);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100404 pos = pblk_ppa_to_pos(geo, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200405
406 while (test_bit(pos, line->blk_bitmap)) {
407 paddr += pblk->min_write_pgs;
408 ppa = addr_to_gen_ppa(pblk, paddr, line->id);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100409 pos = pblk_ppa_to_pos(geo, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200410 }
411
Javier González6ad2f612018-10-09 13:12:05 +0200412 for (j = 0; j < pblk->min_write_pgs; j++, i++)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200413 rqd->ppa_list[i] =
Javier González6ad2f612018-10-09 13:12:05 +0200414 addr_to_gen_ppa(pblk, paddr + j, line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200415 }
416
Javier González1a94b2d2017-10-13 14:46:47 +0200417 ret = pblk_submit_io_sync(pblk, rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200418 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200419 pblk_err(pblk, "I/O submission failed: %d\n", ret);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200420 bio_put(bio);
Igor Konopko55d8ec32018-12-11 20:16:26 +0100421 bio_put(bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200422 return ret;
423 }
424
Javier González588726d32017-06-26 11:57:29 +0200425 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200426
Javier González6ad2f612018-10-09 13:12:05 +0200427 /* If a read fails, do a best effort by padding the line and retrying */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200428 if (rqd->error) {
Javier González6ad2f612018-10-09 13:12:05 +0200429 int pad_distance, ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200430
Javier González6ad2f612018-10-09 13:12:05 +0200431 if (padded) {
432 pblk_log_read_err(pblk, rqd);
Igor Konopko55d8ec32018-12-11 20:16:26 +0100433 bio_put(bio);
Javier González6ad2f612018-10-09 13:12:05 +0200434 return -EINTR;
435 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200436
Javier González6ad2f612018-10-09 13:12:05 +0200437 pad_distance = pblk_pad_distance(pblk, line);
438 ret = pblk_recov_pad_line(pblk, line, pad_distance);
Igor Konopko55d8ec32018-12-11 20:16:26 +0100439 if (ret) {
440 bio_put(bio);
Javier González6ad2f612018-10-09 13:12:05 +0200441 return ret;
Igor Konopko55d8ec32018-12-11 20:16:26 +0100442 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200443
Javier González6ad2f612018-10-09 13:12:05 +0200444 padded = true;
Igor Konopko55d8ec32018-12-11 20:16:26 +0100445 bio_put(bio);
Javier González6ad2f612018-10-09 13:12:05 +0200446 goto retry_rq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200447 }
448
Igor Konopko55d8ec32018-12-11 20:16:26 +0100449 pblk_get_packed_meta(pblk, rqd);
450 bio_put(bio);
451
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200452 for (i = 0; i < rqd->nr_ppas; i++) {
Igor Konopkofaa79f22018-12-11 20:16:23 +0100453 struct pblk_sec_meta *meta = pblk_get_meta(pblk, meta_list, i);
454 u64 lba = le64_to_cpu(meta->lba);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200455
Javier González6ad2f612018-10-09 13:12:05 +0200456 lba_list[paddr++] = cpu_to_le64(lba);
457
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200458 if (lba == ADDR_EMPTY || lba > pblk->rl.nr_secs)
459 continue;
460
Javier González6ad2f612018-10-09 13:12:05 +0200461 line->nr_valid_lbas++;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200462 pblk_update_map(pblk, lba, rqd->ppa_list[i]);
463 }
464
465 left_ppas -= rq_ppas;
466 if (left_ppas > 0)
467 goto next_rq;
468
Javier González6ad2f612018-10-09 13:12:05 +0200469#ifdef CONFIG_NVM_PBLK_DEBUG
470 WARN_ON(padded && !pblk_line_is_full(line));
471#endif
472
473 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200474}
475
476/* Scan line for lbas on out of bound area */
477static int pblk_recov_l2p_from_oob(struct pblk *pblk, struct pblk_line *line)
478{
479 struct nvm_tgt_dev *dev = pblk->dev;
480 struct nvm_geo *geo = &dev->geo;
481 struct nvm_rq *rqd;
482 struct ppa_addr *ppa_list;
Igor Konopkofaa79f22018-12-11 20:16:23 +0100483 void *meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200484 struct pblk_recov_alloc p;
485 void *data;
486 dma_addr_t dma_ppa_list, dma_meta_list;
Javier González6ad2f612018-10-09 13:12:05 +0200487 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200488
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200489 meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL, &dma_meta_list);
Javier González2942f502017-10-13 14:46:10 +0200490 if (!meta_list)
491 return -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200492
Igor Konopko24828d02018-12-11 20:16:24 +0100493 ppa_list = (void *)(meta_list) + pblk_dma_meta_size(pblk);
494 dma_ppa_list = dma_meta_list + pblk_dma_meta_size(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200495
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200496 data = kcalloc(pblk->max_write_pgs, geo->csecs, GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200497 if (!data) {
498 ret = -ENOMEM;
499 goto free_meta_list;
500 }
501
Javier González6ad2f612018-10-09 13:12:05 +0200502 rqd = mempool_alloc(&pblk->r_rq_pool, GFP_KERNEL);
503 memset(rqd, 0, pblk_g_rq_size);
Javier González2942f502017-10-13 14:46:10 +0200504
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200505 p.ppa_list = ppa_list;
506 p.meta_list = meta_list;
507 p.rqd = rqd;
508 p.data = data;
509 p.dma_ppa_list = dma_ppa_list;
510 p.dma_meta_list = dma_meta_list;
511
Javier González6ad2f612018-10-09 13:12:05 +0200512 ret = pblk_recov_scan_oob(pblk, line, p);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200513 if (ret) {
Javier González6ad2f612018-10-09 13:12:05 +0200514 pblk_err(pblk, "could not recover L2P form OOB\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200515 goto out;
516 }
517
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200518 if (pblk_line_is_full(line))
519 pblk_line_recov_close(pblk, line);
520
521out:
Javier González6ad2f612018-10-09 13:12:05 +0200522 mempool_free(rqd, &pblk->r_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200523 kfree(data);
524free_meta_list:
525 nvm_dev_dma_free(dev->parent, meta_list, dma_meta_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200526
527 return ret;
528}
529
530/* Insert lines ordered by sequence number (seq_num) on list */
531static void pblk_recov_line_add_ordered(struct list_head *head,
532 struct pblk_line *line)
533{
534 struct pblk_line *t = NULL;
535
536 list_for_each_entry(t, head, list)
537 if (t->seq_nr > line->seq_nr)
538 break;
539
540 __list_add(&line->list, t->list.prev, &t->list);
541}
542
Hans Holmberg75610cd2017-10-13 14:46:38 +0200543static u64 pblk_line_emeta_start(struct pblk *pblk, struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200544{
545 struct nvm_tgt_dev *dev = pblk->dev;
546 struct nvm_geo *geo = &dev->geo;
547 struct pblk_line_meta *lm = &pblk->lm;
Hans Holmberg75610cd2017-10-13 14:46:38 +0200548 unsigned int emeta_secs;
549 u64 emeta_start;
550 struct ppa_addr ppa;
551 int pos;
552
553 emeta_secs = lm->emeta_sec[0];
554 emeta_start = lm->sec_per_line;
555
556 while (emeta_secs) {
557 emeta_start--;
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100558 ppa = addr_to_gen_ppa(pblk, emeta_start, line->id);
Hans Holmberg75610cd2017-10-13 14:46:38 +0200559 pos = pblk_ppa_to_pos(geo, ppa);
560 if (!test_bit(pos, line->blk_bitmap))
561 emeta_secs--;
562 }
563
564 return emeta_start;
565}
566
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200567static int pblk_recov_check_line_version(struct pblk *pblk,
568 struct line_emeta *emeta)
569{
570 struct line_header *header = &emeta->header;
571
572 if (header->version_major != EMETA_VERSION_MAJOR) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200573 pblk_err(pblk, "line major version mismatch: %d, expected: %d\n",
574 header->version_major, EMETA_VERSION_MAJOR);
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200575 return 1;
576 }
577
Matias Bjørling99b8dad2018-07-13 10:48:38 +0200578#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200579 if (header->version_minor > EMETA_VERSION_MINOR)
Matias Bjørling4e495a42018-07-13 10:48:42 +0200580 pblk_info(pblk, "newer line minor version found: %d\n",
Matias Bjørling99b8dad2018-07-13 10:48:38 +0200581 header->version_minor);
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200582#endif
583
584 return 0;
585}
586
Hans Holmberg76758392018-03-30 00:04:52 +0200587static void pblk_recov_wa_counters(struct pblk *pblk,
588 struct line_emeta *emeta)
589{
590 struct pblk_line_meta *lm = &pblk->lm;
591 struct line_header *header = &emeta->header;
592 struct wa_counters *wa = emeta_to_wa(lm, emeta);
593
594 /* WA counters were introduced in emeta version 0.2 */
595 if (header->version_major > 0 || header->version_minor >= 2) {
596 u64 user = le64_to_cpu(wa->user);
597 u64 pad = le64_to_cpu(wa->pad);
598 u64 gc = le64_to_cpu(wa->gc);
599
600 atomic64_set(&pblk->user_wa, user);
601 atomic64_set(&pblk->pad_wa, pad);
602 atomic64_set(&pblk->gc_wa, gc);
603
604 pblk->user_rst_wa = user;
605 pblk->pad_rst_wa = pad;
606 pblk->gc_rst_wa = gc;
607 }
608}
609
Hans Holmberg9156f362018-03-30 00:05:22 +0200610static int pblk_line_was_written(struct pblk_line *line,
Javier González6ad2f612018-10-09 13:12:05 +0200611 struct pblk *pblk)
Hans Holmberg9156f362018-03-30 00:05:22 +0200612{
613
Hans Holmbergb06be282018-06-01 16:41:12 +0200614 struct pblk_line_meta *lm = &pblk->lm;
615 struct nvm_tgt_dev *dev = pblk->dev;
616 struct nvm_geo *geo = &dev->geo;
617 struct nvm_chk_meta *chunk;
618 struct ppa_addr bppa;
619 int smeta_blk;
Hans Holmberg9156f362018-03-30 00:05:22 +0200620
Hans Holmbergb06be282018-06-01 16:41:12 +0200621 if (line->state == PBLK_LINESTATE_BAD)
622 return 0;
Hans Holmberg9156f362018-03-30 00:05:22 +0200623
Hans Holmbergb06be282018-06-01 16:41:12 +0200624 smeta_blk = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
625 if (smeta_blk >= lm->blk_per_line)
626 return 0;
627
628 bppa = pblk->luns[smeta_blk].bppa;
629 chunk = &line->chks[pblk_ppa_to_pos(geo, bppa)];
630
631 if (chunk->state & NVM_CHK_ST_FREE)
632 return 0;
633
634 return 1;
Hans Holmberg9156f362018-03-30 00:05:22 +0200635}
636
Javier González6ad2f612018-10-09 13:12:05 +0200637static bool pblk_line_is_open(struct pblk *pblk, struct pblk_line *line)
638{
639 struct pblk_line_meta *lm = &pblk->lm;
640 int i;
641
642 for (i = 0; i < lm->blk_per_line; i++)
643 if (line->chks[i].state & NVM_CHK_ST_OPEN)
644 return true;
645
646 return false;
647}
648
Hans Holmberg75610cd2017-10-13 14:46:38 +0200649struct pblk_line *pblk_recov_l2p(struct pblk *pblk)
650{
651 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200652 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
653 struct pblk_line *line, *tline, *data_line = NULL;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200654 struct pblk_smeta *smeta;
655 struct pblk_emeta *emeta;
656 struct line_smeta *smeta_buf;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200657 int found_lines = 0, recovered_lines = 0, open_lines = 0;
658 int is_next = 0;
659 int meta_line;
660 int i, valid_uuid = 0;
661 LIST_HEAD(recov_list);
662
663 /* TODO: Implement FTL snapshot */
664
665 /* Scan recovery - takes place when FTL snapshot fails */
666 spin_lock(&l_mg->free_lock);
667 meta_line = find_first_zero_bit(&l_mg->meta_bitmap, PBLK_DATA_LINES);
668 set_bit(meta_line, &l_mg->meta_bitmap);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200669 smeta = l_mg->sline_meta[meta_line];
670 emeta = l_mg->eline_meta[meta_line];
Javier González8224cbd2017-06-30 17:56:38 +0200671 smeta_buf = (struct line_smeta *)smeta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200672 spin_unlock(&l_mg->free_lock);
673
674 /* Order data lines using their sequence number */
675 for (i = 0; i < l_mg->nr_lines; i++) {
676 u32 crc;
677
678 line = &pblk->lines[i];
679
680 memset(smeta, 0, lm->smeta_len);
681 line->smeta = smeta;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200682 line->lun_bitmap = ((void *)(smeta_buf)) +
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200683 sizeof(struct line_smeta);
684
Hans Holmbergb06be282018-06-01 16:41:12 +0200685 if (!pblk_line_was_written(line, pblk))
Hans Holmberg9156f362018-03-30 00:05:22 +0200686 continue;
687
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200688 /* Lines that cannot be read are assumed as not written here */
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200689 if (pblk_line_smeta_read(pblk, line))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200690 continue;
691
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200692 crc = pblk_calc_smeta_crc(pblk, smeta_buf);
693 if (le32_to_cpu(smeta_buf->crc) != crc)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200694 continue;
695
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200696 if (le32_to_cpu(smeta_buf->header.identifier) != PBLK_MAGIC)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200697 continue;
698
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200699 if (smeta_buf->header.version_major != SMETA_VERSION_MAJOR) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200700 pblk_err(pblk, "found incompatible line version %u\n",
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200701 smeta_buf->header.version_major);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200702 return ERR_PTR(-EINVAL);
703 }
704
705 /* The first valid instance uuid is used for initialization */
706 if (!valid_uuid) {
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200707 memcpy(pblk->instance_uuid, smeta_buf->header.uuid, 16);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200708 valid_uuid = 1;
709 }
710
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200711 if (memcmp(pblk->instance_uuid, smeta_buf->header.uuid, 16)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200712 pblk_debug(pblk, "ignore line %u due to uuid mismatch\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200713 i);
714 continue;
715 }
716
717 /* Update line metadata */
718 spin_lock(&line->lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200719 line->id = le32_to_cpu(smeta_buf->header.id);
720 line->type = le16_to_cpu(smeta_buf->header.type);
721 line->seq_nr = le64_to_cpu(smeta_buf->seq_nr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200722 spin_unlock(&line->lock);
723
724 /* Update general metadata */
725 spin_lock(&l_mg->free_lock);
726 if (line->seq_nr >= l_mg->d_seq_nr)
727 l_mg->d_seq_nr = line->seq_nr + 1;
728 l_mg->nr_free_lines--;
729 spin_unlock(&l_mg->free_lock);
730
731 if (pblk_line_recov_alloc(pblk, line))
732 goto out;
733
734 pblk_recov_line_add_ordered(&recov_list, line);
735 found_lines++;
Matias Bjørling4e495a42018-07-13 10:48:42 +0200736 pblk_debug(pblk, "recovering data line %d, seq:%llu\n",
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200737 line->id, smeta_buf->seq_nr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200738 }
739
740 if (!found_lines) {
741 pblk_setup_uuid(pblk);
742
743 spin_lock(&l_mg->free_lock);
744 WARN_ON_ONCE(!test_and_clear_bit(meta_line,
745 &l_mg->meta_bitmap));
746 spin_unlock(&l_mg->free_lock);
747
748 goto out;
749 }
750
751 /* Verify closed blocks and recover this portion of L2P table*/
752 list_for_each_entry_safe(line, tline, &recov_list, list) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200753 recovered_lines++;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200754
Hans Holmberg75610cd2017-10-13 14:46:38 +0200755 line->emeta_ssec = pblk_line_emeta_start(pblk, line);
Javier González8224cbd2017-06-30 17:56:38 +0200756 line->emeta = emeta;
757 memset(line->emeta->buf, 0, lm->emeta_len[0]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200758
Javier González6ad2f612018-10-09 13:12:05 +0200759 if (pblk_line_is_open(pblk, line)) {
760 pblk_recov_l2p_from_oob(pblk, line);
761 goto next;
762 }
763
Javier Gonzálezaf3fac12018-10-09 13:12:03 +0200764 if (pblk_line_emeta_read(pblk, line, line->emeta->buf)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200765 pblk_recov_l2p_from_oob(pblk, line);
766 goto next;
767 }
768
Hans Holmberg06bc0722018-01-05 14:16:08 +0100769 if (pblk_recov_check_emeta(pblk, line->emeta->buf)) {
770 pblk_recov_l2p_from_oob(pblk, line);
771 goto next;
772 }
773
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200774 if (pblk_recov_check_line_version(pblk, line->emeta->buf))
775 return ERR_PTR(-EINVAL);
776
Hans Holmberg76758392018-03-30 00:04:52 +0200777 pblk_recov_wa_counters(pblk, line->emeta->buf);
778
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200779 if (pblk_recov_l2p_from_emeta(pblk, line))
780 pblk_recov_l2p_from_oob(pblk, line);
781
782next:
783 if (pblk_line_is_full(line)) {
784 struct list_head *move_list;
785
786 spin_lock(&line->lock);
787 line->state = PBLK_LINESTATE_CLOSED;
Hans Holmbergf2937232018-10-09 13:11:53 +0200788 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
789 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200790 move_list = pblk_line_gc_list(pblk, line);
791 spin_unlock(&line->lock);
792
793 spin_lock(&l_mg->gc_lock);
794 list_move_tail(&line->list, move_list);
795 spin_unlock(&l_mg->gc_lock);
796
Hans Holmberg53d82db2018-10-09 13:11:47 +0200797 mempool_free(line->map_bitmap, l_mg->bitmap_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200798 line->map_bitmap = NULL;
799 line->smeta = NULL;
800 line->emeta = NULL;
801 } else {
Javier González6ad2f612018-10-09 13:12:05 +0200802 spin_lock(&line->lock);
803 line->state = PBLK_LINESTATE_OPEN;
804 spin_unlock(&line->lock);
805
806 line->emeta->mem = 0;
807 atomic_set(&line->emeta->sync, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200808
Hans Holmbergf2937232018-10-09 13:11:53 +0200809 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
810 line->state);
811
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200812 data_line = line;
Javier González6ad2f612018-10-09 13:12:05 +0200813 line->meta_line = meta_line;
814
815 open_lines++;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200816 }
817 }
818
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200819 if (!open_lines) {
Jia-Ju Bai7325b4b2018-10-09 13:12:00 +0200820 spin_lock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200821 WARN_ON_ONCE(!test_and_clear_bit(meta_line,
822 &l_mg->meta_bitmap));
Jia-Ju Bai7325b4b2018-10-09 13:12:00 +0200823 spin_unlock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200824 } else {
Jia-Ju Bai7325b4b2018-10-09 13:12:00 +0200825 spin_lock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200826 /* Allocate next line for preparation */
827 l_mg->data_next = pblk_line_get(pblk);
828 if (l_mg->data_next) {
829 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
830 l_mg->data_next->type = PBLK_LINETYPE_DATA;
831 is_next = 1;
832 }
Jia-Ju Bai7325b4b2018-10-09 13:12:00 +0200833 spin_unlock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200834 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200835
Javier Gonzáleza7689932018-01-05 14:16:13 +0100836 if (is_next)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200837 pblk_line_erase(pblk, l_mg->data_next);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200838
839out:
840 if (found_lines != recovered_lines)
Matias Bjørling4e495a42018-07-13 10:48:42 +0200841 pblk_err(pblk, "failed to recover all found lines %d/%d\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200842 found_lines, recovered_lines);
843
844 return data_line;
845}
846
847/*
Javier González588726d32017-06-26 11:57:29 +0200848 * Pad current line
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200849 */
Javier González588726d32017-06-26 11:57:29 +0200850int pblk_recov_pad(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200851{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200852 struct pblk_line *line;
853 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González588726d32017-06-26 11:57:29 +0200854 int left_msecs;
855 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200856
857 spin_lock(&l_mg->free_lock);
858 line = l_mg->data_line;
Javier González588726d32017-06-26 11:57:29 +0200859 left_msecs = line->left_msecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200860 spin_unlock(&l_mg->free_lock);
861
Javier González6ad2f612018-10-09 13:12:05 +0200862 ret = pblk_recov_pad_line(pblk, line, left_msecs);
Javier González588726d32017-06-26 11:57:29 +0200863 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200864 pblk_err(pblk, "tear down padding failed (%d)\n", ret);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200865 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200866 }
867
Javier González588726d32017-06-26 11:57:29 +0200868 pblk_line_close_meta(pblk, line);
Javier González588726d32017-06-26 11:57:29 +0200869 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200870}