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
59 #define MAX_SECTORS_PER_BLOCK 8
64 __u8 log2_interleave_sectors;
65 __u16 integrity_tag_size;
66 __u32 journal_sections;
67 __u64 provided_data_sectors; /* userspace uses this value */
69 __u8 log2_sectors_per_block;
70 __u8 log2_blocks_per_bitmap_bit;
75 #define SB_FLAG_HAVE_JOURNAL_MAC 0x1
76 #define SB_FLAG_RECALCULATING 0x2
77 #define SB_FLAG_DIRTY_BITMAP 0x4
79 #define JOURNAL_ENTRY_ROUNDUP 8
81 typedef __u64 commit_id_t;
82 #define JOURNAL_MAC_PER_SECTOR 8
84 struct journal_entry {
92 commit_id_t last_bytes[0];
96 #define journal_entry_tag(ic, je) ((__u8 *)&(je)->last_bytes[(ic)->sectors_per_block])
98 #if BITS_PER_LONG == 64
99 #define journal_entry_set_sector(je, x) do { smp_wmb(); WRITE_ONCE((je)->u.sector, cpu_to_le64(x)); } while (0)
101 #define journal_entry_set_sector(je, x) do { (je)->u.s.sector_lo = cpu_to_le32(x); smp_wmb(); WRITE_ONCE((je)->u.s.sector_hi, cpu_to_le32((x) >> 32)); } while (0)
103 #define journal_entry_get_sector(je) le64_to_cpu((je)->u.sector)
104 #define journal_entry_is_unused(je) ((je)->u.s.sector_hi == cpu_to_le32(-1))
105 #define journal_entry_set_unused(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-1)); } while (0)
106 #define journal_entry_is_inprogress(je) ((je)->u.s.sector_hi == cpu_to_le32(-2))
107 #define journal_entry_set_inprogress(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-2)); } while (0)
109 #define JOURNAL_BLOCK_SECTORS 8
110 #define JOURNAL_SECTOR_DATA ((1 << SECTOR_SHIFT) - sizeof(commit_id_t))
111 #define JOURNAL_MAC_SIZE (JOURNAL_MAC_PER_SECTOR * JOURNAL_BLOCK_SECTORS)
113 struct journal_sector {
114 __u8 entries[JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR];
115 __u8 mac[JOURNAL_MAC_PER_SECTOR];
116 commit_id_t commit_id;
119 #define MAX_TAG_SIZE (JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR - offsetof(struct journal_entry, last_bytes[MAX_SECTORS_PER_BLOCK]))
121 #define METADATA_PADDING_SECTORS 8
123 #define N_COMMIT_IDS 4
125 static unsigned char prev_commit_seq(unsigned char seq)
127 return (seq + N_COMMIT_IDS - 1) % N_COMMIT_IDS;
130 static unsigned char next_commit_seq(unsigned char seq)
132 return (seq + 1) % N_COMMIT_IDS;
136 * In-memory structures
139 struct journal_node {
151 struct dm_integrity_c {
153 struct dm_dev *meta_dev;
157 mempool_t journal_io_mempool;
158 struct dm_io_client *io;
159 struct dm_bufio_client *bufio;
160 struct workqueue_struct *metadata_wq;
161 struct superblock *sb;
162 unsigned journal_pages;
163 unsigned n_bitmap_blocks;
165 struct page_list *journal;
166 struct page_list *journal_io;
167 struct page_list *journal_xor;
168 struct page_list *recalc_bitmap;
169 struct page_list *may_write_bitmap;
170 struct bitmap_block_status *bbs;
171 unsigned bitmap_flush_interval;
172 int synchronous_mode;
173 struct bio_list synchronous_bios;
174 struct delayed_work bitmap_flush_work;
176 struct crypto_skcipher *journal_crypt;
177 struct scatterlist **journal_scatterlist;
178 struct scatterlist **journal_io_scatterlist;
179 struct skcipher_request **sk_requests;
181 struct crypto_shash *journal_mac;
183 struct journal_node *journal_tree;
184 struct rb_root journal_tree_root;
186 sector_t provided_data_sectors;
188 unsigned short journal_entry_size;
189 unsigned char journal_entries_per_sector;
190 unsigned char journal_section_entries;
191 unsigned short journal_section_sectors;
192 unsigned journal_sections;
193 unsigned journal_entries;
194 sector_t data_device_sectors;
195 sector_t meta_device_sectors;
196 unsigned initial_sectors;
197 unsigned metadata_run;
198 __s8 log2_metadata_run;
199 __u8 log2_buffer_sectors;
200 __u8 sectors_per_block;
201 __u8 log2_blocks_per_bitmap_bit;
207 struct crypto_shash *internal_hash;
209 struct dm_target *ti;
211 /* these variables are locked with endio_wait.lock */
212 struct rb_root in_progress;
213 struct list_head wait_list;
214 wait_queue_head_t endio_wait;
215 struct workqueue_struct *wait_wq;
216 struct workqueue_struct *offload_wq;
218 unsigned char commit_seq;
219 commit_id_t commit_ids[N_COMMIT_IDS];
221 unsigned committed_section;
222 unsigned n_committed_sections;
224 unsigned uncommitted_section;
225 unsigned n_uncommitted_sections;
227 unsigned free_section;
228 unsigned char free_section_entry;
229 unsigned free_sectors;
231 unsigned free_sectors_threshold;
233 struct workqueue_struct *commit_wq;
234 struct work_struct commit_work;
236 struct workqueue_struct *writer_wq;
237 struct work_struct writer_work;
239 struct workqueue_struct *recalc_wq;
240 struct work_struct recalc_work;
244 struct bio_list flush_bio_list;
246 unsigned long autocommit_jiffies;
247 struct timer_list autocommit_timer;
248 unsigned autocommit_msec;
250 wait_queue_head_t copy_to_journal_wait;
252 struct completion crypto_backoff;
254 bool journal_uptodate;
256 bool recalculate_flag;
257 bool legacy_recalculate;
259 struct alg_spec internal_hash_alg;
260 struct alg_spec journal_crypt_alg;
261 struct alg_spec journal_mac_alg;
263 atomic64_t number_of_mismatches;
265 struct notifier_block reboot_notifier;
268 struct dm_integrity_range {
269 sector_t logical_sector;
275 struct task_struct *task;
276 struct list_head wait_entry;
281 struct dm_integrity_io {
282 struct work_struct work;
284 struct dm_integrity_c *ic;
288 struct dm_integrity_range range;
290 sector_t metadata_block;
291 unsigned metadata_offset;
294 blk_status_t bi_status;
296 struct completion *completion;
298 struct dm_bio_details bio_details;
301 struct journal_completion {
302 struct dm_integrity_c *ic;
304 struct completion comp;
308 struct dm_integrity_range range;
309 struct journal_completion *comp;
312 struct bitmap_block_status {
313 struct work_struct work;
314 struct dm_integrity_c *ic;
316 unsigned long *bitmap;
317 struct bio_list bio_queue;
318 spinlock_t bio_queue_lock;
322 static struct kmem_cache *journal_io_cache;
324 #define JOURNAL_IO_MEMPOOL 32
327 #define DEBUG_print(x, ...) printk(KERN_DEBUG x, ##__VA_ARGS__)
328 static void __DEBUG_bytes(__u8 *bytes, size_t len, const char *msg, ...)
337 pr_cont(" %02x", *bytes);
343 #define DEBUG_bytes(bytes, len, msg, ...) __DEBUG_bytes(bytes, len, KERN_DEBUG msg, ##__VA_ARGS__)
345 #define DEBUG_print(x, ...) do { } while (0)
346 #define DEBUG_bytes(bytes, len, msg, ...) do { } while (0)
349 static void dm_integrity_prepare(struct request *rq)
353 static void dm_integrity_complete(struct request *rq, unsigned int nr_bytes)
358 * DM Integrity profile, protection is performed layer above (dm-crypt)
360 static const struct blk_integrity_profile dm_integrity_profile = {
361 .name = "DM-DIF-EXT-TAG",
364 .prepare_fn = dm_integrity_prepare,
365 .complete_fn = dm_integrity_complete,
368 static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map);
369 static void integrity_bio_wait(struct work_struct *w);
370 static void dm_integrity_dtr(struct dm_target *ti);
372 static void dm_integrity_io_error(struct dm_integrity_c *ic, const char *msg, int err)
375 atomic64_inc(&ic->number_of_mismatches);
376 if (!cmpxchg(&ic->failed, 0, err))
377 DMERR("Error on %s: %d", msg, err);
380 static int dm_integrity_failed(struct dm_integrity_c *ic)
382 return READ_ONCE(ic->failed);
385 static bool dm_integrity_disable_recalculate(struct dm_integrity_c *ic)
387 if ((ic->internal_hash_alg.key || ic->journal_mac_alg.key) &&
388 !ic->legacy_recalculate)
393 static commit_id_t dm_integrity_commit_id(struct dm_integrity_c *ic, unsigned i,
394 unsigned j, unsigned char seq)
397 * Xor the number with section and sector, so that if a piece of
398 * journal is written at wrong place, it is detected.
400 return ic->commit_ids[seq] ^ cpu_to_le64(((__u64)i << 32) ^ j);
403 static void get_area_and_offset(struct dm_integrity_c *ic, sector_t data_sector,
404 sector_t *area, sector_t *offset)
407 __u8 log2_interleave_sectors = ic->sb->log2_interleave_sectors;
408 *area = data_sector >> log2_interleave_sectors;
409 *offset = (unsigned)data_sector & ((1U << log2_interleave_sectors) - 1);
412 *offset = data_sector;
416 #define sector_to_block(ic, n) \
418 BUG_ON((n) & (unsigned)((ic)->sectors_per_block - 1)); \
419 (n) >>= (ic)->sb->log2_sectors_per_block; \
422 static __u64 get_metadata_sector_and_offset(struct dm_integrity_c *ic, sector_t area,
423 sector_t offset, unsigned *metadata_offset)
428 ms = area << ic->sb->log2_interleave_sectors;
429 if (likely(ic->log2_metadata_run >= 0))
430 ms += area << ic->log2_metadata_run;
432 ms += area * ic->metadata_run;
433 ms >>= ic->log2_buffer_sectors;
435 sector_to_block(ic, offset);
437 if (likely(ic->log2_tag_size >= 0)) {
438 ms += offset >> (SECTOR_SHIFT + ic->log2_buffer_sectors - ic->log2_tag_size);
439 mo = (offset << ic->log2_tag_size) & ((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - 1);
441 ms += (__u64)offset * ic->tag_size >> (SECTOR_SHIFT + ic->log2_buffer_sectors);
442 mo = (offset * ic->tag_size) & ((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - 1);
444 *metadata_offset = mo;
448 static sector_t get_data_sector(struct dm_integrity_c *ic, sector_t area, sector_t offset)
455 result = area << ic->sb->log2_interleave_sectors;
456 if (likely(ic->log2_metadata_run >= 0))
457 result += (area + 1) << ic->log2_metadata_run;
459 result += (area + 1) * ic->metadata_run;
461 result += (sector_t)ic->initial_sectors + offset;
467 static void wraparound_section(struct dm_integrity_c *ic, unsigned *sec_ptr)
469 if (unlikely(*sec_ptr >= ic->journal_sections))
470 *sec_ptr -= ic->journal_sections;
473 static void sb_set_version(struct dm_integrity_c *ic)
475 if (ic->mode == 'B' || ic->sb->flags & cpu_to_le32(SB_FLAG_DIRTY_BITMAP))
476 ic->sb->version = SB_VERSION_3;
477 else if (ic->meta_dev || ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))
478 ic->sb->version = SB_VERSION_2;
480 ic->sb->version = SB_VERSION_1;
483 static int sync_rw_sb(struct dm_integrity_c *ic, int op, int op_flags)
485 struct dm_io_request io_req;
486 struct dm_io_region io_loc;
489 io_req.bi_op_flags = op_flags;
490 io_req.mem.type = DM_IO_KMEM;
491 io_req.mem.ptr.addr = ic->sb;
492 io_req.notify.fn = NULL;
493 io_req.client = ic->io;
494 io_loc.bdev = ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev;
495 io_loc.sector = ic->start;
496 io_loc.count = SB_SECTORS;
498 if (op == REQ_OP_WRITE)
501 return dm_io(&io_req, 1, &io_loc, NULL);
504 #define BITMAP_OP_TEST_ALL_SET 0
505 #define BITMAP_OP_TEST_ALL_CLEAR 1
506 #define BITMAP_OP_SET 2
507 #define BITMAP_OP_CLEAR 3
509 static bool block_bitmap_op(struct dm_integrity_c *ic, struct page_list *bitmap,
510 sector_t sector, sector_t n_sectors, int mode)
512 unsigned long bit, end_bit, this_end_bit, page, end_page;
515 if (unlikely(((sector | n_sectors) & ((1 << ic->sb->log2_sectors_per_block) - 1)) != 0)) {
516 DMCRIT("invalid bitmap access (%llx,%llx,%d,%d,%d)",
517 (unsigned long long)sector,
518 (unsigned long long)n_sectors,
519 ic->sb->log2_sectors_per_block,
520 ic->log2_blocks_per_bitmap_bit,
525 if (unlikely(!n_sectors))
528 bit = sector >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
529 end_bit = (sector + n_sectors - 1) >>
530 (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
532 page = bit / (PAGE_SIZE * 8);
533 bit %= PAGE_SIZE * 8;
535 end_page = end_bit / (PAGE_SIZE * 8);
536 end_bit %= PAGE_SIZE * 8;
539 if (page < end_page) {
540 this_end_bit = PAGE_SIZE * 8 - 1;
542 this_end_bit = end_bit;
545 data = lowmem_page_address(bitmap[page].page);
547 if (mode == BITMAP_OP_TEST_ALL_SET) {
548 while (bit <= this_end_bit) {
549 if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
551 if (data[bit / BITS_PER_LONG] != -1)
553 bit += BITS_PER_LONG;
554 } while (this_end_bit >= bit + BITS_PER_LONG - 1);
557 if (!test_bit(bit, data))
561 } else if (mode == BITMAP_OP_TEST_ALL_CLEAR) {
562 while (bit <= this_end_bit) {
563 if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
565 if (data[bit / BITS_PER_LONG] != 0)
567 bit += BITS_PER_LONG;
568 } while (this_end_bit >= bit + BITS_PER_LONG - 1);
571 if (test_bit(bit, data))
575 } else if (mode == BITMAP_OP_SET) {
576 while (bit <= this_end_bit) {
577 if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
579 data[bit / BITS_PER_LONG] = -1;
580 bit += BITS_PER_LONG;
581 } while (this_end_bit >= bit + BITS_PER_LONG - 1);
584 __set_bit(bit, data);
587 } else if (mode == BITMAP_OP_CLEAR) {
588 if (!bit && this_end_bit == PAGE_SIZE * 8 - 1)
590 else while (bit <= this_end_bit) {
591 if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
593 data[bit / BITS_PER_LONG] = 0;
594 bit += BITS_PER_LONG;
595 } while (this_end_bit >= bit + BITS_PER_LONG - 1);
598 __clear_bit(bit, data);
605 if (unlikely(page < end_page)) {
614 static void block_bitmap_copy(struct dm_integrity_c *ic, struct page_list *dst, struct page_list *src)
616 unsigned n_bitmap_pages = DIV_ROUND_UP(ic->n_bitmap_blocks, PAGE_SIZE / BITMAP_BLOCK_SIZE);
619 for (i = 0; i < n_bitmap_pages; i++) {
620 unsigned long *dst_data = lowmem_page_address(dst[i].page);
621 unsigned long *src_data = lowmem_page_address(src[i].page);
622 copy_page(dst_data, src_data);
626 static struct bitmap_block_status *sector_to_bitmap_block(struct dm_integrity_c *ic, sector_t sector)
628 unsigned bit = sector >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
629 unsigned bitmap_block = bit / (BITMAP_BLOCK_SIZE * 8);
631 BUG_ON(bitmap_block >= ic->n_bitmap_blocks);
632 return &ic->bbs[bitmap_block];
635 static void access_journal_check(struct dm_integrity_c *ic, unsigned section, unsigned offset,
636 bool e, const char *function)
638 #if defined(CONFIG_DM_DEBUG) || defined(INTERNAL_VERIFY)
639 unsigned limit = e ? ic->journal_section_entries : ic->journal_section_sectors;
641 if (unlikely(section >= ic->journal_sections) ||
642 unlikely(offset >= limit)) {
643 DMCRIT("%s: invalid access at (%u,%u), limit (%u,%u)",
644 function, section, offset, ic->journal_sections, limit);
650 static void page_list_location(struct dm_integrity_c *ic, unsigned section, unsigned offset,
651 unsigned *pl_index, unsigned *pl_offset)
655 access_journal_check(ic, section, offset, false, "page_list_location");
657 sector = section * ic->journal_section_sectors + offset;
659 *pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
660 *pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
663 static struct journal_sector *access_page_list(struct dm_integrity_c *ic, struct page_list *pl,
664 unsigned section, unsigned offset, unsigned *n_sectors)
666 unsigned pl_index, pl_offset;
669 page_list_location(ic, section, offset, &pl_index, &pl_offset);
672 *n_sectors = (PAGE_SIZE - pl_offset) >> SECTOR_SHIFT;
674 va = lowmem_page_address(pl[pl_index].page);
676 return (struct journal_sector *)(va + pl_offset);
679 static struct journal_sector *access_journal(struct dm_integrity_c *ic, unsigned section, unsigned offset)
681 return access_page_list(ic, ic->journal, section, offset, NULL);
684 static struct journal_entry *access_journal_entry(struct dm_integrity_c *ic, unsigned section, unsigned n)
686 unsigned rel_sector, offset;
687 struct journal_sector *js;
689 access_journal_check(ic, section, n, true, "access_journal_entry");
691 rel_sector = n % JOURNAL_BLOCK_SECTORS;
692 offset = n / JOURNAL_BLOCK_SECTORS;
694 js = access_journal(ic, section, rel_sector);
695 return (struct journal_entry *)((char *)js + offset * ic->journal_entry_size);
698 static struct journal_sector *access_journal_data(struct dm_integrity_c *ic, unsigned section, unsigned n)
700 n <<= ic->sb->log2_sectors_per_block;
702 n += JOURNAL_BLOCK_SECTORS;
704 access_journal_check(ic, section, n, false, "access_journal_data");
706 return access_journal(ic, section, n);
709 static void section_mac(struct dm_integrity_c *ic, unsigned section, __u8 result[JOURNAL_MAC_SIZE])
711 SHASH_DESC_ON_STACK(desc, ic->journal_mac);
715 desc->tfm = ic->journal_mac;
717 r = crypto_shash_init(desc);
719 dm_integrity_io_error(ic, "crypto_shash_init", r);
723 for (j = 0; j < ic->journal_section_entries; j++) {
724 struct journal_entry *je = access_journal_entry(ic, section, j);
725 r = crypto_shash_update(desc, (__u8 *)&je->u.sector, sizeof je->u.sector);
727 dm_integrity_io_error(ic, "crypto_shash_update", r);
732 size = crypto_shash_digestsize(ic->journal_mac);
734 if (likely(size <= JOURNAL_MAC_SIZE)) {
735 r = crypto_shash_final(desc, result);
737 dm_integrity_io_error(ic, "crypto_shash_final", r);
740 memset(result + size, 0, JOURNAL_MAC_SIZE - size);
742 __u8 digest[HASH_MAX_DIGESTSIZE];
744 if (WARN_ON(size > sizeof(digest))) {
745 dm_integrity_io_error(ic, "digest_size", -EINVAL);
748 r = crypto_shash_final(desc, digest);
750 dm_integrity_io_error(ic, "crypto_shash_final", r);
753 memcpy(result, digest, JOURNAL_MAC_SIZE);
758 memset(result, 0, JOURNAL_MAC_SIZE);
761 static void rw_section_mac(struct dm_integrity_c *ic, unsigned section, bool wr)
763 __u8 result[JOURNAL_MAC_SIZE];
766 if (!ic->journal_mac)
769 section_mac(ic, section, result);
771 for (j = 0; j < JOURNAL_BLOCK_SECTORS; j++) {
772 struct journal_sector *js = access_journal(ic, section, j);
775 memcpy(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR);
777 if (memcmp(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR))
778 dm_integrity_io_error(ic, "journal mac", -EILSEQ);
783 static void complete_journal_op(void *context)
785 struct journal_completion *comp = context;
786 BUG_ON(!atomic_read(&comp->in_flight));
787 if (likely(atomic_dec_and_test(&comp->in_flight)))
788 complete(&comp->comp);
791 static void xor_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
792 unsigned n_sections, struct journal_completion *comp)
794 struct async_submit_ctl submit;
795 size_t n_bytes = (size_t)(n_sections * ic->journal_section_sectors) << SECTOR_SHIFT;
796 unsigned pl_index, pl_offset, section_index;
797 struct page_list *source_pl, *target_pl;
799 if (likely(encrypt)) {
800 source_pl = ic->journal;
801 target_pl = ic->journal_io;
803 source_pl = ic->journal_io;
804 target_pl = ic->journal;
807 page_list_location(ic, section, 0, &pl_index, &pl_offset);
809 atomic_add(roundup(pl_offset + n_bytes, PAGE_SIZE) >> PAGE_SHIFT, &comp->in_flight);
811 init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL, complete_journal_op, comp, NULL);
813 section_index = pl_index;
817 struct page *src_pages[2];
818 struct page *dst_page;
820 while (unlikely(pl_index == section_index)) {
823 rw_section_mac(ic, section, true);
828 page_list_location(ic, section, 0, §ion_index, &dummy);
831 this_step = min(n_bytes, (size_t)PAGE_SIZE - pl_offset);
832 dst_page = target_pl[pl_index].page;
833 src_pages[0] = source_pl[pl_index].page;
834 src_pages[1] = ic->journal_xor[pl_index].page;
836 async_xor(dst_page, src_pages, pl_offset, 2, this_step, &submit);
840 n_bytes -= this_step;
845 async_tx_issue_pending_all();
848 static void complete_journal_encrypt(struct crypto_async_request *req, int err)
850 struct journal_completion *comp = req->data;
852 if (likely(err == -EINPROGRESS)) {
853 complete(&comp->ic->crypto_backoff);
856 dm_integrity_io_error(comp->ic, "asynchronous encrypt", err);
858 complete_journal_op(comp);
861 static bool do_crypt(bool encrypt, struct skcipher_request *req, struct journal_completion *comp)
864 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
865 complete_journal_encrypt, comp);
867 r = crypto_skcipher_encrypt(req);
869 r = crypto_skcipher_decrypt(req);
872 if (likely(r == -EINPROGRESS))
874 if (likely(r == -EBUSY)) {
875 wait_for_completion(&comp->ic->crypto_backoff);
876 reinit_completion(&comp->ic->crypto_backoff);
879 dm_integrity_io_error(comp->ic, "encrypt", r);
883 static void crypt_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
884 unsigned n_sections, struct journal_completion *comp)
886 struct scatterlist **source_sg;
887 struct scatterlist **target_sg;
889 atomic_add(2, &comp->in_flight);
891 if (likely(encrypt)) {
892 source_sg = ic->journal_scatterlist;
893 target_sg = ic->journal_io_scatterlist;
895 source_sg = ic->journal_io_scatterlist;
896 target_sg = ic->journal_scatterlist;
900 struct skcipher_request *req;
905 rw_section_mac(ic, section, true);
907 req = ic->sk_requests[section];
908 ivsize = crypto_skcipher_ivsize(ic->journal_crypt);
911 memcpy(iv, iv + ivsize, ivsize);
913 req->src = source_sg[section];
914 req->dst = target_sg[section];
916 if (unlikely(do_crypt(encrypt, req, comp)))
917 atomic_inc(&comp->in_flight);
921 } while (n_sections);
923 atomic_dec(&comp->in_flight);
924 complete_journal_op(comp);
927 static void encrypt_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
928 unsigned n_sections, struct journal_completion *comp)
931 return xor_journal(ic, encrypt, section, n_sections, comp);
933 return crypt_journal(ic, encrypt, section, n_sections, comp);
936 static void complete_journal_io(unsigned long error, void *context)
938 struct journal_completion *comp = context;
939 if (unlikely(error != 0))
940 dm_integrity_io_error(comp->ic, "writing journal", -EIO);
941 complete_journal_op(comp);
944 static void rw_journal_sectors(struct dm_integrity_c *ic, int op, int op_flags,
945 unsigned sector, unsigned n_sectors, struct journal_completion *comp)
947 struct dm_io_request io_req;
948 struct dm_io_region io_loc;
949 unsigned pl_index, pl_offset;
952 if (unlikely(dm_integrity_failed(ic))) {
954 complete_journal_io(-1UL, comp);
958 pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
959 pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
962 io_req.bi_op_flags = op_flags;
963 io_req.mem.type = DM_IO_PAGE_LIST;
965 io_req.mem.ptr.pl = &ic->journal_io[pl_index];
967 io_req.mem.ptr.pl = &ic->journal[pl_index];
968 io_req.mem.offset = pl_offset;
969 if (likely(comp != NULL)) {
970 io_req.notify.fn = complete_journal_io;
971 io_req.notify.context = comp;
973 io_req.notify.fn = NULL;
975 io_req.client = ic->io;
976 io_loc.bdev = ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev;
977 io_loc.sector = ic->start + SB_SECTORS + sector;
978 io_loc.count = n_sectors;
980 r = dm_io(&io_req, 1, &io_loc, NULL);
982 dm_integrity_io_error(ic, op == REQ_OP_READ ? "reading journal" : "writing journal", r);
984 WARN_ONCE(1, "asynchronous dm_io failed: %d", r);
985 complete_journal_io(-1UL, comp);
990 static void rw_journal(struct dm_integrity_c *ic, int op, int op_flags, unsigned section,
991 unsigned n_sections, struct journal_completion *comp)
993 unsigned sector, n_sectors;
995 sector = section * ic->journal_section_sectors;
996 n_sectors = n_sections * ic->journal_section_sectors;
998 rw_journal_sectors(ic, op, op_flags, sector, n_sectors, comp);
1001 static void write_journal(struct dm_integrity_c *ic, unsigned commit_start, unsigned commit_sections)
1003 struct journal_completion io_comp;
1004 struct journal_completion crypt_comp_1;
1005 struct journal_completion crypt_comp_2;
1009 init_completion(&io_comp.comp);
1011 if (commit_start + commit_sections <= ic->journal_sections) {
1012 io_comp.in_flight = (atomic_t)ATOMIC_INIT(1);
1013 if (ic->journal_io) {
1014 crypt_comp_1.ic = ic;
1015 init_completion(&crypt_comp_1.comp);
1016 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
1017 encrypt_journal(ic, true, commit_start, commit_sections, &crypt_comp_1);
1018 wait_for_completion_io(&crypt_comp_1.comp);
1020 for (i = 0; i < commit_sections; i++)
1021 rw_section_mac(ic, commit_start + i, true);
1023 rw_journal(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, commit_start,
1024 commit_sections, &io_comp);
1027 io_comp.in_flight = (atomic_t)ATOMIC_INIT(2);
1028 to_end = ic->journal_sections - commit_start;
1029 if (ic->journal_io) {
1030 crypt_comp_1.ic = ic;
1031 init_completion(&crypt_comp_1.comp);
1032 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
1033 encrypt_journal(ic, true, commit_start, to_end, &crypt_comp_1);
1034 if (try_wait_for_completion(&crypt_comp_1.comp)) {
1035 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
1036 reinit_completion(&crypt_comp_1.comp);
1037 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
1038 encrypt_journal(ic, true, 0, commit_sections - to_end, &crypt_comp_1);
1039 wait_for_completion_io(&crypt_comp_1.comp);
1041 crypt_comp_2.ic = ic;
1042 init_completion(&crypt_comp_2.comp);
1043 crypt_comp_2.in_flight = (atomic_t)ATOMIC_INIT(0);
1044 encrypt_journal(ic, true, 0, commit_sections - to_end, &crypt_comp_2);
1045 wait_for_completion_io(&crypt_comp_1.comp);
1046 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
1047 wait_for_completion_io(&crypt_comp_2.comp);
1050 for (i = 0; i < to_end; i++)
1051 rw_section_mac(ic, commit_start + i, true);
1052 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
1053 for (i = 0; i < commit_sections - to_end; i++)
1054 rw_section_mac(ic, i, true);
1056 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, 0, commit_sections - to_end, &io_comp);
1059 wait_for_completion_io(&io_comp.comp);
1062 static void copy_from_journal(struct dm_integrity_c *ic, unsigned section, unsigned offset,
1063 unsigned n_sectors, sector_t target, io_notify_fn fn, void *data)
1065 struct dm_io_request io_req;
1066 struct dm_io_region io_loc;
1068 unsigned sector, pl_index, pl_offset;
1070 BUG_ON((target | n_sectors | offset) & (unsigned)(ic->sectors_per_block - 1));
1072 if (unlikely(dm_integrity_failed(ic))) {
1077 sector = section * ic->journal_section_sectors + JOURNAL_BLOCK_SECTORS + offset;
1079 pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
1080 pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
1082 io_req.bi_op = REQ_OP_WRITE;
1083 io_req.bi_op_flags = 0;
1084 io_req.mem.type = DM_IO_PAGE_LIST;
1085 io_req.mem.ptr.pl = &ic->journal[pl_index];
1086 io_req.mem.offset = pl_offset;
1087 io_req.notify.fn = fn;
1088 io_req.notify.context = data;
1089 io_req.client = ic->io;
1090 io_loc.bdev = ic->dev->bdev;
1091 io_loc.sector = target;
1092 io_loc.count = n_sectors;
1094 r = dm_io(&io_req, 1, &io_loc, NULL);
1096 WARN_ONCE(1, "asynchronous dm_io failed: %d", r);
1101 static bool ranges_overlap(struct dm_integrity_range *range1, struct dm_integrity_range *range2)
1103 return range1->logical_sector < range2->logical_sector + range2->n_sectors &&
1104 range1->logical_sector + range1->n_sectors > range2->logical_sector;
1107 static bool add_new_range(struct dm_integrity_c *ic, struct dm_integrity_range *new_range, bool check_waiting)
1109 struct rb_node **n = &ic->in_progress.rb_node;
1110 struct rb_node *parent;
1112 BUG_ON((new_range->logical_sector | new_range->n_sectors) & (unsigned)(ic->sectors_per_block - 1));
1114 if (likely(check_waiting)) {
1115 struct dm_integrity_range *range;
1116 list_for_each_entry(range, &ic->wait_list, wait_entry) {
1117 if (unlikely(ranges_overlap(range, new_range)))
1125 struct dm_integrity_range *range = container_of(*n, struct dm_integrity_range, node);
1128 if (new_range->logical_sector + new_range->n_sectors <= range->logical_sector) {
1129 n = &range->node.rb_left;
1130 } else if (new_range->logical_sector >= range->logical_sector + range->n_sectors) {
1131 n = &range->node.rb_right;
1137 rb_link_node(&new_range->node, parent, n);
1138 rb_insert_color(&new_range->node, &ic->in_progress);
1143 static void remove_range_unlocked(struct dm_integrity_c *ic, struct dm_integrity_range *range)
1145 rb_erase(&range->node, &ic->in_progress);
1146 while (unlikely(!list_empty(&ic->wait_list))) {
1147 struct dm_integrity_range *last_range =
1148 list_first_entry(&ic->wait_list, struct dm_integrity_range, wait_entry);
1149 struct task_struct *last_range_task;
1150 last_range_task = last_range->task;
1151 list_del(&last_range->wait_entry);
1152 if (!add_new_range(ic, last_range, false)) {
1153 last_range->task = last_range_task;
1154 list_add(&last_range->wait_entry, &ic->wait_list);
1157 last_range->waiting = false;
1158 wake_up_process(last_range_task);
1162 static void remove_range(struct dm_integrity_c *ic, struct dm_integrity_range *range)
1164 unsigned long flags;
1166 spin_lock_irqsave(&ic->endio_wait.lock, flags);
1167 remove_range_unlocked(ic, range);
1168 spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
1171 static void wait_and_add_new_range(struct dm_integrity_c *ic, struct dm_integrity_range *new_range)
1173 new_range->waiting = true;
1174 list_add_tail(&new_range->wait_entry, &ic->wait_list);
1175 new_range->task = current;
1177 __set_current_state(TASK_UNINTERRUPTIBLE);
1178 spin_unlock_irq(&ic->endio_wait.lock);
1180 spin_lock_irq(&ic->endio_wait.lock);
1181 } while (unlikely(new_range->waiting));
1184 static void add_new_range_and_wait(struct dm_integrity_c *ic, struct dm_integrity_range *new_range)
1186 if (unlikely(!add_new_range(ic, new_range, true)))
1187 wait_and_add_new_range(ic, new_range);
1190 static void init_journal_node(struct journal_node *node)
1192 RB_CLEAR_NODE(&node->node);
1193 node->sector = (sector_t)-1;
1196 static void add_journal_node(struct dm_integrity_c *ic, struct journal_node *node, sector_t sector)
1198 struct rb_node **link;
1199 struct rb_node *parent;
1201 node->sector = sector;
1202 BUG_ON(!RB_EMPTY_NODE(&node->node));
1204 link = &ic->journal_tree_root.rb_node;
1208 struct journal_node *j;
1210 j = container_of(parent, struct journal_node, node);
1211 if (sector < j->sector)
1212 link = &j->node.rb_left;
1214 link = &j->node.rb_right;
1217 rb_link_node(&node->node, parent, link);
1218 rb_insert_color(&node->node, &ic->journal_tree_root);
1221 static void remove_journal_node(struct dm_integrity_c *ic, struct journal_node *node)
1223 BUG_ON(RB_EMPTY_NODE(&node->node));
1224 rb_erase(&node->node, &ic->journal_tree_root);
1225 init_journal_node(node);
1228 #define NOT_FOUND (-1U)
1230 static unsigned find_journal_node(struct dm_integrity_c *ic, sector_t sector, sector_t *next_sector)
1232 struct rb_node *n = ic->journal_tree_root.rb_node;
1233 unsigned found = NOT_FOUND;
1234 *next_sector = (sector_t)-1;
1236 struct journal_node *j = container_of(n, struct journal_node, node);
1237 if (sector == j->sector) {
1238 found = j - ic->journal_tree;
1240 if (sector < j->sector) {
1241 *next_sector = j->sector;
1242 n = j->node.rb_left;
1244 n = j->node.rb_right;
1251 static bool test_journal_node(struct dm_integrity_c *ic, unsigned pos, sector_t sector)
1253 struct journal_node *node, *next_node;
1254 struct rb_node *next;
1256 if (unlikely(pos >= ic->journal_entries))
1258 node = &ic->journal_tree[pos];
1259 if (unlikely(RB_EMPTY_NODE(&node->node)))
1261 if (unlikely(node->sector != sector))
1264 next = rb_next(&node->node);
1265 if (unlikely(!next))
1268 next_node = container_of(next, struct journal_node, node);
1269 return next_node->sector != sector;
1272 static bool find_newer_committed_node(struct dm_integrity_c *ic, struct journal_node *node)
1274 struct rb_node *next;
1275 struct journal_node *next_node;
1276 unsigned next_section;
1278 BUG_ON(RB_EMPTY_NODE(&node->node));
1280 next = rb_next(&node->node);
1281 if (unlikely(!next))
1284 next_node = container_of(next, struct journal_node, node);
1286 if (next_node->sector != node->sector)
1289 next_section = (unsigned)(next_node - ic->journal_tree) / ic->journal_section_entries;
1290 if (next_section >= ic->committed_section &&
1291 next_section < ic->committed_section + ic->n_committed_sections)
1293 if (next_section + ic->journal_sections < ic->committed_section + ic->n_committed_sections)
1303 static int dm_integrity_rw_tag(struct dm_integrity_c *ic, unsigned char *tag, sector_t *metadata_block,
1304 unsigned *metadata_offset, unsigned total_size, int op)
1307 unsigned char *data, *dp;
1308 struct dm_buffer *b;
1312 r = dm_integrity_failed(ic);
1316 data = dm_bufio_read(ic->bufio, *metadata_block, &b);
1318 return PTR_ERR(data);
1320 to_copy = min((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - *metadata_offset, total_size);
1321 dp = data + *metadata_offset;
1322 if (op == TAG_READ) {
1323 memcpy(tag, dp, to_copy);
1324 } else if (op == TAG_WRITE) {
1325 memcpy(dp, tag, to_copy);
1326 dm_bufio_mark_partial_buffer_dirty(b, *metadata_offset, *metadata_offset + to_copy);
1328 /* e.g.: op == TAG_CMP */
1329 if (unlikely(memcmp(dp, tag, to_copy))) {
1332 for (i = 0; i < to_copy; i++) {
1333 if (dp[i] != tag[i])
1337 dm_bufio_release(b);
1341 dm_bufio_release(b);
1344 *metadata_offset += to_copy;
1345 if (unlikely(*metadata_offset == 1U << SECTOR_SHIFT << ic->log2_buffer_sectors)) {
1346 (*metadata_block)++;
1347 *metadata_offset = 0;
1349 total_size -= to_copy;
1350 } while (unlikely(total_size));
1355 struct flush_request {
1356 struct dm_io_request io_req;
1357 struct dm_io_region io_reg;
1358 struct dm_integrity_c *ic;
1359 struct completion comp;
1362 static void flush_notify(unsigned long error, void *fr_)
1364 struct flush_request *fr = fr_;
1365 if (unlikely(error != 0))
1366 dm_integrity_io_error(fr->ic, "flusing disk cache", -EIO);
1367 complete(&fr->comp);
1370 static void dm_integrity_flush_buffers(struct dm_integrity_c *ic, bool flush_data)
1374 struct flush_request fr;
1379 fr.io_req.bi_op = REQ_OP_WRITE,
1380 fr.io_req.bi_op_flags = REQ_PREFLUSH | REQ_SYNC,
1381 fr.io_req.mem.type = DM_IO_KMEM,
1382 fr.io_req.mem.ptr.addr = NULL,
1383 fr.io_req.notify.fn = flush_notify,
1384 fr.io_req.notify.context = &fr;
1385 fr.io_req.client = dm_bufio_get_dm_io_client(ic->bufio),
1386 fr.io_reg.bdev = ic->dev->bdev,
1387 fr.io_reg.sector = 0,
1388 fr.io_reg.count = 0,
1390 init_completion(&fr.comp);
1391 r = dm_io(&fr.io_req, 1, &fr.io_reg, NULL);
1395 r = dm_bufio_write_dirty_buffers(ic->bufio);
1397 dm_integrity_io_error(ic, "writing tags", r);
1400 wait_for_completion(&fr.comp);
1403 static void sleep_on_endio_wait(struct dm_integrity_c *ic)
1405 DECLARE_WAITQUEUE(wait, current);
1406 __add_wait_queue(&ic->endio_wait, &wait);
1407 __set_current_state(TASK_UNINTERRUPTIBLE);
1408 spin_unlock_irq(&ic->endio_wait.lock);
1410 spin_lock_irq(&ic->endio_wait.lock);
1411 __remove_wait_queue(&ic->endio_wait, &wait);
1414 static void autocommit_fn(struct timer_list *t)
1416 struct dm_integrity_c *ic = from_timer(ic, t, autocommit_timer);
1418 if (likely(!dm_integrity_failed(ic)))
1419 queue_work(ic->commit_wq, &ic->commit_work);
1422 static void schedule_autocommit(struct dm_integrity_c *ic)
1424 if (!timer_pending(&ic->autocommit_timer))
1425 mod_timer(&ic->autocommit_timer, jiffies + ic->autocommit_jiffies);
1428 static void submit_flush_bio(struct dm_integrity_c *ic, struct dm_integrity_io *dio)
1431 unsigned long flags;
1433 spin_lock_irqsave(&ic->endio_wait.lock, flags);
1434 bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1435 bio_list_add(&ic->flush_bio_list, bio);
1436 spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
1438 queue_work(ic->commit_wq, &ic->commit_work);
1441 static void do_endio(struct dm_integrity_c *ic, struct bio *bio)
1443 int r = dm_integrity_failed(ic);
1444 if (unlikely(r) && !bio->bi_status)
1445 bio->bi_status = errno_to_blk_status(r);
1446 if (unlikely(ic->synchronous_mode) && bio_op(bio) == REQ_OP_WRITE) {
1447 unsigned long flags;
1448 spin_lock_irqsave(&ic->endio_wait.lock, flags);
1449 bio_list_add(&ic->synchronous_bios, bio);
1450 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0);
1451 spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
1457 static void do_endio_flush(struct dm_integrity_c *ic, struct dm_integrity_io *dio)
1459 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1461 if (unlikely(dio->fua) && likely(!bio->bi_status) && likely(!dm_integrity_failed(ic)))
1462 submit_flush_bio(ic, dio);
1467 static void dec_in_flight(struct dm_integrity_io *dio)
1469 if (atomic_dec_and_test(&dio->in_flight)) {
1470 struct dm_integrity_c *ic = dio->ic;
1473 remove_range(ic, &dio->range);
1475 if (unlikely(dio->write))
1476 schedule_autocommit(ic);
1478 bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1480 if (unlikely(dio->bi_status) && !bio->bi_status)
1481 bio->bi_status = dio->bi_status;
1482 if (likely(!bio->bi_status) && unlikely(bio_sectors(bio) != dio->range.n_sectors)) {
1483 dio->range.logical_sector += dio->range.n_sectors;
1484 bio_advance(bio, dio->range.n_sectors << SECTOR_SHIFT);
1485 INIT_WORK(&dio->work, integrity_bio_wait);
1486 queue_work(ic->offload_wq, &dio->work);
1489 do_endio_flush(ic, dio);
1493 static void integrity_end_io(struct bio *bio)
1495 struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
1497 dm_bio_restore(&dio->bio_details, bio);
1498 if (bio->bi_integrity)
1499 bio->bi_opf |= REQ_INTEGRITY;
1501 if (dio->completion)
1502 complete(dio->completion);
1507 static void integrity_sector_checksum(struct dm_integrity_c *ic, sector_t sector,
1508 const char *data, char *result)
1510 __u64 sector_le = cpu_to_le64(sector);
1511 SHASH_DESC_ON_STACK(req, ic->internal_hash);
1513 unsigned digest_size;
1515 req->tfm = ic->internal_hash;
1517 r = crypto_shash_init(req);
1518 if (unlikely(r < 0)) {
1519 dm_integrity_io_error(ic, "crypto_shash_init", r);
1523 r = crypto_shash_update(req, (const __u8 *)§or_le, sizeof sector_le);
1524 if (unlikely(r < 0)) {
1525 dm_integrity_io_error(ic, "crypto_shash_update", r);
1529 r = crypto_shash_update(req, data, ic->sectors_per_block << SECTOR_SHIFT);
1530 if (unlikely(r < 0)) {
1531 dm_integrity_io_error(ic, "crypto_shash_update", r);
1535 r = crypto_shash_final(req, result);
1536 if (unlikely(r < 0)) {
1537 dm_integrity_io_error(ic, "crypto_shash_final", r);
1541 digest_size = crypto_shash_digestsize(ic->internal_hash);
1542 if (unlikely(digest_size < ic->tag_size))
1543 memset(result + digest_size, 0, ic->tag_size - digest_size);
1548 /* this shouldn't happen anyway, the hash functions have no reason to fail */
1549 get_random_bytes(result, ic->tag_size);
1552 static void integrity_metadata(struct work_struct *w)
1554 struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work);
1555 struct dm_integrity_c *ic = dio->ic;
1559 if (ic->internal_hash) {
1560 struct bvec_iter iter;
1562 unsigned digest_size = crypto_shash_digestsize(ic->internal_hash);
1563 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1565 unsigned extra_space = unlikely(digest_size > ic->tag_size) ? digest_size - ic->tag_size : 0;
1566 char checksums_onstack[max((size_t)HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)];
1567 unsigned sectors_to_process = dio->range.n_sectors;
1568 sector_t sector = dio->range.logical_sector;
1570 if (unlikely(ic->mode == 'R'))
1573 checksums = kmalloc((PAGE_SIZE >> SECTOR_SHIFT >> ic->sb->log2_sectors_per_block) * ic->tag_size + extra_space,
1574 GFP_NOIO | __GFP_NORETRY | __GFP_NOWARN);
1576 checksums = checksums_onstack;
1577 if (WARN_ON(extra_space &&
1578 digest_size > sizeof(checksums_onstack))) {
1584 __bio_for_each_segment(bv, bio, iter, dio->bio_details.bi_iter) {
1586 char *mem, *checksums_ptr;
1589 mem = (char *)kmap_atomic(bv.bv_page) + bv.bv_offset;
1591 checksums_ptr = checksums;
1593 integrity_sector_checksum(ic, sector, mem + pos, checksums_ptr);
1594 checksums_ptr += ic->tag_size;
1595 sectors_to_process -= ic->sectors_per_block;
1596 pos += ic->sectors_per_block << SECTOR_SHIFT;
1597 sector += ic->sectors_per_block;
1598 } while (pos < bv.bv_len && sectors_to_process && checksums != checksums_onstack);
1601 r = dm_integrity_rw_tag(ic, checksums, &dio->metadata_block, &dio->metadata_offset,
1602 checksums_ptr - checksums, !dio->write ? TAG_CMP : TAG_WRITE);
1605 DMERR_LIMIT("Checksum failed at sector 0x%llx",
1606 (unsigned long long)(sector - ((r + ic->tag_size - 1) / ic->tag_size)));
1608 atomic64_inc(&ic->number_of_mismatches);
1610 if (likely(checksums != checksums_onstack))
1615 if (!sectors_to_process)
1618 if (unlikely(pos < bv.bv_len)) {
1619 bv.bv_offset += pos;
1625 if (likely(checksums != checksums_onstack))
1628 struct bio_integrity_payload *bip = dio->bio_details.bi_integrity;
1632 struct bvec_iter iter;
1633 unsigned data_to_process = dio->range.n_sectors;
1634 sector_to_block(ic, data_to_process);
1635 data_to_process *= ic->tag_size;
1637 bip_for_each_vec(biv, bip, iter) {
1641 BUG_ON(PageHighMem(biv.bv_page));
1642 tag = lowmem_page_address(biv.bv_page) + biv.bv_offset;
1643 this_len = min(biv.bv_len, data_to_process);
1644 r = dm_integrity_rw_tag(ic, tag, &dio->metadata_block, &dio->metadata_offset,
1645 this_len, !dio->write ? TAG_READ : TAG_WRITE);
1648 data_to_process -= this_len;
1649 if (!data_to_process)
1658 dio->bi_status = errno_to_blk_status(r);
1662 static int dm_integrity_map(struct dm_target *ti, struct bio *bio)
1664 struct dm_integrity_c *ic = ti->private;
1665 struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
1666 struct bio_integrity_payload *bip;
1668 sector_t area, offset;
1673 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1674 submit_flush_bio(ic, dio);
1675 return DM_MAPIO_SUBMITTED;
1678 dio->range.logical_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
1679 dio->write = bio_op(bio) == REQ_OP_WRITE;
1680 dio->fua = dio->write && bio->bi_opf & REQ_FUA;
1681 if (unlikely(dio->fua)) {
1683 * Don't pass down the FUA flag because we have to flush
1684 * disk cache anyway.
1686 bio->bi_opf &= ~REQ_FUA;
1688 if (unlikely(dio->range.logical_sector + bio_sectors(bio) > ic->provided_data_sectors)) {
1689 DMERR("Too big sector number: 0x%llx + 0x%x > 0x%llx",
1690 (unsigned long long)dio->range.logical_sector, bio_sectors(bio),
1691 (unsigned long long)ic->provided_data_sectors);
1692 return DM_MAPIO_KILL;
1694 if (unlikely((dio->range.logical_sector | bio_sectors(bio)) & (unsigned)(ic->sectors_per_block - 1))) {
1695 DMERR("Bio not aligned on %u sectors: 0x%llx, 0x%x",
1696 ic->sectors_per_block,
1697 (unsigned long long)dio->range.logical_sector, bio_sectors(bio));
1698 return DM_MAPIO_KILL;
1701 if (ic->sectors_per_block > 1) {
1702 struct bvec_iter iter;
1704 bio_for_each_segment(bv, bio, iter) {
1705 if (unlikely(bv.bv_len & ((ic->sectors_per_block << SECTOR_SHIFT) - 1))) {
1706 DMERR("Bio vector (%u,%u) is not aligned on %u-sector boundary",
1707 bv.bv_offset, bv.bv_len, ic->sectors_per_block);
1708 return DM_MAPIO_KILL;
1713 bip = bio_integrity(bio);
1714 if (!ic->internal_hash) {
1716 unsigned wanted_tag_size = bio_sectors(bio) >> ic->sb->log2_sectors_per_block;
1717 if (ic->log2_tag_size >= 0)
1718 wanted_tag_size <<= ic->log2_tag_size;
1720 wanted_tag_size *= ic->tag_size;
1721 if (unlikely(wanted_tag_size != bip->bip_iter.bi_size)) {
1722 DMERR("Invalid integrity data size %u, expected %u",
1723 bip->bip_iter.bi_size, wanted_tag_size);
1724 return DM_MAPIO_KILL;
1728 if (unlikely(bip != NULL)) {
1729 DMERR("Unexpected integrity data when using internal hash");
1730 return DM_MAPIO_KILL;
1734 if (unlikely(ic->mode == 'R') && unlikely(dio->write))
1735 return DM_MAPIO_KILL;
1737 get_area_and_offset(ic, dio->range.logical_sector, &area, &offset);
1738 dio->metadata_block = get_metadata_sector_and_offset(ic, area, offset, &dio->metadata_offset);
1739 bio->bi_iter.bi_sector = get_data_sector(ic, area, offset);
1741 dm_integrity_map_continue(dio, true);
1742 return DM_MAPIO_SUBMITTED;
1745 static bool __journal_read_write(struct dm_integrity_io *dio, struct bio *bio,
1746 unsigned journal_section, unsigned journal_entry)
1748 struct dm_integrity_c *ic = dio->ic;
1749 sector_t logical_sector;
1752 logical_sector = dio->range.logical_sector;
1753 n_sectors = dio->range.n_sectors;
1755 struct bio_vec bv = bio_iovec(bio);
1758 if (unlikely(bv.bv_len >> SECTOR_SHIFT > n_sectors))
1759 bv.bv_len = n_sectors << SECTOR_SHIFT;
1760 n_sectors -= bv.bv_len >> SECTOR_SHIFT;
1761 bio_advance_iter(bio, &bio->bi_iter, bv.bv_len);
1763 mem = kmap_atomic(bv.bv_page);
1764 if (likely(dio->write))
1765 flush_dcache_page(bv.bv_page);
1768 struct journal_entry *je = access_journal_entry(ic, journal_section, journal_entry);
1770 if (unlikely(!dio->write)) {
1771 struct journal_sector *js;
1775 if (unlikely(journal_entry_is_inprogress(je))) {
1776 flush_dcache_page(bv.bv_page);
1779 __io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je));
1783 BUG_ON(journal_entry_get_sector(je) != logical_sector);
1784 js = access_journal_data(ic, journal_section, journal_entry);
1785 mem_ptr = mem + bv.bv_offset;
1788 memcpy(mem_ptr, js, JOURNAL_SECTOR_DATA);
1789 *(commit_id_t *)(mem_ptr + JOURNAL_SECTOR_DATA) = je->last_bytes[s];
1791 mem_ptr += 1 << SECTOR_SHIFT;
1792 } while (++s < ic->sectors_per_block);
1793 #ifdef INTERNAL_VERIFY
1794 if (ic->internal_hash) {
1795 char checksums_onstack[max((size_t)HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)];
1797 integrity_sector_checksum(ic, logical_sector, mem + bv.bv_offset, checksums_onstack);
1798 if (unlikely(memcmp(checksums_onstack, journal_entry_tag(ic, je), ic->tag_size))) {
1799 DMERR_LIMIT("Checksum failed when reading from journal, at sector 0x%llx",
1800 (unsigned long long)logical_sector);
1806 if (!ic->internal_hash) {
1807 struct bio_integrity_payload *bip = bio_integrity(bio);
1808 unsigned tag_todo = ic->tag_size;
1809 char *tag_ptr = journal_entry_tag(ic, je);
1812 struct bio_vec biv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter);
1813 unsigned tag_now = min(biv.bv_len, tag_todo);
1815 BUG_ON(PageHighMem(biv.bv_page));
1816 tag_addr = lowmem_page_address(biv.bv_page) + biv.bv_offset;
1817 if (likely(dio->write))
1818 memcpy(tag_ptr, tag_addr, tag_now);
1820 memcpy(tag_addr, tag_ptr, tag_now);
1821 bvec_iter_advance(bip->bip_vec, &bip->bip_iter, tag_now);
1823 tag_todo -= tag_now;
1824 } while (unlikely(tag_todo)); else {
1825 if (likely(dio->write))
1826 memset(tag_ptr, 0, tag_todo);
1830 if (likely(dio->write)) {
1831 struct journal_sector *js;
1834 js = access_journal_data(ic, journal_section, journal_entry);
1835 memcpy(js, mem + bv.bv_offset, ic->sectors_per_block << SECTOR_SHIFT);
1839 je->last_bytes[s] = js[s].commit_id;
1840 } while (++s < ic->sectors_per_block);
1842 if (ic->internal_hash) {
1843 unsigned digest_size = crypto_shash_digestsize(ic->internal_hash);
1844 if (unlikely(digest_size > ic->tag_size)) {
1845 char checksums_onstack[HASH_MAX_DIGESTSIZE];
1846 integrity_sector_checksum(ic, logical_sector, (char *)js, checksums_onstack);
1847 memcpy(journal_entry_tag(ic, je), checksums_onstack, ic->tag_size);
1849 integrity_sector_checksum(ic, logical_sector, (char *)js, journal_entry_tag(ic, je));
1852 journal_entry_set_sector(je, logical_sector);
1854 logical_sector += ic->sectors_per_block;
1857 if (unlikely(journal_entry == ic->journal_section_entries)) {
1860 wraparound_section(ic, &journal_section);
1863 bv.bv_offset += ic->sectors_per_block << SECTOR_SHIFT;
1864 } while (bv.bv_len -= ic->sectors_per_block << SECTOR_SHIFT);
1866 if (unlikely(!dio->write))
1867 flush_dcache_page(bv.bv_page);
1869 } while (n_sectors);
1871 if (likely(dio->write)) {
1873 if (unlikely(waitqueue_active(&ic->copy_to_journal_wait)))
1874 wake_up(&ic->copy_to_journal_wait);
1875 if (READ_ONCE(ic->free_sectors) <= ic->free_sectors_threshold) {
1876 queue_work(ic->commit_wq, &ic->commit_work);
1878 schedule_autocommit(ic);
1881 remove_range(ic, &dio->range);
1884 if (unlikely(bio->bi_iter.bi_size)) {
1885 sector_t area, offset;
1887 dio->range.logical_sector = logical_sector;
1888 get_area_and_offset(ic, dio->range.logical_sector, &area, &offset);
1889 dio->metadata_block = get_metadata_sector_and_offset(ic, area, offset, &dio->metadata_offset);
1896 static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map)
1898 struct dm_integrity_c *ic = dio->ic;
1899 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1900 unsigned journal_section, journal_entry;
1901 unsigned journal_read_pos;
1902 struct completion read_comp;
1903 bool need_sync_io = ic->internal_hash && !dio->write;
1905 if (need_sync_io && from_map) {
1906 INIT_WORK(&dio->work, integrity_bio_wait);
1907 queue_work(ic->offload_wq, &dio->work);
1912 spin_lock_irq(&ic->endio_wait.lock);
1914 if (unlikely(dm_integrity_failed(ic))) {
1915 spin_unlock_irq(&ic->endio_wait.lock);
1919 dio->range.n_sectors = bio_sectors(bio);
1920 journal_read_pos = NOT_FOUND;
1921 if (likely(ic->mode == 'J')) {
1923 unsigned next_entry, i, pos;
1924 unsigned ws, we, range_sectors;
1926 dio->range.n_sectors = min(dio->range.n_sectors,
1927 (sector_t)ic->free_sectors << ic->sb->log2_sectors_per_block);
1928 if (unlikely(!dio->range.n_sectors)) {
1930 goto offload_to_thread;
1931 sleep_on_endio_wait(ic);
1934 range_sectors = dio->range.n_sectors >> ic->sb->log2_sectors_per_block;
1935 ic->free_sectors -= range_sectors;
1936 journal_section = ic->free_section;
1937 journal_entry = ic->free_section_entry;
1939 next_entry = ic->free_section_entry + range_sectors;
1940 ic->free_section_entry = next_entry % ic->journal_section_entries;
1941 ic->free_section += next_entry / ic->journal_section_entries;
1942 ic->n_uncommitted_sections += next_entry / ic->journal_section_entries;
1943 wraparound_section(ic, &ic->free_section);
1945 pos = journal_section * ic->journal_section_entries + journal_entry;
1946 ws = journal_section;
1950 struct journal_entry *je;
1952 add_journal_node(ic, &ic->journal_tree[pos], dio->range.logical_sector + i);
1954 if (unlikely(pos >= ic->journal_entries))
1957 je = access_journal_entry(ic, ws, we);
1958 BUG_ON(!journal_entry_is_unused(je));
1959 journal_entry_set_inprogress(je);
1961 if (unlikely(we == ic->journal_section_entries)) {
1964 wraparound_section(ic, &ws);
1966 } while ((i += ic->sectors_per_block) < dio->range.n_sectors);
1968 spin_unlock_irq(&ic->endio_wait.lock);
1969 goto journal_read_write;
1971 sector_t next_sector;
1972 journal_read_pos = find_journal_node(ic, dio->range.logical_sector, &next_sector);
1973 if (likely(journal_read_pos == NOT_FOUND)) {
1974 if (unlikely(dio->range.n_sectors > next_sector - dio->range.logical_sector))
1975 dio->range.n_sectors = next_sector - dio->range.logical_sector;
1978 unsigned jp = journal_read_pos + 1;
1979 for (i = ic->sectors_per_block; i < dio->range.n_sectors; i += ic->sectors_per_block, jp++) {
1980 if (!test_journal_node(ic, jp, dio->range.logical_sector + i))
1983 dio->range.n_sectors = i;
1987 if (unlikely(!add_new_range(ic, &dio->range, true))) {
1989 * We must not sleep in the request routine because it could
1990 * stall bios on current->bio_list.
1991 * So, we offload the bio to a workqueue if we have to sleep.
1995 spin_unlock_irq(&ic->endio_wait.lock);
1996 INIT_WORK(&dio->work, integrity_bio_wait);
1997 queue_work(ic->wait_wq, &dio->work);
2000 if (journal_read_pos != NOT_FOUND)
2001 dio->range.n_sectors = ic->sectors_per_block;
2002 wait_and_add_new_range(ic, &dio->range);
2004 * wait_and_add_new_range drops the spinlock, so the journal
2005 * may have been changed arbitrarily. We need to recheck.
2006 * To simplify the code, we restrict I/O size to just one block.
2008 if (journal_read_pos != NOT_FOUND) {
2009 sector_t next_sector;
2010 unsigned new_pos = find_journal_node(ic, dio->range.logical_sector, &next_sector);
2011 if (unlikely(new_pos != journal_read_pos)) {
2012 remove_range_unlocked(ic, &dio->range);
2017 spin_unlock_irq(&ic->endio_wait.lock);
2019 if (unlikely(journal_read_pos != NOT_FOUND)) {
2020 journal_section = journal_read_pos / ic->journal_section_entries;
2021 journal_entry = journal_read_pos % ic->journal_section_entries;
2022 goto journal_read_write;
2025 if (ic->mode == 'B' && dio->write) {
2026 if (!block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector,
2027 dio->range.n_sectors, BITMAP_OP_TEST_ALL_SET)) {
2028 struct bitmap_block_status *bbs;
2030 bbs = sector_to_bitmap_block(ic, dio->range.logical_sector);
2031 spin_lock(&bbs->bio_queue_lock);
2032 bio_list_add(&bbs->bio_queue, bio);
2033 spin_unlock(&bbs->bio_queue_lock);
2034 queue_work(ic->writer_wq, &bbs->work);
2039 dio->in_flight = (atomic_t)ATOMIC_INIT(2);
2042 init_completion(&read_comp);
2043 dio->completion = &read_comp;
2045 dio->completion = NULL;
2047 dm_bio_record(&dio->bio_details, bio);
2048 bio_set_dev(bio, ic->dev->bdev);
2049 bio->bi_integrity = NULL;
2050 bio->bi_opf &= ~REQ_INTEGRITY;
2051 bio->bi_end_io = integrity_end_io;
2052 bio->bi_iter.bi_size = dio->range.n_sectors << SECTOR_SHIFT;
2054 generic_make_request(bio);
2057 wait_for_completion_io(&read_comp);
2058 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING) &&
2059 dio->range.logical_sector + dio->range.n_sectors > le64_to_cpu(ic->sb->recalc_sector))
2061 if (ic->mode == 'B') {
2062 if (!block_bitmap_op(ic, ic->recalc_bitmap, dio->range.logical_sector,
2063 dio->range.n_sectors, BITMAP_OP_TEST_ALL_CLEAR))
2067 if (likely(!bio->bi_status))
2068 integrity_metadata(&dio->work);
2074 INIT_WORK(&dio->work, integrity_metadata);
2075 queue_work(ic->metadata_wq, &dio->work);
2081 if (unlikely(__journal_read_write(dio, bio, journal_section, journal_entry)))
2084 do_endio_flush(ic, dio);
2088 static void integrity_bio_wait(struct work_struct *w)
2090 struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work);
2092 dm_integrity_map_continue(dio, false);
2095 static void pad_uncommitted(struct dm_integrity_c *ic)
2097 if (ic->free_section_entry) {
2098 ic->free_sectors -= ic->journal_section_entries - ic->free_section_entry;
2099 ic->free_section_entry = 0;
2101 wraparound_section(ic, &ic->free_section);
2102 ic->n_uncommitted_sections++;
2104 if (WARN_ON(ic->journal_sections * ic->journal_section_entries !=
2105 (ic->n_uncommitted_sections + ic->n_committed_sections) *
2106 ic->journal_section_entries + ic->free_sectors)) {
2107 DMCRIT("journal_sections %u, journal_section_entries %u, "
2108 "n_uncommitted_sections %u, n_committed_sections %u, "
2109 "journal_section_entries %u, free_sectors %u",
2110 ic->journal_sections, ic->journal_section_entries,
2111 ic->n_uncommitted_sections, ic->n_committed_sections,
2112 ic->journal_section_entries, ic->free_sectors);
2116 static void integrity_commit(struct work_struct *w)
2118 struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, commit_work);
2119 unsigned commit_start, commit_sections;
2121 struct bio *flushes;
2123 del_timer(&ic->autocommit_timer);
2125 spin_lock_irq(&ic->endio_wait.lock);
2126 flushes = bio_list_get(&ic->flush_bio_list);
2127 if (unlikely(ic->mode != 'J')) {
2128 spin_unlock_irq(&ic->endio_wait.lock);
2129 dm_integrity_flush_buffers(ic, true);
2130 goto release_flush_bios;
2133 pad_uncommitted(ic);
2134 commit_start = ic->uncommitted_section;
2135 commit_sections = ic->n_uncommitted_sections;
2136 spin_unlock_irq(&ic->endio_wait.lock);
2138 if (!commit_sections)
2139 goto release_flush_bios;
2142 for (n = 0; n < commit_sections; n++) {
2143 for (j = 0; j < ic->journal_section_entries; j++) {
2144 struct journal_entry *je;
2145 je = access_journal_entry(ic, i, j);
2146 io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je));
2148 for (j = 0; j < ic->journal_section_sectors; j++) {
2149 struct journal_sector *js;
2150 js = access_journal(ic, i, j);
2151 js->commit_id = dm_integrity_commit_id(ic, i, j, ic->commit_seq);
2154 if (unlikely(i >= ic->journal_sections))
2155 ic->commit_seq = next_commit_seq(ic->commit_seq);
2156 wraparound_section(ic, &i);
2160 write_journal(ic, commit_start, commit_sections);
2162 spin_lock_irq(&ic->endio_wait.lock);
2163 ic->uncommitted_section += commit_sections;
2164 wraparound_section(ic, &ic->uncommitted_section);
2165 ic->n_uncommitted_sections -= commit_sections;
2166 ic->n_committed_sections += commit_sections;
2167 spin_unlock_irq(&ic->endio_wait.lock);
2169 if (READ_ONCE(ic->free_sectors) <= ic->free_sectors_threshold)
2170 queue_work(ic->writer_wq, &ic->writer_work);
2174 struct bio *next = flushes->bi_next;
2175 flushes->bi_next = NULL;
2176 do_endio(ic, flushes);
2181 static void complete_copy_from_journal(unsigned long error, void *context)
2183 struct journal_io *io = context;
2184 struct journal_completion *comp = io->comp;
2185 struct dm_integrity_c *ic = comp->ic;
2186 remove_range(ic, &io->range);
2187 mempool_free(io, &ic->journal_io_mempool);
2188 if (unlikely(error != 0))
2189 dm_integrity_io_error(ic, "copying from journal", -EIO);
2190 complete_journal_op(comp);
2193 static void restore_last_bytes(struct dm_integrity_c *ic, struct journal_sector *js,
2194 struct journal_entry *je)
2198 js->commit_id = je->last_bytes[s];
2200 } while (++s < ic->sectors_per_block);
2203 static void do_journal_write(struct dm_integrity_c *ic, unsigned write_start,
2204 unsigned write_sections, bool from_replay)
2207 struct journal_completion comp;
2208 struct blk_plug plug;
2210 blk_start_plug(&plug);
2213 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
2214 init_completion(&comp.comp);
2217 for (n = 0; n < write_sections; n++, i++, wraparound_section(ic, &i)) {
2218 #ifndef INTERNAL_VERIFY
2219 if (unlikely(from_replay))
2221 rw_section_mac(ic, i, false);
2222 for (j = 0; j < ic->journal_section_entries; j++) {
2223 struct journal_entry *je = access_journal_entry(ic, i, j);
2224 sector_t sec, area, offset;
2225 unsigned k, l, next_loop;
2226 sector_t metadata_block;
2227 unsigned metadata_offset;
2228 struct journal_io *io;
2230 if (journal_entry_is_unused(je))
2232 BUG_ON(unlikely(journal_entry_is_inprogress(je)) && !from_replay);
2233 sec = journal_entry_get_sector(je);
2234 if (unlikely(from_replay)) {
2235 if (unlikely(sec & (unsigned)(ic->sectors_per_block - 1))) {
2236 dm_integrity_io_error(ic, "invalid sector in journal", -EIO);
2237 sec &= ~(sector_t)(ic->sectors_per_block - 1);
2240 get_area_and_offset(ic, sec, &area, &offset);
2241 restore_last_bytes(ic, access_journal_data(ic, i, j), je);
2242 for (k = j + 1; k < ic->journal_section_entries; k++) {
2243 struct journal_entry *je2 = access_journal_entry(ic, i, k);
2244 sector_t sec2, area2, offset2;
2245 if (journal_entry_is_unused(je2))
2247 BUG_ON(unlikely(journal_entry_is_inprogress(je2)) && !from_replay);
2248 sec2 = journal_entry_get_sector(je2);
2249 get_area_and_offset(ic, sec2, &area2, &offset2);
2250 if (area2 != area || offset2 != offset + ((k - j) << ic->sb->log2_sectors_per_block))
2252 restore_last_bytes(ic, access_journal_data(ic, i, k), je2);
2256 io = mempool_alloc(&ic->journal_io_mempool, GFP_NOIO);
2258 io->range.logical_sector = sec;
2259 io->range.n_sectors = (k - j) << ic->sb->log2_sectors_per_block;
2261 spin_lock_irq(&ic->endio_wait.lock);
2262 add_new_range_and_wait(ic, &io->range);
2264 if (likely(!from_replay)) {
2265 struct journal_node *section_node = &ic->journal_tree[i * ic->journal_section_entries];
2267 /* don't write if there is newer committed sector */
2268 while (j < k && find_newer_committed_node(ic, §ion_node[j])) {
2269 struct journal_entry *je2 = access_journal_entry(ic, i, j);
2271 journal_entry_set_unused(je2);
2272 remove_journal_node(ic, §ion_node[j]);
2274 sec += ic->sectors_per_block;
2275 offset += ic->sectors_per_block;
2277 while (j < k && find_newer_committed_node(ic, §ion_node[k - 1])) {
2278 struct journal_entry *je2 = access_journal_entry(ic, i, k - 1);
2280 journal_entry_set_unused(je2);
2281 remove_journal_node(ic, §ion_node[k - 1]);
2285 remove_range_unlocked(ic, &io->range);
2286 spin_unlock_irq(&ic->endio_wait.lock);
2287 mempool_free(io, &ic->journal_io_mempool);
2290 for (l = j; l < k; l++) {
2291 remove_journal_node(ic, §ion_node[l]);
2294 spin_unlock_irq(&ic->endio_wait.lock);
2296 metadata_block = get_metadata_sector_and_offset(ic, area, offset, &metadata_offset);
2297 for (l = j; l < k; l++) {
2299 struct journal_entry *je2 = access_journal_entry(ic, i, l);
2302 #ifndef INTERNAL_VERIFY
2303 unlikely(from_replay) &&
2305 ic->internal_hash) {
2306 char test_tag[max_t(size_t, HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)];
2308 integrity_sector_checksum(ic, sec + ((l - j) << ic->sb->log2_sectors_per_block),
2309 (char *)access_journal_data(ic, i, l), test_tag);
2310 if (unlikely(memcmp(test_tag, journal_entry_tag(ic, je2), ic->tag_size)))
2311 dm_integrity_io_error(ic, "tag mismatch when replaying journal", -EILSEQ);
2314 journal_entry_set_unused(je2);
2315 r = dm_integrity_rw_tag(ic, journal_entry_tag(ic, je2), &metadata_block, &metadata_offset,
2316 ic->tag_size, TAG_WRITE);
2318 dm_integrity_io_error(ic, "reading tags", r);
2322 atomic_inc(&comp.in_flight);
2323 copy_from_journal(ic, i, j << ic->sb->log2_sectors_per_block,
2324 (k - j) << ic->sb->log2_sectors_per_block,
2325 get_data_sector(ic, area, offset),
2326 complete_copy_from_journal, io);
2332 dm_bufio_write_dirty_buffers_async(ic->bufio);
2334 blk_finish_plug(&plug);
2336 complete_journal_op(&comp);
2337 wait_for_completion_io(&comp.comp);
2339 dm_integrity_flush_buffers(ic, true);
2342 static void integrity_writer(struct work_struct *w)
2344 struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, writer_work);
2345 unsigned write_start, write_sections;
2347 unsigned prev_free_sectors;
2349 spin_lock_irq(&ic->endio_wait.lock);
2350 write_start = ic->committed_section;
2351 write_sections = ic->n_committed_sections;
2352 spin_unlock_irq(&ic->endio_wait.lock);
2354 if (!write_sections)
2357 do_journal_write(ic, write_start, write_sections, false);
2359 spin_lock_irq(&ic->endio_wait.lock);
2361 ic->committed_section += write_sections;
2362 wraparound_section(ic, &ic->committed_section);
2363 ic->n_committed_sections -= write_sections;
2365 prev_free_sectors = ic->free_sectors;
2366 ic->free_sectors += write_sections * ic->journal_section_entries;
2367 if (unlikely(!prev_free_sectors))
2368 wake_up_locked(&ic->endio_wait);
2370 spin_unlock_irq(&ic->endio_wait.lock);
2373 static void recalc_write_super(struct dm_integrity_c *ic)
2377 dm_integrity_flush_buffers(ic, false);
2378 if (dm_integrity_failed(ic))
2381 r = sync_rw_sb(ic, REQ_OP_WRITE, 0);
2383 dm_integrity_io_error(ic, "writing superblock", r);
2386 static void integrity_recalc(struct work_struct *w)
2388 struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, recalc_work);
2389 struct dm_integrity_range range;
2390 struct dm_io_request io_req;
2391 struct dm_io_region io_loc;
2392 sector_t area, offset;
2393 sector_t metadata_block;
2394 unsigned metadata_offset;
2395 sector_t logical_sector, n_sectors;
2399 unsigned super_counter = 0;
2401 DEBUG_print("start recalculation... (position %llx)\n", le64_to_cpu(ic->sb->recalc_sector));
2403 spin_lock_irq(&ic->endio_wait.lock);
2407 if (unlikely(dm_post_suspending(ic->ti)))
2410 range.logical_sector = le64_to_cpu(ic->sb->recalc_sector);
2411 if (unlikely(range.logical_sector >= ic->provided_data_sectors)) {
2412 if (ic->mode == 'B') {
2413 block_bitmap_op(ic, ic->recalc_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR);
2414 DEBUG_print("queue_delayed_work: bitmap_flush_work\n");
2415 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0);
2420 get_area_and_offset(ic, range.logical_sector, &area, &offset);
2421 range.n_sectors = min((sector_t)RECALC_SECTORS, ic->provided_data_sectors - range.logical_sector);
2423 range.n_sectors = min(range.n_sectors, ((sector_t)1U << ic->sb->log2_interleave_sectors) - (unsigned)offset);
2425 add_new_range_and_wait(ic, &range);
2426 spin_unlock_irq(&ic->endio_wait.lock);
2427 logical_sector = range.logical_sector;
2428 n_sectors = range.n_sectors;
2430 if (ic->mode == 'B') {
2431 if (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector, n_sectors, BITMAP_OP_TEST_ALL_CLEAR)) {
2432 goto advance_and_next;
2434 while (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector,
2435 ic->sectors_per_block, BITMAP_OP_TEST_ALL_CLEAR)) {
2436 logical_sector += ic->sectors_per_block;
2437 n_sectors -= ic->sectors_per_block;
2440 while (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector + n_sectors - ic->sectors_per_block,
2441 ic->sectors_per_block, BITMAP_OP_TEST_ALL_CLEAR)) {
2442 n_sectors -= ic->sectors_per_block;
2445 get_area_and_offset(ic, logical_sector, &area, &offset);
2448 DEBUG_print("recalculating: %lx, %lx\n", logical_sector, n_sectors);
2450 if (unlikely(++super_counter == RECALC_WRITE_SUPER)) {
2451 recalc_write_super(ic);
2452 if (ic->mode == 'B') {
2453 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, ic->bitmap_flush_interval);
2458 if (unlikely(dm_integrity_failed(ic)))
2461 io_req.bi_op = REQ_OP_READ;
2462 io_req.bi_op_flags = 0;
2463 io_req.mem.type = DM_IO_VMA;
2464 io_req.mem.ptr.addr = ic->recalc_buffer;
2465 io_req.notify.fn = NULL;
2466 io_req.client = ic->io;
2467 io_loc.bdev = ic->dev->bdev;
2468 io_loc.sector = get_data_sector(ic, area, offset);
2469 io_loc.count = n_sectors;
2471 r = dm_io(&io_req, 1, &io_loc, NULL);
2473 dm_integrity_io_error(ic, "reading data", r);
2477 t = ic->recalc_tags;
2478 for (i = 0; i < n_sectors; i += ic->sectors_per_block) {
2479 integrity_sector_checksum(ic, logical_sector + i, ic->recalc_buffer + (i << SECTOR_SHIFT), t);
2483 metadata_block = get_metadata_sector_and_offset(ic, area, offset, &metadata_offset);
2485 r = dm_integrity_rw_tag(ic, ic->recalc_tags, &metadata_block, &metadata_offset, t - ic->recalc_tags, TAG_WRITE);
2487 dm_integrity_io_error(ic, "writing tags", r);
2491 if (ic->mode == 'B') {
2492 sector_t start, end;
2493 start = (range.logical_sector >>
2494 (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit)) <<
2495 (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
2496 end = ((range.logical_sector + range.n_sectors) >>
2497 (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit)) <<
2498 (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
2499 block_bitmap_op(ic, ic->recalc_bitmap, start, end - start, BITMAP_OP_CLEAR);
2505 spin_lock_irq(&ic->endio_wait.lock);
2506 remove_range_unlocked(ic, &range);
2507 ic->sb->recalc_sector = cpu_to_le64(range.logical_sector + range.n_sectors);
2511 remove_range(ic, &range);
2515 spin_unlock_irq(&ic->endio_wait.lock);
2517 recalc_write_super(ic);
2520 static void bitmap_block_work(struct work_struct *w)
2522 struct bitmap_block_status *bbs = container_of(w, struct bitmap_block_status, work);
2523 struct dm_integrity_c *ic = bbs->ic;
2525 struct bio_list bio_queue;
2526 struct bio_list waiting;
2528 bio_list_init(&waiting);
2530 spin_lock(&bbs->bio_queue_lock);
2531 bio_queue = bbs->bio_queue;
2532 bio_list_init(&bbs->bio_queue);
2533 spin_unlock(&bbs->bio_queue_lock);
2535 while ((bio = bio_list_pop(&bio_queue))) {
2536 struct dm_integrity_io *dio;
2538 dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
2540 if (block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector,
2541 dio->range.n_sectors, BITMAP_OP_TEST_ALL_SET)) {
2542 remove_range(ic, &dio->range);
2543 INIT_WORK(&dio->work, integrity_bio_wait);
2544 queue_work(ic->offload_wq, &dio->work);
2546 block_bitmap_op(ic, ic->journal, dio->range.logical_sector,
2547 dio->range.n_sectors, BITMAP_OP_SET);
2548 bio_list_add(&waiting, bio);
2552 if (bio_list_empty(&waiting))
2555 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC,
2556 bbs->idx * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT),
2557 BITMAP_BLOCK_SIZE >> SECTOR_SHIFT, NULL);
2559 while ((bio = bio_list_pop(&waiting))) {
2560 struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
2562 block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector,
2563 dio->range.n_sectors, BITMAP_OP_SET);
2565 remove_range(ic, &dio->range);
2566 INIT_WORK(&dio->work, integrity_bio_wait);
2567 queue_work(ic->offload_wq, &dio->work);
2570 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, ic->bitmap_flush_interval);
2573 static void bitmap_flush_work(struct work_struct *work)
2575 struct dm_integrity_c *ic = container_of(work, struct dm_integrity_c, bitmap_flush_work.work);
2576 struct dm_integrity_range range;
2577 unsigned long limit;
2580 dm_integrity_flush_buffers(ic, false);
2582 range.logical_sector = 0;
2583 range.n_sectors = ic->provided_data_sectors;
2585 spin_lock_irq(&ic->endio_wait.lock);
2586 add_new_range_and_wait(ic, &range);
2587 spin_unlock_irq(&ic->endio_wait.lock);
2589 dm_integrity_flush_buffers(ic, true);
2591 blkdev_issue_flush(ic->dev->bdev, GFP_NOIO, NULL);
2593 limit = ic->provided_data_sectors;
2594 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) {
2595 limit = le64_to_cpu(ic->sb->recalc_sector)
2596 >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit)
2597 << (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
2599 /*DEBUG_print("zeroing journal\n");*/
2600 block_bitmap_op(ic, ic->journal, 0, limit, BITMAP_OP_CLEAR);
2601 block_bitmap_op(ic, ic->may_write_bitmap, 0, limit, BITMAP_OP_CLEAR);
2603 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, 0,
2604 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
2606 spin_lock_irq(&ic->endio_wait.lock);
2607 remove_range_unlocked(ic, &range);
2608 while (unlikely((bio = bio_list_pop(&ic->synchronous_bios)) != NULL)) {
2610 spin_unlock_irq(&ic->endio_wait.lock);
2611 spin_lock_irq(&ic->endio_wait.lock);
2613 spin_unlock_irq(&ic->endio_wait.lock);
2617 static void init_journal(struct dm_integrity_c *ic, unsigned start_section,
2618 unsigned n_sections, unsigned char commit_seq)
2625 for (n = 0; n < n_sections; n++) {
2626 i = start_section + n;
2627 wraparound_section(ic, &i);
2628 for (j = 0; j < ic->journal_section_sectors; j++) {
2629 struct journal_sector *js = access_journal(ic, i, j);
2630 memset(&js->entries, 0, JOURNAL_SECTOR_DATA);
2631 js->commit_id = dm_integrity_commit_id(ic, i, j, commit_seq);
2633 for (j = 0; j < ic->journal_section_entries; j++) {
2634 struct journal_entry *je = access_journal_entry(ic, i, j);
2635 journal_entry_set_unused(je);
2639 write_journal(ic, start_section, n_sections);
2642 static int find_commit_seq(struct dm_integrity_c *ic, unsigned i, unsigned j, commit_id_t id)
2645 for (k = 0; k < N_COMMIT_IDS; k++) {
2646 if (dm_integrity_commit_id(ic, i, j, k) == id)
2649 dm_integrity_io_error(ic, "journal commit id", -EIO);
2653 static void replay_journal(struct dm_integrity_c *ic)
2656 bool used_commit_ids[N_COMMIT_IDS];
2657 unsigned max_commit_id_sections[N_COMMIT_IDS];
2658 unsigned write_start, write_sections;
2659 unsigned continue_section;
2661 unsigned char unused, last_used, want_commit_seq;
2663 if (ic->mode == 'R')
2666 if (ic->journal_uptodate)
2672 if (!ic->just_formatted) {
2673 DEBUG_print("reading journal\n");
2674 rw_journal(ic, REQ_OP_READ, 0, 0, ic->journal_sections, NULL);
2676 DEBUG_bytes(lowmem_page_address(ic->journal_io[0].page), 64, "read journal");
2677 if (ic->journal_io) {
2678 struct journal_completion crypt_comp;
2680 init_completion(&crypt_comp.comp);
2681 crypt_comp.in_flight = (atomic_t)ATOMIC_INIT(0);
2682 encrypt_journal(ic, false, 0, ic->journal_sections, &crypt_comp);
2683 wait_for_completion(&crypt_comp.comp);
2685 DEBUG_bytes(lowmem_page_address(ic->journal[0].page), 64, "decrypted journal");
2688 if (dm_integrity_failed(ic))
2691 journal_empty = true;
2692 memset(used_commit_ids, 0, sizeof used_commit_ids);
2693 memset(max_commit_id_sections, 0, sizeof max_commit_id_sections);
2694 for (i = 0; i < ic->journal_sections; i++) {
2695 for (j = 0; j < ic->journal_section_sectors; j++) {
2697 struct journal_sector *js = access_journal(ic, i, j);
2698 k = find_commit_seq(ic, i, j, js->commit_id);
2701 used_commit_ids[k] = true;
2702 max_commit_id_sections[k] = i;
2704 if (journal_empty) {
2705 for (j = 0; j < ic->journal_section_entries; j++) {
2706 struct journal_entry *je = access_journal_entry(ic, i, j);
2707 if (!journal_entry_is_unused(je)) {
2708 journal_empty = false;
2715 if (!used_commit_ids[N_COMMIT_IDS - 1]) {
2716 unused = N_COMMIT_IDS - 1;
2717 while (unused && !used_commit_ids[unused - 1])
2720 for (unused = 0; unused < N_COMMIT_IDS; unused++)
2721 if (!used_commit_ids[unused])
2723 if (unused == N_COMMIT_IDS) {
2724 dm_integrity_io_error(ic, "journal commit ids", -EIO);
2728 DEBUG_print("first unused commit seq %d [%d,%d,%d,%d]\n",
2729 unused, used_commit_ids[0], used_commit_ids[1],
2730 used_commit_ids[2], used_commit_ids[3]);
2732 last_used = prev_commit_seq(unused);
2733 want_commit_seq = prev_commit_seq(last_used);
2735 if (!used_commit_ids[want_commit_seq] && used_commit_ids[prev_commit_seq(want_commit_seq)])
2736 journal_empty = true;
2738 write_start = max_commit_id_sections[last_used] + 1;
2739 if (unlikely(write_start >= ic->journal_sections))
2740 want_commit_seq = next_commit_seq(want_commit_seq);
2741 wraparound_section(ic, &write_start);
2744 for (write_sections = 0; write_sections < ic->journal_sections; write_sections++) {
2745 for (j = 0; j < ic->journal_section_sectors; j++) {
2746 struct journal_sector *js = access_journal(ic, i, j);
2748 if (js->commit_id != dm_integrity_commit_id(ic, i, j, want_commit_seq)) {
2750 * This could be caused by crash during writing.
2751 * We won't replay the inconsistent part of the
2754 DEBUG_print("commit id mismatch at position (%u, %u): %d != %d\n",
2755 i, j, find_commit_seq(ic, i, j, js->commit_id), want_commit_seq);
2760 if (unlikely(i >= ic->journal_sections))
2761 want_commit_seq = next_commit_seq(want_commit_seq);
2762 wraparound_section(ic, &i);
2766 if (!journal_empty) {
2767 DEBUG_print("replaying %u sections, starting at %u, commit seq %d\n",
2768 write_sections, write_start, want_commit_seq);
2769 do_journal_write(ic, write_start, write_sections, true);
2772 if (write_sections == ic->journal_sections && (ic->mode == 'J' || journal_empty)) {
2773 continue_section = write_start;
2774 ic->commit_seq = want_commit_seq;
2775 DEBUG_print("continuing from section %u, commit seq %d\n", write_start, ic->commit_seq);
2778 unsigned char erase_seq;
2780 DEBUG_print("clearing journal\n");
2782 erase_seq = prev_commit_seq(prev_commit_seq(last_used));
2784 init_journal(ic, s, 1, erase_seq);
2786 wraparound_section(ic, &s);
2787 if (ic->journal_sections >= 2) {
2788 init_journal(ic, s, ic->journal_sections - 2, erase_seq);
2789 s += ic->journal_sections - 2;
2790 wraparound_section(ic, &s);
2791 init_journal(ic, s, 1, erase_seq);
2794 continue_section = 0;
2795 ic->commit_seq = next_commit_seq(erase_seq);
2798 ic->committed_section = continue_section;
2799 ic->n_committed_sections = 0;
2801 ic->uncommitted_section = continue_section;
2802 ic->n_uncommitted_sections = 0;
2804 ic->free_section = continue_section;
2805 ic->free_section_entry = 0;
2806 ic->free_sectors = ic->journal_entries;
2808 ic->journal_tree_root = RB_ROOT;
2809 for (i = 0; i < ic->journal_entries; i++)
2810 init_journal_node(&ic->journal_tree[i]);
2813 static void dm_integrity_enter_synchronous_mode(struct dm_integrity_c *ic)
2815 DEBUG_print("dm_integrity_enter_synchronous_mode\n");
2817 if (ic->mode == 'B') {
2818 ic->bitmap_flush_interval = msecs_to_jiffies(10) + 1;
2819 ic->synchronous_mode = 1;
2821 cancel_delayed_work_sync(&ic->bitmap_flush_work);
2822 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0);
2823 flush_workqueue(ic->commit_wq);
2827 static int dm_integrity_reboot(struct notifier_block *n, unsigned long code, void *x)
2829 struct dm_integrity_c *ic = container_of(n, struct dm_integrity_c, reboot_notifier);
2831 DEBUG_print("dm_integrity_reboot\n");
2833 dm_integrity_enter_synchronous_mode(ic);
2838 static void dm_integrity_postsuspend(struct dm_target *ti)
2840 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
2843 WARN_ON(unregister_reboot_notifier(&ic->reboot_notifier));
2845 del_timer_sync(&ic->autocommit_timer);
2848 drain_workqueue(ic->recalc_wq);
2850 if (ic->mode == 'B')
2851 cancel_delayed_work_sync(&ic->bitmap_flush_work);
2853 queue_work(ic->commit_wq, &ic->commit_work);
2854 drain_workqueue(ic->commit_wq);
2856 if (ic->mode == 'J') {
2857 queue_work(ic->writer_wq, &ic->writer_work);
2858 drain_workqueue(ic->writer_wq);
2859 dm_integrity_flush_buffers(ic, true);
2862 if (ic->mode == 'B') {
2863 dm_integrity_flush_buffers(ic, true);
2865 /* set to 0 to test bitmap replay code */
2866 init_journal(ic, 0, ic->journal_sections, 0);
2867 ic->sb->flags &= ~cpu_to_le32(SB_FLAG_DIRTY_BITMAP);
2868 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
2870 dm_integrity_io_error(ic, "writing superblock", r);
2874 BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress));
2876 ic->journal_uptodate = true;
2879 static void dm_integrity_resume(struct dm_target *ti)
2881 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
2883 DEBUG_print("resume\n");
2885 if (ic->sb->flags & cpu_to_le32(SB_FLAG_DIRTY_BITMAP)) {
2886 DEBUG_print("resume dirty_bitmap\n");
2887 rw_journal_sectors(ic, REQ_OP_READ, 0, 0,
2888 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
2889 if (ic->mode == 'B') {
2890 if (ic->sb->log2_blocks_per_bitmap_bit == ic->log2_blocks_per_bitmap_bit) {
2891 block_bitmap_copy(ic, ic->recalc_bitmap, ic->journal);
2892 block_bitmap_copy(ic, ic->may_write_bitmap, ic->journal);
2893 if (!block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors,
2894 BITMAP_OP_TEST_ALL_CLEAR)) {
2895 ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
2896 ic->sb->recalc_sector = cpu_to_le64(0);
2899 DEBUG_print("non-matching blocks_per_bitmap_bit: %u, %u\n",
2900 ic->sb->log2_blocks_per_bitmap_bit, ic->log2_blocks_per_bitmap_bit);
2901 ic->sb->log2_blocks_per_bitmap_bit = ic->log2_blocks_per_bitmap_bit;
2902 block_bitmap_op(ic, ic->recalc_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_SET);
2903 block_bitmap_op(ic, ic->may_write_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_SET);
2904 block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_SET);
2905 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, 0,
2906 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
2907 ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
2908 ic->sb->recalc_sector = cpu_to_le64(0);
2911 if (!(ic->sb->log2_blocks_per_bitmap_bit == ic->log2_blocks_per_bitmap_bit &&
2912 block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_TEST_ALL_CLEAR))) {
2913 ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
2914 ic->sb->recalc_sector = cpu_to_le64(0);
2916 init_journal(ic, 0, ic->journal_sections, 0);
2918 ic->sb->flags &= ~cpu_to_le32(SB_FLAG_DIRTY_BITMAP);
2920 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
2922 dm_integrity_io_error(ic, "writing superblock", r);
2925 if (ic->mode == 'B') {
2926 ic->sb->flags |= cpu_to_le32(SB_FLAG_DIRTY_BITMAP);
2927 ic->sb->log2_blocks_per_bitmap_bit = ic->log2_blocks_per_bitmap_bit;
2928 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
2930 dm_integrity_io_error(ic, "writing superblock", r);
2932 block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR);
2933 block_bitmap_op(ic, ic->recalc_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR);
2934 block_bitmap_op(ic, ic->may_write_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR);
2935 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING) &&
2936 le64_to_cpu(ic->sb->recalc_sector) < ic->provided_data_sectors) {
2937 block_bitmap_op(ic, ic->journal, le64_to_cpu(ic->sb->recalc_sector),
2938 ic->provided_data_sectors - le64_to_cpu(ic->sb->recalc_sector), BITMAP_OP_SET);
2939 block_bitmap_op(ic, ic->recalc_bitmap, le64_to_cpu(ic->sb->recalc_sector),
2940 ic->provided_data_sectors - le64_to_cpu(ic->sb->recalc_sector), BITMAP_OP_SET);
2941 block_bitmap_op(ic, ic->may_write_bitmap, le64_to_cpu(ic->sb->recalc_sector),
2942 ic->provided_data_sectors - le64_to_cpu(ic->sb->recalc_sector), BITMAP_OP_SET);
2944 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, 0,
2945 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
2949 DEBUG_print("testing recalc: %x\n", ic->sb->flags);
2950 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) {
2951 __u64 recalc_pos = le64_to_cpu(ic->sb->recalc_sector);
2952 DEBUG_print("recalc pos: %lx / %lx\n", (long)recalc_pos, ic->provided_data_sectors);
2953 if (recalc_pos < ic->provided_data_sectors) {
2954 queue_work(ic->recalc_wq, &ic->recalc_work);
2955 } else if (recalc_pos > ic->provided_data_sectors) {
2956 ic->sb->recalc_sector = cpu_to_le64(ic->provided_data_sectors);
2957 recalc_write_super(ic);
2961 ic->reboot_notifier.notifier_call = dm_integrity_reboot;
2962 ic->reboot_notifier.next = NULL;
2963 ic->reboot_notifier.priority = INT_MAX - 1; /* be notified after md and before hardware drivers */
2964 WARN_ON(register_reboot_notifier(&ic->reboot_notifier));
2967 /* set to 1 to stress test synchronous mode */
2968 dm_integrity_enter_synchronous_mode(ic);
2972 static void dm_integrity_status(struct dm_target *ti, status_type_t type,
2973 unsigned status_flags, char *result, unsigned maxlen)
2975 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
2980 case STATUSTYPE_INFO:
2982 (unsigned long long)atomic64_read(&ic->number_of_mismatches),
2983 (unsigned long long)ic->provided_data_sectors);
2984 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))
2985 DMEMIT(" %llu", (unsigned long long)le64_to_cpu(ic->sb->recalc_sector));
2990 case STATUSTYPE_TABLE: {
2991 __u64 watermark_percentage = (__u64)(ic->journal_entries - ic->free_sectors_threshold) * 100;
2992 watermark_percentage += ic->journal_entries / 2;
2993 do_div(watermark_percentage, ic->journal_entries);
2995 arg_count += !!ic->meta_dev;
2996 arg_count += ic->sectors_per_block != 1;
2997 arg_count += !!(ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING));
2998 arg_count += ic->mode == 'J';
2999 arg_count += ic->mode == 'J';
3000 arg_count += ic->mode == 'B';
3001 arg_count += ic->mode == 'B';
3002 arg_count += !!ic->internal_hash_alg.alg_string;
3003 arg_count += !!ic->journal_crypt_alg.alg_string;
3004 arg_count += !!ic->journal_mac_alg.alg_string;
3005 arg_count += ic->legacy_recalculate;
3006 DMEMIT("%s %llu %u %c %u", ic->dev->name, (unsigned long long)ic->start,
3007 ic->tag_size, ic->mode, arg_count);
3009 DMEMIT(" meta_device:%s", ic->meta_dev->name);
3010 if (ic->sectors_per_block != 1)
3011 DMEMIT(" block_size:%u", ic->sectors_per_block << SECTOR_SHIFT);
3012 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))
3013 DMEMIT(" recalculate");
3014 DMEMIT(" journal_sectors:%u", ic->initial_sectors - SB_SECTORS);
3015 DMEMIT(" interleave_sectors:%u", 1U << ic->sb->log2_interleave_sectors);
3016 DMEMIT(" buffer_sectors:%u", 1U << ic->log2_buffer_sectors);
3017 if (ic->mode == 'J') {
3018 DMEMIT(" journal_watermark:%u", (unsigned)watermark_percentage);
3019 DMEMIT(" commit_time:%u", ic->autocommit_msec);
3021 if (ic->mode == 'B') {
3022 DMEMIT(" sectors_per_bit:%llu", (unsigned long long)ic->sectors_per_block << ic->log2_blocks_per_bitmap_bit);
3023 DMEMIT(" bitmap_flush_interval:%u", jiffies_to_msecs(ic->bitmap_flush_interval));
3025 if (ic->legacy_recalculate)
3026 DMEMIT(" legacy_recalculate");
3028 #define EMIT_ALG(a, n) \
3030 if (ic->a.alg_string) { \
3031 DMEMIT(" %s:%s", n, ic->a.alg_string); \
3032 if (ic->a.key_string) \
3033 DMEMIT(":%s", ic->a.key_string);\
3036 EMIT_ALG(internal_hash_alg, "internal_hash");
3037 EMIT_ALG(journal_crypt_alg, "journal_crypt");
3038 EMIT_ALG(journal_mac_alg, "journal_mac");
3044 static int dm_integrity_iterate_devices(struct dm_target *ti,
3045 iterate_devices_callout_fn fn, void *data)
3047 struct dm_integrity_c *ic = ti->private;
3050 return fn(ti, ic->dev, ic->start + ic->initial_sectors + ic->metadata_run, ti->len, data);
3052 return fn(ti, ic->dev, 0, ti->len, data);
3055 static void dm_integrity_io_hints(struct dm_target *ti, struct queue_limits *limits)
3057 struct dm_integrity_c *ic = ti->private;
3059 if (ic->sectors_per_block > 1) {
3060 limits->logical_block_size = ic->sectors_per_block << SECTOR_SHIFT;
3061 limits->physical_block_size = ic->sectors_per_block << SECTOR_SHIFT;
3062 blk_limits_io_min(limits, ic->sectors_per_block << SECTOR_SHIFT);
3066 static void calculate_journal_section_size(struct dm_integrity_c *ic)
3068 unsigned sector_space = JOURNAL_SECTOR_DATA;
3070 ic->journal_sections = le32_to_cpu(ic->sb->journal_sections);
3071 ic->journal_entry_size = roundup(offsetof(struct journal_entry, last_bytes[ic->sectors_per_block]) + ic->tag_size,
3072 JOURNAL_ENTRY_ROUNDUP);
3074 if (ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC))
3075 sector_space -= JOURNAL_MAC_PER_SECTOR;
3076 ic->journal_entries_per_sector = sector_space / ic->journal_entry_size;
3077 ic->journal_section_entries = ic->journal_entries_per_sector * JOURNAL_BLOCK_SECTORS;
3078 ic->journal_section_sectors = (ic->journal_section_entries << ic->sb->log2_sectors_per_block) + JOURNAL_BLOCK_SECTORS;
3079 ic->journal_entries = ic->journal_section_entries * ic->journal_sections;
3082 static int calculate_device_limits(struct dm_integrity_c *ic)
3084 __u64 initial_sectors;
3086 calculate_journal_section_size(ic);
3087 initial_sectors = SB_SECTORS + (__u64)ic->journal_section_sectors * ic->journal_sections;
3088 if (initial_sectors + METADATA_PADDING_SECTORS >= ic->meta_device_sectors || initial_sectors > UINT_MAX)
3090 ic->initial_sectors = initial_sectors;
3092 if (!ic->meta_dev) {
3093 sector_t last_sector, last_area, last_offset;
3095 ic->metadata_run = roundup((__u64)ic->tag_size << (ic->sb->log2_interleave_sectors - ic->sb->log2_sectors_per_block),
3096 (__u64)(1 << SECTOR_SHIFT << METADATA_PADDING_SECTORS)) >> SECTOR_SHIFT;
3097 if (!(ic->metadata_run & (ic->metadata_run - 1)))
3098 ic->log2_metadata_run = __ffs(ic->metadata_run);
3100 ic->log2_metadata_run = -1;
3102 get_area_and_offset(ic, ic->provided_data_sectors - 1, &last_area, &last_offset);
3103 last_sector = get_data_sector(ic, last_area, last_offset);
3104 if (last_sector < ic->start || last_sector >= ic->meta_device_sectors)
3107 __u64 meta_size = (ic->provided_data_sectors >> ic->sb->log2_sectors_per_block) * ic->tag_size;
3108 meta_size = (meta_size + ((1U << (ic->log2_buffer_sectors + SECTOR_SHIFT)) - 1))
3109 >> (ic->log2_buffer_sectors + SECTOR_SHIFT);
3110 meta_size <<= ic->log2_buffer_sectors;
3111 if (ic->initial_sectors + meta_size < ic->initial_sectors ||
3112 ic->initial_sectors + meta_size > ic->meta_device_sectors)
3114 ic->metadata_run = 1;
3115 ic->log2_metadata_run = 0;
3121 static int initialize_superblock(struct dm_integrity_c *ic, unsigned journal_sectors, unsigned interleave_sectors)
3123 unsigned journal_sections;
3126 memset(ic->sb, 0, SB_SECTORS << SECTOR_SHIFT);
3127 memcpy(ic->sb->magic, SB_MAGIC, 8);
3128 ic->sb->integrity_tag_size = cpu_to_le16(ic->tag_size);
3129 ic->sb->log2_sectors_per_block = __ffs(ic->sectors_per_block);
3130 if (ic->journal_mac_alg.alg_string)
3131 ic->sb->flags |= cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC);
3133 calculate_journal_section_size(ic);
3134 journal_sections = journal_sectors / ic->journal_section_sectors;
3135 if (!journal_sections)
3136 journal_sections = 1;
3138 if (!ic->meta_dev) {
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 ic->provided_data_sectors = 0;
3147 for (test_bit = fls64(ic->meta_device_sectors) - 1; test_bit >= 3; test_bit--) {
3148 __u64 prev_data_sectors = ic->provided_data_sectors;
3150 ic->provided_data_sectors |= (sector_t)1 << test_bit;
3151 if (calculate_device_limits(ic))
3152 ic->provided_data_sectors = prev_data_sectors;
3154 if (!ic->provided_data_sectors)
3157 ic->sb->log2_interleave_sectors = 0;
3158 ic->provided_data_sectors = ic->data_device_sectors;
3159 ic->provided_data_sectors &= ~(sector_t)(ic->sectors_per_block - 1);
3162 ic->sb->journal_sections = cpu_to_le32(0);
3163 for (test_bit = fls(journal_sections) - 1; test_bit >= 0; test_bit--) {
3164 __u32 prev_journal_sections = le32_to_cpu(ic->sb->journal_sections);
3165 __u32 test_journal_sections = prev_journal_sections | (1U << test_bit);
3166 if (test_journal_sections > journal_sections)
3168 ic->sb->journal_sections = cpu_to_le32(test_journal_sections);
3169 if (calculate_device_limits(ic))
3170 ic->sb->journal_sections = cpu_to_le32(prev_journal_sections);
3173 if (!le32_to_cpu(ic->sb->journal_sections)) {
3174 if (ic->log2_buffer_sectors > 3) {
3175 ic->log2_buffer_sectors--;
3176 goto try_smaller_buffer;
3182 ic->sb->provided_data_sectors = cpu_to_le64(ic->provided_data_sectors);
3189 static void dm_integrity_set(struct dm_target *ti, struct dm_integrity_c *ic)
3191 struct gendisk *disk = dm_disk(dm_table_get_md(ti->table));
3192 struct blk_integrity bi;
3194 memset(&bi, 0, sizeof(bi));
3195 bi.profile = &dm_integrity_profile;
3196 bi.tuple_size = ic->tag_size;
3197 bi.tag_size = bi.tuple_size;
3198 bi.interval_exp = ic->sb->log2_sectors_per_block + SECTOR_SHIFT;
3200 blk_integrity_register(disk, &bi);
3201 blk_queue_max_integrity_segments(disk->queue, UINT_MAX);
3204 static void dm_integrity_free_page_list(struct page_list *pl)
3210 for (i = 0; pl[i].page; i++)
3211 __free_page(pl[i].page);
3215 static struct page_list *dm_integrity_alloc_page_list(unsigned n_pages)
3217 struct page_list *pl;
3220 pl = kvmalloc_array(n_pages + 1, sizeof(struct page_list), GFP_KERNEL | __GFP_ZERO);
3224 for (i = 0; i < n_pages; i++) {
3225 pl[i].page = alloc_page(GFP_KERNEL);
3227 dm_integrity_free_page_list(pl);
3231 pl[i - 1].next = &pl[i];
3239 static void dm_integrity_free_journal_scatterlist(struct dm_integrity_c *ic, struct scatterlist **sl)
3242 for (i = 0; i < ic->journal_sections; i++)
3247 static struct scatterlist **dm_integrity_alloc_journal_scatterlist(struct dm_integrity_c *ic,
3248 struct page_list *pl)
3250 struct scatterlist **sl;
3253 sl = kvmalloc_array(ic->journal_sections,
3254 sizeof(struct scatterlist *),
3255 GFP_KERNEL | __GFP_ZERO);
3259 for (i = 0; i < ic->journal_sections; i++) {
3260 struct scatterlist *s;
3261 unsigned start_index, start_offset;
3262 unsigned end_index, end_offset;
3266 page_list_location(ic, i, 0, &start_index, &start_offset);
3267 page_list_location(ic, i, ic->journal_section_sectors - 1,
3268 &end_index, &end_offset);
3270 n_pages = (end_index - start_index + 1);
3272 s = kvmalloc_array(n_pages, sizeof(struct scatterlist),
3275 dm_integrity_free_journal_scatterlist(ic, sl);
3279 sg_init_table(s, n_pages);
3280 for (idx = start_index; idx <= end_index; idx++) {
3281 char *va = lowmem_page_address(pl[idx].page);
3282 unsigned start = 0, end = PAGE_SIZE;
3283 if (idx == start_index)
3284 start = start_offset;
3285 if (idx == end_index)
3286 end = end_offset + (1 << SECTOR_SHIFT);
3287 sg_set_buf(&s[idx - start_index], va + start, end - start);
3296 static void free_alg(struct alg_spec *a)
3298 kzfree(a->alg_string);
3300 memset(a, 0, sizeof *a);
3303 static int get_alg_and_key(const char *arg, struct alg_spec *a, char **error, char *error_inval)
3309 a->alg_string = kstrdup(strchr(arg, ':') + 1, GFP_KERNEL);
3313 k = strchr(a->alg_string, ':');
3316 a->key_string = k + 1;
3317 if (strlen(a->key_string) & 1)
3320 a->key_size = strlen(a->key_string) / 2;
3321 a->key = kmalloc(a->key_size, GFP_KERNEL);
3324 if (hex2bin(a->key, a->key_string, a->key_size))
3330 *error = error_inval;
3333 *error = "Out of memory for an argument";
3337 static int get_mac(struct crypto_shash **hash, struct alg_spec *a, char **error,
3338 char *error_alg, char *error_key)
3342 if (a->alg_string) {
3343 *hash = crypto_alloc_shash(a->alg_string, 0, 0);
3344 if (IS_ERR(*hash)) {
3352 r = crypto_shash_setkey(*hash, a->key, a->key_size);
3357 } else if (crypto_shash_get_flags(*hash) & CRYPTO_TFM_NEED_KEY) {
3366 static int create_journal(struct dm_integrity_c *ic, char **error)
3370 __u64 journal_pages, journal_desc_size, journal_tree_size;
3371 unsigned char *crypt_data = NULL, *crypt_iv = NULL;
3372 struct skcipher_request *req = NULL;
3374 ic->commit_ids[0] = cpu_to_le64(0x1111111111111111ULL);
3375 ic->commit_ids[1] = cpu_to_le64(0x2222222222222222ULL);
3376 ic->commit_ids[2] = cpu_to_le64(0x3333333333333333ULL);
3377 ic->commit_ids[3] = cpu_to_le64(0x4444444444444444ULL);
3379 journal_pages = roundup((__u64)ic->journal_sections * ic->journal_section_sectors,
3380 PAGE_SIZE >> SECTOR_SHIFT) >> (PAGE_SHIFT - SECTOR_SHIFT);
3381 journal_desc_size = journal_pages * sizeof(struct page_list);
3382 if (journal_pages >= totalram_pages() - totalhigh_pages() || journal_desc_size > ULONG_MAX) {
3383 *error = "Journal doesn't fit into memory";
3387 ic->journal_pages = journal_pages;
3389 ic->journal = dm_integrity_alloc_page_list(ic->journal_pages);
3391 *error = "Could not allocate memory for journal";
3395 if (ic->journal_crypt_alg.alg_string) {
3396 unsigned ivsize, blocksize;
3397 struct journal_completion comp;
3400 ic->journal_crypt = crypto_alloc_skcipher(ic->journal_crypt_alg.alg_string, 0, 0);
3401 if (IS_ERR(ic->journal_crypt)) {
3402 *error = "Invalid journal cipher";
3403 r = PTR_ERR(ic->journal_crypt);
3404 ic->journal_crypt = NULL;
3407 ivsize = crypto_skcipher_ivsize(ic->journal_crypt);
3408 blocksize = crypto_skcipher_blocksize(ic->journal_crypt);
3410 if (ic->journal_crypt_alg.key) {
3411 r = crypto_skcipher_setkey(ic->journal_crypt, ic->journal_crypt_alg.key,
3412 ic->journal_crypt_alg.key_size);
3414 *error = "Error setting encryption key";
3418 DEBUG_print("cipher %s, block size %u iv size %u\n",
3419 ic->journal_crypt_alg.alg_string, blocksize, ivsize);
3421 ic->journal_io = dm_integrity_alloc_page_list(ic->journal_pages);
3422 if (!ic->journal_io) {
3423 *error = "Could not allocate memory for journal io";
3428 if (blocksize == 1) {
3429 struct scatterlist *sg;
3431 req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
3433 *error = "Could not allocate crypt request";
3438 crypt_iv = kzalloc(ivsize, GFP_KERNEL);
3440 *error = "Could not allocate iv";
3445 ic->journal_xor = dm_integrity_alloc_page_list(ic->journal_pages);
3446 if (!ic->journal_xor) {
3447 *error = "Could not allocate memory for journal xor";
3452 sg = kvmalloc_array(ic->journal_pages + 1,
3453 sizeof(struct scatterlist),
3456 *error = "Unable to allocate sg list";
3460 sg_init_table(sg, ic->journal_pages + 1);
3461 for (i = 0; i < ic->journal_pages; i++) {
3462 char *va = lowmem_page_address(ic->journal_xor[i].page);
3464 sg_set_buf(&sg[i], va, PAGE_SIZE);
3466 sg_set_buf(&sg[i], &ic->commit_ids, sizeof ic->commit_ids);
3468 skcipher_request_set_crypt(req, sg, sg,
3469 PAGE_SIZE * ic->journal_pages + sizeof ic->commit_ids, crypt_iv);
3470 init_completion(&comp.comp);
3471 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
3472 if (do_crypt(true, req, &comp))
3473 wait_for_completion(&comp.comp);
3475 r = dm_integrity_failed(ic);
3477 *error = "Unable to encrypt journal";
3480 DEBUG_bytes(lowmem_page_address(ic->journal_xor[0].page), 64, "xor data");
3482 crypto_free_skcipher(ic->journal_crypt);
3483 ic->journal_crypt = NULL;
3485 unsigned crypt_len = roundup(ivsize, blocksize);
3487 req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
3489 *error = "Could not allocate crypt request";
3494 crypt_iv = kmalloc(ivsize, GFP_KERNEL);
3496 *error = "Could not allocate iv";
3501 crypt_data = kmalloc(crypt_len, GFP_KERNEL);
3503 *error = "Unable to allocate crypt data";
3508 ic->journal_scatterlist = dm_integrity_alloc_journal_scatterlist(ic, ic->journal);
3509 if (!ic->journal_scatterlist) {
3510 *error = "Unable to allocate sg list";
3514 ic->journal_io_scatterlist = dm_integrity_alloc_journal_scatterlist(ic, ic->journal_io);
3515 if (!ic->journal_io_scatterlist) {
3516 *error = "Unable to allocate sg list";
3520 ic->sk_requests = kvmalloc_array(ic->journal_sections,
3521 sizeof(struct skcipher_request *),
3522 GFP_KERNEL | __GFP_ZERO);
3523 if (!ic->sk_requests) {
3524 *error = "Unable to allocate sk requests";
3528 for (i = 0; i < ic->journal_sections; i++) {
3529 struct scatterlist sg;
3530 struct skcipher_request *section_req;
3531 __u32 section_le = cpu_to_le32(i);
3533 memset(crypt_iv, 0x00, ivsize);
3534 memset(crypt_data, 0x00, crypt_len);
3535 memcpy(crypt_data, §ion_le, min((size_t)crypt_len, sizeof(section_le)));
3537 sg_init_one(&sg, crypt_data, crypt_len);
3538 skcipher_request_set_crypt(req, &sg, &sg, crypt_len, crypt_iv);
3539 init_completion(&comp.comp);
3540 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
3541 if (do_crypt(true, req, &comp))
3542 wait_for_completion(&comp.comp);
3544 r = dm_integrity_failed(ic);
3546 *error = "Unable to generate iv";
3550 section_req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
3552 *error = "Unable to allocate crypt request";
3556 section_req->iv = kmalloc_array(ivsize, 2,
3558 if (!section_req->iv) {
3559 skcipher_request_free(section_req);
3560 *error = "Unable to allocate iv";
3564 memcpy(section_req->iv + ivsize, crypt_data, ivsize);
3565 section_req->cryptlen = (size_t)ic->journal_section_sectors << SECTOR_SHIFT;
3566 ic->sk_requests[i] = section_req;
3567 DEBUG_bytes(crypt_data, ivsize, "iv(%u)", i);
3572 for (i = 0; i < N_COMMIT_IDS; i++) {
3575 for (j = 0; j < i; j++) {
3576 if (ic->commit_ids[j] == ic->commit_ids[i]) {
3577 ic->commit_ids[i] = cpu_to_le64(le64_to_cpu(ic->commit_ids[i]) + 1);
3578 goto retest_commit_id;
3581 DEBUG_print("commit id %u: %016llx\n", i, ic->commit_ids[i]);
3584 journal_tree_size = (__u64)ic->journal_entries * sizeof(struct journal_node);
3585 if (journal_tree_size > ULONG_MAX) {
3586 *error = "Journal doesn't fit into memory";
3590 ic->journal_tree = kvmalloc(journal_tree_size, GFP_KERNEL);
3591 if (!ic->journal_tree) {
3592 *error = "Could not allocate memory for journal tree";
3598 skcipher_request_free(req);
3604 * Construct a integrity mapping
3608 * offset from the start of the device
3610 * D - direct writes, J - journal writes, B - bitmap mode, R - recovery mode
3611 * number of optional arguments
3612 * optional arguments:
3614 * interleave_sectors
3621 * bitmap_flush_interval
3627 static int dm_integrity_ctr(struct dm_target *ti, unsigned argc, char **argv)
3629 struct dm_integrity_c *ic;
3632 unsigned extra_args;
3633 struct dm_arg_set as;
3634 static const struct dm_arg _args[] = {
3635 {0, 14, "Invalid number of feature args"},
3637 unsigned journal_sectors, interleave_sectors, buffer_sectors, journal_watermark, sync_msec;
3638 bool should_write_sb;
3640 unsigned long long start;
3641 __s8 log2_sectors_per_bitmap_bit = -1;
3642 __s8 log2_blocks_per_bitmap_bit;
3643 __u64 bits_in_journal;
3644 __u64 n_bitmap_bits;
3646 #define DIRECT_ARGUMENTS 4
3648 if (argc <= DIRECT_ARGUMENTS) {
3649 ti->error = "Invalid argument count";
3653 ic = kzalloc(sizeof(struct dm_integrity_c), GFP_KERNEL);
3655 ti->error = "Cannot allocate integrity context";
3659 ti->per_io_data_size = sizeof(struct dm_integrity_io);
3662 ic->in_progress = RB_ROOT;
3663 INIT_LIST_HEAD(&ic->wait_list);
3664 init_waitqueue_head(&ic->endio_wait);
3665 bio_list_init(&ic->flush_bio_list);
3666 init_waitqueue_head(&ic->copy_to_journal_wait);
3667 init_completion(&ic->crypto_backoff);
3668 atomic64_set(&ic->number_of_mismatches, 0);
3669 ic->bitmap_flush_interval = BITMAP_FLUSH_INTERVAL;
3671 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &ic->dev);
3673 ti->error = "Device lookup failed";
3677 if (sscanf(argv[1], "%llu%c", &start, &dummy) != 1 || start != (sector_t)start) {
3678 ti->error = "Invalid starting offset";
3684 if (strcmp(argv[2], "-")) {
3685 if (sscanf(argv[2], "%u%c", &ic->tag_size, &dummy) != 1 || !ic->tag_size) {
3686 ti->error = "Invalid tag size";
3692 if (!strcmp(argv[3], "J") || !strcmp(argv[3], "B") ||
3693 !strcmp(argv[3], "D") || !strcmp(argv[3], "R")) {
3694 ic->mode = argv[3][0];
3696 ti->error = "Invalid mode (expecting J, B, D, R)";
3701 journal_sectors = 0;
3702 interleave_sectors = DEFAULT_INTERLEAVE_SECTORS;
3703 buffer_sectors = DEFAULT_BUFFER_SECTORS;
3704 journal_watermark = DEFAULT_JOURNAL_WATERMARK;
3705 sync_msec = DEFAULT_SYNC_MSEC;
3706 ic->sectors_per_block = 1;
3708 as.argc = argc - DIRECT_ARGUMENTS;
3709 as.argv = argv + DIRECT_ARGUMENTS;
3710 r = dm_read_arg_group(_args, &as, &extra_args, &ti->error);
3714 while (extra_args--) {
3715 const char *opt_string;
3717 unsigned long long llval;
3718 opt_string = dm_shift_arg(&as);
3721 ti->error = "Not enough feature arguments";
3724 if (sscanf(opt_string, "journal_sectors:%u%c", &val, &dummy) == 1)
3725 journal_sectors = val ? val : 1;
3726 else if (sscanf(opt_string, "interleave_sectors:%u%c", &val, &dummy) == 1)
3727 interleave_sectors = val;
3728 else if (sscanf(opt_string, "buffer_sectors:%u%c", &val, &dummy) == 1)
3729 buffer_sectors = val;
3730 else if (sscanf(opt_string, "journal_watermark:%u%c", &val, &dummy) == 1 && val <= 100)
3731 journal_watermark = val;
3732 else if (sscanf(opt_string, "commit_time:%u%c", &val, &dummy) == 1)
3734 else if (!strncmp(opt_string, "meta_device:", strlen("meta_device:"))) {
3736 dm_put_device(ti, ic->meta_dev);
3737 ic->meta_dev = NULL;
3739 r = dm_get_device(ti, strchr(opt_string, ':') + 1,
3740 dm_table_get_mode(ti->table), &ic->meta_dev);
3742 ti->error = "Device lookup failed";
3745 } else if (sscanf(opt_string, "block_size:%u%c", &val, &dummy) == 1) {
3746 if (val < 1 << SECTOR_SHIFT ||
3747 val > MAX_SECTORS_PER_BLOCK << SECTOR_SHIFT ||
3750 ti->error = "Invalid block_size argument";
3753 ic->sectors_per_block = val >> SECTOR_SHIFT;
3754 } else if (sscanf(opt_string, "sectors_per_bit:%llu%c", &llval, &dummy) == 1) {
3755 log2_sectors_per_bitmap_bit = !llval ? 0 : __ilog2_u64(llval);
3756 } else if (sscanf(opt_string, "bitmap_flush_interval:%u%c", &val, &dummy) == 1) {
3757 if (val >= (uint64_t)UINT_MAX * 1000 / HZ) {
3759 ti->error = "Invalid bitmap_flush_interval argument";
3762 ic->bitmap_flush_interval = msecs_to_jiffies(val);
3763 } else if (!strncmp(opt_string, "internal_hash:", strlen("internal_hash:"))) {
3764 r = get_alg_and_key(opt_string, &ic->internal_hash_alg, &ti->error,
3765 "Invalid internal_hash argument");
3768 } else if (!strncmp(opt_string, "journal_crypt:", strlen("journal_crypt:"))) {
3769 r = get_alg_and_key(opt_string, &ic->journal_crypt_alg, &ti->error,
3770 "Invalid journal_crypt argument");
3773 } else if (!strncmp(opt_string, "journal_mac:", strlen("journal_mac:"))) {
3774 r = get_alg_and_key(opt_string, &ic->journal_mac_alg, &ti->error,
3775 "Invalid journal_mac argument");
3778 } else if (!strcmp(opt_string, "recalculate")) {
3779 ic->recalculate_flag = true;
3780 } else if (!strcmp(opt_string, "legacy_recalculate")) {
3781 ic->legacy_recalculate = true;
3784 ti->error = "Invalid argument";
3789 ic->data_device_sectors = i_size_read(ic->dev->bdev->bd_inode) >> SECTOR_SHIFT;
3791 ic->meta_device_sectors = ic->data_device_sectors;
3793 ic->meta_device_sectors = i_size_read(ic->meta_dev->bdev->bd_inode) >> SECTOR_SHIFT;
3795 if (!journal_sectors) {
3796 journal_sectors = min((sector_t)DEFAULT_MAX_JOURNAL_SECTORS,
3797 ic->data_device_sectors >> DEFAULT_JOURNAL_SIZE_FACTOR);
3800 if (!buffer_sectors)
3802 ic->log2_buffer_sectors = min((int)__fls(buffer_sectors), 31 - SECTOR_SHIFT);
3804 r = get_mac(&ic->internal_hash, &ic->internal_hash_alg, &ti->error,
3805 "Invalid internal hash", "Error setting internal hash key");
3809 r = get_mac(&ic->journal_mac, &ic->journal_mac_alg, &ti->error,
3810 "Invalid journal mac", "Error setting journal mac key");
3814 if (!ic->tag_size) {
3815 if (!ic->internal_hash) {
3816 ti->error = "Unknown tag size";
3820 ic->tag_size = crypto_shash_digestsize(ic->internal_hash);
3822 if (ic->tag_size > MAX_TAG_SIZE) {
3823 ti->error = "Too big tag size";
3827 if (!(ic->tag_size & (ic->tag_size - 1)))
3828 ic->log2_tag_size = __ffs(ic->tag_size);
3830 ic->log2_tag_size = -1;
3832 if (ic->mode == 'B' && !ic->internal_hash) {
3834 ti->error = "Bitmap mode can be only used with internal hash";
3838 ic->autocommit_jiffies = msecs_to_jiffies(sync_msec);
3839 ic->autocommit_msec = sync_msec;
3840 timer_setup(&ic->autocommit_timer, autocommit_fn, 0);
3842 ic->io = dm_io_client_create();
3843 if (IS_ERR(ic->io)) {
3844 r = PTR_ERR(ic->io);
3846 ti->error = "Cannot allocate dm io";
3850 r = mempool_init_slab_pool(&ic->journal_io_mempool, JOURNAL_IO_MEMPOOL, journal_io_cache);
3852 ti->error = "Cannot allocate mempool";
3856 ic->metadata_wq = alloc_workqueue("dm-integrity-metadata",
3857 WQ_MEM_RECLAIM, METADATA_WORKQUEUE_MAX_ACTIVE);
3858 if (!ic->metadata_wq) {
3859 ti->error = "Cannot allocate workqueue";
3865 * If this workqueue were percpu, it would cause bio reordering
3866 * and reduced performance.
3868 ic->wait_wq = alloc_workqueue("dm-integrity-wait", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
3870 ti->error = "Cannot allocate workqueue";
3875 ic->offload_wq = alloc_workqueue("dm-integrity-offload", WQ_MEM_RECLAIM,
3876 METADATA_WORKQUEUE_MAX_ACTIVE);
3877 if (!ic->offload_wq) {
3878 ti->error = "Cannot allocate workqueue";
3883 ic->commit_wq = alloc_workqueue("dm-integrity-commit", WQ_MEM_RECLAIM, 1);
3884 if (!ic->commit_wq) {
3885 ti->error = "Cannot allocate workqueue";
3889 INIT_WORK(&ic->commit_work, integrity_commit);
3891 if (ic->mode == 'J' || ic->mode == 'B') {
3892 ic->writer_wq = alloc_workqueue("dm-integrity-writer", WQ_MEM_RECLAIM, 1);
3893 if (!ic->writer_wq) {
3894 ti->error = "Cannot allocate workqueue";
3898 INIT_WORK(&ic->writer_work, integrity_writer);
3901 ic->sb = alloc_pages_exact(SB_SECTORS << SECTOR_SHIFT, GFP_KERNEL);
3904 ti->error = "Cannot allocate superblock area";
3908 r = sync_rw_sb(ic, REQ_OP_READ, 0);
3910 ti->error = "Error reading superblock";
3913 should_write_sb = false;
3914 if (memcmp(ic->sb->magic, SB_MAGIC, 8)) {
3915 if (ic->mode != 'R') {
3916 if (memchr_inv(ic->sb, 0, SB_SECTORS << SECTOR_SHIFT)) {
3918 ti->error = "The device is not initialized";
3923 r = initialize_superblock(ic, journal_sectors, interleave_sectors);
3925 ti->error = "Could not initialize superblock";
3928 if (ic->mode != 'R')
3929 should_write_sb = true;
3932 if (!ic->sb->version || ic->sb->version > SB_VERSION_3) {
3934 ti->error = "Unknown version";
3937 if (le16_to_cpu(ic->sb->integrity_tag_size) != ic->tag_size) {
3939 ti->error = "Tag size doesn't match the information in superblock";
3942 if (ic->sb->log2_sectors_per_block != __ffs(ic->sectors_per_block)) {
3944 ti->error = "Block size doesn't match the information in superblock";
3947 if (!le32_to_cpu(ic->sb->journal_sections)) {
3949 ti->error = "Corrupted superblock, journal_sections is 0";
3952 /* make sure that ti->max_io_len doesn't overflow */
3953 if (!ic->meta_dev) {
3954 if (ic->sb->log2_interleave_sectors < MIN_LOG2_INTERLEAVE_SECTORS ||
3955 ic->sb->log2_interleave_sectors > MAX_LOG2_INTERLEAVE_SECTORS) {
3957 ti->error = "Invalid interleave_sectors in the superblock";
3961 if (ic->sb->log2_interleave_sectors) {
3963 ti->error = "Invalid interleave_sectors in the superblock";
3967 ic->provided_data_sectors = le64_to_cpu(ic->sb->provided_data_sectors);
3968 if (ic->provided_data_sectors != le64_to_cpu(ic->sb->provided_data_sectors)) {
3969 /* test for overflow */
3971 ti->error = "The superblock has 64-bit device size, but the kernel was compiled with 32-bit sectors";
3974 if (!!(ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC)) != !!ic->journal_mac_alg.alg_string) {
3976 ti->error = "Journal mac mismatch";
3981 r = calculate_device_limits(ic);
3984 if (ic->log2_buffer_sectors > 3) {
3985 ic->log2_buffer_sectors--;
3986 goto try_smaller_buffer;
3989 ti->error = "The device is too small";
3993 if (log2_sectors_per_bitmap_bit < 0)
3994 log2_sectors_per_bitmap_bit = __fls(DEFAULT_SECTORS_PER_BITMAP_BIT);
3995 if (log2_sectors_per_bitmap_bit < ic->sb->log2_sectors_per_block)
3996 log2_sectors_per_bitmap_bit = ic->sb->log2_sectors_per_block;
3998 bits_in_journal = ((__u64)ic->journal_section_sectors * ic->journal_sections) << (SECTOR_SHIFT + 3);
3999 if (bits_in_journal > UINT_MAX)
4000 bits_in_journal = UINT_MAX;
4001 while (bits_in_journal < (ic->provided_data_sectors + ((sector_t)1 << log2_sectors_per_bitmap_bit) - 1) >> log2_sectors_per_bitmap_bit)
4002 log2_sectors_per_bitmap_bit++;
4004 log2_blocks_per_bitmap_bit = log2_sectors_per_bitmap_bit - ic->sb->log2_sectors_per_block;
4005 ic->log2_blocks_per_bitmap_bit = log2_blocks_per_bitmap_bit;
4006 if (should_write_sb) {
4007 ic->sb->log2_blocks_per_bitmap_bit = log2_blocks_per_bitmap_bit;
4009 n_bitmap_bits = ((ic->provided_data_sectors >> ic->sb->log2_sectors_per_block)
4010 + (((sector_t)1 << log2_blocks_per_bitmap_bit) - 1)) >> log2_blocks_per_bitmap_bit;
4011 ic->n_bitmap_blocks = DIV_ROUND_UP(n_bitmap_bits, BITMAP_BLOCK_SIZE * 8);
4014 ic->log2_buffer_sectors = min(ic->log2_buffer_sectors, (__u8)__ffs(ic->metadata_run));
4016 if (ti->len > ic->provided_data_sectors) {
4018 ti->error = "Not enough provided sectors for requested mapping size";
4023 threshold = (__u64)ic->journal_entries * (100 - journal_watermark);
4025 do_div(threshold, 100);
4026 ic->free_sectors_threshold = threshold;
4028 DEBUG_print("initialized:\n");
4029 DEBUG_print(" integrity_tag_size %u\n", le16_to_cpu(ic->sb->integrity_tag_size));
4030 DEBUG_print(" journal_entry_size %u\n", ic->journal_entry_size);
4031 DEBUG_print(" journal_entries_per_sector %u\n", ic->journal_entries_per_sector);
4032 DEBUG_print(" journal_section_entries %u\n", ic->journal_section_entries);
4033 DEBUG_print(" journal_section_sectors %u\n", ic->journal_section_sectors);
4034 DEBUG_print(" journal_sections %u\n", (unsigned)le32_to_cpu(ic->sb->journal_sections));
4035 DEBUG_print(" journal_entries %u\n", ic->journal_entries);
4036 DEBUG_print(" log2_interleave_sectors %d\n", ic->sb->log2_interleave_sectors);
4037 DEBUG_print(" data_device_sectors 0x%llx\n", i_size_read(ic->dev->bdev->bd_inode) >> SECTOR_SHIFT);
4038 DEBUG_print(" initial_sectors 0x%x\n", ic->initial_sectors);
4039 DEBUG_print(" metadata_run 0x%x\n", ic->metadata_run);
4040 DEBUG_print(" log2_metadata_run %d\n", ic->log2_metadata_run);
4041 DEBUG_print(" provided_data_sectors 0x%llx (%llu)\n", (unsigned long long)ic->provided_data_sectors,
4042 (unsigned long long)ic->provided_data_sectors);
4043 DEBUG_print(" log2_buffer_sectors %u\n", ic->log2_buffer_sectors);
4044 DEBUG_print(" bits_in_journal %llu\n", (unsigned long long)bits_in_journal);
4046 if (ic->recalculate_flag && !(ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))) {
4047 ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
4048 ic->sb->recalc_sector = cpu_to_le64(0);
4051 if (ic->internal_hash) {
4052 size_t recalc_tags_size;
4053 ic->recalc_wq = alloc_workqueue("dm-integrity-recalc", WQ_MEM_RECLAIM, 1);
4054 if (!ic->recalc_wq ) {
4055 ti->error = "Cannot allocate workqueue";
4059 INIT_WORK(&ic->recalc_work, integrity_recalc);
4060 ic->recalc_buffer = vmalloc(RECALC_SECTORS << SECTOR_SHIFT);
4061 if (!ic->recalc_buffer) {
4062 ti->error = "Cannot allocate buffer for recalculating";
4066 recalc_tags_size = (RECALC_SECTORS >> ic->sb->log2_sectors_per_block) * ic->tag_size;
4067 if (crypto_shash_digestsize(ic->internal_hash) > ic->tag_size)
4068 recalc_tags_size += crypto_shash_digestsize(ic->internal_hash) - ic->tag_size;
4069 ic->recalc_tags = kvmalloc(recalc_tags_size, GFP_KERNEL);
4070 if (!ic->recalc_tags) {
4071 ti->error = "Cannot allocate tags for recalculating";
4076 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) {
4077 ti->error = "Recalculate can only be specified with internal_hash";
4083 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING) &&
4084 le64_to_cpu(ic->sb->recalc_sector) < ic->provided_data_sectors &&
4085 dm_integrity_disable_recalculate(ic)) {
4086 ti->error = "Recalculating with HMAC is disabled for security reasons - if you really need it, use the argument \"legacy_recalculate\"";
4091 ic->bufio = dm_bufio_client_create(ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev,
4092 1U << (SECTOR_SHIFT + ic->log2_buffer_sectors), 1, 0, NULL, NULL);
4093 if (IS_ERR(ic->bufio)) {
4094 r = PTR_ERR(ic->bufio);
4095 ti->error = "Cannot initialize dm-bufio";
4099 dm_bufio_set_sector_offset(ic->bufio, ic->start + ic->initial_sectors);
4101 if (ic->mode != 'R') {
4102 r = create_journal(ic, &ti->error);
4108 if (ic->mode == 'B') {
4110 unsigned n_bitmap_pages = DIV_ROUND_UP(ic->n_bitmap_blocks, PAGE_SIZE / BITMAP_BLOCK_SIZE);
4112 ic->recalc_bitmap = dm_integrity_alloc_page_list(n_bitmap_pages);
4113 if (!ic->recalc_bitmap) {
4117 ic->may_write_bitmap = dm_integrity_alloc_page_list(n_bitmap_pages);
4118 if (!ic->may_write_bitmap) {
4122 ic->bbs = kvmalloc_array(ic->n_bitmap_blocks, sizeof(struct bitmap_block_status), GFP_KERNEL);
4127 INIT_DELAYED_WORK(&ic->bitmap_flush_work, bitmap_flush_work);
4128 for (i = 0; i < ic->n_bitmap_blocks; i++) {
4129 struct bitmap_block_status *bbs = &ic->bbs[i];
4130 unsigned sector, pl_index, pl_offset;
4132 INIT_WORK(&bbs->work, bitmap_block_work);
4135 bio_list_init(&bbs->bio_queue);
4136 spin_lock_init(&bbs->bio_queue_lock);
4138 sector = i * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT);
4139 pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
4140 pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
4142 bbs->bitmap = lowmem_page_address(ic->journal[pl_index].page) + pl_offset;
4146 if (should_write_sb) {
4147 init_journal(ic, 0, ic->journal_sections, 0);
4148 r = dm_integrity_failed(ic);
4150 ti->error = "Error initializing journal";
4153 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
4155 ti->error = "Error initializing superblock";
4158 ic->just_formatted = true;
4161 if (!ic->meta_dev) {
4162 r = dm_set_target_max_io_len(ti, 1U << ic->sb->log2_interleave_sectors);
4166 if (ic->mode == 'B') {
4167 unsigned max_io_len = ((sector_t)ic->sectors_per_block << ic->log2_blocks_per_bitmap_bit) * (BITMAP_BLOCK_SIZE * 8);
4169 max_io_len = 1U << 31;
4170 DEBUG_print("max_io_len: old %u, new %u\n", ti->max_io_len, max_io_len);
4171 if (!ti->max_io_len || ti->max_io_len > max_io_len) {
4172 r = dm_set_target_max_io_len(ti, max_io_len);
4178 if (!ic->internal_hash)
4179 dm_integrity_set(ti, ic);
4181 ti->num_flush_bios = 1;
4182 ti->flush_supported = true;
4187 dm_integrity_dtr(ti);
4191 static void dm_integrity_dtr(struct dm_target *ti)
4193 struct dm_integrity_c *ic = ti->private;
4195 BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress));
4196 BUG_ON(!list_empty(&ic->wait_list));
4198 if (ic->mode == 'B')
4199 cancel_delayed_work_sync(&ic->bitmap_flush_work);
4200 if (ic->metadata_wq)
4201 destroy_workqueue(ic->metadata_wq);
4203 destroy_workqueue(ic->wait_wq);
4205 destroy_workqueue(ic->offload_wq);
4207 destroy_workqueue(ic->commit_wq);
4209 destroy_workqueue(ic->writer_wq);
4211 destroy_workqueue(ic->recalc_wq);
4212 vfree(ic->recalc_buffer);
4213 kvfree(ic->recalc_tags);
4216 dm_bufio_client_destroy(ic->bufio);
4217 mempool_exit(&ic->journal_io_mempool);
4219 dm_io_client_destroy(ic->io);
4221 dm_put_device(ti, ic->dev);
4223 dm_put_device(ti, ic->meta_dev);
4224 dm_integrity_free_page_list(ic->journal);
4225 dm_integrity_free_page_list(ic->journal_io);
4226 dm_integrity_free_page_list(ic->journal_xor);
4227 dm_integrity_free_page_list(ic->recalc_bitmap);
4228 dm_integrity_free_page_list(ic->may_write_bitmap);
4229 if (ic->journal_scatterlist)
4230 dm_integrity_free_journal_scatterlist(ic, ic->journal_scatterlist);
4231 if (ic->journal_io_scatterlist)
4232 dm_integrity_free_journal_scatterlist(ic, ic->journal_io_scatterlist);
4233 if (ic->sk_requests) {
4236 for (i = 0; i < ic->journal_sections; i++) {
4237 struct skcipher_request *req = ic->sk_requests[i];
4240 skcipher_request_free(req);
4243 kvfree(ic->sk_requests);
4245 kvfree(ic->journal_tree);
4247 free_pages_exact(ic->sb, SB_SECTORS << SECTOR_SHIFT);
4249 if (ic->internal_hash)
4250 crypto_free_shash(ic->internal_hash);
4251 free_alg(&ic->internal_hash_alg);
4253 if (ic->journal_crypt)
4254 crypto_free_skcipher(ic->journal_crypt);
4255 free_alg(&ic->journal_crypt_alg);
4257 if (ic->journal_mac)
4258 crypto_free_shash(ic->journal_mac);
4259 free_alg(&ic->journal_mac_alg);
4264 static struct target_type integrity_target = {
4265 .name = "integrity",
4266 .version = {1, 3, 0},
4267 .module = THIS_MODULE,
4268 .features = DM_TARGET_SINGLETON | DM_TARGET_INTEGRITY,
4269 .ctr = dm_integrity_ctr,
4270 .dtr = dm_integrity_dtr,
4271 .map = dm_integrity_map,
4272 .postsuspend = dm_integrity_postsuspend,
4273 .resume = dm_integrity_resume,
4274 .status = dm_integrity_status,
4275 .iterate_devices = dm_integrity_iterate_devices,
4276 .io_hints = dm_integrity_io_hints,
4279 static int __init dm_integrity_init(void)
4283 journal_io_cache = kmem_cache_create("integrity_journal_io",
4284 sizeof(struct journal_io), 0, 0, NULL);
4285 if (!journal_io_cache) {
4286 DMERR("can't allocate journal io cache");
4290 r = dm_register_target(&integrity_target);
4293 DMERR("register failed %d", r);
4298 static void __exit dm_integrity_exit(void)
4300 dm_unregister_target(&integrity_target);
4301 kmem_cache_destroy(journal_io_cache);
4304 module_init(dm_integrity_init);
4305 module_exit(dm_integrity_exit);
4307 MODULE_AUTHOR("Milan Broz");
4308 MODULE_AUTHOR("Mikulas Patocka");
4309 MODULE_DESCRIPTION(DM_NAME " target for integrity tags extension");
4310 MODULE_LICENSE("GPL");