]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/md/dm-integrity.c
dm: mark targets that pass integrity data
[mirror_ubuntu-artful-kernel.git] / drivers / md / dm-integrity.c
CommitLineData
7eada909
MP
1/*
2 * Copyright (C) 2016-2017 Red Hat, Inc. All rights reserved.
3 * Copyright (C) 2016-2017 Milan Broz
4 * Copyright (C) 2016-2017 Mikulas Patocka
5 *
6 * This file is released under the GPL.
7 */
8
9#include <linux/module.h>
10#include <linux/device-mapper.h>
11#include <linux/dm-io.h>
12#include <linux/vmalloc.h>
13#include <linux/sort.h>
14#include <linux/rbtree.h>
15#include <linux/delay.h>
16#include <linux/random.h>
17#include <crypto/hash.h>
18#include <crypto/skcipher.h>
19#include <linux/async_tx.h>
20#include "dm-bufio.h"
21
22#define DM_MSG_PREFIX "integrity"
23
24#define DEFAULT_INTERLEAVE_SECTORS 32768
25#define DEFAULT_JOURNAL_SIZE_FACTOR 7
26#define DEFAULT_BUFFER_SECTORS 128
27#define DEFAULT_JOURNAL_WATERMARK 50
28#define DEFAULT_SYNC_MSEC 10000
29#define DEFAULT_MAX_JOURNAL_SECTORS 131072
30#define MIN_INTERLEAVE_SECTORS 3
31#define MAX_INTERLEAVE_SECTORS 31
32#define METADATA_WORKQUEUE_MAX_ACTIVE 16
33
34/*
35 * Warning - DEBUG_PRINT prints security-sensitive data to the log,
36 * so it should not be enabled in the official kernel
37 */
38//#define DEBUG_PRINT
39//#define INTERNAL_VERIFY
40
41/*
42 * On disk structures
43 */
44
45#define SB_MAGIC "integrt"
46#define SB_VERSION 1
47#define SB_SECTORS 8
48
49struct superblock {
50 __u8 magic[8];
51 __u8 version;
52 __u8 log2_interleave_sectors;
53 __u16 integrity_tag_size;
54 __u32 journal_sections;
55 __u64 provided_data_sectors; /* userspace uses this value */
56 __u32 flags;
57};
58
59#define SB_FLAG_HAVE_JOURNAL_MAC 0x1
60
61#define JOURNAL_ENTRY_ROUNDUP 8
62
63typedef __u64 commit_id_t;
64#define JOURNAL_MAC_PER_SECTOR 8
65
66struct journal_entry {
67 union {
68 struct {
69 __u32 sector_lo;
70 __u32 sector_hi;
71 } s;
72 __u64 sector;
73 } u;
74 commit_id_t last_bytes;
75 __u8 tag[0];
76};
77
78#if BITS_PER_LONG == 64
79#define journal_entry_set_sector(je, x) do { smp_wmb(); ACCESS_ONCE((je)->u.sector) = cpu_to_le64(x); } while (0)
80#define journal_entry_get_sector(je) le64_to_cpu((je)->u.sector)
81#elif defined(CONFIG_LBDAF)
82#define journal_entry_set_sector(je, x) do { (je)->u.s.sector_lo = cpu_to_le32(x); smp_wmb(); ACCESS_ONCE((je)->u.s.sector_hi) = cpu_to_le32((x) >> 32); } while (0)
83#define journal_entry_get_sector(je) le64_to_cpu((je)->u.sector)
84#else
85#define journal_entry_set_sector(je, x) do { (je)->u.s.sector_lo = cpu_to_le32(x); smp_wmb(); ACCESS_ONCE((je)->u.s.sector_hi) = cpu_to_le32(0); } while (0)
86#define journal_entry_get_sector(je) le32_to_cpu((je)->u.s.sector_lo)
87#endif
88#define journal_entry_is_unused(je) ((je)->u.s.sector_hi == cpu_to_le32(-1))
89#define journal_entry_set_unused(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-1)); } while (0)
90#define journal_entry_is_inprogress(je) ((je)->u.s.sector_hi == cpu_to_le32(-2))
91#define journal_entry_set_inprogress(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-2)); } while (0)
92
93#define JOURNAL_BLOCK_SECTORS 8
94#define JOURNAL_SECTOR_DATA ((1 << SECTOR_SHIFT) - sizeof(commit_id_t))
95#define JOURNAL_MAC_SIZE (JOURNAL_MAC_PER_SECTOR * JOURNAL_BLOCK_SECTORS)
96
97struct journal_sector {
98 __u8 entries[JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR];
99 __u8 mac[JOURNAL_MAC_PER_SECTOR];
100 commit_id_t commit_id;
101};
102
103#define MAX_TAG_SIZE (JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR - offsetof(struct journal_entry, tag))
104
105#define METADATA_PADDING_SECTORS 8
106
107#define N_COMMIT_IDS 4
108
109static unsigned char prev_commit_seq(unsigned char seq)
110{
111 return (seq + N_COMMIT_IDS - 1) % N_COMMIT_IDS;
112}
113
114static unsigned char next_commit_seq(unsigned char seq)
115{
116 return (seq + 1) % N_COMMIT_IDS;
117}
118
119/*
120 * In-memory structures
121 */
122
123struct journal_node {
124 struct rb_node node;
125 sector_t sector;
126};
127
128struct alg_spec {
129 char *alg_string;
130 char *key_string;
131 __u8 *key;
132 unsigned key_size;
133};
134
135struct dm_integrity_c {
136 struct dm_dev *dev;
137 unsigned tag_size;
138 __s8 log2_tag_size;
139 sector_t start;
140 mempool_t *journal_io_mempool;
141 struct dm_io_client *io;
142 struct dm_bufio_client *bufio;
143 struct workqueue_struct *metadata_wq;
144 struct superblock *sb;
145 unsigned journal_pages;
146 struct page_list *journal;
147 struct page_list *journal_io;
148 struct page_list *journal_xor;
149
150 struct crypto_skcipher *journal_crypt;
151 struct scatterlist **journal_scatterlist;
152 struct scatterlist **journal_io_scatterlist;
153 struct skcipher_request **sk_requests;
154
155 struct crypto_shash *journal_mac;
156
157 struct journal_node *journal_tree;
158 struct rb_root journal_tree_root;
159
160 sector_t provided_data_sectors;
161
162 unsigned short journal_entry_size;
163 unsigned char journal_entries_per_sector;
164 unsigned char journal_section_entries;
165 unsigned char journal_section_sectors;
166 unsigned journal_sections;
167 unsigned journal_entries;
168 sector_t device_sectors;
169 unsigned initial_sectors;
170 unsigned metadata_run;
171 __s8 log2_metadata_run;
172 __u8 log2_buffer_sectors;
173
174 unsigned char mode;
175 bool suspending;
176
177 int failed;
178
179 struct crypto_shash *internal_hash;
180
181 /* these variables are locked with endio_wait.lock */
182 struct rb_root in_progress;
183 wait_queue_head_t endio_wait;
184 struct workqueue_struct *wait_wq;
185
186 unsigned char commit_seq;
187 commit_id_t commit_ids[N_COMMIT_IDS];
188
189 unsigned committed_section;
190 unsigned n_committed_sections;
191
192 unsigned uncommitted_section;
193 unsigned n_uncommitted_sections;
194
195 unsigned free_section;
196 unsigned char free_section_entry;
197 unsigned free_sectors;
198
199 unsigned free_sectors_threshold;
200
201 struct workqueue_struct *commit_wq;
202 struct work_struct commit_work;
203
204 struct workqueue_struct *writer_wq;
205 struct work_struct writer_work;
206
207 struct bio_list flush_bio_list;
208
209 unsigned long autocommit_jiffies;
210 struct timer_list autocommit_timer;
211 unsigned autocommit_msec;
212
213 wait_queue_head_t copy_to_journal_wait;
214
215 struct completion crypto_backoff;
216
217 bool journal_uptodate;
218 bool just_formatted;
219
220 struct alg_spec internal_hash_alg;
221 struct alg_spec journal_crypt_alg;
222 struct alg_spec journal_mac_alg;
223};
224
225struct dm_integrity_range {
226 sector_t logical_sector;
227 unsigned n_sectors;
228 struct rb_node node;
229};
230
231struct dm_integrity_io {
232 struct work_struct work;
233
234 struct dm_integrity_c *ic;
235 bool write;
236 bool fua;
237
238 struct dm_integrity_range range;
239
240 sector_t metadata_block;
241 unsigned metadata_offset;
242
243 atomic_t in_flight;
244 int bi_error;
245
246 struct completion *completion;
247
248 struct block_device *orig_bi_bdev;
249 bio_end_io_t *orig_bi_end_io;
250 struct bio_integrity_payload *orig_bi_integrity;
251 struct bvec_iter orig_bi_iter;
252};
253
254struct journal_completion {
255 struct dm_integrity_c *ic;
256 atomic_t in_flight;
257 struct completion comp;
258};
259
260struct journal_io {
261 struct dm_integrity_range range;
262 struct journal_completion *comp;
263};
264
265static struct kmem_cache *journal_io_cache;
266
267#define JOURNAL_IO_MEMPOOL 32
268
269#ifdef DEBUG_PRINT
270#define DEBUG_print(x, ...) printk(KERN_DEBUG x, ##__VA_ARGS__)
271static void __DEBUG_bytes(__u8 *bytes, size_t len, const char *msg, ...)
272{
273 va_list args;
274 va_start(args, msg);
275 vprintk(msg, args);
276 va_end(args);
277 if (len)
278 pr_cont(":");
279 while (len) {
280 pr_cont(" %02x", *bytes);
281 bytes++;
282 len--;
283 }
284 pr_cont("\n");
285}
286#define DEBUG_bytes(bytes, len, msg, ...) __DEBUG_bytes(bytes, len, KERN_DEBUG msg, ##__VA_ARGS__)
287#else
288#define DEBUG_print(x, ...) do { } while (0)
289#define DEBUG_bytes(bytes, len, msg, ...) do { } while (0)
290#endif
291
292/*
293 * DM Integrity profile, protection is performed layer above (dm-crypt)
294 */
295static struct blk_integrity_profile dm_integrity_profile = {
296 .name = "DM-DIF-EXT-TAG",
297 .generate_fn = NULL,
298 .verify_fn = NULL,
299};
300
301static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map);
302static void integrity_bio_wait(struct work_struct *w);
303static void dm_integrity_dtr(struct dm_target *ti);
304
305static void dm_integrity_io_error(struct dm_integrity_c *ic, const char *msg, int err)
306{
307 if (!cmpxchg(&ic->failed, 0, err))
308 DMERR("Error on %s: %d", msg, err);
309}
310
311static int dm_integrity_failed(struct dm_integrity_c *ic)
312{
313 return ACCESS_ONCE(ic->failed);
314}
315
316static commit_id_t dm_integrity_commit_id(struct dm_integrity_c *ic, unsigned i,
317 unsigned j, unsigned char seq)
318{
319 /*
320 * Xor the number with section and sector, so that if a piece of
321 * journal is written at wrong place, it is detected.
322 */
323 return ic->commit_ids[seq] ^ cpu_to_le64(((__u64)i << 32) ^ j);
324}
325
326static void get_area_and_offset(struct dm_integrity_c *ic, sector_t data_sector,
327 sector_t *area, sector_t *offset)
328{
329 __u8 log2_interleave_sectors = ic->sb->log2_interleave_sectors;
330
331 *area = data_sector >> log2_interleave_sectors;
332 *offset = (unsigned)data_sector & ((1U << log2_interleave_sectors) - 1);
333}
334
335static __u64 get_metadata_sector_and_offset(struct dm_integrity_c *ic, sector_t area,
336 sector_t offset, unsigned *metadata_offset)
337{
338 __u64 ms;
339 unsigned mo;
340
341 ms = area << ic->sb->log2_interleave_sectors;
342 if (likely(ic->log2_metadata_run >= 0))
343 ms += area << ic->log2_metadata_run;
344 else
345 ms += area * ic->metadata_run;
346 ms >>= ic->log2_buffer_sectors;
347
348 if (likely(ic->log2_tag_size >= 0)) {
349 ms += offset >> (SECTOR_SHIFT + ic->log2_buffer_sectors - ic->log2_tag_size);
350 mo = (offset << ic->log2_tag_size) & ((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - 1);
351 } else {
352 ms += (__u64)offset * ic->tag_size >> (SECTOR_SHIFT + ic->log2_buffer_sectors);
353 mo = (offset * ic->tag_size) & ((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - 1);
354 }
355 *metadata_offset = mo;
356 return ms;
357}
358
359static sector_t get_data_sector(struct dm_integrity_c *ic, sector_t area, sector_t offset)
360{
361 sector_t result;
362
363 result = area << ic->sb->log2_interleave_sectors;
364 if (likely(ic->log2_metadata_run >= 0))
365 result += (area + 1) << ic->log2_metadata_run;
366 else
367 result += (area + 1) * ic->metadata_run;
368
369 result += (sector_t)ic->initial_sectors + offset;
370 return result;
371}
372
373static void wraparound_section(struct dm_integrity_c *ic, unsigned *sec_ptr)
374{
375 if (unlikely(*sec_ptr >= ic->journal_sections))
376 *sec_ptr -= ic->journal_sections;
377}
378
379static int sync_rw_sb(struct dm_integrity_c *ic, int op, int op_flags)
380{
381 struct dm_io_request io_req;
382 struct dm_io_region io_loc;
383
384 io_req.bi_op = op;
385 io_req.bi_op_flags = op_flags;
386 io_req.mem.type = DM_IO_KMEM;
387 io_req.mem.ptr.addr = ic->sb;
388 io_req.notify.fn = NULL;
389 io_req.client = ic->io;
390 io_loc.bdev = ic->dev->bdev;
391 io_loc.sector = ic->start;
392 io_loc.count = SB_SECTORS;
393
394 return dm_io(&io_req, 1, &io_loc, NULL);
395}
396
397static void access_journal_check(struct dm_integrity_c *ic, unsigned section, unsigned offset,
398 bool e, const char *function)
399{
400#if defined(CONFIG_DM_DEBUG) || defined(INTERNAL_VERIFY)
401 unsigned limit = e ? ic->journal_section_entries : ic->journal_section_sectors;
402
403 if (unlikely(section >= ic->journal_sections) ||
404 unlikely(offset >= limit)) {
405 printk(KERN_CRIT "%s: invalid access at (%u,%u), limit (%u,%u)\n",
406 function, section, offset, ic->journal_sections, limit);
407 BUG();
408 }
409#endif
410}
411
412static void page_list_location(struct dm_integrity_c *ic, unsigned section, unsigned offset,
413 unsigned *pl_index, unsigned *pl_offset)
414{
415 unsigned sector;
416
417 access_journal_check(ic, section, offset, false, "access_journal");
418
419 sector = section * ic->journal_section_sectors + offset;
420
421 *pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
422 *pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
423}
424
425static struct journal_sector *access_page_list(struct dm_integrity_c *ic, struct page_list *pl,
426 unsigned section, unsigned offset, unsigned *n_sectors)
427{
428 unsigned pl_index, pl_offset;
429 char *va;
430
431 page_list_location(ic, section, offset, &pl_index, &pl_offset);
432
433 if (n_sectors)
434 *n_sectors = (PAGE_SIZE - pl_offset) >> SECTOR_SHIFT;
435
436 va = lowmem_page_address(pl[pl_index].page);
437
438 return (struct journal_sector *)(va + pl_offset);
439}
440
441static struct journal_sector *access_journal(struct dm_integrity_c *ic, unsigned section, unsigned offset)
442{
443 return access_page_list(ic, ic->journal, section, offset, NULL);
444}
445
446static struct journal_entry *access_journal_entry(struct dm_integrity_c *ic, unsigned section, unsigned n)
447{
448 unsigned rel_sector, offset;
449 struct journal_sector *js;
450
451 access_journal_check(ic, section, n, true, "access_journal_entry");
452
453 rel_sector = n % JOURNAL_BLOCK_SECTORS;
454 offset = n / JOURNAL_BLOCK_SECTORS;
455
456 js = access_journal(ic, section, rel_sector);
457 return (struct journal_entry *)((char *)js + offset * ic->journal_entry_size);
458}
459
460static struct journal_sector *access_journal_data(struct dm_integrity_c *ic, unsigned section, unsigned n)
461{
462 access_journal_check(ic, section, n, true, "access_journal_data");
463
464 return access_journal(ic, section, n + JOURNAL_BLOCK_SECTORS);
465}
466
467static void section_mac(struct dm_integrity_c *ic, unsigned section, __u8 result[JOURNAL_MAC_SIZE])
468{
469 SHASH_DESC_ON_STACK(desc, ic->journal_mac);
470 int r;
471 unsigned j, size;
472
473 desc->tfm = ic->journal_mac;
474 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
475
476 r = crypto_shash_init(desc);
477 if (unlikely(r)) {
478 dm_integrity_io_error(ic, "crypto_shash_init", r);
479 goto err;
480 }
481
482 for (j = 0; j < ic->journal_section_entries; j++) {
483 struct journal_entry *je = access_journal_entry(ic, section, j);
484 r = crypto_shash_update(desc, (__u8 *)&je->u.sector, sizeof je->u.sector);
485 if (unlikely(r)) {
486 dm_integrity_io_error(ic, "crypto_shash_update", r);
487 goto err;
488 }
489 }
490
491 size = crypto_shash_digestsize(ic->journal_mac);
492
493 if (likely(size <= JOURNAL_MAC_SIZE)) {
494 r = crypto_shash_final(desc, result);
495 if (unlikely(r)) {
496 dm_integrity_io_error(ic, "crypto_shash_final", r);
497 goto err;
498 }
499 memset(result + size, 0, JOURNAL_MAC_SIZE - size);
500 } else {
501 __u8 digest[size];
502 r = crypto_shash_final(desc, digest);
503 if (unlikely(r)) {
504 dm_integrity_io_error(ic, "crypto_shash_final", r);
505 goto err;
506 }
507 memcpy(result, digest, JOURNAL_MAC_SIZE);
508 }
509
510 return;
511err:
512 memset(result, 0, JOURNAL_MAC_SIZE);
513}
514
515static void rw_section_mac(struct dm_integrity_c *ic, unsigned section, bool wr)
516{
517 __u8 result[JOURNAL_MAC_SIZE];
518 unsigned j;
519
520 if (!ic->journal_mac)
521 return;
522
523 section_mac(ic, section, result);
524
525 for (j = 0; j < JOURNAL_BLOCK_SECTORS; j++) {
526 struct journal_sector *js = access_journal(ic, section, j);
527
528 if (likely(wr))
529 memcpy(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR);
530 else {
531 if (memcmp(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR))
532 dm_integrity_io_error(ic, "journal mac", -EILSEQ);
533 }
534 }
535}
536
537static void complete_journal_op(void *context)
538{
539 struct journal_completion *comp = context;
540 BUG_ON(!atomic_read(&comp->in_flight));
541 if (likely(atomic_dec_and_test(&comp->in_flight)))
542 complete(&comp->comp);
543}
544
545static void xor_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
546 unsigned n_sections, struct journal_completion *comp)
547{
548 struct async_submit_ctl submit;
549 size_t n_bytes = (size_t)(n_sections * ic->journal_section_sectors) << SECTOR_SHIFT;
550 unsigned pl_index, pl_offset, section_index;
551 struct page_list *source_pl, *target_pl;
552
553 if (likely(encrypt)) {
554 source_pl = ic->journal;
555 target_pl = ic->journal_io;
556 } else {
557 source_pl = ic->journal_io;
558 target_pl = ic->journal;
559 }
560
561 page_list_location(ic, section, 0, &pl_index, &pl_offset);
562
563 atomic_add(roundup(pl_offset + n_bytes, PAGE_SIZE) >> PAGE_SHIFT, &comp->in_flight);
564
565 init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL, complete_journal_op, comp, NULL);
566
567 section_index = pl_index;
568
569 do {
570 size_t this_step;
571 struct page *src_pages[2];
572 struct page *dst_page;
573
574 while (unlikely(pl_index == section_index)) {
575 unsigned dummy;
576 if (likely(encrypt))
577 rw_section_mac(ic, section, true);
578 section++;
579 n_sections--;
580 if (!n_sections)
581 break;
582 page_list_location(ic, section, 0, &section_index, &dummy);
583 }
584
585 this_step = min(n_bytes, (size_t)PAGE_SIZE - pl_offset);
586 dst_page = target_pl[pl_index].page;
587 src_pages[0] = source_pl[pl_index].page;
588 src_pages[1] = ic->journal_xor[pl_index].page;
589
590 async_xor(dst_page, src_pages, pl_offset, 2, this_step, &submit);
591
592 pl_index++;
593 pl_offset = 0;
594 n_bytes -= this_step;
595 } while (n_bytes);
596
597 BUG_ON(n_sections);
598
599 async_tx_issue_pending_all();
600}
601
602static void complete_journal_encrypt(struct crypto_async_request *req, int err)
603{
604 struct journal_completion *comp = req->data;
605 if (unlikely(err)) {
606 if (likely(err == -EINPROGRESS)) {
607 complete(&comp->ic->crypto_backoff);
608 return;
609 }
610 dm_integrity_io_error(comp->ic, "asynchronous encrypt", err);
611 }
612 complete_journal_op(comp);
613}
614
615static bool do_crypt(bool encrypt, struct skcipher_request *req, struct journal_completion *comp)
616{
617 int r;
618 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
619 complete_journal_encrypt, comp);
620 if (likely(encrypt))
621 r = crypto_skcipher_encrypt(req);
622 else
623 r = crypto_skcipher_decrypt(req);
624 if (likely(!r))
625 return false;
626 if (likely(r == -EINPROGRESS))
627 return true;
628 if (likely(r == -EBUSY)) {
629 wait_for_completion(&comp->ic->crypto_backoff);
630 reinit_completion(&comp->ic->crypto_backoff);
631 return true;
632 }
633 dm_integrity_io_error(comp->ic, "encrypt", r);
634 return false;
635}
636
637static void crypt_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
638 unsigned n_sections, struct journal_completion *comp)
639{
640 struct scatterlist **source_sg;
641 struct scatterlist **target_sg;
642
643 atomic_add(2, &comp->in_flight);
644
645 if (likely(encrypt)) {
646 source_sg = ic->journal_scatterlist;
647 target_sg = ic->journal_io_scatterlist;
648 } else {
649 source_sg = ic->journal_io_scatterlist;
650 target_sg = ic->journal_scatterlist;
651 }
652
653 do {
654 struct skcipher_request *req;
655 unsigned ivsize;
656 char *iv;
657
658 if (likely(encrypt))
659 rw_section_mac(ic, section, true);
660
661 req = ic->sk_requests[section];
662 ivsize = crypto_skcipher_ivsize(ic->journal_crypt);
663 iv = req->iv;
664
665 memcpy(iv, iv + ivsize, ivsize);
666
667 req->src = source_sg[section];
668 req->dst = target_sg[section];
669
670 if (unlikely(do_crypt(encrypt, req, comp)))
671 atomic_inc(&comp->in_flight);
672
673 section++;
674 n_sections--;
675 } while (n_sections);
676
677 atomic_dec(&comp->in_flight);
678 complete_journal_op(comp);
679}
680
681static void encrypt_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
682 unsigned n_sections, struct journal_completion *comp)
683{
684 if (ic->journal_xor)
685 return xor_journal(ic, encrypt, section, n_sections, comp);
686 else
687 return crypt_journal(ic, encrypt, section, n_sections, comp);
688}
689
690static void complete_journal_io(unsigned long error, void *context)
691{
692 struct journal_completion *comp = context;
693 if (unlikely(error != 0))
694 dm_integrity_io_error(comp->ic, "writing journal", -EIO);
695 complete_journal_op(comp);
696}
697
698static void rw_journal(struct dm_integrity_c *ic, int op, int op_flags, unsigned section,
699 unsigned n_sections, struct journal_completion *comp)
700{
701 struct dm_io_request io_req;
702 struct dm_io_region io_loc;
703 unsigned sector, n_sectors, pl_index, pl_offset;
704 int r;
705
706 if (unlikely(dm_integrity_failed(ic))) {
707 if (comp)
708 complete_journal_io(-1UL, comp);
709 return;
710 }
711
712 sector = section * ic->journal_section_sectors;
713 n_sectors = n_sections * ic->journal_section_sectors;
714
715 pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
716 pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
717
718 io_req.bi_op = op;
719 io_req.bi_op_flags = op_flags;
720 io_req.mem.type = DM_IO_PAGE_LIST;
721 if (ic->journal_io)
722 io_req.mem.ptr.pl = &ic->journal_io[pl_index];
723 else
724 io_req.mem.ptr.pl = &ic->journal[pl_index];
725 io_req.mem.offset = pl_offset;
726 if (likely(comp != NULL)) {
727 io_req.notify.fn = complete_journal_io;
728 io_req.notify.context = comp;
729 } else {
730 io_req.notify.fn = NULL;
731 }
732 io_req.client = ic->io;
733 io_loc.bdev = ic->dev->bdev;
734 io_loc.sector = ic->start + SB_SECTORS + sector;
735 io_loc.count = n_sectors;
736
737 r = dm_io(&io_req, 1, &io_loc, NULL);
738 if (unlikely(r)) {
739 dm_integrity_io_error(ic, op == REQ_OP_READ ? "reading journal" : "writing journal", r);
740 if (comp) {
741 WARN_ONCE(1, "asynchronous dm_io failed: %d", r);
742 complete_journal_io(-1UL, comp);
743 }
744 }
745}
746
747static void write_journal(struct dm_integrity_c *ic, unsigned commit_start, unsigned commit_sections)
748{
749 struct journal_completion io_comp;
750 struct journal_completion crypt_comp_1;
751 struct journal_completion crypt_comp_2;
752 unsigned i;
753
754 io_comp.ic = ic;
755 io_comp.comp = COMPLETION_INITIALIZER_ONSTACK(io_comp.comp);
756
757 if (commit_start + commit_sections <= ic->journal_sections) {
758 io_comp.in_flight = (atomic_t)ATOMIC_INIT(1);
759 if (ic->journal_io) {
760 crypt_comp_1.ic = ic;
761 crypt_comp_1.comp = COMPLETION_INITIALIZER_ONSTACK(crypt_comp_1.comp);
762 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
763 encrypt_journal(ic, true, commit_start, commit_sections, &crypt_comp_1);
764 wait_for_completion_io(&crypt_comp_1.comp);
765 } else {
766 for (i = 0; i < commit_sections; i++)
767 rw_section_mac(ic, commit_start + i, true);
768 }
769 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, commit_sections, &io_comp);
770 } else {
771 unsigned to_end;
772 io_comp.in_flight = (atomic_t)ATOMIC_INIT(2);
773 to_end = ic->journal_sections - commit_start;
774 if (ic->journal_io) {
775 crypt_comp_1.ic = ic;
776 crypt_comp_1.comp = COMPLETION_INITIALIZER_ONSTACK(crypt_comp_1.comp);
777 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
778 encrypt_journal(ic, true, commit_start, to_end, &crypt_comp_1);
779 if (try_wait_for_completion(&crypt_comp_1.comp)) {
780 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
781 crypt_comp_1.comp = COMPLETION_INITIALIZER_ONSTACK(crypt_comp_1.comp);
782 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
783 encrypt_journal(ic, true, 0, commit_sections - to_end, &crypt_comp_1);
784 wait_for_completion_io(&crypt_comp_1.comp);
785 } else {
786 crypt_comp_2.ic = ic;
787 crypt_comp_2.comp = COMPLETION_INITIALIZER_ONSTACK(crypt_comp_2.comp);
788 crypt_comp_2.in_flight = (atomic_t)ATOMIC_INIT(0);
789 encrypt_journal(ic, true, 0, commit_sections - to_end, &crypt_comp_2);
790 wait_for_completion_io(&crypt_comp_1.comp);
791 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
792 wait_for_completion_io(&crypt_comp_2.comp);
793 }
794 } else {
795 for (i = 0; i < to_end; i++)
796 rw_section_mac(ic, commit_start + i, true);
797 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
798 for (i = 0; i < commit_sections - to_end; i++)
799 rw_section_mac(ic, i, true);
800 }
801 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, 0, commit_sections - to_end, &io_comp);
802 }
803
804 wait_for_completion_io(&io_comp.comp);
805}
806
807static void copy_from_journal(struct dm_integrity_c *ic, unsigned section, unsigned offset,
808 unsigned n_sectors, sector_t target, io_notify_fn fn, void *data)
809{
810 struct dm_io_request io_req;
811 struct dm_io_region io_loc;
812 int r;
813 unsigned sector, pl_index, pl_offset;
814
815 if (unlikely(dm_integrity_failed(ic))) {
816 fn(-1UL, data);
817 return;
818 }
819
820 sector = section * ic->journal_section_sectors + JOURNAL_BLOCK_SECTORS + offset;
821
822 pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
823 pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
824
825 io_req.bi_op = REQ_OP_WRITE;
826 io_req.bi_op_flags = 0;
827 io_req.mem.type = DM_IO_PAGE_LIST;
828 io_req.mem.ptr.pl = &ic->journal[pl_index];
829 io_req.mem.offset = pl_offset;
830 io_req.notify.fn = fn;
831 io_req.notify.context = data;
832 io_req.client = ic->io;
833 io_loc.bdev = ic->dev->bdev;
834 io_loc.sector = ic->start + target;
835 io_loc.count = n_sectors;
836
837 r = dm_io(&io_req, 1, &io_loc, NULL);
838 if (unlikely(r)) {
839 WARN_ONCE(1, "asynchronous dm_io failed: %d", r);
840 fn(-1UL, data);
841 }
842}
843
844static bool add_new_range(struct dm_integrity_c *ic, struct dm_integrity_range *new_range)
845{
846 struct rb_node **n = &ic->in_progress.rb_node;
847 struct rb_node *parent;
848
849 parent = NULL;
850
851 while (*n) {
852 struct dm_integrity_range *range = container_of(*n, struct dm_integrity_range, node);
853
854 parent = *n;
855 if (new_range->logical_sector + new_range->n_sectors <= range->logical_sector) {
856 n = &range->node.rb_left;
857 } else if (new_range->logical_sector >= range->logical_sector + range->n_sectors) {
858 n = &range->node.rb_right;
859 } else {
860 return false;
861 }
862 }
863
864 rb_link_node(&new_range->node, parent, n);
865 rb_insert_color(&new_range->node, &ic->in_progress);
866
867 return true;
868}
869
870static void remove_range_unlocked(struct dm_integrity_c *ic, struct dm_integrity_range *range)
871{
872 rb_erase(&range->node, &ic->in_progress);
873 wake_up_locked(&ic->endio_wait);
874}
875
876static void remove_range(struct dm_integrity_c *ic, struct dm_integrity_range *range)
877{
878 unsigned long flags;
879
880 spin_lock_irqsave(&ic->endio_wait.lock, flags);
881 remove_range_unlocked(ic, range);
882 spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
883}
884
885static void init_journal_node(struct journal_node *node)
886{
887 RB_CLEAR_NODE(&node->node);
888 node->sector = (sector_t)-1;
889}
890
891static void add_journal_node(struct dm_integrity_c *ic, struct journal_node *node, sector_t sector)
892{
893 struct rb_node **link;
894 struct rb_node *parent;
895
896 node->sector = sector;
897 BUG_ON(!RB_EMPTY_NODE(&node->node));
898
899 link = &ic->journal_tree_root.rb_node;
900 parent = NULL;
901
902 while (*link) {
903 struct journal_node *j;
904 parent = *link;
905 j = container_of(parent, struct journal_node, node);
906 if (sector < j->sector)
907 link = &j->node.rb_left;
908 else
909 link = &j->node.rb_right;
910 }
911
912 rb_link_node(&node->node, parent, link);
913 rb_insert_color(&node->node, &ic->journal_tree_root);
914}
915
916static void remove_journal_node(struct dm_integrity_c *ic, struct journal_node *node)
917{
918 BUG_ON(RB_EMPTY_NODE(&node->node));
919 rb_erase(&node->node, &ic->journal_tree_root);
920 init_journal_node(node);
921}
922
923#define NOT_FOUND (-1U)
924
925static unsigned find_journal_node(struct dm_integrity_c *ic, sector_t sector, sector_t *next_sector)
926{
927 struct rb_node *n = ic->journal_tree_root.rb_node;
928 unsigned found = NOT_FOUND;
929 *next_sector = (sector_t)-1;
930 while (n) {
931 struct journal_node *j = container_of(n, struct journal_node, node);
932 if (sector == j->sector) {
933 found = j - ic->journal_tree;
934 }
935 if (sector < j->sector) {
936 *next_sector = j->sector;
937 n = j->node.rb_left;
938 } else {
939 n = j->node.rb_right;
940 }
941 }
942
943 return found;
944}
945
946static bool test_journal_node(struct dm_integrity_c *ic, unsigned pos, sector_t sector)
947{
948 struct journal_node *node, *next_node;
949 struct rb_node *next;
950
951 if (unlikely(pos >= ic->journal_entries))
952 return false;
953 node = &ic->journal_tree[pos];
954 if (unlikely(RB_EMPTY_NODE(&node->node)))
955 return false;
956 if (unlikely(node->sector != sector))
957 return false;
958
959 next = rb_next(&node->node);
960 if (unlikely(!next))
961 return true;
962
963 next_node = container_of(next, struct journal_node, node);
964 return next_node->sector != sector;
965}
966
967static bool find_newer_committed_node(struct dm_integrity_c *ic, struct journal_node *node)
968{
969 struct rb_node *next;
970 struct journal_node *next_node;
971 unsigned next_section;
972
973 BUG_ON(RB_EMPTY_NODE(&node->node));
974
975 next = rb_next(&node->node);
976 if (unlikely(!next))
977 return false;
978
979 next_node = container_of(next, struct journal_node, node);
980
981 if (next_node->sector != node->sector)
982 return false;
983
984 next_section = (unsigned)(next_node - ic->journal_tree) / ic->journal_section_entries;
985 if (next_section >= ic->committed_section &&
986 next_section < ic->committed_section + ic->n_committed_sections)
987 return true;
988 if (next_section + ic->journal_sections < ic->committed_section + ic->n_committed_sections)
989 return true;
990
991 return false;
992}
993
994#define TAG_READ 0
995#define TAG_WRITE 1
996#define TAG_CMP 2
997
998static int dm_integrity_rw_tag(struct dm_integrity_c *ic, unsigned char *tag, sector_t *metadata_block,
999 unsigned *metadata_offset, unsigned total_size, int op)
1000{
1001 do {
1002 unsigned char *data, *dp;
1003 struct dm_buffer *b;
1004 unsigned to_copy;
1005 int r;
1006
1007 r = dm_integrity_failed(ic);
1008 if (unlikely(r))
1009 return r;
1010
1011 data = dm_bufio_read(ic->bufio, *metadata_block, &b);
1012 if (unlikely(IS_ERR(data)))
1013 return PTR_ERR(data);
1014
1015 to_copy = min((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - *metadata_offset, total_size);
1016 dp = data + *metadata_offset;
1017 if (op == TAG_READ) {
1018 memcpy(tag, dp, to_copy);
1019 } else if (op == TAG_WRITE) {
1020 memcpy(dp, tag, to_copy);
1021 dm_bufio_mark_buffer_dirty(b);
1022 } else {
1023 /* e.g.: op == TAG_CMP */
1024 if (unlikely(memcmp(dp, tag, to_copy))) {
1025 unsigned i;
1026
1027 for (i = 0; i < to_copy; i++) {
1028 if (dp[i] != tag[i])
1029 break;
1030 total_size--;
1031 }
1032 dm_bufio_release(b);
1033 return total_size;
1034 }
1035 }
1036 dm_bufio_release(b);
1037
1038 tag += to_copy;
1039 *metadata_offset += to_copy;
1040 if (unlikely(*metadata_offset == 1U << SECTOR_SHIFT << ic->log2_buffer_sectors)) {
1041 (*metadata_block)++;
1042 *metadata_offset = 0;
1043 }
1044 total_size -= to_copy;
1045 } while (unlikely(total_size));
1046
1047 return 0;
1048}
1049
1050static void dm_integrity_flush_buffers(struct dm_integrity_c *ic)
1051{
1052 int r;
1053 r = dm_bufio_write_dirty_buffers(ic->bufio);
1054 if (unlikely(r))
1055 dm_integrity_io_error(ic, "writing tags", r);
1056}
1057
1058static void sleep_on_endio_wait(struct dm_integrity_c *ic)
1059{
1060 DECLARE_WAITQUEUE(wait, current);
1061 __add_wait_queue(&ic->endio_wait, &wait);
1062 __set_current_state(TASK_UNINTERRUPTIBLE);
1063 spin_unlock_irq(&ic->endio_wait.lock);
1064 io_schedule();
1065 spin_lock_irq(&ic->endio_wait.lock);
1066 __remove_wait_queue(&ic->endio_wait, &wait);
1067}
1068
1069static void autocommit_fn(unsigned long data)
1070{
1071 struct dm_integrity_c *ic = (struct dm_integrity_c *)data;
1072
1073 if (likely(!dm_integrity_failed(ic)))
1074 queue_work(ic->commit_wq, &ic->commit_work);
1075}
1076
1077static void schedule_autocommit(struct dm_integrity_c *ic)
1078{
1079 if (!timer_pending(&ic->autocommit_timer))
1080 mod_timer(&ic->autocommit_timer, jiffies + ic->autocommit_jiffies);
1081}
1082
1083static void submit_flush_bio(struct dm_integrity_c *ic, struct dm_integrity_io *dio)
1084{
1085 struct bio *bio;
1086 spin_lock_irq(&ic->endio_wait.lock);
1087 bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1088 bio_list_add(&ic->flush_bio_list, bio);
1089 spin_unlock_irq(&ic->endio_wait.lock);
1090 queue_work(ic->commit_wq, &ic->commit_work);
1091}
1092
1093static void do_endio(struct dm_integrity_c *ic, struct bio *bio)
1094{
1095 int r = dm_integrity_failed(ic);
1096 if (unlikely(r) && !bio->bi_error)
1097 bio->bi_error = r;
1098 bio_endio(bio);
1099}
1100
1101static void do_endio_flush(struct dm_integrity_c *ic, struct dm_integrity_io *dio)
1102{
1103 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1104
1105 if (unlikely(dio->fua) && likely(!bio->bi_error) && likely(!dm_integrity_failed(ic)))
1106 submit_flush_bio(ic, dio);
1107 else
1108 do_endio(ic, bio);
1109}
1110
1111static void dec_in_flight(struct dm_integrity_io *dio)
1112{
1113 if (atomic_dec_and_test(&dio->in_flight)) {
1114 struct dm_integrity_c *ic = dio->ic;
1115 struct bio *bio;
1116
1117 remove_range(ic, &dio->range);
1118
1119 if (unlikely(dio->write))
1120 schedule_autocommit(ic);
1121
1122 bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1123
1124 if (unlikely(dio->bi_error) && !bio->bi_error)
1125 bio->bi_error = dio->bi_error;
1126 if (likely(!bio->bi_error) && unlikely(bio_sectors(bio) != dio->range.n_sectors)) {
1127 dio->range.logical_sector += dio->range.n_sectors;
1128 bio_advance(bio, dio->range.n_sectors << SECTOR_SHIFT);
1129 INIT_WORK(&dio->work, integrity_bio_wait);
1130 queue_work(ic->wait_wq, &dio->work);
1131 return;
1132 }
1133 do_endio_flush(ic, dio);
1134 }
1135}
1136
1137static void integrity_end_io(struct bio *bio)
1138{
1139 struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
1140
1141 bio->bi_iter = dio->orig_bi_iter;
1142 bio->bi_bdev = dio->orig_bi_bdev;
1143 if (dio->orig_bi_integrity) {
1144 bio->bi_integrity = dio->orig_bi_integrity;
1145 bio->bi_opf |= REQ_INTEGRITY;
1146 }
1147 bio->bi_end_io = dio->orig_bi_end_io;
1148
1149 if (dio->completion)
1150 complete(dio->completion);
1151
1152 dec_in_flight(dio);
1153}
1154
1155static void integrity_sector_checksum(struct dm_integrity_c *ic, sector_t sector,
1156 const char *data, char *result)
1157{
1158 __u64 sector_le = cpu_to_le64(sector);
1159 SHASH_DESC_ON_STACK(req, ic->internal_hash);
1160 int r;
1161 unsigned digest_size;
1162
1163 req->tfm = ic->internal_hash;
1164 req->flags = 0;
1165
1166 r = crypto_shash_init(req);
1167 if (unlikely(r < 0)) {
1168 dm_integrity_io_error(ic, "crypto_shash_init", r);
1169 goto failed;
1170 }
1171
1172 r = crypto_shash_update(req, (const __u8 *)&sector_le, sizeof sector_le);
1173 if (unlikely(r < 0)) {
1174 dm_integrity_io_error(ic, "crypto_shash_update", r);
1175 goto failed;
1176 }
1177
1178 r = crypto_shash_update(req, data, 1 << SECTOR_SHIFT);
1179 if (unlikely(r < 0)) {
1180 dm_integrity_io_error(ic, "crypto_shash_update", r);
1181 goto failed;
1182 }
1183
1184 r = crypto_shash_final(req, result);
1185 if (unlikely(r < 0)) {
1186 dm_integrity_io_error(ic, "crypto_shash_final", r);
1187 goto failed;
1188 }
1189
1190 digest_size = crypto_shash_digestsize(ic->internal_hash);
1191 if (unlikely(digest_size < ic->tag_size))
1192 memset(result + digest_size, 0, ic->tag_size - digest_size);
1193
1194 return;
1195
1196failed:
1197 /* this shouldn't happen anyway, the hash functions have no reason to fail */
1198 get_random_bytes(result, ic->tag_size);
1199}
1200
1201static void integrity_metadata(struct work_struct *w)
1202{
1203 struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work);
1204 struct dm_integrity_c *ic = dio->ic;
1205
1206 int r;
1207
1208 if (ic->internal_hash) {
1209 struct bvec_iter iter;
1210 struct bio_vec bv;
1211 unsigned digest_size = crypto_shash_digestsize(ic->internal_hash);
1212 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1213 char *checksums;
1214 unsigned extra_space = digest_size > ic->tag_size ? digest_size - ic->tag_size : 0;
1215 char checksums_onstack[ic->tag_size + extra_space];
1216 unsigned sectors_to_process = dio->range.n_sectors;
1217 sector_t sector = dio->range.logical_sector;
1218
c2bcb2b7
MP
1219 if (unlikely(ic->mode == 'R'))
1220 goto skip_io;
1221
7eada909
MP
1222 checksums = kmalloc((PAGE_SIZE >> SECTOR_SHIFT) * ic->tag_size + extra_space,
1223 GFP_NOIO | __GFP_NORETRY | __GFP_NOWARN);
1224 if (!checksums)
1225 checksums = checksums_onstack;
1226
1227 __bio_for_each_segment(bv, bio, iter, dio->orig_bi_iter) {
1228 unsigned pos;
1229 char *mem, *checksums_ptr;
1230
1231again:
1232 mem = (char *)kmap_atomic(bv.bv_page) + bv.bv_offset;
1233 pos = 0;
1234 checksums_ptr = checksums;
1235 do {
1236 integrity_sector_checksum(ic, sector, mem + pos, checksums_ptr);
1237 checksums_ptr += ic->tag_size;
1238 sectors_to_process--;
1239 pos += 1 << SECTOR_SHIFT;
1240 sector++;
1241 } while (pos < bv.bv_len && sectors_to_process && checksums != checksums_onstack);
1242 kunmap_atomic(mem);
1243
1244 r = dm_integrity_rw_tag(ic, checksums, &dio->metadata_block, &dio->metadata_offset,
1245 checksums_ptr - checksums, !dio->write ? TAG_CMP : TAG_WRITE);
1246 if (unlikely(r)) {
1247 if (r > 0) {
1248 DMERR("Checksum failed at sector 0x%llx",
1249 (unsigned long long)(sector - ((r + ic->tag_size - 1) / ic->tag_size)));
1250 r = -EILSEQ;
1251 }
1252 if (likely(checksums != checksums_onstack))
1253 kfree(checksums);
1254 goto error;
1255 }
1256
1257 if (!sectors_to_process)
1258 break;
1259
1260 if (unlikely(pos < bv.bv_len)) {
1261 bv.bv_offset += pos;
1262 bv.bv_len -= pos;
1263 goto again;
1264 }
1265 }
1266
1267 if (likely(checksums != checksums_onstack))
1268 kfree(checksums);
1269 } else {
1270 struct bio_integrity_payload *bip = dio->orig_bi_integrity;
1271
1272 if (bip) {
1273 struct bio_vec biv;
1274 struct bvec_iter iter;
1275 unsigned data_to_process = dio->range.n_sectors * ic->tag_size;
1276
1277 bip_for_each_vec(biv, bip, iter) {
1278 unsigned char *tag;
1279 unsigned this_len;
1280
1281 BUG_ON(PageHighMem(biv.bv_page));
1282 tag = lowmem_page_address(biv.bv_page) + biv.bv_offset;
1283 this_len = min(biv.bv_len, data_to_process);
1284 r = dm_integrity_rw_tag(ic, tag, &dio->metadata_block, &dio->metadata_offset,
1285 this_len, !dio->write ? TAG_READ : TAG_WRITE);
1286 if (unlikely(r))
1287 goto error;
1288 data_to_process -= this_len;
1289 if (!data_to_process)
1290 break;
1291 }
1292 }
1293 }
c2bcb2b7 1294skip_io:
7eada909
MP
1295 dec_in_flight(dio);
1296 return;
1297error:
1298 dio->bi_error = r;
1299 dec_in_flight(dio);
1300}
1301
1302static int dm_integrity_map(struct dm_target *ti, struct bio *bio)
1303{
1304 struct dm_integrity_c *ic = ti->private;
1305 struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
1306
1307 sector_t area, offset;
1308
1309 dio->ic = ic;
1310 dio->bi_error = 0;
1311
1312 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1313 submit_flush_bio(ic, dio);
1314 return DM_MAPIO_SUBMITTED;
1315 }
1316
1317 dio->range.logical_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
1318 dio->write = bio_op(bio) == REQ_OP_WRITE;
1319 dio->fua = dio->write && bio->bi_opf & REQ_FUA;
1320 if (unlikely(dio->fua)) {
1321 /*
1322 * Don't pass down the FUA flag because we have to flush
1323 * disk cache anyway.
1324 */
1325 bio->bi_opf &= ~REQ_FUA;
1326 }
1327 if (unlikely(dio->range.logical_sector + bio_sectors(bio) > ic->provided_data_sectors)) {
1328 DMERR("Too big sector number: 0x%llx + 0x%x > 0x%llx",
1329 (unsigned long long)dio->range.logical_sector, bio_sectors(bio),
1330 (unsigned long long)ic->provided_data_sectors);
1331 return -EIO;
1332 }
1333
c2bcb2b7
MP
1334 if (unlikely(ic->mode == 'R') && unlikely(dio->write))
1335 return -EIO;
1336
7eada909
MP
1337 get_area_and_offset(ic, dio->range.logical_sector, &area, &offset);
1338 dio->metadata_block = get_metadata_sector_and_offset(ic, area, offset, &dio->metadata_offset);
1339 bio->bi_iter.bi_sector = get_data_sector(ic, area, offset);
1340
1341 dm_integrity_map_continue(dio, true);
1342 return DM_MAPIO_SUBMITTED;
1343}
1344
1345static bool __journal_read_write(struct dm_integrity_io *dio, struct bio *bio,
1346 unsigned journal_section, unsigned journal_entry)
1347{
1348 struct dm_integrity_c *ic = dio->ic;
1349 sector_t logical_sector;
1350 unsigned n_sectors;
1351
1352 logical_sector = dio->range.logical_sector;
1353 n_sectors = dio->range.n_sectors;
1354 do {
1355 struct bio_vec bv = bio_iovec(bio);
1356 char *mem;
1357
1358 if (unlikely(bv.bv_len >> SECTOR_SHIFT > n_sectors))
1359 bv.bv_len = n_sectors << SECTOR_SHIFT;
1360 n_sectors -= bv.bv_len >> SECTOR_SHIFT;
1361 bio_advance_iter(bio, &bio->bi_iter, bv.bv_len);
1362retry_kmap:
1363 mem = kmap_atomic(bv.bv_page);
1364 if (likely(dio->write))
1365 flush_dcache_page(bv.bv_page);
1366
1367 do {
1368 struct journal_entry *je = access_journal_entry(ic, journal_section, journal_entry);
1369
1370 if (unlikely(!dio->write)) {
1371 struct journal_sector *js;
1372
1373 if (unlikely(journal_entry_is_inprogress(je))) {
1374 flush_dcache_page(bv.bv_page);
1375 kunmap_atomic(mem);
1376
1377 __io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je));
1378 goto retry_kmap;
1379 }
1380 smp_rmb();
1381 BUG_ON(journal_entry_get_sector(je) != logical_sector);
1382 js = access_journal_data(ic, journal_section, journal_entry);
1383 memcpy(mem + bv.bv_offset, js, JOURNAL_SECTOR_DATA);
1384 memcpy(mem + bv.bv_offset + JOURNAL_SECTOR_DATA, &je->last_bytes, sizeof je->last_bytes);
1385#ifdef INTERNAL_VERIFY
1386 if (ic->internal_hash) {
1387 char checksums_onstack[max(crypto_shash_digestsize(ic->internal_hash), ic->tag_size)];
1388
1389 integrity_sector_checksum(ic, logical_sector, mem + bv.bv_offset, checksums_onstack);
1390 if (unlikely(memcmp(checksums_onstack, je->tag, ic->tag_size))) {
1391 DMERR("Checksum failed when reading from journal, at sector 0x%llx",
1392 (unsigned long long)logical_sector);
1393 }
1394 }
1395#endif
1396 }
1397
1398 if (!ic->internal_hash) {
1399 struct bio_integrity_payload *bip = bio_integrity(bio);
1400 unsigned tag_todo = ic->tag_size;
1401 char *tag_ptr = je->tag;
1402
1403 if (bip) do {
1404 struct bio_vec biv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter);
1405 unsigned tag_now = min(biv.bv_len, tag_todo);
1406 char *tag_addr;
1407 BUG_ON(PageHighMem(biv.bv_page));
1408 tag_addr = lowmem_page_address(biv.bv_page) + biv.bv_offset;
1409 if (likely(dio->write))
1410 memcpy(tag_ptr, tag_addr, tag_now);
1411 else
1412 memcpy(tag_addr, tag_ptr, tag_now);
1413 bvec_iter_advance(bip->bip_vec, &bip->bip_iter, tag_now);
1414 tag_ptr += tag_now;
1415 tag_todo -= tag_now;
1416 } while (unlikely(tag_todo)); else {
1417 if (likely(dio->write))
1418 memset(tag_ptr, 0, tag_todo);
1419 }
1420 }
1421
1422 if (likely(dio->write)) {
1423 struct journal_sector *js;
1424
1425 js = access_journal_data(ic, journal_section, journal_entry);
1426 memcpy(js, mem + bv.bv_offset, 1 << SECTOR_SHIFT);
1427 je->last_bytes = js->commit_id;
1428
1429 if (ic->internal_hash) {
1430 unsigned digest_size = crypto_shash_digestsize(ic->internal_hash);
1431 if (unlikely(digest_size > ic->tag_size)) {
1432 char checksums_onstack[digest_size];
1433 integrity_sector_checksum(ic, logical_sector, (char *)js, checksums_onstack);
1434 memcpy(je->tag, checksums_onstack, ic->tag_size);
1435 } else
1436 integrity_sector_checksum(ic, logical_sector, (char *)js, je->tag);
1437 }
1438
1439 journal_entry_set_sector(je, logical_sector);
1440 }
1441 logical_sector++;
1442
1443 journal_entry++;
1444 if (unlikely(journal_entry == ic->journal_section_entries)) {
1445 journal_entry = 0;
1446 journal_section++;
1447 wraparound_section(ic, &journal_section);
1448 }
1449
1450 bv.bv_offset += 1 << SECTOR_SHIFT;
1451 } while (bv.bv_len -= 1 << SECTOR_SHIFT);
1452
1453 if (unlikely(!dio->write))
1454 flush_dcache_page(bv.bv_page);
1455 kunmap_atomic(mem);
1456 } while (n_sectors);
1457
1458 if (likely(dio->write)) {
1459 smp_mb();
1460 if (unlikely(waitqueue_active(&ic->copy_to_journal_wait)))
1461 wake_up(&ic->copy_to_journal_wait);
1462 if (ACCESS_ONCE(ic->free_sectors) <= ic->free_sectors_threshold) {
1463 queue_work(ic->commit_wq, &ic->commit_work);
1464 } else {
1465 schedule_autocommit(ic);
1466 }
1467 } else {
1468 remove_range(ic, &dio->range);
1469 }
1470
1471 if (unlikely(bio->bi_iter.bi_size)) {
1472 sector_t area, offset;
1473
1474 dio->range.logical_sector = logical_sector;
1475 get_area_and_offset(ic, dio->range.logical_sector, &area, &offset);
1476 dio->metadata_block = get_metadata_sector_and_offset(ic, area, offset, &dio->metadata_offset);
1477 return true;
1478 }
1479
1480 return false;
1481}
1482
1483static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map)
1484{
1485 struct dm_integrity_c *ic = dio->ic;
1486 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1487 unsigned journal_section, journal_entry;
1488 unsigned journal_read_pos;
1489 struct completion read_comp;
1490 bool need_sync_io = ic->internal_hash && !dio->write;
1491
1492 if (need_sync_io && from_map) {
1493 INIT_WORK(&dio->work, integrity_bio_wait);
1494 queue_work(ic->metadata_wq, &dio->work);
1495 return;
1496 }
1497
1498lock_retry:
1499 spin_lock_irq(&ic->endio_wait.lock);
1500retry:
1501 if (unlikely(dm_integrity_failed(ic))) {
1502 spin_unlock_irq(&ic->endio_wait.lock);
1503 do_endio(ic, bio);
1504 return;
1505 }
1506 dio->range.n_sectors = bio_sectors(bio);
1507 journal_read_pos = NOT_FOUND;
1508 if (likely(ic->mode == 'J')) {
1509 if (dio->write) {
1510 unsigned next_entry, i, pos;
1511 unsigned ws, we;
1512
1513 dio->range.n_sectors = min(dio->range.n_sectors, ic->free_sectors);
1514 if (unlikely(!dio->range.n_sectors))
1515 goto sleep;
1516 ic->free_sectors -= dio->range.n_sectors;
1517 journal_section = ic->free_section;
1518 journal_entry = ic->free_section_entry;
1519
1520 next_entry = ic->free_section_entry + dio->range.n_sectors;
1521 ic->free_section_entry = next_entry % ic->journal_section_entries;
1522 ic->free_section += next_entry / ic->journal_section_entries;
1523 ic->n_uncommitted_sections += next_entry / ic->journal_section_entries;
1524 wraparound_section(ic, &ic->free_section);
1525
1526 pos = journal_section * ic->journal_section_entries + journal_entry;
1527 ws = journal_section;
1528 we = journal_entry;
1529 for (i = 0; i < dio->range.n_sectors; i++) {
1530 struct journal_entry *je;
1531
1532 add_journal_node(ic, &ic->journal_tree[pos], dio->range.logical_sector + i);
1533 pos++;
1534 if (unlikely(pos >= ic->journal_entries))
1535 pos = 0;
1536
1537 je = access_journal_entry(ic, ws, we);
1538 BUG_ON(!journal_entry_is_unused(je));
1539 journal_entry_set_inprogress(je);
1540 we++;
1541 if (unlikely(we == ic->journal_section_entries)) {
1542 we = 0;
1543 ws++;
1544 wraparound_section(ic, &ws);
1545 }
1546 }
1547
1548 spin_unlock_irq(&ic->endio_wait.lock);
1549 goto journal_read_write;
1550 } else {
1551 sector_t next_sector;
1552 journal_read_pos = find_journal_node(ic, dio->range.logical_sector, &next_sector);
1553 if (likely(journal_read_pos == NOT_FOUND)) {
1554 if (unlikely(dio->range.n_sectors > next_sector - dio->range.logical_sector))
1555 dio->range.n_sectors = next_sector - dio->range.logical_sector;
1556 } else {
1557 unsigned i;
1558 for (i = 1; i < dio->range.n_sectors; i++) {
1559 if (!test_journal_node(ic, journal_read_pos + i, dio->range.logical_sector + i))
1560 break;
1561 }
1562 dio->range.n_sectors = i;
1563 }
1564 }
1565 }
1566 if (unlikely(!add_new_range(ic, &dio->range))) {
1567 /*
1568 * We must not sleep in the request routine because it could
1569 * stall bios on current->bio_list.
1570 * So, we offload the bio to a workqueue if we have to sleep.
1571 */
1572sleep:
1573 if (from_map) {
1574 spin_unlock_irq(&ic->endio_wait.lock);
1575 INIT_WORK(&dio->work, integrity_bio_wait);
1576 queue_work(ic->wait_wq, &dio->work);
1577 return;
1578 } else {
1579 sleep_on_endio_wait(ic);
1580 goto retry;
1581 }
1582 }
1583 spin_unlock_irq(&ic->endio_wait.lock);
1584
1585 if (unlikely(journal_read_pos != NOT_FOUND)) {
1586 journal_section = journal_read_pos / ic->journal_section_entries;
1587 journal_entry = journal_read_pos % ic->journal_section_entries;
1588 goto journal_read_write;
1589 }
1590
1591 dio->in_flight = (atomic_t)ATOMIC_INIT(2);
1592
1593 if (need_sync_io) {
1594 read_comp = COMPLETION_INITIALIZER_ONSTACK(read_comp);
1595 dio->completion = &read_comp;
1596 } else
1597 dio->completion = NULL;
1598
1599 dio->orig_bi_iter = bio->bi_iter;
1600
1601 dio->orig_bi_bdev = bio->bi_bdev;
1602 bio->bi_bdev = ic->dev->bdev;
1603
1604 dio->orig_bi_integrity = bio_integrity(bio);
1605 bio->bi_integrity = NULL;
1606 bio->bi_opf &= ~REQ_INTEGRITY;
1607
1608 dio->orig_bi_end_io = bio->bi_end_io;
1609 bio->bi_end_io = integrity_end_io;
1610
1611 bio->bi_iter.bi_size = dio->range.n_sectors << SECTOR_SHIFT;
1612 bio->bi_iter.bi_sector += ic->start;
1613 generic_make_request(bio);
1614
1615 if (need_sync_io) {
1616 wait_for_completion_io(&read_comp);
1617 integrity_metadata(&dio->work);
1618 } else {
1619 INIT_WORK(&dio->work, integrity_metadata);
1620 queue_work(ic->metadata_wq, &dio->work);
1621 }
1622
1623 return;
1624
1625journal_read_write:
1626 if (unlikely(__journal_read_write(dio, bio, journal_section, journal_entry)))
1627 goto lock_retry;
1628
1629 do_endio_flush(ic, dio);
1630}
1631
1632
1633static void integrity_bio_wait(struct work_struct *w)
1634{
1635 struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work);
1636
1637 dm_integrity_map_continue(dio, false);
1638}
1639
1640static void pad_uncommitted(struct dm_integrity_c *ic)
1641{
1642 if (ic->free_section_entry) {
1643 ic->free_sectors -= ic->journal_section_entries - ic->free_section_entry;
1644 ic->free_section_entry = 0;
1645 ic->free_section++;
1646 wraparound_section(ic, &ic->free_section);
1647 ic->n_uncommitted_sections++;
1648 }
1649}
1650
1651static void integrity_commit(struct work_struct *w)
1652{
1653 struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, commit_work);
1654 unsigned commit_start, commit_sections;
1655 unsigned i, j, n;
1656 struct bio *flushes;
1657
1658 del_timer(&ic->autocommit_timer);
1659
1660 spin_lock_irq(&ic->endio_wait.lock);
1661 flushes = bio_list_get(&ic->flush_bio_list);
1662 if (unlikely(ic->mode != 'J')) {
1663 spin_unlock_irq(&ic->endio_wait.lock);
1664 dm_integrity_flush_buffers(ic);
1665 goto release_flush_bios;
1666 }
1667
1668 pad_uncommitted(ic);
1669 commit_start = ic->uncommitted_section;
1670 commit_sections = ic->n_uncommitted_sections;
1671 spin_unlock_irq(&ic->endio_wait.lock);
1672
1673 if (!commit_sections)
1674 goto release_flush_bios;
1675
1676 i = commit_start;
1677 for (n = 0; n < commit_sections; n++) {
1678 for (j = 0; j < ic->journal_section_entries; j++) {
1679 struct journal_entry *je;
1680 je = access_journal_entry(ic, i, j);
1681 io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je));
1682 }
1683 for (j = 0; j < ic->journal_section_sectors; j++) {
1684 struct journal_sector *js;
1685 js = access_journal(ic, i, j);
1686 js->commit_id = dm_integrity_commit_id(ic, i, j, ic->commit_seq);
1687 }
1688 i++;
1689 if (unlikely(i >= ic->journal_sections))
1690 ic->commit_seq = next_commit_seq(ic->commit_seq);
1691 wraparound_section(ic, &i);
1692 }
1693 smp_rmb();
1694
1695 write_journal(ic, commit_start, commit_sections);
1696
1697 spin_lock_irq(&ic->endio_wait.lock);
1698 ic->uncommitted_section += commit_sections;
1699 wraparound_section(ic, &ic->uncommitted_section);
1700 ic->n_uncommitted_sections -= commit_sections;
1701 ic->n_committed_sections += commit_sections;
1702 spin_unlock_irq(&ic->endio_wait.lock);
1703
1704 if (ACCESS_ONCE(ic->free_sectors) <= ic->free_sectors_threshold)
1705 queue_work(ic->writer_wq, &ic->writer_work);
1706
1707release_flush_bios:
1708 while (flushes) {
1709 struct bio *next = flushes->bi_next;
1710 flushes->bi_next = NULL;
1711 do_endio(ic, flushes);
1712 flushes = next;
1713 }
1714}
1715
1716static void complete_copy_from_journal(unsigned long error, void *context)
1717{
1718 struct journal_io *io = context;
1719 struct journal_completion *comp = io->comp;
1720 struct dm_integrity_c *ic = comp->ic;
1721 remove_range(ic, &io->range);
1722 mempool_free(io, ic->journal_io_mempool);
1723 if (unlikely(error != 0))
1724 dm_integrity_io_error(ic, "copying from journal", -EIO);
1725 complete_journal_op(comp);
1726}
1727
1728static void do_journal_write(struct dm_integrity_c *ic, unsigned write_start,
1729 unsigned write_sections, bool from_replay)
1730{
1731 unsigned i, j, n;
1732 struct journal_completion comp;
1733
1734 comp.ic = ic;
1735 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
1736 comp.comp = COMPLETION_INITIALIZER_ONSTACK(comp.comp);
1737
1738 i = write_start;
1739 for (n = 0; n < write_sections; n++, i++, wraparound_section(ic, &i)) {
1740#ifndef INTERNAL_VERIFY
1741 if (unlikely(from_replay))
1742#endif
1743 rw_section_mac(ic, i, false);
1744 for (j = 0; j < ic->journal_section_entries; j++) {
1745 struct journal_entry *je = access_journal_entry(ic, i, j);
1746 sector_t sec, area, offset;
1747 unsigned k, l, next_loop;
1748 sector_t metadata_block;
1749 unsigned metadata_offset;
1750 struct journal_io *io;
1751
1752 if (journal_entry_is_unused(je))
1753 continue;
1754 BUG_ON(unlikely(journal_entry_is_inprogress(je)) && !from_replay);
1755 sec = journal_entry_get_sector(je);
1756 get_area_and_offset(ic, sec, &area, &offset);
1757 access_journal_data(ic, i, j)->commit_id = je->last_bytes;
1758 for (k = j + 1; k < ic->journal_section_entries; k++) {
1759 struct journal_entry *je2 = access_journal_entry(ic, i, k);
1760 sector_t sec2, area2, offset2;
1761 if (journal_entry_is_unused(je2))
1762 break;
1763 BUG_ON(unlikely(journal_entry_is_inprogress(je2)) && !from_replay);
1764 sec2 = journal_entry_get_sector(je2);
1765 get_area_and_offset(ic, sec2, &area2, &offset2);
1766 if (area2 != area || offset2 != offset + (k - j))
1767 break;
1768 access_journal_data(ic, i, k)->commit_id = je2->last_bytes;
1769 }
1770 next_loop = k - 1;
1771
1772 io = mempool_alloc(ic->journal_io_mempool, GFP_NOIO);
1773 io->comp = &comp;
1774 io->range.logical_sector = sec;
1775 io->range.n_sectors = k - j;
1776
1777 spin_lock_irq(&ic->endio_wait.lock);
1778 while (unlikely(!add_new_range(ic, &io->range)))
1779 sleep_on_endio_wait(ic);
1780
1781 if (likely(!from_replay)) {
1782 struct journal_node *section_node = &ic->journal_tree[i * ic->journal_section_entries];
1783
1784 /* don't write if there is newer committed sector */
1785 while (j < k && find_newer_committed_node(ic, &section_node[j])) {
1786 struct journal_entry *je2 = access_journal_entry(ic, i, j);
1787
1788 journal_entry_set_unused(je2);
1789 remove_journal_node(ic, &section_node[j]);
1790 j++;
1791 sec++;
1792 offset++;
1793 }
1794 while (j < k && find_newer_committed_node(ic, &section_node[k - 1])) {
1795 struct journal_entry *je2 = access_journal_entry(ic, i, k - 1);
1796
1797 journal_entry_set_unused(je2);
1798 remove_journal_node(ic, &section_node[k - 1]);
1799 k--;
1800 }
1801 if (j == k) {
1802 remove_range_unlocked(ic, &io->range);
1803 spin_unlock_irq(&ic->endio_wait.lock);
1804 mempool_free(io, ic->journal_io_mempool);
1805 goto skip_io;
1806 }
1807 for (l = j; l < k; l++) {
1808 remove_journal_node(ic, &section_node[l]);
1809 }
1810 }
1811 spin_unlock_irq(&ic->endio_wait.lock);
1812
1813 metadata_block = get_metadata_sector_and_offset(ic, area, offset, &metadata_offset);
1814 for (l = j; l < k; l++) {
1815 int r;
1816 struct journal_entry *je2 = access_journal_entry(ic, i, l);
1817
1818 if (
1819#ifndef INTERNAL_VERIFY
1820 unlikely(from_replay) &&
1821#endif
1822 ic->internal_hash) {
1823 unsigned char test_tag[ic->tag_size];
1824
1825 integrity_sector_checksum(ic, sec + (l - j),
1826 (char *)access_journal_data(ic, i, l), test_tag);
1827 if (unlikely(memcmp(test_tag, je2->tag, ic->tag_size)))
1828 dm_integrity_io_error(ic, "tag mismatch when replaying journal", -EILSEQ);
1829 }
1830
1831 journal_entry_set_unused(je2);
1832 r = dm_integrity_rw_tag(ic, je2->tag, &metadata_block, &metadata_offset,
1833 ic->tag_size, TAG_WRITE);
1834 if (unlikely(r)) {
1835 dm_integrity_io_error(ic, "reading tags", r);
1836 }
1837 }
1838
1839 atomic_inc(&comp.in_flight);
1840 copy_from_journal(ic, i, j, k - j, get_data_sector(ic, area, offset),
1841 complete_copy_from_journal, io);
1842skip_io:
1843 j = next_loop;
1844 }
1845 }
1846
1847 dm_bufio_write_dirty_buffers_async(ic->bufio);
1848
1849 complete_journal_op(&comp);
1850 wait_for_completion_io(&comp.comp);
1851
1852 dm_integrity_flush_buffers(ic);
1853}
1854
1855static void integrity_writer(struct work_struct *w)
1856{
1857 struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, writer_work);
1858 unsigned write_start, write_sections;
1859
1860 unsigned prev_free_sectors;
1861
1862 /* the following test is not needed, but it tests the replay code */
1863 if (ACCESS_ONCE(ic->suspending))
1864 return;
1865
1866 spin_lock_irq(&ic->endio_wait.lock);
1867 write_start = ic->committed_section;
1868 write_sections = ic->n_committed_sections;
1869 spin_unlock_irq(&ic->endio_wait.lock);
1870
1871 if (!write_sections)
1872 return;
1873
1874 do_journal_write(ic, write_start, write_sections, false);
1875
1876 spin_lock_irq(&ic->endio_wait.lock);
1877
1878 ic->committed_section += write_sections;
1879 wraparound_section(ic, &ic->committed_section);
1880 ic->n_committed_sections -= write_sections;
1881
1882 prev_free_sectors = ic->free_sectors;
1883 ic->free_sectors += write_sections * ic->journal_section_entries;
1884 if (unlikely(!prev_free_sectors))
1885 wake_up_locked(&ic->endio_wait);
1886
1887 spin_unlock_irq(&ic->endio_wait.lock);
1888}
1889
1890static void init_journal(struct dm_integrity_c *ic, unsigned start_section,
1891 unsigned n_sections, unsigned char commit_seq)
1892{
1893 unsigned i, j, n;
1894
1895 if (!n_sections)
1896 return;
1897
1898 for (n = 0; n < n_sections; n++) {
1899 i = start_section + n;
1900 wraparound_section(ic, &i);
1901 for (j = 0; j < ic->journal_section_sectors; j++) {
1902 struct journal_sector *js = access_journal(ic, i, j);
1903 memset(&js->entries, 0, JOURNAL_SECTOR_DATA);
1904 js->commit_id = dm_integrity_commit_id(ic, i, j, commit_seq);
1905 }
1906 for (j = 0; j < ic->journal_section_entries; j++) {
1907 struct journal_entry *je = access_journal_entry(ic, i, j);
1908 journal_entry_set_unused(je);
1909 }
1910 }
1911
1912 write_journal(ic, start_section, n_sections);
1913}
1914
1915static int find_commit_seq(struct dm_integrity_c *ic, unsigned i, unsigned j, commit_id_t id)
1916{
1917 unsigned char k;
1918 for (k = 0; k < N_COMMIT_IDS; k++) {
1919 if (dm_integrity_commit_id(ic, i, j, k) == id)
1920 return k;
1921 }
1922 dm_integrity_io_error(ic, "journal commit id", -EIO);
1923 return -EIO;
1924}
1925
1926static void replay_journal(struct dm_integrity_c *ic)
1927{
1928 unsigned i, j;
1929 bool used_commit_ids[N_COMMIT_IDS];
1930 unsigned max_commit_id_sections[N_COMMIT_IDS];
1931 unsigned write_start, write_sections;
1932 unsigned continue_section;
1933 bool journal_empty;
1934 unsigned char unused, last_used, want_commit_seq;
1935
c2bcb2b7
MP
1936 if (ic->mode == 'R')
1937 return;
1938
7eada909
MP
1939 if (ic->journal_uptodate)
1940 return;
1941
1942 last_used = 0;
1943 write_start = 0;
1944
1945 if (!ic->just_formatted) {
1946 DEBUG_print("reading journal\n");
1947 rw_journal(ic, REQ_OP_READ, 0, 0, ic->journal_sections, NULL);
1948 if (ic->journal_io)
1949 DEBUG_bytes(lowmem_page_address(ic->journal_io[0].page), 64, "read journal");
1950 if (ic->journal_io) {
1951 struct journal_completion crypt_comp;
1952 crypt_comp.ic = ic;
1953 crypt_comp.comp = COMPLETION_INITIALIZER_ONSTACK(crypt_comp.comp);
1954 crypt_comp.in_flight = (atomic_t)ATOMIC_INIT(0);
1955 encrypt_journal(ic, false, 0, ic->journal_sections, &crypt_comp);
1956 wait_for_completion(&crypt_comp.comp);
1957 }
1958 DEBUG_bytes(lowmem_page_address(ic->journal[0].page), 64, "decrypted journal");
1959 }
1960
1961 if (dm_integrity_failed(ic))
1962 goto clear_journal;
1963
1964 journal_empty = true;
1965 memset(used_commit_ids, 0, sizeof used_commit_ids);
1966 memset(max_commit_id_sections, 0, sizeof max_commit_id_sections);
1967 for (i = 0; i < ic->journal_sections; i++) {
1968 for (j = 0; j < ic->journal_section_sectors; j++) {
1969 int k;
1970 struct journal_sector *js = access_journal(ic, i, j);
1971 k = find_commit_seq(ic, i, j, js->commit_id);
1972 if (k < 0)
1973 goto clear_journal;
1974 used_commit_ids[k] = true;
1975 max_commit_id_sections[k] = i;
1976 }
1977 if (journal_empty) {
1978 for (j = 0; j < ic->journal_section_entries; j++) {
1979 struct journal_entry *je = access_journal_entry(ic, i, j);
1980 if (!journal_entry_is_unused(je)) {
1981 journal_empty = false;
1982 break;
1983 }
1984 }
1985 }
1986 }
1987
1988 if (!used_commit_ids[N_COMMIT_IDS - 1]) {
1989 unused = N_COMMIT_IDS - 1;
1990 while (unused && !used_commit_ids[unused - 1])
1991 unused--;
1992 } else {
1993 for (unused = 0; unused < N_COMMIT_IDS; unused++)
1994 if (!used_commit_ids[unused])
1995 break;
1996 if (unused == N_COMMIT_IDS) {
1997 dm_integrity_io_error(ic, "journal commit ids", -EIO);
1998 goto clear_journal;
1999 }
2000 }
2001 DEBUG_print("first unused commit seq %d [%d,%d,%d,%d]\n",
2002 unused, used_commit_ids[0], used_commit_ids[1],
2003 used_commit_ids[2], used_commit_ids[3]);
2004
2005 last_used = prev_commit_seq(unused);
2006 want_commit_seq = prev_commit_seq(last_used);
2007
2008 if (!used_commit_ids[want_commit_seq] && used_commit_ids[prev_commit_seq(want_commit_seq)])
2009 journal_empty = true;
2010
2011 write_start = max_commit_id_sections[last_used] + 1;
2012 if (unlikely(write_start >= ic->journal_sections))
2013 want_commit_seq = next_commit_seq(want_commit_seq);
2014 wraparound_section(ic, &write_start);
2015
2016 i = write_start;
2017 for (write_sections = 0; write_sections < ic->journal_sections; write_sections++) {
2018 for (j = 0; j < ic->journal_section_sectors; j++) {
2019 struct journal_sector *js = access_journal(ic, i, j);
2020
2021 if (js->commit_id != dm_integrity_commit_id(ic, i, j, want_commit_seq)) {
2022 /*
2023 * This could be caused by crash during writing.
2024 * We won't replay the inconsistent part of the
2025 * journal.
2026 */
2027 DEBUG_print("commit id mismatch at position (%u, %u): %d != %d\n",
2028 i, j, find_commit_seq(ic, i, j, js->commit_id), want_commit_seq);
2029 goto brk;
2030 }
2031 }
2032 i++;
2033 if (unlikely(i >= ic->journal_sections))
2034 want_commit_seq = next_commit_seq(want_commit_seq);
2035 wraparound_section(ic, &i);
2036 }
2037brk:
2038
2039 if (!journal_empty) {
2040 DEBUG_print("replaying %u sections, starting at %u, commit seq %d\n",
2041 write_sections, write_start, want_commit_seq);
2042 do_journal_write(ic, write_start, write_sections, true);
2043 }
2044
2045 if (write_sections == ic->journal_sections && (ic->mode == 'J' || journal_empty)) {
2046 continue_section = write_start;
2047 ic->commit_seq = want_commit_seq;
2048 DEBUG_print("continuing from section %u, commit seq %d\n", write_start, ic->commit_seq);
2049 } else {
2050 unsigned s;
2051 unsigned char erase_seq;
2052clear_journal:
2053 DEBUG_print("clearing journal\n");
2054
2055 erase_seq = prev_commit_seq(prev_commit_seq(last_used));
2056 s = write_start;
2057 init_journal(ic, s, 1, erase_seq);
2058 s++;
2059 wraparound_section(ic, &s);
2060 if (ic->journal_sections >= 2) {
2061 init_journal(ic, s, ic->journal_sections - 2, erase_seq);
2062 s += ic->journal_sections - 2;
2063 wraparound_section(ic, &s);
2064 init_journal(ic, s, 1, erase_seq);
2065 }
2066
2067 continue_section = 0;
2068 ic->commit_seq = next_commit_seq(erase_seq);
2069 }
2070
2071 ic->committed_section = continue_section;
2072 ic->n_committed_sections = 0;
2073
2074 ic->uncommitted_section = continue_section;
2075 ic->n_uncommitted_sections = 0;
2076
2077 ic->free_section = continue_section;
2078 ic->free_section_entry = 0;
2079 ic->free_sectors = ic->journal_entries;
2080
2081 ic->journal_tree_root = RB_ROOT;
2082 for (i = 0; i < ic->journal_entries; i++)
2083 init_journal_node(&ic->journal_tree[i]);
2084}
2085
2086static void dm_integrity_postsuspend(struct dm_target *ti)
2087{
2088 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
2089
2090 del_timer_sync(&ic->autocommit_timer);
2091
2092 ic->suspending = true;
2093
2094 queue_work(ic->commit_wq, &ic->commit_work);
2095 drain_workqueue(ic->commit_wq);
2096
2097 if (ic->mode == 'J') {
2098 drain_workqueue(ic->writer_wq);
2099 dm_integrity_flush_buffers(ic);
2100 }
2101
2102 ic->suspending = false;
2103
2104 BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress));
2105
2106 ic->journal_uptodate = true;
2107}
2108
2109static void dm_integrity_resume(struct dm_target *ti)
2110{
2111 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
2112
2113 replay_journal(ic);
2114}
2115
2116static void dm_integrity_status(struct dm_target *ti, status_type_t type,
2117 unsigned status_flags, char *result, unsigned maxlen)
2118{
2119 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
2120 unsigned arg_count;
2121 size_t sz = 0;
2122
2123 switch (type) {
2124 case STATUSTYPE_INFO:
2125 result[0] = '\0';
2126 break;
2127
2128 case STATUSTYPE_TABLE: {
2129 __u64 watermark_percentage = (__u64)(ic->journal_entries - ic->free_sectors_threshold) * 100;
2130 watermark_percentage += ic->journal_entries / 2;
2131 do_div(watermark_percentage, ic->journal_entries);
2132 arg_count = 5;
2133 arg_count += !!ic->internal_hash_alg.alg_string;
2134 arg_count += !!ic->journal_crypt_alg.alg_string;
2135 arg_count += !!ic->journal_mac_alg.alg_string;
2136 DMEMIT("%s %llu %u %c %u", ic->dev->name, (unsigned long long)ic->start,
2137 ic->tag_size, ic->mode, arg_count);
2138 DMEMIT(" journal-sectors:%u", ic->initial_sectors - SB_SECTORS);
2139 DMEMIT(" interleave-sectors:%u", 1U << ic->sb->log2_interleave_sectors);
2140 DMEMIT(" buffer-sectors:%u", 1U << ic->log2_buffer_sectors);
2141 DMEMIT(" journal-watermark:%u", (unsigned)watermark_percentage);
2142 DMEMIT(" commit-time:%u", ic->autocommit_msec);
2143
2144#define EMIT_ALG(a, n) \
2145 do { \
2146 if (ic->a.alg_string) { \
2147 DMEMIT(" %s:%s", n, ic->a.alg_string); \
2148 if (ic->a.key_string) \
2149 DMEMIT(":%s", ic->a.key_string);\
2150 } \
2151 } while (0)
2152 EMIT_ALG(internal_hash_alg, "internal-hash");
2153 EMIT_ALG(journal_crypt_alg, "journal-crypt");
2154 EMIT_ALG(journal_mac_alg, "journal-mac");
2155 break;
2156 }
2157 }
2158}
2159
2160static int dm_integrity_iterate_devices(struct dm_target *ti,
2161 iterate_devices_callout_fn fn, void *data)
2162{
2163 struct dm_integrity_c *ic = ti->private;
2164
2165 return fn(ti, ic->dev, ic->start + ic->initial_sectors + ic->metadata_run, ti->len, data);
2166}
2167
2168static void calculate_journal_section_size(struct dm_integrity_c *ic)
2169{
2170 unsigned sector_space = JOURNAL_SECTOR_DATA;
2171
2172 ic->journal_sections = le32_to_cpu(ic->sb->journal_sections);
2173 ic->journal_entry_size = roundup(offsetof(struct journal_entry, tag) + ic->tag_size,
2174 JOURNAL_ENTRY_ROUNDUP);
2175
2176 if (ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC))
2177 sector_space -= JOURNAL_MAC_PER_SECTOR;
2178 ic->journal_entries_per_sector = sector_space / ic->journal_entry_size;
2179 ic->journal_section_entries = ic->journal_entries_per_sector * JOURNAL_BLOCK_SECTORS;
2180 ic->journal_section_sectors = ic->journal_section_entries + JOURNAL_BLOCK_SECTORS;
2181 ic->journal_entries = ic->journal_section_entries * ic->journal_sections;
2182}
2183
2184static int calculate_device_limits(struct dm_integrity_c *ic)
2185{
2186 __u64 initial_sectors;
2187 sector_t last_sector, last_area, last_offset;
2188
2189 calculate_journal_section_size(ic);
2190 initial_sectors = SB_SECTORS + (__u64)ic->journal_section_sectors * ic->journal_sections;
2191 if (initial_sectors + METADATA_PADDING_SECTORS >= ic->device_sectors || initial_sectors > UINT_MAX)
2192 return -EINVAL;
2193 ic->initial_sectors = initial_sectors;
2194
2195 ic->metadata_run = roundup((__u64)ic->tag_size << ic->sb->log2_interleave_sectors,
2196 (__u64)(1 << SECTOR_SHIFT << METADATA_PADDING_SECTORS)) >> SECTOR_SHIFT;
2197 if (!(ic->metadata_run & (ic->metadata_run - 1)))
2198 ic->log2_metadata_run = __ffs(ic->metadata_run);
2199 else
2200 ic->log2_metadata_run = -1;
2201
2202 get_area_and_offset(ic, ic->provided_data_sectors - 1, &last_area, &last_offset);
2203 last_sector = get_data_sector(ic, last_area, last_offset);
2204
2205 if (ic->start + last_sector < last_sector || ic->start + last_sector >= ic->device_sectors)
2206 return -EINVAL;
2207
2208 return 0;
2209}
2210
2211static int initialize_superblock(struct dm_integrity_c *ic, unsigned journal_sectors, unsigned interleave_sectors)
2212{
2213 unsigned journal_sections;
2214 int test_bit;
2215
2216 memcpy(ic->sb->magic, SB_MAGIC, 8);
2217 ic->sb->version = SB_VERSION;
2218 ic->sb->integrity_tag_size = cpu_to_le16(ic->tag_size);
2219 if (ic->journal_mac_alg.alg_string)
2220 ic->sb->flags |= cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC);
2221
2222 calculate_journal_section_size(ic);
2223 journal_sections = journal_sectors / ic->journal_section_sectors;
2224 if (!journal_sections)
2225 journal_sections = 1;
2226 ic->sb->journal_sections = cpu_to_le32(journal_sections);
2227
2228 ic->sb->log2_interleave_sectors = __fls(interleave_sectors);
2229 ic->sb->log2_interleave_sectors = max((__u8)MIN_INTERLEAVE_SECTORS, ic->sb->log2_interleave_sectors);
2230 ic->sb->log2_interleave_sectors = min((__u8)MAX_INTERLEAVE_SECTORS, ic->sb->log2_interleave_sectors);
2231
2232 ic->provided_data_sectors = 0;
2233 for (test_bit = fls64(ic->device_sectors) - 1; test_bit >= 3; test_bit--) {
2234 __u64 prev_data_sectors = ic->provided_data_sectors;
2235
2236 ic->provided_data_sectors |= (sector_t)1 << test_bit;
2237 if (calculate_device_limits(ic))
2238 ic->provided_data_sectors = prev_data_sectors;
2239 }
2240
2241 if (!le64_to_cpu(ic->provided_data_sectors))
2242 return -EINVAL;
2243
2244 ic->sb->provided_data_sectors = cpu_to_le64(ic->provided_data_sectors);
2245
2246 return 0;
2247}
2248
2249static void dm_integrity_set(struct dm_target *ti, struct dm_integrity_c *ic)
2250{
2251 struct gendisk *disk = dm_disk(dm_table_get_md(ti->table));
2252 struct blk_integrity bi;
2253
2254 memset(&bi, 0, sizeof(bi));
2255 bi.profile = &dm_integrity_profile;
2256 bi.tuple_size = ic->tag_size * (queue_logical_block_size(disk->queue) >> SECTOR_SHIFT);
2257 bi.tag_size = ic->tag_size;
2258
2259 blk_integrity_register(disk, &bi);
2260 blk_queue_max_integrity_segments(disk->queue, UINT_MAX);
2261}
2262
2263/* FIXME: use new kvmalloc */
2264static void *dm_integrity_kvmalloc(size_t size, gfp_t gfp)
2265{
2266 void *ptr = NULL;
2267
2268 if (size <= PAGE_SIZE)
2269 ptr = kmalloc(size, GFP_KERNEL | gfp);
2270 if (!ptr && size <= KMALLOC_MAX_SIZE)
2271 ptr = kmalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY | gfp);
2272 if (!ptr)
2273 ptr = __vmalloc(size, GFP_KERNEL | gfp, PAGE_KERNEL);
2274
2275 return ptr;
2276}
2277
2278static void dm_integrity_free_page_list(struct dm_integrity_c *ic, struct page_list *pl)
2279{
2280 unsigned i;
2281
2282 if (!pl)
2283 return;
2284 for (i = 0; i < ic->journal_pages; i++)
2285 if (pl[i].page)
2286 __free_page(pl[i].page);
2287 kvfree(pl);
2288}
2289
2290static struct page_list *dm_integrity_alloc_page_list(struct dm_integrity_c *ic)
2291{
2292 size_t page_list_desc_size = ic->journal_pages * sizeof(struct page_list);
2293 struct page_list *pl;
2294 unsigned i;
2295
2296 pl = dm_integrity_kvmalloc(page_list_desc_size, __GFP_ZERO);
2297 if (!pl)
2298 return NULL;
2299
2300 for (i = 0; i < ic->journal_pages; i++) {
2301 pl[i].page = alloc_page(GFP_KERNEL);
2302 if (!pl[i].page) {
2303 dm_integrity_free_page_list(ic, pl);
2304 return NULL;
2305 }
2306 if (i)
2307 pl[i - 1].next = &pl[i];
2308 }
2309
2310 return pl;
2311}
2312
2313static void dm_integrity_free_journal_scatterlist(struct dm_integrity_c *ic, struct scatterlist **sl)
2314{
2315 unsigned i;
2316 for (i = 0; i < ic->journal_sections; i++)
2317 kvfree(sl[i]);
2318 kfree(sl);
2319}
2320
2321static struct scatterlist **dm_integrity_alloc_journal_scatterlist(struct dm_integrity_c *ic, struct page_list *pl)
2322{
2323 struct scatterlist **sl;
2324 unsigned i;
2325
2326 sl = dm_integrity_kvmalloc(ic->journal_sections * sizeof(struct scatterlist *), __GFP_ZERO);
2327 if (!sl)
2328 return NULL;
2329
2330 for (i = 0; i < ic->journal_sections; i++) {
2331 struct scatterlist *s;
2332 unsigned start_index, start_offset;
2333 unsigned end_index, end_offset;
2334 unsigned n_pages;
2335 unsigned idx;
2336
2337 page_list_location(ic, i, 0, &start_index, &start_offset);
2338 page_list_location(ic, i, ic->journal_section_sectors - 1, &end_index, &end_offset);
2339
2340 n_pages = (end_index - start_index + 1);
2341
2342 s = dm_integrity_kvmalloc(n_pages * sizeof(struct scatterlist), 0);
2343 if (!s) {
2344 dm_integrity_free_journal_scatterlist(ic, sl);
2345 return NULL;
2346 }
2347
2348 sg_init_table(s, n_pages);
2349 for (idx = start_index; idx <= end_index; idx++) {
2350 char *va = lowmem_page_address(pl[idx].page);
2351 unsigned start = 0, end = PAGE_SIZE;
2352 if (idx == start_index)
2353 start = start_offset;
2354 if (idx == end_index)
2355 end = end_offset + (1 << SECTOR_SHIFT);
2356 sg_set_buf(&s[idx - start_index], va + start, end - start);
2357 }
2358
2359 sl[i] = s;
2360 }
2361
2362 return sl;
2363}
2364
2365static void free_alg(struct alg_spec *a)
2366{
2367 kzfree(a->alg_string);
2368 kzfree(a->key);
2369 memset(a, 0, sizeof *a);
2370}
2371
2372static int get_alg_and_key(const char *arg, struct alg_spec *a, char **error, char *error_inval)
2373{
2374 char *k;
2375
2376 free_alg(a);
2377
2378 a->alg_string = kstrdup(strchr(arg, ':') + 1, GFP_KERNEL);
2379 if (!a->alg_string)
2380 goto nomem;
2381
2382 k = strchr(a->alg_string, ':');
2383 if (k) {
2384 unsigned i;
2385
2386 *k = 0;
2387 a->key_string = k + 1;
2388 if (strlen(a->key_string) & 1)
2389 goto inval;
2390
2391 a->key_size = strlen(a->key_string) / 2;
2392 a->key = kmalloc(a->key_size, GFP_KERNEL);
2393 if (!a->key)
2394 goto nomem;
2395 for (i = 0; i < a->key_size; i++) {
2396 char digit[3];
2397 digit[0] = a->key_string[i * 2];
2398 digit[1] = a->key_string[i * 2 + 1];
2399 digit[2] = 0;
2400 if (strspn(digit, "0123456789abcdefABCDEF") != 2)
2401 goto inval;
2402 if (kstrtou8(digit, 16, &a->key[i]))
2403 goto inval;
2404 }
2405 }
2406
2407 return 0;
2408inval:
2409 *error = error_inval;
2410 return -EINVAL;
2411nomem:
2412 *error = "Out of memory for an argument";
2413 return -ENOMEM;
2414}
2415
2416static int get_mac(struct crypto_shash **hash, struct alg_spec *a, char **error,
2417 char *error_alg, char *error_key)
2418{
2419 int r;
2420
2421 if (a->alg_string) {
2422 *hash = crypto_alloc_shash(a->alg_string, 0, CRYPTO_ALG_ASYNC);
2423 if (IS_ERR(*hash)) {
2424 *error = error_alg;
2425 r = PTR_ERR(*hash);
2426 *hash = NULL;
2427 return r;
2428 }
2429
2430 if (a->key) {
2431 r = crypto_shash_setkey(*hash, a->key, a->key_size);
2432 if (r) {
2433 *error = error_key;
2434 return r;
2435 }
2436 }
2437 }
2438
2439 return 0;
2440}
2441
1aa0efd4
MS
2442static int create_journal(struct dm_integrity_c *ic, char **error)
2443{
2444 int r = 0;
2445 unsigned i;
2446 __u64 journal_pages, journal_desc_size, journal_tree_size;
2447
2448 journal_pages = roundup((__u64)ic->journal_sections * ic->journal_section_sectors,
2449 PAGE_SIZE >> SECTOR_SHIFT) >> (PAGE_SHIFT - SECTOR_SHIFT);
2450 journal_desc_size = journal_pages * sizeof(struct page_list);
2451 if (journal_pages >= totalram_pages - totalhigh_pages || journal_desc_size > ULONG_MAX) {
2452 *error = "Journal doesn't fit into memory";
2453 r = -ENOMEM;
2454 goto bad;
2455 }
2456 ic->journal_pages = journal_pages;
2457
2458 ic->journal = dm_integrity_alloc_page_list(ic);
2459 if (!ic->journal) {
2460 *error = "Could not allocate memory for journal";
2461 r = -ENOMEM;
2462 goto bad;
2463 }
2464 if (ic->journal_crypt_alg.alg_string) {
2465 unsigned ivsize, blocksize;
2466 struct journal_completion comp;
2467
2468 comp.ic = ic;
2469 ic->journal_crypt = crypto_alloc_skcipher(ic->journal_crypt_alg.alg_string, 0, 0);
2470 if (IS_ERR(ic->journal_crypt)) {
2471 *error = "Invalid journal cipher";
2472 r = PTR_ERR(ic->journal_crypt);
2473 ic->journal_crypt = NULL;
2474 goto bad;
2475 }
2476 ivsize = crypto_skcipher_ivsize(ic->journal_crypt);
2477 blocksize = crypto_skcipher_blocksize(ic->journal_crypt);
2478
2479 if (ic->journal_crypt_alg.key) {
2480 r = crypto_skcipher_setkey(ic->journal_crypt, ic->journal_crypt_alg.key,
2481 ic->journal_crypt_alg.key_size);
2482 if (r) {
2483 *error = "Error setting encryption key";
2484 goto bad;
2485 }
2486 }
2487 DEBUG_print("cipher %s, block size %u iv size %u\n",
2488 ic->journal_crypt_alg.alg_string, blocksize, ivsize);
2489
2490 ic->journal_io = dm_integrity_alloc_page_list(ic);
2491 if (!ic->journal_io) {
2492 *error = "Could not allocate memory for journal io";
2493 r = -ENOMEM;
2494 goto bad;
2495 }
2496
2497 if (blocksize == 1) {
2498 struct scatterlist *sg;
2499 SKCIPHER_REQUEST_ON_STACK(req, ic->journal_crypt);
2500 unsigned char iv[ivsize];
2501 skcipher_request_set_tfm(req, ic->journal_crypt);
2502
2503 ic->journal_xor = dm_integrity_alloc_page_list(ic);
2504 if (!ic->journal_xor) {
2505 *error = "Could not allocate memory for journal xor";
2506 r = -ENOMEM;
2507 goto bad;
2508 }
2509
2510 sg = dm_integrity_kvmalloc((ic->journal_pages + 1) * sizeof(struct scatterlist), 0);
2511 if (!sg) {
2512 *error = "Unable to allocate sg list";
2513 r = -ENOMEM;
2514 goto bad;
2515 }
2516 sg_init_table(sg, ic->journal_pages + 1);
2517 for (i = 0; i < ic->journal_pages; i++) {
2518 char *va = lowmem_page_address(ic->journal_xor[i].page);
2519 clear_page(va);
2520 sg_set_buf(&sg[i], va, PAGE_SIZE);
2521 }
2522 sg_set_buf(&sg[i], &ic->commit_ids, sizeof ic->commit_ids);
2523 memset(iv, 0x00, ivsize);
2524
2525 skcipher_request_set_crypt(req, sg, sg, PAGE_SIZE * ic->journal_pages + sizeof ic->commit_ids, iv);
2526 comp.comp = COMPLETION_INITIALIZER_ONSTACK(comp.comp);
2527 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
2528 if (do_crypt(true, req, &comp))
2529 wait_for_completion(&comp.comp);
2530 kvfree(sg);
2531 r = dm_integrity_failed(ic);
2532 if (r) {
2533 *error = "Unable to encrypt journal";
2534 goto bad;
2535 }
2536 DEBUG_bytes(lowmem_page_address(ic->journal_xor[0].page), 64, "xor data");
2537
2538 crypto_free_skcipher(ic->journal_crypt);
2539 ic->journal_crypt = NULL;
2540 } else {
2541 SKCIPHER_REQUEST_ON_STACK(req, ic->journal_crypt);
2542 unsigned char iv[ivsize];
2543 unsigned crypt_len = roundup(ivsize, blocksize);
2544 unsigned char crypt_data[crypt_len];
2545
2546 skcipher_request_set_tfm(req, ic->journal_crypt);
2547
2548 ic->journal_scatterlist = dm_integrity_alloc_journal_scatterlist(ic, ic->journal);
2549 if (!ic->journal_scatterlist) {
2550 *error = "Unable to allocate sg list";
2551 r = -ENOMEM;
2552 goto bad;
2553 }
2554 ic->journal_io_scatterlist = dm_integrity_alloc_journal_scatterlist(ic, ic->journal_io);
2555 if (!ic->journal_io_scatterlist) {
2556 *error = "Unable to allocate sg list";
2557 r = -ENOMEM;
2558 goto bad;
2559 }
2560 ic->sk_requests = dm_integrity_kvmalloc(ic->journal_sections * sizeof(struct skcipher_request *), __GFP_ZERO);
2561 if (!ic->sk_requests) {
2562 *error = "Unable to allocate sk requests";
2563 r = -ENOMEM;
2564 goto bad;
2565 }
2566 for (i = 0; i < ic->journal_sections; i++) {
2567 struct scatterlist sg;
2568 struct skcipher_request *section_req;
2569 __u32 section_le = cpu_to_le32(i);
2570
2571 memset(iv, 0x00, ivsize);
2572 memset(crypt_data, 0x00, crypt_len);
2573 memcpy(crypt_data, &section_le, min((size_t)crypt_len, sizeof(section_le)));
2574
2575 sg_init_one(&sg, crypt_data, crypt_len);
2576 skcipher_request_set_crypt(req, &sg, &sg, crypt_len, iv);
2577 comp.comp = COMPLETION_INITIALIZER_ONSTACK(comp.comp);
2578 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
2579 if (do_crypt(true, req, &comp))
2580 wait_for_completion(&comp.comp);
2581
2582 r = dm_integrity_failed(ic);
2583 if (r) {
2584 *error = "Unable to generate iv";
2585 goto bad;
2586 }
2587
2588 section_req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
2589 if (!section_req) {
2590 *error = "Unable to allocate crypt request";
2591 r = -ENOMEM;
2592 goto bad;
2593 }
2594 section_req->iv = kmalloc(ivsize * 2, GFP_KERNEL);
2595 if (!section_req->iv) {
2596 skcipher_request_free(section_req);
2597 *error = "Unable to allocate iv";
2598 r = -ENOMEM;
2599 goto bad;
2600 }
2601 memcpy(section_req->iv + ivsize, crypt_data, ivsize);
2602 section_req->cryptlen = (size_t)ic->journal_section_sectors << SECTOR_SHIFT;
2603 ic->sk_requests[i] = section_req;
2604 DEBUG_bytes(crypt_data, ivsize, "iv(%u)", i);
2605 }
2606 }
2607 }
2608
2609 for (i = 0; i < N_COMMIT_IDS; i++) {
2610 unsigned j;
2611retest_commit_id:
2612 for (j = 0; j < i; j++) {
2613 if (ic->commit_ids[j] == ic->commit_ids[i]) {
2614 ic->commit_ids[i] = cpu_to_le64(le64_to_cpu(ic->commit_ids[i]) + 1);
2615 goto retest_commit_id;
2616 }
2617 }
2618 DEBUG_print("commit id %u: %016llx\n", i, ic->commit_ids[i]);
2619 }
2620
2621 journal_tree_size = (__u64)ic->journal_entries * sizeof(struct journal_node);
2622 if (journal_tree_size > ULONG_MAX) {
2623 *error = "Journal doesn't fit into memory";
2624 r = -ENOMEM;
2625 goto bad;
2626 }
2627 ic->journal_tree = dm_integrity_kvmalloc(journal_tree_size, 0);
2628 if (!ic->journal_tree) {
2629 *error = "Could not allocate memory for journal tree";
2630 r = -ENOMEM;
2631 }
2632bad:
2633 return r;
2634}
2635
7eada909
MP
2636/*
2637 * Construct a integrity mapping: <dev_path> <offset> <tag_size>
2638 *
2639 * Arguments:
2640 * device
2641 * offset from the start of the device
2642 * tag size
2643 * D - direct writes, J - journal writes
2644 * number of optional arguments
2645 * optional arguments:
2646 * journal-sectors
2647 * interleave-sectors
2648 * buffer-sectors
2649 * journal-watermark
2650 * commit-time
2651 * internal-hash
2652 * journal-crypt
2653 * journal-mac
2654 */
2655static int dm_integrity_ctr(struct dm_target *ti, unsigned argc, char **argv)
2656{
2657 struct dm_integrity_c *ic;
2658 char dummy;
2659 int r;
2660 unsigned i;
2661 unsigned extra_args;
2662 struct dm_arg_set as;
2663 static struct dm_arg _args[] = {
2664 {0, 7, "Invalid number of feature args"},
2665 };
2666 unsigned journal_sectors, interleave_sectors, buffer_sectors, journal_watermark, sync_msec;
2667 bool should_write_sb;
7eada909
MP
2668 __u64 threshold;
2669 unsigned long long start;
2670
2671#define DIRECT_ARGUMENTS 4
2672
2673 if (argc <= DIRECT_ARGUMENTS) {
2674 ti->error = "Invalid argument count";
2675 return -EINVAL;
2676 }
2677
2678 ic = kzalloc(sizeof(struct dm_integrity_c), GFP_KERNEL);
2679 if (!ic) {
2680 ti->error = "Cannot allocate integrity context";
2681 return -ENOMEM;
2682 }
2683 ti->private = ic;
2684 ti->per_io_data_size = sizeof(struct dm_integrity_io);
2685
2686 ic->commit_ids[0] = cpu_to_le64(0x1111111111111111ULL);
2687 ic->commit_ids[1] = cpu_to_le64(0x2222222222222222ULL);
2688 ic->commit_ids[2] = cpu_to_le64(0x3333333333333333ULL);
2689 ic->commit_ids[3] = cpu_to_le64(0x4444444444444444ULL);
2690
2691 ic->in_progress = RB_ROOT;
2692 init_waitqueue_head(&ic->endio_wait);
2693 bio_list_init(&ic->flush_bio_list);
2694 init_waitqueue_head(&ic->copy_to_journal_wait);
2695 init_completion(&ic->crypto_backoff);
2696
2697 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &ic->dev);
2698 if (r) {
2699 ti->error = "Device lookup failed";
2700 goto bad;
2701 }
2702
2703 if (sscanf(argv[1], "%llu%c", &start, &dummy) != 1 || start != (sector_t)start) {
2704 ti->error = "Invalid starting offset";
2705 r = -EINVAL;
2706 goto bad;
2707 }
2708 ic->start = start;
2709
2710 if (strcmp(argv[2], "-")) {
2711 if (sscanf(argv[2], "%u%c", &ic->tag_size, &dummy) != 1 || !ic->tag_size) {
2712 ti->error = "Invalid tag size";
2713 r = -EINVAL;
2714 goto bad;
2715 }
2716 }
2717
c2bcb2b7 2718 if (!strcmp(argv[3], "J") || !strcmp(argv[3], "D") || !strcmp(argv[3], "R"))
7eada909
MP
2719 ic->mode = argv[3][0];
2720 else {
2721 ti->error = "Invalid mode (expecting J or D)";
2722 r = -EINVAL;
2723 goto bad;
2724 }
2725
2726 ic->device_sectors = i_size_read(ic->dev->bdev->bd_inode) >> SECTOR_SHIFT;
2727 journal_sectors = min((sector_t)DEFAULT_MAX_JOURNAL_SECTORS,
2728 ic->device_sectors >> DEFAULT_JOURNAL_SIZE_FACTOR);
2729 interleave_sectors = DEFAULT_INTERLEAVE_SECTORS;
2730 buffer_sectors = DEFAULT_BUFFER_SECTORS;
2731 journal_watermark = DEFAULT_JOURNAL_WATERMARK;
2732 sync_msec = DEFAULT_SYNC_MSEC;
2733
2734 as.argc = argc - DIRECT_ARGUMENTS;
2735 as.argv = argv + DIRECT_ARGUMENTS;
2736 r = dm_read_arg_group(_args, &as, &extra_args, &ti->error);
2737 if (r)
2738 goto bad;
2739
2740 while (extra_args--) {
2741 const char *opt_string;
2742 unsigned val;
2743 opt_string = dm_shift_arg(&as);
2744 if (!opt_string) {
2745 r = -EINVAL;
2746 ti->error = "Not enough feature arguments";
2747 goto bad;
2748 }
2749 if (sscanf(opt_string, "journal-sectors:%u%c", &val, &dummy) == 1)
2750 journal_sectors = val;
2751 else if (sscanf(opt_string, "interleave-sectors:%u%c", &val, &dummy) == 1)
2752 interleave_sectors = val;
2753 else if (sscanf(opt_string, "buffer-sectors:%u%c", &val, &dummy) == 1)
2754 buffer_sectors = val;
2755 else if (sscanf(opt_string, "journal-watermark:%u%c", &val, &dummy) == 1 && val <= 100)
2756 journal_watermark = val;
2757 else if (sscanf(opt_string, "commit-time:%u%c", &val, &dummy) == 1)
2758 sync_msec = val;
2759 else if (!memcmp(opt_string, "internal-hash:", strlen("internal-hash:"))) {
2760 r = get_alg_and_key(opt_string, &ic->internal_hash_alg, &ti->error,
2761 "Invalid internal-hash argument");
2762 if (r)
2763 goto bad;
2764 } else if (!memcmp(opt_string, "journal-crypt:", strlen("journal-crypt:"))) {
2765 r = get_alg_and_key(opt_string, &ic->journal_crypt_alg, &ti->error,
2766 "Invalid journal-crypt argument");
2767 if (r)
2768 goto bad;
2769 } else if (!memcmp(opt_string, "journal-mac:", strlen("journal-mac:"))) {
2770 r = get_alg_and_key(opt_string, &ic->journal_mac_alg, &ti->error,
2771 "Invalid journal-mac argument");
2772 if (r)
2773 goto bad;
2774 } else {
2775 r = -EINVAL;
2776 ti->error = "Invalid argument";
2777 goto bad;
2778 }
2779 }
2780
2781 r = get_mac(&ic->internal_hash, &ic->internal_hash_alg, &ti->error,
2782 "Invalid internal hash", "Error setting internal hash key");
2783 if (r)
2784 goto bad;
2785
2786 r = get_mac(&ic->journal_mac, &ic->journal_mac_alg, &ti->error,
2787 "Invalid journal mac", "Error setting journal mac key");
2788 if (r)
2789 goto bad;
2790
2791 if (!ic->tag_size) {
2792 if (!ic->internal_hash) {
2793 ti->error = "Unknown tag size";
2794 r = -EINVAL;
2795 goto bad;
2796 }
2797 ic->tag_size = crypto_shash_digestsize(ic->internal_hash);
2798 }
2799 if (ic->tag_size > MAX_TAG_SIZE) {
2800 ti->error = "Too big tag size";
2801 r = -EINVAL;
2802 goto bad;
2803 }
2804 if (!(ic->tag_size & (ic->tag_size - 1)))
2805 ic->log2_tag_size = __ffs(ic->tag_size);
2806 else
2807 ic->log2_tag_size = -1;
2808
2809 ic->autocommit_jiffies = msecs_to_jiffies(sync_msec);
2810 ic->autocommit_msec = sync_msec;
2811 setup_timer(&ic->autocommit_timer, autocommit_fn, (unsigned long)ic);
2812
2813 ic->io = dm_io_client_create();
2814 if (IS_ERR(ic->io)) {
2815 r = PTR_ERR(ic->io);
2816 ic->io = NULL;
2817 ti->error = "Cannot allocate dm io";
2818 goto bad;
2819 }
2820
2821 ic->journal_io_mempool = mempool_create_slab_pool(JOURNAL_IO_MEMPOOL, journal_io_cache);
2822 if (!ic->journal_io_mempool) {
2823 r = -ENOMEM;
2824 ti->error = "Cannot allocate mempool";
2825 goto bad;
2826 }
2827
2828 ic->metadata_wq = alloc_workqueue("dm-integrity-metadata",
2829 WQ_MEM_RECLAIM, METADATA_WORKQUEUE_MAX_ACTIVE);
2830 if (!ic->metadata_wq) {
2831 ti->error = "Cannot allocate workqueue";
2832 r = -ENOMEM;
2833 goto bad;
2834 }
2835
2836 /*
2837 * If this workqueue were percpu, it would cause bio reordering
2838 * and reduced performance.
2839 */
2840 ic->wait_wq = alloc_workqueue("dm-integrity-wait", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
2841 if (!ic->wait_wq) {
2842 ti->error = "Cannot allocate workqueue";
2843 r = -ENOMEM;
2844 goto bad;
2845 }
2846
2847 ic->commit_wq = alloc_workqueue("dm-integrity-commit", WQ_MEM_RECLAIM, 1);
2848 if (!ic->commit_wq) {
2849 ti->error = "Cannot allocate workqueue";
2850 r = -ENOMEM;
2851 goto bad;
2852 }
2853 INIT_WORK(&ic->commit_work, integrity_commit);
2854
2855 if (ic->mode == 'J') {
2856 ic->writer_wq = alloc_workqueue("dm-integrity-writer", WQ_MEM_RECLAIM, 1);
2857 if (!ic->writer_wq) {
2858 ti->error = "Cannot allocate workqueue";
2859 r = -ENOMEM;
2860 goto bad;
2861 }
2862 INIT_WORK(&ic->writer_work, integrity_writer);
2863 }
2864
2865 ic->sb = alloc_pages_exact(SB_SECTORS << SECTOR_SHIFT, GFP_KERNEL);
2866 if (!ic->sb) {
2867 r = -ENOMEM;
2868 ti->error = "Cannot allocate superblock area";
2869 goto bad;
2870 }
2871
2872 r = sync_rw_sb(ic, REQ_OP_READ, 0);
2873 if (r) {
2874 ti->error = "Error reading superblock";
2875 goto bad;
2876 }
c2bcb2b7
MP
2877 should_write_sb = false;
2878 if (memcmp(ic->sb->magic, SB_MAGIC, 8)) {
2879 if (ic->mode != 'R') {
2880 for (i = 0; i < 512; i += 8) {
2881 if (*(__u64 *)((__u8 *)ic->sb + i)) {
2882 r = -EINVAL;
2883 ti->error = "The device is not initialized";
2884 goto bad;
2885 }
7eada909
MP
2886 }
2887 }
2888
2889 r = initialize_superblock(ic, journal_sectors, interleave_sectors);
2890 if (r) {
2891 ti->error = "Could not initialize superblock";
2892 goto bad;
2893 }
c2bcb2b7
MP
2894 if (ic->mode != 'R')
2895 should_write_sb = true;
7eada909
MP
2896 }
2897
2898 if (ic->sb->version != SB_VERSION) {
2899 r = -EINVAL;
2900 ti->error = "Unknown version";
2901 goto bad;
2902 }
2903 if (le16_to_cpu(ic->sb->integrity_tag_size) != ic->tag_size) {
2904 r = -EINVAL;
2905 ti->error = "Invalid tag size";
2906 goto bad;
2907 }
2908 /* make sure that ti->max_io_len doesn't overflow */
2909 if (ic->sb->log2_interleave_sectors < MIN_INTERLEAVE_SECTORS ||
2910 ic->sb->log2_interleave_sectors > MAX_INTERLEAVE_SECTORS) {
2911 r = -EINVAL;
2912 ti->error = "Invalid interleave_sectors in the superblock";
2913 goto bad;
2914 }
2915 ic->provided_data_sectors = le64_to_cpu(ic->sb->provided_data_sectors);
2916 if (ic->provided_data_sectors != le64_to_cpu(ic->sb->provided_data_sectors)) {
2917 /* test for overflow */
2918 r = -EINVAL;
2919 ti->error = "The superblock has 64-bit device size, but the kernel was compiled with 32-bit sectors";
2920 goto bad;
2921 }
2922 if (!!(ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC)) != !!ic->journal_mac_alg.alg_string) {
2923 r = -EINVAL;
2924 ti->error = "Journal mac mismatch";
2925 goto bad;
2926 }
2927 r = calculate_device_limits(ic);
2928 if (r) {
2929 ti->error = "The device is too small";
2930 goto bad;
2931 }
2932
2933 if (!buffer_sectors)
2934 buffer_sectors = 1;
2935 ic->log2_buffer_sectors = min3((int)__fls(buffer_sectors), (int)__ffs(ic->metadata_run), 31 - SECTOR_SHIFT);
2936
2937 threshold = (__u64)ic->journal_entries * (100 - journal_watermark);
2938 threshold += 50;
2939 do_div(threshold, 100);
2940 ic->free_sectors_threshold = threshold;
2941
2942 DEBUG_print("initialized:\n");
2943 DEBUG_print(" integrity_tag_size %u\n", le16_to_cpu(ic->sb->integrity_tag_size));
2944 DEBUG_print(" journal_entry_size %u\n", ic->journal_entry_size);
2945 DEBUG_print(" journal_entries_per_sector %u\n", ic->journal_entries_per_sector);
2946 DEBUG_print(" journal_section_entries %u\n", ic->journal_section_entries);
2947 DEBUG_print(" journal_section_sectors %u\n", ic->journal_section_sectors);
2948 DEBUG_print(" journal_sections %u\n", (unsigned)le32_to_cpu(ic->sb->journal_sections));
2949 DEBUG_print(" journal_entries %u\n", ic->journal_entries);
2950 DEBUG_print(" log2_interleave_sectors %d\n", ic->sb->log2_interleave_sectors);
2951 DEBUG_print(" device_sectors 0x%llx\n", (unsigned long long)ic->device_sectors);
2952 DEBUG_print(" initial_sectors 0x%x\n", ic->initial_sectors);
2953 DEBUG_print(" metadata_run 0x%x\n", ic->metadata_run);
2954 DEBUG_print(" log2_metadata_run %d\n", ic->log2_metadata_run);
2955 DEBUG_print(" provided_data_sectors 0x%llx (%llu)\n", (unsigned long long)ic->provided_data_sectors,
2956 (unsigned long long)ic->provided_data_sectors);
2957 DEBUG_print(" log2_buffer_sectors %u\n", ic->log2_buffer_sectors);
2958
2959 ic->bufio = dm_bufio_client_create(ic->dev->bdev, 1U << (SECTOR_SHIFT + ic->log2_buffer_sectors),
2960 1, 0, NULL, NULL);
2961 if (IS_ERR(ic->bufio)) {
2962 r = PTR_ERR(ic->bufio);
2963 ti->error = "Cannot initialize dm-bufio";
2964 ic->bufio = NULL;
2965 goto bad;
2966 }
2967 dm_bufio_set_sector_offset(ic->bufio, ic->start + ic->initial_sectors);
2968
c2bcb2b7
MP
2969 if (ic->mode != 'R') {
2970 r = create_journal(ic, &ti->error);
2971 if (r)
2972 goto bad;
2973 }
7eada909
MP
2974
2975 if (should_write_sb) {
2976 int r;
2977
2978 init_journal(ic, 0, ic->journal_sections, 0);
2979 r = dm_integrity_failed(ic);
2980 if (unlikely(r)) {
2981 ti->error = "Error initializing journal";
2982 goto bad;
2983 }
2984 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
2985 if (r) {
2986 ti->error = "Error initializing superblock";
2987 goto bad;
2988 }
2989 ic->just_formatted = true;
2990 }
2991
2992 r = dm_set_target_max_io_len(ti, 1U << ic->sb->log2_interleave_sectors);
2993 if (r)
2994 goto bad;
2995
2996 if (!ic->internal_hash)
2997 dm_integrity_set(ti, ic);
2998
2999 ti->num_flush_bios = 1;
3000 ti->flush_supported = true;
3001
3002 return 0;
3003bad:
3004 dm_integrity_dtr(ti);
3005 return r;
3006}
3007
3008static void dm_integrity_dtr(struct dm_target *ti)
3009{
3010 struct dm_integrity_c *ic = ti->private;
3011
3012 BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress));
3013
3014 if (ic->metadata_wq)
3015 destroy_workqueue(ic->metadata_wq);
3016 if (ic->wait_wq)
3017 destroy_workqueue(ic->wait_wq);
3018 if (ic->commit_wq)
3019 destroy_workqueue(ic->commit_wq);
3020 if (ic->writer_wq)
3021 destroy_workqueue(ic->writer_wq);
3022 if (ic->bufio)
3023 dm_bufio_client_destroy(ic->bufio);
3024 mempool_destroy(ic->journal_io_mempool);
3025 if (ic->io)
3026 dm_io_client_destroy(ic->io);
3027 if (ic->dev)
3028 dm_put_device(ti, ic->dev);
3029 dm_integrity_free_page_list(ic, ic->journal);
3030 dm_integrity_free_page_list(ic, ic->journal_io);
3031 dm_integrity_free_page_list(ic, ic->journal_xor);
3032 if (ic->journal_scatterlist)
3033 dm_integrity_free_journal_scatterlist(ic, ic->journal_scatterlist);
3034 if (ic->journal_io_scatterlist)
3035 dm_integrity_free_journal_scatterlist(ic, ic->journal_io_scatterlist);
3036 if (ic->sk_requests) {
3037 unsigned i;
3038
3039 for (i = 0; i < ic->journal_sections; i++) {
3040 struct skcipher_request *req = ic->sk_requests[i];
3041 if (req) {
3042 kzfree(req->iv);
3043 skcipher_request_free(req);
3044 }
3045 }
3046 kvfree(ic->sk_requests);
3047 }
3048 kvfree(ic->journal_tree);
3049 if (ic->sb)
3050 free_pages_exact(ic->sb, SB_SECTORS << SECTOR_SHIFT);
3051
3052 if (ic->internal_hash)
3053 crypto_free_shash(ic->internal_hash);
3054 free_alg(&ic->internal_hash_alg);
3055
3056 if (ic->journal_crypt)
3057 crypto_free_skcipher(ic->journal_crypt);
3058 free_alg(&ic->journal_crypt_alg);
3059
3060 if (ic->journal_mac)
3061 crypto_free_shash(ic->journal_mac);
3062 free_alg(&ic->journal_mac_alg);
3063
3064 kfree(ic);
3065}
3066
3067static struct target_type integrity_target = {
3068 .name = "integrity",
3069 .version = {1, 0, 0},
3070 .module = THIS_MODULE,
3071 .features = DM_TARGET_SINGLETON | DM_TARGET_INTEGRITY,
3072 .ctr = dm_integrity_ctr,
3073 .dtr = dm_integrity_dtr,
3074 .map = dm_integrity_map,
3075 .postsuspend = dm_integrity_postsuspend,
3076 .resume = dm_integrity_resume,
3077 .status = dm_integrity_status,
3078 .iterate_devices = dm_integrity_iterate_devices,
3079};
3080
3081int __init dm_integrity_init(void)
3082{
3083 int r;
3084
3085 journal_io_cache = kmem_cache_create("integrity_journal_io",
3086 sizeof(struct journal_io), 0, 0, NULL);
3087 if (!journal_io_cache) {
3088 DMERR("can't allocate journal io cache");
3089 return -ENOMEM;
3090 }
3091
3092 r = dm_register_target(&integrity_target);
3093
3094 if (r < 0)
3095 DMERR("register failed %d", r);
3096
3097 return r;
3098}
3099
3100void dm_integrity_exit(void)
3101{
3102 dm_unregister_target(&integrity_target);
3103 kmem_cache_destroy(journal_io_cache);
3104}
3105
3106module_init(dm_integrity_init);
3107module_exit(dm_integrity_exit);
3108
3109MODULE_AUTHOR("Milan Broz");
3110MODULE_AUTHOR("Mikulas Patocka");
3111MODULE_DESCRIPTION(DM_NAME " target for integrity tags extension");
3112MODULE_LICENSE("GPL");