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 "dm-bio-record.h"
11 #include <linux/compiler.h>
12 #include <linux/module.h>
13 #include <linux/device-mapper.h>
14 #include <linux/dm-io.h>
15 #include <linux/vmalloc.h>
16 #include <linux/sort.h>
17 #include <linux/rbtree.h>
18 #include <linux/delay.h>
19 #include <linux/random.h>
20 #include <linux/reboot.h>
21 #include <crypto/hash.h>
22 #include <crypto/skcipher.h>
23 #include <linux/async_tx.h>
24 #include <linux/dm-bufio.h>
26 #define DM_MSG_PREFIX "integrity"
28 #define DEFAULT_INTERLEAVE_SECTORS 32768
29 #define DEFAULT_JOURNAL_SIZE_FACTOR 7
30 #define DEFAULT_SECTORS_PER_BITMAP_BIT 32768
31 #define DEFAULT_BUFFER_SECTORS 128
32 #define DEFAULT_JOURNAL_WATERMARK 50
33 #define DEFAULT_SYNC_MSEC 10000
34 #define DEFAULT_MAX_JOURNAL_SECTORS 131072
35 #define MIN_LOG2_INTERLEAVE_SECTORS 3
36 #define MAX_LOG2_INTERLEAVE_SECTORS 31
37 #define METADATA_WORKQUEUE_MAX_ACTIVE 16
38 #define RECALC_SECTORS 8192
39 #define RECALC_WRITE_SUPER 16
40 #define BITMAP_BLOCK_SIZE 4096 /* don't change it */
41 #define BITMAP_FLUSH_INTERVAL (10 * HZ)
44 * Warning - DEBUG_PRINT prints security-sensitive data to the log,
45 * so it should not be enabled in the official kernel
48 //#define INTERNAL_VERIFY
54 #define SB_MAGIC "integrt"
55 #define SB_VERSION_1 1
56 #define SB_VERSION_2 2
57 #define SB_VERSION_3 3
58 #define SB_VERSION_4 4
60 #define MAX_SECTORS_PER_BLOCK 8
65 __u8 log2_interleave_sectors;
66 __u16 integrity_tag_size;
67 __u32 journal_sections;
68 __u64 provided_data_sectors; /* userspace uses this value */
70 __u8 log2_sectors_per_block;
71 __u8 log2_blocks_per_bitmap_bit;
76 #define SB_FLAG_HAVE_JOURNAL_MAC 0x1
77 #define SB_FLAG_RECALCULATING 0x2
78 #define SB_FLAG_DIRTY_BITMAP 0x4
79 #define SB_FLAG_FIXED_PADDING 0x8
81 #define JOURNAL_ENTRY_ROUNDUP 8
83 typedef __u64 commit_id_t;
84 #define JOURNAL_MAC_PER_SECTOR 8
86 struct journal_entry {
94 commit_id_t last_bytes[0];
98 #define journal_entry_tag(ic, je) ((__u8 *)&(je)->last_bytes[(ic)->sectors_per_block])
100 #if BITS_PER_LONG == 64
101 #define journal_entry_set_sector(je, x) do { smp_wmb(); WRITE_ONCE((je)->u.sector, cpu_to_le64(x)); } while (0)
103 #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)
105 #define journal_entry_get_sector(je) le64_to_cpu((je)->u.sector)
106 #define journal_entry_is_unused(je) ((je)->u.s.sector_hi == cpu_to_le32(-1))
107 #define journal_entry_set_unused(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-1)); } while (0)
108 #define journal_entry_is_inprogress(je) ((je)->u.s.sector_hi == cpu_to_le32(-2))
109 #define journal_entry_set_inprogress(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-2)); } while (0)
111 #define JOURNAL_BLOCK_SECTORS 8
112 #define JOURNAL_SECTOR_DATA ((1 << SECTOR_SHIFT) - sizeof(commit_id_t))
113 #define JOURNAL_MAC_SIZE (JOURNAL_MAC_PER_SECTOR * JOURNAL_BLOCK_SECTORS)
115 struct journal_sector {
116 __u8 entries[JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR];
117 __u8 mac[JOURNAL_MAC_PER_SECTOR];
118 commit_id_t commit_id;
121 #define MAX_TAG_SIZE (JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR - offsetof(struct journal_entry, last_bytes[MAX_SECTORS_PER_BLOCK]))
123 #define METADATA_PADDING_SECTORS 8
125 #define N_COMMIT_IDS 4
127 static unsigned char prev_commit_seq(unsigned char seq)
129 return (seq + N_COMMIT_IDS - 1) % N_COMMIT_IDS;
132 static unsigned char next_commit_seq(unsigned char seq)
134 return (seq + 1) % N_COMMIT_IDS;
138 * In-memory structures
141 struct journal_node {
153 struct dm_integrity_c {
155 struct dm_dev *meta_dev;
159 mempool_t journal_io_mempool;
160 struct dm_io_client *io;
161 struct dm_bufio_client *bufio;
162 struct workqueue_struct *metadata_wq;
163 struct superblock *sb;
164 unsigned journal_pages;
165 unsigned n_bitmap_blocks;
167 struct page_list *journal;
168 struct page_list *journal_io;
169 struct page_list *journal_xor;
170 struct page_list *recalc_bitmap;
171 struct page_list *may_write_bitmap;
172 struct bitmap_block_status *bbs;
173 unsigned bitmap_flush_interval;
174 int synchronous_mode;
175 struct bio_list synchronous_bios;
176 struct delayed_work bitmap_flush_work;
178 struct crypto_skcipher *journal_crypt;
179 struct scatterlist **journal_scatterlist;
180 struct scatterlist **journal_io_scatterlist;
181 struct skcipher_request **sk_requests;
183 struct crypto_shash *journal_mac;
185 struct journal_node *journal_tree;
186 struct rb_root journal_tree_root;
188 sector_t provided_data_sectors;
190 unsigned short journal_entry_size;
191 unsigned char journal_entries_per_sector;
192 unsigned char journal_section_entries;
193 unsigned short journal_section_sectors;
194 unsigned journal_sections;
195 unsigned journal_entries;
196 sector_t data_device_sectors;
197 sector_t meta_device_sectors;
198 unsigned initial_sectors;
199 unsigned metadata_run;
200 __s8 log2_metadata_run;
201 __u8 log2_buffer_sectors;
202 __u8 sectors_per_block;
203 __u8 log2_blocks_per_bitmap_bit;
209 struct crypto_shash *internal_hash;
211 struct dm_target *ti;
213 /* these variables are locked with endio_wait.lock */
214 struct rb_root in_progress;
215 struct list_head wait_list;
216 wait_queue_head_t endio_wait;
217 struct workqueue_struct *wait_wq;
218 struct workqueue_struct *offload_wq;
220 unsigned char commit_seq;
221 commit_id_t commit_ids[N_COMMIT_IDS];
223 unsigned committed_section;
224 unsigned n_committed_sections;
226 unsigned uncommitted_section;
227 unsigned n_uncommitted_sections;
229 unsigned free_section;
230 unsigned char free_section_entry;
231 unsigned free_sectors;
233 unsigned free_sectors_threshold;
235 struct workqueue_struct *commit_wq;
236 struct work_struct commit_work;
238 struct workqueue_struct *writer_wq;
239 struct work_struct writer_work;
241 struct workqueue_struct *recalc_wq;
242 struct work_struct recalc_work;
246 struct bio_list flush_bio_list;
248 unsigned long autocommit_jiffies;
249 struct timer_list autocommit_timer;
250 unsigned autocommit_msec;
252 wait_queue_head_t copy_to_journal_wait;
254 struct completion crypto_backoff;
256 bool journal_uptodate;
258 bool recalculate_flag;
261 struct alg_spec internal_hash_alg;
262 struct alg_spec journal_crypt_alg;
263 struct alg_spec journal_mac_alg;
265 atomic64_t number_of_mismatches;
267 struct notifier_block reboot_notifier;
270 struct dm_integrity_range {
271 sector_t logical_sector;
277 struct task_struct *task;
278 struct list_head wait_entry;
283 struct dm_integrity_io {
284 struct work_struct work;
286 struct dm_integrity_c *ic;
290 struct dm_integrity_range range;
292 sector_t metadata_block;
293 unsigned metadata_offset;
296 blk_status_t bi_status;
298 struct completion *completion;
300 struct dm_bio_details bio_details;
303 struct journal_completion {
304 struct dm_integrity_c *ic;
306 struct completion comp;
310 struct dm_integrity_range range;
311 struct journal_completion *comp;
314 struct bitmap_block_status {
315 struct work_struct work;
316 struct dm_integrity_c *ic;
318 unsigned long *bitmap;
319 struct bio_list bio_queue;
320 spinlock_t bio_queue_lock;
324 static struct kmem_cache *journal_io_cache;
326 #define JOURNAL_IO_MEMPOOL 32
329 #define DEBUG_print(x, ...) printk(KERN_DEBUG x, ##__VA_ARGS__)
330 static void __DEBUG_bytes(__u8 *bytes, size_t len, const char *msg, ...)
339 pr_cont(" %02x", *bytes);
345 #define DEBUG_bytes(bytes, len, msg, ...) __DEBUG_bytes(bytes, len, KERN_DEBUG msg, ##__VA_ARGS__)
347 #define DEBUG_print(x, ...) do { } while (0)
348 #define DEBUG_bytes(bytes, len, msg, ...) do { } while (0)
351 static void dm_integrity_prepare(struct request *rq)
355 static void dm_integrity_complete(struct request *rq, unsigned int nr_bytes)
360 * DM Integrity profile, protection is performed layer above (dm-crypt)
362 static const struct blk_integrity_profile dm_integrity_profile = {
363 .name = "DM-DIF-EXT-TAG",
366 .prepare_fn = dm_integrity_prepare,
367 .complete_fn = dm_integrity_complete,
370 static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map);
371 static void integrity_bio_wait(struct work_struct *w);
372 static void dm_integrity_dtr(struct dm_target *ti);
374 static void dm_integrity_io_error(struct dm_integrity_c *ic, const char *msg, int err)
377 atomic64_inc(&ic->number_of_mismatches);
378 if (!cmpxchg(&ic->failed, 0, err))
379 DMERR("Error on %s: %d", msg, err);
382 static int dm_integrity_failed(struct dm_integrity_c *ic)
384 return READ_ONCE(ic->failed);
387 static commit_id_t dm_integrity_commit_id(struct dm_integrity_c *ic, unsigned i,
388 unsigned j, unsigned char seq)
391 * Xor the number with section and sector, so that if a piece of
392 * journal is written at wrong place, it is detected.
394 return ic->commit_ids[seq] ^ cpu_to_le64(((__u64)i << 32) ^ j);
397 static void get_area_and_offset(struct dm_integrity_c *ic, sector_t data_sector,
398 sector_t *area, sector_t *offset)
401 __u8 log2_interleave_sectors = ic->sb->log2_interleave_sectors;
402 *area = data_sector >> log2_interleave_sectors;
403 *offset = (unsigned)data_sector & ((1U << log2_interleave_sectors) - 1);
406 *offset = data_sector;
410 #define sector_to_block(ic, n) \
412 BUG_ON((n) & (unsigned)((ic)->sectors_per_block - 1)); \
413 (n) >>= (ic)->sb->log2_sectors_per_block; \
416 static __u64 get_metadata_sector_and_offset(struct dm_integrity_c *ic, sector_t area,
417 sector_t offset, unsigned *metadata_offset)
422 ms = area << ic->sb->log2_interleave_sectors;
423 if (likely(ic->log2_metadata_run >= 0))
424 ms += area << ic->log2_metadata_run;
426 ms += area * ic->metadata_run;
427 ms >>= ic->log2_buffer_sectors;
429 sector_to_block(ic, offset);
431 if (likely(ic->log2_tag_size >= 0)) {
432 ms += offset >> (SECTOR_SHIFT + ic->log2_buffer_sectors - ic->log2_tag_size);
433 mo = (offset << ic->log2_tag_size) & ((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - 1);
435 ms += (__u64)offset * ic->tag_size >> (SECTOR_SHIFT + ic->log2_buffer_sectors);
436 mo = (offset * ic->tag_size) & ((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - 1);
438 *metadata_offset = mo;
442 static sector_t get_data_sector(struct dm_integrity_c *ic, sector_t area, sector_t offset)
449 result = area << ic->sb->log2_interleave_sectors;
450 if (likely(ic->log2_metadata_run >= 0))
451 result += (area + 1) << ic->log2_metadata_run;
453 result += (area + 1) * ic->metadata_run;
455 result += (sector_t)ic->initial_sectors + offset;
461 static void wraparound_section(struct dm_integrity_c *ic, unsigned *sec_ptr)
463 if (unlikely(*sec_ptr >= ic->journal_sections))
464 *sec_ptr -= ic->journal_sections;
467 static void sb_set_version(struct dm_integrity_c *ic)
469 if (ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_PADDING))
470 ic->sb->version = SB_VERSION_4;
471 else if (ic->mode == 'B' || ic->sb->flags & cpu_to_le32(SB_FLAG_DIRTY_BITMAP))
472 ic->sb->version = SB_VERSION_3;
473 else if (ic->meta_dev || ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))
474 ic->sb->version = SB_VERSION_2;
476 ic->sb->version = SB_VERSION_1;
479 static int sync_rw_sb(struct dm_integrity_c *ic, int op, int op_flags)
481 struct dm_io_request io_req;
482 struct dm_io_region io_loc;
485 io_req.bi_op_flags = op_flags;
486 io_req.mem.type = DM_IO_KMEM;
487 io_req.mem.ptr.addr = ic->sb;
488 io_req.notify.fn = NULL;
489 io_req.client = ic->io;
490 io_loc.bdev = ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev;
491 io_loc.sector = ic->start;
492 io_loc.count = SB_SECTORS;
494 if (op == REQ_OP_WRITE)
497 return dm_io(&io_req, 1, &io_loc, NULL);
500 #define BITMAP_OP_TEST_ALL_SET 0
501 #define BITMAP_OP_TEST_ALL_CLEAR 1
502 #define BITMAP_OP_SET 2
503 #define BITMAP_OP_CLEAR 3
505 static bool block_bitmap_op(struct dm_integrity_c *ic, struct page_list *bitmap,
506 sector_t sector, sector_t n_sectors, int mode)
508 unsigned long bit, end_bit, this_end_bit, page, end_page;
511 if (unlikely(((sector | n_sectors) & ((1 << ic->sb->log2_sectors_per_block) - 1)) != 0)) {
512 DMCRIT("invalid bitmap access (%llx,%llx,%d,%d,%d)",
515 ic->sb->log2_sectors_per_block,
516 ic->log2_blocks_per_bitmap_bit,
521 if (unlikely(!n_sectors))
524 bit = sector >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
525 end_bit = (sector + n_sectors - 1) >>
526 (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
528 page = bit / (PAGE_SIZE * 8);
529 bit %= PAGE_SIZE * 8;
531 end_page = end_bit / (PAGE_SIZE * 8);
532 end_bit %= PAGE_SIZE * 8;
535 if (page < end_page) {
536 this_end_bit = PAGE_SIZE * 8 - 1;
538 this_end_bit = end_bit;
541 data = lowmem_page_address(bitmap[page].page);
543 if (mode == BITMAP_OP_TEST_ALL_SET) {
544 while (bit <= this_end_bit) {
545 if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
547 if (data[bit / BITS_PER_LONG] != -1)
549 bit += BITS_PER_LONG;
550 } while (this_end_bit >= bit + BITS_PER_LONG - 1);
553 if (!test_bit(bit, data))
557 } else if (mode == BITMAP_OP_TEST_ALL_CLEAR) {
558 while (bit <= this_end_bit) {
559 if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
561 if (data[bit / BITS_PER_LONG] != 0)
563 bit += BITS_PER_LONG;
564 } while (this_end_bit >= bit + BITS_PER_LONG - 1);
567 if (test_bit(bit, data))
571 } else if (mode == BITMAP_OP_SET) {
572 while (bit <= this_end_bit) {
573 if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
575 data[bit / BITS_PER_LONG] = -1;
576 bit += BITS_PER_LONG;
577 } while (this_end_bit >= bit + BITS_PER_LONG - 1);
580 __set_bit(bit, data);
583 } else if (mode == BITMAP_OP_CLEAR) {
584 if (!bit && this_end_bit == PAGE_SIZE * 8 - 1)
586 else while (bit <= this_end_bit) {
587 if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
589 data[bit / BITS_PER_LONG] = 0;
590 bit += BITS_PER_LONG;
591 } while (this_end_bit >= bit + BITS_PER_LONG - 1);
594 __clear_bit(bit, data);
601 if (unlikely(page < end_page)) {
610 static void block_bitmap_copy(struct dm_integrity_c *ic, struct page_list *dst, struct page_list *src)
612 unsigned n_bitmap_pages = DIV_ROUND_UP(ic->n_bitmap_blocks, PAGE_SIZE / BITMAP_BLOCK_SIZE);
615 for (i = 0; i < n_bitmap_pages; i++) {
616 unsigned long *dst_data = lowmem_page_address(dst[i].page);
617 unsigned long *src_data = lowmem_page_address(src[i].page);
618 copy_page(dst_data, src_data);
622 static struct bitmap_block_status *sector_to_bitmap_block(struct dm_integrity_c *ic, sector_t sector)
624 unsigned bit = sector >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
625 unsigned bitmap_block = bit / (BITMAP_BLOCK_SIZE * 8);
627 BUG_ON(bitmap_block >= ic->n_bitmap_blocks);
628 return &ic->bbs[bitmap_block];
631 static void access_journal_check(struct dm_integrity_c *ic, unsigned section, unsigned offset,
632 bool e, const char *function)
634 #if defined(CONFIG_DM_DEBUG) || defined(INTERNAL_VERIFY)
635 unsigned limit = e ? ic->journal_section_entries : ic->journal_section_sectors;
637 if (unlikely(section >= ic->journal_sections) ||
638 unlikely(offset >= limit)) {
639 DMCRIT("%s: invalid access at (%u,%u), limit (%u,%u)",
640 function, section, offset, ic->journal_sections, limit);
646 static void page_list_location(struct dm_integrity_c *ic, unsigned section, unsigned offset,
647 unsigned *pl_index, unsigned *pl_offset)
651 access_journal_check(ic, section, offset, false, "page_list_location");
653 sector = section * ic->journal_section_sectors + offset;
655 *pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
656 *pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
659 static struct journal_sector *access_page_list(struct dm_integrity_c *ic, struct page_list *pl,
660 unsigned section, unsigned offset, unsigned *n_sectors)
662 unsigned pl_index, pl_offset;
665 page_list_location(ic, section, offset, &pl_index, &pl_offset);
668 *n_sectors = (PAGE_SIZE - pl_offset) >> SECTOR_SHIFT;
670 va = lowmem_page_address(pl[pl_index].page);
672 return (struct journal_sector *)(va + pl_offset);
675 static struct journal_sector *access_journal(struct dm_integrity_c *ic, unsigned section, unsigned offset)
677 return access_page_list(ic, ic->journal, section, offset, NULL);
680 static struct journal_entry *access_journal_entry(struct dm_integrity_c *ic, unsigned section, unsigned n)
682 unsigned rel_sector, offset;
683 struct journal_sector *js;
685 access_journal_check(ic, section, n, true, "access_journal_entry");
687 rel_sector = n % JOURNAL_BLOCK_SECTORS;
688 offset = n / JOURNAL_BLOCK_SECTORS;
690 js = access_journal(ic, section, rel_sector);
691 return (struct journal_entry *)((char *)js + offset * ic->journal_entry_size);
694 static struct journal_sector *access_journal_data(struct dm_integrity_c *ic, unsigned section, unsigned n)
696 n <<= ic->sb->log2_sectors_per_block;
698 n += JOURNAL_BLOCK_SECTORS;
700 access_journal_check(ic, section, n, false, "access_journal_data");
702 return access_journal(ic, section, n);
705 static void section_mac(struct dm_integrity_c *ic, unsigned section, __u8 result[JOURNAL_MAC_SIZE])
707 SHASH_DESC_ON_STACK(desc, ic->journal_mac);
711 desc->tfm = ic->journal_mac;
713 r = crypto_shash_init(desc);
715 dm_integrity_io_error(ic, "crypto_shash_init", r);
719 for (j = 0; j < ic->journal_section_entries; j++) {
720 struct journal_entry *je = access_journal_entry(ic, section, j);
721 r = crypto_shash_update(desc, (__u8 *)&je->u.sector, sizeof je->u.sector);
723 dm_integrity_io_error(ic, "crypto_shash_update", r);
728 size = crypto_shash_digestsize(ic->journal_mac);
730 if (likely(size <= JOURNAL_MAC_SIZE)) {
731 r = crypto_shash_final(desc, result);
733 dm_integrity_io_error(ic, "crypto_shash_final", r);
736 memset(result + size, 0, JOURNAL_MAC_SIZE - size);
738 __u8 digest[HASH_MAX_DIGESTSIZE];
740 if (WARN_ON(size > sizeof(digest))) {
741 dm_integrity_io_error(ic, "digest_size", -EINVAL);
744 r = crypto_shash_final(desc, digest);
746 dm_integrity_io_error(ic, "crypto_shash_final", r);
749 memcpy(result, digest, JOURNAL_MAC_SIZE);
754 memset(result, 0, JOURNAL_MAC_SIZE);
757 static void rw_section_mac(struct dm_integrity_c *ic, unsigned section, bool wr)
759 __u8 result[JOURNAL_MAC_SIZE];
762 if (!ic->journal_mac)
765 section_mac(ic, section, result);
767 for (j = 0; j < JOURNAL_BLOCK_SECTORS; j++) {
768 struct journal_sector *js = access_journal(ic, section, j);
771 memcpy(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR);
773 if (memcmp(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR))
774 dm_integrity_io_error(ic, "journal mac", -EILSEQ);
779 static void complete_journal_op(void *context)
781 struct journal_completion *comp = context;
782 BUG_ON(!atomic_read(&comp->in_flight));
783 if (likely(atomic_dec_and_test(&comp->in_flight)))
784 complete(&comp->comp);
787 static void xor_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
788 unsigned n_sections, struct journal_completion *comp)
790 struct async_submit_ctl submit;
791 size_t n_bytes = (size_t)(n_sections * ic->journal_section_sectors) << SECTOR_SHIFT;
792 unsigned pl_index, pl_offset, section_index;
793 struct page_list *source_pl, *target_pl;
795 if (likely(encrypt)) {
796 source_pl = ic->journal;
797 target_pl = ic->journal_io;
799 source_pl = ic->journal_io;
800 target_pl = ic->journal;
803 page_list_location(ic, section, 0, &pl_index, &pl_offset);
805 atomic_add(roundup(pl_offset + n_bytes, PAGE_SIZE) >> PAGE_SHIFT, &comp->in_flight);
807 init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL, complete_journal_op, comp, NULL);
809 section_index = pl_index;
813 struct page *src_pages[2];
814 struct page *dst_page;
816 while (unlikely(pl_index == section_index)) {
819 rw_section_mac(ic, section, true);
824 page_list_location(ic, section, 0, §ion_index, &dummy);
827 this_step = min(n_bytes, (size_t)PAGE_SIZE - pl_offset);
828 dst_page = target_pl[pl_index].page;
829 src_pages[0] = source_pl[pl_index].page;
830 src_pages[1] = ic->journal_xor[pl_index].page;
832 async_xor(dst_page, src_pages, pl_offset, 2, this_step, &submit);
836 n_bytes -= this_step;
841 async_tx_issue_pending_all();
844 static void complete_journal_encrypt(struct crypto_async_request *req, int err)
846 struct journal_completion *comp = req->data;
848 if (likely(err == -EINPROGRESS)) {
849 complete(&comp->ic->crypto_backoff);
852 dm_integrity_io_error(comp->ic, "asynchronous encrypt", err);
854 complete_journal_op(comp);
857 static bool do_crypt(bool encrypt, struct skcipher_request *req, struct journal_completion *comp)
860 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
861 complete_journal_encrypt, comp);
863 r = crypto_skcipher_encrypt(req);
865 r = crypto_skcipher_decrypt(req);
868 if (likely(r == -EINPROGRESS))
870 if (likely(r == -EBUSY)) {
871 wait_for_completion(&comp->ic->crypto_backoff);
872 reinit_completion(&comp->ic->crypto_backoff);
875 dm_integrity_io_error(comp->ic, "encrypt", r);
879 static void crypt_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
880 unsigned n_sections, struct journal_completion *comp)
882 struct scatterlist **source_sg;
883 struct scatterlist **target_sg;
885 atomic_add(2, &comp->in_flight);
887 if (likely(encrypt)) {
888 source_sg = ic->journal_scatterlist;
889 target_sg = ic->journal_io_scatterlist;
891 source_sg = ic->journal_io_scatterlist;
892 target_sg = ic->journal_scatterlist;
896 struct skcipher_request *req;
901 rw_section_mac(ic, section, true);
903 req = ic->sk_requests[section];
904 ivsize = crypto_skcipher_ivsize(ic->journal_crypt);
907 memcpy(iv, iv + ivsize, ivsize);
909 req->src = source_sg[section];
910 req->dst = target_sg[section];
912 if (unlikely(do_crypt(encrypt, req, comp)))
913 atomic_inc(&comp->in_flight);
917 } while (n_sections);
919 atomic_dec(&comp->in_flight);
920 complete_journal_op(comp);
923 static void encrypt_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
924 unsigned n_sections, struct journal_completion *comp)
927 return xor_journal(ic, encrypt, section, n_sections, comp);
929 return crypt_journal(ic, encrypt, section, n_sections, comp);
932 static void complete_journal_io(unsigned long error, void *context)
934 struct journal_completion *comp = context;
935 if (unlikely(error != 0))
936 dm_integrity_io_error(comp->ic, "writing journal", -EIO);
937 complete_journal_op(comp);
940 static void rw_journal_sectors(struct dm_integrity_c *ic, int op, int op_flags,
941 unsigned sector, unsigned n_sectors, struct journal_completion *comp)
943 struct dm_io_request io_req;
944 struct dm_io_region io_loc;
945 unsigned pl_index, pl_offset;
948 if (unlikely(dm_integrity_failed(ic))) {
950 complete_journal_io(-1UL, comp);
954 pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
955 pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
958 io_req.bi_op_flags = op_flags;
959 io_req.mem.type = DM_IO_PAGE_LIST;
961 io_req.mem.ptr.pl = &ic->journal_io[pl_index];
963 io_req.mem.ptr.pl = &ic->journal[pl_index];
964 io_req.mem.offset = pl_offset;
965 if (likely(comp != NULL)) {
966 io_req.notify.fn = complete_journal_io;
967 io_req.notify.context = comp;
969 io_req.notify.fn = NULL;
971 io_req.client = ic->io;
972 io_loc.bdev = ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev;
973 io_loc.sector = ic->start + SB_SECTORS + sector;
974 io_loc.count = n_sectors;
976 r = dm_io(&io_req, 1, &io_loc, NULL);
978 dm_integrity_io_error(ic, op == REQ_OP_READ ? "reading journal" : "writing journal", r);
980 WARN_ONCE(1, "asynchronous dm_io failed: %d", r);
981 complete_journal_io(-1UL, comp);
986 static void rw_journal(struct dm_integrity_c *ic, int op, int op_flags, unsigned section,
987 unsigned n_sections, struct journal_completion *comp)
989 unsigned sector, n_sectors;
991 sector = section * ic->journal_section_sectors;
992 n_sectors = n_sections * ic->journal_section_sectors;
994 rw_journal_sectors(ic, op, op_flags, sector, n_sectors, comp);
997 static void write_journal(struct dm_integrity_c *ic, unsigned commit_start, unsigned commit_sections)
999 struct journal_completion io_comp;
1000 struct journal_completion crypt_comp_1;
1001 struct journal_completion crypt_comp_2;
1005 init_completion(&io_comp.comp);
1007 if (commit_start + commit_sections <= ic->journal_sections) {
1008 io_comp.in_flight = (atomic_t)ATOMIC_INIT(1);
1009 if (ic->journal_io) {
1010 crypt_comp_1.ic = ic;
1011 init_completion(&crypt_comp_1.comp);
1012 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
1013 encrypt_journal(ic, true, commit_start, commit_sections, &crypt_comp_1);
1014 wait_for_completion_io(&crypt_comp_1.comp);
1016 for (i = 0; i < commit_sections; i++)
1017 rw_section_mac(ic, commit_start + i, true);
1019 rw_journal(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, commit_start,
1020 commit_sections, &io_comp);
1023 io_comp.in_flight = (atomic_t)ATOMIC_INIT(2);
1024 to_end = ic->journal_sections - commit_start;
1025 if (ic->journal_io) {
1026 crypt_comp_1.ic = ic;
1027 init_completion(&crypt_comp_1.comp);
1028 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
1029 encrypt_journal(ic, true, commit_start, to_end, &crypt_comp_1);
1030 if (try_wait_for_completion(&crypt_comp_1.comp)) {
1031 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
1032 reinit_completion(&crypt_comp_1.comp);
1033 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
1034 encrypt_journal(ic, true, 0, commit_sections - to_end, &crypt_comp_1);
1035 wait_for_completion_io(&crypt_comp_1.comp);
1037 crypt_comp_2.ic = ic;
1038 init_completion(&crypt_comp_2.comp);
1039 crypt_comp_2.in_flight = (atomic_t)ATOMIC_INIT(0);
1040 encrypt_journal(ic, true, 0, commit_sections - to_end, &crypt_comp_2);
1041 wait_for_completion_io(&crypt_comp_1.comp);
1042 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
1043 wait_for_completion_io(&crypt_comp_2.comp);
1046 for (i = 0; i < to_end; i++)
1047 rw_section_mac(ic, commit_start + i, true);
1048 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
1049 for (i = 0; i < commit_sections - to_end; i++)
1050 rw_section_mac(ic, i, true);
1052 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, 0, commit_sections - to_end, &io_comp);
1055 wait_for_completion_io(&io_comp.comp);
1058 static void copy_from_journal(struct dm_integrity_c *ic, unsigned section, unsigned offset,
1059 unsigned n_sectors, sector_t target, io_notify_fn fn, void *data)
1061 struct dm_io_request io_req;
1062 struct dm_io_region io_loc;
1064 unsigned sector, pl_index, pl_offset;
1066 BUG_ON((target | n_sectors | offset) & (unsigned)(ic->sectors_per_block - 1));
1068 if (unlikely(dm_integrity_failed(ic))) {
1073 sector = section * ic->journal_section_sectors + JOURNAL_BLOCK_SECTORS + offset;
1075 pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
1076 pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
1078 io_req.bi_op = REQ_OP_WRITE;
1079 io_req.bi_op_flags = 0;
1080 io_req.mem.type = DM_IO_PAGE_LIST;
1081 io_req.mem.ptr.pl = &ic->journal[pl_index];
1082 io_req.mem.offset = pl_offset;
1083 io_req.notify.fn = fn;
1084 io_req.notify.context = data;
1085 io_req.client = ic->io;
1086 io_loc.bdev = ic->dev->bdev;
1087 io_loc.sector = target;
1088 io_loc.count = n_sectors;
1090 r = dm_io(&io_req, 1, &io_loc, NULL);
1092 WARN_ONCE(1, "asynchronous dm_io failed: %d", r);
1097 static bool ranges_overlap(struct dm_integrity_range *range1, struct dm_integrity_range *range2)
1099 return range1->logical_sector < range2->logical_sector + range2->n_sectors &&
1100 range1->logical_sector + range1->n_sectors > range2->logical_sector;
1103 static bool add_new_range(struct dm_integrity_c *ic, struct dm_integrity_range *new_range, bool check_waiting)
1105 struct rb_node **n = &ic->in_progress.rb_node;
1106 struct rb_node *parent;
1108 BUG_ON((new_range->logical_sector | new_range->n_sectors) & (unsigned)(ic->sectors_per_block - 1));
1110 if (likely(check_waiting)) {
1111 struct dm_integrity_range *range;
1112 list_for_each_entry(range, &ic->wait_list, wait_entry) {
1113 if (unlikely(ranges_overlap(range, new_range)))
1121 struct dm_integrity_range *range = container_of(*n, struct dm_integrity_range, node);
1124 if (new_range->logical_sector + new_range->n_sectors <= range->logical_sector) {
1125 n = &range->node.rb_left;
1126 } else if (new_range->logical_sector >= range->logical_sector + range->n_sectors) {
1127 n = &range->node.rb_right;
1133 rb_link_node(&new_range->node, parent, n);
1134 rb_insert_color(&new_range->node, &ic->in_progress);
1139 static void remove_range_unlocked(struct dm_integrity_c *ic, struct dm_integrity_range *range)
1141 rb_erase(&range->node, &ic->in_progress);
1142 while (unlikely(!list_empty(&ic->wait_list))) {
1143 struct dm_integrity_range *last_range =
1144 list_first_entry(&ic->wait_list, struct dm_integrity_range, wait_entry);
1145 struct task_struct *last_range_task;
1146 last_range_task = last_range->task;
1147 list_del(&last_range->wait_entry);
1148 if (!add_new_range(ic, last_range, false)) {
1149 last_range->task = last_range_task;
1150 list_add(&last_range->wait_entry, &ic->wait_list);
1153 last_range->waiting = false;
1154 wake_up_process(last_range_task);
1158 static void remove_range(struct dm_integrity_c *ic, struct dm_integrity_range *range)
1160 unsigned long flags;
1162 spin_lock_irqsave(&ic->endio_wait.lock, flags);
1163 remove_range_unlocked(ic, range);
1164 spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
1167 static void wait_and_add_new_range(struct dm_integrity_c *ic, struct dm_integrity_range *new_range)
1169 new_range->waiting = true;
1170 list_add_tail(&new_range->wait_entry, &ic->wait_list);
1171 new_range->task = current;
1173 __set_current_state(TASK_UNINTERRUPTIBLE);
1174 spin_unlock_irq(&ic->endio_wait.lock);
1176 spin_lock_irq(&ic->endio_wait.lock);
1177 } while (unlikely(new_range->waiting));
1180 static void add_new_range_and_wait(struct dm_integrity_c *ic, struct dm_integrity_range *new_range)
1182 if (unlikely(!add_new_range(ic, new_range, true)))
1183 wait_and_add_new_range(ic, new_range);
1186 static void init_journal_node(struct journal_node *node)
1188 RB_CLEAR_NODE(&node->node);
1189 node->sector = (sector_t)-1;
1192 static void add_journal_node(struct dm_integrity_c *ic, struct journal_node *node, sector_t sector)
1194 struct rb_node **link;
1195 struct rb_node *parent;
1197 node->sector = sector;
1198 BUG_ON(!RB_EMPTY_NODE(&node->node));
1200 link = &ic->journal_tree_root.rb_node;
1204 struct journal_node *j;
1206 j = container_of(parent, struct journal_node, node);
1207 if (sector < j->sector)
1208 link = &j->node.rb_left;
1210 link = &j->node.rb_right;
1213 rb_link_node(&node->node, parent, link);
1214 rb_insert_color(&node->node, &ic->journal_tree_root);
1217 static void remove_journal_node(struct dm_integrity_c *ic, struct journal_node *node)
1219 BUG_ON(RB_EMPTY_NODE(&node->node));
1220 rb_erase(&node->node, &ic->journal_tree_root);
1221 init_journal_node(node);
1224 #define NOT_FOUND (-1U)
1226 static unsigned find_journal_node(struct dm_integrity_c *ic, sector_t sector, sector_t *next_sector)
1228 struct rb_node *n = ic->journal_tree_root.rb_node;
1229 unsigned found = NOT_FOUND;
1230 *next_sector = (sector_t)-1;
1232 struct journal_node *j = container_of(n, struct journal_node, node);
1233 if (sector == j->sector) {
1234 found = j - ic->journal_tree;
1236 if (sector < j->sector) {
1237 *next_sector = j->sector;
1238 n = j->node.rb_left;
1240 n = j->node.rb_right;
1247 static bool test_journal_node(struct dm_integrity_c *ic, unsigned pos, sector_t sector)
1249 struct journal_node *node, *next_node;
1250 struct rb_node *next;
1252 if (unlikely(pos >= ic->journal_entries))
1254 node = &ic->journal_tree[pos];
1255 if (unlikely(RB_EMPTY_NODE(&node->node)))
1257 if (unlikely(node->sector != sector))
1260 next = rb_next(&node->node);
1261 if (unlikely(!next))
1264 next_node = container_of(next, struct journal_node, node);
1265 return next_node->sector != sector;
1268 static bool find_newer_committed_node(struct dm_integrity_c *ic, struct journal_node *node)
1270 struct rb_node *next;
1271 struct journal_node *next_node;
1272 unsigned next_section;
1274 BUG_ON(RB_EMPTY_NODE(&node->node));
1276 next = rb_next(&node->node);
1277 if (unlikely(!next))
1280 next_node = container_of(next, struct journal_node, node);
1282 if (next_node->sector != node->sector)
1285 next_section = (unsigned)(next_node - ic->journal_tree) / ic->journal_section_entries;
1286 if (next_section >= ic->committed_section &&
1287 next_section < ic->committed_section + ic->n_committed_sections)
1289 if (next_section + ic->journal_sections < ic->committed_section + ic->n_committed_sections)
1299 static int dm_integrity_rw_tag(struct dm_integrity_c *ic, unsigned char *tag, sector_t *metadata_block,
1300 unsigned *metadata_offset, unsigned total_size, int op)
1303 unsigned char *data, *dp;
1304 struct dm_buffer *b;
1308 r = dm_integrity_failed(ic);
1312 data = dm_bufio_read(ic->bufio, *metadata_block, &b);
1314 return PTR_ERR(data);
1316 to_copy = min((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - *metadata_offset, total_size);
1317 dp = data + *metadata_offset;
1318 if (op == TAG_READ) {
1319 memcpy(tag, dp, to_copy);
1320 } else if (op == TAG_WRITE) {
1321 memcpy(dp, tag, to_copy);
1322 dm_bufio_mark_partial_buffer_dirty(b, *metadata_offset, *metadata_offset + to_copy);
1324 /* e.g.: op == TAG_CMP */
1325 if (unlikely(memcmp(dp, tag, to_copy))) {
1328 for (i = 0; i < to_copy; i++) {
1329 if (dp[i] != tag[i])
1333 dm_bufio_release(b);
1337 dm_bufio_release(b);
1340 *metadata_offset += to_copy;
1341 if (unlikely(*metadata_offset == 1U << SECTOR_SHIFT << ic->log2_buffer_sectors)) {
1342 (*metadata_block)++;
1343 *metadata_offset = 0;
1345 total_size -= to_copy;
1346 } while (unlikely(total_size));
1351 static void dm_integrity_flush_buffers(struct dm_integrity_c *ic)
1354 r = dm_bufio_write_dirty_buffers(ic->bufio);
1356 dm_integrity_io_error(ic, "writing tags", r);
1359 static void sleep_on_endio_wait(struct dm_integrity_c *ic)
1361 DECLARE_WAITQUEUE(wait, current);
1362 __add_wait_queue(&ic->endio_wait, &wait);
1363 __set_current_state(TASK_UNINTERRUPTIBLE);
1364 spin_unlock_irq(&ic->endio_wait.lock);
1366 spin_lock_irq(&ic->endio_wait.lock);
1367 __remove_wait_queue(&ic->endio_wait, &wait);
1370 static void autocommit_fn(struct timer_list *t)
1372 struct dm_integrity_c *ic = from_timer(ic, t, autocommit_timer);
1374 if (likely(!dm_integrity_failed(ic)))
1375 queue_work(ic->commit_wq, &ic->commit_work);
1378 static void schedule_autocommit(struct dm_integrity_c *ic)
1380 if (!timer_pending(&ic->autocommit_timer))
1381 mod_timer(&ic->autocommit_timer, jiffies + ic->autocommit_jiffies);
1384 static void submit_flush_bio(struct dm_integrity_c *ic, struct dm_integrity_io *dio)
1387 unsigned long flags;
1389 spin_lock_irqsave(&ic->endio_wait.lock, flags);
1390 bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1391 bio_list_add(&ic->flush_bio_list, bio);
1392 spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
1394 queue_work(ic->commit_wq, &ic->commit_work);
1397 static void do_endio(struct dm_integrity_c *ic, struct bio *bio)
1399 int r = dm_integrity_failed(ic);
1400 if (unlikely(r) && !bio->bi_status)
1401 bio->bi_status = errno_to_blk_status(r);
1402 if (unlikely(ic->synchronous_mode) && bio_op(bio) == REQ_OP_WRITE) {
1403 unsigned long flags;
1404 spin_lock_irqsave(&ic->endio_wait.lock, flags);
1405 bio_list_add(&ic->synchronous_bios, bio);
1406 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0);
1407 spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
1413 static void do_endio_flush(struct dm_integrity_c *ic, struct dm_integrity_io *dio)
1415 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1417 if (unlikely(dio->fua) && likely(!bio->bi_status) && likely(!dm_integrity_failed(ic)))
1418 submit_flush_bio(ic, dio);
1423 static void dec_in_flight(struct dm_integrity_io *dio)
1425 if (atomic_dec_and_test(&dio->in_flight)) {
1426 struct dm_integrity_c *ic = dio->ic;
1429 remove_range(ic, &dio->range);
1431 if (unlikely(dio->write))
1432 schedule_autocommit(ic);
1434 bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1436 if (unlikely(dio->bi_status) && !bio->bi_status)
1437 bio->bi_status = dio->bi_status;
1438 if (likely(!bio->bi_status) && unlikely(bio_sectors(bio) != dio->range.n_sectors)) {
1439 dio->range.logical_sector += dio->range.n_sectors;
1440 bio_advance(bio, dio->range.n_sectors << SECTOR_SHIFT);
1441 INIT_WORK(&dio->work, integrity_bio_wait);
1442 queue_work(ic->offload_wq, &dio->work);
1445 do_endio_flush(ic, dio);
1449 static void integrity_end_io(struct bio *bio)
1451 struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
1453 dm_bio_restore(&dio->bio_details, bio);
1454 if (bio->bi_integrity)
1455 bio->bi_opf |= REQ_INTEGRITY;
1457 if (dio->completion)
1458 complete(dio->completion);
1463 static void integrity_sector_checksum(struct dm_integrity_c *ic, sector_t sector,
1464 const char *data, char *result)
1466 __u64 sector_le = cpu_to_le64(sector);
1467 SHASH_DESC_ON_STACK(req, ic->internal_hash);
1469 unsigned digest_size;
1471 req->tfm = ic->internal_hash;
1473 r = crypto_shash_init(req);
1474 if (unlikely(r < 0)) {
1475 dm_integrity_io_error(ic, "crypto_shash_init", r);
1479 r = crypto_shash_update(req, (const __u8 *)§or_le, sizeof sector_le);
1480 if (unlikely(r < 0)) {
1481 dm_integrity_io_error(ic, "crypto_shash_update", r);
1485 r = crypto_shash_update(req, data, ic->sectors_per_block << SECTOR_SHIFT);
1486 if (unlikely(r < 0)) {
1487 dm_integrity_io_error(ic, "crypto_shash_update", r);
1491 r = crypto_shash_final(req, result);
1492 if (unlikely(r < 0)) {
1493 dm_integrity_io_error(ic, "crypto_shash_final", r);
1497 digest_size = crypto_shash_digestsize(ic->internal_hash);
1498 if (unlikely(digest_size < ic->tag_size))
1499 memset(result + digest_size, 0, ic->tag_size - digest_size);
1504 /* this shouldn't happen anyway, the hash functions have no reason to fail */
1505 get_random_bytes(result, ic->tag_size);
1508 static void integrity_metadata(struct work_struct *w)
1510 struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work);
1511 struct dm_integrity_c *ic = dio->ic;
1515 if (ic->internal_hash) {
1516 struct bvec_iter iter;
1518 unsigned digest_size = crypto_shash_digestsize(ic->internal_hash);
1519 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1521 unsigned extra_space = unlikely(digest_size > ic->tag_size) ? digest_size - ic->tag_size : 0;
1522 char checksums_onstack[max((size_t)HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)];
1523 unsigned sectors_to_process = dio->range.n_sectors;
1524 sector_t sector = dio->range.logical_sector;
1526 if (unlikely(ic->mode == 'R'))
1529 checksums = kmalloc((PAGE_SIZE >> SECTOR_SHIFT >> ic->sb->log2_sectors_per_block) * ic->tag_size + extra_space,
1530 GFP_NOIO | __GFP_NORETRY | __GFP_NOWARN);
1532 checksums = checksums_onstack;
1533 if (WARN_ON(extra_space &&
1534 digest_size > sizeof(checksums_onstack))) {
1540 __bio_for_each_segment(bv, bio, iter, dio->bio_details.bi_iter) {
1542 char *mem, *checksums_ptr;
1545 mem = (char *)kmap_atomic(bv.bv_page) + bv.bv_offset;
1547 checksums_ptr = checksums;
1549 integrity_sector_checksum(ic, sector, mem + pos, checksums_ptr);
1550 checksums_ptr += ic->tag_size;
1551 sectors_to_process -= ic->sectors_per_block;
1552 pos += ic->sectors_per_block << SECTOR_SHIFT;
1553 sector += ic->sectors_per_block;
1554 } while (pos < bv.bv_len && sectors_to_process && checksums != checksums_onstack);
1557 r = dm_integrity_rw_tag(ic, checksums, &dio->metadata_block, &dio->metadata_offset,
1558 checksums_ptr - checksums, !dio->write ? TAG_CMP : TAG_WRITE);
1561 char b[BDEVNAME_SIZE];
1562 DMERR_LIMIT("%s: Checksum failed at sector 0x%llx", bio_devname(bio, b),
1563 (sector - ((r + ic->tag_size - 1) / ic->tag_size)));
1565 atomic64_inc(&ic->number_of_mismatches);
1567 if (likely(checksums != checksums_onstack))
1572 if (!sectors_to_process)
1575 if (unlikely(pos < bv.bv_len)) {
1576 bv.bv_offset += pos;
1582 if (likely(checksums != checksums_onstack))
1585 struct bio_integrity_payload *bip = dio->bio_details.bi_integrity;
1589 struct bvec_iter iter;
1590 unsigned data_to_process = dio->range.n_sectors;
1591 sector_to_block(ic, data_to_process);
1592 data_to_process *= ic->tag_size;
1594 bip_for_each_vec(biv, bip, iter) {
1598 BUG_ON(PageHighMem(biv.bv_page));
1599 tag = lowmem_page_address(biv.bv_page) + biv.bv_offset;
1600 this_len = min(biv.bv_len, data_to_process);
1601 r = dm_integrity_rw_tag(ic, tag, &dio->metadata_block, &dio->metadata_offset,
1602 this_len, !dio->write ? TAG_READ : TAG_WRITE);
1605 data_to_process -= this_len;
1606 if (!data_to_process)
1615 dio->bi_status = errno_to_blk_status(r);
1619 static int dm_integrity_map(struct dm_target *ti, struct bio *bio)
1621 struct dm_integrity_c *ic = ti->private;
1622 struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
1623 struct bio_integrity_payload *bip;
1625 sector_t area, offset;
1630 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1631 submit_flush_bio(ic, dio);
1632 return DM_MAPIO_SUBMITTED;
1635 dio->range.logical_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
1636 dio->write = bio_op(bio) == REQ_OP_WRITE;
1637 dio->fua = dio->write && bio->bi_opf & REQ_FUA;
1638 if (unlikely(dio->fua)) {
1640 * Don't pass down the FUA flag because we have to flush
1641 * disk cache anyway.
1643 bio->bi_opf &= ~REQ_FUA;
1645 if (unlikely(dio->range.logical_sector + bio_sectors(bio) > ic->provided_data_sectors)) {
1646 DMERR("Too big sector number: 0x%llx + 0x%x > 0x%llx",
1647 dio->range.logical_sector, bio_sectors(bio),
1648 ic->provided_data_sectors);
1649 return DM_MAPIO_KILL;
1651 if (unlikely((dio->range.logical_sector | bio_sectors(bio)) & (unsigned)(ic->sectors_per_block - 1))) {
1652 DMERR("Bio not aligned on %u sectors: 0x%llx, 0x%x",
1653 ic->sectors_per_block,
1654 dio->range.logical_sector, bio_sectors(bio));
1655 return DM_MAPIO_KILL;
1658 if (ic->sectors_per_block > 1) {
1659 struct bvec_iter iter;
1661 bio_for_each_segment(bv, bio, iter) {
1662 if (unlikely(bv.bv_len & ((ic->sectors_per_block << SECTOR_SHIFT) - 1))) {
1663 DMERR("Bio vector (%u,%u) is not aligned on %u-sector boundary",
1664 bv.bv_offset, bv.bv_len, ic->sectors_per_block);
1665 return DM_MAPIO_KILL;
1670 bip = bio_integrity(bio);
1671 if (!ic->internal_hash) {
1673 unsigned wanted_tag_size = bio_sectors(bio) >> ic->sb->log2_sectors_per_block;
1674 if (ic->log2_tag_size >= 0)
1675 wanted_tag_size <<= ic->log2_tag_size;
1677 wanted_tag_size *= ic->tag_size;
1678 if (unlikely(wanted_tag_size != bip->bip_iter.bi_size)) {
1679 DMERR("Invalid integrity data size %u, expected %u",
1680 bip->bip_iter.bi_size, wanted_tag_size);
1681 return DM_MAPIO_KILL;
1685 if (unlikely(bip != NULL)) {
1686 DMERR("Unexpected integrity data when using internal hash");
1687 return DM_MAPIO_KILL;
1691 if (unlikely(ic->mode == 'R') && unlikely(dio->write))
1692 return DM_MAPIO_KILL;
1694 get_area_and_offset(ic, dio->range.logical_sector, &area, &offset);
1695 dio->metadata_block = get_metadata_sector_and_offset(ic, area, offset, &dio->metadata_offset);
1696 bio->bi_iter.bi_sector = get_data_sector(ic, area, offset);
1698 dm_integrity_map_continue(dio, true);
1699 return DM_MAPIO_SUBMITTED;
1702 static bool __journal_read_write(struct dm_integrity_io *dio, struct bio *bio,
1703 unsigned journal_section, unsigned journal_entry)
1705 struct dm_integrity_c *ic = dio->ic;
1706 sector_t logical_sector;
1709 logical_sector = dio->range.logical_sector;
1710 n_sectors = dio->range.n_sectors;
1712 struct bio_vec bv = bio_iovec(bio);
1715 if (unlikely(bv.bv_len >> SECTOR_SHIFT > n_sectors))
1716 bv.bv_len = n_sectors << SECTOR_SHIFT;
1717 n_sectors -= bv.bv_len >> SECTOR_SHIFT;
1718 bio_advance_iter(bio, &bio->bi_iter, bv.bv_len);
1720 mem = kmap_atomic(bv.bv_page);
1721 if (likely(dio->write))
1722 flush_dcache_page(bv.bv_page);
1725 struct journal_entry *je = access_journal_entry(ic, journal_section, journal_entry);
1727 if (unlikely(!dio->write)) {
1728 struct journal_sector *js;
1732 if (unlikely(journal_entry_is_inprogress(je))) {
1733 flush_dcache_page(bv.bv_page);
1736 __io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je));
1740 BUG_ON(journal_entry_get_sector(je) != logical_sector);
1741 js = access_journal_data(ic, journal_section, journal_entry);
1742 mem_ptr = mem + bv.bv_offset;
1745 memcpy(mem_ptr, js, JOURNAL_SECTOR_DATA);
1746 *(commit_id_t *)(mem_ptr + JOURNAL_SECTOR_DATA) = je->last_bytes[s];
1748 mem_ptr += 1 << SECTOR_SHIFT;
1749 } while (++s < ic->sectors_per_block);
1750 #ifdef INTERNAL_VERIFY
1751 if (ic->internal_hash) {
1752 char checksums_onstack[max((size_t)HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)];
1754 integrity_sector_checksum(ic, logical_sector, mem + bv.bv_offset, checksums_onstack);
1755 if (unlikely(memcmp(checksums_onstack, journal_entry_tag(ic, je), ic->tag_size))) {
1756 DMERR_LIMIT("Checksum failed when reading from journal, at sector 0x%llx",
1763 if (!ic->internal_hash) {
1764 struct bio_integrity_payload *bip = bio_integrity(bio);
1765 unsigned tag_todo = ic->tag_size;
1766 char *tag_ptr = journal_entry_tag(ic, je);
1769 struct bio_vec biv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter);
1770 unsigned tag_now = min(biv.bv_len, tag_todo);
1772 BUG_ON(PageHighMem(biv.bv_page));
1773 tag_addr = lowmem_page_address(biv.bv_page) + biv.bv_offset;
1774 if (likely(dio->write))
1775 memcpy(tag_ptr, tag_addr, tag_now);
1777 memcpy(tag_addr, tag_ptr, tag_now);
1778 bvec_iter_advance(bip->bip_vec, &bip->bip_iter, tag_now);
1780 tag_todo -= tag_now;
1781 } while (unlikely(tag_todo)); else {
1782 if (likely(dio->write))
1783 memset(tag_ptr, 0, tag_todo);
1787 if (likely(dio->write)) {
1788 struct journal_sector *js;
1791 js = access_journal_data(ic, journal_section, journal_entry);
1792 memcpy(js, mem + bv.bv_offset, ic->sectors_per_block << SECTOR_SHIFT);
1796 je->last_bytes[s] = js[s].commit_id;
1797 } while (++s < ic->sectors_per_block);
1799 if (ic->internal_hash) {
1800 unsigned digest_size = crypto_shash_digestsize(ic->internal_hash);
1801 if (unlikely(digest_size > ic->tag_size)) {
1802 char checksums_onstack[HASH_MAX_DIGESTSIZE];
1803 integrity_sector_checksum(ic, logical_sector, (char *)js, checksums_onstack);
1804 memcpy(journal_entry_tag(ic, je), checksums_onstack, ic->tag_size);
1806 integrity_sector_checksum(ic, logical_sector, (char *)js, journal_entry_tag(ic, je));
1809 journal_entry_set_sector(je, logical_sector);
1811 logical_sector += ic->sectors_per_block;
1814 if (unlikely(journal_entry == ic->journal_section_entries)) {
1817 wraparound_section(ic, &journal_section);
1820 bv.bv_offset += ic->sectors_per_block << SECTOR_SHIFT;
1821 } while (bv.bv_len -= ic->sectors_per_block << SECTOR_SHIFT);
1823 if (unlikely(!dio->write))
1824 flush_dcache_page(bv.bv_page);
1826 } while (n_sectors);
1828 if (likely(dio->write)) {
1830 if (unlikely(waitqueue_active(&ic->copy_to_journal_wait)))
1831 wake_up(&ic->copy_to_journal_wait);
1832 if (READ_ONCE(ic->free_sectors) <= ic->free_sectors_threshold) {
1833 queue_work(ic->commit_wq, &ic->commit_work);
1835 schedule_autocommit(ic);
1838 remove_range(ic, &dio->range);
1841 if (unlikely(bio->bi_iter.bi_size)) {
1842 sector_t area, offset;
1844 dio->range.logical_sector = logical_sector;
1845 get_area_and_offset(ic, dio->range.logical_sector, &area, &offset);
1846 dio->metadata_block = get_metadata_sector_and_offset(ic, area, offset, &dio->metadata_offset);
1853 static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map)
1855 struct dm_integrity_c *ic = dio->ic;
1856 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1857 unsigned journal_section, journal_entry;
1858 unsigned journal_read_pos;
1859 struct completion read_comp;
1860 bool need_sync_io = ic->internal_hash && !dio->write;
1862 if (need_sync_io && from_map) {
1863 INIT_WORK(&dio->work, integrity_bio_wait);
1864 queue_work(ic->offload_wq, &dio->work);
1869 spin_lock_irq(&ic->endio_wait.lock);
1871 if (unlikely(dm_integrity_failed(ic))) {
1872 spin_unlock_irq(&ic->endio_wait.lock);
1876 dio->range.n_sectors = bio_sectors(bio);
1877 journal_read_pos = NOT_FOUND;
1878 if (likely(ic->mode == 'J')) {
1880 unsigned next_entry, i, pos;
1881 unsigned ws, we, range_sectors;
1883 dio->range.n_sectors = min(dio->range.n_sectors,
1884 (sector_t)ic->free_sectors << ic->sb->log2_sectors_per_block);
1885 if (unlikely(!dio->range.n_sectors)) {
1887 goto offload_to_thread;
1888 sleep_on_endio_wait(ic);
1891 range_sectors = dio->range.n_sectors >> ic->sb->log2_sectors_per_block;
1892 ic->free_sectors -= range_sectors;
1893 journal_section = ic->free_section;
1894 journal_entry = ic->free_section_entry;
1896 next_entry = ic->free_section_entry + range_sectors;
1897 ic->free_section_entry = next_entry % ic->journal_section_entries;
1898 ic->free_section += next_entry / ic->journal_section_entries;
1899 ic->n_uncommitted_sections += next_entry / ic->journal_section_entries;
1900 wraparound_section(ic, &ic->free_section);
1902 pos = journal_section * ic->journal_section_entries + journal_entry;
1903 ws = journal_section;
1907 struct journal_entry *je;
1909 add_journal_node(ic, &ic->journal_tree[pos], dio->range.logical_sector + i);
1911 if (unlikely(pos >= ic->journal_entries))
1914 je = access_journal_entry(ic, ws, we);
1915 BUG_ON(!journal_entry_is_unused(je));
1916 journal_entry_set_inprogress(je);
1918 if (unlikely(we == ic->journal_section_entries)) {
1921 wraparound_section(ic, &ws);
1923 } while ((i += ic->sectors_per_block) < dio->range.n_sectors);
1925 spin_unlock_irq(&ic->endio_wait.lock);
1926 goto journal_read_write;
1928 sector_t next_sector;
1929 journal_read_pos = find_journal_node(ic, dio->range.logical_sector, &next_sector);
1930 if (likely(journal_read_pos == NOT_FOUND)) {
1931 if (unlikely(dio->range.n_sectors > next_sector - dio->range.logical_sector))
1932 dio->range.n_sectors = next_sector - dio->range.logical_sector;
1935 unsigned jp = journal_read_pos + 1;
1936 for (i = ic->sectors_per_block; i < dio->range.n_sectors; i += ic->sectors_per_block, jp++) {
1937 if (!test_journal_node(ic, jp, dio->range.logical_sector + i))
1940 dio->range.n_sectors = i;
1944 if (unlikely(!add_new_range(ic, &dio->range, true))) {
1946 * We must not sleep in the request routine because it could
1947 * stall bios on current->bio_list.
1948 * So, we offload the bio to a workqueue if we have to sleep.
1952 spin_unlock_irq(&ic->endio_wait.lock);
1953 INIT_WORK(&dio->work, integrity_bio_wait);
1954 queue_work(ic->wait_wq, &dio->work);
1957 if (journal_read_pos != NOT_FOUND)
1958 dio->range.n_sectors = ic->sectors_per_block;
1959 wait_and_add_new_range(ic, &dio->range);
1961 * wait_and_add_new_range drops the spinlock, so the journal
1962 * may have been changed arbitrarily. We need to recheck.
1963 * To simplify the code, we restrict I/O size to just one block.
1965 if (journal_read_pos != NOT_FOUND) {
1966 sector_t next_sector;
1967 unsigned new_pos = find_journal_node(ic, dio->range.logical_sector, &next_sector);
1968 if (unlikely(new_pos != journal_read_pos)) {
1969 remove_range_unlocked(ic, &dio->range);
1974 spin_unlock_irq(&ic->endio_wait.lock);
1976 if (unlikely(journal_read_pos != NOT_FOUND)) {
1977 journal_section = journal_read_pos / ic->journal_section_entries;
1978 journal_entry = journal_read_pos % ic->journal_section_entries;
1979 goto journal_read_write;
1982 if (ic->mode == 'B' && dio->write) {
1983 if (!block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector,
1984 dio->range.n_sectors, BITMAP_OP_TEST_ALL_SET)) {
1985 struct bitmap_block_status *bbs;
1987 bbs = sector_to_bitmap_block(ic, dio->range.logical_sector);
1988 spin_lock(&bbs->bio_queue_lock);
1989 bio_list_add(&bbs->bio_queue, bio);
1990 spin_unlock(&bbs->bio_queue_lock);
1991 queue_work(ic->writer_wq, &bbs->work);
1996 dio->in_flight = (atomic_t)ATOMIC_INIT(2);
1999 init_completion(&read_comp);
2000 dio->completion = &read_comp;
2002 dio->completion = NULL;
2004 dm_bio_record(&dio->bio_details, bio);
2005 bio_set_dev(bio, ic->dev->bdev);
2006 bio->bi_integrity = NULL;
2007 bio->bi_opf &= ~REQ_INTEGRITY;
2008 bio->bi_end_io = integrity_end_io;
2009 bio->bi_iter.bi_size = dio->range.n_sectors << SECTOR_SHIFT;
2011 generic_make_request(bio);
2014 wait_for_completion_io(&read_comp);
2015 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING) &&
2016 dio->range.logical_sector + dio->range.n_sectors > le64_to_cpu(ic->sb->recalc_sector))
2018 if (ic->mode == 'B') {
2019 if (!block_bitmap_op(ic, ic->recalc_bitmap, dio->range.logical_sector,
2020 dio->range.n_sectors, BITMAP_OP_TEST_ALL_CLEAR))
2024 if (likely(!bio->bi_status))
2025 integrity_metadata(&dio->work);
2031 INIT_WORK(&dio->work, integrity_metadata);
2032 queue_work(ic->metadata_wq, &dio->work);
2038 if (unlikely(__journal_read_write(dio, bio, journal_section, journal_entry)))
2041 do_endio_flush(ic, dio);
2045 static void integrity_bio_wait(struct work_struct *w)
2047 struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work);
2049 dm_integrity_map_continue(dio, false);
2052 static void pad_uncommitted(struct dm_integrity_c *ic)
2054 if (ic->free_section_entry) {
2055 ic->free_sectors -= ic->journal_section_entries - ic->free_section_entry;
2056 ic->free_section_entry = 0;
2058 wraparound_section(ic, &ic->free_section);
2059 ic->n_uncommitted_sections++;
2061 if (WARN_ON(ic->journal_sections * ic->journal_section_entries !=
2062 (ic->n_uncommitted_sections + ic->n_committed_sections) *
2063 ic->journal_section_entries + ic->free_sectors)) {
2064 DMCRIT("journal_sections %u, journal_section_entries %u, "
2065 "n_uncommitted_sections %u, n_committed_sections %u, "
2066 "journal_section_entries %u, free_sectors %u",
2067 ic->journal_sections, ic->journal_section_entries,
2068 ic->n_uncommitted_sections, ic->n_committed_sections,
2069 ic->journal_section_entries, ic->free_sectors);
2073 static void integrity_commit(struct work_struct *w)
2075 struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, commit_work);
2076 unsigned commit_start, commit_sections;
2078 struct bio *flushes;
2080 del_timer(&ic->autocommit_timer);
2082 spin_lock_irq(&ic->endio_wait.lock);
2083 flushes = bio_list_get(&ic->flush_bio_list);
2084 if (unlikely(ic->mode != 'J')) {
2085 spin_unlock_irq(&ic->endio_wait.lock);
2086 dm_integrity_flush_buffers(ic);
2087 goto release_flush_bios;
2090 pad_uncommitted(ic);
2091 commit_start = ic->uncommitted_section;
2092 commit_sections = ic->n_uncommitted_sections;
2093 spin_unlock_irq(&ic->endio_wait.lock);
2095 if (!commit_sections)
2096 goto release_flush_bios;
2099 for (n = 0; n < commit_sections; n++) {
2100 for (j = 0; j < ic->journal_section_entries; j++) {
2101 struct journal_entry *je;
2102 je = access_journal_entry(ic, i, j);
2103 io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je));
2105 for (j = 0; j < ic->journal_section_sectors; j++) {
2106 struct journal_sector *js;
2107 js = access_journal(ic, i, j);
2108 js->commit_id = dm_integrity_commit_id(ic, i, j, ic->commit_seq);
2111 if (unlikely(i >= ic->journal_sections))
2112 ic->commit_seq = next_commit_seq(ic->commit_seq);
2113 wraparound_section(ic, &i);
2117 write_journal(ic, commit_start, commit_sections);
2119 spin_lock_irq(&ic->endio_wait.lock);
2120 ic->uncommitted_section += commit_sections;
2121 wraparound_section(ic, &ic->uncommitted_section);
2122 ic->n_uncommitted_sections -= commit_sections;
2123 ic->n_committed_sections += commit_sections;
2124 spin_unlock_irq(&ic->endio_wait.lock);
2126 if (READ_ONCE(ic->free_sectors) <= ic->free_sectors_threshold)
2127 queue_work(ic->writer_wq, &ic->writer_work);
2131 struct bio *next = flushes->bi_next;
2132 flushes->bi_next = NULL;
2133 do_endio(ic, flushes);
2138 static void complete_copy_from_journal(unsigned long error, void *context)
2140 struct journal_io *io = context;
2141 struct journal_completion *comp = io->comp;
2142 struct dm_integrity_c *ic = comp->ic;
2143 remove_range(ic, &io->range);
2144 mempool_free(io, &ic->journal_io_mempool);
2145 if (unlikely(error != 0))
2146 dm_integrity_io_error(ic, "copying from journal", -EIO);
2147 complete_journal_op(comp);
2150 static void restore_last_bytes(struct dm_integrity_c *ic, struct journal_sector *js,
2151 struct journal_entry *je)
2155 js->commit_id = je->last_bytes[s];
2157 } while (++s < ic->sectors_per_block);
2160 static void do_journal_write(struct dm_integrity_c *ic, unsigned write_start,
2161 unsigned write_sections, bool from_replay)
2164 struct journal_completion comp;
2165 struct blk_plug plug;
2167 blk_start_plug(&plug);
2170 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
2171 init_completion(&comp.comp);
2174 for (n = 0; n < write_sections; n++, i++, wraparound_section(ic, &i)) {
2175 #ifndef INTERNAL_VERIFY
2176 if (unlikely(from_replay))
2178 rw_section_mac(ic, i, false);
2179 for (j = 0; j < ic->journal_section_entries; j++) {
2180 struct journal_entry *je = access_journal_entry(ic, i, j);
2181 sector_t sec, area, offset;
2182 unsigned k, l, next_loop;
2183 sector_t metadata_block;
2184 unsigned metadata_offset;
2185 struct journal_io *io;
2187 if (journal_entry_is_unused(je))
2189 BUG_ON(unlikely(journal_entry_is_inprogress(je)) && !from_replay);
2190 sec = journal_entry_get_sector(je);
2191 if (unlikely(from_replay)) {
2192 if (unlikely(sec & (unsigned)(ic->sectors_per_block - 1))) {
2193 dm_integrity_io_error(ic, "invalid sector in journal", -EIO);
2194 sec &= ~(sector_t)(ic->sectors_per_block - 1);
2197 if (unlikely(sec >= ic->provided_data_sectors))
2199 get_area_and_offset(ic, sec, &area, &offset);
2200 restore_last_bytes(ic, access_journal_data(ic, i, j), je);
2201 for (k = j + 1; k < ic->journal_section_entries; k++) {
2202 struct journal_entry *je2 = access_journal_entry(ic, i, k);
2203 sector_t sec2, area2, offset2;
2204 if (journal_entry_is_unused(je2))
2206 BUG_ON(unlikely(journal_entry_is_inprogress(je2)) && !from_replay);
2207 sec2 = journal_entry_get_sector(je2);
2208 if (unlikely(sec2 >= ic->provided_data_sectors))
2210 get_area_and_offset(ic, sec2, &area2, &offset2);
2211 if (area2 != area || offset2 != offset + ((k - j) << ic->sb->log2_sectors_per_block))
2213 restore_last_bytes(ic, access_journal_data(ic, i, k), je2);
2217 io = mempool_alloc(&ic->journal_io_mempool, GFP_NOIO);
2219 io->range.logical_sector = sec;
2220 io->range.n_sectors = (k - j) << ic->sb->log2_sectors_per_block;
2222 spin_lock_irq(&ic->endio_wait.lock);
2223 add_new_range_and_wait(ic, &io->range);
2225 if (likely(!from_replay)) {
2226 struct journal_node *section_node = &ic->journal_tree[i * ic->journal_section_entries];
2228 /* don't write if there is newer committed sector */
2229 while (j < k && find_newer_committed_node(ic, §ion_node[j])) {
2230 struct journal_entry *je2 = access_journal_entry(ic, i, j);
2232 journal_entry_set_unused(je2);
2233 remove_journal_node(ic, §ion_node[j]);
2235 sec += ic->sectors_per_block;
2236 offset += ic->sectors_per_block;
2238 while (j < k && find_newer_committed_node(ic, §ion_node[k - 1])) {
2239 struct journal_entry *je2 = access_journal_entry(ic, i, k - 1);
2241 journal_entry_set_unused(je2);
2242 remove_journal_node(ic, §ion_node[k - 1]);
2246 remove_range_unlocked(ic, &io->range);
2247 spin_unlock_irq(&ic->endio_wait.lock);
2248 mempool_free(io, &ic->journal_io_mempool);
2251 for (l = j; l < k; l++) {
2252 remove_journal_node(ic, §ion_node[l]);
2255 spin_unlock_irq(&ic->endio_wait.lock);
2257 metadata_block = get_metadata_sector_and_offset(ic, area, offset, &metadata_offset);
2258 for (l = j; l < k; l++) {
2260 struct journal_entry *je2 = access_journal_entry(ic, i, l);
2263 #ifndef INTERNAL_VERIFY
2264 unlikely(from_replay) &&
2266 ic->internal_hash) {
2267 char test_tag[max_t(size_t, HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)];
2269 integrity_sector_checksum(ic, sec + ((l - j) << ic->sb->log2_sectors_per_block),
2270 (char *)access_journal_data(ic, i, l), test_tag);
2271 if (unlikely(memcmp(test_tag, journal_entry_tag(ic, je2), ic->tag_size)))
2272 dm_integrity_io_error(ic, "tag mismatch when replaying journal", -EILSEQ);
2275 journal_entry_set_unused(je2);
2276 r = dm_integrity_rw_tag(ic, journal_entry_tag(ic, je2), &metadata_block, &metadata_offset,
2277 ic->tag_size, TAG_WRITE);
2279 dm_integrity_io_error(ic, "reading tags", r);
2283 atomic_inc(&comp.in_flight);
2284 copy_from_journal(ic, i, j << ic->sb->log2_sectors_per_block,
2285 (k - j) << ic->sb->log2_sectors_per_block,
2286 get_data_sector(ic, area, offset),
2287 complete_copy_from_journal, io);
2293 dm_bufio_write_dirty_buffers_async(ic->bufio);
2295 blk_finish_plug(&plug);
2297 complete_journal_op(&comp);
2298 wait_for_completion_io(&comp.comp);
2300 dm_integrity_flush_buffers(ic);
2303 static void integrity_writer(struct work_struct *w)
2305 struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, writer_work);
2306 unsigned write_start, write_sections;
2308 unsigned prev_free_sectors;
2310 /* the following test is not needed, but it tests the replay code */
2311 if (unlikely(dm_suspended(ic->ti)) && !ic->meta_dev)
2314 spin_lock_irq(&ic->endio_wait.lock);
2315 write_start = ic->committed_section;
2316 write_sections = ic->n_committed_sections;
2317 spin_unlock_irq(&ic->endio_wait.lock);
2319 if (!write_sections)
2322 do_journal_write(ic, write_start, write_sections, false);
2324 spin_lock_irq(&ic->endio_wait.lock);
2326 ic->committed_section += write_sections;
2327 wraparound_section(ic, &ic->committed_section);
2328 ic->n_committed_sections -= write_sections;
2330 prev_free_sectors = ic->free_sectors;
2331 ic->free_sectors += write_sections * ic->journal_section_entries;
2332 if (unlikely(!prev_free_sectors))
2333 wake_up_locked(&ic->endio_wait);
2335 spin_unlock_irq(&ic->endio_wait.lock);
2338 static void recalc_write_super(struct dm_integrity_c *ic)
2342 dm_integrity_flush_buffers(ic);
2343 if (dm_integrity_failed(ic))
2346 r = sync_rw_sb(ic, REQ_OP_WRITE, 0);
2348 dm_integrity_io_error(ic, "writing superblock", r);
2351 static void integrity_recalc(struct work_struct *w)
2353 struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, recalc_work);
2354 struct dm_integrity_range range;
2355 struct dm_io_request io_req;
2356 struct dm_io_region io_loc;
2357 sector_t area, offset;
2358 sector_t metadata_block;
2359 unsigned metadata_offset;
2360 sector_t logical_sector, n_sectors;
2364 unsigned super_counter = 0;
2366 DEBUG_print("start recalculation... (position %llx)\n", le64_to_cpu(ic->sb->recalc_sector));
2368 spin_lock_irq(&ic->endio_wait.lock);
2372 if (unlikely(dm_suspended(ic->ti)))
2375 range.logical_sector = le64_to_cpu(ic->sb->recalc_sector);
2376 if (unlikely(range.logical_sector >= ic->provided_data_sectors)) {
2377 if (ic->mode == 'B') {
2378 DEBUG_print("queue_delayed_work: bitmap_flush_work\n");
2379 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0);
2384 get_area_and_offset(ic, range.logical_sector, &area, &offset);
2385 range.n_sectors = min((sector_t)RECALC_SECTORS, ic->provided_data_sectors - range.logical_sector);
2387 range.n_sectors = min(range.n_sectors, ((sector_t)1U << ic->sb->log2_interleave_sectors) - (unsigned)offset);
2389 add_new_range_and_wait(ic, &range);
2390 spin_unlock_irq(&ic->endio_wait.lock);
2391 logical_sector = range.logical_sector;
2392 n_sectors = range.n_sectors;
2394 if (ic->mode == 'B') {
2395 if (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector, n_sectors, BITMAP_OP_TEST_ALL_CLEAR)) {
2396 goto advance_and_next;
2398 while (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector,
2399 ic->sectors_per_block, BITMAP_OP_TEST_ALL_CLEAR)) {
2400 logical_sector += ic->sectors_per_block;
2401 n_sectors -= ic->sectors_per_block;
2404 while (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector + n_sectors - ic->sectors_per_block,
2405 ic->sectors_per_block, BITMAP_OP_TEST_ALL_CLEAR)) {
2406 n_sectors -= ic->sectors_per_block;
2409 get_area_and_offset(ic, logical_sector, &area, &offset);
2412 DEBUG_print("recalculating: %llx, %llx\n", logical_sector, n_sectors);
2414 if (unlikely(++super_counter == RECALC_WRITE_SUPER)) {
2415 recalc_write_super(ic);
2416 if (ic->mode == 'B') {
2417 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, ic->bitmap_flush_interval);
2422 if (unlikely(dm_integrity_failed(ic)))
2425 io_req.bi_op = REQ_OP_READ;
2426 io_req.bi_op_flags = 0;
2427 io_req.mem.type = DM_IO_VMA;
2428 io_req.mem.ptr.addr = ic->recalc_buffer;
2429 io_req.notify.fn = NULL;
2430 io_req.client = ic->io;
2431 io_loc.bdev = ic->dev->bdev;
2432 io_loc.sector = get_data_sector(ic, area, offset);
2433 io_loc.count = n_sectors;
2435 r = dm_io(&io_req, 1, &io_loc, NULL);
2437 dm_integrity_io_error(ic, "reading data", r);
2441 t = ic->recalc_tags;
2442 for (i = 0; i < n_sectors; i += ic->sectors_per_block) {
2443 integrity_sector_checksum(ic, logical_sector + i, ic->recalc_buffer + (i << SECTOR_SHIFT), t);
2447 metadata_block = get_metadata_sector_and_offset(ic, area, offset, &metadata_offset);
2449 r = dm_integrity_rw_tag(ic, ic->recalc_tags, &metadata_block, &metadata_offset, t - ic->recalc_tags, TAG_WRITE);
2451 dm_integrity_io_error(ic, "writing tags", r);
2458 spin_lock_irq(&ic->endio_wait.lock);
2459 remove_range_unlocked(ic, &range);
2460 ic->sb->recalc_sector = cpu_to_le64(range.logical_sector + range.n_sectors);
2464 remove_range(ic, &range);
2468 spin_unlock_irq(&ic->endio_wait.lock);
2470 recalc_write_super(ic);
2473 static void bitmap_block_work(struct work_struct *w)
2475 struct bitmap_block_status *bbs = container_of(w, struct bitmap_block_status, work);
2476 struct dm_integrity_c *ic = bbs->ic;
2478 struct bio_list bio_queue;
2479 struct bio_list waiting;
2481 bio_list_init(&waiting);
2483 spin_lock(&bbs->bio_queue_lock);
2484 bio_queue = bbs->bio_queue;
2485 bio_list_init(&bbs->bio_queue);
2486 spin_unlock(&bbs->bio_queue_lock);
2488 while ((bio = bio_list_pop(&bio_queue))) {
2489 struct dm_integrity_io *dio;
2491 dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
2493 if (block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector,
2494 dio->range.n_sectors, BITMAP_OP_TEST_ALL_SET)) {
2495 remove_range(ic, &dio->range);
2496 INIT_WORK(&dio->work, integrity_bio_wait);
2497 queue_work(ic->offload_wq, &dio->work);
2499 block_bitmap_op(ic, ic->journal, dio->range.logical_sector,
2500 dio->range.n_sectors, BITMAP_OP_SET);
2501 bio_list_add(&waiting, bio);
2505 if (bio_list_empty(&waiting))
2508 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC,
2509 bbs->idx * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT),
2510 BITMAP_BLOCK_SIZE >> SECTOR_SHIFT, NULL);
2512 while ((bio = bio_list_pop(&waiting))) {
2513 struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
2515 block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector,
2516 dio->range.n_sectors, BITMAP_OP_SET);
2518 remove_range(ic, &dio->range);
2519 INIT_WORK(&dio->work, integrity_bio_wait);
2520 queue_work(ic->offload_wq, &dio->work);
2523 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, ic->bitmap_flush_interval);
2526 static void bitmap_flush_work(struct work_struct *work)
2528 struct dm_integrity_c *ic = container_of(work, struct dm_integrity_c, bitmap_flush_work.work);
2529 struct dm_integrity_range range;
2530 unsigned long limit;
2533 dm_integrity_flush_buffers(ic);
2535 range.logical_sector = 0;
2536 range.n_sectors = ic->provided_data_sectors;
2538 spin_lock_irq(&ic->endio_wait.lock);
2539 add_new_range_and_wait(ic, &range);
2540 spin_unlock_irq(&ic->endio_wait.lock);
2542 dm_integrity_flush_buffers(ic);
2544 blkdev_issue_flush(ic->dev->bdev, GFP_NOIO, NULL);
2546 limit = ic->provided_data_sectors;
2547 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) {
2548 limit = le64_to_cpu(ic->sb->recalc_sector)
2549 >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit)
2550 << (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
2552 /*DEBUG_print("zeroing journal\n");*/
2553 block_bitmap_op(ic, ic->journal, 0, limit, BITMAP_OP_CLEAR);
2554 block_bitmap_op(ic, ic->may_write_bitmap, 0, limit, BITMAP_OP_CLEAR);
2556 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, 0,
2557 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
2559 spin_lock_irq(&ic->endio_wait.lock);
2560 remove_range_unlocked(ic, &range);
2561 while (unlikely((bio = bio_list_pop(&ic->synchronous_bios)) != NULL)) {
2563 spin_unlock_irq(&ic->endio_wait.lock);
2564 spin_lock_irq(&ic->endio_wait.lock);
2566 spin_unlock_irq(&ic->endio_wait.lock);
2570 static void init_journal(struct dm_integrity_c *ic, unsigned start_section,
2571 unsigned n_sections, unsigned char commit_seq)
2578 for (n = 0; n < n_sections; n++) {
2579 i = start_section + n;
2580 wraparound_section(ic, &i);
2581 for (j = 0; j < ic->journal_section_sectors; j++) {
2582 struct journal_sector *js = access_journal(ic, i, j);
2583 memset(&js->entries, 0, JOURNAL_SECTOR_DATA);
2584 js->commit_id = dm_integrity_commit_id(ic, i, j, commit_seq);
2586 for (j = 0; j < ic->journal_section_entries; j++) {
2587 struct journal_entry *je = access_journal_entry(ic, i, j);
2588 journal_entry_set_unused(je);
2592 write_journal(ic, start_section, n_sections);
2595 static int find_commit_seq(struct dm_integrity_c *ic, unsigned i, unsigned j, commit_id_t id)
2598 for (k = 0; k < N_COMMIT_IDS; k++) {
2599 if (dm_integrity_commit_id(ic, i, j, k) == id)
2602 dm_integrity_io_error(ic, "journal commit id", -EIO);
2606 static void replay_journal(struct dm_integrity_c *ic)
2609 bool used_commit_ids[N_COMMIT_IDS];
2610 unsigned max_commit_id_sections[N_COMMIT_IDS];
2611 unsigned write_start, write_sections;
2612 unsigned continue_section;
2614 unsigned char unused, last_used, want_commit_seq;
2616 if (ic->mode == 'R')
2619 if (ic->journal_uptodate)
2625 if (!ic->just_formatted) {
2626 DEBUG_print("reading journal\n");
2627 rw_journal(ic, REQ_OP_READ, 0, 0, ic->journal_sections, NULL);
2629 DEBUG_bytes(lowmem_page_address(ic->journal_io[0].page), 64, "read journal");
2630 if (ic->journal_io) {
2631 struct journal_completion crypt_comp;
2633 init_completion(&crypt_comp.comp);
2634 crypt_comp.in_flight = (atomic_t)ATOMIC_INIT(0);
2635 encrypt_journal(ic, false, 0, ic->journal_sections, &crypt_comp);
2636 wait_for_completion(&crypt_comp.comp);
2638 DEBUG_bytes(lowmem_page_address(ic->journal[0].page), 64, "decrypted journal");
2641 if (dm_integrity_failed(ic))
2644 journal_empty = true;
2645 memset(used_commit_ids, 0, sizeof used_commit_ids);
2646 memset(max_commit_id_sections, 0, sizeof max_commit_id_sections);
2647 for (i = 0; i < ic->journal_sections; i++) {
2648 for (j = 0; j < ic->journal_section_sectors; j++) {
2650 struct journal_sector *js = access_journal(ic, i, j);
2651 k = find_commit_seq(ic, i, j, js->commit_id);
2654 used_commit_ids[k] = true;
2655 max_commit_id_sections[k] = i;
2657 if (journal_empty) {
2658 for (j = 0; j < ic->journal_section_entries; j++) {
2659 struct journal_entry *je = access_journal_entry(ic, i, j);
2660 if (!journal_entry_is_unused(je)) {
2661 journal_empty = false;
2668 if (!used_commit_ids[N_COMMIT_IDS - 1]) {
2669 unused = N_COMMIT_IDS - 1;
2670 while (unused && !used_commit_ids[unused - 1])
2673 for (unused = 0; unused < N_COMMIT_IDS; unused++)
2674 if (!used_commit_ids[unused])
2676 if (unused == N_COMMIT_IDS) {
2677 dm_integrity_io_error(ic, "journal commit ids", -EIO);
2681 DEBUG_print("first unused commit seq %d [%d,%d,%d,%d]\n",
2682 unused, used_commit_ids[0], used_commit_ids[1],
2683 used_commit_ids[2], used_commit_ids[3]);
2685 last_used = prev_commit_seq(unused);
2686 want_commit_seq = prev_commit_seq(last_used);
2688 if (!used_commit_ids[want_commit_seq] && used_commit_ids[prev_commit_seq(want_commit_seq)])
2689 journal_empty = true;
2691 write_start = max_commit_id_sections[last_used] + 1;
2692 if (unlikely(write_start >= ic->journal_sections))
2693 want_commit_seq = next_commit_seq(want_commit_seq);
2694 wraparound_section(ic, &write_start);
2697 for (write_sections = 0; write_sections < ic->journal_sections; write_sections++) {
2698 for (j = 0; j < ic->journal_section_sectors; j++) {
2699 struct journal_sector *js = access_journal(ic, i, j);
2701 if (js->commit_id != dm_integrity_commit_id(ic, i, j, want_commit_seq)) {
2703 * This could be caused by crash during writing.
2704 * We won't replay the inconsistent part of the
2707 DEBUG_print("commit id mismatch at position (%u, %u): %d != %d\n",
2708 i, j, find_commit_seq(ic, i, j, js->commit_id), want_commit_seq);
2713 if (unlikely(i >= ic->journal_sections))
2714 want_commit_seq = next_commit_seq(want_commit_seq);
2715 wraparound_section(ic, &i);
2719 if (!journal_empty) {
2720 DEBUG_print("replaying %u sections, starting at %u, commit seq %d\n",
2721 write_sections, write_start, want_commit_seq);
2722 do_journal_write(ic, write_start, write_sections, true);
2725 if (write_sections == ic->journal_sections && (ic->mode == 'J' || journal_empty)) {
2726 continue_section = write_start;
2727 ic->commit_seq = want_commit_seq;
2728 DEBUG_print("continuing from section %u, commit seq %d\n", write_start, ic->commit_seq);
2731 unsigned char erase_seq;
2733 DEBUG_print("clearing journal\n");
2735 erase_seq = prev_commit_seq(prev_commit_seq(last_used));
2737 init_journal(ic, s, 1, erase_seq);
2739 wraparound_section(ic, &s);
2740 if (ic->journal_sections >= 2) {
2741 init_journal(ic, s, ic->journal_sections - 2, erase_seq);
2742 s += ic->journal_sections - 2;
2743 wraparound_section(ic, &s);
2744 init_journal(ic, s, 1, erase_seq);
2747 continue_section = 0;
2748 ic->commit_seq = next_commit_seq(erase_seq);
2751 ic->committed_section = continue_section;
2752 ic->n_committed_sections = 0;
2754 ic->uncommitted_section = continue_section;
2755 ic->n_uncommitted_sections = 0;
2757 ic->free_section = continue_section;
2758 ic->free_section_entry = 0;
2759 ic->free_sectors = ic->journal_entries;
2761 ic->journal_tree_root = RB_ROOT;
2762 for (i = 0; i < ic->journal_entries; i++)
2763 init_journal_node(&ic->journal_tree[i]);
2766 static void dm_integrity_enter_synchronous_mode(struct dm_integrity_c *ic)
2768 DEBUG_print("dm_integrity_enter_synchronous_mode\n");
2770 if (ic->mode == 'B') {
2771 ic->bitmap_flush_interval = msecs_to_jiffies(10) + 1;
2772 ic->synchronous_mode = 1;
2774 cancel_delayed_work_sync(&ic->bitmap_flush_work);
2775 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0);
2776 flush_workqueue(ic->commit_wq);
2780 static int dm_integrity_reboot(struct notifier_block *n, unsigned long code, void *x)
2782 struct dm_integrity_c *ic = container_of(n, struct dm_integrity_c, reboot_notifier);
2784 DEBUG_print("dm_integrity_reboot\n");
2786 dm_integrity_enter_synchronous_mode(ic);
2791 static void dm_integrity_postsuspend(struct dm_target *ti)
2793 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
2796 WARN_ON(unregister_reboot_notifier(&ic->reboot_notifier));
2798 del_timer_sync(&ic->autocommit_timer);
2801 drain_workqueue(ic->recalc_wq);
2803 if (ic->mode == 'B')
2804 cancel_delayed_work_sync(&ic->bitmap_flush_work);
2806 queue_work(ic->commit_wq, &ic->commit_work);
2807 drain_workqueue(ic->commit_wq);
2809 if (ic->mode == 'J') {
2811 queue_work(ic->writer_wq, &ic->writer_work);
2812 drain_workqueue(ic->writer_wq);
2813 dm_integrity_flush_buffers(ic);
2816 if (ic->mode == 'B') {
2817 dm_integrity_flush_buffers(ic);
2819 /* set to 0 to test bitmap replay code */
2820 init_journal(ic, 0, ic->journal_sections, 0);
2821 ic->sb->flags &= ~cpu_to_le32(SB_FLAG_DIRTY_BITMAP);
2822 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
2824 dm_integrity_io_error(ic, "writing superblock", r);
2828 BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress));
2830 ic->journal_uptodate = true;
2833 static void dm_integrity_resume(struct dm_target *ti)
2835 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
2836 __u64 old_provided_data_sectors = le64_to_cpu(ic->sb->provided_data_sectors);
2839 DEBUG_print("resume\n");
2841 if (ic->provided_data_sectors != old_provided_data_sectors) {
2842 if (ic->provided_data_sectors > old_provided_data_sectors &&
2844 ic->sb->log2_blocks_per_bitmap_bit == ic->log2_blocks_per_bitmap_bit) {
2845 rw_journal_sectors(ic, REQ_OP_READ, 0, 0,
2846 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
2847 block_bitmap_op(ic, ic->journal, old_provided_data_sectors,
2848 ic->provided_data_sectors - old_provided_data_sectors, BITMAP_OP_SET);
2849 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, 0,
2850 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
2853 ic->sb->provided_data_sectors = cpu_to_le64(ic->provided_data_sectors);
2854 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
2856 dm_integrity_io_error(ic, "writing superblock", r);
2859 if (ic->sb->flags & cpu_to_le32(SB_FLAG_DIRTY_BITMAP)) {
2860 DEBUG_print("resume dirty_bitmap\n");
2861 rw_journal_sectors(ic, REQ_OP_READ, 0, 0,
2862 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
2863 if (ic->mode == 'B') {
2864 if (ic->sb->log2_blocks_per_bitmap_bit == ic->log2_blocks_per_bitmap_bit) {
2865 block_bitmap_copy(ic, ic->recalc_bitmap, ic->journal);
2866 block_bitmap_copy(ic, ic->may_write_bitmap, ic->journal);
2867 if (!block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors,
2868 BITMAP_OP_TEST_ALL_CLEAR)) {
2869 ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
2870 ic->sb->recalc_sector = cpu_to_le64(0);
2873 DEBUG_print("non-matching blocks_per_bitmap_bit: %u, %u\n",
2874 ic->sb->log2_blocks_per_bitmap_bit, ic->log2_blocks_per_bitmap_bit);
2875 ic->sb->log2_blocks_per_bitmap_bit = ic->log2_blocks_per_bitmap_bit;
2876 block_bitmap_op(ic, ic->recalc_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_SET);
2877 block_bitmap_op(ic, ic->may_write_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_SET);
2878 block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_SET);
2879 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, 0,
2880 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
2881 ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
2882 ic->sb->recalc_sector = cpu_to_le64(0);
2885 if (!(ic->sb->log2_blocks_per_bitmap_bit == ic->log2_blocks_per_bitmap_bit &&
2886 block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_TEST_ALL_CLEAR))) {
2887 ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
2888 ic->sb->recalc_sector = cpu_to_le64(0);
2890 init_journal(ic, 0, ic->journal_sections, 0);
2892 ic->sb->flags &= ~cpu_to_le32(SB_FLAG_DIRTY_BITMAP);
2894 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
2896 dm_integrity_io_error(ic, "writing superblock", r);
2899 if (ic->mode == 'B') {
2900 ic->sb->flags |= cpu_to_le32(SB_FLAG_DIRTY_BITMAP);
2901 ic->sb->log2_blocks_per_bitmap_bit = ic->log2_blocks_per_bitmap_bit;
2902 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
2904 dm_integrity_io_error(ic, "writing superblock", r);
2906 block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR);
2907 block_bitmap_op(ic, ic->recalc_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR);
2908 block_bitmap_op(ic, ic->may_write_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR);
2909 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING) &&
2910 le64_to_cpu(ic->sb->recalc_sector) < ic->provided_data_sectors) {
2911 block_bitmap_op(ic, ic->journal, le64_to_cpu(ic->sb->recalc_sector),
2912 ic->provided_data_sectors - le64_to_cpu(ic->sb->recalc_sector), BITMAP_OP_SET);
2913 block_bitmap_op(ic, ic->recalc_bitmap, le64_to_cpu(ic->sb->recalc_sector),
2914 ic->provided_data_sectors - le64_to_cpu(ic->sb->recalc_sector), BITMAP_OP_SET);
2915 block_bitmap_op(ic, ic->may_write_bitmap, le64_to_cpu(ic->sb->recalc_sector),
2916 ic->provided_data_sectors - le64_to_cpu(ic->sb->recalc_sector), BITMAP_OP_SET);
2918 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, 0,
2919 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
2923 DEBUG_print("testing recalc: %x\n", ic->sb->flags);
2924 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) {
2925 __u64 recalc_pos = le64_to_cpu(ic->sb->recalc_sector);
2926 DEBUG_print("recalc pos: %llx / %llx\n", recalc_pos, ic->provided_data_sectors);
2927 if (recalc_pos < ic->provided_data_sectors) {
2928 queue_work(ic->recalc_wq, &ic->recalc_work);
2929 } else if (recalc_pos > ic->provided_data_sectors) {
2930 ic->sb->recalc_sector = cpu_to_le64(ic->provided_data_sectors);
2931 recalc_write_super(ic);
2935 ic->reboot_notifier.notifier_call = dm_integrity_reboot;
2936 ic->reboot_notifier.next = NULL;
2937 ic->reboot_notifier.priority = INT_MAX - 1; /* be notified after md and before hardware drivers */
2938 WARN_ON(register_reboot_notifier(&ic->reboot_notifier));
2941 /* set to 1 to stress test synchronous mode */
2942 dm_integrity_enter_synchronous_mode(ic);
2946 static void dm_integrity_status(struct dm_target *ti, status_type_t type,
2947 unsigned status_flags, char *result, unsigned maxlen)
2949 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
2954 case STATUSTYPE_INFO:
2956 atomic64_read(&ic->number_of_mismatches),
2957 ic->provided_data_sectors);
2958 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))
2959 DMEMIT(" %llu", le64_to_cpu(ic->sb->recalc_sector));
2964 case STATUSTYPE_TABLE: {
2965 __u64 watermark_percentage = (__u64)(ic->journal_entries - ic->free_sectors_threshold) * 100;
2966 watermark_percentage += ic->journal_entries / 2;
2967 do_div(watermark_percentage, ic->journal_entries);
2969 arg_count += !!ic->meta_dev;
2970 arg_count += ic->sectors_per_block != 1;
2971 arg_count += !!(ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING));
2972 arg_count += ic->mode == 'J';
2973 arg_count += ic->mode == 'J';
2974 arg_count += ic->mode == 'B';
2975 arg_count += ic->mode == 'B';
2976 arg_count += !!ic->internal_hash_alg.alg_string;
2977 arg_count += !!ic->journal_crypt_alg.alg_string;
2978 arg_count += !!ic->journal_mac_alg.alg_string;
2979 arg_count += (ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_PADDING)) != 0;
2980 DMEMIT("%s %llu %u %c %u", ic->dev->name, ic->start,
2981 ic->tag_size, ic->mode, arg_count);
2983 DMEMIT(" meta_device:%s", ic->meta_dev->name);
2984 if (ic->sectors_per_block != 1)
2985 DMEMIT(" block_size:%u", ic->sectors_per_block << SECTOR_SHIFT);
2986 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))
2987 DMEMIT(" recalculate");
2988 DMEMIT(" journal_sectors:%u", ic->initial_sectors - SB_SECTORS);
2989 DMEMIT(" interleave_sectors:%u", 1U << ic->sb->log2_interleave_sectors);
2990 DMEMIT(" buffer_sectors:%u", 1U << ic->log2_buffer_sectors);
2991 if (ic->mode == 'J') {
2992 DMEMIT(" journal_watermark:%u", (unsigned)watermark_percentage);
2993 DMEMIT(" commit_time:%u", ic->autocommit_msec);
2995 if (ic->mode == 'B') {
2996 DMEMIT(" sectors_per_bit:%llu", (sector_t)ic->sectors_per_block << ic->log2_blocks_per_bitmap_bit);
2997 DMEMIT(" bitmap_flush_interval:%u", jiffies_to_msecs(ic->bitmap_flush_interval));
2999 if ((ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_PADDING)) != 0)
3000 DMEMIT(" fix_padding");
3002 #define EMIT_ALG(a, n) \
3004 if (ic->a.alg_string) { \
3005 DMEMIT(" %s:%s", n, ic->a.alg_string); \
3006 if (ic->a.key_string) \
3007 DMEMIT(":%s", ic->a.key_string);\
3010 EMIT_ALG(internal_hash_alg, "internal_hash");
3011 EMIT_ALG(journal_crypt_alg, "journal_crypt");
3012 EMIT_ALG(journal_mac_alg, "journal_mac");
3018 static int dm_integrity_iterate_devices(struct dm_target *ti,
3019 iterate_devices_callout_fn fn, void *data)
3021 struct dm_integrity_c *ic = ti->private;
3024 return fn(ti, ic->dev, ic->start + ic->initial_sectors + ic->metadata_run, ti->len, data);
3026 return fn(ti, ic->dev, 0, ti->len, data);
3029 static void dm_integrity_io_hints(struct dm_target *ti, struct queue_limits *limits)
3031 struct dm_integrity_c *ic = ti->private;
3033 if (ic->sectors_per_block > 1) {
3034 limits->logical_block_size = ic->sectors_per_block << SECTOR_SHIFT;
3035 limits->physical_block_size = ic->sectors_per_block << SECTOR_SHIFT;
3036 blk_limits_io_min(limits, ic->sectors_per_block << SECTOR_SHIFT);
3040 static void calculate_journal_section_size(struct dm_integrity_c *ic)
3042 unsigned sector_space = JOURNAL_SECTOR_DATA;
3044 ic->journal_sections = le32_to_cpu(ic->sb->journal_sections);
3045 ic->journal_entry_size = roundup(offsetof(struct journal_entry, last_bytes[ic->sectors_per_block]) + ic->tag_size,
3046 JOURNAL_ENTRY_ROUNDUP);
3048 if (ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC))
3049 sector_space -= JOURNAL_MAC_PER_SECTOR;
3050 ic->journal_entries_per_sector = sector_space / ic->journal_entry_size;
3051 ic->journal_section_entries = ic->journal_entries_per_sector * JOURNAL_BLOCK_SECTORS;
3052 ic->journal_section_sectors = (ic->journal_section_entries << ic->sb->log2_sectors_per_block) + JOURNAL_BLOCK_SECTORS;
3053 ic->journal_entries = ic->journal_section_entries * ic->journal_sections;
3056 static int calculate_device_limits(struct dm_integrity_c *ic)
3058 __u64 initial_sectors;
3060 calculate_journal_section_size(ic);
3061 initial_sectors = SB_SECTORS + (__u64)ic->journal_section_sectors * ic->journal_sections;
3062 if (initial_sectors + METADATA_PADDING_SECTORS >= ic->meta_device_sectors || initial_sectors > UINT_MAX)
3064 ic->initial_sectors = initial_sectors;
3066 if (!ic->meta_dev) {
3067 sector_t last_sector, last_area, last_offset;
3069 /* we have to maintain excessive padding for compatibility with existing volumes */
3070 __u64 metadata_run_padding =
3071 ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_PADDING) ?
3072 (__u64)(METADATA_PADDING_SECTORS << SECTOR_SHIFT) :
3073 (__u64)(1 << SECTOR_SHIFT << METADATA_PADDING_SECTORS);
3075 ic->metadata_run = round_up((__u64)ic->tag_size << (ic->sb->log2_interleave_sectors - ic->sb->log2_sectors_per_block),
3076 metadata_run_padding) >> SECTOR_SHIFT;
3077 if (!(ic->metadata_run & (ic->metadata_run - 1)))
3078 ic->log2_metadata_run = __ffs(ic->metadata_run);
3080 ic->log2_metadata_run = -1;
3082 get_area_and_offset(ic, ic->provided_data_sectors - 1, &last_area, &last_offset);
3083 last_sector = get_data_sector(ic, last_area, last_offset);
3084 if (last_sector < ic->start || last_sector >= ic->meta_device_sectors)
3087 __u64 meta_size = (ic->provided_data_sectors >> ic->sb->log2_sectors_per_block) * ic->tag_size;
3088 meta_size = (meta_size + ((1U << (ic->log2_buffer_sectors + SECTOR_SHIFT)) - 1))
3089 >> (ic->log2_buffer_sectors + SECTOR_SHIFT);
3090 meta_size <<= ic->log2_buffer_sectors;
3091 if (ic->initial_sectors + meta_size < ic->initial_sectors ||
3092 ic->initial_sectors + meta_size > ic->meta_device_sectors)
3094 ic->metadata_run = 1;
3095 ic->log2_metadata_run = 0;
3101 static void get_provided_data_sectors(struct dm_integrity_c *ic)
3103 if (!ic->meta_dev) {
3105 ic->provided_data_sectors = 0;
3106 for (test_bit = fls64(ic->meta_device_sectors) - 1; test_bit >= 3; test_bit--) {
3107 __u64 prev_data_sectors = ic->provided_data_sectors;
3109 ic->provided_data_sectors |= (sector_t)1 << test_bit;
3110 if (calculate_device_limits(ic))
3111 ic->provided_data_sectors = prev_data_sectors;
3114 ic->provided_data_sectors = ic->data_device_sectors;
3115 ic->provided_data_sectors &= ~(sector_t)(ic->sectors_per_block - 1);
3119 static int initialize_superblock(struct dm_integrity_c *ic, unsigned journal_sectors, unsigned interleave_sectors)
3121 unsigned journal_sections;
3124 memset(ic->sb, 0, SB_SECTORS << SECTOR_SHIFT);
3125 memcpy(ic->sb->magic, SB_MAGIC, 8);
3126 ic->sb->integrity_tag_size = cpu_to_le16(ic->tag_size);
3127 ic->sb->log2_sectors_per_block = __ffs(ic->sectors_per_block);
3128 if (ic->journal_mac_alg.alg_string)
3129 ic->sb->flags |= cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC);
3131 calculate_journal_section_size(ic);
3132 journal_sections = journal_sectors / ic->journal_section_sectors;
3133 if (!journal_sections)
3134 journal_sections = 1;
3136 if (!ic->meta_dev) {
3137 if (ic->fix_padding)
3138 ic->sb->flags |= cpu_to_le32(SB_FLAG_FIXED_PADDING);
3139 ic->sb->journal_sections = cpu_to_le32(journal_sections);
3140 if (!interleave_sectors)
3141 interleave_sectors = DEFAULT_INTERLEAVE_SECTORS;
3142 ic->sb->log2_interleave_sectors = __fls(interleave_sectors);
3143 ic->sb->log2_interleave_sectors = max((__u8)MIN_LOG2_INTERLEAVE_SECTORS, ic->sb->log2_interleave_sectors);
3144 ic->sb->log2_interleave_sectors = min((__u8)MAX_LOG2_INTERLEAVE_SECTORS, ic->sb->log2_interleave_sectors);
3146 get_provided_data_sectors(ic);
3147 if (!ic->provided_data_sectors)
3150 ic->sb->log2_interleave_sectors = 0;
3152 get_provided_data_sectors(ic);
3153 if (!ic->provided_data_sectors)
3157 ic->sb->journal_sections = cpu_to_le32(0);
3158 for (test_bit = fls(journal_sections) - 1; test_bit >= 0; test_bit--) {
3159 __u32 prev_journal_sections = le32_to_cpu(ic->sb->journal_sections);
3160 __u32 test_journal_sections = prev_journal_sections | (1U << test_bit);
3161 if (test_journal_sections > journal_sections)
3163 ic->sb->journal_sections = cpu_to_le32(test_journal_sections);
3164 if (calculate_device_limits(ic))
3165 ic->sb->journal_sections = cpu_to_le32(prev_journal_sections);
3168 if (!le32_to_cpu(ic->sb->journal_sections)) {
3169 if (ic->log2_buffer_sectors > 3) {
3170 ic->log2_buffer_sectors--;
3171 goto try_smaller_buffer;
3177 ic->sb->provided_data_sectors = cpu_to_le64(ic->provided_data_sectors);
3184 static void dm_integrity_set(struct dm_target *ti, struct dm_integrity_c *ic)
3186 struct gendisk *disk = dm_disk(dm_table_get_md(ti->table));
3187 struct blk_integrity bi;
3189 memset(&bi, 0, sizeof(bi));
3190 bi.profile = &dm_integrity_profile;
3191 bi.tuple_size = ic->tag_size;
3192 bi.tag_size = bi.tuple_size;
3193 bi.interval_exp = ic->sb->log2_sectors_per_block + SECTOR_SHIFT;
3195 blk_integrity_register(disk, &bi);
3196 blk_queue_max_integrity_segments(disk->queue, UINT_MAX);
3199 static void dm_integrity_free_page_list(struct page_list *pl)
3205 for (i = 0; pl[i].page; i++)
3206 __free_page(pl[i].page);
3210 static struct page_list *dm_integrity_alloc_page_list(unsigned n_pages)
3212 struct page_list *pl;
3215 pl = kvmalloc_array(n_pages + 1, sizeof(struct page_list), GFP_KERNEL | __GFP_ZERO);
3219 for (i = 0; i < n_pages; i++) {
3220 pl[i].page = alloc_page(GFP_KERNEL);
3222 dm_integrity_free_page_list(pl);
3226 pl[i - 1].next = &pl[i];
3234 static void dm_integrity_free_journal_scatterlist(struct dm_integrity_c *ic, struct scatterlist **sl)
3237 for (i = 0; i < ic->journal_sections; i++)
3242 static struct scatterlist **dm_integrity_alloc_journal_scatterlist(struct dm_integrity_c *ic,
3243 struct page_list *pl)
3245 struct scatterlist **sl;
3248 sl = kvmalloc_array(ic->journal_sections,
3249 sizeof(struct scatterlist *),
3250 GFP_KERNEL | __GFP_ZERO);
3254 for (i = 0; i < ic->journal_sections; i++) {
3255 struct scatterlist *s;
3256 unsigned start_index, start_offset;
3257 unsigned end_index, end_offset;
3261 page_list_location(ic, i, 0, &start_index, &start_offset);
3262 page_list_location(ic, i, ic->journal_section_sectors - 1,
3263 &end_index, &end_offset);
3265 n_pages = (end_index - start_index + 1);
3267 s = kvmalloc_array(n_pages, sizeof(struct scatterlist),
3270 dm_integrity_free_journal_scatterlist(ic, sl);
3274 sg_init_table(s, n_pages);
3275 for (idx = start_index; idx <= end_index; idx++) {
3276 char *va = lowmem_page_address(pl[idx].page);
3277 unsigned start = 0, end = PAGE_SIZE;
3278 if (idx == start_index)
3279 start = start_offset;
3280 if (idx == end_index)
3281 end = end_offset + (1 << SECTOR_SHIFT);
3282 sg_set_buf(&s[idx - start_index], va + start, end - start);
3291 static void free_alg(struct alg_spec *a)
3293 kzfree(a->alg_string);
3295 memset(a, 0, sizeof *a);
3298 static int get_alg_and_key(const char *arg, struct alg_spec *a, char **error, char *error_inval)
3304 a->alg_string = kstrdup(strchr(arg, ':') + 1, GFP_KERNEL);
3308 k = strchr(a->alg_string, ':');
3311 a->key_string = k + 1;
3312 if (strlen(a->key_string) & 1)
3315 a->key_size = strlen(a->key_string) / 2;
3316 a->key = kmalloc(a->key_size, GFP_KERNEL);
3319 if (hex2bin(a->key, a->key_string, a->key_size))
3325 *error = error_inval;
3328 *error = "Out of memory for an argument";
3332 static int get_mac(struct crypto_shash **hash, struct alg_spec *a, char **error,
3333 char *error_alg, char *error_key)
3337 if (a->alg_string) {
3338 *hash = crypto_alloc_shash(a->alg_string, 0, 0);
3339 if (IS_ERR(*hash)) {
3347 r = crypto_shash_setkey(*hash, a->key, a->key_size);
3352 } else if (crypto_shash_get_flags(*hash) & CRYPTO_TFM_NEED_KEY) {
3361 static int create_journal(struct dm_integrity_c *ic, char **error)
3365 __u64 journal_pages, journal_desc_size, journal_tree_size;
3366 unsigned char *crypt_data = NULL, *crypt_iv = NULL;
3367 struct skcipher_request *req = NULL;
3369 ic->commit_ids[0] = cpu_to_le64(0x1111111111111111ULL);
3370 ic->commit_ids[1] = cpu_to_le64(0x2222222222222222ULL);
3371 ic->commit_ids[2] = cpu_to_le64(0x3333333333333333ULL);
3372 ic->commit_ids[3] = cpu_to_le64(0x4444444444444444ULL);
3374 journal_pages = roundup((__u64)ic->journal_sections * ic->journal_section_sectors,
3375 PAGE_SIZE >> SECTOR_SHIFT) >> (PAGE_SHIFT - SECTOR_SHIFT);
3376 journal_desc_size = journal_pages * sizeof(struct page_list);
3377 if (journal_pages >= totalram_pages() - totalhigh_pages() || journal_desc_size > ULONG_MAX) {
3378 *error = "Journal doesn't fit into memory";
3382 ic->journal_pages = journal_pages;
3384 ic->journal = dm_integrity_alloc_page_list(ic->journal_pages);
3386 *error = "Could not allocate memory for journal";
3390 if (ic->journal_crypt_alg.alg_string) {
3391 unsigned ivsize, blocksize;
3392 struct journal_completion comp;
3395 ic->journal_crypt = crypto_alloc_skcipher(ic->journal_crypt_alg.alg_string, 0, 0);
3396 if (IS_ERR(ic->journal_crypt)) {
3397 *error = "Invalid journal cipher";
3398 r = PTR_ERR(ic->journal_crypt);
3399 ic->journal_crypt = NULL;
3402 ivsize = crypto_skcipher_ivsize(ic->journal_crypt);
3403 blocksize = crypto_skcipher_blocksize(ic->journal_crypt);
3405 if (ic->journal_crypt_alg.key) {
3406 r = crypto_skcipher_setkey(ic->journal_crypt, ic->journal_crypt_alg.key,
3407 ic->journal_crypt_alg.key_size);
3409 *error = "Error setting encryption key";
3413 DEBUG_print("cipher %s, block size %u iv size %u\n",
3414 ic->journal_crypt_alg.alg_string, blocksize, ivsize);
3416 ic->journal_io = dm_integrity_alloc_page_list(ic->journal_pages);
3417 if (!ic->journal_io) {
3418 *error = "Could not allocate memory for journal io";
3423 if (blocksize == 1) {
3424 struct scatterlist *sg;
3426 req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
3428 *error = "Could not allocate crypt request";
3433 crypt_iv = kzalloc(ivsize, GFP_KERNEL);
3435 *error = "Could not allocate iv";
3440 ic->journal_xor = dm_integrity_alloc_page_list(ic->journal_pages);
3441 if (!ic->journal_xor) {
3442 *error = "Could not allocate memory for journal xor";
3447 sg = kvmalloc_array(ic->journal_pages + 1,
3448 sizeof(struct scatterlist),
3451 *error = "Unable to allocate sg list";
3455 sg_init_table(sg, ic->journal_pages + 1);
3456 for (i = 0; i < ic->journal_pages; i++) {
3457 char *va = lowmem_page_address(ic->journal_xor[i].page);
3459 sg_set_buf(&sg[i], va, PAGE_SIZE);
3461 sg_set_buf(&sg[i], &ic->commit_ids, sizeof ic->commit_ids);
3463 skcipher_request_set_crypt(req, sg, sg,
3464 PAGE_SIZE * ic->journal_pages + sizeof ic->commit_ids, crypt_iv);
3465 init_completion(&comp.comp);
3466 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
3467 if (do_crypt(true, req, &comp))
3468 wait_for_completion(&comp.comp);
3470 r = dm_integrity_failed(ic);
3472 *error = "Unable to encrypt journal";
3475 DEBUG_bytes(lowmem_page_address(ic->journal_xor[0].page), 64, "xor data");
3477 crypto_free_skcipher(ic->journal_crypt);
3478 ic->journal_crypt = NULL;
3480 unsigned crypt_len = roundup(ivsize, blocksize);
3482 req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
3484 *error = "Could not allocate crypt request";
3489 crypt_iv = kmalloc(ivsize, GFP_KERNEL);
3491 *error = "Could not allocate iv";
3496 crypt_data = kmalloc(crypt_len, GFP_KERNEL);
3498 *error = "Unable to allocate crypt data";
3503 ic->journal_scatterlist = dm_integrity_alloc_journal_scatterlist(ic, ic->journal);
3504 if (!ic->journal_scatterlist) {
3505 *error = "Unable to allocate sg list";
3509 ic->journal_io_scatterlist = dm_integrity_alloc_journal_scatterlist(ic, ic->journal_io);
3510 if (!ic->journal_io_scatterlist) {
3511 *error = "Unable to allocate sg list";
3515 ic->sk_requests = kvmalloc_array(ic->journal_sections,
3516 sizeof(struct skcipher_request *),
3517 GFP_KERNEL | __GFP_ZERO);
3518 if (!ic->sk_requests) {
3519 *error = "Unable to allocate sk requests";
3523 for (i = 0; i < ic->journal_sections; i++) {
3524 struct scatterlist sg;
3525 struct skcipher_request *section_req;
3526 __u32 section_le = cpu_to_le32(i);
3528 memset(crypt_iv, 0x00, ivsize);
3529 memset(crypt_data, 0x00, crypt_len);
3530 memcpy(crypt_data, §ion_le, min((size_t)crypt_len, sizeof(section_le)));
3532 sg_init_one(&sg, crypt_data, crypt_len);
3533 skcipher_request_set_crypt(req, &sg, &sg, crypt_len, crypt_iv);
3534 init_completion(&comp.comp);
3535 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
3536 if (do_crypt(true, req, &comp))
3537 wait_for_completion(&comp.comp);
3539 r = dm_integrity_failed(ic);
3541 *error = "Unable to generate iv";
3545 section_req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
3547 *error = "Unable to allocate crypt request";
3551 section_req->iv = kmalloc_array(ivsize, 2,
3553 if (!section_req->iv) {
3554 skcipher_request_free(section_req);
3555 *error = "Unable to allocate iv";
3559 memcpy(section_req->iv + ivsize, crypt_data, ivsize);
3560 section_req->cryptlen = (size_t)ic->journal_section_sectors << SECTOR_SHIFT;
3561 ic->sk_requests[i] = section_req;
3562 DEBUG_bytes(crypt_data, ivsize, "iv(%u)", i);
3567 for (i = 0; i < N_COMMIT_IDS; i++) {
3570 for (j = 0; j < i; j++) {
3571 if (ic->commit_ids[j] == ic->commit_ids[i]) {
3572 ic->commit_ids[i] = cpu_to_le64(le64_to_cpu(ic->commit_ids[i]) + 1);
3573 goto retest_commit_id;
3576 DEBUG_print("commit id %u: %016llx\n", i, ic->commit_ids[i]);
3579 journal_tree_size = (__u64)ic->journal_entries * sizeof(struct journal_node);
3580 if (journal_tree_size > ULONG_MAX) {
3581 *error = "Journal doesn't fit into memory";
3585 ic->journal_tree = kvmalloc(journal_tree_size, GFP_KERNEL);
3586 if (!ic->journal_tree) {
3587 *error = "Could not allocate memory for journal tree";
3593 skcipher_request_free(req);
3599 * Construct a integrity mapping
3603 * offset from the start of the device
3605 * D - direct writes, J - journal writes, B - bitmap mode, R - recovery mode
3606 * number of optional arguments
3607 * optional arguments:
3609 * interleave_sectors
3616 * bitmap_flush_interval
3622 static int dm_integrity_ctr(struct dm_target *ti, unsigned argc, char **argv)
3624 struct dm_integrity_c *ic;
3627 unsigned extra_args;
3628 struct dm_arg_set as;
3629 static const struct dm_arg _args[] = {
3630 {0, 9, "Invalid number of feature args"},
3632 unsigned journal_sectors, interleave_sectors, buffer_sectors, journal_watermark, sync_msec;
3633 bool should_write_sb;
3635 unsigned long long start;
3636 __s8 log2_sectors_per_bitmap_bit = -1;
3637 __s8 log2_blocks_per_bitmap_bit;
3638 __u64 bits_in_journal;
3639 __u64 n_bitmap_bits;
3641 #define DIRECT_ARGUMENTS 4
3643 if (argc <= DIRECT_ARGUMENTS) {
3644 ti->error = "Invalid argument count";
3648 ic = kzalloc(sizeof(struct dm_integrity_c), GFP_KERNEL);
3650 ti->error = "Cannot allocate integrity context";
3654 ti->per_io_data_size = sizeof(struct dm_integrity_io);
3657 ic->in_progress = RB_ROOT;
3658 INIT_LIST_HEAD(&ic->wait_list);
3659 init_waitqueue_head(&ic->endio_wait);
3660 bio_list_init(&ic->flush_bio_list);
3661 init_waitqueue_head(&ic->copy_to_journal_wait);
3662 init_completion(&ic->crypto_backoff);
3663 atomic64_set(&ic->number_of_mismatches, 0);
3664 ic->bitmap_flush_interval = BITMAP_FLUSH_INTERVAL;
3666 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &ic->dev);
3668 ti->error = "Device lookup failed";
3672 if (sscanf(argv[1], "%llu%c", &start, &dummy) != 1 || start != (sector_t)start) {
3673 ti->error = "Invalid starting offset";
3679 if (strcmp(argv[2], "-")) {
3680 if (sscanf(argv[2], "%u%c", &ic->tag_size, &dummy) != 1 || !ic->tag_size) {
3681 ti->error = "Invalid tag size";
3687 if (!strcmp(argv[3], "J") || !strcmp(argv[3], "B") ||
3688 !strcmp(argv[3], "D") || !strcmp(argv[3], "R")) {
3689 ic->mode = argv[3][0];
3691 ti->error = "Invalid mode (expecting J, B, D, R)";
3696 journal_sectors = 0;
3697 interleave_sectors = DEFAULT_INTERLEAVE_SECTORS;
3698 buffer_sectors = DEFAULT_BUFFER_SECTORS;
3699 journal_watermark = DEFAULT_JOURNAL_WATERMARK;
3700 sync_msec = DEFAULT_SYNC_MSEC;
3701 ic->sectors_per_block = 1;
3703 as.argc = argc - DIRECT_ARGUMENTS;
3704 as.argv = argv + DIRECT_ARGUMENTS;
3705 r = dm_read_arg_group(_args, &as, &extra_args, &ti->error);
3709 while (extra_args--) {
3710 const char *opt_string;
3712 unsigned long long llval;
3713 opt_string = dm_shift_arg(&as);
3716 ti->error = "Not enough feature arguments";
3719 if (sscanf(opt_string, "journal_sectors:%u%c", &val, &dummy) == 1)
3720 journal_sectors = val ? val : 1;
3721 else if (sscanf(opt_string, "interleave_sectors:%u%c", &val, &dummy) == 1)
3722 interleave_sectors = val;
3723 else if (sscanf(opt_string, "buffer_sectors:%u%c", &val, &dummy) == 1)
3724 buffer_sectors = val;
3725 else if (sscanf(opt_string, "journal_watermark:%u%c", &val, &dummy) == 1 && val <= 100)
3726 journal_watermark = val;
3727 else if (sscanf(opt_string, "commit_time:%u%c", &val, &dummy) == 1)
3729 else if (!strncmp(opt_string, "meta_device:", strlen("meta_device:"))) {
3731 dm_put_device(ti, ic->meta_dev);
3732 ic->meta_dev = NULL;
3734 r = dm_get_device(ti, strchr(opt_string, ':') + 1,
3735 dm_table_get_mode(ti->table), &ic->meta_dev);
3737 ti->error = "Device lookup failed";
3740 } else if (sscanf(opt_string, "block_size:%u%c", &val, &dummy) == 1) {
3741 if (val < 1 << SECTOR_SHIFT ||
3742 val > MAX_SECTORS_PER_BLOCK << SECTOR_SHIFT ||
3745 ti->error = "Invalid block_size argument";
3748 ic->sectors_per_block = val >> SECTOR_SHIFT;
3749 } else if (sscanf(opt_string, "sectors_per_bit:%llu%c", &llval, &dummy) == 1) {
3750 log2_sectors_per_bitmap_bit = !llval ? 0 : __ilog2_u64(llval);
3751 } else if (sscanf(opt_string, "bitmap_flush_interval:%u%c", &val, &dummy) == 1) {
3752 if (val >= (uint64_t)UINT_MAX * 1000 / HZ) {
3754 ti->error = "Invalid bitmap_flush_interval argument";
3756 ic->bitmap_flush_interval = msecs_to_jiffies(val);
3757 } else if (!strncmp(opt_string, "internal_hash:", strlen("internal_hash:"))) {
3758 r = get_alg_and_key(opt_string, &ic->internal_hash_alg, &ti->error,
3759 "Invalid internal_hash argument");
3762 } else if (!strncmp(opt_string, "journal_crypt:", strlen("journal_crypt:"))) {
3763 r = get_alg_and_key(opt_string, &ic->journal_crypt_alg, &ti->error,
3764 "Invalid journal_crypt argument");
3767 } else if (!strncmp(opt_string, "journal_mac:", strlen("journal_mac:"))) {
3768 r = get_alg_and_key(opt_string, &ic->journal_mac_alg, &ti->error,
3769 "Invalid journal_mac argument");
3772 } else if (!strcmp(opt_string, "recalculate")) {
3773 ic->recalculate_flag = true;
3774 } else if (!strcmp(opt_string, "fix_padding")) {
3775 ic->fix_padding = true;
3778 ti->error = "Invalid argument";
3783 ic->data_device_sectors = i_size_read(ic->dev->bdev->bd_inode) >> SECTOR_SHIFT;
3785 ic->meta_device_sectors = ic->data_device_sectors;
3787 ic->meta_device_sectors = i_size_read(ic->meta_dev->bdev->bd_inode) >> SECTOR_SHIFT;
3789 if (!journal_sectors) {
3790 journal_sectors = min((sector_t)DEFAULT_MAX_JOURNAL_SECTORS,
3791 ic->data_device_sectors >> DEFAULT_JOURNAL_SIZE_FACTOR);
3794 if (!buffer_sectors)
3796 ic->log2_buffer_sectors = min((int)__fls(buffer_sectors), 31 - SECTOR_SHIFT);
3798 r = get_mac(&ic->internal_hash, &ic->internal_hash_alg, &ti->error,
3799 "Invalid internal hash", "Error setting internal hash key");
3803 r = get_mac(&ic->journal_mac, &ic->journal_mac_alg, &ti->error,
3804 "Invalid journal mac", "Error setting journal mac key");
3808 if (!ic->tag_size) {
3809 if (!ic->internal_hash) {
3810 ti->error = "Unknown tag size";
3814 ic->tag_size = crypto_shash_digestsize(ic->internal_hash);
3816 if (ic->tag_size > MAX_TAG_SIZE) {
3817 ti->error = "Too big tag size";
3821 if (!(ic->tag_size & (ic->tag_size - 1)))
3822 ic->log2_tag_size = __ffs(ic->tag_size);
3824 ic->log2_tag_size = -1;
3826 if (ic->mode == 'B' && !ic->internal_hash) {
3828 ti->error = "Bitmap mode can be only used with internal hash";
3832 ic->autocommit_jiffies = msecs_to_jiffies(sync_msec);
3833 ic->autocommit_msec = sync_msec;
3834 timer_setup(&ic->autocommit_timer, autocommit_fn, 0);
3836 ic->io = dm_io_client_create();
3837 if (IS_ERR(ic->io)) {
3838 r = PTR_ERR(ic->io);
3840 ti->error = "Cannot allocate dm io";
3844 r = mempool_init_slab_pool(&ic->journal_io_mempool, JOURNAL_IO_MEMPOOL, journal_io_cache);
3846 ti->error = "Cannot allocate mempool";
3850 ic->metadata_wq = alloc_workqueue("dm-integrity-metadata",
3851 WQ_MEM_RECLAIM, METADATA_WORKQUEUE_MAX_ACTIVE);
3852 if (!ic->metadata_wq) {
3853 ti->error = "Cannot allocate workqueue";
3859 * If this workqueue were percpu, it would cause bio reordering
3860 * and reduced performance.
3862 ic->wait_wq = alloc_workqueue("dm-integrity-wait", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
3864 ti->error = "Cannot allocate workqueue";
3869 ic->offload_wq = alloc_workqueue("dm-integrity-offload", WQ_MEM_RECLAIM,
3870 METADATA_WORKQUEUE_MAX_ACTIVE);
3871 if (!ic->offload_wq) {
3872 ti->error = "Cannot allocate workqueue";
3877 ic->commit_wq = alloc_workqueue("dm-integrity-commit", WQ_MEM_RECLAIM, 1);
3878 if (!ic->commit_wq) {
3879 ti->error = "Cannot allocate workqueue";
3883 INIT_WORK(&ic->commit_work, integrity_commit);
3885 if (ic->mode == 'J' || ic->mode == 'B') {
3886 ic->writer_wq = alloc_workqueue("dm-integrity-writer", WQ_MEM_RECLAIM, 1);
3887 if (!ic->writer_wq) {
3888 ti->error = "Cannot allocate workqueue";
3892 INIT_WORK(&ic->writer_work, integrity_writer);
3895 ic->sb = alloc_pages_exact(SB_SECTORS << SECTOR_SHIFT, GFP_KERNEL);
3898 ti->error = "Cannot allocate superblock area";
3902 r = sync_rw_sb(ic, REQ_OP_READ, 0);
3904 ti->error = "Error reading superblock";
3907 should_write_sb = false;
3908 if (memcmp(ic->sb->magic, SB_MAGIC, 8)) {
3909 if (ic->mode != 'R') {
3910 if (memchr_inv(ic->sb, 0, SB_SECTORS << SECTOR_SHIFT)) {
3912 ti->error = "The device is not initialized";
3917 r = initialize_superblock(ic, journal_sectors, interleave_sectors);
3919 ti->error = "Could not initialize superblock";
3922 if (ic->mode != 'R')
3923 should_write_sb = true;
3926 if (!ic->sb->version || ic->sb->version > SB_VERSION_4) {
3928 ti->error = "Unknown version";
3931 if (le16_to_cpu(ic->sb->integrity_tag_size) != ic->tag_size) {
3933 ti->error = "Tag size doesn't match the information in superblock";
3936 if (ic->sb->log2_sectors_per_block != __ffs(ic->sectors_per_block)) {
3938 ti->error = "Block size doesn't match the information in superblock";
3941 if (!le32_to_cpu(ic->sb->journal_sections)) {
3943 ti->error = "Corrupted superblock, journal_sections is 0";
3946 /* make sure that ti->max_io_len doesn't overflow */
3947 if (!ic->meta_dev) {
3948 if (ic->sb->log2_interleave_sectors < MIN_LOG2_INTERLEAVE_SECTORS ||
3949 ic->sb->log2_interleave_sectors > MAX_LOG2_INTERLEAVE_SECTORS) {
3951 ti->error = "Invalid interleave_sectors in the superblock";
3955 if (ic->sb->log2_interleave_sectors) {
3957 ti->error = "Invalid interleave_sectors in the superblock";
3961 if (!!(ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC)) != !!ic->journal_mac_alg.alg_string) {
3963 ti->error = "Journal mac mismatch";
3967 get_provided_data_sectors(ic);
3968 if (!ic->provided_data_sectors) {
3970 ti->error = "The device is too small";
3975 r = calculate_device_limits(ic);
3978 if (ic->log2_buffer_sectors > 3) {
3979 ic->log2_buffer_sectors--;
3980 goto try_smaller_buffer;
3983 ti->error = "The device is too small";
3987 if (log2_sectors_per_bitmap_bit < 0)
3988 log2_sectors_per_bitmap_bit = __fls(DEFAULT_SECTORS_PER_BITMAP_BIT);
3989 if (log2_sectors_per_bitmap_bit < ic->sb->log2_sectors_per_block)
3990 log2_sectors_per_bitmap_bit = ic->sb->log2_sectors_per_block;
3992 bits_in_journal = ((__u64)ic->journal_section_sectors * ic->journal_sections) << (SECTOR_SHIFT + 3);
3993 if (bits_in_journal > UINT_MAX)
3994 bits_in_journal = UINT_MAX;
3995 while (bits_in_journal < (ic->provided_data_sectors + ((sector_t)1 << log2_sectors_per_bitmap_bit) - 1) >> log2_sectors_per_bitmap_bit)
3996 log2_sectors_per_bitmap_bit++;
3998 log2_blocks_per_bitmap_bit = log2_sectors_per_bitmap_bit - ic->sb->log2_sectors_per_block;
3999 ic->log2_blocks_per_bitmap_bit = log2_blocks_per_bitmap_bit;
4000 if (should_write_sb) {
4001 ic->sb->log2_blocks_per_bitmap_bit = log2_blocks_per_bitmap_bit;
4003 n_bitmap_bits = ((ic->provided_data_sectors >> ic->sb->log2_sectors_per_block)
4004 + (((sector_t)1 << log2_blocks_per_bitmap_bit) - 1)) >> log2_blocks_per_bitmap_bit;
4005 ic->n_bitmap_blocks = DIV_ROUND_UP(n_bitmap_bits, BITMAP_BLOCK_SIZE * 8);
4008 ic->log2_buffer_sectors = min(ic->log2_buffer_sectors, (__u8)__ffs(ic->metadata_run));
4010 if (ti->len > ic->provided_data_sectors) {
4012 ti->error = "Not enough provided sectors for requested mapping size";
4017 threshold = (__u64)ic->journal_entries * (100 - journal_watermark);
4019 do_div(threshold, 100);
4020 ic->free_sectors_threshold = threshold;
4022 DEBUG_print("initialized:\n");
4023 DEBUG_print(" integrity_tag_size %u\n", le16_to_cpu(ic->sb->integrity_tag_size));
4024 DEBUG_print(" journal_entry_size %u\n", ic->journal_entry_size);
4025 DEBUG_print(" journal_entries_per_sector %u\n", ic->journal_entries_per_sector);
4026 DEBUG_print(" journal_section_entries %u\n", ic->journal_section_entries);
4027 DEBUG_print(" journal_section_sectors %u\n", ic->journal_section_sectors);
4028 DEBUG_print(" journal_sections %u\n", (unsigned)le32_to_cpu(ic->sb->journal_sections));
4029 DEBUG_print(" journal_entries %u\n", ic->journal_entries);
4030 DEBUG_print(" log2_interleave_sectors %d\n", ic->sb->log2_interleave_sectors);
4031 DEBUG_print(" data_device_sectors 0x%llx\n", i_size_read(ic->dev->bdev->bd_inode) >> SECTOR_SHIFT);
4032 DEBUG_print(" initial_sectors 0x%x\n", ic->initial_sectors);
4033 DEBUG_print(" metadata_run 0x%x\n", ic->metadata_run);
4034 DEBUG_print(" log2_metadata_run %d\n", ic->log2_metadata_run);
4035 DEBUG_print(" provided_data_sectors 0x%llx (%llu)\n", ic->provided_data_sectors, ic->provided_data_sectors);
4036 DEBUG_print(" log2_buffer_sectors %u\n", ic->log2_buffer_sectors);
4037 DEBUG_print(" bits_in_journal %llu\n", bits_in_journal);
4039 if (ic->recalculate_flag && !(ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))) {
4040 ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
4041 ic->sb->recalc_sector = cpu_to_le64(0);
4044 if (ic->internal_hash) {
4045 ic->recalc_wq = alloc_workqueue("dm-integrity-recalc", WQ_MEM_RECLAIM, 1);
4046 if (!ic->recalc_wq ) {
4047 ti->error = "Cannot allocate workqueue";
4051 INIT_WORK(&ic->recalc_work, integrity_recalc);
4052 ic->recalc_buffer = vmalloc(RECALC_SECTORS << SECTOR_SHIFT);
4053 if (!ic->recalc_buffer) {
4054 ti->error = "Cannot allocate buffer for recalculating";
4058 ic->recalc_tags = kvmalloc_array(RECALC_SECTORS >> ic->sb->log2_sectors_per_block,
4059 ic->tag_size, GFP_KERNEL);
4060 if (!ic->recalc_tags) {
4061 ti->error = "Cannot allocate tags for recalculating";
4067 ic->bufio = dm_bufio_client_create(ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev,
4068 1U << (SECTOR_SHIFT + ic->log2_buffer_sectors), 1, 0, NULL, NULL);
4069 if (IS_ERR(ic->bufio)) {
4070 r = PTR_ERR(ic->bufio);
4071 ti->error = "Cannot initialize dm-bufio";
4075 dm_bufio_set_sector_offset(ic->bufio, ic->start + ic->initial_sectors);
4077 if (ic->mode != 'R') {
4078 r = create_journal(ic, &ti->error);
4084 if (ic->mode == 'B') {
4086 unsigned n_bitmap_pages = DIV_ROUND_UP(ic->n_bitmap_blocks, PAGE_SIZE / BITMAP_BLOCK_SIZE);
4088 ic->recalc_bitmap = dm_integrity_alloc_page_list(n_bitmap_pages);
4089 if (!ic->recalc_bitmap) {
4093 ic->may_write_bitmap = dm_integrity_alloc_page_list(n_bitmap_pages);
4094 if (!ic->may_write_bitmap) {
4098 ic->bbs = kvmalloc_array(ic->n_bitmap_blocks, sizeof(struct bitmap_block_status), GFP_KERNEL);
4103 INIT_DELAYED_WORK(&ic->bitmap_flush_work, bitmap_flush_work);
4104 for (i = 0; i < ic->n_bitmap_blocks; i++) {
4105 struct bitmap_block_status *bbs = &ic->bbs[i];
4106 unsigned sector, pl_index, pl_offset;
4108 INIT_WORK(&bbs->work, bitmap_block_work);
4111 bio_list_init(&bbs->bio_queue);
4112 spin_lock_init(&bbs->bio_queue_lock);
4114 sector = i * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT);
4115 pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
4116 pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
4118 bbs->bitmap = lowmem_page_address(ic->journal[pl_index].page) + pl_offset;
4122 if (should_write_sb) {
4125 init_journal(ic, 0, ic->journal_sections, 0);
4126 r = dm_integrity_failed(ic);
4128 ti->error = "Error initializing journal";
4131 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
4133 ti->error = "Error initializing superblock";
4136 ic->just_formatted = true;
4139 if (!ic->meta_dev) {
4140 r = dm_set_target_max_io_len(ti, 1U << ic->sb->log2_interleave_sectors);
4144 if (ic->mode == 'B') {
4145 unsigned max_io_len = ((sector_t)ic->sectors_per_block << ic->log2_blocks_per_bitmap_bit) * (BITMAP_BLOCK_SIZE * 8);
4147 max_io_len = 1U << 31;
4148 DEBUG_print("max_io_len: old %u, new %u\n", ti->max_io_len, max_io_len);
4149 if (!ti->max_io_len || ti->max_io_len > max_io_len) {
4150 r = dm_set_target_max_io_len(ti, max_io_len);
4156 if (!ic->internal_hash)
4157 dm_integrity_set(ti, ic);
4159 ti->num_flush_bios = 1;
4160 ti->flush_supported = true;
4165 dm_integrity_dtr(ti);
4169 static void dm_integrity_dtr(struct dm_target *ti)
4171 struct dm_integrity_c *ic = ti->private;
4173 BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress));
4174 BUG_ON(!list_empty(&ic->wait_list));
4176 if (ic->metadata_wq)
4177 destroy_workqueue(ic->metadata_wq);
4179 destroy_workqueue(ic->wait_wq);
4181 destroy_workqueue(ic->offload_wq);
4183 destroy_workqueue(ic->commit_wq);
4185 destroy_workqueue(ic->writer_wq);
4187 destroy_workqueue(ic->recalc_wq);
4188 vfree(ic->recalc_buffer);
4189 kvfree(ic->recalc_tags);
4192 dm_bufio_client_destroy(ic->bufio);
4193 mempool_exit(&ic->journal_io_mempool);
4195 dm_io_client_destroy(ic->io);
4197 dm_put_device(ti, ic->dev);
4199 dm_put_device(ti, ic->meta_dev);
4200 dm_integrity_free_page_list(ic->journal);
4201 dm_integrity_free_page_list(ic->journal_io);
4202 dm_integrity_free_page_list(ic->journal_xor);
4203 dm_integrity_free_page_list(ic->recalc_bitmap);
4204 dm_integrity_free_page_list(ic->may_write_bitmap);
4205 if (ic->journal_scatterlist)
4206 dm_integrity_free_journal_scatterlist(ic, ic->journal_scatterlist);
4207 if (ic->journal_io_scatterlist)
4208 dm_integrity_free_journal_scatterlist(ic, ic->journal_io_scatterlist);
4209 if (ic->sk_requests) {
4212 for (i = 0; i < ic->journal_sections; i++) {
4213 struct skcipher_request *req = ic->sk_requests[i];
4216 skcipher_request_free(req);
4219 kvfree(ic->sk_requests);
4221 kvfree(ic->journal_tree);
4223 free_pages_exact(ic->sb, SB_SECTORS << SECTOR_SHIFT);
4225 if (ic->internal_hash)
4226 crypto_free_shash(ic->internal_hash);
4227 free_alg(&ic->internal_hash_alg);
4229 if (ic->journal_crypt)
4230 crypto_free_skcipher(ic->journal_crypt);
4231 free_alg(&ic->journal_crypt_alg);
4233 if (ic->journal_mac)
4234 crypto_free_shash(ic->journal_mac);
4235 free_alg(&ic->journal_mac_alg);
4240 static struct target_type integrity_target = {
4241 .name = "integrity",
4242 .version = {1, 6, 0},
4243 .module = THIS_MODULE,
4244 .features = DM_TARGET_SINGLETON | DM_TARGET_INTEGRITY,
4245 .ctr = dm_integrity_ctr,
4246 .dtr = dm_integrity_dtr,
4247 .map = dm_integrity_map,
4248 .postsuspend = dm_integrity_postsuspend,
4249 .resume = dm_integrity_resume,
4250 .status = dm_integrity_status,
4251 .iterate_devices = dm_integrity_iterate_devices,
4252 .io_hints = dm_integrity_io_hints,
4255 static int __init dm_integrity_init(void)
4259 journal_io_cache = kmem_cache_create("integrity_journal_io",
4260 sizeof(struct journal_io), 0, 0, NULL);
4261 if (!journal_io_cache) {
4262 DMERR("can't allocate journal io cache");
4266 r = dm_register_target(&integrity_target);
4269 DMERR("register failed %d", r);
4274 static void __exit dm_integrity_exit(void)
4276 dm_unregister_target(&integrity_target);
4277 kmem_cache_destroy(journal_io_cache);
4280 module_init(dm_integrity_init);
4281 module_exit(dm_integrity_exit);
4283 MODULE_AUTHOR("Milan Broz");
4284 MODULE_AUTHOR("Mikulas Patocka");
4285 MODULE_DESCRIPTION(DM_NAME " target for integrity tags extension");
4286 MODULE_LICENSE("GPL");