blob: 9af98a990079dd5da851a3a9e46e7980f73c9f3b [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
Mark Rutlandd3e632f2017-10-23 14:07:11 -07009#include <linux/compiler.h>
Mikulas Patocka7eada902017-01-04 20:23:53 +010010#include <linux/module.h>
11#include <linux/device-mapper.h>
12#include <linux/dm-io.h>
13#include <linux/vmalloc.h>
14#include <linux/sort.h>
15#include <linux/rbtree.h>
16#include <linux/delay.h>
17#include <linux/random.h>
Mikulas Patocka1f5a7752019-04-29 14:57:25 +020018#include <linux/reboot.h>
Mikulas Patocka7eada902017-01-04 20:23:53 +010019#include <crypto/hash.h>
20#include <crypto/skcipher.h>
21#include <linux/async_tx.h>
Mikulas Patockaafa53df2018-03-15 16:02:31 -040022#include <linux/dm-bufio.h>
Mikulas Patocka7eada902017-01-04 20:23:53 +010023
24#define DM_MSG_PREFIX "integrity"
25
26#define DEFAULT_INTERLEAVE_SECTORS 32768
27#define DEFAULT_JOURNAL_SIZE_FACTOR 7
Mikulas Patocka468dfca2019-04-29 14:57:24 +020028#define DEFAULT_SECTORS_PER_BITMAP_BIT 32768
Mikulas Patocka7eada902017-01-04 20:23:53 +010029#define DEFAULT_BUFFER_SECTORS 128
30#define DEFAULT_JOURNAL_WATERMARK 50
31#define DEFAULT_SYNC_MSEC 10000
32#define DEFAULT_MAX_JOURNAL_SECTORS 131072
Mikulas Patocka56b67a42017-04-18 16:51:50 -040033#define MIN_LOG2_INTERLEAVE_SECTORS 3
34#define MAX_LOG2_INTERLEAVE_SECTORS 31
Mikulas Patocka7eada902017-01-04 20:23:53 +010035#define METADATA_WORKQUEUE_MAX_ACTIVE 16
Mikulas Patockaa3fcf722018-07-03 20:13:33 +020036#define RECALC_SECTORS 8192
37#define RECALC_WRITE_SUPER 16
Mikulas Patocka468dfca2019-04-29 14:57:24 +020038#define BITMAP_BLOCK_SIZE 4096 /* don't change it */
39#define BITMAP_FLUSH_INTERVAL (10 * HZ)
Mikulas Patocka7eada902017-01-04 20:23:53 +010040
41/*
42 * Warning - DEBUG_PRINT prints security-sensitive data to the log,
43 * so it should not be enabled in the official kernel
44 */
45//#define DEBUG_PRINT
46//#define INTERNAL_VERIFY
47
48/*
49 * On disk structures
50 */
51
52#define SB_MAGIC "integrt"
Mikulas Patocka1f9fc0b2018-07-03 20:13:31 +020053#define SB_VERSION_1 1
54#define SB_VERSION_2 2
Mikulas Patocka468dfca2019-04-29 14:57:24 +020055#define SB_VERSION_3 3
Mikulas Patocka7eada902017-01-04 20:23:53 +010056#define SB_SECTORS 8
Mikulas Patocka9d609f852017-04-18 16:51:52 -040057#define MAX_SECTORS_PER_BLOCK 8
Mikulas Patocka7eada902017-01-04 20:23:53 +010058
59struct superblock {
60 __u8 magic[8];
61 __u8 version;
62 __u8 log2_interleave_sectors;
63 __u16 integrity_tag_size;
64 __u32 journal_sections;
65 __u64 provided_data_sectors; /* userspace uses this value */
66 __u32 flags;
Mikulas Patocka9d609f852017-04-18 16:51:52 -040067 __u8 log2_sectors_per_block;
Mikulas Patocka468dfca2019-04-29 14:57:24 +020068 __u8 log2_blocks_per_bitmap_bit;
69 __u8 pad[2];
Mikulas Patockaa3fcf722018-07-03 20:13:33 +020070 __u64 recalc_sector;
Mikulas Patocka7eada902017-01-04 20:23:53 +010071};
72
73#define SB_FLAG_HAVE_JOURNAL_MAC 0x1
Mikulas Patockaa3fcf722018-07-03 20:13:33 +020074#define SB_FLAG_RECALCULATING 0x2
Mikulas Patocka468dfca2019-04-29 14:57:24 +020075#define SB_FLAG_DIRTY_BITMAP 0x4
Mikulas Patocka7eada902017-01-04 20:23:53 +010076
77#define JOURNAL_ENTRY_ROUNDUP 8
78
79typedef __u64 commit_id_t;
80#define JOURNAL_MAC_PER_SECTOR 8
81
82struct journal_entry {
83 union {
84 struct {
85 __u32 sector_lo;
86 __u32 sector_hi;
87 } s;
88 __u64 sector;
89 } u;
Mikulas Patocka9d609f852017-04-18 16:51:52 -040090 commit_id_t last_bytes[0];
91 /* __u8 tag[0]; */
Mikulas Patocka7eada902017-01-04 20:23:53 +010092};
93
Mikulas Patocka9d609f852017-04-18 16:51:52 -040094#define journal_entry_tag(ic, je) ((__u8 *)&(je)->last_bytes[(ic)->sectors_per_block])
95
Mikulas Patocka7eada902017-01-04 20:23:53 +010096#if BITS_PER_LONG == 64
Mark Rutlandd3e632f2017-10-23 14:07:11 -070097#define journal_entry_set_sector(je, x) do { smp_wmb(); WRITE_ONCE((je)->u.sector, cpu_to_le64(x)); } while (0)
Mikulas Patocka7eada902017-01-04 20:23:53 +010098#define journal_entry_get_sector(je) le64_to_cpu((je)->u.sector)
99#elif defined(CONFIG_LBDAF)
Mark Rutlandd3e632f2017-10-23 14:07:11 -0700100#define journal_entry_set_sector(je, x) do { (je)->u.s.sector_lo = cpu_to_le32(x); smp_wmb(); WRITE_ONCE((je)->u.s.sector_hi, cpu_to_le32((x) >> 32)); } while (0)
Mikulas Patocka7eada902017-01-04 20:23:53 +0100101#define journal_entry_get_sector(je) le64_to_cpu((je)->u.sector)
102#else
Mark Rutlandd3e632f2017-10-23 14:07:11 -0700103#define journal_entry_set_sector(je, x) do { (je)->u.s.sector_lo = cpu_to_le32(x); smp_wmb(); WRITE_ONCE((je)->u.s.sector_hi, cpu_to_le32(0)); } while (0)
Mikulas Patocka7eada902017-01-04 20:23:53 +0100104#define journal_entry_get_sector(je) le32_to_cpu((je)->u.s.sector_lo)
105#endif
106#define journal_entry_is_unused(je) ((je)->u.s.sector_hi == cpu_to_le32(-1))
107#define journal_entry_set_unused(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-1)); } while (0)
108#define journal_entry_is_inprogress(je) ((je)->u.s.sector_hi == cpu_to_le32(-2))
109#define journal_entry_set_inprogress(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-2)); } while (0)
110
111#define JOURNAL_BLOCK_SECTORS 8
112#define JOURNAL_SECTOR_DATA ((1 << SECTOR_SHIFT) - sizeof(commit_id_t))
113#define JOURNAL_MAC_SIZE (JOURNAL_MAC_PER_SECTOR * JOURNAL_BLOCK_SECTORS)
114
115struct journal_sector {
116 __u8 entries[JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR];
117 __u8 mac[JOURNAL_MAC_PER_SECTOR];
118 commit_id_t commit_id;
119};
120
Mikulas Patocka9d609f852017-04-18 16:51:52 -0400121#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 +0100122
123#define METADATA_PADDING_SECTORS 8
124
125#define N_COMMIT_IDS 4
126
127static unsigned char prev_commit_seq(unsigned char seq)
128{
129 return (seq + N_COMMIT_IDS - 1) % N_COMMIT_IDS;
130}
131
132static unsigned char next_commit_seq(unsigned char seq)
133{
134 return (seq + 1) % N_COMMIT_IDS;
135}
136
137/*
138 * In-memory structures
139 */
140
141struct journal_node {
142 struct rb_node node;
143 sector_t sector;
144};
145
146struct alg_spec {
147 char *alg_string;
148 char *key_string;
149 __u8 *key;
150 unsigned key_size;
151};
152
153struct dm_integrity_c {
154 struct dm_dev *dev;
Mikulas Patocka356d9d52018-07-03 20:13:30 +0200155 struct dm_dev *meta_dev;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100156 unsigned tag_size;
157 __s8 log2_tag_size;
158 sector_t start;
Kent Overstreet6f1c8192018-05-20 18:25:53 -0400159 mempool_t journal_io_mempool;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100160 struct dm_io_client *io;
161 struct dm_bufio_client *bufio;
162 struct workqueue_struct *metadata_wq;
163 struct superblock *sb;
164 unsigned journal_pages;
Mikulas Patocka468dfca2019-04-29 14:57:24 +0200165 unsigned n_bitmap_blocks;
166
Mikulas Patocka7eada902017-01-04 20:23:53 +0100167 struct page_list *journal;
168 struct page_list *journal_io;
169 struct page_list *journal_xor;
Mikulas Patocka468dfca2019-04-29 14:57:24 +0200170 struct page_list *recalc_bitmap;
171 struct page_list *may_write_bitmap;
172 struct bitmap_block_status *bbs;
173 unsigned bitmap_flush_interval;
Mikulas Patocka48271492019-04-29 14:57:26 +0200174 int synchronous_mode;
175 struct bio_list synchronous_bios;
Mikulas Patocka468dfca2019-04-29 14:57:24 +0200176 struct delayed_work bitmap_flush_work;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100177
178 struct crypto_skcipher *journal_crypt;
179 struct scatterlist **journal_scatterlist;
180 struct scatterlist **journal_io_scatterlist;
181 struct skcipher_request **sk_requests;
182
183 struct crypto_shash *journal_mac;
184
185 struct journal_node *journal_tree;
186 struct rb_root journal_tree_root;
187
188 sector_t provided_data_sectors;
189
190 unsigned short journal_entry_size;
191 unsigned char journal_entries_per_sector;
192 unsigned char journal_section_entries;
Mikulas Patocka9d609f852017-04-18 16:51:52 -0400193 unsigned short journal_section_sectors;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100194 unsigned journal_sections;
195 unsigned journal_entries;
Mikulas Patocka356d9d52018-07-03 20:13:30 +0200196 sector_t data_device_sectors;
197 sector_t meta_device_sectors;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100198 unsigned initial_sectors;
199 unsigned metadata_run;
200 __s8 log2_metadata_run;
201 __u8 log2_buffer_sectors;
Mikulas Patocka9d609f852017-04-18 16:51:52 -0400202 __u8 sectors_per_block;
Mikulas Patocka468dfca2019-04-29 14:57:24 +0200203 __u8 log2_blocks_per_bitmap_bit;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100204
205 unsigned char mode;
Mikulas Patockac21b1632018-07-03 20:13:25 +0200206 int suspending;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100207
208 int failed;
209
210 struct crypto_shash *internal_hash;
211
212 /* these variables are locked with endio_wait.lock */
213 struct rb_root in_progress;
Mikulas Patocka724376a2018-07-03 20:13:27 +0200214 struct list_head wait_list;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100215 wait_queue_head_t endio_wait;
216 struct workqueue_struct *wait_wq;
217
218 unsigned char commit_seq;
219 commit_id_t commit_ids[N_COMMIT_IDS];
220
221 unsigned committed_section;
222 unsigned n_committed_sections;
223
224 unsigned uncommitted_section;
225 unsigned n_uncommitted_sections;
226
227 unsigned free_section;
228 unsigned char free_section_entry;
229 unsigned free_sectors;
230
231 unsigned free_sectors_threshold;
232
233 struct workqueue_struct *commit_wq;
234 struct work_struct commit_work;
235
236 struct workqueue_struct *writer_wq;
237 struct work_struct writer_work;
238
Mikulas Patockaa3fcf722018-07-03 20:13:33 +0200239 struct workqueue_struct *recalc_wq;
240 struct work_struct recalc_work;
241 u8 *recalc_buffer;
242 u8 *recalc_tags;
243
Mikulas Patocka7eada902017-01-04 20:23:53 +0100244 struct bio_list flush_bio_list;
245
246 unsigned long autocommit_jiffies;
247 struct timer_list autocommit_timer;
248 unsigned autocommit_msec;
249
250 wait_queue_head_t copy_to_journal_wait;
251
252 struct completion crypto_backoff;
253
254 bool journal_uptodate;
255 bool just_formatted;
Mikulas Patocka468dfca2019-04-29 14:57:24 +0200256 bool recalculate_flag;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100257
258 struct alg_spec internal_hash_alg;
259 struct alg_spec journal_crypt_alg;
260 struct alg_spec journal_mac_alg;
Mikulas Patocka3f2e5392017-07-21 12:00:00 -0400261
262 atomic64_t number_of_mismatches;
Mikulas Patocka1f5a7752019-04-29 14:57:25 +0200263
264 struct notifier_block reboot_notifier;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100265};
266
267struct dm_integrity_range {
268 sector_t logical_sector;
Mikulas Patocka4f434462019-04-29 14:57:21 +0200269 sector_t n_sectors;
Mikulas Patocka724376a2018-07-03 20:13:27 +0200270 bool waiting;
271 union {
272 struct rb_node node;
273 struct {
274 struct task_struct *task;
275 struct list_head wait_entry;
276 };
277 };
Mikulas Patocka7eada902017-01-04 20:23:53 +0100278};
279
280struct dm_integrity_io {
281 struct work_struct work;
282
283 struct dm_integrity_c *ic;
284 bool write;
285 bool fua;
286
287 struct dm_integrity_range range;
288
289 sector_t metadata_block;
290 unsigned metadata_offset;
291
292 atomic_t in_flight;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200293 blk_status_t bi_status;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100294
295 struct completion *completion;
296
Christoph Hellwig74d46992017-08-23 19:10:32 +0200297 struct gendisk *orig_bi_disk;
298 u8 orig_bi_partno;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100299 bio_end_io_t *orig_bi_end_io;
300 struct bio_integrity_payload *orig_bi_integrity;
301 struct bvec_iter orig_bi_iter;
302};
303
304struct journal_completion {
305 struct dm_integrity_c *ic;
306 atomic_t in_flight;
307 struct completion comp;
308};
309
310struct journal_io {
311 struct dm_integrity_range range;
312 struct journal_completion *comp;
313};
314
Mikulas Patocka468dfca2019-04-29 14:57:24 +0200315struct bitmap_block_status {
316 struct work_struct work;
317 struct dm_integrity_c *ic;
318 unsigned idx;
319 unsigned long *bitmap;
320 struct bio_list bio_queue;
321 spinlock_t bio_queue_lock;
322
323};
324
Mikulas Patocka7eada902017-01-04 20:23:53 +0100325static struct kmem_cache *journal_io_cache;
326
327#define JOURNAL_IO_MEMPOOL 32
328
329#ifdef DEBUG_PRINT
330#define DEBUG_print(x, ...) printk(KERN_DEBUG x, ##__VA_ARGS__)
331static void __DEBUG_bytes(__u8 *bytes, size_t len, const char *msg, ...)
332{
333 va_list args;
334 va_start(args, msg);
335 vprintk(msg, args);
336 va_end(args);
337 if (len)
338 pr_cont(":");
339 while (len) {
340 pr_cont(" %02x", *bytes);
341 bytes++;
342 len--;
343 }
344 pr_cont("\n");
345}
346#define DEBUG_bytes(bytes, len, msg, ...) __DEBUG_bytes(bytes, len, KERN_DEBUG msg, ##__VA_ARGS__)
347#else
348#define DEBUG_print(x, ...) do { } while (0)
349#define DEBUG_bytes(bytes, len, msg, ...) do { } while (0)
350#endif
351
352/*
353 * DM Integrity profile, protection is performed layer above (dm-crypt)
354 */
Bhumika Goyal7c373d662017-08-06 22:54:00 +0530355static const struct blk_integrity_profile dm_integrity_profile = {
Mikulas Patocka7eada902017-01-04 20:23:53 +0100356 .name = "DM-DIF-EXT-TAG",
357 .generate_fn = NULL,
358 .verify_fn = NULL,
359};
360
361static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map);
362static void integrity_bio_wait(struct work_struct *w);
363static void dm_integrity_dtr(struct dm_target *ti);
364
365static void dm_integrity_io_error(struct dm_integrity_c *ic, const char *msg, int err)
366{
Mikulas Patocka3f2e5392017-07-21 12:00:00 -0400367 if (err == -EILSEQ)
368 atomic64_inc(&ic->number_of_mismatches);
Mikulas Patocka7eada902017-01-04 20:23:53 +0100369 if (!cmpxchg(&ic->failed, 0, err))
370 DMERR("Error on %s: %d", msg, err);
371}
372
373static int dm_integrity_failed(struct dm_integrity_c *ic)
374{
Mark Rutlandd3e632f2017-10-23 14:07:11 -0700375 return READ_ONCE(ic->failed);
Mikulas Patocka7eada902017-01-04 20:23:53 +0100376}
377
378static commit_id_t dm_integrity_commit_id(struct dm_integrity_c *ic, unsigned i,
379 unsigned j, unsigned char seq)
380{
381 /*
382 * Xor the number with section and sector, so that if a piece of
383 * journal is written at wrong place, it is detected.
384 */
385 return ic->commit_ids[seq] ^ cpu_to_le64(((__u64)i << 32) ^ j);
386}
387
388static void get_area_and_offset(struct dm_integrity_c *ic, sector_t data_sector,
389 sector_t *area, sector_t *offset)
390{
Mikulas Patocka356d9d52018-07-03 20:13:30 +0200391 if (!ic->meta_dev) {
392 __u8 log2_interleave_sectors = ic->sb->log2_interleave_sectors;
393 *area = data_sector >> log2_interleave_sectors;
394 *offset = (unsigned)data_sector & ((1U << log2_interleave_sectors) - 1);
395 } else {
396 *area = 0;
397 *offset = data_sector;
398 }
Mikulas Patocka7eada902017-01-04 20:23:53 +0100399}
400
Mikulas Patocka9d609f852017-04-18 16:51:52 -0400401#define sector_to_block(ic, n) \
402do { \
403 BUG_ON((n) & (unsigned)((ic)->sectors_per_block - 1)); \
404 (n) >>= (ic)->sb->log2_sectors_per_block; \
405} while (0)
406
Mikulas Patocka7eada902017-01-04 20:23:53 +0100407static __u64 get_metadata_sector_and_offset(struct dm_integrity_c *ic, sector_t area,
408 sector_t offset, unsigned *metadata_offset)
409{
410 __u64 ms;
411 unsigned mo;
412
413 ms = area << ic->sb->log2_interleave_sectors;
414 if (likely(ic->log2_metadata_run >= 0))
415 ms += area << ic->log2_metadata_run;
416 else
417 ms += area * ic->metadata_run;
418 ms >>= ic->log2_buffer_sectors;
419
Mikulas Patocka9d609f852017-04-18 16:51:52 -0400420 sector_to_block(ic, offset);
421
Mikulas Patocka7eada902017-01-04 20:23:53 +0100422 if (likely(ic->log2_tag_size >= 0)) {
423 ms += offset >> (SECTOR_SHIFT + ic->log2_buffer_sectors - ic->log2_tag_size);
424 mo = (offset << ic->log2_tag_size) & ((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - 1);
425 } else {
426 ms += (__u64)offset * ic->tag_size >> (SECTOR_SHIFT + ic->log2_buffer_sectors);
427 mo = (offset * ic->tag_size) & ((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - 1);
428 }
429 *metadata_offset = mo;
430 return ms;
431}
432
433static sector_t get_data_sector(struct dm_integrity_c *ic, sector_t area, sector_t offset)
434{
435 sector_t result;
436
Mikulas Patocka356d9d52018-07-03 20:13:30 +0200437 if (ic->meta_dev)
438 return offset;
439
Mikulas Patocka7eada902017-01-04 20:23:53 +0100440 result = area << ic->sb->log2_interleave_sectors;
441 if (likely(ic->log2_metadata_run >= 0))
442 result += (area + 1) << ic->log2_metadata_run;
443 else
444 result += (area + 1) * ic->metadata_run;
445
446 result += (sector_t)ic->initial_sectors + offset;
Mikulas Patocka71e9ddb2018-07-03 20:13:29 +0200447 result += ic->start;
448
Mikulas Patocka7eada902017-01-04 20:23:53 +0100449 return result;
450}
451
452static void wraparound_section(struct dm_integrity_c *ic, unsigned *sec_ptr)
453{
454 if (unlikely(*sec_ptr >= ic->journal_sections))
455 *sec_ptr -= ic->journal_sections;
456}
457
Mikulas Patocka1f9fc0b2018-07-03 20:13:31 +0200458static void sb_set_version(struct dm_integrity_c *ic)
459{
Mikulas Patocka468dfca2019-04-29 14:57:24 +0200460 if (ic->mode == 'B' || ic->sb->flags & cpu_to_le32(SB_FLAG_DIRTY_BITMAP))
461 ic->sb->version = SB_VERSION_3;
462 else if (ic->meta_dev || ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))
Mikulas Patocka1f9fc0b2018-07-03 20:13:31 +0200463 ic->sb->version = SB_VERSION_2;
464 else
465 ic->sb->version = SB_VERSION_1;
466}
467
Mikulas Patocka7eada902017-01-04 20:23:53 +0100468static int sync_rw_sb(struct dm_integrity_c *ic, int op, int op_flags)
469{
470 struct dm_io_request io_req;
471 struct dm_io_region io_loc;
472
473 io_req.bi_op = op;
474 io_req.bi_op_flags = op_flags;
475 io_req.mem.type = DM_IO_KMEM;
476 io_req.mem.ptr.addr = ic->sb;
477 io_req.notify.fn = NULL;
478 io_req.client = ic->io;
Mikulas Patocka356d9d52018-07-03 20:13:30 +0200479 io_loc.bdev = ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100480 io_loc.sector = ic->start;
481 io_loc.count = SB_SECTORS;
482
483 return dm_io(&io_req, 1, &io_loc, NULL);
484}
485
Mikulas Patocka468dfca2019-04-29 14:57:24 +0200486#define BITMAP_OP_TEST_ALL_SET 0
487#define BITMAP_OP_TEST_ALL_CLEAR 1
488#define BITMAP_OP_SET 2
489#define BITMAP_OP_CLEAR 3
490
Mike Snitzer05d69092019-05-09 15:25:49 -0400491static bool block_bitmap_op(struct dm_integrity_c *ic, struct page_list *bitmap,
492 sector_t sector, sector_t n_sectors, int mode)
Mikulas Patocka468dfca2019-04-29 14:57:24 +0200493{
494 unsigned long bit, end_bit, this_end_bit, page, end_page;
495 unsigned long *data;
496
497 if (unlikely(((sector | n_sectors) & ((1 << ic->sb->log2_sectors_per_block) - 1)) != 0)) {
Mike Snitzer05d69092019-05-09 15:25:49 -0400498 DMCRIT("invalid bitmap access (%llx,%llx,%d,%d,%d)",
Mikulas Patocka468dfca2019-04-29 14:57:24 +0200499 (unsigned long long)sector,
500 (unsigned long long)n_sectors,
501 ic->sb->log2_sectors_per_block,
502 ic->log2_blocks_per_bitmap_bit,
503 mode);
504 BUG();
505 }
506
507 if (unlikely(!n_sectors))
508 return true;
509
510 bit = sector >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
Mike Snitzer05d69092019-05-09 15:25:49 -0400511 end_bit = (sector + n_sectors - 1) >>
512 (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
Mikulas Patocka468dfca2019-04-29 14:57:24 +0200513
514 page = bit / (PAGE_SIZE * 8);
515 bit %= PAGE_SIZE * 8;
516
517 end_page = end_bit / (PAGE_SIZE * 8);
518 end_bit %= PAGE_SIZE * 8;
519
520repeat:
521 if (page < end_page) {
522 this_end_bit = PAGE_SIZE * 8 - 1;
523 } else {
524 this_end_bit = end_bit;
525 }
526
527 data = lowmem_page_address(bitmap[page].page);
528
529 if (mode == BITMAP_OP_TEST_ALL_SET) {
530 while (bit <= this_end_bit) {
531 if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
532 do {
533 if (data[bit / BITS_PER_LONG] != -1)
534 return false;
535 bit += BITS_PER_LONG;
536 } while (this_end_bit >= bit + BITS_PER_LONG - 1);
537 continue;
538 }
539 if (!test_bit(bit, data))
540 return false;
541 bit++;
542 }
543 } else if (mode == BITMAP_OP_TEST_ALL_CLEAR) {
544 while (bit <= this_end_bit) {
545 if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
546 do {
547 if (data[bit / BITS_PER_LONG] != 0)
548 return false;
549 bit += BITS_PER_LONG;
550 } while (this_end_bit >= bit + BITS_PER_LONG - 1);
551 continue;
552 }
553 if (test_bit(bit, data))
554 return false;
555 bit++;
556 }
557 } else if (mode == BITMAP_OP_SET) {
558 while (bit <= this_end_bit) {
559 if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
560 do {
561 data[bit / BITS_PER_LONG] = -1;
562 bit += BITS_PER_LONG;
563 } while (this_end_bit >= bit + BITS_PER_LONG - 1);
564 continue;
565 }
566 __set_bit(bit, data);
567 bit++;
568 }
569 } else if (mode == BITMAP_OP_CLEAR) {
570 if (!bit && this_end_bit == PAGE_SIZE * 8 - 1)
571 clear_page(data);
572 else while (bit <= this_end_bit) {
573 if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
574 do {
575 data[bit / BITS_PER_LONG] = 0;
576 bit += BITS_PER_LONG;
577 } while (this_end_bit >= bit + BITS_PER_LONG - 1);
578 continue;
579 }
580 __clear_bit(bit, data);
581 bit++;
582 }
583 } else {
584 BUG();
585 }
586
587 if (unlikely(page < end_page)) {
588 bit = 0;
589 page++;
590 goto repeat;
591 }
592
593 return true;
594}
595
596static void block_bitmap_copy(struct dm_integrity_c *ic, struct page_list *dst, struct page_list *src)
597{
598 unsigned n_bitmap_pages = DIV_ROUND_UP(ic->n_bitmap_blocks, PAGE_SIZE / BITMAP_BLOCK_SIZE);
599 unsigned i;
600
601 for (i = 0; i < n_bitmap_pages; i++) {
602 unsigned long *dst_data = lowmem_page_address(dst[i].page);
603 unsigned long *src_data = lowmem_page_address(src[i].page);
604 copy_page(dst_data, src_data);
605 }
606}
607
608static struct bitmap_block_status *sector_to_bitmap_block(struct dm_integrity_c *ic, sector_t sector)
609{
610 unsigned bit = sector >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
611 unsigned bitmap_block = bit / (BITMAP_BLOCK_SIZE * 8);
612
613 BUG_ON(bitmap_block >= ic->n_bitmap_blocks);
614 return &ic->bbs[bitmap_block];
615}
616
Mikulas Patocka7eada902017-01-04 20:23:53 +0100617static void access_journal_check(struct dm_integrity_c *ic, unsigned section, unsigned offset,
618 bool e, const char *function)
619{
620#if defined(CONFIG_DM_DEBUG) || defined(INTERNAL_VERIFY)
621 unsigned limit = e ? ic->journal_section_entries : ic->journal_section_sectors;
622
623 if (unlikely(section >= ic->journal_sections) ||
624 unlikely(offset >= limit)) {
Mike Snitzer05d69092019-05-09 15:25:49 -0400625 DMCRIT("%s: invalid access at (%u,%u), limit (%u,%u)",
626 function, section, offset, ic->journal_sections, limit);
Mikulas Patocka7eada902017-01-04 20:23:53 +0100627 BUG();
628 }
629#endif
630}
631
632static void page_list_location(struct dm_integrity_c *ic, unsigned section, unsigned offset,
633 unsigned *pl_index, unsigned *pl_offset)
634{
635 unsigned sector;
636
Mikulas Patocka56b67a42017-04-18 16:51:50 -0400637 access_journal_check(ic, section, offset, false, "page_list_location");
Mikulas Patocka7eada902017-01-04 20:23:53 +0100638
639 sector = section * ic->journal_section_sectors + offset;
640
641 *pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
642 *pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
643}
644
645static struct journal_sector *access_page_list(struct dm_integrity_c *ic, struct page_list *pl,
646 unsigned section, unsigned offset, unsigned *n_sectors)
647{
648 unsigned pl_index, pl_offset;
649 char *va;
650
651 page_list_location(ic, section, offset, &pl_index, &pl_offset);
652
653 if (n_sectors)
654 *n_sectors = (PAGE_SIZE - pl_offset) >> SECTOR_SHIFT;
655
656 va = lowmem_page_address(pl[pl_index].page);
657
658 return (struct journal_sector *)(va + pl_offset);
659}
660
661static struct journal_sector *access_journal(struct dm_integrity_c *ic, unsigned section, unsigned offset)
662{
663 return access_page_list(ic, ic->journal, section, offset, NULL);
664}
665
666static struct journal_entry *access_journal_entry(struct dm_integrity_c *ic, unsigned section, unsigned n)
667{
668 unsigned rel_sector, offset;
669 struct journal_sector *js;
670
671 access_journal_check(ic, section, n, true, "access_journal_entry");
672
673 rel_sector = n % JOURNAL_BLOCK_SECTORS;
674 offset = n / JOURNAL_BLOCK_SECTORS;
675
676 js = access_journal(ic, section, rel_sector);
677 return (struct journal_entry *)((char *)js + offset * ic->journal_entry_size);
678}
679
680static struct journal_sector *access_journal_data(struct dm_integrity_c *ic, unsigned section, unsigned n)
681{
Mikulas Patocka9d609f852017-04-18 16:51:52 -0400682 n <<= ic->sb->log2_sectors_per_block;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100683
Mikulas Patocka9d609f852017-04-18 16:51:52 -0400684 n += JOURNAL_BLOCK_SECTORS;
685
686 access_journal_check(ic, section, n, false, "access_journal_data");
687
688 return access_journal(ic, section, n);
Mikulas Patocka7eada902017-01-04 20:23:53 +0100689}
690
691static void section_mac(struct dm_integrity_c *ic, unsigned section, __u8 result[JOURNAL_MAC_SIZE])
692{
693 SHASH_DESC_ON_STACK(desc, ic->journal_mac);
694 int r;
695 unsigned j, size;
696
697 desc->tfm = ic->journal_mac;
Mikulas Patocka432061b2018-09-05 09:17:45 -0400698 desc->flags = 0;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100699
700 r = crypto_shash_init(desc);
701 if (unlikely(r)) {
702 dm_integrity_io_error(ic, "crypto_shash_init", r);
703 goto err;
704 }
705
706 for (j = 0; j < ic->journal_section_entries; j++) {
707 struct journal_entry *je = access_journal_entry(ic, section, j);
708 r = crypto_shash_update(desc, (__u8 *)&je->u.sector, sizeof je->u.sector);
709 if (unlikely(r)) {
710 dm_integrity_io_error(ic, "crypto_shash_update", r);
711 goto err;
712 }
713 }
714
715 size = crypto_shash_digestsize(ic->journal_mac);
716
717 if (likely(size <= JOURNAL_MAC_SIZE)) {
718 r = crypto_shash_final(desc, result);
719 if (unlikely(r)) {
720 dm_integrity_io_error(ic, "crypto_shash_final", r);
721 goto err;
722 }
723 memset(result + size, 0, JOURNAL_MAC_SIZE - size);
724 } else {
Kees Cook6d39a122018-08-07 14:18:39 -0700725 __u8 digest[HASH_MAX_DIGESTSIZE];
726
727 if (WARN_ON(size > sizeof(digest))) {
728 dm_integrity_io_error(ic, "digest_size", -EINVAL);
729 goto err;
730 }
Mikulas Patocka7eada902017-01-04 20:23:53 +0100731 r = crypto_shash_final(desc, digest);
732 if (unlikely(r)) {
733 dm_integrity_io_error(ic, "crypto_shash_final", r);
734 goto err;
735 }
736 memcpy(result, digest, JOURNAL_MAC_SIZE);
737 }
738
739 return;
740err:
741 memset(result, 0, JOURNAL_MAC_SIZE);
742}
743
744static void rw_section_mac(struct dm_integrity_c *ic, unsigned section, bool wr)
745{
746 __u8 result[JOURNAL_MAC_SIZE];
747 unsigned j;
748
749 if (!ic->journal_mac)
750 return;
751
752 section_mac(ic, section, result);
753
754 for (j = 0; j < JOURNAL_BLOCK_SECTORS; j++) {
755 struct journal_sector *js = access_journal(ic, section, j);
756
757 if (likely(wr))
758 memcpy(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR);
759 else {
760 if (memcmp(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR))
761 dm_integrity_io_error(ic, "journal mac", -EILSEQ);
762 }
763 }
764}
765
766static void complete_journal_op(void *context)
767{
768 struct journal_completion *comp = context;
769 BUG_ON(!atomic_read(&comp->in_flight));
770 if (likely(atomic_dec_and_test(&comp->in_flight)))
771 complete(&comp->comp);
772}
773
774static void xor_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
775 unsigned n_sections, struct journal_completion *comp)
776{
777 struct async_submit_ctl submit;
778 size_t n_bytes = (size_t)(n_sections * ic->journal_section_sectors) << SECTOR_SHIFT;
779 unsigned pl_index, pl_offset, section_index;
780 struct page_list *source_pl, *target_pl;
781
782 if (likely(encrypt)) {
783 source_pl = ic->journal;
784 target_pl = ic->journal_io;
785 } else {
786 source_pl = ic->journal_io;
787 target_pl = ic->journal;
788 }
789
790 page_list_location(ic, section, 0, &pl_index, &pl_offset);
791
792 atomic_add(roundup(pl_offset + n_bytes, PAGE_SIZE) >> PAGE_SHIFT, &comp->in_flight);
793
794 init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL, complete_journal_op, comp, NULL);
795
796 section_index = pl_index;
797
798 do {
799 size_t this_step;
800 struct page *src_pages[2];
801 struct page *dst_page;
802
803 while (unlikely(pl_index == section_index)) {
804 unsigned dummy;
805 if (likely(encrypt))
806 rw_section_mac(ic, section, true);
807 section++;
808 n_sections--;
809 if (!n_sections)
810 break;
811 page_list_location(ic, section, 0, &section_index, &dummy);
812 }
813
814 this_step = min(n_bytes, (size_t)PAGE_SIZE - pl_offset);
815 dst_page = target_pl[pl_index].page;
816 src_pages[0] = source_pl[pl_index].page;
817 src_pages[1] = ic->journal_xor[pl_index].page;
818
819 async_xor(dst_page, src_pages, pl_offset, 2, this_step, &submit);
820
821 pl_index++;
822 pl_offset = 0;
823 n_bytes -= this_step;
824 } while (n_bytes);
825
826 BUG_ON(n_sections);
827
828 async_tx_issue_pending_all();
829}
830
831static void complete_journal_encrypt(struct crypto_async_request *req, int err)
832{
833 struct journal_completion *comp = req->data;
834 if (unlikely(err)) {
835 if (likely(err == -EINPROGRESS)) {
836 complete(&comp->ic->crypto_backoff);
837 return;
838 }
839 dm_integrity_io_error(comp->ic, "asynchronous encrypt", err);
840 }
841 complete_journal_op(comp);
842}
843
844static bool do_crypt(bool encrypt, struct skcipher_request *req, struct journal_completion *comp)
845{
846 int r;
Mikulas Patocka432061b2018-09-05 09:17:45 -0400847 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Mikulas Patocka7eada902017-01-04 20:23:53 +0100848 complete_journal_encrypt, comp);
849 if (likely(encrypt))
850 r = crypto_skcipher_encrypt(req);
851 else
852 r = crypto_skcipher_decrypt(req);
853 if (likely(!r))
854 return false;
855 if (likely(r == -EINPROGRESS))
856 return true;
857 if (likely(r == -EBUSY)) {
858 wait_for_completion(&comp->ic->crypto_backoff);
859 reinit_completion(&comp->ic->crypto_backoff);
860 return true;
861 }
862 dm_integrity_io_error(comp->ic, "encrypt", r);
863 return false;
864}
865
866static void crypt_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
867 unsigned n_sections, struct journal_completion *comp)
868{
869 struct scatterlist **source_sg;
870 struct scatterlist **target_sg;
871
872 atomic_add(2, &comp->in_flight);
873
874 if (likely(encrypt)) {
875 source_sg = ic->journal_scatterlist;
876 target_sg = ic->journal_io_scatterlist;
877 } else {
878 source_sg = ic->journal_io_scatterlist;
879 target_sg = ic->journal_scatterlist;
880 }
881
882 do {
883 struct skcipher_request *req;
884 unsigned ivsize;
885 char *iv;
886
887 if (likely(encrypt))
888 rw_section_mac(ic, section, true);
889
890 req = ic->sk_requests[section];
891 ivsize = crypto_skcipher_ivsize(ic->journal_crypt);
892 iv = req->iv;
893
894 memcpy(iv, iv + ivsize, ivsize);
895
896 req->src = source_sg[section];
897 req->dst = target_sg[section];
898
899 if (unlikely(do_crypt(encrypt, req, comp)))
900 atomic_inc(&comp->in_flight);
901
902 section++;
903 n_sections--;
904 } while (n_sections);
905
906 atomic_dec(&comp->in_flight);
907 complete_journal_op(comp);
908}
909
910static void encrypt_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
911 unsigned n_sections, struct journal_completion *comp)
912{
913 if (ic->journal_xor)
914 return xor_journal(ic, encrypt, section, n_sections, comp);
915 else
916 return crypt_journal(ic, encrypt, section, n_sections, comp);
917}
918
919static void complete_journal_io(unsigned long error, void *context)
920{
921 struct journal_completion *comp = context;
922 if (unlikely(error != 0))
923 dm_integrity_io_error(comp->ic, "writing journal", -EIO);
924 complete_journal_op(comp);
925}
926
Mikulas Patocka981e8a92019-04-29 14:57:19 +0200927static void rw_journal_sectors(struct dm_integrity_c *ic, int op, int op_flags,
928 unsigned sector, unsigned n_sectors, struct journal_completion *comp)
Mikulas Patocka7eada902017-01-04 20:23:53 +0100929{
930 struct dm_io_request io_req;
931 struct dm_io_region io_loc;
Mikulas Patocka981e8a92019-04-29 14:57:19 +0200932 unsigned pl_index, pl_offset;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100933 int r;
934
935 if (unlikely(dm_integrity_failed(ic))) {
936 if (comp)
937 complete_journal_io(-1UL, comp);
938 return;
939 }
940
Mikulas Patocka7eada902017-01-04 20:23:53 +0100941 pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
942 pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
943
944 io_req.bi_op = op;
945 io_req.bi_op_flags = op_flags;
946 io_req.mem.type = DM_IO_PAGE_LIST;
947 if (ic->journal_io)
948 io_req.mem.ptr.pl = &ic->journal_io[pl_index];
949 else
950 io_req.mem.ptr.pl = &ic->journal[pl_index];
951 io_req.mem.offset = pl_offset;
952 if (likely(comp != NULL)) {
953 io_req.notify.fn = complete_journal_io;
954 io_req.notify.context = comp;
955 } else {
956 io_req.notify.fn = NULL;
957 }
958 io_req.client = ic->io;
Mikulas Patocka356d9d52018-07-03 20:13:30 +0200959 io_loc.bdev = ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100960 io_loc.sector = ic->start + SB_SECTORS + sector;
961 io_loc.count = n_sectors;
962
963 r = dm_io(&io_req, 1, &io_loc, NULL);
964 if (unlikely(r)) {
965 dm_integrity_io_error(ic, op == REQ_OP_READ ? "reading journal" : "writing journal", r);
966 if (comp) {
967 WARN_ONCE(1, "asynchronous dm_io failed: %d", r);
968 complete_journal_io(-1UL, comp);
969 }
970 }
971}
972
Mikulas Patocka981e8a92019-04-29 14:57:19 +0200973static void rw_journal(struct dm_integrity_c *ic, int op, int op_flags, unsigned section,
974 unsigned n_sections, struct journal_completion *comp)
975{
976 unsigned sector, n_sectors;
977
978 sector = section * ic->journal_section_sectors;
979 n_sectors = n_sections * ic->journal_section_sectors;
980
981 rw_journal_sectors(ic, op, op_flags, sector, n_sectors, comp);
982}
983
Mikulas Patocka7eada902017-01-04 20:23:53 +0100984static void write_journal(struct dm_integrity_c *ic, unsigned commit_start, unsigned commit_sections)
985{
986 struct journal_completion io_comp;
987 struct journal_completion crypt_comp_1;
988 struct journal_completion crypt_comp_2;
989 unsigned i;
990
991 io_comp.ic = ic;
Arnd Bergmannb5e8ad92017-08-15 17:11:59 +0200992 init_completion(&io_comp.comp);
Mikulas Patocka7eada902017-01-04 20:23:53 +0100993
994 if (commit_start + commit_sections <= ic->journal_sections) {
995 io_comp.in_flight = (atomic_t)ATOMIC_INIT(1);
996 if (ic->journal_io) {
997 crypt_comp_1.ic = ic;
Arnd Bergmannb5e8ad92017-08-15 17:11:59 +0200998 init_completion(&crypt_comp_1.comp);
Mikulas Patocka7eada902017-01-04 20:23:53 +0100999 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
1000 encrypt_journal(ic, true, commit_start, commit_sections, &crypt_comp_1);
1001 wait_for_completion_io(&crypt_comp_1.comp);
1002 } else {
1003 for (i = 0; i < commit_sections; i++)
1004 rw_section_mac(ic, commit_start + i, true);
1005 }
Jan Karaff0361b2017-05-31 09:44:32 +02001006 rw_journal(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, commit_start,
1007 commit_sections, &io_comp);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001008 } else {
1009 unsigned to_end;
1010 io_comp.in_flight = (atomic_t)ATOMIC_INIT(2);
1011 to_end = ic->journal_sections - commit_start;
1012 if (ic->journal_io) {
1013 crypt_comp_1.ic = ic;
Arnd Bergmannb5e8ad92017-08-15 17:11:59 +02001014 init_completion(&crypt_comp_1.comp);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001015 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
1016 encrypt_journal(ic, true, commit_start, to_end, &crypt_comp_1);
1017 if (try_wait_for_completion(&crypt_comp_1.comp)) {
1018 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
Arnd Bergmannb5e8ad92017-08-15 17:11:59 +02001019 reinit_completion(&crypt_comp_1.comp);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001020 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
1021 encrypt_journal(ic, true, 0, commit_sections - to_end, &crypt_comp_1);
1022 wait_for_completion_io(&crypt_comp_1.comp);
1023 } else {
1024 crypt_comp_2.ic = ic;
Arnd Bergmannb5e8ad92017-08-15 17:11:59 +02001025 init_completion(&crypt_comp_2.comp);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001026 crypt_comp_2.in_flight = (atomic_t)ATOMIC_INIT(0);
1027 encrypt_journal(ic, true, 0, commit_sections - to_end, &crypt_comp_2);
1028 wait_for_completion_io(&crypt_comp_1.comp);
1029 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
1030 wait_for_completion_io(&crypt_comp_2.comp);
1031 }
1032 } else {
1033 for (i = 0; i < to_end; i++)
1034 rw_section_mac(ic, commit_start + i, true);
1035 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
1036 for (i = 0; i < commit_sections - to_end; i++)
1037 rw_section_mac(ic, i, true);
1038 }
1039 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, 0, commit_sections - to_end, &io_comp);
1040 }
1041
1042 wait_for_completion_io(&io_comp.comp);
1043}
1044
1045static void copy_from_journal(struct dm_integrity_c *ic, unsigned section, unsigned offset,
1046 unsigned n_sectors, sector_t target, io_notify_fn fn, void *data)
1047{
1048 struct dm_io_request io_req;
1049 struct dm_io_region io_loc;
1050 int r;
1051 unsigned sector, pl_index, pl_offset;
1052
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001053 BUG_ON((target | n_sectors | offset) & (unsigned)(ic->sectors_per_block - 1));
1054
Mikulas Patocka7eada902017-01-04 20:23:53 +01001055 if (unlikely(dm_integrity_failed(ic))) {
1056 fn(-1UL, data);
1057 return;
1058 }
1059
1060 sector = section * ic->journal_section_sectors + JOURNAL_BLOCK_SECTORS + offset;
1061
1062 pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
1063 pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
1064
1065 io_req.bi_op = REQ_OP_WRITE;
1066 io_req.bi_op_flags = 0;
1067 io_req.mem.type = DM_IO_PAGE_LIST;
1068 io_req.mem.ptr.pl = &ic->journal[pl_index];
1069 io_req.mem.offset = pl_offset;
1070 io_req.notify.fn = fn;
1071 io_req.notify.context = data;
1072 io_req.client = ic->io;
1073 io_loc.bdev = ic->dev->bdev;
Mikulas Patocka71e9ddb2018-07-03 20:13:29 +02001074 io_loc.sector = target;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001075 io_loc.count = n_sectors;
1076
1077 r = dm_io(&io_req, 1, &io_loc, NULL);
1078 if (unlikely(r)) {
1079 WARN_ONCE(1, "asynchronous dm_io failed: %d", r);
1080 fn(-1UL, data);
1081 }
1082}
1083
Mikulas Patocka724376a2018-07-03 20:13:27 +02001084static bool ranges_overlap(struct dm_integrity_range *range1, struct dm_integrity_range *range2)
1085{
1086 return range1->logical_sector < range2->logical_sector + range2->n_sectors &&
Mikulas Patocka4ed319c2019-04-05 15:26:39 -04001087 range1->logical_sector + range1->n_sectors > range2->logical_sector;
Mikulas Patocka724376a2018-07-03 20:13:27 +02001088}
1089
1090static bool add_new_range(struct dm_integrity_c *ic, struct dm_integrity_range *new_range, bool check_waiting)
Mikulas Patocka7eada902017-01-04 20:23:53 +01001091{
1092 struct rb_node **n = &ic->in_progress.rb_node;
1093 struct rb_node *parent;
1094
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001095 BUG_ON((new_range->logical_sector | new_range->n_sectors) & (unsigned)(ic->sectors_per_block - 1));
1096
Mikulas Patocka724376a2018-07-03 20:13:27 +02001097 if (likely(check_waiting)) {
1098 struct dm_integrity_range *range;
1099 list_for_each_entry(range, &ic->wait_list, wait_entry) {
1100 if (unlikely(ranges_overlap(range, new_range)))
1101 return false;
1102 }
1103 }
1104
Mikulas Patocka7eada902017-01-04 20:23:53 +01001105 parent = NULL;
1106
1107 while (*n) {
1108 struct dm_integrity_range *range = container_of(*n, struct dm_integrity_range, node);
1109
1110 parent = *n;
1111 if (new_range->logical_sector + new_range->n_sectors <= range->logical_sector) {
1112 n = &range->node.rb_left;
1113 } else if (new_range->logical_sector >= range->logical_sector + range->n_sectors) {
1114 n = &range->node.rb_right;
1115 } else {
1116 return false;
1117 }
1118 }
1119
1120 rb_link_node(&new_range->node, parent, n);
1121 rb_insert_color(&new_range->node, &ic->in_progress);
1122
1123 return true;
1124}
1125
1126static void remove_range_unlocked(struct dm_integrity_c *ic, struct dm_integrity_range *range)
1127{
1128 rb_erase(&range->node, &ic->in_progress);
Mikulas Patocka724376a2018-07-03 20:13:27 +02001129 while (unlikely(!list_empty(&ic->wait_list))) {
1130 struct dm_integrity_range *last_range =
1131 list_first_entry(&ic->wait_list, struct dm_integrity_range, wait_entry);
1132 struct task_struct *last_range_task;
Mikulas Patocka724376a2018-07-03 20:13:27 +02001133 last_range_task = last_range->task;
1134 list_del(&last_range->wait_entry);
1135 if (!add_new_range(ic, last_range, false)) {
1136 last_range->task = last_range_task;
1137 list_add(&last_range->wait_entry, &ic->wait_list);
1138 break;
1139 }
1140 last_range->waiting = false;
1141 wake_up_process(last_range_task);
1142 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01001143}
1144
1145static void remove_range(struct dm_integrity_c *ic, struct dm_integrity_range *range)
1146{
1147 unsigned long flags;
1148
1149 spin_lock_irqsave(&ic->endio_wait.lock, flags);
1150 remove_range_unlocked(ic, range);
1151 spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
1152}
1153
Mikulas Patocka724376a2018-07-03 20:13:27 +02001154static void wait_and_add_new_range(struct dm_integrity_c *ic, struct dm_integrity_range *new_range)
1155{
1156 new_range->waiting = true;
1157 list_add_tail(&new_range->wait_entry, &ic->wait_list);
1158 new_range->task = current;
1159 do {
1160 __set_current_state(TASK_UNINTERRUPTIBLE);
1161 spin_unlock_irq(&ic->endio_wait.lock);
1162 io_schedule();
1163 spin_lock_irq(&ic->endio_wait.lock);
1164 } while (unlikely(new_range->waiting));
1165}
1166
Mikulas Patocka8b3bbd42019-04-29 14:57:22 +02001167static void add_new_range_and_wait(struct dm_integrity_c *ic, struct dm_integrity_range *new_range)
1168{
1169 if (unlikely(!add_new_range(ic, new_range, true)))
1170 wait_and_add_new_range(ic, new_range);
1171}
1172
Mikulas Patocka7eada902017-01-04 20:23:53 +01001173static void init_journal_node(struct journal_node *node)
1174{
1175 RB_CLEAR_NODE(&node->node);
1176 node->sector = (sector_t)-1;
1177}
1178
1179static void add_journal_node(struct dm_integrity_c *ic, struct journal_node *node, sector_t sector)
1180{
1181 struct rb_node **link;
1182 struct rb_node *parent;
1183
1184 node->sector = sector;
1185 BUG_ON(!RB_EMPTY_NODE(&node->node));
1186
1187 link = &ic->journal_tree_root.rb_node;
1188 parent = NULL;
1189
1190 while (*link) {
1191 struct journal_node *j;
1192 parent = *link;
1193 j = container_of(parent, struct journal_node, node);
1194 if (sector < j->sector)
1195 link = &j->node.rb_left;
1196 else
1197 link = &j->node.rb_right;
1198 }
1199
1200 rb_link_node(&node->node, parent, link);
1201 rb_insert_color(&node->node, &ic->journal_tree_root);
1202}
1203
1204static void remove_journal_node(struct dm_integrity_c *ic, struct journal_node *node)
1205{
1206 BUG_ON(RB_EMPTY_NODE(&node->node));
1207 rb_erase(&node->node, &ic->journal_tree_root);
1208 init_journal_node(node);
1209}
1210
1211#define NOT_FOUND (-1U)
1212
1213static unsigned find_journal_node(struct dm_integrity_c *ic, sector_t sector, sector_t *next_sector)
1214{
1215 struct rb_node *n = ic->journal_tree_root.rb_node;
1216 unsigned found = NOT_FOUND;
1217 *next_sector = (sector_t)-1;
1218 while (n) {
1219 struct journal_node *j = container_of(n, struct journal_node, node);
1220 if (sector == j->sector) {
1221 found = j - ic->journal_tree;
1222 }
1223 if (sector < j->sector) {
1224 *next_sector = j->sector;
1225 n = j->node.rb_left;
1226 } else {
1227 n = j->node.rb_right;
1228 }
1229 }
1230
1231 return found;
1232}
1233
1234static bool test_journal_node(struct dm_integrity_c *ic, unsigned pos, sector_t sector)
1235{
1236 struct journal_node *node, *next_node;
1237 struct rb_node *next;
1238
1239 if (unlikely(pos >= ic->journal_entries))
1240 return false;
1241 node = &ic->journal_tree[pos];
1242 if (unlikely(RB_EMPTY_NODE(&node->node)))
1243 return false;
1244 if (unlikely(node->sector != sector))
1245 return false;
1246
1247 next = rb_next(&node->node);
1248 if (unlikely(!next))
1249 return true;
1250
1251 next_node = container_of(next, struct journal_node, node);
1252 return next_node->sector != sector;
1253}
1254
1255static bool find_newer_committed_node(struct dm_integrity_c *ic, struct journal_node *node)
1256{
1257 struct rb_node *next;
1258 struct journal_node *next_node;
1259 unsigned next_section;
1260
1261 BUG_ON(RB_EMPTY_NODE(&node->node));
1262
1263 next = rb_next(&node->node);
1264 if (unlikely(!next))
1265 return false;
1266
1267 next_node = container_of(next, struct journal_node, node);
1268
1269 if (next_node->sector != node->sector)
1270 return false;
1271
1272 next_section = (unsigned)(next_node - ic->journal_tree) / ic->journal_section_entries;
1273 if (next_section >= ic->committed_section &&
1274 next_section < ic->committed_section + ic->n_committed_sections)
1275 return true;
1276 if (next_section + ic->journal_sections < ic->committed_section + ic->n_committed_sections)
1277 return true;
1278
1279 return false;
1280}
1281
1282#define TAG_READ 0
1283#define TAG_WRITE 1
1284#define TAG_CMP 2
1285
1286static int dm_integrity_rw_tag(struct dm_integrity_c *ic, unsigned char *tag, sector_t *metadata_block,
1287 unsigned *metadata_offset, unsigned total_size, int op)
1288{
1289 do {
1290 unsigned char *data, *dp;
1291 struct dm_buffer *b;
1292 unsigned to_copy;
1293 int r;
1294
1295 r = dm_integrity_failed(ic);
1296 if (unlikely(r))
1297 return r;
1298
1299 data = dm_bufio_read(ic->bufio, *metadata_block, &b);
Chengguang Xu5e3d0e32019-02-13 13:46:56 +08001300 if (IS_ERR(data))
Mikulas Patocka7eada902017-01-04 20:23:53 +01001301 return PTR_ERR(data);
1302
1303 to_copy = min((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - *metadata_offset, total_size);
1304 dp = data + *metadata_offset;
1305 if (op == TAG_READ) {
1306 memcpy(tag, dp, to_copy);
1307 } else if (op == TAG_WRITE) {
1308 memcpy(dp, tag, to_copy);
Mikulas Patocka1e3b21c2017-04-30 17:31:22 -04001309 dm_bufio_mark_partial_buffer_dirty(b, *metadata_offset, *metadata_offset + to_copy);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001310 } else {
1311 /* e.g.: op == TAG_CMP */
1312 if (unlikely(memcmp(dp, tag, to_copy))) {
1313 unsigned i;
1314
1315 for (i = 0; i < to_copy; i++) {
1316 if (dp[i] != tag[i])
1317 break;
1318 total_size--;
1319 }
1320 dm_bufio_release(b);
1321 return total_size;
1322 }
1323 }
1324 dm_bufio_release(b);
1325
1326 tag += to_copy;
1327 *metadata_offset += to_copy;
1328 if (unlikely(*metadata_offset == 1U << SECTOR_SHIFT << ic->log2_buffer_sectors)) {
1329 (*metadata_block)++;
1330 *metadata_offset = 0;
1331 }
1332 total_size -= to_copy;
1333 } while (unlikely(total_size));
1334
1335 return 0;
1336}
1337
1338static void dm_integrity_flush_buffers(struct dm_integrity_c *ic)
1339{
1340 int r;
1341 r = dm_bufio_write_dirty_buffers(ic->bufio);
1342 if (unlikely(r))
1343 dm_integrity_io_error(ic, "writing tags", r);
1344}
1345
1346static void sleep_on_endio_wait(struct dm_integrity_c *ic)
1347{
1348 DECLARE_WAITQUEUE(wait, current);
1349 __add_wait_queue(&ic->endio_wait, &wait);
1350 __set_current_state(TASK_UNINTERRUPTIBLE);
1351 spin_unlock_irq(&ic->endio_wait.lock);
1352 io_schedule();
1353 spin_lock_irq(&ic->endio_wait.lock);
1354 __remove_wait_queue(&ic->endio_wait, &wait);
1355}
1356
Kees Cook8376d3c2017-10-16 17:01:48 -07001357static void autocommit_fn(struct timer_list *t)
Mikulas Patocka7eada902017-01-04 20:23:53 +01001358{
Kees Cook8376d3c2017-10-16 17:01:48 -07001359 struct dm_integrity_c *ic = from_timer(ic, t, autocommit_timer);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001360
1361 if (likely(!dm_integrity_failed(ic)))
1362 queue_work(ic->commit_wq, &ic->commit_work);
1363}
1364
1365static void schedule_autocommit(struct dm_integrity_c *ic)
1366{
1367 if (!timer_pending(&ic->autocommit_timer))
1368 mod_timer(&ic->autocommit_timer, jiffies + ic->autocommit_jiffies);
1369}
1370
1371static void submit_flush_bio(struct dm_integrity_c *ic, struct dm_integrity_io *dio)
1372{
1373 struct bio *bio;
Mike Snitzer7def52b2017-06-19 10:55:47 -04001374 unsigned long flags;
1375
1376 spin_lock_irqsave(&ic->endio_wait.lock, flags);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001377 bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1378 bio_list_add(&ic->flush_bio_list, bio);
Mike Snitzer7def52b2017-06-19 10:55:47 -04001379 spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
1380
Mikulas Patocka7eada902017-01-04 20:23:53 +01001381 queue_work(ic->commit_wq, &ic->commit_work);
1382}
1383
1384static void do_endio(struct dm_integrity_c *ic, struct bio *bio)
1385{
1386 int r = dm_integrity_failed(ic);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001387 if (unlikely(r) && !bio->bi_status)
1388 bio->bi_status = errno_to_blk_status(r);
Mikulas Patocka48271492019-04-29 14:57:26 +02001389 if (unlikely(ic->synchronous_mode) && bio_op(bio) == REQ_OP_WRITE) {
1390 unsigned long flags;
1391 spin_lock_irqsave(&ic->endio_wait.lock, flags);
1392 bio_list_add(&ic->synchronous_bios, bio);
1393 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0);
1394 spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
1395 return;
1396 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01001397 bio_endio(bio);
1398}
1399
1400static void do_endio_flush(struct dm_integrity_c *ic, struct dm_integrity_io *dio)
1401{
1402 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1403
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001404 if (unlikely(dio->fua) && likely(!bio->bi_status) && likely(!dm_integrity_failed(ic)))
Mikulas Patocka7eada902017-01-04 20:23:53 +01001405 submit_flush_bio(ic, dio);
1406 else
1407 do_endio(ic, bio);
1408}
1409
1410static void dec_in_flight(struct dm_integrity_io *dio)
1411{
1412 if (atomic_dec_and_test(&dio->in_flight)) {
1413 struct dm_integrity_c *ic = dio->ic;
1414 struct bio *bio;
1415
1416 remove_range(ic, &dio->range);
1417
1418 if (unlikely(dio->write))
1419 schedule_autocommit(ic);
1420
1421 bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1422
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001423 if (unlikely(dio->bi_status) && !bio->bi_status)
1424 bio->bi_status = dio->bi_status;
1425 if (likely(!bio->bi_status) && unlikely(bio_sectors(bio) != dio->range.n_sectors)) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01001426 dio->range.logical_sector += dio->range.n_sectors;
1427 bio_advance(bio, dio->range.n_sectors << SECTOR_SHIFT);
1428 INIT_WORK(&dio->work, integrity_bio_wait);
1429 queue_work(ic->wait_wq, &dio->work);
1430 return;
1431 }
1432 do_endio_flush(ic, dio);
1433 }
1434}
1435
1436static void integrity_end_io(struct bio *bio)
1437{
1438 struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
1439
1440 bio->bi_iter = dio->orig_bi_iter;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001441 bio->bi_disk = dio->orig_bi_disk;
1442 bio->bi_partno = dio->orig_bi_partno;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001443 if (dio->orig_bi_integrity) {
1444 bio->bi_integrity = dio->orig_bi_integrity;
1445 bio->bi_opf |= REQ_INTEGRITY;
1446 }
1447 bio->bi_end_io = dio->orig_bi_end_io;
1448
1449 if (dio->completion)
1450 complete(dio->completion);
1451
1452 dec_in_flight(dio);
1453}
1454
1455static void integrity_sector_checksum(struct dm_integrity_c *ic, sector_t sector,
1456 const char *data, char *result)
1457{
1458 __u64 sector_le = cpu_to_le64(sector);
1459 SHASH_DESC_ON_STACK(req, ic->internal_hash);
1460 int r;
1461 unsigned digest_size;
1462
1463 req->tfm = ic->internal_hash;
1464 req->flags = 0;
1465
1466 r = crypto_shash_init(req);
1467 if (unlikely(r < 0)) {
1468 dm_integrity_io_error(ic, "crypto_shash_init", r);
1469 goto failed;
1470 }
1471
1472 r = crypto_shash_update(req, (const __u8 *)&sector_le, sizeof sector_le);
1473 if (unlikely(r < 0)) {
1474 dm_integrity_io_error(ic, "crypto_shash_update", r);
1475 goto failed;
1476 }
1477
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001478 r = crypto_shash_update(req, data, ic->sectors_per_block << SECTOR_SHIFT);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001479 if (unlikely(r < 0)) {
1480 dm_integrity_io_error(ic, "crypto_shash_update", r);
1481 goto failed;
1482 }
1483
1484 r = crypto_shash_final(req, result);
1485 if (unlikely(r < 0)) {
1486 dm_integrity_io_error(ic, "crypto_shash_final", r);
1487 goto failed;
1488 }
1489
1490 digest_size = crypto_shash_digestsize(ic->internal_hash);
1491 if (unlikely(digest_size < ic->tag_size))
1492 memset(result + digest_size, 0, ic->tag_size - digest_size);
1493
1494 return;
1495
1496failed:
1497 /* this shouldn't happen anyway, the hash functions have no reason to fail */
1498 get_random_bytes(result, ic->tag_size);
1499}
1500
1501static void integrity_metadata(struct work_struct *w)
1502{
1503 struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work);
1504 struct dm_integrity_c *ic = dio->ic;
1505
1506 int r;
1507
1508 if (ic->internal_hash) {
1509 struct bvec_iter iter;
1510 struct bio_vec bv;
1511 unsigned digest_size = crypto_shash_digestsize(ic->internal_hash);
1512 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1513 char *checksums;
Mikulas Patocka56b67a42017-04-18 16:51:50 -04001514 unsigned extra_space = unlikely(digest_size > ic->tag_size) ? digest_size - ic->tag_size : 0;
Kees Cook6d39a122018-08-07 14:18:39 -07001515 char checksums_onstack[HASH_MAX_DIGESTSIZE];
Mikulas Patocka7eada902017-01-04 20:23:53 +01001516 unsigned sectors_to_process = dio->range.n_sectors;
1517 sector_t sector = dio->range.logical_sector;
1518
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04001519 if (unlikely(ic->mode == 'R'))
1520 goto skip_io;
1521
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001522 checksums = kmalloc((PAGE_SIZE >> SECTOR_SHIFT >> ic->sb->log2_sectors_per_block) * ic->tag_size + extra_space,
Mikulas Patocka7eada902017-01-04 20:23:53 +01001523 GFP_NOIO | __GFP_NORETRY | __GFP_NOWARN);
Kees Cook6d39a122018-08-07 14:18:39 -07001524 if (!checksums) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01001525 checksums = checksums_onstack;
Kees Cook6d39a122018-08-07 14:18:39 -07001526 if (WARN_ON(extra_space &&
1527 digest_size > sizeof(checksums_onstack))) {
1528 r = -EINVAL;
1529 goto error;
1530 }
1531 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01001532
1533 __bio_for_each_segment(bv, bio, iter, dio->orig_bi_iter) {
1534 unsigned pos;
1535 char *mem, *checksums_ptr;
1536
1537again:
1538 mem = (char *)kmap_atomic(bv.bv_page) + bv.bv_offset;
1539 pos = 0;
1540 checksums_ptr = checksums;
1541 do {
1542 integrity_sector_checksum(ic, sector, mem + pos, checksums_ptr);
1543 checksums_ptr += ic->tag_size;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001544 sectors_to_process -= ic->sectors_per_block;
1545 pos += ic->sectors_per_block << SECTOR_SHIFT;
1546 sector += ic->sectors_per_block;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001547 } while (pos < bv.bv_len && sectors_to_process && checksums != checksums_onstack);
1548 kunmap_atomic(mem);
1549
1550 r = dm_integrity_rw_tag(ic, checksums, &dio->metadata_block, &dio->metadata_offset,
1551 checksums_ptr - checksums, !dio->write ? TAG_CMP : TAG_WRITE);
1552 if (unlikely(r)) {
1553 if (r > 0) {
Mikulas Patocka22555742019-03-06 08:29:34 -05001554 DMERR_LIMIT("Checksum failed at sector 0x%llx",
1555 (unsigned long long)(sector - ((r + ic->tag_size - 1) / ic->tag_size)));
Mikulas Patocka7eada902017-01-04 20:23:53 +01001556 r = -EILSEQ;
Mikulas Patocka3f2e5392017-07-21 12:00:00 -04001557 atomic64_inc(&ic->number_of_mismatches);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001558 }
1559 if (likely(checksums != checksums_onstack))
1560 kfree(checksums);
1561 goto error;
1562 }
1563
1564 if (!sectors_to_process)
1565 break;
1566
1567 if (unlikely(pos < bv.bv_len)) {
1568 bv.bv_offset += pos;
1569 bv.bv_len -= pos;
1570 goto again;
1571 }
1572 }
1573
1574 if (likely(checksums != checksums_onstack))
1575 kfree(checksums);
1576 } else {
1577 struct bio_integrity_payload *bip = dio->orig_bi_integrity;
1578
1579 if (bip) {
1580 struct bio_vec biv;
1581 struct bvec_iter iter;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001582 unsigned data_to_process = dio->range.n_sectors;
1583 sector_to_block(ic, data_to_process);
1584 data_to_process *= ic->tag_size;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001585
1586 bip_for_each_vec(biv, bip, iter) {
1587 unsigned char *tag;
1588 unsigned this_len;
1589
1590 BUG_ON(PageHighMem(biv.bv_page));
1591 tag = lowmem_page_address(biv.bv_page) + biv.bv_offset;
1592 this_len = min(biv.bv_len, data_to_process);
1593 r = dm_integrity_rw_tag(ic, tag, &dio->metadata_block, &dio->metadata_offset,
1594 this_len, !dio->write ? TAG_READ : TAG_WRITE);
1595 if (unlikely(r))
1596 goto error;
1597 data_to_process -= this_len;
1598 if (!data_to_process)
1599 break;
1600 }
1601 }
1602 }
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04001603skip_io:
Mikulas Patocka7eada902017-01-04 20:23:53 +01001604 dec_in_flight(dio);
1605 return;
1606error:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001607 dio->bi_status = errno_to_blk_status(r);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001608 dec_in_flight(dio);
1609}
1610
1611static int dm_integrity_map(struct dm_target *ti, struct bio *bio)
1612{
1613 struct dm_integrity_c *ic = ti->private;
1614 struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001615 struct bio_integrity_payload *bip;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001616
1617 sector_t area, offset;
1618
1619 dio->ic = ic;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001620 dio->bi_status = 0;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001621
1622 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1623 submit_flush_bio(ic, dio);
1624 return DM_MAPIO_SUBMITTED;
1625 }
1626
1627 dio->range.logical_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
1628 dio->write = bio_op(bio) == REQ_OP_WRITE;
1629 dio->fua = dio->write && bio->bi_opf & REQ_FUA;
1630 if (unlikely(dio->fua)) {
1631 /*
1632 * Don't pass down the FUA flag because we have to flush
1633 * disk cache anyway.
1634 */
1635 bio->bi_opf &= ~REQ_FUA;
1636 }
1637 if (unlikely(dio->range.logical_sector + bio_sectors(bio) > ic->provided_data_sectors)) {
1638 DMERR("Too big sector number: 0x%llx + 0x%x > 0x%llx",
1639 (unsigned long long)dio->range.logical_sector, bio_sectors(bio),
1640 (unsigned long long)ic->provided_data_sectors);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001641 return DM_MAPIO_KILL;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001642 }
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001643 if (unlikely((dio->range.logical_sector | bio_sectors(bio)) & (unsigned)(ic->sectors_per_block - 1))) {
1644 DMERR("Bio not aligned on %u sectors: 0x%llx, 0x%x",
1645 ic->sectors_per_block,
1646 (unsigned long long)dio->range.logical_sector, bio_sectors(bio));
Christoph Hellwig846785e2017-06-03 09:38:02 +02001647 return DM_MAPIO_KILL;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001648 }
1649
1650 if (ic->sectors_per_block > 1) {
1651 struct bvec_iter iter;
1652 struct bio_vec bv;
1653 bio_for_each_segment(bv, bio, iter) {
Mikulas Patocka95b13692017-11-07 10:40:40 -05001654 if (unlikely(bv.bv_len & ((ic->sectors_per_block << SECTOR_SHIFT) - 1))) {
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001655 DMERR("Bio vector (%u,%u) is not aligned on %u-sector boundary",
1656 bv.bv_offset, bv.bv_len, ic->sectors_per_block);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001657 return DM_MAPIO_KILL;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001658 }
1659 }
1660 }
1661
1662 bip = bio_integrity(bio);
1663 if (!ic->internal_hash) {
1664 if (bip) {
1665 unsigned wanted_tag_size = bio_sectors(bio) >> ic->sb->log2_sectors_per_block;
1666 if (ic->log2_tag_size >= 0)
1667 wanted_tag_size <<= ic->log2_tag_size;
1668 else
1669 wanted_tag_size *= ic->tag_size;
1670 if (unlikely(wanted_tag_size != bip->bip_iter.bi_size)) {
Mike Snitzer05d69092019-05-09 15:25:49 -04001671 DMERR("Invalid integrity data size %u, expected %u",
1672 bip->bip_iter.bi_size, wanted_tag_size);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001673 return DM_MAPIO_KILL;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001674 }
1675 }
1676 } else {
1677 if (unlikely(bip != NULL)) {
1678 DMERR("Unexpected integrity data when using internal hash");
Christoph Hellwig846785e2017-06-03 09:38:02 +02001679 return DM_MAPIO_KILL;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001680 }
1681 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01001682
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04001683 if (unlikely(ic->mode == 'R') && unlikely(dio->write))
Christoph Hellwig846785e2017-06-03 09:38:02 +02001684 return DM_MAPIO_KILL;
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04001685
Mikulas Patocka7eada902017-01-04 20:23:53 +01001686 get_area_and_offset(ic, dio->range.logical_sector, &area, &offset);
1687 dio->metadata_block = get_metadata_sector_and_offset(ic, area, offset, &dio->metadata_offset);
1688 bio->bi_iter.bi_sector = get_data_sector(ic, area, offset);
1689
1690 dm_integrity_map_continue(dio, true);
1691 return DM_MAPIO_SUBMITTED;
1692}
1693
1694static bool __journal_read_write(struct dm_integrity_io *dio, struct bio *bio,
1695 unsigned journal_section, unsigned journal_entry)
1696{
1697 struct dm_integrity_c *ic = dio->ic;
1698 sector_t logical_sector;
1699 unsigned n_sectors;
1700
1701 logical_sector = dio->range.logical_sector;
1702 n_sectors = dio->range.n_sectors;
1703 do {
1704 struct bio_vec bv = bio_iovec(bio);
1705 char *mem;
1706
1707 if (unlikely(bv.bv_len >> SECTOR_SHIFT > n_sectors))
1708 bv.bv_len = n_sectors << SECTOR_SHIFT;
1709 n_sectors -= bv.bv_len >> SECTOR_SHIFT;
1710 bio_advance_iter(bio, &bio->bi_iter, bv.bv_len);
1711retry_kmap:
1712 mem = kmap_atomic(bv.bv_page);
1713 if (likely(dio->write))
1714 flush_dcache_page(bv.bv_page);
1715
1716 do {
1717 struct journal_entry *je = access_journal_entry(ic, journal_section, journal_entry);
1718
1719 if (unlikely(!dio->write)) {
1720 struct journal_sector *js;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001721 char *mem_ptr;
1722 unsigned s;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001723
1724 if (unlikely(journal_entry_is_inprogress(je))) {
1725 flush_dcache_page(bv.bv_page);
1726 kunmap_atomic(mem);
1727
1728 __io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je));
1729 goto retry_kmap;
1730 }
1731 smp_rmb();
1732 BUG_ON(journal_entry_get_sector(je) != logical_sector);
1733 js = access_journal_data(ic, journal_section, journal_entry);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001734 mem_ptr = mem + bv.bv_offset;
1735 s = 0;
1736 do {
1737 memcpy(mem_ptr, js, JOURNAL_SECTOR_DATA);
1738 *(commit_id_t *)(mem_ptr + JOURNAL_SECTOR_DATA) = je->last_bytes[s];
1739 js++;
1740 mem_ptr += 1 << SECTOR_SHIFT;
1741 } while (++s < ic->sectors_per_block);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001742#ifdef INTERNAL_VERIFY
1743 if (ic->internal_hash) {
Kees Cook6d39a122018-08-07 14:18:39 -07001744 char checksums_onstack[max(HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)];
Mikulas Patocka7eada902017-01-04 20:23:53 +01001745
1746 integrity_sector_checksum(ic, logical_sector, mem + bv.bv_offset, checksums_onstack);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001747 if (unlikely(memcmp(checksums_onstack, journal_entry_tag(ic, je), ic->tag_size))) {
Mikulas Patocka22555742019-03-06 08:29:34 -05001748 DMERR_LIMIT("Checksum failed when reading from journal, at sector 0x%llx",
1749 (unsigned long long)logical_sector);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001750 }
1751 }
1752#endif
1753 }
1754
1755 if (!ic->internal_hash) {
1756 struct bio_integrity_payload *bip = bio_integrity(bio);
1757 unsigned tag_todo = ic->tag_size;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001758 char *tag_ptr = journal_entry_tag(ic, je);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001759
1760 if (bip) do {
1761 struct bio_vec biv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter);
1762 unsigned tag_now = min(biv.bv_len, tag_todo);
1763 char *tag_addr;
1764 BUG_ON(PageHighMem(biv.bv_page));
1765 tag_addr = lowmem_page_address(biv.bv_page) + biv.bv_offset;
1766 if (likely(dio->write))
1767 memcpy(tag_ptr, tag_addr, tag_now);
1768 else
1769 memcpy(tag_addr, tag_ptr, tag_now);
1770 bvec_iter_advance(bip->bip_vec, &bip->bip_iter, tag_now);
1771 tag_ptr += tag_now;
1772 tag_todo -= tag_now;
1773 } while (unlikely(tag_todo)); else {
1774 if (likely(dio->write))
1775 memset(tag_ptr, 0, tag_todo);
1776 }
1777 }
1778
1779 if (likely(dio->write)) {
1780 struct journal_sector *js;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001781 unsigned s;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001782
1783 js = access_journal_data(ic, journal_section, journal_entry);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001784 memcpy(js, mem + bv.bv_offset, ic->sectors_per_block << SECTOR_SHIFT);
1785
1786 s = 0;
1787 do {
1788 je->last_bytes[s] = js[s].commit_id;
1789 } while (++s < ic->sectors_per_block);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001790
1791 if (ic->internal_hash) {
1792 unsigned digest_size = crypto_shash_digestsize(ic->internal_hash);
1793 if (unlikely(digest_size > ic->tag_size)) {
Kees Cook6d39a122018-08-07 14:18:39 -07001794 char checksums_onstack[HASH_MAX_DIGESTSIZE];
Mikulas Patocka7eada902017-01-04 20:23:53 +01001795 integrity_sector_checksum(ic, logical_sector, (char *)js, checksums_onstack);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001796 memcpy(journal_entry_tag(ic, je), checksums_onstack, ic->tag_size);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001797 } else
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001798 integrity_sector_checksum(ic, logical_sector, (char *)js, journal_entry_tag(ic, je));
Mikulas Patocka7eada902017-01-04 20:23:53 +01001799 }
1800
1801 journal_entry_set_sector(je, logical_sector);
1802 }
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001803 logical_sector += ic->sectors_per_block;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001804
1805 journal_entry++;
1806 if (unlikely(journal_entry == ic->journal_section_entries)) {
1807 journal_entry = 0;
1808 journal_section++;
1809 wraparound_section(ic, &journal_section);
1810 }
1811
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001812 bv.bv_offset += ic->sectors_per_block << SECTOR_SHIFT;
1813 } while (bv.bv_len -= ic->sectors_per_block << SECTOR_SHIFT);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001814
1815 if (unlikely(!dio->write))
1816 flush_dcache_page(bv.bv_page);
1817 kunmap_atomic(mem);
1818 } while (n_sectors);
1819
1820 if (likely(dio->write)) {
1821 smp_mb();
1822 if (unlikely(waitqueue_active(&ic->copy_to_journal_wait)))
1823 wake_up(&ic->copy_to_journal_wait);
Mark Rutlandd3e632f2017-10-23 14:07:11 -07001824 if (READ_ONCE(ic->free_sectors) <= ic->free_sectors_threshold) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01001825 queue_work(ic->commit_wq, &ic->commit_work);
1826 } else {
1827 schedule_autocommit(ic);
1828 }
1829 } else {
1830 remove_range(ic, &dio->range);
1831 }
1832
1833 if (unlikely(bio->bi_iter.bi_size)) {
1834 sector_t area, offset;
1835
1836 dio->range.logical_sector = logical_sector;
1837 get_area_and_offset(ic, dio->range.logical_sector, &area, &offset);
1838 dio->metadata_block = get_metadata_sector_and_offset(ic, area, offset, &dio->metadata_offset);
1839 return true;
1840 }
1841
1842 return false;
1843}
1844
1845static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map)
1846{
1847 struct dm_integrity_c *ic = dio->ic;
1848 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1849 unsigned journal_section, journal_entry;
1850 unsigned journal_read_pos;
1851 struct completion read_comp;
1852 bool need_sync_io = ic->internal_hash && !dio->write;
1853
1854 if (need_sync_io && from_map) {
1855 INIT_WORK(&dio->work, integrity_bio_wait);
1856 queue_work(ic->metadata_wq, &dio->work);
1857 return;
1858 }
1859
1860lock_retry:
1861 spin_lock_irq(&ic->endio_wait.lock);
1862retry:
1863 if (unlikely(dm_integrity_failed(ic))) {
1864 spin_unlock_irq(&ic->endio_wait.lock);
1865 do_endio(ic, bio);
1866 return;
1867 }
1868 dio->range.n_sectors = bio_sectors(bio);
1869 journal_read_pos = NOT_FOUND;
1870 if (likely(ic->mode == 'J')) {
1871 if (dio->write) {
1872 unsigned next_entry, i, pos;
Mikulas Patocka9dd59722017-07-19 11:23:40 -04001873 unsigned ws, we, range_sectors;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001874
Mikulas Patocka9dd59722017-07-19 11:23:40 -04001875 dio->range.n_sectors = min(dio->range.n_sectors,
Mikulas Patocka4f434462019-04-29 14:57:21 +02001876 (sector_t)ic->free_sectors << ic->sb->log2_sectors_per_block);
Mikulas Patocka518748b2018-07-03 20:13:26 +02001877 if (unlikely(!dio->range.n_sectors)) {
1878 if (from_map)
1879 goto offload_to_thread;
1880 sleep_on_endio_wait(ic);
1881 goto retry;
1882 }
Mikulas Patocka9dd59722017-07-19 11:23:40 -04001883 range_sectors = dio->range.n_sectors >> ic->sb->log2_sectors_per_block;
1884 ic->free_sectors -= range_sectors;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001885 journal_section = ic->free_section;
1886 journal_entry = ic->free_section_entry;
1887
Mikulas Patocka9dd59722017-07-19 11:23:40 -04001888 next_entry = ic->free_section_entry + range_sectors;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001889 ic->free_section_entry = next_entry % ic->journal_section_entries;
1890 ic->free_section += next_entry / ic->journal_section_entries;
1891 ic->n_uncommitted_sections += next_entry / ic->journal_section_entries;
1892 wraparound_section(ic, &ic->free_section);
1893
1894 pos = journal_section * ic->journal_section_entries + journal_entry;
1895 ws = journal_section;
1896 we = journal_entry;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001897 i = 0;
1898 do {
Mikulas Patocka7eada902017-01-04 20:23:53 +01001899 struct journal_entry *je;
1900
1901 add_journal_node(ic, &ic->journal_tree[pos], dio->range.logical_sector + i);
1902 pos++;
1903 if (unlikely(pos >= ic->journal_entries))
1904 pos = 0;
1905
1906 je = access_journal_entry(ic, ws, we);
1907 BUG_ON(!journal_entry_is_unused(je));
1908 journal_entry_set_inprogress(je);
1909 we++;
1910 if (unlikely(we == ic->journal_section_entries)) {
1911 we = 0;
1912 ws++;
1913 wraparound_section(ic, &ws);
1914 }
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001915 } while ((i += ic->sectors_per_block) < dio->range.n_sectors);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001916
1917 spin_unlock_irq(&ic->endio_wait.lock);
1918 goto journal_read_write;
1919 } else {
1920 sector_t next_sector;
1921 journal_read_pos = find_journal_node(ic, dio->range.logical_sector, &next_sector);
1922 if (likely(journal_read_pos == NOT_FOUND)) {
1923 if (unlikely(dio->range.n_sectors > next_sector - dio->range.logical_sector))
1924 dio->range.n_sectors = next_sector - dio->range.logical_sector;
1925 } else {
1926 unsigned i;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001927 unsigned jp = journal_read_pos + 1;
1928 for (i = ic->sectors_per_block; i < dio->range.n_sectors; i += ic->sectors_per_block, jp++) {
1929 if (!test_journal_node(ic, jp, dio->range.logical_sector + i))
Mikulas Patocka7eada902017-01-04 20:23:53 +01001930 break;
1931 }
1932 dio->range.n_sectors = i;
1933 }
1934 }
1935 }
Mikulas Patocka724376a2018-07-03 20:13:27 +02001936 if (unlikely(!add_new_range(ic, &dio->range, true))) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01001937 /*
1938 * We must not sleep in the request routine because it could
1939 * stall bios on current->bio_list.
1940 * So, we offload the bio to a workqueue if we have to sleep.
1941 */
Mikulas Patocka7eada902017-01-04 20:23:53 +01001942 if (from_map) {
Mikulas Patocka518748b2018-07-03 20:13:26 +02001943offload_to_thread:
Mikulas Patocka7eada902017-01-04 20:23:53 +01001944 spin_unlock_irq(&ic->endio_wait.lock);
1945 INIT_WORK(&dio->work, integrity_bio_wait);
1946 queue_work(ic->wait_wq, &dio->work);
1947 return;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001948 }
Mikulas Patocka724376a2018-07-03 20:13:27 +02001949 wait_and_add_new_range(ic, &dio->range);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001950 }
1951 spin_unlock_irq(&ic->endio_wait.lock);
1952
1953 if (unlikely(journal_read_pos != NOT_FOUND)) {
1954 journal_section = journal_read_pos / ic->journal_section_entries;
1955 journal_entry = journal_read_pos % ic->journal_section_entries;
1956 goto journal_read_write;
1957 }
1958
Mikulas Patocka468dfca2019-04-29 14:57:24 +02001959 if (ic->mode == 'B' && dio->write) {
Mike Snitzer05d69092019-05-09 15:25:49 -04001960 if (!block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector,
1961 dio->range.n_sectors, BITMAP_OP_TEST_ALL_SET)) {
1962 struct bitmap_block_status *bbs;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02001963
Mike Snitzer05d69092019-05-09 15:25:49 -04001964 bbs = sector_to_bitmap_block(ic, dio->range.logical_sector);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02001965 spin_lock(&bbs->bio_queue_lock);
1966 bio_list_add(&bbs->bio_queue, bio);
1967 spin_unlock(&bbs->bio_queue_lock);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02001968 queue_work(ic->writer_wq, &bbs->work);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02001969 return;
1970 }
1971 }
1972
Mikulas Patocka7eada902017-01-04 20:23:53 +01001973 dio->in_flight = (atomic_t)ATOMIC_INIT(2);
1974
1975 if (need_sync_io) {
Arnd Bergmannb5e8ad92017-08-15 17:11:59 +02001976 init_completion(&read_comp);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001977 dio->completion = &read_comp;
1978 } else
1979 dio->completion = NULL;
1980
1981 dio->orig_bi_iter = bio->bi_iter;
1982
Christoph Hellwig74d46992017-08-23 19:10:32 +02001983 dio->orig_bi_disk = bio->bi_disk;
1984 dio->orig_bi_partno = bio->bi_partno;
1985 bio_set_dev(bio, ic->dev->bdev);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001986
1987 dio->orig_bi_integrity = bio_integrity(bio);
1988 bio->bi_integrity = NULL;
1989 bio->bi_opf &= ~REQ_INTEGRITY;
1990
1991 dio->orig_bi_end_io = bio->bi_end_io;
1992 bio->bi_end_io = integrity_end_io;
1993
1994 bio->bi_iter.bi_size = dio->range.n_sectors << SECTOR_SHIFT;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001995 generic_make_request(bio);
1996
1997 if (need_sync_io) {
1998 wait_for_completion_io(&read_comp);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02001999 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING) &&
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002000 dio->range.logical_sector + dio->range.n_sectors > le64_to_cpu(ic->sb->recalc_sector))
2001 goto skip_check;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002002 if (ic->mode == 'B') {
Mike Snitzer05d69092019-05-09 15:25:49 -04002003 if (!block_bitmap_op(ic, ic->recalc_bitmap, dio->range.logical_sector,
2004 dio->range.n_sectors, BITMAP_OP_TEST_ALL_CLEAR))
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002005 goto skip_check;
2006 }
2007
Hyunchul Leeb7e326f2017-07-31 16:22:20 +09002008 if (likely(!bio->bi_status))
2009 integrity_metadata(&dio->work);
2010 else
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002011skip_check:
Hyunchul Leeb7e326f2017-07-31 16:22:20 +09002012 dec_in_flight(dio);
2013
Mikulas Patocka7eada902017-01-04 20:23:53 +01002014 } else {
2015 INIT_WORK(&dio->work, integrity_metadata);
2016 queue_work(ic->metadata_wq, &dio->work);
2017 }
2018
2019 return;
2020
2021journal_read_write:
2022 if (unlikely(__journal_read_write(dio, bio, journal_section, journal_entry)))
2023 goto lock_retry;
2024
2025 do_endio_flush(ic, dio);
2026}
2027
2028
2029static void integrity_bio_wait(struct work_struct *w)
2030{
2031 struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work);
2032
2033 dm_integrity_map_continue(dio, false);
2034}
2035
2036static void pad_uncommitted(struct dm_integrity_c *ic)
2037{
2038 if (ic->free_section_entry) {
2039 ic->free_sectors -= ic->journal_section_entries - ic->free_section_entry;
2040 ic->free_section_entry = 0;
2041 ic->free_section++;
2042 wraparound_section(ic, &ic->free_section);
2043 ic->n_uncommitted_sections++;
2044 }
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002045 if (WARN_ON(ic->journal_sections * ic->journal_section_entries !=
Mike Snitzer05d69092019-05-09 15:25:49 -04002046 (ic->n_uncommitted_sections + ic->n_committed_sections) *
2047 ic->journal_section_entries + ic->free_sectors)) {
2048 DMCRIT("journal_sections %u, journal_section_entries %u, "
2049 "n_uncommitted_sections %u, n_committed_sections %u, "
2050 "journal_section_entries %u, free_sectors %u",
2051 ic->journal_sections, ic->journal_section_entries,
2052 ic->n_uncommitted_sections, ic->n_committed_sections,
2053 ic->journal_section_entries, ic->free_sectors);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002054 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01002055}
2056
2057static void integrity_commit(struct work_struct *w)
2058{
2059 struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, commit_work);
2060 unsigned commit_start, commit_sections;
2061 unsigned i, j, n;
2062 struct bio *flushes;
2063
2064 del_timer(&ic->autocommit_timer);
2065
2066 spin_lock_irq(&ic->endio_wait.lock);
2067 flushes = bio_list_get(&ic->flush_bio_list);
2068 if (unlikely(ic->mode != 'J')) {
2069 spin_unlock_irq(&ic->endio_wait.lock);
2070 dm_integrity_flush_buffers(ic);
2071 goto release_flush_bios;
2072 }
2073
2074 pad_uncommitted(ic);
2075 commit_start = ic->uncommitted_section;
2076 commit_sections = ic->n_uncommitted_sections;
2077 spin_unlock_irq(&ic->endio_wait.lock);
2078
2079 if (!commit_sections)
2080 goto release_flush_bios;
2081
2082 i = commit_start;
2083 for (n = 0; n < commit_sections; n++) {
2084 for (j = 0; j < ic->journal_section_entries; j++) {
2085 struct journal_entry *je;
2086 je = access_journal_entry(ic, i, j);
2087 io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je));
2088 }
2089 for (j = 0; j < ic->journal_section_sectors; j++) {
2090 struct journal_sector *js;
2091 js = access_journal(ic, i, j);
2092 js->commit_id = dm_integrity_commit_id(ic, i, j, ic->commit_seq);
2093 }
2094 i++;
2095 if (unlikely(i >= ic->journal_sections))
2096 ic->commit_seq = next_commit_seq(ic->commit_seq);
2097 wraparound_section(ic, &i);
2098 }
2099 smp_rmb();
2100
2101 write_journal(ic, commit_start, commit_sections);
2102
2103 spin_lock_irq(&ic->endio_wait.lock);
2104 ic->uncommitted_section += commit_sections;
2105 wraparound_section(ic, &ic->uncommitted_section);
2106 ic->n_uncommitted_sections -= commit_sections;
2107 ic->n_committed_sections += commit_sections;
2108 spin_unlock_irq(&ic->endio_wait.lock);
2109
Mark Rutlandd3e632f2017-10-23 14:07:11 -07002110 if (READ_ONCE(ic->free_sectors) <= ic->free_sectors_threshold)
Mikulas Patocka7eada902017-01-04 20:23:53 +01002111 queue_work(ic->writer_wq, &ic->writer_work);
2112
2113release_flush_bios:
2114 while (flushes) {
2115 struct bio *next = flushes->bi_next;
2116 flushes->bi_next = NULL;
2117 do_endio(ic, flushes);
2118 flushes = next;
2119 }
2120}
2121
2122static void complete_copy_from_journal(unsigned long error, void *context)
2123{
2124 struct journal_io *io = context;
2125 struct journal_completion *comp = io->comp;
2126 struct dm_integrity_c *ic = comp->ic;
2127 remove_range(ic, &io->range);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002128 mempool_free(io, &ic->journal_io_mempool);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002129 if (unlikely(error != 0))
2130 dm_integrity_io_error(ic, "copying from journal", -EIO);
2131 complete_journal_op(comp);
2132}
2133
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002134static void restore_last_bytes(struct dm_integrity_c *ic, struct journal_sector *js,
2135 struct journal_entry *je)
2136{
2137 unsigned s = 0;
2138 do {
2139 js->commit_id = je->last_bytes[s];
2140 js++;
2141 } while (++s < ic->sectors_per_block);
2142}
2143
Mikulas Patocka7eada902017-01-04 20:23:53 +01002144static void do_journal_write(struct dm_integrity_c *ic, unsigned write_start,
2145 unsigned write_sections, bool from_replay)
2146{
2147 unsigned i, j, n;
2148 struct journal_completion comp;
Mikulas Patockaa7c3e62b2017-07-19 11:24:08 -04002149 struct blk_plug plug;
2150
2151 blk_start_plug(&plug);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002152
2153 comp.ic = ic;
2154 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
Arnd Bergmannb5e8ad92017-08-15 17:11:59 +02002155 init_completion(&comp.comp);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002156
2157 i = write_start;
2158 for (n = 0; n < write_sections; n++, i++, wraparound_section(ic, &i)) {
2159#ifndef INTERNAL_VERIFY
2160 if (unlikely(from_replay))
2161#endif
2162 rw_section_mac(ic, i, false);
2163 for (j = 0; j < ic->journal_section_entries; j++) {
2164 struct journal_entry *je = access_journal_entry(ic, i, j);
2165 sector_t sec, area, offset;
2166 unsigned k, l, next_loop;
2167 sector_t metadata_block;
2168 unsigned metadata_offset;
2169 struct journal_io *io;
2170
2171 if (journal_entry_is_unused(je))
2172 continue;
2173 BUG_ON(unlikely(journal_entry_is_inprogress(je)) && !from_replay);
2174 sec = journal_entry_get_sector(je);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002175 if (unlikely(from_replay)) {
2176 if (unlikely(sec & (unsigned)(ic->sectors_per_block - 1))) {
2177 dm_integrity_io_error(ic, "invalid sector in journal", -EIO);
2178 sec &= ~(sector_t)(ic->sectors_per_block - 1);
2179 }
2180 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01002181 get_area_and_offset(ic, sec, &area, &offset);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002182 restore_last_bytes(ic, access_journal_data(ic, i, j), je);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002183 for (k = j + 1; k < ic->journal_section_entries; k++) {
2184 struct journal_entry *je2 = access_journal_entry(ic, i, k);
2185 sector_t sec2, area2, offset2;
2186 if (journal_entry_is_unused(je2))
2187 break;
2188 BUG_ON(unlikely(journal_entry_is_inprogress(je2)) && !from_replay);
2189 sec2 = journal_entry_get_sector(je2);
2190 get_area_and_offset(ic, sec2, &area2, &offset2);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002191 if (area2 != area || offset2 != offset + ((k - j) << ic->sb->log2_sectors_per_block))
Mikulas Patocka7eada902017-01-04 20:23:53 +01002192 break;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002193 restore_last_bytes(ic, access_journal_data(ic, i, k), je2);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002194 }
2195 next_loop = k - 1;
2196
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002197 io = mempool_alloc(&ic->journal_io_mempool, GFP_NOIO);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002198 io->comp = &comp;
2199 io->range.logical_sector = sec;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002200 io->range.n_sectors = (k - j) << ic->sb->log2_sectors_per_block;
Mikulas Patocka7eada902017-01-04 20:23:53 +01002201
2202 spin_lock_irq(&ic->endio_wait.lock);
Mikulas Patocka8b3bbd42019-04-29 14:57:22 +02002203 add_new_range_and_wait(ic, &io->range);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002204
2205 if (likely(!from_replay)) {
2206 struct journal_node *section_node = &ic->journal_tree[i * ic->journal_section_entries];
2207
2208 /* don't write if there is newer committed sector */
2209 while (j < k && find_newer_committed_node(ic, &section_node[j])) {
2210 struct journal_entry *je2 = access_journal_entry(ic, i, j);
2211
2212 journal_entry_set_unused(je2);
2213 remove_journal_node(ic, &section_node[j]);
2214 j++;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002215 sec += ic->sectors_per_block;
2216 offset += ic->sectors_per_block;
Mikulas Patocka7eada902017-01-04 20:23:53 +01002217 }
2218 while (j < k && find_newer_committed_node(ic, &section_node[k - 1])) {
2219 struct journal_entry *je2 = access_journal_entry(ic, i, k - 1);
2220
2221 journal_entry_set_unused(je2);
2222 remove_journal_node(ic, &section_node[k - 1]);
2223 k--;
2224 }
2225 if (j == k) {
2226 remove_range_unlocked(ic, &io->range);
2227 spin_unlock_irq(&ic->endio_wait.lock);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002228 mempool_free(io, &ic->journal_io_mempool);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002229 goto skip_io;
2230 }
2231 for (l = j; l < k; l++) {
2232 remove_journal_node(ic, &section_node[l]);
2233 }
2234 }
2235 spin_unlock_irq(&ic->endio_wait.lock);
2236
2237 metadata_block = get_metadata_sector_and_offset(ic, area, offset, &metadata_offset);
2238 for (l = j; l < k; l++) {
2239 int r;
2240 struct journal_entry *je2 = access_journal_entry(ic, i, l);
2241
2242 if (
2243#ifndef INTERNAL_VERIFY
2244 unlikely(from_replay) &&
2245#endif
2246 ic->internal_hash) {
Kees Cook6d39a122018-08-07 14:18:39 -07002247 char test_tag[max_t(size_t, HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)];
Mikulas Patocka7eada902017-01-04 20:23:53 +01002248
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002249 integrity_sector_checksum(ic, sec + ((l - j) << ic->sb->log2_sectors_per_block),
Mikulas Patocka7eada902017-01-04 20:23:53 +01002250 (char *)access_journal_data(ic, i, l), test_tag);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002251 if (unlikely(memcmp(test_tag, journal_entry_tag(ic, je2), ic->tag_size)))
Mikulas Patocka7eada902017-01-04 20:23:53 +01002252 dm_integrity_io_error(ic, "tag mismatch when replaying journal", -EILSEQ);
2253 }
2254
2255 journal_entry_set_unused(je2);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002256 r = dm_integrity_rw_tag(ic, journal_entry_tag(ic, je2), &metadata_block, &metadata_offset,
Mikulas Patocka7eada902017-01-04 20:23:53 +01002257 ic->tag_size, TAG_WRITE);
2258 if (unlikely(r)) {
2259 dm_integrity_io_error(ic, "reading tags", r);
2260 }
2261 }
2262
2263 atomic_inc(&comp.in_flight);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002264 copy_from_journal(ic, i, j << ic->sb->log2_sectors_per_block,
2265 (k - j) << ic->sb->log2_sectors_per_block,
2266 get_data_sector(ic, area, offset),
Mikulas Patocka7eada902017-01-04 20:23:53 +01002267 complete_copy_from_journal, io);
2268skip_io:
2269 j = next_loop;
2270 }
2271 }
2272
2273 dm_bufio_write_dirty_buffers_async(ic->bufio);
2274
Mikulas Patockaa7c3e62b2017-07-19 11:24:08 -04002275 blk_finish_plug(&plug);
2276
Mikulas Patocka7eada902017-01-04 20:23:53 +01002277 complete_journal_op(&comp);
2278 wait_for_completion_io(&comp.comp);
2279
2280 dm_integrity_flush_buffers(ic);
2281}
2282
2283static void integrity_writer(struct work_struct *w)
2284{
2285 struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, writer_work);
2286 unsigned write_start, write_sections;
2287
2288 unsigned prev_free_sectors;
2289
2290 /* the following test is not needed, but it tests the replay code */
Mikulas Patocka747829a2018-07-03 20:13:32 +02002291 if (READ_ONCE(ic->suspending) && !ic->meta_dev)
Mikulas Patocka7eada902017-01-04 20:23:53 +01002292 return;
2293
2294 spin_lock_irq(&ic->endio_wait.lock);
2295 write_start = ic->committed_section;
2296 write_sections = ic->n_committed_sections;
2297 spin_unlock_irq(&ic->endio_wait.lock);
2298
2299 if (!write_sections)
2300 return;
2301
2302 do_journal_write(ic, write_start, write_sections, false);
2303
2304 spin_lock_irq(&ic->endio_wait.lock);
2305
2306 ic->committed_section += write_sections;
2307 wraparound_section(ic, &ic->committed_section);
2308 ic->n_committed_sections -= write_sections;
2309
2310 prev_free_sectors = ic->free_sectors;
2311 ic->free_sectors += write_sections * ic->journal_section_entries;
2312 if (unlikely(!prev_free_sectors))
2313 wake_up_locked(&ic->endio_wait);
2314
2315 spin_unlock_irq(&ic->endio_wait.lock);
2316}
2317
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002318static void recalc_write_super(struct dm_integrity_c *ic)
2319{
2320 int r;
2321
2322 dm_integrity_flush_buffers(ic);
2323 if (dm_integrity_failed(ic))
2324 return;
2325
2326 sb_set_version(ic);
2327 r = sync_rw_sb(ic, REQ_OP_WRITE, 0);
2328 if (unlikely(r))
2329 dm_integrity_io_error(ic, "writing superblock", r);
2330}
2331
2332static void integrity_recalc(struct work_struct *w)
2333{
2334 struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, recalc_work);
2335 struct dm_integrity_range range;
2336 struct dm_io_request io_req;
2337 struct dm_io_region io_loc;
2338 sector_t area, offset;
2339 sector_t metadata_block;
2340 unsigned metadata_offset;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002341 sector_t logical_sector, n_sectors;
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002342 __u8 *t;
2343 unsigned i;
2344 int r;
2345 unsigned super_counter = 0;
2346
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002347 DEBUG_print("start recalculation... (position %llx)\n", le64_to_cpu(ic->sb->recalc_sector));
2348
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002349 spin_lock_irq(&ic->endio_wait.lock);
2350
2351next_chunk:
2352
2353 if (unlikely(READ_ONCE(ic->suspending)))
2354 goto unlock_ret;
2355
2356 range.logical_sector = le64_to_cpu(ic->sb->recalc_sector);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002357 if (unlikely(range.logical_sector >= ic->provided_data_sectors)) {
2358 if (ic->mode == 'B') {
2359 DEBUG_print("queue_delayed_work: bitmap_flush_work\n");
2360 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0);
2361 }
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002362 goto unlock_ret;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002363 }
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002364
2365 get_area_and_offset(ic, range.logical_sector, &area, &offset);
2366 range.n_sectors = min((sector_t)RECALC_SECTORS, ic->provided_data_sectors - range.logical_sector);
2367 if (!ic->meta_dev)
Mikulas Patocka4f434462019-04-29 14:57:21 +02002368 range.n_sectors = min(range.n_sectors, ((sector_t)1U << ic->sb->log2_interleave_sectors) - (unsigned)offset);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002369
Mikulas Patocka8b3bbd42019-04-29 14:57:22 +02002370 add_new_range_and_wait(ic, &range);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002371 spin_unlock_irq(&ic->endio_wait.lock);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002372 logical_sector = range.logical_sector;
2373 n_sectors = range.n_sectors;
2374
2375 if (ic->mode == 'B') {
2376 if (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector, n_sectors, BITMAP_OP_TEST_ALL_CLEAR)) {
2377 goto advance_and_next;
2378 }
Mike Snitzer05d69092019-05-09 15:25:49 -04002379 while (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector,
2380 ic->sectors_per_block, BITMAP_OP_TEST_ALL_CLEAR)) {
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002381 logical_sector += ic->sectors_per_block;
2382 n_sectors -= ic->sectors_per_block;
2383 cond_resched();
2384 }
Mike Snitzer05d69092019-05-09 15:25:49 -04002385 while (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector + n_sectors - ic->sectors_per_block,
2386 ic->sectors_per_block, BITMAP_OP_TEST_ALL_CLEAR)) {
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002387 n_sectors -= ic->sectors_per_block;
2388 cond_resched();
2389 }
2390 get_area_and_offset(ic, logical_sector, &area, &offset);
2391 }
2392
2393 DEBUG_print("recalculating: %lx, %lx\n", logical_sector, n_sectors);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002394
2395 if (unlikely(++super_counter == RECALC_WRITE_SUPER)) {
2396 recalc_write_super(ic);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002397 if (ic->mode == 'B') {
2398 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, ic->bitmap_flush_interval);
2399 }
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002400 super_counter = 0;
2401 }
2402
2403 if (unlikely(dm_integrity_failed(ic)))
2404 goto err;
2405
2406 io_req.bi_op = REQ_OP_READ;
2407 io_req.bi_op_flags = 0;
2408 io_req.mem.type = DM_IO_VMA;
2409 io_req.mem.ptr.addr = ic->recalc_buffer;
2410 io_req.notify.fn = NULL;
2411 io_req.client = ic->io;
2412 io_loc.bdev = ic->dev->bdev;
2413 io_loc.sector = get_data_sector(ic, area, offset);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002414 io_loc.count = n_sectors;
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002415
2416 r = dm_io(&io_req, 1, &io_loc, NULL);
2417 if (unlikely(r)) {
2418 dm_integrity_io_error(ic, "reading data", r);
2419 goto err;
2420 }
2421
2422 t = ic->recalc_tags;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002423 for (i = 0; i < n_sectors; i += ic->sectors_per_block) {
2424 integrity_sector_checksum(ic, logical_sector + i, ic->recalc_buffer + (i << SECTOR_SHIFT), t);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002425 t += ic->tag_size;
2426 }
2427
2428 metadata_block = get_metadata_sector_and_offset(ic, area, offset, &metadata_offset);
2429
2430 r = dm_integrity_rw_tag(ic, ic->recalc_tags, &metadata_block, &metadata_offset, t - ic->recalc_tags, TAG_WRITE);
2431 if (unlikely(r)) {
2432 dm_integrity_io_error(ic, "writing tags", r);
2433 goto err;
2434 }
2435
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002436advance_and_next:
2437 cond_resched();
2438
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002439 spin_lock_irq(&ic->endio_wait.lock);
2440 remove_range_unlocked(ic, &range);
2441 ic->sb->recalc_sector = cpu_to_le64(range.logical_sector + range.n_sectors);
2442 goto next_chunk;
2443
2444err:
2445 remove_range(ic, &range);
2446 return;
2447
2448unlock_ret:
2449 spin_unlock_irq(&ic->endio_wait.lock);
2450
2451 recalc_write_super(ic);
2452}
2453
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002454static void bitmap_block_work(struct work_struct *w)
2455{
2456 struct bitmap_block_status *bbs = container_of(w, struct bitmap_block_status, work);
2457 struct dm_integrity_c *ic = bbs->ic;
2458 struct bio *bio;
2459 struct bio_list bio_queue;
2460 struct bio_list waiting;
2461
2462 bio_list_init(&waiting);
2463
2464 spin_lock(&bbs->bio_queue_lock);
2465 bio_queue = bbs->bio_queue;
2466 bio_list_init(&bbs->bio_queue);
2467 spin_unlock(&bbs->bio_queue_lock);
2468
2469 while ((bio = bio_list_pop(&bio_queue))) {
2470 struct dm_integrity_io *dio;
2471
2472 dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
2473
Mike Snitzer05d69092019-05-09 15:25:49 -04002474 if (block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector,
2475 dio->range.n_sectors, BITMAP_OP_TEST_ALL_SET)) {
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002476 remove_range(ic, &dio->range);
2477 INIT_WORK(&dio->work, integrity_bio_wait);
2478 queue_work(ic->wait_wq, &dio->work);
2479 } else {
Mike Snitzer05d69092019-05-09 15:25:49 -04002480 block_bitmap_op(ic, ic->journal, dio->range.logical_sector,
2481 dio->range.n_sectors, BITMAP_OP_SET);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002482 bio_list_add(&waiting, bio);
2483 }
2484 }
2485
2486 if (bio_list_empty(&waiting))
2487 return;
2488
Mike Snitzer05d69092019-05-09 15:25:49 -04002489 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC,
2490 bbs->idx * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT),
2491 BITMAP_BLOCK_SIZE >> SECTOR_SHIFT, NULL);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002492
2493 while ((bio = bio_list_pop(&waiting))) {
2494 struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
2495
Mike Snitzer05d69092019-05-09 15:25:49 -04002496 block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector,
2497 dio->range.n_sectors, BITMAP_OP_SET);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002498
2499 remove_range(ic, &dio->range);
2500 INIT_WORK(&dio->work, integrity_bio_wait);
2501 queue_work(ic->wait_wq, &dio->work);
2502 }
2503
2504 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, ic->bitmap_flush_interval);
2505}
2506
2507static void bitmap_flush_work(struct work_struct *work)
2508{
2509 struct dm_integrity_c *ic = container_of(work, struct dm_integrity_c, bitmap_flush_work.work);
2510 struct dm_integrity_range range;
2511 unsigned long limit;
Mikulas Patocka48271492019-04-29 14:57:26 +02002512 struct bio *bio;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002513
2514 dm_integrity_flush_buffers(ic);
2515
2516 range.logical_sector = 0;
2517 range.n_sectors = ic->provided_data_sectors;
2518
2519 spin_lock_irq(&ic->endio_wait.lock);
2520 add_new_range_and_wait(ic, &range);
2521 spin_unlock_irq(&ic->endio_wait.lock);
2522
2523 dm_integrity_flush_buffers(ic);
2524 if (ic->meta_dev)
2525 blkdev_issue_flush(ic->dev->bdev, GFP_NOIO, NULL);
2526
2527 limit = ic->provided_data_sectors;
2528 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) {
2529 limit = le64_to_cpu(ic->sb->recalc_sector)
2530 >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit)
2531 << (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
2532 }
Mikulas Patocka48271492019-04-29 14:57:26 +02002533 /*DEBUG_print("zeroing journal\n");*/
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002534 block_bitmap_op(ic, ic->journal, 0, limit, BITMAP_OP_CLEAR);
2535 block_bitmap_op(ic, ic->may_write_bitmap, 0, limit, BITMAP_OP_CLEAR);
2536
Mike Snitzer05d69092019-05-09 15:25:49 -04002537 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, 0,
2538 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002539
Mikulas Patocka48271492019-04-29 14:57:26 +02002540 spin_lock_irq(&ic->endio_wait.lock);
2541 remove_range_unlocked(ic, &range);
2542 while (unlikely((bio = bio_list_pop(&ic->synchronous_bios)) != NULL)) {
2543 bio_endio(bio);
2544 spin_unlock_irq(&ic->endio_wait.lock);
2545 spin_lock_irq(&ic->endio_wait.lock);
2546 }
2547 spin_unlock_irq(&ic->endio_wait.lock);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002548}
2549
2550
Mikulas Patocka7eada902017-01-04 20:23:53 +01002551static void init_journal(struct dm_integrity_c *ic, unsigned start_section,
2552 unsigned n_sections, unsigned char commit_seq)
2553{
2554 unsigned i, j, n;
2555
2556 if (!n_sections)
2557 return;
2558
2559 for (n = 0; n < n_sections; n++) {
2560 i = start_section + n;
2561 wraparound_section(ic, &i);
2562 for (j = 0; j < ic->journal_section_sectors; j++) {
2563 struct journal_sector *js = access_journal(ic, i, j);
2564 memset(&js->entries, 0, JOURNAL_SECTOR_DATA);
2565 js->commit_id = dm_integrity_commit_id(ic, i, j, commit_seq);
2566 }
2567 for (j = 0; j < ic->journal_section_entries; j++) {
2568 struct journal_entry *je = access_journal_entry(ic, i, j);
2569 journal_entry_set_unused(je);
2570 }
2571 }
2572
2573 write_journal(ic, start_section, n_sections);
2574}
2575
2576static int find_commit_seq(struct dm_integrity_c *ic, unsigned i, unsigned j, commit_id_t id)
2577{
2578 unsigned char k;
2579 for (k = 0; k < N_COMMIT_IDS; k++) {
2580 if (dm_integrity_commit_id(ic, i, j, k) == id)
2581 return k;
2582 }
2583 dm_integrity_io_error(ic, "journal commit id", -EIO);
2584 return -EIO;
2585}
2586
2587static void replay_journal(struct dm_integrity_c *ic)
2588{
2589 unsigned i, j;
2590 bool used_commit_ids[N_COMMIT_IDS];
2591 unsigned max_commit_id_sections[N_COMMIT_IDS];
2592 unsigned write_start, write_sections;
2593 unsigned continue_section;
2594 bool journal_empty;
2595 unsigned char unused, last_used, want_commit_seq;
2596
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04002597 if (ic->mode == 'R')
2598 return;
2599
Mikulas Patocka7eada902017-01-04 20:23:53 +01002600 if (ic->journal_uptodate)
2601 return;
2602
2603 last_used = 0;
2604 write_start = 0;
2605
2606 if (!ic->just_formatted) {
2607 DEBUG_print("reading journal\n");
2608 rw_journal(ic, REQ_OP_READ, 0, 0, ic->journal_sections, NULL);
2609 if (ic->journal_io)
2610 DEBUG_bytes(lowmem_page_address(ic->journal_io[0].page), 64, "read journal");
2611 if (ic->journal_io) {
2612 struct journal_completion crypt_comp;
2613 crypt_comp.ic = ic;
Arnd Bergmannb5e8ad92017-08-15 17:11:59 +02002614 init_completion(&crypt_comp.comp);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002615 crypt_comp.in_flight = (atomic_t)ATOMIC_INIT(0);
2616 encrypt_journal(ic, false, 0, ic->journal_sections, &crypt_comp);
2617 wait_for_completion(&crypt_comp.comp);
2618 }
2619 DEBUG_bytes(lowmem_page_address(ic->journal[0].page), 64, "decrypted journal");
2620 }
2621
2622 if (dm_integrity_failed(ic))
2623 goto clear_journal;
2624
2625 journal_empty = true;
2626 memset(used_commit_ids, 0, sizeof used_commit_ids);
2627 memset(max_commit_id_sections, 0, sizeof max_commit_id_sections);
2628 for (i = 0; i < ic->journal_sections; i++) {
2629 for (j = 0; j < ic->journal_section_sectors; j++) {
2630 int k;
2631 struct journal_sector *js = access_journal(ic, i, j);
2632 k = find_commit_seq(ic, i, j, js->commit_id);
2633 if (k < 0)
2634 goto clear_journal;
2635 used_commit_ids[k] = true;
2636 max_commit_id_sections[k] = i;
2637 }
2638 if (journal_empty) {
2639 for (j = 0; j < ic->journal_section_entries; j++) {
2640 struct journal_entry *je = access_journal_entry(ic, i, j);
2641 if (!journal_entry_is_unused(je)) {
2642 journal_empty = false;
2643 break;
2644 }
2645 }
2646 }
2647 }
2648
2649 if (!used_commit_ids[N_COMMIT_IDS - 1]) {
2650 unused = N_COMMIT_IDS - 1;
2651 while (unused && !used_commit_ids[unused - 1])
2652 unused--;
2653 } else {
2654 for (unused = 0; unused < N_COMMIT_IDS; unused++)
2655 if (!used_commit_ids[unused])
2656 break;
2657 if (unused == N_COMMIT_IDS) {
2658 dm_integrity_io_error(ic, "journal commit ids", -EIO);
2659 goto clear_journal;
2660 }
2661 }
2662 DEBUG_print("first unused commit seq %d [%d,%d,%d,%d]\n",
2663 unused, used_commit_ids[0], used_commit_ids[1],
2664 used_commit_ids[2], used_commit_ids[3]);
2665
2666 last_used = prev_commit_seq(unused);
2667 want_commit_seq = prev_commit_seq(last_used);
2668
2669 if (!used_commit_ids[want_commit_seq] && used_commit_ids[prev_commit_seq(want_commit_seq)])
2670 journal_empty = true;
2671
2672 write_start = max_commit_id_sections[last_used] + 1;
2673 if (unlikely(write_start >= ic->journal_sections))
2674 want_commit_seq = next_commit_seq(want_commit_seq);
2675 wraparound_section(ic, &write_start);
2676
2677 i = write_start;
2678 for (write_sections = 0; write_sections < ic->journal_sections; write_sections++) {
2679 for (j = 0; j < ic->journal_section_sectors; j++) {
2680 struct journal_sector *js = access_journal(ic, i, j);
2681
2682 if (js->commit_id != dm_integrity_commit_id(ic, i, j, want_commit_seq)) {
2683 /*
2684 * This could be caused by crash during writing.
2685 * We won't replay the inconsistent part of the
2686 * journal.
2687 */
2688 DEBUG_print("commit id mismatch at position (%u, %u): %d != %d\n",
2689 i, j, find_commit_seq(ic, i, j, js->commit_id), want_commit_seq);
2690 goto brk;
2691 }
2692 }
2693 i++;
2694 if (unlikely(i >= ic->journal_sections))
2695 want_commit_seq = next_commit_seq(want_commit_seq);
2696 wraparound_section(ic, &i);
2697 }
2698brk:
2699
2700 if (!journal_empty) {
2701 DEBUG_print("replaying %u sections, starting at %u, commit seq %d\n",
2702 write_sections, write_start, want_commit_seq);
2703 do_journal_write(ic, write_start, write_sections, true);
2704 }
2705
2706 if (write_sections == ic->journal_sections && (ic->mode == 'J' || journal_empty)) {
2707 continue_section = write_start;
2708 ic->commit_seq = want_commit_seq;
2709 DEBUG_print("continuing from section %u, commit seq %d\n", write_start, ic->commit_seq);
2710 } else {
2711 unsigned s;
2712 unsigned char erase_seq;
2713clear_journal:
2714 DEBUG_print("clearing journal\n");
2715
2716 erase_seq = prev_commit_seq(prev_commit_seq(last_used));
2717 s = write_start;
2718 init_journal(ic, s, 1, erase_seq);
2719 s++;
2720 wraparound_section(ic, &s);
2721 if (ic->journal_sections >= 2) {
2722 init_journal(ic, s, ic->journal_sections - 2, erase_seq);
2723 s += ic->journal_sections - 2;
2724 wraparound_section(ic, &s);
2725 init_journal(ic, s, 1, erase_seq);
2726 }
2727
2728 continue_section = 0;
2729 ic->commit_seq = next_commit_seq(erase_seq);
2730 }
2731
2732 ic->committed_section = continue_section;
2733 ic->n_committed_sections = 0;
2734
2735 ic->uncommitted_section = continue_section;
2736 ic->n_uncommitted_sections = 0;
2737
2738 ic->free_section = continue_section;
2739 ic->free_section_entry = 0;
2740 ic->free_sectors = ic->journal_entries;
2741
2742 ic->journal_tree_root = RB_ROOT;
2743 for (i = 0; i < ic->journal_entries; i++)
2744 init_journal_node(&ic->journal_tree[i]);
2745}
2746
Mikulas Patocka48271492019-04-29 14:57:26 +02002747static void dm_integrity_enter_synchronous_mode(struct dm_integrity_c *ic)
Mikulas Patocka1f5a7752019-04-29 14:57:25 +02002748{
Mikulas Patocka48271492019-04-29 14:57:26 +02002749 DEBUG_print("dm_integrity_enter_synchronous_mode\n");
Mikulas Patocka1f5a7752019-04-29 14:57:25 +02002750
2751 if (ic->mode == 'B') {
Mikulas Patocka48271492019-04-29 14:57:26 +02002752 ic->bitmap_flush_interval = msecs_to_jiffies(10) + 1;
2753 ic->synchronous_mode = 1;
2754
Mikulas Patocka1f5a7752019-04-29 14:57:25 +02002755 cancel_delayed_work_sync(&ic->bitmap_flush_work);
2756 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0);
2757 flush_workqueue(ic->commit_wq);
2758 }
Mikulas Patocka48271492019-04-29 14:57:26 +02002759}
2760
2761static int dm_integrity_reboot(struct notifier_block *n, unsigned long code, void *x)
2762{
2763 struct dm_integrity_c *ic = container_of(n, struct dm_integrity_c, reboot_notifier);
2764
2765 DEBUG_print("dm_integrity_reboot\n");
2766
2767 dm_integrity_enter_synchronous_mode(ic);
Mikulas Patocka1f5a7752019-04-29 14:57:25 +02002768
2769 return NOTIFY_DONE;
2770}
2771
Mikulas Patocka7eada902017-01-04 20:23:53 +01002772static void dm_integrity_postsuspend(struct dm_target *ti)
2773{
2774 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002775 int r;
Mikulas Patocka7eada902017-01-04 20:23:53 +01002776
Mikulas Patocka1f5a7752019-04-29 14:57:25 +02002777 WARN_ON(unregister_reboot_notifier(&ic->reboot_notifier));
2778
Mikulas Patocka7eada902017-01-04 20:23:53 +01002779 del_timer_sync(&ic->autocommit_timer);
2780
Mikulas Patockac21b1632018-07-03 20:13:25 +02002781 WRITE_ONCE(ic->suspending, 1);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002782
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002783 if (ic->recalc_wq)
2784 drain_workqueue(ic->recalc_wq);
2785
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002786 if (ic->mode == 'B')
2787 cancel_delayed_work_sync(&ic->bitmap_flush_work);
2788
Mikulas Patocka7eada902017-01-04 20:23:53 +01002789 queue_work(ic->commit_wq, &ic->commit_work);
2790 drain_workqueue(ic->commit_wq);
2791
2792 if (ic->mode == 'J') {
Mikulas Patocka747829a2018-07-03 20:13:32 +02002793 if (ic->meta_dev)
2794 queue_work(ic->writer_wq, &ic->writer_work);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002795 drain_workqueue(ic->writer_wq);
2796 dm_integrity_flush_buffers(ic);
2797 }
2798
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002799 if (ic->mode == 'B') {
2800 dm_integrity_flush_buffers(ic);
2801#if 1
Mike Snitzer05d69092019-05-09 15:25:49 -04002802 /* set to 0 to test bitmap replay code */
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002803 init_journal(ic, 0, ic->journal_sections, 0);
2804 ic->sb->flags &= ~cpu_to_le32(SB_FLAG_DIRTY_BITMAP);
2805 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
2806 if (unlikely(r))
2807 dm_integrity_io_error(ic, "writing superblock", r);
2808#endif
2809 }
2810
Mikulas Patockac21b1632018-07-03 20:13:25 +02002811 WRITE_ONCE(ic->suspending, 0);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002812
2813 BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress));
2814
2815 ic->journal_uptodate = true;
2816}
2817
2818static void dm_integrity_resume(struct dm_target *ti)
2819{
2820 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002821 int r;
2822 DEBUG_print("resume\n");
Mikulas Patocka7eada902017-01-04 20:23:53 +01002823
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002824 if (ic->sb->flags & cpu_to_le32(SB_FLAG_DIRTY_BITMAP)) {
2825 DEBUG_print("resume dirty_bitmap\n");
Mike Snitzer05d69092019-05-09 15:25:49 -04002826 rw_journal_sectors(ic, REQ_OP_READ, 0, 0,
2827 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002828 if (ic->mode == 'B') {
2829 if (ic->sb->log2_blocks_per_bitmap_bit == ic->log2_blocks_per_bitmap_bit) {
2830 block_bitmap_copy(ic, ic->recalc_bitmap, ic->journal);
2831 block_bitmap_copy(ic, ic->may_write_bitmap, ic->journal);
Mike Snitzer05d69092019-05-09 15:25:49 -04002832 if (!block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors,
2833 BITMAP_OP_TEST_ALL_CLEAR)) {
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002834 ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
2835 ic->sb->recalc_sector = cpu_to_le64(0);
2836 }
2837 } else {
Mike Snitzer05d69092019-05-09 15:25:49 -04002838 DEBUG_print("non-matching blocks_per_bitmap_bit: %u, %u\n",
2839 ic->sb->log2_blocks_per_bitmap_bit, ic->log2_blocks_per_bitmap_bit);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002840 ic->sb->log2_blocks_per_bitmap_bit = ic->log2_blocks_per_bitmap_bit;
2841 block_bitmap_op(ic, ic->recalc_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_SET);
2842 block_bitmap_op(ic, ic->may_write_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_SET);
2843 block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_SET);
Mike Snitzer05d69092019-05-09 15:25:49 -04002844 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, 0,
2845 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002846 ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
2847 ic->sb->recalc_sector = cpu_to_le64(0);
2848 }
2849 } else {
2850 if (!(ic->sb->log2_blocks_per_bitmap_bit == ic->log2_blocks_per_bitmap_bit &&
2851 block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_TEST_ALL_CLEAR))) {
2852 ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
2853 ic->sb->recalc_sector = cpu_to_le64(0);
2854 }
2855 init_journal(ic, 0, ic->journal_sections, 0);
2856 replay_journal(ic);
2857 ic->sb->flags &= ~cpu_to_le32(SB_FLAG_DIRTY_BITMAP);
2858 }
2859 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
2860 if (unlikely(r))
2861 dm_integrity_io_error(ic, "writing superblock", r);
2862 } else {
2863 replay_journal(ic);
2864 if (ic->mode == 'B') {
2865 int mode;
2866 ic->sb->flags |= cpu_to_le32(SB_FLAG_DIRTY_BITMAP);
2867 ic->sb->log2_blocks_per_bitmap_bit = ic->log2_blocks_per_bitmap_bit;
2868 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
2869 if (unlikely(r))
2870 dm_integrity_io_error(ic, "writing superblock", r);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002871
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002872 mode = ic->recalculate_flag ? BITMAP_OP_SET : BITMAP_OP_CLEAR;
2873 block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, mode);
2874 block_bitmap_op(ic, ic->recalc_bitmap, 0, ic->provided_data_sectors, mode);
2875 block_bitmap_op(ic, ic->may_write_bitmap, 0, ic->provided_data_sectors, mode);
Mike Snitzer05d69092019-05-09 15:25:49 -04002876 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, 0,
2877 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002878 }
2879 }
2880
2881 DEBUG_print("testing recalc: %x\n", ic->sb->flags);
2882 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) {
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002883 __u64 recalc_pos = le64_to_cpu(ic->sb->recalc_sector);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002884 DEBUG_print("recalc pos: %lx / %lx\n", (long)recalc_pos, ic->provided_data_sectors);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002885 if (recalc_pos < ic->provided_data_sectors) {
2886 queue_work(ic->recalc_wq, &ic->recalc_work);
2887 } else if (recalc_pos > ic->provided_data_sectors) {
2888 ic->sb->recalc_sector = cpu_to_le64(ic->provided_data_sectors);
2889 recalc_write_super(ic);
2890 }
2891 }
Mikulas Patocka1f5a7752019-04-29 14:57:25 +02002892
2893 ic->reboot_notifier.notifier_call = dm_integrity_reboot;
2894 ic->reboot_notifier.next = NULL;
2895 ic->reboot_notifier.priority = INT_MAX - 1; /* be notified after md and before hardware drivers */
2896 WARN_ON(register_reboot_notifier(&ic->reboot_notifier));
Mikulas Patocka48271492019-04-29 14:57:26 +02002897
2898#if 0
Mike Snitzer05d69092019-05-09 15:25:49 -04002899 /* set to 1 to stress test synchronous mode */
Mikulas Patocka48271492019-04-29 14:57:26 +02002900 dm_integrity_enter_synchronous_mode(ic);
2901#endif
Mikulas Patocka7eada902017-01-04 20:23:53 +01002902}
2903
2904static void dm_integrity_status(struct dm_target *ti, status_type_t type,
2905 unsigned status_flags, char *result, unsigned maxlen)
2906{
2907 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
2908 unsigned arg_count;
2909 size_t sz = 0;
2910
2911 switch (type) {
2912 case STATUSTYPE_INFO:
Mikulas Patockaf84fd2c2018-07-03 20:13:28 +02002913 DMEMIT("%llu %llu",
2914 (unsigned long long)atomic64_read(&ic->number_of_mismatches),
2915 (unsigned long long)ic->provided_data_sectors);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002916 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))
2917 DMEMIT(" %llu", (unsigned long long)le64_to_cpu(ic->sb->recalc_sector));
2918 else
2919 DMEMIT(" -");
Mikulas Patocka7eada902017-01-04 20:23:53 +01002920 break;
2921
2922 case STATUSTYPE_TABLE: {
2923 __u64 watermark_percentage = (__u64)(ic->journal_entries - ic->free_sectors_threshold) * 100;
2924 watermark_percentage += ic->journal_entries / 2;
2925 do_div(watermark_percentage, ic->journal_entries);
Mikulas Patocka893e3c32019-04-29 14:57:18 +02002926 arg_count = 3;
Mikulas Patocka356d9d52018-07-03 20:13:30 +02002927 arg_count += !!ic->meta_dev;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002928 arg_count += ic->sectors_per_block != 1;
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002929 arg_count += !!(ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING));
Mikulas Patocka893e3c32019-04-29 14:57:18 +02002930 arg_count += ic->mode == 'J';
2931 arg_count += ic->mode == 'J';
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002932 arg_count += ic->mode == 'B';
2933 arg_count += ic->mode == 'B';
Mikulas Patocka7eada902017-01-04 20:23:53 +01002934 arg_count += !!ic->internal_hash_alg.alg_string;
2935 arg_count += !!ic->journal_crypt_alg.alg_string;
2936 arg_count += !!ic->journal_mac_alg.alg_string;
2937 DMEMIT("%s %llu %u %c %u", ic->dev->name, (unsigned long long)ic->start,
2938 ic->tag_size, ic->mode, arg_count);
Mikulas Patocka356d9d52018-07-03 20:13:30 +02002939 if (ic->meta_dev)
2940 DMEMIT(" meta_device:%s", ic->meta_dev->name);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002941 if (ic->sectors_per_block != 1)
2942 DMEMIT(" block_size:%u", ic->sectors_per_block << SECTOR_SHIFT);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002943 if (ic->recalculate_flag)
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002944 DMEMIT(" recalculate");
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002945 DMEMIT(" journal_sectors:%u", ic->initial_sectors - SB_SECTORS);
2946 DMEMIT(" interleave_sectors:%u", 1U << ic->sb->log2_interleave_sectors);
2947 DMEMIT(" buffer_sectors:%u", 1U << ic->log2_buffer_sectors);
Mikulas Patocka893e3c32019-04-29 14:57:18 +02002948 if (ic->mode == 'J') {
2949 DMEMIT(" journal_watermark:%u", (unsigned)watermark_percentage);
2950 DMEMIT(" commit_time:%u", ic->autocommit_msec);
2951 }
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002952 if (ic->mode == 'B') {
2953 DMEMIT(" sectors_per_bit:%llu", (unsigned long long)ic->sectors_per_block << ic->log2_blocks_per_bitmap_bit);
2954 DMEMIT(" bitmap_flush_interval:%u", jiffies_to_msecs(ic->bitmap_flush_interval));
2955 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01002956
2957#define EMIT_ALG(a, n) \
2958 do { \
2959 if (ic->a.alg_string) { \
2960 DMEMIT(" %s:%s", n, ic->a.alg_string); \
2961 if (ic->a.key_string) \
2962 DMEMIT(":%s", ic->a.key_string);\
2963 } \
2964 } while (0)
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002965 EMIT_ALG(internal_hash_alg, "internal_hash");
2966 EMIT_ALG(journal_crypt_alg, "journal_crypt");
2967 EMIT_ALG(journal_mac_alg, "journal_mac");
Mikulas Patocka7eada902017-01-04 20:23:53 +01002968 break;
2969 }
2970 }
2971}
2972
2973static int dm_integrity_iterate_devices(struct dm_target *ti,
2974 iterate_devices_callout_fn fn, void *data)
2975{
2976 struct dm_integrity_c *ic = ti->private;
2977
Mikulas Patocka356d9d52018-07-03 20:13:30 +02002978 if (!ic->meta_dev)
2979 return fn(ti, ic->dev, ic->start + ic->initial_sectors + ic->metadata_run, ti->len, data);
2980 else
2981 return fn(ti, ic->dev, 0, ti->len, data);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002982}
2983
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002984static void dm_integrity_io_hints(struct dm_target *ti, struct queue_limits *limits)
2985{
2986 struct dm_integrity_c *ic = ti->private;
2987
2988 if (ic->sectors_per_block > 1) {
2989 limits->logical_block_size = ic->sectors_per_block << SECTOR_SHIFT;
2990 limits->physical_block_size = ic->sectors_per_block << SECTOR_SHIFT;
2991 blk_limits_io_min(limits, ic->sectors_per_block << SECTOR_SHIFT);
2992 }
2993}
2994
Mikulas Patocka7eada902017-01-04 20:23:53 +01002995static void calculate_journal_section_size(struct dm_integrity_c *ic)
2996{
2997 unsigned sector_space = JOURNAL_SECTOR_DATA;
2998
2999 ic->journal_sections = le32_to_cpu(ic->sb->journal_sections);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04003000 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 +01003001 JOURNAL_ENTRY_ROUNDUP);
3002
3003 if (ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC))
3004 sector_space -= JOURNAL_MAC_PER_SECTOR;
3005 ic->journal_entries_per_sector = sector_space / ic->journal_entry_size;
3006 ic->journal_section_entries = ic->journal_entries_per_sector * JOURNAL_BLOCK_SECTORS;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04003007 ic->journal_section_sectors = (ic->journal_section_entries << ic->sb->log2_sectors_per_block) + JOURNAL_BLOCK_SECTORS;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003008 ic->journal_entries = ic->journal_section_entries * ic->journal_sections;
3009}
3010
3011static int calculate_device_limits(struct dm_integrity_c *ic)
3012{
3013 __u64 initial_sectors;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003014
3015 calculate_journal_section_size(ic);
3016 initial_sectors = SB_SECTORS + (__u64)ic->journal_section_sectors * ic->journal_sections;
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003017 if (initial_sectors + METADATA_PADDING_SECTORS >= ic->meta_device_sectors || initial_sectors > UINT_MAX)
Mikulas Patocka7eada902017-01-04 20:23:53 +01003018 return -EINVAL;
3019 ic->initial_sectors = initial_sectors;
3020
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003021 if (!ic->meta_dev) {
3022 sector_t last_sector, last_area, last_offset;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003023
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003024 ic->metadata_run = roundup((__u64)ic->tag_size << (ic->sb->log2_interleave_sectors - ic->sb->log2_sectors_per_block),
3025 (__u64)(1 << SECTOR_SHIFT << METADATA_PADDING_SECTORS)) >> SECTOR_SHIFT;
3026 if (!(ic->metadata_run & (ic->metadata_run - 1)))
3027 ic->log2_metadata_run = __ffs(ic->metadata_run);
3028 else
3029 ic->log2_metadata_run = -1;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003030
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003031 get_area_and_offset(ic, ic->provided_data_sectors - 1, &last_area, &last_offset);
3032 last_sector = get_data_sector(ic, last_area, last_offset);
3033 if (last_sector < ic->start || last_sector >= ic->meta_device_sectors)
3034 return -EINVAL;
3035 } else {
Mikulas Patocka30bba432019-05-07 14:28:35 -04003036 __u64 meta_size = (ic->provided_data_sectors >> ic->sb->log2_sectors_per_block) * ic->tag_size;
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003037 meta_size = (meta_size + ((1U << (ic->log2_buffer_sectors + SECTOR_SHIFT)) - 1))
3038 >> (ic->log2_buffer_sectors + SECTOR_SHIFT);
3039 meta_size <<= ic->log2_buffer_sectors;
3040 if (ic->initial_sectors + meta_size < ic->initial_sectors ||
3041 ic->initial_sectors + meta_size > ic->meta_device_sectors)
3042 return -EINVAL;
3043 ic->metadata_run = 1;
3044 ic->log2_metadata_run = 0;
3045 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01003046
3047 return 0;
3048}
3049
3050static int initialize_superblock(struct dm_integrity_c *ic, unsigned journal_sectors, unsigned interleave_sectors)
3051{
3052 unsigned journal_sections;
3053 int test_bit;
3054
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003055 memset(ic->sb, 0, SB_SECTORS << SECTOR_SHIFT);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003056 memcpy(ic->sb->magic, SB_MAGIC, 8);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003057 ic->sb->integrity_tag_size = cpu_to_le16(ic->tag_size);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04003058 ic->sb->log2_sectors_per_block = __ffs(ic->sectors_per_block);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003059 if (ic->journal_mac_alg.alg_string)
3060 ic->sb->flags |= cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC);
3061
3062 calculate_journal_section_size(ic);
3063 journal_sections = journal_sectors / ic->journal_section_sectors;
3064 if (!journal_sections)
3065 journal_sections = 1;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003066
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003067 if (!ic->meta_dev) {
3068 ic->sb->journal_sections = cpu_to_le32(journal_sections);
3069 if (!interleave_sectors)
3070 interleave_sectors = DEFAULT_INTERLEAVE_SECTORS;
3071 ic->sb->log2_interleave_sectors = __fls(interleave_sectors);
3072 ic->sb->log2_interleave_sectors = max((__u8)MIN_LOG2_INTERLEAVE_SECTORS, ic->sb->log2_interleave_sectors);
3073 ic->sb->log2_interleave_sectors = min((__u8)MAX_LOG2_INTERLEAVE_SECTORS, ic->sb->log2_interleave_sectors);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003074
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003075 ic->provided_data_sectors = 0;
3076 for (test_bit = fls64(ic->meta_device_sectors) - 1; test_bit >= 3; test_bit--) {
3077 __u64 prev_data_sectors = ic->provided_data_sectors;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003078
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003079 ic->provided_data_sectors |= (sector_t)1 << test_bit;
3080 if (calculate_device_limits(ic))
3081 ic->provided_data_sectors = prev_data_sectors;
3082 }
3083 if (!ic->provided_data_sectors)
3084 return -EINVAL;
3085 } else {
3086 ic->sb->log2_interleave_sectors = 0;
3087 ic->provided_data_sectors = ic->data_device_sectors;
3088 ic->provided_data_sectors &= ~(sector_t)(ic->sectors_per_block - 1);
3089
3090try_smaller_buffer:
3091 ic->sb->journal_sections = cpu_to_le32(0);
3092 for (test_bit = fls(journal_sections) - 1; test_bit >= 0; test_bit--) {
3093 __u32 prev_journal_sections = le32_to_cpu(ic->sb->journal_sections);
3094 __u32 test_journal_sections = prev_journal_sections | (1U << test_bit);
3095 if (test_journal_sections > journal_sections)
3096 continue;
3097 ic->sb->journal_sections = cpu_to_le32(test_journal_sections);
3098 if (calculate_device_limits(ic))
3099 ic->sb->journal_sections = cpu_to_le32(prev_journal_sections);
3100
3101 }
3102 if (!le32_to_cpu(ic->sb->journal_sections)) {
3103 if (ic->log2_buffer_sectors > 3) {
3104 ic->log2_buffer_sectors--;
3105 goto try_smaller_buffer;
3106 }
3107 return -EINVAL;
3108 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01003109 }
3110
Mikulas Patocka7eada902017-01-04 20:23:53 +01003111 ic->sb->provided_data_sectors = cpu_to_le64(ic->provided_data_sectors);
3112
Mikulas Patocka1f9fc0b2018-07-03 20:13:31 +02003113 sb_set_version(ic);
3114
Mikulas Patocka7eada902017-01-04 20:23:53 +01003115 return 0;
3116}
3117
3118static void dm_integrity_set(struct dm_target *ti, struct dm_integrity_c *ic)
3119{
3120 struct gendisk *disk = dm_disk(dm_table_get_md(ti->table));
3121 struct blk_integrity bi;
3122
3123 memset(&bi, 0, sizeof(bi));
3124 bi.profile = &dm_integrity_profile;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04003125 bi.tuple_size = ic->tag_size;
3126 bi.tag_size = bi.tuple_size;
Mikulas Patocka84ff1bc2017-04-26 18:39:47 -04003127 bi.interval_exp = ic->sb->log2_sectors_per_block + SECTOR_SHIFT;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003128
3129 blk_integrity_register(disk, &bi);
3130 blk_queue_max_integrity_segments(disk->queue, UINT_MAX);
3131}
3132
Mikulas Patockad5027e02019-04-29 14:57:20 +02003133static void dm_integrity_free_page_list(struct page_list *pl)
Mikulas Patocka7eada902017-01-04 20:23:53 +01003134{
3135 unsigned i;
3136
3137 if (!pl)
3138 return;
Mikulas Patockad5027e02019-04-29 14:57:20 +02003139 for (i = 0; pl[i].page; i++)
3140 __free_page(pl[i].page);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003141 kvfree(pl);
3142}
3143
Mikulas Patockad5027e02019-04-29 14:57:20 +02003144static struct page_list *dm_integrity_alloc_page_list(unsigned n_pages)
Mikulas Patocka7eada902017-01-04 20:23:53 +01003145{
Mikulas Patocka7eada902017-01-04 20:23:53 +01003146 struct page_list *pl;
3147 unsigned i;
3148
Mikulas Patockad5027e02019-04-29 14:57:20 +02003149 pl = kvmalloc_array(n_pages + 1, sizeof(struct page_list), GFP_KERNEL | __GFP_ZERO);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003150 if (!pl)
3151 return NULL;
3152
Mikulas Patockad5027e02019-04-29 14:57:20 +02003153 for (i = 0; i < n_pages; i++) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01003154 pl[i].page = alloc_page(GFP_KERNEL);
3155 if (!pl[i].page) {
Mikulas Patockad5027e02019-04-29 14:57:20 +02003156 dm_integrity_free_page_list(pl);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003157 return NULL;
3158 }
3159 if (i)
3160 pl[i - 1].next = &pl[i];
3161 }
Mikulas Patockad5027e02019-04-29 14:57:20 +02003162 pl[i].page = NULL;
3163 pl[i].next = NULL;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003164
3165 return pl;
3166}
3167
3168static void dm_integrity_free_journal_scatterlist(struct dm_integrity_c *ic, struct scatterlist **sl)
3169{
3170 unsigned i;
3171 for (i = 0; i < ic->journal_sections; i++)
3172 kvfree(sl[i]);
Mikulas Patockafc8cec12018-04-17 18:32:26 -04003173 kvfree(sl);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003174}
3175
Mike Snitzer05d69092019-05-09 15:25:49 -04003176static struct scatterlist **dm_integrity_alloc_journal_scatterlist(struct dm_integrity_c *ic,
3177 struct page_list *pl)
Mikulas Patocka7eada902017-01-04 20:23:53 +01003178{
3179 struct scatterlist **sl;
3180 unsigned i;
3181
Kees Cook344476e2018-06-12 14:04:32 -07003182 sl = kvmalloc_array(ic->journal_sections,
3183 sizeof(struct scatterlist *),
3184 GFP_KERNEL | __GFP_ZERO);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003185 if (!sl)
3186 return NULL;
3187
3188 for (i = 0; i < ic->journal_sections; i++) {
3189 struct scatterlist *s;
3190 unsigned start_index, start_offset;
3191 unsigned end_index, end_offset;
3192 unsigned n_pages;
3193 unsigned idx;
3194
3195 page_list_location(ic, i, 0, &start_index, &start_offset);
Mike Snitzer05d69092019-05-09 15:25:49 -04003196 page_list_location(ic, i, ic->journal_section_sectors - 1,
3197 &end_index, &end_offset);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003198
3199 n_pages = (end_index - start_index + 1);
3200
Kees Cook344476e2018-06-12 14:04:32 -07003201 s = kvmalloc_array(n_pages, sizeof(struct scatterlist),
3202 GFP_KERNEL);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003203 if (!s) {
3204 dm_integrity_free_journal_scatterlist(ic, sl);
3205 return NULL;
3206 }
3207
3208 sg_init_table(s, n_pages);
3209 for (idx = start_index; idx <= end_index; idx++) {
3210 char *va = lowmem_page_address(pl[idx].page);
3211 unsigned start = 0, end = PAGE_SIZE;
3212 if (idx == start_index)
3213 start = start_offset;
3214 if (idx == end_index)
3215 end = end_offset + (1 << SECTOR_SHIFT);
3216 sg_set_buf(&s[idx - start_index], va + start, end - start);
3217 }
3218
3219 sl[i] = s;
3220 }
3221
3222 return sl;
3223}
3224
3225static void free_alg(struct alg_spec *a)
3226{
3227 kzfree(a->alg_string);
3228 kzfree(a->key);
3229 memset(a, 0, sizeof *a);
3230}
3231
3232static int get_alg_and_key(const char *arg, struct alg_spec *a, char **error, char *error_inval)
3233{
3234 char *k;
3235
3236 free_alg(a);
3237
3238 a->alg_string = kstrdup(strchr(arg, ':') + 1, GFP_KERNEL);
3239 if (!a->alg_string)
3240 goto nomem;
3241
3242 k = strchr(a->alg_string, ':');
3243 if (k) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01003244 *k = 0;
3245 a->key_string = k + 1;
3246 if (strlen(a->key_string) & 1)
3247 goto inval;
3248
3249 a->key_size = strlen(a->key_string) / 2;
3250 a->key = kmalloc(a->key_size, GFP_KERNEL);
3251 if (!a->key)
3252 goto nomem;
Mikulas Patocka6625d902017-04-27 11:49:33 -04003253 if (hex2bin(a->key, a->key_string, a->key_size))
3254 goto inval;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003255 }
3256
3257 return 0;
3258inval:
3259 *error = error_inval;
3260 return -EINVAL;
3261nomem:
3262 *error = "Out of memory for an argument";
3263 return -ENOMEM;
3264}
3265
3266static int get_mac(struct crypto_shash **hash, struct alg_spec *a, char **error,
3267 char *error_alg, char *error_key)
3268{
3269 int r;
3270
3271 if (a->alg_string) {
Eric Biggers3d234b32018-11-14 12:21:11 -08003272 *hash = crypto_alloc_shash(a->alg_string, 0, 0);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003273 if (IS_ERR(*hash)) {
3274 *error = error_alg;
3275 r = PTR_ERR(*hash);
3276 *hash = NULL;
3277 return r;
3278 }
3279
3280 if (a->key) {
3281 r = crypto_shash_setkey(*hash, a->key, a->key_size);
3282 if (r) {
3283 *error = error_key;
3284 return r;
3285 }
Milan Broze16b4f92018-02-13 14:50:50 +01003286 } else if (crypto_shash_get_flags(*hash) & CRYPTO_TFM_NEED_KEY) {
3287 *error = error_key;
3288 return -ENOKEY;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003289 }
3290 }
3291
3292 return 0;
3293}
3294
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003295static int create_journal(struct dm_integrity_c *ic, char **error)
3296{
3297 int r = 0;
3298 unsigned i;
3299 __u64 journal_pages, journal_desc_size, journal_tree_size;
Mikulas Patocka717f4b12018-01-10 09:32:47 -05003300 unsigned char *crypt_data = NULL, *crypt_iv = NULL;
3301 struct skcipher_request *req = NULL;
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003302
3303 ic->commit_ids[0] = cpu_to_le64(0x1111111111111111ULL);
3304 ic->commit_ids[1] = cpu_to_le64(0x2222222222222222ULL);
3305 ic->commit_ids[2] = cpu_to_le64(0x3333333333333333ULL);
3306 ic->commit_ids[3] = cpu_to_le64(0x4444444444444444ULL);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003307
3308 journal_pages = roundup((__u64)ic->journal_sections * ic->journal_section_sectors,
3309 PAGE_SIZE >> SECTOR_SHIFT) >> (PAGE_SHIFT - SECTOR_SHIFT);
3310 journal_desc_size = journal_pages * sizeof(struct page_list);
Arun KSca79b0c2018-12-28 00:34:29 -08003311 if (journal_pages >= totalram_pages() - totalhigh_pages() || journal_desc_size > ULONG_MAX) {
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003312 *error = "Journal doesn't fit into memory";
3313 r = -ENOMEM;
3314 goto bad;
3315 }
3316 ic->journal_pages = journal_pages;
3317
Mikulas Patockad5027e02019-04-29 14:57:20 +02003318 ic->journal = dm_integrity_alloc_page_list(ic->journal_pages);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003319 if (!ic->journal) {
3320 *error = "Could not allocate memory for journal";
3321 r = -ENOMEM;
3322 goto bad;
3323 }
3324 if (ic->journal_crypt_alg.alg_string) {
3325 unsigned ivsize, blocksize;
3326 struct journal_completion comp;
3327
3328 comp.ic = ic;
3329 ic->journal_crypt = crypto_alloc_skcipher(ic->journal_crypt_alg.alg_string, 0, 0);
3330 if (IS_ERR(ic->journal_crypt)) {
3331 *error = "Invalid journal cipher";
3332 r = PTR_ERR(ic->journal_crypt);
3333 ic->journal_crypt = NULL;
3334 goto bad;
3335 }
3336 ivsize = crypto_skcipher_ivsize(ic->journal_crypt);
3337 blocksize = crypto_skcipher_blocksize(ic->journal_crypt);
3338
3339 if (ic->journal_crypt_alg.key) {
3340 r = crypto_skcipher_setkey(ic->journal_crypt, ic->journal_crypt_alg.key,
3341 ic->journal_crypt_alg.key_size);
3342 if (r) {
3343 *error = "Error setting encryption key";
3344 goto bad;
3345 }
3346 }
3347 DEBUG_print("cipher %s, block size %u iv size %u\n",
3348 ic->journal_crypt_alg.alg_string, blocksize, ivsize);
3349
Mikulas Patockad5027e02019-04-29 14:57:20 +02003350 ic->journal_io = dm_integrity_alloc_page_list(ic->journal_pages);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003351 if (!ic->journal_io) {
3352 *error = "Could not allocate memory for journal io";
3353 r = -ENOMEM;
3354 goto bad;
3355 }
3356
3357 if (blocksize == 1) {
3358 struct scatterlist *sg;
Mikulas Patocka717f4b12018-01-10 09:32:47 -05003359
3360 req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
3361 if (!req) {
3362 *error = "Could not allocate crypt request";
3363 r = -ENOMEM;
3364 goto bad;
3365 }
3366
3367 crypt_iv = kmalloc(ivsize, GFP_KERNEL);
3368 if (!crypt_iv) {
3369 *error = "Could not allocate iv";
3370 r = -ENOMEM;
3371 goto bad;
3372 }
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003373
Mikulas Patockad5027e02019-04-29 14:57:20 +02003374 ic->journal_xor = dm_integrity_alloc_page_list(ic->journal_pages);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003375 if (!ic->journal_xor) {
3376 *error = "Could not allocate memory for journal xor";
3377 r = -ENOMEM;
3378 goto bad;
3379 }
3380
Kees Cook344476e2018-06-12 14:04:32 -07003381 sg = kvmalloc_array(ic->journal_pages + 1,
3382 sizeof(struct scatterlist),
3383 GFP_KERNEL);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003384 if (!sg) {
3385 *error = "Unable to allocate sg list";
3386 r = -ENOMEM;
3387 goto bad;
3388 }
3389 sg_init_table(sg, ic->journal_pages + 1);
3390 for (i = 0; i < ic->journal_pages; i++) {
3391 char *va = lowmem_page_address(ic->journal_xor[i].page);
3392 clear_page(va);
3393 sg_set_buf(&sg[i], va, PAGE_SIZE);
3394 }
3395 sg_set_buf(&sg[i], &ic->commit_ids, sizeof ic->commit_ids);
Mikulas Patocka717f4b12018-01-10 09:32:47 -05003396 memset(crypt_iv, 0x00, ivsize);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003397
Mike Snitzer05d69092019-05-09 15:25:49 -04003398 skcipher_request_set_crypt(req, sg, sg,
3399 PAGE_SIZE * ic->journal_pages + sizeof ic->commit_ids, crypt_iv);
Arnd Bergmannb5e8ad92017-08-15 17:11:59 +02003400 init_completion(&comp.comp);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003401 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
3402 if (do_crypt(true, req, &comp))
3403 wait_for_completion(&comp.comp);
3404 kvfree(sg);
3405 r = dm_integrity_failed(ic);
3406 if (r) {
3407 *error = "Unable to encrypt journal";
3408 goto bad;
3409 }
3410 DEBUG_bytes(lowmem_page_address(ic->journal_xor[0].page), 64, "xor data");
3411
3412 crypto_free_skcipher(ic->journal_crypt);
3413 ic->journal_crypt = NULL;
3414 } else {
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003415 unsigned crypt_len = roundup(ivsize, blocksize);
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003416
Mikulas Patocka717f4b12018-01-10 09:32:47 -05003417 req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
3418 if (!req) {
3419 *error = "Could not allocate crypt request";
3420 r = -ENOMEM;
3421 goto bad;
3422 }
3423
3424 crypt_iv = kmalloc(ivsize, GFP_KERNEL);
3425 if (!crypt_iv) {
3426 *error = "Could not allocate iv";
3427 r = -ENOMEM;
3428 goto bad;
3429 }
3430
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003431 crypt_data = kmalloc(crypt_len, GFP_KERNEL);
3432 if (!crypt_data) {
3433 *error = "Unable to allocate crypt data";
3434 r = -ENOMEM;
3435 goto bad;
3436 }
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003437
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003438 ic->journal_scatterlist = dm_integrity_alloc_journal_scatterlist(ic, ic->journal);
3439 if (!ic->journal_scatterlist) {
3440 *error = "Unable to allocate sg list";
3441 r = -ENOMEM;
3442 goto bad;
3443 }
3444 ic->journal_io_scatterlist = dm_integrity_alloc_journal_scatterlist(ic, ic->journal_io);
3445 if (!ic->journal_io_scatterlist) {
3446 *error = "Unable to allocate sg list";
3447 r = -ENOMEM;
3448 goto bad;
3449 }
Kees Cook344476e2018-06-12 14:04:32 -07003450 ic->sk_requests = kvmalloc_array(ic->journal_sections,
3451 sizeof(struct skcipher_request *),
3452 GFP_KERNEL | __GFP_ZERO);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003453 if (!ic->sk_requests) {
3454 *error = "Unable to allocate sk requests";
3455 r = -ENOMEM;
3456 goto bad;
3457 }
3458 for (i = 0; i < ic->journal_sections; i++) {
3459 struct scatterlist sg;
3460 struct skcipher_request *section_req;
3461 __u32 section_le = cpu_to_le32(i);
3462
Mikulas Patocka717f4b12018-01-10 09:32:47 -05003463 memset(crypt_iv, 0x00, ivsize);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003464 memset(crypt_data, 0x00, crypt_len);
3465 memcpy(crypt_data, &section_le, min((size_t)crypt_len, sizeof(section_le)));
3466
3467 sg_init_one(&sg, crypt_data, crypt_len);
Mikulas Patocka717f4b12018-01-10 09:32:47 -05003468 skcipher_request_set_crypt(req, &sg, &sg, crypt_len, crypt_iv);
Arnd Bergmannb5e8ad92017-08-15 17:11:59 +02003469 init_completion(&comp.comp);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003470 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
3471 if (do_crypt(true, req, &comp))
3472 wait_for_completion(&comp.comp);
3473
3474 r = dm_integrity_failed(ic);
3475 if (r) {
3476 *error = "Unable to generate iv";
3477 goto bad;
3478 }
3479
3480 section_req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
3481 if (!section_req) {
3482 *error = "Unable to allocate crypt request";
3483 r = -ENOMEM;
3484 goto bad;
3485 }
Kees Cook6da2ec52018-06-12 13:55:00 -07003486 section_req->iv = kmalloc_array(ivsize, 2,
3487 GFP_KERNEL);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003488 if (!section_req->iv) {
3489 skcipher_request_free(section_req);
3490 *error = "Unable to allocate iv";
3491 r = -ENOMEM;
3492 goto bad;
3493 }
3494 memcpy(section_req->iv + ivsize, crypt_data, ivsize);
3495 section_req->cryptlen = (size_t)ic->journal_section_sectors << SECTOR_SHIFT;
3496 ic->sk_requests[i] = section_req;
3497 DEBUG_bytes(crypt_data, ivsize, "iv(%u)", i);
3498 }
3499 }
3500 }
3501
3502 for (i = 0; i < N_COMMIT_IDS; i++) {
3503 unsigned j;
3504retest_commit_id:
3505 for (j = 0; j < i; j++) {
3506 if (ic->commit_ids[j] == ic->commit_ids[i]) {
3507 ic->commit_ids[i] = cpu_to_le64(le64_to_cpu(ic->commit_ids[i]) + 1);
3508 goto retest_commit_id;
3509 }
3510 }
3511 DEBUG_print("commit id %u: %016llx\n", i, ic->commit_ids[i]);
3512 }
3513
3514 journal_tree_size = (__u64)ic->journal_entries * sizeof(struct journal_node);
3515 if (journal_tree_size > ULONG_MAX) {
3516 *error = "Journal doesn't fit into memory";
3517 r = -ENOMEM;
3518 goto bad;
3519 }
Mikulas Patocka702a6202017-05-20 14:56:21 -04003520 ic->journal_tree = kvmalloc(journal_tree_size, GFP_KERNEL);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003521 if (!ic->journal_tree) {
3522 *error = "Could not allocate memory for journal tree";
3523 r = -ENOMEM;
3524 }
3525bad:
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003526 kfree(crypt_data);
Mikulas Patocka717f4b12018-01-10 09:32:47 -05003527 kfree(crypt_iv);
3528 skcipher_request_free(req);
3529
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003530 return r;
3531}
3532
Mikulas Patocka7eada902017-01-04 20:23:53 +01003533/*
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003534 * Construct a integrity mapping
Mikulas Patocka7eada902017-01-04 20:23:53 +01003535 *
3536 * Arguments:
3537 * device
3538 * offset from the start of the device
3539 * tag size
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003540 * D - direct writes, J - journal writes, B - bitmap mode, R - recovery mode
Mikulas Patocka7eada902017-01-04 20:23:53 +01003541 * number of optional arguments
3542 * optional arguments:
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003543 * journal_sectors
3544 * interleave_sectors
3545 * buffer_sectors
3546 * journal_watermark
3547 * commit_time
Mikulas Patocka88ad5d12019-04-29 14:57:23 +02003548 * meta_device
3549 * block_size
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003550 * sectors_per_bit
3551 * bitmap_flush_interval
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003552 * internal_hash
3553 * journal_crypt
3554 * journal_mac
Mikulas Patocka88ad5d12019-04-29 14:57:23 +02003555 * recalculate
Mikulas Patocka7eada902017-01-04 20:23:53 +01003556 */
3557static int dm_integrity_ctr(struct dm_target *ti, unsigned argc, char **argv)
3558{
3559 struct dm_integrity_c *ic;
3560 char dummy;
3561 int r;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003562 unsigned extra_args;
3563 struct dm_arg_set as;
Eric Biggers5916a222017-06-22 11:32:45 -07003564 static const struct dm_arg _args[] = {
Mikulas Patocka9d609f852017-04-18 16:51:52 -04003565 {0, 9, "Invalid number of feature args"},
Mikulas Patocka7eada902017-01-04 20:23:53 +01003566 };
3567 unsigned journal_sectors, interleave_sectors, buffer_sectors, journal_watermark, sync_msec;
3568 bool should_write_sb;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003569 __u64 threshold;
3570 unsigned long long start;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003571 __s8 log2_sectors_per_bitmap_bit = -1;
3572 __s8 log2_blocks_per_bitmap_bit;
3573 __u64 bits_in_journal;
3574 __u64 n_bitmap_bits;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003575
3576#define DIRECT_ARGUMENTS 4
3577
3578 if (argc <= DIRECT_ARGUMENTS) {
3579 ti->error = "Invalid argument count";
3580 return -EINVAL;
3581 }
3582
3583 ic = kzalloc(sizeof(struct dm_integrity_c), GFP_KERNEL);
3584 if (!ic) {
3585 ti->error = "Cannot allocate integrity context";
3586 return -ENOMEM;
3587 }
3588 ti->private = ic;
3589 ti->per_io_data_size = sizeof(struct dm_integrity_io);
3590
Mikulas Patocka7eada902017-01-04 20:23:53 +01003591 ic->in_progress = RB_ROOT;
Mikulas Patocka724376a2018-07-03 20:13:27 +02003592 INIT_LIST_HEAD(&ic->wait_list);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003593 init_waitqueue_head(&ic->endio_wait);
3594 bio_list_init(&ic->flush_bio_list);
3595 init_waitqueue_head(&ic->copy_to_journal_wait);
3596 init_completion(&ic->crypto_backoff);
Mikulas Patocka3f2e5392017-07-21 12:00:00 -04003597 atomic64_set(&ic->number_of_mismatches, 0);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003598 ic->bitmap_flush_interval = BITMAP_FLUSH_INTERVAL;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003599
3600 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &ic->dev);
3601 if (r) {
3602 ti->error = "Device lookup failed";
3603 goto bad;
3604 }
3605
3606 if (sscanf(argv[1], "%llu%c", &start, &dummy) != 1 || start != (sector_t)start) {
3607 ti->error = "Invalid starting offset";
3608 r = -EINVAL;
3609 goto bad;
3610 }
3611 ic->start = start;
3612
3613 if (strcmp(argv[2], "-")) {
3614 if (sscanf(argv[2], "%u%c", &ic->tag_size, &dummy) != 1 || !ic->tag_size) {
3615 ti->error = "Invalid tag size";
3616 r = -EINVAL;
3617 goto bad;
3618 }
3619 }
3620
Mike Snitzer05d69092019-05-09 15:25:49 -04003621 if (!strcmp(argv[3], "J") || !strcmp(argv[3], "B") ||
3622 !strcmp(argv[3], "D") || !strcmp(argv[3], "R")) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01003623 ic->mode = argv[3][0];
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003624 } else {
3625 ti->error = "Invalid mode (expecting J, B, D, R)";
Mikulas Patocka7eada902017-01-04 20:23:53 +01003626 r = -EINVAL;
3627 goto bad;
3628 }
3629
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003630 journal_sectors = 0;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003631 interleave_sectors = DEFAULT_INTERLEAVE_SECTORS;
3632 buffer_sectors = DEFAULT_BUFFER_SECTORS;
3633 journal_watermark = DEFAULT_JOURNAL_WATERMARK;
3634 sync_msec = DEFAULT_SYNC_MSEC;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04003635 ic->sectors_per_block = 1;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003636
3637 as.argc = argc - DIRECT_ARGUMENTS;
3638 as.argv = argv + DIRECT_ARGUMENTS;
3639 r = dm_read_arg_group(_args, &as, &extra_args, &ti->error);
3640 if (r)
3641 goto bad;
3642
3643 while (extra_args--) {
3644 const char *opt_string;
3645 unsigned val;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003646 unsigned long long llval;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003647 opt_string = dm_shift_arg(&as);
3648 if (!opt_string) {
3649 r = -EINVAL;
3650 ti->error = "Not enough feature arguments";
3651 goto bad;
3652 }
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003653 if (sscanf(opt_string, "journal_sectors:%u%c", &val, &dummy) == 1)
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003654 journal_sectors = val ? val : 1;
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003655 else if (sscanf(opt_string, "interleave_sectors:%u%c", &val, &dummy) == 1)
Mikulas Patocka7eada902017-01-04 20:23:53 +01003656 interleave_sectors = val;
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003657 else if (sscanf(opt_string, "buffer_sectors:%u%c", &val, &dummy) == 1)
Mikulas Patocka7eada902017-01-04 20:23:53 +01003658 buffer_sectors = val;
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003659 else if (sscanf(opt_string, "journal_watermark:%u%c", &val, &dummy) == 1 && val <= 100)
Mikulas Patocka7eada902017-01-04 20:23:53 +01003660 journal_watermark = val;
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003661 else if (sscanf(opt_string, "commit_time:%u%c", &val, &dummy) == 1)
Mikulas Patocka7eada902017-01-04 20:23:53 +01003662 sync_msec = val;
Mikulas Patocka0d74e6a2019-03-13 07:56:02 -04003663 else if (!strncmp(opt_string, "meta_device:", strlen("meta_device:"))) {
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003664 if (ic->meta_dev) {
3665 dm_put_device(ti, ic->meta_dev);
3666 ic->meta_dev = NULL;
3667 }
Mike Snitzer05d69092019-05-09 15:25:49 -04003668 r = dm_get_device(ti, strchr(opt_string, ':') + 1,
3669 dm_table_get_mode(ti->table), &ic->meta_dev);
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003670 if (r) {
3671 ti->error = "Device lookup failed";
3672 goto bad;
3673 }
3674 } else if (sscanf(opt_string, "block_size:%u%c", &val, &dummy) == 1) {
Mikulas Patocka9d609f852017-04-18 16:51:52 -04003675 if (val < 1 << SECTOR_SHIFT ||
3676 val > MAX_SECTORS_PER_BLOCK << SECTOR_SHIFT ||
3677 (val & (val -1))) {
3678 r = -EINVAL;
3679 ti->error = "Invalid block_size argument";
3680 goto bad;
3681 }
3682 ic->sectors_per_block = val >> SECTOR_SHIFT;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003683 } else if (sscanf(opt_string, "sectors_per_bit:%llu%c", &llval, &dummy) == 1) {
3684 log2_sectors_per_bitmap_bit = !llval ? 0 : __ilog2_u64(llval);
3685 } else if (sscanf(opt_string, "bitmap_flush_interval:%u%c", &val, &dummy) == 1) {
3686 if (val >= (uint64_t)UINT_MAX * 1000 / HZ) {
3687 r = -EINVAL;
3688 ti->error = "Invalid bitmap_flush_interval argument";
3689 }
3690 ic->bitmap_flush_interval = msecs_to_jiffies(val);
Mikulas Patocka0d74e6a2019-03-13 07:56:02 -04003691 } else if (!strncmp(opt_string, "internal_hash:", strlen("internal_hash:"))) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01003692 r = get_alg_and_key(opt_string, &ic->internal_hash_alg, &ti->error,
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003693 "Invalid internal_hash argument");
Mikulas Patocka7eada902017-01-04 20:23:53 +01003694 if (r)
3695 goto bad;
Mikulas Patocka0d74e6a2019-03-13 07:56:02 -04003696 } else if (!strncmp(opt_string, "journal_crypt:", strlen("journal_crypt:"))) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01003697 r = get_alg_and_key(opt_string, &ic->journal_crypt_alg, &ti->error,
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003698 "Invalid journal_crypt argument");
Mikulas Patocka7eada902017-01-04 20:23:53 +01003699 if (r)
3700 goto bad;
Mikulas Patocka0d74e6a2019-03-13 07:56:02 -04003701 } else if (!strncmp(opt_string, "journal_mac:", strlen("journal_mac:"))) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01003702 r = get_alg_and_key(opt_string, &ic->journal_mac_alg, &ti->error,
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003703 "Invalid journal_mac argument");
Mikulas Patocka7eada902017-01-04 20:23:53 +01003704 if (r)
3705 goto bad;
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02003706 } else if (!strcmp(opt_string, "recalculate")) {
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003707 ic->recalculate_flag = true;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003708 } else {
3709 r = -EINVAL;
3710 ti->error = "Invalid argument";
3711 goto bad;
3712 }
3713 }
3714
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003715 ic->data_device_sectors = i_size_read(ic->dev->bdev->bd_inode) >> SECTOR_SHIFT;
3716 if (!ic->meta_dev)
3717 ic->meta_device_sectors = ic->data_device_sectors;
3718 else
3719 ic->meta_device_sectors = i_size_read(ic->meta_dev->bdev->bd_inode) >> SECTOR_SHIFT;
3720
3721 if (!journal_sectors) {
3722 journal_sectors = min((sector_t)DEFAULT_MAX_JOURNAL_SECTORS,
Mike Snitzer05d69092019-05-09 15:25:49 -04003723 ic->data_device_sectors >> DEFAULT_JOURNAL_SIZE_FACTOR);
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003724 }
3725
3726 if (!buffer_sectors)
3727 buffer_sectors = 1;
3728 ic->log2_buffer_sectors = min((int)__fls(buffer_sectors), 31 - SECTOR_SHIFT);
3729
Mikulas Patocka7eada902017-01-04 20:23:53 +01003730 r = get_mac(&ic->internal_hash, &ic->internal_hash_alg, &ti->error,
3731 "Invalid internal hash", "Error setting internal hash key");
3732 if (r)
3733 goto bad;
3734
3735 r = get_mac(&ic->journal_mac, &ic->journal_mac_alg, &ti->error,
3736 "Invalid journal mac", "Error setting journal mac key");
3737 if (r)
3738 goto bad;
3739
3740 if (!ic->tag_size) {
3741 if (!ic->internal_hash) {
3742 ti->error = "Unknown tag size";
3743 r = -EINVAL;
3744 goto bad;
3745 }
3746 ic->tag_size = crypto_shash_digestsize(ic->internal_hash);
3747 }
3748 if (ic->tag_size > MAX_TAG_SIZE) {
3749 ti->error = "Too big tag size";
3750 r = -EINVAL;
3751 goto bad;
3752 }
3753 if (!(ic->tag_size & (ic->tag_size - 1)))
3754 ic->log2_tag_size = __ffs(ic->tag_size);
3755 else
3756 ic->log2_tag_size = -1;
3757
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003758 if (ic->mode == 'B' && !ic->internal_hash) {
3759 r = -EINVAL;
3760 ti->error = "Bitmap mode can be only used with internal hash";
3761 goto bad;
3762 }
3763
Mikulas Patocka7eada902017-01-04 20:23:53 +01003764 ic->autocommit_jiffies = msecs_to_jiffies(sync_msec);
3765 ic->autocommit_msec = sync_msec;
Kees Cook8376d3c2017-10-16 17:01:48 -07003766 timer_setup(&ic->autocommit_timer, autocommit_fn, 0);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003767
3768 ic->io = dm_io_client_create();
3769 if (IS_ERR(ic->io)) {
3770 r = PTR_ERR(ic->io);
3771 ic->io = NULL;
3772 ti->error = "Cannot allocate dm io";
3773 goto bad;
3774 }
3775
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003776 r = mempool_init_slab_pool(&ic->journal_io_mempool, JOURNAL_IO_MEMPOOL, journal_io_cache);
3777 if (r) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01003778 ti->error = "Cannot allocate mempool";
3779 goto bad;
3780 }
3781
3782 ic->metadata_wq = alloc_workqueue("dm-integrity-metadata",
3783 WQ_MEM_RECLAIM, METADATA_WORKQUEUE_MAX_ACTIVE);
3784 if (!ic->metadata_wq) {
3785 ti->error = "Cannot allocate workqueue";
3786 r = -ENOMEM;
3787 goto bad;
3788 }
3789
3790 /*
3791 * If this workqueue were percpu, it would cause bio reordering
3792 * and reduced performance.
3793 */
3794 ic->wait_wq = alloc_workqueue("dm-integrity-wait", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
3795 if (!ic->wait_wq) {
3796 ti->error = "Cannot allocate workqueue";
3797 r = -ENOMEM;
3798 goto bad;
3799 }
3800
3801 ic->commit_wq = alloc_workqueue("dm-integrity-commit", WQ_MEM_RECLAIM, 1);
3802 if (!ic->commit_wq) {
3803 ti->error = "Cannot allocate workqueue";
3804 r = -ENOMEM;
3805 goto bad;
3806 }
3807 INIT_WORK(&ic->commit_work, integrity_commit);
3808
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003809 if (ic->mode == 'J' || ic->mode == 'B') {
Mikulas Patocka7eada902017-01-04 20:23:53 +01003810 ic->writer_wq = alloc_workqueue("dm-integrity-writer", WQ_MEM_RECLAIM, 1);
3811 if (!ic->writer_wq) {
3812 ti->error = "Cannot allocate workqueue";
3813 r = -ENOMEM;
3814 goto bad;
3815 }
3816 INIT_WORK(&ic->writer_work, integrity_writer);
3817 }
3818
3819 ic->sb = alloc_pages_exact(SB_SECTORS << SECTOR_SHIFT, GFP_KERNEL);
3820 if (!ic->sb) {
3821 r = -ENOMEM;
3822 ti->error = "Cannot allocate superblock area";
3823 goto bad;
3824 }
3825
3826 r = sync_rw_sb(ic, REQ_OP_READ, 0);
3827 if (r) {
3828 ti->error = "Error reading superblock";
3829 goto bad;
3830 }
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04003831 should_write_sb = false;
3832 if (memcmp(ic->sb->magic, SB_MAGIC, 8)) {
3833 if (ic->mode != 'R') {
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003834 if (memchr_inv(ic->sb, 0, SB_SECTORS << SECTOR_SHIFT)) {
3835 r = -EINVAL;
3836 ti->error = "The device is not initialized";
3837 goto bad;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003838 }
3839 }
3840
3841 r = initialize_superblock(ic, journal_sectors, interleave_sectors);
3842 if (r) {
3843 ti->error = "Could not initialize superblock";
3844 goto bad;
3845 }
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04003846 if (ic->mode != 'R')
3847 should_write_sb = true;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003848 }
3849
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003850 if (!ic->sb->version || ic->sb->version > SB_VERSION_3) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01003851 r = -EINVAL;
3852 ti->error = "Unknown version";
3853 goto bad;
3854 }
3855 if (le16_to_cpu(ic->sb->integrity_tag_size) != ic->tag_size) {
3856 r = -EINVAL;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04003857 ti->error = "Tag size doesn't match the information in superblock";
3858 goto bad;
3859 }
3860 if (ic->sb->log2_sectors_per_block != __ffs(ic->sectors_per_block)) {
3861 r = -EINVAL;
3862 ti->error = "Block size doesn't match the information in superblock";
Mikulas Patocka7eada902017-01-04 20:23:53 +01003863 goto bad;
3864 }
Mikulas Patockabc86a412017-07-21 11:58:38 -04003865 if (!le32_to_cpu(ic->sb->journal_sections)) {
3866 r = -EINVAL;
3867 ti->error = "Corrupted superblock, journal_sections is 0";
3868 goto bad;
3869 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01003870 /* make sure that ti->max_io_len doesn't overflow */
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003871 if (!ic->meta_dev) {
3872 if (ic->sb->log2_interleave_sectors < MIN_LOG2_INTERLEAVE_SECTORS ||
3873 ic->sb->log2_interleave_sectors > MAX_LOG2_INTERLEAVE_SECTORS) {
3874 r = -EINVAL;
3875 ti->error = "Invalid interleave_sectors in the superblock";
3876 goto bad;
3877 }
3878 } else {
3879 if (ic->sb->log2_interleave_sectors) {
3880 r = -EINVAL;
3881 ti->error = "Invalid interleave_sectors in the superblock";
3882 goto bad;
3883 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01003884 }
3885 ic->provided_data_sectors = le64_to_cpu(ic->sb->provided_data_sectors);
3886 if (ic->provided_data_sectors != le64_to_cpu(ic->sb->provided_data_sectors)) {
3887 /* test for overflow */
3888 r = -EINVAL;
3889 ti->error = "The superblock has 64-bit device size, but the kernel was compiled with 32-bit sectors";
3890 goto bad;
3891 }
3892 if (!!(ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC)) != !!ic->journal_mac_alg.alg_string) {
3893 r = -EINVAL;
3894 ti->error = "Journal mac mismatch";
3895 goto bad;
3896 }
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003897
3898try_smaller_buffer:
Mikulas Patocka7eada902017-01-04 20:23:53 +01003899 r = calculate_device_limits(ic);
3900 if (r) {
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003901 if (ic->meta_dev) {
3902 if (ic->log2_buffer_sectors > 3) {
3903 ic->log2_buffer_sectors--;
3904 goto try_smaller_buffer;
3905 }
3906 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01003907 ti->error = "The device is too small";
3908 goto bad;
3909 }
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003910
3911 if (log2_sectors_per_bitmap_bit < 0)
3912 log2_sectors_per_bitmap_bit = __fls(DEFAULT_SECTORS_PER_BITMAP_BIT);
3913 if (log2_sectors_per_bitmap_bit < ic->sb->log2_sectors_per_block)
3914 log2_sectors_per_bitmap_bit = ic->sb->log2_sectors_per_block;
3915
3916 bits_in_journal = ((__u64)ic->journal_section_sectors * ic->journal_sections) << (SECTOR_SHIFT + 3);
3917 if (bits_in_journal > UINT_MAX)
3918 bits_in_journal = UINT_MAX;
3919 while (bits_in_journal < (ic->provided_data_sectors + ((sector_t)1 << log2_sectors_per_bitmap_bit) - 1) >> log2_sectors_per_bitmap_bit)
3920 log2_sectors_per_bitmap_bit++;
3921
3922 log2_blocks_per_bitmap_bit = log2_sectors_per_bitmap_bit - ic->sb->log2_sectors_per_block;
3923 ic->log2_blocks_per_bitmap_bit = log2_blocks_per_bitmap_bit;
3924 if (should_write_sb) {
3925 ic->sb->log2_blocks_per_bitmap_bit = log2_blocks_per_bitmap_bit;
3926 }
3927 n_bitmap_bits = ((ic->provided_data_sectors >> ic->sb->log2_sectors_per_block)
3928 + (((sector_t)1 << log2_blocks_per_bitmap_bit) - 1)) >> log2_blocks_per_bitmap_bit;
3929 ic->n_bitmap_blocks = DIV_ROUND_UP(n_bitmap_bits, BITMAP_BLOCK_SIZE * 8);
3930
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003931 if (!ic->meta_dev)
3932 ic->log2_buffer_sectors = min(ic->log2_buffer_sectors, (__u8)__ffs(ic->metadata_run));
3933
Ondrej Mosnáček2ad50602017-06-05 17:52:39 +02003934 if (ti->len > ic->provided_data_sectors) {
3935 r = -EINVAL;
3936 ti->error = "Not enough provided sectors for requested mapping size";
3937 goto bad;
3938 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01003939
Mikulas Patocka7eada902017-01-04 20:23:53 +01003940
3941 threshold = (__u64)ic->journal_entries * (100 - journal_watermark);
3942 threshold += 50;
3943 do_div(threshold, 100);
3944 ic->free_sectors_threshold = threshold;
3945
3946 DEBUG_print("initialized:\n");
3947 DEBUG_print(" integrity_tag_size %u\n", le16_to_cpu(ic->sb->integrity_tag_size));
3948 DEBUG_print(" journal_entry_size %u\n", ic->journal_entry_size);
3949 DEBUG_print(" journal_entries_per_sector %u\n", ic->journal_entries_per_sector);
3950 DEBUG_print(" journal_section_entries %u\n", ic->journal_section_entries);
3951 DEBUG_print(" journal_section_sectors %u\n", ic->journal_section_sectors);
3952 DEBUG_print(" journal_sections %u\n", (unsigned)le32_to_cpu(ic->sb->journal_sections));
3953 DEBUG_print(" journal_entries %u\n", ic->journal_entries);
3954 DEBUG_print(" log2_interleave_sectors %d\n", ic->sb->log2_interleave_sectors);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003955 DEBUG_print(" data_device_sectors 0x%llx\n", i_size_read(ic->dev->bdev->bd_inode) >> SECTOR_SHIFT);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003956 DEBUG_print(" initial_sectors 0x%x\n", ic->initial_sectors);
3957 DEBUG_print(" metadata_run 0x%x\n", ic->metadata_run);
3958 DEBUG_print(" log2_metadata_run %d\n", ic->log2_metadata_run);
3959 DEBUG_print(" provided_data_sectors 0x%llx (%llu)\n", (unsigned long long)ic->provided_data_sectors,
3960 (unsigned long long)ic->provided_data_sectors);
3961 DEBUG_print(" log2_buffer_sectors %u\n", ic->log2_buffer_sectors);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003962 DEBUG_print(" bits_in_journal %llu\n", (unsigned long long)bits_in_journal);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003963
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003964 if (ic->recalculate_flag && !(ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))) {
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02003965 ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
3966 ic->sb->recalc_sector = cpu_to_le64(0);
3967 }
3968
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003969 if (ic->internal_hash) {
Colin Ian Kinge8c25662018-11-28 15:15:31 +00003970 ic->recalc_wq = alloc_workqueue("dm-integrity-recalc", WQ_MEM_RECLAIM, 1);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02003971 if (!ic->recalc_wq ) {
3972 ti->error = "Cannot allocate workqueue";
3973 r = -ENOMEM;
3974 goto bad;
3975 }
3976 INIT_WORK(&ic->recalc_work, integrity_recalc);
3977 ic->recalc_buffer = vmalloc(RECALC_SECTORS << SECTOR_SHIFT);
3978 if (!ic->recalc_buffer) {
3979 ti->error = "Cannot allocate buffer for recalculating";
3980 r = -ENOMEM;
3981 goto bad;
3982 }
Kees Cook329e0982018-10-05 16:21:46 -07003983 ic->recalc_tags = kvmalloc_array(RECALC_SECTORS >> ic->sb->log2_sectors_per_block,
3984 ic->tag_size, GFP_KERNEL);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02003985 if (!ic->recalc_tags) {
3986 ti->error = "Cannot allocate tags for recalculating";
3987 r = -ENOMEM;
3988 goto bad;
3989 }
3990 }
3991
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003992 ic->bufio = dm_bufio_client_create(ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev,
3993 1U << (SECTOR_SHIFT + ic->log2_buffer_sectors), 1, 0, NULL, NULL);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003994 if (IS_ERR(ic->bufio)) {
3995 r = PTR_ERR(ic->bufio);
3996 ti->error = "Cannot initialize dm-bufio";
3997 ic->bufio = NULL;
3998 goto bad;
3999 }
4000 dm_bufio_set_sector_offset(ic->bufio, ic->start + ic->initial_sectors);
4001
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04004002 if (ic->mode != 'R') {
4003 r = create_journal(ic, &ti->error);
4004 if (r)
4005 goto bad;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02004006
4007 }
4008
4009 if (ic->mode == 'B') {
4010 unsigned i;
4011 unsigned n_bitmap_pages = DIV_ROUND_UP(ic->n_bitmap_blocks, PAGE_SIZE / BITMAP_BLOCK_SIZE);
4012
4013 ic->recalc_bitmap = dm_integrity_alloc_page_list(n_bitmap_pages);
4014 if (!ic->recalc_bitmap) {
4015 r = -ENOMEM;
4016 goto bad;
4017 }
4018 ic->may_write_bitmap = dm_integrity_alloc_page_list(n_bitmap_pages);
4019 if (!ic->may_write_bitmap) {
4020 r = -ENOMEM;
4021 goto bad;
4022 }
4023 ic->bbs = kvmalloc_array(ic->n_bitmap_blocks, sizeof(struct bitmap_block_status), GFP_KERNEL);
4024 if (!ic->bbs) {
4025 r = -ENOMEM;
4026 goto bad;
4027 }
4028 INIT_DELAYED_WORK(&ic->bitmap_flush_work, bitmap_flush_work);
4029 for (i = 0; i < ic->n_bitmap_blocks; i++) {
4030 struct bitmap_block_status *bbs = &ic->bbs[i];
4031 unsigned sector, pl_index, pl_offset;
4032
4033 INIT_WORK(&bbs->work, bitmap_block_work);
4034 bbs->ic = ic;
4035 bbs->idx = i;
4036 bio_list_init(&bbs->bio_queue);
4037 spin_lock_init(&bbs->bio_queue_lock);
4038
4039 sector = i * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT);
4040 pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
4041 pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
4042
4043 bbs->bitmap = lowmem_page_address(ic->journal[pl_index].page) + pl_offset;
4044 }
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04004045 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01004046
4047 if (should_write_sb) {
4048 int r;
4049
4050 init_journal(ic, 0, ic->journal_sections, 0);
4051 r = dm_integrity_failed(ic);
4052 if (unlikely(r)) {
4053 ti->error = "Error initializing journal";
4054 goto bad;
4055 }
4056 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
4057 if (r) {
4058 ti->error = "Error initializing superblock";
4059 goto bad;
4060 }
4061 ic->just_formatted = true;
4062 }
4063
Mikulas Patocka356d9d52018-07-03 20:13:30 +02004064 if (!ic->meta_dev) {
4065 r = dm_set_target_max_io_len(ti, 1U << ic->sb->log2_interleave_sectors);
4066 if (r)
4067 goto bad;
4068 }
Mikulas Patocka468dfca2019-04-29 14:57:24 +02004069 if (ic->mode == 'B') {
4070 unsigned max_io_len = ((sector_t)ic->sectors_per_block << ic->log2_blocks_per_bitmap_bit) * (BITMAP_BLOCK_SIZE * 8);
4071 if (!max_io_len)
4072 max_io_len = 1U << 31;
4073 DEBUG_print("max_io_len: old %u, new %u\n", ti->max_io_len, max_io_len);
4074 if (!ti->max_io_len || ti->max_io_len > max_io_len) {
4075 r = dm_set_target_max_io_len(ti, max_io_len);
4076 if (r)
4077 goto bad;
4078 }
4079 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01004080
4081 if (!ic->internal_hash)
4082 dm_integrity_set(ti, ic);
4083
4084 ti->num_flush_bios = 1;
4085 ti->flush_supported = true;
4086
4087 return 0;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02004088
Mikulas Patocka7eada902017-01-04 20:23:53 +01004089bad:
4090 dm_integrity_dtr(ti);
4091 return r;
4092}
4093
4094static void dm_integrity_dtr(struct dm_target *ti)
4095{
4096 struct dm_integrity_c *ic = ti->private;
4097
4098 BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress));
Mikulas Patocka724376a2018-07-03 20:13:27 +02004099 BUG_ON(!list_empty(&ic->wait_list));
Mikulas Patocka7eada902017-01-04 20:23:53 +01004100
4101 if (ic->metadata_wq)
4102 destroy_workqueue(ic->metadata_wq);
4103 if (ic->wait_wq)
4104 destroy_workqueue(ic->wait_wq);
4105 if (ic->commit_wq)
4106 destroy_workqueue(ic->commit_wq);
4107 if (ic->writer_wq)
4108 destroy_workqueue(ic->writer_wq);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02004109 if (ic->recalc_wq)
4110 destroy_workqueue(ic->recalc_wq);
Mikulas Patocka97abfde2019-04-29 14:57:17 +02004111 vfree(ic->recalc_buffer);
4112 kvfree(ic->recalc_tags);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02004113 kvfree(ic->bbs);
Mikulas Patocka7eada902017-01-04 20:23:53 +01004114 if (ic->bufio)
4115 dm_bufio_client_destroy(ic->bufio);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04004116 mempool_exit(&ic->journal_io_mempool);
Mikulas Patocka7eada902017-01-04 20:23:53 +01004117 if (ic->io)
4118 dm_io_client_destroy(ic->io);
4119 if (ic->dev)
4120 dm_put_device(ti, ic->dev);
Mikulas Patocka356d9d52018-07-03 20:13:30 +02004121 if (ic->meta_dev)
4122 dm_put_device(ti, ic->meta_dev);
Mikulas Patockad5027e02019-04-29 14:57:20 +02004123 dm_integrity_free_page_list(ic->journal);
4124 dm_integrity_free_page_list(ic->journal_io);
4125 dm_integrity_free_page_list(ic->journal_xor);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02004126 dm_integrity_free_page_list(ic->recalc_bitmap);
4127 dm_integrity_free_page_list(ic->may_write_bitmap);
Mikulas Patocka7eada902017-01-04 20:23:53 +01004128 if (ic->journal_scatterlist)
4129 dm_integrity_free_journal_scatterlist(ic, ic->journal_scatterlist);
4130 if (ic->journal_io_scatterlist)
4131 dm_integrity_free_journal_scatterlist(ic, ic->journal_io_scatterlist);
4132 if (ic->sk_requests) {
4133 unsigned i;
4134
4135 for (i = 0; i < ic->journal_sections; i++) {
4136 struct skcipher_request *req = ic->sk_requests[i];
4137 if (req) {
4138 kzfree(req->iv);
4139 skcipher_request_free(req);
4140 }
4141 }
4142 kvfree(ic->sk_requests);
4143 }
4144 kvfree(ic->journal_tree);
4145 if (ic->sb)
4146 free_pages_exact(ic->sb, SB_SECTORS << SECTOR_SHIFT);
4147
4148 if (ic->internal_hash)
4149 crypto_free_shash(ic->internal_hash);
4150 free_alg(&ic->internal_hash_alg);
4151
4152 if (ic->journal_crypt)
4153 crypto_free_skcipher(ic->journal_crypt);
4154 free_alg(&ic->journal_crypt_alg);
4155
4156 if (ic->journal_mac)
4157 crypto_free_shash(ic->journal_mac);
4158 free_alg(&ic->journal_mac_alg);
4159
4160 kfree(ic);
4161}
4162
4163static struct target_type integrity_target = {
4164 .name = "integrity",
Mikulas Patocka468dfca2019-04-29 14:57:24 +02004165 .version = {1, 3, 0},
Mikulas Patocka7eada902017-01-04 20:23:53 +01004166 .module = THIS_MODULE,
4167 .features = DM_TARGET_SINGLETON | DM_TARGET_INTEGRITY,
4168 .ctr = dm_integrity_ctr,
4169 .dtr = dm_integrity_dtr,
4170 .map = dm_integrity_map,
4171 .postsuspend = dm_integrity_postsuspend,
4172 .resume = dm_integrity_resume,
4173 .status = dm_integrity_status,
4174 .iterate_devices = dm_integrity_iterate_devices,
Mikulas Patocka9d609f852017-04-18 16:51:52 -04004175 .io_hints = dm_integrity_io_hints,
Mikulas Patocka7eada902017-01-04 20:23:53 +01004176};
4177
YueHaibing5efedc92019-03-22 22:16:34 +08004178static int __init dm_integrity_init(void)
Mikulas Patocka7eada902017-01-04 20:23:53 +01004179{
4180 int r;
4181
4182 journal_io_cache = kmem_cache_create("integrity_journal_io",
4183 sizeof(struct journal_io), 0, 0, NULL);
4184 if (!journal_io_cache) {
4185 DMERR("can't allocate journal io cache");
4186 return -ENOMEM;
4187 }
4188
4189 r = dm_register_target(&integrity_target);
4190
4191 if (r < 0)
4192 DMERR("register failed %d", r);
4193
4194 return r;
4195}
4196
YueHaibing5efedc92019-03-22 22:16:34 +08004197static void __exit dm_integrity_exit(void)
Mikulas Patocka7eada902017-01-04 20:23:53 +01004198{
4199 dm_unregister_target(&integrity_target);
4200 kmem_cache_destroy(journal_io_cache);
4201}
4202
4203module_init(dm_integrity_init);
4204module_exit(dm_integrity_exit);
4205
4206MODULE_AUTHOR("Milan Broz");
4207MODULE_AUTHOR("Mikulas Patocka");
4208MODULE_DESCRIPTION(DM_NAME " target for integrity tags extension");
4209MODULE_LICENSE("GPL");