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
6 * This file is released under the GPL.
9 #include <linux/compiler.h>
10 #include <linux/module.h>
11 #include <linux/device-mapper.h>
12 #include <linux/dm-io.h>
13 #include <linux/vmalloc.h>
14 #include <linux/sort.h>
15 #include <linux/rbtree.h>
16 #include <linux/delay.h>
17 #include <linux/random.h>
18 #include <linux/reboot.h>
19 #include <crypto/hash.h>
20 #include <crypto/skcipher.h>
21 #include <linux/async_tx.h>
22 #include <linux/dm-bufio.h>
24 #define DM_MSG_PREFIX "integrity"
26 #define DEFAULT_INTERLEAVE_SECTORS 32768
27 #define DEFAULT_JOURNAL_SIZE_FACTOR 7
28 #define DEFAULT_SECTORS_PER_BITMAP_BIT 32768
29 #define DEFAULT_BUFFER_SECTORS 128
30 #define DEFAULT_JOURNAL_WATERMARK 50
31 #define DEFAULT_SYNC_MSEC 10000
32 #define DEFAULT_MAX_JOURNAL_SECTORS 131072
33 #define MIN_LOG2_INTERLEAVE_SECTORS 3
34 #define MAX_LOG2_INTERLEAVE_SECTORS 31
35 #define METADATA_WORKQUEUE_MAX_ACTIVE 16
36 #define RECALC_SECTORS 8192
37 #define RECALC_WRITE_SUPER 16
38 #define BITMAP_BLOCK_SIZE 4096 /* don't change it */
39 #define BITMAP_FLUSH_INTERVAL (10 * HZ)
42 * Warning - DEBUG_PRINT prints security-sensitive data to the log,
43 * so it should not be enabled in the official kernel
46 //#define INTERNAL_VERIFY
52 #define SB_MAGIC "integrt"
53 #define SB_VERSION_1 1
54 #define SB_VERSION_2 2
55 #define SB_VERSION_3 3
56 #define SB_VERSION_4 4
58 #define MAX_SECTORS_PER_BLOCK 8
63 __u8 log2_interleave_sectors;
64 __u16 integrity_tag_size;
65 __u32 journal_sections;
66 __u64 provided_data_sectors; /* userspace uses this value */
68 __u8 log2_sectors_per_block;
69 __u8 log2_blocks_per_bitmap_bit;
74 #define SB_FLAG_HAVE_JOURNAL_MAC 0x1
75 #define SB_FLAG_RECALCULATING 0x2
76 #define SB_FLAG_DIRTY_BITMAP 0x4
77 #define SB_FLAG_FIXED_PADDING 0x8
79 #define JOURNAL_ENTRY_ROUNDUP 8
81 typedef __u64 commit_id_t;
82 #define JOURNAL_MAC_PER_SECTOR 8
84 struct journal_entry {
92 commit_id_t last_bytes[0];
96 #define journal_entry_tag(ic, je) ((__u8 *)&(je)->last_bytes[(ic)->sectors_per_block])
98 #if BITS_PER_LONG == 64
99 #define journal_entry_set_sector(je, x) do { smp_wmb(); WRITE_ONCE((je)->u.sector, cpu_to_le64(x)); } while (0)
101 #define journal_entry_set_sector(je, x) do { (je)->u.s.sector_lo = cpu_to_le32(x); smp_wmb(); WRITE_ONCE((je)->u.s.sector_hi, cpu_to_le32((x) >> 32)); } while (0)
103 #define journal_entry_get_sector(je) le64_to_cpu((je)->u.sector)
104 #define journal_entry_is_unused(je) ((je)->u.s.sector_hi == cpu_to_le32(-1))
105 #define journal_entry_set_unused(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-1)); } while (0)
106 #define journal_entry_is_inprogress(je) ((je)->u.s.sector_hi == cpu_to_le32(-2))
107 #define journal_entry_set_inprogress(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-2)); } while (0)
109 #define JOURNAL_BLOCK_SECTORS 8
110 #define JOURNAL_SECTOR_DATA ((1 << SECTOR_SHIFT) - sizeof(commit_id_t))
111 #define JOURNAL_MAC_SIZE (JOURNAL_MAC_PER_SECTOR * JOURNAL_BLOCK_SECTORS)
113 struct journal_sector {
114 __u8 entries[JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR];
115 __u8 mac[JOURNAL_MAC_PER_SECTOR];
116 commit_id_t commit_id;
119 #define MAX_TAG_SIZE (JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR - offsetof(struct journal_entry, last_bytes[MAX_SECTORS_PER_BLOCK]))
121 #define METADATA_PADDING_SECTORS 8
123 #define N_COMMIT_IDS 4
125 static unsigned char prev_commit_seq(unsigned char seq)
127 return (seq + N_COMMIT_IDS - 1) % N_COMMIT_IDS;
130 static unsigned char next_commit_seq(unsigned char seq)
132 return (seq + 1) % N_COMMIT_IDS;
136 * In-memory structures
139 struct journal_node {
151 struct dm_integrity_c {
153 struct dm_dev *meta_dev;
157 mempool_t journal_io_mempool;
158 struct dm_io_client *io;
159 struct dm_bufio_client *bufio;
160 struct workqueue_struct *metadata_wq;
161 struct superblock *sb;
162 unsigned journal_pages;
163 unsigned n_bitmap_blocks;
165 struct page_list *journal;
166 struct page_list *journal_io;
167 struct page_list *journal_xor;
168 struct page_list *recalc_bitmap;
169 struct page_list *may_write_bitmap;
170 struct bitmap_block_status *bbs;
171 unsigned bitmap_flush_interval;
172 int synchronous_mode;
173 struct bio_list synchronous_bios;
174 struct delayed_work bitmap_flush_work;
176 struct crypto_skcipher *journal_crypt;
177 struct scatterlist **journal_scatterlist;
178 struct scatterlist **journal_io_scatterlist;
179 struct skcipher_request **sk_requests;
181 struct crypto_shash *journal_mac;
183 struct journal_node *journal_tree;
184 struct rb_root journal_tree_root;
186 sector_t provided_data_sectors;
188 unsigned short journal_entry_size;
189 unsigned char journal_entries_per_sector;
190 unsigned char journal_section_entries;
191 unsigned short journal_section_sectors;
192 unsigned journal_sections;
193 unsigned journal_entries;
194 sector_t data_device_sectors;
195 sector_t meta_device_sectors;
196 unsigned initial_sectors;
197 unsigned metadata_run;
198 __s8 log2_metadata_run;
199 __u8 log2_buffer_sectors;
200 __u8 sectors_per_block;
201 __u8 log2_blocks_per_bitmap_bit;
208 struct crypto_shash *internal_hash;
210 /* these variables are locked with endio_wait.lock */
211 struct rb_root in_progress;
212 struct list_head wait_list;
213 wait_queue_head_t endio_wait;
214 struct workqueue_struct *wait_wq;
215 struct workqueue_struct *offload_wq;
217 unsigned char commit_seq;
218 commit_id_t commit_ids[N_COMMIT_IDS];
220 unsigned committed_section;
221 unsigned n_committed_sections;
223 unsigned uncommitted_section;
224 unsigned n_uncommitted_sections;
226 unsigned free_section;
227 unsigned char free_section_entry;
228 unsigned free_sectors;
230 unsigned free_sectors_threshold;
232 struct workqueue_struct *commit_wq;
233 struct work_struct commit_work;
235 struct workqueue_struct *writer_wq;
236 struct work_struct writer_work;
238 struct workqueue_struct *recalc_wq;
239 struct work_struct recalc_work;
243 struct bio_list flush_bio_list;
245 unsigned long autocommit_jiffies;
246 struct timer_list autocommit_timer;
247 unsigned autocommit_msec;
249 wait_queue_head_t copy_to_journal_wait;
251 struct completion crypto_backoff;
253 bool journal_uptodate;
255 bool recalculate_flag;
258 struct alg_spec internal_hash_alg;
259 struct alg_spec journal_crypt_alg;
260 struct alg_spec journal_mac_alg;
262 atomic64_t number_of_mismatches;
264 struct notifier_block reboot_notifier;
267 struct dm_integrity_range {
268 sector_t logical_sector;
274 struct task_struct *task;
275 struct list_head wait_entry;
280 struct dm_integrity_io {
281 struct work_struct work;
283 struct dm_integrity_c *ic;
287 struct dm_integrity_range range;
289 sector_t metadata_block;
290 unsigned metadata_offset;
293 blk_status_t bi_status;
295 struct completion *completion;
297 struct gendisk *orig_bi_disk;
299 bio_end_io_t *orig_bi_end_io;
300 struct bio_integrity_payload *orig_bi_integrity;
301 struct bvec_iter orig_bi_iter;
304 struct journal_completion {
305 struct dm_integrity_c *ic;
307 struct completion comp;
311 struct dm_integrity_range range;
312 struct journal_completion *comp;
315 struct bitmap_block_status {
316 struct work_struct work;
317 struct dm_integrity_c *ic;
319 unsigned long *bitmap;
320 struct bio_list bio_queue;
321 spinlock_t bio_queue_lock;
325 static struct kmem_cache *journal_io_cache;
327 #define JOURNAL_IO_MEMPOOL 32
330 #define DEBUG_print(x, ...) printk(KERN_DEBUG x, ##__VA_ARGS__)
331 static void __DEBUG_bytes(__u8 *bytes, size_t len, const char *msg, ...)
340 pr_cont(" %02x", *bytes);
346 #define DEBUG_bytes(bytes, len, msg, ...) __DEBUG_bytes(bytes, len, KERN_DEBUG msg, ##__VA_ARGS__)
348 #define DEBUG_print(x, ...) do { } while (0)
349 #define DEBUG_bytes(bytes, len, msg, ...) do { } while (0)
352 static void dm_integrity_prepare(struct request *rq)
356 static void dm_integrity_complete(struct request *rq, unsigned int nr_bytes)
361 * DM Integrity profile, protection is performed layer above (dm-crypt)
363 static const struct blk_integrity_profile dm_integrity_profile = {
364 .name = "DM-DIF-EXT-TAG",
367 .prepare_fn = dm_integrity_prepare,
368 .complete_fn = dm_integrity_complete,
371 static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map);
372 static void integrity_bio_wait(struct work_struct *w);
373 static void dm_integrity_dtr(struct dm_target *ti);
375 static void dm_integrity_io_error(struct dm_integrity_c *ic, const char *msg, int err)
378 atomic64_inc(&ic->number_of_mismatches);
379 if (!cmpxchg(&ic->failed, 0, err))
380 DMERR("Error on %s: %d", msg, err);
383 static int dm_integrity_failed(struct dm_integrity_c *ic)
385 return READ_ONCE(ic->failed);
388 static commit_id_t dm_integrity_commit_id(struct dm_integrity_c *ic, unsigned i,
389 unsigned j, unsigned char seq)
392 * Xor the number with section and sector, so that if a piece of
393 * journal is written at wrong place, it is detected.
395 return ic->commit_ids[seq] ^ cpu_to_le64(((__u64)i << 32) ^ j);
398 static void get_area_and_offset(struct dm_integrity_c *ic, sector_t data_sector,
399 sector_t *area, sector_t *offset)
402 __u8 log2_interleave_sectors = ic->sb->log2_interleave_sectors;
403 *area = data_sector >> log2_interleave_sectors;
404 *offset = (unsigned)data_sector & ((1U << log2_interleave_sectors) - 1);
407 *offset = data_sector;
411 #define sector_to_block(ic, n) \
413 BUG_ON((n) & (unsigned)((ic)->sectors_per_block - 1)); \
414 (n) >>= (ic)->sb->log2_sectors_per_block; \
417 static __u64 get_metadata_sector_and_offset(struct dm_integrity_c *ic, sector_t area,
418 sector_t offset, unsigned *metadata_offset)
423 ms = area << ic->sb->log2_interleave_sectors;
424 if (likely(ic->log2_metadata_run >= 0))
425 ms += area << ic->log2_metadata_run;
427 ms += area * ic->metadata_run;
428 ms >>= ic->log2_buffer_sectors;
430 sector_to_block(ic, offset);
432 if (likely(ic->log2_tag_size >= 0)) {
433 ms += offset >> (SECTOR_SHIFT + ic->log2_buffer_sectors - ic->log2_tag_size);
434 mo = (offset << ic->log2_tag_size) & ((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - 1);
436 ms += (__u64)offset * ic->tag_size >> (SECTOR_SHIFT + ic->log2_buffer_sectors);
437 mo = (offset * ic->tag_size) & ((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - 1);
439 *metadata_offset = mo;
443 static sector_t get_data_sector(struct dm_integrity_c *ic, sector_t area, sector_t offset)
450 result = area << ic->sb->log2_interleave_sectors;
451 if (likely(ic->log2_metadata_run >= 0))
452 result += (area + 1) << ic->log2_metadata_run;
454 result += (area + 1) * ic->metadata_run;
456 result += (sector_t)ic->initial_sectors + offset;
462 static void wraparound_section(struct dm_integrity_c *ic, unsigned *sec_ptr)
464 if (unlikely(*sec_ptr >= ic->journal_sections))
465 *sec_ptr -= ic->journal_sections;
468 static void sb_set_version(struct dm_integrity_c *ic)
470 if (ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_PADDING))
471 ic->sb->version = SB_VERSION_4;
472 else if (ic->mode == 'B' || ic->sb->flags & cpu_to_le32(SB_FLAG_DIRTY_BITMAP))
473 ic->sb->version = SB_VERSION_3;
474 else if (ic->meta_dev || ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))
475 ic->sb->version = SB_VERSION_2;
477 ic->sb->version = SB_VERSION_1;
480 static int sync_rw_sb(struct dm_integrity_c *ic, int op, int op_flags)
482 struct dm_io_request io_req;
483 struct dm_io_region io_loc;
486 io_req.bi_op_flags = op_flags;
487 io_req.mem.type = DM_IO_KMEM;
488 io_req.mem.ptr.addr = ic->sb;
489 io_req.notify.fn = NULL;
490 io_req.client = ic->io;
491 io_loc.bdev = ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev;
492 io_loc.sector = ic->start;
493 io_loc.count = SB_SECTORS;
495 if (op == REQ_OP_WRITE)
498 return dm_io(&io_req, 1, &io_loc, NULL);
501 #define BITMAP_OP_TEST_ALL_SET 0
502 #define BITMAP_OP_TEST_ALL_CLEAR 1
503 #define BITMAP_OP_SET 2
504 #define BITMAP_OP_CLEAR 3
506 static bool block_bitmap_op(struct dm_integrity_c *ic, struct page_list *bitmap,
507 sector_t sector, sector_t n_sectors, int mode)
509 unsigned long bit, end_bit, this_end_bit, page, end_page;
512 if (unlikely(((sector | n_sectors) & ((1 << ic->sb->log2_sectors_per_block) - 1)) != 0)) {
513 DMCRIT("invalid bitmap access (%llx,%llx,%d,%d,%d)",
514 (unsigned long long)sector,
515 (unsigned long long)n_sectors,
516 ic->sb->log2_sectors_per_block,
517 ic->log2_blocks_per_bitmap_bit,
522 if (unlikely(!n_sectors))
525 bit = sector >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
526 end_bit = (sector + n_sectors - 1) >>
527 (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
529 page = bit / (PAGE_SIZE * 8);
530 bit %= PAGE_SIZE * 8;
532 end_page = end_bit / (PAGE_SIZE * 8);
533 end_bit %= PAGE_SIZE * 8;
536 if (page < end_page) {
537 this_end_bit = PAGE_SIZE * 8 - 1;
539 this_end_bit = end_bit;
542 data = lowmem_page_address(bitmap[page].page);
544 if (mode == BITMAP_OP_TEST_ALL_SET) {
545 while (bit <= this_end_bit) {
546 if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
548 if (data[bit / BITS_PER_LONG] != -1)
550 bit += BITS_PER_LONG;
551 } while (this_end_bit >= bit + BITS_PER_LONG - 1);
554 if (!test_bit(bit, data))
558 } else if (mode == BITMAP_OP_TEST_ALL_CLEAR) {
559 while (bit <= this_end_bit) {
560 if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
562 if (data[bit / BITS_PER_LONG] != 0)
564 bit += BITS_PER_LONG;
565 } while (this_end_bit >= bit + BITS_PER_LONG - 1);
568 if (test_bit(bit, data))
572 } else if (mode == BITMAP_OP_SET) {
573 while (bit <= this_end_bit) {
574 if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
576 data[bit / BITS_PER_LONG] = -1;
577 bit += BITS_PER_LONG;
578 } while (this_end_bit >= bit + BITS_PER_LONG - 1);
581 __set_bit(bit, data);
584 } else if (mode == BITMAP_OP_CLEAR) {
585 if (!bit && this_end_bit == PAGE_SIZE * 8 - 1)
587 else while (bit <= this_end_bit) {
588 if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
590 data[bit / BITS_PER_LONG] = 0;
591 bit += BITS_PER_LONG;
592 } while (this_end_bit >= bit + BITS_PER_LONG - 1);
595 __clear_bit(bit, data);
602 if (unlikely(page < end_page)) {
611 static void block_bitmap_copy(struct dm_integrity_c *ic, struct page_list *dst, struct page_list *src)
613 unsigned n_bitmap_pages = DIV_ROUND_UP(ic->n_bitmap_blocks, PAGE_SIZE / BITMAP_BLOCK_SIZE);
616 for (i = 0; i < n_bitmap_pages; i++) {
617 unsigned long *dst_data = lowmem_page_address(dst[i].page);
618 unsigned long *src_data = lowmem_page_address(src[i].page);
619 copy_page(dst_data, src_data);
623 static struct bitmap_block_status *sector_to_bitmap_block(struct dm_integrity_c *ic, sector_t sector)
625 unsigned bit = sector >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
626 unsigned bitmap_block = bit / (BITMAP_BLOCK_SIZE * 8);
628 BUG_ON(bitmap_block >= ic->n_bitmap_blocks);
629 return &ic->bbs[bitmap_block];
632 static void access_journal_check(struct dm_integrity_c *ic, unsigned section, unsigned offset,
633 bool e, const char *function)
635 #if defined(CONFIG_DM_DEBUG) || defined(INTERNAL_VERIFY)
636 unsigned limit = e ? ic->journal_section_entries : ic->journal_section_sectors;
638 if (unlikely(section >= ic->journal_sections) ||
639 unlikely(offset >= limit)) {
640 DMCRIT("%s: invalid access at (%u,%u), limit (%u,%u)",
641 function, section, offset, ic->journal_sections, limit);
647 static void page_list_location(struct dm_integrity_c *ic, unsigned section, unsigned offset,
648 unsigned *pl_index, unsigned *pl_offset)
652 access_journal_check(ic, section, offset, false, "page_list_location");
654 sector = section * ic->journal_section_sectors + offset;
656 *pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
657 *pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
660 static struct journal_sector *access_page_list(struct dm_integrity_c *ic, struct page_list *pl,
661 unsigned section, unsigned offset, unsigned *n_sectors)
663 unsigned pl_index, pl_offset;
666 page_list_location(ic, section, offset, &pl_index, &pl_offset);
669 *n_sectors = (PAGE_SIZE - pl_offset) >> SECTOR_SHIFT;
671 va = lowmem_page_address(pl[pl_index].page);
673 return (struct journal_sector *)(va + pl_offset);
676 static struct journal_sector *access_journal(struct dm_integrity_c *ic, unsigned section, unsigned offset)
678 return access_page_list(ic, ic->journal, section, offset, NULL);
681 static struct journal_entry *access_journal_entry(struct dm_integrity_c *ic, unsigned section, unsigned n)
683 unsigned rel_sector, offset;
684 struct journal_sector *js;
686 access_journal_check(ic, section, n, true, "access_journal_entry");
688 rel_sector = n % JOURNAL_BLOCK_SECTORS;
689 offset = n / JOURNAL_BLOCK_SECTORS;
691 js = access_journal(ic, section, rel_sector);
692 return (struct journal_entry *)((char *)js + offset * ic->journal_entry_size);
695 static struct journal_sector *access_journal_data(struct dm_integrity_c *ic, unsigned section, unsigned n)
697 n <<= ic->sb->log2_sectors_per_block;
699 n += JOURNAL_BLOCK_SECTORS;
701 access_journal_check(ic, section, n, false, "access_journal_data");
703 return access_journal(ic, section, n);
706 static void section_mac(struct dm_integrity_c *ic, unsigned section, __u8 result[JOURNAL_MAC_SIZE])
708 SHASH_DESC_ON_STACK(desc, ic->journal_mac);
712 desc->tfm = ic->journal_mac;
714 r = crypto_shash_init(desc);
716 dm_integrity_io_error(ic, "crypto_shash_init", r);
720 for (j = 0; j < ic->journal_section_entries; j++) {
721 struct journal_entry *je = access_journal_entry(ic, section, j);
722 r = crypto_shash_update(desc, (__u8 *)&je->u.sector, sizeof je->u.sector);
724 dm_integrity_io_error(ic, "crypto_shash_update", r);
729 size = crypto_shash_digestsize(ic->journal_mac);
731 if (likely(size <= JOURNAL_MAC_SIZE)) {
732 r = crypto_shash_final(desc, result);
734 dm_integrity_io_error(ic, "crypto_shash_final", r);
737 memset(result + size, 0, JOURNAL_MAC_SIZE - size);
739 __u8 digest[HASH_MAX_DIGESTSIZE];
741 if (WARN_ON(size > sizeof(digest))) {
742 dm_integrity_io_error(ic, "digest_size", -EINVAL);
745 r = crypto_shash_final(desc, digest);
747 dm_integrity_io_error(ic, "crypto_shash_final", r);
750 memcpy(result, digest, JOURNAL_MAC_SIZE);
755 memset(result, 0, JOURNAL_MAC_SIZE);
758 static void rw_section_mac(struct dm_integrity_c *ic, unsigned section, bool wr)
760 __u8 result[JOURNAL_MAC_SIZE];
763 if (!ic->journal_mac)
766 section_mac(ic, section, result);
768 for (j = 0; j < JOURNAL_BLOCK_SECTORS; j++) {
769 struct journal_sector *js = access_journal(ic, section, j);
772 memcpy(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR);
774 if (memcmp(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR))
775 dm_integrity_io_error(ic, "journal mac", -EILSEQ);
780 static void complete_journal_op(void *context)
782 struct journal_completion *comp = context;
783 BUG_ON(!atomic_read(&comp->in_flight));
784 if (likely(atomic_dec_and_test(&comp->in_flight)))
785 complete(&comp->comp);
788 static void xor_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
789 unsigned n_sections, struct journal_completion *comp)
791 struct async_submit_ctl submit;
792 size_t n_bytes = (size_t)(n_sections * ic->journal_section_sectors) << SECTOR_SHIFT;
793 unsigned pl_index, pl_offset, section_index;
794 struct page_list *source_pl, *target_pl;
796 if (likely(encrypt)) {
797 source_pl = ic->journal;
798 target_pl = ic->journal_io;
800 source_pl = ic->journal_io;
801 target_pl = ic->journal;
804 page_list_location(ic, section, 0, &pl_index, &pl_offset);
806 atomic_add(roundup(pl_offset + n_bytes, PAGE_SIZE) >> PAGE_SHIFT, &comp->in_flight);
808 init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL, complete_journal_op, comp, NULL);
810 section_index = pl_index;
814 struct page *src_pages[2];
815 struct page *dst_page;
817 while (unlikely(pl_index == section_index)) {
820 rw_section_mac(ic, section, true);
825 page_list_location(ic, section, 0, §ion_index, &dummy);
828 this_step = min(n_bytes, (size_t)PAGE_SIZE - pl_offset);
829 dst_page = target_pl[pl_index].page;
830 src_pages[0] = source_pl[pl_index].page;
831 src_pages[1] = ic->journal_xor[pl_index].page;
833 async_xor(dst_page, src_pages, pl_offset, 2, this_step, &submit);
837 n_bytes -= this_step;
842 async_tx_issue_pending_all();
845 static void complete_journal_encrypt(struct crypto_async_request *req, int err)
847 struct journal_completion *comp = req->data;
849 if (likely(err == -EINPROGRESS)) {
850 complete(&comp->ic->crypto_backoff);
853 dm_integrity_io_error(comp->ic, "asynchronous encrypt", err);
855 complete_journal_op(comp);
858 static bool do_crypt(bool encrypt, struct skcipher_request *req, struct journal_completion *comp)
861 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
862 complete_journal_encrypt, comp);
864 r = crypto_skcipher_encrypt(req);
866 r = crypto_skcipher_decrypt(req);
869 if (likely(r == -EINPROGRESS))
871 if (likely(r == -EBUSY)) {
872 wait_for_completion(&comp->ic->crypto_backoff);
873 reinit_completion(&comp->ic->crypto_backoff);
876 dm_integrity_io_error(comp->ic, "encrypt", r);
880 static void crypt_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
881 unsigned n_sections, struct journal_completion *comp)
883 struct scatterlist **source_sg;
884 struct scatterlist **target_sg;
886 atomic_add(2, &comp->in_flight);
888 if (likely(encrypt)) {
889 source_sg = ic->journal_scatterlist;
890 target_sg = ic->journal_io_scatterlist;
892 source_sg = ic->journal_io_scatterlist;
893 target_sg = ic->journal_scatterlist;
897 struct skcipher_request *req;
902 rw_section_mac(ic, section, true);
904 req = ic->sk_requests[section];
905 ivsize = crypto_skcipher_ivsize(ic->journal_crypt);
908 memcpy(iv, iv + ivsize, ivsize);
910 req->src = source_sg[section];
911 req->dst = target_sg[section];
913 if (unlikely(do_crypt(encrypt, req, comp)))
914 atomic_inc(&comp->in_flight);
918 } while (n_sections);
920 atomic_dec(&comp->in_flight);
921 complete_journal_op(comp);
924 static void encrypt_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
925 unsigned n_sections, struct journal_completion *comp)
928 return xor_journal(ic, encrypt, section, n_sections, comp);
930 return crypt_journal(ic, encrypt, section, n_sections, comp);
933 static void complete_journal_io(unsigned long error, void *context)
935 struct journal_completion *comp = context;
936 if (unlikely(error != 0))
937 dm_integrity_io_error(comp->ic, "writing journal", -EIO);
938 complete_journal_op(comp);
941 static void rw_journal_sectors(struct dm_integrity_c *ic, int op, int op_flags,
942 unsigned sector, unsigned n_sectors, struct journal_completion *comp)
944 struct dm_io_request io_req;
945 struct dm_io_region io_loc;
946 unsigned pl_index, pl_offset;
949 if (unlikely(dm_integrity_failed(ic))) {
951 complete_journal_io(-1UL, comp);
955 pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
956 pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
959 io_req.bi_op_flags = op_flags;
960 io_req.mem.type = DM_IO_PAGE_LIST;
962 io_req.mem.ptr.pl = &ic->journal_io[pl_index];
964 io_req.mem.ptr.pl = &ic->journal[pl_index];
965 io_req.mem.offset = pl_offset;
966 if (likely(comp != NULL)) {
967 io_req.notify.fn = complete_journal_io;
968 io_req.notify.context = comp;
970 io_req.notify.fn = NULL;
972 io_req.client = ic->io;
973 io_loc.bdev = ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev;
974 io_loc.sector = ic->start + SB_SECTORS + sector;
975 io_loc.count = n_sectors;
977 r = dm_io(&io_req, 1, &io_loc, NULL);
979 dm_integrity_io_error(ic, op == REQ_OP_READ ? "reading journal" : "writing journal", r);
981 WARN_ONCE(1, "asynchronous dm_io failed: %d", r);
982 complete_journal_io(-1UL, comp);
987 static void rw_journal(struct dm_integrity_c *ic, int op, int op_flags, unsigned section,
988 unsigned n_sections, struct journal_completion *comp)
990 unsigned sector, n_sectors;
992 sector = section * ic->journal_section_sectors;
993 n_sectors = n_sections * ic->journal_section_sectors;
995 rw_journal_sectors(ic, op, op_flags, sector, n_sectors, comp);
998 static void write_journal(struct dm_integrity_c *ic, unsigned commit_start, unsigned commit_sections)
1000 struct journal_completion io_comp;
1001 struct journal_completion crypt_comp_1;
1002 struct journal_completion crypt_comp_2;
1006 init_completion(&io_comp.comp);
1008 if (commit_start + commit_sections <= ic->journal_sections) {
1009 io_comp.in_flight = (atomic_t)ATOMIC_INIT(1);
1010 if (ic->journal_io) {
1011 crypt_comp_1.ic = ic;
1012 init_completion(&crypt_comp_1.comp);
1013 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
1014 encrypt_journal(ic, true, commit_start, commit_sections, &crypt_comp_1);
1015 wait_for_completion_io(&crypt_comp_1.comp);
1017 for (i = 0; i < commit_sections; i++)
1018 rw_section_mac(ic, commit_start + i, true);
1020 rw_journal(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, commit_start,
1021 commit_sections, &io_comp);
1024 io_comp.in_flight = (atomic_t)ATOMIC_INIT(2);
1025 to_end = ic->journal_sections - commit_start;
1026 if (ic->journal_io) {
1027 crypt_comp_1.ic = ic;
1028 init_completion(&crypt_comp_1.comp);
1029 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
1030 encrypt_journal(ic, true, commit_start, to_end, &crypt_comp_1);
1031 if (try_wait_for_completion(&crypt_comp_1.comp)) {
1032 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
1033 reinit_completion(&crypt_comp_1.comp);
1034 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
1035 encrypt_journal(ic, true, 0, commit_sections - to_end, &crypt_comp_1);
1036 wait_for_completion_io(&crypt_comp_1.comp);
1038 crypt_comp_2.ic = ic;
1039 init_completion(&crypt_comp_2.comp);
1040 crypt_comp_2.in_flight = (atomic_t)ATOMIC_INIT(0);
1041 encrypt_journal(ic, true, 0, commit_sections - to_end, &crypt_comp_2);
1042 wait_for_completion_io(&crypt_comp_1.comp);
1043 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
1044 wait_for_completion_io(&crypt_comp_2.comp);
1047 for (i = 0; i < to_end; i++)
1048 rw_section_mac(ic, commit_start + i, true);
1049 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
1050 for (i = 0; i < commit_sections - to_end; i++)
1051 rw_section_mac(ic, i, true);
1053 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, 0, commit_sections - to_end, &io_comp);
1056 wait_for_completion_io(&io_comp.comp);
1059 static void copy_from_journal(struct dm_integrity_c *ic, unsigned section, unsigned offset,
1060 unsigned n_sectors, sector_t target, io_notify_fn fn, void *data)
1062 struct dm_io_request io_req;
1063 struct dm_io_region io_loc;
1065 unsigned sector, pl_index, pl_offset;
1067 BUG_ON((target | n_sectors | offset) & (unsigned)(ic->sectors_per_block - 1));
1069 if (unlikely(dm_integrity_failed(ic))) {
1074 sector = section * ic->journal_section_sectors + JOURNAL_BLOCK_SECTORS + offset;
1076 pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
1077 pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
1079 io_req.bi_op = REQ_OP_WRITE;
1080 io_req.bi_op_flags = 0;
1081 io_req.mem.type = DM_IO_PAGE_LIST;
1082 io_req.mem.ptr.pl = &ic->journal[pl_index];
1083 io_req.mem.offset = pl_offset;
1084 io_req.notify.fn = fn;
1085 io_req.notify.context = data;
1086 io_req.client = ic->io;
1087 io_loc.bdev = ic->dev->bdev;
1088 io_loc.sector = target;
1089 io_loc.count = n_sectors;
1091 r = dm_io(&io_req, 1, &io_loc, NULL);
1093 WARN_ONCE(1, "asynchronous dm_io failed: %d", r);
1098 static bool ranges_overlap(struct dm_integrity_range *range1, struct dm_integrity_range *range2)
1100 return range1->logical_sector < range2->logical_sector + range2->n_sectors &&
1101 range1->logical_sector + range1->n_sectors > range2->logical_sector;
1104 static bool add_new_range(struct dm_integrity_c *ic, struct dm_integrity_range *new_range, bool check_waiting)
1106 struct rb_node **n = &ic->in_progress.rb_node;
1107 struct rb_node *parent;
1109 BUG_ON((new_range->logical_sector | new_range->n_sectors) & (unsigned)(ic->sectors_per_block - 1));
1111 if (likely(check_waiting)) {
1112 struct dm_integrity_range *range;
1113 list_for_each_entry(range, &ic->wait_list, wait_entry) {
1114 if (unlikely(ranges_overlap(range, new_range)))
1122 struct dm_integrity_range *range = container_of(*n, struct dm_integrity_range, node);
1125 if (new_range->logical_sector + new_range->n_sectors <= range->logical_sector) {
1126 n = &range->node.rb_left;
1127 } else if (new_range->logical_sector >= range->logical_sector + range->n_sectors) {
1128 n = &range->node.rb_right;
1134 rb_link_node(&new_range->node, parent, n);
1135 rb_insert_color(&new_range->node, &ic->in_progress);
1140 static void remove_range_unlocked(struct dm_integrity_c *ic, struct dm_integrity_range *range)
1142 rb_erase(&range->node, &ic->in_progress);
1143 while (unlikely(!list_empty(&ic->wait_list))) {
1144 struct dm_integrity_range *last_range =
1145 list_first_entry(&ic->wait_list, struct dm_integrity_range, wait_entry);
1146 struct task_struct *last_range_task;
1147 last_range_task = last_range->task;
1148 list_del(&last_range->wait_entry);
1149 if (!add_new_range(ic, last_range, false)) {
1150 last_range->task = last_range_task;
1151 list_add(&last_range->wait_entry, &ic->wait_list);
1154 last_range->waiting = false;
1155 wake_up_process(last_range_task);
1159 static void remove_range(struct dm_integrity_c *ic, struct dm_integrity_range *range)
1161 unsigned long flags;
1163 spin_lock_irqsave(&ic->endio_wait.lock, flags);
1164 remove_range_unlocked(ic, range);
1165 spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
1168 static void wait_and_add_new_range(struct dm_integrity_c *ic, struct dm_integrity_range *new_range)
1170 new_range->waiting = true;
1171 list_add_tail(&new_range->wait_entry, &ic->wait_list);
1172 new_range->task = current;
1174 __set_current_state(TASK_UNINTERRUPTIBLE);
1175 spin_unlock_irq(&ic->endio_wait.lock);
1177 spin_lock_irq(&ic->endio_wait.lock);
1178 } while (unlikely(new_range->waiting));
1181 static void add_new_range_and_wait(struct dm_integrity_c *ic, struct dm_integrity_range *new_range)
1183 if (unlikely(!add_new_range(ic, new_range, true)))
1184 wait_and_add_new_range(ic, new_range);
1187 static void init_journal_node(struct journal_node *node)
1189 RB_CLEAR_NODE(&node->node);
1190 node->sector = (sector_t)-1;
1193 static void add_journal_node(struct dm_integrity_c *ic, struct journal_node *node, sector_t sector)
1195 struct rb_node **link;
1196 struct rb_node *parent;
1198 node->sector = sector;
1199 BUG_ON(!RB_EMPTY_NODE(&node->node));
1201 link = &ic->journal_tree_root.rb_node;
1205 struct journal_node *j;
1207 j = container_of(parent, struct journal_node, node);
1208 if (sector < j->sector)
1209 link = &j->node.rb_left;
1211 link = &j->node.rb_right;
1214 rb_link_node(&node->node, parent, link);
1215 rb_insert_color(&node->node, &ic->journal_tree_root);
1218 static void remove_journal_node(struct dm_integrity_c *ic, struct journal_node *node)
1220 BUG_ON(RB_EMPTY_NODE(&node->node));
1221 rb_erase(&node->node, &ic->journal_tree_root);
1222 init_journal_node(node);
1225 #define NOT_FOUND (-1U)
1227 static unsigned find_journal_node(struct dm_integrity_c *ic, sector_t sector, sector_t *next_sector)
1229 struct rb_node *n = ic->journal_tree_root.rb_node;
1230 unsigned found = NOT_FOUND;
1231 *next_sector = (sector_t)-1;
1233 struct journal_node *j = container_of(n, struct journal_node, node);
1234 if (sector == j->sector) {
1235 found = j - ic->journal_tree;
1237 if (sector < j->sector) {
1238 *next_sector = j->sector;
1239 n = j->node.rb_left;
1241 n = j->node.rb_right;
1248 static bool test_journal_node(struct dm_integrity_c *ic, unsigned pos, sector_t sector)
1250 struct journal_node *node, *next_node;
1251 struct rb_node *next;
1253 if (unlikely(pos >= ic->journal_entries))
1255 node = &ic->journal_tree[pos];
1256 if (unlikely(RB_EMPTY_NODE(&node->node)))
1258 if (unlikely(node->sector != sector))
1261 next = rb_next(&node->node);
1262 if (unlikely(!next))
1265 next_node = container_of(next, struct journal_node, node);
1266 return next_node->sector != sector;
1269 static bool find_newer_committed_node(struct dm_integrity_c *ic, struct journal_node *node)
1271 struct rb_node *next;
1272 struct journal_node *next_node;
1273 unsigned next_section;
1275 BUG_ON(RB_EMPTY_NODE(&node->node));
1277 next = rb_next(&node->node);
1278 if (unlikely(!next))
1281 next_node = container_of(next, struct journal_node, node);
1283 if (next_node->sector != node->sector)
1286 next_section = (unsigned)(next_node - ic->journal_tree) / ic->journal_section_entries;
1287 if (next_section >= ic->committed_section &&
1288 next_section < ic->committed_section + ic->n_committed_sections)
1290 if (next_section + ic->journal_sections < ic->committed_section + ic->n_committed_sections)
1300 static int dm_integrity_rw_tag(struct dm_integrity_c *ic, unsigned char *tag, sector_t *metadata_block,
1301 unsigned *metadata_offset, unsigned total_size, int op)
1304 unsigned char *data, *dp;
1305 struct dm_buffer *b;
1309 r = dm_integrity_failed(ic);
1313 data = dm_bufio_read(ic->bufio, *metadata_block, &b);
1315 return PTR_ERR(data);
1317 to_copy = min((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - *metadata_offset, total_size);
1318 dp = data + *metadata_offset;
1319 if (op == TAG_READ) {
1320 memcpy(tag, dp, to_copy);
1321 } else if (op == TAG_WRITE) {
1322 memcpy(dp, tag, to_copy);
1323 dm_bufio_mark_partial_buffer_dirty(b, *metadata_offset, *metadata_offset + to_copy);
1325 /* e.g.: op == TAG_CMP */
1326 if (unlikely(memcmp(dp, tag, to_copy))) {
1329 for (i = 0; i < to_copy; i++) {
1330 if (dp[i] != tag[i])
1334 dm_bufio_release(b);
1338 dm_bufio_release(b);
1341 *metadata_offset += to_copy;
1342 if (unlikely(*metadata_offset == 1U << SECTOR_SHIFT << ic->log2_buffer_sectors)) {
1343 (*metadata_block)++;
1344 *metadata_offset = 0;
1346 total_size -= to_copy;
1347 } while (unlikely(total_size));
1352 static void dm_integrity_flush_buffers(struct dm_integrity_c *ic)
1355 r = dm_bufio_write_dirty_buffers(ic->bufio);
1357 dm_integrity_io_error(ic, "writing tags", r);
1360 static void sleep_on_endio_wait(struct dm_integrity_c *ic)
1362 DECLARE_WAITQUEUE(wait, current);
1363 __add_wait_queue(&ic->endio_wait, &wait);
1364 __set_current_state(TASK_UNINTERRUPTIBLE);
1365 spin_unlock_irq(&ic->endio_wait.lock);
1367 spin_lock_irq(&ic->endio_wait.lock);
1368 __remove_wait_queue(&ic->endio_wait, &wait);
1371 static void autocommit_fn(struct timer_list *t)
1373 struct dm_integrity_c *ic = from_timer(ic, t, autocommit_timer);
1375 if (likely(!dm_integrity_failed(ic)))
1376 queue_work(ic->commit_wq, &ic->commit_work);
1379 static void schedule_autocommit(struct dm_integrity_c *ic)
1381 if (!timer_pending(&ic->autocommit_timer))
1382 mod_timer(&ic->autocommit_timer, jiffies + ic->autocommit_jiffies);
1385 static void submit_flush_bio(struct dm_integrity_c *ic, struct dm_integrity_io *dio)
1388 unsigned long flags;
1390 spin_lock_irqsave(&ic->endio_wait.lock, flags);
1391 bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1392 bio_list_add(&ic->flush_bio_list, bio);
1393 spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
1395 queue_work(ic->commit_wq, &ic->commit_work);
1398 static void do_endio(struct dm_integrity_c *ic, struct bio *bio)
1400 int r = dm_integrity_failed(ic);
1401 if (unlikely(r) && !bio->bi_status)
1402 bio->bi_status = errno_to_blk_status(r);
1403 if (unlikely(ic->synchronous_mode) && bio_op(bio) == REQ_OP_WRITE) {
1404 unsigned long flags;
1405 spin_lock_irqsave(&ic->endio_wait.lock, flags);
1406 bio_list_add(&ic->synchronous_bios, bio);
1407 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0);
1408 spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
1414 static void do_endio_flush(struct dm_integrity_c *ic, struct dm_integrity_io *dio)
1416 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1418 if (unlikely(dio->fua) && likely(!bio->bi_status) && likely(!dm_integrity_failed(ic)))
1419 submit_flush_bio(ic, dio);
1424 static void dec_in_flight(struct dm_integrity_io *dio)
1426 if (atomic_dec_and_test(&dio->in_flight)) {
1427 struct dm_integrity_c *ic = dio->ic;
1430 remove_range(ic, &dio->range);
1432 if (unlikely(dio->write))
1433 schedule_autocommit(ic);
1435 bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1437 if (unlikely(dio->bi_status) && !bio->bi_status)
1438 bio->bi_status = dio->bi_status;
1439 if (likely(!bio->bi_status) && unlikely(bio_sectors(bio) != dio->range.n_sectors)) {
1440 dio->range.logical_sector += dio->range.n_sectors;
1441 bio_advance(bio, dio->range.n_sectors << SECTOR_SHIFT);
1442 INIT_WORK(&dio->work, integrity_bio_wait);
1443 queue_work(ic->offload_wq, &dio->work);
1446 do_endio_flush(ic, dio);
1450 static void integrity_end_io(struct bio *bio)
1452 struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
1454 bio->bi_iter = dio->orig_bi_iter;
1455 bio->bi_disk = dio->orig_bi_disk;
1456 bio->bi_partno = dio->orig_bi_partno;
1457 if (dio->orig_bi_integrity) {
1458 bio->bi_integrity = dio->orig_bi_integrity;
1459 bio->bi_opf |= REQ_INTEGRITY;
1461 bio->bi_end_io = dio->orig_bi_end_io;
1463 if (dio->completion)
1464 complete(dio->completion);
1469 static void integrity_sector_checksum(struct dm_integrity_c *ic, sector_t sector,
1470 const char *data, char *result)
1472 __u64 sector_le = cpu_to_le64(sector);
1473 SHASH_DESC_ON_STACK(req, ic->internal_hash);
1475 unsigned digest_size;
1477 req->tfm = ic->internal_hash;
1479 r = crypto_shash_init(req);
1480 if (unlikely(r < 0)) {
1481 dm_integrity_io_error(ic, "crypto_shash_init", r);
1485 r = crypto_shash_update(req, (const __u8 *)§or_le, sizeof sector_le);
1486 if (unlikely(r < 0)) {
1487 dm_integrity_io_error(ic, "crypto_shash_update", r);
1491 r = crypto_shash_update(req, data, ic->sectors_per_block << SECTOR_SHIFT);
1492 if (unlikely(r < 0)) {
1493 dm_integrity_io_error(ic, "crypto_shash_update", r);
1497 r = crypto_shash_final(req, result);
1498 if (unlikely(r < 0)) {
1499 dm_integrity_io_error(ic, "crypto_shash_final", r);
1503 digest_size = crypto_shash_digestsize(ic->internal_hash);
1504 if (unlikely(digest_size < ic->tag_size))
1505 memset(result + digest_size, 0, ic->tag_size - digest_size);
1510 /* this shouldn't happen anyway, the hash functions have no reason to fail */
1511 get_random_bytes(result, ic->tag_size);
1514 static void integrity_metadata(struct work_struct *w)
1516 struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work);
1517 struct dm_integrity_c *ic = dio->ic;
1521 if (ic->internal_hash) {
1522 struct bvec_iter iter;
1524 unsigned digest_size = crypto_shash_digestsize(ic->internal_hash);
1525 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1527 unsigned extra_space = unlikely(digest_size > ic->tag_size) ? digest_size - ic->tag_size : 0;
1528 char checksums_onstack[HASH_MAX_DIGESTSIZE];
1529 unsigned sectors_to_process = dio->range.n_sectors;
1530 sector_t sector = dio->range.logical_sector;
1532 if (unlikely(ic->mode == 'R'))
1535 checksums = kmalloc((PAGE_SIZE >> SECTOR_SHIFT >> ic->sb->log2_sectors_per_block) * ic->tag_size + extra_space,
1536 GFP_NOIO | __GFP_NORETRY | __GFP_NOWARN);
1538 checksums = checksums_onstack;
1539 if (WARN_ON(extra_space &&
1540 digest_size > sizeof(checksums_onstack))) {
1546 __bio_for_each_segment(bv, bio, iter, dio->orig_bi_iter) {
1548 char *mem, *checksums_ptr;
1551 mem = (char *)kmap_atomic(bv.bv_page) + bv.bv_offset;
1553 checksums_ptr = checksums;
1555 integrity_sector_checksum(ic, sector, mem + pos, checksums_ptr);
1556 checksums_ptr += ic->tag_size;
1557 sectors_to_process -= ic->sectors_per_block;
1558 pos += ic->sectors_per_block << SECTOR_SHIFT;
1559 sector += ic->sectors_per_block;
1560 } while (pos < bv.bv_len && sectors_to_process && checksums != checksums_onstack);
1563 r = dm_integrity_rw_tag(ic, checksums, &dio->metadata_block, &dio->metadata_offset,
1564 checksums_ptr - checksums, !dio->write ? TAG_CMP : TAG_WRITE);
1567 DMERR_LIMIT("Checksum failed at sector 0x%llx",
1568 (unsigned long long)(sector - ((r + ic->tag_size - 1) / ic->tag_size)));
1570 atomic64_inc(&ic->number_of_mismatches);
1572 if (likely(checksums != checksums_onstack))
1577 if (!sectors_to_process)
1580 if (unlikely(pos < bv.bv_len)) {
1581 bv.bv_offset += pos;
1587 if (likely(checksums != checksums_onstack))
1590 struct bio_integrity_payload *bip = dio->orig_bi_integrity;
1594 struct bvec_iter iter;
1595 unsigned data_to_process = dio->range.n_sectors;
1596 sector_to_block(ic, data_to_process);
1597 data_to_process *= ic->tag_size;
1599 bip_for_each_vec(biv, bip, iter) {
1603 BUG_ON(PageHighMem(biv.bv_page));
1604 tag = lowmem_page_address(biv.bv_page) + biv.bv_offset;
1605 this_len = min(biv.bv_len, data_to_process);
1606 r = dm_integrity_rw_tag(ic, tag, &dio->metadata_block, &dio->metadata_offset,
1607 this_len, !dio->write ? TAG_READ : TAG_WRITE);
1610 data_to_process -= this_len;
1611 if (!data_to_process)
1620 dio->bi_status = errno_to_blk_status(r);
1624 static int dm_integrity_map(struct dm_target *ti, struct bio *bio)
1626 struct dm_integrity_c *ic = ti->private;
1627 struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
1628 struct bio_integrity_payload *bip;
1630 sector_t area, offset;
1635 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1636 submit_flush_bio(ic, dio);
1637 return DM_MAPIO_SUBMITTED;
1640 dio->range.logical_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
1641 dio->write = bio_op(bio) == REQ_OP_WRITE;
1642 dio->fua = dio->write && bio->bi_opf & REQ_FUA;
1643 if (unlikely(dio->fua)) {
1645 * Don't pass down the FUA flag because we have to flush
1646 * disk cache anyway.
1648 bio->bi_opf &= ~REQ_FUA;
1650 if (unlikely(dio->range.logical_sector + bio_sectors(bio) > ic->provided_data_sectors)) {
1651 DMERR("Too big sector number: 0x%llx + 0x%x > 0x%llx",
1652 (unsigned long long)dio->range.logical_sector, bio_sectors(bio),
1653 (unsigned long long)ic->provided_data_sectors);
1654 return DM_MAPIO_KILL;
1656 if (unlikely((dio->range.logical_sector | bio_sectors(bio)) & (unsigned)(ic->sectors_per_block - 1))) {
1657 DMERR("Bio not aligned on %u sectors: 0x%llx, 0x%x",
1658 ic->sectors_per_block,
1659 (unsigned long long)dio->range.logical_sector, bio_sectors(bio));
1660 return DM_MAPIO_KILL;
1663 if (ic->sectors_per_block > 1) {
1664 struct bvec_iter iter;
1666 bio_for_each_segment(bv, bio, iter) {
1667 if (unlikely(bv.bv_len & ((ic->sectors_per_block << SECTOR_SHIFT) - 1))) {
1668 DMERR("Bio vector (%u,%u) is not aligned on %u-sector boundary",
1669 bv.bv_offset, bv.bv_len, ic->sectors_per_block);
1670 return DM_MAPIO_KILL;
1675 bip = bio_integrity(bio);
1676 if (!ic->internal_hash) {
1678 unsigned wanted_tag_size = bio_sectors(bio) >> ic->sb->log2_sectors_per_block;
1679 if (ic->log2_tag_size >= 0)
1680 wanted_tag_size <<= ic->log2_tag_size;
1682 wanted_tag_size *= ic->tag_size;
1683 if (unlikely(wanted_tag_size != bip->bip_iter.bi_size)) {
1684 DMERR("Invalid integrity data size %u, expected %u",
1685 bip->bip_iter.bi_size, wanted_tag_size);
1686 return DM_MAPIO_KILL;
1690 if (unlikely(bip != NULL)) {
1691 DMERR("Unexpected integrity data when using internal hash");
1692 return DM_MAPIO_KILL;
1696 if (unlikely(ic->mode == 'R') && unlikely(dio->write))
1697 return DM_MAPIO_KILL;
1699 get_area_and_offset(ic, dio->range.logical_sector, &area, &offset);
1700 dio->metadata_block = get_metadata_sector_and_offset(ic, area, offset, &dio->metadata_offset);
1701 bio->bi_iter.bi_sector = get_data_sector(ic, area, offset);
1703 dm_integrity_map_continue(dio, true);
1704 return DM_MAPIO_SUBMITTED;
1707 static bool __journal_read_write(struct dm_integrity_io *dio, struct bio *bio,
1708 unsigned journal_section, unsigned journal_entry)
1710 struct dm_integrity_c *ic = dio->ic;
1711 sector_t logical_sector;
1714 logical_sector = dio->range.logical_sector;
1715 n_sectors = dio->range.n_sectors;
1717 struct bio_vec bv = bio_iovec(bio);
1720 if (unlikely(bv.bv_len >> SECTOR_SHIFT > n_sectors))
1721 bv.bv_len = n_sectors << SECTOR_SHIFT;
1722 n_sectors -= bv.bv_len >> SECTOR_SHIFT;
1723 bio_advance_iter(bio, &bio->bi_iter, bv.bv_len);
1725 mem = kmap_atomic(bv.bv_page);
1726 if (likely(dio->write))
1727 flush_dcache_page(bv.bv_page);
1730 struct journal_entry *je = access_journal_entry(ic, journal_section, journal_entry);
1732 if (unlikely(!dio->write)) {
1733 struct journal_sector *js;
1737 if (unlikely(journal_entry_is_inprogress(je))) {
1738 flush_dcache_page(bv.bv_page);
1741 __io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je));
1745 BUG_ON(journal_entry_get_sector(je) != logical_sector);
1746 js = access_journal_data(ic, journal_section, journal_entry);
1747 mem_ptr = mem + bv.bv_offset;
1750 memcpy(mem_ptr, js, JOURNAL_SECTOR_DATA);
1751 *(commit_id_t *)(mem_ptr + JOURNAL_SECTOR_DATA) = je->last_bytes[s];
1753 mem_ptr += 1 << SECTOR_SHIFT;
1754 } while (++s < ic->sectors_per_block);
1755 #ifdef INTERNAL_VERIFY
1756 if (ic->internal_hash) {
1757 char checksums_onstack[max(HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)];
1759 integrity_sector_checksum(ic, logical_sector, mem + bv.bv_offset, checksums_onstack);
1760 if (unlikely(memcmp(checksums_onstack, journal_entry_tag(ic, je), ic->tag_size))) {
1761 DMERR_LIMIT("Checksum failed when reading from journal, at sector 0x%llx",
1762 (unsigned long long)logical_sector);
1768 if (!ic->internal_hash) {
1769 struct bio_integrity_payload *bip = bio_integrity(bio);
1770 unsigned tag_todo = ic->tag_size;
1771 char *tag_ptr = journal_entry_tag(ic, je);
1774 struct bio_vec biv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter);
1775 unsigned tag_now = min(biv.bv_len, tag_todo);
1777 BUG_ON(PageHighMem(biv.bv_page));
1778 tag_addr = lowmem_page_address(biv.bv_page) + biv.bv_offset;
1779 if (likely(dio->write))
1780 memcpy(tag_ptr, tag_addr, tag_now);
1782 memcpy(tag_addr, tag_ptr, tag_now);
1783 bvec_iter_advance(bip->bip_vec, &bip->bip_iter, tag_now);
1785 tag_todo -= tag_now;
1786 } while (unlikely(tag_todo)); else {
1787 if (likely(dio->write))
1788 memset(tag_ptr, 0, tag_todo);
1792 if (likely(dio->write)) {
1793 struct journal_sector *js;
1796 js = access_journal_data(ic, journal_section, journal_entry);
1797 memcpy(js, mem + bv.bv_offset, ic->sectors_per_block << SECTOR_SHIFT);
1801 je->last_bytes[s] = js[s].commit_id;
1802 } while (++s < ic->sectors_per_block);
1804 if (ic->internal_hash) {
1805 unsigned digest_size = crypto_shash_digestsize(ic->internal_hash);
1806 if (unlikely(digest_size > ic->tag_size)) {
1807 char checksums_onstack[HASH_MAX_DIGESTSIZE];
1808 integrity_sector_checksum(ic, logical_sector, (char *)js, checksums_onstack);
1809 memcpy(journal_entry_tag(ic, je), checksums_onstack, ic->tag_size);
1811 integrity_sector_checksum(ic, logical_sector, (char *)js, journal_entry_tag(ic, je));
1814 journal_entry_set_sector(je, logical_sector);
1816 logical_sector += ic->sectors_per_block;
1819 if (unlikely(journal_entry == ic->journal_section_entries)) {
1822 wraparound_section(ic, &journal_section);
1825 bv.bv_offset += ic->sectors_per_block << SECTOR_SHIFT;
1826 } while (bv.bv_len -= ic->sectors_per_block << SECTOR_SHIFT);
1828 if (unlikely(!dio->write))
1829 flush_dcache_page(bv.bv_page);
1831 } while (n_sectors);
1833 if (likely(dio->write)) {
1835 if (unlikely(waitqueue_active(&ic->copy_to_journal_wait)))
1836 wake_up(&ic->copy_to_journal_wait);
1837 if (READ_ONCE(ic->free_sectors) <= ic->free_sectors_threshold) {
1838 queue_work(ic->commit_wq, &ic->commit_work);
1840 schedule_autocommit(ic);
1843 remove_range(ic, &dio->range);
1846 if (unlikely(bio->bi_iter.bi_size)) {
1847 sector_t area, offset;
1849 dio->range.logical_sector = logical_sector;
1850 get_area_and_offset(ic, dio->range.logical_sector, &area, &offset);
1851 dio->metadata_block = get_metadata_sector_and_offset(ic, area, offset, &dio->metadata_offset);
1858 static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map)
1860 struct dm_integrity_c *ic = dio->ic;
1861 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1862 unsigned journal_section, journal_entry;
1863 unsigned journal_read_pos;
1864 struct completion read_comp;
1865 bool need_sync_io = ic->internal_hash && !dio->write;
1867 if (need_sync_io && from_map) {
1868 INIT_WORK(&dio->work, integrity_bio_wait);
1869 queue_work(ic->offload_wq, &dio->work);
1874 spin_lock_irq(&ic->endio_wait.lock);
1876 if (unlikely(dm_integrity_failed(ic))) {
1877 spin_unlock_irq(&ic->endio_wait.lock);
1881 dio->range.n_sectors = bio_sectors(bio);
1882 journal_read_pos = NOT_FOUND;
1883 if (likely(ic->mode == 'J')) {
1885 unsigned next_entry, i, pos;
1886 unsigned ws, we, range_sectors;
1888 dio->range.n_sectors = min(dio->range.n_sectors,
1889 (sector_t)ic->free_sectors << ic->sb->log2_sectors_per_block);
1890 if (unlikely(!dio->range.n_sectors)) {
1892 goto offload_to_thread;
1893 sleep_on_endio_wait(ic);
1896 range_sectors = dio->range.n_sectors >> ic->sb->log2_sectors_per_block;
1897 ic->free_sectors -= range_sectors;
1898 journal_section = ic->free_section;
1899 journal_entry = ic->free_section_entry;
1901 next_entry = ic->free_section_entry + range_sectors;
1902 ic->free_section_entry = next_entry % ic->journal_section_entries;
1903 ic->free_section += next_entry / ic->journal_section_entries;
1904 ic->n_uncommitted_sections += next_entry / ic->journal_section_entries;
1905 wraparound_section(ic, &ic->free_section);
1907 pos = journal_section * ic->journal_section_entries + journal_entry;
1908 ws = journal_section;
1912 struct journal_entry *je;
1914 add_journal_node(ic, &ic->journal_tree[pos], dio->range.logical_sector + i);
1916 if (unlikely(pos >= ic->journal_entries))
1919 je = access_journal_entry(ic, ws, we);
1920 BUG_ON(!journal_entry_is_unused(je));
1921 journal_entry_set_inprogress(je);
1923 if (unlikely(we == ic->journal_section_entries)) {
1926 wraparound_section(ic, &ws);
1928 } while ((i += ic->sectors_per_block) < dio->range.n_sectors);
1930 spin_unlock_irq(&ic->endio_wait.lock);
1931 goto journal_read_write;
1933 sector_t next_sector;
1934 journal_read_pos = find_journal_node(ic, dio->range.logical_sector, &next_sector);
1935 if (likely(journal_read_pos == NOT_FOUND)) {
1936 if (unlikely(dio->range.n_sectors > next_sector - dio->range.logical_sector))
1937 dio->range.n_sectors = next_sector - dio->range.logical_sector;
1940 unsigned jp = journal_read_pos + 1;
1941 for (i = ic->sectors_per_block; i < dio->range.n_sectors; i += ic->sectors_per_block, jp++) {
1942 if (!test_journal_node(ic, jp, dio->range.logical_sector + i))
1945 dio->range.n_sectors = i;
1949 if (unlikely(!add_new_range(ic, &dio->range, true))) {
1951 * We must not sleep in the request routine because it could
1952 * stall bios on current->bio_list.
1953 * So, we offload the bio to a workqueue if we have to sleep.
1957 spin_unlock_irq(&ic->endio_wait.lock);
1958 INIT_WORK(&dio->work, integrity_bio_wait);
1959 queue_work(ic->wait_wq, &dio->work);
1962 if (journal_read_pos != NOT_FOUND)
1963 dio->range.n_sectors = ic->sectors_per_block;
1964 wait_and_add_new_range(ic, &dio->range);
1966 * wait_and_add_new_range drops the spinlock, so the journal
1967 * may have been changed arbitrarily. We need to recheck.
1968 * To simplify the code, we restrict I/O size to just one block.
1970 if (journal_read_pos != NOT_FOUND) {
1971 sector_t next_sector;
1972 unsigned new_pos = find_journal_node(ic, dio->range.logical_sector, &next_sector);
1973 if (unlikely(new_pos != journal_read_pos)) {
1974 remove_range_unlocked(ic, &dio->range);
1979 spin_unlock_irq(&ic->endio_wait.lock);
1981 if (unlikely(journal_read_pos != NOT_FOUND)) {
1982 journal_section = journal_read_pos / ic->journal_section_entries;
1983 journal_entry = journal_read_pos % ic->journal_section_entries;
1984 goto journal_read_write;
1987 if (ic->mode == 'B' && dio->write) {
1988 if (!block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector,
1989 dio->range.n_sectors, BITMAP_OP_TEST_ALL_SET)) {
1990 struct bitmap_block_status *bbs;
1992 bbs = sector_to_bitmap_block(ic, dio->range.logical_sector);
1993 spin_lock(&bbs->bio_queue_lock);
1994 bio_list_add(&bbs->bio_queue, bio);
1995 spin_unlock(&bbs->bio_queue_lock);
1996 queue_work(ic->writer_wq, &bbs->work);
2001 dio->in_flight = (atomic_t)ATOMIC_INIT(2);
2004 init_completion(&read_comp);
2005 dio->completion = &read_comp;
2007 dio->completion = NULL;
2009 dio->orig_bi_iter = bio->bi_iter;
2011 dio->orig_bi_disk = bio->bi_disk;
2012 dio->orig_bi_partno = bio->bi_partno;
2013 bio_set_dev(bio, ic->dev->bdev);
2015 dio->orig_bi_integrity = bio_integrity(bio);
2016 bio->bi_integrity = NULL;
2017 bio->bi_opf &= ~REQ_INTEGRITY;
2019 dio->orig_bi_end_io = bio->bi_end_io;
2020 bio->bi_end_io = integrity_end_io;
2022 bio->bi_iter.bi_size = dio->range.n_sectors << SECTOR_SHIFT;
2023 generic_make_request(bio);
2026 wait_for_completion_io(&read_comp);
2027 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING) &&
2028 dio->range.logical_sector + dio->range.n_sectors > le64_to_cpu(ic->sb->recalc_sector))
2030 if (ic->mode == 'B') {
2031 if (!block_bitmap_op(ic, ic->recalc_bitmap, dio->range.logical_sector,
2032 dio->range.n_sectors, BITMAP_OP_TEST_ALL_CLEAR))
2036 if (likely(!bio->bi_status))
2037 integrity_metadata(&dio->work);
2043 INIT_WORK(&dio->work, integrity_metadata);
2044 queue_work(ic->metadata_wq, &dio->work);
2050 if (unlikely(__journal_read_write(dio, bio, journal_section, journal_entry)))
2053 do_endio_flush(ic, dio);
2057 static void integrity_bio_wait(struct work_struct *w)
2059 struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work);
2061 dm_integrity_map_continue(dio, false);
2064 static void pad_uncommitted(struct dm_integrity_c *ic)
2066 if (ic->free_section_entry) {
2067 ic->free_sectors -= ic->journal_section_entries - ic->free_section_entry;
2068 ic->free_section_entry = 0;
2070 wraparound_section(ic, &ic->free_section);
2071 ic->n_uncommitted_sections++;
2073 if (WARN_ON(ic->journal_sections * ic->journal_section_entries !=
2074 (ic->n_uncommitted_sections + ic->n_committed_sections) *
2075 ic->journal_section_entries + ic->free_sectors)) {
2076 DMCRIT("journal_sections %u, journal_section_entries %u, "
2077 "n_uncommitted_sections %u, n_committed_sections %u, "
2078 "journal_section_entries %u, free_sectors %u",
2079 ic->journal_sections, ic->journal_section_entries,
2080 ic->n_uncommitted_sections, ic->n_committed_sections,
2081 ic->journal_section_entries, ic->free_sectors);
2085 static void integrity_commit(struct work_struct *w)
2087 struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, commit_work);
2088 unsigned commit_start, commit_sections;
2090 struct bio *flushes;
2092 del_timer(&ic->autocommit_timer);
2094 spin_lock_irq(&ic->endio_wait.lock);
2095 flushes = bio_list_get(&ic->flush_bio_list);
2096 if (unlikely(ic->mode != 'J')) {
2097 spin_unlock_irq(&ic->endio_wait.lock);
2098 dm_integrity_flush_buffers(ic);
2099 goto release_flush_bios;
2102 pad_uncommitted(ic);
2103 commit_start = ic->uncommitted_section;
2104 commit_sections = ic->n_uncommitted_sections;
2105 spin_unlock_irq(&ic->endio_wait.lock);
2107 if (!commit_sections)
2108 goto release_flush_bios;
2111 for (n = 0; n < commit_sections; n++) {
2112 for (j = 0; j < ic->journal_section_entries; j++) {
2113 struct journal_entry *je;
2114 je = access_journal_entry(ic, i, j);
2115 io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je));
2117 for (j = 0; j < ic->journal_section_sectors; j++) {
2118 struct journal_sector *js;
2119 js = access_journal(ic, i, j);
2120 js->commit_id = dm_integrity_commit_id(ic, i, j, ic->commit_seq);
2123 if (unlikely(i >= ic->journal_sections))
2124 ic->commit_seq = next_commit_seq(ic->commit_seq);
2125 wraparound_section(ic, &i);
2129 write_journal(ic, commit_start, commit_sections);
2131 spin_lock_irq(&ic->endio_wait.lock);
2132 ic->uncommitted_section += commit_sections;
2133 wraparound_section(ic, &ic->uncommitted_section);
2134 ic->n_uncommitted_sections -= commit_sections;
2135 ic->n_committed_sections += commit_sections;
2136 spin_unlock_irq(&ic->endio_wait.lock);
2138 if (READ_ONCE(ic->free_sectors) <= ic->free_sectors_threshold)
2139 queue_work(ic->writer_wq, &ic->writer_work);
2143 struct bio *next = flushes->bi_next;
2144 flushes->bi_next = NULL;
2145 do_endio(ic, flushes);
2150 static void complete_copy_from_journal(unsigned long error, void *context)
2152 struct journal_io *io = context;
2153 struct journal_completion *comp = io->comp;
2154 struct dm_integrity_c *ic = comp->ic;
2155 remove_range(ic, &io->range);
2156 mempool_free(io, &ic->journal_io_mempool);
2157 if (unlikely(error != 0))
2158 dm_integrity_io_error(ic, "copying from journal", -EIO);
2159 complete_journal_op(comp);
2162 static void restore_last_bytes(struct dm_integrity_c *ic, struct journal_sector *js,
2163 struct journal_entry *je)
2167 js->commit_id = je->last_bytes[s];
2169 } while (++s < ic->sectors_per_block);
2172 static void do_journal_write(struct dm_integrity_c *ic, unsigned write_start,
2173 unsigned write_sections, bool from_replay)
2176 struct journal_completion comp;
2177 struct blk_plug plug;
2179 blk_start_plug(&plug);
2182 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
2183 init_completion(&comp.comp);
2186 for (n = 0; n < write_sections; n++, i++, wraparound_section(ic, &i)) {
2187 #ifndef INTERNAL_VERIFY
2188 if (unlikely(from_replay))
2190 rw_section_mac(ic, i, false);
2191 for (j = 0; j < ic->journal_section_entries; j++) {
2192 struct journal_entry *je = access_journal_entry(ic, i, j);
2193 sector_t sec, area, offset;
2194 unsigned k, l, next_loop;
2195 sector_t metadata_block;
2196 unsigned metadata_offset;
2197 struct journal_io *io;
2199 if (journal_entry_is_unused(je))
2201 BUG_ON(unlikely(journal_entry_is_inprogress(je)) && !from_replay);
2202 sec = journal_entry_get_sector(je);
2203 if (unlikely(from_replay)) {
2204 if (unlikely(sec & (unsigned)(ic->sectors_per_block - 1))) {
2205 dm_integrity_io_error(ic, "invalid sector in journal", -EIO);
2206 sec &= ~(sector_t)(ic->sectors_per_block - 1);
2209 get_area_and_offset(ic, sec, &area, &offset);
2210 restore_last_bytes(ic, access_journal_data(ic, i, j), je);
2211 for (k = j + 1; k < ic->journal_section_entries; k++) {
2212 struct journal_entry *je2 = access_journal_entry(ic, i, k);
2213 sector_t sec2, area2, offset2;
2214 if (journal_entry_is_unused(je2))
2216 BUG_ON(unlikely(journal_entry_is_inprogress(je2)) && !from_replay);
2217 sec2 = journal_entry_get_sector(je2);
2218 get_area_and_offset(ic, sec2, &area2, &offset2);
2219 if (area2 != area || offset2 != offset + ((k - j) << ic->sb->log2_sectors_per_block))
2221 restore_last_bytes(ic, access_journal_data(ic, i, k), je2);
2225 io = mempool_alloc(&ic->journal_io_mempool, GFP_NOIO);
2227 io->range.logical_sector = sec;
2228 io->range.n_sectors = (k - j) << ic->sb->log2_sectors_per_block;
2230 spin_lock_irq(&ic->endio_wait.lock);
2231 add_new_range_and_wait(ic, &io->range);
2233 if (likely(!from_replay)) {
2234 struct journal_node *section_node = &ic->journal_tree[i * ic->journal_section_entries];
2236 /* don't write if there is newer committed sector */
2237 while (j < k && find_newer_committed_node(ic, §ion_node[j])) {
2238 struct journal_entry *je2 = access_journal_entry(ic, i, j);
2240 journal_entry_set_unused(je2);
2241 remove_journal_node(ic, §ion_node[j]);
2243 sec += ic->sectors_per_block;
2244 offset += ic->sectors_per_block;
2246 while (j < k && find_newer_committed_node(ic, §ion_node[k - 1])) {
2247 struct journal_entry *je2 = access_journal_entry(ic, i, k - 1);
2249 journal_entry_set_unused(je2);
2250 remove_journal_node(ic, §ion_node[k - 1]);
2254 remove_range_unlocked(ic, &io->range);
2255 spin_unlock_irq(&ic->endio_wait.lock);
2256 mempool_free(io, &ic->journal_io_mempool);
2259 for (l = j; l < k; l++) {
2260 remove_journal_node(ic, §ion_node[l]);
2263 spin_unlock_irq(&ic->endio_wait.lock);
2265 metadata_block = get_metadata_sector_and_offset(ic, area, offset, &metadata_offset);
2266 for (l = j; l < k; l++) {
2268 struct journal_entry *je2 = access_journal_entry(ic, i, l);
2271 #ifndef INTERNAL_VERIFY
2272 unlikely(from_replay) &&
2274 ic->internal_hash) {
2275 char test_tag[max_t(size_t, HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)];
2277 integrity_sector_checksum(ic, sec + ((l - j) << ic->sb->log2_sectors_per_block),
2278 (char *)access_journal_data(ic, i, l), test_tag);
2279 if (unlikely(memcmp(test_tag, journal_entry_tag(ic, je2), ic->tag_size)))
2280 dm_integrity_io_error(ic, "tag mismatch when replaying journal", -EILSEQ);
2283 journal_entry_set_unused(je2);
2284 r = dm_integrity_rw_tag(ic, journal_entry_tag(ic, je2), &metadata_block, &metadata_offset,
2285 ic->tag_size, TAG_WRITE);
2287 dm_integrity_io_error(ic, "reading tags", r);
2291 atomic_inc(&comp.in_flight);
2292 copy_from_journal(ic, i, j << ic->sb->log2_sectors_per_block,
2293 (k - j) << ic->sb->log2_sectors_per_block,
2294 get_data_sector(ic, area, offset),
2295 complete_copy_from_journal, io);
2301 dm_bufio_write_dirty_buffers_async(ic->bufio);
2303 blk_finish_plug(&plug);
2305 complete_journal_op(&comp);
2306 wait_for_completion_io(&comp.comp);
2308 dm_integrity_flush_buffers(ic);
2311 static void integrity_writer(struct work_struct *w)
2313 struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, writer_work);
2314 unsigned write_start, write_sections;
2316 unsigned prev_free_sectors;
2318 /* the following test is not needed, but it tests the replay code */
2319 if (READ_ONCE(ic->suspending) && !ic->meta_dev)
2322 spin_lock_irq(&ic->endio_wait.lock);
2323 write_start = ic->committed_section;
2324 write_sections = ic->n_committed_sections;
2325 spin_unlock_irq(&ic->endio_wait.lock);
2327 if (!write_sections)
2330 do_journal_write(ic, write_start, write_sections, false);
2332 spin_lock_irq(&ic->endio_wait.lock);
2334 ic->committed_section += write_sections;
2335 wraparound_section(ic, &ic->committed_section);
2336 ic->n_committed_sections -= write_sections;
2338 prev_free_sectors = ic->free_sectors;
2339 ic->free_sectors += write_sections * ic->journal_section_entries;
2340 if (unlikely(!prev_free_sectors))
2341 wake_up_locked(&ic->endio_wait);
2343 spin_unlock_irq(&ic->endio_wait.lock);
2346 static void recalc_write_super(struct dm_integrity_c *ic)
2350 dm_integrity_flush_buffers(ic);
2351 if (dm_integrity_failed(ic))
2354 r = sync_rw_sb(ic, REQ_OP_WRITE, 0);
2356 dm_integrity_io_error(ic, "writing superblock", r);
2359 static void integrity_recalc(struct work_struct *w)
2361 struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, recalc_work);
2362 struct dm_integrity_range range;
2363 struct dm_io_request io_req;
2364 struct dm_io_region io_loc;
2365 sector_t area, offset;
2366 sector_t metadata_block;
2367 unsigned metadata_offset;
2368 sector_t logical_sector, n_sectors;
2372 unsigned super_counter = 0;
2374 DEBUG_print("start recalculation... (position %llx)\n", le64_to_cpu(ic->sb->recalc_sector));
2376 spin_lock_irq(&ic->endio_wait.lock);
2380 if (unlikely(READ_ONCE(ic->suspending)))
2383 range.logical_sector = le64_to_cpu(ic->sb->recalc_sector);
2384 if (unlikely(range.logical_sector >= ic->provided_data_sectors)) {
2385 if (ic->mode == 'B') {
2386 DEBUG_print("queue_delayed_work: bitmap_flush_work\n");
2387 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0);
2392 get_area_and_offset(ic, range.logical_sector, &area, &offset);
2393 range.n_sectors = min((sector_t)RECALC_SECTORS, ic->provided_data_sectors - range.logical_sector);
2395 range.n_sectors = min(range.n_sectors, ((sector_t)1U << ic->sb->log2_interleave_sectors) - (unsigned)offset);
2397 add_new_range_and_wait(ic, &range);
2398 spin_unlock_irq(&ic->endio_wait.lock);
2399 logical_sector = range.logical_sector;
2400 n_sectors = range.n_sectors;
2402 if (ic->mode == 'B') {
2403 if (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector, n_sectors, BITMAP_OP_TEST_ALL_CLEAR)) {
2404 goto advance_and_next;
2406 while (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector,
2407 ic->sectors_per_block, BITMAP_OP_TEST_ALL_CLEAR)) {
2408 logical_sector += ic->sectors_per_block;
2409 n_sectors -= ic->sectors_per_block;
2412 while (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector + n_sectors - ic->sectors_per_block,
2413 ic->sectors_per_block, BITMAP_OP_TEST_ALL_CLEAR)) {
2414 n_sectors -= ic->sectors_per_block;
2417 get_area_and_offset(ic, logical_sector, &area, &offset);
2420 DEBUG_print("recalculating: %lx, %lx\n", logical_sector, n_sectors);
2422 if (unlikely(++super_counter == RECALC_WRITE_SUPER)) {
2423 recalc_write_super(ic);
2424 if (ic->mode == 'B') {
2425 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, ic->bitmap_flush_interval);
2430 if (unlikely(dm_integrity_failed(ic)))
2433 io_req.bi_op = REQ_OP_READ;
2434 io_req.bi_op_flags = 0;
2435 io_req.mem.type = DM_IO_VMA;
2436 io_req.mem.ptr.addr = ic->recalc_buffer;
2437 io_req.notify.fn = NULL;
2438 io_req.client = ic->io;
2439 io_loc.bdev = ic->dev->bdev;
2440 io_loc.sector = get_data_sector(ic, area, offset);
2441 io_loc.count = n_sectors;
2443 r = dm_io(&io_req, 1, &io_loc, NULL);
2445 dm_integrity_io_error(ic, "reading data", r);
2449 t = ic->recalc_tags;
2450 for (i = 0; i < n_sectors; i += ic->sectors_per_block) {
2451 integrity_sector_checksum(ic, logical_sector + i, ic->recalc_buffer + (i << SECTOR_SHIFT), t);
2455 metadata_block = get_metadata_sector_and_offset(ic, area, offset, &metadata_offset);
2457 r = dm_integrity_rw_tag(ic, ic->recalc_tags, &metadata_block, &metadata_offset, t - ic->recalc_tags, TAG_WRITE);
2459 dm_integrity_io_error(ic, "writing tags", r);
2466 spin_lock_irq(&ic->endio_wait.lock);
2467 remove_range_unlocked(ic, &range);
2468 ic->sb->recalc_sector = cpu_to_le64(range.logical_sector + range.n_sectors);
2472 remove_range(ic, &range);
2476 spin_unlock_irq(&ic->endio_wait.lock);
2478 recalc_write_super(ic);
2481 static void bitmap_block_work(struct work_struct *w)
2483 struct bitmap_block_status *bbs = container_of(w, struct bitmap_block_status, work);
2484 struct dm_integrity_c *ic = bbs->ic;
2486 struct bio_list bio_queue;
2487 struct bio_list waiting;
2489 bio_list_init(&waiting);
2491 spin_lock(&bbs->bio_queue_lock);
2492 bio_queue = bbs->bio_queue;
2493 bio_list_init(&bbs->bio_queue);
2494 spin_unlock(&bbs->bio_queue_lock);
2496 while ((bio = bio_list_pop(&bio_queue))) {
2497 struct dm_integrity_io *dio;
2499 dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
2501 if (block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector,
2502 dio->range.n_sectors, BITMAP_OP_TEST_ALL_SET)) {
2503 remove_range(ic, &dio->range);
2504 INIT_WORK(&dio->work, integrity_bio_wait);
2505 queue_work(ic->offload_wq, &dio->work);
2507 block_bitmap_op(ic, ic->journal, dio->range.logical_sector,
2508 dio->range.n_sectors, BITMAP_OP_SET);
2509 bio_list_add(&waiting, bio);
2513 if (bio_list_empty(&waiting))
2516 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC,
2517 bbs->idx * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT),
2518 BITMAP_BLOCK_SIZE >> SECTOR_SHIFT, NULL);
2520 while ((bio = bio_list_pop(&waiting))) {
2521 struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
2523 block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector,
2524 dio->range.n_sectors, BITMAP_OP_SET);
2526 remove_range(ic, &dio->range);
2527 INIT_WORK(&dio->work, integrity_bio_wait);
2528 queue_work(ic->offload_wq, &dio->work);
2531 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, ic->bitmap_flush_interval);
2534 static void bitmap_flush_work(struct work_struct *work)
2536 struct dm_integrity_c *ic = container_of(work, struct dm_integrity_c, bitmap_flush_work.work);
2537 struct dm_integrity_range range;
2538 unsigned long limit;
2541 dm_integrity_flush_buffers(ic);
2543 range.logical_sector = 0;
2544 range.n_sectors = ic->provided_data_sectors;
2546 spin_lock_irq(&ic->endio_wait.lock);
2547 add_new_range_and_wait(ic, &range);
2548 spin_unlock_irq(&ic->endio_wait.lock);
2550 dm_integrity_flush_buffers(ic);
2552 blkdev_issue_flush(ic->dev->bdev, GFP_NOIO, NULL);
2554 limit = ic->provided_data_sectors;
2555 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) {
2556 limit = le64_to_cpu(ic->sb->recalc_sector)
2557 >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit)
2558 << (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
2560 /*DEBUG_print("zeroing journal\n");*/
2561 block_bitmap_op(ic, ic->journal, 0, limit, BITMAP_OP_CLEAR);
2562 block_bitmap_op(ic, ic->may_write_bitmap, 0, limit, BITMAP_OP_CLEAR);
2564 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, 0,
2565 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
2567 spin_lock_irq(&ic->endio_wait.lock);
2568 remove_range_unlocked(ic, &range);
2569 while (unlikely((bio = bio_list_pop(&ic->synchronous_bios)) != NULL)) {
2571 spin_unlock_irq(&ic->endio_wait.lock);
2572 spin_lock_irq(&ic->endio_wait.lock);
2574 spin_unlock_irq(&ic->endio_wait.lock);
2578 static void init_journal(struct dm_integrity_c *ic, unsigned start_section,
2579 unsigned n_sections, unsigned char commit_seq)
2586 for (n = 0; n < n_sections; n++) {
2587 i = start_section + n;
2588 wraparound_section(ic, &i);
2589 for (j = 0; j < ic->journal_section_sectors; j++) {
2590 struct journal_sector *js = access_journal(ic, i, j);
2591 memset(&js->entries, 0, JOURNAL_SECTOR_DATA);
2592 js->commit_id = dm_integrity_commit_id(ic, i, j, commit_seq);
2594 for (j = 0; j < ic->journal_section_entries; j++) {
2595 struct journal_entry *je = access_journal_entry(ic, i, j);
2596 journal_entry_set_unused(je);
2600 write_journal(ic, start_section, n_sections);
2603 static int find_commit_seq(struct dm_integrity_c *ic, unsigned i, unsigned j, commit_id_t id)
2606 for (k = 0; k < N_COMMIT_IDS; k++) {
2607 if (dm_integrity_commit_id(ic, i, j, k) == id)
2610 dm_integrity_io_error(ic, "journal commit id", -EIO);
2614 static void replay_journal(struct dm_integrity_c *ic)
2617 bool used_commit_ids[N_COMMIT_IDS];
2618 unsigned max_commit_id_sections[N_COMMIT_IDS];
2619 unsigned write_start, write_sections;
2620 unsigned continue_section;
2622 unsigned char unused, last_used, want_commit_seq;
2624 if (ic->mode == 'R')
2627 if (ic->journal_uptodate)
2633 if (!ic->just_formatted) {
2634 DEBUG_print("reading journal\n");
2635 rw_journal(ic, REQ_OP_READ, 0, 0, ic->journal_sections, NULL);
2637 DEBUG_bytes(lowmem_page_address(ic->journal_io[0].page), 64, "read journal");
2638 if (ic->journal_io) {
2639 struct journal_completion crypt_comp;
2641 init_completion(&crypt_comp.comp);
2642 crypt_comp.in_flight = (atomic_t)ATOMIC_INIT(0);
2643 encrypt_journal(ic, false, 0, ic->journal_sections, &crypt_comp);
2644 wait_for_completion(&crypt_comp.comp);
2646 DEBUG_bytes(lowmem_page_address(ic->journal[0].page), 64, "decrypted journal");
2649 if (dm_integrity_failed(ic))
2652 journal_empty = true;
2653 memset(used_commit_ids, 0, sizeof used_commit_ids);
2654 memset(max_commit_id_sections, 0, sizeof max_commit_id_sections);
2655 for (i = 0; i < ic->journal_sections; i++) {
2656 for (j = 0; j < ic->journal_section_sectors; j++) {
2658 struct journal_sector *js = access_journal(ic, i, j);
2659 k = find_commit_seq(ic, i, j, js->commit_id);
2662 used_commit_ids[k] = true;
2663 max_commit_id_sections[k] = i;
2665 if (journal_empty) {
2666 for (j = 0; j < ic->journal_section_entries; j++) {
2667 struct journal_entry *je = access_journal_entry(ic, i, j);
2668 if (!journal_entry_is_unused(je)) {
2669 journal_empty = false;
2676 if (!used_commit_ids[N_COMMIT_IDS - 1]) {
2677 unused = N_COMMIT_IDS - 1;
2678 while (unused && !used_commit_ids[unused - 1])
2681 for (unused = 0; unused < N_COMMIT_IDS; unused++)
2682 if (!used_commit_ids[unused])
2684 if (unused == N_COMMIT_IDS) {
2685 dm_integrity_io_error(ic, "journal commit ids", -EIO);
2689 DEBUG_print("first unused commit seq %d [%d,%d,%d,%d]\n",
2690 unused, used_commit_ids[0], used_commit_ids[1],
2691 used_commit_ids[2], used_commit_ids[3]);
2693 last_used = prev_commit_seq(unused);
2694 want_commit_seq = prev_commit_seq(last_used);
2696 if (!used_commit_ids[want_commit_seq] && used_commit_ids[prev_commit_seq(want_commit_seq)])
2697 journal_empty = true;
2699 write_start = max_commit_id_sections[last_used] + 1;
2700 if (unlikely(write_start >= ic->journal_sections))
2701 want_commit_seq = next_commit_seq(want_commit_seq);
2702 wraparound_section(ic, &write_start);
2705 for (write_sections = 0; write_sections < ic->journal_sections; write_sections++) {
2706 for (j = 0; j < ic->journal_section_sectors; j++) {
2707 struct journal_sector *js = access_journal(ic, i, j);
2709 if (js->commit_id != dm_integrity_commit_id(ic, i, j, want_commit_seq)) {
2711 * This could be caused by crash during writing.
2712 * We won't replay the inconsistent part of the
2715 DEBUG_print("commit id mismatch at position (%u, %u): %d != %d\n",
2716 i, j, find_commit_seq(ic, i, j, js->commit_id), want_commit_seq);
2721 if (unlikely(i >= ic->journal_sections))
2722 want_commit_seq = next_commit_seq(want_commit_seq);
2723 wraparound_section(ic, &i);
2727 if (!journal_empty) {
2728 DEBUG_print("replaying %u sections, starting at %u, commit seq %d\n",
2729 write_sections, write_start, want_commit_seq);
2730 do_journal_write(ic, write_start, write_sections, true);
2733 if (write_sections == ic->journal_sections && (ic->mode == 'J' || journal_empty)) {
2734 continue_section = write_start;
2735 ic->commit_seq = want_commit_seq;
2736 DEBUG_print("continuing from section %u, commit seq %d\n", write_start, ic->commit_seq);
2739 unsigned char erase_seq;
2741 DEBUG_print("clearing journal\n");
2743 erase_seq = prev_commit_seq(prev_commit_seq(last_used));
2745 init_journal(ic, s, 1, erase_seq);
2747 wraparound_section(ic, &s);
2748 if (ic->journal_sections >= 2) {
2749 init_journal(ic, s, ic->journal_sections - 2, erase_seq);
2750 s += ic->journal_sections - 2;
2751 wraparound_section(ic, &s);
2752 init_journal(ic, s, 1, erase_seq);
2755 continue_section = 0;
2756 ic->commit_seq = next_commit_seq(erase_seq);
2759 ic->committed_section = continue_section;
2760 ic->n_committed_sections = 0;
2762 ic->uncommitted_section = continue_section;
2763 ic->n_uncommitted_sections = 0;
2765 ic->free_section = continue_section;
2766 ic->free_section_entry = 0;
2767 ic->free_sectors = ic->journal_entries;
2769 ic->journal_tree_root = RB_ROOT;
2770 for (i = 0; i < ic->journal_entries; i++)
2771 init_journal_node(&ic->journal_tree[i]);
2774 static void dm_integrity_enter_synchronous_mode(struct dm_integrity_c *ic)
2776 DEBUG_print("dm_integrity_enter_synchronous_mode\n");
2778 if (ic->mode == 'B') {
2779 ic->bitmap_flush_interval = msecs_to_jiffies(10) + 1;
2780 ic->synchronous_mode = 1;
2782 cancel_delayed_work_sync(&ic->bitmap_flush_work);
2783 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0);
2784 flush_workqueue(ic->commit_wq);
2788 static int dm_integrity_reboot(struct notifier_block *n, unsigned long code, void *x)
2790 struct dm_integrity_c *ic = container_of(n, struct dm_integrity_c, reboot_notifier);
2792 DEBUG_print("dm_integrity_reboot\n");
2794 dm_integrity_enter_synchronous_mode(ic);
2799 static void dm_integrity_postsuspend(struct dm_target *ti)
2801 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
2804 WARN_ON(unregister_reboot_notifier(&ic->reboot_notifier));
2806 del_timer_sync(&ic->autocommit_timer);
2808 WRITE_ONCE(ic->suspending, 1);
2811 drain_workqueue(ic->recalc_wq);
2813 if (ic->mode == 'B')
2814 cancel_delayed_work_sync(&ic->bitmap_flush_work);
2816 queue_work(ic->commit_wq, &ic->commit_work);
2817 drain_workqueue(ic->commit_wq);
2819 if (ic->mode == 'J') {
2821 queue_work(ic->writer_wq, &ic->writer_work);
2822 drain_workqueue(ic->writer_wq);
2823 dm_integrity_flush_buffers(ic);
2826 if (ic->mode == 'B') {
2827 dm_integrity_flush_buffers(ic);
2829 /* set to 0 to test bitmap replay code */
2830 init_journal(ic, 0, ic->journal_sections, 0);
2831 ic->sb->flags &= ~cpu_to_le32(SB_FLAG_DIRTY_BITMAP);
2832 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
2834 dm_integrity_io_error(ic, "writing superblock", r);
2838 WRITE_ONCE(ic->suspending, 0);
2840 BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress));
2842 ic->journal_uptodate = true;
2845 static void dm_integrity_resume(struct dm_target *ti)
2847 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
2849 DEBUG_print("resume\n");
2851 if (ic->sb->flags & cpu_to_le32(SB_FLAG_DIRTY_BITMAP)) {
2852 DEBUG_print("resume dirty_bitmap\n");
2853 rw_journal_sectors(ic, REQ_OP_READ, 0, 0,
2854 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
2855 if (ic->mode == 'B') {
2856 if (ic->sb->log2_blocks_per_bitmap_bit == ic->log2_blocks_per_bitmap_bit) {
2857 block_bitmap_copy(ic, ic->recalc_bitmap, ic->journal);
2858 block_bitmap_copy(ic, ic->may_write_bitmap, ic->journal);
2859 if (!block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors,
2860 BITMAP_OP_TEST_ALL_CLEAR)) {
2861 ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
2862 ic->sb->recalc_sector = cpu_to_le64(0);
2865 DEBUG_print("non-matching blocks_per_bitmap_bit: %u, %u\n",
2866 ic->sb->log2_blocks_per_bitmap_bit, ic->log2_blocks_per_bitmap_bit);
2867 ic->sb->log2_blocks_per_bitmap_bit = ic->log2_blocks_per_bitmap_bit;
2868 block_bitmap_op(ic, ic->recalc_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_SET);
2869 block_bitmap_op(ic, ic->may_write_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_SET);
2870 block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_SET);
2871 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, 0,
2872 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
2873 ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
2874 ic->sb->recalc_sector = cpu_to_le64(0);
2877 if (!(ic->sb->log2_blocks_per_bitmap_bit == ic->log2_blocks_per_bitmap_bit &&
2878 block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_TEST_ALL_CLEAR))) {
2879 ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
2880 ic->sb->recalc_sector = cpu_to_le64(0);
2882 init_journal(ic, 0, ic->journal_sections, 0);
2884 ic->sb->flags &= ~cpu_to_le32(SB_FLAG_DIRTY_BITMAP);
2886 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
2888 dm_integrity_io_error(ic, "writing superblock", r);
2891 if (ic->mode == 'B') {
2892 ic->sb->flags |= cpu_to_le32(SB_FLAG_DIRTY_BITMAP);
2893 ic->sb->log2_blocks_per_bitmap_bit = ic->log2_blocks_per_bitmap_bit;
2894 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
2896 dm_integrity_io_error(ic, "writing superblock", r);
2898 block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR);
2899 block_bitmap_op(ic, ic->recalc_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR);
2900 block_bitmap_op(ic, ic->may_write_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR);
2901 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING) &&
2902 le64_to_cpu(ic->sb->recalc_sector) < ic->provided_data_sectors) {
2903 block_bitmap_op(ic, ic->journal, le64_to_cpu(ic->sb->recalc_sector),
2904 ic->provided_data_sectors - le64_to_cpu(ic->sb->recalc_sector), BITMAP_OP_SET);
2905 block_bitmap_op(ic, ic->recalc_bitmap, le64_to_cpu(ic->sb->recalc_sector),
2906 ic->provided_data_sectors - le64_to_cpu(ic->sb->recalc_sector), BITMAP_OP_SET);
2907 block_bitmap_op(ic, ic->may_write_bitmap, le64_to_cpu(ic->sb->recalc_sector),
2908 ic->provided_data_sectors - le64_to_cpu(ic->sb->recalc_sector), BITMAP_OP_SET);
2910 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, 0,
2911 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
2915 DEBUG_print("testing recalc: %x\n", ic->sb->flags);
2916 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) {
2917 __u64 recalc_pos = le64_to_cpu(ic->sb->recalc_sector);
2918 DEBUG_print("recalc pos: %lx / %lx\n", (long)recalc_pos, ic->provided_data_sectors);
2919 if (recalc_pos < ic->provided_data_sectors) {
2920 queue_work(ic->recalc_wq, &ic->recalc_work);
2921 } else if (recalc_pos > ic->provided_data_sectors) {
2922 ic->sb->recalc_sector = cpu_to_le64(ic->provided_data_sectors);
2923 recalc_write_super(ic);
2927 ic->reboot_notifier.notifier_call = dm_integrity_reboot;
2928 ic->reboot_notifier.next = NULL;
2929 ic->reboot_notifier.priority = INT_MAX - 1; /* be notified after md and before hardware drivers */
2930 WARN_ON(register_reboot_notifier(&ic->reboot_notifier));
2933 /* set to 1 to stress test synchronous mode */
2934 dm_integrity_enter_synchronous_mode(ic);
2938 static void dm_integrity_status(struct dm_target *ti, status_type_t type,
2939 unsigned status_flags, char *result, unsigned maxlen)
2941 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
2946 case STATUSTYPE_INFO:
2948 (unsigned long long)atomic64_read(&ic->number_of_mismatches),
2949 (unsigned long long)ic->provided_data_sectors);
2950 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))
2951 DMEMIT(" %llu", (unsigned long long)le64_to_cpu(ic->sb->recalc_sector));
2956 case STATUSTYPE_TABLE: {
2957 __u64 watermark_percentage = (__u64)(ic->journal_entries - ic->free_sectors_threshold) * 100;
2958 watermark_percentage += ic->journal_entries / 2;
2959 do_div(watermark_percentage, ic->journal_entries);
2961 arg_count += !!ic->meta_dev;
2962 arg_count += ic->sectors_per_block != 1;
2963 arg_count += !!(ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING));
2964 arg_count += ic->mode == 'J';
2965 arg_count += ic->mode == 'J';
2966 arg_count += ic->mode == 'B';
2967 arg_count += ic->mode == 'B';
2968 arg_count += !!ic->internal_hash_alg.alg_string;
2969 arg_count += !!ic->journal_crypt_alg.alg_string;
2970 arg_count += !!ic->journal_mac_alg.alg_string;
2971 arg_count += (ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_PADDING)) != 0;
2972 DMEMIT("%s %llu %u %c %u", ic->dev->name, (unsigned long long)ic->start,
2973 ic->tag_size, ic->mode, arg_count);
2975 DMEMIT(" meta_device:%s", ic->meta_dev->name);
2976 if (ic->sectors_per_block != 1)
2977 DMEMIT(" block_size:%u", ic->sectors_per_block << SECTOR_SHIFT);
2978 if (ic->recalculate_flag)
2979 DMEMIT(" recalculate");
2980 DMEMIT(" journal_sectors:%u", ic->initial_sectors - SB_SECTORS);
2981 DMEMIT(" interleave_sectors:%u", 1U << ic->sb->log2_interleave_sectors);
2982 DMEMIT(" buffer_sectors:%u", 1U << ic->log2_buffer_sectors);
2983 if (ic->mode == 'J') {
2984 DMEMIT(" journal_watermark:%u", (unsigned)watermark_percentage);
2985 DMEMIT(" commit_time:%u", ic->autocommit_msec);
2987 if (ic->mode == 'B') {
2988 DMEMIT(" sectors_per_bit:%llu", (unsigned long long)ic->sectors_per_block << ic->log2_blocks_per_bitmap_bit);
2989 DMEMIT(" bitmap_flush_interval:%u", jiffies_to_msecs(ic->bitmap_flush_interval));
2991 if ((ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_PADDING)) != 0)
2992 DMEMIT(" fix_padding");
2994 #define EMIT_ALG(a, n) \
2996 if (ic->a.alg_string) { \
2997 DMEMIT(" %s:%s", n, ic->a.alg_string); \
2998 if (ic->a.key_string) \
2999 DMEMIT(":%s", ic->a.key_string);\
3002 EMIT_ALG(internal_hash_alg, "internal_hash");
3003 EMIT_ALG(journal_crypt_alg, "journal_crypt");
3004 EMIT_ALG(journal_mac_alg, "journal_mac");
3010 static int dm_integrity_iterate_devices(struct dm_target *ti,
3011 iterate_devices_callout_fn fn, void *data)
3013 struct dm_integrity_c *ic = ti->private;
3016 return fn(ti, ic->dev, ic->start + ic->initial_sectors + ic->metadata_run, ti->len, data);
3018 return fn(ti, ic->dev, 0, ti->len, data);
3021 static void dm_integrity_io_hints(struct dm_target *ti, struct queue_limits *limits)
3023 struct dm_integrity_c *ic = ti->private;
3025 if (ic->sectors_per_block > 1) {
3026 limits->logical_block_size = ic->sectors_per_block << SECTOR_SHIFT;
3027 limits->physical_block_size = ic->sectors_per_block << SECTOR_SHIFT;
3028 blk_limits_io_min(limits, ic->sectors_per_block << SECTOR_SHIFT);
3032 static void calculate_journal_section_size(struct dm_integrity_c *ic)
3034 unsigned sector_space = JOURNAL_SECTOR_DATA;
3036 ic->journal_sections = le32_to_cpu(ic->sb->journal_sections);
3037 ic->journal_entry_size = roundup(offsetof(struct journal_entry, last_bytes[ic->sectors_per_block]) + ic->tag_size,
3038 JOURNAL_ENTRY_ROUNDUP);
3040 if (ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC))
3041 sector_space -= JOURNAL_MAC_PER_SECTOR;
3042 ic->journal_entries_per_sector = sector_space / ic->journal_entry_size;
3043 ic->journal_section_entries = ic->journal_entries_per_sector * JOURNAL_BLOCK_SECTORS;
3044 ic->journal_section_sectors = (ic->journal_section_entries << ic->sb->log2_sectors_per_block) + JOURNAL_BLOCK_SECTORS;
3045 ic->journal_entries = ic->journal_section_entries * ic->journal_sections;
3048 static int calculate_device_limits(struct dm_integrity_c *ic)
3050 __u64 initial_sectors;
3052 calculate_journal_section_size(ic);
3053 initial_sectors = SB_SECTORS + (__u64)ic->journal_section_sectors * ic->journal_sections;
3054 if (initial_sectors + METADATA_PADDING_SECTORS >= ic->meta_device_sectors || initial_sectors > UINT_MAX)
3056 ic->initial_sectors = initial_sectors;
3058 if (!ic->meta_dev) {
3059 sector_t last_sector, last_area, last_offset;
3061 /* we have to maintain excessive padding for compatibility with existing volumes */
3062 __u64 metadata_run_padding =
3063 ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_PADDING) ?
3064 (__u64)(METADATA_PADDING_SECTORS << SECTOR_SHIFT) :
3065 (__u64)(1 << SECTOR_SHIFT << METADATA_PADDING_SECTORS);
3067 ic->metadata_run = round_up((__u64)ic->tag_size << (ic->sb->log2_interleave_sectors - ic->sb->log2_sectors_per_block),
3068 metadata_run_padding) >> SECTOR_SHIFT;
3069 if (!(ic->metadata_run & (ic->metadata_run - 1)))
3070 ic->log2_metadata_run = __ffs(ic->metadata_run);
3072 ic->log2_metadata_run = -1;
3074 get_area_and_offset(ic, ic->provided_data_sectors - 1, &last_area, &last_offset);
3075 last_sector = get_data_sector(ic, last_area, last_offset);
3076 if (last_sector < ic->start || last_sector >= ic->meta_device_sectors)
3079 __u64 meta_size = (ic->provided_data_sectors >> ic->sb->log2_sectors_per_block) * ic->tag_size;
3080 meta_size = (meta_size + ((1U << (ic->log2_buffer_sectors + SECTOR_SHIFT)) - 1))
3081 >> (ic->log2_buffer_sectors + SECTOR_SHIFT);
3082 meta_size <<= ic->log2_buffer_sectors;
3083 if (ic->initial_sectors + meta_size < ic->initial_sectors ||
3084 ic->initial_sectors + meta_size > ic->meta_device_sectors)
3086 ic->metadata_run = 1;
3087 ic->log2_metadata_run = 0;
3093 static int initialize_superblock(struct dm_integrity_c *ic, unsigned journal_sectors, unsigned interleave_sectors)
3095 unsigned journal_sections;
3098 memset(ic->sb, 0, SB_SECTORS << SECTOR_SHIFT);
3099 memcpy(ic->sb->magic, SB_MAGIC, 8);
3100 ic->sb->integrity_tag_size = cpu_to_le16(ic->tag_size);
3101 ic->sb->log2_sectors_per_block = __ffs(ic->sectors_per_block);
3102 if (ic->journal_mac_alg.alg_string)
3103 ic->sb->flags |= cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC);
3105 calculate_journal_section_size(ic);
3106 journal_sections = journal_sectors / ic->journal_section_sectors;
3107 if (!journal_sections)
3108 journal_sections = 1;
3110 if (!ic->meta_dev) {
3111 if (ic->fix_padding)
3112 ic->sb->flags |= cpu_to_le32(SB_FLAG_FIXED_PADDING);
3113 ic->sb->journal_sections = cpu_to_le32(journal_sections);
3114 if (!interleave_sectors)
3115 interleave_sectors = DEFAULT_INTERLEAVE_SECTORS;
3116 ic->sb->log2_interleave_sectors = __fls(interleave_sectors);
3117 ic->sb->log2_interleave_sectors = max((__u8)MIN_LOG2_INTERLEAVE_SECTORS, ic->sb->log2_interleave_sectors);
3118 ic->sb->log2_interleave_sectors = min((__u8)MAX_LOG2_INTERLEAVE_SECTORS, ic->sb->log2_interleave_sectors);
3120 ic->provided_data_sectors = 0;
3121 for (test_bit = fls64(ic->meta_device_sectors) - 1; test_bit >= 3; test_bit--) {
3122 __u64 prev_data_sectors = ic->provided_data_sectors;
3124 ic->provided_data_sectors |= (sector_t)1 << test_bit;
3125 if (calculate_device_limits(ic))
3126 ic->provided_data_sectors = prev_data_sectors;
3128 if (!ic->provided_data_sectors)
3131 ic->sb->log2_interleave_sectors = 0;
3132 ic->provided_data_sectors = ic->data_device_sectors;
3133 ic->provided_data_sectors &= ~(sector_t)(ic->sectors_per_block - 1);
3136 ic->sb->journal_sections = cpu_to_le32(0);
3137 for (test_bit = fls(journal_sections) - 1; test_bit >= 0; test_bit--) {
3138 __u32 prev_journal_sections = le32_to_cpu(ic->sb->journal_sections);
3139 __u32 test_journal_sections = prev_journal_sections | (1U << test_bit);
3140 if (test_journal_sections > journal_sections)
3142 ic->sb->journal_sections = cpu_to_le32(test_journal_sections);
3143 if (calculate_device_limits(ic))
3144 ic->sb->journal_sections = cpu_to_le32(prev_journal_sections);
3147 if (!le32_to_cpu(ic->sb->journal_sections)) {
3148 if (ic->log2_buffer_sectors > 3) {
3149 ic->log2_buffer_sectors--;
3150 goto try_smaller_buffer;
3156 ic->sb->provided_data_sectors = cpu_to_le64(ic->provided_data_sectors);
3163 static void dm_integrity_set(struct dm_target *ti, struct dm_integrity_c *ic)
3165 struct gendisk *disk = dm_disk(dm_table_get_md(ti->table));
3166 struct blk_integrity bi;
3168 memset(&bi, 0, sizeof(bi));
3169 bi.profile = &dm_integrity_profile;
3170 bi.tuple_size = ic->tag_size;
3171 bi.tag_size = bi.tuple_size;
3172 bi.interval_exp = ic->sb->log2_sectors_per_block + SECTOR_SHIFT;
3174 blk_integrity_register(disk, &bi);
3175 blk_queue_max_integrity_segments(disk->queue, UINT_MAX);
3178 static void dm_integrity_free_page_list(struct page_list *pl)
3184 for (i = 0; pl[i].page; i++)
3185 __free_page(pl[i].page);
3189 static struct page_list *dm_integrity_alloc_page_list(unsigned n_pages)
3191 struct page_list *pl;
3194 pl = kvmalloc_array(n_pages + 1, sizeof(struct page_list), GFP_KERNEL | __GFP_ZERO);
3198 for (i = 0; i < n_pages; i++) {
3199 pl[i].page = alloc_page(GFP_KERNEL);
3201 dm_integrity_free_page_list(pl);
3205 pl[i - 1].next = &pl[i];
3213 static void dm_integrity_free_journal_scatterlist(struct dm_integrity_c *ic, struct scatterlist **sl)
3216 for (i = 0; i < ic->journal_sections; i++)
3221 static struct scatterlist **dm_integrity_alloc_journal_scatterlist(struct dm_integrity_c *ic,
3222 struct page_list *pl)
3224 struct scatterlist **sl;
3227 sl = kvmalloc_array(ic->journal_sections,
3228 sizeof(struct scatterlist *),
3229 GFP_KERNEL | __GFP_ZERO);
3233 for (i = 0; i < ic->journal_sections; i++) {
3234 struct scatterlist *s;
3235 unsigned start_index, start_offset;
3236 unsigned end_index, end_offset;
3240 page_list_location(ic, i, 0, &start_index, &start_offset);
3241 page_list_location(ic, i, ic->journal_section_sectors - 1,
3242 &end_index, &end_offset);
3244 n_pages = (end_index - start_index + 1);
3246 s = kvmalloc_array(n_pages, sizeof(struct scatterlist),
3249 dm_integrity_free_journal_scatterlist(ic, sl);
3253 sg_init_table(s, n_pages);
3254 for (idx = start_index; idx <= end_index; idx++) {
3255 char *va = lowmem_page_address(pl[idx].page);
3256 unsigned start = 0, end = PAGE_SIZE;
3257 if (idx == start_index)
3258 start = start_offset;
3259 if (idx == end_index)
3260 end = end_offset + (1 << SECTOR_SHIFT);
3261 sg_set_buf(&s[idx - start_index], va + start, end - start);
3270 static void free_alg(struct alg_spec *a)
3272 kzfree(a->alg_string);
3274 memset(a, 0, sizeof *a);
3277 static int get_alg_and_key(const char *arg, struct alg_spec *a, char **error, char *error_inval)
3283 a->alg_string = kstrdup(strchr(arg, ':') + 1, GFP_KERNEL);
3287 k = strchr(a->alg_string, ':');
3290 a->key_string = k + 1;
3291 if (strlen(a->key_string) & 1)
3294 a->key_size = strlen(a->key_string) / 2;
3295 a->key = kmalloc(a->key_size, GFP_KERNEL);
3298 if (hex2bin(a->key, a->key_string, a->key_size))
3304 *error = error_inval;
3307 *error = "Out of memory for an argument";
3311 static int get_mac(struct crypto_shash **hash, struct alg_spec *a, char **error,
3312 char *error_alg, char *error_key)
3316 if (a->alg_string) {
3317 *hash = crypto_alloc_shash(a->alg_string, 0, 0);
3318 if (IS_ERR(*hash)) {
3326 r = crypto_shash_setkey(*hash, a->key, a->key_size);
3331 } else if (crypto_shash_get_flags(*hash) & CRYPTO_TFM_NEED_KEY) {
3340 static int create_journal(struct dm_integrity_c *ic, char **error)
3344 __u64 journal_pages, journal_desc_size, journal_tree_size;
3345 unsigned char *crypt_data = NULL, *crypt_iv = NULL;
3346 struct skcipher_request *req = NULL;
3348 ic->commit_ids[0] = cpu_to_le64(0x1111111111111111ULL);
3349 ic->commit_ids[1] = cpu_to_le64(0x2222222222222222ULL);
3350 ic->commit_ids[2] = cpu_to_le64(0x3333333333333333ULL);
3351 ic->commit_ids[3] = cpu_to_le64(0x4444444444444444ULL);
3353 journal_pages = roundup((__u64)ic->journal_sections * ic->journal_section_sectors,
3354 PAGE_SIZE >> SECTOR_SHIFT) >> (PAGE_SHIFT - SECTOR_SHIFT);
3355 journal_desc_size = journal_pages * sizeof(struct page_list);
3356 if (journal_pages >= totalram_pages() - totalhigh_pages() || journal_desc_size > ULONG_MAX) {
3357 *error = "Journal doesn't fit into memory";
3361 ic->journal_pages = journal_pages;
3363 ic->journal = dm_integrity_alloc_page_list(ic->journal_pages);
3365 *error = "Could not allocate memory for journal";
3369 if (ic->journal_crypt_alg.alg_string) {
3370 unsigned ivsize, blocksize;
3371 struct journal_completion comp;
3374 ic->journal_crypt = crypto_alloc_skcipher(ic->journal_crypt_alg.alg_string, 0, 0);
3375 if (IS_ERR(ic->journal_crypt)) {
3376 *error = "Invalid journal cipher";
3377 r = PTR_ERR(ic->journal_crypt);
3378 ic->journal_crypt = NULL;
3381 ivsize = crypto_skcipher_ivsize(ic->journal_crypt);
3382 blocksize = crypto_skcipher_blocksize(ic->journal_crypt);
3384 if (ic->journal_crypt_alg.key) {
3385 r = crypto_skcipher_setkey(ic->journal_crypt, ic->journal_crypt_alg.key,
3386 ic->journal_crypt_alg.key_size);
3388 *error = "Error setting encryption key";
3392 DEBUG_print("cipher %s, block size %u iv size %u\n",
3393 ic->journal_crypt_alg.alg_string, blocksize, ivsize);
3395 ic->journal_io = dm_integrity_alloc_page_list(ic->journal_pages);
3396 if (!ic->journal_io) {
3397 *error = "Could not allocate memory for journal io";
3402 if (blocksize == 1) {
3403 struct scatterlist *sg;
3405 req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
3407 *error = "Could not allocate crypt request";
3412 crypt_iv = kzalloc(ivsize, GFP_KERNEL);
3414 *error = "Could not allocate iv";
3419 ic->journal_xor = dm_integrity_alloc_page_list(ic->journal_pages);
3420 if (!ic->journal_xor) {
3421 *error = "Could not allocate memory for journal xor";
3426 sg = kvmalloc_array(ic->journal_pages + 1,
3427 sizeof(struct scatterlist),
3430 *error = "Unable to allocate sg list";
3434 sg_init_table(sg, ic->journal_pages + 1);
3435 for (i = 0; i < ic->journal_pages; i++) {
3436 char *va = lowmem_page_address(ic->journal_xor[i].page);
3438 sg_set_buf(&sg[i], va, PAGE_SIZE);
3440 sg_set_buf(&sg[i], &ic->commit_ids, sizeof ic->commit_ids);
3442 skcipher_request_set_crypt(req, sg, sg,
3443 PAGE_SIZE * ic->journal_pages + sizeof ic->commit_ids, crypt_iv);
3444 init_completion(&comp.comp);
3445 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
3446 if (do_crypt(true, req, &comp))
3447 wait_for_completion(&comp.comp);
3449 r = dm_integrity_failed(ic);
3451 *error = "Unable to encrypt journal";
3454 DEBUG_bytes(lowmem_page_address(ic->journal_xor[0].page), 64, "xor data");
3456 crypto_free_skcipher(ic->journal_crypt);
3457 ic->journal_crypt = NULL;
3459 unsigned crypt_len = roundup(ivsize, blocksize);
3461 req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
3463 *error = "Could not allocate crypt request";
3468 crypt_iv = kmalloc(ivsize, GFP_KERNEL);
3470 *error = "Could not allocate iv";
3475 crypt_data = kmalloc(crypt_len, GFP_KERNEL);
3477 *error = "Unable to allocate crypt data";
3482 ic->journal_scatterlist = dm_integrity_alloc_journal_scatterlist(ic, ic->journal);
3483 if (!ic->journal_scatterlist) {
3484 *error = "Unable to allocate sg list";
3488 ic->journal_io_scatterlist = dm_integrity_alloc_journal_scatterlist(ic, ic->journal_io);
3489 if (!ic->journal_io_scatterlist) {
3490 *error = "Unable to allocate sg list";
3494 ic->sk_requests = kvmalloc_array(ic->journal_sections,
3495 sizeof(struct skcipher_request *),
3496 GFP_KERNEL | __GFP_ZERO);
3497 if (!ic->sk_requests) {
3498 *error = "Unable to allocate sk requests";
3502 for (i = 0; i < ic->journal_sections; i++) {
3503 struct scatterlist sg;
3504 struct skcipher_request *section_req;
3505 __u32 section_le = cpu_to_le32(i);
3507 memset(crypt_iv, 0x00, ivsize);
3508 memset(crypt_data, 0x00, crypt_len);
3509 memcpy(crypt_data, §ion_le, min((size_t)crypt_len, sizeof(section_le)));
3511 sg_init_one(&sg, crypt_data, crypt_len);
3512 skcipher_request_set_crypt(req, &sg, &sg, crypt_len, crypt_iv);
3513 init_completion(&comp.comp);
3514 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
3515 if (do_crypt(true, req, &comp))
3516 wait_for_completion(&comp.comp);
3518 r = dm_integrity_failed(ic);
3520 *error = "Unable to generate iv";
3524 section_req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
3526 *error = "Unable to allocate crypt request";
3530 section_req->iv = kmalloc_array(ivsize, 2,
3532 if (!section_req->iv) {
3533 skcipher_request_free(section_req);
3534 *error = "Unable to allocate iv";
3538 memcpy(section_req->iv + ivsize, crypt_data, ivsize);
3539 section_req->cryptlen = (size_t)ic->journal_section_sectors << SECTOR_SHIFT;
3540 ic->sk_requests[i] = section_req;
3541 DEBUG_bytes(crypt_data, ivsize, "iv(%u)", i);
3546 for (i = 0; i < N_COMMIT_IDS; i++) {
3549 for (j = 0; j < i; j++) {
3550 if (ic->commit_ids[j] == ic->commit_ids[i]) {
3551 ic->commit_ids[i] = cpu_to_le64(le64_to_cpu(ic->commit_ids[i]) + 1);
3552 goto retest_commit_id;
3555 DEBUG_print("commit id %u: %016llx\n", i, ic->commit_ids[i]);
3558 journal_tree_size = (__u64)ic->journal_entries * sizeof(struct journal_node);
3559 if (journal_tree_size > ULONG_MAX) {
3560 *error = "Journal doesn't fit into memory";
3564 ic->journal_tree = kvmalloc(journal_tree_size, GFP_KERNEL);
3565 if (!ic->journal_tree) {
3566 *error = "Could not allocate memory for journal tree";
3572 skcipher_request_free(req);
3578 * Construct a integrity mapping
3582 * offset from the start of the device
3584 * D - direct writes, J - journal writes, B - bitmap mode, R - recovery mode
3585 * number of optional arguments
3586 * optional arguments:
3588 * interleave_sectors
3595 * bitmap_flush_interval
3601 static int dm_integrity_ctr(struct dm_target *ti, unsigned argc, char **argv)
3603 struct dm_integrity_c *ic;
3606 unsigned extra_args;
3607 struct dm_arg_set as;
3608 static const struct dm_arg _args[] = {
3609 {0, 9, "Invalid number of feature args"},
3611 unsigned journal_sectors, interleave_sectors, buffer_sectors, journal_watermark, sync_msec;
3612 bool should_write_sb;
3614 unsigned long long start;
3615 __s8 log2_sectors_per_bitmap_bit = -1;
3616 __s8 log2_blocks_per_bitmap_bit;
3617 __u64 bits_in_journal;
3618 __u64 n_bitmap_bits;
3620 #define DIRECT_ARGUMENTS 4
3622 if (argc <= DIRECT_ARGUMENTS) {
3623 ti->error = "Invalid argument count";
3627 ic = kzalloc(sizeof(struct dm_integrity_c), GFP_KERNEL);
3629 ti->error = "Cannot allocate integrity context";
3633 ti->per_io_data_size = sizeof(struct dm_integrity_io);
3635 ic->in_progress = RB_ROOT;
3636 INIT_LIST_HEAD(&ic->wait_list);
3637 init_waitqueue_head(&ic->endio_wait);
3638 bio_list_init(&ic->flush_bio_list);
3639 init_waitqueue_head(&ic->copy_to_journal_wait);
3640 init_completion(&ic->crypto_backoff);
3641 atomic64_set(&ic->number_of_mismatches, 0);
3642 ic->bitmap_flush_interval = BITMAP_FLUSH_INTERVAL;
3644 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &ic->dev);
3646 ti->error = "Device lookup failed";
3650 if (sscanf(argv[1], "%llu%c", &start, &dummy) != 1 || start != (sector_t)start) {
3651 ti->error = "Invalid starting offset";
3657 if (strcmp(argv[2], "-")) {
3658 if (sscanf(argv[2], "%u%c", &ic->tag_size, &dummy) != 1 || !ic->tag_size) {
3659 ti->error = "Invalid tag size";
3665 if (!strcmp(argv[3], "J") || !strcmp(argv[3], "B") ||
3666 !strcmp(argv[3], "D") || !strcmp(argv[3], "R")) {
3667 ic->mode = argv[3][0];
3669 ti->error = "Invalid mode (expecting J, B, D, R)";
3674 journal_sectors = 0;
3675 interleave_sectors = DEFAULT_INTERLEAVE_SECTORS;
3676 buffer_sectors = DEFAULT_BUFFER_SECTORS;
3677 journal_watermark = DEFAULT_JOURNAL_WATERMARK;
3678 sync_msec = DEFAULT_SYNC_MSEC;
3679 ic->sectors_per_block = 1;
3681 as.argc = argc - DIRECT_ARGUMENTS;
3682 as.argv = argv + DIRECT_ARGUMENTS;
3683 r = dm_read_arg_group(_args, &as, &extra_args, &ti->error);
3687 while (extra_args--) {
3688 const char *opt_string;
3690 unsigned long long llval;
3691 opt_string = dm_shift_arg(&as);
3694 ti->error = "Not enough feature arguments";
3697 if (sscanf(opt_string, "journal_sectors:%u%c", &val, &dummy) == 1)
3698 journal_sectors = val ? val : 1;
3699 else if (sscanf(opt_string, "interleave_sectors:%u%c", &val, &dummy) == 1)
3700 interleave_sectors = val;
3701 else if (sscanf(opt_string, "buffer_sectors:%u%c", &val, &dummy) == 1)
3702 buffer_sectors = val;
3703 else if (sscanf(opt_string, "journal_watermark:%u%c", &val, &dummy) == 1 && val <= 100)
3704 journal_watermark = val;
3705 else if (sscanf(opt_string, "commit_time:%u%c", &val, &dummy) == 1)
3707 else if (!strncmp(opt_string, "meta_device:", strlen("meta_device:"))) {
3709 dm_put_device(ti, ic->meta_dev);
3710 ic->meta_dev = NULL;
3712 r = dm_get_device(ti, strchr(opt_string, ':') + 1,
3713 dm_table_get_mode(ti->table), &ic->meta_dev);
3715 ti->error = "Device lookup failed";
3718 } else if (sscanf(opt_string, "block_size:%u%c", &val, &dummy) == 1) {
3719 if (val < 1 << SECTOR_SHIFT ||
3720 val > MAX_SECTORS_PER_BLOCK << SECTOR_SHIFT ||
3723 ti->error = "Invalid block_size argument";
3726 ic->sectors_per_block = val >> SECTOR_SHIFT;
3727 } else if (sscanf(opt_string, "sectors_per_bit:%llu%c", &llval, &dummy) == 1) {
3728 log2_sectors_per_bitmap_bit = !llval ? 0 : __ilog2_u64(llval);
3729 } else if (sscanf(opt_string, "bitmap_flush_interval:%u%c", &val, &dummy) == 1) {
3730 if (val >= (uint64_t)UINT_MAX * 1000 / HZ) {
3732 ti->error = "Invalid bitmap_flush_interval argument";
3734 ic->bitmap_flush_interval = msecs_to_jiffies(val);
3735 } else if (!strncmp(opt_string, "internal_hash:", strlen("internal_hash:"))) {
3736 r = get_alg_and_key(opt_string, &ic->internal_hash_alg, &ti->error,
3737 "Invalid internal_hash argument");
3740 } else if (!strncmp(opt_string, "journal_crypt:", strlen("journal_crypt:"))) {
3741 r = get_alg_and_key(opt_string, &ic->journal_crypt_alg, &ti->error,
3742 "Invalid journal_crypt argument");
3745 } else if (!strncmp(opt_string, "journal_mac:", strlen("journal_mac:"))) {
3746 r = get_alg_and_key(opt_string, &ic->journal_mac_alg, &ti->error,
3747 "Invalid journal_mac argument");
3750 } else if (!strcmp(opt_string, "recalculate")) {
3751 ic->recalculate_flag = true;
3752 } else if (!strcmp(opt_string, "fix_padding")) {
3753 ic->fix_padding = true;
3756 ti->error = "Invalid argument";
3761 ic->data_device_sectors = i_size_read(ic->dev->bdev->bd_inode) >> SECTOR_SHIFT;
3763 ic->meta_device_sectors = ic->data_device_sectors;
3765 ic->meta_device_sectors = i_size_read(ic->meta_dev->bdev->bd_inode) >> SECTOR_SHIFT;
3767 if (!journal_sectors) {
3768 journal_sectors = min((sector_t)DEFAULT_MAX_JOURNAL_SECTORS,
3769 ic->data_device_sectors >> DEFAULT_JOURNAL_SIZE_FACTOR);
3772 if (!buffer_sectors)
3774 ic->log2_buffer_sectors = min((int)__fls(buffer_sectors), 31 - SECTOR_SHIFT);
3776 r = get_mac(&ic->internal_hash, &ic->internal_hash_alg, &ti->error,
3777 "Invalid internal hash", "Error setting internal hash key");
3781 r = get_mac(&ic->journal_mac, &ic->journal_mac_alg, &ti->error,
3782 "Invalid journal mac", "Error setting journal mac key");
3786 if (!ic->tag_size) {
3787 if (!ic->internal_hash) {
3788 ti->error = "Unknown tag size";
3792 ic->tag_size = crypto_shash_digestsize(ic->internal_hash);
3794 if (ic->tag_size > MAX_TAG_SIZE) {
3795 ti->error = "Too big tag size";
3799 if (!(ic->tag_size & (ic->tag_size - 1)))
3800 ic->log2_tag_size = __ffs(ic->tag_size);
3802 ic->log2_tag_size = -1;
3804 if (ic->mode == 'B' && !ic->internal_hash) {
3806 ti->error = "Bitmap mode can be only used with internal hash";
3810 ic->autocommit_jiffies = msecs_to_jiffies(sync_msec);
3811 ic->autocommit_msec = sync_msec;
3812 timer_setup(&ic->autocommit_timer, autocommit_fn, 0);
3814 ic->io = dm_io_client_create();
3815 if (IS_ERR(ic->io)) {
3816 r = PTR_ERR(ic->io);
3818 ti->error = "Cannot allocate dm io";
3822 r = mempool_init_slab_pool(&ic->journal_io_mempool, JOURNAL_IO_MEMPOOL, journal_io_cache);
3824 ti->error = "Cannot allocate mempool";
3828 ic->metadata_wq = alloc_workqueue("dm-integrity-metadata",
3829 WQ_MEM_RECLAIM, METADATA_WORKQUEUE_MAX_ACTIVE);
3830 if (!ic->metadata_wq) {
3831 ti->error = "Cannot allocate workqueue";
3837 * If this workqueue were percpu, it would cause bio reordering
3838 * and reduced performance.
3840 ic->wait_wq = alloc_workqueue("dm-integrity-wait", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
3842 ti->error = "Cannot allocate workqueue";
3847 ic->offload_wq = alloc_workqueue("dm-integrity-offload", WQ_MEM_RECLAIM,
3848 METADATA_WORKQUEUE_MAX_ACTIVE);
3849 if (!ic->offload_wq) {
3850 ti->error = "Cannot allocate workqueue";
3855 ic->commit_wq = alloc_workqueue("dm-integrity-commit", WQ_MEM_RECLAIM, 1);
3856 if (!ic->commit_wq) {
3857 ti->error = "Cannot allocate workqueue";
3861 INIT_WORK(&ic->commit_work, integrity_commit);
3863 if (ic->mode == 'J' || ic->mode == 'B') {
3864 ic->writer_wq = alloc_workqueue("dm-integrity-writer", WQ_MEM_RECLAIM, 1);
3865 if (!ic->writer_wq) {
3866 ti->error = "Cannot allocate workqueue";
3870 INIT_WORK(&ic->writer_work, integrity_writer);
3873 ic->sb = alloc_pages_exact(SB_SECTORS << SECTOR_SHIFT, GFP_KERNEL);
3876 ti->error = "Cannot allocate superblock area";
3880 r = sync_rw_sb(ic, REQ_OP_READ, 0);
3882 ti->error = "Error reading superblock";
3885 should_write_sb = false;
3886 if (memcmp(ic->sb->magic, SB_MAGIC, 8)) {
3887 if (ic->mode != 'R') {
3888 if (memchr_inv(ic->sb, 0, SB_SECTORS << SECTOR_SHIFT)) {
3890 ti->error = "The device is not initialized";
3895 r = initialize_superblock(ic, journal_sectors, interleave_sectors);
3897 ti->error = "Could not initialize superblock";
3900 if (ic->mode != 'R')
3901 should_write_sb = true;
3904 if (!ic->sb->version || ic->sb->version > SB_VERSION_4) {
3906 ti->error = "Unknown version";
3909 if (le16_to_cpu(ic->sb->integrity_tag_size) != ic->tag_size) {
3911 ti->error = "Tag size doesn't match the information in superblock";
3914 if (ic->sb->log2_sectors_per_block != __ffs(ic->sectors_per_block)) {
3916 ti->error = "Block size doesn't match the information in superblock";
3919 if (!le32_to_cpu(ic->sb->journal_sections)) {
3921 ti->error = "Corrupted superblock, journal_sections is 0";
3924 /* make sure that ti->max_io_len doesn't overflow */
3925 if (!ic->meta_dev) {
3926 if (ic->sb->log2_interleave_sectors < MIN_LOG2_INTERLEAVE_SECTORS ||
3927 ic->sb->log2_interleave_sectors > MAX_LOG2_INTERLEAVE_SECTORS) {
3929 ti->error = "Invalid interleave_sectors in the superblock";
3933 if (ic->sb->log2_interleave_sectors) {
3935 ti->error = "Invalid interleave_sectors in the superblock";
3939 ic->provided_data_sectors = le64_to_cpu(ic->sb->provided_data_sectors);
3940 if (ic->provided_data_sectors != le64_to_cpu(ic->sb->provided_data_sectors)) {
3941 /* test for overflow */
3943 ti->error = "The superblock has 64-bit device size, but the kernel was compiled with 32-bit sectors";
3946 if (!!(ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC)) != !!ic->journal_mac_alg.alg_string) {
3948 ti->error = "Journal mac mismatch";
3953 r = calculate_device_limits(ic);
3956 if (ic->log2_buffer_sectors > 3) {
3957 ic->log2_buffer_sectors--;
3958 goto try_smaller_buffer;
3961 ti->error = "The device is too small";
3965 if (log2_sectors_per_bitmap_bit < 0)
3966 log2_sectors_per_bitmap_bit = __fls(DEFAULT_SECTORS_PER_BITMAP_BIT);
3967 if (log2_sectors_per_bitmap_bit < ic->sb->log2_sectors_per_block)
3968 log2_sectors_per_bitmap_bit = ic->sb->log2_sectors_per_block;
3970 bits_in_journal = ((__u64)ic->journal_section_sectors * ic->journal_sections) << (SECTOR_SHIFT + 3);
3971 if (bits_in_journal > UINT_MAX)
3972 bits_in_journal = UINT_MAX;
3973 while (bits_in_journal < (ic->provided_data_sectors + ((sector_t)1 << log2_sectors_per_bitmap_bit) - 1) >> log2_sectors_per_bitmap_bit)
3974 log2_sectors_per_bitmap_bit++;
3976 log2_blocks_per_bitmap_bit = log2_sectors_per_bitmap_bit - ic->sb->log2_sectors_per_block;
3977 ic->log2_blocks_per_bitmap_bit = log2_blocks_per_bitmap_bit;
3978 if (should_write_sb) {
3979 ic->sb->log2_blocks_per_bitmap_bit = log2_blocks_per_bitmap_bit;
3981 n_bitmap_bits = ((ic->provided_data_sectors >> ic->sb->log2_sectors_per_block)
3982 + (((sector_t)1 << log2_blocks_per_bitmap_bit) - 1)) >> log2_blocks_per_bitmap_bit;
3983 ic->n_bitmap_blocks = DIV_ROUND_UP(n_bitmap_bits, BITMAP_BLOCK_SIZE * 8);
3986 ic->log2_buffer_sectors = min(ic->log2_buffer_sectors, (__u8)__ffs(ic->metadata_run));
3988 if (ti->len > ic->provided_data_sectors) {
3990 ti->error = "Not enough provided sectors for requested mapping size";
3995 threshold = (__u64)ic->journal_entries * (100 - journal_watermark);
3997 do_div(threshold, 100);
3998 ic->free_sectors_threshold = threshold;
4000 DEBUG_print("initialized:\n");
4001 DEBUG_print(" integrity_tag_size %u\n", le16_to_cpu(ic->sb->integrity_tag_size));
4002 DEBUG_print(" journal_entry_size %u\n", ic->journal_entry_size);
4003 DEBUG_print(" journal_entries_per_sector %u\n", ic->journal_entries_per_sector);
4004 DEBUG_print(" journal_section_entries %u\n", ic->journal_section_entries);
4005 DEBUG_print(" journal_section_sectors %u\n", ic->journal_section_sectors);
4006 DEBUG_print(" journal_sections %u\n", (unsigned)le32_to_cpu(ic->sb->journal_sections));
4007 DEBUG_print(" journal_entries %u\n", ic->journal_entries);
4008 DEBUG_print(" log2_interleave_sectors %d\n", ic->sb->log2_interleave_sectors);
4009 DEBUG_print(" data_device_sectors 0x%llx\n", i_size_read(ic->dev->bdev->bd_inode) >> SECTOR_SHIFT);
4010 DEBUG_print(" initial_sectors 0x%x\n", ic->initial_sectors);
4011 DEBUG_print(" metadata_run 0x%x\n", ic->metadata_run);
4012 DEBUG_print(" log2_metadata_run %d\n", ic->log2_metadata_run);
4013 DEBUG_print(" provided_data_sectors 0x%llx (%llu)\n", (unsigned long long)ic->provided_data_sectors,
4014 (unsigned long long)ic->provided_data_sectors);
4015 DEBUG_print(" log2_buffer_sectors %u\n", ic->log2_buffer_sectors);
4016 DEBUG_print(" bits_in_journal %llu\n", (unsigned long long)bits_in_journal);
4018 if (ic->recalculate_flag && !(ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))) {
4019 ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
4020 ic->sb->recalc_sector = cpu_to_le64(0);
4023 if (ic->internal_hash) {
4024 ic->recalc_wq = alloc_workqueue("dm-integrity-recalc", WQ_MEM_RECLAIM, 1);
4025 if (!ic->recalc_wq ) {
4026 ti->error = "Cannot allocate workqueue";
4030 INIT_WORK(&ic->recalc_work, integrity_recalc);
4031 ic->recalc_buffer = vmalloc(RECALC_SECTORS << SECTOR_SHIFT);
4032 if (!ic->recalc_buffer) {
4033 ti->error = "Cannot allocate buffer for recalculating";
4037 ic->recalc_tags = kvmalloc_array(RECALC_SECTORS >> ic->sb->log2_sectors_per_block,
4038 ic->tag_size, GFP_KERNEL);
4039 if (!ic->recalc_tags) {
4040 ti->error = "Cannot allocate tags for recalculating";
4046 ic->bufio = dm_bufio_client_create(ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev,
4047 1U << (SECTOR_SHIFT + ic->log2_buffer_sectors), 1, 0, NULL, NULL);
4048 if (IS_ERR(ic->bufio)) {
4049 r = PTR_ERR(ic->bufio);
4050 ti->error = "Cannot initialize dm-bufio";
4054 dm_bufio_set_sector_offset(ic->bufio, ic->start + ic->initial_sectors);
4056 if (ic->mode != 'R') {
4057 r = create_journal(ic, &ti->error);
4063 if (ic->mode == 'B') {
4065 unsigned n_bitmap_pages = DIV_ROUND_UP(ic->n_bitmap_blocks, PAGE_SIZE / BITMAP_BLOCK_SIZE);
4067 ic->recalc_bitmap = dm_integrity_alloc_page_list(n_bitmap_pages);
4068 if (!ic->recalc_bitmap) {
4072 ic->may_write_bitmap = dm_integrity_alloc_page_list(n_bitmap_pages);
4073 if (!ic->may_write_bitmap) {
4077 ic->bbs = kvmalloc_array(ic->n_bitmap_blocks, sizeof(struct bitmap_block_status), GFP_KERNEL);
4082 INIT_DELAYED_WORK(&ic->bitmap_flush_work, bitmap_flush_work);
4083 for (i = 0; i < ic->n_bitmap_blocks; i++) {
4084 struct bitmap_block_status *bbs = &ic->bbs[i];
4085 unsigned sector, pl_index, pl_offset;
4087 INIT_WORK(&bbs->work, bitmap_block_work);
4090 bio_list_init(&bbs->bio_queue);
4091 spin_lock_init(&bbs->bio_queue_lock);
4093 sector = i * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT);
4094 pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
4095 pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
4097 bbs->bitmap = lowmem_page_address(ic->journal[pl_index].page) + pl_offset;
4101 if (should_write_sb) {
4104 init_journal(ic, 0, ic->journal_sections, 0);
4105 r = dm_integrity_failed(ic);
4107 ti->error = "Error initializing journal";
4110 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
4112 ti->error = "Error initializing superblock";
4115 ic->just_formatted = true;
4118 if (!ic->meta_dev) {
4119 r = dm_set_target_max_io_len(ti, 1U << ic->sb->log2_interleave_sectors);
4123 if (ic->mode == 'B') {
4124 unsigned max_io_len = ((sector_t)ic->sectors_per_block << ic->log2_blocks_per_bitmap_bit) * (BITMAP_BLOCK_SIZE * 8);
4126 max_io_len = 1U << 31;
4127 DEBUG_print("max_io_len: old %u, new %u\n", ti->max_io_len, max_io_len);
4128 if (!ti->max_io_len || ti->max_io_len > max_io_len) {
4129 r = dm_set_target_max_io_len(ti, max_io_len);
4135 if (!ic->internal_hash)
4136 dm_integrity_set(ti, ic);
4138 ti->num_flush_bios = 1;
4139 ti->flush_supported = true;
4144 dm_integrity_dtr(ti);
4148 static void dm_integrity_dtr(struct dm_target *ti)
4150 struct dm_integrity_c *ic = ti->private;
4152 BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress));
4153 BUG_ON(!list_empty(&ic->wait_list));
4155 if (ic->metadata_wq)
4156 destroy_workqueue(ic->metadata_wq);
4158 destroy_workqueue(ic->wait_wq);
4160 destroy_workqueue(ic->offload_wq);
4162 destroy_workqueue(ic->commit_wq);
4164 destroy_workqueue(ic->writer_wq);
4166 destroy_workqueue(ic->recalc_wq);
4167 vfree(ic->recalc_buffer);
4168 kvfree(ic->recalc_tags);
4171 dm_bufio_client_destroy(ic->bufio);
4172 mempool_exit(&ic->journal_io_mempool);
4174 dm_io_client_destroy(ic->io);
4176 dm_put_device(ti, ic->dev);
4178 dm_put_device(ti, ic->meta_dev);
4179 dm_integrity_free_page_list(ic->journal);
4180 dm_integrity_free_page_list(ic->journal_io);
4181 dm_integrity_free_page_list(ic->journal_xor);
4182 dm_integrity_free_page_list(ic->recalc_bitmap);
4183 dm_integrity_free_page_list(ic->may_write_bitmap);
4184 if (ic->journal_scatterlist)
4185 dm_integrity_free_journal_scatterlist(ic, ic->journal_scatterlist);
4186 if (ic->journal_io_scatterlist)
4187 dm_integrity_free_journal_scatterlist(ic, ic->journal_io_scatterlist);
4188 if (ic->sk_requests) {
4191 for (i = 0; i < ic->journal_sections; i++) {
4192 struct skcipher_request *req = ic->sk_requests[i];
4195 skcipher_request_free(req);
4198 kvfree(ic->sk_requests);
4200 kvfree(ic->journal_tree);
4202 free_pages_exact(ic->sb, SB_SECTORS << SECTOR_SHIFT);
4204 if (ic->internal_hash)
4205 crypto_free_shash(ic->internal_hash);
4206 free_alg(&ic->internal_hash_alg);
4208 if (ic->journal_crypt)
4209 crypto_free_skcipher(ic->journal_crypt);
4210 free_alg(&ic->journal_crypt_alg);
4212 if (ic->journal_mac)
4213 crypto_free_shash(ic->journal_mac);
4214 free_alg(&ic->journal_mac_alg);
4219 static struct target_type integrity_target = {
4220 .name = "integrity",
4221 .version = {1, 4, 0},
4222 .module = THIS_MODULE,
4223 .features = DM_TARGET_SINGLETON | DM_TARGET_INTEGRITY,
4224 .ctr = dm_integrity_ctr,
4225 .dtr = dm_integrity_dtr,
4226 .map = dm_integrity_map,
4227 .postsuspend = dm_integrity_postsuspend,
4228 .resume = dm_integrity_resume,
4229 .status = dm_integrity_status,
4230 .iterate_devices = dm_integrity_iterate_devices,
4231 .io_hints = dm_integrity_io_hints,
4234 static int __init dm_integrity_init(void)
4238 journal_io_cache = kmem_cache_create("integrity_journal_io",
4239 sizeof(struct journal_io), 0, 0, NULL);
4240 if (!journal_io_cache) {
4241 DMERR("can't allocate journal io cache");
4245 r = dm_register_target(&integrity_target);
4248 DMERR("register failed %d", r);
4253 static void __exit dm_integrity_exit(void)
4255 dm_unregister_target(&integrity_target);
4256 kmem_cache_destroy(journal_io_cache);
4259 module_init(dm_integrity_init);
4260 module_exit(dm_integrity_exit);
4262 MODULE_AUTHOR("Milan Broz");
4263 MODULE_AUTHOR("Mikulas Patocka");
4264 MODULE_DESCRIPTION(DM_NAME " target for integrity tags extension");
4265 MODULE_LICENSE("GPL");