blob: 47fd409b2e2ae40b12d4788c0f6e8e2ede9a31a1 [file] [log] [blame]
Mikulas Patocka7eada902017-01-04 20:23:53 +01001/*
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
5 *
6 * This file is released under the GPL.
7 */
8
9#include <linux/module.h>
10#include <linux/device-mapper.h>
11#include <linux/dm-io.h>
12#include <linux/vmalloc.h>
13#include <linux/sort.h>
14#include <linux/rbtree.h>
15#include <linux/delay.h>
16#include <linux/random.h>
17#include <crypto/hash.h>
18#include <crypto/skcipher.h>
19#include <linux/async_tx.h>
20#include "dm-bufio.h"
21
22#define DM_MSG_PREFIX "integrity"
23
24#define DEFAULT_INTERLEAVE_SECTORS 32768
25#define DEFAULT_JOURNAL_SIZE_FACTOR 7
26#define DEFAULT_BUFFER_SECTORS 128
27#define DEFAULT_JOURNAL_WATERMARK 50
28#define DEFAULT_SYNC_MSEC 10000
29#define DEFAULT_MAX_JOURNAL_SECTORS 131072
Mikulas Patocka56b67a42017-04-18 16:51:50 -040030#define MIN_LOG2_INTERLEAVE_SECTORS 3
31#define MAX_LOG2_INTERLEAVE_SECTORS 31
Mikulas Patocka7eada902017-01-04 20:23:53 +010032#define METADATA_WORKQUEUE_MAX_ACTIVE 16
33
34/*
35 * Warning - DEBUG_PRINT prints security-sensitive data to the log,
36 * so it should not be enabled in the official kernel
37 */
38//#define DEBUG_PRINT
39//#define INTERNAL_VERIFY
40
41/*
42 * On disk structures
43 */
44
45#define SB_MAGIC "integrt"
46#define SB_VERSION 1
47#define SB_SECTORS 8
Mikulas Patocka9d609f82017-04-18 16:51:52 -040048#define MAX_SECTORS_PER_BLOCK 8
Mikulas Patocka7eada902017-01-04 20:23:53 +010049
50struct superblock {
51 __u8 magic[8];
52 __u8 version;
53 __u8 log2_interleave_sectors;
54 __u16 integrity_tag_size;
55 __u32 journal_sections;
56 __u64 provided_data_sectors; /* userspace uses this value */
57 __u32 flags;
Mikulas Patocka9d609f82017-04-18 16:51:52 -040058 __u8 log2_sectors_per_block;
Mikulas Patocka7eada902017-01-04 20:23:53 +010059};
60
61#define SB_FLAG_HAVE_JOURNAL_MAC 0x1
62
63#define JOURNAL_ENTRY_ROUNDUP 8
64
65typedef __u64 commit_id_t;
66#define JOURNAL_MAC_PER_SECTOR 8
67
68struct journal_entry {
69 union {
70 struct {
71 __u32 sector_lo;
72 __u32 sector_hi;
73 } s;
74 __u64 sector;
75 } u;
Mikulas Patocka9d609f82017-04-18 16:51:52 -040076 commit_id_t last_bytes[0];
77 /* __u8 tag[0]; */
Mikulas Patocka7eada902017-01-04 20:23:53 +010078};
79
Mikulas Patocka9d609f82017-04-18 16:51:52 -040080#define journal_entry_tag(ic, je) ((__u8 *)&(je)->last_bytes[(ic)->sectors_per_block])
81
Mikulas Patocka7eada902017-01-04 20:23:53 +010082#if BITS_PER_LONG == 64
83#define journal_entry_set_sector(je, x) do { smp_wmb(); ACCESS_ONCE((je)->u.sector) = cpu_to_le64(x); } while (0)
84#define journal_entry_get_sector(je) le64_to_cpu((je)->u.sector)
85#elif defined(CONFIG_LBDAF)
86#define journal_entry_set_sector(je, x) do { (je)->u.s.sector_lo = cpu_to_le32(x); smp_wmb(); ACCESS_ONCE((je)->u.s.sector_hi) = cpu_to_le32((x) >> 32); } while (0)
87#define journal_entry_get_sector(je) le64_to_cpu((je)->u.sector)
88#else
89#define journal_entry_set_sector(je, x) do { (je)->u.s.sector_lo = cpu_to_le32(x); smp_wmb(); ACCESS_ONCE((je)->u.s.sector_hi) = cpu_to_le32(0); } while (0)
90#define journal_entry_get_sector(je) le32_to_cpu((je)->u.s.sector_lo)
91#endif
92#define journal_entry_is_unused(je) ((je)->u.s.sector_hi == cpu_to_le32(-1))
93#define journal_entry_set_unused(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-1)); } while (0)
94#define journal_entry_is_inprogress(je) ((je)->u.s.sector_hi == cpu_to_le32(-2))
95#define journal_entry_set_inprogress(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-2)); } while (0)
96
97#define JOURNAL_BLOCK_SECTORS 8
98#define JOURNAL_SECTOR_DATA ((1 << SECTOR_SHIFT) - sizeof(commit_id_t))
99#define JOURNAL_MAC_SIZE (JOURNAL_MAC_PER_SECTOR * JOURNAL_BLOCK_SECTORS)
100
101struct journal_sector {
102 __u8 entries[JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR];
103 __u8 mac[JOURNAL_MAC_PER_SECTOR];
104 commit_id_t commit_id;
105};
106
Mikulas Patocka9d609f82017-04-18 16:51:52 -0400107#define MAX_TAG_SIZE (JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR - offsetof(struct journal_entry, last_bytes[MAX_SECTORS_PER_BLOCK]))
Mikulas Patocka7eada902017-01-04 20:23:53 +0100108
109#define METADATA_PADDING_SECTORS 8
110
111#define N_COMMIT_IDS 4
112
113static unsigned char prev_commit_seq(unsigned char seq)
114{
115 return (seq + N_COMMIT_IDS - 1) % N_COMMIT_IDS;
116}
117
118static unsigned char next_commit_seq(unsigned char seq)
119{
120 return (seq + 1) % N_COMMIT_IDS;
121}
122
123/*
124 * In-memory structures
125 */
126
127struct journal_node {
128 struct rb_node node;
129 sector_t sector;
130};
131
132struct alg_spec {
133 char *alg_string;
134 char *key_string;
135 __u8 *key;
136 unsigned key_size;
137};
138
139struct dm_integrity_c {
140 struct dm_dev *dev;
141 unsigned tag_size;
142 __s8 log2_tag_size;
143 sector_t start;
144 mempool_t *journal_io_mempool;
145 struct dm_io_client *io;
146 struct dm_bufio_client *bufio;
147 struct workqueue_struct *metadata_wq;
148 struct superblock *sb;
149 unsigned journal_pages;
150 struct page_list *journal;
151 struct page_list *journal_io;
152 struct page_list *journal_xor;
153
154 struct crypto_skcipher *journal_crypt;
155 struct scatterlist **journal_scatterlist;
156 struct scatterlist **journal_io_scatterlist;
157 struct skcipher_request **sk_requests;
158
159 struct crypto_shash *journal_mac;
160
161 struct journal_node *journal_tree;
162 struct rb_root journal_tree_root;
163
164 sector_t provided_data_sectors;
165
166 unsigned short journal_entry_size;
167 unsigned char journal_entries_per_sector;
168 unsigned char journal_section_entries;
Mikulas Patocka9d609f82017-04-18 16:51:52 -0400169 unsigned short journal_section_sectors;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100170 unsigned journal_sections;
171 unsigned journal_entries;
172 sector_t device_sectors;
173 unsigned initial_sectors;
174 unsigned metadata_run;
175 __s8 log2_metadata_run;
176 __u8 log2_buffer_sectors;
Mikulas Patocka9d609f82017-04-18 16:51:52 -0400177 __u8 sectors_per_block;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100178
179 unsigned char mode;
180 bool suspending;
181
182 int failed;
183
184 struct crypto_shash *internal_hash;
185
186 /* these variables are locked with endio_wait.lock */
187 struct rb_root in_progress;
188 wait_queue_head_t endio_wait;
189 struct workqueue_struct *wait_wq;
190
191 unsigned char commit_seq;
192 commit_id_t commit_ids[N_COMMIT_IDS];
193
194 unsigned committed_section;
195 unsigned n_committed_sections;
196
197 unsigned uncommitted_section;
198 unsigned n_uncommitted_sections;
199
200 unsigned free_section;
201 unsigned char free_section_entry;
202 unsigned free_sectors;
203
204 unsigned free_sectors_threshold;
205
206 struct workqueue_struct *commit_wq;
207 struct work_struct commit_work;
208
209 struct workqueue_struct *writer_wq;
210 struct work_struct writer_work;
211
212 struct bio_list flush_bio_list;
213
214 unsigned long autocommit_jiffies;
215 struct timer_list autocommit_timer;
216 unsigned autocommit_msec;
217
218 wait_queue_head_t copy_to_journal_wait;
219
220 struct completion crypto_backoff;
221
222 bool journal_uptodate;
223 bool just_formatted;
224
225 struct alg_spec internal_hash_alg;
226 struct alg_spec journal_crypt_alg;
227 struct alg_spec journal_mac_alg;
Mikulas Patocka3f2e5392017-07-21 12:00:00 -0400228
229 atomic64_t number_of_mismatches;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100230};
231
232struct dm_integrity_range {
233 sector_t logical_sector;
234 unsigned n_sectors;
235 struct rb_node node;
236};
237
238struct dm_integrity_io {
239 struct work_struct work;
240
241 struct dm_integrity_c *ic;
242 bool write;
243 bool fua;
244
245 struct dm_integrity_range range;
246
247 sector_t metadata_block;
248 unsigned metadata_offset;
249
250 atomic_t in_flight;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200251 blk_status_t bi_status;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100252
253 struct completion *completion;
254
255 struct block_device *orig_bi_bdev;
256 bio_end_io_t *orig_bi_end_io;
257 struct bio_integrity_payload *orig_bi_integrity;
258 struct bvec_iter orig_bi_iter;
259};
260
261struct journal_completion {
262 struct dm_integrity_c *ic;
263 atomic_t in_flight;
264 struct completion comp;
265};
266
267struct journal_io {
268 struct dm_integrity_range range;
269 struct journal_completion *comp;
270};
271
272static struct kmem_cache *journal_io_cache;
273
274#define JOURNAL_IO_MEMPOOL 32
275
276#ifdef DEBUG_PRINT
277#define DEBUG_print(x, ...) printk(KERN_DEBUG x, ##__VA_ARGS__)
278static void __DEBUG_bytes(__u8 *bytes, size_t len, const char *msg, ...)
279{
280 va_list args;
281 va_start(args, msg);
282 vprintk(msg, args);
283 va_end(args);
284 if (len)
285 pr_cont(":");
286 while (len) {
287 pr_cont(" %02x", *bytes);
288 bytes++;
289 len--;
290 }
291 pr_cont("\n");
292}
293#define DEBUG_bytes(bytes, len, msg, ...) __DEBUG_bytes(bytes, len, KERN_DEBUG msg, ##__VA_ARGS__)
294#else
295#define DEBUG_print(x, ...) do { } while (0)
296#define DEBUG_bytes(bytes, len, msg, ...) do { } while (0)
297#endif
298
299/*
300 * DM Integrity profile, protection is performed layer above (dm-crypt)
301 */
302static struct blk_integrity_profile dm_integrity_profile = {
303 .name = "DM-DIF-EXT-TAG",
304 .generate_fn = NULL,
305 .verify_fn = NULL,
306};
307
308static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map);
309static void integrity_bio_wait(struct work_struct *w);
310static void dm_integrity_dtr(struct dm_target *ti);
311
312static void dm_integrity_io_error(struct dm_integrity_c *ic, const char *msg, int err)
313{
Mikulas Patocka3f2e5392017-07-21 12:00:00 -0400314 if (err == -EILSEQ)
315 atomic64_inc(&ic->number_of_mismatches);
Mikulas Patocka7eada902017-01-04 20:23:53 +0100316 if (!cmpxchg(&ic->failed, 0, err))
317 DMERR("Error on %s: %d", msg, err);
318}
319
320static int dm_integrity_failed(struct dm_integrity_c *ic)
321{
322 return ACCESS_ONCE(ic->failed);
323}
324
325static commit_id_t dm_integrity_commit_id(struct dm_integrity_c *ic, unsigned i,
326 unsigned j, unsigned char seq)
327{
328 /*
329 * Xor the number with section and sector, so that if a piece of
330 * journal is written at wrong place, it is detected.
331 */
332 return ic->commit_ids[seq] ^ cpu_to_le64(((__u64)i << 32) ^ j);
333}
334
335static void get_area_and_offset(struct dm_integrity_c *ic, sector_t data_sector,
336 sector_t *area, sector_t *offset)
337{
338 __u8 log2_interleave_sectors = ic->sb->log2_interleave_sectors;
339
340 *area = data_sector >> log2_interleave_sectors;
341 *offset = (unsigned)data_sector & ((1U << log2_interleave_sectors) - 1);
342}
343
Mikulas Patocka9d609f82017-04-18 16:51:52 -0400344#define sector_to_block(ic, n) \
345do { \
346 BUG_ON((n) & (unsigned)((ic)->sectors_per_block - 1)); \
347 (n) >>= (ic)->sb->log2_sectors_per_block; \
348} while (0)
349
Mikulas Patocka7eada902017-01-04 20:23:53 +0100350static __u64 get_metadata_sector_and_offset(struct dm_integrity_c *ic, sector_t area,
351 sector_t offset, unsigned *metadata_offset)
352{
353 __u64 ms;
354 unsigned mo;
355
356 ms = area << ic->sb->log2_interleave_sectors;
357 if (likely(ic->log2_metadata_run >= 0))
358 ms += area << ic->log2_metadata_run;
359 else
360 ms += area * ic->metadata_run;
361 ms >>= ic->log2_buffer_sectors;
362
Mikulas Patocka9d609f82017-04-18 16:51:52 -0400363 sector_to_block(ic, offset);
364
Mikulas Patocka7eada902017-01-04 20:23:53 +0100365 if (likely(ic->log2_tag_size >= 0)) {
366 ms += offset >> (SECTOR_SHIFT + ic->log2_buffer_sectors - ic->log2_tag_size);
367 mo = (offset << ic->log2_tag_size) & ((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - 1);
368 } else {
369 ms += (__u64)offset * ic->tag_size >> (SECTOR_SHIFT + ic->log2_buffer_sectors);
370 mo = (offset * ic->tag_size) & ((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - 1);
371 }
372 *metadata_offset = mo;
373 return ms;
374}
375
376static sector_t get_data_sector(struct dm_integrity_c *ic, sector_t area, sector_t offset)
377{
378 sector_t result;
379
380 result = area << ic->sb->log2_interleave_sectors;
381 if (likely(ic->log2_metadata_run >= 0))
382 result += (area + 1) << ic->log2_metadata_run;
383 else
384 result += (area + 1) * ic->metadata_run;
385
386 result += (sector_t)ic->initial_sectors + offset;
387 return result;
388}
389
390static void wraparound_section(struct dm_integrity_c *ic, unsigned *sec_ptr)
391{
392 if (unlikely(*sec_ptr >= ic->journal_sections))
393 *sec_ptr -= ic->journal_sections;
394}
395
396static int sync_rw_sb(struct dm_integrity_c *ic, int op, int op_flags)
397{
398 struct dm_io_request io_req;
399 struct dm_io_region io_loc;
400
401 io_req.bi_op = op;
402 io_req.bi_op_flags = op_flags;
403 io_req.mem.type = DM_IO_KMEM;
404 io_req.mem.ptr.addr = ic->sb;
405 io_req.notify.fn = NULL;
406 io_req.client = ic->io;
407 io_loc.bdev = ic->dev->bdev;
408 io_loc.sector = ic->start;
409 io_loc.count = SB_SECTORS;
410
411 return dm_io(&io_req, 1, &io_loc, NULL);
412}
413
414static void access_journal_check(struct dm_integrity_c *ic, unsigned section, unsigned offset,
415 bool e, const char *function)
416{
417#if defined(CONFIG_DM_DEBUG) || defined(INTERNAL_VERIFY)
418 unsigned limit = e ? ic->journal_section_entries : ic->journal_section_sectors;
419
420 if (unlikely(section >= ic->journal_sections) ||
421 unlikely(offset >= limit)) {
422 printk(KERN_CRIT "%s: invalid access at (%u,%u), limit (%u,%u)\n",
423 function, section, offset, ic->journal_sections, limit);
424 BUG();
425 }
426#endif
427}
428
429static void page_list_location(struct dm_integrity_c *ic, unsigned section, unsigned offset,
430 unsigned *pl_index, unsigned *pl_offset)
431{
432 unsigned sector;
433
Mikulas Patocka56b67a42017-04-18 16:51:50 -0400434 access_journal_check(ic, section, offset, false, "page_list_location");
Mikulas Patocka7eada902017-01-04 20:23:53 +0100435
436 sector = section * ic->journal_section_sectors + offset;
437
438 *pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
439 *pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
440}
441
442static struct journal_sector *access_page_list(struct dm_integrity_c *ic, struct page_list *pl,
443 unsigned section, unsigned offset, unsigned *n_sectors)
444{
445 unsigned pl_index, pl_offset;
446 char *va;
447
448 page_list_location(ic, section, offset, &pl_index, &pl_offset);
449
450 if (n_sectors)
451 *n_sectors = (PAGE_SIZE - pl_offset) >> SECTOR_SHIFT;
452
453 va = lowmem_page_address(pl[pl_index].page);
454
455 return (struct journal_sector *)(va + pl_offset);
456}
457
458static struct journal_sector *access_journal(struct dm_integrity_c *ic, unsigned section, unsigned offset)
459{
460 return access_page_list(ic, ic->journal, section, offset, NULL);
461}
462
463static struct journal_entry *access_journal_entry(struct dm_integrity_c *ic, unsigned section, unsigned n)
464{
465 unsigned rel_sector, offset;
466 struct journal_sector *js;
467
468 access_journal_check(ic, section, n, true, "access_journal_entry");
469
470 rel_sector = n % JOURNAL_BLOCK_SECTORS;
471 offset = n / JOURNAL_BLOCK_SECTORS;
472
473 js = access_journal(ic, section, rel_sector);
474 return (struct journal_entry *)((char *)js + offset * ic->journal_entry_size);
475}
476
477static struct journal_sector *access_journal_data(struct dm_integrity_c *ic, unsigned section, unsigned n)
478{
Mikulas Patocka9d609f82017-04-18 16:51:52 -0400479 n <<= ic->sb->log2_sectors_per_block;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100480
Mikulas Patocka9d609f82017-04-18 16:51:52 -0400481 n += JOURNAL_BLOCK_SECTORS;
482
483 access_journal_check(ic, section, n, false, "access_journal_data");
484
485 return access_journal(ic, section, n);
Mikulas Patocka7eada902017-01-04 20:23:53 +0100486}
487
488static void section_mac(struct dm_integrity_c *ic, unsigned section, __u8 result[JOURNAL_MAC_SIZE])
489{
490 SHASH_DESC_ON_STACK(desc, ic->journal_mac);
491 int r;
492 unsigned j, size;
493
494 desc->tfm = ic->journal_mac;
495 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
496
497 r = crypto_shash_init(desc);
498 if (unlikely(r)) {
499 dm_integrity_io_error(ic, "crypto_shash_init", r);
500 goto err;
501 }
502
503 for (j = 0; j < ic->journal_section_entries; j++) {
504 struct journal_entry *je = access_journal_entry(ic, section, j);
505 r = crypto_shash_update(desc, (__u8 *)&je->u.sector, sizeof je->u.sector);
506 if (unlikely(r)) {
507 dm_integrity_io_error(ic, "crypto_shash_update", r);
508 goto err;
509 }
510 }
511
512 size = crypto_shash_digestsize(ic->journal_mac);
513
514 if (likely(size <= JOURNAL_MAC_SIZE)) {
515 r = crypto_shash_final(desc, result);
516 if (unlikely(r)) {
517 dm_integrity_io_error(ic, "crypto_shash_final", r);
518 goto err;
519 }
520 memset(result + size, 0, JOURNAL_MAC_SIZE - size);
521 } else {
522 __u8 digest[size];
523 r = crypto_shash_final(desc, digest);
524 if (unlikely(r)) {
525 dm_integrity_io_error(ic, "crypto_shash_final", r);
526 goto err;
527 }
528 memcpy(result, digest, JOURNAL_MAC_SIZE);
529 }
530
531 return;
532err:
533 memset(result, 0, JOURNAL_MAC_SIZE);
534}
535
536static void rw_section_mac(struct dm_integrity_c *ic, unsigned section, bool wr)
537{
538 __u8 result[JOURNAL_MAC_SIZE];
539 unsigned j;
540
541 if (!ic->journal_mac)
542 return;
543
544 section_mac(ic, section, result);
545
546 for (j = 0; j < JOURNAL_BLOCK_SECTORS; j++) {
547 struct journal_sector *js = access_journal(ic, section, j);
548
549 if (likely(wr))
550 memcpy(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR);
551 else {
552 if (memcmp(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR))
553 dm_integrity_io_error(ic, "journal mac", -EILSEQ);
554 }
555 }
556}
557
558static void complete_journal_op(void *context)
559{
560 struct journal_completion *comp = context;
561 BUG_ON(!atomic_read(&comp->in_flight));
562 if (likely(atomic_dec_and_test(&comp->in_flight)))
563 complete(&comp->comp);
564}
565
566static void xor_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
567 unsigned n_sections, struct journal_completion *comp)
568{
569 struct async_submit_ctl submit;
570 size_t n_bytes = (size_t)(n_sections * ic->journal_section_sectors) << SECTOR_SHIFT;
571 unsigned pl_index, pl_offset, section_index;
572 struct page_list *source_pl, *target_pl;
573
574 if (likely(encrypt)) {
575 source_pl = ic->journal;
576 target_pl = ic->journal_io;
577 } else {
578 source_pl = ic->journal_io;
579 target_pl = ic->journal;
580 }
581
582 page_list_location(ic, section, 0, &pl_index, &pl_offset);
583
584 atomic_add(roundup(pl_offset + n_bytes, PAGE_SIZE) >> PAGE_SHIFT, &comp->in_flight);
585
586 init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL, complete_journal_op, comp, NULL);
587
588 section_index = pl_index;
589
590 do {
591 size_t this_step;
592 struct page *src_pages[2];
593 struct page *dst_page;
594
595 while (unlikely(pl_index == section_index)) {
596 unsigned dummy;
597 if (likely(encrypt))
598 rw_section_mac(ic, section, true);
599 section++;
600 n_sections--;
601 if (!n_sections)
602 break;
603 page_list_location(ic, section, 0, &section_index, &dummy);
604 }
605
606 this_step = min(n_bytes, (size_t)PAGE_SIZE - pl_offset);
607 dst_page = target_pl[pl_index].page;
608 src_pages[0] = source_pl[pl_index].page;
609 src_pages[1] = ic->journal_xor[pl_index].page;
610
611 async_xor(dst_page, src_pages, pl_offset, 2, this_step, &submit);
612
613 pl_index++;
614 pl_offset = 0;
615 n_bytes -= this_step;
616 } while (n_bytes);
617
618 BUG_ON(n_sections);
619
620 async_tx_issue_pending_all();
621}
622
623static void complete_journal_encrypt(struct crypto_async_request *req, int err)
624{
625 struct journal_completion *comp = req->data;
626 if (unlikely(err)) {
627 if (likely(err == -EINPROGRESS)) {
628 complete(&comp->ic->crypto_backoff);
629 return;
630 }
631 dm_integrity_io_error(comp->ic, "asynchronous encrypt", err);
632 }
633 complete_journal_op(comp);
634}
635
636static bool do_crypt(bool encrypt, struct skcipher_request *req, struct journal_completion *comp)
637{
638 int r;
639 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
640 complete_journal_encrypt, comp);
641 if (likely(encrypt))
642 r = crypto_skcipher_encrypt(req);
643 else
644 r = crypto_skcipher_decrypt(req);
645 if (likely(!r))
646 return false;
647 if (likely(r == -EINPROGRESS))
648 return true;
649 if (likely(r == -EBUSY)) {
650 wait_for_completion(&comp->ic->crypto_backoff);
651 reinit_completion(&comp->ic->crypto_backoff);
652 return true;
653 }
654 dm_integrity_io_error(comp->ic, "encrypt", r);
655 return false;
656}
657
658static void crypt_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
659 unsigned n_sections, struct journal_completion *comp)
660{
661 struct scatterlist **source_sg;
662 struct scatterlist **target_sg;
663
664 atomic_add(2, &comp->in_flight);
665
666 if (likely(encrypt)) {
667 source_sg = ic->journal_scatterlist;
668 target_sg = ic->journal_io_scatterlist;
669 } else {
670 source_sg = ic->journal_io_scatterlist;
671 target_sg = ic->journal_scatterlist;
672 }
673
674 do {
675 struct skcipher_request *req;
676 unsigned ivsize;
677 char *iv;
678
679 if (likely(encrypt))
680 rw_section_mac(ic, section, true);
681
682 req = ic->sk_requests[section];
683 ivsize = crypto_skcipher_ivsize(ic->journal_crypt);
684 iv = req->iv;
685
686 memcpy(iv, iv + ivsize, ivsize);
687
688 req->src = source_sg[section];
689 req->dst = target_sg[section];
690
691 if (unlikely(do_crypt(encrypt, req, comp)))
692 atomic_inc(&comp->in_flight);
693
694 section++;
695 n_sections--;
696 } while (n_sections);
697
698 atomic_dec(&comp->in_flight);
699 complete_journal_op(comp);
700}
701
702static void encrypt_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
703 unsigned n_sections, struct journal_completion *comp)
704{
705 if (ic->journal_xor)
706 return xor_journal(ic, encrypt, section, n_sections, comp);
707 else
708 return crypt_journal(ic, encrypt, section, n_sections, comp);
709}
710
711static void complete_journal_io(unsigned long error, void *context)
712{
713 struct journal_completion *comp = context;
714 if (unlikely(error != 0))
715 dm_integrity_io_error(comp->ic, "writing journal", -EIO);
716 complete_journal_op(comp);
717}
718
719static void rw_journal(struct dm_integrity_c *ic, int op, int op_flags, unsigned section,
720 unsigned n_sections, struct journal_completion *comp)
721{
722 struct dm_io_request io_req;
723 struct dm_io_region io_loc;
724 unsigned sector, n_sectors, pl_index, pl_offset;
725 int r;
726
727 if (unlikely(dm_integrity_failed(ic))) {
728 if (comp)
729 complete_journal_io(-1UL, comp);
730 return;
731 }
732
733 sector = section * ic->journal_section_sectors;
734 n_sectors = n_sections * ic->journal_section_sectors;
735
736 pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
737 pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
738
739 io_req.bi_op = op;
740 io_req.bi_op_flags = op_flags;
741 io_req.mem.type = DM_IO_PAGE_LIST;
742 if (ic->journal_io)
743 io_req.mem.ptr.pl = &ic->journal_io[pl_index];
744 else
745 io_req.mem.ptr.pl = &ic->journal[pl_index];
746 io_req.mem.offset = pl_offset;
747 if (likely(comp != NULL)) {
748 io_req.notify.fn = complete_journal_io;
749 io_req.notify.context = comp;
750 } else {
751 io_req.notify.fn = NULL;
752 }
753 io_req.client = ic->io;
754 io_loc.bdev = ic->dev->bdev;
755 io_loc.sector = ic->start + SB_SECTORS + sector;
756 io_loc.count = n_sectors;
757
758 r = dm_io(&io_req, 1, &io_loc, NULL);
759 if (unlikely(r)) {
760 dm_integrity_io_error(ic, op == REQ_OP_READ ? "reading journal" : "writing journal", r);
761 if (comp) {
762 WARN_ONCE(1, "asynchronous dm_io failed: %d", r);
763 complete_journal_io(-1UL, comp);
764 }
765 }
766}
767
768static void write_journal(struct dm_integrity_c *ic, unsigned commit_start, unsigned commit_sections)
769{
770 struct journal_completion io_comp;
771 struct journal_completion crypt_comp_1;
772 struct journal_completion crypt_comp_2;
773 unsigned i;
774
775 io_comp.ic = ic;
776 io_comp.comp = COMPLETION_INITIALIZER_ONSTACK(io_comp.comp);
777
778 if (commit_start + commit_sections <= ic->journal_sections) {
779 io_comp.in_flight = (atomic_t)ATOMIC_INIT(1);
780 if (ic->journal_io) {
781 crypt_comp_1.ic = ic;
782 crypt_comp_1.comp = COMPLETION_INITIALIZER_ONSTACK(crypt_comp_1.comp);
783 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
784 encrypt_journal(ic, true, commit_start, commit_sections, &crypt_comp_1);
785 wait_for_completion_io(&crypt_comp_1.comp);
786 } else {
787 for (i = 0; i < commit_sections; i++)
788 rw_section_mac(ic, commit_start + i, true);
789 }
Jan Karaff0361b2017-05-31 09:44:32 +0200790 rw_journal(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, commit_start,
791 commit_sections, &io_comp);
Mikulas Patocka7eada902017-01-04 20:23:53 +0100792 } else {
793 unsigned to_end;
794 io_comp.in_flight = (atomic_t)ATOMIC_INIT(2);
795 to_end = ic->journal_sections - commit_start;
796 if (ic->journal_io) {
797 crypt_comp_1.ic = ic;
798 crypt_comp_1.comp = COMPLETION_INITIALIZER_ONSTACK(crypt_comp_1.comp);
799 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
800 encrypt_journal(ic, true, commit_start, to_end, &crypt_comp_1);
801 if (try_wait_for_completion(&crypt_comp_1.comp)) {
802 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
803 crypt_comp_1.comp = COMPLETION_INITIALIZER_ONSTACK(crypt_comp_1.comp);
804 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
805 encrypt_journal(ic, true, 0, commit_sections - to_end, &crypt_comp_1);
806 wait_for_completion_io(&crypt_comp_1.comp);
807 } else {
808 crypt_comp_2.ic = ic;
809 crypt_comp_2.comp = COMPLETION_INITIALIZER_ONSTACK(crypt_comp_2.comp);
810 crypt_comp_2.in_flight = (atomic_t)ATOMIC_INIT(0);
811 encrypt_journal(ic, true, 0, commit_sections - to_end, &crypt_comp_2);
812 wait_for_completion_io(&crypt_comp_1.comp);
813 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
814 wait_for_completion_io(&crypt_comp_2.comp);
815 }
816 } else {
817 for (i = 0; i < to_end; i++)
818 rw_section_mac(ic, commit_start + i, true);
819 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
820 for (i = 0; i < commit_sections - to_end; i++)
821 rw_section_mac(ic, i, true);
822 }
823 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, 0, commit_sections - to_end, &io_comp);
824 }
825
826 wait_for_completion_io(&io_comp.comp);
827}
828
829static void copy_from_journal(struct dm_integrity_c *ic, unsigned section, unsigned offset,
830 unsigned n_sectors, sector_t target, io_notify_fn fn, void *data)
831{
832 struct dm_io_request io_req;
833 struct dm_io_region io_loc;
834 int r;
835 unsigned sector, pl_index, pl_offset;
836
Mikulas Patocka9d609f82017-04-18 16:51:52 -0400837 BUG_ON((target | n_sectors | offset) & (unsigned)(ic->sectors_per_block - 1));
838
Mikulas Patocka7eada902017-01-04 20:23:53 +0100839 if (unlikely(dm_integrity_failed(ic))) {
840 fn(-1UL, data);
841 return;
842 }
843
844 sector = section * ic->journal_section_sectors + JOURNAL_BLOCK_SECTORS + offset;
845
846 pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
847 pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
848
849 io_req.bi_op = REQ_OP_WRITE;
850 io_req.bi_op_flags = 0;
851 io_req.mem.type = DM_IO_PAGE_LIST;
852 io_req.mem.ptr.pl = &ic->journal[pl_index];
853 io_req.mem.offset = pl_offset;
854 io_req.notify.fn = fn;
855 io_req.notify.context = data;
856 io_req.client = ic->io;
857 io_loc.bdev = ic->dev->bdev;
858 io_loc.sector = ic->start + target;
859 io_loc.count = n_sectors;
860
861 r = dm_io(&io_req, 1, &io_loc, NULL);
862 if (unlikely(r)) {
863 WARN_ONCE(1, "asynchronous dm_io failed: %d", r);
864 fn(-1UL, data);
865 }
866}
867
868static bool add_new_range(struct dm_integrity_c *ic, struct dm_integrity_range *new_range)
869{
870 struct rb_node **n = &ic->in_progress.rb_node;
871 struct rb_node *parent;
872
Mikulas Patocka9d609f82017-04-18 16:51:52 -0400873 BUG_ON((new_range->logical_sector | new_range->n_sectors) & (unsigned)(ic->sectors_per_block - 1));
874
Mikulas Patocka7eada902017-01-04 20:23:53 +0100875 parent = NULL;
876
877 while (*n) {
878 struct dm_integrity_range *range = container_of(*n, struct dm_integrity_range, node);
879
880 parent = *n;
881 if (new_range->logical_sector + new_range->n_sectors <= range->logical_sector) {
882 n = &range->node.rb_left;
883 } else if (new_range->logical_sector >= range->logical_sector + range->n_sectors) {
884 n = &range->node.rb_right;
885 } else {
886 return false;
887 }
888 }
889
890 rb_link_node(&new_range->node, parent, n);
891 rb_insert_color(&new_range->node, &ic->in_progress);
892
893 return true;
894}
895
896static void remove_range_unlocked(struct dm_integrity_c *ic, struct dm_integrity_range *range)
897{
898 rb_erase(&range->node, &ic->in_progress);
899 wake_up_locked(&ic->endio_wait);
900}
901
902static void remove_range(struct dm_integrity_c *ic, struct dm_integrity_range *range)
903{
904 unsigned long flags;
905
906 spin_lock_irqsave(&ic->endio_wait.lock, flags);
907 remove_range_unlocked(ic, range);
908 spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
909}
910
911static void init_journal_node(struct journal_node *node)
912{
913 RB_CLEAR_NODE(&node->node);
914 node->sector = (sector_t)-1;
915}
916
917static void add_journal_node(struct dm_integrity_c *ic, struct journal_node *node, sector_t sector)
918{
919 struct rb_node **link;
920 struct rb_node *parent;
921
922 node->sector = sector;
923 BUG_ON(!RB_EMPTY_NODE(&node->node));
924
925 link = &ic->journal_tree_root.rb_node;
926 parent = NULL;
927
928 while (*link) {
929 struct journal_node *j;
930 parent = *link;
931 j = container_of(parent, struct journal_node, node);
932 if (sector < j->sector)
933 link = &j->node.rb_left;
934 else
935 link = &j->node.rb_right;
936 }
937
938 rb_link_node(&node->node, parent, link);
939 rb_insert_color(&node->node, &ic->journal_tree_root);
940}
941
942static void remove_journal_node(struct dm_integrity_c *ic, struct journal_node *node)
943{
944 BUG_ON(RB_EMPTY_NODE(&node->node));
945 rb_erase(&node->node, &ic->journal_tree_root);
946 init_journal_node(node);
947}
948
949#define NOT_FOUND (-1U)
950
951static unsigned find_journal_node(struct dm_integrity_c *ic, sector_t sector, sector_t *next_sector)
952{
953 struct rb_node *n = ic->journal_tree_root.rb_node;
954 unsigned found = NOT_FOUND;
955 *next_sector = (sector_t)-1;
956 while (n) {
957 struct journal_node *j = container_of(n, struct journal_node, node);
958 if (sector == j->sector) {
959 found = j - ic->journal_tree;
960 }
961 if (sector < j->sector) {
962 *next_sector = j->sector;
963 n = j->node.rb_left;
964 } else {
965 n = j->node.rb_right;
966 }
967 }
968
969 return found;
970}
971
972static bool test_journal_node(struct dm_integrity_c *ic, unsigned pos, sector_t sector)
973{
974 struct journal_node *node, *next_node;
975 struct rb_node *next;
976
977 if (unlikely(pos >= ic->journal_entries))
978 return false;
979 node = &ic->journal_tree[pos];
980 if (unlikely(RB_EMPTY_NODE(&node->node)))
981 return false;
982 if (unlikely(node->sector != sector))
983 return false;
984
985 next = rb_next(&node->node);
986 if (unlikely(!next))
987 return true;
988
989 next_node = container_of(next, struct journal_node, node);
990 return next_node->sector != sector;
991}
992
993static bool find_newer_committed_node(struct dm_integrity_c *ic, struct journal_node *node)
994{
995 struct rb_node *next;
996 struct journal_node *next_node;
997 unsigned next_section;
998
999 BUG_ON(RB_EMPTY_NODE(&node->node));
1000
1001 next = rb_next(&node->node);
1002 if (unlikely(!next))
1003 return false;
1004
1005 next_node = container_of(next, struct journal_node, node);
1006
1007 if (next_node->sector != node->sector)
1008 return false;
1009
1010 next_section = (unsigned)(next_node - ic->journal_tree) / ic->journal_section_entries;
1011 if (next_section >= ic->committed_section &&
1012 next_section < ic->committed_section + ic->n_committed_sections)
1013 return true;
1014 if (next_section + ic->journal_sections < ic->committed_section + ic->n_committed_sections)
1015 return true;
1016
1017 return false;
1018}
1019
1020#define TAG_READ 0
1021#define TAG_WRITE 1
1022#define TAG_CMP 2
1023
1024static int dm_integrity_rw_tag(struct dm_integrity_c *ic, unsigned char *tag, sector_t *metadata_block,
1025 unsigned *metadata_offset, unsigned total_size, int op)
1026{
1027 do {
1028 unsigned char *data, *dp;
1029 struct dm_buffer *b;
1030 unsigned to_copy;
1031 int r;
1032
1033 r = dm_integrity_failed(ic);
1034 if (unlikely(r))
1035 return r;
1036
1037 data = dm_bufio_read(ic->bufio, *metadata_block, &b);
1038 if (unlikely(IS_ERR(data)))
1039 return PTR_ERR(data);
1040
1041 to_copy = min((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - *metadata_offset, total_size);
1042 dp = data + *metadata_offset;
1043 if (op == TAG_READ) {
1044 memcpy(tag, dp, to_copy);
1045 } else if (op == TAG_WRITE) {
1046 memcpy(dp, tag, to_copy);
Mikulas Patocka1e3b21c2017-04-30 17:31:22 -04001047 dm_bufio_mark_partial_buffer_dirty(b, *metadata_offset, *metadata_offset + to_copy);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001048 } else {
1049 /* e.g.: op == TAG_CMP */
1050 if (unlikely(memcmp(dp, tag, to_copy))) {
1051 unsigned i;
1052
1053 for (i = 0; i < to_copy; i++) {
1054 if (dp[i] != tag[i])
1055 break;
1056 total_size--;
1057 }
1058 dm_bufio_release(b);
1059 return total_size;
1060 }
1061 }
1062 dm_bufio_release(b);
1063
1064 tag += to_copy;
1065 *metadata_offset += to_copy;
1066 if (unlikely(*metadata_offset == 1U << SECTOR_SHIFT << ic->log2_buffer_sectors)) {
1067 (*metadata_block)++;
1068 *metadata_offset = 0;
1069 }
1070 total_size -= to_copy;
1071 } while (unlikely(total_size));
1072
1073 return 0;
1074}
1075
1076static void dm_integrity_flush_buffers(struct dm_integrity_c *ic)
1077{
1078 int r;
1079 r = dm_bufio_write_dirty_buffers(ic->bufio);
1080 if (unlikely(r))
1081 dm_integrity_io_error(ic, "writing tags", r);
1082}
1083
1084static void sleep_on_endio_wait(struct dm_integrity_c *ic)
1085{
1086 DECLARE_WAITQUEUE(wait, current);
1087 __add_wait_queue(&ic->endio_wait, &wait);
1088 __set_current_state(TASK_UNINTERRUPTIBLE);
1089 spin_unlock_irq(&ic->endio_wait.lock);
1090 io_schedule();
1091 spin_lock_irq(&ic->endio_wait.lock);
1092 __remove_wait_queue(&ic->endio_wait, &wait);
1093}
1094
1095static void autocommit_fn(unsigned long data)
1096{
1097 struct dm_integrity_c *ic = (struct dm_integrity_c *)data;
1098
1099 if (likely(!dm_integrity_failed(ic)))
1100 queue_work(ic->commit_wq, &ic->commit_work);
1101}
1102
1103static void schedule_autocommit(struct dm_integrity_c *ic)
1104{
1105 if (!timer_pending(&ic->autocommit_timer))
1106 mod_timer(&ic->autocommit_timer, jiffies + ic->autocommit_jiffies);
1107}
1108
1109static void submit_flush_bio(struct dm_integrity_c *ic, struct dm_integrity_io *dio)
1110{
1111 struct bio *bio;
Mike Snitzer7def52b2017-06-19 10:55:47 -04001112 unsigned long flags;
1113
1114 spin_lock_irqsave(&ic->endio_wait.lock, flags);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001115 bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1116 bio_list_add(&ic->flush_bio_list, bio);
Mike Snitzer7def52b2017-06-19 10:55:47 -04001117 spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
1118
Mikulas Patocka7eada902017-01-04 20:23:53 +01001119 queue_work(ic->commit_wq, &ic->commit_work);
1120}
1121
1122static void do_endio(struct dm_integrity_c *ic, struct bio *bio)
1123{
1124 int r = dm_integrity_failed(ic);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001125 if (unlikely(r) && !bio->bi_status)
1126 bio->bi_status = errno_to_blk_status(r);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001127 bio_endio(bio);
1128}
1129
1130static void do_endio_flush(struct dm_integrity_c *ic, struct dm_integrity_io *dio)
1131{
1132 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1133
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001134 if (unlikely(dio->fua) && likely(!bio->bi_status) && likely(!dm_integrity_failed(ic)))
Mikulas Patocka7eada902017-01-04 20:23:53 +01001135 submit_flush_bio(ic, dio);
1136 else
1137 do_endio(ic, bio);
1138}
1139
1140static void dec_in_flight(struct dm_integrity_io *dio)
1141{
1142 if (atomic_dec_and_test(&dio->in_flight)) {
1143 struct dm_integrity_c *ic = dio->ic;
1144 struct bio *bio;
1145
1146 remove_range(ic, &dio->range);
1147
1148 if (unlikely(dio->write))
1149 schedule_autocommit(ic);
1150
1151 bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1152
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001153 if (unlikely(dio->bi_status) && !bio->bi_status)
1154 bio->bi_status = dio->bi_status;
1155 if (likely(!bio->bi_status) && unlikely(bio_sectors(bio) != dio->range.n_sectors)) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01001156 dio->range.logical_sector += dio->range.n_sectors;
1157 bio_advance(bio, dio->range.n_sectors << SECTOR_SHIFT);
1158 INIT_WORK(&dio->work, integrity_bio_wait);
1159 queue_work(ic->wait_wq, &dio->work);
1160 return;
1161 }
1162 do_endio_flush(ic, dio);
1163 }
1164}
1165
1166static void integrity_end_io(struct bio *bio)
1167{
1168 struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
1169
1170 bio->bi_iter = dio->orig_bi_iter;
1171 bio->bi_bdev = dio->orig_bi_bdev;
1172 if (dio->orig_bi_integrity) {
1173 bio->bi_integrity = dio->orig_bi_integrity;
1174 bio->bi_opf |= REQ_INTEGRITY;
1175 }
1176 bio->bi_end_io = dio->orig_bi_end_io;
1177
1178 if (dio->completion)
1179 complete(dio->completion);
1180
1181 dec_in_flight(dio);
1182}
1183
1184static void integrity_sector_checksum(struct dm_integrity_c *ic, sector_t sector,
1185 const char *data, char *result)
1186{
1187 __u64 sector_le = cpu_to_le64(sector);
1188 SHASH_DESC_ON_STACK(req, ic->internal_hash);
1189 int r;
1190 unsigned digest_size;
1191
1192 req->tfm = ic->internal_hash;
1193 req->flags = 0;
1194
1195 r = crypto_shash_init(req);
1196 if (unlikely(r < 0)) {
1197 dm_integrity_io_error(ic, "crypto_shash_init", r);
1198 goto failed;
1199 }
1200
1201 r = crypto_shash_update(req, (const __u8 *)&sector_le, sizeof sector_le);
1202 if (unlikely(r < 0)) {
1203 dm_integrity_io_error(ic, "crypto_shash_update", r);
1204 goto failed;
1205 }
1206
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001207 r = crypto_shash_update(req, data, ic->sectors_per_block << SECTOR_SHIFT);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001208 if (unlikely(r < 0)) {
1209 dm_integrity_io_error(ic, "crypto_shash_update", r);
1210 goto failed;
1211 }
1212
1213 r = crypto_shash_final(req, result);
1214 if (unlikely(r < 0)) {
1215 dm_integrity_io_error(ic, "crypto_shash_final", r);
1216 goto failed;
1217 }
1218
1219 digest_size = crypto_shash_digestsize(ic->internal_hash);
1220 if (unlikely(digest_size < ic->tag_size))
1221 memset(result + digest_size, 0, ic->tag_size - digest_size);
1222
1223 return;
1224
1225failed:
1226 /* this shouldn't happen anyway, the hash functions have no reason to fail */
1227 get_random_bytes(result, ic->tag_size);
1228}
1229
1230static void integrity_metadata(struct work_struct *w)
1231{
1232 struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work);
1233 struct dm_integrity_c *ic = dio->ic;
1234
1235 int r;
1236
1237 if (ic->internal_hash) {
1238 struct bvec_iter iter;
1239 struct bio_vec bv;
1240 unsigned digest_size = crypto_shash_digestsize(ic->internal_hash);
1241 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1242 char *checksums;
Mikulas Patocka56b67a42017-04-18 16:51:50 -04001243 unsigned extra_space = unlikely(digest_size > ic->tag_size) ? digest_size - ic->tag_size : 0;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001244 char checksums_onstack[ic->tag_size + extra_space];
1245 unsigned sectors_to_process = dio->range.n_sectors;
1246 sector_t sector = dio->range.logical_sector;
1247
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04001248 if (unlikely(ic->mode == 'R'))
1249 goto skip_io;
1250
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001251 checksums = kmalloc((PAGE_SIZE >> SECTOR_SHIFT >> ic->sb->log2_sectors_per_block) * ic->tag_size + extra_space,
Mikulas Patocka7eada902017-01-04 20:23:53 +01001252 GFP_NOIO | __GFP_NORETRY | __GFP_NOWARN);
1253 if (!checksums)
1254 checksums = checksums_onstack;
1255
1256 __bio_for_each_segment(bv, bio, iter, dio->orig_bi_iter) {
1257 unsigned pos;
1258 char *mem, *checksums_ptr;
1259
1260again:
1261 mem = (char *)kmap_atomic(bv.bv_page) + bv.bv_offset;
1262 pos = 0;
1263 checksums_ptr = checksums;
1264 do {
1265 integrity_sector_checksum(ic, sector, mem + pos, checksums_ptr);
1266 checksums_ptr += ic->tag_size;
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001267 sectors_to_process -= ic->sectors_per_block;
1268 pos += ic->sectors_per_block << SECTOR_SHIFT;
1269 sector += ic->sectors_per_block;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001270 } while (pos < bv.bv_len && sectors_to_process && checksums != checksums_onstack);
1271 kunmap_atomic(mem);
1272
1273 r = dm_integrity_rw_tag(ic, checksums, &dio->metadata_block, &dio->metadata_offset,
1274 checksums_ptr - checksums, !dio->write ? TAG_CMP : TAG_WRITE);
1275 if (unlikely(r)) {
1276 if (r > 0) {
1277 DMERR("Checksum failed at sector 0x%llx",
1278 (unsigned long long)(sector - ((r + ic->tag_size - 1) / ic->tag_size)));
1279 r = -EILSEQ;
Mikulas Patocka3f2e5392017-07-21 12:00:00 -04001280 atomic64_inc(&ic->number_of_mismatches);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001281 }
1282 if (likely(checksums != checksums_onstack))
1283 kfree(checksums);
1284 goto error;
1285 }
1286
1287 if (!sectors_to_process)
1288 break;
1289
1290 if (unlikely(pos < bv.bv_len)) {
1291 bv.bv_offset += pos;
1292 bv.bv_len -= pos;
1293 goto again;
1294 }
1295 }
1296
1297 if (likely(checksums != checksums_onstack))
1298 kfree(checksums);
1299 } else {
1300 struct bio_integrity_payload *bip = dio->orig_bi_integrity;
1301
1302 if (bip) {
1303 struct bio_vec biv;
1304 struct bvec_iter iter;
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001305 unsigned data_to_process = dio->range.n_sectors;
1306 sector_to_block(ic, data_to_process);
1307 data_to_process *= ic->tag_size;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001308
1309 bip_for_each_vec(biv, bip, iter) {
1310 unsigned char *tag;
1311 unsigned this_len;
1312
1313 BUG_ON(PageHighMem(biv.bv_page));
1314 tag = lowmem_page_address(biv.bv_page) + biv.bv_offset;
1315 this_len = min(biv.bv_len, data_to_process);
1316 r = dm_integrity_rw_tag(ic, tag, &dio->metadata_block, &dio->metadata_offset,
1317 this_len, !dio->write ? TAG_READ : TAG_WRITE);
1318 if (unlikely(r))
1319 goto error;
1320 data_to_process -= this_len;
1321 if (!data_to_process)
1322 break;
1323 }
1324 }
1325 }
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04001326skip_io:
Mikulas Patocka7eada902017-01-04 20:23:53 +01001327 dec_in_flight(dio);
1328 return;
1329error:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001330 dio->bi_status = errno_to_blk_status(r);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001331 dec_in_flight(dio);
1332}
1333
1334static int dm_integrity_map(struct dm_target *ti, struct bio *bio)
1335{
1336 struct dm_integrity_c *ic = ti->private;
1337 struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001338 struct bio_integrity_payload *bip;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001339
1340 sector_t area, offset;
1341
1342 dio->ic = ic;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001343 dio->bi_status = 0;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001344
1345 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1346 submit_flush_bio(ic, dio);
1347 return DM_MAPIO_SUBMITTED;
1348 }
1349
1350 dio->range.logical_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
1351 dio->write = bio_op(bio) == REQ_OP_WRITE;
1352 dio->fua = dio->write && bio->bi_opf & REQ_FUA;
1353 if (unlikely(dio->fua)) {
1354 /*
1355 * Don't pass down the FUA flag because we have to flush
1356 * disk cache anyway.
1357 */
1358 bio->bi_opf &= ~REQ_FUA;
1359 }
1360 if (unlikely(dio->range.logical_sector + bio_sectors(bio) > ic->provided_data_sectors)) {
1361 DMERR("Too big sector number: 0x%llx + 0x%x > 0x%llx",
1362 (unsigned long long)dio->range.logical_sector, bio_sectors(bio),
1363 (unsigned long long)ic->provided_data_sectors);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001364 return DM_MAPIO_KILL;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001365 }
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001366 if (unlikely((dio->range.logical_sector | bio_sectors(bio)) & (unsigned)(ic->sectors_per_block - 1))) {
1367 DMERR("Bio not aligned on %u sectors: 0x%llx, 0x%x",
1368 ic->sectors_per_block,
1369 (unsigned long long)dio->range.logical_sector, bio_sectors(bio));
Christoph Hellwig846785e2017-06-03 09:38:02 +02001370 return DM_MAPIO_KILL;
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001371 }
1372
1373 if (ic->sectors_per_block > 1) {
1374 struct bvec_iter iter;
1375 struct bio_vec bv;
1376 bio_for_each_segment(bv, bio, iter) {
1377 if (unlikely((bv.bv_offset | bv.bv_len) & ((ic->sectors_per_block << SECTOR_SHIFT) - 1))) {
1378 DMERR("Bio vector (%u,%u) is not aligned on %u-sector boundary",
1379 bv.bv_offset, bv.bv_len, ic->sectors_per_block);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001380 return DM_MAPIO_KILL;
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001381 }
1382 }
1383 }
1384
1385 bip = bio_integrity(bio);
1386 if (!ic->internal_hash) {
1387 if (bip) {
1388 unsigned wanted_tag_size = bio_sectors(bio) >> ic->sb->log2_sectors_per_block;
1389 if (ic->log2_tag_size >= 0)
1390 wanted_tag_size <<= ic->log2_tag_size;
1391 else
1392 wanted_tag_size *= ic->tag_size;
1393 if (unlikely(wanted_tag_size != bip->bip_iter.bi_size)) {
1394 DMERR("Invalid integrity data size %u, expected %u", bip->bip_iter.bi_size, wanted_tag_size);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001395 return DM_MAPIO_KILL;
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001396 }
1397 }
1398 } else {
1399 if (unlikely(bip != NULL)) {
1400 DMERR("Unexpected integrity data when using internal hash");
Christoph Hellwig846785e2017-06-03 09:38:02 +02001401 return DM_MAPIO_KILL;
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001402 }
1403 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01001404
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04001405 if (unlikely(ic->mode == 'R') && unlikely(dio->write))
Christoph Hellwig846785e2017-06-03 09:38:02 +02001406 return DM_MAPIO_KILL;
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04001407
Mikulas Patocka7eada902017-01-04 20:23:53 +01001408 get_area_and_offset(ic, dio->range.logical_sector, &area, &offset);
1409 dio->metadata_block = get_metadata_sector_and_offset(ic, area, offset, &dio->metadata_offset);
1410 bio->bi_iter.bi_sector = get_data_sector(ic, area, offset);
1411
1412 dm_integrity_map_continue(dio, true);
1413 return DM_MAPIO_SUBMITTED;
1414}
1415
1416static bool __journal_read_write(struct dm_integrity_io *dio, struct bio *bio,
1417 unsigned journal_section, unsigned journal_entry)
1418{
1419 struct dm_integrity_c *ic = dio->ic;
1420 sector_t logical_sector;
1421 unsigned n_sectors;
1422
1423 logical_sector = dio->range.logical_sector;
1424 n_sectors = dio->range.n_sectors;
1425 do {
1426 struct bio_vec bv = bio_iovec(bio);
1427 char *mem;
1428
1429 if (unlikely(bv.bv_len >> SECTOR_SHIFT > n_sectors))
1430 bv.bv_len = n_sectors << SECTOR_SHIFT;
1431 n_sectors -= bv.bv_len >> SECTOR_SHIFT;
1432 bio_advance_iter(bio, &bio->bi_iter, bv.bv_len);
1433retry_kmap:
1434 mem = kmap_atomic(bv.bv_page);
1435 if (likely(dio->write))
1436 flush_dcache_page(bv.bv_page);
1437
1438 do {
1439 struct journal_entry *je = access_journal_entry(ic, journal_section, journal_entry);
1440
1441 if (unlikely(!dio->write)) {
1442 struct journal_sector *js;
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001443 char *mem_ptr;
1444 unsigned s;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001445
1446 if (unlikely(journal_entry_is_inprogress(je))) {
1447 flush_dcache_page(bv.bv_page);
1448 kunmap_atomic(mem);
1449
1450 __io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je));
1451 goto retry_kmap;
1452 }
1453 smp_rmb();
1454 BUG_ON(journal_entry_get_sector(je) != logical_sector);
1455 js = access_journal_data(ic, journal_section, journal_entry);
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001456 mem_ptr = mem + bv.bv_offset;
1457 s = 0;
1458 do {
1459 memcpy(mem_ptr, js, JOURNAL_SECTOR_DATA);
1460 *(commit_id_t *)(mem_ptr + JOURNAL_SECTOR_DATA) = je->last_bytes[s];
1461 js++;
1462 mem_ptr += 1 << SECTOR_SHIFT;
1463 } while (++s < ic->sectors_per_block);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001464#ifdef INTERNAL_VERIFY
1465 if (ic->internal_hash) {
1466 char checksums_onstack[max(crypto_shash_digestsize(ic->internal_hash), ic->tag_size)];
1467
1468 integrity_sector_checksum(ic, logical_sector, mem + bv.bv_offset, checksums_onstack);
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001469 if (unlikely(memcmp(checksums_onstack, journal_entry_tag(ic, je), ic->tag_size))) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01001470 DMERR("Checksum failed when reading from journal, at sector 0x%llx",
1471 (unsigned long long)logical_sector);
1472 }
1473 }
1474#endif
1475 }
1476
1477 if (!ic->internal_hash) {
1478 struct bio_integrity_payload *bip = bio_integrity(bio);
1479 unsigned tag_todo = ic->tag_size;
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001480 char *tag_ptr = journal_entry_tag(ic, je);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001481
1482 if (bip) do {
1483 struct bio_vec biv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter);
1484 unsigned tag_now = min(biv.bv_len, tag_todo);
1485 char *tag_addr;
1486 BUG_ON(PageHighMem(biv.bv_page));
1487 tag_addr = lowmem_page_address(biv.bv_page) + biv.bv_offset;
1488 if (likely(dio->write))
1489 memcpy(tag_ptr, tag_addr, tag_now);
1490 else
1491 memcpy(tag_addr, tag_ptr, tag_now);
1492 bvec_iter_advance(bip->bip_vec, &bip->bip_iter, tag_now);
1493 tag_ptr += tag_now;
1494 tag_todo -= tag_now;
1495 } while (unlikely(tag_todo)); else {
1496 if (likely(dio->write))
1497 memset(tag_ptr, 0, tag_todo);
1498 }
1499 }
1500
1501 if (likely(dio->write)) {
1502 struct journal_sector *js;
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001503 unsigned s;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001504
1505 js = access_journal_data(ic, journal_section, journal_entry);
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001506 memcpy(js, mem + bv.bv_offset, ic->sectors_per_block << SECTOR_SHIFT);
1507
1508 s = 0;
1509 do {
1510 je->last_bytes[s] = js[s].commit_id;
1511 } while (++s < ic->sectors_per_block);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001512
1513 if (ic->internal_hash) {
1514 unsigned digest_size = crypto_shash_digestsize(ic->internal_hash);
1515 if (unlikely(digest_size > ic->tag_size)) {
1516 char checksums_onstack[digest_size];
1517 integrity_sector_checksum(ic, logical_sector, (char *)js, checksums_onstack);
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001518 memcpy(journal_entry_tag(ic, je), checksums_onstack, ic->tag_size);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001519 } else
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001520 integrity_sector_checksum(ic, logical_sector, (char *)js, journal_entry_tag(ic, je));
Mikulas Patocka7eada902017-01-04 20:23:53 +01001521 }
1522
1523 journal_entry_set_sector(je, logical_sector);
1524 }
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001525 logical_sector += ic->sectors_per_block;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001526
1527 journal_entry++;
1528 if (unlikely(journal_entry == ic->journal_section_entries)) {
1529 journal_entry = 0;
1530 journal_section++;
1531 wraparound_section(ic, &journal_section);
1532 }
1533
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001534 bv.bv_offset += ic->sectors_per_block << SECTOR_SHIFT;
1535 } while (bv.bv_len -= ic->sectors_per_block << SECTOR_SHIFT);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001536
1537 if (unlikely(!dio->write))
1538 flush_dcache_page(bv.bv_page);
1539 kunmap_atomic(mem);
1540 } while (n_sectors);
1541
1542 if (likely(dio->write)) {
1543 smp_mb();
1544 if (unlikely(waitqueue_active(&ic->copy_to_journal_wait)))
1545 wake_up(&ic->copy_to_journal_wait);
1546 if (ACCESS_ONCE(ic->free_sectors) <= ic->free_sectors_threshold) {
1547 queue_work(ic->commit_wq, &ic->commit_work);
1548 } else {
1549 schedule_autocommit(ic);
1550 }
1551 } else {
1552 remove_range(ic, &dio->range);
1553 }
1554
1555 if (unlikely(bio->bi_iter.bi_size)) {
1556 sector_t area, offset;
1557
1558 dio->range.logical_sector = logical_sector;
1559 get_area_and_offset(ic, dio->range.logical_sector, &area, &offset);
1560 dio->metadata_block = get_metadata_sector_and_offset(ic, area, offset, &dio->metadata_offset);
1561 return true;
1562 }
1563
1564 return false;
1565}
1566
1567static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map)
1568{
1569 struct dm_integrity_c *ic = dio->ic;
1570 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1571 unsigned journal_section, journal_entry;
1572 unsigned journal_read_pos;
1573 struct completion read_comp;
1574 bool need_sync_io = ic->internal_hash && !dio->write;
1575
1576 if (need_sync_io && from_map) {
1577 INIT_WORK(&dio->work, integrity_bio_wait);
1578 queue_work(ic->metadata_wq, &dio->work);
1579 return;
1580 }
1581
1582lock_retry:
1583 spin_lock_irq(&ic->endio_wait.lock);
1584retry:
1585 if (unlikely(dm_integrity_failed(ic))) {
1586 spin_unlock_irq(&ic->endio_wait.lock);
1587 do_endio(ic, bio);
1588 return;
1589 }
1590 dio->range.n_sectors = bio_sectors(bio);
1591 journal_read_pos = NOT_FOUND;
1592 if (likely(ic->mode == 'J')) {
1593 if (dio->write) {
1594 unsigned next_entry, i, pos;
Mikulas Patocka9dd59722017-07-19 11:23:40 -04001595 unsigned ws, we, range_sectors;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001596
Mikulas Patocka9dd59722017-07-19 11:23:40 -04001597 dio->range.n_sectors = min(dio->range.n_sectors,
1598 ic->free_sectors << ic->sb->log2_sectors_per_block);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001599 if (unlikely(!dio->range.n_sectors))
1600 goto sleep;
Mikulas Patocka9dd59722017-07-19 11:23:40 -04001601 range_sectors = dio->range.n_sectors >> ic->sb->log2_sectors_per_block;
1602 ic->free_sectors -= range_sectors;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001603 journal_section = ic->free_section;
1604 journal_entry = ic->free_section_entry;
1605
Mikulas Patocka9dd59722017-07-19 11:23:40 -04001606 next_entry = ic->free_section_entry + range_sectors;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001607 ic->free_section_entry = next_entry % ic->journal_section_entries;
1608 ic->free_section += next_entry / ic->journal_section_entries;
1609 ic->n_uncommitted_sections += next_entry / ic->journal_section_entries;
1610 wraparound_section(ic, &ic->free_section);
1611
1612 pos = journal_section * ic->journal_section_entries + journal_entry;
1613 ws = journal_section;
1614 we = journal_entry;
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001615 i = 0;
1616 do {
Mikulas Patocka7eada902017-01-04 20:23:53 +01001617 struct journal_entry *je;
1618
1619 add_journal_node(ic, &ic->journal_tree[pos], dio->range.logical_sector + i);
1620 pos++;
1621 if (unlikely(pos >= ic->journal_entries))
1622 pos = 0;
1623
1624 je = access_journal_entry(ic, ws, we);
1625 BUG_ON(!journal_entry_is_unused(je));
1626 journal_entry_set_inprogress(je);
1627 we++;
1628 if (unlikely(we == ic->journal_section_entries)) {
1629 we = 0;
1630 ws++;
1631 wraparound_section(ic, &ws);
1632 }
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001633 } while ((i += ic->sectors_per_block) < dio->range.n_sectors);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001634
1635 spin_unlock_irq(&ic->endio_wait.lock);
1636 goto journal_read_write;
1637 } else {
1638 sector_t next_sector;
1639 journal_read_pos = find_journal_node(ic, dio->range.logical_sector, &next_sector);
1640 if (likely(journal_read_pos == NOT_FOUND)) {
1641 if (unlikely(dio->range.n_sectors > next_sector - dio->range.logical_sector))
1642 dio->range.n_sectors = next_sector - dio->range.logical_sector;
1643 } else {
1644 unsigned i;
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001645 unsigned jp = journal_read_pos + 1;
1646 for (i = ic->sectors_per_block; i < dio->range.n_sectors; i += ic->sectors_per_block, jp++) {
1647 if (!test_journal_node(ic, jp, dio->range.logical_sector + i))
Mikulas Patocka7eada902017-01-04 20:23:53 +01001648 break;
1649 }
1650 dio->range.n_sectors = i;
1651 }
1652 }
1653 }
1654 if (unlikely(!add_new_range(ic, &dio->range))) {
1655 /*
1656 * We must not sleep in the request routine because it could
1657 * stall bios on current->bio_list.
1658 * So, we offload the bio to a workqueue if we have to sleep.
1659 */
1660sleep:
1661 if (from_map) {
1662 spin_unlock_irq(&ic->endio_wait.lock);
1663 INIT_WORK(&dio->work, integrity_bio_wait);
1664 queue_work(ic->wait_wq, &dio->work);
1665 return;
1666 } else {
1667 sleep_on_endio_wait(ic);
1668 goto retry;
1669 }
1670 }
1671 spin_unlock_irq(&ic->endio_wait.lock);
1672
1673 if (unlikely(journal_read_pos != NOT_FOUND)) {
1674 journal_section = journal_read_pos / ic->journal_section_entries;
1675 journal_entry = journal_read_pos % ic->journal_section_entries;
1676 goto journal_read_write;
1677 }
1678
1679 dio->in_flight = (atomic_t)ATOMIC_INIT(2);
1680
1681 if (need_sync_io) {
1682 read_comp = COMPLETION_INITIALIZER_ONSTACK(read_comp);
1683 dio->completion = &read_comp;
1684 } else
1685 dio->completion = NULL;
1686
1687 dio->orig_bi_iter = bio->bi_iter;
1688
1689 dio->orig_bi_bdev = bio->bi_bdev;
1690 bio->bi_bdev = ic->dev->bdev;
1691
1692 dio->orig_bi_integrity = bio_integrity(bio);
1693 bio->bi_integrity = NULL;
1694 bio->bi_opf &= ~REQ_INTEGRITY;
1695
1696 dio->orig_bi_end_io = bio->bi_end_io;
1697 bio->bi_end_io = integrity_end_io;
1698
1699 bio->bi_iter.bi_size = dio->range.n_sectors << SECTOR_SHIFT;
1700 bio->bi_iter.bi_sector += ic->start;
1701 generic_make_request(bio);
1702
1703 if (need_sync_io) {
1704 wait_for_completion_io(&read_comp);
1705 integrity_metadata(&dio->work);
1706 } else {
1707 INIT_WORK(&dio->work, integrity_metadata);
1708 queue_work(ic->metadata_wq, &dio->work);
1709 }
1710
1711 return;
1712
1713journal_read_write:
1714 if (unlikely(__journal_read_write(dio, bio, journal_section, journal_entry)))
1715 goto lock_retry;
1716
1717 do_endio_flush(ic, dio);
1718}
1719
1720
1721static void integrity_bio_wait(struct work_struct *w)
1722{
1723 struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work);
1724
1725 dm_integrity_map_continue(dio, false);
1726}
1727
1728static void pad_uncommitted(struct dm_integrity_c *ic)
1729{
1730 if (ic->free_section_entry) {
1731 ic->free_sectors -= ic->journal_section_entries - ic->free_section_entry;
1732 ic->free_section_entry = 0;
1733 ic->free_section++;
1734 wraparound_section(ic, &ic->free_section);
1735 ic->n_uncommitted_sections++;
1736 }
Mikulas Patockaaa03a912017-07-21 13:16:06 -04001737 WARN_ON(ic->journal_sections * ic->journal_section_entries !=
1738 (ic->n_uncommitted_sections + ic->n_committed_sections) * ic->journal_section_entries + ic->free_sectors);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001739}
1740
1741static void integrity_commit(struct work_struct *w)
1742{
1743 struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, commit_work);
1744 unsigned commit_start, commit_sections;
1745 unsigned i, j, n;
1746 struct bio *flushes;
1747
1748 del_timer(&ic->autocommit_timer);
1749
1750 spin_lock_irq(&ic->endio_wait.lock);
1751 flushes = bio_list_get(&ic->flush_bio_list);
1752 if (unlikely(ic->mode != 'J')) {
1753 spin_unlock_irq(&ic->endio_wait.lock);
1754 dm_integrity_flush_buffers(ic);
1755 goto release_flush_bios;
1756 }
1757
1758 pad_uncommitted(ic);
1759 commit_start = ic->uncommitted_section;
1760 commit_sections = ic->n_uncommitted_sections;
1761 spin_unlock_irq(&ic->endio_wait.lock);
1762
1763 if (!commit_sections)
1764 goto release_flush_bios;
1765
1766 i = commit_start;
1767 for (n = 0; n < commit_sections; n++) {
1768 for (j = 0; j < ic->journal_section_entries; j++) {
1769 struct journal_entry *je;
1770 je = access_journal_entry(ic, i, j);
1771 io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je));
1772 }
1773 for (j = 0; j < ic->journal_section_sectors; j++) {
1774 struct journal_sector *js;
1775 js = access_journal(ic, i, j);
1776 js->commit_id = dm_integrity_commit_id(ic, i, j, ic->commit_seq);
1777 }
1778 i++;
1779 if (unlikely(i >= ic->journal_sections))
1780 ic->commit_seq = next_commit_seq(ic->commit_seq);
1781 wraparound_section(ic, &i);
1782 }
1783 smp_rmb();
1784
1785 write_journal(ic, commit_start, commit_sections);
1786
1787 spin_lock_irq(&ic->endio_wait.lock);
1788 ic->uncommitted_section += commit_sections;
1789 wraparound_section(ic, &ic->uncommitted_section);
1790 ic->n_uncommitted_sections -= commit_sections;
1791 ic->n_committed_sections += commit_sections;
1792 spin_unlock_irq(&ic->endio_wait.lock);
1793
1794 if (ACCESS_ONCE(ic->free_sectors) <= ic->free_sectors_threshold)
1795 queue_work(ic->writer_wq, &ic->writer_work);
1796
1797release_flush_bios:
1798 while (flushes) {
1799 struct bio *next = flushes->bi_next;
1800 flushes->bi_next = NULL;
1801 do_endio(ic, flushes);
1802 flushes = next;
1803 }
1804}
1805
1806static void complete_copy_from_journal(unsigned long error, void *context)
1807{
1808 struct journal_io *io = context;
1809 struct journal_completion *comp = io->comp;
1810 struct dm_integrity_c *ic = comp->ic;
1811 remove_range(ic, &io->range);
1812 mempool_free(io, ic->journal_io_mempool);
1813 if (unlikely(error != 0))
1814 dm_integrity_io_error(ic, "copying from journal", -EIO);
1815 complete_journal_op(comp);
1816}
1817
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001818static void restore_last_bytes(struct dm_integrity_c *ic, struct journal_sector *js,
1819 struct journal_entry *je)
1820{
1821 unsigned s = 0;
1822 do {
1823 js->commit_id = je->last_bytes[s];
1824 js++;
1825 } while (++s < ic->sectors_per_block);
1826}
1827
Mikulas Patocka7eada902017-01-04 20:23:53 +01001828static void do_journal_write(struct dm_integrity_c *ic, unsigned write_start,
1829 unsigned write_sections, bool from_replay)
1830{
1831 unsigned i, j, n;
1832 struct journal_completion comp;
Mikulas Patockaa7c3e62b2017-07-19 11:24:08 -04001833 struct blk_plug plug;
1834
1835 blk_start_plug(&plug);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001836
1837 comp.ic = ic;
1838 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
1839 comp.comp = COMPLETION_INITIALIZER_ONSTACK(comp.comp);
1840
1841 i = write_start;
1842 for (n = 0; n < write_sections; n++, i++, wraparound_section(ic, &i)) {
1843#ifndef INTERNAL_VERIFY
1844 if (unlikely(from_replay))
1845#endif
1846 rw_section_mac(ic, i, false);
1847 for (j = 0; j < ic->journal_section_entries; j++) {
1848 struct journal_entry *je = access_journal_entry(ic, i, j);
1849 sector_t sec, area, offset;
1850 unsigned k, l, next_loop;
1851 sector_t metadata_block;
1852 unsigned metadata_offset;
1853 struct journal_io *io;
1854
1855 if (journal_entry_is_unused(je))
1856 continue;
1857 BUG_ON(unlikely(journal_entry_is_inprogress(je)) && !from_replay);
1858 sec = journal_entry_get_sector(je);
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001859 if (unlikely(from_replay)) {
1860 if (unlikely(sec & (unsigned)(ic->sectors_per_block - 1))) {
1861 dm_integrity_io_error(ic, "invalid sector in journal", -EIO);
1862 sec &= ~(sector_t)(ic->sectors_per_block - 1);
1863 }
1864 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01001865 get_area_and_offset(ic, sec, &area, &offset);
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001866 restore_last_bytes(ic, access_journal_data(ic, i, j), je);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001867 for (k = j + 1; k < ic->journal_section_entries; k++) {
1868 struct journal_entry *je2 = access_journal_entry(ic, i, k);
1869 sector_t sec2, area2, offset2;
1870 if (journal_entry_is_unused(je2))
1871 break;
1872 BUG_ON(unlikely(journal_entry_is_inprogress(je2)) && !from_replay);
1873 sec2 = journal_entry_get_sector(je2);
1874 get_area_and_offset(ic, sec2, &area2, &offset2);
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001875 if (area2 != area || offset2 != offset + ((k - j) << ic->sb->log2_sectors_per_block))
Mikulas Patocka7eada902017-01-04 20:23:53 +01001876 break;
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001877 restore_last_bytes(ic, access_journal_data(ic, i, k), je2);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001878 }
1879 next_loop = k - 1;
1880
1881 io = mempool_alloc(ic->journal_io_mempool, GFP_NOIO);
1882 io->comp = &comp;
1883 io->range.logical_sector = sec;
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001884 io->range.n_sectors = (k - j) << ic->sb->log2_sectors_per_block;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001885
1886 spin_lock_irq(&ic->endio_wait.lock);
1887 while (unlikely(!add_new_range(ic, &io->range)))
1888 sleep_on_endio_wait(ic);
1889
1890 if (likely(!from_replay)) {
1891 struct journal_node *section_node = &ic->journal_tree[i * ic->journal_section_entries];
1892
1893 /* don't write if there is newer committed sector */
1894 while (j < k && find_newer_committed_node(ic, &section_node[j])) {
1895 struct journal_entry *je2 = access_journal_entry(ic, i, j);
1896
1897 journal_entry_set_unused(je2);
1898 remove_journal_node(ic, &section_node[j]);
1899 j++;
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001900 sec += ic->sectors_per_block;
1901 offset += ic->sectors_per_block;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001902 }
1903 while (j < k && find_newer_committed_node(ic, &section_node[k - 1])) {
1904 struct journal_entry *je2 = access_journal_entry(ic, i, k - 1);
1905
1906 journal_entry_set_unused(je2);
1907 remove_journal_node(ic, &section_node[k - 1]);
1908 k--;
1909 }
1910 if (j == k) {
1911 remove_range_unlocked(ic, &io->range);
1912 spin_unlock_irq(&ic->endio_wait.lock);
1913 mempool_free(io, ic->journal_io_mempool);
1914 goto skip_io;
1915 }
1916 for (l = j; l < k; l++) {
1917 remove_journal_node(ic, &section_node[l]);
1918 }
1919 }
1920 spin_unlock_irq(&ic->endio_wait.lock);
1921
1922 metadata_block = get_metadata_sector_and_offset(ic, area, offset, &metadata_offset);
1923 for (l = j; l < k; l++) {
1924 int r;
1925 struct journal_entry *je2 = access_journal_entry(ic, i, l);
1926
1927 if (
1928#ifndef INTERNAL_VERIFY
1929 unlikely(from_replay) &&
1930#endif
1931 ic->internal_hash) {
Mikulas Patocka56b67a42017-04-18 16:51:50 -04001932 char test_tag[max(crypto_shash_digestsize(ic->internal_hash), ic->tag_size)];
Mikulas Patocka7eada902017-01-04 20:23:53 +01001933
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001934 integrity_sector_checksum(ic, sec + ((l - j) << ic->sb->log2_sectors_per_block),
Mikulas Patocka7eada902017-01-04 20:23:53 +01001935 (char *)access_journal_data(ic, i, l), test_tag);
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001936 if (unlikely(memcmp(test_tag, journal_entry_tag(ic, je2), ic->tag_size)))
Mikulas Patocka7eada902017-01-04 20:23:53 +01001937 dm_integrity_io_error(ic, "tag mismatch when replaying journal", -EILSEQ);
1938 }
1939
1940 journal_entry_set_unused(je2);
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001941 r = dm_integrity_rw_tag(ic, journal_entry_tag(ic, je2), &metadata_block, &metadata_offset,
Mikulas Patocka7eada902017-01-04 20:23:53 +01001942 ic->tag_size, TAG_WRITE);
1943 if (unlikely(r)) {
1944 dm_integrity_io_error(ic, "reading tags", r);
1945 }
1946 }
1947
1948 atomic_inc(&comp.in_flight);
Mikulas Patocka9d609f82017-04-18 16:51:52 -04001949 copy_from_journal(ic, i, j << ic->sb->log2_sectors_per_block,
1950 (k - j) << ic->sb->log2_sectors_per_block,
1951 get_data_sector(ic, area, offset),
Mikulas Patocka7eada902017-01-04 20:23:53 +01001952 complete_copy_from_journal, io);
1953skip_io:
1954 j = next_loop;
1955 }
1956 }
1957
1958 dm_bufio_write_dirty_buffers_async(ic->bufio);
1959
Mikulas Patockaa7c3e62b2017-07-19 11:24:08 -04001960 blk_finish_plug(&plug);
1961
Mikulas Patocka7eada902017-01-04 20:23:53 +01001962 complete_journal_op(&comp);
1963 wait_for_completion_io(&comp.comp);
1964
1965 dm_integrity_flush_buffers(ic);
1966}
1967
1968static void integrity_writer(struct work_struct *w)
1969{
1970 struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, writer_work);
1971 unsigned write_start, write_sections;
1972
1973 unsigned prev_free_sectors;
1974
1975 /* the following test is not needed, but it tests the replay code */
1976 if (ACCESS_ONCE(ic->suspending))
1977 return;
1978
1979 spin_lock_irq(&ic->endio_wait.lock);
1980 write_start = ic->committed_section;
1981 write_sections = ic->n_committed_sections;
1982 spin_unlock_irq(&ic->endio_wait.lock);
1983
1984 if (!write_sections)
1985 return;
1986
1987 do_journal_write(ic, write_start, write_sections, false);
1988
1989 spin_lock_irq(&ic->endio_wait.lock);
1990
1991 ic->committed_section += write_sections;
1992 wraparound_section(ic, &ic->committed_section);
1993 ic->n_committed_sections -= write_sections;
1994
1995 prev_free_sectors = ic->free_sectors;
1996 ic->free_sectors += write_sections * ic->journal_section_entries;
1997 if (unlikely(!prev_free_sectors))
1998 wake_up_locked(&ic->endio_wait);
1999
2000 spin_unlock_irq(&ic->endio_wait.lock);
2001}
2002
2003static void init_journal(struct dm_integrity_c *ic, unsigned start_section,
2004 unsigned n_sections, unsigned char commit_seq)
2005{
2006 unsigned i, j, n;
2007
2008 if (!n_sections)
2009 return;
2010
2011 for (n = 0; n < n_sections; n++) {
2012 i = start_section + n;
2013 wraparound_section(ic, &i);
2014 for (j = 0; j < ic->journal_section_sectors; j++) {
2015 struct journal_sector *js = access_journal(ic, i, j);
2016 memset(&js->entries, 0, JOURNAL_SECTOR_DATA);
2017 js->commit_id = dm_integrity_commit_id(ic, i, j, commit_seq);
2018 }
2019 for (j = 0; j < ic->journal_section_entries; j++) {
2020 struct journal_entry *je = access_journal_entry(ic, i, j);
2021 journal_entry_set_unused(je);
2022 }
2023 }
2024
2025 write_journal(ic, start_section, n_sections);
2026}
2027
2028static int find_commit_seq(struct dm_integrity_c *ic, unsigned i, unsigned j, commit_id_t id)
2029{
2030 unsigned char k;
2031 for (k = 0; k < N_COMMIT_IDS; k++) {
2032 if (dm_integrity_commit_id(ic, i, j, k) == id)
2033 return k;
2034 }
2035 dm_integrity_io_error(ic, "journal commit id", -EIO);
2036 return -EIO;
2037}
2038
2039static void replay_journal(struct dm_integrity_c *ic)
2040{
2041 unsigned i, j;
2042 bool used_commit_ids[N_COMMIT_IDS];
2043 unsigned max_commit_id_sections[N_COMMIT_IDS];
2044 unsigned write_start, write_sections;
2045 unsigned continue_section;
2046 bool journal_empty;
2047 unsigned char unused, last_used, want_commit_seq;
2048
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04002049 if (ic->mode == 'R')
2050 return;
2051
Mikulas Patocka7eada902017-01-04 20:23:53 +01002052 if (ic->journal_uptodate)
2053 return;
2054
2055 last_used = 0;
2056 write_start = 0;
2057
2058 if (!ic->just_formatted) {
2059 DEBUG_print("reading journal\n");
2060 rw_journal(ic, REQ_OP_READ, 0, 0, ic->journal_sections, NULL);
2061 if (ic->journal_io)
2062 DEBUG_bytes(lowmem_page_address(ic->journal_io[0].page), 64, "read journal");
2063 if (ic->journal_io) {
2064 struct journal_completion crypt_comp;
2065 crypt_comp.ic = ic;
2066 crypt_comp.comp = COMPLETION_INITIALIZER_ONSTACK(crypt_comp.comp);
2067 crypt_comp.in_flight = (atomic_t)ATOMIC_INIT(0);
2068 encrypt_journal(ic, false, 0, ic->journal_sections, &crypt_comp);
2069 wait_for_completion(&crypt_comp.comp);
2070 }
2071 DEBUG_bytes(lowmem_page_address(ic->journal[0].page), 64, "decrypted journal");
2072 }
2073
2074 if (dm_integrity_failed(ic))
2075 goto clear_journal;
2076
2077 journal_empty = true;
2078 memset(used_commit_ids, 0, sizeof used_commit_ids);
2079 memset(max_commit_id_sections, 0, sizeof max_commit_id_sections);
2080 for (i = 0; i < ic->journal_sections; i++) {
2081 for (j = 0; j < ic->journal_section_sectors; j++) {
2082 int k;
2083 struct journal_sector *js = access_journal(ic, i, j);
2084 k = find_commit_seq(ic, i, j, js->commit_id);
2085 if (k < 0)
2086 goto clear_journal;
2087 used_commit_ids[k] = true;
2088 max_commit_id_sections[k] = i;
2089 }
2090 if (journal_empty) {
2091 for (j = 0; j < ic->journal_section_entries; j++) {
2092 struct journal_entry *je = access_journal_entry(ic, i, j);
2093 if (!journal_entry_is_unused(je)) {
2094 journal_empty = false;
2095 break;
2096 }
2097 }
2098 }
2099 }
2100
2101 if (!used_commit_ids[N_COMMIT_IDS - 1]) {
2102 unused = N_COMMIT_IDS - 1;
2103 while (unused && !used_commit_ids[unused - 1])
2104 unused--;
2105 } else {
2106 for (unused = 0; unused < N_COMMIT_IDS; unused++)
2107 if (!used_commit_ids[unused])
2108 break;
2109 if (unused == N_COMMIT_IDS) {
2110 dm_integrity_io_error(ic, "journal commit ids", -EIO);
2111 goto clear_journal;
2112 }
2113 }
2114 DEBUG_print("first unused commit seq %d [%d,%d,%d,%d]\n",
2115 unused, used_commit_ids[0], used_commit_ids[1],
2116 used_commit_ids[2], used_commit_ids[3]);
2117
2118 last_used = prev_commit_seq(unused);
2119 want_commit_seq = prev_commit_seq(last_used);
2120
2121 if (!used_commit_ids[want_commit_seq] && used_commit_ids[prev_commit_seq(want_commit_seq)])
2122 journal_empty = true;
2123
2124 write_start = max_commit_id_sections[last_used] + 1;
2125 if (unlikely(write_start >= ic->journal_sections))
2126 want_commit_seq = next_commit_seq(want_commit_seq);
2127 wraparound_section(ic, &write_start);
2128
2129 i = write_start;
2130 for (write_sections = 0; write_sections < ic->journal_sections; write_sections++) {
2131 for (j = 0; j < ic->journal_section_sectors; j++) {
2132 struct journal_sector *js = access_journal(ic, i, j);
2133
2134 if (js->commit_id != dm_integrity_commit_id(ic, i, j, want_commit_seq)) {
2135 /*
2136 * This could be caused by crash during writing.
2137 * We won't replay the inconsistent part of the
2138 * journal.
2139 */
2140 DEBUG_print("commit id mismatch at position (%u, %u): %d != %d\n",
2141 i, j, find_commit_seq(ic, i, j, js->commit_id), want_commit_seq);
2142 goto brk;
2143 }
2144 }
2145 i++;
2146 if (unlikely(i >= ic->journal_sections))
2147 want_commit_seq = next_commit_seq(want_commit_seq);
2148 wraparound_section(ic, &i);
2149 }
2150brk:
2151
2152 if (!journal_empty) {
2153 DEBUG_print("replaying %u sections, starting at %u, commit seq %d\n",
2154 write_sections, write_start, want_commit_seq);
2155 do_journal_write(ic, write_start, write_sections, true);
2156 }
2157
2158 if (write_sections == ic->journal_sections && (ic->mode == 'J' || journal_empty)) {
2159 continue_section = write_start;
2160 ic->commit_seq = want_commit_seq;
2161 DEBUG_print("continuing from section %u, commit seq %d\n", write_start, ic->commit_seq);
2162 } else {
2163 unsigned s;
2164 unsigned char erase_seq;
2165clear_journal:
2166 DEBUG_print("clearing journal\n");
2167
2168 erase_seq = prev_commit_seq(prev_commit_seq(last_used));
2169 s = write_start;
2170 init_journal(ic, s, 1, erase_seq);
2171 s++;
2172 wraparound_section(ic, &s);
2173 if (ic->journal_sections >= 2) {
2174 init_journal(ic, s, ic->journal_sections - 2, erase_seq);
2175 s += ic->journal_sections - 2;
2176 wraparound_section(ic, &s);
2177 init_journal(ic, s, 1, erase_seq);
2178 }
2179
2180 continue_section = 0;
2181 ic->commit_seq = next_commit_seq(erase_seq);
2182 }
2183
2184 ic->committed_section = continue_section;
2185 ic->n_committed_sections = 0;
2186
2187 ic->uncommitted_section = continue_section;
2188 ic->n_uncommitted_sections = 0;
2189
2190 ic->free_section = continue_section;
2191 ic->free_section_entry = 0;
2192 ic->free_sectors = ic->journal_entries;
2193
2194 ic->journal_tree_root = RB_ROOT;
2195 for (i = 0; i < ic->journal_entries; i++)
2196 init_journal_node(&ic->journal_tree[i]);
2197}
2198
2199static void dm_integrity_postsuspend(struct dm_target *ti)
2200{
2201 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
2202
2203 del_timer_sync(&ic->autocommit_timer);
2204
2205 ic->suspending = true;
2206
2207 queue_work(ic->commit_wq, &ic->commit_work);
2208 drain_workqueue(ic->commit_wq);
2209
2210 if (ic->mode == 'J') {
2211 drain_workqueue(ic->writer_wq);
2212 dm_integrity_flush_buffers(ic);
2213 }
2214
2215 ic->suspending = false;
2216
2217 BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress));
2218
2219 ic->journal_uptodate = true;
2220}
2221
2222static void dm_integrity_resume(struct dm_target *ti)
2223{
2224 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
2225
2226 replay_journal(ic);
2227}
2228
2229static void dm_integrity_status(struct dm_target *ti, status_type_t type,
2230 unsigned status_flags, char *result, unsigned maxlen)
2231{
2232 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
2233 unsigned arg_count;
2234 size_t sz = 0;
2235
2236 switch (type) {
2237 case STATUSTYPE_INFO:
Mikulas Patocka3f2e5392017-07-21 12:00:00 -04002238 DMEMIT("%llu", (unsigned long long)atomic64_read(&ic->number_of_mismatches));
Mikulas Patocka7eada902017-01-04 20:23:53 +01002239 break;
2240
2241 case STATUSTYPE_TABLE: {
2242 __u64 watermark_percentage = (__u64)(ic->journal_entries - ic->free_sectors_threshold) * 100;
2243 watermark_percentage += ic->journal_entries / 2;
2244 do_div(watermark_percentage, ic->journal_entries);
2245 arg_count = 5;
Mikulas Patocka9d609f82017-04-18 16:51:52 -04002246 arg_count += ic->sectors_per_block != 1;
Mikulas Patocka7eada902017-01-04 20:23:53 +01002247 arg_count += !!ic->internal_hash_alg.alg_string;
2248 arg_count += !!ic->journal_crypt_alg.alg_string;
2249 arg_count += !!ic->journal_mac_alg.alg_string;
2250 DMEMIT("%s %llu %u %c %u", ic->dev->name, (unsigned long long)ic->start,
2251 ic->tag_size, ic->mode, arg_count);
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002252 DMEMIT(" journal_sectors:%u", ic->initial_sectors - SB_SECTORS);
2253 DMEMIT(" interleave_sectors:%u", 1U << ic->sb->log2_interleave_sectors);
2254 DMEMIT(" buffer_sectors:%u", 1U << ic->log2_buffer_sectors);
2255 DMEMIT(" journal_watermark:%u", (unsigned)watermark_percentage);
2256 DMEMIT(" commit_time:%u", ic->autocommit_msec);
Mikulas Patocka9d609f82017-04-18 16:51:52 -04002257 if (ic->sectors_per_block != 1)
2258 DMEMIT(" block_size:%u", ic->sectors_per_block << SECTOR_SHIFT);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002259
2260#define EMIT_ALG(a, n) \
2261 do { \
2262 if (ic->a.alg_string) { \
2263 DMEMIT(" %s:%s", n, ic->a.alg_string); \
2264 if (ic->a.key_string) \
2265 DMEMIT(":%s", ic->a.key_string);\
2266 } \
2267 } while (0)
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002268 EMIT_ALG(internal_hash_alg, "internal_hash");
2269 EMIT_ALG(journal_crypt_alg, "journal_crypt");
2270 EMIT_ALG(journal_mac_alg, "journal_mac");
Mikulas Patocka7eada902017-01-04 20:23:53 +01002271 break;
2272 }
2273 }
2274}
2275
2276static int dm_integrity_iterate_devices(struct dm_target *ti,
2277 iterate_devices_callout_fn fn, void *data)
2278{
2279 struct dm_integrity_c *ic = ti->private;
2280
2281 return fn(ti, ic->dev, ic->start + ic->initial_sectors + ic->metadata_run, ti->len, data);
2282}
2283
Mikulas Patocka9d609f82017-04-18 16:51:52 -04002284static void dm_integrity_io_hints(struct dm_target *ti, struct queue_limits *limits)
2285{
2286 struct dm_integrity_c *ic = ti->private;
2287
2288 if (ic->sectors_per_block > 1) {
2289 limits->logical_block_size = ic->sectors_per_block << SECTOR_SHIFT;
2290 limits->physical_block_size = ic->sectors_per_block << SECTOR_SHIFT;
2291 blk_limits_io_min(limits, ic->sectors_per_block << SECTOR_SHIFT);
2292 }
2293}
2294
Mikulas Patocka7eada902017-01-04 20:23:53 +01002295static void calculate_journal_section_size(struct dm_integrity_c *ic)
2296{
2297 unsigned sector_space = JOURNAL_SECTOR_DATA;
2298
2299 ic->journal_sections = le32_to_cpu(ic->sb->journal_sections);
Mikulas Patocka9d609f82017-04-18 16:51:52 -04002300 ic->journal_entry_size = roundup(offsetof(struct journal_entry, last_bytes[ic->sectors_per_block]) + ic->tag_size,
Mikulas Patocka7eada902017-01-04 20:23:53 +01002301 JOURNAL_ENTRY_ROUNDUP);
2302
2303 if (ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC))
2304 sector_space -= JOURNAL_MAC_PER_SECTOR;
2305 ic->journal_entries_per_sector = sector_space / ic->journal_entry_size;
2306 ic->journal_section_entries = ic->journal_entries_per_sector * JOURNAL_BLOCK_SECTORS;
Mikulas Patocka9d609f82017-04-18 16:51:52 -04002307 ic->journal_section_sectors = (ic->journal_section_entries << ic->sb->log2_sectors_per_block) + JOURNAL_BLOCK_SECTORS;
Mikulas Patocka7eada902017-01-04 20:23:53 +01002308 ic->journal_entries = ic->journal_section_entries * ic->journal_sections;
2309}
2310
2311static int calculate_device_limits(struct dm_integrity_c *ic)
2312{
2313 __u64 initial_sectors;
2314 sector_t last_sector, last_area, last_offset;
2315
2316 calculate_journal_section_size(ic);
2317 initial_sectors = SB_SECTORS + (__u64)ic->journal_section_sectors * ic->journal_sections;
2318 if (initial_sectors + METADATA_PADDING_SECTORS >= ic->device_sectors || initial_sectors > UINT_MAX)
2319 return -EINVAL;
2320 ic->initial_sectors = initial_sectors;
2321
Mikulas Patocka9d609f82017-04-18 16:51:52 -04002322 ic->metadata_run = roundup((__u64)ic->tag_size << (ic->sb->log2_interleave_sectors - ic->sb->log2_sectors_per_block),
Mikulas Patocka7eada902017-01-04 20:23:53 +01002323 (__u64)(1 << SECTOR_SHIFT << METADATA_PADDING_SECTORS)) >> SECTOR_SHIFT;
2324 if (!(ic->metadata_run & (ic->metadata_run - 1)))
2325 ic->log2_metadata_run = __ffs(ic->metadata_run);
2326 else
2327 ic->log2_metadata_run = -1;
2328
2329 get_area_and_offset(ic, ic->provided_data_sectors - 1, &last_area, &last_offset);
2330 last_sector = get_data_sector(ic, last_area, last_offset);
2331
2332 if (ic->start + last_sector < last_sector || ic->start + last_sector >= ic->device_sectors)
2333 return -EINVAL;
2334
2335 return 0;
2336}
2337
2338static int initialize_superblock(struct dm_integrity_c *ic, unsigned journal_sectors, unsigned interleave_sectors)
2339{
2340 unsigned journal_sections;
2341 int test_bit;
2342
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002343 memset(ic->sb, 0, SB_SECTORS << SECTOR_SHIFT);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002344 memcpy(ic->sb->magic, SB_MAGIC, 8);
2345 ic->sb->version = SB_VERSION;
2346 ic->sb->integrity_tag_size = cpu_to_le16(ic->tag_size);
Mikulas Patocka9d609f82017-04-18 16:51:52 -04002347 ic->sb->log2_sectors_per_block = __ffs(ic->sectors_per_block);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002348 if (ic->journal_mac_alg.alg_string)
2349 ic->sb->flags |= cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC);
2350
2351 calculate_journal_section_size(ic);
2352 journal_sections = journal_sectors / ic->journal_section_sectors;
2353 if (!journal_sections)
2354 journal_sections = 1;
2355 ic->sb->journal_sections = cpu_to_le32(journal_sections);
2356
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002357 if (!interleave_sectors)
2358 interleave_sectors = DEFAULT_INTERLEAVE_SECTORS;
Mikulas Patocka7eada902017-01-04 20:23:53 +01002359 ic->sb->log2_interleave_sectors = __fls(interleave_sectors);
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002360 ic->sb->log2_interleave_sectors = max((__u8)MIN_LOG2_INTERLEAVE_SECTORS, ic->sb->log2_interleave_sectors);
2361 ic->sb->log2_interleave_sectors = min((__u8)MAX_LOG2_INTERLEAVE_SECTORS, ic->sb->log2_interleave_sectors);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002362
2363 ic->provided_data_sectors = 0;
2364 for (test_bit = fls64(ic->device_sectors) - 1; test_bit >= 3; test_bit--) {
2365 __u64 prev_data_sectors = ic->provided_data_sectors;
2366
2367 ic->provided_data_sectors |= (sector_t)1 << test_bit;
2368 if (calculate_device_limits(ic))
2369 ic->provided_data_sectors = prev_data_sectors;
2370 }
2371
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002372 if (!ic->provided_data_sectors)
Mikulas Patocka7eada902017-01-04 20:23:53 +01002373 return -EINVAL;
2374
2375 ic->sb->provided_data_sectors = cpu_to_le64(ic->provided_data_sectors);
2376
2377 return 0;
2378}
2379
2380static void dm_integrity_set(struct dm_target *ti, struct dm_integrity_c *ic)
2381{
2382 struct gendisk *disk = dm_disk(dm_table_get_md(ti->table));
2383 struct blk_integrity bi;
2384
2385 memset(&bi, 0, sizeof(bi));
2386 bi.profile = &dm_integrity_profile;
Mikulas Patocka9d609f82017-04-18 16:51:52 -04002387 bi.tuple_size = ic->tag_size;
2388 bi.tag_size = bi.tuple_size;
Mikulas Patocka84ff1bc2017-04-26 18:39:47 -04002389 bi.interval_exp = ic->sb->log2_sectors_per_block + SECTOR_SHIFT;
Mikulas Patocka7eada902017-01-04 20:23:53 +01002390
2391 blk_integrity_register(disk, &bi);
2392 blk_queue_max_integrity_segments(disk->queue, UINT_MAX);
2393}
2394
Mikulas Patocka7eada902017-01-04 20:23:53 +01002395static void dm_integrity_free_page_list(struct dm_integrity_c *ic, struct page_list *pl)
2396{
2397 unsigned i;
2398
2399 if (!pl)
2400 return;
2401 for (i = 0; i < ic->journal_pages; i++)
2402 if (pl[i].page)
2403 __free_page(pl[i].page);
2404 kvfree(pl);
2405}
2406
2407static struct page_list *dm_integrity_alloc_page_list(struct dm_integrity_c *ic)
2408{
2409 size_t page_list_desc_size = ic->journal_pages * sizeof(struct page_list);
2410 struct page_list *pl;
2411 unsigned i;
2412
Mikulas Patocka702a6202017-05-20 14:56:21 -04002413 pl = kvmalloc(page_list_desc_size, GFP_KERNEL | __GFP_ZERO);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002414 if (!pl)
2415 return NULL;
2416
2417 for (i = 0; i < ic->journal_pages; i++) {
2418 pl[i].page = alloc_page(GFP_KERNEL);
2419 if (!pl[i].page) {
2420 dm_integrity_free_page_list(ic, pl);
2421 return NULL;
2422 }
2423 if (i)
2424 pl[i - 1].next = &pl[i];
2425 }
2426
2427 return pl;
2428}
2429
2430static void dm_integrity_free_journal_scatterlist(struct dm_integrity_c *ic, struct scatterlist **sl)
2431{
2432 unsigned i;
2433 for (i = 0; i < ic->journal_sections; i++)
2434 kvfree(sl[i]);
2435 kfree(sl);
2436}
2437
2438static struct scatterlist **dm_integrity_alloc_journal_scatterlist(struct dm_integrity_c *ic, struct page_list *pl)
2439{
2440 struct scatterlist **sl;
2441 unsigned i;
2442
Mikulas Patocka702a6202017-05-20 14:56:21 -04002443 sl = kvmalloc(ic->journal_sections * sizeof(struct scatterlist *), GFP_KERNEL | __GFP_ZERO);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002444 if (!sl)
2445 return NULL;
2446
2447 for (i = 0; i < ic->journal_sections; i++) {
2448 struct scatterlist *s;
2449 unsigned start_index, start_offset;
2450 unsigned end_index, end_offset;
2451 unsigned n_pages;
2452 unsigned idx;
2453
2454 page_list_location(ic, i, 0, &start_index, &start_offset);
2455 page_list_location(ic, i, ic->journal_section_sectors - 1, &end_index, &end_offset);
2456
2457 n_pages = (end_index - start_index + 1);
2458
Mikulas Patocka702a6202017-05-20 14:56:21 -04002459 s = kvmalloc(n_pages * sizeof(struct scatterlist), GFP_KERNEL);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002460 if (!s) {
2461 dm_integrity_free_journal_scatterlist(ic, sl);
2462 return NULL;
2463 }
2464
2465 sg_init_table(s, n_pages);
2466 for (idx = start_index; idx <= end_index; idx++) {
2467 char *va = lowmem_page_address(pl[idx].page);
2468 unsigned start = 0, end = PAGE_SIZE;
2469 if (idx == start_index)
2470 start = start_offset;
2471 if (idx == end_index)
2472 end = end_offset + (1 << SECTOR_SHIFT);
2473 sg_set_buf(&s[idx - start_index], va + start, end - start);
2474 }
2475
2476 sl[i] = s;
2477 }
2478
2479 return sl;
2480}
2481
2482static void free_alg(struct alg_spec *a)
2483{
2484 kzfree(a->alg_string);
2485 kzfree(a->key);
2486 memset(a, 0, sizeof *a);
2487}
2488
2489static int get_alg_and_key(const char *arg, struct alg_spec *a, char **error, char *error_inval)
2490{
2491 char *k;
2492
2493 free_alg(a);
2494
2495 a->alg_string = kstrdup(strchr(arg, ':') + 1, GFP_KERNEL);
2496 if (!a->alg_string)
2497 goto nomem;
2498
2499 k = strchr(a->alg_string, ':');
2500 if (k) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01002501 *k = 0;
2502 a->key_string = k + 1;
2503 if (strlen(a->key_string) & 1)
2504 goto inval;
2505
2506 a->key_size = strlen(a->key_string) / 2;
2507 a->key = kmalloc(a->key_size, GFP_KERNEL);
2508 if (!a->key)
2509 goto nomem;
Mikulas Patocka6625d902017-04-27 11:49:33 -04002510 if (hex2bin(a->key, a->key_string, a->key_size))
2511 goto inval;
Mikulas Patocka7eada902017-01-04 20:23:53 +01002512 }
2513
2514 return 0;
2515inval:
2516 *error = error_inval;
2517 return -EINVAL;
2518nomem:
2519 *error = "Out of memory for an argument";
2520 return -ENOMEM;
2521}
2522
2523static int get_mac(struct crypto_shash **hash, struct alg_spec *a, char **error,
2524 char *error_alg, char *error_key)
2525{
2526 int r;
2527
2528 if (a->alg_string) {
2529 *hash = crypto_alloc_shash(a->alg_string, 0, CRYPTO_ALG_ASYNC);
2530 if (IS_ERR(*hash)) {
2531 *error = error_alg;
2532 r = PTR_ERR(*hash);
2533 *hash = NULL;
2534 return r;
2535 }
2536
2537 if (a->key) {
2538 r = crypto_shash_setkey(*hash, a->key, a->key_size);
2539 if (r) {
2540 *error = error_key;
2541 return r;
2542 }
2543 }
2544 }
2545
2546 return 0;
2547}
2548
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04002549static int create_journal(struct dm_integrity_c *ic, char **error)
2550{
2551 int r = 0;
2552 unsigned i;
2553 __u64 journal_pages, journal_desc_size, journal_tree_size;
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002554 unsigned char *crypt_data = NULL;
2555
2556 ic->commit_ids[0] = cpu_to_le64(0x1111111111111111ULL);
2557 ic->commit_ids[1] = cpu_to_le64(0x2222222222222222ULL);
2558 ic->commit_ids[2] = cpu_to_le64(0x3333333333333333ULL);
2559 ic->commit_ids[3] = cpu_to_le64(0x4444444444444444ULL);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04002560
2561 journal_pages = roundup((__u64)ic->journal_sections * ic->journal_section_sectors,
2562 PAGE_SIZE >> SECTOR_SHIFT) >> (PAGE_SHIFT - SECTOR_SHIFT);
2563 journal_desc_size = journal_pages * sizeof(struct page_list);
2564 if (journal_pages >= totalram_pages - totalhigh_pages || journal_desc_size > ULONG_MAX) {
2565 *error = "Journal doesn't fit into memory";
2566 r = -ENOMEM;
2567 goto bad;
2568 }
2569 ic->journal_pages = journal_pages;
2570
2571 ic->journal = dm_integrity_alloc_page_list(ic);
2572 if (!ic->journal) {
2573 *error = "Could not allocate memory for journal";
2574 r = -ENOMEM;
2575 goto bad;
2576 }
2577 if (ic->journal_crypt_alg.alg_string) {
2578 unsigned ivsize, blocksize;
2579 struct journal_completion comp;
2580
2581 comp.ic = ic;
2582 ic->journal_crypt = crypto_alloc_skcipher(ic->journal_crypt_alg.alg_string, 0, 0);
2583 if (IS_ERR(ic->journal_crypt)) {
2584 *error = "Invalid journal cipher";
2585 r = PTR_ERR(ic->journal_crypt);
2586 ic->journal_crypt = NULL;
2587 goto bad;
2588 }
2589 ivsize = crypto_skcipher_ivsize(ic->journal_crypt);
2590 blocksize = crypto_skcipher_blocksize(ic->journal_crypt);
2591
2592 if (ic->journal_crypt_alg.key) {
2593 r = crypto_skcipher_setkey(ic->journal_crypt, ic->journal_crypt_alg.key,
2594 ic->journal_crypt_alg.key_size);
2595 if (r) {
2596 *error = "Error setting encryption key";
2597 goto bad;
2598 }
2599 }
2600 DEBUG_print("cipher %s, block size %u iv size %u\n",
2601 ic->journal_crypt_alg.alg_string, blocksize, ivsize);
2602
2603 ic->journal_io = dm_integrity_alloc_page_list(ic);
2604 if (!ic->journal_io) {
2605 *error = "Could not allocate memory for journal io";
2606 r = -ENOMEM;
2607 goto bad;
2608 }
2609
2610 if (blocksize == 1) {
2611 struct scatterlist *sg;
2612 SKCIPHER_REQUEST_ON_STACK(req, ic->journal_crypt);
2613 unsigned char iv[ivsize];
2614 skcipher_request_set_tfm(req, ic->journal_crypt);
2615
2616 ic->journal_xor = dm_integrity_alloc_page_list(ic);
2617 if (!ic->journal_xor) {
2618 *error = "Could not allocate memory for journal xor";
2619 r = -ENOMEM;
2620 goto bad;
2621 }
2622
Mikulas Patocka702a6202017-05-20 14:56:21 -04002623 sg = kvmalloc((ic->journal_pages + 1) * sizeof(struct scatterlist), GFP_KERNEL);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04002624 if (!sg) {
2625 *error = "Unable to allocate sg list";
2626 r = -ENOMEM;
2627 goto bad;
2628 }
2629 sg_init_table(sg, ic->journal_pages + 1);
2630 for (i = 0; i < ic->journal_pages; i++) {
2631 char *va = lowmem_page_address(ic->journal_xor[i].page);
2632 clear_page(va);
2633 sg_set_buf(&sg[i], va, PAGE_SIZE);
2634 }
2635 sg_set_buf(&sg[i], &ic->commit_ids, sizeof ic->commit_ids);
2636 memset(iv, 0x00, ivsize);
2637
2638 skcipher_request_set_crypt(req, sg, sg, PAGE_SIZE * ic->journal_pages + sizeof ic->commit_ids, iv);
2639 comp.comp = COMPLETION_INITIALIZER_ONSTACK(comp.comp);
2640 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
2641 if (do_crypt(true, req, &comp))
2642 wait_for_completion(&comp.comp);
2643 kvfree(sg);
2644 r = dm_integrity_failed(ic);
2645 if (r) {
2646 *error = "Unable to encrypt journal";
2647 goto bad;
2648 }
2649 DEBUG_bytes(lowmem_page_address(ic->journal_xor[0].page), 64, "xor data");
2650
2651 crypto_free_skcipher(ic->journal_crypt);
2652 ic->journal_crypt = NULL;
2653 } else {
2654 SKCIPHER_REQUEST_ON_STACK(req, ic->journal_crypt);
2655 unsigned char iv[ivsize];
2656 unsigned crypt_len = roundup(ivsize, blocksize);
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002657
2658 crypt_data = kmalloc(crypt_len, GFP_KERNEL);
2659 if (!crypt_data) {
2660 *error = "Unable to allocate crypt data";
2661 r = -ENOMEM;
2662 goto bad;
2663 }
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04002664
2665 skcipher_request_set_tfm(req, ic->journal_crypt);
2666
2667 ic->journal_scatterlist = dm_integrity_alloc_journal_scatterlist(ic, ic->journal);
2668 if (!ic->journal_scatterlist) {
2669 *error = "Unable to allocate sg list";
2670 r = -ENOMEM;
2671 goto bad;
2672 }
2673 ic->journal_io_scatterlist = dm_integrity_alloc_journal_scatterlist(ic, ic->journal_io);
2674 if (!ic->journal_io_scatterlist) {
2675 *error = "Unable to allocate sg list";
2676 r = -ENOMEM;
2677 goto bad;
2678 }
Mikulas Patocka702a6202017-05-20 14:56:21 -04002679 ic->sk_requests = kvmalloc(ic->journal_sections * sizeof(struct skcipher_request *), GFP_KERNEL | __GFP_ZERO);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04002680 if (!ic->sk_requests) {
2681 *error = "Unable to allocate sk requests";
2682 r = -ENOMEM;
2683 goto bad;
2684 }
2685 for (i = 0; i < ic->journal_sections; i++) {
2686 struct scatterlist sg;
2687 struct skcipher_request *section_req;
2688 __u32 section_le = cpu_to_le32(i);
2689
2690 memset(iv, 0x00, ivsize);
2691 memset(crypt_data, 0x00, crypt_len);
2692 memcpy(crypt_data, &section_le, min((size_t)crypt_len, sizeof(section_le)));
2693
2694 sg_init_one(&sg, crypt_data, crypt_len);
2695 skcipher_request_set_crypt(req, &sg, &sg, crypt_len, iv);
2696 comp.comp = COMPLETION_INITIALIZER_ONSTACK(comp.comp);
2697 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
2698 if (do_crypt(true, req, &comp))
2699 wait_for_completion(&comp.comp);
2700
2701 r = dm_integrity_failed(ic);
2702 if (r) {
2703 *error = "Unable to generate iv";
2704 goto bad;
2705 }
2706
2707 section_req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
2708 if (!section_req) {
2709 *error = "Unable to allocate crypt request";
2710 r = -ENOMEM;
2711 goto bad;
2712 }
2713 section_req->iv = kmalloc(ivsize * 2, GFP_KERNEL);
2714 if (!section_req->iv) {
2715 skcipher_request_free(section_req);
2716 *error = "Unable to allocate iv";
2717 r = -ENOMEM;
2718 goto bad;
2719 }
2720 memcpy(section_req->iv + ivsize, crypt_data, ivsize);
2721 section_req->cryptlen = (size_t)ic->journal_section_sectors << SECTOR_SHIFT;
2722 ic->sk_requests[i] = section_req;
2723 DEBUG_bytes(crypt_data, ivsize, "iv(%u)", i);
2724 }
2725 }
2726 }
2727
2728 for (i = 0; i < N_COMMIT_IDS; i++) {
2729 unsigned j;
2730retest_commit_id:
2731 for (j = 0; j < i; j++) {
2732 if (ic->commit_ids[j] == ic->commit_ids[i]) {
2733 ic->commit_ids[i] = cpu_to_le64(le64_to_cpu(ic->commit_ids[i]) + 1);
2734 goto retest_commit_id;
2735 }
2736 }
2737 DEBUG_print("commit id %u: %016llx\n", i, ic->commit_ids[i]);
2738 }
2739
2740 journal_tree_size = (__u64)ic->journal_entries * sizeof(struct journal_node);
2741 if (journal_tree_size > ULONG_MAX) {
2742 *error = "Journal doesn't fit into memory";
2743 r = -ENOMEM;
2744 goto bad;
2745 }
Mikulas Patocka702a6202017-05-20 14:56:21 -04002746 ic->journal_tree = kvmalloc(journal_tree_size, GFP_KERNEL);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04002747 if (!ic->journal_tree) {
2748 *error = "Could not allocate memory for journal tree";
2749 r = -ENOMEM;
2750 }
2751bad:
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002752 kfree(crypt_data);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04002753 return r;
2754}
2755
Mikulas Patocka7eada902017-01-04 20:23:53 +01002756/*
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002757 * Construct a integrity mapping
Mikulas Patocka7eada902017-01-04 20:23:53 +01002758 *
2759 * Arguments:
2760 * device
2761 * offset from the start of the device
2762 * tag size
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002763 * D - direct writes, J - journal writes, R - recovery mode
Mikulas Patocka7eada902017-01-04 20:23:53 +01002764 * number of optional arguments
2765 * optional arguments:
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002766 * journal_sectors
2767 * interleave_sectors
2768 * buffer_sectors
2769 * journal_watermark
2770 * commit_time
2771 * internal_hash
2772 * journal_crypt
2773 * journal_mac
Mikulas Patocka9d609f82017-04-18 16:51:52 -04002774 * block_size
Mikulas Patocka7eada902017-01-04 20:23:53 +01002775 */
2776static int dm_integrity_ctr(struct dm_target *ti, unsigned argc, char **argv)
2777{
2778 struct dm_integrity_c *ic;
2779 char dummy;
2780 int r;
Mikulas Patocka7eada902017-01-04 20:23:53 +01002781 unsigned extra_args;
2782 struct dm_arg_set as;
2783 static struct dm_arg _args[] = {
Mikulas Patocka9d609f82017-04-18 16:51:52 -04002784 {0, 9, "Invalid number of feature args"},
Mikulas Patocka7eada902017-01-04 20:23:53 +01002785 };
2786 unsigned journal_sectors, interleave_sectors, buffer_sectors, journal_watermark, sync_msec;
2787 bool should_write_sb;
Mikulas Patocka7eada902017-01-04 20:23:53 +01002788 __u64 threshold;
2789 unsigned long long start;
2790
2791#define DIRECT_ARGUMENTS 4
2792
2793 if (argc <= DIRECT_ARGUMENTS) {
2794 ti->error = "Invalid argument count";
2795 return -EINVAL;
2796 }
2797
2798 ic = kzalloc(sizeof(struct dm_integrity_c), GFP_KERNEL);
2799 if (!ic) {
2800 ti->error = "Cannot allocate integrity context";
2801 return -ENOMEM;
2802 }
2803 ti->private = ic;
2804 ti->per_io_data_size = sizeof(struct dm_integrity_io);
2805
Mikulas Patocka7eada902017-01-04 20:23:53 +01002806 ic->in_progress = RB_ROOT;
2807 init_waitqueue_head(&ic->endio_wait);
2808 bio_list_init(&ic->flush_bio_list);
2809 init_waitqueue_head(&ic->copy_to_journal_wait);
2810 init_completion(&ic->crypto_backoff);
Mikulas Patocka3f2e5392017-07-21 12:00:00 -04002811 atomic64_set(&ic->number_of_mismatches, 0);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002812
2813 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &ic->dev);
2814 if (r) {
2815 ti->error = "Device lookup failed";
2816 goto bad;
2817 }
2818
2819 if (sscanf(argv[1], "%llu%c", &start, &dummy) != 1 || start != (sector_t)start) {
2820 ti->error = "Invalid starting offset";
2821 r = -EINVAL;
2822 goto bad;
2823 }
2824 ic->start = start;
2825
2826 if (strcmp(argv[2], "-")) {
2827 if (sscanf(argv[2], "%u%c", &ic->tag_size, &dummy) != 1 || !ic->tag_size) {
2828 ti->error = "Invalid tag size";
2829 r = -EINVAL;
2830 goto bad;
2831 }
2832 }
2833
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04002834 if (!strcmp(argv[3], "J") || !strcmp(argv[3], "D") || !strcmp(argv[3], "R"))
Mikulas Patocka7eada902017-01-04 20:23:53 +01002835 ic->mode = argv[3][0];
2836 else {
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002837 ti->error = "Invalid mode (expecting J, D, R)";
Mikulas Patocka7eada902017-01-04 20:23:53 +01002838 r = -EINVAL;
2839 goto bad;
2840 }
2841
2842 ic->device_sectors = i_size_read(ic->dev->bdev->bd_inode) >> SECTOR_SHIFT;
2843 journal_sectors = min((sector_t)DEFAULT_MAX_JOURNAL_SECTORS,
2844 ic->device_sectors >> DEFAULT_JOURNAL_SIZE_FACTOR);
2845 interleave_sectors = DEFAULT_INTERLEAVE_SECTORS;
2846 buffer_sectors = DEFAULT_BUFFER_SECTORS;
2847 journal_watermark = DEFAULT_JOURNAL_WATERMARK;
2848 sync_msec = DEFAULT_SYNC_MSEC;
Mikulas Patocka9d609f82017-04-18 16:51:52 -04002849 ic->sectors_per_block = 1;
Mikulas Patocka7eada902017-01-04 20:23:53 +01002850
2851 as.argc = argc - DIRECT_ARGUMENTS;
2852 as.argv = argv + DIRECT_ARGUMENTS;
2853 r = dm_read_arg_group(_args, &as, &extra_args, &ti->error);
2854 if (r)
2855 goto bad;
2856
2857 while (extra_args--) {
2858 const char *opt_string;
2859 unsigned val;
2860 opt_string = dm_shift_arg(&as);
2861 if (!opt_string) {
2862 r = -EINVAL;
2863 ti->error = "Not enough feature arguments";
2864 goto bad;
2865 }
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002866 if (sscanf(opt_string, "journal_sectors:%u%c", &val, &dummy) == 1)
Mikulas Patocka7eada902017-01-04 20:23:53 +01002867 journal_sectors = val;
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002868 else if (sscanf(opt_string, "interleave_sectors:%u%c", &val, &dummy) == 1)
Mikulas Patocka7eada902017-01-04 20:23:53 +01002869 interleave_sectors = val;
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002870 else if (sscanf(opt_string, "buffer_sectors:%u%c", &val, &dummy) == 1)
Mikulas Patocka7eada902017-01-04 20:23:53 +01002871 buffer_sectors = val;
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002872 else if (sscanf(opt_string, "journal_watermark:%u%c", &val, &dummy) == 1 && val <= 100)
Mikulas Patocka7eada902017-01-04 20:23:53 +01002873 journal_watermark = val;
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002874 else if (sscanf(opt_string, "commit_time:%u%c", &val, &dummy) == 1)
Mikulas Patocka7eada902017-01-04 20:23:53 +01002875 sync_msec = val;
Mikulas Patocka9d609f82017-04-18 16:51:52 -04002876 else if (sscanf(opt_string, "block_size:%u%c", &val, &dummy) == 1) {
2877 if (val < 1 << SECTOR_SHIFT ||
2878 val > MAX_SECTORS_PER_BLOCK << SECTOR_SHIFT ||
2879 (val & (val -1))) {
2880 r = -EINVAL;
2881 ti->error = "Invalid block_size argument";
2882 goto bad;
2883 }
2884 ic->sectors_per_block = val >> SECTOR_SHIFT;
2885 } else if (!memcmp(opt_string, "internal_hash:", strlen("internal_hash:"))) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01002886 r = get_alg_and_key(opt_string, &ic->internal_hash_alg, &ti->error,
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002887 "Invalid internal_hash argument");
Mikulas Patocka7eada902017-01-04 20:23:53 +01002888 if (r)
2889 goto bad;
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002890 } else if (!memcmp(opt_string, "journal_crypt:", strlen("journal_crypt:"))) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01002891 r = get_alg_and_key(opt_string, &ic->journal_crypt_alg, &ti->error,
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002892 "Invalid journal_crypt argument");
Mikulas Patocka7eada902017-01-04 20:23:53 +01002893 if (r)
2894 goto bad;
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002895 } else if (!memcmp(opt_string, "journal_mac:", strlen("journal_mac:"))) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01002896 r = get_alg_and_key(opt_string, &ic->journal_mac_alg, &ti->error,
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002897 "Invalid journal_mac argument");
Mikulas Patocka7eada902017-01-04 20:23:53 +01002898 if (r)
2899 goto bad;
2900 } else {
2901 r = -EINVAL;
2902 ti->error = "Invalid argument";
2903 goto bad;
2904 }
2905 }
2906
2907 r = get_mac(&ic->internal_hash, &ic->internal_hash_alg, &ti->error,
2908 "Invalid internal hash", "Error setting internal hash key");
2909 if (r)
2910 goto bad;
2911
2912 r = get_mac(&ic->journal_mac, &ic->journal_mac_alg, &ti->error,
2913 "Invalid journal mac", "Error setting journal mac key");
2914 if (r)
2915 goto bad;
2916
2917 if (!ic->tag_size) {
2918 if (!ic->internal_hash) {
2919 ti->error = "Unknown tag size";
2920 r = -EINVAL;
2921 goto bad;
2922 }
2923 ic->tag_size = crypto_shash_digestsize(ic->internal_hash);
2924 }
2925 if (ic->tag_size > MAX_TAG_SIZE) {
2926 ti->error = "Too big tag size";
2927 r = -EINVAL;
2928 goto bad;
2929 }
2930 if (!(ic->tag_size & (ic->tag_size - 1)))
2931 ic->log2_tag_size = __ffs(ic->tag_size);
2932 else
2933 ic->log2_tag_size = -1;
2934
2935 ic->autocommit_jiffies = msecs_to_jiffies(sync_msec);
2936 ic->autocommit_msec = sync_msec;
2937 setup_timer(&ic->autocommit_timer, autocommit_fn, (unsigned long)ic);
2938
2939 ic->io = dm_io_client_create();
2940 if (IS_ERR(ic->io)) {
2941 r = PTR_ERR(ic->io);
2942 ic->io = NULL;
2943 ti->error = "Cannot allocate dm io";
2944 goto bad;
2945 }
2946
2947 ic->journal_io_mempool = mempool_create_slab_pool(JOURNAL_IO_MEMPOOL, journal_io_cache);
2948 if (!ic->journal_io_mempool) {
2949 r = -ENOMEM;
2950 ti->error = "Cannot allocate mempool";
2951 goto bad;
2952 }
2953
2954 ic->metadata_wq = alloc_workqueue("dm-integrity-metadata",
2955 WQ_MEM_RECLAIM, METADATA_WORKQUEUE_MAX_ACTIVE);
2956 if (!ic->metadata_wq) {
2957 ti->error = "Cannot allocate workqueue";
2958 r = -ENOMEM;
2959 goto bad;
2960 }
2961
2962 /*
2963 * If this workqueue were percpu, it would cause bio reordering
2964 * and reduced performance.
2965 */
2966 ic->wait_wq = alloc_workqueue("dm-integrity-wait", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
2967 if (!ic->wait_wq) {
2968 ti->error = "Cannot allocate workqueue";
2969 r = -ENOMEM;
2970 goto bad;
2971 }
2972
2973 ic->commit_wq = alloc_workqueue("dm-integrity-commit", WQ_MEM_RECLAIM, 1);
2974 if (!ic->commit_wq) {
2975 ti->error = "Cannot allocate workqueue";
2976 r = -ENOMEM;
2977 goto bad;
2978 }
2979 INIT_WORK(&ic->commit_work, integrity_commit);
2980
2981 if (ic->mode == 'J') {
2982 ic->writer_wq = alloc_workqueue("dm-integrity-writer", WQ_MEM_RECLAIM, 1);
2983 if (!ic->writer_wq) {
2984 ti->error = "Cannot allocate workqueue";
2985 r = -ENOMEM;
2986 goto bad;
2987 }
2988 INIT_WORK(&ic->writer_work, integrity_writer);
2989 }
2990
2991 ic->sb = alloc_pages_exact(SB_SECTORS << SECTOR_SHIFT, GFP_KERNEL);
2992 if (!ic->sb) {
2993 r = -ENOMEM;
2994 ti->error = "Cannot allocate superblock area";
2995 goto bad;
2996 }
2997
2998 r = sync_rw_sb(ic, REQ_OP_READ, 0);
2999 if (r) {
3000 ti->error = "Error reading superblock";
3001 goto bad;
3002 }
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04003003 should_write_sb = false;
3004 if (memcmp(ic->sb->magic, SB_MAGIC, 8)) {
3005 if (ic->mode != 'R') {
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003006 if (memchr_inv(ic->sb, 0, SB_SECTORS << SECTOR_SHIFT)) {
3007 r = -EINVAL;
3008 ti->error = "The device is not initialized";
3009 goto bad;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003010 }
3011 }
3012
3013 r = initialize_superblock(ic, journal_sectors, interleave_sectors);
3014 if (r) {
3015 ti->error = "Could not initialize superblock";
3016 goto bad;
3017 }
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04003018 if (ic->mode != 'R')
3019 should_write_sb = true;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003020 }
3021
3022 if (ic->sb->version != SB_VERSION) {
3023 r = -EINVAL;
3024 ti->error = "Unknown version";
3025 goto bad;
3026 }
3027 if (le16_to_cpu(ic->sb->integrity_tag_size) != ic->tag_size) {
3028 r = -EINVAL;
Mikulas Patocka9d609f82017-04-18 16:51:52 -04003029 ti->error = "Tag size doesn't match the information in superblock";
3030 goto bad;
3031 }
3032 if (ic->sb->log2_sectors_per_block != __ffs(ic->sectors_per_block)) {
3033 r = -EINVAL;
3034 ti->error = "Block size doesn't match the information in superblock";
Mikulas Patocka7eada902017-01-04 20:23:53 +01003035 goto bad;
3036 }
Mikulas Patockabc86a412017-07-21 11:58:38 -04003037 if (!le32_to_cpu(ic->sb->journal_sections)) {
3038 r = -EINVAL;
3039 ti->error = "Corrupted superblock, journal_sections is 0";
3040 goto bad;
3041 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01003042 /* make sure that ti->max_io_len doesn't overflow */
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003043 if (ic->sb->log2_interleave_sectors < MIN_LOG2_INTERLEAVE_SECTORS ||
3044 ic->sb->log2_interleave_sectors > MAX_LOG2_INTERLEAVE_SECTORS) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01003045 r = -EINVAL;
3046 ti->error = "Invalid interleave_sectors in the superblock";
3047 goto bad;
3048 }
3049 ic->provided_data_sectors = le64_to_cpu(ic->sb->provided_data_sectors);
3050 if (ic->provided_data_sectors != le64_to_cpu(ic->sb->provided_data_sectors)) {
3051 /* test for overflow */
3052 r = -EINVAL;
3053 ti->error = "The superblock has 64-bit device size, but the kernel was compiled with 32-bit sectors";
3054 goto bad;
3055 }
3056 if (!!(ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC)) != !!ic->journal_mac_alg.alg_string) {
3057 r = -EINVAL;
3058 ti->error = "Journal mac mismatch";
3059 goto bad;
3060 }
3061 r = calculate_device_limits(ic);
3062 if (r) {
3063 ti->error = "The device is too small";
3064 goto bad;
3065 }
Ondrej Mosnáček2ad50602017-06-05 17:52:39 +02003066 if (ti->len > ic->provided_data_sectors) {
3067 r = -EINVAL;
3068 ti->error = "Not enough provided sectors for requested mapping size";
3069 goto bad;
3070 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01003071
3072 if (!buffer_sectors)
3073 buffer_sectors = 1;
3074 ic->log2_buffer_sectors = min3((int)__fls(buffer_sectors), (int)__ffs(ic->metadata_run), 31 - SECTOR_SHIFT);
3075
3076 threshold = (__u64)ic->journal_entries * (100 - journal_watermark);
3077 threshold += 50;
3078 do_div(threshold, 100);
3079 ic->free_sectors_threshold = threshold;
3080
3081 DEBUG_print("initialized:\n");
3082 DEBUG_print(" integrity_tag_size %u\n", le16_to_cpu(ic->sb->integrity_tag_size));
3083 DEBUG_print(" journal_entry_size %u\n", ic->journal_entry_size);
3084 DEBUG_print(" journal_entries_per_sector %u\n", ic->journal_entries_per_sector);
3085 DEBUG_print(" journal_section_entries %u\n", ic->journal_section_entries);
3086 DEBUG_print(" journal_section_sectors %u\n", ic->journal_section_sectors);
3087 DEBUG_print(" journal_sections %u\n", (unsigned)le32_to_cpu(ic->sb->journal_sections));
3088 DEBUG_print(" journal_entries %u\n", ic->journal_entries);
3089 DEBUG_print(" log2_interleave_sectors %d\n", ic->sb->log2_interleave_sectors);
3090 DEBUG_print(" device_sectors 0x%llx\n", (unsigned long long)ic->device_sectors);
3091 DEBUG_print(" initial_sectors 0x%x\n", ic->initial_sectors);
3092 DEBUG_print(" metadata_run 0x%x\n", ic->metadata_run);
3093 DEBUG_print(" log2_metadata_run %d\n", ic->log2_metadata_run);
3094 DEBUG_print(" provided_data_sectors 0x%llx (%llu)\n", (unsigned long long)ic->provided_data_sectors,
3095 (unsigned long long)ic->provided_data_sectors);
3096 DEBUG_print(" log2_buffer_sectors %u\n", ic->log2_buffer_sectors);
3097
3098 ic->bufio = dm_bufio_client_create(ic->dev->bdev, 1U << (SECTOR_SHIFT + ic->log2_buffer_sectors),
3099 1, 0, NULL, NULL);
3100 if (IS_ERR(ic->bufio)) {
3101 r = PTR_ERR(ic->bufio);
3102 ti->error = "Cannot initialize dm-bufio";
3103 ic->bufio = NULL;
3104 goto bad;
3105 }
3106 dm_bufio_set_sector_offset(ic->bufio, ic->start + ic->initial_sectors);
3107
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04003108 if (ic->mode != 'R') {
3109 r = create_journal(ic, &ti->error);
3110 if (r)
3111 goto bad;
3112 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01003113
3114 if (should_write_sb) {
3115 int r;
3116
3117 init_journal(ic, 0, ic->journal_sections, 0);
3118 r = dm_integrity_failed(ic);
3119 if (unlikely(r)) {
3120 ti->error = "Error initializing journal";
3121 goto bad;
3122 }
3123 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
3124 if (r) {
3125 ti->error = "Error initializing superblock";
3126 goto bad;
3127 }
3128 ic->just_formatted = true;
3129 }
3130
3131 r = dm_set_target_max_io_len(ti, 1U << ic->sb->log2_interleave_sectors);
3132 if (r)
3133 goto bad;
3134
3135 if (!ic->internal_hash)
3136 dm_integrity_set(ti, ic);
3137
3138 ti->num_flush_bios = 1;
3139 ti->flush_supported = true;
3140
3141 return 0;
3142bad:
3143 dm_integrity_dtr(ti);
3144 return r;
3145}
3146
3147static void dm_integrity_dtr(struct dm_target *ti)
3148{
3149 struct dm_integrity_c *ic = ti->private;
3150
3151 BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress));
3152
3153 if (ic->metadata_wq)
3154 destroy_workqueue(ic->metadata_wq);
3155 if (ic->wait_wq)
3156 destroy_workqueue(ic->wait_wq);
3157 if (ic->commit_wq)
3158 destroy_workqueue(ic->commit_wq);
3159 if (ic->writer_wq)
3160 destroy_workqueue(ic->writer_wq);
3161 if (ic->bufio)
3162 dm_bufio_client_destroy(ic->bufio);
3163 mempool_destroy(ic->journal_io_mempool);
3164 if (ic->io)
3165 dm_io_client_destroy(ic->io);
3166 if (ic->dev)
3167 dm_put_device(ti, ic->dev);
3168 dm_integrity_free_page_list(ic, ic->journal);
3169 dm_integrity_free_page_list(ic, ic->journal_io);
3170 dm_integrity_free_page_list(ic, ic->journal_xor);
3171 if (ic->journal_scatterlist)
3172 dm_integrity_free_journal_scatterlist(ic, ic->journal_scatterlist);
3173 if (ic->journal_io_scatterlist)
3174 dm_integrity_free_journal_scatterlist(ic, ic->journal_io_scatterlist);
3175 if (ic->sk_requests) {
3176 unsigned i;
3177
3178 for (i = 0; i < ic->journal_sections; i++) {
3179 struct skcipher_request *req = ic->sk_requests[i];
3180 if (req) {
3181 kzfree(req->iv);
3182 skcipher_request_free(req);
3183 }
3184 }
3185 kvfree(ic->sk_requests);
3186 }
3187 kvfree(ic->journal_tree);
3188 if (ic->sb)
3189 free_pages_exact(ic->sb, SB_SECTORS << SECTOR_SHIFT);
3190
3191 if (ic->internal_hash)
3192 crypto_free_shash(ic->internal_hash);
3193 free_alg(&ic->internal_hash_alg);
3194
3195 if (ic->journal_crypt)
3196 crypto_free_skcipher(ic->journal_crypt);
3197 free_alg(&ic->journal_crypt_alg);
3198
3199 if (ic->journal_mac)
3200 crypto_free_shash(ic->journal_mac);
3201 free_alg(&ic->journal_mac_alg);
3202
3203 kfree(ic);
3204}
3205
3206static struct target_type integrity_target = {
3207 .name = "integrity",
Mikulas Patocka3f2e5392017-07-21 12:00:00 -04003208 .version = {1, 1, 0},
Mikulas Patocka7eada902017-01-04 20:23:53 +01003209 .module = THIS_MODULE,
3210 .features = DM_TARGET_SINGLETON | DM_TARGET_INTEGRITY,
3211 .ctr = dm_integrity_ctr,
3212 .dtr = dm_integrity_dtr,
3213 .map = dm_integrity_map,
3214 .postsuspend = dm_integrity_postsuspend,
3215 .resume = dm_integrity_resume,
3216 .status = dm_integrity_status,
3217 .iterate_devices = dm_integrity_iterate_devices,
Mikulas Patocka9d609f82017-04-18 16:51:52 -04003218 .io_hints = dm_integrity_io_hints,
Mikulas Patocka7eada902017-01-04 20:23:53 +01003219};
3220
3221int __init dm_integrity_init(void)
3222{
3223 int r;
3224
3225 journal_io_cache = kmem_cache_create("integrity_journal_io",
3226 sizeof(struct journal_io), 0, 0, NULL);
3227 if (!journal_io_cache) {
3228 DMERR("can't allocate journal io cache");
3229 return -ENOMEM;
3230 }
3231
3232 r = dm_register_target(&integrity_target);
3233
3234 if (r < 0)
3235 DMERR("register failed %d", r);
3236
3237 return r;
3238}
3239
3240void dm_integrity_exit(void)
3241{
3242 dm_unregister_target(&integrity_target);
3243 kmem_cache_destroy(journal_io_cache);
3244}
3245
3246module_init(dm_integrity_init);
3247module_exit(dm_integrity_exit);
3248
3249MODULE_AUTHOR("Milan Broz");
3250MODULE_AUTHOR("Mikulas Patocka");
3251MODULE_DESCRIPTION(DM_NAME " target for integrity tags extension");
3252MODULE_LICENSE("GPL");