1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2015 Google, Inc.
4  *
5  * Author: Sami Tolvanen <samitolvanen@google.com>
6  */
7 
8 #include "dm-verity-fec.h"
9 #include <linux/math64.h>
10 
11 #define DM_MSG_PREFIX	"verity-fec"
12 
13 /*
14  * If error correction has been configured, returns true.
15  */
verity_fec_is_enabled(struct dm_verity * v)16 bool verity_fec_is_enabled(struct dm_verity *v)
17 {
18 	return v->fec && v->fec->dev;
19 }
20 
21 /*
22  * Return a pointer to dm_verity_fec_io after dm_verity_io and its variable
23  * length fields.
24  */
fec_io(struct dm_verity_io * io)25 static inline struct dm_verity_fec_io *fec_io(struct dm_verity_io *io)
26 {
27 	return (struct dm_verity_fec_io *)
28 		((char *)io + io->v->ti->per_io_data_size - sizeof(struct dm_verity_fec_io));
29 }
30 
31 /*
32  * Return an interleaved offset for a byte in RS block.
33  */
fec_interleave(struct dm_verity * v,u64 offset)34 static inline u64 fec_interleave(struct dm_verity *v, u64 offset)
35 {
36 	u32 mod;
37 
38 	mod = do_div(offset, v->fec->rsn);
39 	return offset + mod * (v->fec->rounds << v->data_dev_block_bits);
40 }
41 
42 /*
43  * Decode an RS block using Reed-Solomon.
44  */
fec_decode_rs8(struct dm_verity * v,struct dm_verity_fec_io * fio,u8 * data,u8 * fec,int neras)45 static int fec_decode_rs8(struct dm_verity *v, struct dm_verity_fec_io *fio,
46 			  u8 *data, u8 *fec, int neras)
47 {
48 	int i;
49 	uint16_t par[DM_VERITY_FEC_RSM - DM_VERITY_FEC_MIN_RSN];
50 
51 	for (i = 0; i < v->fec->roots; i++)
52 		par[i] = fec[i];
53 
54 	return decode_rs8(fio->rs, data, par, v->fec->rsn, NULL, neras,
55 			  fio->erasures, 0, NULL);
56 }
57 
58 /*
59  * Read error-correcting codes for the requested RS block. Returns a pointer
60  * to the data block. Caller is responsible for releasing buf.
61  */
fec_read_parity(struct dm_verity * v,u64 rsb,int index,unsigned int * offset,unsigned int par_buf_offset,struct dm_buffer ** buf,unsigned short ioprio)62 static u8 *fec_read_parity(struct dm_verity *v, u64 rsb, int index,
63 			   unsigned int *offset, unsigned int par_buf_offset,
64 			   struct dm_buffer **buf, unsigned short ioprio)
65 {
66 	u64 position, block, rem;
67 	u8 *res;
68 
69 	/* We have already part of parity bytes read, skip to the next block */
70 	if (par_buf_offset)
71 		index++;
72 
73 	position = (index + rsb) * v->fec->roots;
74 	block = div64_u64_rem(position, v->fec->io_size, &rem);
75 	*offset = par_buf_offset ? 0 : (unsigned int)rem;
76 
77 	res = dm_bufio_read_with_ioprio(v->fec->bufio, block, buf, ioprio);
78 	if (IS_ERR(res)) {
79 		DMERR("%s: FEC %llu: parity read failed (block %llu): %ld",
80 		      v->data_dev->name, (unsigned long long)rsb,
81 		      (unsigned long long)block, PTR_ERR(res));
82 		*buf = NULL;
83 	}
84 
85 	return res;
86 }
87 
88 /* Loop over each preallocated buffer slot. */
89 #define fec_for_each_prealloc_buffer(__i) \
90 	for (__i = 0; __i < DM_VERITY_FEC_BUF_PREALLOC; __i++)
91 
92 /* Loop over each extra buffer slot. */
93 #define fec_for_each_extra_buffer(io, __i) \
94 	for (__i = DM_VERITY_FEC_BUF_PREALLOC; __i < DM_VERITY_FEC_BUF_MAX; __i++)
95 
96 /* Loop over each allocated buffer. */
97 #define fec_for_each_buffer(io, __i) \
98 	for (__i = 0; __i < (io)->nbufs; __i++)
99 
100 /* Loop over each RS block in each allocated buffer. */
101 #define fec_for_each_buffer_rs_block(io, __i, __j) \
102 	fec_for_each_buffer(io, __i) \
103 		for (__j = 0; __j < 1 << DM_VERITY_FEC_BUF_RS_BITS; __j++)
104 
105 /*
106  * Return a pointer to the current RS block when called inside
107  * fec_for_each_buffer_rs_block.
108  */
fec_buffer_rs_block(struct dm_verity * v,struct dm_verity_fec_io * fio,unsigned int i,unsigned int j)109 static inline u8 *fec_buffer_rs_block(struct dm_verity *v,
110 				      struct dm_verity_fec_io *fio,
111 				      unsigned int i, unsigned int j)
112 {
113 	return &fio->bufs[i][j * v->fec->rsn];
114 }
115 
116 /*
117  * Return an index to the current RS block when called inside
118  * fec_for_each_buffer_rs_block.
119  */
fec_buffer_rs_index(unsigned int i,unsigned int j)120 static inline unsigned int fec_buffer_rs_index(unsigned int i, unsigned int j)
121 {
122 	return (i << DM_VERITY_FEC_BUF_RS_BITS) + j;
123 }
124 
125 /*
126  * Decode all RS blocks from buffers and copy corrected bytes into fio->output
127  * starting from block_offset.
128  */
fec_decode_bufs(struct dm_verity * v,struct dm_verity_io * io,struct dm_verity_fec_io * fio,u64 rsb,int byte_index,unsigned int block_offset,int neras)129 static int fec_decode_bufs(struct dm_verity *v, struct dm_verity_io *io,
130 			   struct dm_verity_fec_io *fio, u64 rsb, int byte_index,
131 			   unsigned int block_offset, int neras)
132 {
133 	int r, corrected = 0, res;
134 	struct dm_buffer *buf;
135 	unsigned int n, i, offset, par_buf_offset = 0;
136 	u8 *par, *block, par_buf[DM_VERITY_FEC_RSM - DM_VERITY_FEC_MIN_RSN];
137 	struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
138 
139 	par = fec_read_parity(v, rsb, block_offset, &offset,
140 			      par_buf_offset, &buf, bio_prio(bio));
141 	if (IS_ERR(par))
142 		return PTR_ERR(par);
143 
144 	/*
145 	 * Decode the RS blocks we have in bufs. Each RS block results in
146 	 * one corrected target byte and consumes fec->roots parity bytes.
147 	 */
148 	fec_for_each_buffer_rs_block(fio, n, i) {
149 		block = fec_buffer_rs_block(v, fio, n, i);
150 		memcpy(&par_buf[par_buf_offset], &par[offset], v->fec->roots - par_buf_offset);
151 		res = fec_decode_rs8(v, fio, block, par_buf, neras);
152 		if (res < 0) {
153 			r = res;
154 			goto error;
155 		}
156 
157 		corrected += res;
158 		fio->output[block_offset] = block[byte_index];
159 
160 		block_offset++;
161 		if (block_offset >= 1 << v->data_dev_block_bits)
162 			goto done;
163 
164 		/* Read the next block when we run out of parity bytes */
165 		offset += (v->fec->roots - par_buf_offset);
166 		/* Check if parity bytes are split between blocks */
167 		if (offset < v->fec->io_size && (offset + v->fec->roots) > v->fec->io_size) {
168 			par_buf_offset = v->fec->io_size - offset;
169 			memcpy(par_buf, &par[offset], par_buf_offset);
170 			offset += par_buf_offset;
171 		} else
172 			par_buf_offset = 0;
173 
174 		if (offset >= v->fec->io_size) {
175 			dm_bufio_release(buf);
176 
177 			par = fec_read_parity(v, rsb, block_offset, &offset,
178 					      par_buf_offset, &buf, bio_prio(bio));
179 			if (IS_ERR(par))
180 				return PTR_ERR(par);
181 		}
182 	}
183 done:
184 	r = corrected;
185 error:
186 	dm_bufio_release(buf);
187 
188 	if (r < 0 && neras)
189 		DMERR_LIMIT("%s: FEC %llu: failed to correct: %d",
190 			    v->data_dev->name, (unsigned long long)rsb, r);
191 	else if (r > 0)
192 		DMWARN_LIMIT("%s: FEC %llu: corrected %d errors",
193 			     v->data_dev->name, (unsigned long long)rsb, r);
194 
195 	return r;
196 }
197 
198 /*
199  * Locate data block erasures using verity hashes.
200  */
fec_is_erasure(struct dm_verity * v,struct dm_verity_io * io,u8 * want_digest,u8 * data)201 static int fec_is_erasure(struct dm_verity *v, struct dm_verity_io *io,
202 			  u8 *want_digest, u8 *data)
203 {
204 	if (unlikely(verity_hash(v, io, data, 1 << v->data_dev_block_bits,
205 				 io->tmp_digest, true)))
206 		return 0;
207 
208 	return memcmp(io->tmp_digest, want_digest, v->digest_size) != 0;
209 }
210 
211 /*
212  * Read data blocks that are part of the RS block and deinterleave as much as
213  * fits into buffers. Check for erasure locations if @neras is non-NULL.
214  */
fec_read_bufs(struct dm_verity * v,struct dm_verity_io * io,u64 rsb,u64 target,unsigned int block_offset,int * neras)215 static int fec_read_bufs(struct dm_verity *v, struct dm_verity_io *io,
216 			 u64 rsb, u64 target, unsigned int block_offset,
217 			 int *neras)
218 {
219 	bool is_zero;
220 	int i, j, target_index = -1;
221 	struct dm_buffer *buf;
222 	struct dm_bufio_client *bufio;
223 	struct dm_verity_fec_io *fio = fec_io(io);
224 	u64 block, ileaved;
225 	u8 *bbuf, *rs_block;
226 	u8 want_digest[HASH_MAX_DIGESTSIZE];
227 	unsigned int n, k;
228 	struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
229 
230 	if (neras)
231 		*neras = 0;
232 
233 	if (WARN_ON(v->digest_size > sizeof(want_digest)))
234 		return -EINVAL;
235 
236 	/*
237 	 * read each of the rsn data blocks that are part of the RS block, and
238 	 * interleave contents to available bufs
239 	 */
240 	for (i = 0; i < v->fec->rsn; i++) {
241 		ileaved = fec_interleave(v, rsb * v->fec->rsn + i);
242 
243 		/*
244 		 * target is the data block we want to correct, target_index is
245 		 * the index of this block within the rsn RS blocks
246 		 */
247 		if (ileaved == target)
248 			target_index = i;
249 
250 		block = ileaved >> v->data_dev_block_bits;
251 		bufio = v->fec->data_bufio;
252 
253 		if (block >= v->data_blocks) {
254 			block -= v->data_blocks;
255 
256 			/*
257 			 * blocks outside the area were assumed to contain
258 			 * zeros when encoding data was generated
259 			 */
260 			if (unlikely(block >= v->fec->hash_blocks))
261 				continue;
262 
263 			block += v->hash_start;
264 			bufio = v->bufio;
265 		}
266 
267 		bbuf = dm_bufio_read_with_ioprio(bufio, block, &buf, bio_prio(bio));
268 		if (IS_ERR(bbuf)) {
269 			DMWARN_LIMIT("%s: FEC %llu: read failed (%llu): %ld",
270 				     v->data_dev->name,
271 				     (unsigned long long)rsb,
272 				     (unsigned long long)block, PTR_ERR(bbuf));
273 
274 			/* assume the block is corrupted */
275 			if (neras && *neras <= v->fec->roots)
276 				fio->erasures[(*neras)++] = i;
277 
278 			continue;
279 		}
280 
281 		/* locate erasures if the block is on the data device */
282 		if (bufio == v->fec->data_bufio &&
283 		    verity_hash_for_block(v, io, block, want_digest,
284 					  &is_zero) == 0) {
285 			/* skip known zero blocks entirely */
286 			if (is_zero)
287 				goto done;
288 
289 			/*
290 			 * skip if we have already found the theoretical
291 			 * maximum number (i.e. fec->roots) of erasures
292 			 */
293 			if (neras && *neras <= v->fec->roots &&
294 			    fec_is_erasure(v, io, want_digest, bbuf))
295 				fio->erasures[(*neras)++] = i;
296 		}
297 
298 		/*
299 		 * deinterleave and copy the bytes that fit into bufs,
300 		 * starting from block_offset
301 		 */
302 		fec_for_each_buffer_rs_block(fio, n, j) {
303 			k = fec_buffer_rs_index(n, j) + block_offset;
304 
305 			if (k >= 1 << v->data_dev_block_bits)
306 				goto done;
307 
308 			rs_block = fec_buffer_rs_block(v, fio, n, j);
309 			rs_block[i] = bbuf[k];
310 		}
311 done:
312 		dm_bufio_release(buf);
313 	}
314 
315 	return target_index;
316 }
317 
318 /*
319  * Allocate RS control structure and FEC buffers from preallocated mempools,
320  * and attempt to allocate as many extra buffers as available.
321  */
fec_alloc_bufs(struct dm_verity * v,struct dm_verity_fec_io * fio)322 static int fec_alloc_bufs(struct dm_verity *v, struct dm_verity_fec_io *fio)
323 {
324 	unsigned int n;
325 
326 	if (!fio->rs)
327 		fio->rs = mempool_alloc(&v->fec->rs_pool, GFP_NOIO);
328 
329 	fec_for_each_prealloc_buffer(n) {
330 		if (fio->bufs[n])
331 			continue;
332 
333 		fio->bufs[n] = mempool_alloc(&v->fec->prealloc_pool, GFP_NOWAIT);
334 		if (unlikely(!fio->bufs[n])) {
335 			DMERR("failed to allocate FEC buffer");
336 			return -ENOMEM;
337 		}
338 	}
339 
340 	/* try to allocate the maximum number of buffers */
341 	fec_for_each_extra_buffer(fio, n) {
342 		if (fio->bufs[n])
343 			continue;
344 
345 		fio->bufs[n] = mempool_alloc(&v->fec->extra_pool, GFP_NOWAIT);
346 		/* we can manage with even one buffer if necessary */
347 		if (unlikely(!fio->bufs[n]))
348 			break;
349 	}
350 	fio->nbufs = n;
351 
352 	if (!fio->output)
353 		fio->output = mempool_alloc(&v->fec->output_pool, GFP_NOIO);
354 
355 	return 0;
356 }
357 
358 /*
359  * Initialize buffers and clear erasures. fec_read_bufs() assumes buffers are
360  * zeroed before deinterleaving.
361  */
fec_init_bufs(struct dm_verity * v,struct dm_verity_fec_io * fio)362 static void fec_init_bufs(struct dm_verity *v, struct dm_verity_fec_io *fio)
363 {
364 	unsigned int n;
365 
366 	fec_for_each_buffer(fio, n)
367 		memset(fio->bufs[n], 0, v->fec->rsn << DM_VERITY_FEC_BUF_RS_BITS);
368 
369 	memset(fio->erasures, 0, sizeof(fio->erasures));
370 }
371 
372 /*
373  * Decode all RS blocks in a single data block and return the target block
374  * (indicated by @offset) in fio->output. If @use_erasures is non-zero, uses
375  * hashes to locate erasures.
376  */
fec_decode_rsb(struct dm_verity * v,struct dm_verity_io * io,struct dm_verity_fec_io * fio,u64 rsb,u64 offset,const u8 * want_digest,bool use_erasures)377 static int fec_decode_rsb(struct dm_verity *v, struct dm_verity_io *io,
378 			  struct dm_verity_fec_io *fio, u64 rsb, u64 offset,
379 			  const u8 *want_digest, bool use_erasures)
380 {
381 	int r, neras = 0;
382 	unsigned int pos;
383 
384 	r = fec_alloc_bufs(v, fio);
385 	if (unlikely(r < 0))
386 		return r;
387 
388 	for (pos = 0; pos < 1 << v->data_dev_block_bits; ) {
389 		fec_init_bufs(v, fio);
390 
391 		r = fec_read_bufs(v, io, rsb, offset, pos,
392 				  use_erasures ? &neras : NULL);
393 		if (unlikely(r < 0))
394 			return r;
395 
396 		r = fec_decode_bufs(v, io, fio, rsb, r, pos, neras);
397 		if (r < 0)
398 			return r;
399 
400 		pos += fio->nbufs << DM_VERITY_FEC_BUF_RS_BITS;
401 	}
402 
403 	/* Always re-validate the corrected block against the expected hash */
404 	r = verity_hash(v, io, fio->output, 1 << v->data_dev_block_bits,
405 			io->tmp_digest, true);
406 	if (unlikely(r < 0))
407 		return r;
408 
409 	if (memcmp(io->tmp_digest, want_digest, v->digest_size)) {
410 		DMERR_LIMIT("%s: FEC %llu: failed to correct (%d erasures)",
411 			    v->data_dev->name, (unsigned long long)rsb, neras);
412 		return -EILSEQ;
413 	}
414 
415 	return 0;
416 }
417 
418 /* Correct errors in a block. Copies corrected block to dest. */
verity_fec_decode(struct dm_verity * v,struct dm_verity_io * io,enum verity_block_type type,const u8 * want_digest,sector_t block,u8 * dest)419 int verity_fec_decode(struct dm_verity *v, struct dm_verity_io *io,
420 		      enum verity_block_type type, const u8 *want_digest,
421 		      sector_t block, u8 *dest)
422 {
423 	int r;
424 	struct dm_verity_fec_io *fio = fec_io(io);
425 	u64 offset, res, rsb;
426 
427 	if (!verity_fec_is_enabled(v))
428 		return -EOPNOTSUPP;
429 
430 	if (fio->level >= DM_VERITY_FEC_MAX_RECURSION) {
431 		DMWARN_LIMIT("%s: FEC: recursion too deep", v->data_dev->name);
432 		return -EIO;
433 	}
434 
435 	fio->level++;
436 
437 	if (type == DM_VERITY_BLOCK_TYPE_METADATA)
438 		block = block - v->hash_start + v->data_blocks;
439 
440 	/*
441 	 * For RS(M, N), the continuous FEC data is divided into blocks of N
442 	 * bytes. Since block size may not be divisible by N, the last block
443 	 * is zero padded when decoding.
444 	 *
445 	 * Each byte of the block is covered by a different RS(M, N) code,
446 	 * and each code is interleaved over N blocks to make it less likely
447 	 * that bursty corruption will leave us in unrecoverable state.
448 	 */
449 
450 	offset = block << v->data_dev_block_bits;
451 	res = div64_u64(offset, v->fec->rounds << v->data_dev_block_bits);
452 
453 	/*
454 	 * The base RS block we can feed to the interleaver to find out all
455 	 * blocks required for decoding.
456 	 */
457 	rsb = offset - res * (v->fec->rounds << v->data_dev_block_bits);
458 
459 	/*
460 	 * Locating erasures is slow, so attempt to recover the block without
461 	 * them first. Do a second attempt with erasures if the corruption is
462 	 * bad enough.
463 	 */
464 	r = fec_decode_rsb(v, io, fio, rsb, offset, want_digest, false);
465 	if (r < 0) {
466 		r = fec_decode_rsb(v, io, fio, rsb, offset, want_digest, true);
467 		if (r < 0)
468 			goto done;
469 	}
470 
471 	memcpy(dest, fio->output, 1 << v->data_dev_block_bits);
472 
473 done:
474 	fio->level--;
475 	return r;
476 }
477 
478 /*
479  * Clean up per-bio data.
480  */
verity_fec_finish_io(struct dm_verity_io * io)481 void verity_fec_finish_io(struct dm_verity_io *io)
482 {
483 	unsigned int n;
484 	struct dm_verity_fec *f = io->v->fec;
485 	struct dm_verity_fec_io *fio = fec_io(io);
486 
487 	if (!verity_fec_is_enabled(io->v))
488 		return;
489 
490 	mempool_free(fio->rs, &f->rs_pool);
491 
492 	fec_for_each_prealloc_buffer(n)
493 		mempool_free(fio->bufs[n], &f->prealloc_pool);
494 
495 	fec_for_each_extra_buffer(fio, n)
496 		mempool_free(fio->bufs[n], &f->extra_pool);
497 
498 	mempool_free(fio->output, &f->output_pool);
499 }
500 
501 /*
502  * Initialize per-bio data.
503  */
verity_fec_init_io(struct dm_verity_io * io)504 void verity_fec_init_io(struct dm_verity_io *io)
505 {
506 	struct dm_verity_fec_io *fio = fec_io(io);
507 
508 	if (!verity_fec_is_enabled(io->v))
509 		return;
510 
511 	fio->rs = NULL;
512 	memset(fio->bufs, 0, sizeof(fio->bufs));
513 	fio->nbufs = 0;
514 	fio->output = NULL;
515 	fio->level = 0;
516 }
517 
518 /*
519  * Append feature arguments and values to the status table.
520  */
verity_fec_status_table(struct dm_verity * v,unsigned int sz,char * result,unsigned int maxlen)521 unsigned int verity_fec_status_table(struct dm_verity *v, unsigned int sz,
522 				 char *result, unsigned int maxlen)
523 {
524 	if (!verity_fec_is_enabled(v))
525 		return sz;
526 
527 	DMEMIT(" " DM_VERITY_OPT_FEC_DEV " %s "
528 	       DM_VERITY_OPT_FEC_BLOCKS " %llu "
529 	       DM_VERITY_OPT_FEC_START " %llu "
530 	       DM_VERITY_OPT_FEC_ROOTS " %d",
531 	       v->fec->dev->name,
532 	       (unsigned long long)v->fec->blocks,
533 	       (unsigned long long)v->fec->start,
534 	       v->fec->roots);
535 
536 	return sz;
537 }
538 
verity_fec_dtr(struct dm_verity * v)539 void verity_fec_dtr(struct dm_verity *v)
540 {
541 	struct dm_verity_fec *f = v->fec;
542 
543 	if (!verity_fec_is_enabled(v))
544 		goto out;
545 
546 	mempool_exit(&f->rs_pool);
547 	mempool_exit(&f->prealloc_pool);
548 	mempool_exit(&f->extra_pool);
549 	mempool_exit(&f->output_pool);
550 	kmem_cache_destroy(f->cache);
551 
552 	if (f->data_bufio)
553 		dm_bufio_client_destroy(f->data_bufio);
554 	if (f->bufio)
555 		dm_bufio_client_destroy(f->bufio);
556 
557 	if (f->dev)
558 		dm_put_device(v->ti, f->dev);
559 out:
560 	kfree(f);
561 	v->fec = NULL;
562 }
563 
fec_rs_alloc(gfp_t gfp_mask,void * pool_data)564 static void *fec_rs_alloc(gfp_t gfp_mask, void *pool_data)
565 {
566 	struct dm_verity *v = pool_data;
567 
568 	return init_rs_gfp(8, 0x11d, 0, 1, v->fec->roots, gfp_mask);
569 }
570 
fec_rs_free(void * element,void * pool_data)571 static void fec_rs_free(void *element, void *pool_data)
572 {
573 	struct rs_control *rs = element;
574 
575 	if (rs)
576 		free_rs(rs);
577 }
578 
verity_is_fec_opt_arg(const char * arg_name)579 bool verity_is_fec_opt_arg(const char *arg_name)
580 {
581 	return (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_DEV) ||
582 		!strcasecmp(arg_name, DM_VERITY_OPT_FEC_BLOCKS) ||
583 		!strcasecmp(arg_name, DM_VERITY_OPT_FEC_START) ||
584 		!strcasecmp(arg_name, DM_VERITY_OPT_FEC_ROOTS));
585 }
586 
verity_fec_parse_opt_args(struct dm_arg_set * as,struct dm_verity * v,unsigned int * argc,const char * arg_name)587 int verity_fec_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v,
588 			      unsigned int *argc, const char *arg_name)
589 {
590 	int r;
591 	struct dm_target *ti = v->ti;
592 	const char *arg_value;
593 	unsigned long long num_ll;
594 	unsigned char num_c;
595 	char dummy;
596 
597 	if (!*argc) {
598 		ti->error = "FEC feature arguments require a value";
599 		return -EINVAL;
600 	}
601 
602 	arg_value = dm_shift_arg(as);
603 	(*argc)--;
604 
605 	if (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_DEV)) {
606 		if (v->fec->dev) {
607 			ti->error = "FEC device already specified";
608 			return -EINVAL;
609 		}
610 		r = dm_get_device(ti, arg_value, BLK_OPEN_READ, &v->fec->dev);
611 		if (r) {
612 			ti->error = "FEC device lookup failed";
613 			return r;
614 		}
615 
616 	} else if (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_BLOCKS)) {
617 		if (sscanf(arg_value, "%llu%c", &num_ll, &dummy) != 1 ||
618 		    ((sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
619 		     >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll)) {
620 			ti->error = "Invalid " DM_VERITY_OPT_FEC_BLOCKS;
621 			return -EINVAL;
622 		}
623 		v->fec->blocks = num_ll;
624 
625 	} else if (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_START)) {
626 		if (sscanf(arg_value, "%llu%c", &num_ll, &dummy) != 1 ||
627 		    ((sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT)) >>
628 		     (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll)) {
629 			ti->error = "Invalid " DM_VERITY_OPT_FEC_START;
630 			return -EINVAL;
631 		}
632 		v->fec->start = num_ll;
633 
634 	} else if (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_ROOTS)) {
635 		if (sscanf(arg_value, "%hhu%c", &num_c, &dummy) != 1 || !num_c ||
636 		    num_c < (DM_VERITY_FEC_RSM - DM_VERITY_FEC_MAX_RSN) ||
637 		    num_c > (DM_VERITY_FEC_RSM - DM_VERITY_FEC_MIN_RSN)) {
638 			ti->error = "Invalid " DM_VERITY_OPT_FEC_ROOTS;
639 			return -EINVAL;
640 		}
641 		v->fec->roots = num_c;
642 
643 	} else {
644 		ti->error = "Unrecognized verity FEC feature request";
645 		return -EINVAL;
646 	}
647 
648 	return 0;
649 }
650 
651 /*
652  * Allocate dm_verity_fec for v->fec. Must be called before verity_fec_ctr.
653  */
verity_fec_ctr_alloc(struct dm_verity * v)654 int verity_fec_ctr_alloc(struct dm_verity *v)
655 {
656 	struct dm_verity_fec *f;
657 
658 	f = kzalloc(sizeof(struct dm_verity_fec), GFP_KERNEL);
659 	if (!f) {
660 		v->ti->error = "Cannot allocate FEC structure";
661 		return -ENOMEM;
662 	}
663 	v->fec = f;
664 
665 	return 0;
666 }
667 
668 /*
669  * Validate arguments and preallocate memory. Must be called after arguments
670  * have been parsed using verity_fec_parse_opt_args.
671  */
verity_fec_ctr(struct dm_verity * v)672 int verity_fec_ctr(struct dm_verity *v)
673 {
674 	struct dm_verity_fec *f = v->fec;
675 	struct dm_target *ti = v->ti;
676 	u64 hash_blocks, fec_blocks;
677 	int ret;
678 
679 	if (!verity_fec_is_enabled(v)) {
680 		verity_fec_dtr(v);
681 		return 0;
682 	}
683 
684 	/*
685 	 * FEC is computed over data blocks, possible metadata, and
686 	 * hash blocks. In other words, FEC covers total of fec_blocks
687 	 * blocks consisting of the following:
688 	 *
689 	 *  data blocks | hash blocks | metadata (optional)
690 	 *
691 	 * We allow metadata after hash blocks to support a use case
692 	 * where all data is stored on the same device and FEC covers
693 	 * the entire area.
694 	 *
695 	 * If metadata is included, we require it to be available on the
696 	 * hash device after the hash blocks.
697 	 */
698 
699 	hash_blocks = v->hash_blocks - v->hash_start;
700 
701 	/*
702 	 * Require matching block sizes for data and hash devices for
703 	 * simplicity.
704 	 */
705 	if (v->data_dev_block_bits != v->hash_dev_block_bits) {
706 		ti->error = "Block sizes must match to use FEC";
707 		return -EINVAL;
708 	}
709 
710 	if (!f->roots) {
711 		ti->error = "Missing " DM_VERITY_OPT_FEC_ROOTS;
712 		return -EINVAL;
713 	}
714 	f->rsn = DM_VERITY_FEC_RSM - f->roots;
715 
716 	if (!f->blocks) {
717 		ti->error = "Missing " DM_VERITY_OPT_FEC_BLOCKS;
718 		return -EINVAL;
719 	}
720 
721 	f->rounds = f->blocks;
722 	if (sector_div(f->rounds, f->rsn))
723 		f->rounds++;
724 
725 	/*
726 	 * Due to optional metadata, f->blocks can be larger than
727 	 * data_blocks and hash_blocks combined.
728 	 */
729 	if (f->blocks < v->data_blocks + hash_blocks || !f->rounds) {
730 		ti->error = "Invalid " DM_VERITY_OPT_FEC_BLOCKS;
731 		return -EINVAL;
732 	}
733 
734 	/*
735 	 * Metadata is accessed through the hash device, so we require
736 	 * it to be large enough.
737 	 */
738 	f->hash_blocks = f->blocks - v->data_blocks;
739 	if (dm_bufio_get_device_size(v->bufio) < f->hash_blocks) {
740 		ti->error = "Hash device is too small for "
741 			DM_VERITY_OPT_FEC_BLOCKS;
742 		return -E2BIG;
743 	}
744 
745 	f->io_size = 1 << v->data_dev_block_bits;
746 
747 	f->bufio = dm_bufio_client_create(f->dev->bdev,
748 					  f->io_size,
749 					  1, 0, NULL, NULL, 0);
750 	if (IS_ERR(f->bufio)) {
751 		ti->error = "Cannot initialize FEC bufio client";
752 		return PTR_ERR(f->bufio);
753 	}
754 
755 	dm_bufio_set_sector_offset(f->bufio, f->start << (v->data_dev_block_bits - SECTOR_SHIFT));
756 
757 	fec_blocks = div64_u64(f->rounds * f->roots, v->fec->roots << SECTOR_SHIFT);
758 	if (dm_bufio_get_device_size(f->bufio) < fec_blocks) {
759 		ti->error = "FEC device is too small";
760 		return -E2BIG;
761 	}
762 
763 	f->data_bufio = dm_bufio_client_create(v->data_dev->bdev,
764 					       1 << v->data_dev_block_bits,
765 					       1, 0, NULL, NULL, 0);
766 	if (IS_ERR(f->data_bufio)) {
767 		ti->error = "Cannot initialize FEC data bufio client";
768 		return PTR_ERR(f->data_bufio);
769 	}
770 
771 	if (dm_bufio_get_device_size(f->data_bufio) < v->data_blocks) {
772 		ti->error = "Data device is too small";
773 		return -E2BIG;
774 	}
775 
776 	/* Preallocate an rs_control structure for each worker thread */
777 	ret = mempool_init(&f->rs_pool, num_online_cpus(), fec_rs_alloc,
778 			   fec_rs_free, (void *) v);
779 	if (ret) {
780 		ti->error = "Cannot allocate RS pool";
781 		return ret;
782 	}
783 
784 	f->cache = kmem_cache_create("dm_verity_fec_buffers",
785 				     f->rsn << DM_VERITY_FEC_BUF_RS_BITS,
786 				     0, 0, NULL);
787 	if (!f->cache) {
788 		ti->error = "Cannot create FEC buffer cache";
789 		return -ENOMEM;
790 	}
791 
792 	/* Preallocate DM_VERITY_FEC_BUF_PREALLOC buffers for each thread */
793 	ret = mempool_init_slab_pool(&f->prealloc_pool, num_online_cpus() *
794 				     DM_VERITY_FEC_BUF_PREALLOC,
795 				     f->cache);
796 	if (ret) {
797 		ti->error = "Cannot allocate FEC buffer prealloc pool";
798 		return ret;
799 	}
800 
801 	ret = mempool_init_slab_pool(&f->extra_pool, 0, f->cache);
802 	if (ret) {
803 		ti->error = "Cannot allocate FEC buffer extra pool";
804 		return ret;
805 	}
806 
807 	/* Preallocate an output buffer for each thread */
808 	ret = mempool_init_kmalloc_pool(&f->output_pool, num_online_cpus(),
809 					1 << v->data_dev_block_bits);
810 	if (ret) {
811 		ti->error = "Cannot allocate FEC output pool";
812 		return ret;
813 	}
814 
815 	/* Reserve space for our per-bio data */
816 	ti->per_io_data_size += sizeof(struct dm_verity_fec_io);
817 
818 	return 0;
819 }
820