blob: 3c10a672322f67fd863e0801e00a6eb8f371fa93 [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
Mike Snitzer248aa262020-02-28 18:11:53 -05009#include "dm-bio-record.h"
10
Mark Rutlandd3e632f2017-10-23 14:07:11 -070011#include <linux/compiler.h>
Mikulas Patocka7eada902017-01-04 20:23:53 +010012#include <linux/module.h>
13#include <linux/device-mapper.h>
14#include <linux/dm-io.h>
15#include <linux/vmalloc.h>
16#include <linux/sort.h>
17#include <linux/rbtree.h>
18#include <linux/delay.h>
19#include <linux/random.h>
Mikulas Patocka1f5a7752019-04-29 14:57:25 +020020#include <linux/reboot.h>
Mikulas Patocka7eada902017-01-04 20:23:53 +010021#include <crypto/hash.h>
22#include <crypto/skcipher.h>
23#include <linux/async_tx.h>
Mikulas Patockaafa53df2018-03-15 16:02:31 -040024#include <linux/dm-bufio.h>
Mikulas Patocka7eada902017-01-04 20:23:53 +010025
26#define DM_MSG_PREFIX "integrity"
27
28#define DEFAULT_INTERLEAVE_SECTORS 32768
29#define DEFAULT_JOURNAL_SIZE_FACTOR 7
Mikulas Patocka468dfca2019-04-29 14:57:24 +020030#define DEFAULT_SECTORS_PER_BITMAP_BIT 32768
Mikulas Patocka7eada902017-01-04 20:23:53 +010031#define DEFAULT_BUFFER_SECTORS 128
32#define DEFAULT_JOURNAL_WATERMARK 50
33#define DEFAULT_SYNC_MSEC 10000
34#define DEFAULT_MAX_JOURNAL_SECTORS 131072
Mikulas Patocka56b67a42017-04-18 16:51:50 -040035#define MIN_LOG2_INTERLEAVE_SECTORS 3
36#define MAX_LOG2_INTERLEAVE_SECTORS 31
Mikulas Patocka7eada902017-01-04 20:23:53 +010037#define METADATA_WORKQUEUE_MAX_ACTIVE 16
Mikulas Patockaa3fcf722018-07-03 20:13:33 +020038#define RECALC_SECTORS 8192
39#define RECALC_WRITE_SUPER 16
Mikulas Patocka468dfca2019-04-29 14:57:24 +020040#define BITMAP_BLOCK_SIZE 4096 /* don't change it */
41#define BITMAP_FLUSH_INTERVAL (10 * HZ)
Mikulas Patocka7eada902017-01-04 20:23:53 +010042
43/*
44 * Warning - DEBUG_PRINT prints security-sensitive data to the log,
45 * so it should not be enabled in the official kernel
46 */
47//#define DEBUG_PRINT
48//#define INTERNAL_VERIFY
49
50/*
51 * On disk structures
52 */
53
54#define SB_MAGIC "integrt"
Mikulas Patocka1f9fc0b2018-07-03 20:13:31 +020055#define SB_VERSION_1 1
56#define SB_VERSION_2 2
Mikulas Patocka468dfca2019-04-29 14:57:24 +020057#define SB_VERSION_3 3
Mikulas Patockad5378582019-11-13 06:48:16 -050058#define SB_VERSION_4 4
Mikulas Patocka7eada902017-01-04 20:23:53 +010059#define SB_SECTORS 8
Mikulas Patocka9d609f852017-04-18 16:51:52 -040060#define MAX_SECTORS_PER_BLOCK 8
Mikulas Patocka7eada902017-01-04 20:23:53 +010061
62struct superblock {
63 __u8 magic[8];
64 __u8 version;
65 __u8 log2_interleave_sectors;
66 __u16 integrity_tag_size;
67 __u32 journal_sections;
68 __u64 provided_data_sectors; /* userspace uses this value */
69 __u32 flags;
Mikulas Patocka9d609f852017-04-18 16:51:52 -040070 __u8 log2_sectors_per_block;
Mikulas Patocka468dfca2019-04-29 14:57:24 +020071 __u8 log2_blocks_per_bitmap_bit;
72 __u8 pad[2];
Mikulas Patockaa3fcf722018-07-03 20:13:33 +020073 __u64 recalc_sector;
Mikulas Patocka7eada902017-01-04 20:23:53 +010074};
75
76#define SB_FLAG_HAVE_JOURNAL_MAC 0x1
Mikulas Patockaa3fcf722018-07-03 20:13:33 +020077#define SB_FLAG_RECALCULATING 0x2
Mikulas Patocka468dfca2019-04-29 14:57:24 +020078#define SB_FLAG_DIRTY_BITMAP 0x4
Mikulas Patockad5378582019-11-13 06:48:16 -050079#define SB_FLAG_FIXED_PADDING 0x8
Mikulas Patocka7eada902017-01-04 20:23:53 +010080
81#define JOURNAL_ENTRY_ROUNDUP 8
82
83typedef __u64 commit_id_t;
84#define JOURNAL_MAC_PER_SECTOR 8
85
86struct journal_entry {
87 union {
88 struct {
89 __u32 sector_lo;
90 __u32 sector_hi;
91 } s;
92 __u64 sector;
93 } u;
Mikulas Patocka9d609f852017-04-18 16:51:52 -040094 commit_id_t last_bytes[0];
95 /* __u8 tag[0]; */
Mikulas Patocka7eada902017-01-04 20:23:53 +010096};
97
Mikulas Patocka9d609f852017-04-18 16:51:52 -040098#define journal_entry_tag(ic, je) ((__u8 *)&(je)->last_bytes[(ic)->sectors_per_block])
99
Mikulas Patocka7eada902017-01-04 20:23:53 +0100100#if BITS_PER_LONG == 64
Mark Rutlandd3e632f2017-10-23 14:07:11 -0700101#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 +0100102#else
Christoph Hellwig72deb452019-04-05 18:08:59 +0200103#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 +0100104#endif
Christoph Hellwig72deb452019-04-05 18:08:59 +0200105#define journal_entry_get_sector(je) le64_to_cpu((je)->u.sector)
Mikulas Patocka7eada902017-01-04 20:23:53 +0100106#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 Patocka7eada902017-01-04 20:23:53 +0100206
207 int failed;
208
209 struct crypto_shash *internal_hash;
210
Mikulas Patockaadc0daa2020-02-24 10:20:28 +0100211 struct dm_target *ti;
212
Mikulas Patocka7eada902017-01-04 20:23:53 +0100213 /* these variables are locked with endio_wait.lock */
214 struct rb_root in_progress;
Mikulas Patocka724376a2018-07-03 20:13:27 +0200215 struct list_head wait_list;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100216 wait_queue_head_t endio_wait;
217 struct workqueue_struct *wait_wq;
Mikulas Patocka53770f02020-02-17 07:43:03 -0500218 struct workqueue_struct *offload_wq;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100219
220 unsigned char commit_seq;
221 commit_id_t commit_ids[N_COMMIT_IDS];
222
223 unsigned committed_section;
224 unsigned n_committed_sections;
225
226 unsigned uncommitted_section;
227 unsigned n_uncommitted_sections;
228
229 unsigned free_section;
230 unsigned char free_section_entry;
231 unsigned free_sectors;
232
233 unsigned free_sectors_threshold;
234
235 struct workqueue_struct *commit_wq;
236 struct work_struct commit_work;
237
238 struct workqueue_struct *writer_wq;
239 struct work_struct writer_work;
240
Mikulas Patockaa3fcf722018-07-03 20:13:33 +0200241 struct workqueue_struct *recalc_wq;
242 struct work_struct recalc_work;
243 u8 *recalc_buffer;
244 u8 *recalc_tags;
245
Mikulas Patocka7eada902017-01-04 20:23:53 +0100246 struct bio_list flush_bio_list;
247
248 unsigned long autocommit_jiffies;
249 struct timer_list autocommit_timer;
250 unsigned autocommit_msec;
251
252 wait_queue_head_t copy_to_journal_wait;
253
254 struct completion crypto_backoff;
255
256 bool journal_uptodate;
257 bool just_formatted;
Mikulas Patocka468dfca2019-04-29 14:57:24 +0200258 bool recalculate_flag;
Mikulas Patockad5378582019-11-13 06:48:16 -0500259 bool fix_padding;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100260
261 struct alg_spec internal_hash_alg;
262 struct alg_spec journal_crypt_alg;
263 struct alg_spec journal_mac_alg;
Mikulas Patocka3f2e5392017-07-21 12:00:00 -0400264
265 atomic64_t number_of_mismatches;
Mikulas Patocka1f5a7752019-04-29 14:57:25 +0200266
267 struct notifier_block reboot_notifier;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100268};
269
270struct dm_integrity_range {
271 sector_t logical_sector;
Mikulas Patocka4f434462019-04-29 14:57:21 +0200272 sector_t n_sectors;
Mikulas Patocka724376a2018-07-03 20:13:27 +0200273 bool waiting;
274 union {
275 struct rb_node node;
276 struct {
277 struct task_struct *task;
278 struct list_head wait_entry;
279 };
280 };
Mikulas Patocka7eada902017-01-04 20:23:53 +0100281};
282
283struct dm_integrity_io {
284 struct work_struct work;
285
286 struct dm_integrity_c *ic;
287 bool write;
288 bool fua;
289
290 struct dm_integrity_range range;
291
292 sector_t metadata_block;
293 unsigned metadata_offset;
294
295 atomic_t in_flight;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200296 blk_status_t bi_status;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100297
298 struct completion *completion;
299
Mike Snitzer248aa262020-02-28 18:11:53 -0500300 struct dm_bio_details bio_details;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100301};
302
303struct journal_completion {
304 struct dm_integrity_c *ic;
305 atomic_t in_flight;
306 struct completion comp;
307};
308
309struct journal_io {
310 struct dm_integrity_range range;
311 struct journal_completion *comp;
312};
313
Mikulas Patocka468dfca2019-04-29 14:57:24 +0200314struct bitmap_block_status {
315 struct work_struct work;
316 struct dm_integrity_c *ic;
317 unsigned idx;
318 unsigned long *bitmap;
319 struct bio_list bio_queue;
320 spinlock_t bio_queue_lock;
321
322};
323
Mikulas Patocka7eada902017-01-04 20:23:53 +0100324static struct kmem_cache *journal_io_cache;
325
326#define JOURNAL_IO_MEMPOOL 32
327
328#ifdef DEBUG_PRINT
329#define DEBUG_print(x, ...) printk(KERN_DEBUG x, ##__VA_ARGS__)
330static void __DEBUG_bytes(__u8 *bytes, size_t len, const char *msg, ...)
331{
332 va_list args;
333 va_start(args, msg);
334 vprintk(msg, args);
335 va_end(args);
336 if (len)
337 pr_cont(":");
338 while (len) {
339 pr_cont(" %02x", *bytes);
340 bytes++;
341 len--;
342 }
343 pr_cont("\n");
344}
345#define DEBUG_bytes(bytes, len, msg, ...) __DEBUG_bytes(bytes, len, KERN_DEBUG msg, ##__VA_ARGS__)
346#else
347#define DEBUG_print(x, ...) do { } while (0)
348#define DEBUG_bytes(bytes, len, msg, ...) do { } while (0)
349#endif
350
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +0300351static void dm_integrity_prepare(struct request *rq)
352{
353}
354
355static void dm_integrity_complete(struct request *rq, unsigned int nr_bytes)
356{
357}
358
Mikulas Patocka7eada902017-01-04 20:23:53 +0100359/*
360 * DM Integrity profile, protection is performed layer above (dm-crypt)
361 */
Bhumika Goyal7c373d662017-08-06 22:54:00 +0530362static const struct blk_integrity_profile dm_integrity_profile = {
Mikulas Patocka7eada902017-01-04 20:23:53 +0100363 .name = "DM-DIF-EXT-TAG",
364 .generate_fn = NULL,
365 .verify_fn = NULL,
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +0300366 .prepare_fn = dm_integrity_prepare,
367 .complete_fn = dm_integrity_complete,
Mikulas Patocka7eada902017-01-04 20:23:53 +0100368};
369
370static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map);
371static void integrity_bio_wait(struct work_struct *w);
372static void dm_integrity_dtr(struct dm_target *ti);
373
374static void dm_integrity_io_error(struct dm_integrity_c *ic, const char *msg, int err)
375{
Mikulas Patocka3f2e5392017-07-21 12:00:00 -0400376 if (err == -EILSEQ)
377 atomic64_inc(&ic->number_of_mismatches);
Mikulas Patocka7eada902017-01-04 20:23:53 +0100378 if (!cmpxchg(&ic->failed, 0, err))
379 DMERR("Error on %s: %d", msg, err);
380}
381
382static int dm_integrity_failed(struct dm_integrity_c *ic)
383{
Mark Rutlandd3e632f2017-10-23 14:07:11 -0700384 return READ_ONCE(ic->failed);
Mikulas Patocka7eada902017-01-04 20:23:53 +0100385}
386
387static commit_id_t dm_integrity_commit_id(struct dm_integrity_c *ic, unsigned i,
388 unsigned j, unsigned char seq)
389{
390 /*
391 * Xor the number with section and sector, so that if a piece of
392 * journal is written at wrong place, it is detected.
393 */
394 return ic->commit_ids[seq] ^ cpu_to_le64(((__u64)i << 32) ^ j);
395}
396
397static void get_area_and_offset(struct dm_integrity_c *ic, sector_t data_sector,
398 sector_t *area, sector_t *offset)
399{
Mikulas Patocka356d9d52018-07-03 20:13:30 +0200400 if (!ic->meta_dev) {
401 __u8 log2_interleave_sectors = ic->sb->log2_interleave_sectors;
402 *area = data_sector >> log2_interleave_sectors;
403 *offset = (unsigned)data_sector & ((1U << log2_interleave_sectors) - 1);
404 } else {
405 *area = 0;
406 *offset = data_sector;
407 }
Mikulas Patocka7eada902017-01-04 20:23:53 +0100408}
409
Mikulas Patocka9d609f852017-04-18 16:51:52 -0400410#define sector_to_block(ic, n) \
411do { \
412 BUG_ON((n) & (unsigned)((ic)->sectors_per_block - 1)); \
413 (n) >>= (ic)->sb->log2_sectors_per_block; \
414} while (0)
415
Mikulas Patocka7eada902017-01-04 20:23:53 +0100416static __u64 get_metadata_sector_and_offset(struct dm_integrity_c *ic, sector_t area,
417 sector_t offset, unsigned *metadata_offset)
418{
419 __u64 ms;
420 unsigned mo;
421
422 ms = area << ic->sb->log2_interleave_sectors;
423 if (likely(ic->log2_metadata_run >= 0))
424 ms += area << ic->log2_metadata_run;
425 else
426 ms += area * ic->metadata_run;
427 ms >>= ic->log2_buffer_sectors;
428
Mikulas Patocka9d609f852017-04-18 16:51:52 -0400429 sector_to_block(ic, offset);
430
Mikulas Patocka7eada902017-01-04 20:23:53 +0100431 if (likely(ic->log2_tag_size >= 0)) {
432 ms += offset >> (SECTOR_SHIFT + ic->log2_buffer_sectors - ic->log2_tag_size);
433 mo = (offset << ic->log2_tag_size) & ((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - 1);
434 } else {
435 ms += (__u64)offset * ic->tag_size >> (SECTOR_SHIFT + ic->log2_buffer_sectors);
436 mo = (offset * ic->tag_size) & ((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - 1);
437 }
438 *metadata_offset = mo;
439 return ms;
440}
441
442static sector_t get_data_sector(struct dm_integrity_c *ic, sector_t area, sector_t offset)
443{
444 sector_t result;
445
Mikulas Patocka356d9d52018-07-03 20:13:30 +0200446 if (ic->meta_dev)
447 return offset;
448
Mikulas Patocka7eada902017-01-04 20:23:53 +0100449 result = area << ic->sb->log2_interleave_sectors;
450 if (likely(ic->log2_metadata_run >= 0))
451 result += (area + 1) << ic->log2_metadata_run;
452 else
453 result += (area + 1) * ic->metadata_run;
454
455 result += (sector_t)ic->initial_sectors + offset;
Mikulas Patocka71e9ddb2018-07-03 20:13:29 +0200456 result += ic->start;
457
Mikulas Patocka7eada902017-01-04 20:23:53 +0100458 return result;
459}
460
461static void wraparound_section(struct dm_integrity_c *ic, unsigned *sec_ptr)
462{
463 if (unlikely(*sec_ptr >= ic->journal_sections))
464 *sec_ptr -= ic->journal_sections;
465}
466
Mikulas Patocka1f9fc0b2018-07-03 20:13:31 +0200467static void sb_set_version(struct dm_integrity_c *ic)
468{
Mikulas Patockad5378582019-11-13 06:48:16 -0500469 if (ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_PADDING))
470 ic->sb->version = SB_VERSION_4;
471 else if (ic->mode == 'B' || ic->sb->flags & cpu_to_le32(SB_FLAG_DIRTY_BITMAP))
Mikulas Patocka468dfca2019-04-29 14:57:24 +0200472 ic->sb->version = SB_VERSION_3;
473 else if (ic->meta_dev || ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))
Mikulas Patocka1f9fc0b2018-07-03 20:13:31 +0200474 ic->sb->version = SB_VERSION_2;
475 else
476 ic->sb->version = SB_VERSION_1;
477}
478
Mikulas Patocka7eada902017-01-04 20:23:53 +0100479static int sync_rw_sb(struct dm_integrity_c *ic, int op, int op_flags)
480{
481 struct dm_io_request io_req;
482 struct dm_io_region io_loc;
483
484 io_req.bi_op = op;
485 io_req.bi_op_flags = op_flags;
486 io_req.mem.type = DM_IO_KMEM;
487 io_req.mem.ptr.addr = ic->sb;
488 io_req.notify.fn = NULL;
489 io_req.client = ic->io;
Mikulas Patocka356d9d52018-07-03 20:13:30 +0200490 io_loc.bdev = ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100491 io_loc.sector = ic->start;
492 io_loc.count = SB_SECTORS;
493
Milan Broz5f1c56b2019-05-22 13:29:44 +0200494 if (op == REQ_OP_WRITE)
495 sb_set_version(ic);
496
Mikulas Patocka7eada902017-01-04 20:23:53 +0100497 return dm_io(&io_req, 1, &io_loc, NULL);
498}
499
Mikulas Patocka468dfca2019-04-29 14:57:24 +0200500#define BITMAP_OP_TEST_ALL_SET 0
501#define BITMAP_OP_TEST_ALL_CLEAR 1
502#define BITMAP_OP_SET 2
503#define BITMAP_OP_CLEAR 3
504
Mike Snitzer05d69092019-05-09 15:25:49 -0400505static bool block_bitmap_op(struct dm_integrity_c *ic, struct page_list *bitmap,
506 sector_t sector, sector_t n_sectors, int mode)
Mikulas Patocka468dfca2019-04-29 14:57:24 +0200507{
508 unsigned long bit, end_bit, this_end_bit, page, end_page;
509 unsigned long *data;
510
511 if (unlikely(((sector | n_sectors) & ((1 << ic->sb->log2_sectors_per_block) - 1)) != 0)) {
Mike Snitzer05d69092019-05-09 15:25:49 -0400512 DMCRIT("invalid bitmap access (%llx,%llx,%d,%d,%d)",
Mikulas Patocka76491942020-03-22 20:42:22 +0100513 sector,
514 n_sectors,
Mikulas Patocka468dfca2019-04-29 14:57:24 +0200515 ic->sb->log2_sectors_per_block,
516 ic->log2_blocks_per_bitmap_bit,
517 mode);
518 BUG();
519 }
520
521 if (unlikely(!n_sectors))
522 return true;
523
524 bit = sector >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
Mike Snitzer05d69092019-05-09 15:25:49 -0400525 end_bit = (sector + n_sectors - 1) >>
526 (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
Mikulas Patocka468dfca2019-04-29 14:57:24 +0200527
528 page = bit / (PAGE_SIZE * 8);
529 bit %= PAGE_SIZE * 8;
530
531 end_page = end_bit / (PAGE_SIZE * 8);
532 end_bit %= PAGE_SIZE * 8;
533
534repeat:
535 if (page < end_page) {
536 this_end_bit = PAGE_SIZE * 8 - 1;
537 } else {
538 this_end_bit = end_bit;
539 }
540
541 data = lowmem_page_address(bitmap[page].page);
542
543 if (mode == BITMAP_OP_TEST_ALL_SET) {
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] != -1)
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_TEST_ALL_CLEAR) {
558 while (bit <= this_end_bit) {
559 if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
560 do {
561 if (data[bit / BITS_PER_LONG] != 0)
562 return false;
563 bit += BITS_PER_LONG;
564 } while (this_end_bit >= bit + BITS_PER_LONG - 1);
565 continue;
566 }
567 if (test_bit(bit, data))
568 return false;
569 bit++;
570 }
571 } else if (mode == BITMAP_OP_SET) {
572 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] = -1;
576 bit += BITS_PER_LONG;
577 } while (this_end_bit >= bit + BITS_PER_LONG - 1);
578 continue;
579 }
580 __set_bit(bit, data);
581 bit++;
582 }
583 } else if (mode == BITMAP_OP_CLEAR) {
584 if (!bit && this_end_bit == PAGE_SIZE * 8 - 1)
585 clear_page(data);
586 else while (bit <= this_end_bit) {
587 if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
588 do {
589 data[bit / BITS_PER_LONG] = 0;
590 bit += BITS_PER_LONG;
591 } while (this_end_bit >= bit + BITS_PER_LONG - 1);
592 continue;
593 }
594 __clear_bit(bit, data);
595 bit++;
596 }
597 } else {
598 BUG();
599 }
600
601 if (unlikely(page < end_page)) {
602 bit = 0;
603 page++;
604 goto repeat;
605 }
606
607 return true;
608}
609
610static void block_bitmap_copy(struct dm_integrity_c *ic, struct page_list *dst, struct page_list *src)
611{
612 unsigned n_bitmap_pages = DIV_ROUND_UP(ic->n_bitmap_blocks, PAGE_SIZE / BITMAP_BLOCK_SIZE);
613 unsigned i;
614
615 for (i = 0; i < n_bitmap_pages; i++) {
616 unsigned long *dst_data = lowmem_page_address(dst[i].page);
617 unsigned long *src_data = lowmem_page_address(src[i].page);
618 copy_page(dst_data, src_data);
619 }
620}
621
622static struct bitmap_block_status *sector_to_bitmap_block(struct dm_integrity_c *ic, sector_t sector)
623{
624 unsigned bit = sector >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
625 unsigned bitmap_block = bit / (BITMAP_BLOCK_SIZE * 8);
626
627 BUG_ON(bitmap_block >= ic->n_bitmap_blocks);
628 return &ic->bbs[bitmap_block];
629}
630
Mikulas Patocka7eada902017-01-04 20:23:53 +0100631static void access_journal_check(struct dm_integrity_c *ic, unsigned section, unsigned offset,
632 bool e, const char *function)
633{
634#if defined(CONFIG_DM_DEBUG) || defined(INTERNAL_VERIFY)
635 unsigned limit = e ? ic->journal_section_entries : ic->journal_section_sectors;
636
637 if (unlikely(section >= ic->journal_sections) ||
638 unlikely(offset >= limit)) {
Mike Snitzer05d69092019-05-09 15:25:49 -0400639 DMCRIT("%s: invalid access at (%u,%u), limit (%u,%u)",
640 function, section, offset, ic->journal_sections, limit);
Mikulas Patocka7eada902017-01-04 20:23:53 +0100641 BUG();
642 }
643#endif
644}
645
646static void page_list_location(struct dm_integrity_c *ic, unsigned section, unsigned offset,
647 unsigned *pl_index, unsigned *pl_offset)
648{
649 unsigned sector;
650
Mikulas Patocka56b67a42017-04-18 16:51:50 -0400651 access_journal_check(ic, section, offset, false, "page_list_location");
Mikulas Patocka7eada902017-01-04 20:23:53 +0100652
653 sector = section * ic->journal_section_sectors + offset;
654
655 *pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
656 *pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
657}
658
659static struct journal_sector *access_page_list(struct dm_integrity_c *ic, struct page_list *pl,
660 unsigned section, unsigned offset, unsigned *n_sectors)
661{
662 unsigned pl_index, pl_offset;
663 char *va;
664
665 page_list_location(ic, section, offset, &pl_index, &pl_offset);
666
667 if (n_sectors)
668 *n_sectors = (PAGE_SIZE - pl_offset) >> SECTOR_SHIFT;
669
670 va = lowmem_page_address(pl[pl_index].page);
671
672 return (struct journal_sector *)(va + pl_offset);
673}
674
675static struct journal_sector *access_journal(struct dm_integrity_c *ic, unsigned section, unsigned offset)
676{
677 return access_page_list(ic, ic->journal, section, offset, NULL);
678}
679
680static struct journal_entry *access_journal_entry(struct dm_integrity_c *ic, unsigned section, unsigned n)
681{
682 unsigned rel_sector, offset;
683 struct journal_sector *js;
684
685 access_journal_check(ic, section, n, true, "access_journal_entry");
686
687 rel_sector = n % JOURNAL_BLOCK_SECTORS;
688 offset = n / JOURNAL_BLOCK_SECTORS;
689
690 js = access_journal(ic, section, rel_sector);
691 return (struct journal_entry *)((char *)js + offset * ic->journal_entry_size);
692}
693
694static struct journal_sector *access_journal_data(struct dm_integrity_c *ic, unsigned section, unsigned n)
695{
Mikulas Patocka9d609f852017-04-18 16:51:52 -0400696 n <<= ic->sb->log2_sectors_per_block;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100697
Mikulas Patocka9d609f852017-04-18 16:51:52 -0400698 n += JOURNAL_BLOCK_SECTORS;
699
700 access_journal_check(ic, section, n, false, "access_journal_data");
701
702 return access_journal(ic, section, n);
Mikulas Patocka7eada902017-01-04 20:23:53 +0100703}
704
705static void section_mac(struct dm_integrity_c *ic, unsigned section, __u8 result[JOURNAL_MAC_SIZE])
706{
707 SHASH_DESC_ON_STACK(desc, ic->journal_mac);
708 int r;
709 unsigned j, size;
710
711 desc->tfm = ic->journal_mac;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100712
713 r = crypto_shash_init(desc);
714 if (unlikely(r)) {
715 dm_integrity_io_error(ic, "crypto_shash_init", r);
716 goto err;
717 }
718
719 for (j = 0; j < ic->journal_section_entries; j++) {
720 struct journal_entry *je = access_journal_entry(ic, section, j);
721 r = crypto_shash_update(desc, (__u8 *)&je->u.sector, sizeof je->u.sector);
722 if (unlikely(r)) {
723 dm_integrity_io_error(ic, "crypto_shash_update", r);
724 goto err;
725 }
726 }
727
728 size = crypto_shash_digestsize(ic->journal_mac);
729
730 if (likely(size <= JOURNAL_MAC_SIZE)) {
731 r = crypto_shash_final(desc, result);
732 if (unlikely(r)) {
733 dm_integrity_io_error(ic, "crypto_shash_final", r);
734 goto err;
735 }
736 memset(result + size, 0, JOURNAL_MAC_SIZE - size);
737 } else {
Kees Cook6d39a122018-08-07 14:18:39 -0700738 __u8 digest[HASH_MAX_DIGESTSIZE];
739
740 if (WARN_ON(size > sizeof(digest))) {
741 dm_integrity_io_error(ic, "digest_size", -EINVAL);
742 goto err;
743 }
Mikulas Patocka7eada902017-01-04 20:23:53 +0100744 r = crypto_shash_final(desc, digest);
745 if (unlikely(r)) {
746 dm_integrity_io_error(ic, "crypto_shash_final", r);
747 goto err;
748 }
749 memcpy(result, digest, JOURNAL_MAC_SIZE);
750 }
751
752 return;
753err:
754 memset(result, 0, JOURNAL_MAC_SIZE);
755}
756
757static void rw_section_mac(struct dm_integrity_c *ic, unsigned section, bool wr)
758{
759 __u8 result[JOURNAL_MAC_SIZE];
760 unsigned j;
761
762 if (!ic->journal_mac)
763 return;
764
765 section_mac(ic, section, result);
766
767 for (j = 0; j < JOURNAL_BLOCK_SECTORS; j++) {
768 struct journal_sector *js = access_journal(ic, section, j);
769
770 if (likely(wr))
771 memcpy(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR);
772 else {
773 if (memcmp(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR))
774 dm_integrity_io_error(ic, "journal mac", -EILSEQ);
775 }
776 }
777}
778
779static void complete_journal_op(void *context)
780{
781 struct journal_completion *comp = context;
782 BUG_ON(!atomic_read(&comp->in_flight));
783 if (likely(atomic_dec_and_test(&comp->in_flight)))
784 complete(&comp->comp);
785}
786
787static void xor_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
788 unsigned n_sections, struct journal_completion *comp)
789{
790 struct async_submit_ctl submit;
791 size_t n_bytes = (size_t)(n_sections * ic->journal_section_sectors) << SECTOR_SHIFT;
792 unsigned pl_index, pl_offset, section_index;
793 struct page_list *source_pl, *target_pl;
794
795 if (likely(encrypt)) {
796 source_pl = ic->journal;
797 target_pl = ic->journal_io;
798 } else {
799 source_pl = ic->journal_io;
800 target_pl = ic->journal;
801 }
802
803 page_list_location(ic, section, 0, &pl_index, &pl_offset);
804
805 atomic_add(roundup(pl_offset + n_bytes, PAGE_SIZE) >> PAGE_SHIFT, &comp->in_flight);
806
807 init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL, complete_journal_op, comp, NULL);
808
809 section_index = pl_index;
810
811 do {
812 size_t this_step;
813 struct page *src_pages[2];
814 struct page *dst_page;
815
816 while (unlikely(pl_index == section_index)) {
817 unsigned dummy;
818 if (likely(encrypt))
819 rw_section_mac(ic, section, true);
820 section++;
821 n_sections--;
822 if (!n_sections)
823 break;
824 page_list_location(ic, section, 0, &section_index, &dummy);
825 }
826
827 this_step = min(n_bytes, (size_t)PAGE_SIZE - pl_offset);
828 dst_page = target_pl[pl_index].page;
829 src_pages[0] = source_pl[pl_index].page;
830 src_pages[1] = ic->journal_xor[pl_index].page;
831
832 async_xor(dst_page, src_pages, pl_offset, 2, this_step, &submit);
833
834 pl_index++;
835 pl_offset = 0;
836 n_bytes -= this_step;
837 } while (n_bytes);
838
839 BUG_ON(n_sections);
840
841 async_tx_issue_pending_all();
842}
843
844static void complete_journal_encrypt(struct crypto_async_request *req, int err)
845{
846 struct journal_completion *comp = req->data;
847 if (unlikely(err)) {
848 if (likely(err == -EINPROGRESS)) {
849 complete(&comp->ic->crypto_backoff);
850 return;
851 }
852 dm_integrity_io_error(comp->ic, "asynchronous encrypt", err);
853 }
854 complete_journal_op(comp);
855}
856
857static bool do_crypt(bool encrypt, struct skcipher_request *req, struct journal_completion *comp)
858{
859 int r;
Mikulas Patocka432061b2018-09-05 09:17:45 -0400860 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Mikulas Patocka7eada902017-01-04 20:23:53 +0100861 complete_journal_encrypt, comp);
862 if (likely(encrypt))
863 r = crypto_skcipher_encrypt(req);
864 else
865 r = crypto_skcipher_decrypt(req);
866 if (likely(!r))
867 return false;
868 if (likely(r == -EINPROGRESS))
869 return true;
870 if (likely(r == -EBUSY)) {
871 wait_for_completion(&comp->ic->crypto_backoff);
872 reinit_completion(&comp->ic->crypto_backoff);
873 return true;
874 }
875 dm_integrity_io_error(comp->ic, "encrypt", r);
876 return false;
877}
878
879static void crypt_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
880 unsigned n_sections, struct journal_completion *comp)
881{
882 struct scatterlist **source_sg;
883 struct scatterlist **target_sg;
884
885 atomic_add(2, &comp->in_flight);
886
887 if (likely(encrypt)) {
888 source_sg = ic->journal_scatterlist;
889 target_sg = ic->journal_io_scatterlist;
890 } else {
891 source_sg = ic->journal_io_scatterlist;
892 target_sg = ic->journal_scatterlist;
893 }
894
895 do {
896 struct skcipher_request *req;
897 unsigned ivsize;
898 char *iv;
899
900 if (likely(encrypt))
901 rw_section_mac(ic, section, true);
902
903 req = ic->sk_requests[section];
904 ivsize = crypto_skcipher_ivsize(ic->journal_crypt);
905 iv = req->iv;
906
907 memcpy(iv, iv + ivsize, ivsize);
908
909 req->src = source_sg[section];
910 req->dst = target_sg[section];
911
912 if (unlikely(do_crypt(encrypt, req, comp)))
913 atomic_inc(&comp->in_flight);
914
915 section++;
916 n_sections--;
917 } while (n_sections);
918
919 atomic_dec(&comp->in_flight);
920 complete_journal_op(comp);
921}
922
923static void encrypt_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
924 unsigned n_sections, struct journal_completion *comp)
925{
926 if (ic->journal_xor)
927 return xor_journal(ic, encrypt, section, n_sections, comp);
928 else
929 return crypt_journal(ic, encrypt, section, n_sections, comp);
930}
931
932static void complete_journal_io(unsigned long error, void *context)
933{
934 struct journal_completion *comp = context;
935 if (unlikely(error != 0))
936 dm_integrity_io_error(comp->ic, "writing journal", -EIO);
937 complete_journal_op(comp);
938}
939
Mikulas Patocka981e8a92019-04-29 14:57:19 +0200940static void rw_journal_sectors(struct dm_integrity_c *ic, int op, int op_flags,
941 unsigned sector, unsigned n_sectors, struct journal_completion *comp)
Mikulas Patocka7eada902017-01-04 20:23:53 +0100942{
943 struct dm_io_request io_req;
944 struct dm_io_region io_loc;
Mikulas Patocka981e8a92019-04-29 14:57:19 +0200945 unsigned pl_index, pl_offset;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100946 int r;
947
948 if (unlikely(dm_integrity_failed(ic))) {
949 if (comp)
950 complete_journal_io(-1UL, comp);
951 return;
952 }
953
Mikulas Patocka7eada902017-01-04 20:23:53 +0100954 pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
955 pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
956
957 io_req.bi_op = op;
958 io_req.bi_op_flags = op_flags;
959 io_req.mem.type = DM_IO_PAGE_LIST;
960 if (ic->journal_io)
961 io_req.mem.ptr.pl = &ic->journal_io[pl_index];
962 else
963 io_req.mem.ptr.pl = &ic->journal[pl_index];
964 io_req.mem.offset = pl_offset;
965 if (likely(comp != NULL)) {
966 io_req.notify.fn = complete_journal_io;
967 io_req.notify.context = comp;
968 } else {
969 io_req.notify.fn = NULL;
970 }
971 io_req.client = ic->io;
Mikulas Patocka356d9d52018-07-03 20:13:30 +0200972 io_loc.bdev = ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev;
Mikulas Patocka7eada902017-01-04 20:23:53 +0100973 io_loc.sector = ic->start + SB_SECTORS + sector;
974 io_loc.count = n_sectors;
975
976 r = dm_io(&io_req, 1, &io_loc, NULL);
977 if (unlikely(r)) {
978 dm_integrity_io_error(ic, op == REQ_OP_READ ? "reading journal" : "writing journal", r);
979 if (comp) {
980 WARN_ONCE(1, "asynchronous dm_io failed: %d", r);
981 complete_journal_io(-1UL, comp);
982 }
983 }
984}
985
Mikulas Patocka981e8a92019-04-29 14:57:19 +0200986static void rw_journal(struct dm_integrity_c *ic, int op, int op_flags, unsigned section,
987 unsigned n_sections, struct journal_completion *comp)
988{
989 unsigned sector, n_sectors;
990
991 sector = section * ic->journal_section_sectors;
992 n_sectors = n_sections * ic->journal_section_sectors;
993
994 rw_journal_sectors(ic, op, op_flags, sector, n_sectors, comp);
995}
996
Mikulas Patocka7eada902017-01-04 20:23:53 +0100997static void write_journal(struct dm_integrity_c *ic, unsigned commit_start, unsigned commit_sections)
998{
999 struct journal_completion io_comp;
1000 struct journal_completion crypt_comp_1;
1001 struct journal_completion crypt_comp_2;
1002 unsigned i;
1003
1004 io_comp.ic = ic;
Arnd Bergmannb5e8ad92017-08-15 17:11:59 +02001005 init_completion(&io_comp.comp);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001006
1007 if (commit_start + commit_sections <= ic->journal_sections) {
1008 io_comp.in_flight = (atomic_t)ATOMIC_INIT(1);
1009 if (ic->journal_io) {
1010 crypt_comp_1.ic = ic;
Arnd Bergmannb5e8ad92017-08-15 17:11:59 +02001011 init_completion(&crypt_comp_1.comp);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001012 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
1013 encrypt_journal(ic, true, commit_start, commit_sections, &crypt_comp_1);
1014 wait_for_completion_io(&crypt_comp_1.comp);
1015 } else {
1016 for (i = 0; i < commit_sections; i++)
1017 rw_section_mac(ic, commit_start + i, true);
1018 }
Jan Karaff0361b2017-05-31 09:44:32 +02001019 rw_journal(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, commit_start,
1020 commit_sections, &io_comp);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001021 } else {
1022 unsigned to_end;
1023 io_comp.in_flight = (atomic_t)ATOMIC_INIT(2);
1024 to_end = ic->journal_sections - commit_start;
1025 if (ic->journal_io) {
1026 crypt_comp_1.ic = ic;
Arnd Bergmannb5e8ad92017-08-15 17:11:59 +02001027 init_completion(&crypt_comp_1.comp);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001028 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
1029 encrypt_journal(ic, true, commit_start, to_end, &crypt_comp_1);
1030 if (try_wait_for_completion(&crypt_comp_1.comp)) {
1031 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
Arnd Bergmannb5e8ad92017-08-15 17:11:59 +02001032 reinit_completion(&crypt_comp_1.comp);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001033 crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
1034 encrypt_journal(ic, true, 0, commit_sections - to_end, &crypt_comp_1);
1035 wait_for_completion_io(&crypt_comp_1.comp);
1036 } else {
1037 crypt_comp_2.ic = ic;
Arnd Bergmannb5e8ad92017-08-15 17:11:59 +02001038 init_completion(&crypt_comp_2.comp);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001039 crypt_comp_2.in_flight = (atomic_t)ATOMIC_INIT(0);
1040 encrypt_journal(ic, true, 0, commit_sections - to_end, &crypt_comp_2);
1041 wait_for_completion_io(&crypt_comp_1.comp);
1042 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
1043 wait_for_completion_io(&crypt_comp_2.comp);
1044 }
1045 } else {
1046 for (i = 0; i < to_end; i++)
1047 rw_section_mac(ic, commit_start + i, true);
1048 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
1049 for (i = 0; i < commit_sections - to_end; i++)
1050 rw_section_mac(ic, i, true);
1051 }
1052 rw_journal(ic, REQ_OP_WRITE, REQ_FUA, 0, commit_sections - to_end, &io_comp);
1053 }
1054
1055 wait_for_completion_io(&io_comp.comp);
1056}
1057
1058static void copy_from_journal(struct dm_integrity_c *ic, unsigned section, unsigned offset,
1059 unsigned n_sectors, sector_t target, io_notify_fn fn, void *data)
1060{
1061 struct dm_io_request io_req;
1062 struct dm_io_region io_loc;
1063 int r;
1064 unsigned sector, pl_index, pl_offset;
1065
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001066 BUG_ON((target | n_sectors | offset) & (unsigned)(ic->sectors_per_block - 1));
1067
Mikulas Patocka7eada902017-01-04 20:23:53 +01001068 if (unlikely(dm_integrity_failed(ic))) {
1069 fn(-1UL, data);
1070 return;
1071 }
1072
1073 sector = section * ic->journal_section_sectors + JOURNAL_BLOCK_SECTORS + offset;
1074
1075 pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
1076 pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
1077
1078 io_req.bi_op = REQ_OP_WRITE;
1079 io_req.bi_op_flags = 0;
1080 io_req.mem.type = DM_IO_PAGE_LIST;
1081 io_req.mem.ptr.pl = &ic->journal[pl_index];
1082 io_req.mem.offset = pl_offset;
1083 io_req.notify.fn = fn;
1084 io_req.notify.context = data;
1085 io_req.client = ic->io;
1086 io_loc.bdev = ic->dev->bdev;
Mikulas Patocka71e9ddb2018-07-03 20:13:29 +02001087 io_loc.sector = target;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001088 io_loc.count = n_sectors;
1089
1090 r = dm_io(&io_req, 1, &io_loc, NULL);
1091 if (unlikely(r)) {
1092 WARN_ONCE(1, "asynchronous dm_io failed: %d", r);
1093 fn(-1UL, data);
1094 }
1095}
1096
Mikulas Patocka724376a2018-07-03 20:13:27 +02001097static bool ranges_overlap(struct dm_integrity_range *range1, struct dm_integrity_range *range2)
1098{
1099 return range1->logical_sector < range2->logical_sector + range2->n_sectors &&
Mikulas Patocka4ed319c2019-04-05 15:26:39 -04001100 range1->logical_sector + range1->n_sectors > range2->logical_sector;
Mikulas Patocka724376a2018-07-03 20:13:27 +02001101}
1102
1103static 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 +01001104{
1105 struct rb_node **n = &ic->in_progress.rb_node;
1106 struct rb_node *parent;
1107
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001108 BUG_ON((new_range->logical_sector | new_range->n_sectors) & (unsigned)(ic->sectors_per_block - 1));
1109
Mikulas Patocka724376a2018-07-03 20:13:27 +02001110 if (likely(check_waiting)) {
1111 struct dm_integrity_range *range;
1112 list_for_each_entry(range, &ic->wait_list, wait_entry) {
1113 if (unlikely(ranges_overlap(range, new_range)))
1114 return false;
1115 }
1116 }
1117
Mikulas Patocka7eada902017-01-04 20:23:53 +01001118 parent = NULL;
1119
1120 while (*n) {
1121 struct dm_integrity_range *range = container_of(*n, struct dm_integrity_range, node);
1122
1123 parent = *n;
1124 if (new_range->logical_sector + new_range->n_sectors <= range->logical_sector) {
1125 n = &range->node.rb_left;
1126 } else if (new_range->logical_sector >= range->logical_sector + range->n_sectors) {
1127 n = &range->node.rb_right;
1128 } else {
1129 return false;
1130 }
1131 }
1132
1133 rb_link_node(&new_range->node, parent, n);
1134 rb_insert_color(&new_range->node, &ic->in_progress);
1135
1136 return true;
1137}
1138
1139static void remove_range_unlocked(struct dm_integrity_c *ic, struct dm_integrity_range *range)
1140{
1141 rb_erase(&range->node, &ic->in_progress);
Mikulas Patocka724376a2018-07-03 20:13:27 +02001142 while (unlikely(!list_empty(&ic->wait_list))) {
1143 struct dm_integrity_range *last_range =
1144 list_first_entry(&ic->wait_list, struct dm_integrity_range, wait_entry);
1145 struct task_struct *last_range_task;
Mikulas Patocka724376a2018-07-03 20:13:27 +02001146 last_range_task = last_range->task;
1147 list_del(&last_range->wait_entry);
1148 if (!add_new_range(ic, last_range, false)) {
1149 last_range->task = last_range_task;
1150 list_add(&last_range->wait_entry, &ic->wait_list);
1151 break;
1152 }
1153 last_range->waiting = false;
1154 wake_up_process(last_range_task);
1155 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01001156}
1157
1158static void remove_range(struct dm_integrity_c *ic, struct dm_integrity_range *range)
1159{
1160 unsigned long flags;
1161
1162 spin_lock_irqsave(&ic->endio_wait.lock, flags);
1163 remove_range_unlocked(ic, range);
1164 spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
1165}
1166
Mikulas Patocka724376a2018-07-03 20:13:27 +02001167static void wait_and_add_new_range(struct dm_integrity_c *ic, struct dm_integrity_range *new_range)
1168{
1169 new_range->waiting = true;
1170 list_add_tail(&new_range->wait_entry, &ic->wait_list);
1171 new_range->task = current;
1172 do {
1173 __set_current_state(TASK_UNINTERRUPTIBLE);
1174 spin_unlock_irq(&ic->endio_wait.lock);
1175 io_schedule();
1176 spin_lock_irq(&ic->endio_wait.lock);
1177 } while (unlikely(new_range->waiting));
1178}
1179
Mikulas Patocka8b3bbd42019-04-29 14:57:22 +02001180static void add_new_range_and_wait(struct dm_integrity_c *ic, struct dm_integrity_range *new_range)
1181{
1182 if (unlikely(!add_new_range(ic, new_range, true)))
1183 wait_and_add_new_range(ic, new_range);
1184}
1185
Mikulas Patocka7eada902017-01-04 20:23:53 +01001186static void init_journal_node(struct journal_node *node)
1187{
1188 RB_CLEAR_NODE(&node->node);
1189 node->sector = (sector_t)-1;
1190}
1191
1192static void add_journal_node(struct dm_integrity_c *ic, struct journal_node *node, sector_t sector)
1193{
1194 struct rb_node **link;
1195 struct rb_node *parent;
1196
1197 node->sector = sector;
1198 BUG_ON(!RB_EMPTY_NODE(&node->node));
1199
1200 link = &ic->journal_tree_root.rb_node;
1201 parent = NULL;
1202
1203 while (*link) {
1204 struct journal_node *j;
1205 parent = *link;
1206 j = container_of(parent, struct journal_node, node);
1207 if (sector < j->sector)
1208 link = &j->node.rb_left;
1209 else
1210 link = &j->node.rb_right;
1211 }
1212
1213 rb_link_node(&node->node, parent, link);
1214 rb_insert_color(&node->node, &ic->journal_tree_root);
1215}
1216
1217static void remove_journal_node(struct dm_integrity_c *ic, struct journal_node *node)
1218{
1219 BUG_ON(RB_EMPTY_NODE(&node->node));
1220 rb_erase(&node->node, &ic->journal_tree_root);
1221 init_journal_node(node);
1222}
1223
1224#define NOT_FOUND (-1U)
1225
1226static unsigned find_journal_node(struct dm_integrity_c *ic, sector_t sector, sector_t *next_sector)
1227{
1228 struct rb_node *n = ic->journal_tree_root.rb_node;
1229 unsigned found = NOT_FOUND;
1230 *next_sector = (sector_t)-1;
1231 while (n) {
1232 struct journal_node *j = container_of(n, struct journal_node, node);
1233 if (sector == j->sector) {
1234 found = j - ic->journal_tree;
1235 }
1236 if (sector < j->sector) {
1237 *next_sector = j->sector;
1238 n = j->node.rb_left;
1239 } else {
1240 n = j->node.rb_right;
1241 }
1242 }
1243
1244 return found;
1245}
1246
1247static bool test_journal_node(struct dm_integrity_c *ic, unsigned pos, sector_t sector)
1248{
1249 struct journal_node *node, *next_node;
1250 struct rb_node *next;
1251
1252 if (unlikely(pos >= ic->journal_entries))
1253 return false;
1254 node = &ic->journal_tree[pos];
1255 if (unlikely(RB_EMPTY_NODE(&node->node)))
1256 return false;
1257 if (unlikely(node->sector != sector))
1258 return false;
1259
1260 next = rb_next(&node->node);
1261 if (unlikely(!next))
1262 return true;
1263
1264 next_node = container_of(next, struct journal_node, node);
1265 return next_node->sector != sector;
1266}
1267
1268static bool find_newer_committed_node(struct dm_integrity_c *ic, struct journal_node *node)
1269{
1270 struct rb_node *next;
1271 struct journal_node *next_node;
1272 unsigned next_section;
1273
1274 BUG_ON(RB_EMPTY_NODE(&node->node));
1275
1276 next = rb_next(&node->node);
1277 if (unlikely(!next))
1278 return false;
1279
1280 next_node = container_of(next, struct journal_node, node);
1281
1282 if (next_node->sector != node->sector)
1283 return false;
1284
1285 next_section = (unsigned)(next_node - ic->journal_tree) / ic->journal_section_entries;
1286 if (next_section >= ic->committed_section &&
1287 next_section < ic->committed_section + ic->n_committed_sections)
1288 return true;
1289 if (next_section + ic->journal_sections < ic->committed_section + ic->n_committed_sections)
1290 return true;
1291
1292 return false;
1293}
1294
1295#define TAG_READ 0
1296#define TAG_WRITE 1
1297#define TAG_CMP 2
1298
1299static int dm_integrity_rw_tag(struct dm_integrity_c *ic, unsigned char *tag, sector_t *metadata_block,
1300 unsigned *metadata_offset, unsigned total_size, int op)
1301{
1302 do {
1303 unsigned char *data, *dp;
1304 struct dm_buffer *b;
1305 unsigned to_copy;
1306 int r;
1307
1308 r = dm_integrity_failed(ic);
1309 if (unlikely(r))
1310 return r;
1311
1312 data = dm_bufio_read(ic->bufio, *metadata_block, &b);
Chengguang Xu5e3d0e32019-02-13 13:46:56 +08001313 if (IS_ERR(data))
Mikulas Patocka7eada902017-01-04 20:23:53 +01001314 return PTR_ERR(data);
1315
1316 to_copy = min((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - *metadata_offset, total_size);
1317 dp = data + *metadata_offset;
1318 if (op == TAG_READ) {
1319 memcpy(tag, dp, to_copy);
1320 } else if (op == TAG_WRITE) {
1321 memcpy(dp, tag, to_copy);
Mikulas Patocka1e3b21c2017-04-30 17:31:22 -04001322 dm_bufio_mark_partial_buffer_dirty(b, *metadata_offset, *metadata_offset + to_copy);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001323 } else {
1324 /* e.g.: op == TAG_CMP */
1325 if (unlikely(memcmp(dp, tag, to_copy))) {
1326 unsigned i;
1327
1328 for (i = 0; i < to_copy; i++) {
1329 if (dp[i] != tag[i])
1330 break;
1331 total_size--;
1332 }
1333 dm_bufio_release(b);
1334 return total_size;
1335 }
1336 }
1337 dm_bufio_release(b);
1338
1339 tag += to_copy;
1340 *metadata_offset += to_copy;
1341 if (unlikely(*metadata_offset == 1U << SECTOR_SHIFT << ic->log2_buffer_sectors)) {
1342 (*metadata_block)++;
1343 *metadata_offset = 0;
1344 }
1345 total_size -= to_copy;
1346 } while (unlikely(total_size));
1347
1348 return 0;
1349}
1350
1351static void dm_integrity_flush_buffers(struct dm_integrity_c *ic)
1352{
1353 int r;
1354 r = dm_bufio_write_dirty_buffers(ic->bufio);
1355 if (unlikely(r))
1356 dm_integrity_io_error(ic, "writing tags", r);
1357}
1358
1359static void sleep_on_endio_wait(struct dm_integrity_c *ic)
1360{
1361 DECLARE_WAITQUEUE(wait, current);
1362 __add_wait_queue(&ic->endio_wait, &wait);
1363 __set_current_state(TASK_UNINTERRUPTIBLE);
1364 spin_unlock_irq(&ic->endio_wait.lock);
1365 io_schedule();
1366 spin_lock_irq(&ic->endio_wait.lock);
1367 __remove_wait_queue(&ic->endio_wait, &wait);
1368}
1369
Kees Cook8376d3c2017-10-16 17:01:48 -07001370static void autocommit_fn(struct timer_list *t)
Mikulas Patocka7eada902017-01-04 20:23:53 +01001371{
Kees Cook8376d3c2017-10-16 17:01:48 -07001372 struct dm_integrity_c *ic = from_timer(ic, t, autocommit_timer);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001373
1374 if (likely(!dm_integrity_failed(ic)))
1375 queue_work(ic->commit_wq, &ic->commit_work);
1376}
1377
1378static void schedule_autocommit(struct dm_integrity_c *ic)
1379{
1380 if (!timer_pending(&ic->autocommit_timer))
1381 mod_timer(&ic->autocommit_timer, jiffies + ic->autocommit_jiffies);
1382}
1383
1384static void submit_flush_bio(struct dm_integrity_c *ic, struct dm_integrity_io *dio)
1385{
1386 struct bio *bio;
Mike Snitzer7def52b2017-06-19 10:55:47 -04001387 unsigned long flags;
1388
1389 spin_lock_irqsave(&ic->endio_wait.lock, flags);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001390 bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1391 bio_list_add(&ic->flush_bio_list, bio);
Mike Snitzer7def52b2017-06-19 10:55:47 -04001392 spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
1393
Mikulas Patocka7eada902017-01-04 20:23:53 +01001394 queue_work(ic->commit_wq, &ic->commit_work);
1395}
1396
1397static void do_endio(struct dm_integrity_c *ic, struct bio *bio)
1398{
1399 int r = dm_integrity_failed(ic);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001400 if (unlikely(r) && !bio->bi_status)
1401 bio->bi_status = errno_to_blk_status(r);
Mikulas Patocka48271492019-04-29 14:57:26 +02001402 if (unlikely(ic->synchronous_mode) && bio_op(bio) == REQ_OP_WRITE) {
1403 unsigned long flags;
1404 spin_lock_irqsave(&ic->endio_wait.lock, flags);
1405 bio_list_add(&ic->synchronous_bios, bio);
1406 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0);
1407 spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
1408 return;
1409 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01001410 bio_endio(bio);
1411}
1412
1413static void do_endio_flush(struct dm_integrity_c *ic, struct dm_integrity_io *dio)
1414{
1415 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1416
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001417 if (unlikely(dio->fua) && likely(!bio->bi_status) && likely(!dm_integrity_failed(ic)))
Mikulas Patocka7eada902017-01-04 20:23:53 +01001418 submit_flush_bio(ic, dio);
1419 else
1420 do_endio(ic, bio);
1421}
1422
1423static void dec_in_flight(struct dm_integrity_io *dio)
1424{
1425 if (atomic_dec_and_test(&dio->in_flight)) {
1426 struct dm_integrity_c *ic = dio->ic;
1427 struct bio *bio;
1428
1429 remove_range(ic, &dio->range);
1430
1431 if (unlikely(dio->write))
1432 schedule_autocommit(ic);
1433
1434 bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1435
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001436 if (unlikely(dio->bi_status) && !bio->bi_status)
1437 bio->bi_status = dio->bi_status;
1438 if (likely(!bio->bi_status) && unlikely(bio_sectors(bio) != dio->range.n_sectors)) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01001439 dio->range.logical_sector += dio->range.n_sectors;
1440 bio_advance(bio, dio->range.n_sectors << SECTOR_SHIFT);
1441 INIT_WORK(&dio->work, integrity_bio_wait);
Mikulas Patocka53770f02020-02-17 07:43:03 -05001442 queue_work(ic->offload_wq, &dio->work);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001443 return;
1444 }
1445 do_endio_flush(ic, dio);
1446 }
1447}
1448
1449static void integrity_end_io(struct bio *bio)
1450{
1451 struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
1452
Mike Snitzer248aa262020-02-28 18:11:53 -05001453 dm_bio_restore(&dio->bio_details, bio);
1454 if (bio->bi_integrity)
Mikulas Patocka7eada902017-01-04 20:23:53 +01001455 bio->bi_opf |= REQ_INTEGRITY;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001456
1457 if (dio->completion)
1458 complete(dio->completion);
1459
1460 dec_in_flight(dio);
1461}
1462
1463static void integrity_sector_checksum(struct dm_integrity_c *ic, sector_t sector,
1464 const char *data, char *result)
1465{
1466 __u64 sector_le = cpu_to_le64(sector);
1467 SHASH_DESC_ON_STACK(req, ic->internal_hash);
1468 int r;
1469 unsigned digest_size;
1470
1471 req->tfm = ic->internal_hash;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001472
1473 r = crypto_shash_init(req);
1474 if (unlikely(r < 0)) {
1475 dm_integrity_io_error(ic, "crypto_shash_init", r);
1476 goto failed;
1477 }
1478
1479 r = crypto_shash_update(req, (const __u8 *)&sector_le, sizeof sector_le);
1480 if (unlikely(r < 0)) {
1481 dm_integrity_io_error(ic, "crypto_shash_update", r);
1482 goto failed;
1483 }
1484
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001485 r = crypto_shash_update(req, data, ic->sectors_per_block << SECTOR_SHIFT);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001486 if (unlikely(r < 0)) {
1487 dm_integrity_io_error(ic, "crypto_shash_update", r);
1488 goto failed;
1489 }
1490
1491 r = crypto_shash_final(req, result);
1492 if (unlikely(r < 0)) {
1493 dm_integrity_io_error(ic, "crypto_shash_final", r);
1494 goto failed;
1495 }
1496
1497 digest_size = crypto_shash_digestsize(ic->internal_hash);
1498 if (unlikely(digest_size < ic->tag_size))
1499 memset(result + digest_size, 0, ic->tag_size - digest_size);
1500
1501 return;
1502
1503failed:
1504 /* this shouldn't happen anyway, the hash functions have no reason to fail */
1505 get_random_bytes(result, ic->tag_size);
1506}
1507
1508static void integrity_metadata(struct work_struct *w)
1509{
1510 struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work);
1511 struct dm_integrity_c *ic = dio->ic;
1512
1513 int r;
1514
1515 if (ic->internal_hash) {
1516 struct bvec_iter iter;
1517 struct bio_vec bv;
1518 unsigned digest_size = crypto_shash_digestsize(ic->internal_hash);
1519 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1520 char *checksums;
Mikulas Patocka56b67a42017-04-18 16:51:50 -04001521 unsigned extra_space = unlikely(digest_size > ic->tag_size) ? digest_size - ic->tag_size : 0;
Mikulas Patockab93b6642020-03-22 20:42:21 +01001522 char checksums_onstack[max((size_t)HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)];
Mikulas Patocka7eada902017-01-04 20:23:53 +01001523 unsigned sectors_to_process = dio->range.n_sectors;
1524 sector_t sector = dio->range.logical_sector;
1525
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04001526 if (unlikely(ic->mode == 'R'))
1527 goto skip_io;
1528
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001529 checksums = kmalloc((PAGE_SIZE >> SECTOR_SHIFT >> ic->sb->log2_sectors_per_block) * ic->tag_size + extra_space,
Mikulas Patocka7eada902017-01-04 20:23:53 +01001530 GFP_NOIO | __GFP_NORETRY | __GFP_NOWARN);
Kees Cook6d39a122018-08-07 14:18:39 -07001531 if (!checksums) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01001532 checksums = checksums_onstack;
Kees Cook6d39a122018-08-07 14:18:39 -07001533 if (WARN_ON(extra_space &&
1534 digest_size > sizeof(checksums_onstack))) {
1535 r = -EINVAL;
1536 goto error;
1537 }
1538 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01001539
Mike Snitzer248aa262020-02-28 18:11:53 -05001540 __bio_for_each_segment(bv, bio, iter, dio->bio_details.bi_iter) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01001541 unsigned pos;
1542 char *mem, *checksums_ptr;
1543
1544again:
1545 mem = (char *)kmap_atomic(bv.bv_page) + bv.bv_offset;
1546 pos = 0;
1547 checksums_ptr = checksums;
1548 do {
1549 integrity_sector_checksum(ic, sector, mem + pos, checksums_ptr);
1550 checksums_ptr += ic->tag_size;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001551 sectors_to_process -= ic->sectors_per_block;
1552 pos += ic->sectors_per_block << SECTOR_SHIFT;
1553 sector += ic->sectors_per_block;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001554 } while (pos < bv.bv_len && sectors_to_process && checksums != checksums_onstack);
1555 kunmap_atomic(mem);
1556
1557 r = dm_integrity_rw_tag(ic, checksums, &dio->metadata_block, &dio->metadata_offset,
1558 checksums_ptr - checksums, !dio->write ? TAG_CMP : TAG_WRITE);
1559 if (unlikely(r)) {
1560 if (r > 0) {
Erich Ecknereaab4bd2020-02-12 11:43:10 +01001561 char b[BDEVNAME_SIZE];
1562 DMERR_LIMIT("%s: Checksum failed at sector 0x%llx", bio_devname(bio, b),
Mikulas Patocka76491942020-03-22 20:42:22 +01001563 (sector - ((r + ic->tag_size - 1) / ic->tag_size)));
Mikulas Patocka7eada902017-01-04 20:23:53 +01001564 r = -EILSEQ;
Mikulas Patocka3f2e5392017-07-21 12:00:00 -04001565 atomic64_inc(&ic->number_of_mismatches);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001566 }
1567 if (likely(checksums != checksums_onstack))
1568 kfree(checksums);
1569 goto error;
1570 }
1571
1572 if (!sectors_to_process)
1573 break;
1574
1575 if (unlikely(pos < bv.bv_len)) {
1576 bv.bv_offset += pos;
1577 bv.bv_len -= pos;
1578 goto again;
1579 }
1580 }
1581
1582 if (likely(checksums != checksums_onstack))
1583 kfree(checksums);
1584 } else {
Mike Snitzer248aa262020-02-28 18:11:53 -05001585 struct bio_integrity_payload *bip = dio->bio_details.bi_integrity;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001586
1587 if (bip) {
1588 struct bio_vec biv;
1589 struct bvec_iter iter;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001590 unsigned data_to_process = dio->range.n_sectors;
1591 sector_to_block(ic, data_to_process);
1592 data_to_process *= ic->tag_size;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001593
1594 bip_for_each_vec(biv, bip, iter) {
1595 unsigned char *tag;
1596 unsigned this_len;
1597
1598 BUG_ON(PageHighMem(biv.bv_page));
1599 tag = lowmem_page_address(biv.bv_page) + biv.bv_offset;
1600 this_len = min(biv.bv_len, data_to_process);
1601 r = dm_integrity_rw_tag(ic, tag, &dio->metadata_block, &dio->metadata_offset,
1602 this_len, !dio->write ? TAG_READ : TAG_WRITE);
1603 if (unlikely(r))
1604 goto error;
1605 data_to_process -= this_len;
1606 if (!data_to_process)
1607 break;
1608 }
1609 }
1610 }
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04001611skip_io:
Mikulas Patocka7eada902017-01-04 20:23:53 +01001612 dec_in_flight(dio);
1613 return;
1614error:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001615 dio->bi_status = errno_to_blk_status(r);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001616 dec_in_flight(dio);
1617}
1618
1619static int dm_integrity_map(struct dm_target *ti, struct bio *bio)
1620{
1621 struct dm_integrity_c *ic = ti->private;
1622 struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001623 struct bio_integrity_payload *bip;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001624
1625 sector_t area, offset;
1626
1627 dio->ic = ic;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001628 dio->bi_status = 0;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001629
1630 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1631 submit_flush_bio(ic, dio);
1632 return DM_MAPIO_SUBMITTED;
1633 }
1634
1635 dio->range.logical_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
1636 dio->write = bio_op(bio) == REQ_OP_WRITE;
1637 dio->fua = dio->write && bio->bi_opf & REQ_FUA;
1638 if (unlikely(dio->fua)) {
1639 /*
1640 * Don't pass down the FUA flag because we have to flush
1641 * disk cache anyway.
1642 */
1643 bio->bi_opf &= ~REQ_FUA;
1644 }
1645 if (unlikely(dio->range.logical_sector + bio_sectors(bio) > ic->provided_data_sectors)) {
1646 DMERR("Too big sector number: 0x%llx + 0x%x > 0x%llx",
Mikulas Patocka76491942020-03-22 20:42:22 +01001647 dio->range.logical_sector, bio_sectors(bio),
1648 ic->provided_data_sectors);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001649 return DM_MAPIO_KILL;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001650 }
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001651 if (unlikely((dio->range.logical_sector | bio_sectors(bio)) & (unsigned)(ic->sectors_per_block - 1))) {
1652 DMERR("Bio not aligned on %u sectors: 0x%llx, 0x%x",
1653 ic->sectors_per_block,
Mikulas Patocka76491942020-03-22 20:42:22 +01001654 dio->range.logical_sector, bio_sectors(bio));
Christoph Hellwig846785e2017-06-03 09:38:02 +02001655 return DM_MAPIO_KILL;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001656 }
1657
1658 if (ic->sectors_per_block > 1) {
1659 struct bvec_iter iter;
1660 struct bio_vec bv;
1661 bio_for_each_segment(bv, bio, iter) {
Mikulas Patocka95b13692017-11-07 10:40:40 -05001662 if (unlikely(bv.bv_len & ((ic->sectors_per_block << SECTOR_SHIFT) - 1))) {
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001663 DMERR("Bio vector (%u,%u) is not aligned on %u-sector boundary",
1664 bv.bv_offset, bv.bv_len, ic->sectors_per_block);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001665 return DM_MAPIO_KILL;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001666 }
1667 }
1668 }
1669
1670 bip = bio_integrity(bio);
1671 if (!ic->internal_hash) {
1672 if (bip) {
1673 unsigned wanted_tag_size = bio_sectors(bio) >> ic->sb->log2_sectors_per_block;
1674 if (ic->log2_tag_size >= 0)
1675 wanted_tag_size <<= ic->log2_tag_size;
1676 else
1677 wanted_tag_size *= ic->tag_size;
1678 if (unlikely(wanted_tag_size != bip->bip_iter.bi_size)) {
Mike Snitzer05d69092019-05-09 15:25:49 -04001679 DMERR("Invalid integrity data size %u, expected %u",
1680 bip->bip_iter.bi_size, wanted_tag_size);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001681 return DM_MAPIO_KILL;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001682 }
1683 }
1684 } else {
1685 if (unlikely(bip != NULL)) {
1686 DMERR("Unexpected integrity data when using internal hash");
Christoph Hellwig846785e2017-06-03 09:38:02 +02001687 return DM_MAPIO_KILL;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001688 }
1689 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01001690
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04001691 if (unlikely(ic->mode == 'R') && unlikely(dio->write))
Christoph Hellwig846785e2017-06-03 09:38:02 +02001692 return DM_MAPIO_KILL;
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04001693
Mikulas Patocka7eada902017-01-04 20:23:53 +01001694 get_area_and_offset(ic, dio->range.logical_sector, &area, &offset);
1695 dio->metadata_block = get_metadata_sector_and_offset(ic, area, offset, &dio->metadata_offset);
1696 bio->bi_iter.bi_sector = get_data_sector(ic, area, offset);
1697
1698 dm_integrity_map_continue(dio, true);
1699 return DM_MAPIO_SUBMITTED;
1700}
1701
1702static bool __journal_read_write(struct dm_integrity_io *dio, struct bio *bio,
1703 unsigned journal_section, unsigned journal_entry)
1704{
1705 struct dm_integrity_c *ic = dio->ic;
1706 sector_t logical_sector;
1707 unsigned n_sectors;
1708
1709 logical_sector = dio->range.logical_sector;
1710 n_sectors = dio->range.n_sectors;
1711 do {
1712 struct bio_vec bv = bio_iovec(bio);
1713 char *mem;
1714
1715 if (unlikely(bv.bv_len >> SECTOR_SHIFT > n_sectors))
1716 bv.bv_len = n_sectors << SECTOR_SHIFT;
1717 n_sectors -= bv.bv_len >> SECTOR_SHIFT;
1718 bio_advance_iter(bio, &bio->bi_iter, bv.bv_len);
1719retry_kmap:
1720 mem = kmap_atomic(bv.bv_page);
1721 if (likely(dio->write))
1722 flush_dcache_page(bv.bv_page);
1723
1724 do {
1725 struct journal_entry *je = access_journal_entry(ic, journal_section, journal_entry);
1726
1727 if (unlikely(!dio->write)) {
1728 struct journal_sector *js;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001729 char *mem_ptr;
1730 unsigned s;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001731
1732 if (unlikely(journal_entry_is_inprogress(je))) {
1733 flush_dcache_page(bv.bv_page);
1734 kunmap_atomic(mem);
1735
1736 __io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je));
1737 goto retry_kmap;
1738 }
1739 smp_rmb();
1740 BUG_ON(journal_entry_get_sector(je) != logical_sector);
1741 js = access_journal_data(ic, journal_section, journal_entry);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001742 mem_ptr = mem + bv.bv_offset;
1743 s = 0;
1744 do {
1745 memcpy(mem_ptr, js, JOURNAL_SECTOR_DATA);
1746 *(commit_id_t *)(mem_ptr + JOURNAL_SECTOR_DATA) = je->last_bytes[s];
1747 js++;
1748 mem_ptr += 1 << SECTOR_SHIFT;
1749 } while (++s < ic->sectors_per_block);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001750#ifdef INTERNAL_VERIFY
1751 if (ic->internal_hash) {
Mikulas Patockab93b6642020-03-22 20:42:21 +01001752 char checksums_onstack[max((size_t)HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)];
Mikulas Patocka7eada902017-01-04 20:23:53 +01001753
1754 integrity_sector_checksum(ic, logical_sector, mem + bv.bv_offset, checksums_onstack);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001755 if (unlikely(memcmp(checksums_onstack, journal_entry_tag(ic, je), ic->tag_size))) {
Mikulas Patocka22555742019-03-06 08:29:34 -05001756 DMERR_LIMIT("Checksum failed when reading from journal, at sector 0x%llx",
Mikulas Patocka76491942020-03-22 20:42:22 +01001757 logical_sector);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001758 }
1759 }
1760#endif
1761 }
1762
1763 if (!ic->internal_hash) {
1764 struct bio_integrity_payload *bip = bio_integrity(bio);
1765 unsigned tag_todo = ic->tag_size;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001766 char *tag_ptr = journal_entry_tag(ic, je);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001767
1768 if (bip) do {
1769 struct bio_vec biv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter);
1770 unsigned tag_now = min(biv.bv_len, tag_todo);
1771 char *tag_addr;
1772 BUG_ON(PageHighMem(biv.bv_page));
1773 tag_addr = lowmem_page_address(biv.bv_page) + biv.bv_offset;
1774 if (likely(dio->write))
1775 memcpy(tag_ptr, tag_addr, tag_now);
1776 else
1777 memcpy(tag_addr, tag_ptr, tag_now);
1778 bvec_iter_advance(bip->bip_vec, &bip->bip_iter, tag_now);
1779 tag_ptr += tag_now;
1780 tag_todo -= tag_now;
1781 } while (unlikely(tag_todo)); else {
1782 if (likely(dio->write))
1783 memset(tag_ptr, 0, tag_todo);
1784 }
1785 }
1786
1787 if (likely(dio->write)) {
1788 struct journal_sector *js;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001789 unsigned s;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001790
1791 js = access_journal_data(ic, journal_section, journal_entry);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001792 memcpy(js, mem + bv.bv_offset, ic->sectors_per_block << SECTOR_SHIFT);
1793
1794 s = 0;
1795 do {
1796 je->last_bytes[s] = js[s].commit_id;
1797 } while (++s < ic->sectors_per_block);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001798
1799 if (ic->internal_hash) {
1800 unsigned digest_size = crypto_shash_digestsize(ic->internal_hash);
1801 if (unlikely(digest_size > ic->tag_size)) {
Kees Cook6d39a122018-08-07 14:18:39 -07001802 char checksums_onstack[HASH_MAX_DIGESTSIZE];
Mikulas Patocka7eada902017-01-04 20:23:53 +01001803 integrity_sector_checksum(ic, logical_sector, (char *)js, checksums_onstack);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001804 memcpy(journal_entry_tag(ic, je), checksums_onstack, ic->tag_size);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001805 } else
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001806 integrity_sector_checksum(ic, logical_sector, (char *)js, journal_entry_tag(ic, je));
Mikulas Patocka7eada902017-01-04 20:23:53 +01001807 }
1808
1809 journal_entry_set_sector(je, logical_sector);
1810 }
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001811 logical_sector += ic->sectors_per_block;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001812
1813 journal_entry++;
1814 if (unlikely(journal_entry == ic->journal_section_entries)) {
1815 journal_entry = 0;
1816 journal_section++;
1817 wraparound_section(ic, &journal_section);
1818 }
1819
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001820 bv.bv_offset += ic->sectors_per_block << SECTOR_SHIFT;
1821 } while (bv.bv_len -= ic->sectors_per_block << SECTOR_SHIFT);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001822
1823 if (unlikely(!dio->write))
1824 flush_dcache_page(bv.bv_page);
1825 kunmap_atomic(mem);
1826 } while (n_sectors);
1827
1828 if (likely(dio->write)) {
1829 smp_mb();
1830 if (unlikely(waitqueue_active(&ic->copy_to_journal_wait)))
1831 wake_up(&ic->copy_to_journal_wait);
Mark Rutlandd3e632f2017-10-23 14:07:11 -07001832 if (READ_ONCE(ic->free_sectors) <= ic->free_sectors_threshold) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01001833 queue_work(ic->commit_wq, &ic->commit_work);
1834 } else {
1835 schedule_autocommit(ic);
1836 }
1837 } else {
1838 remove_range(ic, &dio->range);
1839 }
1840
1841 if (unlikely(bio->bi_iter.bi_size)) {
1842 sector_t area, offset;
1843
1844 dio->range.logical_sector = logical_sector;
1845 get_area_and_offset(ic, dio->range.logical_sector, &area, &offset);
1846 dio->metadata_block = get_metadata_sector_and_offset(ic, area, offset, &dio->metadata_offset);
1847 return true;
1848 }
1849
1850 return false;
1851}
1852
1853static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map)
1854{
1855 struct dm_integrity_c *ic = dio->ic;
1856 struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
1857 unsigned journal_section, journal_entry;
1858 unsigned journal_read_pos;
1859 struct completion read_comp;
1860 bool need_sync_io = ic->internal_hash && !dio->write;
1861
1862 if (need_sync_io && from_map) {
1863 INIT_WORK(&dio->work, integrity_bio_wait);
Mikulas Patocka53770f02020-02-17 07:43:03 -05001864 queue_work(ic->offload_wq, &dio->work);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001865 return;
1866 }
1867
1868lock_retry:
1869 spin_lock_irq(&ic->endio_wait.lock);
1870retry:
1871 if (unlikely(dm_integrity_failed(ic))) {
1872 spin_unlock_irq(&ic->endio_wait.lock);
1873 do_endio(ic, bio);
1874 return;
1875 }
1876 dio->range.n_sectors = bio_sectors(bio);
1877 journal_read_pos = NOT_FOUND;
1878 if (likely(ic->mode == 'J')) {
1879 if (dio->write) {
1880 unsigned next_entry, i, pos;
Mikulas Patocka9dd59722017-07-19 11:23:40 -04001881 unsigned ws, we, range_sectors;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001882
Mikulas Patocka9dd59722017-07-19 11:23:40 -04001883 dio->range.n_sectors = min(dio->range.n_sectors,
Mikulas Patocka4f434462019-04-29 14:57:21 +02001884 (sector_t)ic->free_sectors << ic->sb->log2_sectors_per_block);
Mikulas Patocka518748b2018-07-03 20:13:26 +02001885 if (unlikely(!dio->range.n_sectors)) {
1886 if (from_map)
1887 goto offload_to_thread;
1888 sleep_on_endio_wait(ic);
1889 goto retry;
1890 }
Mikulas Patocka9dd59722017-07-19 11:23:40 -04001891 range_sectors = dio->range.n_sectors >> ic->sb->log2_sectors_per_block;
1892 ic->free_sectors -= range_sectors;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001893 journal_section = ic->free_section;
1894 journal_entry = ic->free_section_entry;
1895
Mikulas Patocka9dd59722017-07-19 11:23:40 -04001896 next_entry = ic->free_section_entry + range_sectors;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001897 ic->free_section_entry = next_entry % ic->journal_section_entries;
1898 ic->free_section += next_entry / ic->journal_section_entries;
1899 ic->n_uncommitted_sections += next_entry / ic->journal_section_entries;
1900 wraparound_section(ic, &ic->free_section);
1901
1902 pos = journal_section * ic->journal_section_entries + journal_entry;
1903 ws = journal_section;
1904 we = journal_entry;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001905 i = 0;
1906 do {
Mikulas Patocka7eada902017-01-04 20:23:53 +01001907 struct journal_entry *je;
1908
1909 add_journal_node(ic, &ic->journal_tree[pos], dio->range.logical_sector + i);
1910 pos++;
1911 if (unlikely(pos >= ic->journal_entries))
1912 pos = 0;
1913
1914 je = access_journal_entry(ic, ws, we);
1915 BUG_ON(!journal_entry_is_unused(je));
1916 journal_entry_set_inprogress(je);
1917 we++;
1918 if (unlikely(we == ic->journal_section_entries)) {
1919 we = 0;
1920 ws++;
1921 wraparound_section(ic, &ws);
1922 }
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001923 } while ((i += ic->sectors_per_block) < dio->range.n_sectors);
Mikulas Patocka7eada902017-01-04 20:23:53 +01001924
1925 spin_unlock_irq(&ic->endio_wait.lock);
1926 goto journal_read_write;
1927 } else {
1928 sector_t next_sector;
1929 journal_read_pos = find_journal_node(ic, dio->range.logical_sector, &next_sector);
1930 if (likely(journal_read_pos == NOT_FOUND)) {
1931 if (unlikely(dio->range.n_sectors > next_sector - dio->range.logical_sector))
1932 dio->range.n_sectors = next_sector - dio->range.logical_sector;
1933 } else {
1934 unsigned i;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04001935 unsigned jp = journal_read_pos + 1;
1936 for (i = ic->sectors_per_block; i < dio->range.n_sectors; i += ic->sectors_per_block, jp++) {
1937 if (!test_journal_node(ic, jp, dio->range.logical_sector + i))
Mikulas Patocka7eada902017-01-04 20:23:53 +01001938 break;
1939 }
1940 dio->range.n_sectors = i;
1941 }
1942 }
1943 }
Mikulas Patocka724376a2018-07-03 20:13:27 +02001944 if (unlikely(!add_new_range(ic, &dio->range, true))) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01001945 /*
1946 * We must not sleep in the request routine because it could
1947 * stall bios on current->bio_list.
1948 * So, we offload the bio to a workqueue if we have to sleep.
1949 */
Mikulas Patocka7eada902017-01-04 20:23:53 +01001950 if (from_map) {
Mikulas Patocka518748b2018-07-03 20:13:26 +02001951offload_to_thread:
Mikulas Patocka7eada902017-01-04 20:23:53 +01001952 spin_unlock_irq(&ic->endio_wait.lock);
1953 INIT_WORK(&dio->work, integrity_bio_wait);
1954 queue_work(ic->wait_wq, &dio->work);
1955 return;
Mikulas Patocka7eada902017-01-04 20:23:53 +01001956 }
Mikulas Patocka5729b6e2019-08-10 12:30:27 -04001957 if (journal_read_pos != NOT_FOUND)
1958 dio->range.n_sectors = ic->sectors_per_block;
Mikulas Patocka724376a2018-07-03 20:13:27 +02001959 wait_and_add_new_range(ic, &dio->range);
Mikulas Patocka5729b6e2019-08-10 12:30:27 -04001960 /*
1961 * wait_and_add_new_range drops the spinlock, so the journal
1962 * may have been changed arbitrarily. We need to recheck.
1963 * To simplify the code, we restrict I/O size to just one block.
1964 */
1965 if (journal_read_pos != NOT_FOUND) {
1966 sector_t next_sector;
1967 unsigned new_pos = find_journal_node(ic, dio->range.logical_sector, &next_sector);
1968 if (unlikely(new_pos != journal_read_pos)) {
1969 remove_range_unlocked(ic, &dio->range);
1970 goto retry;
1971 }
1972 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01001973 }
1974 spin_unlock_irq(&ic->endio_wait.lock);
1975
1976 if (unlikely(journal_read_pos != NOT_FOUND)) {
1977 journal_section = journal_read_pos / ic->journal_section_entries;
1978 journal_entry = journal_read_pos % ic->journal_section_entries;
1979 goto journal_read_write;
1980 }
1981
Mikulas Patocka468dfca2019-04-29 14:57:24 +02001982 if (ic->mode == 'B' && dio->write) {
Mike Snitzer05d69092019-05-09 15:25:49 -04001983 if (!block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector,
1984 dio->range.n_sectors, BITMAP_OP_TEST_ALL_SET)) {
1985 struct bitmap_block_status *bbs;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02001986
Mike Snitzer05d69092019-05-09 15:25:49 -04001987 bbs = sector_to_bitmap_block(ic, dio->range.logical_sector);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02001988 spin_lock(&bbs->bio_queue_lock);
1989 bio_list_add(&bbs->bio_queue, bio);
1990 spin_unlock(&bbs->bio_queue_lock);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02001991 queue_work(ic->writer_wq, &bbs->work);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02001992 return;
1993 }
1994 }
1995
Mikulas Patocka7eada902017-01-04 20:23:53 +01001996 dio->in_flight = (atomic_t)ATOMIC_INIT(2);
1997
1998 if (need_sync_io) {
Arnd Bergmannb5e8ad92017-08-15 17:11:59 +02001999 init_completion(&read_comp);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002000 dio->completion = &read_comp;
2001 } else
2002 dio->completion = NULL;
2003
Mike Snitzer248aa262020-02-28 18:11:53 -05002004 dm_bio_record(&dio->bio_details, bio);
Christoph Hellwig74d46992017-08-23 19:10:32 +02002005 bio_set_dev(bio, ic->dev->bdev);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002006 bio->bi_integrity = NULL;
2007 bio->bi_opf &= ~REQ_INTEGRITY;
Mikulas Patocka7eada902017-01-04 20:23:53 +01002008 bio->bi_end_io = integrity_end_io;
Mikulas Patocka7eada902017-01-04 20:23:53 +01002009 bio->bi_iter.bi_size = dio->range.n_sectors << SECTOR_SHIFT;
Mike Snitzer248aa262020-02-28 18:11:53 -05002010
Mikulas Patocka7eada902017-01-04 20:23:53 +01002011 generic_make_request(bio);
2012
2013 if (need_sync_io) {
2014 wait_for_completion_io(&read_comp);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002015 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING) &&
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002016 dio->range.logical_sector + dio->range.n_sectors > le64_to_cpu(ic->sb->recalc_sector))
2017 goto skip_check;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002018 if (ic->mode == 'B') {
Mike Snitzer05d69092019-05-09 15:25:49 -04002019 if (!block_bitmap_op(ic, ic->recalc_bitmap, dio->range.logical_sector,
2020 dio->range.n_sectors, BITMAP_OP_TEST_ALL_CLEAR))
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002021 goto skip_check;
2022 }
2023
Hyunchul Leeb7e326f2017-07-31 16:22:20 +09002024 if (likely(!bio->bi_status))
2025 integrity_metadata(&dio->work);
2026 else
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002027skip_check:
Hyunchul Leeb7e326f2017-07-31 16:22:20 +09002028 dec_in_flight(dio);
2029
Mikulas Patocka7eada902017-01-04 20:23:53 +01002030 } else {
2031 INIT_WORK(&dio->work, integrity_metadata);
2032 queue_work(ic->metadata_wq, &dio->work);
2033 }
2034
2035 return;
2036
2037journal_read_write:
2038 if (unlikely(__journal_read_write(dio, bio, journal_section, journal_entry)))
2039 goto lock_retry;
2040
2041 do_endio_flush(ic, dio);
2042}
2043
2044
2045static void integrity_bio_wait(struct work_struct *w)
2046{
2047 struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work);
2048
2049 dm_integrity_map_continue(dio, false);
2050}
2051
2052static void pad_uncommitted(struct dm_integrity_c *ic)
2053{
2054 if (ic->free_section_entry) {
2055 ic->free_sectors -= ic->journal_section_entries - ic->free_section_entry;
2056 ic->free_section_entry = 0;
2057 ic->free_section++;
2058 wraparound_section(ic, &ic->free_section);
2059 ic->n_uncommitted_sections++;
2060 }
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002061 if (WARN_ON(ic->journal_sections * ic->journal_section_entries !=
Mike Snitzer05d69092019-05-09 15:25:49 -04002062 (ic->n_uncommitted_sections + ic->n_committed_sections) *
2063 ic->journal_section_entries + ic->free_sectors)) {
2064 DMCRIT("journal_sections %u, journal_section_entries %u, "
2065 "n_uncommitted_sections %u, n_committed_sections %u, "
2066 "journal_section_entries %u, free_sectors %u",
2067 ic->journal_sections, ic->journal_section_entries,
2068 ic->n_uncommitted_sections, ic->n_committed_sections,
2069 ic->journal_section_entries, ic->free_sectors);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002070 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01002071}
2072
2073static void integrity_commit(struct work_struct *w)
2074{
2075 struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, commit_work);
2076 unsigned commit_start, commit_sections;
2077 unsigned i, j, n;
2078 struct bio *flushes;
2079
2080 del_timer(&ic->autocommit_timer);
2081
2082 spin_lock_irq(&ic->endio_wait.lock);
2083 flushes = bio_list_get(&ic->flush_bio_list);
2084 if (unlikely(ic->mode != 'J')) {
2085 spin_unlock_irq(&ic->endio_wait.lock);
2086 dm_integrity_flush_buffers(ic);
2087 goto release_flush_bios;
2088 }
2089
2090 pad_uncommitted(ic);
2091 commit_start = ic->uncommitted_section;
2092 commit_sections = ic->n_uncommitted_sections;
2093 spin_unlock_irq(&ic->endio_wait.lock);
2094
2095 if (!commit_sections)
2096 goto release_flush_bios;
2097
2098 i = commit_start;
2099 for (n = 0; n < commit_sections; n++) {
2100 for (j = 0; j < ic->journal_section_entries; j++) {
2101 struct journal_entry *je;
2102 je = access_journal_entry(ic, i, j);
2103 io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je));
2104 }
2105 for (j = 0; j < ic->journal_section_sectors; j++) {
2106 struct journal_sector *js;
2107 js = access_journal(ic, i, j);
2108 js->commit_id = dm_integrity_commit_id(ic, i, j, ic->commit_seq);
2109 }
2110 i++;
2111 if (unlikely(i >= ic->journal_sections))
2112 ic->commit_seq = next_commit_seq(ic->commit_seq);
2113 wraparound_section(ic, &i);
2114 }
2115 smp_rmb();
2116
2117 write_journal(ic, commit_start, commit_sections);
2118
2119 spin_lock_irq(&ic->endio_wait.lock);
2120 ic->uncommitted_section += commit_sections;
2121 wraparound_section(ic, &ic->uncommitted_section);
2122 ic->n_uncommitted_sections -= commit_sections;
2123 ic->n_committed_sections += commit_sections;
2124 spin_unlock_irq(&ic->endio_wait.lock);
2125
Mark Rutlandd3e632f2017-10-23 14:07:11 -07002126 if (READ_ONCE(ic->free_sectors) <= ic->free_sectors_threshold)
Mikulas Patocka7eada902017-01-04 20:23:53 +01002127 queue_work(ic->writer_wq, &ic->writer_work);
2128
2129release_flush_bios:
2130 while (flushes) {
2131 struct bio *next = flushes->bi_next;
2132 flushes->bi_next = NULL;
2133 do_endio(ic, flushes);
2134 flushes = next;
2135 }
2136}
2137
2138static void complete_copy_from_journal(unsigned long error, void *context)
2139{
2140 struct journal_io *io = context;
2141 struct journal_completion *comp = io->comp;
2142 struct dm_integrity_c *ic = comp->ic;
2143 remove_range(ic, &io->range);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002144 mempool_free(io, &ic->journal_io_mempool);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002145 if (unlikely(error != 0))
2146 dm_integrity_io_error(ic, "copying from journal", -EIO);
2147 complete_journal_op(comp);
2148}
2149
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002150static void restore_last_bytes(struct dm_integrity_c *ic, struct journal_sector *js,
2151 struct journal_entry *je)
2152{
2153 unsigned s = 0;
2154 do {
2155 js->commit_id = je->last_bytes[s];
2156 js++;
2157 } while (++s < ic->sectors_per_block);
2158}
2159
Mikulas Patocka7eada902017-01-04 20:23:53 +01002160static void do_journal_write(struct dm_integrity_c *ic, unsigned write_start,
2161 unsigned write_sections, bool from_replay)
2162{
2163 unsigned i, j, n;
2164 struct journal_completion comp;
Mikulas Patockaa7c3e62b2017-07-19 11:24:08 -04002165 struct blk_plug plug;
2166
2167 blk_start_plug(&plug);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002168
2169 comp.ic = ic;
2170 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
Arnd Bergmannb5e8ad92017-08-15 17:11:59 +02002171 init_completion(&comp.comp);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002172
2173 i = write_start;
2174 for (n = 0; n < write_sections; n++, i++, wraparound_section(ic, &i)) {
2175#ifndef INTERNAL_VERIFY
2176 if (unlikely(from_replay))
2177#endif
2178 rw_section_mac(ic, i, false);
2179 for (j = 0; j < ic->journal_section_entries; j++) {
2180 struct journal_entry *je = access_journal_entry(ic, i, j);
2181 sector_t sec, area, offset;
2182 unsigned k, l, next_loop;
2183 sector_t metadata_block;
2184 unsigned metadata_offset;
2185 struct journal_io *io;
2186
2187 if (journal_entry_is_unused(je))
2188 continue;
2189 BUG_ON(unlikely(journal_entry_is_inprogress(je)) && !from_replay);
2190 sec = journal_entry_get_sector(je);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002191 if (unlikely(from_replay)) {
2192 if (unlikely(sec & (unsigned)(ic->sectors_per_block - 1))) {
2193 dm_integrity_io_error(ic, "invalid sector in journal", -EIO);
2194 sec &= ~(sector_t)(ic->sectors_per_block - 1);
2195 }
2196 }
Mikulas Patockaf6f72f32020-03-22 20:42:23 +01002197 if (unlikely(sec >= ic->provided_data_sectors))
2198 continue;
Mikulas Patocka7eada902017-01-04 20:23:53 +01002199 get_area_and_offset(ic, sec, &area, &offset);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002200 restore_last_bytes(ic, access_journal_data(ic, i, j), je);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002201 for (k = j + 1; k < ic->journal_section_entries; k++) {
2202 struct journal_entry *je2 = access_journal_entry(ic, i, k);
2203 sector_t sec2, area2, offset2;
2204 if (journal_entry_is_unused(je2))
2205 break;
2206 BUG_ON(unlikely(journal_entry_is_inprogress(je2)) && !from_replay);
2207 sec2 = journal_entry_get_sector(je2);
Mikulas Patockaf6f72f32020-03-22 20:42:23 +01002208 if (unlikely(sec2 >= ic->provided_data_sectors))
2209 break;
Mikulas Patocka7eada902017-01-04 20:23:53 +01002210 get_area_and_offset(ic, sec2, &area2, &offset2);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002211 if (area2 != area || offset2 != offset + ((k - j) << ic->sb->log2_sectors_per_block))
Mikulas Patocka7eada902017-01-04 20:23:53 +01002212 break;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002213 restore_last_bytes(ic, access_journal_data(ic, i, k), je2);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002214 }
2215 next_loop = k - 1;
2216
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002217 io = mempool_alloc(&ic->journal_io_mempool, GFP_NOIO);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002218 io->comp = &comp;
2219 io->range.logical_sector = sec;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002220 io->range.n_sectors = (k - j) << ic->sb->log2_sectors_per_block;
Mikulas Patocka7eada902017-01-04 20:23:53 +01002221
2222 spin_lock_irq(&ic->endio_wait.lock);
Mikulas Patocka8b3bbd42019-04-29 14:57:22 +02002223 add_new_range_and_wait(ic, &io->range);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002224
2225 if (likely(!from_replay)) {
2226 struct journal_node *section_node = &ic->journal_tree[i * ic->journal_section_entries];
2227
2228 /* don't write if there is newer committed sector */
2229 while (j < k && find_newer_committed_node(ic, &section_node[j])) {
2230 struct journal_entry *je2 = access_journal_entry(ic, i, j);
2231
2232 journal_entry_set_unused(je2);
2233 remove_journal_node(ic, &section_node[j]);
2234 j++;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002235 sec += ic->sectors_per_block;
2236 offset += ic->sectors_per_block;
Mikulas Patocka7eada902017-01-04 20:23:53 +01002237 }
2238 while (j < k && find_newer_committed_node(ic, &section_node[k - 1])) {
2239 struct journal_entry *je2 = access_journal_entry(ic, i, k - 1);
2240
2241 journal_entry_set_unused(je2);
2242 remove_journal_node(ic, &section_node[k - 1]);
2243 k--;
2244 }
2245 if (j == k) {
2246 remove_range_unlocked(ic, &io->range);
2247 spin_unlock_irq(&ic->endio_wait.lock);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002248 mempool_free(io, &ic->journal_io_mempool);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002249 goto skip_io;
2250 }
2251 for (l = j; l < k; l++) {
2252 remove_journal_node(ic, &section_node[l]);
2253 }
2254 }
2255 spin_unlock_irq(&ic->endio_wait.lock);
2256
2257 metadata_block = get_metadata_sector_and_offset(ic, area, offset, &metadata_offset);
2258 for (l = j; l < k; l++) {
2259 int r;
2260 struct journal_entry *je2 = access_journal_entry(ic, i, l);
2261
2262 if (
2263#ifndef INTERNAL_VERIFY
2264 unlikely(from_replay) &&
2265#endif
2266 ic->internal_hash) {
Kees Cook6d39a122018-08-07 14:18:39 -07002267 char test_tag[max_t(size_t, HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)];
Mikulas Patocka7eada902017-01-04 20:23:53 +01002268
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002269 integrity_sector_checksum(ic, sec + ((l - j) << ic->sb->log2_sectors_per_block),
Mikulas Patocka7eada902017-01-04 20:23:53 +01002270 (char *)access_journal_data(ic, i, l), test_tag);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002271 if (unlikely(memcmp(test_tag, journal_entry_tag(ic, je2), ic->tag_size)))
Mikulas Patocka7eada902017-01-04 20:23:53 +01002272 dm_integrity_io_error(ic, "tag mismatch when replaying journal", -EILSEQ);
2273 }
2274
2275 journal_entry_set_unused(je2);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002276 r = dm_integrity_rw_tag(ic, journal_entry_tag(ic, je2), &metadata_block, &metadata_offset,
Mikulas Patocka7eada902017-01-04 20:23:53 +01002277 ic->tag_size, TAG_WRITE);
2278 if (unlikely(r)) {
2279 dm_integrity_io_error(ic, "reading tags", r);
2280 }
2281 }
2282
2283 atomic_inc(&comp.in_flight);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002284 copy_from_journal(ic, i, j << ic->sb->log2_sectors_per_block,
2285 (k - j) << ic->sb->log2_sectors_per_block,
2286 get_data_sector(ic, area, offset),
Mikulas Patocka7eada902017-01-04 20:23:53 +01002287 complete_copy_from_journal, io);
2288skip_io:
2289 j = next_loop;
2290 }
2291 }
2292
2293 dm_bufio_write_dirty_buffers_async(ic->bufio);
2294
Mikulas Patockaa7c3e62b2017-07-19 11:24:08 -04002295 blk_finish_plug(&plug);
2296
Mikulas Patocka7eada902017-01-04 20:23:53 +01002297 complete_journal_op(&comp);
2298 wait_for_completion_io(&comp.comp);
2299
2300 dm_integrity_flush_buffers(ic);
2301}
2302
2303static void integrity_writer(struct work_struct *w)
2304{
2305 struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, writer_work);
2306 unsigned write_start, write_sections;
2307
2308 unsigned prev_free_sectors;
2309
2310 /* the following test is not needed, but it tests the replay code */
Mikulas Patockaadc0daa2020-02-24 10:20:28 +01002311 if (unlikely(dm_suspended(ic->ti)) && !ic->meta_dev)
Mikulas Patocka7eada902017-01-04 20:23:53 +01002312 return;
2313
2314 spin_lock_irq(&ic->endio_wait.lock);
2315 write_start = ic->committed_section;
2316 write_sections = ic->n_committed_sections;
2317 spin_unlock_irq(&ic->endio_wait.lock);
2318
2319 if (!write_sections)
2320 return;
2321
2322 do_journal_write(ic, write_start, write_sections, false);
2323
2324 spin_lock_irq(&ic->endio_wait.lock);
2325
2326 ic->committed_section += write_sections;
2327 wraparound_section(ic, &ic->committed_section);
2328 ic->n_committed_sections -= write_sections;
2329
2330 prev_free_sectors = ic->free_sectors;
2331 ic->free_sectors += write_sections * ic->journal_section_entries;
2332 if (unlikely(!prev_free_sectors))
2333 wake_up_locked(&ic->endio_wait);
2334
2335 spin_unlock_irq(&ic->endio_wait.lock);
2336}
2337
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002338static void recalc_write_super(struct dm_integrity_c *ic)
2339{
2340 int r;
2341
2342 dm_integrity_flush_buffers(ic);
2343 if (dm_integrity_failed(ic))
2344 return;
2345
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002346 r = sync_rw_sb(ic, REQ_OP_WRITE, 0);
2347 if (unlikely(r))
2348 dm_integrity_io_error(ic, "writing superblock", r);
2349}
2350
2351static void integrity_recalc(struct work_struct *w)
2352{
2353 struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, recalc_work);
2354 struct dm_integrity_range range;
2355 struct dm_io_request io_req;
2356 struct dm_io_region io_loc;
2357 sector_t area, offset;
2358 sector_t metadata_block;
2359 unsigned metadata_offset;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002360 sector_t logical_sector, n_sectors;
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002361 __u8 *t;
2362 unsigned i;
2363 int r;
2364 unsigned super_counter = 0;
2365
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002366 DEBUG_print("start recalculation... (position %llx)\n", le64_to_cpu(ic->sb->recalc_sector));
2367
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002368 spin_lock_irq(&ic->endio_wait.lock);
2369
2370next_chunk:
2371
Mikulas Patockaadc0daa2020-02-24 10:20:28 +01002372 if (unlikely(dm_suspended(ic->ti)))
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002373 goto unlock_ret;
2374
2375 range.logical_sector = le64_to_cpu(ic->sb->recalc_sector);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002376 if (unlikely(range.logical_sector >= ic->provided_data_sectors)) {
2377 if (ic->mode == 'B') {
2378 DEBUG_print("queue_delayed_work: bitmap_flush_work\n");
2379 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0);
2380 }
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002381 goto unlock_ret;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002382 }
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002383
2384 get_area_and_offset(ic, range.logical_sector, &area, &offset);
2385 range.n_sectors = min((sector_t)RECALC_SECTORS, ic->provided_data_sectors - range.logical_sector);
2386 if (!ic->meta_dev)
Mikulas Patocka4f434462019-04-29 14:57:21 +02002387 range.n_sectors = min(range.n_sectors, ((sector_t)1U << ic->sb->log2_interleave_sectors) - (unsigned)offset);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002388
Mikulas Patocka8b3bbd42019-04-29 14:57:22 +02002389 add_new_range_and_wait(ic, &range);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002390 spin_unlock_irq(&ic->endio_wait.lock);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002391 logical_sector = range.logical_sector;
2392 n_sectors = range.n_sectors;
2393
2394 if (ic->mode == 'B') {
2395 if (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector, n_sectors, BITMAP_OP_TEST_ALL_CLEAR)) {
2396 goto advance_and_next;
2397 }
Mike Snitzer05d69092019-05-09 15:25:49 -04002398 while (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector,
2399 ic->sectors_per_block, BITMAP_OP_TEST_ALL_CLEAR)) {
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002400 logical_sector += ic->sectors_per_block;
2401 n_sectors -= ic->sectors_per_block;
2402 cond_resched();
2403 }
Mike Snitzer05d69092019-05-09 15:25:49 -04002404 while (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector + n_sectors - ic->sectors_per_block,
2405 ic->sectors_per_block, BITMAP_OP_TEST_ALL_CLEAR)) {
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002406 n_sectors -= ic->sectors_per_block;
2407 cond_resched();
2408 }
2409 get_area_and_offset(ic, logical_sector, &area, &offset);
2410 }
2411
Mikulas Patocka76491942020-03-22 20:42:22 +01002412 DEBUG_print("recalculating: %llx, %llx\n", logical_sector, n_sectors);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002413
2414 if (unlikely(++super_counter == RECALC_WRITE_SUPER)) {
2415 recalc_write_super(ic);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002416 if (ic->mode == 'B') {
2417 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, ic->bitmap_flush_interval);
2418 }
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002419 super_counter = 0;
2420 }
2421
2422 if (unlikely(dm_integrity_failed(ic)))
2423 goto err;
2424
2425 io_req.bi_op = REQ_OP_READ;
2426 io_req.bi_op_flags = 0;
2427 io_req.mem.type = DM_IO_VMA;
2428 io_req.mem.ptr.addr = ic->recalc_buffer;
2429 io_req.notify.fn = NULL;
2430 io_req.client = ic->io;
2431 io_loc.bdev = ic->dev->bdev;
2432 io_loc.sector = get_data_sector(ic, area, offset);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002433 io_loc.count = n_sectors;
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002434
2435 r = dm_io(&io_req, 1, &io_loc, NULL);
2436 if (unlikely(r)) {
2437 dm_integrity_io_error(ic, "reading data", r);
2438 goto err;
2439 }
2440
2441 t = ic->recalc_tags;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002442 for (i = 0; i < n_sectors; i += ic->sectors_per_block) {
2443 integrity_sector_checksum(ic, logical_sector + i, ic->recalc_buffer + (i << SECTOR_SHIFT), t);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002444 t += ic->tag_size;
2445 }
2446
2447 metadata_block = get_metadata_sector_and_offset(ic, area, offset, &metadata_offset);
2448
2449 r = dm_integrity_rw_tag(ic, ic->recalc_tags, &metadata_block, &metadata_offset, t - ic->recalc_tags, TAG_WRITE);
2450 if (unlikely(r)) {
2451 dm_integrity_io_error(ic, "writing tags", r);
2452 goto err;
2453 }
2454
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002455advance_and_next:
2456 cond_resched();
2457
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002458 spin_lock_irq(&ic->endio_wait.lock);
2459 remove_range_unlocked(ic, &range);
2460 ic->sb->recalc_sector = cpu_to_le64(range.logical_sector + range.n_sectors);
2461 goto next_chunk;
2462
2463err:
2464 remove_range(ic, &range);
2465 return;
2466
2467unlock_ret:
2468 spin_unlock_irq(&ic->endio_wait.lock);
2469
2470 recalc_write_super(ic);
2471}
2472
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002473static void bitmap_block_work(struct work_struct *w)
2474{
2475 struct bitmap_block_status *bbs = container_of(w, struct bitmap_block_status, work);
2476 struct dm_integrity_c *ic = bbs->ic;
2477 struct bio *bio;
2478 struct bio_list bio_queue;
2479 struct bio_list waiting;
2480
2481 bio_list_init(&waiting);
2482
2483 spin_lock(&bbs->bio_queue_lock);
2484 bio_queue = bbs->bio_queue;
2485 bio_list_init(&bbs->bio_queue);
2486 spin_unlock(&bbs->bio_queue_lock);
2487
2488 while ((bio = bio_list_pop(&bio_queue))) {
2489 struct dm_integrity_io *dio;
2490
2491 dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
2492
Mike Snitzer05d69092019-05-09 15:25:49 -04002493 if (block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector,
2494 dio->range.n_sectors, BITMAP_OP_TEST_ALL_SET)) {
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002495 remove_range(ic, &dio->range);
2496 INIT_WORK(&dio->work, integrity_bio_wait);
Mikulas Patocka53770f02020-02-17 07:43:03 -05002497 queue_work(ic->offload_wq, &dio->work);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002498 } else {
Mike Snitzer05d69092019-05-09 15:25:49 -04002499 block_bitmap_op(ic, ic->journal, dio->range.logical_sector,
2500 dio->range.n_sectors, BITMAP_OP_SET);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002501 bio_list_add(&waiting, bio);
2502 }
2503 }
2504
2505 if (bio_list_empty(&waiting))
2506 return;
2507
Mike Snitzer05d69092019-05-09 15:25:49 -04002508 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC,
2509 bbs->idx * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT),
2510 BITMAP_BLOCK_SIZE >> SECTOR_SHIFT, NULL);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002511
2512 while ((bio = bio_list_pop(&waiting))) {
2513 struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
2514
Mike Snitzer05d69092019-05-09 15:25:49 -04002515 block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector,
2516 dio->range.n_sectors, BITMAP_OP_SET);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002517
2518 remove_range(ic, &dio->range);
2519 INIT_WORK(&dio->work, integrity_bio_wait);
Mikulas Patocka53770f02020-02-17 07:43:03 -05002520 queue_work(ic->offload_wq, &dio->work);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002521 }
2522
2523 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, ic->bitmap_flush_interval);
2524}
2525
2526static void bitmap_flush_work(struct work_struct *work)
2527{
2528 struct dm_integrity_c *ic = container_of(work, struct dm_integrity_c, bitmap_flush_work.work);
2529 struct dm_integrity_range range;
2530 unsigned long limit;
Mikulas Patocka48271492019-04-29 14:57:26 +02002531 struct bio *bio;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002532
2533 dm_integrity_flush_buffers(ic);
2534
2535 range.logical_sector = 0;
2536 range.n_sectors = ic->provided_data_sectors;
2537
2538 spin_lock_irq(&ic->endio_wait.lock);
2539 add_new_range_and_wait(ic, &range);
2540 spin_unlock_irq(&ic->endio_wait.lock);
2541
2542 dm_integrity_flush_buffers(ic);
2543 if (ic->meta_dev)
2544 blkdev_issue_flush(ic->dev->bdev, GFP_NOIO, NULL);
2545
2546 limit = ic->provided_data_sectors;
2547 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) {
2548 limit = le64_to_cpu(ic->sb->recalc_sector)
2549 >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit)
2550 << (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
2551 }
Mikulas Patocka48271492019-04-29 14:57:26 +02002552 /*DEBUG_print("zeroing journal\n");*/
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002553 block_bitmap_op(ic, ic->journal, 0, limit, BITMAP_OP_CLEAR);
2554 block_bitmap_op(ic, ic->may_write_bitmap, 0, limit, BITMAP_OP_CLEAR);
2555
Mike Snitzer05d69092019-05-09 15:25:49 -04002556 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, 0,
2557 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002558
Mikulas Patocka48271492019-04-29 14:57:26 +02002559 spin_lock_irq(&ic->endio_wait.lock);
2560 remove_range_unlocked(ic, &range);
2561 while (unlikely((bio = bio_list_pop(&ic->synchronous_bios)) != NULL)) {
2562 bio_endio(bio);
2563 spin_unlock_irq(&ic->endio_wait.lock);
2564 spin_lock_irq(&ic->endio_wait.lock);
2565 }
2566 spin_unlock_irq(&ic->endio_wait.lock);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002567}
2568
2569
Mikulas Patocka7eada902017-01-04 20:23:53 +01002570static void init_journal(struct dm_integrity_c *ic, unsigned start_section,
2571 unsigned n_sections, unsigned char commit_seq)
2572{
2573 unsigned i, j, n;
2574
2575 if (!n_sections)
2576 return;
2577
2578 for (n = 0; n < n_sections; n++) {
2579 i = start_section + n;
2580 wraparound_section(ic, &i);
2581 for (j = 0; j < ic->journal_section_sectors; j++) {
2582 struct journal_sector *js = access_journal(ic, i, j);
2583 memset(&js->entries, 0, JOURNAL_SECTOR_DATA);
2584 js->commit_id = dm_integrity_commit_id(ic, i, j, commit_seq);
2585 }
2586 for (j = 0; j < ic->journal_section_entries; j++) {
2587 struct journal_entry *je = access_journal_entry(ic, i, j);
2588 journal_entry_set_unused(je);
2589 }
2590 }
2591
2592 write_journal(ic, start_section, n_sections);
2593}
2594
2595static int find_commit_seq(struct dm_integrity_c *ic, unsigned i, unsigned j, commit_id_t id)
2596{
2597 unsigned char k;
2598 for (k = 0; k < N_COMMIT_IDS; k++) {
2599 if (dm_integrity_commit_id(ic, i, j, k) == id)
2600 return k;
2601 }
2602 dm_integrity_io_error(ic, "journal commit id", -EIO);
2603 return -EIO;
2604}
2605
2606static void replay_journal(struct dm_integrity_c *ic)
2607{
2608 unsigned i, j;
2609 bool used_commit_ids[N_COMMIT_IDS];
2610 unsigned max_commit_id_sections[N_COMMIT_IDS];
2611 unsigned write_start, write_sections;
2612 unsigned continue_section;
2613 bool journal_empty;
2614 unsigned char unused, last_used, want_commit_seq;
2615
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04002616 if (ic->mode == 'R')
2617 return;
2618
Mikulas Patocka7eada902017-01-04 20:23:53 +01002619 if (ic->journal_uptodate)
2620 return;
2621
2622 last_used = 0;
2623 write_start = 0;
2624
2625 if (!ic->just_formatted) {
2626 DEBUG_print("reading journal\n");
2627 rw_journal(ic, REQ_OP_READ, 0, 0, ic->journal_sections, NULL);
2628 if (ic->journal_io)
2629 DEBUG_bytes(lowmem_page_address(ic->journal_io[0].page), 64, "read journal");
2630 if (ic->journal_io) {
2631 struct journal_completion crypt_comp;
2632 crypt_comp.ic = ic;
Arnd Bergmannb5e8ad92017-08-15 17:11:59 +02002633 init_completion(&crypt_comp.comp);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002634 crypt_comp.in_flight = (atomic_t)ATOMIC_INIT(0);
2635 encrypt_journal(ic, false, 0, ic->journal_sections, &crypt_comp);
2636 wait_for_completion(&crypt_comp.comp);
2637 }
2638 DEBUG_bytes(lowmem_page_address(ic->journal[0].page), 64, "decrypted journal");
2639 }
2640
2641 if (dm_integrity_failed(ic))
2642 goto clear_journal;
2643
2644 journal_empty = true;
2645 memset(used_commit_ids, 0, sizeof used_commit_ids);
2646 memset(max_commit_id_sections, 0, sizeof max_commit_id_sections);
2647 for (i = 0; i < ic->journal_sections; i++) {
2648 for (j = 0; j < ic->journal_section_sectors; j++) {
2649 int k;
2650 struct journal_sector *js = access_journal(ic, i, j);
2651 k = find_commit_seq(ic, i, j, js->commit_id);
2652 if (k < 0)
2653 goto clear_journal;
2654 used_commit_ids[k] = true;
2655 max_commit_id_sections[k] = i;
2656 }
2657 if (journal_empty) {
2658 for (j = 0; j < ic->journal_section_entries; j++) {
2659 struct journal_entry *je = access_journal_entry(ic, i, j);
2660 if (!journal_entry_is_unused(je)) {
2661 journal_empty = false;
2662 break;
2663 }
2664 }
2665 }
2666 }
2667
2668 if (!used_commit_ids[N_COMMIT_IDS - 1]) {
2669 unused = N_COMMIT_IDS - 1;
2670 while (unused && !used_commit_ids[unused - 1])
2671 unused--;
2672 } else {
2673 for (unused = 0; unused < N_COMMIT_IDS; unused++)
2674 if (!used_commit_ids[unused])
2675 break;
2676 if (unused == N_COMMIT_IDS) {
2677 dm_integrity_io_error(ic, "journal commit ids", -EIO);
2678 goto clear_journal;
2679 }
2680 }
2681 DEBUG_print("first unused commit seq %d [%d,%d,%d,%d]\n",
2682 unused, used_commit_ids[0], used_commit_ids[1],
2683 used_commit_ids[2], used_commit_ids[3]);
2684
2685 last_used = prev_commit_seq(unused);
2686 want_commit_seq = prev_commit_seq(last_used);
2687
2688 if (!used_commit_ids[want_commit_seq] && used_commit_ids[prev_commit_seq(want_commit_seq)])
2689 journal_empty = true;
2690
2691 write_start = max_commit_id_sections[last_used] + 1;
2692 if (unlikely(write_start >= ic->journal_sections))
2693 want_commit_seq = next_commit_seq(want_commit_seq);
2694 wraparound_section(ic, &write_start);
2695
2696 i = write_start;
2697 for (write_sections = 0; write_sections < ic->journal_sections; write_sections++) {
2698 for (j = 0; j < ic->journal_section_sectors; j++) {
2699 struct journal_sector *js = access_journal(ic, i, j);
2700
2701 if (js->commit_id != dm_integrity_commit_id(ic, i, j, want_commit_seq)) {
2702 /*
2703 * This could be caused by crash during writing.
2704 * We won't replay the inconsistent part of the
2705 * journal.
2706 */
2707 DEBUG_print("commit id mismatch at position (%u, %u): %d != %d\n",
2708 i, j, find_commit_seq(ic, i, j, js->commit_id), want_commit_seq);
2709 goto brk;
2710 }
2711 }
2712 i++;
2713 if (unlikely(i >= ic->journal_sections))
2714 want_commit_seq = next_commit_seq(want_commit_seq);
2715 wraparound_section(ic, &i);
2716 }
2717brk:
2718
2719 if (!journal_empty) {
2720 DEBUG_print("replaying %u sections, starting at %u, commit seq %d\n",
2721 write_sections, write_start, want_commit_seq);
2722 do_journal_write(ic, write_start, write_sections, true);
2723 }
2724
2725 if (write_sections == ic->journal_sections && (ic->mode == 'J' || journal_empty)) {
2726 continue_section = write_start;
2727 ic->commit_seq = want_commit_seq;
2728 DEBUG_print("continuing from section %u, commit seq %d\n", write_start, ic->commit_seq);
2729 } else {
2730 unsigned s;
2731 unsigned char erase_seq;
2732clear_journal:
2733 DEBUG_print("clearing journal\n");
2734
2735 erase_seq = prev_commit_seq(prev_commit_seq(last_used));
2736 s = write_start;
2737 init_journal(ic, s, 1, erase_seq);
2738 s++;
2739 wraparound_section(ic, &s);
2740 if (ic->journal_sections >= 2) {
2741 init_journal(ic, s, ic->journal_sections - 2, erase_seq);
2742 s += ic->journal_sections - 2;
2743 wraparound_section(ic, &s);
2744 init_journal(ic, s, 1, erase_seq);
2745 }
2746
2747 continue_section = 0;
2748 ic->commit_seq = next_commit_seq(erase_seq);
2749 }
2750
2751 ic->committed_section = continue_section;
2752 ic->n_committed_sections = 0;
2753
2754 ic->uncommitted_section = continue_section;
2755 ic->n_uncommitted_sections = 0;
2756
2757 ic->free_section = continue_section;
2758 ic->free_section_entry = 0;
2759 ic->free_sectors = ic->journal_entries;
2760
2761 ic->journal_tree_root = RB_ROOT;
2762 for (i = 0; i < ic->journal_entries; i++)
2763 init_journal_node(&ic->journal_tree[i]);
2764}
2765
Mikulas Patocka48271492019-04-29 14:57:26 +02002766static void dm_integrity_enter_synchronous_mode(struct dm_integrity_c *ic)
Mikulas Patocka1f5a7752019-04-29 14:57:25 +02002767{
Mikulas Patocka48271492019-04-29 14:57:26 +02002768 DEBUG_print("dm_integrity_enter_synchronous_mode\n");
Mikulas Patocka1f5a7752019-04-29 14:57:25 +02002769
2770 if (ic->mode == 'B') {
Mikulas Patocka48271492019-04-29 14:57:26 +02002771 ic->bitmap_flush_interval = msecs_to_jiffies(10) + 1;
2772 ic->synchronous_mode = 1;
2773
Mikulas Patocka1f5a7752019-04-29 14:57:25 +02002774 cancel_delayed_work_sync(&ic->bitmap_flush_work);
2775 queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0);
2776 flush_workqueue(ic->commit_wq);
2777 }
Mikulas Patocka48271492019-04-29 14:57:26 +02002778}
2779
2780static int dm_integrity_reboot(struct notifier_block *n, unsigned long code, void *x)
2781{
2782 struct dm_integrity_c *ic = container_of(n, struct dm_integrity_c, reboot_notifier);
2783
2784 DEBUG_print("dm_integrity_reboot\n");
2785
2786 dm_integrity_enter_synchronous_mode(ic);
Mikulas Patocka1f5a7752019-04-29 14:57:25 +02002787
2788 return NOTIFY_DONE;
2789}
2790
Mikulas Patocka7eada902017-01-04 20:23:53 +01002791static void dm_integrity_postsuspend(struct dm_target *ti)
2792{
2793 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002794 int r;
Mikulas Patocka7eada902017-01-04 20:23:53 +01002795
Mikulas Patocka1f5a7752019-04-29 14:57:25 +02002796 WARN_ON(unregister_reboot_notifier(&ic->reboot_notifier));
Mikulas Patocka7eada902017-01-04 20:23:53 +01002797
2798 del_timer_sync(&ic->autocommit_timer);
2799
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002800 if (ic->recalc_wq)
2801 drain_workqueue(ic->recalc_wq);
2802
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002803 if (ic->mode == 'B')
2804 cancel_delayed_work_sync(&ic->bitmap_flush_work);
2805
Mikulas Patocka7eada902017-01-04 20:23:53 +01002806 queue_work(ic->commit_wq, &ic->commit_work);
2807 drain_workqueue(ic->commit_wq);
2808
2809 if (ic->mode == 'J') {
Mikulas Patocka747829a2018-07-03 20:13:32 +02002810 if (ic->meta_dev)
2811 queue_work(ic->writer_wq, &ic->writer_work);
Mikulas Patocka7eada902017-01-04 20:23:53 +01002812 drain_workqueue(ic->writer_wq);
2813 dm_integrity_flush_buffers(ic);
2814 }
2815
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002816 if (ic->mode == 'B') {
2817 dm_integrity_flush_buffers(ic);
2818#if 1
Mike Snitzer05d69092019-05-09 15:25:49 -04002819 /* set to 0 to test bitmap replay code */
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002820 init_journal(ic, 0, ic->journal_sections, 0);
2821 ic->sb->flags &= ~cpu_to_le32(SB_FLAG_DIRTY_BITMAP);
2822 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
2823 if (unlikely(r))
2824 dm_integrity_io_error(ic, "writing superblock", r);
2825#endif
2826 }
2827
Mikulas Patocka7eada902017-01-04 20:23:53 +01002828 BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress));
2829
2830 ic->journal_uptodate = true;
2831}
2832
2833static void dm_integrity_resume(struct dm_target *ti)
2834{
2835 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002836 int r;
2837 DEBUG_print("resume\n");
Mikulas Patocka7eada902017-01-04 20:23:53 +01002838
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002839 if (ic->sb->flags & cpu_to_le32(SB_FLAG_DIRTY_BITMAP)) {
2840 DEBUG_print("resume dirty_bitmap\n");
Mike Snitzer05d69092019-05-09 15:25:49 -04002841 rw_journal_sectors(ic, REQ_OP_READ, 0, 0,
2842 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002843 if (ic->mode == 'B') {
2844 if (ic->sb->log2_blocks_per_bitmap_bit == ic->log2_blocks_per_bitmap_bit) {
2845 block_bitmap_copy(ic, ic->recalc_bitmap, ic->journal);
2846 block_bitmap_copy(ic, ic->may_write_bitmap, ic->journal);
Mike Snitzer05d69092019-05-09 15:25:49 -04002847 if (!block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors,
2848 BITMAP_OP_TEST_ALL_CLEAR)) {
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002849 ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
2850 ic->sb->recalc_sector = cpu_to_le64(0);
2851 }
2852 } else {
Mike Snitzer05d69092019-05-09 15:25:49 -04002853 DEBUG_print("non-matching blocks_per_bitmap_bit: %u, %u\n",
2854 ic->sb->log2_blocks_per_bitmap_bit, ic->log2_blocks_per_bitmap_bit);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002855 ic->sb->log2_blocks_per_bitmap_bit = ic->log2_blocks_per_bitmap_bit;
2856 block_bitmap_op(ic, ic->recalc_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_SET);
2857 block_bitmap_op(ic, ic->may_write_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_SET);
2858 block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_SET);
Mike Snitzer05d69092019-05-09 15:25:49 -04002859 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, 0,
2860 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002861 ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
2862 ic->sb->recalc_sector = cpu_to_le64(0);
2863 }
2864 } else {
2865 if (!(ic->sb->log2_blocks_per_bitmap_bit == ic->log2_blocks_per_bitmap_bit &&
2866 block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_TEST_ALL_CLEAR))) {
2867 ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
2868 ic->sb->recalc_sector = cpu_to_le64(0);
2869 }
2870 init_journal(ic, 0, ic->journal_sections, 0);
2871 replay_journal(ic);
2872 ic->sb->flags &= ~cpu_to_le32(SB_FLAG_DIRTY_BITMAP);
2873 }
2874 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
2875 if (unlikely(r))
2876 dm_integrity_io_error(ic, "writing superblock", r);
2877 } else {
2878 replay_journal(ic);
2879 if (ic->mode == 'B') {
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002880 ic->sb->flags |= cpu_to_le32(SB_FLAG_DIRTY_BITMAP);
2881 ic->sb->log2_blocks_per_bitmap_bit = ic->log2_blocks_per_bitmap_bit;
2882 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
2883 if (unlikely(r))
2884 dm_integrity_io_error(ic, "writing superblock", r);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002885
Mikulas Patockad5bdf662020-02-07 11:42:30 -05002886 block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR);
2887 block_bitmap_op(ic, ic->recalc_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR);
2888 block_bitmap_op(ic, ic->may_write_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR);
2889 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING) &&
2890 le64_to_cpu(ic->sb->recalc_sector) < ic->provided_data_sectors) {
2891 block_bitmap_op(ic, ic->journal, le64_to_cpu(ic->sb->recalc_sector),
2892 ic->provided_data_sectors - le64_to_cpu(ic->sb->recalc_sector), BITMAP_OP_SET);
2893 block_bitmap_op(ic, ic->recalc_bitmap, le64_to_cpu(ic->sb->recalc_sector),
2894 ic->provided_data_sectors - le64_to_cpu(ic->sb->recalc_sector), BITMAP_OP_SET);
2895 block_bitmap_op(ic, ic->may_write_bitmap, le64_to_cpu(ic->sb->recalc_sector),
2896 ic->provided_data_sectors - le64_to_cpu(ic->sb->recalc_sector), BITMAP_OP_SET);
2897 }
Mike Snitzer05d69092019-05-09 15:25:49 -04002898 rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, 0,
2899 ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002900 }
2901 }
2902
2903 DEBUG_print("testing recalc: %x\n", ic->sb->flags);
2904 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) {
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002905 __u64 recalc_pos = le64_to_cpu(ic->sb->recalc_sector);
Mikulas Patocka76491942020-03-22 20:42:22 +01002906 DEBUG_print("recalc pos: %llx / %llx\n", recalc_pos, ic->provided_data_sectors);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002907 if (recalc_pos < ic->provided_data_sectors) {
2908 queue_work(ic->recalc_wq, &ic->recalc_work);
2909 } else if (recalc_pos > ic->provided_data_sectors) {
2910 ic->sb->recalc_sector = cpu_to_le64(ic->provided_data_sectors);
2911 recalc_write_super(ic);
2912 }
2913 }
Mikulas Patocka1f5a7752019-04-29 14:57:25 +02002914
2915 ic->reboot_notifier.notifier_call = dm_integrity_reboot;
2916 ic->reboot_notifier.next = NULL;
2917 ic->reboot_notifier.priority = INT_MAX - 1; /* be notified after md and before hardware drivers */
2918 WARN_ON(register_reboot_notifier(&ic->reboot_notifier));
Mikulas Patocka48271492019-04-29 14:57:26 +02002919
2920#if 0
Mike Snitzer05d69092019-05-09 15:25:49 -04002921 /* set to 1 to stress test synchronous mode */
Mikulas Patocka48271492019-04-29 14:57:26 +02002922 dm_integrity_enter_synchronous_mode(ic);
2923#endif
Mikulas Patocka7eada902017-01-04 20:23:53 +01002924}
2925
2926static void dm_integrity_status(struct dm_target *ti, status_type_t type,
2927 unsigned status_flags, char *result, unsigned maxlen)
2928{
2929 struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
2930 unsigned arg_count;
2931 size_t sz = 0;
2932
2933 switch (type) {
2934 case STATUSTYPE_INFO:
Mikulas Patockaf84fd2c2018-07-03 20:13:28 +02002935 DMEMIT("%llu %llu",
Mikulas Patocka76491942020-03-22 20:42:22 +01002936 atomic64_read(&ic->number_of_mismatches),
2937 ic->provided_data_sectors);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002938 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))
Mikulas Patocka76491942020-03-22 20:42:22 +01002939 DMEMIT(" %llu", le64_to_cpu(ic->sb->recalc_sector));
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002940 else
2941 DMEMIT(" -");
Mikulas Patocka7eada902017-01-04 20:23:53 +01002942 break;
2943
2944 case STATUSTYPE_TABLE: {
2945 __u64 watermark_percentage = (__u64)(ic->journal_entries - ic->free_sectors_threshold) * 100;
2946 watermark_percentage += ic->journal_entries / 2;
2947 do_div(watermark_percentage, ic->journal_entries);
Mikulas Patocka893e3c32019-04-29 14:57:18 +02002948 arg_count = 3;
Mikulas Patocka356d9d52018-07-03 20:13:30 +02002949 arg_count += !!ic->meta_dev;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04002950 arg_count += ic->sectors_per_block != 1;
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002951 arg_count += !!(ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING));
Mikulas Patocka893e3c32019-04-29 14:57:18 +02002952 arg_count += ic->mode == 'J';
2953 arg_count += ic->mode == 'J';
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002954 arg_count += ic->mode == 'B';
2955 arg_count += ic->mode == 'B';
Mikulas Patocka7eada902017-01-04 20:23:53 +01002956 arg_count += !!ic->internal_hash_alg.alg_string;
2957 arg_count += !!ic->journal_crypt_alg.alg_string;
2958 arg_count += !!ic->journal_mac_alg.alg_string;
Mikulas Patockad5378582019-11-13 06:48:16 -05002959 arg_count += (ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_PADDING)) != 0;
Mikulas Patocka76491942020-03-22 20:42:22 +01002960 DMEMIT("%s %llu %u %c %u", ic->dev->name, ic->start,
Mikulas Patocka7eada902017-01-04 20:23:53 +01002961 ic->tag_size, ic->mode, arg_count);
Mikulas Patocka356d9d52018-07-03 20:13:30 +02002962 if (ic->meta_dev)
2963 DMEMIT(" meta_device:%s", ic->meta_dev->name);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002964 if (ic->sectors_per_block != 1)
2965 DMEMIT(" block_size:%u", ic->sectors_per_block << SECTOR_SHIFT);
Mikulas Patocka7fc2e472020-02-17 08:11:35 -05002966 if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02002967 DMEMIT(" recalculate");
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002968 DMEMIT(" journal_sectors:%u", ic->initial_sectors - SB_SECTORS);
2969 DMEMIT(" interleave_sectors:%u", 1U << ic->sb->log2_interleave_sectors);
2970 DMEMIT(" buffer_sectors:%u", 1U << ic->log2_buffer_sectors);
Mikulas Patocka893e3c32019-04-29 14:57:18 +02002971 if (ic->mode == 'J') {
2972 DMEMIT(" journal_watermark:%u", (unsigned)watermark_percentage);
2973 DMEMIT(" commit_time:%u", ic->autocommit_msec);
2974 }
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002975 if (ic->mode == 'B') {
Mikulas Patocka76491942020-03-22 20:42:22 +01002976 DMEMIT(" sectors_per_bit:%llu", (sector_t)ic->sectors_per_block << ic->log2_blocks_per_bitmap_bit);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02002977 DMEMIT(" bitmap_flush_interval:%u", jiffies_to_msecs(ic->bitmap_flush_interval));
2978 }
Mikulas Patockad5378582019-11-13 06:48:16 -05002979 if ((ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_PADDING)) != 0)
2980 DMEMIT(" fix_padding");
Mikulas Patocka7eada902017-01-04 20:23:53 +01002981
2982#define EMIT_ALG(a, n) \
2983 do { \
2984 if (ic->a.alg_string) { \
2985 DMEMIT(" %s:%s", n, ic->a.alg_string); \
2986 if (ic->a.key_string) \
2987 DMEMIT(":%s", ic->a.key_string);\
2988 } \
2989 } while (0)
Mikulas Patocka56b67a42017-04-18 16:51:50 -04002990 EMIT_ALG(internal_hash_alg, "internal_hash");
2991 EMIT_ALG(journal_crypt_alg, "journal_crypt");
2992 EMIT_ALG(journal_mac_alg, "journal_mac");
Mikulas Patocka7eada902017-01-04 20:23:53 +01002993 break;
2994 }
2995 }
2996}
2997
2998static int dm_integrity_iterate_devices(struct dm_target *ti,
2999 iterate_devices_callout_fn fn, void *data)
3000{
3001 struct dm_integrity_c *ic = ti->private;
3002
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003003 if (!ic->meta_dev)
3004 return fn(ti, ic->dev, ic->start + ic->initial_sectors + ic->metadata_run, ti->len, data);
3005 else
3006 return fn(ti, ic->dev, 0, ti->len, data);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003007}
3008
Mikulas Patocka9d609f852017-04-18 16:51:52 -04003009static void dm_integrity_io_hints(struct dm_target *ti, struct queue_limits *limits)
3010{
3011 struct dm_integrity_c *ic = ti->private;
3012
3013 if (ic->sectors_per_block > 1) {
3014 limits->logical_block_size = ic->sectors_per_block << SECTOR_SHIFT;
3015 limits->physical_block_size = ic->sectors_per_block << SECTOR_SHIFT;
3016 blk_limits_io_min(limits, ic->sectors_per_block << SECTOR_SHIFT);
3017 }
3018}
3019
Mikulas Patocka7eada902017-01-04 20:23:53 +01003020static void calculate_journal_section_size(struct dm_integrity_c *ic)
3021{
3022 unsigned sector_space = JOURNAL_SECTOR_DATA;
3023
3024 ic->journal_sections = le32_to_cpu(ic->sb->journal_sections);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04003025 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 +01003026 JOURNAL_ENTRY_ROUNDUP);
3027
3028 if (ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC))
3029 sector_space -= JOURNAL_MAC_PER_SECTOR;
3030 ic->journal_entries_per_sector = sector_space / ic->journal_entry_size;
3031 ic->journal_section_entries = ic->journal_entries_per_sector * JOURNAL_BLOCK_SECTORS;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04003032 ic->journal_section_sectors = (ic->journal_section_entries << ic->sb->log2_sectors_per_block) + JOURNAL_BLOCK_SECTORS;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003033 ic->journal_entries = ic->journal_section_entries * ic->journal_sections;
3034}
3035
3036static int calculate_device_limits(struct dm_integrity_c *ic)
3037{
3038 __u64 initial_sectors;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003039
3040 calculate_journal_section_size(ic);
3041 initial_sectors = SB_SECTORS + (__u64)ic->journal_section_sectors * ic->journal_sections;
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003042 if (initial_sectors + METADATA_PADDING_SECTORS >= ic->meta_device_sectors || initial_sectors > UINT_MAX)
Mikulas Patocka7eada902017-01-04 20:23:53 +01003043 return -EINVAL;
3044 ic->initial_sectors = initial_sectors;
3045
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003046 if (!ic->meta_dev) {
3047 sector_t last_sector, last_area, last_offset;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003048
Mikulas Patockad5378582019-11-13 06:48:16 -05003049 /* we have to maintain excessive padding for compatibility with existing volumes */
3050 __u64 metadata_run_padding =
3051 ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_PADDING) ?
3052 (__u64)(METADATA_PADDING_SECTORS << SECTOR_SHIFT) :
3053 (__u64)(1 << SECTOR_SHIFT << METADATA_PADDING_SECTORS);
3054
3055 ic->metadata_run = round_up((__u64)ic->tag_size << (ic->sb->log2_interleave_sectors - ic->sb->log2_sectors_per_block),
3056 metadata_run_padding) >> SECTOR_SHIFT;
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003057 if (!(ic->metadata_run & (ic->metadata_run - 1)))
3058 ic->log2_metadata_run = __ffs(ic->metadata_run);
3059 else
3060 ic->log2_metadata_run = -1;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003061
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003062 get_area_and_offset(ic, ic->provided_data_sectors - 1, &last_area, &last_offset);
3063 last_sector = get_data_sector(ic, last_area, last_offset);
3064 if (last_sector < ic->start || last_sector >= ic->meta_device_sectors)
3065 return -EINVAL;
3066 } else {
Mikulas Patocka30bba432019-05-07 14:28:35 -04003067 __u64 meta_size = (ic->provided_data_sectors >> ic->sb->log2_sectors_per_block) * ic->tag_size;
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003068 meta_size = (meta_size + ((1U << (ic->log2_buffer_sectors + SECTOR_SHIFT)) - 1))
3069 >> (ic->log2_buffer_sectors + SECTOR_SHIFT);
3070 meta_size <<= ic->log2_buffer_sectors;
3071 if (ic->initial_sectors + meta_size < ic->initial_sectors ||
3072 ic->initial_sectors + meta_size > ic->meta_device_sectors)
3073 return -EINVAL;
3074 ic->metadata_run = 1;
3075 ic->log2_metadata_run = 0;
3076 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01003077
3078 return 0;
3079}
3080
3081static int initialize_superblock(struct dm_integrity_c *ic, unsigned journal_sectors, unsigned interleave_sectors)
3082{
3083 unsigned journal_sections;
3084 int test_bit;
3085
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003086 memset(ic->sb, 0, SB_SECTORS << SECTOR_SHIFT);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003087 memcpy(ic->sb->magic, SB_MAGIC, 8);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003088 ic->sb->integrity_tag_size = cpu_to_le16(ic->tag_size);
Mikulas Patocka9d609f852017-04-18 16:51:52 -04003089 ic->sb->log2_sectors_per_block = __ffs(ic->sectors_per_block);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003090 if (ic->journal_mac_alg.alg_string)
3091 ic->sb->flags |= cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC);
3092
3093 calculate_journal_section_size(ic);
3094 journal_sections = journal_sectors / ic->journal_section_sectors;
3095 if (!journal_sections)
3096 journal_sections = 1;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003097
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003098 if (!ic->meta_dev) {
Mikulas Patockad5378582019-11-13 06:48:16 -05003099 if (ic->fix_padding)
3100 ic->sb->flags |= cpu_to_le32(SB_FLAG_FIXED_PADDING);
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003101 ic->sb->journal_sections = cpu_to_le32(journal_sections);
3102 if (!interleave_sectors)
3103 interleave_sectors = DEFAULT_INTERLEAVE_SECTORS;
3104 ic->sb->log2_interleave_sectors = __fls(interleave_sectors);
3105 ic->sb->log2_interleave_sectors = max((__u8)MIN_LOG2_INTERLEAVE_SECTORS, ic->sb->log2_interleave_sectors);
3106 ic->sb->log2_interleave_sectors = min((__u8)MAX_LOG2_INTERLEAVE_SECTORS, ic->sb->log2_interleave_sectors);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003107
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003108 ic->provided_data_sectors = 0;
3109 for (test_bit = fls64(ic->meta_device_sectors) - 1; test_bit >= 3; test_bit--) {
3110 __u64 prev_data_sectors = ic->provided_data_sectors;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003111
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003112 ic->provided_data_sectors |= (sector_t)1 << test_bit;
3113 if (calculate_device_limits(ic))
3114 ic->provided_data_sectors = prev_data_sectors;
3115 }
3116 if (!ic->provided_data_sectors)
3117 return -EINVAL;
3118 } else {
3119 ic->sb->log2_interleave_sectors = 0;
3120 ic->provided_data_sectors = ic->data_device_sectors;
3121 ic->provided_data_sectors &= ~(sector_t)(ic->sectors_per_block - 1);
3122
3123try_smaller_buffer:
3124 ic->sb->journal_sections = cpu_to_le32(0);
3125 for (test_bit = fls(journal_sections) - 1; test_bit >= 0; test_bit--) {
3126 __u32 prev_journal_sections = le32_to_cpu(ic->sb->journal_sections);
3127 __u32 test_journal_sections = prev_journal_sections | (1U << test_bit);
3128 if (test_journal_sections > journal_sections)
3129 continue;
3130 ic->sb->journal_sections = cpu_to_le32(test_journal_sections);
3131 if (calculate_device_limits(ic))
3132 ic->sb->journal_sections = cpu_to_le32(prev_journal_sections);
3133
3134 }
3135 if (!le32_to_cpu(ic->sb->journal_sections)) {
3136 if (ic->log2_buffer_sectors > 3) {
3137 ic->log2_buffer_sectors--;
3138 goto try_smaller_buffer;
3139 }
3140 return -EINVAL;
3141 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01003142 }
3143
Mikulas Patocka7eada902017-01-04 20:23:53 +01003144 ic->sb->provided_data_sectors = cpu_to_le64(ic->provided_data_sectors);
3145
Mikulas Patocka1f9fc0b2018-07-03 20:13:31 +02003146 sb_set_version(ic);
3147
Mikulas Patocka7eada902017-01-04 20:23:53 +01003148 return 0;
3149}
3150
3151static void dm_integrity_set(struct dm_target *ti, struct dm_integrity_c *ic)
3152{
3153 struct gendisk *disk = dm_disk(dm_table_get_md(ti->table));
3154 struct blk_integrity bi;
3155
3156 memset(&bi, 0, sizeof(bi));
3157 bi.profile = &dm_integrity_profile;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04003158 bi.tuple_size = ic->tag_size;
3159 bi.tag_size = bi.tuple_size;
Mikulas Patocka84ff1bc2017-04-26 18:39:47 -04003160 bi.interval_exp = ic->sb->log2_sectors_per_block + SECTOR_SHIFT;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003161
3162 blk_integrity_register(disk, &bi);
3163 blk_queue_max_integrity_segments(disk->queue, UINT_MAX);
3164}
3165
Mikulas Patockad5027e02019-04-29 14:57:20 +02003166static void dm_integrity_free_page_list(struct page_list *pl)
Mikulas Patocka7eada902017-01-04 20:23:53 +01003167{
3168 unsigned i;
3169
3170 if (!pl)
3171 return;
Mikulas Patockad5027e02019-04-29 14:57:20 +02003172 for (i = 0; pl[i].page; i++)
3173 __free_page(pl[i].page);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003174 kvfree(pl);
3175}
3176
Mikulas Patockad5027e02019-04-29 14:57:20 +02003177static struct page_list *dm_integrity_alloc_page_list(unsigned n_pages)
Mikulas Patocka7eada902017-01-04 20:23:53 +01003178{
Mikulas Patocka7eada902017-01-04 20:23:53 +01003179 struct page_list *pl;
3180 unsigned i;
3181
Mikulas Patockad5027e02019-04-29 14:57:20 +02003182 pl = kvmalloc_array(n_pages + 1, sizeof(struct page_list), GFP_KERNEL | __GFP_ZERO);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003183 if (!pl)
3184 return NULL;
3185
Mikulas Patockad5027e02019-04-29 14:57:20 +02003186 for (i = 0; i < n_pages; i++) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01003187 pl[i].page = alloc_page(GFP_KERNEL);
3188 if (!pl[i].page) {
Mikulas Patockad5027e02019-04-29 14:57:20 +02003189 dm_integrity_free_page_list(pl);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003190 return NULL;
3191 }
3192 if (i)
3193 pl[i - 1].next = &pl[i];
3194 }
Mikulas Patockad5027e02019-04-29 14:57:20 +02003195 pl[i].page = NULL;
3196 pl[i].next = NULL;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003197
3198 return pl;
3199}
3200
3201static void dm_integrity_free_journal_scatterlist(struct dm_integrity_c *ic, struct scatterlist **sl)
3202{
3203 unsigned i;
3204 for (i = 0; i < ic->journal_sections; i++)
3205 kvfree(sl[i]);
Mikulas Patockafc8cec12018-04-17 18:32:26 -04003206 kvfree(sl);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003207}
3208
Mike Snitzer05d69092019-05-09 15:25:49 -04003209static struct scatterlist **dm_integrity_alloc_journal_scatterlist(struct dm_integrity_c *ic,
3210 struct page_list *pl)
Mikulas Patocka7eada902017-01-04 20:23:53 +01003211{
3212 struct scatterlist **sl;
3213 unsigned i;
3214
Kees Cook344476e2018-06-12 14:04:32 -07003215 sl = kvmalloc_array(ic->journal_sections,
3216 sizeof(struct scatterlist *),
3217 GFP_KERNEL | __GFP_ZERO);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003218 if (!sl)
3219 return NULL;
3220
3221 for (i = 0; i < ic->journal_sections; i++) {
3222 struct scatterlist *s;
3223 unsigned start_index, start_offset;
3224 unsigned end_index, end_offset;
3225 unsigned n_pages;
3226 unsigned idx;
3227
3228 page_list_location(ic, i, 0, &start_index, &start_offset);
Mike Snitzer05d69092019-05-09 15:25:49 -04003229 page_list_location(ic, i, ic->journal_section_sectors - 1,
3230 &end_index, &end_offset);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003231
3232 n_pages = (end_index - start_index + 1);
3233
Kees Cook344476e2018-06-12 14:04:32 -07003234 s = kvmalloc_array(n_pages, sizeof(struct scatterlist),
3235 GFP_KERNEL);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003236 if (!s) {
3237 dm_integrity_free_journal_scatterlist(ic, sl);
3238 return NULL;
3239 }
3240
3241 sg_init_table(s, n_pages);
3242 for (idx = start_index; idx <= end_index; idx++) {
3243 char *va = lowmem_page_address(pl[idx].page);
3244 unsigned start = 0, end = PAGE_SIZE;
3245 if (idx == start_index)
3246 start = start_offset;
3247 if (idx == end_index)
3248 end = end_offset + (1 << SECTOR_SHIFT);
3249 sg_set_buf(&s[idx - start_index], va + start, end - start);
3250 }
3251
3252 sl[i] = s;
3253 }
3254
3255 return sl;
3256}
3257
3258static void free_alg(struct alg_spec *a)
3259{
3260 kzfree(a->alg_string);
3261 kzfree(a->key);
3262 memset(a, 0, sizeof *a);
3263}
3264
3265static int get_alg_and_key(const char *arg, struct alg_spec *a, char **error, char *error_inval)
3266{
3267 char *k;
3268
3269 free_alg(a);
3270
3271 a->alg_string = kstrdup(strchr(arg, ':') + 1, GFP_KERNEL);
3272 if (!a->alg_string)
3273 goto nomem;
3274
3275 k = strchr(a->alg_string, ':');
3276 if (k) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01003277 *k = 0;
3278 a->key_string = k + 1;
3279 if (strlen(a->key_string) & 1)
3280 goto inval;
3281
3282 a->key_size = strlen(a->key_string) / 2;
3283 a->key = kmalloc(a->key_size, GFP_KERNEL);
3284 if (!a->key)
3285 goto nomem;
Mikulas Patocka6625d902017-04-27 11:49:33 -04003286 if (hex2bin(a->key, a->key_string, a->key_size))
3287 goto inval;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003288 }
3289
3290 return 0;
3291inval:
3292 *error = error_inval;
3293 return -EINVAL;
3294nomem:
3295 *error = "Out of memory for an argument";
3296 return -ENOMEM;
3297}
3298
3299static int get_mac(struct crypto_shash **hash, struct alg_spec *a, char **error,
3300 char *error_alg, char *error_key)
3301{
3302 int r;
3303
3304 if (a->alg_string) {
Eric Biggers3d234b32018-11-14 12:21:11 -08003305 *hash = crypto_alloc_shash(a->alg_string, 0, 0);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003306 if (IS_ERR(*hash)) {
3307 *error = error_alg;
3308 r = PTR_ERR(*hash);
3309 *hash = NULL;
3310 return r;
3311 }
3312
3313 if (a->key) {
3314 r = crypto_shash_setkey(*hash, a->key, a->key_size);
3315 if (r) {
3316 *error = error_key;
3317 return r;
3318 }
Milan Broze16b4f92018-02-13 14:50:50 +01003319 } else if (crypto_shash_get_flags(*hash) & CRYPTO_TFM_NEED_KEY) {
3320 *error = error_key;
3321 return -ENOKEY;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003322 }
3323 }
3324
3325 return 0;
3326}
3327
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003328static int create_journal(struct dm_integrity_c *ic, char **error)
3329{
3330 int r = 0;
3331 unsigned i;
3332 __u64 journal_pages, journal_desc_size, journal_tree_size;
Mikulas Patocka717f4b12018-01-10 09:32:47 -05003333 unsigned char *crypt_data = NULL, *crypt_iv = NULL;
3334 struct skcipher_request *req = NULL;
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003335
3336 ic->commit_ids[0] = cpu_to_le64(0x1111111111111111ULL);
3337 ic->commit_ids[1] = cpu_to_le64(0x2222222222222222ULL);
3338 ic->commit_ids[2] = cpu_to_le64(0x3333333333333333ULL);
3339 ic->commit_ids[3] = cpu_to_le64(0x4444444444444444ULL);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003340
3341 journal_pages = roundup((__u64)ic->journal_sections * ic->journal_section_sectors,
3342 PAGE_SIZE >> SECTOR_SHIFT) >> (PAGE_SHIFT - SECTOR_SHIFT);
3343 journal_desc_size = journal_pages * sizeof(struct page_list);
Arun KSca79b0c2018-12-28 00:34:29 -08003344 if (journal_pages >= totalram_pages() - totalhigh_pages() || journal_desc_size > ULONG_MAX) {
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003345 *error = "Journal doesn't fit into memory";
3346 r = -ENOMEM;
3347 goto bad;
3348 }
3349 ic->journal_pages = journal_pages;
3350
Mikulas Patockad5027e02019-04-29 14:57:20 +02003351 ic->journal = dm_integrity_alloc_page_list(ic->journal_pages);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003352 if (!ic->journal) {
3353 *error = "Could not allocate memory for journal";
3354 r = -ENOMEM;
3355 goto bad;
3356 }
3357 if (ic->journal_crypt_alg.alg_string) {
3358 unsigned ivsize, blocksize;
3359 struct journal_completion comp;
3360
3361 comp.ic = ic;
3362 ic->journal_crypt = crypto_alloc_skcipher(ic->journal_crypt_alg.alg_string, 0, 0);
3363 if (IS_ERR(ic->journal_crypt)) {
3364 *error = "Invalid journal cipher";
3365 r = PTR_ERR(ic->journal_crypt);
3366 ic->journal_crypt = NULL;
3367 goto bad;
3368 }
3369 ivsize = crypto_skcipher_ivsize(ic->journal_crypt);
3370 blocksize = crypto_skcipher_blocksize(ic->journal_crypt);
3371
3372 if (ic->journal_crypt_alg.key) {
3373 r = crypto_skcipher_setkey(ic->journal_crypt, ic->journal_crypt_alg.key,
3374 ic->journal_crypt_alg.key_size);
3375 if (r) {
3376 *error = "Error setting encryption key";
3377 goto bad;
3378 }
3379 }
3380 DEBUG_print("cipher %s, block size %u iv size %u\n",
3381 ic->journal_crypt_alg.alg_string, blocksize, ivsize);
3382
Mikulas Patockad5027e02019-04-29 14:57:20 +02003383 ic->journal_io = dm_integrity_alloc_page_list(ic->journal_pages);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003384 if (!ic->journal_io) {
3385 *error = "Could not allocate memory for journal io";
3386 r = -ENOMEM;
3387 goto bad;
3388 }
3389
3390 if (blocksize == 1) {
3391 struct scatterlist *sg;
Mikulas Patocka717f4b12018-01-10 09:32:47 -05003392
3393 req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
3394 if (!req) {
3395 *error = "Could not allocate crypt request";
3396 r = -ENOMEM;
3397 goto bad;
3398 }
3399
Fuqian Huang131670c2019-06-28 10:47:34 +08003400 crypt_iv = kzalloc(ivsize, GFP_KERNEL);
Mikulas Patocka717f4b12018-01-10 09:32:47 -05003401 if (!crypt_iv) {
3402 *error = "Could not allocate iv";
3403 r = -ENOMEM;
3404 goto bad;
3405 }
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003406
Mikulas Patockad5027e02019-04-29 14:57:20 +02003407 ic->journal_xor = dm_integrity_alloc_page_list(ic->journal_pages);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003408 if (!ic->journal_xor) {
3409 *error = "Could not allocate memory for journal xor";
3410 r = -ENOMEM;
3411 goto bad;
3412 }
3413
Kees Cook344476e2018-06-12 14:04:32 -07003414 sg = kvmalloc_array(ic->journal_pages + 1,
3415 sizeof(struct scatterlist),
3416 GFP_KERNEL);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003417 if (!sg) {
3418 *error = "Unable to allocate sg list";
3419 r = -ENOMEM;
3420 goto bad;
3421 }
3422 sg_init_table(sg, ic->journal_pages + 1);
3423 for (i = 0; i < ic->journal_pages; i++) {
3424 char *va = lowmem_page_address(ic->journal_xor[i].page);
3425 clear_page(va);
3426 sg_set_buf(&sg[i], va, PAGE_SIZE);
3427 }
3428 sg_set_buf(&sg[i], &ic->commit_ids, sizeof ic->commit_ids);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003429
Mike Snitzer05d69092019-05-09 15:25:49 -04003430 skcipher_request_set_crypt(req, sg, sg,
3431 PAGE_SIZE * ic->journal_pages + sizeof ic->commit_ids, crypt_iv);
Arnd Bergmannb5e8ad92017-08-15 17:11:59 +02003432 init_completion(&comp.comp);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003433 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
3434 if (do_crypt(true, req, &comp))
3435 wait_for_completion(&comp.comp);
3436 kvfree(sg);
3437 r = dm_integrity_failed(ic);
3438 if (r) {
3439 *error = "Unable to encrypt journal";
3440 goto bad;
3441 }
3442 DEBUG_bytes(lowmem_page_address(ic->journal_xor[0].page), 64, "xor data");
3443
3444 crypto_free_skcipher(ic->journal_crypt);
3445 ic->journal_crypt = NULL;
3446 } else {
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003447 unsigned crypt_len = roundup(ivsize, blocksize);
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003448
Mikulas Patocka717f4b12018-01-10 09:32:47 -05003449 req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
3450 if (!req) {
3451 *error = "Could not allocate crypt request";
3452 r = -ENOMEM;
3453 goto bad;
3454 }
3455
3456 crypt_iv = kmalloc(ivsize, GFP_KERNEL);
3457 if (!crypt_iv) {
3458 *error = "Could not allocate iv";
3459 r = -ENOMEM;
3460 goto bad;
3461 }
3462
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003463 crypt_data = kmalloc(crypt_len, GFP_KERNEL);
3464 if (!crypt_data) {
3465 *error = "Unable to allocate crypt data";
3466 r = -ENOMEM;
3467 goto bad;
3468 }
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003469
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003470 ic->journal_scatterlist = dm_integrity_alloc_journal_scatterlist(ic, ic->journal);
3471 if (!ic->journal_scatterlist) {
3472 *error = "Unable to allocate sg list";
3473 r = -ENOMEM;
3474 goto bad;
3475 }
3476 ic->journal_io_scatterlist = dm_integrity_alloc_journal_scatterlist(ic, ic->journal_io);
3477 if (!ic->journal_io_scatterlist) {
3478 *error = "Unable to allocate sg list";
3479 r = -ENOMEM;
3480 goto bad;
3481 }
Kees Cook344476e2018-06-12 14:04:32 -07003482 ic->sk_requests = kvmalloc_array(ic->journal_sections,
3483 sizeof(struct skcipher_request *),
3484 GFP_KERNEL | __GFP_ZERO);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003485 if (!ic->sk_requests) {
3486 *error = "Unable to allocate sk requests";
3487 r = -ENOMEM;
3488 goto bad;
3489 }
3490 for (i = 0; i < ic->journal_sections; i++) {
3491 struct scatterlist sg;
3492 struct skcipher_request *section_req;
3493 __u32 section_le = cpu_to_le32(i);
3494
Mikulas Patocka717f4b12018-01-10 09:32:47 -05003495 memset(crypt_iv, 0x00, ivsize);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003496 memset(crypt_data, 0x00, crypt_len);
3497 memcpy(crypt_data, &section_le, min((size_t)crypt_len, sizeof(section_le)));
3498
3499 sg_init_one(&sg, crypt_data, crypt_len);
Mikulas Patocka717f4b12018-01-10 09:32:47 -05003500 skcipher_request_set_crypt(req, &sg, &sg, crypt_len, crypt_iv);
Arnd Bergmannb5e8ad92017-08-15 17:11:59 +02003501 init_completion(&comp.comp);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003502 comp.in_flight = (atomic_t)ATOMIC_INIT(1);
3503 if (do_crypt(true, req, &comp))
3504 wait_for_completion(&comp.comp);
3505
3506 r = dm_integrity_failed(ic);
3507 if (r) {
3508 *error = "Unable to generate iv";
3509 goto bad;
3510 }
3511
3512 section_req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
3513 if (!section_req) {
3514 *error = "Unable to allocate crypt request";
3515 r = -ENOMEM;
3516 goto bad;
3517 }
Kees Cook6da2ec52018-06-12 13:55:00 -07003518 section_req->iv = kmalloc_array(ivsize, 2,
3519 GFP_KERNEL);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003520 if (!section_req->iv) {
3521 skcipher_request_free(section_req);
3522 *error = "Unable to allocate iv";
3523 r = -ENOMEM;
3524 goto bad;
3525 }
3526 memcpy(section_req->iv + ivsize, crypt_data, ivsize);
3527 section_req->cryptlen = (size_t)ic->journal_section_sectors << SECTOR_SHIFT;
3528 ic->sk_requests[i] = section_req;
3529 DEBUG_bytes(crypt_data, ivsize, "iv(%u)", i);
3530 }
3531 }
3532 }
3533
3534 for (i = 0; i < N_COMMIT_IDS; i++) {
3535 unsigned j;
3536retest_commit_id:
3537 for (j = 0; j < i; j++) {
3538 if (ic->commit_ids[j] == ic->commit_ids[i]) {
3539 ic->commit_ids[i] = cpu_to_le64(le64_to_cpu(ic->commit_ids[i]) + 1);
3540 goto retest_commit_id;
3541 }
3542 }
3543 DEBUG_print("commit id %u: %016llx\n", i, ic->commit_ids[i]);
3544 }
3545
3546 journal_tree_size = (__u64)ic->journal_entries * sizeof(struct journal_node);
3547 if (journal_tree_size > ULONG_MAX) {
3548 *error = "Journal doesn't fit into memory";
3549 r = -ENOMEM;
3550 goto bad;
3551 }
Mikulas Patocka702a6202017-05-20 14:56:21 -04003552 ic->journal_tree = kvmalloc(journal_tree_size, GFP_KERNEL);
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003553 if (!ic->journal_tree) {
3554 *error = "Could not allocate memory for journal tree";
3555 r = -ENOMEM;
3556 }
3557bad:
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003558 kfree(crypt_data);
Mikulas Patocka717f4b12018-01-10 09:32:47 -05003559 kfree(crypt_iv);
3560 skcipher_request_free(req);
3561
Mike Snitzer1aa0efd2017-03-17 14:56:17 -04003562 return r;
3563}
3564
Mikulas Patocka7eada902017-01-04 20:23:53 +01003565/*
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003566 * Construct a integrity mapping
Mikulas Patocka7eada902017-01-04 20:23:53 +01003567 *
3568 * Arguments:
3569 * device
3570 * offset from the start of the device
3571 * tag size
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003572 * D - direct writes, J - journal writes, B - bitmap mode, R - recovery mode
Mikulas Patocka7eada902017-01-04 20:23:53 +01003573 * number of optional arguments
3574 * optional arguments:
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003575 * journal_sectors
3576 * interleave_sectors
3577 * buffer_sectors
3578 * journal_watermark
3579 * commit_time
Mikulas Patocka88ad5d12019-04-29 14:57:23 +02003580 * meta_device
3581 * block_size
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003582 * sectors_per_bit
3583 * bitmap_flush_interval
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003584 * internal_hash
3585 * journal_crypt
3586 * journal_mac
Mikulas Patocka88ad5d12019-04-29 14:57:23 +02003587 * recalculate
Mikulas Patocka7eada902017-01-04 20:23:53 +01003588 */
3589static int dm_integrity_ctr(struct dm_target *ti, unsigned argc, char **argv)
3590{
3591 struct dm_integrity_c *ic;
3592 char dummy;
3593 int r;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003594 unsigned extra_args;
3595 struct dm_arg_set as;
Eric Biggers5916a222017-06-22 11:32:45 -07003596 static const struct dm_arg _args[] = {
Mikulas Patocka9d609f852017-04-18 16:51:52 -04003597 {0, 9, "Invalid number of feature args"},
Mikulas Patocka7eada902017-01-04 20:23:53 +01003598 };
3599 unsigned journal_sectors, interleave_sectors, buffer_sectors, journal_watermark, sync_msec;
3600 bool should_write_sb;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003601 __u64 threshold;
3602 unsigned long long start;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003603 __s8 log2_sectors_per_bitmap_bit = -1;
3604 __s8 log2_blocks_per_bitmap_bit;
3605 __u64 bits_in_journal;
3606 __u64 n_bitmap_bits;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003607
3608#define DIRECT_ARGUMENTS 4
3609
3610 if (argc <= DIRECT_ARGUMENTS) {
3611 ti->error = "Invalid argument count";
3612 return -EINVAL;
3613 }
3614
3615 ic = kzalloc(sizeof(struct dm_integrity_c), GFP_KERNEL);
3616 if (!ic) {
3617 ti->error = "Cannot allocate integrity context";
3618 return -ENOMEM;
3619 }
3620 ti->private = ic;
3621 ti->per_io_data_size = sizeof(struct dm_integrity_io);
Mikulas Patockaadc0daa2020-02-24 10:20:28 +01003622 ic->ti = ti;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003623
Mikulas Patocka7eada902017-01-04 20:23:53 +01003624 ic->in_progress = RB_ROOT;
Mikulas Patocka724376a2018-07-03 20:13:27 +02003625 INIT_LIST_HEAD(&ic->wait_list);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003626 init_waitqueue_head(&ic->endio_wait);
3627 bio_list_init(&ic->flush_bio_list);
3628 init_waitqueue_head(&ic->copy_to_journal_wait);
3629 init_completion(&ic->crypto_backoff);
Mikulas Patocka3f2e5392017-07-21 12:00:00 -04003630 atomic64_set(&ic->number_of_mismatches, 0);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003631 ic->bitmap_flush_interval = BITMAP_FLUSH_INTERVAL;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003632
3633 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &ic->dev);
3634 if (r) {
3635 ti->error = "Device lookup failed";
3636 goto bad;
3637 }
3638
3639 if (sscanf(argv[1], "%llu%c", &start, &dummy) != 1 || start != (sector_t)start) {
3640 ti->error = "Invalid starting offset";
3641 r = -EINVAL;
3642 goto bad;
3643 }
3644 ic->start = start;
3645
3646 if (strcmp(argv[2], "-")) {
3647 if (sscanf(argv[2], "%u%c", &ic->tag_size, &dummy) != 1 || !ic->tag_size) {
3648 ti->error = "Invalid tag size";
3649 r = -EINVAL;
3650 goto bad;
3651 }
3652 }
3653
Mike Snitzer05d69092019-05-09 15:25:49 -04003654 if (!strcmp(argv[3], "J") || !strcmp(argv[3], "B") ||
3655 !strcmp(argv[3], "D") || !strcmp(argv[3], "R")) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01003656 ic->mode = argv[3][0];
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003657 } else {
3658 ti->error = "Invalid mode (expecting J, B, D, R)";
Mikulas Patocka7eada902017-01-04 20:23:53 +01003659 r = -EINVAL;
3660 goto bad;
3661 }
3662
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003663 journal_sectors = 0;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003664 interleave_sectors = DEFAULT_INTERLEAVE_SECTORS;
3665 buffer_sectors = DEFAULT_BUFFER_SECTORS;
3666 journal_watermark = DEFAULT_JOURNAL_WATERMARK;
3667 sync_msec = DEFAULT_SYNC_MSEC;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04003668 ic->sectors_per_block = 1;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003669
3670 as.argc = argc - DIRECT_ARGUMENTS;
3671 as.argv = argv + DIRECT_ARGUMENTS;
3672 r = dm_read_arg_group(_args, &as, &extra_args, &ti->error);
3673 if (r)
3674 goto bad;
3675
3676 while (extra_args--) {
3677 const char *opt_string;
3678 unsigned val;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003679 unsigned long long llval;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003680 opt_string = dm_shift_arg(&as);
3681 if (!opt_string) {
3682 r = -EINVAL;
3683 ti->error = "Not enough feature arguments";
3684 goto bad;
3685 }
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003686 if (sscanf(opt_string, "journal_sectors:%u%c", &val, &dummy) == 1)
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003687 journal_sectors = val ? val : 1;
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003688 else if (sscanf(opt_string, "interleave_sectors:%u%c", &val, &dummy) == 1)
Mikulas Patocka7eada902017-01-04 20:23:53 +01003689 interleave_sectors = val;
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003690 else if (sscanf(opt_string, "buffer_sectors:%u%c", &val, &dummy) == 1)
Mikulas Patocka7eada902017-01-04 20:23:53 +01003691 buffer_sectors = val;
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003692 else if (sscanf(opt_string, "journal_watermark:%u%c", &val, &dummy) == 1 && val <= 100)
Mikulas Patocka7eada902017-01-04 20:23:53 +01003693 journal_watermark = val;
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003694 else if (sscanf(opt_string, "commit_time:%u%c", &val, &dummy) == 1)
Mikulas Patocka7eada902017-01-04 20:23:53 +01003695 sync_msec = val;
Mikulas Patocka0d74e6a2019-03-13 07:56:02 -04003696 else if (!strncmp(opt_string, "meta_device:", strlen("meta_device:"))) {
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003697 if (ic->meta_dev) {
3698 dm_put_device(ti, ic->meta_dev);
3699 ic->meta_dev = NULL;
3700 }
Mike Snitzer05d69092019-05-09 15:25:49 -04003701 r = dm_get_device(ti, strchr(opt_string, ':') + 1,
3702 dm_table_get_mode(ti->table), &ic->meta_dev);
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003703 if (r) {
3704 ti->error = "Device lookup failed";
3705 goto bad;
3706 }
3707 } else if (sscanf(opt_string, "block_size:%u%c", &val, &dummy) == 1) {
Mikulas Patocka9d609f852017-04-18 16:51:52 -04003708 if (val < 1 << SECTOR_SHIFT ||
3709 val > MAX_SECTORS_PER_BLOCK << SECTOR_SHIFT ||
3710 (val & (val -1))) {
3711 r = -EINVAL;
3712 ti->error = "Invalid block_size argument";
3713 goto bad;
3714 }
3715 ic->sectors_per_block = val >> SECTOR_SHIFT;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003716 } else if (sscanf(opt_string, "sectors_per_bit:%llu%c", &llval, &dummy) == 1) {
3717 log2_sectors_per_bitmap_bit = !llval ? 0 : __ilog2_u64(llval);
3718 } else if (sscanf(opt_string, "bitmap_flush_interval:%u%c", &val, &dummy) == 1) {
3719 if (val >= (uint64_t)UINT_MAX * 1000 / HZ) {
3720 r = -EINVAL;
3721 ti->error = "Invalid bitmap_flush_interval argument";
3722 }
3723 ic->bitmap_flush_interval = msecs_to_jiffies(val);
Mikulas Patocka0d74e6a2019-03-13 07:56:02 -04003724 } else if (!strncmp(opt_string, "internal_hash:", strlen("internal_hash:"))) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01003725 r = get_alg_and_key(opt_string, &ic->internal_hash_alg, &ti->error,
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003726 "Invalid internal_hash argument");
Mikulas Patocka7eada902017-01-04 20:23:53 +01003727 if (r)
3728 goto bad;
Mikulas Patocka0d74e6a2019-03-13 07:56:02 -04003729 } else if (!strncmp(opt_string, "journal_crypt:", strlen("journal_crypt:"))) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01003730 r = get_alg_and_key(opt_string, &ic->journal_crypt_alg, &ti->error,
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003731 "Invalid journal_crypt argument");
Mikulas Patocka7eada902017-01-04 20:23:53 +01003732 if (r)
3733 goto bad;
Mikulas Patocka0d74e6a2019-03-13 07:56:02 -04003734 } else if (!strncmp(opt_string, "journal_mac:", strlen("journal_mac:"))) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01003735 r = get_alg_and_key(opt_string, &ic->journal_mac_alg, &ti->error,
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003736 "Invalid journal_mac argument");
Mikulas Patocka7eada902017-01-04 20:23:53 +01003737 if (r)
3738 goto bad;
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02003739 } else if (!strcmp(opt_string, "recalculate")) {
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003740 ic->recalculate_flag = true;
Mikulas Patockad5378582019-11-13 06:48:16 -05003741 } else if (!strcmp(opt_string, "fix_padding")) {
3742 ic->fix_padding = true;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003743 } else {
3744 r = -EINVAL;
3745 ti->error = "Invalid argument";
3746 goto bad;
3747 }
3748 }
3749
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003750 ic->data_device_sectors = i_size_read(ic->dev->bdev->bd_inode) >> SECTOR_SHIFT;
3751 if (!ic->meta_dev)
3752 ic->meta_device_sectors = ic->data_device_sectors;
3753 else
3754 ic->meta_device_sectors = i_size_read(ic->meta_dev->bdev->bd_inode) >> SECTOR_SHIFT;
3755
3756 if (!journal_sectors) {
3757 journal_sectors = min((sector_t)DEFAULT_MAX_JOURNAL_SECTORS,
Mike Snitzer05d69092019-05-09 15:25:49 -04003758 ic->data_device_sectors >> DEFAULT_JOURNAL_SIZE_FACTOR);
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003759 }
3760
3761 if (!buffer_sectors)
3762 buffer_sectors = 1;
3763 ic->log2_buffer_sectors = min((int)__fls(buffer_sectors), 31 - SECTOR_SHIFT);
3764
Mikulas Patocka7eada902017-01-04 20:23:53 +01003765 r = get_mac(&ic->internal_hash, &ic->internal_hash_alg, &ti->error,
3766 "Invalid internal hash", "Error setting internal hash key");
3767 if (r)
3768 goto bad;
3769
3770 r = get_mac(&ic->journal_mac, &ic->journal_mac_alg, &ti->error,
3771 "Invalid journal mac", "Error setting journal mac key");
3772 if (r)
3773 goto bad;
3774
3775 if (!ic->tag_size) {
3776 if (!ic->internal_hash) {
3777 ti->error = "Unknown tag size";
3778 r = -EINVAL;
3779 goto bad;
3780 }
3781 ic->tag_size = crypto_shash_digestsize(ic->internal_hash);
3782 }
3783 if (ic->tag_size > MAX_TAG_SIZE) {
3784 ti->error = "Too big tag size";
3785 r = -EINVAL;
3786 goto bad;
3787 }
3788 if (!(ic->tag_size & (ic->tag_size - 1)))
3789 ic->log2_tag_size = __ffs(ic->tag_size);
3790 else
3791 ic->log2_tag_size = -1;
3792
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003793 if (ic->mode == 'B' && !ic->internal_hash) {
3794 r = -EINVAL;
3795 ti->error = "Bitmap mode can be only used with internal hash";
3796 goto bad;
3797 }
3798
Mikulas Patocka7eada902017-01-04 20:23:53 +01003799 ic->autocommit_jiffies = msecs_to_jiffies(sync_msec);
3800 ic->autocommit_msec = sync_msec;
Kees Cook8376d3c2017-10-16 17:01:48 -07003801 timer_setup(&ic->autocommit_timer, autocommit_fn, 0);
Mikulas Patocka7eada902017-01-04 20:23:53 +01003802
3803 ic->io = dm_io_client_create();
3804 if (IS_ERR(ic->io)) {
3805 r = PTR_ERR(ic->io);
3806 ic->io = NULL;
3807 ti->error = "Cannot allocate dm io";
3808 goto bad;
3809 }
3810
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003811 r = mempool_init_slab_pool(&ic->journal_io_mempool, JOURNAL_IO_MEMPOOL, journal_io_cache);
3812 if (r) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01003813 ti->error = "Cannot allocate mempool";
3814 goto bad;
3815 }
3816
3817 ic->metadata_wq = alloc_workqueue("dm-integrity-metadata",
3818 WQ_MEM_RECLAIM, METADATA_WORKQUEUE_MAX_ACTIVE);
3819 if (!ic->metadata_wq) {
3820 ti->error = "Cannot allocate workqueue";
3821 r = -ENOMEM;
3822 goto bad;
3823 }
3824
3825 /*
3826 * If this workqueue were percpu, it would cause bio reordering
3827 * and reduced performance.
3828 */
3829 ic->wait_wq = alloc_workqueue("dm-integrity-wait", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
3830 if (!ic->wait_wq) {
3831 ti->error = "Cannot allocate workqueue";
3832 r = -ENOMEM;
3833 goto bad;
3834 }
3835
Mikulas Patocka53770f02020-02-17 07:43:03 -05003836 ic->offload_wq = alloc_workqueue("dm-integrity-offload", WQ_MEM_RECLAIM,
3837 METADATA_WORKQUEUE_MAX_ACTIVE);
3838 if (!ic->offload_wq) {
3839 ti->error = "Cannot allocate workqueue";
3840 r = -ENOMEM;
3841 goto bad;
3842 }
3843
Mikulas Patocka7eada902017-01-04 20:23:53 +01003844 ic->commit_wq = alloc_workqueue("dm-integrity-commit", WQ_MEM_RECLAIM, 1);
3845 if (!ic->commit_wq) {
3846 ti->error = "Cannot allocate workqueue";
3847 r = -ENOMEM;
3848 goto bad;
3849 }
3850 INIT_WORK(&ic->commit_work, integrity_commit);
3851
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003852 if (ic->mode == 'J' || ic->mode == 'B') {
Mikulas Patocka7eada902017-01-04 20:23:53 +01003853 ic->writer_wq = alloc_workqueue("dm-integrity-writer", WQ_MEM_RECLAIM, 1);
3854 if (!ic->writer_wq) {
3855 ti->error = "Cannot allocate workqueue";
3856 r = -ENOMEM;
3857 goto bad;
3858 }
3859 INIT_WORK(&ic->writer_work, integrity_writer);
3860 }
3861
3862 ic->sb = alloc_pages_exact(SB_SECTORS << SECTOR_SHIFT, GFP_KERNEL);
3863 if (!ic->sb) {
3864 r = -ENOMEM;
3865 ti->error = "Cannot allocate superblock area";
3866 goto bad;
3867 }
3868
3869 r = sync_rw_sb(ic, REQ_OP_READ, 0);
3870 if (r) {
3871 ti->error = "Error reading superblock";
3872 goto bad;
3873 }
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04003874 should_write_sb = false;
3875 if (memcmp(ic->sb->magic, SB_MAGIC, 8)) {
3876 if (ic->mode != 'R') {
Mikulas Patocka56b67a42017-04-18 16:51:50 -04003877 if (memchr_inv(ic->sb, 0, SB_SECTORS << SECTOR_SHIFT)) {
3878 r = -EINVAL;
3879 ti->error = "The device is not initialized";
3880 goto bad;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003881 }
3882 }
3883
3884 r = initialize_superblock(ic, journal_sectors, interleave_sectors);
3885 if (r) {
3886 ti->error = "Could not initialize superblock";
3887 goto bad;
3888 }
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04003889 if (ic->mode != 'R')
3890 should_write_sb = true;
Mikulas Patocka7eada902017-01-04 20:23:53 +01003891 }
3892
Mikulas Patockad5378582019-11-13 06:48:16 -05003893 if (!ic->sb->version || ic->sb->version > SB_VERSION_4) {
Mikulas Patocka7eada902017-01-04 20:23:53 +01003894 r = -EINVAL;
3895 ti->error = "Unknown version";
3896 goto bad;
3897 }
3898 if (le16_to_cpu(ic->sb->integrity_tag_size) != ic->tag_size) {
3899 r = -EINVAL;
Mikulas Patocka9d609f852017-04-18 16:51:52 -04003900 ti->error = "Tag size doesn't match the information in superblock";
3901 goto bad;
3902 }
3903 if (ic->sb->log2_sectors_per_block != __ffs(ic->sectors_per_block)) {
3904 r = -EINVAL;
3905 ti->error = "Block size doesn't match the information in superblock";
Mikulas Patocka7eada902017-01-04 20:23:53 +01003906 goto bad;
3907 }
Mikulas Patockabc86a412017-07-21 11:58:38 -04003908 if (!le32_to_cpu(ic->sb->journal_sections)) {
3909 r = -EINVAL;
3910 ti->error = "Corrupted superblock, journal_sections is 0";
3911 goto bad;
3912 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01003913 /* make sure that ti->max_io_len doesn't overflow */
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003914 if (!ic->meta_dev) {
3915 if (ic->sb->log2_interleave_sectors < MIN_LOG2_INTERLEAVE_SECTORS ||
3916 ic->sb->log2_interleave_sectors > MAX_LOG2_INTERLEAVE_SECTORS) {
3917 r = -EINVAL;
3918 ti->error = "Invalid interleave_sectors in the superblock";
3919 goto bad;
3920 }
3921 } else {
3922 if (ic->sb->log2_interleave_sectors) {
3923 r = -EINVAL;
3924 ti->error = "Invalid interleave_sectors in the superblock";
3925 goto bad;
3926 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01003927 }
3928 ic->provided_data_sectors = le64_to_cpu(ic->sb->provided_data_sectors);
3929 if (ic->provided_data_sectors != le64_to_cpu(ic->sb->provided_data_sectors)) {
3930 /* test for overflow */
3931 r = -EINVAL;
3932 ti->error = "The superblock has 64-bit device size, but the kernel was compiled with 32-bit sectors";
3933 goto bad;
3934 }
3935 if (!!(ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC)) != !!ic->journal_mac_alg.alg_string) {
3936 r = -EINVAL;
3937 ti->error = "Journal mac mismatch";
3938 goto bad;
3939 }
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003940
3941try_smaller_buffer:
Mikulas Patocka7eada902017-01-04 20:23:53 +01003942 r = calculate_device_limits(ic);
3943 if (r) {
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003944 if (ic->meta_dev) {
3945 if (ic->log2_buffer_sectors > 3) {
3946 ic->log2_buffer_sectors--;
3947 goto try_smaller_buffer;
3948 }
3949 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01003950 ti->error = "The device is too small";
3951 goto bad;
3952 }
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003953
3954 if (log2_sectors_per_bitmap_bit < 0)
3955 log2_sectors_per_bitmap_bit = __fls(DEFAULT_SECTORS_PER_BITMAP_BIT);
3956 if (log2_sectors_per_bitmap_bit < ic->sb->log2_sectors_per_block)
3957 log2_sectors_per_bitmap_bit = ic->sb->log2_sectors_per_block;
3958
3959 bits_in_journal = ((__u64)ic->journal_section_sectors * ic->journal_sections) << (SECTOR_SHIFT + 3);
3960 if (bits_in_journal > UINT_MAX)
3961 bits_in_journal = UINT_MAX;
3962 while (bits_in_journal < (ic->provided_data_sectors + ((sector_t)1 << log2_sectors_per_bitmap_bit) - 1) >> log2_sectors_per_bitmap_bit)
3963 log2_sectors_per_bitmap_bit++;
3964
3965 log2_blocks_per_bitmap_bit = log2_sectors_per_bitmap_bit - ic->sb->log2_sectors_per_block;
3966 ic->log2_blocks_per_bitmap_bit = log2_blocks_per_bitmap_bit;
3967 if (should_write_sb) {
3968 ic->sb->log2_blocks_per_bitmap_bit = log2_blocks_per_bitmap_bit;
3969 }
3970 n_bitmap_bits = ((ic->provided_data_sectors >> ic->sb->log2_sectors_per_block)
3971 + (((sector_t)1 << log2_blocks_per_bitmap_bit) - 1)) >> log2_blocks_per_bitmap_bit;
3972 ic->n_bitmap_blocks = DIV_ROUND_UP(n_bitmap_bits, BITMAP_BLOCK_SIZE * 8);
3973
Mikulas Patocka356d9d52018-07-03 20:13:30 +02003974 if (!ic->meta_dev)
3975 ic->log2_buffer_sectors = min(ic->log2_buffer_sectors, (__u8)__ffs(ic->metadata_run));
3976
Ondrej Mosnáček2ad50602017-06-05 17:52:39 +02003977 if (ti->len > ic->provided_data_sectors) {
3978 r = -EINVAL;
3979 ti->error = "Not enough provided sectors for requested mapping size";
3980 goto bad;
3981 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01003982
Mikulas Patocka7eada902017-01-04 20:23:53 +01003983
3984 threshold = (__u64)ic->journal_entries * (100 - journal_watermark);
3985 threshold += 50;
3986 do_div(threshold, 100);
3987 ic->free_sectors_threshold = threshold;
3988
3989 DEBUG_print("initialized:\n");
3990 DEBUG_print(" integrity_tag_size %u\n", le16_to_cpu(ic->sb->integrity_tag_size));
3991 DEBUG_print(" journal_entry_size %u\n", ic->journal_entry_size);
3992 DEBUG_print(" journal_entries_per_sector %u\n", ic->journal_entries_per_sector);
3993 DEBUG_print(" journal_section_entries %u\n", ic->journal_section_entries);
3994 DEBUG_print(" journal_section_sectors %u\n", ic->journal_section_sectors);
3995 DEBUG_print(" journal_sections %u\n", (unsigned)le32_to_cpu(ic->sb->journal_sections));
3996 DEBUG_print(" journal_entries %u\n", ic->journal_entries);
3997 DEBUG_print(" log2_interleave_sectors %d\n", ic->sb->log2_interleave_sectors);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02003998 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 +01003999 DEBUG_print(" initial_sectors 0x%x\n", ic->initial_sectors);
4000 DEBUG_print(" metadata_run 0x%x\n", ic->metadata_run);
4001 DEBUG_print(" log2_metadata_run %d\n", ic->log2_metadata_run);
Mikulas Patocka76491942020-03-22 20:42:22 +01004002 DEBUG_print(" provided_data_sectors 0x%llx (%llu)\n", ic->provided_data_sectors, ic->provided_data_sectors);
Mikulas Patocka7eada902017-01-04 20:23:53 +01004003 DEBUG_print(" log2_buffer_sectors %u\n", ic->log2_buffer_sectors);
Mikulas Patocka76491942020-03-22 20:42:22 +01004004 DEBUG_print(" bits_in_journal %llu\n", bits_in_journal);
Mikulas Patocka7eada902017-01-04 20:23:53 +01004005
Mikulas Patocka468dfca2019-04-29 14:57:24 +02004006 if (ic->recalculate_flag && !(ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))) {
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02004007 ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
4008 ic->sb->recalc_sector = cpu_to_le64(0);
4009 }
4010
Mikulas Patocka468dfca2019-04-29 14:57:24 +02004011 if (ic->internal_hash) {
Colin Ian Kinge8c25662018-11-28 15:15:31 +00004012 ic->recalc_wq = alloc_workqueue("dm-integrity-recalc", WQ_MEM_RECLAIM, 1);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02004013 if (!ic->recalc_wq ) {
4014 ti->error = "Cannot allocate workqueue";
4015 r = -ENOMEM;
4016 goto bad;
4017 }
4018 INIT_WORK(&ic->recalc_work, integrity_recalc);
4019 ic->recalc_buffer = vmalloc(RECALC_SECTORS << SECTOR_SHIFT);
4020 if (!ic->recalc_buffer) {
4021 ti->error = "Cannot allocate buffer for recalculating";
4022 r = -ENOMEM;
4023 goto bad;
4024 }
Kees Cook329e0982018-10-05 16:21:46 -07004025 ic->recalc_tags = kvmalloc_array(RECALC_SECTORS >> ic->sb->log2_sectors_per_block,
4026 ic->tag_size, GFP_KERNEL);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02004027 if (!ic->recalc_tags) {
4028 ti->error = "Cannot allocate tags for recalculating";
4029 r = -ENOMEM;
4030 goto bad;
4031 }
4032 }
4033
Mikulas Patocka356d9d52018-07-03 20:13:30 +02004034 ic->bufio = dm_bufio_client_create(ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev,
4035 1U << (SECTOR_SHIFT + ic->log2_buffer_sectors), 1, 0, NULL, NULL);
Mikulas Patocka7eada902017-01-04 20:23:53 +01004036 if (IS_ERR(ic->bufio)) {
4037 r = PTR_ERR(ic->bufio);
4038 ti->error = "Cannot initialize dm-bufio";
4039 ic->bufio = NULL;
4040 goto bad;
4041 }
4042 dm_bufio_set_sector_offset(ic->bufio, ic->start + ic->initial_sectors);
4043
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04004044 if (ic->mode != 'R') {
4045 r = create_journal(ic, &ti->error);
4046 if (r)
4047 goto bad;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02004048
4049 }
4050
4051 if (ic->mode == 'B') {
4052 unsigned i;
4053 unsigned n_bitmap_pages = DIV_ROUND_UP(ic->n_bitmap_blocks, PAGE_SIZE / BITMAP_BLOCK_SIZE);
4054
4055 ic->recalc_bitmap = dm_integrity_alloc_page_list(n_bitmap_pages);
4056 if (!ic->recalc_bitmap) {
4057 r = -ENOMEM;
4058 goto bad;
4059 }
4060 ic->may_write_bitmap = dm_integrity_alloc_page_list(n_bitmap_pages);
4061 if (!ic->may_write_bitmap) {
4062 r = -ENOMEM;
4063 goto bad;
4064 }
4065 ic->bbs = kvmalloc_array(ic->n_bitmap_blocks, sizeof(struct bitmap_block_status), GFP_KERNEL);
4066 if (!ic->bbs) {
4067 r = -ENOMEM;
4068 goto bad;
4069 }
4070 INIT_DELAYED_WORK(&ic->bitmap_flush_work, bitmap_flush_work);
4071 for (i = 0; i < ic->n_bitmap_blocks; i++) {
4072 struct bitmap_block_status *bbs = &ic->bbs[i];
4073 unsigned sector, pl_index, pl_offset;
4074
4075 INIT_WORK(&bbs->work, bitmap_block_work);
4076 bbs->ic = ic;
4077 bbs->idx = i;
4078 bio_list_init(&bbs->bio_queue);
4079 spin_lock_init(&bbs->bio_queue_lock);
4080
4081 sector = i * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT);
4082 pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
4083 pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
4084
4085 bbs->bitmap = lowmem_page_address(ic->journal[pl_index].page) + pl_offset;
4086 }
Mikulas Patockac2bcb2b2017-03-17 12:40:51 -04004087 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01004088
4089 if (should_write_sb) {
4090 int r;
4091
4092 init_journal(ic, 0, ic->journal_sections, 0);
4093 r = dm_integrity_failed(ic);
4094 if (unlikely(r)) {
4095 ti->error = "Error initializing journal";
4096 goto bad;
4097 }
4098 r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
4099 if (r) {
4100 ti->error = "Error initializing superblock";
4101 goto bad;
4102 }
4103 ic->just_formatted = true;
4104 }
4105
Mikulas Patocka356d9d52018-07-03 20:13:30 +02004106 if (!ic->meta_dev) {
4107 r = dm_set_target_max_io_len(ti, 1U << ic->sb->log2_interleave_sectors);
4108 if (r)
4109 goto bad;
4110 }
Mikulas Patocka468dfca2019-04-29 14:57:24 +02004111 if (ic->mode == 'B') {
4112 unsigned max_io_len = ((sector_t)ic->sectors_per_block << ic->log2_blocks_per_bitmap_bit) * (BITMAP_BLOCK_SIZE * 8);
4113 if (!max_io_len)
4114 max_io_len = 1U << 31;
4115 DEBUG_print("max_io_len: old %u, new %u\n", ti->max_io_len, max_io_len);
4116 if (!ti->max_io_len || ti->max_io_len > max_io_len) {
4117 r = dm_set_target_max_io_len(ti, max_io_len);
4118 if (r)
4119 goto bad;
4120 }
4121 }
Mikulas Patocka7eada902017-01-04 20:23:53 +01004122
4123 if (!ic->internal_hash)
4124 dm_integrity_set(ti, ic);
4125
4126 ti->num_flush_bios = 1;
4127 ti->flush_supported = true;
4128
4129 return 0;
Mikulas Patocka468dfca2019-04-29 14:57:24 +02004130
Mikulas Patocka7eada902017-01-04 20:23:53 +01004131bad:
4132 dm_integrity_dtr(ti);
4133 return r;
4134}
4135
4136static void dm_integrity_dtr(struct dm_target *ti)
4137{
4138 struct dm_integrity_c *ic = ti->private;
4139
4140 BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress));
Mikulas Patocka724376a2018-07-03 20:13:27 +02004141 BUG_ON(!list_empty(&ic->wait_list));
Mikulas Patocka7eada902017-01-04 20:23:53 +01004142
4143 if (ic->metadata_wq)
4144 destroy_workqueue(ic->metadata_wq);
4145 if (ic->wait_wq)
4146 destroy_workqueue(ic->wait_wq);
Mikulas Patocka53770f02020-02-17 07:43:03 -05004147 if (ic->offload_wq)
4148 destroy_workqueue(ic->offload_wq);
Mikulas Patocka7eada902017-01-04 20:23:53 +01004149 if (ic->commit_wq)
4150 destroy_workqueue(ic->commit_wq);
4151 if (ic->writer_wq)
4152 destroy_workqueue(ic->writer_wq);
Mikulas Patockaa3fcf722018-07-03 20:13:33 +02004153 if (ic->recalc_wq)
4154 destroy_workqueue(ic->recalc_wq);
Mikulas Patocka97abfde2019-04-29 14:57:17 +02004155 vfree(ic->recalc_buffer);
4156 kvfree(ic->recalc_tags);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02004157 kvfree(ic->bbs);
Mikulas Patocka7eada902017-01-04 20:23:53 +01004158 if (ic->bufio)
4159 dm_bufio_client_destroy(ic->bufio);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04004160 mempool_exit(&ic->journal_io_mempool);
Mikulas Patocka7eada902017-01-04 20:23:53 +01004161 if (ic->io)
4162 dm_io_client_destroy(ic->io);
4163 if (ic->dev)
4164 dm_put_device(ti, ic->dev);
Mikulas Patocka356d9d52018-07-03 20:13:30 +02004165 if (ic->meta_dev)
4166 dm_put_device(ti, ic->meta_dev);
Mikulas Patockad5027e02019-04-29 14:57:20 +02004167 dm_integrity_free_page_list(ic->journal);
4168 dm_integrity_free_page_list(ic->journal_io);
4169 dm_integrity_free_page_list(ic->journal_xor);
Mikulas Patocka468dfca2019-04-29 14:57:24 +02004170 dm_integrity_free_page_list(ic->recalc_bitmap);
4171 dm_integrity_free_page_list(ic->may_write_bitmap);
Mikulas Patocka7eada902017-01-04 20:23:53 +01004172 if (ic->journal_scatterlist)
4173 dm_integrity_free_journal_scatterlist(ic, ic->journal_scatterlist);
4174 if (ic->journal_io_scatterlist)
4175 dm_integrity_free_journal_scatterlist(ic, ic->journal_io_scatterlist);
4176 if (ic->sk_requests) {
4177 unsigned i;
4178
4179 for (i = 0; i < ic->journal_sections; i++) {
4180 struct skcipher_request *req = ic->sk_requests[i];
4181 if (req) {
4182 kzfree(req->iv);
4183 skcipher_request_free(req);
4184 }
4185 }
4186 kvfree(ic->sk_requests);
4187 }
4188 kvfree(ic->journal_tree);
4189 if (ic->sb)
4190 free_pages_exact(ic->sb, SB_SECTORS << SECTOR_SHIFT);
4191
4192 if (ic->internal_hash)
4193 crypto_free_shash(ic->internal_hash);
4194 free_alg(&ic->internal_hash_alg);
4195
4196 if (ic->journal_crypt)
4197 crypto_free_skcipher(ic->journal_crypt);
4198 free_alg(&ic->journal_crypt_alg);
4199
4200 if (ic->journal_mac)
4201 crypto_free_shash(ic->journal_mac);
4202 free_alg(&ic->journal_mac_alg);
4203
4204 kfree(ic);
4205}
4206
4207static struct target_type integrity_target = {
4208 .name = "integrity",
Mike Snitzer636be422020-02-27 14:25:31 -05004209 .version = {1, 5, 0},
Mikulas Patocka7eada902017-01-04 20:23:53 +01004210 .module = THIS_MODULE,
4211 .features = DM_TARGET_SINGLETON | DM_TARGET_INTEGRITY,
4212 .ctr = dm_integrity_ctr,
4213 .dtr = dm_integrity_dtr,
4214 .map = dm_integrity_map,
4215 .postsuspend = dm_integrity_postsuspend,
4216 .resume = dm_integrity_resume,
4217 .status = dm_integrity_status,
4218 .iterate_devices = dm_integrity_iterate_devices,
Mikulas Patocka9d609f852017-04-18 16:51:52 -04004219 .io_hints = dm_integrity_io_hints,
Mikulas Patocka7eada902017-01-04 20:23:53 +01004220};
4221
YueHaibing5efedc92019-03-22 22:16:34 +08004222static int __init dm_integrity_init(void)
Mikulas Patocka7eada902017-01-04 20:23:53 +01004223{
4224 int r;
4225
4226 journal_io_cache = kmem_cache_create("integrity_journal_io",
4227 sizeof(struct journal_io), 0, 0, NULL);
4228 if (!journal_io_cache) {
4229 DMERR("can't allocate journal io cache");
4230 return -ENOMEM;
4231 }
4232
4233 r = dm_register_target(&integrity_target);
4234
4235 if (r < 0)
4236 DMERR("register failed %d", r);
4237
4238 return r;
4239}
4240
YueHaibing5efedc92019-03-22 22:16:34 +08004241static void __exit dm_integrity_exit(void)
Mikulas Patocka7eada902017-01-04 20:23:53 +01004242{
4243 dm_unregister_target(&integrity_target);
4244 kmem_cache_destroy(journal_io_cache);
4245}
4246
4247module_init(dm_integrity_init);
4248module_exit(dm_integrity_exit);
4249
4250MODULE_AUTHOR("Milan Broz");
4251MODULE_AUTHOR("Mikulas Patocka");
4252MODULE_DESCRIPTION(DM_NAME " target for integrity tags extension");
4253MODULE_LICENSE("GPL");