2 * Copyright (C) 2016-2017 Red Hat, Inc. All rights reserved.
3 * Copyright (C) 2016-2017 Milan Broz
4 * Copyright (C) 2016-2017 Mikulas Patocka
6 * This file is released under the GPL.
9 #include <linux/module.h>
10 #include <linux/device-mapper.h>
11 #include <linux/dm-io.h>
12 #include <linux/vmalloc.h>
13 #include <linux/sort.h>
14 #include <linux/rbtree.h>
15 #include <linux/delay.h>
16 #include <linux/random.h>
17 #include <crypto/hash.h>
18 #include <crypto/skcipher.h>
19 #include <linux/async_tx.h>
22 #define DM_MSG_PREFIX "integrity"
24 #define DEFAULT_INTERLEAVE_SECTORS 32768
25 #define DEFAULT_JOURNAL_SIZE_FACTOR 7
26 #define DEFAULT_BUFFER_SECTORS 128
27 #define DEFAULT_JOURNAL_WATERMARK 50
28 #define DEFAULT_SYNC_MSEC 10000
29 #define DEFAULT_MAX_JOURNAL_SECTORS 131072
30 #define MIN_LOG2_INTERLEAVE_SECTORS 3
31 #define MAX_LOG2_INTERLEAVE_SECTORS 31
32 #define METADATA_WORKQUEUE_MAX_ACTIVE 16
35 * Warning - DEBUG_PRINT prints security-sensitive data to the log,
36 * so it should not be enabled in the official kernel
39 //#define INTERNAL_VERIFY
45 #define SB_MAGIC "integrt"
48 #define MAX_SECTORS_PER_BLOCK 8
53 __u8 log2_interleave_sectors
;
54 __u16 integrity_tag_size
;
55 __u32 journal_sections
;
56 __u64 provided_data_sectors
; /* userspace uses this value */
58 __u8 log2_sectors_per_block
;
61 #define SB_FLAG_HAVE_JOURNAL_MAC 0x1
63 #define JOURNAL_ENTRY_ROUNDUP 8
65 typedef __u64 commit_id_t
;
66 #define JOURNAL_MAC_PER_SECTOR 8
68 struct journal_entry
{
76 commit_id_t last_bytes
[0];
80 #define journal_entry_tag(ic, je) ((__u8 *)&(je)->last_bytes[(ic)->sectors_per_block])
82 #if BITS_PER_LONG == 64
83 #define journal_entry_set_sector(je, x) do { smp_wmb(); ACCESS_ONCE((je)->u.sector) = cpu_to_le64(x); } while (0)
84 #define journal_entry_get_sector(je) le64_to_cpu((je)->u.sector)
85 #elif defined(CONFIG_LBDAF)
86 #define journal_entry_set_sector(je, x) do { (je)->u.s.sector_lo = cpu_to_le32(x); smp_wmb(); ACCESS_ONCE((je)->u.s.sector_hi) = cpu_to_le32((x) >> 32); } while (0)
87 #define journal_entry_get_sector(je) le64_to_cpu((je)->u.sector)
89 #define journal_entry_set_sector(je, x) do { (je)->u.s.sector_lo = cpu_to_le32(x); smp_wmb(); ACCESS_ONCE((je)->u.s.sector_hi) = cpu_to_le32(0); } while (0)
90 #define journal_entry_get_sector(je) le32_to_cpu((je)->u.s.sector_lo)
92 #define journal_entry_is_unused(je) ((je)->u.s.sector_hi == cpu_to_le32(-1))
93 #define journal_entry_set_unused(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-1)); } while (0)
94 #define journal_entry_is_inprogress(je) ((je)->u.s.sector_hi == cpu_to_le32(-2))
95 #define journal_entry_set_inprogress(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-2)); } while (0)
97 #define JOURNAL_BLOCK_SECTORS 8
98 #define JOURNAL_SECTOR_DATA ((1 << SECTOR_SHIFT) - sizeof(commit_id_t))
99 #define JOURNAL_MAC_SIZE (JOURNAL_MAC_PER_SECTOR * JOURNAL_BLOCK_SECTORS)
101 struct journal_sector
{
102 __u8 entries
[JOURNAL_SECTOR_DATA
- JOURNAL_MAC_PER_SECTOR
];
103 __u8 mac
[JOURNAL_MAC_PER_SECTOR
];
104 commit_id_t commit_id
;
107 #define MAX_TAG_SIZE (JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR - offsetof(struct journal_entry, last_bytes[MAX_SECTORS_PER_BLOCK]))
109 #define METADATA_PADDING_SECTORS 8
111 #define N_COMMIT_IDS 4
113 static unsigned char prev_commit_seq(unsigned char seq
)
115 return (seq
+ N_COMMIT_IDS
- 1) % N_COMMIT_IDS
;
118 static unsigned char next_commit_seq(unsigned char seq
)
120 return (seq
+ 1) % N_COMMIT_IDS
;
124 * In-memory structures
127 struct journal_node
{
139 struct dm_integrity_c
{
144 mempool_t
*journal_io_mempool
;
145 struct dm_io_client
*io
;
146 struct dm_bufio_client
*bufio
;
147 struct workqueue_struct
*metadata_wq
;
148 struct superblock
*sb
;
149 unsigned journal_pages
;
150 struct page_list
*journal
;
151 struct page_list
*journal_io
;
152 struct page_list
*journal_xor
;
154 struct crypto_skcipher
*journal_crypt
;
155 struct scatterlist
**journal_scatterlist
;
156 struct scatterlist
**journal_io_scatterlist
;
157 struct skcipher_request
**sk_requests
;
159 struct crypto_shash
*journal_mac
;
161 struct journal_node
*journal_tree
;
162 struct rb_root journal_tree_root
;
164 sector_t provided_data_sectors
;
166 unsigned short journal_entry_size
;
167 unsigned char journal_entries_per_sector
;
168 unsigned char journal_section_entries
;
169 unsigned short journal_section_sectors
;
170 unsigned journal_sections
;
171 unsigned journal_entries
;
172 sector_t device_sectors
;
173 unsigned initial_sectors
;
174 unsigned metadata_run
;
175 __s8 log2_metadata_run
;
176 __u8 log2_buffer_sectors
;
177 __u8 sectors_per_block
;
184 struct crypto_shash
*internal_hash
;
186 /* these variables are locked with endio_wait.lock */
187 struct rb_root in_progress
;
188 wait_queue_head_t endio_wait
;
189 struct workqueue_struct
*wait_wq
;
191 unsigned char commit_seq
;
192 commit_id_t commit_ids
[N_COMMIT_IDS
];
194 unsigned committed_section
;
195 unsigned n_committed_sections
;
197 unsigned uncommitted_section
;
198 unsigned n_uncommitted_sections
;
200 unsigned free_section
;
201 unsigned char free_section_entry
;
202 unsigned free_sectors
;
204 unsigned free_sectors_threshold
;
206 struct workqueue_struct
*commit_wq
;
207 struct work_struct commit_work
;
209 struct workqueue_struct
*writer_wq
;
210 struct work_struct writer_work
;
212 struct bio_list flush_bio_list
;
214 unsigned long autocommit_jiffies
;
215 struct timer_list autocommit_timer
;
216 unsigned autocommit_msec
;
218 wait_queue_head_t copy_to_journal_wait
;
220 struct completion crypto_backoff
;
222 bool journal_uptodate
;
225 struct alg_spec internal_hash_alg
;
226 struct alg_spec journal_crypt_alg
;
227 struct alg_spec journal_mac_alg
;
229 atomic64_t number_of_mismatches
;
232 struct dm_integrity_range
{
233 sector_t logical_sector
;
238 struct dm_integrity_io
{
239 struct work_struct work
;
241 struct dm_integrity_c
*ic
;
245 struct dm_integrity_range range
;
247 sector_t metadata_block
;
248 unsigned metadata_offset
;
251 blk_status_t bi_status
;
253 struct completion
*completion
;
255 struct gendisk
*orig_bi_disk
;
257 bio_end_io_t
*orig_bi_end_io
;
258 struct bio_integrity_payload
*orig_bi_integrity
;
259 struct bvec_iter orig_bi_iter
;
262 struct journal_completion
{
263 struct dm_integrity_c
*ic
;
265 struct completion comp
;
269 struct dm_integrity_range range
;
270 struct journal_completion
*comp
;
273 static struct kmem_cache
*journal_io_cache
;
275 #define JOURNAL_IO_MEMPOOL 32
278 #define DEBUG_print(x, ...) printk(KERN_DEBUG x, ##__VA_ARGS__)
279 static void __DEBUG_bytes(__u8
*bytes
, size_t len
, const char *msg
, ...)
288 pr_cont(" %02x", *bytes
);
294 #define DEBUG_bytes(bytes, len, msg, ...) __DEBUG_bytes(bytes, len, KERN_DEBUG msg, ##__VA_ARGS__)
296 #define DEBUG_print(x, ...) do { } while (0)
297 #define DEBUG_bytes(bytes, len, msg, ...) do { } while (0)
301 * DM Integrity profile, protection is performed layer above (dm-crypt)
303 static const struct blk_integrity_profile dm_integrity_profile
= {
304 .name
= "DM-DIF-EXT-TAG",
309 static void dm_integrity_map_continue(struct dm_integrity_io
*dio
, bool from_map
);
310 static void integrity_bio_wait(struct work_struct
*w
);
311 static void dm_integrity_dtr(struct dm_target
*ti
);
313 static void dm_integrity_io_error(struct dm_integrity_c
*ic
, const char *msg
, int err
)
316 atomic64_inc(&ic
->number_of_mismatches
);
317 if (!cmpxchg(&ic
->failed
, 0, err
))
318 DMERR("Error on %s: %d", msg
, err
);
321 static int dm_integrity_failed(struct dm_integrity_c
*ic
)
323 return ACCESS_ONCE(ic
->failed
);
326 static commit_id_t
dm_integrity_commit_id(struct dm_integrity_c
*ic
, unsigned i
,
327 unsigned j
, unsigned char seq
)
330 * Xor the number with section and sector, so that if a piece of
331 * journal is written at wrong place, it is detected.
333 return ic
->commit_ids
[seq
] ^ cpu_to_le64(((__u64
)i
<< 32) ^ j
);
336 static void get_area_and_offset(struct dm_integrity_c
*ic
, sector_t data_sector
,
337 sector_t
*area
, sector_t
*offset
)
339 __u8 log2_interleave_sectors
= ic
->sb
->log2_interleave_sectors
;
341 *area
= data_sector
>> log2_interleave_sectors
;
342 *offset
= (unsigned)data_sector
& ((1U << log2_interleave_sectors
) - 1);
345 #define sector_to_block(ic, n) \
347 BUG_ON((n) & (unsigned)((ic)->sectors_per_block - 1)); \
348 (n) >>= (ic)->sb->log2_sectors_per_block; \
351 static __u64
get_metadata_sector_and_offset(struct dm_integrity_c
*ic
, sector_t area
,
352 sector_t offset
, unsigned *metadata_offset
)
357 ms
= area
<< ic
->sb
->log2_interleave_sectors
;
358 if (likely(ic
->log2_metadata_run
>= 0))
359 ms
+= area
<< ic
->log2_metadata_run
;
361 ms
+= area
* ic
->metadata_run
;
362 ms
>>= ic
->log2_buffer_sectors
;
364 sector_to_block(ic
, offset
);
366 if (likely(ic
->log2_tag_size
>= 0)) {
367 ms
+= offset
>> (SECTOR_SHIFT
+ ic
->log2_buffer_sectors
- ic
->log2_tag_size
);
368 mo
= (offset
<< ic
->log2_tag_size
) & ((1U << SECTOR_SHIFT
<< ic
->log2_buffer_sectors
) - 1);
370 ms
+= (__u64
)offset
* ic
->tag_size
>> (SECTOR_SHIFT
+ ic
->log2_buffer_sectors
);
371 mo
= (offset
* ic
->tag_size
) & ((1U << SECTOR_SHIFT
<< ic
->log2_buffer_sectors
) - 1);
373 *metadata_offset
= mo
;
377 static sector_t
get_data_sector(struct dm_integrity_c
*ic
, sector_t area
, sector_t offset
)
381 result
= area
<< ic
->sb
->log2_interleave_sectors
;
382 if (likely(ic
->log2_metadata_run
>= 0))
383 result
+= (area
+ 1) << ic
->log2_metadata_run
;
385 result
+= (area
+ 1) * ic
->metadata_run
;
387 result
+= (sector_t
)ic
->initial_sectors
+ offset
;
391 static void wraparound_section(struct dm_integrity_c
*ic
, unsigned *sec_ptr
)
393 if (unlikely(*sec_ptr
>= ic
->journal_sections
))
394 *sec_ptr
-= ic
->journal_sections
;
397 static int sync_rw_sb(struct dm_integrity_c
*ic
, int op
, int op_flags
)
399 struct dm_io_request io_req
;
400 struct dm_io_region io_loc
;
403 io_req
.bi_op_flags
= op_flags
;
404 io_req
.mem
.type
= DM_IO_KMEM
;
405 io_req
.mem
.ptr
.addr
= ic
->sb
;
406 io_req
.notify
.fn
= NULL
;
407 io_req
.client
= ic
->io
;
408 io_loc
.bdev
= ic
->dev
->bdev
;
409 io_loc
.sector
= ic
->start
;
410 io_loc
.count
= SB_SECTORS
;
412 return dm_io(&io_req
, 1, &io_loc
, NULL
);
415 static void access_journal_check(struct dm_integrity_c
*ic
, unsigned section
, unsigned offset
,
416 bool e
, const char *function
)
418 #if defined(CONFIG_DM_DEBUG) || defined(INTERNAL_VERIFY)
419 unsigned limit
= e
? ic
->journal_section_entries
: ic
->journal_section_sectors
;
421 if (unlikely(section
>= ic
->journal_sections
) ||
422 unlikely(offset
>= limit
)) {
423 printk(KERN_CRIT
"%s: invalid access at (%u,%u), limit (%u,%u)\n",
424 function
, section
, offset
, ic
->journal_sections
, limit
);
430 static void page_list_location(struct dm_integrity_c
*ic
, unsigned section
, unsigned offset
,
431 unsigned *pl_index
, unsigned *pl_offset
)
435 access_journal_check(ic
, section
, offset
, false, "page_list_location");
437 sector
= section
* ic
->journal_section_sectors
+ offset
;
439 *pl_index
= sector
>> (PAGE_SHIFT
- SECTOR_SHIFT
);
440 *pl_offset
= (sector
<< SECTOR_SHIFT
) & (PAGE_SIZE
- 1);
443 static struct journal_sector
*access_page_list(struct dm_integrity_c
*ic
, struct page_list
*pl
,
444 unsigned section
, unsigned offset
, unsigned *n_sectors
)
446 unsigned pl_index
, pl_offset
;
449 page_list_location(ic
, section
, offset
, &pl_index
, &pl_offset
);
452 *n_sectors
= (PAGE_SIZE
- pl_offset
) >> SECTOR_SHIFT
;
454 va
= lowmem_page_address(pl
[pl_index
].page
);
456 return (struct journal_sector
*)(va
+ pl_offset
);
459 static struct journal_sector
*access_journal(struct dm_integrity_c
*ic
, unsigned section
, unsigned offset
)
461 return access_page_list(ic
, ic
->journal
, section
, offset
, NULL
);
464 static struct journal_entry
*access_journal_entry(struct dm_integrity_c
*ic
, unsigned section
, unsigned n
)
466 unsigned rel_sector
, offset
;
467 struct journal_sector
*js
;
469 access_journal_check(ic
, section
, n
, true, "access_journal_entry");
471 rel_sector
= n
% JOURNAL_BLOCK_SECTORS
;
472 offset
= n
/ JOURNAL_BLOCK_SECTORS
;
474 js
= access_journal(ic
, section
, rel_sector
);
475 return (struct journal_entry
*)((char *)js
+ offset
* ic
->journal_entry_size
);
478 static struct journal_sector
*access_journal_data(struct dm_integrity_c
*ic
, unsigned section
, unsigned n
)
480 n
<<= ic
->sb
->log2_sectors_per_block
;
482 n
+= JOURNAL_BLOCK_SECTORS
;
484 access_journal_check(ic
, section
, n
, false, "access_journal_data");
486 return access_journal(ic
, section
, n
);
489 static void section_mac(struct dm_integrity_c
*ic
, unsigned section
, __u8 result
[JOURNAL_MAC_SIZE
])
491 SHASH_DESC_ON_STACK(desc
, ic
->journal_mac
);
495 desc
->tfm
= ic
->journal_mac
;
498 r
= crypto_shash_init(desc
);
500 dm_integrity_io_error(ic
, "crypto_shash_init", r
);
504 for (j
= 0; j
< ic
->journal_section_entries
; j
++) {
505 struct journal_entry
*je
= access_journal_entry(ic
, section
, j
);
506 r
= crypto_shash_update(desc
, (__u8
*)&je
->u
.sector
, sizeof je
->u
.sector
);
508 dm_integrity_io_error(ic
, "crypto_shash_update", r
);
513 size
= crypto_shash_digestsize(ic
->journal_mac
);
515 if (likely(size
<= JOURNAL_MAC_SIZE
)) {
516 r
= crypto_shash_final(desc
, result
);
518 dm_integrity_io_error(ic
, "crypto_shash_final", r
);
521 memset(result
+ size
, 0, JOURNAL_MAC_SIZE
- size
);
524 r
= crypto_shash_final(desc
, digest
);
526 dm_integrity_io_error(ic
, "crypto_shash_final", r
);
529 memcpy(result
, digest
, JOURNAL_MAC_SIZE
);
534 memset(result
, 0, JOURNAL_MAC_SIZE
);
537 static void rw_section_mac(struct dm_integrity_c
*ic
, unsigned section
, bool wr
)
539 __u8 result
[JOURNAL_MAC_SIZE
];
542 if (!ic
->journal_mac
)
545 section_mac(ic
, section
, result
);
547 for (j
= 0; j
< JOURNAL_BLOCK_SECTORS
; j
++) {
548 struct journal_sector
*js
= access_journal(ic
, section
, j
);
551 memcpy(&js
->mac
, result
+ (j
* JOURNAL_MAC_PER_SECTOR
), JOURNAL_MAC_PER_SECTOR
);
553 if (memcmp(&js
->mac
, result
+ (j
* JOURNAL_MAC_PER_SECTOR
), JOURNAL_MAC_PER_SECTOR
))
554 dm_integrity_io_error(ic
, "journal mac", -EILSEQ
);
559 static void complete_journal_op(void *context
)
561 struct journal_completion
*comp
= context
;
562 BUG_ON(!atomic_read(&comp
->in_flight
));
563 if (likely(atomic_dec_and_test(&comp
->in_flight
)))
564 complete(&comp
->comp
);
567 static void xor_journal(struct dm_integrity_c
*ic
, bool encrypt
, unsigned section
,
568 unsigned n_sections
, struct journal_completion
*comp
)
570 struct async_submit_ctl submit
;
571 size_t n_bytes
= (size_t)(n_sections
* ic
->journal_section_sectors
) << SECTOR_SHIFT
;
572 unsigned pl_index
, pl_offset
, section_index
;
573 struct page_list
*source_pl
, *target_pl
;
575 if (likely(encrypt
)) {
576 source_pl
= ic
->journal
;
577 target_pl
= ic
->journal_io
;
579 source_pl
= ic
->journal_io
;
580 target_pl
= ic
->journal
;
583 page_list_location(ic
, section
, 0, &pl_index
, &pl_offset
);
585 atomic_add(roundup(pl_offset
+ n_bytes
, PAGE_SIZE
) >> PAGE_SHIFT
, &comp
->in_flight
);
587 init_async_submit(&submit
, ASYNC_TX_XOR_ZERO_DST
, NULL
, complete_journal_op
, comp
, NULL
);
589 section_index
= pl_index
;
593 struct page
*src_pages
[2];
594 struct page
*dst_page
;
596 while (unlikely(pl_index
== section_index
)) {
599 rw_section_mac(ic
, section
, true);
604 page_list_location(ic
, section
, 0, §ion_index
, &dummy
);
607 this_step
= min(n_bytes
, (size_t)PAGE_SIZE
- pl_offset
);
608 dst_page
= target_pl
[pl_index
].page
;
609 src_pages
[0] = source_pl
[pl_index
].page
;
610 src_pages
[1] = ic
->journal_xor
[pl_index
].page
;
612 async_xor(dst_page
, src_pages
, pl_offset
, 2, this_step
, &submit
);
616 n_bytes
-= this_step
;
621 async_tx_issue_pending_all();
624 static void complete_journal_encrypt(struct crypto_async_request
*req
, int err
)
626 struct journal_completion
*comp
= req
->data
;
628 if (likely(err
== -EINPROGRESS
)) {
629 complete(&comp
->ic
->crypto_backoff
);
632 dm_integrity_io_error(comp
->ic
, "asynchronous encrypt", err
);
634 complete_journal_op(comp
);
637 static bool do_crypt(bool encrypt
, struct skcipher_request
*req
, struct journal_completion
*comp
)
640 skcipher_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
641 complete_journal_encrypt
, comp
);
643 r
= crypto_skcipher_encrypt(req
);
645 r
= crypto_skcipher_decrypt(req
);
648 if (likely(r
== -EINPROGRESS
))
650 if (likely(r
== -EBUSY
)) {
651 wait_for_completion(&comp
->ic
->crypto_backoff
);
652 reinit_completion(&comp
->ic
->crypto_backoff
);
655 dm_integrity_io_error(comp
->ic
, "encrypt", r
);
659 static void crypt_journal(struct dm_integrity_c
*ic
, bool encrypt
, unsigned section
,
660 unsigned n_sections
, struct journal_completion
*comp
)
662 struct scatterlist
**source_sg
;
663 struct scatterlist
**target_sg
;
665 atomic_add(2, &comp
->in_flight
);
667 if (likely(encrypt
)) {
668 source_sg
= ic
->journal_scatterlist
;
669 target_sg
= ic
->journal_io_scatterlist
;
671 source_sg
= ic
->journal_io_scatterlist
;
672 target_sg
= ic
->journal_scatterlist
;
676 struct skcipher_request
*req
;
681 rw_section_mac(ic
, section
, true);
683 req
= ic
->sk_requests
[section
];
684 ivsize
= crypto_skcipher_ivsize(ic
->journal_crypt
);
687 memcpy(iv
, iv
+ ivsize
, ivsize
);
689 req
->src
= source_sg
[section
];
690 req
->dst
= target_sg
[section
];
692 if (unlikely(do_crypt(encrypt
, req
, comp
)))
693 atomic_inc(&comp
->in_flight
);
697 } while (n_sections
);
699 atomic_dec(&comp
->in_flight
);
700 complete_journal_op(comp
);
703 static void encrypt_journal(struct dm_integrity_c
*ic
, bool encrypt
, unsigned section
,
704 unsigned n_sections
, struct journal_completion
*comp
)
707 return xor_journal(ic
, encrypt
, section
, n_sections
, comp
);
709 return crypt_journal(ic
, encrypt
, section
, n_sections
, comp
);
712 static void complete_journal_io(unsigned long error
, void *context
)
714 struct journal_completion
*comp
= context
;
715 if (unlikely(error
!= 0))
716 dm_integrity_io_error(comp
->ic
, "writing journal", -EIO
);
717 complete_journal_op(comp
);
720 static void rw_journal(struct dm_integrity_c
*ic
, int op
, int op_flags
, unsigned section
,
721 unsigned n_sections
, struct journal_completion
*comp
)
723 struct dm_io_request io_req
;
724 struct dm_io_region io_loc
;
725 unsigned sector
, n_sectors
, pl_index
, pl_offset
;
728 if (unlikely(dm_integrity_failed(ic
))) {
730 complete_journal_io(-1UL, comp
);
734 sector
= section
* ic
->journal_section_sectors
;
735 n_sectors
= n_sections
* ic
->journal_section_sectors
;
737 pl_index
= sector
>> (PAGE_SHIFT
- SECTOR_SHIFT
);
738 pl_offset
= (sector
<< SECTOR_SHIFT
) & (PAGE_SIZE
- 1);
741 io_req
.bi_op_flags
= op_flags
;
742 io_req
.mem
.type
= DM_IO_PAGE_LIST
;
744 io_req
.mem
.ptr
.pl
= &ic
->journal_io
[pl_index
];
746 io_req
.mem
.ptr
.pl
= &ic
->journal
[pl_index
];
747 io_req
.mem
.offset
= pl_offset
;
748 if (likely(comp
!= NULL
)) {
749 io_req
.notify
.fn
= complete_journal_io
;
750 io_req
.notify
.context
= comp
;
752 io_req
.notify
.fn
= NULL
;
754 io_req
.client
= ic
->io
;
755 io_loc
.bdev
= ic
->dev
->bdev
;
756 io_loc
.sector
= ic
->start
+ SB_SECTORS
+ sector
;
757 io_loc
.count
= n_sectors
;
759 r
= dm_io(&io_req
, 1, &io_loc
, NULL
);
761 dm_integrity_io_error(ic
, op
== REQ_OP_READ
? "reading journal" : "writing journal", r
);
763 WARN_ONCE(1, "asynchronous dm_io failed: %d", r
);
764 complete_journal_io(-1UL, comp
);
769 static void write_journal(struct dm_integrity_c
*ic
, unsigned commit_start
, unsigned commit_sections
)
771 struct journal_completion io_comp
;
772 struct journal_completion crypt_comp_1
;
773 struct journal_completion crypt_comp_2
;
777 init_completion(&io_comp
.comp
);
779 if (commit_start
+ commit_sections
<= ic
->journal_sections
) {
780 io_comp
.in_flight
= (atomic_t
)ATOMIC_INIT(1);
781 if (ic
->journal_io
) {
782 crypt_comp_1
.ic
= ic
;
783 init_completion(&crypt_comp_1
.comp
);
784 crypt_comp_1
.in_flight
= (atomic_t
)ATOMIC_INIT(0);
785 encrypt_journal(ic
, true, commit_start
, commit_sections
, &crypt_comp_1
);
786 wait_for_completion_io(&crypt_comp_1
.comp
);
788 for (i
= 0; i
< commit_sections
; i
++)
789 rw_section_mac(ic
, commit_start
+ i
, true);
791 rw_journal(ic
, REQ_OP_WRITE
, REQ_FUA
| REQ_SYNC
, commit_start
,
792 commit_sections
, &io_comp
);
795 io_comp
.in_flight
= (atomic_t
)ATOMIC_INIT(2);
796 to_end
= ic
->journal_sections
- commit_start
;
797 if (ic
->journal_io
) {
798 crypt_comp_1
.ic
= ic
;
799 init_completion(&crypt_comp_1
.comp
);
800 crypt_comp_1
.in_flight
= (atomic_t
)ATOMIC_INIT(0);
801 encrypt_journal(ic
, true, commit_start
, to_end
, &crypt_comp_1
);
802 if (try_wait_for_completion(&crypt_comp_1
.comp
)) {
803 rw_journal(ic
, REQ_OP_WRITE
, REQ_FUA
, commit_start
, to_end
, &io_comp
);
804 reinit_completion(&crypt_comp_1
.comp
);
805 crypt_comp_1
.in_flight
= (atomic_t
)ATOMIC_INIT(0);
806 encrypt_journal(ic
, true, 0, commit_sections
- to_end
, &crypt_comp_1
);
807 wait_for_completion_io(&crypt_comp_1
.comp
);
809 crypt_comp_2
.ic
= ic
;
810 init_completion(&crypt_comp_2
.comp
);
811 crypt_comp_2
.in_flight
= (atomic_t
)ATOMIC_INIT(0);
812 encrypt_journal(ic
, true, 0, commit_sections
- to_end
, &crypt_comp_2
);
813 wait_for_completion_io(&crypt_comp_1
.comp
);
814 rw_journal(ic
, REQ_OP_WRITE
, REQ_FUA
, commit_start
, to_end
, &io_comp
);
815 wait_for_completion_io(&crypt_comp_2
.comp
);
818 for (i
= 0; i
< to_end
; i
++)
819 rw_section_mac(ic
, commit_start
+ i
, true);
820 rw_journal(ic
, REQ_OP_WRITE
, REQ_FUA
, commit_start
, to_end
, &io_comp
);
821 for (i
= 0; i
< commit_sections
- to_end
; i
++)
822 rw_section_mac(ic
, i
, true);
824 rw_journal(ic
, REQ_OP_WRITE
, REQ_FUA
, 0, commit_sections
- to_end
, &io_comp
);
827 wait_for_completion_io(&io_comp
.comp
);
830 static void copy_from_journal(struct dm_integrity_c
*ic
, unsigned section
, unsigned offset
,
831 unsigned n_sectors
, sector_t target
, io_notify_fn fn
, void *data
)
833 struct dm_io_request io_req
;
834 struct dm_io_region io_loc
;
836 unsigned sector
, pl_index
, pl_offset
;
838 BUG_ON((target
| n_sectors
| offset
) & (unsigned)(ic
->sectors_per_block
- 1));
840 if (unlikely(dm_integrity_failed(ic
))) {
845 sector
= section
* ic
->journal_section_sectors
+ JOURNAL_BLOCK_SECTORS
+ offset
;
847 pl_index
= sector
>> (PAGE_SHIFT
- SECTOR_SHIFT
);
848 pl_offset
= (sector
<< SECTOR_SHIFT
) & (PAGE_SIZE
- 1);
850 io_req
.bi_op
= REQ_OP_WRITE
;
851 io_req
.bi_op_flags
= 0;
852 io_req
.mem
.type
= DM_IO_PAGE_LIST
;
853 io_req
.mem
.ptr
.pl
= &ic
->journal
[pl_index
];
854 io_req
.mem
.offset
= pl_offset
;
855 io_req
.notify
.fn
= fn
;
856 io_req
.notify
.context
= data
;
857 io_req
.client
= ic
->io
;
858 io_loc
.bdev
= ic
->dev
->bdev
;
859 io_loc
.sector
= ic
->start
+ target
;
860 io_loc
.count
= n_sectors
;
862 r
= dm_io(&io_req
, 1, &io_loc
, NULL
);
864 WARN_ONCE(1, "asynchronous dm_io failed: %d", r
);
869 static bool add_new_range(struct dm_integrity_c
*ic
, struct dm_integrity_range
*new_range
)
871 struct rb_node
**n
= &ic
->in_progress
.rb_node
;
872 struct rb_node
*parent
;
874 BUG_ON((new_range
->logical_sector
| new_range
->n_sectors
) & (unsigned)(ic
->sectors_per_block
- 1));
879 struct dm_integrity_range
*range
= container_of(*n
, struct dm_integrity_range
, node
);
882 if (new_range
->logical_sector
+ new_range
->n_sectors
<= range
->logical_sector
) {
883 n
= &range
->node
.rb_left
;
884 } else if (new_range
->logical_sector
>= range
->logical_sector
+ range
->n_sectors
) {
885 n
= &range
->node
.rb_right
;
891 rb_link_node(&new_range
->node
, parent
, n
);
892 rb_insert_color(&new_range
->node
, &ic
->in_progress
);
897 static void remove_range_unlocked(struct dm_integrity_c
*ic
, struct dm_integrity_range
*range
)
899 rb_erase(&range
->node
, &ic
->in_progress
);
900 wake_up_locked(&ic
->endio_wait
);
903 static void remove_range(struct dm_integrity_c
*ic
, struct dm_integrity_range
*range
)
907 spin_lock_irqsave(&ic
->endio_wait
.lock
, flags
);
908 remove_range_unlocked(ic
, range
);
909 spin_unlock_irqrestore(&ic
->endio_wait
.lock
, flags
);
912 static void init_journal_node(struct journal_node
*node
)
914 RB_CLEAR_NODE(&node
->node
);
915 node
->sector
= (sector_t
)-1;
918 static void add_journal_node(struct dm_integrity_c
*ic
, struct journal_node
*node
, sector_t sector
)
920 struct rb_node
**link
;
921 struct rb_node
*parent
;
923 node
->sector
= sector
;
924 BUG_ON(!RB_EMPTY_NODE(&node
->node
));
926 link
= &ic
->journal_tree_root
.rb_node
;
930 struct journal_node
*j
;
932 j
= container_of(parent
, struct journal_node
, node
);
933 if (sector
< j
->sector
)
934 link
= &j
->node
.rb_left
;
936 link
= &j
->node
.rb_right
;
939 rb_link_node(&node
->node
, parent
, link
);
940 rb_insert_color(&node
->node
, &ic
->journal_tree_root
);
943 static void remove_journal_node(struct dm_integrity_c
*ic
, struct journal_node
*node
)
945 BUG_ON(RB_EMPTY_NODE(&node
->node
));
946 rb_erase(&node
->node
, &ic
->journal_tree_root
);
947 init_journal_node(node
);
950 #define NOT_FOUND (-1U)
952 static unsigned find_journal_node(struct dm_integrity_c
*ic
, sector_t sector
, sector_t
*next_sector
)
954 struct rb_node
*n
= ic
->journal_tree_root
.rb_node
;
955 unsigned found
= NOT_FOUND
;
956 *next_sector
= (sector_t
)-1;
958 struct journal_node
*j
= container_of(n
, struct journal_node
, node
);
959 if (sector
== j
->sector
) {
960 found
= j
- ic
->journal_tree
;
962 if (sector
< j
->sector
) {
963 *next_sector
= j
->sector
;
966 n
= j
->node
.rb_right
;
973 static bool test_journal_node(struct dm_integrity_c
*ic
, unsigned pos
, sector_t sector
)
975 struct journal_node
*node
, *next_node
;
976 struct rb_node
*next
;
978 if (unlikely(pos
>= ic
->journal_entries
))
980 node
= &ic
->journal_tree
[pos
];
981 if (unlikely(RB_EMPTY_NODE(&node
->node
)))
983 if (unlikely(node
->sector
!= sector
))
986 next
= rb_next(&node
->node
);
990 next_node
= container_of(next
, struct journal_node
, node
);
991 return next_node
->sector
!= sector
;
994 static bool find_newer_committed_node(struct dm_integrity_c
*ic
, struct journal_node
*node
)
996 struct rb_node
*next
;
997 struct journal_node
*next_node
;
998 unsigned next_section
;
1000 BUG_ON(RB_EMPTY_NODE(&node
->node
));
1002 next
= rb_next(&node
->node
);
1003 if (unlikely(!next
))
1006 next_node
= container_of(next
, struct journal_node
, node
);
1008 if (next_node
->sector
!= node
->sector
)
1011 next_section
= (unsigned)(next_node
- ic
->journal_tree
) / ic
->journal_section_entries
;
1012 if (next_section
>= ic
->committed_section
&&
1013 next_section
< ic
->committed_section
+ ic
->n_committed_sections
)
1015 if (next_section
+ ic
->journal_sections
< ic
->committed_section
+ ic
->n_committed_sections
)
1025 static int dm_integrity_rw_tag(struct dm_integrity_c
*ic
, unsigned char *tag
, sector_t
*metadata_block
,
1026 unsigned *metadata_offset
, unsigned total_size
, int op
)
1029 unsigned char *data
, *dp
;
1030 struct dm_buffer
*b
;
1034 r
= dm_integrity_failed(ic
);
1038 data
= dm_bufio_read(ic
->bufio
, *metadata_block
, &b
);
1039 if (unlikely(IS_ERR(data
)))
1040 return PTR_ERR(data
);
1042 to_copy
= min((1U << SECTOR_SHIFT
<< ic
->log2_buffer_sectors
) - *metadata_offset
, total_size
);
1043 dp
= data
+ *metadata_offset
;
1044 if (op
== TAG_READ
) {
1045 memcpy(tag
, dp
, to_copy
);
1046 } else if (op
== TAG_WRITE
) {
1047 memcpy(dp
, tag
, to_copy
);
1048 dm_bufio_mark_partial_buffer_dirty(b
, *metadata_offset
, *metadata_offset
+ to_copy
);
1050 /* e.g.: op == TAG_CMP */
1051 if (unlikely(memcmp(dp
, tag
, to_copy
))) {
1054 for (i
= 0; i
< to_copy
; i
++) {
1055 if (dp
[i
] != tag
[i
])
1059 dm_bufio_release(b
);
1063 dm_bufio_release(b
);
1066 *metadata_offset
+= to_copy
;
1067 if (unlikely(*metadata_offset
== 1U << SECTOR_SHIFT
<< ic
->log2_buffer_sectors
)) {
1068 (*metadata_block
)++;
1069 *metadata_offset
= 0;
1071 total_size
-= to_copy
;
1072 } while (unlikely(total_size
));
1077 static void dm_integrity_flush_buffers(struct dm_integrity_c
*ic
)
1080 r
= dm_bufio_write_dirty_buffers(ic
->bufio
);
1082 dm_integrity_io_error(ic
, "writing tags", r
);
1085 static void sleep_on_endio_wait(struct dm_integrity_c
*ic
)
1087 DECLARE_WAITQUEUE(wait
, current
);
1088 __add_wait_queue(&ic
->endio_wait
, &wait
);
1089 __set_current_state(TASK_UNINTERRUPTIBLE
);
1090 spin_unlock_irq(&ic
->endio_wait
.lock
);
1092 spin_lock_irq(&ic
->endio_wait
.lock
);
1093 __remove_wait_queue(&ic
->endio_wait
, &wait
);
1096 static void autocommit_fn(unsigned long data
)
1098 struct dm_integrity_c
*ic
= (struct dm_integrity_c
*)data
;
1100 if (likely(!dm_integrity_failed(ic
)))
1101 queue_work(ic
->commit_wq
, &ic
->commit_work
);
1104 static void schedule_autocommit(struct dm_integrity_c
*ic
)
1106 if (!timer_pending(&ic
->autocommit_timer
))
1107 mod_timer(&ic
->autocommit_timer
, jiffies
+ ic
->autocommit_jiffies
);
1110 static void submit_flush_bio(struct dm_integrity_c
*ic
, struct dm_integrity_io
*dio
)
1113 unsigned long flags
;
1115 spin_lock_irqsave(&ic
->endio_wait
.lock
, flags
);
1116 bio
= dm_bio_from_per_bio_data(dio
, sizeof(struct dm_integrity_io
));
1117 bio_list_add(&ic
->flush_bio_list
, bio
);
1118 spin_unlock_irqrestore(&ic
->endio_wait
.lock
, flags
);
1120 queue_work(ic
->commit_wq
, &ic
->commit_work
);
1123 static void do_endio(struct dm_integrity_c
*ic
, struct bio
*bio
)
1125 int r
= dm_integrity_failed(ic
);
1126 if (unlikely(r
) && !bio
->bi_status
)
1127 bio
->bi_status
= errno_to_blk_status(r
);
1131 static void do_endio_flush(struct dm_integrity_c
*ic
, struct dm_integrity_io
*dio
)
1133 struct bio
*bio
= dm_bio_from_per_bio_data(dio
, sizeof(struct dm_integrity_io
));
1135 if (unlikely(dio
->fua
) && likely(!bio
->bi_status
) && likely(!dm_integrity_failed(ic
)))
1136 submit_flush_bio(ic
, dio
);
1141 static void dec_in_flight(struct dm_integrity_io
*dio
)
1143 if (atomic_dec_and_test(&dio
->in_flight
)) {
1144 struct dm_integrity_c
*ic
= dio
->ic
;
1147 remove_range(ic
, &dio
->range
);
1149 if (unlikely(dio
->write
))
1150 schedule_autocommit(ic
);
1152 bio
= dm_bio_from_per_bio_data(dio
, sizeof(struct dm_integrity_io
));
1154 if (unlikely(dio
->bi_status
) && !bio
->bi_status
)
1155 bio
->bi_status
= dio
->bi_status
;
1156 if (likely(!bio
->bi_status
) && unlikely(bio_sectors(bio
) != dio
->range
.n_sectors
)) {
1157 dio
->range
.logical_sector
+= dio
->range
.n_sectors
;
1158 bio_advance(bio
, dio
->range
.n_sectors
<< SECTOR_SHIFT
);
1159 INIT_WORK(&dio
->work
, integrity_bio_wait
);
1160 queue_work(ic
->wait_wq
, &dio
->work
);
1163 do_endio_flush(ic
, dio
);
1167 static void integrity_end_io(struct bio
*bio
)
1169 struct dm_integrity_io
*dio
= dm_per_bio_data(bio
, sizeof(struct dm_integrity_io
));
1171 bio
->bi_iter
= dio
->orig_bi_iter
;
1172 bio
->bi_disk
= dio
->orig_bi_disk
;
1173 bio
->bi_partno
= dio
->orig_bi_partno
;
1174 if (dio
->orig_bi_integrity
) {
1175 bio
->bi_integrity
= dio
->orig_bi_integrity
;
1176 bio
->bi_opf
|= REQ_INTEGRITY
;
1178 bio
->bi_end_io
= dio
->orig_bi_end_io
;
1180 if (dio
->completion
)
1181 complete(dio
->completion
);
1186 static void integrity_sector_checksum(struct dm_integrity_c
*ic
, sector_t sector
,
1187 const char *data
, char *result
)
1189 __u64 sector_le
= cpu_to_le64(sector
);
1190 SHASH_DESC_ON_STACK(req
, ic
->internal_hash
);
1192 unsigned digest_size
;
1194 req
->tfm
= ic
->internal_hash
;
1197 r
= crypto_shash_init(req
);
1198 if (unlikely(r
< 0)) {
1199 dm_integrity_io_error(ic
, "crypto_shash_init", r
);
1203 r
= crypto_shash_update(req
, (const __u8
*)§or_le
, sizeof sector_le
);
1204 if (unlikely(r
< 0)) {
1205 dm_integrity_io_error(ic
, "crypto_shash_update", r
);
1209 r
= crypto_shash_update(req
, data
, ic
->sectors_per_block
<< SECTOR_SHIFT
);
1210 if (unlikely(r
< 0)) {
1211 dm_integrity_io_error(ic
, "crypto_shash_update", r
);
1215 r
= crypto_shash_final(req
, result
);
1216 if (unlikely(r
< 0)) {
1217 dm_integrity_io_error(ic
, "crypto_shash_final", r
);
1221 digest_size
= crypto_shash_digestsize(ic
->internal_hash
);
1222 if (unlikely(digest_size
< ic
->tag_size
))
1223 memset(result
+ digest_size
, 0, ic
->tag_size
- digest_size
);
1228 /* this shouldn't happen anyway, the hash functions have no reason to fail */
1229 get_random_bytes(result
, ic
->tag_size
);
1232 static void integrity_metadata(struct work_struct
*w
)
1234 struct dm_integrity_io
*dio
= container_of(w
, struct dm_integrity_io
, work
);
1235 struct dm_integrity_c
*ic
= dio
->ic
;
1239 if (ic
->internal_hash
) {
1240 struct bvec_iter iter
;
1242 unsigned digest_size
= crypto_shash_digestsize(ic
->internal_hash
);
1243 struct bio
*bio
= dm_bio_from_per_bio_data(dio
, sizeof(struct dm_integrity_io
));
1245 unsigned extra_space
= unlikely(digest_size
> ic
->tag_size
) ? digest_size
- ic
->tag_size
: 0;
1246 char checksums_onstack
[ic
->tag_size
+ extra_space
];
1247 unsigned sectors_to_process
= dio
->range
.n_sectors
;
1248 sector_t sector
= dio
->range
.logical_sector
;
1250 if (unlikely(ic
->mode
== 'R'))
1253 checksums
= kmalloc((PAGE_SIZE
>> SECTOR_SHIFT
>> ic
->sb
->log2_sectors_per_block
) * ic
->tag_size
+ extra_space
,
1254 GFP_NOIO
| __GFP_NORETRY
| __GFP_NOWARN
);
1256 checksums
= checksums_onstack
;
1258 __bio_for_each_segment(bv
, bio
, iter
, dio
->orig_bi_iter
) {
1260 char *mem
, *checksums_ptr
;
1263 mem
= (char *)kmap_atomic(bv
.bv_page
) + bv
.bv_offset
;
1265 checksums_ptr
= checksums
;
1267 integrity_sector_checksum(ic
, sector
, mem
+ pos
, checksums_ptr
);
1268 checksums_ptr
+= ic
->tag_size
;
1269 sectors_to_process
-= ic
->sectors_per_block
;
1270 pos
+= ic
->sectors_per_block
<< SECTOR_SHIFT
;
1271 sector
+= ic
->sectors_per_block
;
1272 } while (pos
< bv
.bv_len
&& sectors_to_process
&& checksums
!= checksums_onstack
);
1275 r
= dm_integrity_rw_tag(ic
, checksums
, &dio
->metadata_block
, &dio
->metadata_offset
,
1276 checksums_ptr
- checksums
, !dio
->write
? TAG_CMP
: TAG_WRITE
);
1279 DMERR_LIMIT("Checksum failed at sector 0x%llx",
1280 (unsigned long long)(sector
- ((r
+ ic
->tag_size
- 1) / ic
->tag_size
)));
1282 atomic64_inc(&ic
->number_of_mismatches
);
1284 if (likely(checksums
!= checksums_onstack
))
1289 if (!sectors_to_process
)
1292 if (unlikely(pos
< bv
.bv_len
)) {
1293 bv
.bv_offset
+= pos
;
1299 if (likely(checksums
!= checksums_onstack
))
1302 struct bio_integrity_payload
*bip
= dio
->orig_bi_integrity
;
1306 struct bvec_iter iter
;
1307 unsigned data_to_process
= dio
->range
.n_sectors
;
1308 sector_to_block(ic
, data_to_process
);
1309 data_to_process
*= ic
->tag_size
;
1311 bip_for_each_vec(biv
, bip
, iter
) {
1315 BUG_ON(PageHighMem(biv
.bv_page
));
1316 tag
= lowmem_page_address(biv
.bv_page
) + biv
.bv_offset
;
1317 this_len
= min(biv
.bv_len
, data_to_process
);
1318 r
= dm_integrity_rw_tag(ic
, tag
, &dio
->metadata_block
, &dio
->metadata_offset
,
1319 this_len
, !dio
->write
? TAG_READ
: TAG_WRITE
);
1322 data_to_process
-= this_len
;
1323 if (!data_to_process
)
1332 dio
->bi_status
= errno_to_blk_status(r
);
1336 static int dm_integrity_map(struct dm_target
*ti
, struct bio
*bio
)
1338 struct dm_integrity_c
*ic
= ti
->private;
1339 struct dm_integrity_io
*dio
= dm_per_bio_data(bio
, sizeof(struct dm_integrity_io
));
1340 struct bio_integrity_payload
*bip
;
1342 sector_t area
, offset
;
1347 if (unlikely(bio
->bi_opf
& REQ_PREFLUSH
)) {
1348 submit_flush_bio(ic
, dio
);
1349 return DM_MAPIO_SUBMITTED
;
1352 dio
->range
.logical_sector
= dm_target_offset(ti
, bio
->bi_iter
.bi_sector
);
1353 dio
->write
= bio_op(bio
) == REQ_OP_WRITE
;
1354 dio
->fua
= dio
->write
&& bio
->bi_opf
& REQ_FUA
;
1355 if (unlikely(dio
->fua
)) {
1357 * Don't pass down the FUA flag because we have to flush
1358 * disk cache anyway.
1360 bio
->bi_opf
&= ~REQ_FUA
;
1362 if (unlikely(dio
->range
.logical_sector
+ bio_sectors(bio
) > ic
->provided_data_sectors
)) {
1363 DMERR("Too big sector number: 0x%llx + 0x%x > 0x%llx",
1364 (unsigned long long)dio
->range
.logical_sector
, bio_sectors(bio
),
1365 (unsigned long long)ic
->provided_data_sectors
);
1366 return DM_MAPIO_KILL
;
1368 if (unlikely((dio
->range
.logical_sector
| bio_sectors(bio
)) & (unsigned)(ic
->sectors_per_block
- 1))) {
1369 DMERR("Bio not aligned on %u sectors: 0x%llx, 0x%x",
1370 ic
->sectors_per_block
,
1371 (unsigned long long)dio
->range
.logical_sector
, bio_sectors(bio
));
1372 return DM_MAPIO_KILL
;
1375 if (ic
->sectors_per_block
> 1) {
1376 struct bvec_iter iter
;
1378 bio_for_each_segment(bv
, bio
, iter
) {
1379 if (unlikely(bv
.bv_len
& ((ic
->sectors_per_block
<< SECTOR_SHIFT
) - 1))) {
1380 DMERR("Bio vector (%u,%u) is not aligned on %u-sector boundary",
1381 bv
.bv_offset
, bv
.bv_len
, ic
->sectors_per_block
);
1382 return DM_MAPIO_KILL
;
1387 bip
= bio_integrity(bio
);
1388 if (!ic
->internal_hash
) {
1390 unsigned wanted_tag_size
= bio_sectors(bio
) >> ic
->sb
->log2_sectors_per_block
;
1391 if (ic
->log2_tag_size
>= 0)
1392 wanted_tag_size
<<= ic
->log2_tag_size
;
1394 wanted_tag_size
*= ic
->tag_size
;
1395 if (unlikely(wanted_tag_size
!= bip
->bip_iter
.bi_size
)) {
1396 DMERR("Invalid integrity data size %u, expected %u", bip
->bip_iter
.bi_size
, wanted_tag_size
);
1397 return DM_MAPIO_KILL
;
1401 if (unlikely(bip
!= NULL
)) {
1402 DMERR("Unexpected integrity data when using internal hash");
1403 return DM_MAPIO_KILL
;
1407 if (unlikely(ic
->mode
== 'R') && unlikely(dio
->write
))
1408 return DM_MAPIO_KILL
;
1410 get_area_and_offset(ic
, dio
->range
.logical_sector
, &area
, &offset
);
1411 dio
->metadata_block
= get_metadata_sector_and_offset(ic
, area
, offset
, &dio
->metadata_offset
);
1412 bio
->bi_iter
.bi_sector
= get_data_sector(ic
, area
, offset
);
1414 dm_integrity_map_continue(dio
, true);
1415 return DM_MAPIO_SUBMITTED
;
1418 static bool __journal_read_write(struct dm_integrity_io
*dio
, struct bio
*bio
,
1419 unsigned journal_section
, unsigned journal_entry
)
1421 struct dm_integrity_c
*ic
= dio
->ic
;
1422 sector_t logical_sector
;
1425 logical_sector
= dio
->range
.logical_sector
;
1426 n_sectors
= dio
->range
.n_sectors
;
1428 struct bio_vec bv
= bio_iovec(bio
);
1431 if (unlikely(bv
.bv_len
>> SECTOR_SHIFT
> n_sectors
))
1432 bv
.bv_len
= n_sectors
<< SECTOR_SHIFT
;
1433 n_sectors
-= bv
.bv_len
>> SECTOR_SHIFT
;
1434 bio_advance_iter(bio
, &bio
->bi_iter
, bv
.bv_len
);
1436 mem
= kmap_atomic(bv
.bv_page
);
1437 if (likely(dio
->write
))
1438 flush_dcache_page(bv
.bv_page
);
1441 struct journal_entry
*je
= access_journal_entry(ic
, journal_section
, journal_entry
);
1443 if (unlikely(!dio
->write
)) {
1444 struct journal_sector
*js
;
1448 if (unlikely(journal_entry_is_inprogress(je
))) {
1449 flush_dcache_page(bv
.bv_page
);
1452 __io_wait_event(ic
->copy_to_journal_wait
, !journal_entry_is_inprogress(je
));
1456 BUG_ON(journal_entry_get_sector(je
) != logical_sector
);
1457 js
= access_journal_data(ic
, journal_section
, journal_entry
);
1458 mem_ptr
= mem
+ bv
.bv_offset
;
1461 memcpy(mem_ptr
, js
, JOURNAL_SECTOR_DATA
);
1462 *(commit_id_t
*)(mem_ptr
+ JOURNAL_SECTOR_DATA
) = je
->last_bytes
[s
];
1464 mem_ptr
+= 1 << SECTOR_SHIFT
;
1465 } while (++s
< ic
->sectors_per_block
);
1466 #ifdef INTERNAL_VERIFY
1467 if (ic
->internal_hash
) {
1468 char checksums_onstack
[max(crypto_shash_digestsize(ic
->internal_hash
), ic
->tag_size
)];
1470 integrity_sector_checksum(ic
, logical_sector
, mem
+ bv
.bv_offset
, checksums_onstack
);
1471 if (unlikely(memcmp(checksums_onstack
, journal_entry_tag(ic
, je
), ic
->tag_size
))) {
1472 DMERR_LIMIT("Checksum failed when reading from journal, at sector 0x%llx",
1473 (unsigned long long)logical_sector
);
1479 if (!ic
->internal_hash
) {
1480 struct bio_integrity_payload
*bip
= bio_integrity(bio
);
1481 unsigned tag_todo
= ic
->tag_size
;
1482 char *tag_ptr
= journal_entry_tag(ic
, je
);
1485 struct bio_vec biv
= bvec_iter_bvec(bip
->bip_vec
, bip
->bip_iter
);
1486 unsigned tag_now
= min(biv
.bv_len
, tag_todo
);
1488 BUG_ON(PageHighMem(biv
.bv_page
));
1489 tag_addr
= lowmem_page_address(biv
.bv_page
) + biv
.bv_offset
;
1490 if (likely(dio
->write
))
1491 memcpy(tag_ptr
, tag_addr
, tag_now
);
1493 memcpy(tag_addr
, tag_ptr
, tag_now
);
1494 bvec_iter_advance(bip
->bip_vec
, &bip
->bip_iter
, tag_now
);
1496 tag_todo
-= tag_now
;
1497 } while (unlikely(tag_todo
)); else {
1498 if (likely(dio
->write
))
1499 memset(tag_ptr
, 0, tag_todo
);
1503 if (likely(dio
->write
)) {
1504 struct journal_sector
*js
;
1507 js
= access_journal_data(ic
, journal_section
, journal_entry
);
1508 memcpy(js
, mem
+ bv
.bv_offset
, ic
->sectors_per_block
<< SECTOR_SHIFT
);
1512 je
->last_bytes
[s
] = js
[s
].commit_id
;
1513 } while (++s
< ic
->sectors_per_block
);
1515 if (ic
->internal_hash
) {
1516 unsigned digest_size
= crypto_shash_digestsize(ic
->internal_hash
);
1517 if (unlikely(digest_size
> ic
->tag_size
)) {
1518 char checksums_onstack
[digest_size
];
1519 integrity_sector_checksum(ic
, logical_sector
, (char *)js
, checksums_onstack
);
1520 memcpy(journal_entry_tag(ic
, je
), checksums_onstack
, ic
->tag_size
);
1522 integrity_sector_checksum(ic
, logical_sector
, (char *)js
, journal_entry_tag(ic
, je
));
1525 journal_entry_set_sector(je
, logical_sector
);
1527 logical_sector
+= ic
->sectors_per_block
;
1530 if (unlikely(journal_entry
== ic
->journal_section_entries
)) {
1533 wraparound_section(ic
, &journal_section
);
1536 bv
.bv_offset
+= ic
->sectors_per_block
<< SECTOR_SHIFT
;
1537 } while (bv
.bv_len
-= ic
->sectors_per_block
<< SECTOR_SHIFT
);
1539 if (unlikely(!dio
->write
))
1540 flush_dcache_page(bv
.bv_page
);
1542 } while (n_sectors
);
1544 if (likely(dio
->write
)) {
1546 if (unlikely(waitqueue_active(&ic
->copy_to_journal_wait
)))
1547 wake_up(&ic
->copy_to_journal_wait
);
1548 if (ACCESS_ONCE(ic
->free_sectors
) <= ic
->free_sectors_threshold
) {
1549 queue_work(ic
->commit_wq
, &ic
->commit_work
);
1551 schedule_autocommit(ic
);
1554 remove_range(ic
, &dio
->range
);
1557 if (unlikely(bio
->bi_iter
.bi_size
)) {
1558 sector_t area
, offset
;
1560 dio
->range
.logical_sector
= logical_sector
;
1561 get_area_and_offset(ic
, dio
->range
.logical_sector
, &area
, &offset
);
1562 dio
->metadata_block
= get_metadata_sector_and_offset(ic
, area
, offset
, &dio
->metadata_offset
);
1569 static void dm_integrity_map_continue(struct dm_integrity_io
*dio
, bool from_map
)
1571 struct dm_integrity_c
*ic
= dio
->ic
;
1572 struct bio
*bio
= dm_bio_from_per_bio_data(dio
, sizeof(struct dm_integrity_io
));
1573 unsigned journal_section
, journal_entry
;
1574 unsigned journal_read_pos
;
1575 struct completion read_comp
;
1576 bool need_sync_io
= ic
->internal_hash
&& !dio
->write
;
1578 if (need_sync_io
&& from_map
) {
1579 INIT_WORK(&dio
->work
, integrity_bio_wait
);
1580 queue_work(ic
->metadata_wq
, &dio
->work
);
1585 spin_lock_irq(&ic
->endio_wait
.lock
);
1587 if (unlikely(dm_integrity_failed(ic
))) {
1588 spin_unlock_irq(&ic
->endio_wait
.lock
);
1592 dio
->range
.n_sectors
= bio_sectors(bio
);
1593 journal_read_pos
= NOT_FOUND
;
1594 if (likely(ic
->mode
== 'J')) {
1596 unsigned next_entry
, i
, pos
;
1597 unsigned ws
, we
, range_sectors
;
1599 dio
->range
.n_sectors
= min(dio
->range
.n_sectors
,
1600 ic
->free_sectors
<< ic
->sb
->log2_sectors_per_block
);
1601 if (unlikely(!dio
->range
.n_sectors
))
1603 range_sectors
= dio
->range
.n_sectors
>> ic
->sb
->log2_sectors_per_block
;
1604 ic
->free_sectors
-= range_sectors
;
1605 journal_section
= ic
->free_section
;
1606 journal_entry
= ic
->free_section_entry
;
1608 next_entry
= ic
->free_section_entry
+ range_sectors
;
1609 ic
->free_section_entry
= next_entry
% ic
->journal_section_entries
;
1610 ic
->free_section
+= next_entry
/ ic
->journal_section_entries
;
1611 ic
->n_uncommitted_sections
+= next_entry
/ ic
->journal_section_entries
;
1612 wraparound_section(ic
, &ic
->free_section
);
1614 pos
= journal_section
* ic
->journal_section_entries
+ journal_entry
;
1615 ws
= journal_section
;
1619 struct journal_entry
*je
;
1621 add_journal_node(ic
, &ic
->journal_tree
[pos
], dio
->range
.logical_sector
+ i
);
1623 if (unlikely(pos
>= ic
->journal_entries
))
1626 je
= access_journal_entry(ic
, ws
, we
);
1627 BUG_ON(!journal_entry_is_unused(je
));
1628 journal_entry_set_inprogress(je
);
1630 if (unlikely(we
== ic
->journal_section_entries
)) {
1633 wraparound_section(ic
, &ws
);
1635 } while ((i
+= ic
->sectors_per_block
) < dio
->range
.n_sectors
);
1637 spin_unlock_irq(&ic
->endio_wait
.lock
);
1638 goto journal_read_write
;
1640 sector_t next_sector
;
1641 journal_read_pos
= find_journal_node(ic
, dio
->range
.logical_sector
, &next_sector
);
1642 if (likely(journal_read_pos
== NOT_FOUND
)) {
1643 if (unlikely(dio
->range
.n_sectors
> next_sector
- dio
->range
.logical_sector
))
1644 dio
->range
.n_sectors
= next_sector
- dio
->range
.logical_sector
;
1647 unsigned jp
= journal_read_pos
+ 1;
1648 for (i
= ic
->sectors_per_block
; i
< dio
->range
.n_sectors
; i
+= ic
->sectors_per_block
, jp
++) {
1649 if (!test_journal_node(ic
, jp
, dio
->range
.logical_sector
+ i
))
1652 dio
->range
.n_sectors
= i
;
1656 if (unlikely(!add_new_range(ic
, &dio
->range
))) {
1658 * We must not sleep in the request routine because it could
1659 * stall bios on current->bio_list.
1660 * So, we offload the bio to a workqueue if we have to sleep.
1664 spin_unlock_irq(&ic
->endio_wait
.lock
);
1665 INIT_WORK(&dio
->work
, integrity_bio_wait
);
1666 queue_work(ic
->wait_wq
, &dio
->work
);
1669 sleep_on_endio_wait(ic
);
1673 spin_unlock_irq(&ic
->endio_wait
.lock
);
1675 if (unlikely(journal_read_pos
!= NOT_FOUND
)) {
1676 journal_section
= journal_read_pos
/ ic
->journal_section_entries
;
1677 journal_entry
= journal_read_pos
% ic
->journal_section_entries
;
1678 goto journal_read_write
;
1681 dio
->in_flight
= (atomic_t
)ATOMIC_INIT(2);
1684 init_completion(&read_comp
);
1685 dio
->completion
= &read_comp
;
1687 dio
->completion
= NULL
;
1689 dio
->orig_bi_iter
= bio
->bi_iter
;
1691 dio
->orig_bi_disk
= bio
->bi_disk
;
1692 dio
->orig_bi_partno
= bio
->bi_partno
;
1693 bio_set_dev(bio
, ic
->dev
->bdev
);
1695 dio
->orig_bi_integrity
= bio_integrity(bio
);
1696 bio
->bi_integrity
= NULL
;
1697 bio
->bi_opf
&= ~REQ_INTEGRITY
;
1699 dio
->orig_bi_end_io
= bio
->bi_end_io
;
1700 bio
->bi_end_io
= integrity_end_io
;
1702 bio
->bi_iter
.bi_size
= dio
->range
.n_sectors
<< SECTOR_SHIFT
;
1703 bio
->bi_iter
.bi_sector
+= ic
->start
;
1704 generic_make_request(bio
);
1707 wait_for_completion_io(&read_comp
);
1708 if (likely(!bio
->bi_status
))
1709 integrity_metadata(&dio
->work
);
1714 INIT_WORK(&dio
->work
, integrity_metadata
);
1715 queue_work(ic
->metadata_wq
, &dio
->work
);
1721 if (unlikely(__journal_read_write(dio
, bio
, journal_section
, journal_entry
)))
1724 do_endio_flush(ic
, dio
);
1728 static void integrity_bio_wait(struct work_struct
*w
)
1730 struct dm_integrity_io
*dio
= container_of(w
, struct dm_integrity_io
, work
);
1732 dm_integrity_map_continue(dio
, false);
1735 static void pad_uncommitted(struct dm_integrity_c
*ic
)
1737 if (ic
->free_section_entry
) {
1738 ic
->free_sectors
-= ic
->journal_section_entries
- ic
->free_section_entry
;
1739 ic
->free_section_entry
= 0;
1741 wraparound_section(ic
, &ic
->free_section
);
1742 ic
->n_uncommitted_sections
++;
1744 WARN_ON(ic
->journal_sections
* ic
->journal_section_entries
!=
1745 (ic
->n_uncommitted_sections
+ ic
->n_committed_sections
) * ic
->journal_section_entries
+ ic
->free_sectors
);
1748 static void integrity_commit(struct work_struct
*w
)
1750 struct dm_integrity_c
*ic
= container_of(w
, struct dm_integrity_c
, commit_work
);
1751 unsigned commit_start
, commit_sections
;
1753 struct bio
*flushes
;
1755 del_timer(&ic
->autocommit_timer
);
1757 spin_lock_irq(&ic
->endio_wait
.lock
);
1758 flushes
= bio_list_get(&ic
->flush_bio_list
);
1759 if (unlikely(ic
->mode
!= 'J')) {
1760 spin_unlock_irq(&ic
->endio_wait
.lock
);
1761 dm_integrity_flush_buffers(ic
);
1762 goto release_flush_bios
;
1765 pad_uncommitted(ic
);
1766 commit_start
= ic
->uncommitted_section
;
1767 commit_sections
= ic
->n_uncommitted_sections
;
1768 spin_unlock_irq(&ic
->endio_wait
.lock
);
1770 if (!commit_sections
)
1771 goto release_flush_bios
;
1774 for (n
= 0; n
< commit_sections
; n
++) {
1775 for (j
= 0; j
< ic
->journal_section_entries
; j
++) {
1776 struct journal_entry
*je
;
1777 je
= access_journal_entry(ic
, i
, j
);
1778 io_wait_event(ic
->copy_to_journal_wait
, !journal_entry_is_inprogress(je
));
1780 for (j
= 0; j
< ic
->journal_section_sectors
; j
++) {
1781 struct journal_sector
*js
;
1782 js
= access_journal(ic
, i
, j
);
1783 js
->commit_id
= dm_integrity_commit_id(ic
, i
, j
, ic
->commit_seq
);
1786 if (unlikely(i
>= ic
->journal_sections
))
1787 ic
->commit_seq
= next_commit_seq(ic
->commit_seq
);
1788 wraparound_section(ic
, &i
);
1792 write_journal(ic
, commit_start
, commit_sections
);
1794 spin_lock_irq(&ic
->endio_wait
.lock
);
1795 ic
->uncommitted_section
+= commit_sections
;
1796 wraparound_section(ic
, &ic
->uncommitted_section
);
1797 ic
->n_uncommitted_sections
-= commit_sections
;
1798 ic
->n_committed_sections
+= commit_sections
;
1799 spin_unlock_irq(&ic
->endio_wait
.lock
);
1801 if (ACCESS_ONCE(ic
->free_sectors
) <= ic
->free_sectors_threshold
)
1802 queue_work(ic
->writer_wq
, &ic
->writer_work
);
1806 struct bio
*next
= flushes
->bi_next
;
1807 flushes
->bi_next
= NULL
;
1808 do_endio(ic
, flushes
);
1813 static void complete_copy_from_journal(unsigned long error
, void *context
)
1815 struct journal_io
*io
= context
;
1816 struct journal_completion
*comp
= io
->comp
;
1817 struct dm_integrity_c
*ic
= comp
->ic
;
1818 remove_range(ic
, &io
->range
);
1819 mempool_free(io
, ic
->journal_io_mempool
);
1820 if (unlikely(error
!= 0))
1821 dm_integrity_io_error(ic
, "copying from journal", -EIO
);
1822 complete_journal_op(comp
);
1825 static void restore_last_bytes(struct dm_integrity_c
*ic
, struct journal_sector
*js
,
1826 struct journal_entry
*je
)
1830 js
->commit_id
= je
->last_bytes
[s
];
1832 } while (++s
< ic
->sectors_per_block
);
1835 static void do_journal_write(struct dm_integrity_c
*ic
, unsigned write_start
,
1836 unsigned write_sections
, bool from_replay
)
1839 struct journal_completion comp
;
1840 struct blk_plug plug
;
1842 blk_start_plug(&plug
);
1845 comp
.in_flight
= (atomic_t
)ATOMIC_INIT(1);
1846 init_completion(&comp
.comp
);
1849 for (n
= 0; n
< write_sections
; n
++, i
++, wraparound_section(ic
, &i
)) {
1850 #ifndef INTERNAL_VERIFY
1851 if (unlikely(from_replay
))
1853 rw_section_mac(ic
, i
, false);
1854 for (j
= 0; j
< ic
->journal_section_entries
; j
++) {
1855 struct journal_entry
*je
= access_journal_entry(ic
, i
, j
);
1856 sector_t sec
, area
, offset
;
1857 unsigned k
, l
, next_loop
;
1858 sector_t metadata_block
;
1859 unsigned metadata_offset
;
1860 struct journal_io
*io
;
1862 if (journal_entry_is_unused(je
))
1864 BUG_ON(unlikely(journal_entry_is_inprogress(je
)) && !from_replay
);
1865 sec
= journal_entry_get_sector(je
);
1866 if (unlikely(from_replay
)) {
1867 if (unlikely(sec
& (unsigned)(ic
->sectors_per_block
- 1))) {
1868 dm_integrity_io_error(ic
, "invalid sector in journal", -EIO
);
1869 sec
&= ~(sector_t
)(ic
->sectors_per_block
- 1);
1872 get_area_and_offset(ic
, sec
, &area
, &offset
);
1873 restore_last_bytes(ic
, access_journal_data(ic
, i
, j
), je
);
1874 for (k
= j
+ 1; k
< ic
->journal_section_entries
; k
++) {
1875 struct journal_entry
*je2
= access_journal_entry(ic
, i
, k
);
1876 sector_t sec2
, area2
, offset2
;
1877 if (journal_entry_is_unused(je2
))
1879 BUG_ON(unlikely(journal_entry_is_inprogress(je2
)) && !from_replay
);
1880 sec2
= journal_entry_get_sector(je2
);
1881 get_area_and_offset(ic
, sec2
, &area2
, &offset2
);
1882 if (area2
!= area
|| offset2
!= offset
+ ((k
- j
) << ic
->sb
->log2_sectors_per_block
))
1884 restore_last_bytes(ic
, access_journal_data(ic
, i
, k
), je2
);
1888 io
= mempool_alloc(ic
->journal_io_mempool
, GFP_NOIO
);
1890 io
->range
.logical_sector
= sec
;
1891 io
->range
.n_sectors
= (k
- j
) << ic
->sb
->log2_sectors_per_block
;
1893 spin_lock_irq(&ic
->endio_wait
.lock
);
1894 while (unlikely(!add_new_range(ic
, &io
->range
)))
1895 sleep_on_endio_wait(ic
);
1897 if (likely(!from_replay
)) {
1898 struct journal_node
*section_node
= &ic
->journal_tree
[i
* ic
->journal_section_entries
];
1900 /* don't write if there is newer committed sector */
1901 while (j
< k
&& find_newer_committed_node(ic
, §ion_node
[j
])) {
1902 struct journal_entry
*je2
= access_journal_entry(ic
, i
, j
);
1904 journal_entry_set_unused(je2
);
1905 remove_journal_node(ic
, §ion_node
[j
]);
1907 sec
+= ic
->sectors_per_block
;
1908 offset
+= ic
->sectors_per_block
;
1910 while (j
< k
&& find_newer_committed_node(ic
, §ion_node
[k
- 1])) {
1911 struct journal_entry
*je2
= access_journal_entry(ic
, i
, k
- 1);
1913 journal_entry_set_unused(je2
);
1914 remove_journal_node(ic
, §ion_node
[k
- 1]);
1918 remove_range_unlocked(ic
, &io
->range
);
1919 spin_unlock_irq(&ic
->endio_wait
.lock
);
1920 mempool_free(io
, ic
->journal_io_mempool
);
1923 for (l
= j
; l
< k
; l
++) {
1924 remove_journal_node(ic
, §ion_node
[l
]);
1927 spin_unlock_irq(&ic
->endio_wait
.lock
);
1929 metadata_block
= get_metadata_sector_and_offset(ic
, area
, offset
, &metadata_offset
);
1930 for (l
= j
; l
< k
; l
++) {
1932 struct journal_entry
*je2
= access_journal_entry(ic
, i
, l
);
1935 #ifndef INTERNAL_VERIFY
1936 unlikely(from_replay
) &&
1938 ic
->internal_hash
) {
1939 char test_tag
[max(crypto_shash_digestsize(ic
->internal_hash
), ic
->tag_size
)];
1941 integrity_sector_checksum(ic
, sec
+ ((l
- j
) << ic
->sb
->log2_sectors_per_block
),
1942 (char *)access_journal_data(ic
, i
, l
), test_tag
);
1943 if (unlikely(memcmp(test_tag
, journal_entry_tag(ic
, je2
), ic
->tag_size
)))
1944 dm_integrity_io_error(ic
, "tag mismatch when replaying journal", -EILSEQ
);
1947 journal_entry_set_unused(je2
);
1948 r
= dm_integrity_rw_tag(ic
, journal_entry_tag(ic
, je2
), &metadata_block
, &metadata_offset
,
1949 ic
->tag_size
, TAG_WRITE
);
1951 dm_integrity_io_error(ic
, "reading tags", r
);
1955 atomic_inc(&comp
.in_flight
);
1956 copy_from_journal(ic
, i
, j
<< ic
->sb
->log2_sectors_per_block
,
1957 (k
- j
) << ic
->sb
->log2_sectors_per_block
,
1958 get_data_sector(ic
, area
, offset
),
1959 complete_copy_from_journal
, io
);
1965 dm_bufio_write_dirty_buffers_async(ic
->bufio
);
1967 blk_finish_plug(&plug
);
1969 complete_journal_op(&comp
);
1970 wait_for_completion_io(&comp
.comp
);
1972 dm_integrity_flush_buffers(ic
);
1975 static void integrity_writer(struct work_struct
*w
)
1977 struct dm_integrity_c
*ic
= container_of(w
, struct dm_integrity_c
, writer_work
);
1978 unsigned write_start
, write_sections
;
1980 unsigned prev_free_sectors
;
1982 /* the following test is not needed, but it tests the replay code */
1983 if (ACCESS_ONCE(ic
->suspending
))
1986 spin_lock_irq(&ic
->endio_wait
.lock
);
1987 write_start
= ic
->committed_section
;
1988 write_sections
= ic
->n_committed_sections
;
1989 spin_unlock_irq(&ic
->endio_wait
.lock
);
1991 if (!write_sections
)
1994 do_journal_write(ic
, write_start
, write_sections
, false);
1996 spin_lock_irq(&ic
->endio_wait
.lock
);
1998 ic
->committed_section
+= write_sections
;
1999 wraparound_section(ic
, &ic
->committed_section
);
2000 ic
->n_committed_sections
-= write_sections
;
2002 prev_free_sectors
= ic
->free_sectors
;
2003 ic
->free_sectors
+= write_sections
* ic
->journal_section_entries
;
2004 if (unlikely(!prev_free_sectors
))
2005 wake_up_locked(&ic
->endio_wait
);
2007 spin_unlock_irq(&ic
->endio_wait
.lock
);
2010 static void init_journal(struct dm_integrity_c
*ic
, unsigned start_section
,
2011 unsigned n_sections
, unsigned char commit_seq
)
2018 for (n
= 0; n
< n_sections
; n
++) {
2019 i
= start_section
+ n
;
2020 wraparound_section(ic
, &i
);
2021 for (j
= 0; j
< ic
->journal_section_sectors
; j
++) {
2022 struct journal_sector
*js
= access_journal(ic
, i
, j
);
2023 memset(&js
->entries
, 0, JOURNAL_SECTOR_DATA
);
2024 js
->commit_id
= dm_integrity_commit_id(ic
, i
, j
, commit_seq
);
2026 for (j
= 0; j
< ic
->journal_section_entries
; j
++) {
2027 struct journal_entry
*je
= access_journal_entry(ic
, i
, j
);
2028 journal_entry_set_unused(je
);
2032 write_journal(ic
, start_section
, n_sections
);
2035 static int find_commit_seq(struct dm_integrity_c
*ic
, unsigned i
, unsigned j
, commit_id_t id
)
2038 for (k
= 0; k
< N_COMMIT_IDS
; k
++) {
2039 if (dm_integrity_commit_id(ic
, i
, j
, k
) == id
)
2042 dm_integrity_io_error(ic
, "journal commit id", -EIO
);
2046 static void replay_journal(struct dm_integrity_c
*ic
)
2049 bool used_commit_ids
[N_COMMIT_IDS
];
2050 unsigned max_commit_id_sections
[N_COMMIT_IDS
];
2051 unsigned write_start
, write_sections
;
2052 unsigned continue_section
;
2054 unsigned char unused
, last_used
, want_commit_seq
;
2056 if (ic
->mode
== 'R')
2059 if (ic
->journal_uptodate
)
2065 if (!ic
->just_formatted
) {
2066 DEBUG_print("reading journal\n");
2067 rw_journal(ic
, REQ_OP_READ
, 0, 0, ic
->journal_sections
, NULL
);
2069 DEBUG_bytes(lowmem_page_address(ic
->journal_io
[0].page
), 64, "read journal");
2070 if (ic
->journal_io
) {
2071 struct journal_completion crypt_comp
;
2073 init_completion(&crypt_comp
.comp
);
2074 crypt_comp
.in_flight
= (atomic_t
)ATOMIC_INIT(0);
2075 encrypt_journal(ic
, false, 0, ic
->journal_sections
, &crypt_comp
);
2076 wait_for_completion(&crypt_comp
.comp
);
2078 DEBUG_bytes(lowmem_page_address(ic
->journal
[0].page
), 64, "decrypted journal");
2081 if (dm_integrity_failed(ic
))
2084 journal_empty
= true;
2085 memset(used_commit_ids
, 0, sizeof used_commit_ids
);
2086 memset(max_commit_id_sections
, 0, sizeof max_commit_id_sections
);
2087 for (i
= 0; i
< ic
->journal_sections
; i
++) {
2088 for (j
= 0; j
< ic
->journal_section_sectors
; j
++) {
2090 struct journal_sector
*js
= access_journal(ic
, i
, j
);
2091 k
= find_commit_seq(ic
, i
, j
, js
->commit_id
);
2094 used_commit_ids
[k
] = true;
2095 max_commit_id_sections
[k
] = i
;
2097 if (journal_empty
) {
2098 for (j
= 0; j
< ic
->journal_section_entries
; j
++) {
2099 struct journal_entry
*je
= access_journal_entry(ic
, i
, j
);
2100 if (!journal_entry_is_unused(je
)) {
2101 journal_empty
= false;
2108 if (!used_commit_ids
[N_COMMIT_IDS
- 1]) {
2109 unused
= N_COMMIT_IDS
- 1;
2110 while (unused
&& !used_commit_ids
[unused
- 1])
2113 for (unused
= 0; unused
< N_COMMIT_IDS
; unused
++)
2114 if (!used_commit_ids
[unused
])
2116 if (unused
== N_COMMIT_IDS
) {
2117 dm_integrity_io_error(ic
, "journal commit ids", -EIO
);
2121 DEBUG_print("first unused commit seq %d [%d,%d,%d,%d]\n",
2122 unused
, used_commit_ids
[0], used_commit_ids
[1],
2123 used_commit_ids
[2], used_commit_ids
[3]);
2125 last_used
= prev_commit_seq(unused
);
2126 want_commit_seq
= prev_commit_seq(last_used
);
2128 if (!used_commit_ids
[want_commit_seq
] && used_commit_ids
[prev_commit_seq(want_commit_seq
)])
2129 journal_empty
= true;
2131 write_start
= max_commit_id_sections
[last_used
] + 1;
2132 if (unlikely(write_start
>= ic
->journal_sections
))
2133 want_commit_seq
= next_commit_seq(want_commit_seq
);
2134 wraparound_section(ic
, &write_start
);
2137 for (write_sections
= 0; write_sections
< ic
->journal_sections
; write_sections
++) {
2138 for (j
= 0; j
< ic
->journal_section_sectors
; j
++) {
2139 struct journal_sector
*js
= access_journal(ic
, i
, j
);
2141 if (js
->commit_id
!= dm_integrity_commit_id(ic
, i
, j
, want_commit_seq
)) {
2143 * This could be caused by crash during writing.
2144 * We won't replay the inconsistent part of the
2147 DEBUG_print("commit id mismatch at position (%u, %u): %d != %d\n",
2148 i
, j
, find_commit_seq(ic
, i
, j
, js
->commit_id
), want_commit_seq
);
2153 if (unlikely(i
>= ic
->journal_sections
))
2154 want_commit_seq
= next_commit_seq(want_commit_seq
);
2155 wraparound_section(ic
, &i
);
2159 if (!journal_empty
) {
2160 DEBUG_print("replaying %u sections, starting at %u, commit seq %d\n",
2161 write_sections
, write_start
, want_commit_seq
);
2162 do_journal_write(ic
, write_start
, write_sections
, true);
2165 if (write_sections
== ic
->journal_sections
&& (ic
->mode
== 'J' || journal_empty
)) {
2166 continue_section
= write_start
;
2167 ic
->commit_seq
= want_commit_seq
;
2168 DEBUG_print("continuing from section %u, commit seq %d\n", write_start
, ic
->commit_seq
);
2171 unsigned char erase_seq
;
2173 DEBUG_print("clearing journal\n");
2175 erase_seq
= prev_commit_seq(prev_commit_seq(last_used
));
2177 init_journal(ic
, s
, 1, erase_seq
);
2179 wraparound_section(ic
, &s
);
2180 if (ic
->journal_sections
>= 2) {
2181 init_journal(ic
, s
, ic
->journal_sections
- 2, erase_seq
);
2182 s
+= ic
->journal_sections
- 2;
2183 wraparound_section(ic
, &s
);
2184 init_journal(ic
, s
, 1, erase_seq
);
2187 continue_section
= 0;
2188 ic
->commit_seq
= next_commit_seq(erase_seq
);
2191 ic
->committed_section
= continue_section
;
2192 ic
->n_committed_sections
= 0;
2194 ic
->uncommitted_section
= continue_section
;
2195 ic
->n_uncommitted_sections
= 0;
2197 ic
->free_section
= continue_section
;
2198 ic
->free_section_entry
= 0;
2199 ic
->free_sectors
= ic
->journal_entries
;
2201 ic
->journal_tree_root
= RB_ROOT
;
2202 for (i
= 0; i
< ic
->journal_entries
; i
++)
2203 init_journal_node(&ic
->journal_tree
[i
]);
2206 static void dm_integrity_postsuspend(struct dm_target
*ti
)
2208 struct dm_integrity_c
*ic
= (struct dm_integrity_c
*)ti
->private;
2210 del_timer_sync(&ic
->autocommit_timer
);
2212 WRITE_ONCE(ic
->suspending
, 1);
2214 queue_work(ic
->commit_wq
, &ic
->commit_work
);
2215 drain_workqueue(ic
->commit_wq
);
2217 if (ic
->mode
== 'J') {
2218 drain_workqueue(ic
->writer_wq
);
2219 dm_integrity_flush_buffers(ic
);
2222 WRITE_ONCE(ic
->suspending
, 0);
2224 BUG_ON(!RB_EMPTY_ROOT(&ic
->in_progress
));
2226 ic
->journal_uptodate
= true;
2229 static void dm_integrity_resume(struct dm_target
*ti
)
2231 struct dm_integrity_c
*ic
= (struct dm_integrity_c
*)ti
->private;
2236 static void dm_integrity_status(struct dm_target
*ti
, status_type_t type
,
2237 unsigned status_flags
, char *result
, unsigned maxlen
)
2239 struct dm_integrity_c
*ic
= (struct dm_integrity_c
*)ti
->private;
2244 case STATUSTYPE_INFO
:
2245 DMEMIT("%llu", (unsigned long long)atomic64_read(&ic
->number_of_mismatches
));
2248 case STATUSTYPE_TABLE
: {
2249 __u64 watermark_percentage
= (__u64
)(ic
->journal_entries
- ic
->free_sectors_threshold
) * 100;
2250 watermark_percentage
+= ic
->journal_entries
/ 2;
2251 do_div(watermark_percentage
, ic
->journal_entries
);
2253 arg_count
+= ic
->sectors_per_block
!= 1;
2254 arg_count
+= !!ic
->internal_hash_alg
.alg_string
;
2255 arg_count
+= !!ic
->journal_crypt_alg
.alg_string
;
2256 arg_count
+= !!ic
->journal_mac_alg
.alg_string
;
2257 DMEMIT("%s %llu %u %c %u", ic
->dev
->name
, (unsigned long long)ic
->start
,
2258 ic
->tag_size
, ic
->mode
, arg_count
);
2259 DMEMIT(" journal_sectors:%u", ic
->initial_sectors
- SB_SECTORS
);
2260 DMEMIT(" interleave_sectors:%u", 1U << ic
->sb
->log2_interleave_sectors
);
2261 DMEMIT(" buffer_sectors:%u", 1U << ic
->log2_buffer_sectors
);
2262 DMEMIT(" journal_watermark:%u", (unsigned)watermark_percentage
);
2263 DMEMIT(" commit_time:%u", ic
->autocommit_msec
);
2264 if (ic
->sectors_per_block
!= 1)
2265 DMEMIT(" block_size:%u", ic
->sectors_per_block
<< SECTOR_SHIFT
);
2267 #define EMIT_ALG(a, n) \
2269 if (ic->a.alg_string) { \
2270 DMEMIT(" %s:%s", n, ic->a.alg_string); \
2271 if (ic->a.key_string) \
2272 DMEMIT(":%s", ic->a.key_string);\
2275 EMIT_ALG(internal_hash_alg
, "internal_hash");
2276 EMIT_ALG(journal_crypt_alg
, "journal_crypt");
2277 EMIT_ALG(journal_mac_alg
, "journal_mac");
2283 static int dm_integrity_iterate_devices(struct dm_target
*ti
,
2284 iterate_devices_callout_fn fn
, void *data
)
2286 struct dm_integrity_c
*ic
= ti
->private;
2288 return fn(ti
, ic
->dev
, ic
->start
+ ic
->initial_sectors
+ ic
->metadata_run
, ti
->len
, data
);
2291 static void dm_integrity_io_hints(struct dm_target
*ti
, struct queue_limits
*limits
)
2293 struct dm_integrity_c
*ic
= ti
->private;
2295 if (ic
->sectors_per_block
> 1) {
2296 limits
->logical_block_size
= ic
->sectors_per_block
<< SECTOR_SHIFT
;
2297 limits
->physical_block_size
= ic
->sectors_per_block
<< SECTOR_SHIFT
;
2298 blk_limits_io_min(limits
, ic
->sectors_per_block
<< SECTOR_SHIFT
);
2302 static void calculate_journal_section_size(struct dm_integrity_c
*ic
)
2304 unsigned sector_space
= JOURNAL_SECTOR_DATA
;
2306 ic
->journal_sections
= le32_to_cpu(ic
->sb
->journal_sections
);
2307 ic
->journal_entry_size
= roundup(offsetof(struct journal_entry
, last_bytes
[ic
->sectors_per_block
]) + ic
->tag_size
,
2308 JOURNAL_ENTRY_ROUNDUP
);
2310 if (ic
->sb
->flags
& cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC
))
2311 sector_space
-= JOURNAL_MAC_PER_SECTOR
;
2312 ic
->journal_entries_per_sector
= sector_space
/ ic
->journal_entry_size
;
2313 ic
->journal_section_entries
= ic
->journal_entries_per_sector
* JOURNAL_BLOCK_SECTORS
;
2314 ic
->journal_section_sectors
= (ic
->journal_section_entries
<< ic
->sb
->log2_sectors_per_block
) + JOURNAL_BLOCK_SECTORS
;
2315 ic
->journal_entries
= ic
->journal_section_entries
* ic
->journal_sections
;
2318 static int calculate_device_limits(struct dm_integrity_c
*ic
)
2320 __u64 initial_sectors
;
2321 sector_t last_sector
, last_area
, last_offset
;
2323 calculate_journal_section_size(ic
);
2324 initial_sectors
= SB_SECTORS
+ (__u64
)ic
->journal_section_sectors
* ic
->journal_sections
;
2325 if (initial_sectors
+ METADATA_PADDING_SECTORS
>= ic
->device_sectors
|| initial_sectors
> UINT_MAX
)
2327 ic
->initial_sectors
= initial_sectors
;
2329 ic
->metadata_run
= roundup((__u64
)ic
->tag_size
<< (ic
->sb
->log2_interleave_sectors
- ic
->sb
->log2_sectors_per_block
),
2330 (__u64
)(1 << SECTOR_SHIFT
<< METADATA_PADDING_SECTORS
)) >> SECTOR_SHIFT
;
2331 if (!(ic
->metadata_run
& (ic
->metadata_run
- 1)))
2332 ic
->log2_metadata_run
= __ffs(ic
->metadata_run
);
2334 ic
->log2_metadata_run
= -1;
2336 get_area_and_offset(ic
, ic
->provided_data_sectors
- 1, &last_area
, &last_offset
);
2337 last_sector
= get_data_sector(ic
, last_area
, last_offset
);
2339 if (ic
->start
+ last_sector
< last_sector
|| ic
->start
+ last_sector
>= ic
->device_sectors
)
2345 static int initialize_superblock(struct dm_integrity_c
*ic
, unsigned journal_sectors
, unsigned interleave_sectors
)
2347 unsigned journal_sections
;
2350 memset(ic
->sb
, 0, SB_SECTORS
<< SECTOR_SHIFT
);
2351 memcpy(ic
->sb
->magic
, SB_MAGIC
, 8);
2352 ic
->sb
->version
= SB_VERSION
;
2353 ic
->sb
->integrity_tag_size
= cpu_to_le16(ic
->tag_size
);
2354 ic
->sb
->log2_sectors_per_block
= __ffs(ic
->sectors_per_block
);
2355 if (ic
->journal_mac_alg
.alg_string
)
2356 ic
->sb
->flags
|= cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC
);
2358 calculate_journal_section_size(ic
);
2359 journal_sections
= journal_sectors
/ ic
->journal_section_sectors
;
2360 if (!journal_sections
)
2361 journal_sections
= 1;
2362 ic
->sb
->journal_sections
= cpu_to_le32(journal_sections
);
2364 if (!interleave_sectors
)
2365 interleave_sectors
= DEFAULT_INTERLEAVE_SECTORS
;
2366 ic
->sb
->log2_interleave_sectors
= __fls(interleave_sectors
);
2367 ic
->sb
->log2_interleave_sectors
= max((__u8
)MIN_LOG2_INTERLEAVE_SECTORS
, ic
->sb
->log2_interleave_sectors
);
2368 ic
->sb
->log2_interleave_sectors
= min((__u8
)MAX_LOG2_INTERLEAVE_SECTORS
, ic
->sb
->log2_interleave_sectors
);
2370 ic
->provided_data_sectors
= 0;
2371 for (test_bit
= fls64(ic
->device_sectors
) - 1; test_bit
>= 3; test_bit
--) {
2372 __u64 prev_data_sectors
= ic
->provided_data_sectors
;
2374 ic
->provided_data_sectors
|= (sector_t
)1 << test_bit
;
2375 if (calculate_device_limits(ic
))
2376 ic
->provided_data_sectors
= prev_data_sectors
;
2379 if (!ic
->provided_data_sectors
)
2382 ic
->sb
->provided_data_sectors
= cpu_to_le64(ic
->provided_data_sectors
);
2387 static void dm_integrity_set(struct dm_target
*ti
, struct dm_integrity_c
*ic
)
2389 struct gendisk
*disk
= dm_disk(dm_table_get_md(ti
->table
));
2390 struct blk_integrity bi
;
2392 memset(&bi
, 0, sizeof(bi
));
2393 bi
.profile
= &dm_integrity_profile
;
2394 bi
.tuple_size
= ic
->tag_size
;
2395 bi
.tag_size
= bi
.tuple_size
;
2396 bi
.interval_exp
= ic
->sb
->log2_sectors_per_block
+ SECTOR_SHIFT
;
2398 blk_integrity_register(disk
, &bi
);
2399 blk_queue_max_integrity_segments(disk
->queue
, UINT_MAX
);
2402 static void dm_integrity_free_page_list(struct dm_integrity_c
*ic
, struct page_list
*pl
)
2408 for (i
= 0; i
< ic
->journal_pages
; i
++)
2410 __free_page(pl
[i
].page
);
2414 static struct page_list
*dm_integrity_alloc_page_list(struct dm_integrity_c
*ic
)
2416 size_t page_list_desc_size
= ic
->journal_pages
* sizeof(struct page_list
);
2417 struct page_list
*pl
;
2420 pl
= kvmalloc(page_list_desc_size
, GFP_KERNEL
| __GFP_ZERO
);
2424 for (i
= 0; i
< ic
->journal_pages
; i
++) {
2425 pl
[i
].page
= alloc_page(GFP_KERNEL
);
2427 dm_integrity_free_page_list(ic
, pl
);
2431 pl
[i
- 1].next
= &pl
[i
];
2437 static void dm_integrity_free_journal_scatterlist(struct dm_integrity_c
*ic
, struct scatterlist
**sl
)
2440 for (i
= 0; i
< ic
->journal_sections
; i
++)
2445 static struct scatterlist
**dm_integrity_alloc_journal_scatterlist(struct dm_integrity_c
*ic
, struct page_list
*pl
)
2447 struct scatterlist
**sl
;
2450 sl
= kvmalloc(ic
->journal_sections
* sizeof(struct scatterlist
*), GFP_KERNEL
| __GFP_ZERO
);
2454 for (i
= 0; i
< ic
->journal_sections
; i
++) {
2455 struct scatterlist
*s
;
2456 unsigned start_index
, start_offset
;
2457 unsigned end_index
, end_offset
;
2461 page_list_location(ic
, i
, 0, &start_index
, &start_offset
);
2462 page_list_location(ic
, i
, ic
->journal_section_sectors
- 1, &end_index
, &end_offset
);
2464 n_pages
= (end_index
- start_index
+ 1);
2466 s
= kvmalloc(n_pages
* sizeof(struct scatterlist
), GFP_KERNEL
);
2468 dm_integrity_free_journal_scatterlist(ic
, sl
);
2472 sg_init_table(s
, n_pages
);
2473 for (idx
= start_index
; idx
<= end_index
; idx
++) {
2474 char *va
= lowmem_page_address(pl
[idx
].page
);
2475 unsigned start
= 0, end
= PAGE_SIZE
;
2476 if (idx
== start_index
)
2477 start
= start_offset
;
2478 if (idx
== end_index
)
2479 end
= end_offset
+ (1 << SECTOR_SHIFT
);
2480 sg_set_buf(&s
[idx
- start_index
], va
+ start
, end
- start
);
2489 static void free_alg(struct alg_spec
*a
)
2491 kzfree(a
->alg_string
);
2493 memset(a
, 0, sizeof *a
);
2496 static int get_alg_and_key(const char *arg
, struct alg_spec
*a
, char **error
, char *error_inval
)
2502 a
->alg_string
= kstrdup(strchr(arg
, ':') + 1, GFP_KERNEL
);
2506 k
= strchr(a
->alg_string
, ':');
2509 a
->key_string
= k
+ 1;
2510 if (strlen(a
->key_string
) & 1)
2513 a
->key_size
= strlen(a
->key_string
) / 2;
2514 a
->key
= kmalloc(a
->key_size
, GFP_KERNEL
);
2517 if (hex2bin(a
->key
, a
->key_string
, a
->key_size
))
2523 *error
= error_inval
;
2526 *error
= "Out of memory for an argument";
2530 static int get_mac(struct crypto_shash
**hash
, struct alg_spec
*a
, char **error
,
2531 char *error_alg
, char *error_key
)
2535 if (a
->alg_string
) {
2536 *hash
= crypto_alloc_shash(a
->alg_string
, 0, CRYPTO_ALG_ASYNC
);
2537 if (IS_ERR(*hash
)) {
2545 r
= crypto_shash_setkey(*hash
, a
->key
, a
->key_size
);
2550 } else if (crypto_shash_get_flags(*hash
) & CRYPTO_TFM_NEED_KEY
) {
2559 static int create_journal(struct dm_integrity_c
*ic
, char **error
)
2563 __u64 journal_pages
, journal_desc_size
, journal_tree_size
;
2564 unsigned char *crypt_data
= NULL
, *crypt_iv
= NULL
;
2565 struct skcipher_request
*req
= NULL
;
2567 ic
->commit_ids
[0] = cpu_to_le64(0x1111111111111111ULL
);
2568 ic
->commit_ids
[1] = cpu_to_le64(0x2222222222222222ULL
);
2569 ic
->commit_ids
[2] = cpu_to_le64(0x3333333333333333ULL
);
2570 ic
->commit_ids
[3] = cpu_to_le64(0x4444444444444444ULL
);
2572 journal_pages
= roundup((__u64
)ic
->journal_sections
* ic
->journal_section_sectors
,
2573 PAGE_SIZE
>> SECTOR_SHIFT
) >> (PAGE_SHIFT
- SECTOR_SHIFT
);
2574 journal_desc_size
= journal_pages
* sizeof(struct page_list
);
2575 if (journal_pages
>= totalram_pages
- totalhigh_pages
|| journal_desc_size
> ULONG_MAX
) {
2576 *error
= "Journal doesn't fit into memory";
2580 ic
->journal_pages
= journal_pages
;
2582 ic
->journal
= dm_integrity_alloc_page_list(ic
);
2584 *error
= "Could not allocate memory for journal";
2588 if (ic
->journal_crypt_alg
.alg_string
) {
2589 unsigned ivsize
, blocksize
;
2590 struct journal_completion comp
;
2593 ic
->journal_crypt
= crypto_alloc_skcipher(ic
->journal_crypt_alg
.alg_string
, 0, 0);
2594 if (IS_ERR(ic
->journal_crypt
)) {
2595 *error
= "Invalid journal cipher";
2596 r
= PTR_ERR(ic
->journal_crypt
);
2597 ic
->journal_crypt
= NULL
;
2600 ivsize
= crypto_skcipher_ivsize(ic
->journal_crypt
);
2601 blocksize
= crypto_skcipher_blocksize(ic
->journal_crypt
);
2603 if (ic
->journal_crypt_alg
.key
) {
2604 r
= crypto_skcipher_setkey(ic
->journal_crypt
, ic
->journal_crypt_alg
.key
,
2605 ic
->journal_crypt_alg
.key_size
);
2607 *error
= "Error setting encryption key";
2611 DEBUG_print("cipher %s, block size %u iv size %u\n",
2612 ic
->journal_crypt_alg
.alg_string
, blocksize
, ivsize
);
2614 ic
->journal_io
= dm_integrity_alloc_page_list(ic
);
2615 if (!ic
->journal_io
) {
2616 *error
= "Could not allocate memory for journal io";
2621 if (blocksize
== 1) {
2622 struct scatterlist
*sg
;
2624 req
= skcipher_request_alloc(ic
->journal_crypt
, GFP_KERNEL
);
2626 *error
= "Could not allocate crypt request";
2631 crypt_iv
= kmalloc(ivsize
, GFP_KERNEL
);
2633 *error
= "Could not allocate iv";
2638 ic
->journal_xor
= dm_integrity_alloc_page_list(ic
);
2639 if (!ic
->journal_xor
) {
2640 *error
= "Could not allocate memory for journal xor";
2645 sg
= kvmalloc((ic
->journal_pages
+ 1) * sizeof(struct scatterlist
), GFP_KERNEL
);
2647 *error
= "Unable to allocate sg list";
2651 sg_init_table(sg
, ic
->journal_pages
+ 1);
2652 for (i
= 0; i
< ic
->journal_pages
; i
++) {
2653 char *va
= lowmem_page_address(ic
->journal_xor
[i
].page
);
2655 sg_set_buf(&sg
[i
], va
, PAGE_SIZE
);
2657 sg_set_buf(&sg
[i
], &ic
->commit_ids
, sizeof ic
->commit_ids
);
2658 memset(crypt_iv
, 0x00, ivsize
);
2660 skcipher_request_set_crypt(req
, sg
, sg
, PAGE_SIZE
* ic
->journal_pages
+ sizeof ic
->commit_ids
, crypt_iv
);
2661 init_completion(&comp
.comp
);
2662 comp
.in_flight
= (atomic_t
)ATOMIC_INIT(1);
2663 if (do_crypt(true, req
, &comp
))
2664 wait_for_completion(&comp
.comp
);
2666 r
= dm_integrity_failed(ic
);
2668 *error
= "Unable to encrypt journal";
2671 DEBUG_bytes(lowmem_page_address(ic
->journal_xor
[0].page
), 64, "xor data");
2673 crypto_free_skcipher(ic
->journal_crypt
);
2674 ic
->journal_crypt
= NULL
;
2676 unsigned crypt_len
= roundup(ivsize
, blocksize
);
2678 req
= skcipher_request_alloc(ic
->journal_crypt
, GFP_KERNEL
);
2680 *error
= "Could not allocate crypt request";
2685 crypt_iv
= kmalloc(ivsize
, GFP_KERNEL
);
2687 *error
= "Could not allocate iv";
2692 crypt_data
= kmalloc(crypt_len
, GFP_KERNEL
);
2694 *error
= "Unable to allocate crypt data";
2699 ic
->journal_scatterlist
= dm_integrity_alloc_journal_scatterlist(ic
, ic
->journal
);
2700 if (!ic
->journal_scatterlist
) {
2701 *error
= "Unable to allocate sg list";
2705 ic
->journal_io_scatterlist
= dm_integrity_alloc_journal_scatterlist(ic
, ic
->journal_io
);
2706 if (!ic
->journal_io_scatterlist
) {
2707 *error
= "Unable to allocate sg list";
2711 ic
->sk_requests
= kvmalloc(ic
->journal_sections
* sizeof(struct skcipher_request
*), GFP_KERNEL
| __GFP_ZERO
);
2712 if (!ic
->sk_requests
) {
2713 *error
= "Unable to allocate sk requests";
2717 for (i
= 0; i
< ic
->journal_sections
; i
++) {
2718 struct scatterlist sg
;
2719 struct skcipher_request
*section_req
;
2720 __u32 section_le
= cpu_to_le32(i
);
2722 memset(crypt_iv
, 0x00, ivsize
);
2723 memset(crypt_data
, 0x00, crypt_len
);
2724 memcpy(crypt_data
, §ion_le
, min((size_t)crypt_len
, sizeof(section_le
)));
2726 sg_init_one(&sg
, crypt_data
, crypt_len
);
2727 skcipher_request_set_crypt(req
, &sg
, &sg
, crypt_len
, crypt_iv
);
2728 init_completion(&comp
.comp
);
2729 comp
.in_flight
= (atomic_t
)ATOMIC_INIT(1);
2730 if (do_crypt(true, req
, &comp
))
2731 wait_for_completion(&comp
.comp
);
2733 r
= dm_integrity_failed(ic
);
2735 *error
= "Unable to generate iv";
2739 section_req
= skcipher_request_alloc(ic
->journal_crypt
, GFP_KERNEL
);
2741 *error
= "Unable to allocate crypt request";
2745 section_req
->iv
= kmalloc(ivsize
* 2, GFP_KERNEL
);
2746 if (!section_req
->iv
) {
2747 skcipher_request_free(section_req
);
2748 *error
= "Unable to allocate iv";
2752 memcpy(section_req
->iv
+ ivsize
, crypt_data
, ivsize
);
2753 section_req
->cryptlen
= (size_t)ic
->journal_section_sectors
<< SECTOR_SHIFT
;
2754 ic
->sk_requests
[i
] = section_req
;
2755 DEBUG_bytes(crypt_data
, ivsize
, "iv(%u)", i
);
2760 for (i
= 0; i
< N_COMMIT_IDS
; i
++) {
2763 for (j
= 0; j
< i
; j
++) {
2764 if (ic
->commit_ids
[j
] == ic
->commit_ids
[i
]) {
2765 ic
->commit_ids
[i
] = cpu_to_le64(le64_to_cpu(ic
->commit_ids
[i
]) + 1);
2766 goto retest_commit_id
;
2769 DEBUG_print("commit id %u: %016llx\n", i
, ic
->commit_ids
[i
]);
2772 journal_tree_size
= (__u64
)ic
->journal_entries
* sizeof(struct journal_node
);
2773 if (journal_tree_size
> ULONG_MAX
) {
2774 *error
= "Journal doesn't fit into memory";
2778 ic
->journal_tree
= kvmalloc(journal_tree_size
, GFP_KERNEL
);
2779 if (!ic
->journal_tree
) {
2780 *error
= "Could not allocate memory for journal tree";
2786 skcipher_request_free(req
);
2792 * Construct a integrity mapping
2796 * offset from the start of the device
2798 * D - direct writes, J - journal writes, R - recovery mode
2799 * number of optional arguments
2800 * optional arguments:
2802 * interleave_sectors
2811 static int dm_integrity_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
2813 struct dm_integrity_c
*ic
;
2816 unsigned extra_args
;
2817 struct dm_arg_set as
;
2818 static const struct dm_arg _args
[] = {
2819 {0, 9, "Invalid number of feature args"},
2821 unsigned journal_sectors
, interleave_sectors
, buffer_sectors
, journal_watermark
, sync_msec
;
2822 bool should_write_sb
;
2824 unsigned long long start
;
2826 #define DIRECT_ARGUMENTS 4
2828 if (argc
<= DIRECT_ARGUMENTS
) {
2829 ti
->error
= "Invalid argument count";
2833 ic
= kzalloc(sizeof(struct dm_integrity_c
), GFP_KERNEL
);
2835 ti
->error
= "Cannot allocate integrity context";
2839 ti
->per_io_data_size
= sizeof(struct dm_integrity_io
);
2841 ic
->in_progress
= RB_ROOT
;
2842 init_waitqueue_head(&ic
->endio_wait
);
2843 bio_list_init(&ic
->flush_bio_list
);
2844 init_waitqueue_head(&ic
->copy_to_journal_wait
);
2845 init_completion(&ic
->crypto_backoff
);
2846 atomic64_set(&ic
->number_of_mismatches
, 0);
2848 r
= dm_get_device(ti
, argv
[0], dm_table_get_mode(ti
->table
), &ic
->dev
);
2850 ti
->error
= "Device lookup failed";
2854 if (sscanf(argv
[1], "%llu%c", &start
, &dummy
) != 1 || start
!= (sector_t
)start
) {
2855 ti
->error
= "Invalid starting offset";
2861 if (strcmp(argv
[2], "-")) {
2862 if (sscanf(argv
[2], "%u%c", &ic
->tag_size
, &dummy
) != 1 || !ic
->tag_size
) {
2863 ti
->error
= "Invalid tag size";
2869 if (!strcmp(argv
[3], "J") || !strcmp(argv
[3], "D") || !strcmp(argv
[3], "R"))
2870 ic
->mode
= argv
[3][0];
2872 ti
->error
= "Invalid mode (expecting J, D, R)";
2877 ic
->device_sectors
= i_size_read(ic
->dev
->bdev
->bd_inode
) >> SECTOR_SHIFT
;
2878 journal_sectors
= min((sector_t
)DEFAULT_MAX_JOURNAL_SECTORS
,
2879 ic
->device_sectors
>> DEFAULT_JOURNAL_SIZE_FACTOR
);
2880 interleave_sectors
= DEFAULT_INTERLEAVE_SECTORS
;
2881 buffer_sectors
= DEFAULT_BUFFER_SECTORS
;
2882 journal_watermark
= DEFAULT_JOURNAL_WATERMARK
;
2883 sync_msec
= DEFAULT_SYNC_MSEC
;
2884 ic
->sectors_per_block
= 1;
2886 as
.argc
= argc
- DIRECT_ARGUMENTS
;
2887 as
.argv
= argv
+ DIRECT_ARGUMENTS
;
2888 r
= dm_read_arg_group(_args
, &as
, &extra_args
, &ti
->error
);
2892 while (extra_args
--) {
2893 const char *opt_string
;
2895 opt_string
= dm_shift_arg(&as
);
2898 ti
->error
= "Not enough feature arguments";
2901 if (sscanf(opt_string
, "journal_sectors:%u%c", &val
, &dummy
) == 1)
2902 journal_sectors
= val
;
2903 else if (sscanf(opt_string
, "interleave_sectors:%u%c", &val
, &dummy
) == 1)
2904 interleave_sectors
= val
;
2905 else if (sscanf(opt_string
, "buffer_sectors:%u%c", &val
, &dummy
) == 1)
2906 buffer_sectors
= val
;
2907 else if (sscanf(opt_string
, "journal_watermark:%u%c", &val
, &dummy
) == 1 && val
<= 100)
2908 journal_watermark
= val
;
2909 else if (sscanf(opt_string
, "commit_time:%u%c", &val
, &dummy
) == 1)
2911 else if (sscanf(opt_string
, "block_size:%u%c", &val
, &dummy
) == 1) {
2912 if (val
< 1 << SECTOR_SHIFT
||
2913 val
> MAX_SECTORS_PER_BLOCK
<< SECTOR_SHIFT
||
2916 ti
->error
= "Invalid block_size argument";
2919 ic
->sectors_per_block
= val
>> SECTOR_SHIFT
;
2920 } else if (!memcmp(opt_string
, "internal_hash:", strlen("internal_hash:"))) {
2921 r
= get_alg_and_key(opt_string
, &ic
->internal_hash_alg
, &ti
->error
,
2922 "Invalid internal_hash argument");
2925 } else if (!memcmp(opt_string
, "journal_crypt:", strlen("journal_crypt:"))) {
2926 r
= get_alg_and_key(opt_string
, &ic
->journal_crypt_alg
, &ti
->error
,
2927 "Invalid journal_crypt argument");
2930 } else if (!memcmp(opt_string
, "journal_mac:", strlen("journal_mac:"))) {
2931 r
= get_alg_and_key(opt_string
, &ic
->journal_mac_alg
, &ti
->error
,
2932 "Invalid journal_mac argument");
2937 ti
->error
= "Invalid argument";
2942 r
= get_mac(&ic
->internal_hash
, &ic
->internal_hash_alg
, &ti
->error
,
2943 "Invalid internal hash", "Error setting internal hash key");
2947 r
= get_mac(&ic
->journal_mac
, &ic
->journal_mac_alg
, &ti
->error
,
2948 "Invalid journal mac", "Error setting journal mac key");
2952 if (!ic
->tag_size
) {
2953 if (!ic
->internal_hash
) {
2954 ti
->error
= "Unknown tag size";
2958 ic
->tag_size
= crypto_shash_digestsize(ic
->internal_hash
);
2960 if (ic
->tag_size
> MAX_TAG_SIZE
) {
2961 ti
->error
= "Too big tag size";
2965 if (!(ic
->tag_size
& (ic
->tag_size
- 1)))
2966 ic
->log2_tag_size
= __ffs(ic
->tag_size
);
2968 ic
->log2_tag_size
= -1;
2970 ic
->autocommit_jiffies
= msecs_to_jiffies(sync_msec
);
2971 ic
->autocommit_msec
= sync_msec
;
2972 setup_timer(&ic
->autocommit_timer
, autocommit_fn
, (unsigned long)ic
);
2974 ic
->io
= dm_io_client_create();
2975 if (IS_ERR(ic
->io
)) {
2976 r
= PTR_ERR(ic
->io
);
2978 ti
->error
= "Cannot allocate dm io";
2982 ic
->journal_io_mempool
= mempool_create_slab_pool(JOURNAL_IO_MEMPOOL
, journal_io_cache
);
2983 if (!ic
->journal_io_mempool
) {
2985 ti
->error
= "Cannot allocate mempool";
2989 ic
->metadata_wq
= alloc_workqueue("dm-integrity-metadata",
2990 WQ_MEM_RECLAIM
, METADATA_WORKQUEUE_MAX_ACTIVE
);
2991 if (!ic
->metadata_wq
) {
2992 ti
->error
= "Cannot allocate workqueue";
2998 * If this workqueue were percpu, it would cause bio reordering
2999 * and reduced performance.
3001 ic
->wait_wq
= alloc_workqueue("dm-integrity-wait", WQ_MEM_RECLAIM
| WQ_UNBOUND
, 1);
3003 ti
->error
= "Cannot allocate workqueue";
3008 ic
->commit_wq
= alloc_workqueue("dm-integrity-commit", WQ_MEM_RECLAIM
, 1);
3009 if (!ic
->commit_wq
) {
3010 ti
->error
= "Cannot allocate workqueue";
3014 INIT_WORK(&ic
->commit_work
, integrity_commit
);
3016 if (ic
->mode
== 'J') {
3017 ic
->writer_wq
= alloc_workqueue("dm-integrity-writer", WQ_MEM_RECLAIM
, 1);
3018 if (!ic
->writer_wq
) {
3019 ti
->error
= "Cannot allocate workqueue";
3023 INIT_WORK(&ic
->writer_work
, integrity_writer
);
3026 ic
->sb
= alloc_pages_exact(SB_SECTORS
<< SECTOR_SHIFT
, GFP_KERNEL
);
3029 ti
->error
= "Cannot allocate superblock area";
3033 r
= sync_rw_sb(ic
, REQ_OP_READ
, 0);
3035 ti
->error
= "Error reading superblock";
3038 should_write_sb
= false;
3039 if (memcmp(ic
->sb
->magic
, SB_MAGIC
, 8)) {
3040 if (ic
->mode
!= 'R') {
3041 if (memchr_inv(ic
->sb
, 0, SB_SECTORS
<< SECTOR_SHIFT
)) {
3043 ti
->error
= "The device is not initialized";
3048 r
= initialize_superblock(ic
, journal_sectors
, interleave_sectors
);
3050 ti
->error
= "Could not initialize superblock";
3053 if (ic
->mode
!= 'R')
3054 should_write_sb
= true;
3057 if (ic
->sb
->version
!= SB_VERSION
) {
3059 ti
->error
= "Unknown version";
3062 if (le16_to_cpu(ic
->sb
->integrity_tag_size
) != ic
->tag_size
) {
3064 ti
->error
= "Tag size doesn't match the information in superblock";
3067 if (ic
->sb
->log2_sectors_per_block
!= __ffs(ic
->sectors_per_block
)) {
3069 ti
->error
= "Block size doesn't match the information in superblock";
3072 if (!le32_to_cpu(ic
->sb
->journal_sections
)) {
3074 ti
->error
= "Corrupted superblock, journal_sections is 0";
3077 /* make sure that ti->max_io_len doesn't overflow */
3078 if (ic
->sb
->log2_interleave_sectors
< MIN_LOG2_INTERLEAVE_SECTORS
||
3079 ic
->sb
->log2_interleave_sectors
> MAX_LOG2_INTERLEAVE_SECTORS
) {
3081 ti
->error
= "Invalid interleave_sectors in the superblock";
3084 ic
->provided_data_sectors
= le64_to_cpu(ic
->sb
->provided_data_sectors
);
3085 if (ic
->provided_data_sectors
!= le64_to_cpu(ic
->sb
->provided_data_sectors
)) {
3086 /* test for overflow */
3088 ti
->error
= "The superblock has 64-bit device size, but the kernel was compiled with 32-bit sectors";
3091 if (!!(ic
->sb
->flags
& cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC
)) != !!ic
->journal_mac_alg
.alg_string
) {
3093 ti
->error
= "Journal mac mismatch";
3096 r
= calculate_device_limits(ic
);
3098 ti
->error
= "The device is too small";
3101 if (ti
->len
> ic
->provided_data_sectors
) {
3103 ti
->error
= "Not enough provided sectors for requested mapping size";
3107 if (!buffer_sectors
)
3109 ic
->log2_buffer_sectors
= min3((int)__fls(buffer_sectors
), (int)__ffs(ic
->metadata_run
), 31 - SECTOR_SHIFT
);
3111 threshold
= (__u64
)ic
->journal_entries
* (100 - journal_watermark
);
3113 do_div(threshold
, 100);
3114 ic
->free_sectors_threshold
= threshold
;
3116 DEBUG_print("initialized:\n");
3117 DEBUG_print(" integrity_tag_size %u\n", le16_to_cpu(ic
->sb
->integrity_tag_size
));
3118 DEBUG_print(" journal_entry_size %u\n", ic
->journal_entry_size
);
3119 DEBUG_print(" journal_entries_per_sector %u\n", ic
->journal_entries_per_sector
);
3120 DEBUG_print(" journal_section_entries %u\n", ic
->journal_section_entries
);
3121 DEBUG_print(" journal_section_sectors %u\n", ic
->journal_section_sectors
);
3122 DEBUG_print(" journal_sections %u\n", (unsigned)le32_to_cpu(ic
->sb
->journal_sections
));
3123 DEBUG_print(" journal_entries %u\n", ic
->journal_entries
);
3124 DEBUG_print(" log2_interleave_sectors %d\n", ic
->sb
->log2_interleave_sectors
);
3125 DEBUG_print(" device_sectors 0x%llx\n", (unsigned long long)ic
->device_sectors
);
3126 DEBUG_print(" initial_sectors 0x%x\n", ic
->initial_sectors
);
3127 DEBUG_print(" metadata_run 0x%x\n", ic
->metadata_run
);
3128 DEBUG_print(" log2_metadata_run %d\n", ic
->log2_metadata_run
);
3129 DEBUG_print(" provided_data_sectors 0x%llx (%llu)\n", (unsigned long long)ic
->provided_data_sectors
,
3130 (unsigned long long)ic
->provided_data_sectors
);
3131 DEBUG_print(" log2_buffer_sectors %u\n", ic
->log2_buffer_sectors
);
3133 ic
->bufio
= dm_bufio_client_create(ic
->dev
->bdev
, 1U << (SECTOR_SHIFT
+ ic
->log2_buffer_sectors
),
3135 if (IS_ERR(ic
->bufio
)) {
3136 r
= PTR_ERR(ic
->bufio
);
3137 ti
->error
= "Cannot initialize dm-bufio";
3141 dm_bufio_set_sector_offset(ic
->bufio
, ic
->start
+ ic
->initial_sectors
);
3143 if (ic
->mode
!= 'R') {
3144 r
= create_journal(ic
, &ti
->error
);
3149 if (should_write_sb
) {
3152 init_journal(ic
, 0, ic
->journal_sections
, 0);
3153 r
= dm_integrity_failed(ic
);
3155 ti
->error
= "Error initializing journal";
3158 r
= sync_rw_sb(ic
, REQ_OP_WRITE
, REQ_FUA
);
3160 ti
->error
= "Error initializing superblock";
3163 ic
->just_formatted
= true;
3166 r
= dm_set_target_max_io_len(ti
, 1U << ic
->sb
->log2_interleave_sectors
);
3170 if (!ic
->internal_hash
)
3171 dm_integrity_set(ti
, ic
);
3173 ti
->num_flush_bios
= 1;
3174 ti
->flush_supported
= true;
3178 dm_integrity_dtr(ti
);
3182 static void dm_integrity_dtr(struct dm_target
*ti
)
3184 struct dm_integrity_c
*ic
= ti
->private;
3186 BUG_ON(!RB_EMPTY_ROOT(&ic
->in_progress
));
3188 if (ic
->metadata_wq
)
3189 destroy_workqueue(ic
->metadata_wq
);
3191 destroy_workqueue(ic
->wait_wq
);
3193 destroy_workqueue(ic
->commit_wq
);
3195 destroy_workqueue(ic
->writer_wq
);
3197 dm_bufio_client_destroy(ic
->bufio
);
3198 mempool_destroy(ic
->journal_io_mempool
);
3200 dm_io_client_destroy(ic
->io
);
3202 dm_put_device(ti
, ic
->dev
);
3203 dm_integrity_free_page_list(ic
, ic
->journal
);
3204 dm_integrity_free_page_list(ic
, ic
->journal_io
);
3205 dm_integrity_free_page_list(ic
, ic
->journal_xor
);
3206 if (ic
->journal_scatterlist
)
3207 dm_integrity_free_journal_scatterlist(ic
, ic
->journal_scatterlist
);
3208 if (ic
->journal_io_scatterlist
)
3209 dm_integrity_free_journal_scatterlist(ic
, ic
->journal_io_scatterlist
);
3210 if (ic
->sk_requests
) {
3213 for (i
= 0; i
< ic
->journal_sections
; i
++) {
3214 struct skcipher_request
*req
= ic
->sk_requests
[i
];
3217 skcipher_request_free(req
);
3220 kvfree(ic
->sk_requests
);
3222 kvfree(ic
->journal_tree
);
3224 free_pages_exact(ic
->sb
, SB_SECTORS
<< SECTOR_SHIFT
);
3226 if (ic
->internal_hash
)
3227 crypto_free_shash(ic
->internal_hash
);
3228 free_alg(&ic
->internal_hash_alg
);
3230 if (ic
->journal_crypt
)
3231 crypto_free_skcipher(ic
->journal_crypt
);
3232 free_alg(&ic
->journal_crypt_alg
);
3234 if (ic
->journal_mac
)
3235 crypto_free_shash(ic
->journal_mac
);
3236 free_alg(&ic
->journal_mac_alg
);
3241 static struct target_type integrity_target
= {
3242 .name
= "integrity",
3243 .version
= {1, 1, 0},
3244 .module
= THIS_MODULE
,
3245 .features
= DM_TARGET_SINGLETON
| DM_TARGET_INTEGRITY
,
3246 .ctr
= dm_integrity_ctr
,
3247 .dtr
= dm_integrity_dtr
,
3248 .map
= dm_integrity_map
,
3249 .postsuspend
= dm_integrity_postsuspend
,
3250 .resume
= dm_integrity_resume
,
3251 .status
= dm_integrity_status
,
3252 .iterate_devices
= dm_integrity_iterate_devices
,
3253 .io_hints
= dm_integrity_io_hints
,
3256 int __init
dm_integrity_init(void)
3260 journal_io_cache
= kmem_cache_create("integrity_journal_io",
3261 sizeof(struct journal_io
), 0, 0, NULL
);
3262 if (!journal_io_cache
) {
3263 DMERR("can't allocate journal io cache");
3267 r
= dm_register_target(&integrity_target
);
3270 DMERR("register failed %d", r
);
3275 void dm_integrity_exit(void)
3277 dm_unregister_target(&integrity_target
);
3278 kmem_cache_destroy(journal_io_cache
);
3281 module_init(dm_integrity_init
);
3282 module_exit(dm_integrity_exit
);
3284 MODULE_AUTHOR("Milan Broz");
3285 MODULE_AUTHOR("Mikulas Patocka");
3286 MODULE_DESCRIPTION(DM_NAME
" target for integrity tags extension");
3287 MODULE_LICENSE("GPL");