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 <crypto/hash.h>
21 #include <crypto/skcipher.h>
22 #include <linux/async_tx.h>
23 #include <linux/dm-bufio.h>
25 #define DM_MSG_PREFIX "integrity"
27 #define DEFAULT_INTERLEAVE_SECTORS 32768
28 #define DEFAULT_JOURNAL_SIZE_FACTOR 7
29 #define DEFAULT_BUFFER_SECTORS 128
30 #define DEFAULT_JOURNAL_WATERMARK 50
31 #define DEFAULT_SYNC_MSEC 10000
32 #define DEFAULT_MAX_JOURNAL_SECTORS 131072
33 #define MIN_LOG2_INTERLEAVE_SECTORS 3
34 #define MAX_LOG2_INTERLEAVE_SECTORS 31
35 #define METADATA_WORKQUEUE_MAX_ACTIVE 16
36 #define RECALC_SECTORS 8192
37 #define RECALC_WRITE_SUPER 16
40 * Warning - DEBUG_PRINT prints security-sensitive data to the log,
41 * so it should not be enabled in the official kernel
44 //#define INTERNAL_VERIFY
50 #define SB_MAGIC "integrt"
51 #define SB_VERSION_1 1
52 #define SB_VERSION_2 2
54 #define MAX_SECTORS_PER_BLOCK 8
59 __u8 log2_interleave_sectors
;
60 __u16 integrity_tag_size
;
61 __u32 journal_sections
;
62 __u64 provided_data_sectors
; /* userspace uses this value */
64 __u8 log2_sectors_per_block
;
69 #define SB_FLAG_HAVE_JOURNAL_MAC 0x1
70 #define SB_FLAG_RECALCULATING 0x2
72 #define JOURNAL_ENTRY_ROUNDUP 8
74 typedef __u64 commit_id_t
;
75 #define JOURNAL_MAC_PER_SECTOR 8
77 struct journal_entry
{
85 commit_id_t last_bytes
[0];
89 #define journal_entry_tag(ic, je) ((__u8 *)&(je)->last_bytes[(ic)->sectors_per_block])
91 #if BITS_PER_LONG == 64
92 #define journal_entry_set_sector(je, x) do { smp_wmb(); WRITE_ONCE((je)->u.sector, cpu_to_le64(x)); } while (0)
93 #define journal_entry_get_sector(je) le64_to_cpu((je)->u.sector)
94 #elif defined(CONFIG_LBDAF)
95 #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)
96 #define journal_entry_get_sector(je) le64_to_cpu((je)->u.sector)
98 #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(0)); } while (0)
99 #define journal_entry_get_sector(je) le32_to_cpu((je)->u.s.sector_lo)
101 #define journal_entry_is_unused(je) ((je)->u.s.sector_hi == cpu_to_le32(-1))
102 #define journal_entry_set_unused(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-1)); } while (0)
103 #define journal_entry_is_inprogress(je) ((je)->u.s.sector_hi == cpu_to_le32(-2))
104 #define journal_entry_set_inprogress(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-2)); } while (0)
106 #define JOURNAL_BLOCK_SECTORS 8
107 #define JOURNAL_SECTOR_DATA ((1 << SECTOR_SHIFT) - sizeof(commit_id_t))
108 #define JOURNAL_MAC_SIZE (JOURNAL_MAC_PER_SECTOR * JOURNAL_BLOCK_SECTORS)
110 struct journal_sector
{
111 __u8 entries
[JOURNAL_SECTOR_DATA
- JOURNAL_MAC_PER_SECTOR
];
112 __u8 mac
[JOURNAL_MAC_PER_SECTOR
];
113 commit_id_t commit_id
;
116 #define MAX_TAG_SIZE (JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR - offsetof(struct journal_entry, last_bytes[MAX_SECTORS_PER_BLOCK]))
118 #define METADATA_PADDING_SECTORS 8
120 #define N_COMMIT_IDS 4
122 static unsigned char prev_commit_seq(unsigned char seq
)
124 return (seq
+ N_COMMIT_IDS
- 1) % N_COMMIT_IDS
;
127 static unsigned char next_commit_seq(unsigned char seq
)
129 return (seq
+ 1) % N_COMMIT_IDS
;
133 * In-memory structures
136 struct journal_node
{
148 struct dm_integrity_c
{
150 struct dm_dev
*meta_dev
;
154 mempool_t journal_io_mempool
;
155 struct dm_io_client
*io
;
156 struct dm_bufio_client
*bufio
;
157 struct workqueue_struct
*metadata_wq
;
158 struct superblock
*sb
;
159 unsigned journal_pages
;
160 struct page_list
*journal
;
161 struct page_list
*journal_io
;
162 struct page_list
*journal_xor
;
164 struct crypto_skcipher
*journal_crypt
;
165 struct scatterlist
**journal_scatterlist
;
166 struct scatterlist
**journal_io_scatterlist
;
167 struct skcipher_request
**sk_requests
;
169 struct crypto_shash
*journal_mac
;
171 struct journal_node
*journal_tree
;
172 struct rb_root journal_tree_root
;
174 sector_t provided_data_sectors
;
176 unsigned short journal_entry_size
;
177 unsigned char journal_entries_per_sector
;
178 unsigned char journal_section_entries
;
179 unsigned short journal_section_sectors
;
180 unsigned journal_sections
;
181 unsigned journal_entries
;
182 sector_t data_device_sectors
;
183 sector_t meta_device_sectors
;
184 unsigned initial_sectors
;
185 unsigned metadata_run
;
186 __s8 log2_metadata_run
;
187 __u8 log2_buffer_sectors
;
188 __u8 sectors_per_block
;
194 struct crypto_shash
*internal_hash
;
196 struct dm_target
*ti
;
198 /* these variables are locked with endio_wait.lock */
199 struct rb_root in_progress
;
200 struct list_head wait_list
;
201 wait_queue_head_t endio_wait
;
202 struct workqueue_struct
*wait_wq
;
203 struct workqueue_struct
*offload_wq
;
205 unsigned char commit_seq
;
206 commit_id_t commit_ids
[N_COMMIT_IDS
];
208 unsigned committed_section
;
209 unsigned n_committed_sections
;
211 unsigned uncommitted_section
;
212 unsigned n_uncommitted_sections
;
214 unsigned free_section
;
215 unsigned char free_section_entry
;
216 unsigned free_sectors
;
218 unsigned free_sectors_threshold
;
220 struct workqueue_struct
*commit_wq
;
221 struct work_struct commit_work
;
223 struct workqueue_struct
*writer_wq
;
224 struct work_struct writer_work
;
226 struct workqueue_struct
*recalc_wq
;
227 struct work_struct recalc_work
;
231 struct bio_list flush_bio_list
;
233 unsigned long autocommit_jiffies
;
234 struct timer_list autocommit_timer
;
235 unsigned autocommit_msec
;
237 wait_queue_head_t copy_to_journal_wait
;
239 struct completion crypto_backoff
;
241 bool journal_uptodate
;
244 struct alg_spec internal_hash_alg
;
245 struct alg_spec journal_crypt_alg
;
246 struct alg_spec journal_mac_alg
;
248 atomic64_t number_of_mismatches
;
251 struct dm_integrity_range
{
252 sector_t logical_sector
;
258 struct task_struct
*task
;
259 struct list_head wait_entry
;
264 struct dm_integrity_io
{
265 struct work_struct work
;
267 struct dm_integrity_c
*ic
;
271 struct dm_integrity_range range
;
273 sector_t metadata_block
;
274 unsigned metadata_offset
;
277 blk_status_t bi_status
;
279 struct completion
*completion
;
281 struct dm_bio_details bio_details
;
284 struct journal_completion
{
285 struct dm_integrity_c
*ic
;
287 struct completion comp
;
291 struct dm_integrity_range range
;
292 struct journal_completion
*comp
;
295 static struct kmem_cache
*journal_io_cache
;
297 #define JOURNAL_IO_MEMPOOL 32
300 #define DEBUG_print(x, ...) printk(KERN_DEBUG x, ##__VA_ARGS__)
301 static void __DEBUG_bytes(__u8
*bytes
, size_t len
, const char *msg
, ...)
310 pr_cont(" %02x", *bytes
);
316 #define DEBUG_bytes(bytes, len, msg, ...) __DEBUG_bytes(bytes, len, KERN_DEBUG msg, ##__VA_ARGS__)
318 #define DEBUG_print(x, ...) do { } while (0)
319 #define DEBUG_bytes(bytes, len, msg, ...) do { } while (0)
323 * DM Integrity profile, protection is performed layer above (dm-crypt)
325 static const struct blk_integrity_profile dm_integrity_profile
= {
326 .name
= "DM-DIF-EXT-TAG",
331 static void dm_integrity_map_continue(struct dm_integrity_io
*dio
, bool from_map
);
332 static void integrity_bio_wait(struct work_struct
*w
);
333 static void dm_integrity_dtr(struct dm_target
*ti
);
335 static void dm_integrity_io_error(struct dm_integrity_c
*ic
, const char *msg
, int err
)
338 atomic64_inc(&ic
->number_of_mismatches
);
339 if (!cmpxchg(&ic
->failed
, 0, err
))
340 DMERR("Error on %s: %d", msg
, err
);
343 static int dm_integrity_failed(struct dm_integrity_c
*ic
)
345 return READ_ONCE(ic
->failed
);
348 static commit_id_t
dm_integrity_commit_id(struct dm_integrity_c
*ic
, unsigned i
,
349 unsigned j
, unsigned char seq
)
352 * Xor the number with section and sector, so that if a piece of
353 * journal is written at wrong place, it is detected.
355 return ic
->commit_ids
[seq
] ^ cpu_to_le64(((__u64
)i
<< 32) ^ j
);
358 static void get_area_and_offset(struct dm_integrity_c
*ic
, sector_t data_sector
,
359 sector_t
*area
, sector_t
*offset
)
362 __u8 log2_interleave_sectors
= ic
->sb
->log2_interleave_sectors
;
363 *area
= data_sector
>> log2_interleave_sectors
;
364 *offset
= (unsigned)data_sector
& ((1U << log2_interleave_sectors
) - 1);
367 *offset
= data_sector
;
371 #define sector_to_block(ic, n) \
373 BUG_ON((n) & (unsigned)((ic)->sectors_per_block - 1)); \
374 (n) >>= (ic)->sb->log2_sectors_per_block; \
377 static __u64
get_metadata_sector_and_offset(struct dm_integrity_c
*ic
, sector_t area
,
378 sector_t offset
, unsigned *metadata_offset
)
383 ms
= area
<< ic
->sb
->log2_interleave_sectors
;
384 if (likely(ic
->log2_metadata_run
>= 0))
385 ms
+= area
<< ic
->log2_metadata_run
;
387 ms
+= area
* ic
->metadata_run
;
388 ms
>>= ic
->log2_buffer_sectors
;
390 sector_to_block(ic
, offset
);
392 if (likely(ic
->log2_tag_size
>= 0)) {
393 ms
+= offset
>> (SECTOR_SHIFT
+ ic
->log2_buffer_sectors
- ic
->log2_tag_size
);
394 mo
= (offset
<< ic
->log2_tag_size
) & ((1U << SECTOR_SHIFT
<< ic
->log2_buffer_sectors
) - 1);
396 ms
+= (__u64
)offset
* ic
->tag_size
>> (SECTOR_SHIFT
+ ic
->log2_buffer_sectors
);
397 mo
= (offset
* ic
->tag_size
) & ((1U << SECTOR_SHIFT
<< ic
->log2_buffer_sectors
) - 1);
399 *metadata_offset
= mo
;
403 static sector_t
get_data_sector(struct dm_integrity_c
*ic
, sector_t area
, sector_t offset
)
410 result
= area
<< ic
->sb
->log2_interleave_sectors
;
411 if (likely(ic
->log2_metadata_run
>= 0))
412 result
+= (area
+ 1) << ic
->log2_metadata_run
;
414 result
+= (area
+ 1) * ic
->metadata_run
;
416 result
+= (sector_t
)ic
->initial_sectors
+ offset
;
422 static void wraparound_section(struct dm_integrity_c
*ic
, unsigned *sec_ptr
)
424 if (unlikely(*sec_ptr
>= ic
->journal_sections
))
425 *sec_ptr
-= ic
->journal_sections
;
428 static void sb_set_version(struct dm_integrity_c
*ic
)
430 if (ic
->meta_dev
|| ic
->sb
->flags
& cpu_to_le32(SB_FLAG_RECALCULATING
))
431 ic
->sb
->version
= SB_VERSION_2
;
433 ic
->sb
->version
= SB_VERSION_1
;
436 static int sync_rw_sb(struct dm_integrity_c
*ic
, int op
, int op_flags
)
438 struct dm_io_request io_req
;
439 struct dm_io_region io_loc
;
442 io_req
.bi_op_flags
= op_flags
;
443 io_req
.mem
.type
= DM_IO_KMEM
;
444 io_req
.mem
.ptr
.addr
= ic
->sb
;
445 io_req
.notify
.fn
= NULL
;
446 io_req
.client
= ic
->io
;
447 io_loc
.bdev
= ic
->meta_dev
? ic
->meta_dev
->bdev
: ic
->dev
->bdev
;
448 io_loc
.sector
= ic
->start
;
449 io_loc
.count
= SB_SECTORS
;
451 return dm_io(&io_req
, 1, &io_loc
, NULL
);
454 static void access_journal_check(struct dm_integrity_c
*ic
, unsigned section
, unsigned offset
,
455 bool e
, const char *function
)
457 #if defined(CONFIG_DM_DEBUG) || defined(INTERNAL_VERIFY)
458 unsigned limit
= e
? ic
->journal_section_entries
: ic
->journal_section_sectors
;
460 if (unlikely(section
>= ic
->journal_sections
) ||
461 unlikely(offset
>= limit
)) {
462 printk(KERN_CRIT
"%s: invalid access at (%u,%u), limit (%u,%u)\n",
463 function
, section
, offset
, ic
->journal_sections
, limit
);
469 static void page_list_location(struct dm_integrity_c
*ic
, unsigned section
, unsigned offset
,
470 unsigned *pl_index
, unsigned *pl_offset
)
474 access_journal_check(ic
, section
, offset
, false, "page_list_location");
476 sector
= section
* ic
->journal_section_sectors
+ offset
;
478 *pl_index
= sector
>> (PAGE_SHIFT
- SECTOR_SHIFT
);
479 *pl_offset
= (sector
<< SECTOR_SHIFT
) & (PAGE_SIZE
- 1);
482 static struct journal_sector
*access_page_list(struct dm_integrity_c
*ic
, struct page_list
*pl
,
483 unsigned section
, unsigned offset
, unsigned *n_sectors
)
485 unsigned pl_index
, pl_offset
;
488 page_list_location(ic
, section
, offset
, &pl_index
, &pl_offset
);
491 *n_sectors
= (PAGE_SIZE
- pl_offset
) >> SECTOR_SHIFT
;
493 va
= lowmem_page_address(pl
[pl_index
].page
);
495 return (struct journal_sector
*)(va
+ pl_offset
);
498 static struct journal_sector
*access_journal(struct dm_integrity_c
*ic
, unsigned section
, unsigned offset
)
500 return access_page_list(ic
, ic
->journal
, section
, offset
, NULL
);
503 static struct journal_entry
*access_journal_entry(struct dm_integrity_c
*ic
, unsigned section
, unsigned n
)
505 unsigned rel_sector
, offset
;
506 struct journal_sector
*js
;
508 access_journal_check(ic
, section
, n
, true, "access_journal_entry");
510 rel_sector
= n
% JOURNAL_BLOCK_SECTORS
;
511 offset
= n
/ JOURNAL_BLOCK_SECTORS
;
513 js
= access_journal(ic
, section
, rel_sector
);
514 return (struct journal_entry
*)((char *)js
+ offset
* ic
->journal_entry_size
);
517 static struct journal_sector
*access_journal_data(struct dm_integrity_c
*ic
, unsigned section
, unsigned n
)
519 n
<<= ic
->sb
->log2_sectors_per_block
;
521 n
+= JOURNAL_BLOCK_SECTORS
;
523 access_journal_check(ic
, section
, n
, false, "access_journal_data");
525 return access_journal(ic
, section
, n
);
528 static void section_mac(struct dm_integrity_c
*ic
, unsigned section
, __u8 result
[JOURNAL_MAC_SIZE
])
530 SHASH_DESC_ON_STACK(desc
, ic
->journal_mac
);
534 desc
->tfm
= ic
->journal_mac
;
537 r
= crypto_shash_init(desc
);
539 dm_integrity_io_error(ic
, "crypto_shash_init", r
);
543 for (j
= 0; j
< ic
->journal_section_entries
; j
++) {
544 struct journal_entry
*je
= access_journal_entry(ic
, section
, j
);
545 r
= crypto_shash_update(desc
, (__u8
*)&je
->u
.sector
, sizeof je
->u
.sector
);
547 dm_integrity_io_error(ic
, "crypto_shash_update", r
);
552 size
= crypto_shash_digestsize(ic
->journal_mac
);
554 if (likely(size
<= JOURNAL_MAC_SIZE
)) {
555 r
= crypto_shash_final(desc
, result
);
557 dm_integrity_io_error(ic
, "crypto_shash_final", r
);
560 memset(result
+ size
, 0, JOURNAL_MAC_SIZE
- size
);
563 r
= crypto_shash_final(desc
, digest
);
565 dm_integrity_io_error(ic
, "crypto_shash_final", r
);
568 memcpy(result
, digest
, JOURNAL_MAC_SIZE
);
573 memset(result
, 0, JOURNAL_MAC_SIZE
);
576 static void rw_section_mac(struct dm_integrity_c
*ic
, unsigned section
, bool wr
)
578 __u8 result
[JOURNAL_MAC_SIZE
];
581 if (!ic
->journal_mac
)
584 section_mac(ic
, section
, result
);
586 for (j
= 0; j
< JOURNAL_BLOCK_SECTORS
; j
++) {
587 struct journal_sector
*js
= access_journal(ic
, section
, j
);
590 memcpy(&js
->mac
, result
+ (j
* JOURNAL_MAC_PER_SECTOR
), JOURNAL_MAC_PER_SECTOR
);
592 if (memcmp(&js
->mac
, result
+ (j
* JOURNAL_MAC_PER_SECTOR
), JOURNAL_MAC_PER_SECTOR
))
593 dm_integrity_io_error(ic
, "journal mac", -EILSEQ
);
598 static void complete_journal_op(void *context
)
600 struct journal_completion
*comp
= context
;
601 BUG_ON(!atomic_read(&comp
->in_flight
));
602 if (likely(atomic_dec_and_test(&comp
->in_flight
)))
603 complete(&comp
->comp
);
606 static void xor_journal(struct dm_integrity_c
*ic
, bool encrypt
, unsigned section
,
607 unsigned n_sections
, struct journal_completion
*comp
)
609 struct async_submit_ctl submit
;
610 size_t n_bytes
= (size_t)(n_sections
* ic
->journal_section_sectors
) << SECTOR_SHIFT
;
611 unsigned pl_index
, pl_offset
, section_index
;
612 struct page_list
*source_pl
, *target_pl
;
614 if (likely(encrypt
)) {
615 source_pl
= ic
->journal
;
616 target_pl
= ic
->journal_io
;
618 source_pl
= ic
->journal_io
;
619 target_pl
= ic
->journal
;
622 page_list_location(ic
, section
, 0, &pl_index
, &pl_offset
);
624 atomic_add(roundup(pl_offset
+ n_bytes
, PAGE_SIZE
) >> PAGE_SHIFT
, &comp
->in_flight
);
626 init_async_submit(&submit
, ASYNC_TX_XOR_ZERO_DST
, NULL
, complete_journal_op
, comp
, NULL
);
628 section_index
= pl_index
;
632 struct page
*src_pages
[2];
633 struct page
*dst_page
;
635 while (unlikely(pl_index
== section_index
)) {
638 rw_section_mac(ic
, section
, true);
643 page_list_location(ic
, section
, 0, §ion_index
, &dummy
);
646 this_step
= min(n_bytes
, (size_t)PAGE_SIZE
- pl_offset
);
647 dst_page
= target_pl
[pl_index
].page
;
648 src_pages
[0] = source_pl
[pl_index
].page
;
649 src_pages
[1] = ic
->journal_xor
[pl_index
].page
;
651 async_xor(dst_page
, src_pages
, pl_offset
, 2, this_step
, &submit
);
655 n_bytes
-= this_step
;
660 async_tx_issue_pending_all();
663 static void complete_journal_encrypt(struct crypto_async_request
*req
, int err
)
665 struct journal_completion
*comp
= req
->data
;
667 if (likely(err
== -EINPROGRESS
)) {
668 complete(&comp
->ic
->crypto_backoff
);
671 dm_integrity_io_error(comp
->ic
, "asynchronous encrypt", err
);
673 complete_journal_op(comp
);
676 static bool do_crypt(bool encrypt
, struct skcipher_request
*req
, struct journal_completion
*comp
)
679 skcipher_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
680 complete_journal_encrypt
, comp
);
682 r
= crypto_skcipher_encrypt(req
);
684 r
= crypto_skcipher_decrypt(req
);
687 if (likely(r
== -EINPROGRESS
))
689 if (likely(r
== -EBUSY
)) {
690 wait_for_completion(&comp
->ic
->crypto_backoff
);
691 reinit_completion(&comp
->ic
->crypto_backoff
);
694 dm_integrity_io_error(comp
->ic
, "encrypt", r
);
698 static void crypt_journal(struct dm_integrity_c
*ic
, bool encrypt
, unsigned section
,
699 unsigned n_sections
, struct journal_completion
*comp
)
701 struct scatterlist
**source_sg
;
702 struct scatterlist
**target_sg
;
704 atomic_add(2, &comp
->in_flight
);
706 if (likely(encrypt
)) {
707 source_sg
= ic
->journal_scatterlist
;
708 target_sg
= ic
->journal_io_scatterlist
;
710 source_sg
= ic
->journal_io_scatterlist
;
711 target_sg
= ic
->journal_scatterlist
;
715 struct skcipher_request
*req
;
720 rw_section_mac(ic
, section
, true);
722 req
= ic
->sk_requests
[section
];
723 ivsize
= crypto_skcipher_ivsize(ic
->journal_crypt
);
726 memcpy(iv
, iv
+ ivsize
, ivsize
);
728 req
->src
= source_sg
[section
];
729 req
->dst
= target_sg
[section
];
731 if (unlikely(do_crypt(encrypt
, req
, comp
)))
732 atomic_inc(&comp
->in_flight
);
736 } while (n_sections
);
738 atomic_dec(&comp
->in_flight
);
739 complete_journal_op(comp
);
742 static void encrypt_journal(struct dm_integrity_c
*ic
, bool encrypt
, unsigned section
,
743 unsigned n_sections
, struct journal_completion
*comp
)
746 return xor_journal(ic
, encrypt
, section
, n_sections
, comp
);
748 return crypt_journal(ic
, encrypt
, section
, n_sections
, comp
);
751 static void complete_journal_io(unsigned long error
, void *context
)
753 struct journal_completion
*comp
= context
;
754 if (unlikely(error
!= 0))
755 dm_integrity_io_error(comp
->ic
, "writing journal", -EIO
);
756 complete_journal_op(comp
);
759 static void rw_journal(struct dm_integrity_c
*ic
, int op
, int op_flags
, unsigned section
,
760 unsigned n_sections
, struct journal_completion
*comp
)
762 struct dm_io_request io_req
;
763 struct dm_io_region io_loc
;
764 unsigned sector
, n_sectors
, pl_index
, pl_offset
;
767 if (unlikely(dm_integrity_failed(ic
))) {
769 complete_journal_io(-1UL, comp
);
773 sector
= section
* ic
->journal_section_sectors
;
774 n_sectors
= n_sections
* ic
->journal_section_sectors
;
776 pl_index
= sector
>> (PAGE_SHIFT
- SECTOR_SHIFT
);
777 pl_offset
= (sector
<< SECTOR_SHIFT
) & (PAGE_SIZE
- 1);
780 io_req
.bi_op_flags
= op_flags
;
781 io_req
.mem
.type
= DM_IO_PAGE_LIST
;
783 io_req
.mem
.ptr
.pl
= &ic
->journal_io
[pl_index
];
785 io_req
.mem
.ptr
.pl
= &ic
->journal
[pl_index
];
786 io_req
.mem
.offset
= pl_offset
;
787 if (likely(comp
!= NULL
)) {
788 io_req
.notify
.fn
= complete_journal_io
;
789 io_req
.notify
.context
= comp
;
791 io_req
.notify
.fn
= NULL
;
793 io_req
.client
= ic
->io
;
794 io_loc
.bdev
= ic
->meta_dev
? ic
->meta_dev
->bdev
: ic
->dev
->bdev
;
795 io_loc
.sector
= ic
->start
+ SB_SECTORS
+ sector
;
796 io_loc
.count
= n_sectors
;
798 r
= dm_io(&io_req
, 1, &io_loc
, NULL
);
800 dm_integrity_io_error(ic
, op
== REQ_OP_READ
? "reading journal" : "writing journal", r
);
802 WARN_ONCE(1, "asynchronous dm_io failed: %d", r
);
803 complete_journal_io(-1UL, comp
);
808 static void write_journal(struct dm_integrity_c
*ic
, unsigned commit_start
, unsigned commit_sections
)
810 struct journal_completion io_comp
;
811 struct journal_completion crypt_comp_1
;
812 struct journal_completion crypt_comp_2
;
816 init_completion(&io_comp
.comp
);
818 if (commit_start
+ commit_sections
<= ic
->journal_sections
) {
819 io_comp
.in_flight
= (atomic_t
)ATOMIC_INIT(1);
820 if (ic
->journal_io
) {
821 crypt_comp_1
.ic
= ic
;
822 init_completion(&crypt_comp_1
.comp
);
823 crypt_comp_1
.in_flight
= (atomic_t
)ATOMIC_INIT(0);
824 encrypt_journal(ic
, true, commit_start
, commit_sections
, &crypt_comp_1
);
825 wait_for_completion_io(&crypt_comp_1
.comp
);
827 for (i
= 0; i
< commit_sections
; i
++)
828 rw_section_mac(ic
, commit_start
+ i
, true);
830 rw_journal(ic
, REQ_OP_WRITE
, REQ_FUA
| REQ_SYNC
, commit_start
,
831 commit_sections
, &io_comp
);
834 io_comp
.in_flight
= (atomic_t
)ATOMIC_INIT(2);
835 to_end
= ic
->journal_sections
- commit_start
;
836 if (ic
->journal_io
) {
837 crypt_comp_1
.ic
= ic
;
838 init_completion(&crypt_comp_1
.comp
);
839 crypt_comp_1
.in_flight
= (atomic_t
)ATOMIC_INIT(0);
840 encrypt_journal(ic
, true, commit_start
, to_end
, &crypt_comp_1
);
841 if (try_wait_for_completion(&crypt_comp_1
.comp
)) {
842 rw_journal(ic
, REQ_OP_WRITE
, REQ_FUA
, commit_start
, to_end
, &io_comp
);
843 reinit_completion(&crypt_comp_1
.comp
);
844 crypt_comp_1
.in_flight
= (atomic_t
)ATOMIC_INIT(0);
845 encrypt_journal(ic
, true, 0, commit_sections
- to_end
, &crypt_comp_1
);
846 wait_for_completion_io(&crypt_comp_1
.comp
);
848 crypt_comp_2
.ic
= ic
;
849 init_completion(&crypt_comp_2
.comp
);
850 crypt_comp_2
.in_flight
= (atomic_t
)ATOMIC_INIT(0);
851 encrypt_journal(ic
, true, 0, commit_sections
- to_end
, &crypt_comp_2
);
852 wait_for_completion_io(&crypt_comp_1
.comp
);
853 rw_journal(ic
, REQ_OP_WRITE
, REQ_FUA
, commit_start
, to_end
, &io_comp
);
854 wait_for_completion_io(&crypt_comp_2
.comp
);
857 for (i
= 0; i
< to_end
; i
++)
858 rw_section_mac(ic
, commit_start
+ i
, true);
859 rw_journal(ic
, REQ_OP_WRITE
, REQ_FUA
, commit_start
, to_end
, &io_comp
);
860 for (i
= 0; i
< commit_sections
- to_end
; i
++)
861 rw_section_mac(ic
, i
, true);
863 rw_journal(ic
, REQ_OP_WRITE
, REQ_FUA
, 0, commit_sections
- to_end
, &io_comp
);
866 wait_for_completion_io(&io_comp
.comp
);
869 static void copy_from_journal(struct dm_integrity_c
*ic
, unsigned section
, unsigned offset
,
870 unsigned n_sectors
, sector_t target
, io_notify_fn fn
, void *data
)
872 struct dm_io_request io_req
;
873 struct dm_io_region io_loc
;
875 unsigned sector
, pl_index
, pl_offset
;
877 BUG_ON((target
| n_sectors
| offset
) & (unsigned)(ic
->sectors_per_block
- 1));
879 if (unlikely(dm_integrity_failed(ic
))) {
884 sector
= section
* ic
->journal_section_sectors
+ JOURNAL_BLOCK_SECTORS
+ offset
;
886 pl_index
= sector
>> (PAGE_SHIFT
- SECTOR_SHIFT
);
887 pl_offset
= (sector
<< SECTOR_SHIFT
) & (PAGE_SIZE
- 1);
889 io_req
.bi_op
= REQ_OP_WRITE
;
890 io_req
.bi_op_flags
= 0;
891 io_req
.mem
.type
= DM_IO_PAGE_LIST
;
892 io_req
.mem
.ptr
.pl
= &ic
->journal
[pl_index
];
893 io_req
.mem
.offset
= pl_offset
;
894 io_req
.notify
.fn
= fn
;
895 io_req
.notify
.context
= data
;
896 io_req
.client
= ic
->io
;
897 io_loc
.bdev
= ic
->dev
->bdev
;
898 io_loc
.sector
= target
;
899 io_loc
.count
= n_sectors
;
901 r
= dm_io(&io_req
, 1, &io_loc
, NULL
);
903 WARN_ONCE(1, "asynchronous dm_io failed: %d", r
);
908 static bool ranges_overlap(struct dm_integrity_range
*range1
, struct dm_integrity_range
*range2
)
910 return range1
->logical_sector
< range2
->logical_sector
+ range2
->n_sectors
&&
911 range1
->logical_sector
+ range1
->n_sectors
> range2
->logical_sector
;
914 static bool add_new_range(struct dm_integrity_c
*ic
, struct dm_integrity_range
*new_range
, bool check_waiting
)
916 struct rb_node
**n
= &ic
->in_progress
.rb_node
;
917 struct rb_node
*parent
;
919 BUG_ON((new_range
->logical_sector
| new_range
->n_sectors
) & (unsigned)(ic
->sectors_per_block
- 1));
921 if (likely(check_waiting
)) {
922 struct dm_integrity_range
*range
;
923 list_for_each_entry(range
, &ic
->wait_list
, wait_entry
) {
924 if (unlikely(ranges_overlap(range
, new_range
)))
932 struct dm_integrity_range
*range
= container_of(*n
, struct dm_integrity_range
, node
);
935 if (new_range
->logical_sector
+ new_range
->n_sectors
<= range
->logical_sector
) {
936 n
= &range
->node
.rb_left
;
937 } else if (new_range
->logical_sector
>= range
->logical_sector
+ range
->n_sectors
) {
938 n
= &range
->node
.rb_right
;
944 rb_link_node(&new_range
->node
, parent
, n
);
945 rb_insert_color(&new_range
->node
, &ic
->in_progress
);
950 static void remove_range_unlocked(struct dm_integrity_c
*ic
, struct dm_integrity_range
*range
)
952 rb_erase(&range
->node
, &ic
->in_progress
);
953 while (unlikely(!list_empty(&ic
->wait_list
))) {
954 struct dm_integrity_range
*last_range
=
955 list_first_entry(&ic
->wait_list
, struct dm_integrity_range
, wait_entry
);
956 struct task_struct
*last_range_task
;
957 last_range_task
= last_range
->task
;
958 list_del(&last_range
->wait_entry
);
959 if (!add_new_range(ic
, last_range
, false)) {
960 last_range
->task
= last_range_task
;
961 list_add(&last_range
->wait_entry
, &ic
->wait_list
);
964 last_range
->waiting
= false;
965 wake_up_process(last_range_task
);
969 static void remove_range(struct dm_integrity_c
*ic
, struct dm_integrity_range
*range
)
973 spin_lock_irqsave(&ic
->endio_wait
.lock
, flags
);
974 remove_range_unlocked(ic
, range
);
975 spin_unlock_irqrestore(&ic
->endio_wait
.lock
, flags
);
978 static void wait_and_add_new_range(struct dm_integrity_c
*ic
, struct dm_integrity_range
*new_range
)
980 new_range
->waiting
= true;
981 list_add_tail(&new_range
->wait_entry
, &ic
->wait_list
);
982 new_range
->task
= current
;
984 __set_current_state(TASK_UNINTERRUPTIBLE
);
985 spin_unlock_irq(&ic
->endio_wait
.lock
);
987 spin_lock_irq(&ic
->endio_wait
.lock
);
988 } while (unlikely(new_range
->waiting
));
991 static void init_journal_node(struct journal_node
*node
)
993 RB_CLEAR_NODE(&node
->node
);
994 node
->sector
= (sector_t
)-1;
997 static void add_journal_node(struct dm_integrity_c
*ic
, struct journal_node
*node
, sector_t sector
)
999 struct rb_node
**link
;
1000 struct rb_node
*parent
;
1002 node
->sector
= sector
;
1003 BUG_ON(!RB_EMPTY_NODE(&node
->node
));
1005 link
= &ic
->journal_tree_root
.rb_node
;
1009 struct journal_node
*j
;
1011 j
= container_of(parent
, struct journal_node
, node
);
1012 if (sector
< j
->sector
)
1013 link
= &j
->node
.rb_left
;
1015 link
= &j
->node
.rb_right
;
1018 rb_link_node(&node
->node
, parent
, link
);
1019 rb_insert_color(&node
->node
, &ic
->journal_tree_root
);
1022 static void remove_journal_node(struct dm_integrity_c
*ic
, struct journal_node
*node
)
1024 BUG_ON(RB_EMPTY_NODE(&node
->node
));
1025 rb_erase(&node
->node
, &ic
->journal_tree_root
);
1026 init_journal_node(node
);
1029 #define NOT_FOUND (-1U)
1031 static unsigned find_journal_node(struct dm_integrity_c
*ic
, sector_t sector
, sector_t
*next_sector
)
1033 struct rb_node
*n
= ic
->journal_tree_root
.rb_node
;
1034 unsigned found
= NOT_FOUND
;
1035 *next_sector
= (sector_t
)-1;
1037 struct journal_node
*j
= container_of(n
, struct journal_node
, node
);
1038 if (sector
== j
->sector
) {
1039 found
= j
- ic
->journal_tree
;
1041 if (sector
< j
->sector
) {
1042 *next_sector
= j
->sector
;
1043 n
= j
->node
.rb_left
;
1045 n
= j
->node
.rb_right
;
1052 static bool test_journal_node(struct dm_integrity_c
*ic
, unsigned pos
, sector_t sector
)
1054 struct journal_node
*node
, *next_node
;
1055 struct rb_node
*next
;
1057 if (unlikely(pos
>= ic
->journal_entries
))
1059 node
= &ic
->journal_tree
[pos
];
1060 if (unlikely(RB_EMPTY_NODE(&node
->node
)))
1062 if (unlikely(node
->sector
!= sector
))
1065 next
= rb_next(&node
->node
);
1066 if (unlikely(!next
))
1069 next_node
= container_of(next
, struct journal_node
, node
);
1070 return next_node
->sector
!= sector
;
1073 static bool find_newer_committed_node(struct dm_integrity_c
*ic
, struct journal_node
*node
)
1075 struct rb_node
*next
;
1076 struct journal_node
*next_node
;
1077 unsigned next_section
;
1079 BUG_ON(RB_EMPTY_NODE(&node
->node
));
1081 next
= rb_next(&node
->node
);
1082 if (unlikely(!next
))
1085 next_node
= container_of(next
, struct journal_node
, node
);
1087 if (next_node
->sector
!= node
->sector
)
1090 next_section
= (unsigned)(next_node
- ic
->journal_tree
) / ic
->journal_section_entries
;
1091 if (next_section
>= ic
->committed_section
&&
1092 next_section
< ic
->committed_section
+ ic
->n_committed_sections
)
1094 if (next_section
+ ic
->journal_sections
< ic
->committed_section
+ ic
->n_committed_sections
)
1104 static int dm_integrity_rw_tag(struct dm_integrity_c
*ic
, unsigned char *tag
, sector_t
*metadata_block
,
1105 unsigned *metadata_offset
, unsigned total_size
, int op
)
1108 unsigned char *data
, *dp
;
1109 struct dm_buffer
*b
;
1113 r
= dm_integrity_failed(ic
);
1117 data
= dm_bufio_read(ic
->bufio
, *metadata_block
, &b
);
1118 if (unlikely(IS_ERR(data
)))
1119 return PTR_ERR(data
);
1121 to_copy
= min((1U << SECTOR_SHIFT
<< ic
->log2_buffer_sectors
) - *metadata_offset
, total_size
);
1122 dp
= data
+ *metadata_offset
;
1123 if (op
== TAG_READ
) {
1124 memcpy(tag
, dp
, to_copy
);
1125 } else if (op
== TAG_WRITE
) {
1126 memcpy(dp
, tag
, to_copy
);
1127 dm_bufio_mark_partial_buffer_dirty(b
, *metadata_offset
, *metadata_offset
+ to_copy
);
1129 /* e.g.: op == TAG_CMP */
1130 if (unlikely(memcmp(dp
, tag
, to_copy
))) {
1133 for (i
= 0; i
< to_copy
; i
++) {
1134 if (dp
[i
] != tag
[i
])
1138 dm_bufio_release(b
);
1142 dm_bufio_release(b
);
1145 *metadata_offset
+= to_copy
;
1146 if (unlikely(*metadata_offset
== 1U << SECTOR_SHIFT
<< ic
->log2_buffer_sectors
)) {
1147 (*metadata_block
)++;
1148 *metadata_offset
= 0;
1150 total_size
-= to_copy
;
1151 } while (unlikely(total_size
));
1156 static void dm_integrity_flush_buffers(struct dm_integrity_c
*ic
)
1159 r
= dm_bufio_write_dirty_buffers(ic
->bufio
);
1161 dm_integrity_io_error(ic
, "writing tags", r
);
1164 static void sleep_on_endio_wait(struct dm_integrity_c
*ic
)
1166 DECLARE_WAITQUEUE(wait
, current
);
1167 __add_wait_queue(&ic
->endio_wait
, &wait
);
1168 __set_current_state(TASK_UNINTERRUPTIBLE
);
1169 spin_unlock_irq(&ic
->endio_wait
.lock
);
1171 spin_lock_irq(&ic
->endio_wait
.lock
);
1172 __remove_wait_queue(&ic
->endio_wait
, &wait
);
1175 static void autocommit_fn(struct timer_list
*t
)
1177 struct dm_integrity_c
*ic
= from_timer(ic
, t
, autocommit_timer
);
1179 if (likely(!dm_integrity_failed(ic
)))
1180 queue_work(ic
->commit_wq
, &ic
->commit_work
);
1183 static void schedule_autocommit(struct dm_integrity_c
*ic
)
1185 if (!timer_pending(&ic
->autocommit_timer
))
1186 mod_timer(&ic
->autocommit_timer
, jiffies
+ ic
->autocommit_jiffies
);
1189 static void submit_flush_bio(struct dm_integrity_c
*ic
, struct dm_integrity_io
*dio
)
1192 unsigned long flags
;
1194 spin_lock_irqsave(&ic
->endio_wait
.lock
, flags
);
1195 bio
= dm_bio_from_per_bio_data(dio
, sizeof(struct dm_integrity_io
));
1196 bio_list_add(&ic
->flush_bio_list
, bio
);
1197 spin_unlock_irqrestore(&ic
->endio_wait
.lock
, flags
);
1199 queue_work(ic
->commit_wq
, &ic
->commit_work
);
1202 static void do_endio(struct dm_integrity_c
*ic
, struct bio
*bio
)
1204 int r
= dm_integrity_failed(ic
);
1205 if (unlikely(r
) && !bio
->bi_status
)
1206 bio
->bi_status
= errno_to_blk_status(r
);
1210 static void do_endio_flush(struct dm_integrity_c
*ic
, struct dm_integrity_io
*dio
)
1212 struct bio
*bio
= dm_bio_from_per_bio_data(dio
, sizeof(struct dm_integrity_io
));
1214 if (unlikely(dio
->fua
) && likely(!bio
->bi_status
) && likely(!dm_integrity_failed(ic
)))
1215 submit_flush_bio(ic
, dio
);
1220 static void dec_in_flight(struct dm_integrity_io
*dio
)
1222 if (atomic_dec_and_test(&dio
->in_flight
)) {
1223 struct dm_integrity_c
*ic
= dio
->ic
;
1226 remove_range(ic
, &dio
->range
);
1228 if (unlikely(dio
->write
))
1229 schedule_autocommit(ic
);
1231 bio
= dm_bio_from_per_bio_data(dio
, sizeof(struct dm_integrity_io
));
1233 if (unlikely(dio
->bi_status
) && !bio
->bi_status
)
1234 bio
->bi_status
= dio
->bi_status
;
1235 if (likely(!bio
->bi_status
) && unlikely(bio_sectors(bio
) != dio
->range
.n_sectors
)) {
1236 dio
->range
.logical_sector
+= dio
->range
.n_sectors
;
1237 bio_advance(bio
, dio
->range
.n_sectors
<< SECTOR_SHIFT
);
1238 INIT_WORK(&dio
->work
, integrity_bio_wait
);
1239 queue_work(ic
->offload_wq
, &dio
->work
);
1242 do_endio_flush(ic
, dio
);
1246 static void integrity_end_io(struct bio
*bio
)
1248 struct dm_integrity_io
*dio
= dm_per_bio_data(bio
, sizeof(struct dm_integrity_io
));
1250 dm_bio_restore(&dio
->bio_details
, bio
);
1251 if (bio
->bi_integrity
)
1252 bio
->bi_opf
|= REQ_INTEGRITY
;
1254 if (dio
->completion
)
1255 complete(dio
->completion
);
1260 static void integrity_sector_checksum(struct dm_integrity_c
*ic
, sector_t sector
,
1261 const char *data
, char *result
)
1263 __u64 sector_le
= cpu_to_le64(sector
);
1264 SHASH_DESC_ON_STACK(req
, ic
->internal_hash
);
1266 unsigned digest_size
;
1268 req
->tfm
= ic
->internal_hash
;
1271 r
= crypto_shash_init(req
);
1272 if (unlikely(r
< 0)) {
1273 dm_integrity_io_error(ic
, "crypto_shash_init", r
);
1277 r
= crypto_shash_update(req
, (const __u8
*)§or_le
, sizeof sector_le
);
1278 if (unlikely(r
< 0)) {
1279 dm_integrity_io_error(ic
, "crypto_shash_update", r
);
1283 r
= crypto_shash_update(req
, data
, ic
->sectors_per_block
<< SECTOR_SHIFT
);
1284 if (unlikely(r
< 0)) {
1285 dm_integrity_io_error(ic
, "crypto_shash_update", r
);
1289 r
= crypto_shash_final(req
, result
);
1290 if (unlikely(r
< 0)) {
1291 dm_integrity_io_error(ic
, "crypto_shash_final", r
);
1295 digest_size
= crypto_shash_digestsize(ic
->internal_hash
);
1296 if (unlikely(digest_size
< ic
->tag_size
))
1297 memset(result
+ digest_size
, 0, ic
->tag_size
- digest_size
);
1302 /* this shouldn't happen anyway, the hash functions have no reason to fail */
1303 get_random_bytes(result
, ic
->tag_size
);
1306 static void integrity_metadata(struct work_struct
*w
)
1308 struct dm_integrity_io
*dio
= container_of(w
, struct dm_integrity_io
, work
);
1309 struct dm_integrity_c
*ic
= dio
->ic
;
1313 if (ic
->internal_hash
) {
1314 struct bvec_iter iter
;
1316 unsigned digest_size
= crypto_shash_digestsize(ic
->internal_hash
);
1317 struct bio
*bio
= dm_bio_from_per_bio_data(dio
, sizeof(struct dm_integrity_io
));
1319 unsigned extra_space
= unlikely(digest_size
> ic
->tag_size
) ? digest_size
- ic
->tag_size
: 0;
1320 char checksums_onstack
[ic
->tag_size
+ extra_space
];
1321 unsigned sectors_to_process
= dio
->range
.n_sectors
;
1322 sector_t sector
= dio
->range
.logical_sector
;
1324 if (unlikely(ic
->mode
== 'R'))
1327 checksums
= kmalloc((PAGE_SIZE
>> SECTOR_SHIFT
>> ic
->sb
->log2_sectors_per_block
) * ic
->tag_size
+ extra_space
,
1328 GFP_NOIO
| __GFP_NORETRY
| __GFP_NOWARN
);
1330 checksums
= checksums_onstack
;
1332 __bio_for_each_segment(bv
, bio
, iter
, dio
->bio_details
.bi_iter
) {
1334 char *mem
, *checksums_ptr
;
1337 mem
= (char *)kmap_atomic(bv
.bv_page
) + bv
.bv_offset
;
1339 checksums_ptr
= checksums
;
1341 integrity_sector_checksum(ic
, sector
, mem
+ pos
, checksums_ptr
);
1342 checksums_ptr
+= ic
->tag_size
;
1343 sectors_to_process
-= ic
->sectors_per_block
;
1344 pos
+= ic
->sectors_per_block
<< SECTOR_SHIFT
;
1345 sector
+= ic
->sectors_per_block
;
1346 } while (pos
< bv
.bv_len
&& sectors_to_process
&& checksums
!= checksums_onstack
);
1349 r
= dm_integrity_rw_tag(ic
, checksums
, &dio
->metadata_block
, &dio
->metadata_offset
,
1350 checksums_ptr
- checksums
, !dio
->write
? TAG_CMP
: TAG_WRITE
);
1353 DMERR_LIMIT("Checksum failed at sector 0x%llx",
1354 (unsigned long long)(sector
- ((r
+ ic
->tag_size
- 1) / ic
->tag_size
)));
1356 atomic64_inc(&ic
->number_of_mismatches
);
1358 if (likely(checksums
!= checksums_onstack
))
1363 if (!sectors_to_process
)
1366 if (unlikely(pos
< bv
.bv_len
)) {
1367 bv
.bv_offset
+= pos
;
1373 if (likely(checksums
!= checksums_onstack
))
1376 struct bio_integrity_payload
*bip
= dio
->bio_details
.bi_integrity
;
1380 struct bvec_iter iter
;
1381 unsigned data_to_process
= dio
->range
.n_sectors
;
1382 sector_to_block(ic
, data_to_process
);
1383 data_to_process
*= ic
->tag_size
;
1385 bip_for_each_vec(biv
, bip
, iter
) {
1389 BUG_ON(PageHighMem(biv
.bv_page
));
1390 tag
= lowmem_page_address(biv
.bv_page
) + biv
.bv_offset
;
1391 this_len
= min(biv
.bv_len
, data_to_process
);
1392 r
= dm_integrity_rw_tag(ic
, tag
, &dio
->metadata_block
, &dio
->metadata_offset
,
1393 this_len
, !dio
->write
? TAG_READ
: TAG_WRITE
);
1396 data_to_process
-= this_len
;
1397 if (!data_to_process
)
1406 dio
->bi_status
= errno_to_blk_status(r
);
1410 static int dm_integrity_map(struct dm_target
*ti
, struct bio
*bio
)
1412 struct dm_integrity_c
*ic
= ti
->private;
1413 struct dm_integrity_io
*dio
= dm_per_bio_data(bio
, sizeof(struct dm_integrity_io
));
1414 struct bio_integrity_payload
*bip
;
1416 sector_t area
, offset
;
1421 if (unlikely(bio
->bi_opf
& REQ_PREFLUSH
)) {
1422 submit_flush_bio(ic
, dio
);
1423 return DM_MAPIO_SUBMITTED
;
1426 dio
->range
.logical_sector
= dm_target_offset(ti
, bio
->bi_iter
.bi_sector
);
1427 dio
->write
= bio_op(bio
) == REQ_OP_WRITE
;
1428 dio
->fua
= dio
->write
&& bio
->bi_opf
& REQ_FUA
;
1429 if (unlikely(dio
->fua
)) {
1431 * Don't pass down the FUA flag because we have to flush
1432 * disk cache anyway.
1434 bio
->bi_opf
&= ~REQ_FUA
;
1436 if (unlikely(dio
->range
.logical_sector
+ bio_sectors(bio
) > ic
->provided_data_sectors
)) {
1437 DMERR("Too big sector number: 0x%llx + 0x%x > 0x%llx",
1438 (unsigned long long)dio
->range
.logical_sector
, bio_sectors(bio
),
1439 (unsigned long long)ic
->provided_data_sectors
);
1440 return DM_MAPIO_KILL
;
1442 if (unlikely((dio
->range
.logical_sector
| bio_sectors(bio
)) & (unsigned)(ic
->sectors_per_block
- 1))) {
1443 DMERR("Bio not aligned on %u sectors: 0x%llx, 0x%x",
1444 ic
->sectors_per_block
,
1445 (unsigned long long)dio
->range
.logical_sector
, bio_sectors(bio
));
1446 return DM_MAPIO_KILL
;
1449 if (ic
->sectors_per_block
> 1) {
1450 struct bvec_iter iter
;
1452 bio_for_each_segment(bv
, bio
, iter
) {
1453 if (unlikely(bv
.bv_len
& ((ic
->sectors_per_block
<< SECTOR_SHIFT
) - 1))) {
1454 DMERR("Bio vector (%u,%u) is not aligned on %u-sector boundary",
1455 bv
.bv_offset
, bv
.bv_len
, ic
->sectors_per_block
);
1456 return DM_MAPIO_KILL
;
1461 bip
= bio_integrity(bio
);
1462 if (!ic
->internal_hash
) {
1464 unsigned wanted_tag_size
= bio_sectors(bio
) >> ic
->sb
->log2_sectors_per_block
;
1465 if (ic
->log2_tag_size
>= 0)
1466 wanted_tag_size
<<= ic
->log2_tag_size
;
1468 wanted_tag_size
*= ic
->tag_size
;
1469 if (unlikely(wanted_tag_size
!= bip
->bip_iter
.bi_size
)) {
1470 DMERR("Invalid integrity data size %u, expected %u", bip
->bip_iter
.bi_size
, wanted_tag_size
);
1471 return DM_MAPIO_KILL
;
1475 if (unlikely(bip
!= NULL
)) {
1476 DMERR("Unexpected integrity data when using internal hash");
1477 return DM_MAPIO_KILL
;
1481 if (unlikely(ic
->mode
== 'R') && unlikely(dio
->write
))
1482 return DM_MAPIO_KILL
;
1484 get_area_and_offset(ic
, dio
->range
.logical_sector
, &area
, &offset
);
1485 dio
->metadata_block
= get_metadata_sector_and_offset(ic
, area
, offset
, &dio
->metadata_offset
);
1486 bio
->bi_iter
.bi_sector
= get_data_sector(ic
, area
, offset
);
1488 dm_integrity_map_continue(dio
, true);
1489 return DM_MAPIO_SUBMITTED
;
1492 static bool __journal_read_write(struct dm_integrity_io
*dio
, struct bio
*bio
,
1493 unsigned journal_section
, unsigned journal_entry
)
1495 struct dm_integrity_c
*ic
= dio
->ic
;
1496 sector_t logical_sector
;
1499 logical_sector
= dio
->range
.logical_sector
;
1500 n_sectors
= dio
->range
.n_sectors
;
1502 struct bio_vec bv
= bio_iovec(bio
);
1505 if (unlikely(bv
.bv_len
>> SECTOR_SHIFT
> n_sectors
))
1506 bv
.bv_len
= n_sectors
<< SECTOR_SHIFT
;
1507 n_sectors
-= bv
.bv_len
>> SECTOR_SHIFT
;
1508 bio_advance_iter(bio
, &bio
->bi_iter
, bv
.bv_len
);
1510 mem
= kmap_atomic(bv
.bv_page
);
1511 if (likely(dio
->write
))
1512 flush_dcache_page(bv
.bv_page
);
1515 struct journal_entry
*je
= access_journal_entry(ic
, journal_section
, journal_entry
);
1517 if (unlikely(!dio
->write
)) {
1518 struct journal_sector
*js
;
1522 if (unlikely(journal_entry_is_inprogress(je
))) {
1523 flush_dcache_page(bv
.bv_page
);
1526 __io_wait_event(ic
->copy_to_journal_wait
, !journal_entry_is_inprogress(je
));
1530 BUG_ON(journal_entry_get_sector(je
) != logical_sector
);
1531 js
= access_journal_data(ic
, journal_section
, journal_entry
);
1532 mem_ptr
= mem
+ bv
.bv_offset
;
1535 memcpy(mem_ptr
, js
, JOURNAL_SECTOR_DATA
);
1536 *(commit_id_t
*)(mem_ptr
+ JOURNAL_SECTOR_DATA
) = je
->last_bytes
[s
];
1538 mem_ptr
+= 1 << SECTOR_SHIFT
;
1539 } while (++s
< ic
->sectors_per_block
);
1540 #ifdef INTERNAL_VERIFY
1541 if (ic
->internal_hash
) {
1542 char checksums_onstack
[max(crypto_shash_digestsize(ic
->internal_hash
), ic
->tag_size
)];
1544 integrity_sector_checksum(ic
, logical_sector
, mem
+ bv
.bv_offset
, checksums_onstack
);
1545 if (unlikely(memcmp(checksums_onstack
, journal_entry_tag(ic
, je
), ic
->tag_size
))) {
1546 DMERR_LIMIT("Checksum failed when reading from journal, at sector 0x%llx",
1547 (unsigned long long)logical_sector
);
1553 if (!ic
->internal_hash
) {
1554 struct bio_integrity_payload
*bip
= bio_integrity(bio
);
1555 unsigned tag_todo
= ic
->tag_size
;
1556 char *tag_ptr
= journal_entry_tag(ic
, je
);
1559 struct bio_vec biv
= bvec_iter_bvec(bip
->bip_vec
, bip
->bip_iter
);
1560 unsigned tag_now
= min(biv
.bv_len
, tag_todo
);
1562 BUG_ON(PageHighMem(biv
.bv_page
));
1563 tag_addr
= lowmem_page_address(biv
.bv_page
) + biv
.bv_offset
;
1564 if (likely(dio
->write
))
1565 memcpy(tag_ptr
, tag_addr
, tag_now
);
1567 memcpy(tag_addr
, tag_ptr
, tag_now
);
1568 bvec_iter_advance(bip
->bip_vec
, &bip
->bip_iter
, tag_now
);
1570 tag_todo
-= tag_now
;
1571 } while (unlikely(tag_todo
)); else {
1572 if (likely(dio
->write
))
1573 memset(tag_ptr
, 0, tag_todo
);
1577 if (likely(dio
->write
)) {
1578 struct journal_sector
*js
;
1581 js
= access_journal_data(ic
, journal_section
, journal_entry
);
1582 memcpy(js
, mem
+ bv
.bv_offset
, ic
->sectors_per_block
<< SECTOR_SHIFT
);
1586 je
->last_bytes
[s
] = js
[s
].commit_id
;
1587 } while (++s
< ic
->sectors_per_block
);
1589 if (ic
->internal_hash
) {
1590 unsigned digest_size
= crypto_shash_digestsize(ic
->internal_hash
);
1591 if (unlikely(digest_size
> ic
->tag_size
)) {
1592 char checksums_onstack
[digest_size
];
1593 integrity_sector_checksum(ic
, logical_sector
, (char *)js
, checksums_onstack
);
1594 memcpy(journal_entry_tag(ic
, je
), checksums_onstack
, ic
->tag_size
);
1596 integrity_sector_checksum(ic
, logical_sector
, (char *)js
, journal_entry_tag(ic
, je
));
1599 journal_entry_set_sector(je
, logical_sector
);
1601 logical_sector
+= ic
->sectors_per_block
;
1604 if (unlikely(journal_entry
== ic
->journal_section_entries
)) {
1607 wraparound_section(ic
, &journal_section
);
1610 bv
.bv_offset
+= ic
->sectors_per_block
<< SECTOR_SHIFT
;
1611 } while (bv
.bv_len
-= ic
->sectors_per_block
<< SECTOR_SHIFT
);
1613 if (unlikely(!dio
->write
))
1614 flush_dcache_page(bv
.bv_page
);
1616 } while (n_sectors
);
1618 if (likely(dio
->write
)) {
1620 if (unlikely(waitqueue_active(&ic
->copy_to_journal_wait
)))
1621 wake_up(&ic
->copy_to_journal_wait
);
1622 if (READ_ONCE(ic
->free_sectors
) <= ic
->free_sectors_threshold
) {
1623 queue_work(ic
->commit_wq
, &ic
->commit_work
);
1625 schedule_autocommit(ic
);
1628 remove_range(ic
, &dio
->range
);
1631 if (unlikely(bio
->bi_iter
.bi_size
)) {
1632 sector_t area
, offset
;
1634 dio
->range
.logical_sector
= logical_sector
;
1635 get_area_and_offset(ic
, dio
->range
.logical_sector
, &area
, &offset
);
1636 dio
->metadata_block
= get_metadata_sector_and_offset(ic
, area
, offset
, &dio
->metadata_offset
);
1643 static void dm_integrity_map_continue(struct dm_integrity_io
*dio
, bool from_map
)
1645 struct dm_integrity_c
*ic
= dio
->ic
;
1646 struct bio
*bio
= dm_bio_from_per_bio_data(dio
, sizeof(struct dm_integrity_io
));
1647 unsigned journal_section
, journal_entry
;
1648 unsigned journal_read_pos
;
1649 struct completion read_comp
;
1650 bool need_sync_io
= ic
->internal_hash
&& !dio
->write
;
1652 if (need_sync_io
&& from_map
) {
1653 INIT_WORK(&dio
->work
, integrity_bio_wait
);
1654 queue_work(ic
->offload_wq
, &dio
->work
);
1659 spin_lock_irq(&ic
->endio_wait
.lock
);
1661 if (unlikely(dm_integrity_failed(ic
))) {
1662 spin_unlock_irq(&ic
->endio_wait
.lock
);
1666 dio
->range
.n_sectors
= bio_sectors(bio
);
1667 journal_read_pos
= NOT_FOUND
;
1668 if (likely(ic
->mode
== 'J')) {
1670 unsigned next_entry
, i
, pos
;
1671 unsigned ws
, we
, range_sectors
;
1673 dio
->range
.n_sectors
= min(dio
->range
.n_sectors
,
1674 ic
->free_sectors
<< ic
->sb
->log2_sectors_per_block
);
1675 if (unlikely(!dio
->range
.n_sectors
)) {
1677 goto offload_to_thread
;
1678 sleep_on_endio_wait(ic
);
1681 range_sectors
= dio
->range
.n_sectors
>> ic
->sb
->log2_sectors_per_block
;
1682 ic
->free_sectors
-= range_sectors
;
1683 journal_section
= ic
->free_section
;
1684 journal_entry
= ic
->free_section_entry
;
1686 next_entry
= ic
->free_section_entry
+ range_sectors
;
1687 ic
->free_section_entry
= next_entry
% ic
->journal_section_entries
;
1688 ic
->free_section
+= next_entry
/ ic
->journal_section_entries
;
1689 ic
->n_uncommitted_sections
+= next_entry
/ ic
->journal_section_entries
;
1690 wraparound_section(ic
, &ic
->free_section
);
1692 pos
= journal_section
* ic
->journal_section_entries
+ journal_entry
;
1693 ws
= journal_section
;
1697 struct journal_entry
*je
;
1699 add_journal_node(ic
, &ic
->journal_tree
[pos
], dio
->range
.logical_sector
+ i
);
1701 if (unlikely(pos
>= ic
->journal_entries
))
1704 je
= access_journal_entry(ic
, ws
, we
);
1705 BUG_ON(!journal_entry_is_unused(je
));
1706 journal_entry_set_inprogress(je
);
1708 if (unlikely(we
== ic
->journal_section_entries
)) {
1711 wraparound_section(ic
, &ws
);
1713 } while ((i
+= ic
->sectors_per_block
) < dio
->range
.n_sectors
);
1715 spin_unlock_irq(&ic
->endio_wait
.lock
);
1716 goto journal_read_write
;
1718 sector_t next_sector
;
1719 journal_read_pos
= find_journal_node(ic
, dio
->range
.logical_sector
, &next_sector
);
1720 if (likely(journal_read_pos
== NOT_FOUND
)) {
1721 if (unlikely(dio
->range
.n_sectors
> next_sector
- dio
->range
.logical_sector
))
1722 dio
->range
.n_sectors
= next_sector
- dio
->range
.logical_sector
;
1725 unsigned jp
= journal_read_pos
+ 1;
1726 for (i
= ic
->sectors_per_block
; i
< dio
->range
.n_sectors
; i
+= ic
->sectors_per_block
, jp
++) {
1727 if (!test_journal_node(ic
, jp
, dio
->range
.logical_sector
+ i
))
1730 dio
->range
.n_sectors
= i
;
1734 if (unlikely(!add_new_range(ic
, &dio
->range
, true))) {
1736 * We must not sleep in the request routine because it could
1737 * stall bios on current->bio_list.
1738 * So, we offload the bio to a workqueue if we have to sleep.
1742 spin_unlock_irq(&ic
->endio_wait
.lock
);
1743 INIT_WORK(&dio
->work
, integrity_bio_wait
);
1744 queue_work(ic
->wait_wq
, &dio
->work
);
1747 if (journal_read_pos
!= NOT_FOUND
)
1748 dio
->range
.n_sectors
= ic
->sectors_per_block
;
1749 wait_and_add_new_range(ic
, &dio
->range
);
1751 * wait_and_add_new_range drops the spinlock, so the journal
1752 * may have been changed arbitrarily. We need to recheck.
1753 * To simplify the code, we restrict I/O size to just one block.
1755 if (journal_read_pos
!= NOT_FOUND
) {
1756 sector_t next_sector
;
1757 unsigned new_pos
= find_journal_node(ic
, dio
->range
.logical_sector
, &next_sector
);
1758 if (unlikely(new_pos
!= journal_read_pos
)) {
1759 remove_range_unlocked(ic
, &dio
->range
);
1764 spin_unlock_irq(&ic
->endio_wait
.lock
);
1766 if (unlikely(journal_read_pos
!= NOT_FOUND
)) {
1767 journal_section
= journal_read_pos
/ ic
->journal_section_entries
;
1768 journal_entry
= journal_read_pos
% ic
->journal_section_entries
;
1769 goto journal_read_write
;
1772 dio
->in_flight
= (atomic_t
)ATOMIC_INIT(2);
1775 init_completion(&read_comp
);
1776 dio
->completion
= &read_comp
;
1778 dio
->completion
= NULL
;
1780 dm_bio_record(&dio
->bio_details
, bio
);
1781 bio_set_dev(bio
, ic
->dev
->bdev
);
1782 bio
->bi_integrity
= NULL
;
1783 bio
->bi_opf
&= ~REQ_INTEGRITY
;
1784 bio
->bi_end_io
= integrity_end_io
;
1785 bio
->bi_iter
.bi_size
= dio
->range
.n_sectors
<< SECTOR_SHIFT
;
1787 generic_make_request(bio
);
1790 wait_for_completion_io(&read_comp
);
1791 if (unlikely(ic
->recalc_wq
!= NULL
) &&
1792 ic
->sb
->flags
& cpu_to_le32(SB_FLAG_RECALCULATING
) &&
1793 dio
->range
.logical_sector
+ dio
->range
.n_sectors
> le64_to_cpu(ic
->sb
->recalc_sector
))
1795 if (likely(!bio
->bi_status
))
1796 integrity_metadata(&dio
->work
);
1802 INIT_WORK(&dio
->work
, integrity_metadata
);
1803 queue_work(ic
->metadata_wq
, &dio
->work
);
1809 if (unlikely(__journal_read_write(dio
, bio
, journal_section
, journal_entry
)))
1812 do_endio_flush(ic
, dio
);
1816 static void integrity_bio_wait(struct work_struct
*w
)
1818 struct dm_integrity_io
*dio
= container_of(w
, struct dm_integrity_io
, work
);
1820 dm_integrity_map_continue(dio
, false);
1823 static void pad_uncommitted(struct dm_integrity_c
*ic
)
1825 if (ic
->free_section_entry
) {
1826 ic
->free_sectors
-= ic
->journal_section_entries
- ic
->free_section_entry
;
1827 ic
->free_section_entry
= 0;
1829 wraparound_section(ic
, &ic
->free_section
);
1830 ic
->n_uncommitted_sections
++;
1832 WARN_ON(ic
->journal_sections
* ic
->journal_section_entries
!=
1833 (ic
->n_uncommitted_sections
+ ic
->n_committed_sections
) * ic
->journal_section_entries
+ ic
->free_sectors
);
1836 static void integrity_commit(struct work_struct
*w
)
1838 struct dm_integrity_c
*ic
= container_of(w
, struct dm_integrity_c
, commit_work
);
1839 unsigned commit_start
, commit_sections
;
1841 struct bio
*flushes
;
1843 del_timer(&ic
->autocommit_timer
);
1845 spin_lock_irq(&ic
->endio_wait
.lock
);
1846 flushes
= bio_list_get(&ic
->flush_bio_list
);
1847 if (unlikely(ic
->mode
!= 'J')) {
1848 spin_unlock_irq(&ic
->endio_wait
.lock
);
1849 dm_integrity_flush_buffers(ic
);
1850 goto release_flush_bios
;
1853 pad_uncommitted(ic
);
1854 commit_start
= ic
->uncommitted_section
;
1855 commit_sections
= ic
->n_uncommitted_sections
;
1856 spin_unlock_irq(&ic
->endio_wait
.lock
);
1858 if (!commit_sections
)
1859 goto release_flush_bios
;
1862 for (n
= 0; n
< commit_sections
; n
++) {
1863 for (j
= 0; j
< ic
->journal_section_entries
; j
++) {
1864 struct journal_entry
*je
;
1865 je
= access_journal_entry(ic
, i
, j
);
1866 io_wait_event(ic
->copy_to_journal_wait
, !journal_entry_is_inprogress(je
));
1868 for (j
= 0; j
< ic
->journal_section_sectors
; j
++) {
1869 struct journal_sector
*js
;
1870 js
= access_journal(ic
, i
, j
);
1871 js
->commit_id
= dm_integrity_commit_id(ic
, i
, j
, ic
->commit_seq
);
1874 if (unlikely(i
>= ic
->journal_sections
))
1875 ic
->commit_seq
= next_commit_seq(ic
->commit_seq
);
1876 wraparound_section(ic
, &i
);
1880 write_journal(ic
, commit_start
, commit_sections
);
1882 spin_lock_irq(&ic
->endio_wait
.lock
);
1883 ic
->uncommitted_section
+= commit_sections
;
1884 wraparound_section(ic
, &ic
->uncommitted_section
);
1885 ic
->n_uncommitted_sections
-= commit_sections
;
1886 ic
->n_committed_sections
+= commit_sections
;
1887 spin_unlock_irq(&ic
->endio_wait
.lock
);
1889 if (READ_ONCE(ic
->free_sectors
) <= ic
->free_sectors_threshold
)
1890 queue_work(ic
->writer_wq
, &ic
->writer_work
);
1894 struct bio
*next
= flushes
->bi_next
;
1895 flushes
->bi_next
= NULL
;
1896 do_endio(ic
, flushes
);
1901 static void complete_copy_from_journal(unsigned long error
, void *context
)
1903 struct journal_io
*io
= context
;
1904 struct journal_completion
*comp
= io
->comp
;
1905 struct dm_integrity_c
*ic
= comp
->ic
;
1906 remove_range(ic
, &io
->range
);
1907 mempool_free(io
, &ic
->journal_io_mempool
);
1908 if (unlikely(error
!= 0))
1909 dm_integrity_io_error(ic
, "copying from journal", -EIO
);
1910 complete_journal_op(comp
);
1913 static void restore_last_bytes(struct dm_integrity_c
*ic
, struct journal_sector
*js
,
1914 struct journal_entry
*je
)
1918 js
->commit_id
= je
->last_bytes
[s
];
1920 } while (++s
< ic
->sectors_per_block
);
1923 static void do_journal_write(struct dm_integrity_c
*ic
, unsigned write_start
,
1924 unsigned write_sections
, bool from_replay
)
1927 struct journal_completion comp
;
1928 struct blk_plug plug
;
1930 blk_start_plug(&plug
);
1933 comp
.in_flight
= (atomic_t
)ATOMIC_INIT(1);
1934 init_completion(&comp
.comp
);
1937 for (n
= 0; n
< write_sections
; n
++, i
++, wraparound_section(ic
, &i
)) {
1938 #ifndef INTERNAL_VERIFY
1939 if (unlikely(from_replay
))
1941 rw_section_mac(ic
, i
, false);
1942 for (j
= 0; j
< ic
->journal_section_entries
; j
++) {
1943 struct journal_entry
*je
= access_journal_entry(ic
, i
, j
);
1944 sector_t sec
, area
, offset
;
1945 unsigned k
, l
, next_loop
;
1946 sector_t metadata_block
;
1947 unsigned metadata_offset
;
1948 struct journal_io
*io
;
1950 if (journal_entry_is_unused(je
))
1952 BUG_ON(unlikely(journal_entry_is_inprogress(je
)) && !from_replay
);
1953 sec
= journal_entry_get_sector(je
);
1954 if (unlikely(from_replay
)) {
1955 if (unlikely(sec
& (unsigned)(ic
->sectors_per_block
- 1))) {
1956 dm_integrity_io_error(ic
, "invalid sector in journal", -EIO
);
1957 sec
&= ~(sector_t
)(ic
->sectors_per_block
- 1);
1960 get_area_and_offset(ic
, sec
, &area
, &offset
);
1961 restore_last_bytes(ic
, access_journal_data(ic
, i
, j
), je
);
1962 for (k
= j
+ 1; k
< ic
->journal_section_entries
; k
++) {
1963 struct journal_entry
*je2
= access_journal_entry(ic
, i
, k
);
1964 sector_t sec2
, area2
, offset2
;
1965 if (journal_entry_is_unused(je2
))
1967 BUG_ON(unlikely(journal_entry_is_inprogress(je2
)) && !from_replay
);
1968 sec2
= journal_entry_get_sector(je2
);
1969 get_area_and_offset(ic
, sec2
, &area2
, &offset2
);
1970 if (area2
!= area
|| offset2
!= offset
+ ((k
- j
) << ic
->sb
->log2_sectors_per_block
))
1972 restore_last_bytes(ic
, access_journal_data(ic
, i
, k
), je2
);
1976 io
= mempool_alloc(&ic
->journal_io_mempool
, GFP_NOIO
);
1978 io
->range
.logical_sector
= sec
;
1979 io
->range
.n_sectors
= (k
- j
) << ic
->sb
->log2_sectors_per_block
;
1981 spin_lock_irq(&ic
->endio_wait
.lock
);
1982 if (unlikely(!add_new_range(ic
, &io
->range
, true)))
1983 wait_and_add_new_range(ic
, &io
->range
);
1985 if (likely(!from_replay
)) {
1986 struct journal_node
*section_node
= &ic
->journal_tree
[i
* ic
->journal_section_entries
];
1988 /* don't write if there is newer committed sector */
1989 while (j
< k
&& find_newer_committed_node(ic
, §ion_node
[j
])) {
1990 struct journal_entry
*je2
= access_journal_entry(ic
, i
, j
);
1992 journal_entry_set_unused(je2
);
1993 remove_journal_node(ic
, §ion_node
[j
]);
1995 sec
+= ic
->sectors_per_block
;
1996 offset
+= ic
->sectors_per_block
;
1998 while (j
< k
&& find_newer_committed_node(ic
, §ion_node
[k
- 1])) {
1999 struct journal_entry
*je2
= access_journal_entry(ic
, i
, k
- 1);
2001 journal_entry_set_unused(je2
);
2002 remove_journal_node(ic
, §ion_node
[k
- 1]);
2006 remove_range_unlocked(ic
, &io
->range
);
2007 spin_unlock_irq(&ic
->endio_wait
.lock
);
2008 mempool_free(io
, &ic
->journal_io_mempool
);
2011 for (l
= j
; l
< k
; l
++) {
2012 remove_journal_node(ic
, §ion_node
[l
]);
2015 spin_unlock_irq(&ic
->endio_wait
.lock
);
2017 metadata_block
= get_metadata_sector_and_offset(ic
, area
, offset
, &metadata_offset
);
2018 for (l
= j
; l
< k
; l
++) {
2020 struct journal_entry
*je2
= access_journal_entry(ic
, i
, l
);
2023 #ifndef INTERNAL_VERIFY
2024 unlikely(from_replay
) &&
2026 ic
->internal_hash
) {
2027 char test_tag
[max(crypto_shash_digestsize(ic
->internal_hash
), ic
->tag_size
)];
2029 integrity_sector_checksum(ic
, sec
+ ((l
- j
) << ic
->sb
->log2_sectors_per_block
),
2030 (char *)access_journal_data(ic
, i
, l
), test_tag
);
2031 if (unlikely(memcmp(test_tag
, journal_entry_tag(ic
, je2
), ic
->tag_size
)))
2032 dm_integrity_io_error(ic
, "tag mismatch when replaying journal", -EILSEQ
);
2035 journal_entry_set_unused(je2
);
2036 r
= dm_integrity_rw_tag(ic
, journal_entry_tag(ic
, je2
), &metadata_block
, &metadata_offset
,
2037 ic
->tag_size
, TAG_WRITE
);
2039 dm_integrity_io_error(ic
, "reading tags", r
);
2043 atomic_inc(&comp
.in_flight
);
2044 copy_from_journal(ic
, i
, j
<< ic
->sb
->log2_sectors_per_block
,
2045 (k
- j
) << ic
->sb
->log2_sectors_per_block
,
2046 get_data_sector(ic
, area
, offset
),
2047 complete_copy_from_journal
, io
);
2053 dm_bufio_write_dirty_buffers_async(ic
->bufio
);
2055 blk_finish_plug(&plug
);
2057 complete_journal_op(&comp
);
2058 wait_for_completion_io(&comp
.comp
);
2060 dm_integrity_flush_buffers(ic
);
2063 static void integrity_writer(struct work_struct
*w
)
2065 struct dm_integrity_c
*ic
= container_of(w
, struct dm_integrity_c
, writer_work
);
2066 unsigned write_start
, write_sections
;
2068 unsigned prev_free_sectors
;
2070 /* the following test is not needed, but it tests the replay code */
2071 if (unlikely(dm_suspended(ic
->ti
)) && !ic
->meta_dev
)
2074 spin_lock_irq(&ic
->endio_wait
.lock
);
2075 write_start
= ic
->committed_section
;
2076 write_sections
= ic
->n_committed_sections
;
2077 spin_unlock_irq(&ic
->endio_wait
.lock
);
2079 if (!write_sections
)
2082 do_journal_write(ic
, write_start
, write_sections
, false);
2084 spin_lock_irq(&ic
->endio_wait
.lock
);
2086 ic
->committed_section
+= write_sections
;
2087 wraparound_section(ic
, &ic
->committed_section
);
2088 ic
->n_committed_sections
-= write_sections
;
2090 prev_free_sectors
= ic
->free_sectors
;
2091 ic
->free_sectors
+= write_sections
* ic
->journal_section_entries
;
2092 if (unlikely(!prev_free_sectors
))
2093 wake_up_locked(&ic
->endio_wait
);
2095 spin_unlock_irq(&ic
->endio_wait
.lock
);
2098 static void recalc_write_super(struct dm_integrity_c
*ic
)
2102 dm_integrity_flush_buffers(ic
);
2103 if (dm_integrity_failed(ic
))
2107 r
= sync_rw_sb(ic
, REQ_OP_WRITE
, 0);
2109 dm_integrity_io_error(ic
, "writing superblock", r
);
2112 static void integrity_recalc(struct work_struct
*w
)
2114 struct dm_integrity_c
*ic
= container_of(w
, struct dm_integrity_c
, recalc_work
);
2115 struct dm_integrity_range range
;
2116 struct dm_io_request io_req
;
2117 struct dm_io_region io_loc
;
2118 sector_t area
, offset
;
2119 sector_t metadata_block
;
2120 unsigned metadata_offset
;
2124 unsigned super_counter
= 0;
2126 spin_lock_irq(&ic
->endio_wait
.lock
);
2130 if (unlikely(dm_suspended(ic
->ti
)))
2133 range
.logical_sector
= le64_to_cpu(ic
->sb
->recalc_sector
);
2134 if (unlikely(range
.logical_sector
>= ic
->provided_data_sectors
))
2137 get_area_and_offset(ic
, range
.logical_sector
, &area
, &offset
);
2138 range
.n_sectors
= min((sector_t
)RECALC_SECTORS
, ic
->provided_data_sectors
- range
.logical_sector
);
2140 range
.n_sectors
= min(range
.n_sectors
, (1U << ic
->sb
->log2_interleave_sectors
) - (unsigned)offset
);
2142 if (unlikely(!add_new_range(ic
, &range
, true)))
2143 wait_and_add_new_range(ic
, &range
);
2145 spin_unlock_irq(&ic
->endio_wait
.lock
);
2147 if (unlikely(++super_counter
== RECALC_WRITE_SUPER
)) {
2148 recalc_write_super(ic
);
2152 if (unlikely(dm_integrity_failed(ic
)))
2155 io_req
.bi_op
= REQ_OP_READ
;
2156 io_req
.bi_op_flags
= 0;
2157 io_req
.mem
.type
= DM_IO_VMA
;
2158 io_req
.mem
.ptr
.addr
= ic
->recalc_buffer
;
2159 io_req
.notify
.fn
= NULL
;
2160 io_req
.client
= ic
->io
;
2161 io_loc
.bdev
= ic
->dev
->bdev
;
2162 io_loc
.sector
= get_data_sector(ic
, area
, offset
);
2163 io_loc
.count
= range
.n_sectors
;
2165 r
= dm_io(&io_req
, 1, &io_loc
, NULL
);
2167 dm_integrity_io_error(ic
, "reading data", r
);
2171 t
= ic
->recalc_tags
;
2172 for (i
= 0; i
< range
.n_sectors
; i
+= ic
->sectors_per_block
) {
2173 integrity_sector_checksum(ic
, range
.logical_sector
+ i
, ic
->recalc_buffer
+ (i
<< SECTOR_SHIFT
), t
);
2177 metadata_block
= get_metadata_sector_and_offset(ic
, area
, offset
, &metadata_offset
);
2179 r
= dm_integrity_rw_tag(ic
, ic
->recalc_tags
, &metadata_block
, &metadata_offset
, t
- ic
->recalc_tags
, TAG_WRITE
);
2181 dm_integrity_io_error(ic
, "writing tags", r
);
2185 spin_lock_irq(&ic
->endio_wait
.lock
);
2186 remove_range_unlocked(ic
, &range
);
2187 ic
->sb
->recalc_sector
= cpu_to_le64(range
.logical_sector
+ range
.n_sectors
);
2191 remove_range(ic
, &range
);
2195 spin_unlock_irq(&ic
->endio_wait
.lock
);
2197 recalc_write_super(ic
);
2200 static void init_journal(struct dm_integrity_c
*ic
, unsigned start_section
,
2201 unsigned n_sections
, unsigned char commit_seq
)
2208 for (n
= 0; n
< n_sections
; n
++) {
2209 i
= start_section
+ n
;
2210 wraparound_section(ic
, &i
);
2211 for (j
= 0; j
< ic
->journal_section_sectors
; j
++) {
2212 struct journal_sector
*js
= access_journal(ic
, i
, j
);
2213 memset(&js
->entries
, 0, JOURNAL_SECTOR_DATA
);
2214 js
->commit_id
= dm_integrity_commit_id(ic
, i
, j
, commit_seq
);
2216 for (j
= 0; j
< ic
->journal_section_entries
; j
++) {
2217 struct journal_entry
*je
= access_journal_entry(ic
, i
, j
);
2218 journal_entry_set_unused(je
);
2222 write_journal(ic
, start_section
, n_sections
);
2225 static int find_commit_seq(struct dm_integrity_c
*ic
, unsigned i
, unsigned j
, commit_id_t id
)
2228 for (k
= 0; k
< N_COMMIT_IDS
; k
++) {
2229 if (dm_integrity_commit_id(ic
, i
, j
, k
) == id
)
2232 dm_integrity_io_error(ic
, "journal commit id", -EIO
);
2236 static void replay_journal(struct dm_integrity_c
*ic
)
2239 bool used_commit_ids
[N_COMMIT_IDS
];
2240 unsigned max_commit_id_sections
[N_COMMIT_IDS
];
2241 unsigned write_start
, write_sections
;
2242 unsigned continue_section
;
2244 unsigned char unused
, last_used
, want_commit_seq
;
2246 if (ic
->mode
== 'R')
2249 if (ic
->journal_uptodate
)
2255 if (!ic
->just_formatted
) {
2256 DEBUG_print("reading journal\n");
2257 rw_journal(ic
, REQ_OP_READ
, 0, 0, ic
->journal_sections
, NULL
);
2259 DEBUG_bytes(lowmem_page_address(ic
->journal_io
[0].page
), 64, "read journal");
2260 if (ic
->journal_io
) {
2261 struct journal_completion crypt_comp
;
2263 init_completion(&crypt_comp
.comp
);
2264 crypt_comp
.in_flight
= (atomic_t
)ATOMIC_INIT(0);
2265 encrypt_journal(ic
, false, 0, ic
->journal_sections
, &crypt_comp
);
2266 wait_for_completion(&crypt_comp
.comp
);
2268 DEBUG_bytes(lowmem_page_address(ic
->journal
[0].page
), 64, "decrypted journal");
2271 if (dm_integrity_failed(ic
))
2274 journal_empty
= true;
2275 memset(used_commit_ids
, 0, sizeof used_commit_ids
);
2276 memset(max_commit_id_sections
, 0, sizeof max_commit_id_sections
);
2277 for (i
= 0; i
< ic
->journal_sections
; i
++) {
2278 for (j
= 0; j
< ic
->journal_section_sectors
; j
++) {
2280 struct journal_sector
*js
= access_journal(ic
, i
, j
);
2281 k
= find_commit_seq(ic
, i
, j
, js
->commit_id
);
2284 used_commit_ids
[k
] = true;
2285 max_commit_id_sections
[k
] = i
;
2287 if (journal_empty
) {
2288 for (j
= 0; j
< ic
->journal_section_entries
; j
++) {
2289 struct journal_entry
*je
= access_journal_entry(ic
, i
, j
);
2290 if (!journal_entry_is_unused(je
)) {
2291 journal_empty
= false;
2298 if (!used_commit_ids
[N_COMMIT_IDS
- 1]) {
2299 unused
= N_COMMIT_IDS
- 1;
2300 while (unused
&& !used_commit_ids
[unused
- 1])
2303 for (unused
= 0; unused
< N_COMMIT_IDS
; unused
++)
2304 if (!used_commit_ids
[unused
])
2306 if (unused
== N_COMMIT_IDS
) {
2307 dm_integrity_io_error(ic
, "journal commit ids", -EIO
);
2311 DEBUG_print("first unused commit seq %d [%d,%d,%d,%d]\n",
2312 unused
, used_commit_ids
[0], used_commit_ids
[1],
2313 used_commit_ids
[2], used_commit_ids
[3]);
2315 last_used
= prev_commit_seq(unused
);
2316 want_commit_seq
= prev_commit_seq(last_used
);
2318 if (!used_commit_ids
[want_commit_seq
] && used_commit_ids
[prev_commit_seq(want_commit_seq
)])
2319 journal_empty
= true;
2321 write_start
= max_commit_id_sections
[last_used
] + 1;
2322 if (unlikely(write_start
>= ic
->journal_sections
))
2323 want_commit_seq
= next_commit_seq(want_commit_seq
);
2324 wraparound_section(ic
, &write_start
);
2327 for (write_sections
= 0; write_sections
< ic
->journal_sections
; write_sections
++) {
2328 for (j
= 0; j
< ic
->journal_section_sectors
; j
++) {
2329 struct journal_sector
*js
= access_journal(ic
, i
, j
);
2331 if (js
->commit_id
!= dm_integrity_commit_id(ic
, i
, j
, want_commit_seq
)) {
2333 * This could be caused by crash during writing.
2334 * We won't replay the inconsistent part of the
2337 DEBUG_print("commit id mismatch at position (%u, %u): %d != %d\n",
2338 i
, j
, find_commit_seq(ic
, i
, j
, js
->commit_id
), want_commit_seq
);
2343 if (unlikely(i
>= ic
->journal_sections
))
2344 want_commit_seq
= next_commit_seq(want_commit_seq
);
2345 wraparound_section(ic
, &i
);
2349 if (!journal_empty
) {
2350 DEBUG_print("replaying %u sections, starting at %u, commit seq %d\n",
2351 write_sections
, write_start
, want_commit_seq
);
2352 do_journal_write(ic
, write_start
, write_sections
, true);
2355 if (write_sections
== ic
->journal_sections
&& (ic
->mode
== 'J' || journal_empty
)) {
2356 continue_section
= write_start
;
2357 ic
->commit_seq
= want_commit_seq
;
2358 DEBUG_print("continuing from section %u, commit seq %d\n", write_start
, ic
->commit_seq
);
2361 unsigned char erase_seq
;
2363 DEBUG_print("clearing journal\n");
2365 erase_seq
= prev_commit_seq(prev_commit_seq(last_used
));
2367 init_journal(ic
, s
, 1, erase_seq
);
2369 wraparound_section(ic
, &s
);
2370 if (ic
->journal_sections
>= 2) {
2371 init_journal(ic
, s
, ic
->journal_sections
- 2, erase_seq
);
2372 s
+= ic
->journal_sections
- 2;
2373 wraparound_section(ic
, &s
);
2374 init_journal(ic
, s
, 1, erase_seq
);
2377 continue_section
= 0;
2378 ic
->commit_seq
= next_commit_seq(erase_seq
);
2381 ic
->committed_section
= continue_section
;
2382 ic
->n_committed_sections
= 0;
2384 ic
->uncommitted_section
= continue_section
;
2385 ic
->n_uncommitted_sections
= 0;
2387 ic
->free_section
= continue_section
;
2388 ic
->free_section_entry
= 0;
2389 ic
->free_sectors
= ic
->journal_entries
;
2391 ic
->journal_tree_root
= RB_ROOT
;
2392 for (i
= 0; i
< ic
->journal_entries
; i
++)
2393 init_journal_node(&ic
->journal_tree
[i
]);
2396 static void dm_integrity_postsuspend(struct dm_target
*ti
)
2398 struct dm_integrity_c
*ic
= (struct dm_integrity_c
*)ti
->private;
2400 del_timer_sync(&ic
->autocommit_timer
);
2403 drain_workqueue(ic
->recalc_wq
);
2405 queue_work(ic
->commit_wq
, &ic
->commit_work
);
2406 drain_workqueue(ic
->commit_wq
);
2408 if (ic
->mode
== 'J') {
2410 queue_work(ic
->writer_wq
, &ic
->writer_work
);
2411 drain_workqueue(ic
->writer_wq
);
2412 dm_integrity_flush_buffers(ic
);
2415 BUG_ON(!RB_EMPTY_ROOT(&ic
->in_progress
));
2417 ic
->journal_uptodate
= true;
2420 static void dm_integrity_resume(struct dm_target
*ti
)
2422 struct dm_integrity_c
*ic
= (struct dm_integrity_c
*)ti
->private;
2426 if (ic
->recalc_wq
&& ic
->sb
->flags
& cpu_to_le32(SB_FLAG_RECALCULATING
)) {
2427 __u64 recalc_pos
= le64_to_cpu(ic
->sb
->recalc_sector
);
2428 if (recalc_pos
< ic
->provided_data_sectors
) {
2429 queue_work(ic
->recalc_wq
, &ic
->recalc_work
);
2430 } else if (recalc_pos
> ic
->provided_data_sectors
) {
2431 ic
->sb
->recalc_sector
= cpu_to_le64(ic
->provided_data_sectors
);
2432 recalc_write_super(ic
);
2437 static void dm_integrity_status(struct dm_target
*ti
, status_type_t type
,
2438 unsigned status_flags
, char *result
, unsigned maxlen
)
2440 struct dm_integrity_c
*ic
= (struct dm_integrity_c
*)ti
->private;
2445 case STATUSTYPE_INFO
:
2447 (unsigned long long)atomic64_read(&ic
->number_of_mismatches
),
2448 (unsigned long long)ic
->provided_data_sectors
);
2449 if (ic
->sb
->flags
& cpu_to_le32(SB_FLAG_RECALCULATING
))
2450 DMEMIT(" %llu", (unsigned long long)le64_to_cpu(ic
->sb
->recalc_sector
));
2455 case STATUSTYPE_TABLE
: {
2456 __u64 watermark_percentage
= (__u64
)(ic
->journal_entries
- ic
->free_sectors_threshold
) * 100;
2457 watermark_percentage
+= ic
->journal_entries
/ 2;
2458 do_div(watermark_percentage
, ic
->journal_entries
);
2460 arg_count
+= !!ic
->meta_dev
;
2461 arg_count
+= ic
->sectors_per_block
!= 1;
2462 arg_count
+= !!(ic
->sb
->flags
& cpu_to_le32(SB_FLAG_RECALCULATING
));
2463 arg_count
+= !!ic
->internal_hash_alg
.alg_string
;
2464 arg_count
+= !!ic
->journal_crypt_alg
.alg_string
;
2465 arg_count
+= !!ic
->journal_mac_alg
.alg_string
;
2466 DMEMIT("%s %llu %u %c %u", ic
->dev
->name
, (unsigned long long)ic
->start
,
2467 ic
->tag_size
, ic
->mode
, arg_count
);
2469 DMEMIT(" meta_device:%s", ic
->meta_dev
->name
);
2470 if (ic
->sectors_per_block
!= 1)
2471 DMEMIT(" block_size:%u", ic
->sectors_per_block
<< SECTOR_SHIFT
);
2472 if (ic
->sb
->flags
& cpu_to_le32(SB_FLAG_RECALCULATING
))
2473 DMEMIT(" recalculate");
2474 DMEMIT(" journal_sectors:%u", ic
->initial_sectors
- SB_SECTORS
);
2475 DMEMIT(" interleave_sectors:%u", 1U << ic
->sb
->log2_interleave_sectors
);
2476 DMEMIT(" buffer_sectors:%u", 1U << ic
->log2_buffer_sectors
);
2477 DMEMIT(" journal_watermark:%u", (unsigned)watermark_percentage
);
2478 DMEMIT(" commit_time:%u", ic
->autocommit_msec
);
2480 #define EMIT_ALG(a, n) \
2482 if (ic->a.alg_string) { \
2483 DMEMIT(" %s:%s", n, ic->a.alg_string); \
2484 if (ic->a.key_string) \
2485 DMEMIT(":%s", ic->a.key_string);\
2488 EMIT_ALG(internal_hash_alg
, "internal_hash");
2489 EMIT_ALG(journal_crypt_alg
, "journal_crypt");
2490 EMIT_ALG(journal_mac_alg
, "journal_mac");
2496 static int dm_integrity_iterate_devices(struct dm_target
*ti
,
2497 iterate_devices_callout_fn fn
, void *data
)
2499 struct dm_integrity_c
*ic
= ti
->private;
2502 return fn(ti
, ic
->dev
, ic
->start
+ ic
->initial_sectors
+ ic
->metadata_run
, ti
->len
, data
);
2504 return fn(ti
, ic
->dev
, 0, ti
->len
, data
);
2507 static void dm_integrity_io_hints(struct dm_target
*ti
, struct queue_limits
*limits
)
2509 struct dm_integrity_c
*ic
= ti
->private;
2511 if (ic
->sectors_per_block
> 1) {
2512 limits
->logical_block_size
= ic
->sectors_per_block
<< SECTOR_SHIFT
;
2513 limits
->physical_block_size
= ic
->sectors_per_block
<< SECTOR_SHIFT
;
2514 blk_limits_io_min(limits
, ic
->sectors_per_block
<< SECTOR_SHIFT
);
2518 static void calculate_journal_section_size(struct dm_integrity_c
*ic
)
2520 unsigned sector_space
= JOURNAL_SECTOR_DATA
;
2522 ic
->journal_sections
= le32_to_cpu(ic
->sb
->journal_sections
);
2523 ic
->journal_entry_size
= roundup(offsetof(struct journal_entry
, last_bytes
[ic
->sectors_per_block
]) + ic
->tag_size
,
2524 JOURNAL_ENTRY_ROUNDUP
);
2526 if (ic
->sb
->flags
& cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC
))
2527 sector_space
-= JOURNAL_MAC_PER_SECTOR
;
2528 ic
->journal_entries_per_sector
= sector_space
/ ic
->journal_entry_size
;
2529 ic
->journal_section_entries
= ic
->journal_entries_per_sector
* JOURNAL_BLOCK_SECTORS
;
2530 ic
->journal_section_sectors
= (ic
->journal_section_entries
<< ic
->sb
->log2_sectors_per_block
) + JOURNAL_BLOCK_SECTORS
;
2531 ic
->journal_entries
= ic
->journal_section_entries
* ic
->journal_sections
;
2534 static int calculate_device_limits(struct dm_integrity_c
*ic
)
2536 __u64 initial_sectors
;
2538 calculate_journal_section_size(ic
);
2539 initial_sectors
= SB_SECTORS
+ (__u64
)ic
->journal_section_sectors
* ic
->journal_sections
;
2540 if (initial_sectors
+ METADATA_PADDING_SECTORS
>= ic
->meta_device_sectors
|| initial_sectors
> UINT_MAX
)
2542 ic
->initial_sectors
= initial_sectors
;
2544 if (!ic
->meta_dev
) {
2545 sector_t last_sector
, last_area
, last_offset
;
2547 ic
->metadata_run
= roundup((__u64
)ic
->tag_size
<< (ic
->sb
->log2_interleave_sectors
- ic
->sb
->log2_sectors_per_block
),
2548 (__u64
)(1 << SECTOR_SHIFT
<< METADATA_PADDING_SECTORS
)) >> SECTOR_SHIFT
;
2549 if (!(ic
->metadata_run
& (ic
->metadata_run
- 1)))
2550 ic
->log2_metadata_run
= __ffs(ic
->metadata_run
);
2552 ic
->log2_metadata_run
= -1;
2554 get_area_and_offset(ic
, ic
->provided_data_sectors
- 1, &last_area
, &last_offset
);
2555 last_sector
= get_data_sector(ic
, last_area
, last_offset
);
2556 if (last_sector
< ic
->start
|| last_sector
>= ic
->meta_device_sectors
)
2559 __u64 meta_size
= (ic
->provided_data_sectors
>> ic
->sb
->log2_sectors_per_block
) * ic
->tag_size
;
2560 meta_size
= (meta_size
+ ((1U << (ic
->log2_buffer_sectors
+ SECTOR_SHIFT
)) - 1))
2561 >> (ic
->log2_buffer_sectors
+ SECTOR_SHIFT
);
2562 meta_size
<<= ic
->log2_buffer_sectors
;
2563 if (ic
->initial_sectors
+ meta_size
< ic
->initial_sectors
||
2564 ic
->initial_sectors
+ meta_size
> ic
->meta_device_sectors
)
2566 ic
->metadata_run
= 1;
2567 ic
->log2_metadata_run
= 0;
2573 static int initialize_superblock(struct dm_integrity_c
*ic
, unsigned journal_sectors
, unsigned interleave_sectors
)
2575 unsigned journal_sections
;
2578 memset(ic
->sb
, 0, SB_SECTORS
<< SECTOR_SHIFT
);
2579 memcpy(ic
->sb
->magic
, SB_MAGIC
, 8);
2580 ic
->sb
->integrity_tag_size
= cpu_to_le16(ic
->tag_size
);
2581 ic
->sb
->log2_sectors_per_block
= __ffs(ic
->sectors_per_block
);
2582 if (ic
->journal_mac_alg
.alg_string
)
2583 ic
->sb
->flags
|= cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC
);
2585 calculate_journal_section_size(ic
);
2586 journal_sections
= journal_sectors
/ ic
->journal_section_sectors
;
2587 if (!journal_sections
)
2588 journal_sections
= 1;
2590 if (!ic
->meta_dev
) {
2591 ic
->sb
->journal_sections
= cpu_to_le32(journal_sections
);
2592 if (!interleave_sectors
)
2593 interleave_sectors
= DEFAULT_INTERLEAVE_SECTORS
;
2594 ic
->sb
->log2_interleave_sectors
= __fls(interleave_sectors
);
2595 ic
->sb
->log2_interleave_sectors
= max((__u8
)MIN_LOG2_INTERLEAVE_SECTORS
, ic
->sb
->log2_interleave_sectors
);
2596 ic
->sb
->log2_interleave_sectors
= min((__u8
)MAX_LOG2_INTERLEAVE_SECTORS
, ic
->sb
->log2_interleave_sectors
);
2598 ic
->provided_data_sectors
= 0;
2599 for (test_bit
= fls64(ic
->meta_device_sectors
) - 1; test_bit
>= 3; test_bit
--) {
2600 __u64 prev_data_sectors
= ic
->provided_data_sectors
;
2602 ic
->provided_data_sectors
|= (sector_t
)1 << test_bit
;
2603 if (calculate_device_limits(ic
))
2604 ic
->provided_data_sectors
= prev_data_sectors
;
2606 if (!ic
->provided_data_sectors
)
2609 ic
->sb
->log2_interleave_sectors
= 0;
2610 ic
->provided_data_sectors
= ic
->data_device_sectors
;
2611 ic
->provided_data_sectors
&= ~(sector_t
)(ic
->sectors_per_block
- 1);
2614 ic
->sb
->journal_sections
= cpu_to_le32(0);
2615 for (test_bit
= fls(journal_sections
) - 1; test_bit
>= 0; test_bit
--) {
2616 __u32 prev_journal_sections
= le32_to_cpu(ic
->sb
->journal_sections
);
2617 __u32 test_journal_sections
= prev_journal_sections
| (1U << test_bit
);
2618 if (test_journal_sections
> journal_sections
)
2620 ic
->sb
->journal_sections
= cpu_to_le32(test_journal_sections
);
2621 if (calculate_device_limits(ic
))
2622 ic
->sb
->journal_sections
= cpu_to_le32(prev_journal_sections
);
2625 if (!le32_to_cpu(ic
->sb
->journal_sections
)) {
2626 if (ic
->log2_buffer_sectors
> 3) {
2627 ic
->log2_buffer_sectors
--;
2628 goto try_smaller_buffer
;
2634 ic
->sb
->provided_data_sectors
= cpu_to_le64(ic
->provided_data_sectors
);
2641 static void dm_integrity_set(struct dm_target
*ti
, struct dm_integrity_c
*ic
)
2643 struct gendisk
*disk
= dm_disk(dm_table_get_md(ti
->table
));
2644 struct blk_integrity bi
;
2646 memset(&bi
, 0, sizeof(bi
));
2647 bi
.profile
= &dm_integrity_profile
;
2648 bi
.tuple_size
= ic
->tag_size
;
2649 bi
.tag_size
= bi
.tuple_size
;
2650 bi
.interval_exp
= ic
->sb
->log2_sectors_per_block
+ SECTOR_SHIFT
;
2652 blk_integrity_register(disk
, &bi
);
2653 blk_queue_max_integrity_segments(disk
->queue
, UINT_MAX
);
2656 static void dm_integrity_free_page_list(struct dm_integrity_c
*ic
, struct page_list
*pl
)
2662 for (i
= 0; i
< ic
->journal_pages
; i
++)
2664 __free_page(pl
[i
].page
);
2668 static struct page_list
*dm_integrity_alloc_page_list(struct dm_integrity_c
*ic
)
2670 size_t page_list_desc_size
= ic
->journal_pages
* sizeof(struct page_list
);
2671 struct page_list
*pl
;
2674 pl
= kvmalloc(page_list_desc_size
, GFP_KERNEL
| __GFP_ZERO
);
2678 for (i
= 0; i
< ic
->journal_pages
; i
++) {
2679 pl
[i
].page
= alloc_page(GFP_KERNEL
);
2681 dm_integrity_free_page_list(ic
, pl
);
2685 pl
[i
- 1].next
= &pl
[i
];
2691 static void dm_integrity_free_journal_scatterlist(struct dm_integrity_c
*ic
, struct scatterlist
**sl
)
2694 for (i
= 0; i
< ic
->journal_sections
; i
++)
2699 static struct scatterlist
**dm_integrity_alloc_journal_scatterlist(struct dm_integrity_c
*ic
, struct page_list
*pl
)
2701 struct scatterlist
**sl
;
2704 sl
= kvmalloc_array(ic
->journal_sections
,
2705 sizeof(struct scatterlist
*),
2706 GFP_KERNEL
| __GFP_ZERO
);
2710 for (i
= 0; i
< ic
->journal_sections
; i
++) {
2711 struct scatterlist
*s
;
2712 unsigned start_index
, start_offset
;
2713 unsigned end_index
, end_offset
;
2717 page_list_location(ic
, i
, 0, &start_index
, &start_offset
);
2718 page_list_location(ic
, i
, ic
->journal_section_sectors
- 1, &end_index
, &end_offset
);
2720 n_pages
= (end_index
- start_index
+ 1);
2722 s
= kvmalloc_array(n_pages
, sizeof(struct scatterlist
),
2725 dm_integrity_free_journal_scatterlist(ic
, sl
);
2729 sg_init_table(s
, n_pages
);
2730 for (idx
= start_index
; idx
<= end_index
; idx
++) {
2731 char *va
= lowmem_page_address(pl
[idx
].page
);
2732 unsigned start
= 0, end
= PAGE_SIZE
;
2733 if (idx
== start_index
)
2734 start
= start_offset
;
2735 if (idx
== end_index
)
2736 end
= end_offset
+ (1 << SECTOR_SHIFT
);
2737 sg_set_buf(&s
[idx
- start_index
], va
+ start
, end
- start
);
2746 static void free_alg(struct alg_spec
*a
)
2748 kzfree(a
->alg_string
);
2750 memset(a
, 0, sizeof *a
);
2753 static int get_alg_and_key(const char *arg
, struct alg_spec
*a
, char **error
, char *error_inval
)
2759 a
->alg_string
= kstrdup(strchr(arg
, ':') + 1, GFP_KERNEL
);
2763 k
= strchr(a
->alg_string
, ':');
2766 a
->key_string
= k
+ 1;
2767 if (strlen(a
->key_string
) & 1)
2770 a
->key_size
= strlen(a
->key_string
) / 2;
2771 a
->key
= kmalloc(a
->key_size
, GFP_KERNEL
);
2774 if (hex2bin(a
->key
, a
->key_string
, a
->key_size
))
2780 *error
= error_inval
;
2783 *error
= "Out of memory for an argument";
2787 static int get_mac(struct crypto_shash
**hash
, struct alg_spec
*a
, char **error
,
2788 char *error_alg
, char *error_key
)
2792 if (a
->alg_string
) {
2793 *hash
= crypto_alloc_shash(a
->alg_string
, 0, CRYPTO_ALG_ASYNC
);
2794 if (IS_ERR(*hash
)) {
2802 r
= crypto_shash_setkey(*hash
, a
->key
, a
->key_size
);
2807 } else if (crypto_shash_get_flags(*hash
) & CRYPTO_TFM_NEED_KEY
) {
2816 static int create_journal(struct dm_integrity_c
*ic
, char **error
)
2820 __u64 journal_pages
, journal_desc_size
, journal_tree_size
;
2821 unsigned char *crypt_data
= NULL
, *crypt_iv
= NULL
;
2822 struct skcipher_request
*req
= NULL
;
2824 ic
->commit_ids
[0] = cpu_to_le64(0x1111111111111111ULL
);
2825 ic
->commit_ids
[1] = cpu_to_le64(0x2222222222222222ULL
);
2826 ic
->commit_ids
[2] = cpu_to_le64(0x3333333333333333ULL
);
2827 ic
->commit_ids
[3] = cpu_to_le64(0x4444444444444444ULL
);
2829 journal_pages
= roundup((__u64
)ic
->journal_sections
* ic
->journal_section_sectors
,
2830 PAGE_SIZE
>> SECTOR_SHIFT
) >> (PAGE_SHIFT
- SECTOR_SHIFT
);
2831 journal_desc_size
= journal_pages
* sizeof(struct page_list
);
2832 if (journal_pages
>= totalram_pages
- totalhigh_pages
|| journal_desc_size
> ULONG_MAX
) {
2833 *error
= "Journal doesn't fit into memory";
2837 ic
->journal_pages
= journal_pages
;
2839 ic
->journal
= dm_integrity_alloc_page_list(ic
);
2841 *error
= "Could not allocate memory for journal";
2845 if (ic
->journal_crypt_alg
.alg_string
) {
2846 unsigned ivsize
, blocksize
;
2847 struct journal_completion comp
;
2850 ic
->journal_crypt
= crypto_alloc_skcipher(ic
->journal_crypt_alg
.alg_string
, 0, 0);
2851 if (IS_ERR(ic
->journal_crypt
)) {
2852 *error
= "Invalid journal cipher";
2853 r
= PTR_ERR(ic
->journal_crypt
);
2854 ic
->journal_crypt
= NULL
;
2857 ivsize
= crypto_skcipher_ivsize(ic
->journal_crypt
);
2858 blocksize
= crypto_skcipher_blocksize(ic
->journal_crypt
);
2860 if (ic
->journal_crypt_alg
.key
) {
2861 r
= crypto_skcipher_setkey(ic
->journal_crypt
, ic
->journal_crypt_alg
.key
,
2862 ic
->journal_crypt_alg
.key_size
);
2864 *error
= "Error setting encryption key";
2868 DEBUG_print("cipher %s, block size %u iv size %u\n",
2869 ic
->journal_crypt_alg
.alg_string
, blocksize
, ivsize
);
2871 ic
->journal_io
= dm_integrity_alloc_page_list(ic
);
2872 if (!ic
->journal_io
) {
2873 *error
= "Could not allocate memory for journal io";
2878 if (blocksize
== 1) {
2879 struct scatterlist
*sg
;
2881 req
= skcipher_request_alloc(ic
->journal_crypt
, GFP_KERNEL
);
2883 *error
= "Could not allocate crypt request";
2888 crypt_iv
= kmalloc(ivsize
, GFP_KERNEL
);
2890 *error
= "Could not allocate iv";
2895 ic
->journal_xor
= dm_integrity_alloc_page_list(ic
);
2896 if (!ic
->journal_xor
) {
2897 *error
= "Could not allocate memory for journal xor";
2902 sg
= kvmalloc_array(ic
->journal_pages
+ 1,
2903 sizeof(struct scatterlist
),
2906 *error
= "Unable to allocate sg list";
2910 sg_init_table(sg
, ic
->journal_pages
+ 1);
2911 for (i
= 0; i
< ic
->journal_pages
; i
++) {
2912 char *va
= lowmem_page_address(ic
->journal_xor
[i
].page
);
2914 sg_set_buf(&sg
[i
], va
, PAGE_SIZE
);
2916 sg_set_buf(&sg
[i
], &ic
->commit_ids
, sizeof ic
->commit_ids
);
2917 memset(crypt_iv
, 0x00, ivsize
);
2919 skcipher_request_set_crypt(req
, sg
, sg
, PAGE_SIZE
* ic
->journal_pages
+ sizeof ic
->commit_ids
, crypt_iv
);
2920 init_completion(&comp
.comp
);
2921 comp
.in_flight
= (atomic_t
)ATOMIC_INIT(1);
2922 if (do_crypt(true, req
, &comp
))
2923 wait_for_completion(&comp
.comp
);
2925 r
= dm_integrity_failed(ic
);
2927 *error
= "Unable to encrypt journal";
2930 DEBUG_bytes(lowmem_page_address(ic
->journal_xor
[0].page
), 64, "xor data");
2932 crypto_free_skcipher(ic
->journal_crypt
);
2933 ic
->journal_crypt
= NULL
;
2935 unsigned crypt_len
= roundup(ivsize
, blocksize
);
2937 req
= skcipher_request_alloc(ic
->journal_crypt
, GFP_KERNEL
);
2939 *error
= "Could not allocate crypt request";
2944 crypt_iv
= kmalloc(ivsize
, GFP_KERNEL
);
2946 *error
= "Could not allocate iv";
2951 crypt_data
= kmalloc(crypt_len
, GFP_KERNEL
);
2953 *error
= "Unable to allocate crypt data";
2958 ic
->journal_scatterlist
= dm_integrity_alloc_journal_scatterlist(ic
, ic
->journal
);
2959 if (!ic
->journal_scatterlist
) {
2960 *error
= "Unable to allocate sg list";
2964 ic
->journal_io_scatterlist
= dm_integrity_alloc_journal_scatterlist(ic
, ic
->journal_io
);
2965 if (!ic
->journal_io_scatterlist
) {
2966 *error
= "Unable to allocate sg list";
2970 ic
->sk_requests
= kvmalloc_array(ic
->journal_sections
,
2971 sizeof(struct skcipher_request
*),
2972 GFP_KERNEL
| __GFP_ZERO
);
2973 if (!ic
->sk_requests
) {
2974 *error
= "Unable to allocate sk requests";
2978 for (i
= 0; i
< ic
->journal_sections
; i
++) {
2979 struct scatterlist sg
;
2980 struct skcipher_request
*section_req
;
2981 __u32 section_le
= cpu_to_le32(i
);
2983 memset(crypt_iv
, 0x00, ivsize
);
2984 memset(crypt_data
, 0x00, crypt_len
);
2985 memcpy(crypt_data
, §ion_le
, min((size_t)crypt_len
, sizeof(section_le
)));
2987 sg_init_one(&sg
, crypt_data
, crypt_len
);
2988 skcipher_request_set_crypt(req
, &sg
, &sg
, crypt_len
, crypt_iv
);
2989 init_completion(&comp
.comp
);
2990 comp
.in_flight
= (atomic_t
)ATOMIC_INIT(1);
2991 if (do_crypt(true, req
, &comp
))
2992 wait_for_completion(&comp
.comp
);
2994 r
= dm_integrity_failed(ic
);
2996 *error
= "Unable to generate iv";
3000 section_req
= skcipher_request_alloc(ic
->journal_crypt
, GFP_KERNEL
);
3002 *error
= "Unable to allocate crypt request";
3006 section_req
->iv
= kmalloc_array(ivsize
, 2,
3008 if (!section_req
->iv
) {
3009 skcipher_request_free(section_req
);
3010 *error
= "Unable to allocate iv";
3014 memcpy(section_req
->iv
+ ivsize
, crypt_data
, ivsize
);
3015 section_req
->cryptlen
= (size_t)ic
->journal_section_sectors
<< SECTOR_SHIFT
;
3016 ic
->sk_requests
[i
] = section_req
;
3017 DEBUG_bytes(crypt_data
, ivsize
, "iv(%u)", i
);
3022 for (i
= 0; i
< N_COMMIT_IDS
; i
++) {
3025 for (j
= 0; j
< i
; j
++) {
3026 if (ic
->commit_ids
[j
] == ic
->commit_ids
[i
]) {
3027 ic
->commit_ids
[i
] = cpu_to_le64(le64_to_cpu(ic
->commit_ids
[i
]) + 1);
3028 goto retest_commit_id
;
3031 DEBUG_print("commit id %u: %016llx\n", i
, ic
->commit_ids
[i
]);
3034 journal_tree_size
= (__u64
)ic
->journal_entries
* sizeof(struct journal_node
);
3035 if (journal_tree_size
> ULONG_MAX
) {
3036 *error
= "Journal doesn't fit into memory";
3040 ic
->journal_tree
= kvmalloc(journal_tree_size
, GFP_KERNEL
);
3041 if (!ic
->journal_tree
) {
3042 *error
= "Could not allocate memory for journal tree";
3048 skcipher_request_free(req
);
3054 * Construct a integrity mapping
3058 * offset from the start of the device
3060 * D - direct writes, J - journal writes, R - recovery mode
3061 * number of optional arguments
3062 * optional arguments:
3064 * interleave_sectors
3073 static int dm_integrity_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
3075 struct dm_integrity_c
*ic
;
3078 unsigned extra_args
;
3079 struct dm_arg_set as
;
3080 static const struct dm_arg _args
[] = {
3081 {0, 9, "Invalid number of feature args"},
3083 unsigned journal_sectors
, interleave_sectors
, buffer_sectors
, journal_watermark
, sync_msec
;
3085 bool should_write_sb
;
3087 unsigned long long start
;
3089 #define DIRECT_ARGUMENTS 4
3091 if (argc
<= DIRECT_ARGUMENTS
) {
3092 ti
->error
= "Invalid argument count";
3096 ic
= kzalloc(sizeof(struct dm_integrity_c
), GFP_KERNEL
);
3098 ti
->error
= "Cannot allocate integrity context";
3102 ti
->per_io_data_size
= sizeof(struct dm_integrity_io
);
3105 ic
->in_progress
= RB_ROOT
;
3106 INIT_LIST_HEAD(&ic
->wait_list
);
3107 init_waitqueue_head(&ic
->endio_wait
);
3108 bio_list_init(&ic
->flush_bio_list
);
3109 init_waitqueue_head(&ic
->copy_to_journal_wait
);
3110 init_completion(&ic
->crypto_backoff
);
3111 atomic64_set(&ic
->number_of_mismatches
, 0);
3113 r
= dm_get_device(ti
, argv
[0], dm_table_get_mode(ti
->table
), &ic
->dev
);
3115 ti
->error
= "Device lookup failed";
3119 if (sscanf(argv
[1], "%llu%c", &start
, &dummy
) != 1 || start
!= (sector_t
)start
) {
3120 ti
->error
= "Invalid starting offset";
3126 if (strcmp(argv
[2], "-")) {
3127 if (sscanf(argv
[2], "%u%c", &ic
->tag_size
, &dummy
) != 1 || !ic
->tag_size
) {
3128 ti
->error
= "Invalid tag size";
3134 if (!strcmp(argv
[3], "J") || !strcmp(argv
[3], "D") || !strcmp(argv
[3], "R"))
3135 ic
->mode
= argv
[3][0];
3137 ti
->error
= "Invalid mode (expecting J, D, R)";
3142 journal_sectors
= 0;
3143 interleave_sectors
= DEFAULT_INTERLEAVE_SECTORS
;
3144 buffer_sectors
= DEFAULT_BUFFER_SECTORS
;
3145 journal_watermark
= DEFAULT_JOURNAL_WATERMARK
;
3146 sync_msec
= DEFAULT_SYNC_MSEC
;
3147 recalculate
= false;
3148 ic
->sectors_per_block
= 1;
3150 as
.argc
= argc
- DIRECT_ARGUMENTS
;
3151 as
.argv
= argv
+ DIRECT_ARGUMENTS
;
3152 r
= dm_read_arg_group(_args
, &as
, &extra_args
, &ti
->error
);
3156 while (extra_args
--) {
3157 const char *opt_string
;
3159 opt_string
= dm_shift_arg(&as
);
3162 ti
->error
= "Not enough feature arguments";
3165 if (sscanf(opt_string
, "journal_sectors:%u%c", &val
, &dummy
) == 1)
3166 journal_sectors
= val
? val
: 1;
3167 else if (sscanf(opt_string
, "interleave_sectors:%u%c", &val
, &dummy
) == 1)
3168 interleave_sectors
= val
;
3169 else if (sscanf(opt_string
, "buffer_sectors:%u%c", &val
, &dummy
) == 1)
3170 buffer_sectors
= val
;
3171 else if (sscanf(opt_string
, "journal_watermark:%u%c", &val
, &dummy
) == 1 && val
<= 100)
3172 journal_watermark
= val
;
3173 else if (sscanf(opt_string
, "commit_time:%u%c", &val
, &dummy
) == 1)
3175 else if (!strncmp(opt_string
, "meta_device:", strlen("meta_device:"))) {
3177 dm_put_device(ti
, ic
->meta_dev
);
3178 ic
->meta_dev
= NULL
;
3180 r
= dm_get_device(ti
, strchr(opt_string
, ':') + 1, dm_table_get_mode(ti
->table
), &ic
->meta_dev
);
3182 ti
->error
= "Device lookup failed";
3185 } else if (sscanf(opt_string
, "block_size:%u%c", &val
, &dummy
) == 1) {
3186 if (val
< 1 << SECTOR_SHIFT
||
3187 val
> MAX_SECTORS_PER_BLOCK
<< SECTOR_SHIFT
||
3190 ti
->error
= "Invalid block_size argument";
3193 ic
->sectors_per_block
= val
>> SECTOR_SHIFT
;
3194 } else if (!strncmp(opt_string
, "internal_hash:", strlen("internal_hash:"))) {
3195 r
= get_alg_and_key(opt_string
, &ic
->internal_hash_alg
, &ti
->error
,
3196 "Invalid internal_hash argument");
3199 } else if (!strncmp(opt_string
, "journal_crypt:", strlen("journal_crypt:"))) {
3200 r
= get_alg_and_key(opt_string
, &ic
->journal_crypt_alg
, &ti
->error
,
3201 "Invalid journal_crypt argument");
3204 } else if (!strncmp(opt_string
, "journal_mac:", strlen("journal_mac:"))) {
3205 r
= get_alg_and_key(opt_string
, &ic
->journal_mac_alg
, &ti
->error
,
3206 "Invalid journal_mac argument");
3209 } else if (!strcmp(opt_string
, "recalculate")) {
3213 ti
->error
= "Invalid argument";
3218 ic
->data_device_sectors
= i_size_read(ic
->dev
->bdev
->bd_inode
) >> SECTOR_SHIFT
;
3220 ic
->meta_device_sectors
= ic
->data_device_sectors
;
3222 ic
->meta_device_sectors
= i_size_read(ic
->meta_dev
->bdev
->bd_inode
) >> SECTOR_SHIFT
;
3224 if (!journal_sectors
) {
3225 journal_sectors
= min((sector_t
)DEFAULT_MAX_JOURNAL_SECTORS
,
3226 ic
->data_device_sectors
>> DEFAULT_JOURNAL_SIZE_FACTOR
);
3229 if (!buffer_sectors
)
3231 ic
->log2_buffer_sectors
= min((int)__fls(buffer_sectors
), 31 - SECTOR_SHIFT
);
3233 r
= get_mac(&ic
->internal_hash
, &ic
->internal_hash_alg
, &ti
->error
,
3234 "Invalid internal hash", "Error setting internal hash key");
3238 r
= get_mac(&ic
->journal_mac
, &ic
->journal_mac_alg
, &ti
->error
,
3239 "Invalid journal mac", "Error setting journal mac key");
3243 if (!ic
->tag_size
) {
3244 if (!ic
->internal_hash
) {
3245 ti
->error
= "Unknown tag size";
3249 ic
->tag_size
= crypto_shash_digestsize(ic
->internal_hash
);
3251 if (ic
->tag_size
> MAX_TAG_SIZE
) {
3252 ti
->error
= "Too big tag size";
3256 if (!(ic
->tag_size
& (ic
->tag_size
- 1)))
3257 ic
->log2_tag_size
= __ffs(ic
->tag_size
);
3259 ic
->log2_tag_size
= -1;
3261 ic
->autocommit_jiffies
= msecs_to_jiffies(sync_msec
);
3262 ic
->autocommit_msec
= sync_msec
;
3263 timer_setup(&ic
->autocommit_timer
, autocommit_fn
, 0);
3265 ic
->io
= dm_io_client_create();
3266 if (IS_ERR(ic
->io
)) {
3267 r
= PTR_ERR(ic
->io
);
3269 ti
->error
= "Cannot allocate dm io";
3273 r
= mempool_init_slab_pool(&ic
->journal_io_mempool
, JOURNAL_IO_MEMPOOL
, journal_io_cache
);
3275 ti
->error
= "Cannot allocate mempool";
3279 ic
->metadata_wq
= alloc_workqueue("dm-integrity-metadata",
3280 WQ_MEM_RECLAIM
, METADATA_WORKQUEUE_MAX_ACTIVE
);
3281 if (!ic
->metadata_wq
) {
3282 ti
->error
= "Cannot allocate workqueue";
3288 * If this workqueue were percpu, it would cause bio reordering
3289 * and reduced performance.
3291 ic
->wait_wq
= alloc_workqueue("dm-integrity-wait", WQ_MEM_RECLAIM
| WQ_UNBOUND
, 1);
3293 ti
->error
= "Cannot allocate workqueue";
3298 ic
->offload_wq
= alloc_workqueue("dm-integrity-offload", WQ_MEM_RECLAIM
,
3299 METADATA_WORKQUEUE_MAX_ACTIVE
);
3300 if (!ic
->offload_wq
) {
3301 ti
->error
= "Cannot allocate workqueue";
3306 ic
->commit_wq
= alloc_workqueue("dm-integrity-commit", WQ_MEM_RECLAIM
, 1);
3307 if (!ic
->commit_wq
) {
3308 ti
->error
= "Cannot allocate workqueue";
3312 INIT_WORK(&ic
->commit_work
, integrity_commit
);
3314 if (ic
->mode
== 'J') {
3315 ic
->writer_wq
= alloc_workqueue("dm-integrity-writer", WQ_MEM_RECLAIM
, 1);
3316 if (!ic
->writer_wq
) {
3317 ti
->error
= "Cannot allocate workqueue";
3321 INIT_WORK(&ic
->writer_work
, integrity_writer
);
3324 ic
->sb
= alloc_pages_exact(SB_SECTORS
<< SECTOR_SHIFT
, GFP_KERNEL
);
3327 ti
->error
= "Cannot allocate superblock area";
3331 r
= sync_rw_sb(ic
, REQ_OP_READ
, 0);
3333 ti
->error
= "Error reading superblock";
3336 should_write_sb
= false;
3337 if (memcmp(ic
->sb
->magic
, SB_MAGIC
, 8)) {
3338 if (ic
->mode
!= 'R') {
3339 if (memchr_inv(ic
->sb
, 0, SB_SECTORS
<< SECTOR_SHIFT
)) {
3341 ti
->error
= "The device is not initialized";
3346 r
= initialize_superblock(ic
, journal_sectors
, interleave_sectors
);
3348 ti
->error
= "Could not initialize superblock";
3351 if (ic
->mode
!= 'R')
3352 should_write_sb
= true;
3355 if (!ic
->sb
->version
|| ic
->sb
->version
> SB_VERSION_2
) {
3357 ti
->error
= "Unknown version";
3360 if (le16_to_cpu(ic
->sb
->integrity_tag_size
) != ic
->tag_size
) {
3362 ti
->error
= "Tag size doesn't match the information in superblock";
3365 if (ic
->sb
->log2_sectors_per_block
!= __ffs(ic
->sectors_per_block
)) {
3367 ti
->error
= "Block size doesn't match the information in superblock";
3370 if (!le32_to_cpu(ic
->sb
->journal_sections
)) {
3372 ti
->error
= "Corrupted superblock, journal_sections is 0";
3375 /* make sure that ti->max_io_len doesn't overflow */
3376 if (!ic
->meta_dev
) {
3377 if (ic
->sb
->log2_interleave_sectors
< MIN_LOG2_INTERLEAVE_SECTORS
||
3378 ic
->sb
->log2_interleave_sectors
> MAX_LOG2_INTERLEAVE_SECTORS
) {
3380 ti
->error
= "Invalid interleave_sectors in the superblock";
3384 if (ic
->sb
->log2_interleave_sectors
) {
3386 ti
->error
= "Invalid interleave_sectors in the superblock";
3390 ic
->provided_data_sectors
= le64_to_cpu(ic
->sb
->provided_data_sectors
);
3391 if (ic
->provided_data_sectors
!= le64_to_cpu(ic
->sb
->provided_data_sectors
)) {
3392 /* test for overflow */
3394 ti
->error
= "The superblock has 64-bit device size, but the kernel was compiled with 32-bit sectors";
3397 if (!!(ic
->sb
->flags
& cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC
)) != !!ic
->journal_mac_alg
.alg_string
) {
3399 ti
->error
= "Journal mac mismatch";
3404 r
= calculate_device_limits(ic
);
3407 if (ic
->log2_buffer_sectors
> 3) {
3408 ic
->log2_buffer_sectors
--;
3409 goto try_smaller_buffer
;
3412 ti
->error
= "The device is too small";
3416 ic
->log2_buffer_sectors
= min(ic
->log2_buffer_sectors
, (__u8
)__ffs(ic
->metadata_run
));
3418 if (ti
->len
> ic
->provided_data_sectors
) {
3420 ti
->error
= "Not enough provided sectors for requested mapping size";
3425 threshold
= (__u64
)ic
->journal_entries
* (100 - journal_watermark
);
3427 do_div(threshold
, 100);
3428 ic
->free_sectors_threshold
= threshold
;
3430 DEBUG_print("initialized:\n");
3431 DEBUG_print(" integrity_tag_size %u\n", le16_to_cpu(ic
->sb
->integrity_tag_size
));
3432 DEBUG_print(" journal_entry_size %u\n", ic
->journal_entry_size
);
3433 DEBUG_print(" journal_entries_per_sector %u\n", ic
->journal_entries_per_sector
);
3434 DEBUG_print(" journal_section_entries %u\n", ic
->journal_section_entries
);
3435 DEBUG_print(" journal_section_sectors %u\n", ic
->journal_section_sectors
);
3436 DEBUG_print(" journal_sections %u\n", (unsigned)le32_to_cpu(ic
->sb
->journal_sections
));
3437 DEBUG_print(" journal_entries %u\n", ic
->journal_entries
);
3438 DEBUG_print(" log2_interleave_sectors %d\n", ic
->sb
->log2_interleave_sectors
);
3439 DEBUG_print(" data_device_sectors 0x%llx\n", (unsigned long long)ic
->data_device_sectors
);
3440 DEBUG_print(" initial_sectors 0x%x\n", ic
->initial_sectors
);
3441 DEBUG_print(" metadata_run 0x%x\n", ic
->metadata_run
);
3442 DEBUG_print(" log2_metadata_run %d\n", ic
->log2_metadata_run
);
3443 DEBUG_print(" provided_data_sectors 0x%llx (%llu)\n", (unsigned long long)ic
->provided_data_sectors
,
3444 (unsigned long long)ic
->provided_data_sectors
);
3445 DEBUG_print(" log2_buffer_sectors %u\n", ic
->log2_buffer_sectors
);
3447 if (recalculate
&& !(ic
->sb
->flags
& cpu_to_le32(SB_FLAG_RECALCULATING
))) {
3448 ic
->sb
->flags
|= cpu_to_le32(SB_FLAG_RECALCULATING
);
3449 ic
->sb
->recalc_sector
= cpu_to_le64(0);
3452 if (ic
->sb
->flags
& cpu_to_le32(SB_FLAG_RECALCULATING
)) {
3453 if (!ic
->internal_hash
) {
3455 ti
->error
= "Recalculate is only valid with internal hash";
3458 ic
->recalc_wq
= alloc_workqueue("dm-intergrity-recalc", WQ_MEM_RECLAIM
, 1);
3459 if (!ic
->recalc_wq
) {
3460 ti
->error
= "Cannot allocate workqueue";
3464 INIT_WORK(&ic
->recalc_work
, integrity_recalc
);
3465 ic
->recalc_buffer
= vmalloc(RECALC_SECTORS
<< SECTOR_SHIFT
);
3466 if (!ic
->recalc_buffer
) {
3467 ti
->error
= "Cannot allocate buffer for recalculating";
3471 ic
->recalc_tags
= kvmalloc_array(RECALC_SECTORS
>> ic
->sb
->log2_sectors_per_block
,
3472 ic
->tag_size
, GFP_KERNEL
);
3473 if (!ic
->recalc_tags
) {
3474 ti
->error
= "Cannot allocate tags for recalculating";
3480 ic
->bufio
= dm_bufio_client_create(ic
->meta_dev
? ic
->meta_dev
->bdev
: ic
->dev
->bdev
,
3481 1U << (SECTOR_SHIFT
+ ic
->log2_buffer_sectors
), 1, 0, NULL
, NULL
);
3482 if (IS_ERR(ic
->bufio
)) {
3483 r
= PTR_ERR(ic
->bufio
);
3484 ti
->error
= "Cannot initialize dm-bufio";
3488 dm_bufio_set_sector_offset(ic
->bufio
, ic
->start
+ ic
->initial_sectors
);
3490 if (ic
->mode
!= 'R') {
3491 r
= create_journal(ic
, &ti
->error
);
3496 if (should_write_sb
) {
3499 init_journal(ic
, 0, ic
->journal_sections
, 0);
3500 r
= dm_integrity_failed(ic
);
3502 ti
->error
= "Error initializing journal";
3505 r
= sync_rw_sb(ic
, REQ_OP_WRITE
, REQ_FUA
);
3507 ti
->error
= "Error initializing superblock";
3510 ic
->just_formatted
= true;
3513 if (!ic
->meta_dev
) {
3514 r
= dm_set_target_max_io_len(ti
, 1U << ic
->sb
->log2_interleave_sectors
);
3519 if (!ic
->internal_hash
)
3520 dm_integrity_set(ti
, ic
);
3522 ti
->num_flush_bios
= 1;
3523 ti
->flush_supported
= true;
3527 dm_integrity_dtr(ti
);
3531 static void dm_integrity_dtr(struct dm_target
*ti
)
3533 struct dm_integrity_c
*ic
= ti
->private;
3535 BUG_ON(!RB_EMPTY_ROOT(&ic
->in_progress
));
3536 BUG_ON(!list_empty(&ic
->wait_list
));
3538 if (ic
->metadata_wq
)
3539 destroy_workqueue(ic
->metadata_wq
);
3541 destroy_workqueue(ic
->wait_wq
);
3543 destroy_workqueue(ic
->offload_wq
);
3545 destroy_workqueue(ic
->commit_wq
);
3547 destroy_workqueue(ic
->writer_wq
);
3549 destroy_workqueue(ic
->recalc_wq
);
3550 if (ic
->recalc_buffer
)
3551 vfree(ic
->recalc_buffer
);
3552 if (ic
->recalc_tags
)
3553 kvfree(ic
->recalc_tags
);
3555 dm_bufio_client_destroy(ic
->bufio
);
3556 mempool_exit(&ic
->journal_io_mempool
);
3558 dm_io_client_destroy(ic
->io
);
3560 dm_put_device(ti
, ic
->dev
);
3562 dm_put_device(ti
, ic
->meta_dev
);
3563 dm_integrity_free_page_list(ic
, ic
->journal
);
3564 dm_integrity_free_page_list(ic
, ic
->journal_io
);
3565 dm_integrity_free_page_list(ic
, ic
->journal_xor
);
3566 if (ic
->journal_scatterlist
)
3567 dm_integrity_free_journal_scatterlist(ic
, ic
->journal_scatterlist
);
3568 if (ic
->journal_io_scatterlist
)
3569 dm_integrity_free_journal_scatterlist(ic
, ic
->journal_io_scatterlist
);
3570 if (ic
->sk_requests
) {
3573 for (i
= 0; i
< ic
->journal_sections
; i
++) {
3574 struct skcipher_request
*req
= ic
->sk_requests
[i
];
3577 skcipher_request_free(req
);
3580 kvfree(ic
->sk_requests
);
3582 kvfree(ic
->journal_tree
);
3584 free_pages_exact(ic
->sb
, SB_SECTORS
<< SECTOR_SHIFT
);
3586 if (ic
->internal_hash
)
3587 crypto_free_shash(ic
->internal_hash
);
3588 free_alg(&ic
->internal_hash_alg
);
3590 if (ic
->journal_crypt
)
3591 crypto_free_skcipher(ic
->journal_crypt
);
3592 free_alg(&ic
->journal_crypt_alg
);
3594 if (ic
->journal_mac
)
3595 crypto_free_shash(ic
->journal_mac
);
3596 free_alg(&ic
->journal_mac_alg
);
3601 static struct target_type integrity_target
= {
3602 .name
= "integrity",
3603 .version
= {1, 2, 0},
3604 .module
= THIS_MODULE
,
3605 .features
= DM_TARGET_SINGLETON
| DM_TARGET_INTEGRITY
,
3606 .ctr
= dm_integrity_ctr
,
3607 .dtr
= dm_integrity_dtr
,
3608 .map
= dm_integrity_map
,
3609 .postsuspend
= dm_integrity_postsuspend
,
3610 .resume
= dm_integrity_resume
,
3611 .status
= dm_integrity_status
,
3612 .iterate_devices
= dm_integrity_iterate_devices
,
3613 .io_hints
= dm_integrity_io_hints
,
3616 int __init
dm_integrity_init(void)
3620 journal_io_cache
= kmem_cache_create("integrity_journal_io",
3621 sizeof(struct journal_io
), 0, 0, NULL
);
3622 if (!journal_io_cache
) {
3623 DMERR("can't allocate journal io cache");
3627 r
= dm_register_target(&integrity_target
);
3630 DMERR("register failed %d", r
);
3635 void dm_integrity_exit(void)
3637 dm_unregister_target(&integrity_target
);
3638 kmem_cache_destroy(journal_io_cache
);
3641 module_init(dm_integrity_init
);
3642 module_exit(dm_integrity_exit
);
3644 MODULE_AUTHOR("Milan Broz");
3645 MODULE_AUTHOR("Mikulas Patocka");
3646 MODULE_DESCRIPTION(DM_NAME
" target for integrity tags extension");
3647 MODULE_LICENSE("GPL");