blob: 3bd2b6b0a35983ee4bc6902e34cc33ca93648313 [file] [log] [blame]
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001/*
2 * Copyright (C) 2016 CNEX Labs
3 * Initial: Javier Gonzalez <javier@cnexlabs.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License version
7 * 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * pblk-recovery.c - pblk's recovery path
15 */
16
17#include "pblk.h"
18
Hans Holmberg06bc0722018-01-05 14:16:08 +010019int pblk_recov_check_emeta(struct pblk *pblk, struct line_emeta *emeta_buf)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020020{
21 u32 crc;
22
Javier Gonzálezdd2a4342017-06-26 11:57:17 +020023 crc = pblk_calc_emeta_crc(pblk, emeta_buf);
24 if (le32_to_cpu(emeta_buf->crc) != crc)
Hans Holmberg06bc0722018-01-05 14:16:08 +010025 return 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020026
Javier Gonzálezdd2a4342017-06-26 11:57:17 +020027 if (le32_to_cpu(emeta_buf->header.identifier) != PBLK_MAGIC)
Hans Holmberg06bc0722018-01-05 14:16:08 +010028 return 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020029
Hans Holmberg06bc0722018-01-05 14:16:08 +010030 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020031}
32
33static int pblk_recov_l2p_from_emeta(struct pblk *pblk, struct pblk_line *line)
34{
35 struct nvm_tgt_dev *dev = pblk->dev;
36 struct nvm_geo *geo = &dev->geo;
37 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +020038 struct pblk_emeta *emeta = line->emeta;
39 struct line_emeta *emeta_buf = emeta->buf;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020040 __le64 *lba_list;
Hans Holmberg75610cd2017-10-13 14:46:38 +020041 u64 data_start, data_end;
42 u64 nr_valid_lbas, nr_lbas = 0;
43 u64 i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020044
Hans Holmberg06bc0722018-01-05 14:16:08 +010045 lba_list = emeta_to_lbas(pblk, emeta_buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020046 if (!lba_list)
47 return 1;
48
49 data_start = pblk_line_smeta_start(pblk, line) + lm->smeta_sec;
Hans Holmberg75610cd2017-10-13 14:46:38 +020050 data_end = line->emeta_ssec;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +020051 nr_valid_lbas = le64_to_cpu(emeta_buf->nr_valid_lbas);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020052
Hans Holmberg92957092017-10-13 14:46:35 +020053 for (i = data_start; i < data_end; i++) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020054 struct ppa_addr ppa;
55 int pos;
56
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +010057 ppa = addr_to_gen_ppa(pblk, i, line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020058 pos = pblk_ppa_to_pos(geo, ppa);
59
60 /* Do not update bad blocks */
61 if (test_bit(pos, line->blk_bitmap))
62 continue;
63
64 if (le64_to_cpu(lba_list[i]) == ADDR_EMPTY) {
65 spin_lock(&line->lock);
66 if (test_and_set_bit(i, line->invalid_bitmap))
Dan Carpenter2a79efd2017-04-15 20:55:52 +020067 WARN_ONCE(1, "pblk: rec. double invalidate:\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020068 else
Javier Gonzálezdd2a4342017-06-26 11:57:17 +020069 le32_add_cpu(line->vsc, -1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020070 spin_unlock(&line->lock);
71
72 continue;
73 }
74
75 pblk_update_map(pblk, le64_to_cpu(lba_list[i]), ppa);
76 nr_lbas++;
77 }
78
79 if (nr_valid_lbas != nr_lbas)
Matias Bjørling4e495a42018-07-13 10:48:42 +020080 pblk_err(pblk, "line %d - inconsistent lba list(%llu/%llu)\n",
Hans Holmberg75610cd2017-10-13 14:46:38 +020081 line->id, nr_valid_lbas, nr_lbas);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020082
83 line->left_msecs = 0;
84
85 return 0;
86}
87
88static int pblk_calc_sec_in_line(struct pblk *pblk, struct pblk_line *line)
89{
90 struct nvm_tgt_dev *dev = pblk->dev;
91 struct nvm_geo *geo = &dev->geo;
92 struct pblk_line_meta *lm = &pblk->lm;
93 int nr_bb = bitmap_weight(line->blk_bitmap, lm->blk_per_line);
94
Javier Gonzálezdd2a4342017-06-26 11:57:17 +020095 return lm->sec_per_line - lm->smeta_sec - lm->emeta_sec[0] -
Javier Gonzáleze46f4e42018-03-30 00:05:10 +020096 nr_bb * geo->clba;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020097}
98
99struct pblk_recov_alloc {
100 struct ppa_addr *ppa_list;
101 struct pblk_sec_meta *meta_list;
102 struct nvm_rq *rqd;
103 void *data;
104 dma_addr_t dma_ppa_list;
105 dma_addr_t dma_meta_list;
106};
107
108static int pblk_recov_read_oob(struct pblk *pblk, struct pblk_line *line,
109 struct pblk_recov_alloc p, u64 r_ptr)
110{
111 struct nvm_tgt_dev *dev = pblk->dev;
112 struct nvm_geo *geo = &dev->geo;
113 struct ppa_addr *ppa_list;
114 struct pblk_sec_meta *meta_list;
115 struct nvm_rq *rqd;
116 struct bio *bio;
117 void *data;
118 dma_addr_t dma_ppa_list, dma_meta_list;
119 u64 r_ptr_int;
120 int left_ppas;
121 int rq_ppas, rq_len;
122 int i, j;
123 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200124
125 ppa_list = p.ppa_list;
126 meta_list = p.meta_list;
127 rqd = p.rqd;
128 data = p.data;
129 dma_ppa_list = p.dma_ppa_list;
130 dma_meta_list = p.dma_meta_list;
131
132 left_ppas = line->cur_sec - r_ptr;
133 if (!left_ppas)
134 return 0;
135
136 r_ptr_int = r_ptr;
137
138next_read_rq:
Javier González084ec9b2017-06-26 16:27:13 -0600139 memset(rqd, 0, pblk_g_rq_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200140
141 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0);
142 if (!rq_ppas)
143 rq_ppas = pblk->min_write_pgs;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200144 rq_len = rq_ppas * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200145
146 bio = bio_map_kern(dev->q, data, rq_len, GFP_KERNEL);
147 if (IS_ERR(bio))
148 return PTR_ERR(bio);
149
150 bio->bi_iter.bi_sector = 0; /* internal bio */
151 bio_set_op_attrs(bio, REQ_OP_READ, 0);
152
153 rqd->bio = bio;
154 rqd->opcode = NVM_OP_PREAD;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200155 rqd->meta_list = meta_list;
156 rqd->nr_ppas = rq_ppas;
157 rqd->ppa_list = ppa_list;
158 rqd->dma_ppa_list = dma_ppa_list;
159 rqd->dma_meta_list = dma_meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200160
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200161 if (pblk_io_aligned(pblk, rq_ppas))
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200162 rqd->is_seq = 1;
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200163
Hans Holmbergd68a9342018-10-09 13:11:46 +0200164 ppa_list = nvm_rq_to_ppa_list(rqd);
165
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200166 for (i = 0; i < rqd->nr_ppas; ) {
167 struct ppa_addr ppa;
168 int pos;
169
170 ppa = addr_to_gen_ppa(pblk, r_ptr_int, line->id);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100171 pos = pblk_ppa_to_pos(geo, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200172
173 while (test_bit(pos, line->blk_bitmap)) {
174 r_ptr_int += pblk->min_write_pgs;
175 ppa = addr_to_gen_ppa(pblk, r_ptr_int, line->id);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100176 pos = pblk_ppa_to_pos(geo, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200177 }
178
179 for (j = 0; j < pblk->min_write_pgs; j++, i++, r_ptr_int++)
Hans Holmbergd68a9342018-10-09 13:11:46 +0200180 ppa_list[i] =
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200181 addr_to_gen_ppa(pblk, r_ptr_int, line->id);
182 }
183
184 /* If read fails, more padding is needed */
Javier González1a94b2d2017-10-13 14:46:47 +0200185 ret = pblk_submit_io_sync(pblk, rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200186 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200187 pblk_err(pblk, "I/O submission failed: %d\n", ret);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200188 return ret;
189 }
190
Javier González588726d32017-06-26 11:57:29 +0200191 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200192
193 /* At this point, the read should not fail. If it does, it is a problem
194 * we cannot recover from here. Need FTL log.
195 */
Javier González5d201f02018-01-05 14:16:15 +0100196 if (rqd->error && rqd->error != NVM_RSP_WARN_HIGHECC) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200197 pblk_err(pblk, "L2P recovery failed (%d)\n", rqd->error);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200198 return -EINTR;
199 }
200
201 for (i = 0; i < rqd->nr_ppas; i++) {
202 u64 lba = le64_to_cpu(meta_list[i].lba);
203
204 if (lba == ADDR_EMPTY || lba > pblk->rl.nr_secs)
205 continue;
206
Hans Holmbergd68a9342018-10-09 13:11:46 +0200207 pblk_update_map(pblk, lba, ppa_list[i]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200208 }
209
210 left_ppas -= rq_ppas;
211 if (left_ppas > 0)
212 goto next_read_rq;
213
214 return 0;
215}
216
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200217static void pblk_recov_complete(struct kref *ref)
218{
219 struct pblk_pad_rq *pad_rq = container_of(ref, struct pblk_pad_rq, ref);
220
221 complete(&pad_rq->wait);
222}
223
224static void pblk_end_io_recov(struct nvm_rq *rqd)
225{
Hans Holmbergd68a9342018-10-09 13:11:46 +0200226 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200227 struct pblk_pad_rq *pad_rq = rqd->private;
228 struct pblk *pblk = pad_rq->pblk;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200229
Hans Holmbergd68a9342018-10-09 13:11:46 +0200230 pblk_up_page(pblk, ppa_list, rqd->nr_ppas);
Javier González3eaa11e2017-07-07 21:08:52 +0200231
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200232 pblk_free_rqd(pblk, rqd, PBLK_WRITE_INT);
Javier González3eaa11e2017-07-07 21:08:52 +0200233
234 atomic_dec(&pblk->inflight_io);
235 kref_put(&pad_rq->ref, pblk_recov_complete);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200236}
237
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200238static int pblk_recov_pad_oob(struct pblk *pblk, struct pblk_line *line,
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200239 int left_ppas)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200240{
241 struct nvm_tgt_dev *dev = pblk->dev;
242 struct nvm_geo *geo = &dev->geo;
243 struct ppa_addr *ppa_list;
244 struct pblk_sec_meta *meta_list;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200245 struct pblk_pad_rq *pad_rq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200246 struct nvm_rq *rqd;
247 struct bio *bio;
248 void *data;
249 dma_addr_t dma_ppa_list, dma_meta_list;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200250 __le64 *lba_list = emeta_to_lbas(pblk, line->emeta->buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200251 u64 w_ptr = line->cur_sec;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200252 int left_line_ppas, rq_ppas, rq_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200253 int i, j;
254 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200255
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200256 spin_lock(&line->lock);
257 left_line_ppas = line->left_msecs;
258 spin_unlock(&line->lock);
259
260 pad_rq = kmalloc(sizeof(struct pblk_pad_rq), GFP_KERNEL);
261 if (!pad_rq)
262 return -ENOMEM;
263
Kees Cookfad953c2018-06-12 14:27:37 -0700264 data = vzalloc(array_size(pblk->max_write_pgs, geo->csecs));
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200265 if (!data) {
266 ret = -ENOMEM;
267 goto free_rq;
268 }
269
270 pad_rq->pblk = pblk;
271 init_completion(&pad_rq->wait);
272 kref_init(&pad_rq->ref);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200273
274next_pad_rq:
275 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200276 if (rq_ppas < pblk->min_write_pgs) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200277 pblk_err(pblk, "corrupted pad line %d\n", line->id);
Javier González3eaa11e2017-07-07 21:08:52 +0200278 goto fail_free_pad;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200279 }
280
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200281 rq_len = rq_ppas * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200282
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200283 meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL, &dma_meta_list);
284 if (!meta_list) {
285 ret = -ENOMEM;
Javier González3eaa11e2017-07-07 21:08:52 +0200286 goto fail_free_pad;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200287 }
288
289 ppa_list = (void *)(meta_list) + pblk_dma_meta_size;
290 dma_ppa_list = dma_meta_list + pblk_dma_meta_size;
291
Javier González3eaa11e2017-07-07 21:08:52 +0200292 bio = pblk_bio_map_addr(pblk, data, rq_ppas, rq_len,
293 PBLK_VMALLOC_META, GFP_KERNEL);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200294 if (IS_ERR(bio)) {
295 ret = PTR_ERR(bio);
Javier González2942f502017-10-13 14:46:10 +0200296 goto fail_free_meta;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200297 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200298
299 bio->bi_iter.bi_sector = 0; /* internal bio */
300 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
301
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200302 rqd = pblk_alloc_rqd(pblk, PBLK_WRITE_INT);
Javier González2942f502017-10-13 14:46:10 +0200303
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200304 rqd->bio = bio;
305 rqd->opcode = NVM_OP_PWRITE;
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200306 rqd->is_seq = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200307 rqd->meta_list = meta_list;
308 rqd->nr_ppas = rq_ppas;
309 rqd->ppa_list = ppa_list;
310 rqd->dma_ppa_list = dma_ppa_list;
311 rqd->dma_meta_list = dma_meta_list;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200312 rqd->end_io = pblk_end_io_recov;
313 rqd->private = pad_rq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200314
315 for (i = 0; i < rqd->nr_ppas; ) {
316 struct ppa_addr ppa;
317 int pos;
318
319 w_ptr = pblk_alloc_page(pblk, line, pblk->min_write_pgs);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100320 ppa = addr_to_gen_ppa(pblk, w_ptr, line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200321 pos = pblk_ppa_to_pos(geo, ppa);
322
323 while (test_bit(pos, line->blk_bitmap)) {
324 w_ptr += pblk->min_write_pgs;
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100325 ppa = addr_to_gen_ppa(pblk, w_ptr, line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200326 pos = pblk_ppa_to_pos(geo, ppa);
327 }
328
329 for (j = 0; j < pblk->min_write_pgs; j++, i++, w_ptr++) {
330 struct ppa_addr dev_ppa;
Javier Gonzálezf417aa02017-06-30 17:56:34 +0200331 __le64 addr_empty = cpu_to_le64(ADDR_EMPTY);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200332
333 dev_ppa = addr_to_gen_ppa(pblk, w_ptr, line->id);
334
335 pblk_map_invalidate(pblk, dev_ppa);
Javier Gonzálezcaa69fa2017-06-26 11:57:12 +0200336 lba_list[w_ptr] = meta_list[i].lba = addr_empty;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200337 rqd->ppa_list[i] = dev_ppa;
338 }
339 }
340
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200341 kref_get(&pad_rq->ref);
Javier González3eaa11e2017-07-07 21:08:52 +0200342 pblk_down_page(pblk, rqd->ppa_list, rqd->nr_ppas);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200343
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200344 ret = pblk_submit_io(pblk, rqd);
345 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200346 pblk_err(pblk, "I/O submission failed: %d\n", ret);
Javier González3eaa11e2017-07-07 21:08:52 +0200347 pblk_up_page(pblk, rqd->ppa_list, rqd->nr_ppas);
348 goto fail_free_bio;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200349 }
350
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200351 left_line_ppas -= rq_ppas;
352 left_ppas -= rq_ppas;
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200353 if (left_ppas && left_line_ppas)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200354 goto next_pad_rq;
355
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200356 kref_put(&pad_rq->ref, pblk_recov_complete);
357
358 if (!wait_for_completion_io_timeout(&pad_rq->wait,
359 msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200360 pblk_err(pblk, "pad write timed out\n");
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200361 ret = -ETIME;
362 }
363
Javier González3eaa11e2017-07-07 21:08:52 +0200364 if (!pblk_line_is_full(line))
Matias Bjørling4e495a42018-07-13 10:48:42 +0200365 pblk_err(pblk, "corrupted padded line: %d\n", line->id);
Javier González3eaa11e2017-07-07 21:08:52 +0200366
367 vfree(data);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200368free_rq:
369 kfree(pad_rq);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200370 return ret;
371
Javier González3eaa11e2017-07-07 21:08:52 +0200372fail_free_bio:
373 bio_put(bio);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200374fail_free_meta:
375 nvm_dev_dma_free(dev->parent, meta_list, dma_meta_list);
Javier González3eaa11e2017-07-07 21:08:52 +0200376fail_free_pad:
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200377 kfree(pad_rq);
Javier González3eaa11e2017-07-07 21:08:52 +0200378 vfree(data);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200379 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200380}
381
382/* When this function is called, it means that not all upper pages have been
383 * written in a page that contains valid data. In order to recover this data, we
384 * first find the write pointer on the device, then we pad all necessary
385 * sectors, and finally attempt to read the valid data
386 */
387static int pblk_recov_scan_all_oob(struct pblk *pblk, struct pblk_line *line,
388 struct pblk_recov_alloc p)
389{
390 struct nvm_tgt_dev *dev = pblk->dev;
391 struct nvm_geo *geo = &dev->geo;
392 struct ppa_addr *ppa_list;
393 struct pblk_sec_meta *meta_list;
394 struct nvm_rq *rqd;
395 struct bio *bio;
396 void *data;
397 dma_addr_t dma_ppa_list, dma_meta_list;
398 u64 w_ptr = 0, r_ptr;
399 int rq_ppas, rq_len;
400 int i, j;
401 int ret = 0;
402 int rec_round;
403 int left_ppas = pblk_calc_sec_in_line(pblk, line) - line->cur_sec;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200404
405 ppa_list = p.ppa_list;
406 meta_list = p.meta_list;
407 rqd = p.rqd;
408 data = p.data;
409 dma_ppa_list = p.dma_ppa_list;
410 dma_meta_list = p.dma_meta_list;
411
412 /* we could recover up until the line write pointer */
413 r_ptr = line->cur_sec;
414 rec_round = 0;
415
416next_rq:
Javier González084ec9b2017-06-26 16:27:13 -0600417 memset(rqd, 0, pblk_g_rq_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200418
419 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0);
420 if (!rq_ppas)
421 rq_ppas = pblk->min_write_pgs;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200422 rq_len = rq_ppas * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200423
424 bio = bio_map_kern(dev->q, data, rq_len, GFP_KERNEL);
425 if (IS_ERR(bio))
426 return PTR_ERR(bio);
427
428 bio->bi_iter.bi_sector = 0; /* internal bio */
429 bio_set_op_attrs(bio, REQ_OP_READ, 0);
430
431 rqd->bio = bio;
432 rqd->opcode = NVM_OP_PREAD;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200433 rqd->meta_list = meta_list;
434 rqd->nr_ppas = rq_ppas;
435 rqd->ppa_list = ppa_list;
436 rqd->dma_ppa_list = dma_ppa_list;
437 rqd->dma_meta_list = dma_meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200438
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200439 if (pblk_io_aligned(pblk, rq_ppas))
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200440 rqd->is_seq = 1;
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200441
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200442 for (i = 0; i < rqd->nr_ppas; ) {
443 struct ppa_addr ppa;
444 int pos;
445
446 w_ptr = pblk_alloc_page(pblk, line, pblk->min_write_pgs);
447 ppa = addr_to_gen_ppa(pblk, w_ptr, line->id);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100448 pos = pblk_ppa_to_pos(geo, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200449
450 while (test_bit(pos, line->blk_bitmap)) {
451 w_ptr += pblk->min_write_pgs;
452 ppa = addr_to_gen_ppa(pblk, w_ptr, line->id);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100453 pos = pblk_ppa_to_pos(geo, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200454 }
455
456 for (j = 0; j < pblk->min_write_pgs; j++, i++, w_ptr++)
457 rqd->ppa_list[i] =
458 addr_to_gen_ppa(pblk, w_ptr, line->id);
459 }
460
Javier González1a94b2d2017-10-13 14:46:47 +0200461 ret = pblk_submit_io_sync(pblk, rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200462 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200463 pblk_err(pblk, "I/O submission failed: %d\n", ret);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200464 return ret;
465 }
466
Javier González588726d32017-06-26 11:57:29 +0200467 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200468
469 /* This should not happen since the read failed during normal recovery,
470 * but the media works funny sometimes...
471 */
472 if (!rec_round++ && !rqd->error) {
473 rec_round = 0;
474 for (i = 0; i < rqd->nr_ppas; i++, r_ptr++) {
475 u64 lba = le64_to_cpu(meta_list[i].lba);
476
477 if (lba == ADDR_EMPTY || lba > pblk->rl.nr_secs)
478 continue;
479
480 pblk_update_map(pblk, lba, rqd->ppa_list[i]);
481 }
482 }
483
484 /* Reached the end of the written line */
485 if (rqd->error == NVM_RSP_ERR_EMPTYPAGE) {
486 int pad_secs, nr_error_bits, bit;
487 int ret;
488
489 bit = find_first_bit((void *)&rqd->ppa_status, rqd->nr_ppas);
490 nr_error_bits = rqd->nr_ppas - bit;
491
492 /* Roll back failed sectors */
493 line->cur_sec -= nr_error_bits;
494 line->left_msecs += nr_error_bits;
495 bitmap_clear(line->map_bitmap, line->cur_sec, nr_error_bits);
496
497 pad_secs = pblk_pad_distance(pblk);
498 if (pad_secs > line->left_msecs)
499 pad_secs = line->left_msecs;
500
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200501 ret = pblk_recov_pad_oob(pblk, line, pad_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200502 if (ret)
Matias Bjørling4e495a42018-07-13 10:48:42 +0200503 pblk_err(pblk, "OOB padding failed (err:%d)\n", ret);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200504
505 ret = pblk_recov_read_oob(pblk, line, p, r_ptr);
506 if (ret)
Matias Bjørling4e495a42018-07-13 10:48:42 +0200507 pblk_err(pblk, "OOB read failed (err:%d)\n", ret);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200508
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200509 left_ppas = 0;
510 }
511
512 left_ppas -= rq_ppas;
513 if (left_ppas > 0)
514 goto next_rq;
515
516 return ret;
517}
518
519static int pblk_recov_scan_oob(struct pblk *pblk, struct pblk_line *line,
520 struct pblk_recov_alloc p, int *done)
521{
522 struct nvm_tgt_dev *dev = pblk->dev;
523 struct nvm_geo *geo = &dev->geo;
524 struct ppa_addr *ppa_list;
525 struct pblk_sec_meta *meta_list;
526 struct nvm_rq *rqd;
527 struct bio *bio;
528 void *data;
529 dma_addr_t dma_ppa_list, dma_meta_list;
530 u64 paddr;
531 int rq_ppas, rq_len;
532 int i, j;
533 int ret = 0;
534 int left_ppas = pblk_calc_sec_in_line(pblk, line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200535
536 ppa_list = p.ppa_list;
537 meta_list = p.meta_list;
538 rqd = p.rqd;
539 data = p.data;
540 dma_ppa_list = p.dma_ppa_list;
541 dma_meta_list = p.dma_meta_list;
542
543 *done = 1;
544
545next_rq:
Javier González084ec9b2017-06-26 16:27:13 -0600546 memset(rqd, 0, pblk_g_rq_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200547
548 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0);
549 if (!rq_ppas)
550 rq_ppas = pblk->min_write_pgs;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200551 rq_len = rq_ppas * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200552
553 bio = bio_map_kern(dev->q, data, rq_len, GFP_KERNEL);
554 if (IS_ERR(bio))
555 return PTR_ERR(bio);
556
557 bio->bi_iter.bi_sector = 0; /* internal bio */
558 bio_set_op_attrs(bio, REQ_OP_READ, 0);
559
560 rqd->bio = bio;
561 rqd->opcode = NVM_OP_PREAD;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200562 rqd->meta_list = meta_list;
563 rqd->nr_ppas = rq_ppas;
564 rqd->ppa_list = ppa_list;
565 rqd->dma_ppa_list = dma_ppa_list;
566 rqd->dma_meta_list = dma_meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200567
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200568 if (pblk_io_aligned(pblk, rq_ppas))
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200569 rqd->is_seq = 1;
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200570
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200571 for (i = 0; i < rqd->nr_ppas; ) {
572 struct ppa_addr ppa;
573 int pos;
574
575 paddr = pblk_alloc_page(pblk, line, pblk->min_write_pgs);
576 ppa = addr_to_gen_ppa(pblk, paddr, line->id);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100577 pos = pblk_ppa_to_pos(geo, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200578
579 while (test_bit(pos, line->blk_bitmap)) {
580 paddr += pblk->min_write_pgs;
581 ppa = addr_to_gen_ppa(pblk, paddr, line->id);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100582 pos = pblk_ppa_to_pos(geo, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200583 }
584
585 for (j = 0; j < pblk->min_write_pgs; j++, i++, paddr++)
586 rqd->ppa_list[i] =
587 addr_to_gen_ppa(pblk, paddr, line->id);
588 }
589
Javier González1a94b2d2017-10-13 14:46:47 +0200590 ret = pblk_submit_io_sync(pblk, rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200591 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200592 pblk_err(pblk, "I/O submission failed: %d\n", ret);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200593 bio_put(bio);
594 return ret;
595 }
596
Javier González588726d32017-06-26 11:57:29 +0200597 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200598
599 /* Reached the end of the written line */
600 if (rqd->error) {
601 int nr_error_bits, bit;
602
603 bit = find_first_bit((void *)&rqd->ppa_status, rqd->nr_ppas);
604 nr_error_bits = rqd->nr_ppas - bit;
605
606 /* Roll back failed sectors */
607 line->cur_sec -= nr_error_bits;
608 line->left_msecs += nr_error_bits;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200609 bitmap_clear(line->map_bitmap, line->cur_sec, nr_error_bits);
610
611 left_ppas = 0;
612 rqd->nr_ppas = bit;
613
614 if (rqd->error != NVM_RSP_ERR_EMPTYPAGE)
615 *done = 0;
616 }
617
618 for (i = 0; i < rqd->nr_ppas; i++) {
619 u64 lba = le64_to_cpu(meta_list[i].lba);
620
621 if (lba == ADDR_EMPTY || lba > pblk->rl.nr_secs)
622 continue;
623
624 pblk_update_map(pblk, lba, rqd->ppa_list[i]);
625 }
626
627 left_ppas -= rq_ppas;
628 if (left_ppas > 0)
629 goto next_rq;
630
631 return ret;
632}
633
634/* Scan line for lbas on out of bound area */
635static int pblk_recov_l2p_from_oob(struct pblk *pblk, struct pblk_line *line)
636{
637 struct nvm_tgt_dev *dev = pblk->dev;
638 struct nvm_geo *geo = &dev->geo;
639 struct nvm_rq *rqd;
640 struct ppa_addr *ppa_list;
641 struct pblk_sec_meta *meta_list;
642 struct pblk_recov_alloc p;
643 void *data;
644 dma_addr_t dma_ppa_list, dma_meta_list;
645 int done, ret = 0;
646
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200647 meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL, &dma_meta_list);
Javier González2942f502017-10-13 14:46:10 +0200648 if (!meta_list)
649 return -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200650
651 ppa_list = (void *)(meta_list) + pblk_dma_meta_size;
652 dma_ppa_list = dma_meta_list + pblk_dma_meta_size;
653
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200654 data = kcalloc(pblk->max_write_pgs, geo->csecs, GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200655 if (!data) {
656 ret = -ENOMEM;
657 goto free_meta_list;
658 }
659
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200660 rqd = pblk_alloc_rqd(pblk, PBLK_READ);
Javier González2942f502017-10-13 14:46:10 +0200661
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200662 p.ppa_list = ppa_list;
663 p.meta_list = meta_list;
664 p.rqd = rqd;
665 p.data = data;
666 p.dma_ppa_list = dma_ppa_list;
667 p.dma_meta_list = dma_meta_list;
668
669 ret = pblk_recov_scan_oob(pblk, line, p, &done);
670 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200671 pblk_err(pblk, "could not recover L2P from OOB\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200672 goto out;
673 }
674
675 if (!done) {
676 ret = pblk_recov_scan_all_oob(pblk, line, p);
677 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200678 pblk_err(pblk, "could not recover L2P from OOB\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200679 goto out;
680 }
681 }
682
683 if (pblk_line_is_full(line))
684 pblk_line_recov_close(pblk, line);
685
686out:
687 kfree(data);
688free_meta_list:
689 nvm_dev_dma_free(dev->parent, meta_list, dma_meta_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200690
691 return ret;
692}
693
694/* Insert lines ordered by sequence number (seq_num) on list */
695static void pblk_recov_line_add_ordered(struct list_head *head,
696 struct pblk_line *line)
697{
698 struct pblk_line *t = NULL;
699
700 list_for_each_entry(t, head, list)
701 if (t->seq_nr > line->seq_nr)
702 break;
703
704 __list_add(&line->list, t->list.prev, &t->list);
705}
706
Hans Holmberg75610cd2017-10-13 14:46:38 +0200707static u64 pblk_line_emeta_start(struct pblk *pblk, struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200708{
709 struct nvm_tgt_dev *dev = pblk->dev;
710 struct nvm_geo *geo = &dev->geo;
711 struct pblk_line_meta *lm = &pblk->lm;
Hans Holmberg75610cd2017-10-13 14:46:38 +0200712 unsigned int emeta_secs;
713 u64 emeta_start;
714 struct ppa_addr ppa;
715 int pos;
716
717 emeta_secs = lm->emeta_sec[0];
718 emeta_start = lm->sec_per_line;
719
720 while (emeta_secs) {
721 emeta_start--;
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100722 ppa = addr_to_gen_ppa(pblk, emeta_start, line->id);
Hans Holmberg75610cd2017-10-13 14:46:38 +0200723 pos = pblk_ppa_to_pos(geo, ppa);
724 if (!test_bit(pos, line->blk_bitmap))
725 emeta_secs--;
726 }
727
728 return emeta_start;
729}
730
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200731static int pblk_recov_check_line_version(struct pblk *pblk,
732 struct line_emeta *emeta)
733{
734 struct line_header *header = &emeta->header;
735
736 if (header->version_major != EMETA_VERSION_MAJOR) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200737 pblk_err(pblk, "line major version mismatch: %d, expected: %d\n",
738 header->version_major, EMETA_VERSION_MAJOR);
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200739 return 1;
740 }
741
Matias Bjørling99b8dad2018-07-13 10:48:38 +0200742#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200743 if (header->version_minor > EMETA_VERSION_MINOR)
Matias Bjørling4e495a42018-07-13 10:48:42 +0200744 pblk_info(pblk, "newer line minor version found: %d\n",
Matias Bjørling99b8dad2018-07-13 10:48:38 +0200745 header->version_minor);
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200746#endif
747
748 return 0;
749}
750
Hans Holmberg76758392018-03-30 00:04:52 +0200751static void pblk_recov_wa_counters(struct pblk *pblk,
752 struct line_emeta *emeta)
753{
754 struct pblk_line_meta *lm = &pblk->lm;
755 struct line_header *header = &emeta->header;
756 struct wa_counters *wa = emeta_to_wa(lm, emeta);
757
758 /* WA counters were introduced in emeta version 0.2 */
759 if (header->version_major > 0 || header->version_minor >= 2) {
760 u64 user = le64_to_cpu(wa->user);
761 u64 pad = le64_to_cpu(wa->pad);
762 u64 gc = le64_to_cpu(wa->gc);
763
764 atomic64_set(&pblk->user_wa, user);
765 atomic64_set(&pblk->pad_wa, pad);
766 atomic64_set(&pblk->gc_wa, gc);
767
768 pblk->user_rst_wa = user;
769 pblk->pad_rst_wa = pad;
770 pblk->gc_rst_wa = gc;
771 }
772}
773
Hans Holmberg9156f362018-03-30 00:05:22 +0200774static int pblk_line_was_written(struct pblk_line *line,
Hans Holmbergb06be282018-06-01 16:41:12 +0200775 struct pblk *pblk)
Hans Holmberg9156f362018-03-30 00:05:22 +0200776{
777
Hans Holmbergb06be282018-06-01 16:41:12 +0200778 struct pblk_line_meta *lm = &pblk->lm;
779 struct nvm_tgt_dev *dev = pblk->dev;
780 struct nvm_geo *geo = &dev->geo;
781 struct nvm_chk_meta *chunk;
782 struct ppa_addr bppa;
783 int smeta_blk;
Hans Holmberg9156f362018-03-30 00:05:22 +0200784
Hans Holmbergb06be282018-06-01 16:41:12 +0200785 if (line->state == PBLK_LINESTATE_BAD)
786 return 0;
Hans Holmberg9156f362018-03-30 00:05:22 +0200787
Hans Holmbergb06be282018-06-01 16:41:12 +0200788 smeta_blk = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
789 if (smeta_blk >= lm->blk_per_line)
790 return 0;
791
792 bppa = pblk->luns[smeta_blk].bppa;
793 chunk = &line->chks[pblk_ppa_to_pos(geo, bppa)];
794
795 if (chunk->state & NVM_CHK_ST_FREE)
796 return 0;
797
798 return 1;
Hans Holmberg9156f362018-03-30 00:05:22 +0200799}
800
Hans Holmberg75610cd2017-10-13 14:46:38 +0200801struct pblk_line *pblk_recov_l2p(struct pblk *pblk)
802{
803 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200804 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
805 struct pblk_line *line, *tline, *data_line = NULL;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200806 struct pblk_smeta *smeta;
807 struct pblk_emeta *emeta;
808 struct line_smeta *smeta_buf;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200809 int found_lines = 0, recovered_lines = 0, open_lines = 0;
810 int is_next = 0;
811 int meta_line;
812 int i, valid_uuid = 0;
813 LIST_HEAD(recov_list);
814
815 /* TODO: Implement FTL snapshot */
816
817 /* Scan recovery - takes place when FTL snapshot fails */
818 spin_lock(&l_mg->free_lock);
819 meta_line = find_first_zero_bit(&l_mg->meta_bitmap, PBLK_DATA_LINES);
820 set_bit(meta_line, &l_mg->meta_bitmap);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200821 smeta = l_mg->sline_meta[meta_line];
822 emeta = l_mg->eline_meta[meta_line];
Javier González8224cbd2017-06-30 17:56:38 +0200823 smeta_buf = (struct line_smeta *)smeta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200824 spin_unlock(&l_mg->free_lock);
825
826 /* Order data lines using their sequence number */
827 for (i = 0; i < l_mg->nr_lines; i++) {
828 u32 crc;
829
830 line = &pblk->lines[i];
831
832 memset(smeta, 0, lm->smeta_len);
833 line->smeta = smeta;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200834 line->lun_bitmap = ((void *)(smeta_buf)) +
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200835 sizeof(struct line_smeta);
836
Hans Holmbergb06be282018-06-01 16:41:12 +0200837 if (!pblk_line_was_written(line, pblk))
Hans Holmberg9156f362018-03-30 00:05:22 +0200838 continue;
839
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200840 /* Lines that cannot be read are assumed as not written here */
841 if (pblk_line_read_smeta(pblk, line))
842 continue;
843
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200844 crc = pblk_calc_smeta_crc(pblk, smeta_buf);
845 if (le32_to_cpu(smeta_buf->crc) != crc)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200846 continue;
847
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200848 if (le32_to_cpu(smeta_buf->header.identifier) != PBLK_MAGIC)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200849 continue;
850
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200851 if (smeta_buf->header.version_major != SMETA_VERSION_MAJOR) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200852 pblk_err(pblk, "found incompatible line version %u\n",
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200853 smeta_buf->header.version_major);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200854 return ERR_PTR(-EINVAL);
855 }
856
857 /* The first valid instance uuid is used for initialization */
858 if (!valid_uuid) {
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200859 memcpy(pblk->instance_uuid, smeta_buf->header.uuid, 16);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200860 valid_uuid = 1;
861 }
862
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200863 if (memcmp(pblk->instance_uuid, smeta_buf->header.uuid, 16)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200864 pblk_debug(pblk, "ignore line %u due to uuid mismatch\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200865 i);
866 continue;
867 }
868
869 /* Update line metadata */
870 spin_lock(&line->lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200871 line->id = le32_to_cpu(smeta_buf->header.id);
872 line->type = le16_to_cpu(smeta_buf->header.type);
873 line->seq_nr = le64_to_cpu(smeta_buf->seq_nr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200874 spin_unlock(&line->lock);
875
876 /* Update general metadata */
877 spin_lock(&l_mg->free_lock);
878 if (line->seq_nr >= l_mg->d_seq_nr)
879 l_mg->d_seq_nr = line->seq_nr + 1;
880 l_mg->nr_free_lines--;
881 spin_unlock(&l_mg->free_lock);
882
883 if (pblk_line_recov_alloc(pblk, line))
884 goto out;
885
886 pblk_recov_line_add_ordered(&recov_list, line);
887 found_lines++;
Matias Bjørling4e495a42018-07-13 10:48:42 +0200888 pblk_debug(pblk, "recovering data line %d, seq:%llu\n",
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200889 line->id, smeta_buf->seq_nr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200890 }
891
892 if (!found_lines) {
893 pblk_setup_uuid(pblk);
894
895 spin_lock(&l_mg->free_lock);
896 WARN_ON_ONCE(!test_and_clear_bit(meta_line,
897 &l_mg->meta_bitmap));
898 spin_unlock(&l_mg->free_lock);
899
900 goto out;
901 }
902
903 /* Verify closed blocks and recover this portion of L2P table*/
904 list_for_each_entry_safe(line, tline, &recov_list, list) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200905 recovered_lines++;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200906
Hans Holmberg75610cd2017-10-13 14:46:38 +0200907 line->emeta_ssec = pblk_line_emeta_start(pblk, line);
Javier González8224cbd2017-06-30 17:56:38 +0200908 line->emeta = emeta;
909 memset(line->emeta->buf, 0, lm->emeta_len[0]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200910
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200911 if (pblk_line_read_emeta(pblk, line, line->emeta->buf)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200912 pblk_recov_l2p_from_oob(pblk, line);
913 goto next;
914 }
915
Hans Holmberg06bc0722018-01-05 14:16:08 +0100916 if (pblk_recov_check_emeta(pblk, line->emeta->buf)) {
917 pblk_recov_l2p_from_oob(pblk, line);
918 goto next;
919 }
920
Hans Holmbergd0ab0b12018-03-30 00:04:51 +0200921 if (pblk_recov_check_line_version(pblk, line->emeta->buf))
922 return ERR_PTR(-EINVAL);
923
Hans Holmberg76758392018-03-30 00:04:52 +0200924 pblk_recov_wa_counters(pblk, line->emeta->buf);
925
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200926 if (pblk_recov_l2p_from_emeta(pblk, line))
927 pblk_recov_l2p_from_oob(pblk, line);
928
929next:
930 if (pblk_line_is_full(line)) {
931 struct list_head *move_list;
932
933 spin_lock(&line->lock);
934 line->state = PBLK_LINESTATE_CLOSED;
935 move_list = pblk_line_gc_list(pblk, line);
936 spin_unlock(&line->lock);
937
938 spin_lock(&l_mg->gc_lock);
939 list_move_tail(&line->list, move_list);
940 spin_unlock(&l_mg->gc_lock);
941
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +0200942 kfree(line->map_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200943 line->map_bitmap = NULL;
944 line->smeta = NULL;
945 line->emeta = NULL;
946 } else {
947 if (open_lines > 1)
Matias Bjørling4e495a42018-07-13 10:48:42 +0200948 pblk_err(pblk, "failed to recover L2P\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200949
950 open_lines++;
951 line->meta_line = meta_line;
952 data_line = line;
953 }
954 }
955
956 spin_lock(&l_mg->free_lock);
957 if (!open_lines) {
958 WARN_ON_ONCE(!test_and_clear_bit(meta_line,
959 &l_mg->meta_bitmap));
960 pblk_line_replace_data(pblk);
961 } else {
962 /* Allocate next line for preparation */
963 l_mg->data_next = pblk_line_get(pblk);
964 if (l_mg->data_next) {
965 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
966 l_mg->data_next->type = PBLK_LINETYPE_DATA;
967 is_next = 1;
968 }
969 }
970 spin_unlock(&l_mg->free_lock);
971
Javier Gonzáleza7689932018-01-05 14:16:13 +0100972 if (is_next)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200973 pblk_line_erase(pblk, l_mg->data_next);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200974
975out:
976 if (found_lines != recovered_lines)
Matias Bjørling4e495a42018-07-13 10:48:42 +0200977 pblk_err(pblk, "failed to recover all found lines %d/%d\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200978 found_lines, recovered_lines);
979
980 return data_line;
981}
982
983/*
Javier González588726d32017-06-26 11:57:29 +0200984 * Pad current line
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200985 */
Javier González588726d32017-06-26 11:57:29 +0200986int pblk_recov_pad(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200987{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200988 struct pblk_line *line;
989 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González588726d32017-06-26 11:57:29 +0200990 int left_msecs;
991 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200992
993 spin_lock(&l_mg->free_lock);
994 line = l_mg->data_line;
Javier González588726d32017-06-26 11:57:29 +0200995 left_msecs = line->left_msecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200996 spin_unlock(&l_mg->free_lock);
997
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200998 ret = pblk_recov_pad_oob(pblk, line, left_msecs);
Javier González588726d32017-06-26 11:57:29 +0200999 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001000 pblk_err(pblk, "tear down padding failed (%d)\n", ret);
Javier Gonzálezee8d5c12017-06-30 17:56:40 +02001001 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001002 }
1003
Javier González588726d32017-06-26 11:57:29 +02001004 pblk_line_close_meta(pblk, line);
Javier González588726d32017-06-26 11:57:29 +02001005 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001006}