b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016-2017 Red Hat, Inc. All rights reserved. |
| 3 | * Copyright (C) 2016-2017 Milan Broz |
| 4 | * Copyright (C) 2016-2017 Mikulas Patocka |
| 5 | * |
| 6 | * This file is released under the GPL. |
| 7 | */ |
| 8 | |
| 9 | #include "dm-bio-record.h" |
| 10 | |
| 11 | #include <linux/compiler.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/device-mapper.h> |
| 14 | #include <linux/dm-io.h> |
| 15 | #include <linux/vmalloc.h> |
| 16 | #include <linux/sort.h> |
| 17 | #include <linux/rbtree.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/random.h> |
| 20 | #include <linux/reboot.h> |
| 21 | #include <crypto/hash.h> |
| 22 | #include <crypto/skcipher.h> |
| 23 | #include <linux/async_tx.h> |
| 24 | #include <linux/dm-bufio.h> |
| 25 | |
| 26 | #define DM_MSG_PREFIX "integrity" |
| 27 | |
| 28 | #define DEFAULT_INTERLEAVE_SECTORS 32768 |
| 29 | #define DEFAULT_JOURNAL_SIZE_FACTOR 7 |
| 30 | #define DEFAULT_SECTORS_PER_BITMAP_BIT 32768 |
| 31 | #define DEFAULT_BUFFER_SECTORS 128 |
| 32 | #define DEFAULT_JOURNAL_WATERMARK 50 |
| 33 | #define DEFAULT_SYNC_MSEC 10000 |
| 34 | #define DEFAULT_MAX_JOURNAL_SECTORS (IS_ENABLED(CONFIG_64BIT) ? 131072 : 8192) |
| 35 | #define MIN_LOG2_INTERLEAVE_SECTORS 3 |
| 36 | #define MAX_LOG2_INTERLEAVE_SECTORS 31 |
| 37 | #define METADATA_WORKQUEUE_MAX_ACTIVE 16 |
| 38 | #define RECALC_SECTORS (IS_ENABLED(CONFIG_64BIT) ? 32768 : 2048) |
| 39 | #define RECALC_WRITE_SUPER 16 |
| 40 | #define BITMAP_BLOCK_SIZE 4096 /* don't change it */ |
| 41 | #define BITMAP_FLUSH_INTERVAL (10 * HZ) |
| 42 | |
| 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" |
| 55 | #define SB_VERSION_1 1 |
| 56 | #define SB_VERSION_2 2 |
| 57 | #define SB_VERSION_3 3 |
| 58 | #define SB_SECTORS 8 |
| 59 | #define MAX_SECTORS_PER_BLOCK 8 |
| 60 | |
| 61 | struct superblock { |
| 62 | __u8 magic[8]; |
| 63 | __u8 version; |
| 64 | __u8 log2_interleave_sectors; |
| 65 | __u16 integrity_tag_size; |
| 66 | __u32 journal_sections; |
| 67 | __u64 provided_data_sectors; /* userspace uses this value */ |
| 68 | __u32 flags; |
| 69 | __u8 log2_sectors_per_block; |
| 70 | __u8 log2_blocks_per_bitmap_bit; |
| 71 | __u8 pad[2]; |
| 72 | __u64 recalc_sector; |
| 73 | }; |
| 74 | |
| 75 | #define SB_FLAG_HAVE_JOURNAL_MAC 0x1 |
| 76 | #define SB_FLAG_RECALCULATING 0x2 |
| 77 | #define SB_FLAG_DIRTY_BITMAP 0x4 |
| 78 | |
| 79 | #define JOURNAL_ENTRY_ROUNDUP 8 |
| 80 | |
| 81 | typedef __u64 commit_id_t; |
| 82 | #define JOURNAL_MAC_PER_SECTOR 8 |
| 83 | |
| 84 | struct journal_entry { |
| 85 | union { |
| 86 | struct { |
| 87 | __u32 sector_lo; |
| 88 | __u32 sector_hi; |
| 89 | } s; |
| 90 | __u64 sector; |
| 91 | } u; |
| 92 | commit_id_t last_bytes[0]; |
| 93 | /* __u8 tag[0]; */ |
| 94 | }; |
| 95 | |
| 96 | #define journal_entry_tag(ic, je) ((__u8 *)&(je)->last_bytes[(ic)->sectors_per_block]) |
| 97 | |
| 98 | #if BITS_PER_LONG == 64 |
| 99 | #define journal_entry_set_sector(je, x) do { smp_wmb(); WRITE_ONCE((je)->u.sector, cpu_to_le64(x)); } while (0) |
| 100 | #else |
| 101 | #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) |
| 102 | #endif |
| 103 | #define journal_entry_get_sector(je) le64_to_cpu((je)->u.sector) |
| 104 | #define journal_entry_is_unused(je) ((je)->u.s.sector_hi == cpu_to_le32(-1)) |
| 105 | #define journal_entry_set_unused(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-1)); } while (0) |
| 106 | #define journal_entry_is_inprogress(je) ((je)->u.s.sector_hi == cpu_to_le32(-2)) |
| 107 | #define journal_entry_set_inprogress(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-2)); } while (0) |
| 108 | |
| 109 | #define JOURNAL_BLOCK_SECTORS 8 |
| 110 | #define JOURNAL_SECTOR_DATA ((1 << SECTOR_SHIFT) - sizeof(commit_id_t)) |
| 111 | #define JOURNAL_MAC_SIZE (JOURNAL_MAC_PER_SECTOR * JOURNAL_BLOCK_SECTORS) |
| 112 | |
| 113 | struct journal_sector { |
| 114 | __u8 entries[JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR]; |
| 115 | __u8 mac[JOURNAL_MAC_PER_SECTOR]; |
| 116 | commit_id_t commit_id; |
| 117 | }; |
| 118 | |
| 119 | #define MAX_TAG_SIZE (JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR - offsetof(struct journal_entry, last_bytes[MAX_SECTORS_PER_BLOCK])) |
| 120 | |
| 121 | #define METADATA_PADDING_SECTORS 8 |
| 122 | |
| 123 | #define N_COMMIT_IDS 4 |
| 124 | |
| 125 | static unsigned char prev_commit_seq(unsigned char seq) |
| 126 | { |
| 127 | return (seq + N_COMMIT_IDS - 1) % N_COMMIT_IDS; |
| 128 | } |
| 129 | |
| 130 | static unsigned char next_commit_seq(unsigned char seq) |
| 131 | { |
| 132 | return (seq + 1) % N_COMMIT_IDS; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * In-memory structures |
| 137 | */ |
| 138 | |
| 139 | struct journal_node { |
| 140 | struct rb_node node; |
| 141 | sector_t sector; |
| 142 | }; |
| 143 | |
| 144 | struct alg_spec { |
| 145 | char *alg_string; |
| 146 | char *key_string; |
| 147 | __u8 *key; |
| 148 | unsigned key_size; |
| 149 | }; |
| 150 | |
| 151 | struct dm_integrity_c { |
| 152 | struct dm_dev *dev; |
| 153 | struct dm_dev *meta_dev; |
| 154 | unsigned tag_size; |
| 155 | __s8 log2_tag_size; |
| 156 | sector_t start; |
| 157 | mempool_t journal_io_mempool; |
| 158 | struct dm_io_client *io; |
| 159 | struct dm_bufio_client *bufio; |
| 160 | struct workqueue_struct *metadata_wq; |
| 161 | struct superblock *sb; |
| 162 | unsigned journal_pages; |
| 163 | unsigned n_bitmap_blocks; |
| 164 | |
| 165 | struct page_list *journal; |
| 166 | struct page_list *journal_io; |
| 167 | struct page_list *journal_xor; |
| 168 | struct page_list *recalc_bitmap; |
| 169 | struct page_list *may_write_bitmap; |
| 170 | struct bitmap_block_status *bbs; |
| 171 | unsigned bitmap_flush_interval; |
| 172 | int synchronous_mode; |
| 173 | struct bio_list synchronous_bios; |
| 174 | struct delayed_work bitmap_flush_work; |
| 175 | |
| 176 | struct crypto_skcipher *journal_crypt; |
| 177 | struct scatterlist **journal_scatterlist; |
| 178 | struct scatterlist **journal_io_scatterlist; |
| 179 | struct skcipher_request **sk_requests; |
| 180 | |
| 181 | struct crypto_shash *journal_mac; |
| 182 | |
| 183 | struct journal_node *journal_tree; |
| 184 | struct rb_root journal_tree_root; |
| 185 | |
| 186 | sector_t provided_data_sectors; |
| 187 | |
| 188 | unsigned short journal_entry_size; |
| 189 | unsigned char journal_entries_per_sector; |
| 190 | unsigned char journal_section_entries; |
| 191 | unsigned short journal_section_sectors; |
| 192 | unsigned journal_sections; |
| 193 | unsigned journal_entries; |
| 194 | sector_t data_device_sectors; |
| 195 | sector_t meta_device_sectors; |
| 196 | unsigned initial_sectors; |
| 197 | unsigned metadata_run; |
| 198 | __s8 log2_metadata_run; |
| 199 | __u8 log2_buffer_sectors; |
| 200 | __u8 sectors_per_block; |
| 201 | __u8 log2_blocks_per_bitmap_bit; |
| 202 | |
| 203 | unsigned char mode; |
| 204 | |
| 205 | int failed; |
| 206 | |
| 207 | struct crypto_shash *internal_hash; |
| 208 | |
| 209 | struct dm_target *ti; |
| 210 | |
| 211 | /* these variables are locked with endio_wait.lock */ |
| 212 | struct rb_root in_progress; |
| 213 | struct list_head wait_list; |
| 214 | wait_queue_head_t endio_wait; |
| 215 | struct workqueue_struct *wait_wq; |
| 216 | struct workqueue_struct *offload_wq; |
| 217 | |
| 218 | unsigned char commit_seq; |
| 219 | commit_id_t commit_ids[N_COMMIT_IDS]; |
| 220 | |
| 221 | unsigned committed_section; |
| 222 | unsigned n_committed_sections; |
| 223 | |
| 224 | unsigned uncommitted_section; |
| 225 | unsigned n_uncommitted_sections; |
| 226 | |
| 227 | unsigned free_section; |
| 228 | unsigned char free_section_entry; |
| 229 | unsigned free_sectors; |
| 230 | |
| 231 | unsigned free_sectors_threshold; |
| 232 | |
| 233 | struct workqueue_struct *commit_wq; |
| 234 | struct work_struct commit_work; |
| 235 | |
| 236 | struct workqueue_struct *writer_wq; |
| 237 | struct work_struct writer_work; |
| 238 | |
| 239 | struct workqueue_struct *recalc_wq; |
| 240 | struct work_struct recalc_work; |
| 241 | u8 *recalc_buffer; |
| 242 | u8 *recalc_tags; |
| 243 | |
| 244 | struct bio_list flush_bio_list; |
| 245 | |
| 246 | unsigned long autocommit_jiffies; |
| 247 | struct timer_list autocommit_timer; |
| 248 | unsigned autocommit_msec; |
| 249 | |
| 250 | wait_queue_head_t copy_to_journal_wait; |
| 251 | |
| 252 | struct completion crypto_backoff; |
| 253 | |
| 254 | bool journal_uptodate; |
| 255 | bool just_formatted; |
| 256 | bool recalculate_flag; |
| 257 | bool legacy_recalculate; |
| 258 | |
| 259 | struct alg_spec internal_hash_alg; |
| 260 | struct alg_spec journal_crypt_alg; |
| 261 | struct alg_spec journal_mac_alg; |
| 262 | |
| 263 | atomic64_t number_of_mismatches; |
| 264 | |
| 265 | struct notifier_block reboot_notifier; |
| 266 | }; |
| 267 | |
| 268 | struct dm_integrity_range { |
| 269 | sector_t logical_sector; |
| 270 | sector_t n_sectors; |
| 271 | bool waiting; |
| 272 | union { |
| 273 | struct rb_node node; |
| 274 | struct { |
| 275 | struct task_struct *task; |
| 276 | struct list_head wait_entry; |
| 277 | }; |
| 278 | }; |
| 279 | }; |
| 280 | |
| 281 | struct dm_integrity_io { |
| 282 | struct work_struct work; |
| 283 | |
| 284 | struct dm_integrity_c *ic; |
| 285 | bool write; |
| 286 | bool fua; |
| 287 | |
| 288 | struct dm_integrity_range range; |
| 289 | |
| 290 | sector_t metadata_block; |
| 291 | unsigned metadata_offset; |
| 292 | |
| 293 | atomic_t in_flight; |
| 294 | blk_status_t bi_status; |
| 295 | |
| 296 | struct completion *completion; |
| 297 | |
| 298 | struct dm_bio_details bio_details; |
| 299 | }; |
| 300 | |
| 301 | struct journal_completion { |
| 302 | struct dm_integrity_c *ic; |
| 303 | atomic_t in_flight; |
| 304 | struct completion comp; |
| 305 | }; |
| 306 | |
| 307 | struct journal_io { |
| 308 | struct dm_integrity_range range; |
| 309 | struct journal_completion *comp; |
| 310 | }; |
| 311 | |
| 312 | struct bitmap_block_status { |
| 313 | struct work_struct work; |
| 314 | struct dm_integrity_c *ic; |
| 315 | unsigned idx; |
| 316 | unsigned long *bitmap; |
| 317 | struct bio_list bio_queue; |
| 318 | spinlock_t bio_queue_lock; |
| 319 | |
| 320 | }; |
| 321 | |
| 322 | static struct kmem_cache *journal_io_cache; |
| 323 | |
| 324 | #define JOURNAL_IO_MEMPOOL 32 |
| 325 | |
| 326 | #ifdef DEBUG_PRINT |
| 327 | #define DEBUG_print(x, ...) printk(KERN_DEBUG x, ##__VA_ARGS__) |
| 328 | static void __DEBUG_bytes(__u8 *bytes, size_t len, const char *msg, ...) |
| 329 | { |
| 330 | va_list args; |
| 331 | va_start(args, msg); |
| 332 | vprintk(msg, args); |
| 333 | va_end(args); |
| 334 | if (len) |
| 335 | pr_cont(":"); |
| 336 | while (len) { |
| 337 | pr_cont(" %02x", *bytes); |
| 338 | bytes++; |
| 339 | len--; |
| 340 | } |
| 341 | pr_cont("\n"); |
| 342 | } |
| 343 | #define DEBUG_bytes(bytes, len, msg, ...) __DEBUG_bytes(bytes, len, KERN_DEBUG msg, ##__VA_ARGS__) |
| 344 | #else |
| 345 | #define DEBUG_print(x, ...) do { } while (0) |
| 346 | #define DEBUG_bytes(bytes, len, msg, ...) do { } while (0) |
| 347 | #endif |
| 348 | |
| 349 | static void dm_integrity_prepare(struct request *rq) |
| 350 | { |
| 351 | } |
| 352 | |
| 353 | static void dm_integrity_complete(struct request *rq, unsigned int nr_bytes) |
| 354 | { |
| 355 | } |
| 356 | |
| 357 | /* |
| 358 | * DM Integrity profile, protection is performed layer above (dm-crypt) |
| 359 | */ |
| 360 | static const struct blk_integrity_profile dm_integrity_profile = { |
| 361 | .name = "DM-DIF-EXT-TAG", |
| 362 | .generate_fn = NULL, |
| 363 | .verify_fn = NULL, |
| 364 | .prepare_fn = dm_integrity_prepare, |
| 365 | .complete_fn = dm_integrity_complete, |
| 366 | }; |
| 367 | |
| 368 | static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map); |
| 369 | static void integrity_bio_wait(struct work_struct *w); |
| 370 | static void dm_integrity_dtr(struct dm_target *ti); |
| 371 | |
| 372 | static void dm_integrity_io_error(struct dm_integrity_c *ic, const char *msg, int err) |
| 373 | { |
| 374 | if (err == -EILSEQ) |
| 375 | atomic64_inc(&ic->number_of_mismatches); |
| 376 | if (!cmpxchg(&ic->failed, 0, err)) |
| 377 | DMERR("Error on %s: %d", msg, err); |
| 378 | } |
| 379 | |
| 380 | static int dm_integrity_failed(struct dm_integrity_c *ic) |
| 381 | { |
| 382 | return READ_ONCE(ic->failed); |
| 383 | } |
| 384 | |
| 385 | static bool dm_integrity_disable_recalculate(struct dm_integrity_c *ic) |
| 386 | { |
| 387 | if ((ic->internal_hash_alg.key || ic->journal_mac_alg.key) && |
| 388 | !ic->legacy_recalculate) |
| 389 | return true; |
| 390 | return false; |
| 391 | } |
| 392 | |
| 393 | static commit_id_t dm_integrity_commit_id(struct dm_integrity_c *ic, unsigned i, |
| 394 | unsigned j, unsigned char seq) |
| 395 | { |
| 396 | /* |
| 397 | * Xor the number with section and sector, so that if a piece of |
| 398 | * journal is written at wrong place, it is detected. |
| 399 | */ |
| 400 | return ic->commit_ids[seq] ^ cpu_to_le64(((__u64)i << 32) ^ j); |
| 401 | } |
| 402 | |
| 403 | static void get_area_and_offset(struct dm_integrity_c *ic, sector_t data_sector, |
| 404 | sector_t *area, sector_t *offset) |
| 405 | { |
| 406 | if (!ic->meta_dev) { |
| 407 | __u8 log2_interleave_sectors = ic->sb->log2_interleave_sectors; |
| 408 | *area = data_sector >> log2_interleave_sectors; |
| 409 | *offset = (unsigned)data_sector & ((1U << log2_interleave_sectors) - 1); |
| 410 | } else { |
| 411 | *area = 0; |
| 412 | *offset = data_sector; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | #define sector_to_block(ic, n) \ |
| 417 | do { \ |
| 418 | BUG_ON((n) & (unsigned)((ic)->sectors_per_block - 1)); \ |
| 419 | (n) >>= (ic)->sb->log2_sectors_per_block; \ |
| 420 | } while (0) |
| 421 | |
| 422 | static __u64 get_metadata_sector_and_offset(struct dm_integrity_c *ic, sector_t area, |
| 423 | sector_t offset, unsigned *metadata_offset) |
| 424 | { |
| 425 | __u64 ms; |
| 426 | unsigned mo; |
| 427 | |
| 428 | ms = area << ic->sb->log2_interleave_sectors; |
| 429 | if (likely(ic->log2_metadata_run >= 0)) |
| 430 | ms += area << ic->log2_metadata_run; |
| 431 | else |
| 432 | ms += area * ic->metadata_run; |
| 433 | ms >>= ic->log2_buffer_sectors; |
| 434 | |
| 435 | sector_to_block(ic, offset); |
| 436 | |
| 437 | if (likely(ic->log2_tag_size >= 0)) { |
| 438 | ms += offset >> (SECTOR_SHIFT + ic->log2_buffer_sectors - ic->log2_tag_size); |
| 439 | mo = (offset << ic->log2_tag_size) & ((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - 1); |
| 440 | } else { |
| 441 | ms += (__u64)offset * ic->tag_size >> (SECTOR_SHIFT + ic->log2_buffer_sectors); |
| 442 | mo = (offset * ic->tag_size) & ((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - 1); |
| 443 | } |
| 444 | *metadata_offset = mo; |
| 445 | return ms; |
| 446 | } |
| 447 | |
| 448 | static sector_t get_data_sector(struct dm_integrity_c *ic, sector_t area, sector_t offset) |
| 449 | { |
| 450 | sector_t result; |
| 451 | |
| 452 | if (ic->meta_dev) |
| 453 | return offset; |
| 454 | |
| 455 | result = area << ic->sb->log2_interleave_sectors; |
| 456 | if (likely(ic->log2_metadata_run >= 0)) |
| 457 | result += (area + 1) << ic->log2_metadata_run; |
| 458 | else |
| 459 | result += (area + 1) * ic->metadata_run; |
| 460 | |
| 461 | result += (sector_t)ic->initial_sectors + offset; |
| 462 | result += ic->start; |
| 463 | |
| 464 | return result; |
| 465 | } |
| 466 | |
| 467 | static void wraparound_section(struct dm_integrity_c *ic, unsigned *sec_ptr) |
| 468 | { |
| 469 | if (unlikely(*sec_ptr >= ic->journal_sections)) |
| 470 | *sec_ptr -= ic->journal_sections; |
| 471 | } |
| 472 | |
| 473 | static void sb_set_version(struct dm_integrity_c *ic) |
| 474 | { |
| 475 | if (ic->mode == 'B' || ic->sb->flags & cpu_to_le32(SB_FLAG_DIRTY_BITMAP)) |
| 476 | ic->sb->version = SB_VERSION_3; |
| 477 | else if (ic->meta_dev || ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) |
| 478 | ic->sb->version = SB_VERSION_2; |
| 479 | else |
| 480 | ic->sb->version = SB_VERSION_1; |
| 481 | } |
| 482 | |
| 483 | static int sync_rw_sb(struct dm_integrity_c *ic, int op, int op_flags) |
| 484 | { |
| 485 | struct dm_io_request io_req; |
| 486 | struct dm_io_region io_loc; |
| 487 | |
| 488 | io_req.bi_op = op; |
| 489 | io_req.bi_op_flags = op_flags; |
| 490 | io_req.mem.type = DM_IO_KMEM; |
| 491 | io_req.mem.ptr.addr = ic->sb; |
| 492 | io_req.notify.fn = NULL; |
| 493 | io_req.client = ic->io; |
| 494 | io_loc.bdev = ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev; |
| 495 | io_loc.sector = ic->start; |
| 496 | io_loc.count = SB_SECTORS; |
| 497 | |
| 498 | if (op == REQ_OP_WRITE) |
| 499 | sb_set_version(ic); |
| 500 | |
| 501 | return dm_io(&io_req, 1, &io_loc, NULL); |
| 502 | } |
| 503 | |
| 504 | #define BITMAP_OP_TEST_ALL_SET 0 |
| 505 | #define BITMAP_OP_TEST_ALL_CLEAR 1 |
| 506 | #define BITMAP_OP_SET 2 |
| 507 | #define BITMAP_OP_CLEAR 3 |
| 508 | |
| 509 | static bool block_bitmap_op(struct dm_integrity_c *ic, struct page_list *bitmap, |
| 510 | sector_t sector, sector_t n_sectors, int mode) |
| 511 | { |
| 512 | unsigned long bit, end_bit, this_end_bit, page, end_page; |
| 513 | unsigned long *data; |
| 514 | |
| 515 | if (unlikely(((sector | n_sectors) & ((1 << ic->sb->log2_sectors_per_block) - 1)) != 0)) { |
| 516 | DMCRIT("invalid bitmap access (%llx,%llx,%d,%d,%d)", |
| 517 | (unsigned long long)sector, |
| 518 | (unsigned long long)n_sectors, |
| 519 | ic->sb->log2_sectors_per_block, |
| 520 | ic->log2_blocks_per_bitmap_bit, |
| 521 | mode); |
| 522 | BUG(); |
| 523 | } |
| 524 | |
| 525 | if (unlikely(!n_sectors)) |
| 526 | return true; |
| 527 | |
| 528 | bit = sector >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit); |
| 529 | end_bit = (sector + n_sectors - 1) >> |
| 530 | (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit); |
| 531 | |
| 532 | page = bit / (PAGE_SIZE * 8); |
| 533 | bit %= PAGE_SIZE * 8; |
| 534 | |
| 535 | end_page = end_bit / (PAGE_SIZE * 8); |
| 536 | end_bit %= PAGE_SIZE * 8; |
| 537 | |
| 538 | repeat: |
| 539 | if (page < end_page) { |
| 540 | this_end_bit = PAGE_SIZE * 8 - 1; |
| 541 | } else { |
| 542 | this_end_bit = end_bit; |
| 543 | } |
| 544 | |
| 545 | data = lowmem_page_address(bitmap[page].page); |
| 546 | |
| 547 | if (mode == BITMAP_OP_TEST_ALL_SET) { |
| 548 | while (bit <= this_end_bit) { |
| 549 | if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) { |
| 550 | do { |
| 551 | if (data[bit / BITS_PER_LONG] != -1) |
| 552 | return false; |
| 553 | bit += BITS_PER_LONG; |
| 554 | } while (this_end_bit >= bit + BITS_PER_LONG - 1); |
| 555 | continue; |
| 556 | } |
| 557 | if (!test_bit(bit, data)) |
| 558 | return false; |
| 559 | bit++; |
| 560 | } |
| 561 | } else if (mode == BITMAP_OP_TEST_ALL_CLEAR) { |
| 562 | while (bit <= this_end_bit) { |
| 563 | if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) { |
| 564 | do { |
| 565 | if (data[bit / BITS_PER_LONG] != 0) |
| 566 | return false; |
| 567 | bit += BITS_PER_LONG; |
| 568 | } while (this_end_bit >= bit + BITS_PER_LONG - 1); |
| 569 | continue; |
| 570 | } |
| 571 | if (test_bit(bit, data)) |
| 572 | return false; |
| 573 | bit++; |
| 574 | } |
| 575 | } else if (mode == BITMAP_OP_SET) { |
| 576 | while (bit <= this_end_bit) { |
| 577 | if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) { |
| 578 | do { |
| 579 | data[bit / BITS_PER_LONG] = -1; |
| 580 | bit += BITS_PER_LONG; |
| 581 | } while (this_end_bit >= bit + BITS_PER_LONG - 1); |
| 582 | continue; |
| 583 | } |
| 584 | __set_bit(bit, data); |
| 585 | bit++; |
| 586 | } |
| 587 | } else if (mode == BITMAP_OP_CLEAR) { |
| 588 | if (!bit && this_end_bit == PAGE_SIZE * 8 - 1) |
| 589 | clear_page(data); |
| 590 | else while (bit <= this_end_bit) { |
| 591 | if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) { |
| 592 | do { |
| 593 | data[bit / BITS_PER_LONG] = 0; |
| 594 | bit += BITS_PER_LONG; |
| 595 | } while (this_end_bit >= bit + BITS_PER_LONG - 1); |
| 596 | continue; |
| 597 | } |
| 598 | __clear_bit(bit, data); |
| 599 | bit++; |
| 600 | } |
| 601 | } else { |
| 602 | BUG(); |
| 603 | } |
| 604 | |
| 605 | if (unlikely(page < end_page)) { |
| 606 | bit = 0; |
| 607 | page++; |
| 608 | goto repeat; |
| 609 | } |
| 610 | |
| 611 | return true; |
| 612 | } |
| 613 | |
| 614 | static void block_bitmap_copy(struct dm_integrity_c *ic, struct page_list *dst, struct page_list *src) |
| 615 | { |
| 616 | unsigned n_bitmap_pages = DIV_ROUND_UP(ic->n_bitmap_blocks, PAGE_SIZE / BITMAP_BLOCK_SIZE); |
| 617 | unsigned i; |
| 618 | |
| 619 | for (i = 0; i < n_bitmap_pages; i++) { |
| 620 | unsigned long *dst_data = lowmem_page_address(dst[i].page); |
| 621 | unsigned long *src_data = lowmem_page_address(src[i].page); |
| 622 | copy_page(dst_data, src_data); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | static struct bitmap_block_status *sector_to_bitmap_block(struct dm_integrity_c *ic, sector_t sector) |
| 627 | { |
| 628 | unsigned bit = sector >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit); |
| 629 | unsigned bitmap_block = bit / (BITMAP_BLOCK_SIZE * 8); |
| 630 | |
| 631 | BUG_ON(bitmap_block >= ic->n_bitmap_blocks); |
| 632 | return &ic->bbs[bitmap_block]; |
| 633 | } |
| 634 | |
| 635 | static void access_journal_check(struct dm_integrity_c *ic, unsigned section, unsigned offset, |
| 636 | bool e, const char *function) |
| 637 | { |
| 638 | #if defined(CONFIG_DM_DEBUG) || defined(INTERNAL_VERIFY) |
| 639 | unsigned limit = e ? ic->journal_section_entries : ic->journal_section_sectors; |
| 640 | |
| 641 | if (unlikely(section >= ic->journal_sections) || |
| 642 | unlikely(offset >= limit)) { |
| 643 | DMCRIT("%s: invalid access at (%u,%u), limit (%u,%u)", |
| 644 | function, section, offset, ic->journal_sections, limit); |
| 645 | BUG(); |
| 646 | } |
| 647 | #endif |
| 648 | } |
| 649 | |
| 650 | static void page_list_location(struct dm_integrity_c *ic, unsigned section, unsigned offset, |
| 651 | unsigned *pl_index, unsigned *pl_offset) |
| 652 | { |
| 653 | unsigned sector; |
| 654 | |
| 655 | access_journal_check(ic, section, offset, false, "page_list_location"); |
| 656 | |
| 657 | sector = section * ic->journal_section_sectors + offset; |
| 658 | |
| 659 | *pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT); |
| 660 | *pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1); |
| 661 | } |
| 662 | |
| 663 | static struct journal_sector *access_page_list(struct dm_integrity_c *ic, struct page_list *pl, |
| 664 | unsigned section, unsigned offset, unsigned *n_sectors) |
| 665 | { |
| 666 | unsigned pl_index, pl_offset; |
| 667 | char *va; |
| 668 | |
| 669 | page_list_location(ic, section, offset, &pl_index, &pl_offset); |
| 670 | |
| 671 | if (n_sectors) |
| 672 | *n_sectors = (PAGE_SIZE - pl_offset) >> SECTOR_SHIFT; |
| 673 | |
| 674 | va = lowmem_page_address(pl[pl_index].page); |
| 675 | |
| 676 | return (struct journal_sector *)(va + pl_offset); |
| 677 | } |
| 678 | |
| 679 | static struct journal_sector *access_journal(struct dm_integrity_c *ic, unsigned section, unsigned offset) |
| 680 | { |
| 681 | return access_page_list(ic, ic->journal, section, offset, NULL); |
| 682 | } |
| 683 | |
| 684 | static struct journal_entry *access_journal_entry(struct dm_integrity_c *ic, unsigned section, unsigned n) |
| 685 | { |
| 686 | unsigned rel_sector, offset; |
| 687 | struct journal_sector *js; |
| 688 | |
| 689 | access_journal_check(ic, section, n, true, "access_journal_entry"); |
| 690 | |
| 691 | rel_sector = n % JOURNAL_BLOCK_SECTORS; |
| 692 | offset = n / JOURNAL_BLOCK_SECTORS; |
| 693 | |
| 694 | js = access_journal(ic, section, rel_sector); |
| 695 | return (struct journal_entry *)((char *)js + offset * ic->journal_entry_size); |
| 696 | } |
| 697 | |
| 698 | static struct journal_sector *access_journal_data(struct dm_integrity_c *ic, unsigned section, unsigned n) |
| 699 | { |
| 700 | n <<= ic->sb->log2_sectors_per_block; |
| 701 | |
| 702 | n += JOURNAL_BLOCK_SECTORS; |
| 703 | |
| 704 | access_journal_check(ic, section, n, false, "access_journal_data"); |
| 705 | |
| 706 | return access_journal(ic, section, n); |
| 707 | } |
| 708 | |
| 709 | static void section_mac(struct dm_integrity_c *ic, unsigned section, __u8 result[JOURNAL_MAC_SIZE]) |
| 710 | { |
| 711 | SHASH_DESC_ON_STACK(desc, ic->journal_mac); |
| 712 | int r; |
| 713 | unsigned j, size; |
| 714 | |
| 715 | desc->tfm = ic->journal_mac; |
| 716 | |
| 717 | r = crypto_shash_init(desc); |
| 718 | if (unlikely(r)) { |
| 719 | dm_integrity_io_error(ic, "crypto_shash_init", r); |
| 720 | goto err; |
| 721 | } |
| 722 | |
| 723 | for (j = 0; j < ic->journal_section_entries; j++) { |
| 724 | struct journal_entry *je = access_journal_entry(ic, section, j); |
| 725 | r = crypto_shash_update(desc, (__u8 *)&je->u.sector, sizeof je->u.sector); |
| 726 | if (unlikely(r)) { |
| 727 | dm_integrity_io_error(ic, "crypto_shash_update", r); |
| 728 | goto err; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | size = crypto_shash_digestsize(ic->journal_mac); |
| 733 | |
| 734 | if (likely(size <= JOURNAL_MAC_SIZE)) { |
| 735 | r = crypto_shash_final(desc, result); |
| 736 | if (unlikely(r)) { |
| 737 | dm_integrity_io_error(ic, "crypto_shash_final", r); |
| 738 | goto err; |
| 739 | } |
| 740 | memset(result + size, 0, JOURNAL_MAC_SIZE - size); |
| 741 | } else { |
| 742 | __u8 digest[HASH_MAX_DIGESTSIZE]; |
| 743 | |
| 744 | if (WARN_ON(size > sizeof(digest))) { |
| 745 | dm_integrity_io_error(ic, "digest_size", -EINVAL); |
| 746 | goto err; |
| 747 | } |
| 748 | r = crypto_shash_final(desc, digest); |
| 749 | if (unlikely(r)) { |
| 750 | dm_integrity_io_error(ic, "crypto_shash_final", r); |
| 751 | goto err; |
| 752 | } |
| 753 | memcpy(result, digest, JOURNAL_MAC_SIZE); |
| 754 | } |
| 755 | |
| 756 | return; |
| 757 | err: |
| 758 | memset(result, 0, JOURNAL_MAC_SIZE); |
| 759 | } |
| 760 | |
| 761 | static void rw_section_mac(struct dm_integrity_c *ic, unsigned section, bool wr) |
| 762 | { |
| 763 | __u8 result[JOURNAL_MAC_SIZE]; |
| 764 | unsigned j; |
| 765 | |
| 766 | if (!ic->journal_mac) |
| 767 | return; |
| 768 | |
| 769 | section_mac(ic, section, result); |
| 770 | |
| 771 | for (j = 0; j < JOURNAL_BLOCK_SECTORS; j++) { |
| 772 | struct journal_sector *js = access_journal(ic, section, j); |
| 773 | |
| 774 | if (likely(wr)) |
| 775 | memcpy(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR); |
| 776 | else { |
| 777 | if (memcmp(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR)) |
| 778 | dm_integrity_io_error(ic, "journal mac", -EILSEQ); |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | static void complete_journal_op(void *context) |
| 784 | { |
| 785 | struct journal_completion *comp = context; |
| 786 | BUG_ON(!atomic_read(&comp->in_flight)); |
| 787 | if (likely(atomic_dec_and_test(&comp->in_flight))) |
| 788 | complete(&comp->comp); |
| 789 | } |
| 790 | |
| 791 | static void xor_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section, |
| 792 | unsigned n_sections, struct journal_completion *comp) |
| 793 | { |
| 794 | struct async_submit_ctl submit; |
| 795 | size_t n_bytes = (size_t)(n_sections * ic->journal_section_sectors) << SECTOR_SHIFT; |
| 796 | unsigned pl_index, pl_offset, section_index; |
| 797 | struct page_list *source_pl, *target_pl; |
| 798 | |
| 799 | if (likely(encrypt)) { |
| 800 | source_pl = ic->journal; |
| 801 | target_pl = ic->journal_io; |
| 802 | } else { |
| 803 | source_pl = ic->journal_io; |
| 804 | target_pl = ic->journal; |
| 805 | } |
| 806 | |
| 807 | page_list_location(ic, section, 0, &pl_index, &pl_offset); |
| 808 | |
| 809 | atomic_add(roundup(pl_offset + n_bytes, PAGE_SIZE) >> PAGE_SHIFT, &comp->in_flight); |
| 810 | |
| 811 | init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL, complete_journal_op, comp, NULL); |
| 812 | |
| 813 | section_index = pl_index; |
| 814 | |
| 815 | do { |
| 816 | size_t this_step; |
| 817 | struct page *src_pages[2]; |
| 818 | struct page *dst_page; |
| 819 | |
| 820 | while (unlikely(pl_index == section_index)) { |
| 821 | unsigned dummy; |
| 822 | if (likely(encrypt)) |
| 823 | rw_section_mac(ic, section, true); |
| 824 | section++; |
| 825 | n_sections--; |
| 826 | if (!n_sections) |
| 827 | break; |
| 828 | page_list_location(ic, section, 0, §ion_index, &dummy); |
| 829 | } |
| 830 | |
| 831 | this_step = min(n_bytes, (size_t)PAGE_SIZE - pl_offset); |
| 832 | dst_page = target_pl[pl_index].page; |
| 833 | src_pages[0] = source_pl[pl_index].page; |
| 834 | src_pages[1] = ic->journal_xor[pl_index].page; |
| 835 | |
| 836 | async_xor(dst_page, src_pages, pl_offset, 2, this_step, &submit); |
| 837 | |
| 838 | pl_index++; |
| 839 | pl_offset = 0; |
| 840 | n_bytes -= this_step; |
| 841 | } while (n_bytes); |
| 842 | |
| 843 | BUG_ON(n_sections); |
| 844 | |
| 845 | async_tx_issue_pending_all(); |
| 846 | } |
| 847 | |
| 848 | static void complete_journal_encrypt(struct crypto_async_request *req, int err) |
| 849 | { |
| 850 | struct journal_completion *comp = req->data; |
| 851 | if (unlikely(err)) { |
| 852 | if (likely(err == -EINPROGRESS)) { |
| 853 | complete(&comp->ic->crypto_backoff); |
| 854 | return; |
| 855 | } |
| 856 | dm_integrity_io_error(comp->ic, "asynchronous encrypt", err); |
| 857 | } |
| 858 | complete_journal_op(comp); |
| 859 | } |
| 860 | |
| 861 | static bool do_crypt(bool encrypt, struct skcipher_request *req, struct journal_completion *comp) |
| 862 | { |
| 863 | int r; |
| 864 | skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 865 | complete_journal_encrypt, comp); |
| 866 | if (likely(encrypt)) |
| 867 | r = crypto_skcipher_encrypt(req); |
| 868 | else |
| 869 | r = crypto_skcipher_decrypt(req); |
| 870 | if (likely(!r)) |
| 871 | return false; |
| 872 | if (likely(r == -EINPROGRESS)) |
| 873 | return true; |
| 874 | if (likely(r == -EBUSY)) { |
| 875 | wait_for_completion(&comp->ic->crypto_backoff); |
| 876 | reinit_completion(&comp->ic->crypto_backoff); |
| 877 | return true; |
| 878 | } |
| 879 | dm_integrity_io_error(comp->ic, "encrypt", r); |
| 880 | return false; |
| 881 | } |
| 882 | |
| 883 | static void crypt_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section, |
| 884 | unsigned n_sections, struct journal_completion *comp) |
| 885 | { |
| 886 | struct scatterlist **source_sg; |
| 887 | struct scatterlist **target_sg; |
| 888 | |
| 889 | atomic_add(2, &comp->in_flight); |
| 890 | |
| 891 | if (likely(encrypt)) { |
| 892 | source_sg = ic->journal_scatterlist; |
| 893 | target_sg = ic->journal_io_scatterlist; |
| 894 | } else { |
| 895 | source_sg = ic->journal_io_scatterlist; |
| 896 | target_sg = ic->journal_scatterlist; |
| 897 | } |
| 898 | |
| 899 | do { |
| 900 | struct skcipher_request *req; |
| 901 | unsigned ivsize; |
| 902 | char *iv; |
| 903 | |
| 904 | if (likely(encrypt)) |
| 905 | rw_section_mac(ic, section, true); |
| 906 | |
| 907 | req = ic->sk_requests[section]; |
| 908 | ivsize = crypto_skcipher_ivsize(ic->journal_crypt); |
| 909 | iv = req->iv; |
| 910 | |
| 911 | memcpy(iv, iv + ivsize, ivsize); |
| 912 | |
| 913 | req->src = source_sg[section]; |
| 914 | req->dst = target_sg[section]; |
| 915 | |
| 916 | if (unlikely(do_crypt(encrypt, req, comp))) |
| 917 | atomic_inc(&comp->in_flight); |
| 918 | |
| 919 | section++; |
| 920 | n_sections--; |
| 921 | } while (n_sections); |
| 922 | |
| 923 | atomic_dec(&comp->in_flight); |
| 924 | complete_journal_op(comp); |
| 925 | } |
| 926 | |
| 927 | static void encrypt_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section, |
| 928 | unsigned n_sections, struct journal_completion *comp) |
| 929 | { |
| 930 | if (ic->journal_xor) |
| 931 | return xor_journal(ic, encrypt, section, n_sections, comp); |
| 932 | else |
| 933 | return crypt_journal(ic, encrypt, section, n_sections, comp); |
| 934 | } |
| 935 | |
| 936 | static void complete_journal_io(unsigned long error, void *context) |
| 937 | { |
| 938 | struct journal_completion *comp = context; |
| 939 | if (unlikely(error != 0)) |
| 940 | dm_integrity_io_error(comp->ic, "writing journal", -EIO); |
| 941 | complete_journal_op(comp); |
| 942 | } |
| 943 | |
| 944 | static void rw_journal_sectors(struct dm_integrity_c *ic, int op, int op_flags, |
| 945 | unsigned sector, unsigned n_sectors, struct journal_completion *comp) |
| 946 | { |
| 947 | struct dm_io_request io_req; |
| 948 | struct dm_io_region io_loc; |
| 949 | unsigned pl_index, pl_offset; |
| 950 | int r; |
| 951 | |
| 952 | if (unlikely(dm_integrity_failed(ic))) { |
| 953 | if (comp) |
| 954 | complete_journal_io(-1UL, comp); |
| 955 | return; |
| 956 | } |
| 957 | |
| 958 | pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT); |
| 959 | pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1); |
| 960 | |
| 961 | io_req.bi_op = op; |
| 962 | io_req.bi_op_flags = op_flags; |
| 963 | io_req.mem.type = DM_IO_PAGE_LIST; |
| 964 | if (ic->journal_io) |
| 965 | io_req.mem.ptr.pl = &ic->journal_io[pl_index]; |
| 966 | else |
| 967 | io_req.mem.ptr.pl = &ic->journal[pl_index]; |
| 968 | io_req.mem.offset = pl_offset; |
| 969 | if (likely(comp != NULL)) { |
| 970 | io_req.notify.fn = complete_journal_io; |
| 971 | io_req.notify.context = comp; |
| 972 | } else { |
| 973 | io_req.notify.fn = NULL; |
| 974 | } |
| 975 | io_req.client = ic->io; |
| 976 | io_loc.bdev = ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev; |
| 977 | io_loc.sector = ic->start + SB_SECTORS + sector; |
| 978 | io_loc.count = n_sectors; |
| 979 | |
| 980 | r = dm_io(&io_req, 1, &io_loc, NULL); |
| 981 | if (unlikely(r)) { |
| 982 | dm_integrity_io_error(ic, op == REQ_OP_READ ? "reading journal" : "writing journal", r); |
| 983 | if (comp) { |
| 984 | WARN_ONCE(1, "asynchronous dm_io failed: %d", r); |
| 985 | complete_journal_io(-1UL, comp); |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | static void rw_journal(struct dm_integrity_c *ic, int op, int op_flags, unsigned section, |
| 991 | unsigned n_sections, struct journal_completion *comp) |
| 992 | { |
| 993 | unsigned sector, n_sectors; |
| 994 | |
| 995 | sector = section * ic->journal_section_sectors; |
| 996 | n_sectors = n_sections * ic->journal_section_sectors; |
| 997 | |
| 998 | rw_journal_sectors(ic, op, op_flags, sector, n_sectors, comp); |
| 999 | } |
| 1000 | |
| 1001 | static void write_journal(struct dm_integrity_c *ic, unsigned commit_start, unsigned commit_sections) |
| 1002 | { |
| 1003 | struct journal_completion io_comp; |
| 1004 | struct journal_completion crypt_comp_1; |
| 1005 | struct journal_completion crypt_comp_2; |
| 1006 | unsigned i; |
| 1007 | |
| 1008 | io_comp.ic = ic; |
| 1009 | init_completion(&io_comp.comp); |
| 1010 | |
| 1011 | if (commit_start + commit_sections <= ic->journal_sections) { |
| 1012 | io_comp.in_flight = (atomic_t)ATOMIC_INIT(1); |
| 1013 | if (ic->journal_io) { |
| 1014 | crypt_comp_1.ic = ic; |
| 1015 | init_completion(&crypt_comp_1.comp); |
| 1016 | crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0); |
| 1017 | encrypt_journal(ic, true, commit_start, commit_sections, &crypt_comp_1); |
| 1018 | wait_for_completion_io(&crypt_comp_1.comp); |
| 1019 | } else { |
| 1020 | for (i = 0; i < commit_sections; i++) |
| 1021 | rw_section_mac(ic, commit_start + i, true); |
| 1022 | } |
| 1023 | rw_journal(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, commit_start, |
| 1024 | commit_sections, &io_comp); |
| 1025 | } else { |
| 1026 | unsigned to_end; |
| 1027 | io_comp.in_flight = (atomic_t)ATOMIC_INIT(2); |
| 1028 | to_end = ic->journal_sections - commit_start; |
| 1029 | if (ic->journal_io) { |
| 1030 | crypt_comp_1.ic = ic; |
| 1031 | init_completion(&crypt_comp_1.comp); |
| 1032 | crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0); |
| 1033 | encrypt_journal(ic, true, commit_start, to_end, &crypt_comp_1); |
| 1034 | if (try_wait_for_completion(&crypt_comp_1.comp)) { |
| 1035 | rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp); |
| 1036 | reinit_completion(&crypt_comp_1.comp); |
| 1037 | crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0); |
| 1038 | encrypt_journal(ic, true, 0, commit_sections - to_end, &crypt_comp_1); |
| 1039 | wait_for_completion_io(&crypt_comp_1.comp); |
| 1040 | } else { |
| 1041 | crypt_comp_2.ic = ic; |
| 1042 | init_completion(&crypt_comp_2.comp); |
| 1043 | crypt_comp_2.in_flight = (atomic_t)ATOMIC_INIT(0); |
| 1044 | encrypt_journal(ic, true, 0, commit_sections - to_end, &crypt_comp_2); |
| 1045 | wait_for_completion_io(&crypt_comp_1.comp); |
| 1046 | rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp); |
| 1047 | wait_for_completion_io(&crypt_comp_2.comp); |
| 1048 | } |
| 1049 | } else { |
| 1050 | for (i = 0; i < to_end; i++) |
| 1051 | rw_section_mac(ic, commit_start + i, true); |
| 1052 | rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp); |
| 1053 | for (i = 0; i < commit_sections - to_end; i++) |
| 1054 | rw_section_mac(ic, i, true); |
| 1055 | } |
| 1056 | rw_journal(ic, REQ_OP_WRITE, REQ_FUA, 0, commit_sections - to_end, &io_comp); |
| 1057 | } |
| 1058 | |
| 1059 | wait_for_completion_io(&io_comp.comp); |
| 1060 | } |
| 1061 | |
| 1062 | static void copy_from_journal(struct dm_integrity_c *ic, unsigned section, unsigned offset, |
| 1063 | unsigned n_sectors, sector_t target, io_notify_fn fn, void *data) |
| 1064 | { |
| 1065 | struct dm_io_request io_req; |
| 1066 | struct dm_io_region io_loc; |
| 1067 | int r; |
| 1068 | unsigned sector, pl_index, pl_offset; |
| 1069 | |
| 1070 | BUG_ON((target | n_sectors | offset) & (unsigned)(ic->sectors_per_block - 1)); |
| 1071 | |
| 1072 | if (unlikely(dm_integrity_failed(ic))) { |
| 1073 | fn(-1UL, data); |
| 1074 | return; |
| 1075 | } |
| 1076 | |
| 1077 | sector = section * ic->journal_section_sectors + JOURNAL_BLOCK_SECTORS + offset; |
| 1078 | |
| 1079 | pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT); |
| 1080 | pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1); |
| 1081 | |
| 1082 | io_req.bi_op = REQ_OP_WRITE; |
| 1083 | io_req.bi_op_flags = 0; |
| 1084 | io_req.mem.type = DM_IO_PAGE_LIST; |
| 1085 | io_req.mem.ptr.pl = &ic->journal[pl_index]; |
| 1086 | io_req.mem.offset = pl_offset; |
| 1087 | io_req.notify.fn = fn; |
| 1088 | io_req.notify.context = data; |
| 1089 | io_req.client = ic->io; |
| 1090 | io_loc.bdev = ic->dev->bdev; |
| 1091 | io_loc.sector = target; |
| 1092 | io_loc.count = n_sectors; |
| 1093 | |
| 1094 | r = dm_io(&io_req, 1, &io_loc, NULL); |
| 1095 | if (unlikely(r)) { |
| 1096 | WARN_ONCE(1, "asynchronous dm_io failed: %d", r); |
| 1097 | fn(-1UL, data); |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | static bool ranges_overlap(struct dm_integrity_range *range1, struct dm_integrity_range *range2) |
| 1102 | { |
| 1103 | return range1->logical_sector < range2->logical_sector + range2->n_sectors && |
| 1104 | range1->logical_sector + range1->n_sectors > range2->logical_sector; |
| 1105 | } |
| 1106 | |
| 1107 | static bool add_new_range(struct dm_integrity_c *ic, struct dm_integrity_range *new_range, bool check_waiting) |
| 1108 | { |
| 1109 | struct rb_node **n = &ic->in_progress.rb_node; |
| 1110 | struct rb_node *parent; |
| 1111 | |
| 1112 | BUG_ON((new_range->logical_sector | new_range->n_sectors) & (unsigned)(ic->sectors_per_block - 1)); |
| 1113 | |
| 1114 | if (likely(check_waiting)) { |
| 1115 | struct dm_integrity_range *range; |
| 1116 | list_for_each_entry(range, &ic->wait_list, wait_entry) { |
| 1117 | if (unlikely(ranges_overlap(range, new_range))) |
| 1118 | return false; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | parent = NULL; |
| 1123 | |
| 1124 | while (*n) { |
| 1125 | struct dm_integrity_range *range = container_of(*n, struct dm_integrity_range, node); |
| 1126 | |
| 1127 | parent = *n; |
| 1128 | if (new_range->logical_sector + new_range->n_sectors <= range->logical_sector) { |
| 1129 | n = &range->node.rb_left; |
| 1130 | } else if (new_range->logical_sector >= range->logical_sector + range->n_sectors) { |
| 1131 | n = &range->node.rb_right; |
| 1132 | } else { |
| 1133 | return false; |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | rb_link_node(&new_range->node, parent, n); |
| 1138 | rb_insert_color(&new_range->node, &ic->in_progress); |
| 1139 | |
| 1140 | return true; |
| 1141 | } |
| 1142 | |
| 1143 | static void remove_range_unlocked(struct dm_integrity_c *ic, struct dm_integrity_range *range) |
| 1144 | { |
| 1145 | rb_erase(&range->node, &ic->in_progress); |
| 1146 | while (unlikely(!list_empty(&ic->wait_list))) { |
| 1147 | struct dm_integrity_range *last_range = |
| 1148 | list_first_entry(&ic->wait_list, struct dm_integrity_range, wait_entry); |
| 1149 | struct task_struct *last_range_task; |
| 1150 | last_range_task = last_range->task; |
| 1151 | list_del(&last_range->wait_entry); |
| 1152 | if (!add_new_range(ic, last_range, false)) { |
| 1153 | last_range->task = last_range_task; |
| 1154 | list_add(&last_range->wait_entry, &ic->wait_list); |
| 1155 | break; |
| 1156 | } |
| 1157 | last_range->waiting = false; |
| 1158 | wake_up_process(last_range_task); |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | static void remove_range(struct dm_integrity_c *ic, struct dm_integrity_range *range) |
| 1163 | { |
| 1164 | unsigned long flags; |
| 1165 | |
| 1166 | spin_lock_irqsave(&ic->endio_wait.lock, flags); |
| 1167 | remove_range_unlocked(ic, range); |
| 1168 | spin_unlock_irqrestore(&ic->endio_wait.lock, flags); |
| 1169 | } |
| 1170 | |
| 1171 | static void wait_and_add_new_range(struct dm_integrity_c *ic, struct dm_integrity_range *new_range) |
| 1172 | { |
| 1173 | new_range->waiting = true; |
| 1174 | list_add_tail(&new_range->wait_entry, &ic->wait_list); |
| 1175 | new_range->task = current; |
| 1176 | do { |
| 1177 | __set_current_state(TASK_UNINTERRUPTIBLE); |
| 1178 | spin_unlock_irq(&ic->endio_wait.lock); |
| 1179 | io_schedule(); |
| 1180 | spin_lock_irq(&ic->endio_wait.lock); |
| 1181 | } while (unlikely(new_range->waiting)); |
| 1182 | } |
| 1183 | |
| 1184 | static void add_new_range_and_wait(struct dm_integrity_c *ic, struct dm_integrity_range *new_range) |
| 1185 | { |
| 1186 | if (unlikely(!add_new_range(ic, new_range, true))) |
| 1187 | wait_and_add_new_range(ic, new_range); |
| 1188 | } |
| 1189 | |
| 1190 | static void init_journal_node(struct journal_node *node) |
| 1191 | { |
| 1192 | RB_CLEAR_NODE(&node->node); |
| 1193 | node->sector = (sector_t)-1; |
| 1194 | } |
| 1195 | |
| 1196 | static void add_journal_node(struct dm_integrity_c *ic, struct journal_node *node, sector_t sector) |
| 1197 | { |
| 1198 | struct rb_node **link; |
| 1199 | struct rb_node *parent; |
| 1200 | |
| 1201 | node->sector = sector; |
| 1202 | BUG_ON(!RB_EMPTY_NODE(&node->node)); |
| 1203 | |
| 1204 | link = &ic->journal_tree_root.rb_node; |
| 1205 | parent = NULL; |
| 1206 | |
| 1207 | while (*link) { |
| 1208 | struct journal_node *j; |
| 1209 | parent = *link; |
| 1210 | j = container_of(parent, struct journal_node, node); |
| 1211 | if (sector < j->sector) |
| 1212 | link = &j->node.rb_left; |
| 1213 | else |
| 1214 | link = &j->node.rb_right; |
| 1215 | } |
| 1216 | |
| 1217 | rb_link_node(&node->node, parent, link); |
| 1218 | rb_insert_color(&node->node, &ic->journal_tree_root); |
| 1219 | } |
| 1220 | |
| 1221 | static void remove_journal_node(struct dm_integrity_c *ic, struct journal_node *node) |
| 1222 | { |
| 1223 | BUG_ON(RB_EMPTY_NODE(&node->node)); |
| 1224 | rb_erase(&node->node, &ic->journal_tree_root); |
| 1225 | init_journal_node(node); |
| 1226 | } |
| 1227 | |
| 1228 | #define NOT_FOUND (-1U) |
| 1229 | |
| 1230 | static unsigned find_journal_node(struct dm_integrity_c *ic, sector_t sector, sector_t *next_sector) |
| 1231 | { |
| 1232 | struct rb_node *n = ic->journal_tree_root.rb_node; |
| 1233 | unsigned found = NOT_FOUND; |
| 1234 | *next_sector = (sector_t)-1; |
| 1235 | while (n) { |
| 1236 | struct journal_node *j = container_of(n, struct journal_node, node); |
| 1237 | if (sector == j->sector) { |
| 1238 | found = j - ic->journal_tree; |
| 1239 | } |
| 1240 | if (sector < j->sector) { |
| 1241 | *next_sector = j->sector; |
| 1242 | n = j->node.rb_left; |
| 1243 | } else { |
| 1244 | n = j->node.rb_right; |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | return found; |
| 1249 | } |
| 1250 | |
| 1251 | static bool test_journal_node(struct dm_integrity_c *ic, unsigned pos, sector_t sector) |
| 1252 | { |
| 1253 | struct journal_node *node, *next_node; |
| 1254 | struct rb_node *next; |
| 1255 | |
| 1256 | if (unlikely(pos >= ic->journal_entries)) |
| 1257 | return false; |
| 1258 | node = &ic->journal_tree[pos]; |
| 1259 | if (unlikely(RB_EMPTY_NODE(&node->node))) |
| 1260 | return false; |
| 1261 | if (unlikely(node->sector != sector)) |
| 1262 | return false; |
| 1263 | |
| 1264 | next = rb_next(&node->node); |
| 1265 | if (unlikely(!next)) |
| 1266 | return true; |
| 1267 | |
| 1268 | next_node = container_of(next, struct journal_node, node); |
| 1269 | return next_node->sector != sector; |
| 1270 | } |
| 1271 | |
| 1272 | static bool find_newer_committed_node(struct dm_integrity_c *ic, struct journal_node *node) |
| 1273 | { |
| 1274 | struct rb_node *next; |
| 1275 | struct journal_node *next_node; |
| 1276 | unsigned next_section; |
| 1277 | |
| 1278 | BUG_ON(RB_EMPTY_NODE(&node->node)); |
| 1279 | |
| 1280 | next = rb_next(&node->node); |
| 1281 | if (unlikely(!next)) |
| 1282 | return false; |
| 1283 | |
| 1284 | next_node = container_of(next, struct journal_node, node); |
| 1285 | |
| 1286 | if (next_node->sector != node->sector) |
| 1287 | return false; |
| 1288 | |
| 1289 | next_section = (unsigned)(next_node - ic->journal_tree) / ic->journal_section_entries; |
| 1290 | if (next_section >= ic->committed_section && |
| 1291 | next_section < ic->committed_section + ic->n_committed_sections) |
| 1292 | return true; |
| 1293 | if (next_section + ic->journal_sections < ic->committed_section + ic->n_committed_sections) |
| 1294 | return true; |
| 1295 | |
| 1296 | return false; |
| 1297 | } |
| 1298 | |
| 1299 | #define TAG_READ 0 |
| 1300 | #define TAG_WRITE 1 |
| 1301 | #define TAG_CMP 2 |
| 1302 | |
| 1303 | static int dm_integrity_rw_tag(struct dm_integrity_c *ic, unsigned char *tag, sector_t *metadata_block, |
| 1304 | unsigned *metadata_offset, unsigned total_size, int op) |
| 1305 | { |
| 1306 | do { |
| 1307 | unsigned char *data, *dp; |
| 1308 | struct dm_buffer *b; |
| 1309 | unsigned to_copy; |
| 1310 | int r; |
| 1311 | |
| 1312 | r = dm_integrity_failed(ic); |
| 1313 | if (unlikely(r)) |
| 1314 | return r; |
| 1315 | |
| 1316 | data = dm_bufio_read(ic->bufio, *metadata_block, &b); |
| 1317 | if (IS_ERR(data)) |
| 1318 | return PTR_ERR(data); |
| 1319 | |
| 1320 | to_copy = min((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - *metadata_offset, total_size); |
| 1321 | dp = data + *metadata_offset; |
| 1322 | if (op == TAG_READ) { |
| 1323 | memcpy(tag, dp, to_copy); |
| 1324 | } else if (op == TAG_WRITE) { |
| 1325 | memcpy(dp, tag, to_copy); |
| 1326 | dm_bufio_mark_partial_buffer_dirty(b, *metadata_offset, *metadata_offset + to_copy); |
| 1327 | } else { |
| 1328 | /* e.g.: op == TAG_CMP */ |
| 1329 | if (unlikely(memcmp(dp, tag, to_copy))) { |
| 1330 | unsigned i; |
| 1331 | |
| 1332 | for (i = 0; i < to_copy; i++) { |
| 1333 | if (dp[i] != tag[i]) |
| 1334 | break; |
| 1335 | total_size--; |
| 1336 | } |
| 1337 | dm_bufio_release(b); |
| 1338 | return total_size; |
| 1339 | } |
| 1340 | } |
| 1341 | dm_bufio_release(b); |
| 1342 | |
| 1343 | tag += to_copy; |
| 1344 | *metadata_offset += to_copy; |
| 1345 | if (unlikely(*metadata_offset == 1U << SECTOR_SHIFT << ic->log2_buffer_sectors)) { |
| 1346 | (*metadata_block)++; |
| 1347 | *metadata_offset = 0; |
| 1348 | } |
| 1349 | total_size -= to_copy; |
| 1350 | } while (unlikely(total_size)); |
| 1351 | |
| 1352 | return 0; |
| 1353 | } |
| 1354 | |
| 1355 | struct flush_request { |
| 1356 | struct dm_io_request io_req; |
| 1357 | struct dm_io_region io_reg; |
| 1358 | struct dm_integrity_c *ic; |
| 1359 | struct completion comp; |
| 1360 | }; |
| 1361 | |
| 1362 | static void flush_notify(unsigned long error, void *fr_) |
| 1363 | { |
| 1364 | struct flush_request *fr = fr_; |
| 1365 | if (unlikely(error != 0)) |
| 1366 | dm_integrity_io_error(fr->ic, "flusing disk cache", -EIO); |
| 1367 | complete(&fr->comp); |
| 1368 | } |
| 1369 | |
| 1370 | static void dm_integrity_flush_buffers(struct dm_integrity_c *ic, bool flush_data) |
| 1371 | { |
| 1372 | int r; |
| 1373 | |
| 1374 | struct flush_request fr; |
| 1375 | |
| 1376 | if (!ic->meta_dev) |
| 1377 | flush_data = false; |
| 1378 | if (flush_data) { |
| 1379 | fr.io_req.bi_op = REQ_OP_WRITE, |
| 1380 | fr.io_req.bi_op_flags = REQ_PREFLUSH | REQ_SYNC, |
| 1381 | fr.io_req.mem.type = DM_IO_KMEM, |
| 1382 | fr.io_req.mem.ptr.addr = NULL, |
| 1383 | fr.io_req.notify.fn = flush_notify, |
| 1384 | fr.io_req.notify.context = &fr; |
| 1385 | fr.io_req.client = dm_bufio_get_dm_io_client(ic->bufio), |
| 1386 | fr.io_reg.bdev = ic->dev->bdev, |
| 1387 | fr.io_reg.sector = 0, |
| 1388 | fr.io_reg.count = 0, |
| 1389 | fr.ic = ic; |
| 1390 | init_completion(&fr.comp); |
| 1391 | r = dm_io(&fr.io_req, 1, &fr.io_reg, NULL); |
| 1392 | BUG_ON(r); |
| 1393 | } |
| 1394 | |
| 1395 | r = dm_bufio_write_dirty_buffers(ic->bufio); |
| 1396 | if (unlikely(r)) |
| 1397 | dm_integrity_io_error(ic, "writing tags", r); |
| 1398 | |
| 1399 | if (flush_data) |
| 1400 | wait_for_completion(&fr.comp); |
| 1401 | } |
| 1402 | |
| 1403 | static void sleep_on_endio_wait(struct dm_integrity_c *ic) |
| 1404 | { |
| 1405 | DECLARE_WAITQUEUE(wait, current); |
| 1406 | __add_wait_queue(&ic->endio_wait, &wait); |
| 1407 | __set_current_state(TASK_UNINTERRUPTIBLE); |
| 1408 | spin_unlock_irq(&ic->endio_wait.lock); |
| 1409 | io_schedule(); |
| 1410 | spin_lock_irq(&ic->endio_wait.lock); |
| 1411 | __remove_wait_queue(&ic->endio_wait, &wait); |
| 1412 | } |
| 1413 | |
| 1414 | static void autocommit_fn(struct timer_list *t) |
| 1415 | { |
| 1416 | struct dm_integrity_c *ic = from_timer(ic, t, autocommit_timer); |
| 1417 | |
| 1418 | if (likely(!dm_integrity_failed(ic))) |
| 1419 | queue_work(ic->commit_wq, &ic->commit_work); |
| 1420 | } |
| 1421 | |
| 1422 | static void schedule_autocommit(struct dm_integrity_c *ic) |
| 1423 | { |
| 1424 | if (!timer_pending(&ic->autocommit_timer)) |
| 1425 | mod_timer(&ic->autocommit_timer, jiffies + ic->autocommit_jiffies); |
| 1426 | } |
| 1427 | |
| 1428 | static void submit_flush_bio(struct dm_integrity_c *ic, struct dm_integrity_io *dio) |
| 1429 | { |
| 1430 | struct bio *bio; |
| 1431 | unsigned long flags; |
| 1432 | |
| 1433 | spin_lock_irqsave(&ic->endio_wait.lock, flags); |
| 1434 | bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io)); |
| 1435 | bio_list_add(&ic->flush_bio_list, bio); |
| 1436 | spin_unlock_irqrestore(&ic->endio_wait.lock, flags); |
| 1437 | |
| 1438 | queue_work(ic->commit_wq, &ic->commit_work); |
| 1439 | } |
| 1440 | |
| 1441 | static void do_endio(struct dm_integrity_c *ic, struct bio *bio) |
| 1442 | { |
| 1443 | int r = dm_integrity_failed(ic); |
| 1444 | if (unlikely(r) && !bio->bi_status) |
| 1445 | bio->bi_status = errno_to_blk_status(r); |
| 1446 | if (unlikely(ic->synchronous_mode) && bio_op(bio) == REQ_OP_WRITE) { |
| 1447 | unsigned long flags; |
| 1448 | spin_lock_irqsave(&ic->endio_wait.lock, flags); |
| 1449 | bio_list_add(&ic->synchronous_bios, bio); |
| 1450 | queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0); |
| 1451 | spin_unlock_irqrestore(&ic->endio_wait.lock, flags); |
| 1452 | return; |
| 1453 | } |
| 1454 | bio_endio(bio); |
| 1455 | } |
| 1456 | |
| 1457 | static void do_endio_flush(struct dm_integrity_c *ic, struct dm_integrity_io *dio) |
| 1458 | { |
| 1459 | struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io)); |
| 1460 | |
| 1461 | if (unlikely(dio->fua) && likely(!bio->bi_status) && likely(!dm_integrity_failed(ic))) |
| 1462 | submit_flush_bio(ic, dio); |
| 1463 | else |
| 1464 | do_endio(ic, bio); |
| 1465 | } |
| 1466 | |
| 1467 | static void dec_in_flight(struct dm_integrity_io *dio) |
| 1468 | { |
| 1469 | if (atomic_dec_and_test(&dio->in_flight)) { |
| 1470 | struct dm_integrity_c *ic = dio->ic; |
| 1471 | struct bio *bio; |
| 1472 | |
| 1473 | remove_range(ic, &dio->range); |
| 1474 | |
| 1475 | if (unlikely(dio->write)) |
| 1476 | schedule_autocommit(ic); |
| 1477 | |
| 1478 | bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io)); |
| 1479 | |
| 1480 | if (unlikely(dio->bi_status) && !bio->bi_status) |
| 1481 | bio->bi_status = dio->bi_status; |
| 1482 | if (likely(!bio->bi_status) && unlikely(bio_sectors(bio) != dio->range.n_sectors)) { |
| 1483 | dio->range.logical_sector += dio->range.n_sectors; |
| 1484 | bio_advance(bio, dio->range.n_sectors << SECTOR_SHIFT); |
| 1485 | INIT_WORK(&dio->work, integrity_bio_wait); |
| 1486 | queue_work(ic->offload_wq, &dio->work); |
| 1487 | return; |
| 1488 | } |
| 1489 | do_endio_flush(ic, dio); |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | static void integrity_end_io(struct bio *bio) |
| 1494 | { |
| 1495 | struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io)); |
| 1496 | |
| 1497 | dm_bio_restore(&dio->bio_details, bio); |
| 1498 | if (bio->bi_integrity) |
| 1499 | bio->bi_opf |= REQ_INTEGRITY; |
| 1500 | |
| 1501 | if (dio->completion) |
| 1502 | complete(dio->completion); |
| 1503 | |
| 1504 | dec_in_flight(dio); |
| 1505 | } |
| 1506 | |
| 1507 | static void integrity_sector_checksum(struct dm_integrity_c *ic, sector_t sector, |
| 1508 | const char *data, char *result) |
| 1509 | { |
| 1510 | __u64 sector_le = cpu_to_le64(sector); |
| 1511 | SHASH_DESC_ON_STACK(req, ic->internal_hash); |
| 1512 | int r; |
| 1513 | unsigned digest_size; |
| 1514 | |
| 1515 | req->tfm = ic->internal_hash; |
| 1516 | |
| 1517 | r = crypto_shash_init(req); |
| 1518 | if (unlikely(r < 0)) { |
| 1519 | dm_integrity_io_error(ic, "crypto_shash_init", r); |
| 1520 | goto failed; |
| 1521 | } |
| 1522 | |
| 1523 | r = crypto_shash_update(req, (const __u8 *)§or_le, sizeof sector_le); |
| 1524 | if (unlikely(r < 0)) { |
| 1525 | dm_integrity_io_error(ic, "crypto_shash_update", r); |
| 1526 | goto failed; |
| 1527 | } |
| 1528 | |
| 1529 | r = crypto_shash_update(req, data, ic->sectors_per_block << SECTOR_SHIFT); |
| 1530 | if (unlikely(r < 0)) { |
| 1531 | dm_integrity_io_error(ic, "crypto_shash_update", r); |
| 1532 | goto failed; |
| 1533 | } |
| 1534 | |
| 1535 | r = crypto_shash_final(req, result); |
| 1536 | if (unlikely(r < 0)) { |
| 1537 | dm_integrity_io_error(ic, "crypto_shash_final", r); |
| 1538 | goto failed; |
| 1539 | } |
| 1540 | |
| 1541 | digest_size = crypto_shash_digestsize(ic->internal_hash); |
| 1542 | if (unlikely(digest_size < ic->tag_size)) |
| 1543 | memset(result + digest_size, 0, ic->tag_size - digest_size); |
| 1544 | |
| 1545 | return; |
| 1546 | |
| 1547 | failed: |
| 1548 | /* this shouldn't happen anyway, the hash functions have no reason to fail */ |
| 1549 | get_random_bytes(result, ic->tag_size); |
| 1550 | } |
| 1551 | |
| 1552 | static void integrity_metadata(struct work_struct *w) |
| 1553 | { |
| 1554 | struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work); |
| 1555 | struct dm_integrity_c *ic = dio->ic; |
| 1556 | |
| 1557 | int r; |
| 1558 | |
| 1559 | if (ic->internal_hash) { |
| 1560 | struct bvec_iter iter; |
| 1561 | struct bio_vec bv; |
| 1562 | unsigned digest_size = crypto_shash_digestsize(ic->internal_hash); |
| 1563 | struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io)); |
| 1564 | char *checksums; |
| 1565 | unsigned extra_space = unlikely(digest_size > ic->tag_size) ? digest_size - ic->tag_size : 0; |
| 1566 | char checksums_onstack[max((size_t)HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)]; |
| 1567 | unsigned sectors_to_process = dio->range.n_sectors; |
| 1568 | sector_t sector = dio->range.logical_sector; |
| 1569 | |
| 1570 | if (unlikely(ic->mode == 'R')) |
| 1571 | goto skip_io; |
| 1572 | |
| 1573 | checksums = kmalloc((PAGE_SIZE >> SECTOR_SHIFT >> ic->sb->log2_sectors_per_block) * ic->tag_size + extra_space, |
| 1574 | GFP_NOIO | __GFP_NORETRY | __GFP_NOWARN); |
| 1575 | if (!checksums) { |
| 1576 | checksums = checksums_onstack; |
| 1577 | if (WARN_ON(extra_space && |
| 1578 | digest_size > sizeof(checksums_onstack))) { |
| 1579 | r = -EINVAL; |
| 1580 | goto error; |
| 1581 | } |
| 1582 | } |
| 1583 | |
| 1584 | __bio_for_each_segment(bv, bio, iter, dio->bio_details.bi_iter) { |
| 1585 | struct bio_vec bv_copy = bv; |
| 1586 | unsigned pos; |
| 1587 | char *mem, *checksums_ptr; |
| 1588 | |
| 1589 | again: |
| 1590 | mem = (char *)kmap_atomic(bv_copy.bv_page) + bv_copy.bv_offset; |
| 1591 | pos = 0; |
| 1592 | checksums_ptr = checksums; |
| 1593 | do { |
| 1594 | integrity_sector_checksum(ic, sector, mem + pos, checksums_ptr); |
| 1595 | checksums_ptr += ic->tag_size; |
| 1596 | sectors_to_process -= ic->sectors_per_block; |
| 1597 | pos += ic->sectors_per_block << SECTOR_SHIFT; |
| 1598 | sector += ic->sectors_per_block; |
| 1599 | } while (pos < bv_copy.bv_len && sectors_to_process && checksums != checksums_onstack); |
| 1600 | kunmap_atomic(mem); |
| 1601 | |
| 1602 | r = dm_integrity_rw_tag(ic, checksums, &dio->metadata_block, &dio->metadata_offset, |
| 1603 | checksums_ptr - checksums, !dio->write ? TAG_CMP : TAG_WRITE); |
| 1604 | if (unlikely(r)) { |
| 1605 | if (r > 0) { |
| 1606 | DMERR_LIMIT("Checksum failed at sector 0x%llx", |
| 1607 | (unsigned long long)(sector - ((r + ic->tag_size - 1) / ic->tag_size))); |
| 1608 | r = -EILSEQ; |
| 1609 | atomic64_inc(&ic->number_of_mismatches); |
| 1610 | } |
| 1611 | if (likely(checksums != checksums_onstack)) |
| 1612 | kfree(checksums); |
| 1613 | goto error; |
| 1614 | } |
| 1615 | |
| 1616 | if (!sectors_to_process) |
| 1617 | break; |
| 1618 | |
| 1619 | if (unlikely(pos < bv_copy.bv_len)) { |
| 1620 | bv_copy.bv_offset += pos; |
| 1621 | bv_copy.bv_len -= pos; |
| 1622 | goto again; |
| 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | if (likely(checksums != checksums_onstack)) |
| 1627 | kfree(checksums); |
| 1628 | } else { |
| 1629 | struct bio_integrity_payload *bip = dio->bio_details.bi_integrity; |
| 1630 | |
| 1631 | if (bip) { |
| 1632 | struct bio_vec biv; |
| 1633 | struct bvec_iter iter; |
| 1634 | unsigned data_to_process = dio->range.n_sectors; |
| 1635 | sector_to_block(ic, data_to_process); |
| 1636 | data_to_process *= ic->tag_size; |
| 1637 | |
| 1638 | bip_for_each_vec(biv, bip, iter) { |
| 1639 | unsigned char *tag; |
| 1640 | unsigned this_len; |
| 1641 | |
| 1642 | BUG_ON(PageHighMem(biv.bv_page)); |
| 1643 | tag = lowmem_page_address(biv.bv_page) + biv.bv_offset; |
| 1644 | this_len = min(biv.bv_len, data_to_process); |
| 1645 | r = dm_integrity_rw_tag(ic, tag, &dio->metadata_block, &dio->metadata_offset, |
| 1646 | this_len, !dio->write ? TAG_READ : TAG_WRITE); |
| 1647 | if (unlikely(r)) |
| 1648 | goto error; |
| 1649 | data_to_process -= this_len; |
| 1650 | if (!data_to_process) |
| 1651 | break; |
| 1652 | } |
| 1653 | } |
| 1654 | } |
| 1655 | skip_io: |
| 1656 | dec_in_flight(dio); |
| 1657 | return; |
| 1658 | error: |
| 1659 | dio->bi_status = errno_to_blk_status(r); |
| 1660 | dec_in_flight(dio); |
| 1661 | } |
| 1662 | |
| 1663 | static int dm_integrity_map(struct dm_target *ti, struct bio *bio) |
| 1664 | { |
| 1665 | struct dm_integrity_c *ic = ti->private; |
| 1666 | struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io)); |
| 1667 | struct bio_integrity_payload *bip; |
| 1668 | |
| 1669 | sector_t area, offset; |
| 1670 | |
| 1671 | dio->ic = ic; |
| 1672 | dio->bi_status = 0; |
| 1673 | |
| 1674 | if (unlikely(bio->bi_opf & REQ_PREFLUSH)) { |
| 1675 | submit_flush_bio(ic, dio); |
| 1676 | return DM_MAPIO_SUBMITTED; |
| 1677 | } |
| 1678 | |
| 1679 | dio->range.logical_sector = dm_target_offset(ti, bio->bi_iter.bi_sector); |
| 1680 | dio->write = bio_op(bio) == REQ_OP_WRITE; |
| 1681 | dio->fua = dio->write && bio->bi_opf & REQ_FUA; |
| 1682 | if (unlikely(dio->fua)) { |
| 1683 | /* |
| 1684 | * Don't pass down the FUA flag because we have to flush |
| 1685 | * disk cache anyway. |
| 1686 | */ |
| 1687 | bio->bi_opf &= ~REQ_FUA; |
| 1688 | } |
| 1689 | if (unlikely(dio->range.logical_sector + bio_sectors(bio) > ic->provided_data_sectors)) { |
| 1690 | DMERR("Too big sector number: 0x%llx + 0x%x > 0x%llx", |
| 1691 | (unsigned long long)dio->range.logical_sector, bio_sectors(bio), |
| 1692 | (unsigned long long)ic->provided_data_sectors); |
| 1693 | return DM_MAPIO_KILL; |
| 1694 | } |
| 1695 | if (unlikely((dio->range.logical_sector | bio_sectors(bio)) & (unsigned)(ic->sectors_per_block - 1))) { |
| 1696 | DMERR("Bio not aligned on %u sectors: 0x%llx, 0x%x", |
| 1697 | ic->sectors_per_block, |
| 1698 | (unsigned long long)dio->range.logical_sector, bio_sectors(bio)); |
| 1699 | return DM_MAPIO_KILL; |
| 1700 | } |
| 1701 | |
| 1702 | if (ic->sectors_per_block > 1) { |
| 1703 | struct bvec_iter iter; |
| 1704 | struct bio_vec bv; |
| 1705 | bio_for_each_segment(bv, bio, iter) { |
| 1706 | if (unlikely(bv.bv_len & ((ic->sectors_per_block << SECTOR_SHIFT) - 1))) { |
| 1707 | DMERR("Bio vector (%u,%u) is not aligned on %u-sector boundary", |
| 1708 | bv.bv_offset, bv.bv_len, ic->sectors_per_block); |
| 1709 | return DM_MAPIO_KILL; |
| 1710 | } |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | bip = bio_integrity(bio); |
| 1715 | if (!ic->internal_hash) { |
| 1716 | if (bip) { |
| 1717 | unsigned wanted_tag_size = bio_sectors(bio) >> ic->sb->log2_sectors_per_block; |
| 1718 | if (ic->log2_tag_size >= 0) |
| 1719 | wanted_tag_size <<= ic->log2_tag_size; |
| 1720 | else |
| 1721 | wanted_tag_size *= ic->tag_size; |
| 1722 | if (unlikely(wanted_tag_size != bip->bip_iter.bi_size)) { |
| 1723 | DMERR("Invalid integrity data size %u, expected %u", |
| 1724 | bip->bip_iter.bi_size, wanted_tag_size); |
| 1725 | return DM_MAPIO_KILL; |
| 1726 | } |
| 1727 | } |
| 1728 | } else { |
| 1729 | if (unlikely(bip != NULL)) { |
| 1730 | DMERR("Unexpected integrity data when using internal hash"); |
| 1731 | return DM_MAPIO_KILL; |
| 1732 | } |
| 1733 | } |
| 1734 | |
| 1735 | if (unlikely(ic->mode == 'R') && unlikely(dio->write)) |
| 1736 | return DM_MAPIO_KILL; |
| 1737 | |
| 1738 | get_area_and_offset(ic, dio->range.logical_sector, &area, &offset); |
| 1739 | dio->metadata_block = get_metadata_sector_and_offset(ic, area, offset, &dio->metadata_offset); |
| 1740 | bio->bi_iter.bi_sector = get_data_sector(ic, area, offset); |
| 1741 | |
| 1742 | dm_integrity_map_continue(dio, true); |
| 1743 | return DM_MAPIO_SUBMITTED; |
| 1744 | } |
| 1745 | |
| 1746 | static bool __journal_read_write(struct dm_integrity_io *dio, struct bio *bio, |
| 1747 | unsigned journal_section, unsigned journal_entry) |
| 1748 | { |
| 1749 | struct dm_integrity_c *ic = dio->ic; |
| 1750 | sector_t logical_sector; |
| 1751 | unsigned n_sectors; |
| 1752 | |
| 1753 | logical_sector = dio->range.logical_sector; |
| 1754 | n_sectors = dio->range.n_sectors; |
| 1755 | do { |
| 1756 | struct bio_vec bv = bio_iovec(bio); |
| 1757 | char *mem; |
| 1758 | |
| 1759 | if (unlikely(bv.bv_len >> SECTOR_SHIFT > n_sectors)) |
| 1760 | bv.bv_len = n_sectors << SECTOR_SHIFT; |
| 1761 | n_sectors -= bv.bv_len >> SECTOR_SHIFT; |
| 1762 | bio_advance_iter(bio, &bio->bi_iter, bv.bv_len); |
| 1763 | retry_kmap: |
| 1764 | mem = kmap_atomic(bv.bv_page); |
| 1765 | if (likely(dio->write)) |
| 1766 | flush_dcache_page(bv.bv_page); |
| 1767 | |
| 1768 | do { |
| 1769 | struct journal_entry *je = access_journal_entry(ic, journal_section, journal_entry); |
| 1770 | |
| 1771 | if (unlikely(!dio->write)) { |
| 1772 | struct journal_sector *js; |
| 1773 | char *mem_ptr; |
| 1774 | unsigned s; |
| 1775 | |
| 1776 | if (unlikely(journal_entry_is_inprogress(je))) { |
| 1777 | flush_dcache_page(bv.bv_page); |
| 1778 | kunmap_atomic(mem); |
| 1779 | |
| 1780 | __io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je)); |
| 1781 | goto retry_kmap; |
| 1782 | } |
| 1783 | smp_rmb(); |
| 1784 | BUG_ON(journal_entry_get_sector(je) != logical_sector); |
| 1785 | js = access_journal_data(ic, journal_section, journal_entry); |
| 1786 | mem_ptr = mem + bv.bv_offset; |
| 1787 | s = 0; |
| 1788 | do { |
| 1789 | memcpy(mem_ptr, js, JOURNAL_SECTOR_DATA); |
| 1790 | *(commit_id_t *)(mem_ptr + JOURNAL_SECTOR_DATA) = je->last_bytes[s]; |
| 1791 | js++; |
| 1792 | mem_ptr += 1 << SECTOR_SHIFT; |
| 1793 | } while (++s < ic->sectors_per_block); |
| 1794 | #ifdef INTERNAL_VERIFY |
| 1795 | if (ic->internal_hash) { |
| 1796 | char checksums_onstack[max((size_t)HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)]; |
| 1797 | |
| 1798 | integrity_sector_checksum(ic, logical_sector, mem + bv.bv_offset, checksums_onstack); |
| 1799 | if (unlikely(memcmp(checksums_onstack, journal_entry_tag(ic, je), ic->tag_size))) { |
| 1800 | DMERR_LIMIT("Checksum failed when reading from journal, at sector 0x%llx", |
| 1801 | (unsigned long long)logical_sector); |
| 1802 | } |
| 1803 | } |
| 1804 | #endif |
| 1805 | } |
| 1806 | |
| 1807 | if (!ic->internal_hash) { |
| 1808 | struct bio_integrity_payload *bip = bio_integrity(bio); |
| 1809 | unsigned tag_todo = ic->tag_size; |
| 1810 | char *tag_ptr = journal_entry_tag(ic, je); |
| 1811 | |
| 1812 | if (bip) do { |
| 1813 | struct bio_vec biv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter); |
| 1814 | unsigned tag_now = min(biv.bv_len, tag_todo); |
| 1815 | char *tag_addr; |
| 1816 | BUG_ON(PageHighMem(biv.bv_page)); |
| 1817 | tag_addr = lowmem_page_address(biv.bv_page) + biv.bv_offset; |
| 1818 | if (likely(dio->write)) |
| 1819 | memcpy(tag_ptr, tag_addr, tag_now); |
| 1820 | else |
| 1821 | memcpy(tag_addr, tag_ptr, tag_now); |
| 1822 | bvec_iter_advance(bip->bip_vec, &bip->bip_iter, tag_now); |
| 1823 | tag_ptr += tag_now; |
| 1824 | tag_todo -= tag_now; |
| 1825 | } while (unlikely(tag_todo)); else { |
| 1826 | if (likely(dio->write)) |
| 1827 | memset(tag_ptr, 0, tag_todo); |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | if (likely(dio->write)) { |
| 1832 | struct journal_sector *js; |
| 1833 | unsigned s; |
| 1834 | |
| 1835 | js = access_journal_data(ic, journal_section, journal_entry); |
| 1836 | memcpy(js, mem + bv.bv_offset, ic->sectors_per_block << SECTOR_SHIFT); |
| 1837 | |
| 1838 | s = 0; |
| 1839 | do { |
| 1840 | je->last_bytes[s] = js[s].commit_id; |
| 1841 | } while (++s < ic->sectors_per_block); |
| 1842 | |
| 1843 | if (ic->internal_hash) { |
| 1844 | unsigned digest_size = crypto_shash_digestsize(ic->internal_hash); |
| 1845 | if (unlikely(digest_size > ic->tag_size)) { |
| 1846 | char checksums_onstack[HASH_MAX_DIGESTSIZE]; |
| 1847 | integrity_sector_checksum(ic, logical_sector, (char *)js, checksums_onstack); |
| 1848 | memcpy(journal_entry_tag(ic, je), checksums_onstack, ic->tag_size); |
| 1849 | } else |
| 1850 | integrity_sector_checksum(ic, logical_sector, (char *)js, journal_entry_tag(ic, je)); |
| 1851 | } |
| 1852 | |
| 1853 | journal_entry_set_sector(je, logical_sector); |
| 1854 | } |
| 1855 | logical_sector += ic->sectors_per_block; |
| 1856 | |
| 1857 | journal_entry++; |
| 1858 | if (unlikely(journal_entry == ic->journal_section_entries)) { |
| 1859 | journal_entry = 0; |
| 1860 | journal_section++; |
| 1861 | wraparound_section(ic, &journal_section); |
| 1862 | } |
| 1863 | |
| 1864 | bv.bv_offset += ic->sectors_per_block << SECTOR_SHIFT; |
| 1865 | } while (bv.bv_len -= ic->sectors_per_block << SECTOR_SHIFT); |
| 1866 | |
| 1867 | if (unlikely(!dio->write)) |
| 1868 | flush_dcache_page(bv.bv_page); |
| 1869 | kunmap_atomic(mem); |
| 1870 | } while (n_sectors); |
| 1871 | |
| 1872 | if (likely(dio->write)) { |
| 1873 | smp_mb(); |
| 1874 | if (unlikely(waitqueue_active(&ic->copy_to_journal_wait))) |
| 1875 | wake_up(&ic->copy_to_journal_wait); |
| 1876 | if (READ_ONCE(ic->free_sectors) <= ic->free_sectors_threshold) { |
| 1877 | queue_work(ic->commit_wq, &ic->commit_work); |
| 1878 | } else { |
| 1879 | schedule_autocommit(ic); |
| 1880 | } |
| 1881 | } else { |
| 1882 | remove_range(ic, &dio->range); |
| 1883 | } |
| 1884 | |
| 1885 | if (unlikely(bio->bi_iter.bi_size)) { |
| 1886 | sector_t area, offset; |
| 1887 | |
| 1888 | dio->range.logical_sector = logical_sector; |
| 1889 | get_area_and_offset(ic, dio->range.logical_sector, &area, &offset); |
| 1890 | dio->metadata_block = get_metadata_sector_and_offset(ic, area, offset, &dio->metadata_offset); |
| 1891 | return true; |
| 1892 | } |
| 1893 | |
| 1894 | return false; |
| 1895 | } |
| 1896 | |
| 1897 | static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map) |
| 1898 | { |
| 1899 | struct dm_integrity_c *ic = dio->ic; |
| 1900 | struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io)); |
| 1901 | unsigned journal_section, journal_entry; |
| 1902 | unsigned journal_read_pos; |
| 1903 | struct completion read_comp; |
| 1904 | bool need_sync_io = ic->internal_hash && !dio->write; |
| 1905 | |
| 1906 | if (need_sync_io && from_map) { |
| 1907 | INIT_WORK(&dio->work, integrity_bio_wait); |
| 1908 | queue_work(ic->offload_wq, &dio->work); |
| 1909 | return; |
| 1910 | } |
| 1911 | |
| 1912 | lock_retry: |
| 1913 | spin_lock_irq(&ic->endio_wait.lock); |
| 1914 | retry: |
| 1915 | if (unlikely(dm_integrity_failed(ic))) { |
| 1916 | spin_unlock_irq(&ic->endio_wait.lock); |
| 1917 | do_endio(ic, bio); |
| 1918 | return; |
| 1919 | } |
| 1920 | dio->range.n_sectors = bio_sectors(bio); |
| 1921 | journal_read_pos = NOT_FOUND; |
| 1922 | if (likely(ic->mode == 'J')) { |
| 1923 | if (dio->write) { |
| 1924 | unsigned next_entry, i, pos; |
| 1925 | unsigned ws, we, range_sectors; |
| 1926 | |
| 1927 | dio->range.n_sectors = min(dio->range.n_sectors, |
| 1928 | (sector_t)ic->free_sectors << ic->sb->log2_sectors_per_block); |
| 1929 | if (unlikely(!dio->range.n_sectors)) { |
| 1930 | if (from_map) |
| 1931 | goto offload_to_thread; |
| 1932 | sleep_on_endio_wait(ic); |
| 1933 | goto retry; |
| 1934 | } |
| 1935 | range_sectors = dio->range.n_sectors >> ic->sb->log2_sectors_per_block; |
| 1936 | ic->free_sectors -= range_sectors; |
| 1937 | journal_section = ic->free_section; |
| 1938 | journal_entry = ic->free_section_entry; |
| 1939 | |
| 1940 | next_entry = ic->free_section_entry + range_sectors; |
| 1941 | ic->free_section_entry = next_entry % ic->journal_section_entries; |
| 1942 | ic->free_section += next_entry / ic->journal_section_entries; |
| 1943 | ic->n_uncommitted_sections += next_entry / ic->journal_section_entries; |
| 1944 | wraparound_section(ic, &ic->free_section); |
| 1945 | |
| 1946 | pos = journal_section * ic->journal_section_entries + journal_entry; |
| 1947 | ws = journal_section; |
| 1948 | we = journal_entry; |
| 1949 | i = 0; |
| 1950 | do { |
| 1951 | struct journal_entry *je; |
| 1952 | |
| 1953 | add_journal_node(ic, &ic->journal_tree[pos], dio->range.logical_sector + i); |
| 1954 | pos++; |
| 1955 | if (unlikely(pos >= ic->journal_entries)) |
| 1956 | pos = 0; |
| 1957 | |
| 1958 | je = access_journal_entry(ic, ws, we); |
| 1959 | BUG_ON(!journal_entry_is_unused(je)); |
| 1960 | journal_entry_set_inprogress(je); |
| 1961 | we++; |
| 1962 | if (unlikely(we == ic->journal_section_entries)) { |
| 1963 | we = 0; |
| 1964 | ws++; |
| 1965 | wraparound_section(ic, &ws); |
| 1966 | } |
| 1967 | } while ((i += ic->sectors_per_block) < dio->range.n_sectors); |
| 1968 | |
| 1969 | spin_unlock_irq(&ic->endio_wait.lock); |
| 1970 | goto journal_read_write; |
| 1971 | } else { |
| 1972 | sector_t next_sector; |
| 1973 | journal_read_pos = find_journal_node(ic, dio->range.logical_sector, &next_sector); |
| 1974 | if (likely(journal_read_pos == NOT_FOUND)) { |
| 1975 | if (unlikely(dio->range.n_sectors > next_sector - dio->range.logical_sector)) |
| 1976 | dio->range.n_sectors = next_sector - dio->range.logical_sector; |
| 1977 | } else { |
| 1978 | unsigned i; |
| 1979 | unsigned jp = journal_read_pos + 1; |
| 1980 | for (i = ic->sectors_per_block; i < dio->range.n_sectors; i += ic->sectors_per_block, jp++) { |
| 1981 | if (!test_journal_node(ic, jp, dio->range.logical_sector + i)) |
| 1982 | break; |
| 1983 | } |
| 1984 | dio->range.n_sectors = i; |
| 1985 | } |
| 1986 | } |
| 1987 | } |
| 1988 | if (unlikely(!add_new_range(ic, &dio->range, true))) { |
| 1989 | /* |
| 1990 | * We must not sleep in the request routine because it could |
| 1991 | * stall bios on current->bio_list. |
| 1992 | * So, we offload the bio to a workqueue if we have to sleep. |
| 1993 | */ |
| 1994 | if (from_map) { |
| 1995 | offload_to_thread: |
| 1996 | spin_unlock_irq(&ic->endio_wait.lock); |
| 1997 | INIT_WORK(&dio->work, integrity_bio_wait); |
| 1998 | queue_work(ic->wait_wq, &dio->work); |
| 1999 | return; |
| 2000 | } |
| 2001 | if (journal_read_pos != NOT_FOUND) |
| 2002 | dio->range.n_sectors = ic->sectors_per_block; |
| 2003 | wait_and_add_new_range(ic, &dio->range); |
| 2004 | /* |
| 2005 | * wait_and_add_new_range drops the spinlock, so the journal |
| 2006 | * may have been changed arbitrarily. We need to recheck. |
| 2007 | * To simplify the code, we restrict I/O size to just one block. |
| 2008 | */ |
| 2009 | if (journal_read_pos != NOT_FOUND) { |
| 2010 | sector_t next_sector; |
| 2011 | unsigned new_pos = find_journal_node(ic, dio->range.logical_sector, &next_sector); |
| 2012 | if (unlikely(new_pos != journal_read_pos)) { |
| 2013 | remove_range_unlocked(ic, &dio->range); |
| 2014 | goto retry; |
| 2015 | } |
| 2016 | } |
| 2017 | } |
| 2018 | spin_unlock_irq(&ic->endio_wait.lock); |
| 2019 | |
| 2020 | if (unlikely(journal_read_pos != NOT_FOUND)) { |
| 2021 | journal_section = journal_read_pos / ic->journal_section_entries; |
| 2022 | journal_entry = journal_read_pos % ic->journal_section_entries; |
| 2023 | goto journal_read_write; |
| 2024 | } |
| 2025 | |
| 2026 | if (ic->mode == 'B' && dio->write) { |
| 2027 | if (!block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector, |
| 2028 | dio->range.n_sectors, BITMAP_OP_TEST_ALL_SET)) { |
| 2029 | struct bitmap_block_status *bbs; |
| 2030 | |
| 2031 | bbs = sector_to_bitmap_block(ic, dio->range.logical_sector); |
| 2032 | spin_lock(&bbs->bio_queue_lock); |
| 2033 | bio_list_add(&bbs->bio_queue, bio); |
| 2034 | spin_unlock(&bbs->bio_queue_lock); |
| 2035 | queue_work(ic->writer_wq, &bbs->work); |
| 2036 | return; |
| 2037 | } |
| 2038 | } |
| 2039 | |
| 2040 | dio->in_flight = (atomic_t)ATOMIC_INIT(2); |
| 2041 | |
| 2042 | if (need_sync_io) { |
| 2043 | init_completion(&read_comp); |
| 2044 | dio->completion = &read_comp; |
| 2045 | } else |
| 2046 | dio->completion = NULL; |
| 2047 | |
| 2048 | dm_bio_record(&dio->bio_details, bio); |
| 2049 | bio_set_dev(bio, ic->dev->bdev); |
| 2050 | bio->bi_integrity = NULL; |
| 2051 | bio->bi_opf &= ~REQ_INTEGRITY; |
| 2052 | bio->bi_end_io = integrity_end_io; |
| 2053 | bio->bi_iter.bi_size = dio->range.n_sectors << SECTOR_SHIFT; |
| 2054 | |
| 2055 | generic_make_request(bio); |
| 2056 | |
| 2057 | if (need_sync_io) { |
| 2058 | wait_for_completion_io(&read_comp); |
| 2059 | if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING) && |
| 2060 | dio->range.logical_sector + dio->range.n_sectors > le64_to_cpu(ic->sb->recalc_sector)) |
| 2061 | goto skip_check; |
| 2062 | if (ic->mode == 'B') { |
| 2063 | if (!block_bitmap_op(ic, ic->recalc_bitmap, dio->range.logical_sector, |
| 2064 | dio->range.n_sectors, BITMAP_OP_TEST_ALL_CLEAR)) |
| 2065 | goto skip_check; |
| 2066 | } |
| 2067 | |
| 2068 | if (likely(!bio->bi_status)) |
| 2069 | integrity_metadata(&dio->work); |
| 2070 | else |
| 2071 | skip_check: |
| 2072 | dec_in_flight(dio); |
| 2073 | |
| 2074 | } else { |
| 2075 | INIT_WORK(&dio->work, integrity_metadata); |
| 2076 | queue_work(ic->metadata_wq, &dio->work); |
| 2077 | } |
| 2078 | |
| 2079 | return; |
| 2080 | |
| 2081 | journal_read_write: |
| 2082 | if (unlikely(__journal_read_write(dio, bio, journal_section, journal_entry))) |
| 2083 | goto lock_retry; |
| 2084 | |
| 2085 | do_endio_flush(ic, dio); |
| 2086 | } |
| 2087 | |
| 2088 | |
| 2089 | static void integrity_bio_wait(struct work_struct *w) |
| 2090 | { |
| 2091 | struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work); |
| 2092 | |
| 2093 | dm_integrity_map_continue(dio, false); |
| 2094 | } |
| 2095 | |
| 2096 | static void pad_uncommitted(struct dm_integrity_c *ic) |
| 2097 | { |
| 2098 | if (ic->free_section_entry) { |
| 2099 | ic->free_sectors -= ic->journal_section_entries - ic->free_section_entry; |
| 2100 | ic->free_section_entry = 0; |
| 2101 | ic->free_section++; |
| 2102 | wraparound_section(ic, &ic->free_section); |
| 2103 | ic->n_uncommitted_sections++; |
| 2104 | } |
| 2105 | if (WARN_ON(ic->journal_sections * ic->journal_section_entries != |
| 2106 | (ic->n_uncommitted_sections + ic->n_committed_sections) * |
| 2107 | ic->journal_section_entries + ic->free_sectors)) { |
| 2108 | DMCRIT("journal_sections %u, journal_section_entries %u, " |
| 2109 | "n_uncommitted_sections %u, n_committed_sections %u, " |
| 2110 | "journal_section_entries %u, free_sectors %u", |
| 2111 | ic->journal_sections, ic->journal_section_entries, |
| 2112 | ic->n_uncommitted_sections, ic->n_committed_sections, |
| 2113 | ic->journal_section_entries, ic->free_sectors); |
| 2114 | } |
| 2115 | } |
| 2116 | |
| 2117 | static void integrity_commit(struct work_struct *w) |
| 2118 | { |
| 2119 | struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, commit_work); |
| 2120 | unsigned commit_start, commit_sections; |
| 2121 | unsigned i, j, n; |
| 2122 | struct bio *flushes; |
| 2123 | |
| 2124 | del_timer(&ic->autocommit_timer); |
| 2125 | |
| 2126 | spin_lock_irq(&ic->endio_wait.lock); |
| 2127 | flushes = bio_list_get(&ic->flush_bio_list); |
| 2128 | if (unlikely(ic->mode != 'J')) { |
| 2129 | spin_unlock_irq(&ic->endio_wait.lock); |
| 2130 | dm_integrity_flush_buffers(ic, true); |
| 2131 | goto release_flush_bios; |
| 2132 | } |
| 2133 | |
| 2134 | pad_uncommitted(ic); |
| 2135 | commit_start = ic->uncommitted_section; |
| 2136 | commit_sections = ic->n_uncommitted_sections; |
| 2137 | spin_unlock_irq(&ic->endio_wait.lock); |
| 2138 | |
| 2139 | if (!commit_sections) |
| 2140 | goto release_flush_bios; |
| 2141 | |
| 2142 | i = commit_start; |
| 2143 | for (n = 0; n < commit_sections; n++) { |
| 2144 | for (j = 0; j < ic->journal_section_entries; j++) { |
| 2145 | struct journal_entry *je; |
| 2146 | je = access_journal_entry(ic, i, j); |
| 2147 | io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je)); |
| 2148 | } |
| 2149 | for (j = 0; j < ic->journal_section_sectors; j++) { |
| 2150 | struct journal_sector *js; |
| 2151 | js = access_journal(ic, i, j); |
| 2152 | js->commit_id = dm_integrity_commit_id(ic, i, j, ic->commit_seq); |
| 2153 | } |
| 2154 | i++; |
| 2155 | if (unlikely(i >= ic->journal_sections)) |
| 2156 | ic->commit_seq = next_commit_seq(ic->commit_seq); |
| 2157 | wraparound_section(ic, &i); |
| 2158 | } |
| 2159 | smp_rmb(); |
| 2160 | |
| 2161 | write_journal(ic, commit_start, commit_sections); |
| 2162 | |
| 2163 | spin_lock_irq(&ic->endio_wait.lock); |
| 2164 | ic->uncommitted_section += commit_sections; |
| 2165 | wraparound_section(ic, &ic->uncommitted_section); |
| 2166 | ic->n_uncommitted_sections -= commit_sections; |
| 2167 | ic->n_committed_sections += commit_sections; |
| 2168 | spin_unlock_irq(&ic->endio_wait.lock); |
| 2169 | |
| 2170 | if (READ_ONCE(ic->free_sectors) <= ic->free_sectors_threshold) |
| 2171 | queue_work(ic->writer_wq, &ic->writer_work); |
| 2172 | |
| 2173 | release_flush_bios: |
| 2174 | while (flushes) { |
| 2175 | struct bio *next = flushes->bi_next; |
| 2176 | flushes->bi_next = NULL; |
| 2177 | do_endio(ic, flushes); |
| 2178 | flushes = next; |
| 2179 | } |
| 2180 | } |
| 2181 | |
| 2182 | static void complete_copy_from_journal(unsigned long error, void *context) |
| 2183 | { |
| 2184 | struct journal_io *io = context; |
| 2185 | struct journal_completion *comp = io->comp; |
| 2186 | struct dm_integrity_c *ic = comp->ic; |
| 2187 | remove_range(ic, &io->range); |
| 2188 | mempool_free(io, &ic->journal_io_mempool); |
| 2189 | if (unlikely(error != 0)) |
| 2190 | dm_integrity_io_error(ic, "copying from journal", -EIO); |
| 2191 | complete_journal_op(comp); |
| 2192 | } |
| 2193 | |
| 2194 | static void restore_last_bytes(struct dm_integrity_c *ic, struct journal_sector *js, |
| 2195 | struct journal_entry *je) |
| 2196 | { |
| 2197 | unsigned s = 0; |
| 2198 | do { |
| 2199 | js->commit_id = je->last_bytes[s]; |
| 2200 | js++; |
| 2201 | } while (++s < ic->sectors_per_block); |
| 2202 | } |
| 2203 | |
| 2204 | static void do_journal_write(struct dm_integrity_c *ic, unsigned write_start, |
| 2205 | unsigned write_sections, bool from_replay) |
| 2206 | { |
| 2207 | unsigned i, j, n; |
| 2208 | struct journal_completion comp; |
| 2209 | struct blk_plug plug; |
| 2210 | |
| 2211 | blk_start_plug(&plug); |
| 2212 | |
| 2213 | comp.ic = ic; |
| 2214 | comp.in_flight = (atomic_t)ATOMIC_INIT(1); |
| 2215 | init_completion(&comp.comp); |
| 2216 | |
| 2217 | i = write_start; |
| 2218 | for (n = 0; n < write_sections; n++, i++, wraparound_section(ic, &i)) { |
| 2219 | #ifndef INTERNAL_VERIFY |
| 2220 | if (unlikely(from_replay)) |
| 2221 | #endif |
| 2222 | rw_section_mac(ic, i, false); |
| 2223 | for (j = 0; j < ic->journal_section_entries; j++) { |
| 2224 | struct journal_entry *je = access_journal_entry(ic, i, j); |
| 2225 | sector_t sec, area, offset; |
| 2226 | unsigned k, l, next_loop; |
| 2227 | sector_t metadata_block; |
| 2228 | unsigned metadata_offset; |
| 2229 | struct journal_io *io; |
| 2230 | |
| 2231 | if (journal_entry_is_unused(je)) |
| 2232 | continue; |
| 2233 | BUG_ON(unlikely(journal_entry_is_inprogress(je)) && !from_replay); |
| 2234 | sec = journal_entry_get_sector(je); |
| 2235 | if (unlikely(from_replay)) { |
| 2236 | if (unlikely(sec & (unsigned)(ic->sectors_per_block - 1))) { |
| 2237 | dm_integrity_io_error(ic, "invalid sector in journal", -EIO); |
| 2238 | sec &= ~(sector_t)(ic->sectors_per_block - 1); |
| 2239 | } |
| 2240 | } |
| 2241 | get_area_and_offset(ic, sec, &area, &offset); |
| 2242 | restore_last_bytes(ic, access_journal_data(ic, i, j), je); |
| 2243 | for (k = j + 1; k < ic->journal_section_entries; k++) { |
| 2244 | struct journal_entry *je2 = access_journal_entry(ic, i, k); |
| 2245 | sector_t sec2, area2, offset2; |
| 2246 | if (journal_entry_is_unused(je2)) |
| 2247 | break; |
| 2248 | BUG_ON(unlikely(journal_entry_is_inprogress(je2)) && !from_replay); |
| 2249 | sec2 = journal_entry_get_sector(je2); |
| 2250 | get_area_and_offset(ic, sec2, &area2, &offset2); |
| 2251 | if (area2 != area || offset2 != offset + ((k - j) << ic->sb->log2_sectors_per_block)) |
| 2252 | break; |
| 2253 | restore_last_bytes(ic, access_journal_data(ic, i, k), je2); |
| 2254 | } |
| 2255 | next_loop = k - 1; |
| 2256 | |
| 2257 | io = mempool_alloc(&ic->journal_io_mempool, GFP_NOIO); |
| 2258 | io->comp = ∁ |
| 2259 | io->range.logical_sector = sec; |
| 2260 | io->range.n_sectors = (k - j) << ic->sb->log2_sectors_per_block; |
| 2261 | |
| 2262 | spin_lock_irq(&ic->endio_wait.lock); |
| 2263 | add_new_range_and_wait(ic, &io->range); |
| 2264 | |
| 2265 | if (likely(!from_replay)) { |
| 2266 | struct journal_node *section_node = &ic->journal_tree[i * ic->journal_section_entries]; |
| 2267 | |
| 2268 | /* don't write if there is newer committed sector */ |
| 2269 | while (j < k && find_newer_committed_node(ic, §ion_node[j])) { |
| 2270 | struct journal_entry *je2 = access_journal_entry(ic, i, j); |
| 2271 | |
| 2272 | journal_entry_set_unused(je2); |
| 2273 | remove_journal_node(ic, §ion_node[j]); |
| 2274 | j++; |
| 2275 | sec += ic->sectors_per_block; |
| 2276 | offset += ic->sectors_per_block; |
| 2277 | } |
| 2278 | while (j < k && find_newer_committed_node(ic, §ion_node[k - 1])) { |
| 2279 | struct journal_entry *je2 = access_journal_entry(ic, i, k - 1); |
| 2280 | |
| 2281 | journal_entry_set_unused(je2); |
| 2282 | remove_journal_node(ic, §ion_node[k - 1]); |
| 2283 | k--; |
| 2284 | } |
| 2285 | if (j == k) { |
| 2286 | remove_range_unlocked(ic, &io->range); |
| 2287 | spin_unlock_irq(&ic->endio_wait.lock); |
| 2288 | mempool_free(io, &ic->journal_io_mempool); |
| 2289 | goto skip_io; |
| 2290 | } |
| 2291 | for (l = j; l < k; l++) { |
| 2292 | remove_journal_node(ic, §ion_node[l]); |
| 2293 | } |
| 2294 | } |
| 2295 | spin_unlock_irq(&ic->endio_wait.lock); |
| 2296 | |
| 2297 | metadata_block = get_metadata_sector_and_offset(ic, area, offset, &metadata_offset); |
| 2298 | for (l = j; l < k; l++) { |
| 2299 | int r; |
| 2300 | struct journal_entry *je2 = access_journal_entry(ic, i, l); |
| 2301 | |
| 2302 | if ( |
| 2303 | #ifndef INTERNAL_VERIFY |
| 2304 | unlikely(from_replay) && |
| 2305 | #endif |
| 2306 | ic->internal_hash) { |
| 2307 | char test_tag[max_t(size_t, HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)]; |
| 2308 | |
| 2309 | integrity_sector_checksum(ic, sec + ((l - j) << ic->sb->log2_sectors_per_block), |
| 2310 | (char *)access_journal_data(ic, i, l), test_tag); |
| 2311 | if (unlikely(memcmp(test_tag, journal_entry_tag(ic, je2), ic->tag_size))) |
| 2312 | dm_integrity_io_error(ic, "tag mismatch when replaying journal", -EILSEQ); |
| 2313 | } |
| 2314 | |
| 2315 | journal_entry_set_unused(je2); |
| 2316 | r = dm_integrity_rw_tag(ic, journal_entry_tag(ic, je2), &metadata_block, &metadata_offset, |
| 2317 | ic->tag_size, TAG_WRITE); |
| 2318 | if (unlikely(r)) { |
| 2319 | dm_integrity_io_error(ic, "reading tags", r); |
| 2320 | } |
| 2321 | } |
| 2322 | |
| 2323 | atomic_inc(&comp.in_flight); |
| 2324 | copy_from_journal(ic, i, j << ic->sb->log2_sectors_per_block, |
| 2325 | (k - j) << ic->sb->log2_sectors_per_block, |
| 2326 | get_data_sector(ic, area, offset), |
| 2327 | complete_copy_from_journal, io); |
| 2328 | skip_io: |
| 2329 | j = next_loop; |
| 2330 | } |
| 2331 | } |
| 2332 | |
| 2333 | dm_bufio_write_dirty_buffers_async(ic->bufio); |
| 2334 | |
| 2335 | blk_finish_plug(&plug); |
| 2336 | |
| 2337 | complete_journal_op(&comp); |
| 2338 | wait_for_completion_io(&comp.comp); |
| 2339 | |
| 2340 | dm_integrity_flush_buffers(ic, true); |
| 2341 | } |
| 2342 | |
| 2343 | static void integrity_writer(struct work_struct *w) |
| 2344 | { |
| 2345 | struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, writer_work); |
| 2346 | unsigned write_start, write_sections; |
| 2347 | |
| 2348 | unsigned prev_free_sectors; |
| 2349 | |
| 2350 | spin_lock_irq(&ic->endio_wait.lock); |
| 2351 | write_start = ic->committed_section; |
| 2352 | write_sections = ic->n_committed_sections; |
| 2353 | spin_unlock_irq(&ic->endio_wait.lock); |
| 2354 | |
| 2355 | if (!write_sections) |
| 2356 | return; |
| 2357 | |
| 2358 | do_journal_write(ic, write_start, write_sections, false); |
| 2359 | |
| 2360 | spin_lock_irq(&ic->endio_wait.lock); |
| 2361 | |
| 2362 | ic->committed_section += write_sections; |
| 2363 | wraparound_section(ic, &ic->committed_section); |
| 2364 | ic->n_committed_sections -= write_sections; |
| 2365 | |
| 2366 | prev_free_sectors = ic->free_sectors; |
| 2367 | ic->free_sectors += write_sections * ic->journal_section_entries; |
| 2368 | if (unlikely(!prev_free_sectors)) |
| 2369 | wake_up_locked(&ic->endio_wait); |
| 2370 | |
| 2371 | spin_unlock_irq(&ic->endio_wait.lock); |
| 2372 | } |
| 2373 | |
| 2374 | static void recalc_write_super(struct dm_integrity_c *ic) |
| 2375 | { |
| 2376 | int r; |
| 2377 | |
| 2378 | dm_integrity_flush_buffers(ic, false); |
| 2379 | if (dm_integrity_failed(ic)) |
| 2380 | return; |
| 2381 | |
| 2382 | r = sync_rw_sb(ic, REQ_OP_WRITE, 0); |
| 2383 | if (unlikely(r)) |
| 2384 | dm_integrity_io_error(ic, "writing superblock", r); |
| 2385 | } |
| 2386 | |
| 2387 | static void integrity_recalc(struct work_struct *w) |
| 2388 | { |
| 2389 | struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, recalc_work); |
| 2390 | struct dm_integrity_range range; |
| 2391 | struct dm_io_request io_req; |
| 2392 | struct dm_io_region io_loc; |
| 2393 | sector_t area, offset; |
| 2394 | sector_t metadata_block; |
| 2395 | unsigned metadata_offset; |
| 2396 | sector_t logical_sector, n_sectors; |
| 2397 | __u8 *t; |
| 2398 | unsigned i; |
| 2399 | int r; |
| 2400 | unsigned super_counter = 0; |
| 2401 | |
| 2402 | DEBUG_print("start recalculation... (position %llx)\n", le64_to_cpu(ic->sb->recalc_sector)); |
| 2403 | |
| 2404 | spin_lock_irq(&ic->endio_wait.lock); |
| 2405 | |
| 2406 | next_chunk: |
| 2407 | |
| 2408 | if (unlikely(dm_post_suspending(ic->ti))) |
| 2409 | goto unlock_ret; |
| 2410 | |
| 2411 | range.logical_sector = le64_to_cpu(ic->sb->recalc_sector); |
| 2412 | if (unlikely(range.logical_sector >= ic->provided_data_sectors)) { |
| 2413 | if (ic->mode == 'B') { |
| 2414 | block_bitmap_op(ic, ic->recalc_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR); |
| 2415 | DEBUG_print("queue_delayed_work: bitmap_flush_work\n"); |
| 2416 | queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0); |
| 2417 | } |
| 2418 | goto unlock_ret; |
| 2419 | } |
| 2420 | |
| 2421 | get_area_and_offset(ic, range.logical_sector, &area, &offset); |
| 2422 | range.n_sectors = min((sector_t)RECALC_SECTORS, ic->provided_data_sectors - range.logical_sector); |
| 2423 | if (!ic->meta_dev) |
| 2424 | range.n_sectors = min(range.n_sectors, ((sector_t)1U << ic->sb->log2_interleave_sectors) - (unsigned)offset); |
| 2425 | |
| 2426 | add_new_range_and_wait(ic, &range); |
| 2427 | spin_unlock_irq(&ic->endio_wait.lock); |
| 2428 | logical_sector = range.logical_sector; |
| 2429 | n_sectors = range.n_sectors; |
| 2430 | |
| 2431 | if (ic->mode == 'B') { |
| 2432 | if (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector, n_sectors, BITMAP_OP_TEST_ALL_CLEAR)) { |
| 2433 | goto advance_and_next; |
| 2434 | } |
| 2435 | while (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector, |
| 2436 | ic->sectors_per_block, BITMAP_OP_TEST_ALL_CLEAR)) { |
| 2437 | logical_sector += ic->sectors_per_block; |
| 2438 | n_sectors -= ic->sectors_per_block; |
| 2439 | cond_resched(); |
| 2440 | } |
| 2441 | while (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector + n_sectors - ic->sectors_per_block, |
| 2442 | ic->sectors_per_block, BITMAP_OP_TEST_ALL_CLEAR)) { |
| 2443 | n_sectors -= ic->sectors_per_block; |
| 2444 | cond_resched(); |
| 2445 | } |
| 2446 | get_area_and_offset(ic, logical_sector, &area, &offset); |
| 2447 | } |
| 2448 | |
| 2449 | DEBUG_print("recalculating: %lx, %lx\n", logical_sector, n_sectors); |
| 2450 | |
| 2451 | if (unlikely(++super_counter == RECALC_WRITE_SUPER)) { |
| 2452 | recalc_write_super(ic); |
| 2453 | if (ic->mode == 'B') { |
| 2454 | queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, ic->bitmap_flush_interval); |
| 2455 | } |
| 2456 | super_counter = 0; |
| 2457 | } |
| 2458 | |
| 2459 | if (unlikely(dm_integrity_failed(ic))) |
| 2460 | goto err; |
| 2461 | |
| 2462 | io_req.bi_op = REQ_OP_READ; |
| 2463 | io_req.bi_op_flags = 0; |
| 2464 | io_req.mem.type = DM_IO_VMA; |
| 2465 | io_req.mem.ptr.addr = ic->recalc_buffer; |
| 2466 | io_req.notify.fn = NULL; |
| 2467 | io_req.client = ic->io; |
| 2468 | io_loc.bdev = ic->dev->bdev; |
| 2469 | io_loc.sector = get_data_sector(ic, area, offset); |
| 2470 | io_loc.count = n_sectors; |
| 2471 | |
| 2472 | r = dm_io(&io_req, 1, &io_loc, NULL); |
| 2473 | if (unlikely(r)) { |
| 2474 | dm_integrity_io_error(ic, "reading data", r); |
| 2475 | goto err; |
| 2476 | } |
| 2477 | |
| 2478 | t = ic->recalc_tags; |
| 2479 | for (i = 0; i < n_sectors; i += ic->sectors_per_block) { |
| 2480 | integrity_sector_checksum(ic, logical_sector + i, ic->recalc_buffer + (i << SECTOR_SHIFT), t); |
| 2481 | t += ic->tag_size; |
| 2482 | } |
| 2483 | |
| 2484 | metadata_block = get_metadata_sector_and_offset(ic, area, offset, &metadata_offset); |
| 2485 | |
| 2486 | r = dm_integrity_rw_tag(ic, ic->recalc_tags, &metadata_block, &metadata_offset, t - ic->recalc_tags, TAG_WRITE); |
| 2487 | if (unlikely(r)) { |
| 2488 | dm_integrity_io_error(ic, "writing tags", r); |
| 2489 | goto err; |
| 2490 | } |
| 2491 | |
| 2492 | if (ic->mode == 'B') { |
| 2493 | sector_t start, end; |
| 2494 | start = (range.logical_sector >> |
| 2495 | (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit)) << |
| 2496 | (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit); |
| 2497 | end = ((range.logical_sector + range.n_sectors) >> |
| 2498 | (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit)) << |
| 2499 | (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit); |
| 2500 | block_bitmap_op(ic, ic->recalc_bitmap, start, end - start, BITMAP_OP_CLEAR); |
| 2501 | } |
| 2502 | |
| 2503 | advance_and_next: |
| 2504 | cond_resched(); |
| 2505 | |
| 2506 | spin_lock_irq(&ic->endio_wait.lock); |
| 2507 | remove_range_unlocked(ic, &range); |
| 2508 | ic->sb->recalc_sector = cpu_to_le64(range.logical_sector + range.n_sectors); |
| 2509 | goto next_chunk; |
| 2510 | |
| 2511 | err: |
| 2512 | remove_range(ic, &range); |
| 2513 | return; |
| 2514 | |
| 2515 | unlock_ret: |
| 2516 | spin_unlock_irq(&ic->endio_wait.lock); |
| 2517 | |
| 2518 | recalc_write_super(ic); |
| 2519 | } |
| 2520 | |
| 2521 | static void bitmap_block_work(struct work_struct *w) |
| 2522 | { |
| 2523 | struct bitmap_block_status *bbs = container_of(w, struct bitmap_block_status, work); |
| 2524 | struct dm_integrity_c *ic = bbs->ic; |
| 2525 | struct bio *bio; |
| 2526 | struct bio_list bio_queue; |
| 2527 | struct bio_list waiting; |
| 2528 | |
| 2529 | bio_list_init(&waiting); |
| 2530 | |
| 2531 | spin_lock(&bbs->bio_queue_lock); |
| 2532 | bio_queue = bbs->bio_queue; |
| 2533 | bio_list_init(&bbs->bio_queue); |
| 2534 | spin_unlock(&bbs->bio_queue_lock); |
| 2535 | |
| 2536 | while ((bio = bio_list_pop(&bio_queue))) { |
| 2537 | struct dm_integrity_io *dio; |
| 2538 | |
| 2539 | dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io)); |
| 2540 | |
| 2541 | if (block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector, |
| 2542 | dio->range.n_sectors, BITMAP_OP_TEST_ALL_SET)) { |
| 2543 | remove_range(ic, &dio->range); |
| 2544 | INIT_WORK(&dio->work, integrity_bio_wait); |
| 2545 | queue_work(ic->offload_wq, &dio->work); |
| 2546 | } else { |
| 2547 | block_bitmap_op(ic, ic->journal, dio->range.logical_sector, |
| 2548 | dio->range.n_sectors, BITMAP_OP_SET); |
| 2549 | bio_list_add(&waiting, bio); |
| 2550 | } |
| 2551 | } |
| 2552 | |
| 2553 | if (bio_list_empty(&waiting)) |
| 2554 | return; |
| 2555 | |
| 2556 | rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, |
| 2557 | bbs->idx * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), |
| 2558 | BITMAP_BLOCK_SIZE >> SECTOR_SHIFT, NULL); |
| 2559 | |
| 2560 | while ((bio = bio_list_pop(&waiting))) { |
| 2561 | struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io)); |
| 2562 | |
| 2563 | block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector, |
| 2564 | dio->range.n_sectors, BITMAP_OP_SET); |
| 2565 | |
| 2566 | remove_range(ic, &dio->range); |
| 2567 | INIT_WORK(&dio->work, integrity_bio_wait); |
| 2568 | queue_work(ic->offload_wq, &dio->work); |
| 2569 | } |
| 2570 | |
| 2571 | queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, ic->bitmap_flush_interval); |
| 2572 | } |
| 2573 | |
| 2574 | static void bitmap_flush_work(struct work_struct *work) |
| 2575 | { |
| 2576 | struct dm_integrity_c *ic = container_of(work, struct dm_integrity_c, bitmap_flush_work.work); |
| 2577 | struct dm_integrity_range range; |
| 2578 | unsigned long limit; |
| 2579 | struct bio *bio; |
| 2580 | |
| 2581 | dm_integrity_flush_buffers(ic, false); |
| 2582 | |
| 2583 | range.logical_sector = 0; |
| 2584 | range.n_sectors = ic->provided_data_sectors; |
| 2585 | |
| 2586 | spin_lock_irq(&ic->endio_wait.lock); |
| 2587 | add_new_range_and_wait(ic, &range); |
| 2588 | spin_unlock_irq(&ic->endio_wait.lock); |
| 2589 | |
| 2590 | dm_integrity_flush_buffers(ic, true); |
| 2591 | if (ic->meta_dev) |
| 2592 | blkdev_issue_flush(ic->dev->bdev, GFP_NOIO, NULL); |
| 2593 | |
| 2594 | limit = ic->provided_data_sectors; |
| 2595 | if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) { |
| 2596 | limit = le64_to_cpu(ic->sb->recalc_sector) |
| 2597 | >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit) |
| 2598 | << (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit); |
| 2599 | } |
| 2600 | /*DEBUG_print("zeroing journal\n");*/ |
| 2601 | block_bitmap_op(ic, ic->journal, 0, limit, BITMAP_OP_CLEAR); |
| 2602 | block_bitmap_op(ic, ic->may_write_bitmap, 0, limit, BITMAP_OP_CLEAR); |
| 2603 | |
| 2604 | rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, 0, |
| 2605 | ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL); |
| 2606 | |
| 2607 | spin_lock_irq(&ic->endio_wait.lock); |
| 2608 | remove_range_unlocked(ic, &range); |
| 2609 | while (unlikely((bio = bio_list_pop(&ic->synchronous_bios)) != NULL)) { |
| 2610 | bio_endio(bio); |
| 2611 | spin_unlock_irq(&ic->endio_wait.lock); |
| 2612 | spin_lock_irq(&ic->endio_wait.lock); |
| 2613 | } |
| 2614 | spin_unlock_irq(&ic->endio_wait.lock); |
| 2615 | } |
| 2616 | |
| 2617 | |
| 2618 | static void init_journal(struct dm_integrity_c *ic, unsigned start_section, |
| 2619 | unsigned n_sections, unsigned char commit_seq) |
| 2620 | { |
| 2621 | unsigned i, j, n; |
| 2622 | |
| 2623 | if (!n_sections) |
| 2624 | return; |
| 2625 | |
| 2626 | for (n = 0; n < n_sections; n++) { |
| 2627 | i = start_section + n; |
| 2628 | wraparound_section(ic, &i); |
| 2629 | for (j = 0; j < ic->journal_section_sectors; j++) { |
| 2630 | struct journal_sector *js = access_journal(ic, i, j); |
| 2631 | memset(&js->entries, 0, JOURNAL_SECTOR_DATA); |
| 2632 | js->commit_id = dm_integrity_commit_id(ic, i, j, commit_seq); |
| 2633 | } |
| 2634 | for (j = 0; j < ic->journal_section_entries; j++) { |
| 2635 | struct journal_entry *je = access_journal_entry(ic, i, j); |
| 2636 | journal_entry_set_unused(je); |
| 2637 | } |
| 2638 | } |
| 2639 | |
| 2640 | write_journal(ic, start_section, n_sections); |
| 2641 | } |
| 2642 | |
| 2643 | static int find_commit_seq(struct dm_integrity_c *ic, unsigned i, unsigned j, commit_id_t id) |
| 2644 | { |
| 2645 | unsigned char k; |
| 2646 | for (k = 0; k < N_COMMIT_IDS; k++) { |
| 2647 | if (dm_integrity_commit_id(ic, i, j, k) == id) |
| 2648 | return k; |
| 2649 | } |
| 2650 | dm_integrity_io_error(ic, "journal commit id", -EIO); |
| 2651 | return -EIO; |
| 2652 | } |
| 2653 | |
| 2654 | static void replay_journal(struct dm_integrity_c *ic) |
| 2655 | { |
| 2656 | unsigned i, j; |
| 2657 | bool used_commit_ids[N_COMMIT_IDS]; |
| 2658 | unsigned max_commit_id_sections[N_COMMIT_IDS]; |
| 2659 | unsigned write_start, write_sections; |
| 2660 | unsigned continue_section; |
| 2661 | bool journal_empty; |
| 2662 | unsigned char unused, last_used, want_commit_seq; |
| 2663 | |
| 2664 | if (ic->mode == 'R') |
| 2665 | return; |
| 2666 | |
| 2667 | if (ic->journal_uptodate) |
| 2668 | return; |
| 2669 | |
| 2670 | last_used = 0; |
| 2671 | write_start = 0; |
| 2672 | |
| 2673 | if (!ic->just_formatted) { |
| 2674 | DEBUG_print("reading journal\n"); |
| 2675 | rw_journal(ic, REQ_OP_READ, 0, 0, ic->journal_sections, NULL); |
| 2676 | if (ic->journal_io) |
| 2677 | DEBUG_bytes(lowmem_page_address(ic->journal_io[0].page), 64, "read journal"); |
| 2678 | if (ic->journal_io) { |
| 2679 | struct journal_completion crypt_comp; |
| 2680 | crypt_comp.ic = ic; |
| 2681 | init_completion(&crypt_comp.comp); |
| 2682 | crypt_comp.in_flight = (atomic_t)ATOMIC_INIT(0); |
| 2683 | encrypt_journal(ic, false, 0, ic->journal_sections, &crypt_comp); |
| 2684 | wait_for_completion(&crypt_comp.comp); |
| 2685 | } |
| 2686 | DEBUG_bytes(lowmem_page_address(ic->journal[0].page), 64, "decrypted journal"); |
| 2687 | } |
| 2688 | |
| 2689 | if (dm_integrity_failed(ic)) |
| 2690 | goto clear_journal; |
| 2691 | |
| 2692 | journal_empty = true; |
| 2693 | memset(used_commit_ids, 0, sizeof used_commit_ids); |
| 2694 | memset(max_commit_id_sections, 0, sizeof max_commit_id_sections); |
| 2695 | for (i = 0; i < ic->journal_sections; i++) { |
| 2696 | for (j = 0; j < ic->journal_section_sectors; j++) { |
| 2697 | int k; |
| 2698 | struct journal_sector *js = access_journal(ic, i, j); |
| 2699 | k = find_commit_seq(ic, i, j, js->commit_id); |
| 2700 | if (k < 0) |
| 2701 | goto clear_journal; |
| 2702 | used_commit_ids[k] = true; |
| 2703 | max_commit_id_sections[k] = i; |
| 2704 | } |
| 2705 | if (journal_empty) { |
| 2706 | for (j = 0; j < ic->journal_section_entries; j++) { |
| 2707 | struct journal_entry *je = access_journal_entry(ic, i, j); |
| 2708 | if (!journal_entry_is_unused(je)) { |
| 2709 | journal_empty = false; |
| 2710 | break; |
| 2711 | } |
| 2712 | } |
| 2713 | } |
| 2714 | } |
| 2715 | |
| 2716 | if (!used_commit_ids[N_COMMIT_IDS - 1]) { |
| 2717 | unused = N_COMMIT_IDS - 1; |
| 2718 | while (unused && !used_commit_ids[unused - 1]) |
| 2719 | unused--; |
| 2720 | } else { |
| 2721 | for (unused = 0; unused < N_COMMIT_IDS; unused++) |
| 2722 | if (!used_commit_ids[unused]) |
| 2723 | break; |
| 2724 | if (unused == N_COMMIT_IDS) { |
| 2725 | dm_integrity_io_error(ic, "journal commit ids", -EIO); |
| 2726 | goto clear_journal; |
| 2727 | } |
| 2728 | } |
| 2729 | DEBUG_print("first unused commit seq %d [%d,%d,%d,%d]\n", |
| 2730 | unused, used_commit_ids[0], used_commit_ids[1], |
| 2731 | used_commit_ids[2], used_commit_ids[3]); |
| 2732 | |
| 2733 | last_used = prev_commit_seq(unused); |
| 2734 | want_commit_seq = prev_commit_seq(last_used); |
| 2735 | |
| 2736 | if (!used_commit_ids[want_commit_seq] && used_commit_ids[prev_commit_seq(want_commit_seq)]) |
| 2737 | journal_empty = true; |
| 2738 | |
| 2739 | write_start = max_commit_id_sections[last_used] + 1; |
| 2740 | if (unlikely(write_start >= ic->journal_sections)) |
| 2741 | want_commit_seq = next_commit_seq(want_commit_seq); |
| 2742 | wraparound_section(ic, &write_start); |
| 2743 | |
| 2744 | i = write_start; |
| 2745 | for (write_sections = 0; write_sections < ic->journal_sections; write_sections++) { |
| 2746 | for (j = 0; j < ic->journal_section_sectors; j++) { |
| 2747 | struct journal_sector *js = access_journal(ic, i, j); |
| 2748 | |
| 2749 | if (js->commit_id != dm_integrity_commit_id(ic, i, j, want_commit_seq)) { |
| 2750 | /* |
| 2751 | * This could be caused by crash during writing. |
| 2752 | * We won't replay the inconsistent part of the |
| 2753 | * journal. |
| 2754 | */ |
| 2755 | DEBUG_print("commit id mismatch at position (%u, %u): %d != %d\n", |
| 2756 | i, j, find_commit_seq(ic, i, j, js->commit_id), want_commit_seq); |
| 2757 | goto brk; |
| 2758 | } |
| 2759 | } |
| 2760 | i++; |
| 2761 | if (unlikely(i >= ic->journal_sections)) |
| 2762 | want_commit_seq = next_commit_seq(want_commit_seq); |
| 2763 | wraparound_section(ic, &i); |
| 2764 | } |
| 2765 | brk: |
| 2766 | |
| 2767 | if (!journal_empty) { |
| 2768 | DEBUG_print("replaying %u sections, starting at %u, commit seq %d\n", |
| 2769 | write_sections, write_start, want_commit_seq); |
| 2770 | do_journal_write(ic, write_start, write_sections, true); |
| 2771 | } |
| 2772 | |
| 2773 | if (write_sections == ic->journal_sections && (ic->mode == 'J' || journal_empty)) { |
| 2774 | continue_section = write_start; |
| 2775 | ic->commit_seq = want_commit_seq; |
| 2776 | DEBUG_print("continuing from section %u, commit seq %d\n", write_start, ic->commit_seq); |
| 2777 | } else { |
| 2778 | unsigned s; |
| 2779 | unsigned char erase_seq; |
| 2780 | clear_journal: |
| 2781 | DEBUG_print("clearing journal\n"); |
| 2782 | |
| 2783 | erase_seq = prev_commit_seq(prev_commit_seq(last_used)); |
| 2784 | s = write_start; |
| 2785 | init_journal(ic, s, 1, erase_seq); |
| 2786 | s++; |
| 2787 | wraparound_section(ic, &s); |
| 2788 | if (ic->journal_sections >= 2) { |
| 2789 | init_journal(ic, s, ic->journal_sections - 2, erase_seq); |
| 2790 | s += ic->journal_sections - 2; |
| 2791 | wraparound_section(ic, &s); |
| 2792 | init_journal(ic, s, 1, erase_seq); |
| 2793 | } |
| 2794 | |
| 2795 | continue_section = 0; |
| 2796 | ic->commit_seq = next_commit_seq(erase_seq); |
| 2797 | } |
| 2798 | |
| 2799 | ic->committed_section = continue_section; |
| 2800 | ic->n_committed_sections = 0; |
| 2801 | |
| 2802 | ic->uncommitted_section = continue_section; |
| 2803 | ic->n_uncommitted_sections = 0; |
| 2804 | |
| 2805 | ic->free_section = continue_section; |
| 2806 | ic->free_section_entry = 0; |
| 2807 | ic->free_sectors = ic->journal_entries; |
| 2808 | |
| 2809 | ic->journal_tree_root = RB_ROOT; |
| 2810 | for (i = 0; i < ic->journal_entries; i++) |
| 2811 | init_journal_node(&ic->journal_tree[i]); |
| 2812 | } |
| 2813 | |
| 2814 | static void dm_integrity_enter_synchronous_mode(struct dm_integrity_c *ic) |
| 2815 | { |
| 2816 | DEBUG_print("dm_integrity_enter_synchronous_mode\n"); |
| 2817 | |
| 2818 | if (ic->mode == 'B') { |
| 2819 | ic->bitmap_flush_interval = msecs_to_jiffies(10) + 1; |
| 2820 | ic->synchronous_mode = 1; |
| 2821 | |
| 2822 | cancel_delayed_work_sync(&ic->bitmap_flush_work); |
| 2823 | queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0); |
| 2824 | flush_workqueue(ic->commit_wq); |
| 2825 | } |
| 2826 | } |
| 2827 | |
| 2828 | static int dm_integrity_reboot(struct notifier_block *n, unsigned long code, void *x) |
| 2829 | { |
| 2830 | struct dm_integrity_c *ic = container_of(n, struct dm_integrity_c, reboot_notifier); |
| 2831 | |
| 2832 | DEBUG_print("dm_integrity_reboot\n"); |
| 2833 | |
| 2834 | dm_integrity_enter_synchronous_mode(ic); |
| 2835 | |
| 2836 | return NOTIFY_DONE; |
| 2837 | } |
| 2838 | |
| 2839 | static void dm_integrity_postsuspend(struct dm_target *ti) |
| 2840 | { |
| 2841 | struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private; |
| 2842 | int r; |
| 2843 | |
| 2844 | WARN_ON(unregister_reboot_notifier(&ic->reboot_notifier)); |
| 2845 | |
| 2846 | del_timer_sync(&ic->autocommit_timer); |
| 2847 | |
| 2848 | if (ic->recalc_wq) |
| 2849 | drain_workqueue(ic->recalc_wq); |
| 2850 | |
| 2851 | if (ic->mode == 'B') |
| 2852 | cancel_delayed_work_sync(&ic->bitmap_flush_work); |
| 2853 | |
| 2854 | queue_work(ic->commit_wq, &ic->commit_work); |
| 2855 | drain_workqueue(ic->commit_wq); |
| 2856 | |
| 2857 | if (ic->mode == 'J') { |
| 2858 | queue_work(ic->writer_wq, &ic->writer_work); |
| 2859 | drain_workqueue(ic->writer_wq); |
| 2860 | dm_integrity_flush_buffers(ic, true); |
| 2861 | } |
| 2862 | |
| 2863 | if (ic->mode == 'B') { |
| 2864 | dm_integrity_flush_buffers(ic, true); |
| 2865 | #if 1 |
| 2866 | /* set to 0 to test bitmap replay code */ |
| 2867 | init_journal(ic, 0, ic->journal_sections, 0); |
| 2868 | ic->sb->flags &= ~cpu_to_le32(SB_FLAG_DIRTY_BITMAP); |
| 2869 | r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA); |
| 2870 | if (unlikely(r)) |
| 2871 | dm_integrity_io_error(ic, "writing superblock", r); |
| 2872 | #endif |
| 2873 | } |
| 2874 | |
| 2875 | BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress)); |
| 2876 | |
| 2877 | ic->journal_uptodate = true; |
| 2878 | } |
| 2879 | |
| 2880 | static void dm_integrity_resume(struct dm_target *ti) |
| 2881 | { |
| 2882 | struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private; |
| 2883 | int r; |
| 2884 | DEBUG_print("resume\n"); |
| 2885 | |
| 2886 | if (ic->sb->flags & cpu_to_le32(SB_FLAG_DIRTY_BITMAP)) { |
| 2887 | DEBUG_print("resume dirty_bitmap\n"); |
| 2888 | rw_journal_sectors(ic, REQ_OP_READ, 0, 0, |
| 2889 | ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL); |
| 2890 | if (ic->mode == 'B') { |
| 2891 | if (ic->sb->log2_blocks_per_bitmap_bit == ic->log2_blocks_per_bitmap_bit) { |
| 2892 | block_bitmap_copy(ic, ic->recalc_bitmap, ic->journal); |
| 2893 | block_bitmap_copy(ic, ic->may_write_bitmap, ic->journal); |
| 2894 | if (!block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, |
| 2895 | BITMAP_OP_TEST_ALL_CLEAR)) { |
| 2896 | ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING); |
| 2897 | ic->sb->recalc_sector = cpu_to_le64(0); |
| 2898 | } |
| 2899 | } else { |
| 2900 | DEBUG_print("non-matching blocks_per_bitmap_bit: %u, %u\n", |
| 2901 | ic->sb->log2_blocks_per_bitmap_bit, ic->log2_blocks_per_bitmap_bit); |
| 2902 | ic->sb->log2_blocks_per_bitmap_bit = ic->log2_blocks_per_bitmap_bit; |
| 2903 | block_bitmap_op(ic, ic->recalc_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_SET); |
| 2904 | block_bitmap_op(ic, ic->may_write_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_SET); |
| 2905 | block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_SET); |
| 2906 | rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, 0, |
| 2907 | ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL); |
| 2908 | ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING); |
| 2909 | ic->sb->recalc_sector = cpu_to_le64(0); |
| 2910 | } |
| 2911 | } else { |
| 2912 | if (!(ic->sb->log2_blocks_per_bitmap_bit == ic->log2_blocks_per_bitmap_bit && |
| 2913 | block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_TEST_ALL_CLEAR))) { |
| 2914 | ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING); |
| 2915 | ic->sb->recalc_sector = cpu_to_le64(0); |
| 2916 | } |
| 2917 | init_journal(ic, 0, ic->journal_sections, 0); |
| 2918 | replay_journal(ic); |
| 2919 | ic->sb->flags &= ~cpu_to_le32(SB_FLAG_DIRTY_BITMAP); |
| 2920 | } |
| 2921 | r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA); |
| 2922 | if (unlikely(r)) |
| 2923 | dm_integrity_io_error(ic, "writing superblock", r); |
| 2924 | } else { |
| 2925 | replay_journal(ic); |
| 2926 | if (ic->mode == 'B') { |
| 2927 | ic->sb->flags |= cpu_to_le32(SB_FLAG_DIRTY_BITMAP); |
| 2928 | ic->sb->log2_blocks_per_bitmap_bit = ic->log2_blocks_per_bitmap_bit; |
| 2929 | r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA); |
| 2930 | if (unlikely(r)) |
| 2931 | dm_integrity_io_error(ic, "writing superblock", r); |
| 2932 | |
| 2933 | block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR); |
| 2934 | block_bitmap_op(ic, ic->recalc_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR); |
| 2935 | block_bitmap_op(ic, ic->may_write_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR); |
| 2936 | if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING) && |
| 2937 | le64_to_cpu(ic->sb->recalc_sector) < ic->provided_data_sectors) { |
| 2938 | block_bitmap_op(ic, ic->journal, le64_to_cpu(ic->sb->recalc_sector), |
| 2939 | ic->provided_data_sectors - le64_to_cpu(ic->sb->recalc_sector), BITMAP_OP_SET); |
| 2940 | block_bitmap_op(ic, ic->recalc_bitmap, le64_to_cpu(ic->sb->recalc_sector), |
| 2941 | ic->provided_data_sectors - le64_to_cpu(ic->sb->recalc_sector), BITMAP_OP_SET); |
| 2942 | block_bitmap_op(ic, ic->may_write_bitmap, le64_to_cpu(ic->sb->recalc_sector), |
| 2943 | ic->provided_data_sectors - le64_to_cpu(ic->sb->recalc_sector), BITMAP_OP_SET); |
| 2944 | } |
| 2945 | rw_journal_sectors(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, 0, |
| 2946 | ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL); |
| 2947 | } |
| 2948 | } |
| 2949 | |
| 2950 | DEBUG_print("testing recalc: %x\n", ic->sb->flags); |
| 2951 | if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) { |
| 2952 | __u64 recalc_pos = le64_to_cpu(ic->sb->recalc_sector); |
| 2953 | DEBUG_print("recalc pos: %lx / %lx\n", (long)recalc_pos, ic->provided_data_sectors); |
| 2954 | if (recalc_pos < ic->provided_data_sectors) { |
| 2955 | queue_work(ic->recalc_wq, &ic->recalc_work); |
| 2956 | } else if (recalc_pos > ic->provided_data_sectors) { |
| 2957 | ic->sb->recalc_sector = cpu_to_le64(ic->provided_data_sectors); |
| 2958 | recalc_write_super(ic); |
| 2959 | } |
| 2960 | } |
| 2961 | |
| 2962 | ic->reboot_notifier.notifier_call = dm_integrity_reboot; |
| 2963 | ic->reboot_notifier.next = NULL; |
| 2964 | ic->reboot_notifier.priority = INT_MAX - 1; /* be notified after md and before hardware drivers */ |
| 2965 | WARN_ON(register_reboot_notifier(&ic->reboot_notifier)); |
| 2966 | |
| 2967 | #if 0 |
| 2968 | /* set to 1 to stress test synchronous mode */ |
| 2969 | dm_integrity_enter_synchronous_mode(ic); |
| 2970 | #endif |
| 2971 | } |
| 2972 | |
| 2973 | static void dm_integrity_status(struct dm_target *ti, status_type_t type, |
| 2974 | unsigned status_flags, char *result, unsigned maxlen) |
| 2975 | { |
| 2976 | struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private; |
| 2977 | unsigned arg_count; |
| 2978 | size_t sz = 0; |
| 2979 | |
| 2980 | switch (type) { |
| 2981 | case STATUSTYPE_INFO: |
| 2982 | DMEMIT("%llu %llu", |
| 2983 | (unsigned long long)atomic64_read(&ic->number_of_mismatches), |
| 2984 | (unsigned long long)ic->provided_data_sectors); |
| 2985 | if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) |
| 2986 | DMEMIT(" %llu", (unsigned long long)le64_to_cpu(ic->sb->recalc_sector)); |
| 2987 | else |
| 2988 | DMEMIT(" -"); |
| 2989 | break; |
| 2990 | |
| 2991 | case STATUSTYPE_TABLE: { |
| 2992 | __u64 watermark_percentage = (__u64)(ic->journal_entries - ic->free_sectors_threshold) * 100; |
| 2993 | watermark_percentage += ic->journal_entries / 2; |
| 2994 | do_div(watermark_percentage, ic->journal_entries); |
| 2995 | arg_count = 3; |
| 2996 | arg_count += !!ic->meta_dev; |
| 2997 | arg_count += ic->sectors_per_block != 1; |
| 2998 | arg_count += !!(ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)); |
| 2999 | arg_count += ic->mode == 'J'; |
| 3000 | arg_count += ic->mode == 'J'; |
| 3001 | arg_count += ic->mode == 'B'; |
| 3002 | arg_count += ic->mode == 'B'; |
| 3003 | arg_count += !!ic->internal_hash_alg.alg_string; |
| 3004 | arg_count += !!ic->journal_crypt_alg.alg_string; |
| 3005 | arg_count += !!ic->journal_mac_alg.alg_string; |
| 3006 | arg_count += ic->legacy_recalculate; |
| 3007 | DMEMIT("%s %llu %u %c %u", ic->dev->name, (unsigned long long)ic->start, |
| 3008 | ic->tag_size, ic->mode, arg_count); |
| 3009 | if (ic->meta_dev) |
| 3010 | DMEMIT(" meta_device:%s", ic->meta_dev->name); |
| 3011 | if (ic->sectors_per_block != 1) |
| 3012 | DMEMIT(" block_size:%u", ic->sectors_per_block << SECTOR_SHIFT); |
| 3013 | if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) |
| 3014 | DMEMIT(" recalculate"); |
| 3015 | DMEMIT(" journal_sectors:%u", ic->initial_sectors - SB_SECTORS); |
| 3016 | DMEMIT(" interleave_sectors:%u", 1U << ic->sb->log2_interleave_sectors); |
| 3017 | DMEMIT(" buffer_sectors:%u", 1U << ic->log2_buffer_sectors); |
| 3018 | if (ic->mode == 'J') { |
| 3019 | DMEMIT(" journal_watermark:%u", (unsigned)watermark_percentage); |
| 3020 | DMEMIT(" commit_time:%u", ic->autocommit_msec); |
| 3021 | } |
| 3022 | if (ic->mode == 'B') { |
| 3023 | DMEMIT(" sectors_per_bit:%llu", (unsigned long long)ic->sectors_per_block << ic->log2_blocks_per_bitmap_bit); |
| 3024 | DMEMIT(" bitmap_flush_interval:%u", jiffies_to_msecs(ic->bitmap_flush_interval)); |
| 3025 | } |
| 3026 | if (ic->legacy_recalculate) |
| 3027 | DMEMIT(" legacy_recalculate"); |
| 3028 | |
| 3029 | #define EMIT_ALG(a, n) \ |
| 3030 | do { \ |
| 3031 | if (ic->a.alg_string) { \ |
| 3032 | DMEMIT(" %s:%s", n, ic->a.alg_string); \ |
| 3033 | if (ic->a.key_string) \ |
| 3034 | DMEMIT(":%s", ic->a.key_string);\ |
| 3035 | } \ |
| 3036 | } while (0) |
| 3037 | EMIT_ALG(internal_hash_alg, "internal_hash"); |
| 3038 | EMIT_ALG(journal_crypt_alg, "journal_crypt"); |
| 3039 | EMIT_ALG(journal_mac_alg, "journal_mac"); |
| 3040 | break; |
| 3041 | } |
| 3042 | } |
| 3043 | } |
| 3044 | |
| 3045 | static int dm_integrity_iterate_devices(struct dm_target *ti, |
| 3046 | iterate_devices_callout_fn fn, void *data) |
| 3047 | { |
| 3048 | struct dm_integrity_c *ic = ti->private; |
| 3049 | |
| 3050 | if (!ic->meta_dev) |
| 3051 | return fn(ti, ic->dev, ic->start + ic->initial_sectors + ic->metadata_run, ti->len, data); |
| 3052 | else |
| 3053 | return fn(ti, ic->dev, 0, ti->len, data); |
| 3054 | } |
| 3055 | |
| 3056 | static void dm_integrity_io_hints(struct dm_target *ti, struct queue_limits *limits) |
| 3057 | { |
| 3058 | struct dm_integrity_c *ic = ti->private; |
| 3059 | |
| 3060 | if (ic->sectors_per_block > 1) { |
| 3061 | limits->logical_block_size = ic->sectors_per_block << SECTOR_SHIFT; |
| 3062 | limits->physical_block_size = ic->sectors_per_block << SECTOR_SHIFT; |
| 3063 | blk_limits_io_min(limits, ic->sectors_per_block << SECTOR_SHIFT); |
| 3064 | } |
| 3065 | } |
| 3066 | |
| 3067 | static void calculate_journal_section_size(struct dm_integrity_c *ic) |
| 3068 | { |
| 3069 | unsigned sector_space = JOURNAL_SECTOR_DATA; |
| 3070 | |
| 3071 | ic->journal_sections = le32_to_cpu(ic->sb->journal_sections); |
| 3072 | ic->journal_entry_size = roundup(offsetof(struct journal_entry, last_bytes[ic->sectors_per_block]) + ic->tag_size, |
| 3073 | JOURNAL_ENTRY_ROUNDUP); |
| 3074 | |
| 3075 | if (ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC)) |
| 3076 | sector_space -= JOURNAL_MAC_PER_SECTOR; |
| 3077 | ic->journal_entries_per_sector = sector_space / ic->journal_entry_size; |
| 3078 | ic->journal_section_entries = ic->journal_entries_per_sector * JOURNAL_BLOCK_SECTORS; |
| 3079 | ic->journal_section_sectors = (ic->journal_section_entries << ic->sb->log2_sectors_per_block) + JOURNAL_BLOCK_SECTORS; |
| 3080 | ic->journal_entries = ic->journal_section_entries * ic->journal_sections; |
| 3081 | } |
| 3082 | |
| 3083 | static int calculate_device_limits(struct dm_integrity_c *ic) |
| 3084 | { |
| 3085 | __u64 initial_sectors; |
| 3086 | |
| 3087 | calculate_journal_section_size(ic); |
| 3088 | initial_sectors = SB_SECTORS + (__u64)ic->journal_section_sectors * ic->journal_sections; |
| 3089 | if (initial_sectors + METADATA_PADDING_SECTORS >= ic->meta_device_sectors || initial_sectors > UINT_MAX) |
| 3090 | return -EINVAL; |
| 3091 | ic->initial_sectors = initial_sectors; |
| 3092 | |
| 3093 | if (!ic->meta_dev) { |
| 3094 | sector_t last_sector, last_area, last_offset; |
| 3095 | |
| 3096 | ic->metadata_run = roundup((__u64)ic->tag_size << (ic->sb->log2_interleave_sectors - ic->sb->log2_sectors_per_block), |
| 3097 | (__u64)(1 << SECTOR_SHIFT << METADATA_PADDING_SECTORS)) >> SECTOR_SHIFT; |
| 3098 | if (!(ic->metadata_run & (ic->metadata_run - 1))) |
| 3099 | ic->log2_metadata_run = __ffs(ic->metadata_run); |
| 3100 | else |
| 3101 | ic->log2_metadata_run = -1; |
| 3102 | |
| 3103 | get_area_and_offset(ic, ic->provided_data_sectors - 1, &last_area, &last_offset); |
| 3104 | last_sector = get_data_sector(ic, last_area, last_offset); |
| 3105 | if (last_sector < ic->start || last_sector >= ic->meta_device_sectors) |
| 3106 | return -EINVAL; |
| 3107 | } else { |
| 3108 | __u64 meta_size = (ic->provided_data_sectors >> ic->sb->log2_sectors_per_block) * ic->tag_size; |
| 3109 | meta_size = (meta_size + ((1U << (ic->log2_buffer_sectors + SECTOR_SHIFT)) - 1)) |
| 3110 | >> (ic->log2_buffer_sectors + SECTOR_SHIFT); |
| 3111 | meta_size <<= ic->log2_buffer_sectors; |
| 3112 | if (ic->initial_sectors + meta_size < ic->initial_sectors || |
| 3113 | ic->initial_sectors + meta_size > ic->meta_device_sectors) |
| 3114 | return -EINVAL; |
| 3115 | ic->metadata_run = 1; |
| 3116 | ic->log2_metadata_run = 0; |
| 3117 | } |
| 3118 | |
| 3119 | return 0; |
| 3120 | } |
| 3121 | |
| 3122 | static int initialize_superblock(struct dm_integrity_c *ic, unsigned journal_sectors, unsigned interleave_sectors) |
| 3123 | { |
| 3124 | unsigned journal_sections; |
| 3125 | int test_bit; |
| 3126 | |
| 3127 | memset(ic->sb, 0, SB_SECTORS << SECTOR_SHIFT); |
| 3128 | memcpy(ic->sb->magic, SB_MAGIC, 8); |
| 3129 | ic->sb->integrity_tag_size = cpu_to_le16(ic->tag_size); |
| 3130 | ic->sb->log2_sectors_per_block = __ffs(ic->sectors_per_block); |
| 3131 | if (ic->journal_mac_alg.alg_string) |
| 3132 | ic->sb->flags |= cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC); |
| 3133 | |
| 3134 | calculate_journal_section_size(ic); |
| 3135 | journal_sections = journal_sectors / ic->journal_section_sectors; |
| 3136 | if (!journal_sections) |
| 3137 | journal_sections = 1; |
| 3138 | |
| 3139 | if (!ic->meta_dev) { |
| 3140 | ic->sb->journal_sections = cpu_to_le32(journal_sections); |
| 3141 | if (!interleave_sectors) |
| 3142 | interleave_sectors = DEFAULT_INTERLEAVE_SECTORS; |
| 3143 | ic->sb->log2_interleave_sectors = __fls(interleave_sectors); |
| 3144 | ic->sb->log2_interleave_sectors = max((__u8)MIN_LOG2_INTERLEAVE_SECTORS, ic->sb->log2_interleave_sectors); |
| 3145 | ic->sb->log2_interleave_sectors = min((__u8)MAX_LOG2_INTERLEAVE_SECTORS, ic->sb->log2_interleave_sectors); |
| 3146 | |
| 3147 | ic->provided_data_sectors = 0; |
| 3148 | for (test_bit = fls64(ic->meta_device_sectors) - 1; test_bit >= 3; test_bit--) { |
| 3149 | __u64 prev_data_sectors = ic->provided_data_sectors; |
| 3150 | |
| 3151 | ic->provided_data_sectors |= (sector_t)1 << test_bit; |
| 3152 | if (calculate_device_limits(ic)) |
| 3153 | ic->provided_data_sectors = prev_data_sectors; |
| 3154 | } |
| 3155 | if (!ic->provided_data_sectors) |
| 3156 | return -EINVAL; |
| 3157 | } else { |
| 3158 | ic->sb->log2_interleave_sectors = 0; |
| 3159 | ic->provided_data_sectors = ic->data_device_sectors; |
| 3160 | ic->provided_data_sectors &= ~(sector_t)(ic->sectors_per_block - 1); |
| 3161 | |
| 3162 | try_smaller_buffer: |
| 3163 | ic->sb->journal_sections = cpu_to_le32(0); |
| 3164 | for (test_bit = fls(journal_sections) - 1; test_bit >= 0; test_bit--) { |
| 3165 | __u32 prev_journal_sections = le32_to_cpu(ic->sb->journal_sections); |
| 3166 | __u32 test_journal_sections = prev_journal_sections | (1U << test_bit); |
| 3167 | if (test_journal_sections > journal_sections) |
| 3168 | continue; |
| 3169 | ic->sb->journal_sections = cpu_to_le32(test_journal_sections); |
| 3170 | if (calculate_device_limits(ic)) |
| 3171 | ic->sb->journal_sections = cpu_to_le32(prev_journal_sections); |
| 3172 | |
| 3173 | } |
| 3174 | if (!le32_to_cpu(ic->sb->journal_sections)) { |
| 3175 | if (ic->log2_buffer_sectors > 3) { |
| 3176 | ic->log2_buffer_sectors--; |
| 3177 | goto try_smaller_buffer; |
| 3178 | } |
| 3179 | return -EINVAL; |
| 3180 | } |
| 3181 | } |
| 3182 | |
| 3183 | ic->sb->provided_data_sectors = cpu_to_le64(ic->provided_data_sectors); |
| 3184 | |
| 3185 | sb_set_version(ic); |
| 3186 | |
| 3187 | return 0; |
| 3188 | } |
| 3189 | |
| 3190 | static void dm_integrity_set(struct dm_target *ti, struct dm_integrity_c *ic) |
| 3191 | { |
| 3192 | struct gendisk *disk = dm_disk(dm_table_get_md(ti->table)); |
| 3193 | struct blk_integrity bi; |
| 3194 | |
| 3195 | memset(&bi, 0, sizeof(bi)); |
| 3196 | bi.profile = &dm_integrity_profile; |
| 3197 | bi.tuple_size = ic->tag_size; |
| 3198 | bi.tag_size = bi.tuple_size; |
| 3199 | bi.interval_exp = ic->sb->log2_sectors_per_block + SECTOR_SHIFT; |
| 3200 | |
| 3201 | blk_integrity_register(disk, &bi); |
| 3202 | blk_queue_max_integrity_segments(disk->queue, UINT_MAX); |
| 3203 | } |
| 3204 | |
| 3205 | static void dm_integrity_free_page_list(struct page_list *pl) |
| 3206 | { |
| 3207 | unsigned i; |
| 3208 | |
| 3209 | if (!pl) |
| 3210 | return; |
| 3211 | for (i = 0; pl[i].page; i++) |
| 3212 | __free_page(pl[i].page); |
| 3213 | kvfree(pl); |
| 3214 | } |
| 3215 | |
| 3216 | static struct page_list *dm_integrity_alloc_page_list(unsigned n_pages) |
| 3217 | { |
| 3218 | struct page_list *pl; |
| 3219 | unsigned i; |
| 3220 | |
| 3221 | pl = kvmalloc_array(n_pages + 1, sizeof(struct page_list), GFP_KERNEL | __GFP_ZERO); |
| 3222 | if (!pl) |
| 3223 | return NULL; |
| 3224 | |
| 3225 | for (i = 0; i < n_pages; i++) { |
| 3226 | pl[i].page = alloc_page(GFP_KERNEL); |
| 3227 | if (!pl[i].page) { |
| 3228 | dm_integrity_free_page_list(pl); |
| 3229 | return NULL; |
| 3230 | } |
| 3231 | if (i) |
| 3232 | pl[i - 1].next = &pl[i]; |
| 3233 | } |
| 3234 | pl[i].page = NULL; |
| 3235 | pl[i].next = NULL; |
| 3236 | |
| 3237 | return pl; |
| 3238 | } |
| 3239 | |
| 3240 | static void dm_integrity_free_journal_scatterlist(struct dm_integrity_c *ic, struct scatterlist **sl) |
| 3241 | { |
| 3242 | unsigned i; |
| 3243 | for (i = 0; i < ic->journal_sections; i++) |
| 3244 | kvfree(sl[i]); |
| 3245 | kvfree(sl); |
| 3246 | } |
| 3247 | |
| 3248 | static struct scatterlist **dm_integrity_alloc_journal_scatterlist(struct dm_integrity_c *ic, |
| 3249 | struct page_list *pl) |
| 3250 | { |
| 3251 | struct scatterlist **sl; |
| 3252 | unsigned i; |
| 3253 | |
| 3254 | sl = kvmalloc_array(ic->journal_sections, |
| 3255 | sizeof(struct scatterlist *), |
| 3256 | GFP_KERNEL | __GFP_ZERO); |
| 3257 | if (!sl) |
| 3258 | return NULL; |
| 3259 | |
| 3260 | for (i = 0; i < ic->journal_sections; i++) { |
| 3261 | struct scatterlist *s; |
| 3262 | unsigned start_index, start_offset; |
| 3263 | unsigned end_index, end_offset; |
| 3264 | unsigned n_pages; |
| 3265 | unsigned idx; |
| 3266 | |
| 3267 | page_list_location(ic, i, 0, &start_index, &start_offset); |
| 3268 | page_list_location(ic, i, ic->journal_section_sectors - 1, |
| 3269 | &end_index, &end_offset); |
| 3270 | |
| 3271 | n_pages = (end_index - start_index + 1); |
| 3272 | |
| 3273 | s = kvmalloc_array(n_pages, sizeof(struct scatterlist), |
| 3274 | GFP_KERNEL); |
| 3275 | if (!s) { |
| 3276 | dm_integrity_free_journal_scatterlist(ic, sl); |
| 3277 | return NULL; |
| 3278 | } |
| 3279 | |
| 3280 | sg_init_table(s, n_pages); |
| 3281 | for (idx = start_index; idx <= end_index; idx++) { |
| 3282 | char *va = lowmem_page_address(pl[idx].page); |
| 3283 | unsigned start = 0, end = PAGE_SIZE; |
| 3284 | if (idx == start_index) |
| 3285 | start = start_offset; |
| 3286 | if (idx == end_index) |
| 3287 | end = end_offset + (1 << SECTOR_SHIFT); |
| 3288 | sg_set_buf(&s[idx - start_index], va + start, end - start); |
| 3289 | } |
| 3290 | |
| 3291 | sl[i] = s; |
| 3292 | } |
| 3293 | |
| 3294 | return sl; |
| 3295 | } |
| 3296 | |
| 3297 | static void free_alg(struct alg_spec *a) |
| 3298 | { |
| 3299 | kzfree(a->alg_string); |
| 3300 | kzfree(a->key); |
| 3301 | memset(a, 0, sizeof *a); |
| 3302 | } |
| 3303 | |
| 3304 | static int get_alg_and_key(const char *arg, struct alg_spec *a, char **error, char *error_inval) |
| 3305 | { |
| 3306 | char *k; |
| 3307 | |
| 3308 | free_alg(a); |
| 3309 | |
| 3310 | a->alg_string = kstrdup(strchr(arg, ':') + 1, GFP_KERNEL); |
| 3311 | if (!a->alg_string) |
| 3312 | goto nomem; |
| 3313 | |
| 3314 | k = strchr(a->alg_string, ':'); |
| 3315 | if (k) { |
| 3316 | *k = 0; |
| 3317 | a->key_string = k + 1; |
| 3318 | if (strlen(a->key_string) & 1) |
| 3319 | goto inval; |
| 3320 | |
| 3321 | a->key_size = strlen(a->key_string) / 2; |
| 3322 | a->key = kmalloc(a->key_size, GFP_KERNEL); |
| 3323 | if (!a->key) |
| 3324 | goto nomem; |
| 3325 | if (hex2bin(a->key, a->key_string, a->key_size)) |
| 3326 | goto inval; |
| 3327 | } |
| 3328 | |
| 3329 | return 0; |
| 3330 | inval: |
| 3331 | *error = error_inval; |
| 3332 | return -EINVAL; |
| 3333 | nomem: |
| 3334 | *error = "Out of memory for an argument"; |
| 3335 | return -ENOMEM; |
| 3336 | } |
| 3337 | |
| 3338 | static int get_mac(struct crypto_shash **hash, struct alg_spec *a, char **error, |
| 3339 | char *error_alg, char *error_key) |
| 3340 | { |
| 3341 | int r; |
| 3342 | |
| 3343 | if (a->alg_string) { |
| 3344 | *hash = crypto_alloc_shash(a->alg_string, 0, 0); |
| 3345 | if (IS_ERR(*hash)) { |
| 3346 | *error = error_alg; |
| 3347 | r = PTR_ERR(*hash); |
| 3348 | *hash = NULL; |
| 3349 | return r; |
| 3350 | } |
| 3351 | |
| 3352 | if (a->key) { |
| 3353 | r = crypto_shash_setkey(*hash, a->key, a->key_size); |
| 3354 | if (r) { |
| 3355 | *error = error_key; |
| 3356 | return r; |
| 3357 | } |
| 3358 | } else if (crypto_shash_get_flags(*hash) & CRYPTO_TFM_NEED_KEY) { |
| 3359 | *error = error_key; |
| 3360 | return -ENOKEY; |
| 3361 | } |
| 3362 | } |
| 3363 | |
| 3364 | return 0; |
| 3365 | } |
| 3366 | |
| 3367 | static int create_journal(struct dm_integrity_c *ic, char **error) |
| 3368 | { |
| 3369 | int r = 0; |
| 3370 | unsigned i; |
| 3371 | __u64 journal_pages, journal_desc_size, journal_tree_size; |
| 3372 | unsigned char *crypt_data = NULL, *crypt_iv = NULL; |
| 3373 | struct skcipher_request *req = NULL; |
| 3374 | |
| 3375 | ic->commit_ids[0] = cpu_to_le64(0x1111111111111111ULL); |
| 3376 | ic->commit_ids[1] = cpu_to_le64(0x2222222222222222ULL); |
| 3377 | ic->commit_ids[2] = cpu_to_le64(0x3333333333333333ULL); |
| 3378 | ic->commit_ids[3] = cpu_to_le64(0x4444444444444444ULL); |
| 3379 | |
| 3380 | journal_pages = roundup((__u64)ic->journal_sections * ic->journal_section_sectors, |
| 3381 | PAGE_SIZE >> SECTOR_SHIFT) >> (PAGE_SHIFT - SECTOR_SHIFT); |
| 3382 | journal_desc_size = journal_pages * sizeof(struct page_list); |
| 3383 | if (journal_pages >= totalram_pages() - totalhigh_pages() || journal_desc_size > ULONG_MAX) { |
| 3384 | *error = "Journal doesn't fit into memory"; |
| 3385 | r = -ENOMEM; |
| 3386 | goto bad; |
| 3387 | } |
| 3388 | ic->journal_pages = journal_pages; |
| 3389 | |
| 3390 | ic->journal = dm_integrity_alloc_page_list(ic->journal_pages); |
| 3391 | if (!ic->journal) { |
| 3392 | *error = "Could not allocate memory for journal"; |
| 3393 | r = -ENOMEM; |
| 3394 | goto bad; |
| 3395 | } |
| 3396 | if (ic->journal_crypt_alg.alg_string) { |
| 3397 | unsigned ivsize, blocksize; |
| 3398 | struct journal_completion comp; |
| 3399 | |
| 3400 | comp.ic = ic; |
| 3401 | ic->journal_crypt = crypto_alloc_skcipher(ic->journal_crypt_alg.alg_string, 0, 0); |
| 3402 | if (IS_ERR(ic->journal_crypt)) { |
| 3403 | *error = "Invalid journal cipher"; |
| 3404 | r = PTR_ERR(ic->journal_crypt); |
| 3405 | ic->journal_crypt = NULL; |
| 3406 | goto bad; |
| 3407 | } |
| 3408 | ivsize = crypto_skcipher_ivsize(ic->journal_crypt); |
| 3409 | blocksize = crypto_skcipher_blocksize(ic->journal_crypt); |
| 3410 | |
| 3411 | if (ic->journal_crypt_alg.key) { |
| 3412 | r = crypto_skcipher_setkey(ic->journal_crypt, ic->journal_crypt_alg.key, |
| 3413 | ic->journal_crypt_alg.key_size); |
| 3414 | if (r) { |
| 3415 | *error = "Error setting encryption key"; |
| 3416 | goto bad; |
| 3417 | } |
| 3418 | } |
| 3419 | DEBUG_print("cipher %s, block size %u iv size %u\n", |
| 3420 | ic->journal_crypt_alg.alg_string, blocksize, ivsize); |
| 3421 | |
| 3422 | ic->journal_io = dm_integrity_alloc_page_list(ic->journal_pages); |
| 3423 | if (!ic->journal_io) { |
| 3424 | *error = "Could not allocate memory for journal io"; |
| 3425 | r = -ENOMEM; |
| 3426 | goto bad; |
| 3427 | } |
| 3428 | |
| 3429 | if (blocksize == 1) { |
| 3430 | struct scatterlist *sg; |
| 3431 | |
| 3432 | req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL); |
| 3433 | if (!req) { |
| 3434 | *error = "Could not allocate crypt request"; |
| 3435 | r = -ENOMEM; |
| 3436 | goto bad; |
| 3437 | } |
| 3438 | |
| 3439 | crypt_iv = kzalloc(ivsize, GFP_KERNEL); |
| 3440 | if (!crypt_iv) { |
| 3441 | *error = "Could not allocate iv"; |
| 3442 | r = -ENOMEM; |
| 3443 | goto bad; |
| 3444 | } |
| 3445 | |
| 3446 | ic->journal_xor = dm_integrity_alloc_page_list(ic->journal_pages); |
| 3447 | if (!ic->journal_xor) { |
| 3448 | *error = "Could not allocate memory for journal xor"; |
| 3449 | r = -ENOMEM; |
| 3450 | goto bad; |
| 3451 | } |
| 3452 | |
| 3453 | sg = kvmalloc_array(ic->journal_pages + 1, |
| 3454 | sizeof(struct scatterlist), |
| 3455 | GFP_KERNEL); |
| 3456 | if (!sg) { |
| 3457 | *error = "Unable to allocate sg list"; |
| 3458 | r = -ENOMEM; |
| 3459 | goto bad; |
| 3460 | } |
| 3461 | sg_init_table(sg, ic->journal_pages + 1); |
| 3462 | for (i = 0; i < ic->journal_pages; i++) { |
| 3463 | char *va = lowmem_page_address(ic->journal_xor[i].page); |
| 3464 | clear_page(va); |
| 3465 | sg_set_buf(&sg[i], va, PAGE_SIZE); |
| 3466 | } |
| 3467 | sg_set_buf(&sg[i], &ic->commit_ids, sizeof ic->commit_ids); |
| 3468 | |
| 3469 | skcipher_request_set_crypt(req, sg, sg, |
| 3470 | PAGE_SIZE * ic->journal_pages + sizeof ic->commit_ids, crypt_iv); |
| 3471 | init_completion(&comp.comp); |
| 3472 | comp.in_flight = (atomic_t)ATOMIC_INIT(1); |
| 3473 | if (do_crypt(true, req, &comp)) |
| 3474 | wait_for_completion(&comp.comp); |
| 3475 | kvfree(sg); |
| 3476 | r = dm_integrity_failed(ic); |
| 3477 | if (r) { |
| 3478 | *error = "Unable to encrypt journal"; |
| 3479 | goto bad; |
| 3480 | } |
| 3481 | DEBUG_bytes(lowmem_page_address(ic->journal_xor[0].page), 64, "xor data"); |
| 3482 | |
| 3483 | crypto_free_skcipher(ic->journal_crypt); |
| 3484 | ic->journal_crypt = NULL; |
| 3485 | } else { |
| 3486 | unsigned crypt_len = roundup(ivsize, blocksize); |
| 3487 | |
| 3488 | req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL); |
| 3489 | if (!req) { |
| 3490 | *error = "Could not allocate crypt request"; |
| 3491 | r = -ENOMEM; |
| 3492 | goto bad; |
| 3493 | } |
| 3494 | |
| 3495 | crypt_iv = kmalloc(ivsize, GFP_KERNEL); |
| 3496 | if (!crypt_iv) { |
| 3497 | *error = "Could not allocate iv"; |
| 3498 | r = -ENOMEM; |
| 3499 | goto bad; |
| 3500 | } |
| 3501 | |
| 3502 | crypt_data = kmalloc(crypt_len, GFP_KERNEL); |
| 3503 | if (!crypt_data) { |
| 3504 | *error = "Unable to allocate crypt data"; |
| 3505 | r = -ENOMEM; |
| 3506 | goto bad; |
| 3507 | } |
| 3508 | |
| 3509 | ic->journal_scatterlist = dm_integrity_alloc_journal_scatterlist(ic, ic->journal); |
| 3510 | if (!ic->journal_scatterlist) { |
| 3511 | *error = "Unable to allocate sg list"; |
| 3512 | r = -ENOMEM; |
| 3513 | goto bad; |
| 3514 | } |
| 3515 | ic->journal_io_scatterlist = dm_integrity_alloc_journal_scatterlist(ic, ic->journal_io); |
| 3516 | if (!ic->journal_io_scatterlist) { |
| 3517 | *error = "Unable to allocate sg list"; |
| 3518 | r = -ENOMEM; |
| 3519 | goto bad; |
| 3520 | } |
| 3521 | ic->sk_requests = kvmalloc_array(ic->journal_sections, |
| 3522 | sizeof(struct skcipher_request *), |
| 3523 | GFP_KERNEL | __GFP_ZERO); |
| 3524 | if (!ic->sk_requests) { |
| 3525 | *error = "Unable to allocate sk requests"; |
| 3526 | r = -ENOMEM; |
| 3527 | goto bad; |
| 3528 | } |
| 3529 | for (i = 0; i < ic->journal_sections; i++) { |
| 3530 | struct scatterlist sg; |
| 3531 | struct skcipher_request *section_req; |
| 3532 | __u32 section_le = cpu_to_le32(i); |
| 3533 | |
| 3534 | memset(crypt_iv, 0x00, ivsize); |
| 3535 | memset(crypt_data, 0x00, crypt_len); |
| 3536 | memcpy(crypt_data, §ion_le, min((size_t)crypt_len, sizeof(section_le))); |
| 3537 | |
| 3538 | sg_init_one(&sg, crypt_data, crypt_len); |
| 3539 | skcipher_request_set_crypt(req, &sg, &sg, crypt_len, crypt_iv); |
| 3540 | init_completion(&comp.comp); |
| 3541 | comp.in_flight = (atomic_t)ATOMIC_INIT(1); |
| 3542 | if (do_crypt(true, req, &comp)) |
| 3543 | wait_for_completion(&comp.comp); |
| 3544 | |
| 3545 | r = dm_integrity_failed(ic); |
| 3546 | if (r) { |
| 3547 | *error = "Unable to generate iv"; |
| 3548 | goto bad; |
| 3549 | } |
| 3550 | |
| 3551 | section_req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL); |
| 3552 | if (!section_req) { |
| 3553 | *error = "Unable to allocate crypt request"; |
| 3554 | r = -ENOMEM; |
| 3555 | goto bad; |
| 3556 | } |
| 3557 | section_req->iv = kmalloc_array(ivsize, 2, |
| 3558 | GFP_KERNEL); |
| 3559 | if (!section_req->iv) { |
| 3560 | skcipher_request_free(section_req); |
| 3561 | *error = "Unable to allocate iv"; |
| 3562 | r = -ENOMEM; |
| 3563 | goto bad; |
| 3564 | } |
| 3565 | memcpy(section_req->iv + ivsize, crypt_data, ivsize); |
| 3566 | section_req->cryptlen = (size_t)ic->journal_section_sectors << SECTOR_SHIFT; |
| 3567 | ic->sk_requests[i] = section_req; |
| 3568 | DEBUG_bytes(crypt_data, ivsize, "iv(%u)", i); |
| 3569 | } |
| 3570 | } |
| 3571 | } |
| 3572 | |
| 3573 | for (i = 0; i < N_COMMIT_IDS; i++) { |
| 3574 | unsigned j; |
| 3575 | retest_commit_id: |
| 3576 | for (j = 0; j < i; j++) { |
| 3577 | if (ic->commit_ids[j] == ic->commit_ids[i]) { |
| 3578 | ic->commit_ids[i] = cpu_to_le64(le64_to_cpu(ic->commit_ids[i]) + 1); |
| 3579 | goto retest_commit_id; |
| 3580 | } |
| 3581 | } |
| 3582 | DEBUG_print("commit id %u: %016llx\n", i, ic->commit_ids[i]); |
| 3583 | } |
| 3584 | |
| 3585 | journal_tree_size = (__u64)ic->journal_entries * sizeof(struct journal_node); |
| 3586 | if (journal_tree_size > ULONG_MAX) { |
| 3587 | *error = "Journal doesn't fit into memory"; |
| 3588 | r = -ENOMEM; |
| 3589 | goto bad; |
| 3590 | } |
| 3591 | ic->journal_tree = kvmalloc(journal_tree_size, GFP_KERNEL); |
| 3592 | if (!ic->journal_tree) { |
| 3593 | *error = "Could not allocate memory for journal tree"; |
| 3594 | r = -ENOMEM; |
| 3595 | } |
| 3596 | bad: |
| 3597 | kfree(crypt_data); |
| 3598 | kfree(crypt_iv); |
| 3599 | skcipher_request_free(req); |
| 3600 | |
| 3601 | return r; |
| 3602 | } |
| 3603 | |
| 3604 | /* |
| 3605 | * Construct a integrity mapping |
| 3606 | * |
| 3607 | * Arguments: |
| 3608 | * device |
| 3609 | * offset from the start of the device |
| 3610 | * tag size |
| 3611 | * D - direct writes, J - journal writes, B - bitmap mode, R - recovery mode |
| 3612 | * number of optional arguments |
| 3613 | * optional arguments: |
| 3614 | * journal_sectors |
| 3615 | * interleave_sectors |
| 3616 | * buffer_sectors |
| 3617 | * journal_watermark |
| 3618 | * commit_time |
| 3619 | * meta_device |
| 3620 | * block_size |
| 3621 | * sectors_per_bit |
| 3622 | * bitmap_flush_interval |
| 3623 | * internal_hash |
| 3624 | * journal_crypt |
| 3625 | * journal_mac |
| 3626 | * recalculate |
| 3627 | */ |
| 3628 | static int dm_integrity_ctr(struct dm_target *ti, unsigned argc, char **argv) |
| 3629 | { |
| 3630 | struct dm_integrity_c *ic; |
| 3631 | char dummy; |
| 3632 | int r; |
| 3633 | unsigned extra_args; |
| 3634 | struct dm_arg_set as; |
| 3635 | static const struct dm_arg _args[] = { |
| 3636 | {0, 14, "Invalid number of feature args"}, |
| 3637 | }; |
| 3638 | unsigned journal_sectors, interleave_sectors, buffer_sectors, journal_watermark, sync_msec; |
| 3639 | bool should_write_sb; |
| 3640 | __u64 threshold; |
| 3641 | unsigned long long start; |
| 3642 | __s8 log2_sectors_per_bitmap_bit = -1; |
| 3643 | __s8 log2_blocks_per_bitmap_bit; |
| 3644 | __u64 bits_in_journal; |
| 3645 | __u64 n_bitmap_bits; |
| 3646 | |
| 3647 | #define DIRECT_ARGUMENTS 4 |
| 3648 | |
| 3649 | if (argc <= DIRECT_ARGUMENTS) { |
| 3650 | ti->error = "Invalid argument count"; |
| 3651 | return -EINVAL; |
| 3652 | } |
| 3653 | |
| 3654 | ic = kzalloc(sizeof(struct dm_integrity_c), GFP_KERNEL); |
| 3655 | if (!ic) { |
| 3656 | ti->error = "Cannot allocate integrity context"; |
| 3657 | return -ENOMEM; |
| 3658 | } |
| 3659 | ti->private = ic; |
| 3660 | ti->per_io_data_size = sizeof(struct dm_integrity_io); |
| 3661 | ic->ti = ti; |
| 3662 | |
| 3663 | ic->in_progress = RB_ROOT; |
| 3664 | INIT_LIST_HEAD(&ic->wait_list); |
| 3665 | init_waitqueue_head(&ic->endio_wait); |
| 3666 | bio_list_init(&ic->flush_bio_list); |
| 3667 | init_waitqueue_head(&ic->copy_to_journal_wait); |
| 3668 | init_completion(&ic->crypto_backoff); |
| 3669 | atomic64_set(&ic->number_of_mismatches, 0); |
| 3670 | ic->bitmap_flush_interval = BITMAP_FLUSH_INTERVAL; |
| 3671 | |
| 3672 | r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &ic->dev); |
| 3673 | if (r) { |
| 3674 | ti->error = "Device lookup failed"; |
| 3675 | goto bad; |
| 3676 | } |
| 3677 | |
| 3678 | if (sscanf(argv[1], "%llu%c", &start, &dummy) != 1 || start != (sector_t)start) { |
| 3679 | ti->error = "Invalid starting offset"; |
| 3680 | r = -EINVAL; |
| 3681 | goto bad; |
| 3682 | } |
| 3683 | ic->start = start; |
| 3684 | |
| 3685 | if (strcmp(argv[2], "-")) { |
| 3686 | if (sscanf(argv[2], "%u%c", &ic->tag_size, &dummy) != 1 || !ic->tag_size) { |
| 3687 | ti->error = "Invalid tag size"; |
| 3688 | r = -EINVAL; |
| 3689 | goto bad; |
| 3690 | } |
| 3691 | } |
| 3692 | |
| 3693 | if (!strcmp(argv[3], "J") || !strcmp(argv[3], "B") || |
| 3694 | !strcmp(argv[3], "D") || !strcmp(argv[3], "R")) { |
| 3695 | ic->mode = argv[3][0]; |
| 3696 | } else { |
| 3697 | ti->error = "Invalid mode (expecting J, B, D, R)"; |
| 3698 | r = -EINVAL; |
| 3699 | goto bad; |
| 3700 | } |
| 3701 | |
| 3702 | journal_sectors = 0; |
| 3703 | interleave_sectors = DEFAULT_INTERLEAVE_SECTORS; |
| 3704 | buffer_sectors = DEFAULT_BUFFER_SECTORS; |
| 3705 | journal_watermark = DEFAULT_JOURNAL_WATERMARK; |
| 3706 | sync_msec = DEFAULT_SYNC_MSEC; |
| 3707 | ic->sectors_per_block = 1; |
| 3708 | |
| 3709 | as.argc = argc - DIRECT_ARGUMENTS; |
| 3710 | as.argv = argv + DIRECT_ARGUMENTS; |
| 3711 | r = dm_read_arg_group(_args, &as, &extra_args, &ti->error); |
| 3712 | if (r) |
| 3713 | goto bad; |
| 3714 | |
| 3715 | while (extra_args--) { |
| 3716 | const char *opt_string; |
| 3717 | unsigned val; |
| 3718 | unsigned long long llval; |
| 3719 | opt_string = dm_shift_arg(&as); |
| 3720 | if (!opt_string) { |
| 3721 | r = -EINVAL; |
| 3722 | ti->error = "Not enough feature arguments"; |
| 3723 | goto bad; |
| 3724 | } |
| 3725 | if (sscanf(opt_string, "journal_sectors:%u%c", &val, &dummy) == 1) |
| 3726 | journal_sectors = val ? val : 1; |
| 3727 | else if (sscanf(opt_string, "interleave_sectors:%u%c", &val, &dummy) == 1) |
| 3728 | interleave_sectors = val; |
| 3729 | else if (sscanf(opt_string, "buffer_sectors:%u%c", &val, &dummy) == 1) |
| 3730 | buffer_sectors = val; |
| 3731 | else if (sscanf(opt_string, "journal_watermark:%u%c", &val, &dummy) == 1 && val <= 100) |
| 3732 | journal_watermark = val; |
| 3733 | else if (sscanf(opt_string, "commit_time:%u%c", &val, &dummy) == 1) |
| 3734 | sync_msec = val; |
| 3735 | else if (!strncmp(opt_string, "meta_device:", strlen("meta_device:"))) { |
| 3736 | if (ic->meta_dev) { |
| 3737 | dm_put_device(ti, ic->meta_dev); |
| 3738 | ic->meta_dev = NULL; |
| 3739 | } |
| 3740 | r = dm_get_device(ti, strchr(opt_string, ':') + 1, |
| 3741 | dm_table_get_mode(ti->table), &ic->meta_dev); |
| 3742 | if (r) { |
| 3743 | ti->error = "Device lookup failed"; |
| 3744 | goto bad; |
| 3745 | } |
| 3746 | } else if (sscanf(opt_string, "block_size:%u%c", &val, &dummy) == 1) { |
| 3747 | if (val < 1 << SECTOR_SHIFT || |
| 3748 | val > MAX_SECTORS_PER_BLOCK << SECTOR_SHIFT || |
| 3749 | (val & (val -1))) { |
| 3750 | r = -EINVAL; |
| 3751 | ti->error = "Invalid block_size argument"; |
| 3752 | goto bad; |
| 3753 | } |
| 3754 | ic->sectors_per_block = val >> SECTOR_SHIFT; |
| 3755 | } else if (sscanf(opt_string, "sectors_per_bit:%llu%c", &llval, &dummy) == 1) { |
| 3756 | log2_sectors_per_bitmap_bit = !llval ? 0 : __ilog2_u64(llval); |
| 3757 | } else if (sscanf(opt_string, "bitmap_flush_interval:%u%c", &val, &dummy) == 1) { |
| 3758 | if ((uint64_t)val >= (uint64_t)UINT_MAX * 1000 / HZ) { |
| 3759 | r = -EINVAL; |
| 3760 | ti->error = "Invalid bitmap_flush_interval argument"; |
| 3761 | goto bad; |
| 3762 | } |
| 3763 | ic->bitmap_flush_interval = msecs_to_jiffies(val); |
| 3764 | } else if (!strncmp(opt_string, "internal_hash:", strlen("internal_hash:"))) { |
| 3765 | r = get_alg_and_key(opt_string, &ic->internal_hash_alg, &ti->error, |
| 3766 | "Invalid internal_hash argument"); |
| 3767 | if (r) |
| 3768 | goto bad; |
| 3769 | } else if (!strncmp(opt_string, "journal_crypt:", strlen("journal_crypt:"))) { |
| 3770 | r = get_alg_and_key(opt_string, &ic->journal_crypt_alg, &ti->error, |
| 3771 | "Invalid journal_crypt argument"); |
| 3772 | if (r) |
| 3773 | goto bad; |
| 3774 | } else if (!strncmp(opt_string, "journal_mac:", strlen("journal_mac:"))) { |
| 3775 | r = get_alg_and_key(opt_string, &ic->journal_mac_alg, &ti->error, |
| 3776 | "Invalid journal_mac argument"); |
| 3777 | if (r) |
| 3778 | goto bad; |
| 3779 | } else if (!strcmp(opt_string, "recalculate")) { |
| 3780 | ic->recalculate_flag = true; |
| 3781 | } else if (!strcmp(opt_string, "legacy_recalculate")) { |
| 3782 | ic->legacy_recalculate = true; |
| 3783 | } else { |
| 3784 | r = -EINVAL; |
| 3785 | ti->error = "Invalid argument"; |
| 3786 | goto bad; |
| 3787 | } |
| 3788 | } |
| 3789 | |
| 3790 | ic->data_device_sectors = i_size_read(ic->dev->bdev->bd_inode) >> SECTOR_SHIFT; |
| 3791 | if (!ic->meta_dev) |
| 3792 | ic->meta_device_sectors = ic->data_device_sectors; |
| 3793 | else |
| 3794 | ic->meta_device_sectors = i_size_read(ic->meta_dev->bdev->bd_inode) >> SECTOR_SHIFT; |
| 3795 | |
| 3796 | if (!journal_sectors) { |
| 3797 | journal_sectors = min((sector_t)DEFAULT_MAX_JOURNAL_SECTORS, |
| 3798 | ic->data_device_sectors >> DEFAULT_JOURNAL_SIZE_FACTOR); |
| 3799 | } |
| 3800 | |
| 3801 | if (!buffer_sectors) |
| 3802 | buffer_sectors = 1; |
| 3803 | ic->log2_buffer_sectors = min((int)__fls(buffer_sectors), 31 - SECTOR_SHIFT); |
| 3804 | |
| 3805 | r = get_mac(&ic->internal_hash, &ic->internal_hash_alg, &ti->error, |
| 3806 | "Invalid internal hash", "Error setting internal hash key"); |
| 3807 | if (r) |
| 3808 | goto bad; |
| 3809 | |
| 3810 | r = get_mac(&ic->journal_mac, &ic->journal_mac_alg, &ti->error, |
| 3811 | "Invalid journal mac", "Error setting journal mac key"); |
| 3812 | if (r) |
| 3813 | goto bad; |
| 3814 | |
| 3815 | if (!ic->tag_size) { |
| 3816 | if (!ic->internal_hash) { |
| 3817 | ti->error = "Unknown tag size"; |
| 3818 | r = -EINVAL; |
| 3819 | goto bad; |
| 3820 | } |
| 3821 | ic->tag_size = crypto_shash_digestsize(ic->internal_hash); |
| 3822 | } |
| 3823 | if (ic->tag_size > MAX_TAG_SIZE) { |
| 3824 | ti->error = "Too big tag size"; |
| 3825 | r = -EINVAL; |
| 3826 | goto bad; |
| 3827 | } |
| 3828 | if (!(ic->tag_size & (ic->tag_size - 1))) |
| 3829 | ic->log2_tag_size = __ffs(ic->tag_size); |
| 3830 | else |
| 3831 | ic->log2_tag_size = -1; |
| 3832 | |
| 3833 | if (ic->mode == 'B' && !ic->internal_hash) { |
| 3834 | r = -EINVAL; |
| 3835 | ti->error = "Bitmap mode can be only used with internal hash"; |
| 3836 | goto bad; |
| 3837 | } |
| 3838 | |
| 3839 | ic->autocommit_jiffies = msecs_to_jiffies(sync_msec); |
| 3840 | ic->autocommit_msec = sync_msec; |
| 3841 | timer_setup(&ic->autocommit_timer, autocommit_fn, 0); |
| 3842 | |
| 3843 | ic->io = dm_io_client_create(); |
| 3844 | if (IS_ERR(ic->io)) { |
| 3845 | r = PTR_ERR(ic->io); |
| 3846 | ic->io = NULL; |
| 3847 | ti->error = "Cannot allocate dm io"; |
| 3848 | goto bad; |
| 3849 | } |
| 3850 | |
| 3851 | r = mempool_init_slab_pool(&ic->journal_io_mempool, JOURNAL_IO_MEMPOOL, journal_io_cache); |
| 3852 | if (r) { |
| 3853 | ti->error = "Cannot allocate mempool"; |
| 3854 | goto bad; |
| 3855 | } |
| 3856 | |
| 3857 | ic->metadata_wq = alloc_workqueue("dm-integrity-metadata", |
| 3858 | WQ_MEM_RECLAIM, METADATA_WORKQUEUE_MAX_ACTIVE); |
| 3859 | if (!ic->metadata_wq) { |
| 3860 | ti->error = "Cannot allocate workqueue"; |
| 3861 | r = -ENOMEM; |
| 3862 | goto bad; |
| 3863 | } |
| 3864 | |
| 3865 | /* |
| 3866 | * If this workqueue were percpu, it would cause bio reordering |
| 3867 | * and reduced performance. |
| 3868 | */ |
| 3869 | ic->wait_wq = alloc_workqueue("dm-integrity-wait", WQ_MEM_RECLAIM | WQ_UNBOUND, 1); |
| 3870 | if (!ic->wait_wq) { |
| 3871 | ti->error = "Cannot allocate workqueue"; |
| 3872 | r = -ENOMEM; |
| 3873 | goto bad; |
| 3874 | } |
| 3875 | |
| 3876 | ic->offload_wq = alloc_workqueue("dm-integrity-offload", WQ_MEM_RECLAIM, |
| 3877 | METADATA_WORKQUEUE_MAX_ACTIVE); |
| 3878 | if (!ic->offload_wq) { |
| 3879 | ti->error = "Cannot allocate workqueue"; |
| 3880 | r = -ENOMEM; |
| 3881 | goto bad; |
| 3882 | } |
| 3883 | |
| 3884 | ic->commit_wq = alloc_workqueue("dm-integrity-commit", WQ_MEM_RECLAIM, 1); |
| 3885 | if (!ic->commit_wq) { |
| 3886 | ti->error = "Cannot allocate workqueue"; |
| 3887 | r = -ENOMEM; |
| 3888 | goto bad; |
| 3889 | } |
| 3890 | INIT_WORK(&ic->commit_work, integrity_commit); |
| 3891 | |
| 3892 | if (ic->mode == 'J' || ic->mode == 'B') { |
| 3893 | ic->writer_wq = alloc_workqueue("dm-integrity-writer", WQ_MEM_RECLAIM, 1); |
| 3894 | if (!ic->writer_wq) { |
| 3895 | ti->error = "Cannot allocate workqueue"; |
| 3896 | r = -ENOMEM; |
| 3897 | goto bad; |
| 3898 | } |
| 3899 | INIT_WORK(&ic->writer_work, integrity_writer); |
| 3900 | } |
| 3901 | |
| 3902 | ic->sb = alloc_pages_exact(SB_SECTORS << SECTOR_SHIFT, GFP_KERNEL); |
| 3903 | if (!ic->sb) { |
| 3904 | r = -ENOMEM; |
| 3905 | ti->error = "Cannot allocate superblock area"; |
| 3906 | goto bad; |
| 3907 | } |
| 3908 | |
| 3909 | r = sync_rw_sb(ic, REQ_OP_READ, 0); |
| 3910 | if (r) { |
| 3911 | ti->error = "Error reading superblock"; |
| 3912 | goto bad; |
| 3913 | } |
| 3914 | should_write_sb = false; |
| 3915 | if (memcmp(ic->sb->magic, SB_MAGIC, 8)) { |
| 3916 | if (ic->mode != 'R') { |
| 3917 | if (memchr_inv(ic->sb, 0, SB_SECTORS << SECTOR_SHIFT)) { |
| 3918 | r = -EINVAL; |
| 3919 | ti->error = "The device is not initialized"; |
| 3920 | goto bad; |
| 3921 | } |
| 3922 | } |
| 3923 | |
| 3924 | r = initialize_superblock(ic, journal_sectors, interleave_sectors); |
| 3925 | if (r) { |
| 3926 | ti->error = "Could not initialize superblock"; |
| 3927 | goto bad; |
| 3928 | } |
| 3929 | if (ic->mode != 'R') |
| 3930 | should_write_sb = true; |
| 3931 | } |
| 3932 | |
| 3933 | if (!ic->sb->version || ic->sb->version > SB_VERSION_3) { |
| 3934 | r = -EINVAL; |
| 3935 | ti->error = "Unknown version"; |
| 3936 | goto bad; |
| 3937 | } |
| 3938 | if (le16_to_cpu(ic->sb->integrity_tag_size) != ic->tag_size) { |
| 3939 | r = -EINVAL; |
| 3940 | ti->error = "Tag size doesn't match the information in superblock"; |
| 3941 | goto bad; |
| 3942 | } |
| 3943 | if (ic->sb->log2_sectors_per_block != __ffs(ic->sectors_per_block)) { |
| 3944 | r = -EINVAL; |
| 3945 | ti->error = "Block size doesn't match the information in superblock"; |
| 3946 | goto bad; |
| 3947 | } |
| 3948 | if (!le32_to_cpu(ic->sb->journal_sections)) { |
| 3949 | r = -EINVAL; |
| 3950 | ti->error = "Corrupted superblock, journal_sections is 0"; |
| 3951 | goto bad; |
| 3952 | } |
| 3953 | /* make sure that ti->max_io_len doesn't overflow */ |
| 3954 | if (!ic->meta_dev) { |
| 3955 | if (ic->sb->log2_interleave_sectors < MIN_LOG2_INTERLEAVE_SECTORS || |
| 3956 | ic->sb->log2_interleave_sectors > MAX_LOG2_INTERLEAVE_SECTORS) { |
| 3957 | r = -EINVAL; |
| 3958 | ti->error = "Invalid interleave_sectors in the superblock"; |
| 3959 | goto bad; |
| 3960 | } |
| 3961 | } else { |
| 3962 | if (ic->sb->log2_interleave_sectors) { |
| 3963 | r = -EINVAL; |
| 3964 | ti->error = "Invalid interleave_sectors in the superblock"; |
| 3965 | goto bad; |
| 3966 | } |
| 3967 | } |
| 3968 | ic->provided_data_sectors = le64_to_cpu(ic->sb->provided_data_sectors); |
| 3969 | if (ic->provided_data_sectors != le64_to_cpu(ic->sb->provided_data_sectors)) { |
| 3970 | /* test for overflow */ |
| 3971 | r = -EINVAL; |
| 3972 | ti->error = "The superblock has 64-bit device size, but the kernel was compiled with 32-bit sectors"; |
| 3973 | goto bad; |
| 3974 | } |
| 3975 | if (!!(ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC)) != !!ic->journal_mac_alg.alg_string) { |
| 3976 | r = -EINVAL; |
| 3977 | ti->error = "Journal mac mismatch"; |
| 3978 | goto bad; |
| 3979 | } |
| 3980 | |
| 3981 | try_smaller_buffer: |
| 3982 | r = calculate_device_limits(ic); |
| 3983 | if (r) { |
| 3984 | if (ic->meta_dev) { |
| 3985 | if (ic->log2_buffer_sectors > 3) { |
| 3986 | ic->log2_buffer_sectors--; |
| 3987 | goto try_smaller_buffer; |
| 3988 | } |
| 3989 | } |
| 3990 | ti->error = "The device is too small"; |
| 3991 | goto bad; |
| 3992 | } |
| 3993 | |
| 3994 | if (log2_sectors_per_bitmap_bit < 0) |
| 3995 | log2_sectors_per_bitmap_bit = __fls(DEFAULT_SECTORS_PER_BITMAP_BIT); |
| 3996 | if (log2_sectors_per_bitmap_bit < ic->sb->log2_sectors_per_block) |
| 3997 | log2_sectors_per_bitmap_bit = ic->sb->log2_sectors_per_block; |
| 3998 | |
| 3999 | bits_in_journal = ((__u64)ic->journal_section_sectors * ic->journal_sections) << (SECTOR_SHIFT + 3); |
| 4000 | if (bits_in_journal > UINT_MAX) |
| 4001 | bits_in_journal = UINT_MAX; |
| 4002 | while (bits_in_journal < (ic->provided_data_sectors + ((sector_t)1 << log2_sectors_per_bitmap_bit) - 1) >> log2_sectors_per_bitmap_bit) |
| 4003 | log2_sectors_per_bitmap_bit++; |
| 4004 | |
| 4005 | log2_blocks_per_bitmap_bit = log2_sectors_per_bitmap_bit - ic->sb->log2_sectors_per_block; |
| 4006 | ic->log2_blocks_per_bitmap_bit = log2_blocks_per_bitmap_bit; |
| 4007 | if (should_write_sb) { |
| 4008 | ic->sb->log2_blocks_per_bitmap_bit = log2_blocks_per_bitmap_bit; |
| 4009 | } |
| 4010 | n_bitmap_bits = ((ic->provided_data_sectors >> ic->sb->log2_sectors_per_block) |
| 4011 | + (((sector_t)1 << log2_blocks_per_bitmap_bit) - 1)) >> log2_blocks_per_bitmap_bit; |
| 4012 | ic->n_bitmap_blocks = DIV_ROUND_UP(n_bitmap_bits, BITMAP_BLOCK_SIZE * 8); |
| 4013 | |
| 4014 | if (!ic->meta_dev) |
| 4015 | ic->log2_buffer_sectors = min(ic->log2_buffer_sectors, (__u8)__ffs(ic->metadata_run)); |
| 4016 | |
| 4017 | if (ti->len > ic->provided_data_sectors) { |
| 4018 | r = -EINVAL; |
| 4019 | ti->error = "Not enough provided sectors for requested mapping size"; |
| 4020 | goto bad; |
| 4021 | } |
| 4022 | |
| 4023 | |
| 4024 | threshold = (__u64)ic->journal_entries * (100 - journal_watermark); |
| 4025 | threshold += 50; |
| 4026 | do_div(threshold, 100); |
| 4027 | ic->free_sectors_threshold = threshold; |
| 4028 | |
| 4029 | DEBUG_print("initialized:\n"); |
| 4030 | DEBUG_print(" integrity_tag_size %u\n", le16_to_cpu(ic->sb->integrity_tag_size)); |
| 4031 | DEBUG_print(" journal_entry_size %u\n", ic->journal_entry_size); |
| 4032 | DEBUG_print(" journal_entries_per_sector %u\n", ic->journal_entries_per_sector); |
| 4033 | DEBUG_print(" journal_section_entries %u\n", ic->journal_section_entries); |
| 4034 | DEBUG_print(" journal_section_sectors %u\n", ic->journal_section_sectors); |
| 4035 | DEBUG_print(" journal_sections %u\n", (unsigned)le32_to_cpu(ic->sb->journal_sections)); |
| 4036 | DEBUG_print(" journal_entries %u\n", ic->journal_entries); |
| 4037 | DEBUG_print(" log2_interleave_sectors %d\n", ic->sb->log2_interleave_sectors); |
| 4038 | DEBUG_print(" data_device_sectors 0x%llx\n", i_size_read(ic->dev->bdev->bd_inode) >> SECTOR_SHIFT); |
| 4039 | DEBUG_print(" initial_sectors 0x%x\n", ic->initial_sectors); |
| 4040 | DEBUG_print(" metadata_run 0x%x\n", ic->metadata_run); |
| 4041 | DEBUG_print(" log2_metadata_run %d\n", ic->log2_metadata_run); |
| 4042 | DEBUG_print(" provided_data_sectors 0x%llx (%llu)\n", (unsigned long long)ic->provided_data_sectors, |
| 4043 | (unsigned long long)ic->provided_data_sectors); |
| 4044 | DEBUG_print(" log2_buffer_sectors %u\n", ic->log2_buffer_sectors); |
| 4045 | DEBUG_print(" bits_in_journal %llu\n", (unsigned long long)bits_in_journal); |
| 4046 | |
| 4047 | if (ic->recalculate_flag && !(ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))) { |
| 4048 | ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING); |
| 4049 | ic->sb->recalc_sector = cpu_to_le64(0); |
| 4050 | } |
| 4051 | |
| 4052 | if (ic->internal_hash) { |
| 4053 | size_t recalc_tags_size; |
| 4054 | ic->recalc_wq = alloc_workqueue("dm-integrity-recalc", WQ_MEM_RECLAIM, 1); |
| 4055 | if (!ic->recalc_wq ) { |
| 4056 | ti->error = "Cannot allocate workqueue"; |
| 4057 | r = -ENOMEM; |
| 4058 | goto bad; |
| 4059 | } |
| 4060 | INIT_WORK(&ic->recalc_work, integrity_recalc); |
| 4061 | ic->recalc_buffer = vmalloc(RECALC_SECTORS << SECTOR_SHIFT); |
| 4062 | if (!ic->recalc_buffer) { |
| 4063 | ti->error = "Cannot allocate buffer for recalculating"; |
| 4064 | r = -ENOMEM; |
| 4065 | goto bad; |
| 4066 | } |
| 4067 | recalc_tags_size = (RECALC_SECTORS >> ic->sb->log2_sectors_per_block) * ic->tag_size; |
| 4068 | if (crypto_shash_digestsize(ic->internal_hash) > ic->tag_size) |
| 4069 | recalc_tags_size += crypto_shash_digestsize(ic->internal_hash) - ic->tag_size; |
| 4070 | ic->recalc_tags = kvmalloc(recalc_tags_size, GFP_KERNEL); |
| 4071 | if (!ic->recalc_tags) { |
| 4072 | ti->error = "Cannot allocate tags for recalculating"; |
| 4073 | r = -ENOMEM; |
| 4074 | goto bad; |
| 4075 | } |
| 4076 | } else { |
| 4077 | if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) { |
| 4078 | ti->error = "Recalculate can only be specified with internal_hash"; |
| 4079 | r = -EINVAL; |
| 4080 | goto bad; |
| 4081 | } |
| 4082 | } |
| 4083 | |
| 4084 | if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING) && |
| 4085 | le64_to_cpu(ic->sb->recalc_sector) < ic->provided_data_sectors && |
| 4086 | dm_integrity_disable_recalculate(ic)) { |
| 4087 | ti->error = "Recalculating with HMAC is disabled for security reasons - if you really need it, use the argument \"legacy_recalculate\""; |
| 4088 | r = -EOPNOTSUPP; |
| 4089 | goto bad; |
| 4090 | } |
| 4091 | |
| 4092 | ic->bufio = dm_bufio_client_create(ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev, |
| 4093 | 1U << (SECTOR_SHIFT + ic->log2_buffer_sectors), 1, 0, NULL, NULL); |
| 4094 | if (IS_ERR(ic->bufio)) { |
| 4095 | r = PTR_ERR(ic->bufio); |
| 4096 | ti->error = "Cannot initialize dm-bufio"; |
| 4097 | ic->bufio = NULL; |
| 4098 | goto bad; |
| 4099 | } |
| 4100 | dm_bufio_set_sector_offset(ic->bufio, ic->start + ic->initial_sectors); |
| 4101 | |
| 4102 | if (ic->mode != 'R') { |
| 4103 | r = create_journal(ic, &ti->error); |
| 4104 | if (r) |
| 4105 | goto bad; |
| 4106 | |
| 4107 | } |
| 4108 | |
| 4109 | if (ic->mode == 'B') { |
| 4110 | unsigned i; |
| 4111 | unsigned n_bitmap_pages = DIV_ROUND_UP(ic->n_bitmap_blocks, PAGE_SIZE / BITMAP_BLOCK_SIZE); |
| 4112 | |
| 4113 | ic->recalc_bitmap = dm_integrity_alloc_page_list(n_bitmap_pages); |
| 4114 | if (!ic->recalc_bitmap) { |
| 4115 | r = -ENOMEM; |
| 4116 | goto bad; |
| 4117 | } |
| 4118 | ic->may_write_bitmap = dm_integrity_alloc_page_list(n_bitmap_pages); |
| 4119 | if (!ic->may_write_bitmap) { |
| 4120 | r = -ENOMEM; |
| 4121 | goto bad; |
| 4122 | } |
| 4123 | ic->bbs = kvmalloc_array(ic->n_bitmap_blocks, sizeof(struct bitmap_block_status), GFP_KERNEL); |
| 4124 | if (!ic->bbs) { |
| 4125 | r = -ENOMEM; |
| 4126 | goto bad; |
| 4127 | } |
| 4128 | INIT_DELAYED_WORK(&ic->bitmap_flush_work, bitmap_flush_work); |
| 4129 | for (i = 0; i < ic->n_bitmap_blocks; i++) { |
| 4130 | struct bitmap_block_status *bbs = &ic->bbs[i]; |
| 4131 | unsigned sector, pl_index, pl_offset; |
| 4132 | |
| 4133 | INIT_WORK(&bbs->work, bitmap_block_work); |
| 4134 | bbs->ic = ic; |
| 4135 | bbs->idx = i; |
| 4136 | bio_list_init(&bbs->bio_queue); |
| 4137 | spin_lock_init(&bbs->bio_queue_lock); |
| 4138 | |
| 4139 | sector = i * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT); |
| 4140 | pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT); |
| 4141 | pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1); |
| 4142 | |
| 4143 | bbs->bitmap = lowmem_page_address(ic->journal[pl_index].page) + pl_offset; |
| 4144 | } |
| 4145 | } |
| 4146 | |
| 4147 | if (should_write_sb) { |
| 4148 | init_journal(ic, 0, ic->journal_sections, 0); |
| 4149 | r = dm_integrity_failed(ic); |
| 4150 | if (unlikely(r)) { |
| 4151 | ti->error = "Error initializing journal"; |
| 4152 | goto bad; |
| 4153 | } |
| 4154 | r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA); |
| 4155 | if (r) { |
| 4156 | ti->error = "Error initializing superblock"; |
| 4157 | goto bad; |
| 4158 | } |
| 4159 | ic->just_formatted = true; |
| 4160 | } |
| 4161 | |
| 4162 | if (!ic->meta_dev) { |
| 4163 | r = dm_set_target_max_io_len(ti, 1U << ic->sb->log2_interleave_sectors); |
| 4164 | if (r) |
| 4165 | goto bad; |
| 4166 | } |
| 4167 | if (ic->mode == 'B') { |
| 4168 | unsigned max_io_len = ((sector_t)ic->sectors_per_block << ic->log2_blocks_per_bitmap_bit) * (BITMAP_BLOCK_SIZE * 8); |
| 4169 | if (!max_io_len) |
| 4170 | max_io_len = 1U << 31; |
| 4171 | DEBUG_print("max_io_len: old %u, new %u\n", ti->max_io_len, max_io_len); |
| 4172 | if (!ti->max_io_len || ti->max_io_len > max_io_len) { |
| 4173 | r = dm_set_target_max_io_len(ti, max_io_len); |
| 4174 | if (r) |
| 4175 | goto bad; |
| 4176 | } |
| 4177 | } |
| 4178 | |
| 4179 | if (!ic->internal_hash) |
| 4180 | dm_integrity_set(ti, ic); |
| 4181 | |
| 4182 | ti->num_flush_bios = 1; |
| 4183 | ti->flush_supported = true; |
| 4184 | |
| 4185 | return 0; |
| 4186 | |
| 4187 | bad: |
| 4188 | dm_integrity_dtr(ti); |
| 4189 | return r; |
| 4190 | } |
| 4191 | |
| 4192 | static void dm_integrity_dtr(struct dm_target *ti) |
| 4193 | { |
| 4194 | struct dm_integrity_c *ic = ti->private; |
| 4195 | |
| 4196 | BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress)); |
| 4197 | BUG_ON(!list_empty(&ic->wait_list)); |
| 4198 | |
| 4199 | if (ic->mode == 'B') |
| 4200 | cancel_delayed_work_sync(&ic->bitmap_flush_work); |
| 4201 | if (ic->metadata_wq) |
| 4202 | destroy_workqueue(ic->metadata_wq); |
| 4203 | if (ic->wait_wq) |
| 4204 | destroy_workqueue(ic->wait_wq); |
| 4205 | if (ic->offload_wq) |
| 4206 | destroy_workqueue(ic->offload_wq); |
| 4207 | if (ic->commit_wq) |
| 4208 | destroy_workqueue(ic->commit_wq); |
| 4209 | if (ic->writer_wq) |
| 4210 | destroy_workqueue(ic->writer_wq); |
| 4211 | if (ic->recalc_wq) |
| 4212 | destroy_workqueue(ic->recalc_wq); |
| 4213 | vfree(ic->recalc_buffer); |
| 4214 | kvfree(ic->recalc_tags); |
| 4215 | kvfree(ic->bbs); |
| 4216 | if (ic->bufio) |
| 4217 | dm_bufio_client_destroy(ic->bufio); |
| 4218 | mempool_exit(&ic->journal_io_mempool); |
| 4219 | if (ic->io) |
| 4220 | dm_io_client_destroy(ic->io); |
| 4221 | if (ic->dev) |
| 4222 | dm_put_device(ti, ic->dev); |
| 4223 | if (ic->meta_dev) |
| 4224 | dm_put_device(ti, ic->meta_dev); |
| 4225 | dm_integrity_free_page_list(ic->journal); |
| 4226 | dm_integrity_free_page_list(ic->journal_io); |
| 4227 | dm_integrity_free_page_list(ic->journal_xor); |
| 4228 | dm_integrity_free_page_list(ic->recalc_bitmap); |
| 4229 | dm_integrity_free_page_list(ic->may_write_bitmap); |
| 4230 | if (ic->journal_scatterlist) |
| 4231 | dm_integrity_free_journal_scatterlist(ic, ic->journal_scatterlist); |
| 4232 | if (ic->journal_io_scatterlist) |
| 4233 | dm_integrity_free_journal_scatterlist(ic, ic->journal_io_scatterlist); |
| 4234 | if (ic->sk_requests) { |
| 4235 | unsigned i; |
| 4236 | |
| 4237 | for (i = 0; i < ic->journal_sections; i++) { |
| 4238 | struct skcipher_request *req = ic->sk_requests[i]; |
| 4239 | if (req) { |
| 4240 | kzfree(req->iv); |
| 4241 | skcipher_request_free(req); |
| 4242 | } |
| 4243 | } |
| 4244 | kvfree(ic->sk_requests); |
| 4245 | } |
| 4246 | kvfree(ic->journal_tree); |
| 4247 | if (ic->sb) |
| 4248 | free_pages_exact(ic->sb, SB_SECTORS << SECTOR_SHIFT); |
| 4249 | |
| 4250 | if (ic->internal_hash) |
| 4251 | crypto_free_shash(ic->internal_hash); |
| 4252 | free_alg(&ic->internal_hash_alg); |
| 4253 | |
| 4254 | if (ic->journal_crypt) |
| 4255 | crypto_free_skcipher(ic->journal_crypt); |
| 4256 | free_alg(&ic->journal_crypt_alg); |
| 4257 | |
| 4258 | if (ic->journal_mac) |
| 4259 | crypto_free_shash(ic->journal_mac); |
| 4260 | free_alg(&ic->journal_mac_alg); |
| 4261 | |
| 4262 | kfree(ic); |
| 4263 | } |
| 4264 | |
| 4265 | static struct target_type integrity_target = { |
| 4266 | .name = "integrity", |
| 4267 | .version = {1, 3, 0}, |
| 4268 | .module = THIS_MODULE, |
| 4269 | .features = DM_TARGET_SINGLETON | DM_TARGET_INTEGRITY, |
| 4270 | .ctr = dm_integrity_ctr, |
| 4271 | .dtr = dm_integrity_dtr, |
| 4272 | .map = dm_integrity_map, |
| 4273 | .postsuspend = dm_integrity_postsuspend, |
| 4274 | .resume = dm_integrity_resume, |
| 4275 | .status = dm_integrity_status, |
| 4276 | .iterate_devices = dm_integrity_iterate_devices, |
| 4277 | .io_hints = dm_integrity_io_hints, |
| 4278 | }; |
| 4279 | |
| 4280 | static int __init dm_integrity_init(void) |
| 4281 | { |
| 4282 | int r; |
| 4283 | |
| 4284 | journal_io_cache = kmem_cache_create("integrity_journal_io", |
| 4285 | sizeof(struct journal_io), 0, 0, NULL); |
| 4286 | if (!journal_io_cache) { |
| 4287 | DMERR("can't allocate journal io cache"); |
| 4288 | return -ENOMEM; |
| 4289 | } |
| 4290 | |
| 4291 | r = dm_register_target(&integrity_target); |
| 4292 | if (r < 0) { |
| 4293 | DMERR("register failed %d", r); |
| 4294 | kmem_cache_destroy(journal_io_cache); |
| 4295 | return r; |
| 4296 | } |
| 4297 | |
| 4298 | return 0; |
| 4299 | } |
| 4300 | |
| 4301 | static void __exit dm_integrity_exit(void) |
| 4302 | { |
| 4303 | dm_unregister_target(&integrity_target); |
| 4304 | kmem_cache_destroy(journal_io_cache); |
| 4305 | } |
| 4306 | |
| 4307 | module_init(dm_integrity_init); |
| 4308 | module_exit(dm_integrity_exit); |
| 4309 | |
| 4310 | MODULE_AUTHOR("Milan Broz"); |
| 4311 | MODULE_AUTHOR("Mikulas Patocka"); |
| 4312 | MODULE_DESCRIPTION(DM_NAME " target for integrity tags extension"); |
| 4313 | MODULE_LICENSE("GPL"); |