Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Shaohua Li <shli@fb.com> |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 3 | * Copyright (C) 2016 Song Liu <songliubraving@fb.com> |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | */ |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/wait.h> |
| 17 | #include <linux/blkdev.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/raid/md_p.h> |
Shaohua Li | 5cb2fbd | 2015-10-28 08:41:25 -0700 | [diff] [blame] | 20 | #include <linux/crc32c.h> |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 21 | #include <linux/random.h> |
Shaohua Li | ce1ccd0 | 2016-11-21 10:29:18 -0800 | [diff] [blame] | 22 | #include <linux/kthread.h> |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 23 | #include <linux/types.h> |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 24 | #include "md.h" |
| 25 | #include "raid5.h" |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 26 | #include "bitmap.h" |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * metadata/data stored in disk with 4k size unit (a block) regardless |
| 30 | * underneath hardware sector size. only works with PAGE_SIZE == 4096 |
| 31 | */ |
| 32 | #define BLOCK_SECTORS (8) |
| 33 | |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 34 | /* |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 35 | * log->max_free_space is min(1/4 disk size, 10G reclaimable space). |
| 36 | * |
| 37 | * In write through mode, the reclaim runs every log->max_free_space. |
| 38 | * This can prevent the recovery scans for too long |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 39 | */ |
| 40 | #define RECLAIM_MAX_FREE_SPACE (10 * 1024 * 1024 * 2) /* sector */ |
| 41 | #define RECLAIM_MAX_FREE_SPACE_SHIFT (2) |
| 42 | |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 43 | /* wake up reclaim thread periodically */ |
| 44 | #define R5C_RECLAIM_WAKEUP_INTERVAL (30 * HZ) |
| 45 | /* start flush with these full stripes */ |
| 46 | #define R5C_FULL_STRIPE_FLUSH_BATCH 256 |
| 47 | /* reclaim stripes in groups */ |
| 48 | #define R5C_RECLAIM_STRIPE_GROUP (NR_STRIPE_HASH_LOCKS * 2) |
| 49 | |
Christoph Hellwig | c38d29b | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 50 | /* |
| 51 | * We only need 2 bios per I/O unit to make progress, but ensure we |
| 52 | * have a few more available to not get too tight. |
| 53 | */ |
| 54 | #define R5L_POOL_SIZE 4 |
| 55 | |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 56 | /* |
| 57 | * r5c journal modes of the array: write-back or write-through. |
| 58 | * write-through mode has identical behavior as existing log only |
| 59 | * implementation. |
| 60 | */ |
| 61 | enum r5c_journal_mode { |
| 62 | R5C_JOURNAL_MODE_WRITE_THROUGH = 0, |
| 63 | R5C_JOURNAL_MODE_WRITE_BACK = 1, |
| 64 | }; |
| 65 | |
Song Liu | 2c7da14 | 2016-11-17 15:24:41 -0800 | [diff] [blame] | 66 | static char *r5c_journal_mode_str[] = {"write-through", |
| 67 | "write-back"}; |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 68 | /* |
| 69 | * raid5 cache state machine |
| 70 | * |
JackieLiu | 9b69173 | 2016-11-28 16:19:18 +0800 | [diff] [blame] | 71 | * With the RAID cache, each stripe works in two phases: |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 72 | * - caching phase |
| 73 | * - writing-out phase |
| 74 | * |
| 75 | * These two phases are controlled by bit STRIPE_R5C_CACHING: |
| 76 | * if STRIPE_R5C_CACHING == 0, the stripe is in writing-out phase |
| 77 | * if STRIPE_R5C_CACHING == 1, the stripe is in caching phase |
| 78 | * |
| 79 | * When there is no journal, or the journal is in write-through mode, |
| 80 | * the stripe is always in writing-out phase. |
| 81 | * |
| 82 | * For write-back journal, the stripe is sent to caching phase on write |
| 83 | * (r5c_try_caching_write). r5c_make_stripe_write_out() kicks off |
| 84 | * the write-out phase by clearing STRIPE_R5C_CACHING. |
| 85 | * |
| 86 | * Stripes in caching phase do not write the raid disks. Instead, all |
| 87 | * writes are committed from the log device. Therefore, a stripe in |
| 88 | * caching phase handles writes as: |
| 89 | * - write to log device |
| 90 | * - return IO |
| 91 | * |
| 92 | * Stripes in writing-out phase handle writes as: |
| 93 | * - calculate parity |
| 94 | * - write pending data and parity to journal |
| 95 | * - write data and parity to raid disks |
| 96 | * - return IO for pending writes |
| 97 | */ |
| 98 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 99 | struct r5l_log { |
| 100 | struct md_rdev *rdev; |
| 101 | |
| 102 | u32 uuid_checksum; |
| 103 | |
| 104 | sector_t device_size; /* log device size, round to |
| 105 | * BLOCK_SECTORS */ |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 106 | sector_t max_free_space; /* reclaim run if free space is at |
| 107 | * this size */ |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 108 | |
| 109 | sector_t last_checkpoint; /* log tail. where recovery scan |
| 110 | * starts from */ |
| 111 | u64 last_cp_seq; /* log tail sequence */ |
| 112 | |
| 113 | sector_t log_start; /* log head. where new data appends */ |
| 114 | u64 seq; /* log head sequence */ |
| 115 | |
Christoph Hellwig | 1703646 | 2015-10-05 09:31:06 +0200 | [diff] [blame] | 116 | sector_t next_checkpoint; |
Christoph Hellwig | 1703646 | 2015-10-05 09:31:06 +0200 | [diff] [blame] | 117 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 118 | struct mutex io_mutex; |
| 119 | struct r5l_io_unit *current_io; /* current io_unit accepting new data */ |
| 120 | |
| 121 | spinlock_t io_list_lock; |
| 122 | struct list_head running_ios; /* io_units which are still running, |
| 123 | * and have not yet been completely |
| 124 | * written to the log */ |
| 125 | struct list_head io_end_ios; /* io_units which have been completely |
| 126 | * written to the log but not yet written |
| 127 | * to the RAID */ |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 128 | struct list_head flushing_ios; /* io_units which are waiting for log |
| 129 | * cache flush */ |
Christoph Hellwig | 04732f7 | 2015-10-05 09:31:07 +0200 | [diff] [blame] | 130 | struct list_head finished_ios; /* io_units which settle down in log disk */ |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 131 | struct bio flush_bio; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 132 | |
Christoph Hellwig | 5036c390 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 133 | struct list_head no_mem_stripes; /* pending stripes, -ENOMEM */ |
| 134 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 135 | struct kmem_cache *io_kc; |
Christoph Hellwig | 5036c390 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 136 | mempool_t *io_pool; |
Christoph Hellwig | c38d29b | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 137 | struct bio_set *bs; |
Christoph Hellwig | e8deb63 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 138 | mempool_t *meta_pool; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 139 | |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 140 | struct md_thread *reclaim_thread; |
| 141 | unsigned long reclaim_target; /* number of space that need to be |
| 142 | * reclaimed. if it's 0, reclaim spaces |
| 143 | * used by io_units which are in |
| 144 | * IO_UNIT_STRIPE_END state (eg, reclaim |
| 145 | * dones't wait for specific io_unit |
| 146 | * switching to IO_UNIT_STRIPE_END |
| 147 | * state) */ |
Shaohua Li | 0fd22b4 | 2015-09-02 13:49:47 -0700 | [diff] [blame] | 148 | wait_queue_head_t iounit_wait; |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 149 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 150 | struct list_head no_space_stripes; /* pending stripes, log has no space */ |
| 151 | spinlock_t no_space_stripes_lock; |
Christoph Hellwig | 56fef7c | 2015-10-05 09:31:09 +0200 | [diff] [blame] | 152 | |
| 153 | bool need_cache_flush; |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 154 | |
| 155 | /* for r5c_cache */ |
| 156 | enum r5c_journal_mode r5c_journal_mode; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 157 | |
| 158 | /* all stripes in r5cache, in the order of seq at sh->log_start */ |
| 159 | struct list_head stripe_in_journal_list; |
| 160 | |
| 161 | spinlock_t stripe_in_journal_lock; |
| 162 | atomic_t stripe_in_journal_count; |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 163 | |
| 164 | /* to submit async io_units, to fulfill ordering of flush */ |
| 165 | struct work_struct deferred_io_work; |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 166 | /* to disable write back during in degraded mode */ |
| 167 | struct work_struct disable_writeback_work; |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 168 | |
| 169 | /* to for chunk_aligned_read in writeback mode, details below */ |
| 170 | spinlock_t tree_lock; |
| 171 | struct radix_tree_root big_stripe_tree; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | /* |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 175 | * Enable chunk_aligned_read() with write back cache. |
| 176 | * |
| 177 | * Each chunk may contain more than one stripe (for example, a 256kB |
| 178 | * chunk contains 64 4kB-page, so this chunk contain 64 stripes). For |
| 179 | * chunk_aligned_read, these stripes are grouped into one "big_stripe". |
| 180 | * For each big_stripe, we count how many stripes of this big_stripe |
| 181 | * are in the write back cache. These data are tracked in a radix tree |
| 182 | * (big_stripe_tree). We use radix_tree item pointer as the counter. |
| 183 | * r5c_tree_index() is used to calculate keys for the radix tree. |
| 184 | * |
| 185 | * chunk_aligned_read() calls r5c_big_stripe_cached() to look up |
| 186 | * big_stripe of each chunk in the tree. If this big_stripe is in the |
| 187 | * tree, chunk_aligned_read() aborts. This look up is protected by |
| 188 | * rcu_read_lock(). |
| 189 | * |
| 190 | * It is necessary to remember whether a stripe is counted in |
| 191 | * big_stripe_tree. Instead of adding new flag, we reuses existing flags: |
| 192 | * STRIPE_R5C_PARTIAL_STRIPE and STRIPE_R5C_FULL_STRIPE. If either of these |
| 193 | * two flags are set, the stripe is counted in big_stripe_tree. This |
| 194 | * requires moving set_bit(STRIPE_R5C_PARTIAL_STRIPE) to |
| 195 | * r5c_try_caching_write(); and moving clear_bit of |
| 196 | * STRIPE_R5C_PARTIAL_STRIPE and STRIPE_R5C_FULL_STRIPE to |
| 197 | * r5c_finish_stripe_write_out(). |
| 198 | */ |
| 199 | |
| 200 | /* |
| 201 | * radix tree requests lowest 2 bits of data pointer to be 2b'00. |
| 202 | * So it is necessary to left shift the counter by 2 bits before using it |
| 203 | * as data pointer of the tree. |
| 204 | */ |
| 205 | #define R5C_RADIX_COUNT_SHIFT 2 |
| 206 | |
| 207 | /* |
| 208 | * calculate key for big_stripe_tree |
| 209 | * |
| 210 | * sect: align_bi->bi_iter.bi_sector or sh->sector |
| 211 | */ |
| 212 | static inline sector_t r5c_tree_index(struct r5conf *conf, |
| 213 | sector_t sect) |
| 214 | { |
| 215 | sector_t offset; |
| 216 | |
| 217 | offset = sector_div(sect, conf->chunk_sectors); |
| 218 | return sect; |
| 219 | } |
| 220 | |
| 221 | /* |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 222 | * an IO range starts from a meta data block and end at the next meta data |
| 223 | * block. The io unit's the meta data block tracks data/parity followed it. io |
| 224 | * unit is written to log disk with normal write, as we always flush log disk |
| 225 | * first and then start move data to raid disks, there is no requirement to |
| 226 | * write io unit with FLUSH/FUA |
| 227 | */ |
| 228 | struct r5l_io_unit { |
| 229 | struct r5l_log *log; |
| 230 | |
| 231 | struct page *meta_page; /* store meta block */ |
| 232 | int meta_offset; /* current offset in meta_page */ |
| 233 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 234 | struct bio *current_bio;/* current_bio accepting new data */ |
| 235 | |
| 236 | atomic_t pending_stripe;/* how many stripes not flushed to raid */ |
| 237 | u64 seq; /* seq number of the metablock */ |
| 238 | sector_t log_start; /* where the io_unit starts */ |
| 239 | sector_t log_end; /* where the io_unit ends */ |
| 240 | struct list_head log_sibling; /* log->running_ios */ |
| 241 | struct list_head stripe_list; /* stripes added to the io_unit */ |
| 242 | |
| 243 | int state; |
Christoph Hellwig | 6143e2c | 2015-10-05 09:31:16 +0200 | [diff] [blame] | 244 | bool need_split_bio; |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 245 | struct bio *split_bio; |
| 246 | |
| 247 | unsigned int has_flush:1; /* include flush request */ |
| 248 | unsigned int has_fua:1; /* include fua request */ |
| 249 | unsigned int has_null_flush:1; /* include empty flush request */ |
| 250 | /* |
| 251 | * io isn't sent yet, flush/fua request can only be submitted till it's |
| 252 | * the first IO in running_ios list |
| 253 | */ |
| 254 | unsigned int io_deferred:1; |
| 255 | |
| 256 | struct bio_list flush_barriers; /* size == 0 flush bios */ |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 257 | }; |
| 258 | |
| 259 | /* r5l_io_unit state */ |
| 260 | enum r5l_io_unit_state { |
| 261 | IO_UNIT_RUNNING = 0, /* accepting new IO */ |
| 262 | IO_UNIT_IO_START = 1, /* io_unit bio start writing to log, |
| 263 | * don't accepting new bio */ |
| 264 | IO_UNIT_IO_END = 2, /* io_unit bio finish writing to log */ |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 265 | IO_UNIT_STRIPE_END = 3, /* stripes data finished writing to raid */ |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 266 | }; |
| 267 | |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 268 | bool r5c_is_writeback(struct r5l_log *log) |
| 269 | { |
| 270 | return (log != NULL && |
| 271 | log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK); |
| 272 | } |
| 273 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 274 | static sector_t r5l_ring_add(struct r5l_log *log, sector_t start, sector_t inc) |
| 275 | { |
| 276 | start += inc; |
| 277 | if (start >= log->device_size) |
| 278 | start = start - log->device_size; |
| 279 | return start; |
| 280 | } |
| 281 | |
| 282 | static sector_t r5l_ring_distance(struct r5l_log *log, sector_t start, |
| 283 | sector_t end) |
| 284 | { |
| 285 | if (end >= start) |
| 286 | return end - start; |
| 287 | else |
| 288 | return end + log->device_size - start; |
| 289 | } |
| 290 | |
| 291 | static bool r5l_has_free_space(struct r5l_log *log, sector_t size) |
| 292 | { |
| 293 | sector_t used_size; |
| 294 | |
| 295 | used_size = r5l_ring_distance(log, log->last_checkpoint, |
| 296 | log->log_start); |
| 297 | |
| 298 | return log->device_size > used_size + size; |
| 299 | } |
| 300 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 301 | static void __r5l_set_io_unit_state(struct r5l_io_unit *io, |
| 302 | enum r5l_io_unit_state state) |
| 303 | { |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 304 | if (WARN_ON(io->state >= state)) |
| 305 | return; |
| 306 | io->state = state; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 309 | static void |
| 310 | r5c_return_dev_pending_writes(struct r5conf *conf, struct r5dev *dev, |
| 311 | struct bio_list *return_bi) |
| 312 | { |
| 313 | struct bio *wbi, *wbi2; |
| 314 | |
| 315 | wbi = dev->written; |
| 316 | dev->written = NULL; |
| 317 | while (wbi && wbi->bi_iter.bi_sector < |
| 318 | dev->sector + STRIPE_SECTORS) { |
| 319 | wbi2 = r5_next_bio(wbi, dev->sector); |
| 320 | if (!raid5_dec_bi_active_stripes(wbi)) { |
| 321 | md_write_end(conf->mddev); |
| 322 | bio_list_add(return_bi, wbi); |
| 323 | } |
| 324 | wbi = wbi2; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | void r5c_handle_cached_data_endio(struct r5conf *conf, |
| 329 | struct stripe_head *sh, int disks, struct bio_list *return_bi) |
| 330 | { |
| 331 | int i; |
| 332 | |
| 333 | for (i = sh->disks; i--; ) { |
| 334 | if (sh->dev[i].written) { |
| 335 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
| 336 | r5c_return_dev_pending_writes(conf, &sh->dev[i], |
| 337 | return_bi); |
| 338 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 339 | STRIPE_SECTORS, |
| 340 | !test_bit(STRIPE_DEGRADED, &sh->state), |
| 341 | 0); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 346 | /* Check whether we should flush some stripes to free up stripe cache */ |
| 347 | void r5c_check_stripe_cache_usage(struct r5conf *conf) |
| 348 | { |
| 349 | int total_cached; |
| 350 | |
| 351 | if (!r5c_is_writeback(conf->log)) |
| 352 | return; |
| 353 | |
| 354 | total_cached = atomic_read(&conf->r5c_cached_partial_stripes) + |
| 355 | atomic_read(&conf->r5c_cached_full_stripes); |
| 356 | |
| 357 | /* |
| 358 | * The following condition is true for either of the following: |
| 359 | * - stripe cache pressure high: |
| 360 | * total_cached > 3/4 min_nr_stripes || |
| 361 | * empty_inactive_list_nr > 0 |
| 362 | * - stripe cache pressure moderate: |
| 363 | * total_cached > 1/2 min_nr_stripes |
| 364 | */ |
| 365 | if (total_cached > conf->min_nr_stripes * 1 / 2 || |
| 366 | atomic_read(&conf->empty_inactive_list_nr) > 0) |
| 367 | r5l_wake_reclaim(conf->log, 0); |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * flush cache when there are R5C_FULL_STRIPE_FLUSH_BATCH or more full |
| 372 | * stripes in the cache |
| 373 | */ |
| 374 | void r5c_check_cached_full_stripe(struct r5conf *conf) |
| 375 | { |
| 376 | if (!r5c_is_writeback(conf->log)) |
| 377 | return; |
| 378 | |
| 379 | /* |
| 380 | * wake up reclaim for R5C_FULL_STRIPE_FLUSH_BATCH cached stripes |
| 381 | * or a full stripe (chunk size / 4k stripes). |
| 382 | */ |
| 383 | if (atomic_read(&conf->r5c_cached_full_stripes) >= |
| 384 | min(R5C_FULL_STRIPE_FLUSH_BATCH, |
| 385 | conf->chunk_sectors >> STRIPE_SHIFT)) |
| 386 | r5l_wake_reclaim(conf->log, 0); |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Total log space (in sectors) needed to flush all data in cache |
| 391 | * |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame^] | 392 | * To avoid deadlock due to log space, it is necessary to reserve log |
| 393 | * space to flush critical stripes (stripes that occupying log space near |
| 394 | * last_checkpoint). This function helps check how much log space is |
| 395 | * required to flush all cached stripes. |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 396 | * |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame^] | 397 | * To reduce log space requirements, two mechanisms are used to give cache |
| 398 | * flush higher priorities: |
| 399 | * 1. In handle_stripe_dirtying() and schedule_reconstruction(), |
| 400 | * stripes ALREADY in journal can be flushed w/o pending writes; |
| 401 | * 2. In r5l_write_stripe() and r5c_cache_data(), stripes NOT in journal |
| 402 | * can be delayed (r5l_add_no_space_stripe). |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 403 | * |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame^] | 404 | * In cache flush, the stripe goes through 1 and then 2. For a stripe that |
| 405 | * already passed 1, flushing it requires at most (conf->max_degraded + 1) |
| 406 | * pages of journal space. For stripes that has not passed 1, flushing it |
| 407 | * requires (conf->raid_disks + 1) pages of journal space. There are at |
| 408 | * most (conf->group_cnt + 1) stripe that passed 1. So total journal space |
| 409 | * required to flush all cached stripes (in pages) is: |
| 410 | * |
| 411 | * (stripe_in_journal_count - group_cnt - 1) * (max_degraded + 1) + |
| 412 | * (group_cnt + 1) * (raid_disks + 1) |
| 413 | * or |
| 414 | * (stripe_in_journal_count) * (max_degraded + 1) + |
| 415 | * (group_cnt + 1) * (raid_disks - max_degraded) |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 416 | */ |
| 417 | static sector_t r5c_log_required_to_flush_cache(struct r5conf *conf) |
| 418 | { |
| 419 | struct r5l_log *log = conf->log; |
| 420 | |
| 421 | if (!r5c_is_writeback(log)) |
| 422 | return 0; |
| 423 | |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame^] | 424 | return BLOCK_SECTORS * |
| 425 | ((conf->max_degraded + 1) * atomic_read(&log->stripe_in_journal_count) + |
| 426 | (conf->raid_disks - conf->max_degraded) * (conf->group_cnt + 1)); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /* |
| 430 | * evaluate log space usage and update R5C_LOG_TIGHT and R5C_LOG_CRITICAL |
| 431 | * |
| 432 | * R5C_LOG_TIGHT is set when free space on the log device is less than 3x of |
| 433 | * reclaim_required_space. R5C_LOG_CRITICAL is set when free space on the log |
| 434 | * device is less than 2x of reclaim_required_space. |
| 435 | */ |
| 436 | static inline void r5c_update_log_state(struct r5l_log *log) |
| 437 | { |
| 438 | struct r5conf *conf = log->rdev->mddev->private; |
| 439 | sector_t free_space; |
| 440 | sector_t reclaim_space; |
Song Liu | f687a33 | 2016-11-30 16:57:54 -0800 | [diff] [blame] | 441 | bool wake_reclaim = false; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 442 | |
| 443 | if (!r5c_is_writeback(log)) |
| 444 | return; |
| 445 | |
| 446 | free_space = r5l_ring_distance(log, log->log_start, |
| 447 | log->last_checkpoint); |
| 448 | reclaim_space = r5c_log_required_to_flush_cache(conf); |
| 449 | if (free_space < 2 * reclaim_space) |
| 450 | set_bit(R5C_LOG_CRITICAL, &conf->cache_state); |
Song Liu | f687a33 | 2016-11-30 16:57:54 -0800 | [diff] [blame] | 451 | else { |
| 452 | if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state)) |
| 453 | wake_reclaim = true; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 454 | clear_bit(R5C_LOG_CRITICAL, &conf->cache_state); |
Song Liu | f687a33 | 2016-11-30 16:57:54 -0800 | [diff] [blame] | 455 | } |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 456 | if (free_space < 3 * reclaim_space) |
| 457 | set_bit(R5C_LOG_TIGHT, &conf->cache_state); |
| 458 | else |
| 459 | clear_bit(R5C_LOG_TIGHT, &conf->cache_state); |
Song Liu | f687a33 | 2016-11-30 16:57:54 -0800 | [diff] [blame] | 460 | |
| 461 | if (wake_reclaim) |
| 462 | r5l_wake_reclaim(log, 0); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 463 | } |
| 464 | |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 465 | /* |
| 466 | * Put the stripe into writing-out phase by clearing STRIPE_R5C_CACHING. |
| 467 | * This function should only be called in write-back mode. |
| 468 | */ |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 469 | void r5c_make_stripe_write_out(struct stripe_head *sh) |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 470 | { |
| 471 | struct r5conf *conf = sh->raid_conf; |
| 472 | struct r5l_log *log = conf->log; |
| 473 | |
| 474 | BUG_ON(!r5c_is_writeback(log)); |
| 475 | |
| 476 | WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state)); |
| 477 | clear_bit(STRIPE_R5C_CACHING, &sh->state); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 478 | |
| 479 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 480 | atomic_inc(&conf->preread_active_stripes); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static void r5c_handle_data_cached(struct stripe_head *sh) |
| 484 | { |
| 485 | int i; |
| 486 | |
| 487 | for (i = sh->disks; i--; ) |
| 488 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { |
| 489 | set_bit(R5_InJournal, &sh->dev[i].flags); |
| 490 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 491 | } |
| 492 | clear_bit(STRIPE_LOG_TRAPPED, &sh->state); |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * this journal write must contain full parity, |
| 497 | * it may also contain some data pages |
| 498 | */ |
| 499 | static void r5c_handle_parity_cached(struct stripe_head *sh) |
| 500 | { |
| 501 | int i; |
| 502 | |
| 503 | for (i = sh->disks; i--; ) |
| 504 | if (test_bit(R5_InJournal, &sh->dev[i].flags)) |
| 505 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /* |
| 509 | * Setting proper flags after writing (or flushing) data and/or parity to the |
| 510 | * log device. This is called from r5l_log_endio() or r5l_log_flush_endio(). |
| 511 | */ |
| 512 | static void r5c_finish_cache_stripe(struct stripe_head *sh) |
| 513 | { |
| 514 | struct r5l_log *log = sh->raid_conf->log; |
| 515 | |
| 516 | if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) { |
| 517 | BUG_ON(test_bit(STRIPE_R5C_CACHING, &sh->state)); |
| 518 | /* |
| 519 | * Set R5_InJournal for parity dev[pd_idx]. This means |
| 520 | * all data AND parity in the journal. For RAID 6, it is |
| 521 | * NOT necessary to set the flag for dev[qd_idx], as the |
| 522 | * two parities are written out together. |
| 523 | */ |
| 524 | set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 525 | } else if (test_bit(STRIPE_R5C_CACHING, &sh->state)) { |
| 526 | r5c_handle_data_cached(sh); |
| 527 | } else { |
| 528 | r5c_handle_parity_cached(sh); |
| 529 | set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags); |
| 530 | } |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Christoph Hellwig | d8858f4 | 2015-10-05 09:31:08 +0200 | [diff] [blame] | 533 | static void r5l_io_run_stripes(struct r5l_io_unit *io) |
| 534 | { |
| 535 | struct stripe_head *sh, *next; |
| 536 | |
| 537 | list_for_each_entry_safe(sh, next, &io->stripe_list, log_list) { |
| 538 | list_del_init(&sh->log_list); |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 539 | |
| 540 | r5c_finish_cache_stripe(sh); |
| 541 | |
Christoph Hellwig | d8858f4 | 2015-10-05 09:31:08 +0200 | [diff] [blame] | 542 | set_bit(STRIPE_HANDLE, &sh->state); |
| 543 | raid5_release_stripe(sh); |
| 544 | } |
| 545 | } |
| 546 | |
Christoph Hellwig | 56fef7c | 2015-10-05 09:31:09 +0200 | [diff] [blame] | 547 | static void r5l_log_run_stripes(struct r5l_log *log) |
| 548 | { |
| 549 | struct r5l_io_unit *io, *next; |
| 550 | |
| 551 | assert_spin_locked(&log->io_list_lock); |
| 552 | |
| 553 | list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) { |
| 554 | /* don't change list order */ |
| 555 | if (io->state < IO_UNIT_IO_END) |
| 556 | break; |
| 557 | |
| 558 | list_move_tail(&io->log_sibling, &log->finished_ios); |
| 559 | r5l_io_run_stripes(io); |
| 560 | } |
| 561 | } |
| 562 | |
Christoph Hellwig | 3848c0b | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 563 | static void r5l_move_to_end_ios(struct r5l_log *log) |
| 564 | { |
| 565 | struct r5l_io_unit *io, *next; |
| 566 | |
| 567 | assert_spin_locked(&log->io_list_lock); |
| 568 | |
| 569 | list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) { |
| 570 | /* don't change list order */ |
| 571 | if (io->state < IO_UNIT_IO_END) |
| 572 | break; |
| 573 | list_move_tail(&io->log_sibling, &log->io_end_ios); |
| 574 | } |
| 575 | } |
| 576 | |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 577 | static void __r5l_stripe_write_finished(struct r5l_io_unit *io); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 578 | static void r5l_log_endio(struct bio *bio) |
| 579 | { |
| 580 | struct r5l_io_unit *io = bio->bi_private; |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 581 | struct r5l_io_unit *io_deferred; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 582 | struct r5l_log *log = io->log; |
Christoph Hellwig | 509ffec | 2015-09-02 13:49:48 -0700 | [diff] [blame] | 583 | unsigned long flags; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 584 | |
Shaohua Li | 6e74a9c | 2015-10-08 21:54:08 -0700 | [diff] [blame] | 585 | if (bio->bi_error) |
| 586 | md_error(log->rdev->mddev, log->rdev); |
| 587 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 588 | bio_put(bio); |
Christoph Hellwig | e8deb63 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 589 | mempool_free(io->meta_page, log->meta_pool); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 590 | |
Christoph Hellwig | 509ffec | 2015-09-02 13:49:48 -0700 | [diff] [blame] | 591 | spin_lock_irqsave(&log->io_list_lock, flags); |
| 592 | __r5l_set_io_unit_state(io, IO_UNIT_IO_END); |
Christoph Hellwig | 56fef7c | 2015-10-05 09:31:09 +0200 | [diff] [blame] | 593 | if (log->need_cache_flush) |
Christoph Hellwig | 3848c0b | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 594 | r5l_move_to_end_ios(log); |
Christoph Hellwig | 56fef7c | 2015-10-05 09:31:09 +0200 | [diff] [blame] | 595 | else |
| 596 | r5l_log_run_stripes(log); |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 597 | if (!list_empty(&log->running_ios)) { |
| 598 | /* |
| 599 | * FLUSH/FUA io_unit is deferred because of ordering, now we |
| 600 | * can dispatch it |
| 601 | */ |
| 602 | io_deferred = list_first_entry(&log->running_ios, |
| 603 | struct r5l_io_unit, log_sibling); |
| 604 | if (io_deferred->io_deferred) |
| 605 | schedule_work(&log->deferred_io_work); |
| 606 | } |
| 607 | |
Christoph Hellwig | 509ffec | 2015-09-02 13:49:48 -0700 | [diff] [blame] | 608 | spin_unlock_irqrestore(&log->io_list_lock, flags); |
| 609 | |
Christoph Hellwig | 56fef7c | 2015-10-05 09:31:09 +0200 | [diff] [blame] | 610 | if (log->need_cache_flush) |
| 611 | md_wakeup_thread(log->rdev->mddev->thread); |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 612 | |
| 613 | if (io->has_null_flush) { |
| 614 | struct bio *bi; |
| 615 | |
| 616 | WARN_ON(bio_list_empty(&io->flush_barriers)); |
| 617 | while ((bi = bio_list_pop(&io->flush_barriers)) != NULL) { |
| 618 | bio_endio(bi); |
| 619 | atomic_dec(&io->pending_stripe); |
| 620 | } |
| 621 | if (atomic_read(&io->pending_stripe) == 0) |
| 622 | __r5l_stripe_write_finished(io); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | static void r5l_do_submit_io(struct r5l_log *log, struct r5l_io_unit *io) |
| 627 | { |
| 628 | unsigned long flags; |
| 629 | |
| 630 | spin_lock_irqsave(&log->io_list_lock, flags); |
| 631 | __r5l_set_io_unit_state(io, IO_UNIT_IO_START); |
| 632 | spin_unlock_irqrestore(&log->io_list_lock, flags); |
| 633 | |
| 634 | if (io->has_flush) |
Shaohua Li | 2073773 | 2016-12-13 12:40:15 -0800 | [diff] [blame] | 635 | io->current_bio->bi_opf |= REQ_PREFLUSH; |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 636 | if (io->has_fua) |
Shaohua Li | 2073773 | 2016-12-13 12:40:15 -0800 | [diff] [blame] | 637 | io->current_bio->bi_opf |= REQ_FUA; |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 638 | submit_bio(io->current_bio); |
| 639 | |
| 640 | if (!io->split_bio) |
| 641 | return; |
| 642 | |
| 643 | if (io->has_flush) |
Shaohua Li | 2073773 | 2016-12-13 12:40:15 -0800 | [diff] [blame] | 644 | io->split_bio->bi_opf |= REQ_PREFLUSH; |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 645 | if (io->has_fua) |
Shaohua Li | 2073773 | 2016-12-13 12:40:15 -0800 | [diff] [blame] | 646 | io->split_bio->bi_opf |= REQ_FUA; |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 647 | submit_bio(io->split_bio); |
| 648 | } |
| 649 | |
| 650 | /* deferred io_unit will be dispatched here */ |
| 651 | static void r5l_submit_io_async(struct work_struct *work) |
| 652 | { |
| 653 | struct r5l_log *log = container_of(work, struct r5l_log, |
| 654 | deferred_io_work); |
| 655 | struct r5l_io_unit *io = NULL; |
| 656 | unsigned long flags; |
| 657 | |
| 658 | spin_lock_irqsave(&log->io_list_lock, flags); |
| 659 | if (!list_empty(&log->running_ios)) { |
| 660 | io = list_first_entry(&log->running_ios, struct r5l_io_unit, |
| 661 | log_sibling); |
| 662 | if (!io->io_deferred) |
| 663 | io = NULL; |
| 664 | else |
| 665 | io->io_deferred = 0; |
| 666 | } |
| 667 | spin_unlock_irqrestore(&log->io_list_lock, flags); |
| 668 | if (io) |
| 669 | r5l_do_submit_io(log, io); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 672 | static void r5c_disable_writeback_async(struct work_struct *work) |
| 673 | { |
| 674 | struct r5l_log *log = container_of(work, struct r5l_log, |
| 675 | disable_writeback_work); |
| 676 | struct mddev *mddev = log->rdev->mddev; |
| 677 | |
| 678 | if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) |
| 679 | return; |
| 680 | pr_info("md/raid:%s: Disabling writeback cache for degraded array.\n", |
| 681 | mdname(mddev)); |
| 682 | mddev_suspend(mddev); |
| 683 | log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH; |
| 684 | mddev_resume(mddev); |
| 685 | } |
| 686 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 687 | static void r5l_submit_current_io(struct r5l_log *log) |
| 688 | { |
| 689 | struct r5l_io_unit *io = log->current_io; |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 690 | struct bio *bio; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 691 | struct r5l_meta_block *block; |
Christoph Hellwig | 509ffec | 2015-09-02 13:49:48 -0700 | [diff] [blame] | 692 | unsigned long flags; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 693 | u32 crc; |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 694 | bool do_submit = true; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 695 | |
| 696 | if (!io) |
| 697 | return; |
| 698 | |
| 699 | block = page_address(io->meta_page); |
| 700 | block->meta_size = cpu_to_le32(io->meta_offset); |
Shaohua Li | 5cb2fbd | 2015-10-28 08:41:25 -0700 | [diff] [blame] | 701 | crc = crc32c_le(log->uuid_checksum, block, PAGE_SIZE); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 702 | block->checksum = cpu_to_le32(crc); |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 703 | bio = io->current_bio; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 704 | |
| 705 | log->current_io = NULL; |
Christoph Hellwig | 509ffec | 2015-09-02 13:49:48 -0700 | [diff] [blame] | 706 | spin_lock_irqsave(&log->io_list_lock, flags); |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 707 | if (io->has_flush || io->has_fua) { |
| 708 | if (io != list_first_entry(&log->running_ios, |
| 709 | struct r5l_io_unit, log_sibling)) { |
| 710 | io->io_deferred = 1; |
| 711 | do_submit = false; |
| 712 | } |
| 713 | } |
Christoph Hellwig | 509ffec | 2015-09-02 13:49:48 -0700 | [diff] [blame] | 714 | spin_unlock_irqrestore(&log->io_list_lock, flags); |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 715 | if (do_submit) |
| 716 | r5l_do_submit_io(log, io); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Christoph Hellwig | 6143e2c | 2015-10-05 09:31:16 +0200 | [diff] [blame] | 719 | static struct bio *r5l_bio_alloc(struct r5l_log *log) |
Christoph Hellwig | b349feb | 2015-10-05 09:31:11 +0200 | [diff] [blame] | 720 | { |
Christoph Hellwig | c38d29b | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 721 | struct bio *bio = bio_alloc_bioset(GFP_NOIO, BIO_MAX_PAGES, log->bs); |
Christoph Hellwig | b349feb | 2015-10-05 09:31:11 +0200 | [diff] [blame] | 722 | |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 723 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
Christoph Hellwig | b349feb | 2015-10-05 09:31:11 +0200 | [diff] [blame] | 724 | bio->bi_bdev = log->rdev->bdev; |
Christoph Hellwig | 1e932a3 | 2015-10-05 09:31:12 +0200 | [diff] [blame] | 725 | bio->bi_iter.bi_sector = log->rdev->data_offset + log->log_start; |
Christoph Hellwig | b349feb | 2015-10-05 09:31:11 +0200 | [diff] [blame] | 726 | |
Christoph Hellwig | b349feb | 2015-10-05 09:31:11 +0200 | [diff] [blame] | 727 | return bio; |
| 728 | } |
| 729 | |
Christoph Hellwig | c1b9919 | 2015-10-05 09:31:14 +0200 | [diff] [blame] | 730 | static void r5_reserve_log_entry(struct r5l_log *log, struct r5l_io_unit *io) |
| 731 | { |
| 732 | log->log_start = r5l_ring_add(log, log->log_start, BLOCK_SECTORS); |
| 733 | |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 734 | r5c_update_log_state(log); |
Christoph Hellwig | c1b9919 | 2015-10-05 09:31:14 +0200 | [diff] [blame] | 735 | /* |
| 736 | * If we filled up the log device start from the beginning again, |
| 737 | * which will require a new bio. |
| 738 | * |
| 739 | * Note: for this to work properly the log size needs to me a multiple |
| 740 | * of BLOCK_SECTORS. |
| 741 | */ |
| 742 | if (log->log_start == 0) |
Christoph Hellwig | 6143e2c | 2015-10-05 09:31:16 +0200 | [diff] [blame] | 743 | io->need_split_bio = true; |
Christoph Hellwig | c1b9919 | 2015-10-05 09:31:14 +0200 | [diff] [blame] | 744 | |
| 745 | io->log_end = log->log_start; |
| 746 | } |
| 747 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 748 | static struct r5l_io_unit *r5l_new_meta(struct r5l_log *log) |
| 749 | { |
| 750 | struct r5l_io_unit *io; |
| 751 | struct r5l_meta_block *block; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 752 | |
Christoph Hellwig | 5036c390 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 753 | io = mempool_alloc(log->io_pool, GFP_ATOMIC); |
| 754 | if (!io) |
| 755 | return NULL; |
| 756 | memset(io, 0, sizeof(*io)); |
| 757 | |
Christoph Hellwig | 51039cd | 2015-10-05 09:31:13 +0200 | [diff] [blame] | 758 | io->log = log; |
Christoph Hellwig | 51039cd | 2015-10-05 09:31:13 +0200 | [diff] [blame] | 759 | INIT_LIST_HEAD(&io->log_sibling); |
| 760 | INIT_LIST_HEAD(&io->stripe_list); |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 761 | bio_list_init(&io->flush_barriers); |
Christoph Hellwig | 51039cd | 2015-10-05 09:31:13 +0200 | [diff] [blame] | 762 | io->state = IO_UNIT_RUNNING; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 763 | |
Christoph Hellwig | e8deb63 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 764 | io->meta_page = mempool_alloc(log->meta_pool, GFP_NOIO); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 765 | block = page_address(io->meta_page); |
Christoph Hellwig | e8deb63 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 766 | clear_page(block); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 767 | block->magic = cpu_to_le32(R5LOG_MAGIC); |
| 768 | block->version = R5LOG_VERSION; |
| 769 | block->seq = cpu_to_le64(log->seq); |
| 770 | block->position = cpu_to_le64(log->log_start); |
| 771 | |
| 772 | io->log_start = log->log_start; |
| 773 | io->meta_offset = sizeof(struct r5l_meta_block); |
Christoph Hellwig | 2b8ef16 | 2015-10-05 09:31:15 +0200 | [diff] [blame] | 774 | io->seq = log->seq++; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 775 | |
Christoph Hellwig | 6143e2c | 2015-10-05 09:31:16 +0200 | [diff] [blame] | 776 | io->current_bio = r5l_bio_alloc(log); |
| 777 | io->current_bio->bi_end_io = r5l_log_endio; |
| 778 | io->current_bio->bi_private = io; |
Christoph Hellwig | b349feb | 2015-10-05 09:31:11 +0200 | [diff] [blame] | 779 | bio_add_page(io->current_bio, io->meta_page, PAGE_SIZE, 0); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 780 | |
Christoph Hellwig | c1b9919 | 2015-10-05 09:31:14 +0200 | [diff] [blame] | 781 | r5_reserve_log_entry(log, io); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 782 | |
| 783 | spin_lock_irq(&log->io_list_lock); |
| 784 | list_add_tail(&io->log_sibling, &log->running_ios); |
| 785 | spin_unlock_irq(&log->io_list_lock); |
| 786 | |
| 787 | return io; |
| 788 | } |
| 789 | |
| 790 | static int r5l_get_meta(struct r5l_log *log, unsigned int payload_size) |
| 791 | { |
Christoph Hellwig | 22581f5 | 2015-10-05 09:31:10 +0200 | [diff] [blame] | 792 | if (log->current_io && |
| 793 | log->current_io->meta_offset + payload_size > PAGE_SIZE) |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 794 | r5l_submit_current_io(log); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 795 | |
Christoph Hellwig | 5036c390 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 796 | if (!log->current_io) { |
Christoph Hellwig | 22581f5 | 2015-10-05 09:31:10 +0200 | [diff] [blame] | 797 | log->current_io = r5l_new_meta(log); |
Christoph Hellwig | 5036c390 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 798 | if (!log->current_io) |
| 799 | return -ENOMEM; |
| 800 | } |
| 801 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | static void r5l_append_payload_meta(struct r5l_log *log, u16 type, |
| 806 | sector_t location, |
| 807 | u32 checksum1, u32 checksum2, |
| 808 | bool checksum2_valid) |
| 809 | { |
| 810 | struct r5l_io_unit *io = log->current_io; |
| 811 | struct r5l_payload_data_parity *payload; |
| 812 | |
| 813 | payload = page_address(io->meta_page) + io->meta_offset; |
| 814 | payload->header.type = cpu_to_le16(type); |
| 815 | payload->header.flags = cpu_to_le16(0); |
| 816 | payload->size = cpu_to_le32((1 + !!checksum2_valid) << |
| 817 | (PAGE_SHIFT - 9)); |
| 818 | payload->location = cpu_to_le64(location); |
| 819 | payload->checksum[0] = cpu_to_le32(checksum1); |
| 820 | if (checksum2_valid) |
| 821 | payload->checksum[1] = cpu_to_le32(checksum2); |
| 822 | |
| 823 | io->meta_offset += sizeof(struct r5l_payload_data_parity) + |
| 824 | sizeof(__le32) * (1 + !!checksum2_valid); |
| 825 | } |
| 826 | |
| 827 | static void r5l_append_payload_page(struct r5l_log *log, struct page *page) |
| 828 | { |
| 829 | struct r5l_io_unit *io = log->current_io; |
| 830 | |
Christoph Hellwig | 6143e2c | 2015-10-05 09:31:16 +0200 | [diff] [blame] | 831 | if (io->need_split_bio) { |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 832 | BUG_ON(io->split_bio); |
| 833 | io->split_bio = io->current_bio; |
Christoph Hellwig | 6143e2c | 2015-10-05 09:31:16 +0200 | [diff] [blame] | 834 | io->current_bio = r5l_bio_alloc(log); |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 835 | bio_chain(io->current_bio, io->split_bio); |
| 836 | io->need_split_bio = false; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 837 | } |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 838 | |
Christoph Hellwig | 6143e2c | 2015-10-05 09:31:16 +0200 | [diff] [blame] | 839 | if (!bio_add_page(io->current_bio, page, PAGE_SIZE, 0)) |
| 840 | BUG(); |
| 841 | |
Christoph Hellwig | c1b9919 | 2015-10-05 09:31:14 +0200 | [diff] [blame] | 842 | r5_reserve_log_entry(log, io); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 843 | } |
| 844 | |
Christoph Hellwig | 5036c390 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 845 | static int r5l_log_stripe(struct r5l_log *log, struct stripe_head *sh, |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 846 | int data_pages, int parity_pages) |
| 847 | { |
| 848 | int i; |
| 849 | int meta_size; |
Christoph Hellwig | 5036c390 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 850 | int ret; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 851 | struct r5l_io_unit *io; |
| 852 | |
| 853 | meta_size = |
| 854 | ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32)) |
| 855 | * data_pages) + |
| 856 | sizeof(struct r5l_payload_data_parity) + |
| 857 | sizeof(__le32) * parity_pages; |
| 858 | |
Christoph Hellwig | 5036c390 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 859 | ret = r5l_get_meta(log, meta_size); |
| 860 | if (ret) |
| 861 | return ret; |
| 862 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 863 | io = log->current_io; |
| 864 | |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 865 | if (test_and_clear_bit(STRIPE_R5C_PREFLUSH, &sh->state)) |
| 866 | io->has_flush = 1; |
| 867 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 868 | for (i = 0; i < sh->disks; i++) { |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 869 | if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) || |
| 870 | test_bit(R5_InJournal, &sh->dev[i].flags)) |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 871 | continue; |
| 872 | if (i == sh->pd_idx || i == sh->qd_idx) |
| 873 | continue; |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 874 | if (test_bit(R5_WantFUA, &sh->dev[i].flags) && |
| 875 | log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK) { |
| 876 | io->has_fua = 1; |
| 877 | /* |
| 878 | * we need to flush journal to make sure recovery can |
| 879 | * reach the data with fua flag |
| 880 | */ |
| 881 | io->has_flush = 1; |
| 882 | } |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 883 | r5l_append_payload_meta(log, R5LOG_PAYLOAD_DATA, |
| 884 | raid5_compute_blocknr(sh, i, 0), |
| 885 | sh->dev[i].log_checksum, 0, false); |
| 886 | r5l_append_payload_page(log, sh->dev[i].page); |
| 887 | } |
| 888 | |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 889 | if (parity_pages == 2) { |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 890 | r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY, |
| 891 | sh->sector, sh->dev[sh->pd_idx].log_checksum, |
| 892 | sh->dev[sh->qd_idx].log_checksum, true); |
| 893 | r5l_append_payload_page(log, sh->dev[sh->pd_idx].page); |
| 894 | r5l_append_payload_page(log, sh->dev[sh->qd_idx].page); |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 895 | } else if (parity_pages == 1) { |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 896 | r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY, |
| 897 | sh->sector, sh->dev[sh->pd_idx].log_checksum, |
| 898 | 0, false); |
| 899 | r5l_append_payload_page(log, sh->dev[sh->pd_idx].page); |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 900 | } else /* Just writing data, not parity, in caching phase */ |
| 901 | BUG_ON(parity_pages != 0); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 902 | |
| 903 | list_add_tail(&sh->log_list, &io->stripe_list); |
| 904 | atomic_inc(&io->pending_stripe); |
| 905 | sh->log_io = io; |
Christoph Hellwig | 5036c390 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 906 | |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 907 | if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) |
| 908 | return 0; |
| 909 | |
| 910 | if (sh->log_start == MaxSector) { |
| 911 | BUG_ON(!list_empty(&sh->r5c)); |
| 912 | sh->log_start = io->log_start; |
| 913 | spin_lock_irq(&log->stripe_in_journal_lock); |
| 914 | list_add_tail(&sh->r5c, |
| 915 | &log->stripe_in_journal_list); |
| 916 | spin_unlock_irq(&log->stripe_in_journal_lock); |
| 917 | atomic_inc(&log->stripe_in_journal_count); |
| 918 | } |
Christoph Hellwig | 5036c390 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 919 | return 0; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 920 | } |
| 921 | |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 922 | /* add stripe to no_space_stripes, and then wake up reclaim */ |
| 923 | static inline void r5l_add_no_space_stripe(struct r5l_log *log, |
| 924 | struct stripe_head *sh) |
| 925 | { |
| 926 | spin_lock(&log->no_space_stripes_lock); |
| 927 | list_add_tail(&sh->log_list, &log->no_space_stripes); |
| 928 | spin_unlock(&log->no_space_stripes_lock); |
| 929 | } |
| 930 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 931 | /* |
| 932 | * running in raid5d, where reclaim could wait for raid5d too (when it flushes |
| 933 | * data from log to raid disks), so we shouldn't wait for reclaim here |
| 934 | */ |
| 935 | int r5l_write_stripe(struct r5l_log *log, struct stripe_head *sh) |
| 936 | { |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 937 | struct r5conf *conf = sh->raid_conf; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 938 | int write_disks = 0; |
| 939 | int data_pages, parity_pages; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 940 | int reserve; |
| 941 | int i; |
Christoph Hellwig | 5036c390 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 942 | int ret = 0; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 943 | bool wake_reclaim = false; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 944 | |
| 945 | if (!log) |
| 946 | return -EAGAIN; |
| 947 | /* Don't support stripe batch */ |
| 948 | if (sh->log_io || !test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags) || |
| 949 | test_bit(STRIPE_SYNCING, &sh->state)) { |
| 950 | /* the stripe is written to log, we start writing it to raid */ |
| 951 | clear_bit(STRIPE_LOG_TRAPPED, &sh->state); |
| 952 | return -EAGAIN; |
| 953 | } |
| 954 | |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 955 | WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state)); |
| 956 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 957 | for (i = 0; i < sh->disks; i++) { |
| 958 | void *addr; |
| 959 | |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 960 | if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) || |
| 961 | test_bit(R5_InJournal, &sh->dev[i].flags)) |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 962 | continue; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 963 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 964 | write_disks++; |
| 965 | /* checksum is already calculated in last run */ |
| 966 | if (test_bit(STRIPE_LOG_TRAPPED, &sh->state)) |
| 967 | continue; |
| 968 | addr = kmap_atomic(sh->dev[i].page); |
Shaohua Li | 5cb2fbd | 2015-10-28 08:41:25 -0700 | [diff] [blame] | 969 | sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum, |
| 970 | addr, PAGE_SIZE); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 971 | kunmap_atomic(addr); |
| 972 | } |
| 973 | parity_pages = 1 + !!(sh->qd_idx >= 0); |
| 974 | data_pages = write_disks - parity_pages; |
| 975 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 976 | set_bit(STRIPE_LOG_TRAPPED, &sh->state); |
Shaohua Li | 253f9fd4 | 2015-09-04 14:14:16 -0700 | [diff] [blame] | 977 | /* |
| 978 | * The stripe must enter state machine again to finish the write, so |
| 979 | * don't delay. |
| 980 | */ |
| 981 | clear_bit(STRIPE_DELAYED, &sh->state); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 982 | atomic_inc(&sh->count); |
| 983 | |
| 984 | mutex_lock(&log->io_mutex); |
| 985 | /* meta + data */ |
| 986 | reserve = (1 + write_disks) << (PAGE_SHIFT - 9); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 987 | |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 988 | if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) { |
| 989 | if (!r5l_has_free_space(log, reserve)) { |
| 990 | r5l_add_no_space_stripe(log, sh); |
| 991 | wake_reclaim = true; |
| 992 | } else { |
| 993 | ret = r5l_log_stripe(log, sh, data_pages, parity_pages); |
| 994 | if (ret) { |
| 995 | spin_lock_irq(&log->io_list_lock); |
| 996 | list_add_tail(&sh->log_list, |
| 997 | &log->no_mem_stripes); |
| 998 | spin_unlock_irq(&log->io_list_lock); |
| 999 | } |
| 1000 | } |
| 1001 | } else { /* R5C_JOURNAL_MODE_WRITE_BACK */ |
| 1002 | /* |
| 1003 | * log space critical, do not process stripes that are |
| 1004 | * not in cache yet (sh->log_start == MaxSector). |
| 1005 | */ |
| 1006 | if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) && |
| 1007 | sh->log_start == MaxSector) { |
| 1008 | r5l_add_no_space_stripe(log, sh); |
| 1009 | wake_reclaim = true; |
| 1010 | reserve = 0; |
| 1011 | } else if (!r5l_has_free_space(log, reserve)) { |
| 1012 | if (sh->log_start == log->last_checkpoint) |
| 1013 | BUG(); |
| 1014 | else |
| 1015 | r5l_add_no_space_stripe(log, sh); |
| 1016 | } else { |
| 1017 | ret = r5l_log_stripe(log, sh, data_pages, parity_pages); |
| 1018 | if (ret) { |
| 1019 | spin_lock_irq(&log->io_list_lock); |
| 1020 | list_add_tail(&sh->log_list, |
| 1021 | &log->no_mem_stripes); |
| 1022 | spin_unlock_irq(&log->io_list_lock); |
| 1023 | } |
Christoph Hellwig | 5036c390 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 1024 | } |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 1025 | } |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 1026 | |
Christoph Hellwig | 5036c390 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 1027 | mutex_unlock(&log->io_mutex); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 1028 | if (wake_reclaim) |
| 1029 | r5l_wake_reclaim(log, reserve); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | void r5l_write_stripe_run(struct r5l_log *log) |
| 1034 | { |
| 1035 | if (!log) |
| 1036 | return; |
| 1037 | mutex_lock(&log->io_mutex); |
| 1038 | r5l_submit_current_io(log); |
| 1039 | mutex_unlock(&log->io_mutex); |
| 1040 | } |
| 1041 | |
Shaohua Li | 828cbe9 | 2015-09-02 13:49:49 -0700 | [diff] [blame] | 1042 | int r5l_handle_flush_request(struct r5l_log *log, struct bio *bio) |
| 1043 | { |
| 1044 | if (!log) |
| 1045 | return -ENODEV; |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 1046 | |
| 1047 | if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) { |
| 1048 | /* |
| 1049 | * in write through (journal only) |
| 1050 | * we flush log disk cache first, then write stripe data to |
| 1051 | * raid disks. So if bio is finished, the log disk cache is |
| 1052 | * flushed already. The recovery guarantees we can recovery |
| 1053 | * the bio from log disk, so we don't need to flush again |
| 1054 | */ |
| 1055 | if (bio->bi_iter.bi_size == 0) { |
| 1056 | bio_endio(bio); |
| 1057 | return 0; |
| 1058 | } |
| 1059 | bio->bi_opf &= ~REQ_PREFLUSH; |
| 1060 | } else { |
| 1061 | /* write back (with cache) */ |
| 1062 | if (bio->bi_iter.bi_size == 0) { |
| 1063 | mutex_lock(&log->io_mutex); |
| 1064 | r5l_get_meta(log, 0); |
| 1065 | bio_list_add(&log->current_io->flush_barriers, bio); |
| 1066 | log->current_io->has_flush = 1; |
| 1067 | log->current_io->has_null_flush = 1; |
| 1068 | atomic_inc(&log->current_io->pending_stripe); |
| 1069 | r5l_submit_current_io(log); |
| 1070 | mutex_unlock(&log->io_mutex); |
| 1071 | return 0; |
| 1072 | } |
Shaohua Li | 828cbe9 | 2015-09-02 13:49:49 -0700 | [diff] [blame] | 1073 | } |
Shaohua Li | 828cbe9 | 2015-09-02 13:49:49 -0700 | [diff] [blame] | 1074 | return -EAGAIN; |
| 1075 | } |
| 1076 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 1077 | /* This will run after log space is reclaimed */ |
| 1078 | static void r5l_run_no_space_stripes(struct r5l_log *log) |
| 1079 | { |
| 1080 | struct stripe_head *sh; |
| 1081 | |
| 1082 | spin_lock(&log->no_space_stripes_lock); |
| 1083 | while (!list_empty(&log->no_space_stripes)) { |
| 1084 | sh = list_first_entry(&log->no_space_stripes, |
| 1085 | struct stripe_head, log_list); |
| 1086 | list_del_init(&sh->log_list); |
| 1087 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1088 | raid5_release_stripe(sh); |
| 1089 | } |
| 1090 | spin_unlock(&log->no_space_stripes_lock); |
| 1091 | } |
| 1092 | |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 1093 | /* |
| 1094 | * calculate new last_checkpoint |
| 1095 | * for write through mode, returns log->next_checkpoint |
| 1096 | * for write back, returns log_start of first sh in stripe_in_journal_list |
| 1097 | */ |
| 1098 | static sector_t r5c_calculate_new_cp(struct r5conf *conf) |
| 1099 | { |
| 1100 | struct stripe_head *sh; |
| 1101 | struct r5l_log *log = conf->log; |
| 1102 | sector_t new_cp; |
| 1103 | unsigned long flags; |
| 1104 | |
| 1105 | if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) |
| 1106 | return log->next_checkpoint; |
| 1107 | |
| 1108 | spin_lock_irqsave(&log->stripe_in_journal_lock, flags); |
| 1109 | if (list_empty(&conf->log->stripe_in_journal_list)) { |
| 1110 | /* all stripes flushed */ |
Dan Carpenter | d3014e2 | 2016-11-24 14:13:04 +0300 | [diff] [blame] | 1111 | spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 1112 | return log->next_checkpoint; |
| 1113 | } |
| 1114 | sh = list_first_entry(&conf->log->stripe_in_journal_list, |
| 1115 | struct stripe_head, r5c); |
| 1116 | new_cp = sh->log_start; |
| 1117 | spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags); |
| 1118 | return new_cp; |
| 1119 | } |
| 1120 | |
Christoph Hellwig | 1703646 | 2015-10-05 09:31:06 +0200 | [diff] [blame] | 1121 | static sector_t r5l_reclaimable_space(struct r5l_log *log) |
| 1122 | { |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 1123 | struct r5conf *conf = log->rdev->mddev->private; |
| 1124 | |
Christoph Hellwig | 1703646 | 2015-10-05 09:31:06 +0200 | [diff] [blame] | 1125 | return r5l_ring_distance(log, log->last_checkpoint, |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 1126 | r5c_calculate_new_cp(conf)); |
Christoph Hellwig | 1703646 | 2015-10-05 09:31:06 +0200 | [diff] [blame] | 1127 | } |
| 1128 | |
Christoph Hellwig | 5036c390 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 1129 | static void r5l_run_no_mem_stripe(struct r5l_log *log) |
| 1130 | { |
| 1131 | struct stripe_head *sh; |
| 1132 | |
| 1133 | assert_spin_locked(&log->io_list_lock); |
| 1134 | |
| 1135 | if (!list_empty(&log->no_mem_stripes)) { |
| 1136 | sh = list_first_entry(&log->no_mem_stripes, |
| 1137 | struct stripe_head, log_list); |
| 1138 | list_del_init(&sh->log_list); |
| 1139 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1140 | raid5_release_stripe(sh); |
| 1141 | } |
| 1142 | } |
| 1143 | |
Christoph Hellwig | 04732f7 | 2015-10-05 09:31:07 +0200 | [diff] [blame] | 1144 | static bool r5l_complete_finished_ios(struct r5l_log *log) |
Christoph Hellwig | 1703646 | 2015-10-05 09:31:06 +0200 | [diff] [blame] | 1145 | { |
| 1146 | struct r5l_io_unit *io, *next; |
| 1147 | bool found = false; |
| 1148 | |
| 1149 | assert_spin_locked(&log->io_list_lock); |
| 1150 | |
Christoph Hellwig | 04732f7 | 2015-10-05 09:31:07 +0200 | [diff] [blame] | 1151 | list_for_each_entry_safe(io, next, &log->finished_ios, log_sibling) { |
Christoph Hellwig | 1703646 | 2015-10-05 09:31:06 +0200 | [diff] [blame] | 1152 | /* don't change list order */ |
| 1153 | if (io->state < IO_UNIT_STRIPE_END) |
| 1154 | break; |
| 1155 | |
| 1156 | log->next_checkpoint = io->log_start; |
Christoph Hellwig | 1703646 | 2015-10-05 09:31:06 +0200 | [diff] [blame] | 1157 | |
| 1158 | list_del(&io->log_sibling); |
Christoph Hellwig | 5036c390 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 1159 | mempool_free(io, log->io_pool); |
| 1160 | r5l_run_no_mem_stripe(log); |
Christoph Hellwig | 1703646 | 2015-10-05 09:31:06 +0200 | [diff] [blame] | 1161 | |
| 1162 | found = true; |
| 1163 | } |
| 1164 | |
| 1165 | return found; |
| 1166 | } |
| 1167 | |
Christoph Hellwig | 509ffec | 2015-09-02 13:49:48 -0700 | [diff] [blame] | 1168 | static void __r5l_stripe_write_finished(struct r5l_io_unit *io) |
| 1169 | { |
| 1170 | struct r5l_log *log = io->log; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 1171 | struct r5conf *conf = log->rdev->mddev->private; |
Christoph Hellwig | 509ffec | 2015-09-02 13:49:48 -0700 | [diff] [blame] | 1172 | unsigned long flags; |
| 1173 | |
| 1174 | spin_lock_irqsave(&log->io_list_lock, flags); |
| 1175 | __r5l_set_io_unit_state(io, IO_UNIT_STRIPE_END); |
Christoph Hellwig | 1703646 | 2015-10-05 09:31:06 +0200 | [diff] [blame] | 1176 | |
Christoph Hellwig | 04732f7 | 2015-10-05 09:31:07 +0200 | [diff] [blame] | 1177 | if (!r5l_complete_finished_ios(log)) { |
Shaohua Li | 85f2f9a | 2015-09-04 14:14:05 -0700 | [diff] [blame] | 1178 | spin_unlock_irqrestore(&log->io_list_lock, flags); |
| 1179 | return; |
| 1180 | } |
Christoph Hellwig | 509ffec | 2015-09-02 13:49:48 -0700 | [diff] [blame] | 1181 | |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 1182 | if (r5l_reclaimable_space(log) > log->max_free_space || |
| 1183 | test_bit(R5C_LOG_TIGHT, &conf->cache_state)) |
Christoph Hellwig | 509ffec | 2015-09-02 13:49:48 -0700 | [diff] [blame] | 1184 | r5l_wake_reclaim(log, 0); |
| 1185 | |
Christoph Hellwig | 509ffec | 2015-09-02 13:49:48 -0700 | [diff] [blame] | 1186 | spin_unlock_irqrestore(&log->io_list_lock, flags); |
| 1187 | wake_up(&log->iounit_wait); |
| 1188 | } |
| 1189 | |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1190 | void r5l_stripe_write_finished(struct stripe_head *sh) |
| 1191 | { |
| 1192 | struct r5l_io_unit *io; |
| 1193 | |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1194 | io = sh->log_io; |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1195 | sh->log_io = NULL; |
| 1196 | |
Christoph Hellwig | 509ffec | 2015-09-02 13:49:48 -0700 | [diff] [blame] | 1197 | if (io && atomic_dec_and_test(&io->pending_stripe)) |
| 1198 | __r5l_stripe_write_finished(io); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 1201 | static void r5l_log_flush_endio(struct bio *bio) |
| 1202 | { |
| 1203 | struct r5l_log *log = container_of(bio, struct r5l_log, |
| 1204 | flush_bio); |
| 1205 | unsigned long flags; |
| 1206 | struct r5l_io_unit *io; |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 1207 | |
Shaohua Li | 6e74a9c | 2015-10-08 21:54:08 -0700 | [diff] [blame] | 1208 | if (bio->bi_error) |
| 1209 | md_error(log->rdev->mddev, log->rdev); |
| 1210 | |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 1211 | spin_lock_irqsave(&log->io_list_lock, flags); |
Christoph Hellwig | d8858f4 | 2015-10-05 09:31:08 +0200 | [diff] [blame] | 1212 | list_for_each_entry(io, &log->flushing_ios, log_sibling) |
| 1213 | r5l_io_run_stripes(io); |
Christoph Hellwig | 04732f7 | 2015-10-05 09:31:07 +0200 | [diff] [blame] | 1214 | list_splice_tail_init(&log->flushing_ios, &log->finished_ios); |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 1215 | spin_unlock_irqrestore(&log->io_list_lock, flags); |
| 1216 | } |
| 1217 | |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1218 | /* |
| 1219 | * Starting dispatch IO to raid. |
| 1220 | * io_unit(meta) consists of a log. There is one situation we want to avoid. A |
| 1221 | * broken meta in the middle of a log causes recovery can't find meta at the |
| 1222 | * head of log. If operations require meta at the head persistent in log, we |
| 1223 | * must make sure meta before it persistent in log too. A case is: |
| 1224 | * |
| 1225 | * stripe data/parity is in log, we start write stripe to raid disks. stripe |
| 1226 | * data/parity must be persistent in log before we do the write to raid disks. |
| 1227 | * |
| 1228 | * The solution is we restrictly maintain io_unit list order. In this case, we |
| 1229 | * only write stripes of an io_unit to raid disks till the io_unit is the first |
| 1230 | * one whose data/parity is in log. |
| 1231 | */ |
| 1232 | void r5l_flush_stripe_to_raid(struct r5l_log *log) |
| 1233 | { |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 1234 | bool do_flush; |
Christoph Hellwig | 56fef7c | 2015-10-05 09:31:09 +0200 | [diff] [blame] | 1235 | |
| 1236 | if (!log || !log->need_cache_flush) |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1237 | return; |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1238 | |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 1239 | spin_lock_irq(&log->io_list_lock); |
| 1240 | /* flush bio is running */ |
| 1241 | if (!list_empty(&log->flushing_ios)) { |
| 1242 | spin_unlock_irq(&log->io_list_lock); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1243 | return; |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1244 | } |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 1245 | list_splice_tail_init(&log->io_end_ios, &log->flushing_ios); |
| 1246 | do_flush = !list_empty(&log->flushing_ios); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1247 | spin_unlock_irq(&log->io_list_lock); |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 1248 | |
| 1249 | if (!do_flush) |
| 1250 | return; |
| 1251 | bio_reset(&log->flush_bio); |
| 1252 | log->flush_bio.bi_bdev = log->rdev->bdev; |
| 1253 | log->flush_bio.bi_end_io = r5l_log_flush_endio; |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 1254 | log->flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH; |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 1255 | submit_bio(&log->flush_bio); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1258 | static void r5l_write_super(struct r5l_log *log, sector_t cp); |
Shaohua Li | 4b48204 | 2015-10-08 21:54:06 -0700 | [diff] [blame] | 1259 | static void r5l_write_super_and_discard_space(struct r5l_log *log, |
| 1260 | sector_t end) |
| 1261 | { |
| 1262 | struct block_device *bdev = log->rdev->bdev; |
| 1263 | struct mddev *mddev; |
| 1264 | |
| 1265 | r5l_write_super(log, end); |
| 1266 | |
| 1267 | if (!blk_queue_discard(bdev_get_queue(bdev))) |
| 1268 | return; |
| 1269 | |
| 1270 | mddev = log->rdev->mddev; |
| 1271 | /* |
Shaohua Li | 8e018c2 | 2016-08-25 10:09:39 -0700 | [diff] [blame] | 1272 | * Discard could zero data, so before discard we must make sure |
| 1273 | * superblock is updated to new log tail. Updating superblock (either |
| 1274 | * directly call md_update_sb() or depend on md thread) must hold |
| 1275 | * reconfig mutex. On the other hand, raid5_quiesce is called with |
| 1276 | * reconfig_mutex hold. The first step of raid5_quiesce() is waitting |
| 1277 | * for all IO finish, hence waitting for reclaim thread, while reclaim |
| 1278 | * thread is calling this function and waitting for reconfig mutex. So |
| 1279 | * there is a deadlock. We workaround this issue with a trylock. |
| 1280 | * FIXME: we could miss discard if we can't take reconfig mutex |
Shaohua Li | 4b48204 | 2015-10-08 21:54:06 -0700 | [diff] [blame] | 1281 | */ |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 1282 | set_mask_bits(&mddev->sb_flags, 0, |
| 1283 | BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING)); |
Shaohua Li | 8e018c2 | 2016-08-25 10:09:39 -0700 | [diff] [blame] | 1284 | if (!mddev_trylock(mddev)) |
| 1285 | return; |
| 1286 | md_update_sb(mddev, 1); |
| 1287 | mddev_unlock(mddev); |
Shaohua Li | 4b48204 | 2015-10-08 21:54:06 -0700 | [diff] [blame] | 1288 | |
Shaohua Li | 6e74a9c | 2015-10-08 21:54:08 -0700 | [diff] [blame] | 1289 | /* discard IO error really doesn't matter, ignore it */ |
Shaohua Li | 4b48204 | 2015-10-08 21:54:06 -0700 | [diff] [blame] | 1290 | if (log->last_checkpoint < end) { |
| 1291 | blkdev_issue_discard(bdev, |
| 1292 | log->last_checkpoint + log->rdev->data_offset, |
| 1293 | end - log->last_checkpoint, GFP_NOIO, 0); |
| 1294 | } else { |
| 1295 | blkdev_issue_discard(bdev, |
| 1296 | log->last_checkpoint + log->rdev->data_offset, |
| 1297 | log->device_size - log->last_checkpoint, |
| 1298 | GFP_NOIO, 0); |
| 1299 | blkdev_issue_discard(bdev, log->rdev->data_offset, end, |
| 1300 | GFP_NOIO, 0); |
| 1301 | } |
| 1302 | } |
| 1303 | |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 1304 | /* |
| 1305 | * r5c_flush_stripe moves stripe from cached list to handle_list. When called, |
| 1306 | * the stripe must be on r5c_cached_full_stripes or r5c_cached_partial_stripes. |
| 1307 | * |
| 1308 | * must hold conf->device_lock |
| 1309 | */ |
| 1310 | static void r5c_flush_stripe(struct r5conf *conf, struct stripe_head *sh) |
| 1311 | { |
| 1312 | BUG_ON(list_empty(&sh->lru)); |
| 1313 | BUG_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state)); |
| 1314 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); |
| 1315 | |
| 1316 | /* |
| 1317 | * The stripe is not ON_RELEASE_LIST, so it is safe to call |
| 1318 | * raid5_release_stripe() while holding conf->device_lock |
| 1319 | */ |
| 1320 | BUG_ON(test_bit(STRIPE_ON_RELEASE_LIST, &sh->state)); |
| 1321 | assert_spin_locked(&conf->device_lock); |
| 1322 | |
| 1323 | list_del_init(&sh->lru); |
| 1324 | atomic_inc(&sh->count); |
| 1325 | |
| 1326 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1327 | atomic_inc(&conf->active_stripes); |
| 1328 | r5c_make_stripe_write_out(sh); |
| 1329 | |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 1330 | raid5_release_stripe(sh); |
| 1331 | } |
| 1332 | |
| 1333 | /* |
| 1334 | * if num == 0, flush all full stripes |
| 1335 | * if num > 0, flush all full stripes. If less than num full stripes are |
| 1336 | * flushed, flush some partial stripes until totally num stripes are |
| 1337 | * flushed or there is no more cached stripes. |
| 1338 | */ |
| 1339 | void r5c_flush_cache(struct r5conf *conf, int num) |
| 1340 | { |
| 1341 | int count; |
| 1342 | struct stripe_head *sh, *next; |
| 1343 | |
| 1344 | assert_spin_locked(&conf->device_lock); |
| 1345 | if (!conf->log) |
| 1346 | return; |
| 1347 | |
| 1348 | count = 0; |
| 1349 | list_for_each_entry_safe(sh, next, &conf->r5c_full_stripe_list, lru) { |
| 1350 | r5c_flush_stripe(conf, sh); |
| 1351 | count++; |
| 1352 | } |
| 1353 | |
| 1354 | if (count >= num) |
| 1355 | return; |
| 1356 | list_for_each_entry_safe(sh, next, |
| 1357 | &conf->r5c_partial_stripe_list, lru) { |
| 1358 | r5c_flush_stripe(conf, sh); |
| 1359 | if (++count >= num) |
| 1360 | break; |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | static void r5c_do_reclaim(struct r5conf *conf) |
| 1365 | { |
| 1366 | struct r5l_log *log = conf->log; |
| 1367 | struct stripe_head *sh; |
| 1368 | int count = 0; |
| 1369 | unsigned long flags; |
| 1370 | int total_cached; |
| 1371 | int stripes_to_flush; |
| 1372 | |
| 1373 | if (!r5c_is_writeback(log)) |
| 1374 | return; |
| 1375 | |
| 1376 | total_cached = atomic_read(&conf->r5c_cached_partial_stripes) + |
| 1377 | atomic_read(&conf->r5c_cached_full_stripes); |
| 1378 | |
| 1379 | if (total_cached > conf->min_nr_stripes * 3 / 4 || |
| 1380 | atomic_read(&conf->empty_inactive_list_nr) > 0) |
| 1381 | /* |
| 1382 | * if stripe cache pressure high, flush all full stripes and |
| 1383 | * some partial stripes |
| 1384 | */ |
| 1385 | stripes_to_flush = R5C_RECLAIM_STRIPE_GROUP; |
| 1386 | else if (total_cached > conf->min_nr_stripes * 1 / 2 || |
| 1387 | atomic_read(&conf->r5c_cached_full_stripes) > |
| 1388 | R5C_FULL_STRIPE_FLUSH_BATCH) |
| 1389 | /* |
| 1390 | * if stripe cache pressure moderate, or if there is many full |
| 1391 | * stripes,flush all full stripes |
| 1392 | */ |
| 1393 | stripes_to_flush = 0; |
| 1394 | else |
| 1395 | /* no need to flush */ |
| 1396 | stripes_to_flush = -1; |
| 1397 | |
| 1398 | if (stripes_to_flush >= 0) { |
| 1399 | spin_lock_irqsave(&conf->device_lock, flags); |
| 1400 | r5c_flush_cache(conf, stripes_to_flush); |
| 1401 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 1402 | } |
| 1403 | |
| 1404 | /* if log space is tight, flush stripes on stripe_in_journal_list */ |
| 1405 | if (test_bit(R5C_LOG_TIGHT, &conf->cache_state)) { |
| 1406 | spin_lock_irqsave(&log->stripe_in_journal_lock, flags); |
| 1407 | spin_lock(&conf->device_lock); |
| 1408 | list_for_each_entry(sh, &log->stripe_in_journal_list, r5c) { |
| 1409 | /* |
| 1410 | * stripes on stripe_in_journal_list could be in any |
| 1411 | * state of the stripe_cache state machine. In this |
| 1412 | * case, we only want to flush stripe on |
| 1413 | * r5c_cached_full/partial_stripes. The following |
| 1414 | * condition makes sure the stripe is on one of the |
| 1415 | * two lists. |
| 1416 | */ |
| 1417 | if (!list_empty(&sh->lru) && |
| 1418 | !test_bit(STRIPE_HANDLE, &sh->state) && |
| 1419 | atomic_read(&sh->count) == 0) { |
| 1420 | r5c_flush_stripe(conf, sh); |
| 1421 | } |
| 1422 | if (count++ >= R5C_RECLAIM_STRIPE_GROUP) |
| 1423 | break; |
| 1424 | } |
| 1425 | spin_unlock(&conf->device_lock); |
| 1426 | spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags); |
| 1427 | } |
Song Liu | f687a33 | 2016-11-30 16:57:54 -0800 | [diff] [blame] | 1428 | |
| 1429 | if (!test_bit(R5C_LOG_CRITICAL, &conf->cache_state)) |
| 1430 | r5l_run_no_space_stripes(log); |
| 1431 | |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 1432 | md_wakeup_thread(conf->mddev->thread); |
| 1433 | } |
Shaohua Li | 4b48204 | 2015-10-08 21:54:06 -0700 | [diff] [blame] | 1434 | |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1435 | static void r5l_do_reclaim(struct r5l_log *log) |
| 1436 | { |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 1437 | struct r5conf *conf = log->rdev->mddev->private; |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1438 | sector_t reclaim_target = xchg(&log->reclaim_target, 0); |
Christoph Hellwig | 1703646 | 2015-10-05 09:31:06 +0200 | [diff] [blame] | 1439 | sector_t reclaimable; |
| 1440 | sector_t next_checkpoint; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 1441 | bool write_super; |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1442 | |
| 1443 | spin_lock_irq(&log->io_list_lock); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 1444 | write_super = r5l_reclaimable_space(log) > log->max_free_space || |
| 1445 | reclaim_target != 0 || !list_empty(&log->no_space_stripes); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1446 | /* |
| 1447 | * move proper io_unit to reclaim list. We should not change the order. |
| 1448 | * reclaimable/unreclaimable io_unit can be mixed in the list, we |
| 1449 | * shouldn't reuse space of an unreclaimable io_unit |
| 1450 | */ |
| 1451 | while (1) { |
Christoph Hellwig | 1703646 | 2015-10-05 09:31:06 +0200 | [diff] [blame] | 1452 | reclaimable = r5l_reclaimable_space(log); |
| 1453 | if (reclaimable >= reclaim_target || |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1454 | (list_empty(&log->running_ios) && |
| 1455 | list_empty(&log->io_end_ios) && |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 1456 | list_empty(&log->flushing_ios) && |
Christoph Hellwig | 04732f7 | 2015-10-05 09:31:07 +0200 | [diff] [blame] | 1457 | list_empty(&log->finished_ios))) |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1458 | break; |
| 1459 | |
Christoph Hellwig | 1703646 | 2015-10-05 09:31:06 +0200 | [diff] [blame] | 1460 | md_wakeup_thread(log->rdev->mddev->thread); |
| 1461 | wait_event_lock_irq(log->iounit_wait, |
| 1462 | r5l_reclaimable_space(log) > reclaimable, |
| 1463 | log->io_list_lock); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1464 | } |
Christoph Hellwig | 1703646 | 2015-10-05 09:31:06 +0200 | [diff] [blame] | 1465 | |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 1466 | next_checkpoint = r5c_calculate_new_cp(conf); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1467 | spin_unlock_irq(&log->io_list_lock); |
| 1468 | |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 1469 | if (reclaimable == 0 || !write_super) |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1470 | return; |
| 1471 | |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1472 | /* |
| 1473 | * write_super will flush cache of each raid disk. We must write super |
| 1474 | * here, because the log area might be reused soon and we don't want to |
| 1475 | * confuse recovery |
| 1476 | */ |
Shaohua Li | 4b48204 | 2015-10-08 21:54:06 -0700 | [diff] [blame] | 1477 | r5l_write_super_and_discard_space(log, next_checkpoint); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1478 | |
| 1479 | mutex_lock(&log->io_mutex); |
Christoph Hellwig | 1703646 | 2015-10-05 09:31:06 +0200 | [diff] [blame] | 1480 | log->last_checkpoint = next_checkpoint; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 1481 | r5c_update_log_state(log); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1482 | mutex_unlock(&log->io_mutex); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1483 | |
Christoph Hellwig | 1703646 | 2015-10-05 09:31:06 +0200 | [diff] [blame] | 1484 | r5l_run_no_space_stripes(log); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | static void r5l_reclaim_thread(struct md_thread *thread) |
| 1488 | { |
| 1489 | struct mddev *mddev = thread->mddev; |
| 1490 | struct r5conf *conf = mddev->private; |
| 1491 | struct r5l_log *log = conf->log; |
| 1492 | |
| 1493 | if (!log) |
| 1494 | return; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 1495 | r5c_do_reclaim(conf); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1496 | r5l_do_reclaim(log); |
| 1497 | } |
| 1498 | |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 1499 | void r5l_wake_reclaim(struct r5l_log *log, sector_t space) |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 1500 | { |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1501 | unsigned long target; |
| 1502 | unsigned long new = (unsigned long)space; /* overflow in theory */ |
| 1503 | |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 1504 | if (!log) |
| 1505 | return; |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 1506 | do { |
| 1507 | target = log->reclaim_target; |
| 1508 | if (new < target) |
| 1509 | return; |
| 1510 | } while (cmpxchg(&log->reclaim_target, target, new) != target); |
| 1511 | md_wakeup_thread(log->reclaim_thread); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
Shaohua Li | e6c033f | 2015-10-04 09:20:12 -0700 | [diff] [blame] | 1514 | void r5l_quiesce(struct r5l_log *log, int state) |
| 1515 | { |
Shaohua Li | 4b48204 | 2015-10-08 21:54:06 -0700 | [diff] [blame] | 1516 | struct mddev *mddev; |
Shaohua Li | e6c033f | 2015-10-04 09:20:12 -0700 | [diff] [blame] | 1517 | if (!log || state == 2) |
| 1518 | return; |
Shaohua Li | ce1ccd0 | 2016-11-21 10:29:18 -0800 | [diff] [blame] | 1519 | if (state == 0) |
| 1520 | kthread_unpark(log->reclaim_thread->tsk); |
| 1521 | else if (state == 1) { |
Shaohua Li | 4b48204 | 2015-10-08 21:54:06 -0700 | [diff] [blame] | 1522 | /* make sure r5l_write_super_and_discard_space exits */ |
| 1523 | mddev = log->rdev->mddev; |
| 1524 | wake_up(&mddev->sb_wait); |
Shaohua Li | ce1ccd0 | 2016-11-21 10:29:18 -0800 | [diff] [blame] | 1525 | kthread_park(log->reclaim_thread->tsk); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 1526 | r5l_wake_reclaim(log, MaxSector); |
Shaohua Li | e6c033f | 2015-10-04 09:20:12 -0700 | [diff] [blame] | 1527 | r5l_do_reclaim(log); |
| 1528 | } |
| 1529 | } |
| 1530 | |
Shaohua Li | 6e74a9c | 2015-10-08 21:54:08 -0700 | [diff] [blame] | 1531 | bool r5l_log_disk_error(struct r5conf *conf) |
| 1532 | { |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 1533 | struct r5l_log *log; |
| 1534 | bool ret; |
Shaohua Li | 7dde2ad | 2015-10-08 21:54:10 -0700 | [diff] [blame] | 1535 | /* don't allow write if journal disk is missing */ |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 1536 | rcu_read_lock(); |
| 1537 | log = rcu_dereference(conf->log); |
| 1538 | |
| 1539 | if (!log) |
| 1540 | ret = test_bit(MD_HAS_JOURNAL, &conf->mddev->flags); |
| 1541 | else |
| 1542 | ret = test_bit(Faulty, &log->rdev->flags); |
| 1543 | rcu_read_unlock(); |
| 1544 | return ret; |
Shaohua Li | 6e74a9c | 2015-10-08 21:54:08 -0700 | [diff] [blame] | 1545 | } |
| 1546 | |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 1547 | struct r5l_recovery_ctx { |
| 1548 | struct page *meta_page; /* current meta */ |
| 1549 | sector_t meta_total_blocks; /* total size of current meta and data */ |
| 1550 | sector_t pos; /* recovery position */ |
| 1551 | u64 seq; /* recovery position seq */ |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 1552 | int data_parity_stripes; /* number of data_parity stripes */ |
| 1553 | int data_only_stripes; /* number of data_only stripes */ |
| 1554 | struct list_head cached_list; |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 1555 | }; |
| 1556 | |
Song Liu | 9ed988f5 | 2016-11-17 15:24:42 -0800 | [diff] [blame] | 1557 | static int r5l_recovery_read_meta_block(struct r5l_log *log, |
| 1558 | struct r5l_recovery_ctx *ctx) |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 1559 | { |
| 1560 | struct page *page = ctx->meta_page; |
| 1561 | struct r5l_meta_block *mb; |
| 1562 | u32 crc, stored_crc; |
| 1563 | |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1564 | if (!sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page, REQ_OP_READ, 0, |
| 1565 | false)) |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 1566 | return -EIO; |
| 1567 | |
| 1568 | mb = page_address(page); |
| 1569 | stored_crc = le32_to_cpu(mb->checksum); |
| 1570 | mb->checksum = 0; |
| 1571 | |
| 1572 | if (le32_to_cpu(mb->magic) != R5LOG_MAGIC || |
| 1573 | le64_to_cpu(mb->seq) != ctx->seq || |
| 1574 | mb->version != R5LOG_VERSION || |
| 1575 | le64_to_cpu(mb->position) != ctx->pos) |
| 1576 | return -EINVAL; |
| 1577 | |
Shaohua Li | 5cb2fbd | 2015-10-28 08:41:25 -0700 | [diff] [blame] | 1578 | crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE); |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 1579 | if (stored_crc != crc) |
| 1580 | return -EINVAL; |
| 1581 | |
| 1582 | if (le32_to_cpu(mb->meta_size) > PAGE_SIZE) |
| 1583 | return -EINVAL; |
| 1584 | |
| 1585 | ctx->meta_total_blocks = BLOCK_SECTORS; |
| 1586 | |
| 1587 | return 0; |
| 1588 | } |
| 1589 | |
Song Liu | 9ed988f5 | 2016-11-17 15:24:42 -0800 | [diff] [blame] | 1590 | static void |
| 1591 | r5l_recovery_create_empty_meta_block(struct r5l_log *log, |
| 1592 | struct page *page, |
| 1593 | sector_t pos, u64 seq) |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 1594 | { |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 1595 | struct r5l_meta_block *mb; |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 1596 | |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 1597 | mb = page_address(page); |
Song Liu | 9ed988f5 | 2016-11-17 15:24:42 -0800 | [diff] [blame] | 1598 | clear_page(mb); |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 1599 | mb->magic = cpu_to_le32(R5LOG_MAGIC); |
| 1600 | mb->version = R5LOG_VERSION; |
| 1601 | mb->meta_size = cpu_to_le32(sizeof(struct r5l_meta_block)); |
| 1602 | mb->seq = cpu_to_le64(seq); |
| 1603 | mb->position = cpu_to_le64(pos); |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | static int r5l_log_write_empty_meta_block(struct r5l_log *log, sector_t pos, |
| 1607 | u64 seq) |
| 1608 | { |
| 1609 | struct page *page; |
| 1610 | struct r5l_meta_block *mb; |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 1611 | |
Song Liu | 9ed988f5 | 2016-11-17 15:24:42 -0800 | [diff] [blame] | 1612 | page = alloc_page(GFP_KERNEL); |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 1613 | if (!page) |
| 1614 | return -ENOMEM; |
Song Liu | 9ed988f5 | 2016-11-17 15:24:42 -0800 | [diff] [blame] | 1615 | r5l_recovery_create_empty_meta_block(log, page, pos, seq); |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 1616 | mb = page_address(page); |
Song Liu | 5c88f40 | 2016-12-07 09:42:05 -0800 | [diff] [blame] | 1617 | mb->checksum = cpu_to_le32(crc32c_le(log->uuid_checksum, |
| 1618 | mb, PAGE_SIZE)); |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1619 | if (!sync_page_io(log->rdev, pos, PAGE_SIZE, page, REQ_OP_WRITE, |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 1620 | REQ_FUA, false)) { |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 1621 | __free_page(page); |
| 1622 | return -EIO; |
| 1623 | } |
| 1624 | __free_page(page); |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 1628 | /* |
| 1629 | * r5l_recovery_load_data and r5l_recovery_load_parity uses flag R5_Wantwrite |
| 1630 | * to mark valid (potentially not flushed) data in the journal. |
| 1631 | * |
| 1632 | * We already verified checksum in r5l_recovery_verify_data_checksum_for_mb, |
| 1633 | * so there should not be any mismatch here. |
| 1634 | */ |
| 1635 | static void r5l_recovery_load_data(struct r5l_log *log, |
| 1636 | struct stripe_head *sh, |
| 1637 | struct r5l_recovery_ctx *ctx, |
| 1638 | struct r5l_payload_data_parity *payload, |
| 1639 | sector_t log_offset) |
| 1640 | { |
| 1641 | struct mddev *mddev = log->rdev->mddev; |
| 1642 | struct r5conf *conf = mddev->private; |
| 1643 | int dd_idx; |
| 1644 | |
| 1645 | raid5_compute_sector(conf, |
| 1646 | le64_to_cpu(payload->location), 0, |
| 1647 | &dd_idx, sh); |
| 1648 | sync_page_io(log->rdev, log_offset, PAGE_SIZE, |
| 1649 | sh->dev[dd_idx].page, REQ_OP_READ, 0, false); |
| 1650 | sh->dev[dd_idx].log_checksum = |
| 1651 | le32_to_cpu(payload->checksum[0]); |
| 1652 | ctx->meta_total_blocks += BLOCK_SECTORS; |
| 1653 | |
| 1654 | set_bit(R5_Wantwrite, &sh->dev[dd_idx].flags); |
| 1655 | set_bit(STRIPE_R5C_CACHING, &sh->state); |
| 1656 | } |
| 1657 | |
| 1658 | static void r5l_recovery_load_parity(struct r5l_log *log, |
| 1659 | struct stripe_head *sh, |
| 1660 | struct r5l_recovery_ctx *ctx, |
| 1661 | struct r5l_payload_data_parity *payload, |
| 1662 | sector_t log_offset) |
| 1663 | { |
| 1664 | struct mddev *mddev = log->rdev->mddev; |
| 1665 | struct r5conf *conf = mddev->private; |
| 1666 | |
| 1667 | ctx->meta_total_blocks += BLOCK_SECTORS * conf->max_degraded; |
| 1668 | sync_page_io(log->rdev, log_offset, PAGE_SIZE, |
| 1669 | sh->dev[sh->pd_idx].page, REQ_OP_READ, 0, false); |
| 1670 | sh->dev[sh->pd_idx].log_checksum = |
| 1671 | le32_to_cpu(payload->checksum[0]); |
| 1672 | set_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags); |
| 1673 | |
| 1674 | if (sh->qd_idx >= 0) { |
| 1675 | sync_page_io(log->rdev, |
| 1676 | r5l_ring_add(log, log_offset, BLOCK_SECTORS), |
| 1677 | PAGE_SIZE, sh->dev[sh->qd_idx].page, |
| 1678 | REQ_OP_READ, 0, false); |
| 1679 | sh->dev[sh->qd_idx].log_checksum = |
| 1680 | le32_to_cpu(payload->checksum[1]); |
| 1681 | set_bit(R5_Wantwrite, &sh->dev[sh->qd_idx].flags); |
| 1682 | } |
| 1683 | clear_bit(STRIPE_R5C_CACHING, &sh->state); |
| 1684 | } |
| 1685 | |
| 1686 | static void r5l_recovery_reset_stripe(struct stripe_head *sh) |
| 1687 | { |
| 1688 | int i; |
| 1689 | |
| 1690 | sh->state = 0; |
| 1691 | sh->log_start = MaxSector; |
| 1692 | for (i = sh->disks; i--; ) |
| 1693 | sh->dev[i].flags = 0; |
| 1694 | } |
| 1695 | |
| 1696 | static void |
| 1697 | r5l_recovery_replay_one_stripe(struct r5conf *conf, |
| 1698 | struct stripe_head *sh, |
| 1699 | struct r5l_recovery_ctx *ctx) |
| 1700 | { |
| 1701 | struct md_rdev *rdev, *rrdev; |
| 1702 | int disk_index; |
| 1703 | int data_count = 0; |
| 1704 | |
| 1705 | for (disk_index = 0; disk_index < sh->disks; disk_index++) { |
| 1706 | if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags)) |
| 1707 | continue; |
| 1708 | if (disk_index == sh->qd_idx || disk_index == sh->pd_idx) |
| 1709 | continue; |
| 1710 | data_count++; |
| 1711 | } |
| 1712 | |
| 1713 | /* |
| 1714 | * stripes that only have parity must have been flushed |
| 1715 | * before the crash that we are now recovering from, so |
| 1716 | * there is nothing more to recovery. |
| 1717 | */ |
| 1718 | if (data_count == 0) |
| 1719 | goto out; |
| 1720 | |
| 1721 | for (disk_index = 0; disk_index < sh->disks; disk_index++) { |
| 1722 | if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags)) |
| 1723 | continue; |
| 1724 | |
| 1725 | /* in case device is broken */ |
| 1726 | rcu_read_lock(); |
| 1727 | rdev = rcu_dereference(conf->disks[disk_index].rdev); |
| 1728 | if (rdev) { |
| 1729 | atomic_inc(&rdev->nr_pending); |
| 1730 | rcu_read_unlock(); |
| 1731 | sync_page_io(rdev, sh->sector, PAGE_SIZE, |
| 1732 | sh->dev[disk_index].page, REQ_OP_WRITE, 0, |
| 1733 | false); |
| 1734 | rdev_dec_pending(rdev, rdev->mddev); |
| 1735 | rcu_read_lock(); |
| 1736 | } |
| 1737 | rrdev = rcu_dereference(conf->disks[disk_index].replacement); |
| 1738 | if (rrdev) { |
| 1739 | atomic_inc(&rrdev->nr_pending); |
| 1740 | rcu_read_unlock(); |
| 1741 | sync_page_io(rrdev, sh->sector, PAGE_SIZE, |
| 1742 | sh->dev[disk_index].page, REQ_OP_WRITE, 0, |
| 1743 | false); |
| 1744 | rdev_dec_pending(rrdev, rrdev->mddev); |
| 1745 | rcu_read_lock(); |
| 1746 | } |
| 1747 | rcu_read_unlock(); |
| 1748 | } |
| 1749 | ctx->data_parity_stripes++; |
| 1750 | out: |
| 1751 | r5l_recovery_reset_stripe(sh); |
| 1752 | } |
| 1753 | |
| 1754 | static struct stripe_head * |
| 1755 | r5c_recovery_alloc_stripe(struct r5conf *conf, |
Song Liu | 3c66abb | 2016-12-14 15:38:01 -0800 | [diff] [blame] | 1756 | sector_t stripe_sect) |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 1757 | { |
| 1758 | struct stripe_head *sh; |
| 1759 | |
| 1760 | sh = raid5_get_active_stripe(conf, stripe_sect, 0, 1, 0); |
| 1761 | if (!sh) |
| 1762 | return NULL; /* no more stripe available */ |
| 1763 | |
| 1764 | r5l_recovery_reset_stripe(sh); |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 1765 | |
| 1766 | return sh; |
| 1767 | } |
| 1768 | |
| 1769 | static struct stripe_head * |
| 1770 | r5c_recovery_lookup_stripe(struct list_head *list, sector_t sect) |
| 1771 | { |
| 1772 | struct stripe_head *sh; |
| 1773 | |
| 1774 | list_for_each_entry(sh, list, lru) |
| 1775 | if (sh->sector == sect) |
| 1776 | return sh; |
| 1777 | return NULL; |
| 1778 | } |
| 1779 | |
| 1780 | static void |
| 1781 | r5c_recovery_drop_stripes(struct list_head *cached_stripe_list, |
| 1782 | struct r5l_recovery_ctx *ctx) |
| 1783 | { |
| 1784 | struct stripe_head *sh, *next; |
| 1785 | |
| 1786 | list_for_each_entry_safe(sh, next, cached_stripe_list, lru) { |
| 1787 | r5l_recovery_reset_stripe(sh); |
| 1788 | list_del_init(&sh->lru); |
| 1789 | raid5_release_stripe(sh); |
| 1790 | } |
| 1791 | } |
| 1792 | |
| 1793 | static void |
| 1794 | r5c_recovery_replay_stripes(struct list_head *cached_stripe_list, |
| 1795 | struct r5l_recovery_ctx *ctx) |
| 1796 | { |
| 1797 | struct stripe_head *sh, *next; |
| 1798 | |
| 1799 | list_for_each_entry_safe(sh, next, cached_stripe_list, lru) |
| 1800 | if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) { |
| 1801 | r5l_recovery_replay_one_stripe(sh->raid_conf, sh, ctx); |
| 1802 | list_del_init(&sh->lru); |
| 1803 | raid5_release_stripe(sh); |
| 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | /* if matches return 0; otherwise return -EINVAL */ |
| 1808 | static int |
| 1809 | r5l_recovery_verify_data_checksum(struct r5l_log *log, struct page *page, |
| 1810 | sector_t log_offset, __le32 log_checksum) |
| 1811 | { |
| 1812 | void *addr; |
| 1813 | u32 checksum; |
| 1814 | |
| 1815 | sync_page_io(log->rdev, log_offset, PAGE_SIZE, |
| 1816 | page, REQ_OP_READ, 0, false); |
| 1817 | addr = kmap_atomic(page); |
| 1818 | checksum = crc32c_le(log->uuid_checksum, addr, PAGE_SIZE); |
| 1819 | kunmap_atomic(addr); |
| 1820 | return (le32_to_cpu(log_checksum) == checksum) ? 0 : -EINVAL; |
| 1821 | } |
| 1822 | |
| 1823 | /* |
| 1824 | * before loading data to stripe cache, we need verify checksum for all data, |
| 1825 | * if there is mismatch for any data page, we drop all data in the mata block |
| 1826 | */ |
| 1827 | static int |
| 1828 | r5l_recovery_verify_data_checksum_for_mb(struct r5l_log *log, |
| 1829 | struct r5l_recovery_ctx *ctx) |
| 1830 | { |
| 1831 | struct mddev *mddev = log->rdev->mddev; |
| 1832 | struct r5conf *conf = mddev->private; |
| 1833 | struct r5l_meta_block *mb = page_address(ctx->meta_page); |
| 1834 | sector_t mb_offset = sizeof(struct r5l_meta_block); |
| 1835 | sector_t log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS); |
| 1836 | struct page *page; |
| 1837 | struct r5l_payload_data_parity *payload; |
| 1838 | |
| 1839 | page = alloc_page(GFP_KERNEL); |
| 1840 | if (!page) |
| 1841 | return -ENOMEM; |
| 1842 | |
| 1843 | while (mb_offset < le32_to_cpu(mb->meta_size)) { |
| 1844 | payload = (void *)mb + mb_offset; |
| 1845 | |
| 1846 | if (payload->header.type == R5LOG_PAYLOAD_DATA) { |
| 1847 | if (r5l_recovery_verify_data_checksum( |
| 1848 | log, page, log_offset, |
| 1849 | payload->checksum[0]) < 0) |
| 1850 | goto mismatch; |
| 1851 | } else if (payload->header.type == R5LOG_PAYLOAD_PARITY) { |
| 1852 | if (r5l_recovery_verify_data_checksum( |
| 1853 | log, page, log_offset, |
| 1854 | payload->checksum[0]) < 0) |
| 1855 | goto mismatch; |
| 1856 | if (conf->max_degraded == 2 && /* q for RAID 6 */ |
| 1857 | r5l_recovery_verify_data_checksum( |
| 1858 | log, page, |
| 1859 | r5l_ring_add(log, log_offset, |
| 1860 | BLOCK_SECTORS), |
| 1861 | payload->checksum[1]) < 0) |
| 1862 | goto mismatch; |
| 1863 | } else /* not R5LOG_PAYLOAD_DATA or R5LOG_PAYLOAD_PARITY */ |
| 1864 | goto mismatch; |
| 1865 | |
| 1866 | log_offset = r5l_ring_add(log, log_offset, |
| 1867 | le32_to_cpu(payload->size)); |
| 1868 | |
| 1869 | mb_offset += sizeof(struct r5l_payload_data_parity) + |
| 1870 | sizeof(__le32) * |
| 1871 | (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9)); |
| 1872 | } |
| 1873 | |
| 1874 | put_page(page); |
| 1875 | return 0; |
| 1876 | |
| 1877 | mismatch: |
| 1878 | put_page(page); |
| 1879 | return -EINVAL; |
| 1880 | } |
| 1881 | |
| 1882 | /* |
| 1883 | * Analyze all data/parity pages in one meta block |
| 1884 | * Returns: |
| 1885 | * 0 for success |
| 1886 | * -EINVAL for unknown playload type |
| 1887 | * -EAGAIN for checksum mismatch of data page |
| 1888 | * -ENOMEM for run out of memory (alloc_page failed or run out of stripes) |
| 1889 | */ |
| 1890 | static int |
| 1891 | r5c_recovery_analyze_meta_block(struct r5l_log *log, |
| 1892 | struct r5l_recovery_ctx *ctx, |
| 1893 | struct list_head *cached_stripe_list) |
| 1894 | { |
| 1895 | struct mddev *mddev = log->rdev->mddev; |
| 1896 | struct r5conf *conf = mddev->private; |
| 1897 | struct r5l_meta_block *mb; |
| 1898 | struct r5l_payload_data_parity *payload; |
| 1899 | int mb_offset; |
| 1900 | sector_t log_offset; |
| 1901 | sector_t stripe_sect; |
| 1902 | struct stripe_head *sh; |
| 1903 | int ret; |
| 1904 | |
| 1905 | /* |
| 1906 | * for mismatch in data blocks, we will drop all data in this mb, but |
| 1907 | * we will still read next mb for other data with FLUSH flag, as |
| 1908 | * io_unit could finish out of order. |
| 1909 | */ |
| 1910 | ret = r5l_recovery_verify_data_checksum_for_mb(log, ctx); |
| 1911 | if (ret == -EINVAL) |
| 1912 | return -EAGAIN; |
| 1913 | else if (ret) |
| 1914 | return ret; /* -ENOMEM duo to alloc_page() failed */ |
| 1915 | |
| 1916 | mb = page_address(ctx->meta_page); |
| 1917 | mb_offset = sizeof(struct r5l_meta_block); |
| 1918 | log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS); |
| 1919 | |
| 1920 | while (mb_offset < le32_to_cpu(mb->meta_size)) { |
| 1921 | int dd; |
| 1922 | |
| 1923 | payload = (void *)mb + mb_offset; |
| 1924 | stripe_sect = (payload->header.type == R5LOG_PAYLOAD_DATA) ? |
| 1925 | raid5_compute_sector( |
| 1926 | conf, le64_to_cpu(payload->location), 0, &dd, |
| 1927 | NULL) |
| 1928 | : le64_to_cpu(payload->location); |
| 1929 | |
| 1930 | sh = r5c_recovery_lookup_stripe(cached_stripe_list, |
| 1931 | stripe_sect); |
| 1932 | |
| 1933 | if (!sh) { |
Song Liu | 3c66abb | 2016-12-14 15:38:01 -0800 | [diff] [blame] | 1934 | sh = r5c_recovery_alloc_stripe(conf, stripe_sect); |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 1935 | /* |
| 1936 | * cannot get stripe from raid5_get_active_stripe |
| 1937 | * try replay some stripes |
| 1938 | */ |
| 1939 | if (!sh) { |
| 1940 | r5c_recovery_replay_stripes( |
| 1941 | cached_stripe_list, ctx); |
| 1942 | sh = r5c_recovery_alloc_stripe( |
Song Liu | 3c66abb | 2016-12-14 15:38:01 -0800 | [diff] [blame] | 1943 | conf, stripe_sect); |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 1944 | } |
| 1945 | if (!sh) { |
| 1946 | pr_debug("md/raid:%s: Increasing stripe cache size to %d to recovery data on journal.\n", |
| 1947 | mdname(mddev), |
| 1948 | conf->min_nr_stripes * 2); |
| 1949 | raid5_set_cache_size(mddev, |
| 1950 | conf->min_nr_stripes * 2); |
Song Liu | 3c66abb | 2016-12-14 15:38:01 -0800 | [diff] [blame] | 1951 | sh = r5c_recovery_alloc_stripe(conf, |
| 1952 | stripe_sect); |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 1953 | } |
| 1954 | if (!sh) { |
| 1955 | pr_err("md/raid:%s: Cannot get enough stripes due to memory pressure. Recovery failed.\n", |
| 1956 | mdname(mddev)); |
| 1957 | return -ENOMEM; |
| 1958 | } |
| 1959 | list_add_tail(&sh->lru, cached_stripe_list); |
| 1960 | } |
| 1961 | |
| 1962 | if (payload->header.type == R5LOG_PAYLOAD_DATA) { |
Zhengyuan Liu | f7b7bee | 2016-11-26 10:57:13 +0800 | [diff] [blame] | 1963 | if (!test_bit(STRIPE_R5C_CACHING, &sh->state) && |
| 1964 | test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags)) { |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 1965 | r5l_recovery_replay_one_stripe(conf, sh, ctx); |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 1966 | list_move_tail(&sh->lru, cached_stripe_list); |
| 1967 | } |
| 1968 | r5l_recovery_load_data(log, sh, ctx, payload, |
| 1969 | log_offset); |
| 1970 | } else if (payload->header.type == R5LOG_PAYLOAD_PARITY) |
| 1971 | r5l_recovery_load_parity(log, sh, ctx, payload, |
| 1972 | log_offset); |
| 1973 | else |
| 1974 | return -EINVAL; |
| 1975 | |
| 1976 | log_offset = r5l_ring_add(log, log_offset, |
| 1977 | le32_to_cpu(payload->size)); |
| 1978 | |
| 1979 | mb_offset += sizeof(struct r5l_payload_data_parity) + |
| 1980 | sizeof(__le32) * |
| 1981 | (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9)); |
| 1982 | } |
| 1983 | |
| 1984 | return 0; |
| 1985 | } |
| 1986 | |
| 1987 | /* |
| 1988 | * Load the stripe into cache. The stripe will be written out later by |
| 1989 | * the stripe cache state machine. |
| 1990 | */ |
| 1991 | static void r5c_recovery_load_one_stripe(struct r5l_log *log, |
| 1992 | struct stripe_head *sh) |
| 1993 | { |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 1994 | struct r5dev *dev; |
| 1995 | int i; |
| 1996 | |
| 1997 | for (i = sh->disks; i--; ) { |
| 1998 | dev = sh->dev + i; |
| 1999 | if (test_and_clear_bit(R5_Wantwrite, &dev->flags)) { |
| 2000 | set_bit(R5_InJournal, &dev->flags); |
| 2001 | set_bit(R5_UPTODATE, &dev->flags); |
| 2002 | } |
| 2003 | } |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 2004 | } |
| 2005 | |
| 2006 | /* |
| 2007 | * Scan through the log for all to-be-flushed data |
| 2008 | * |
| 2009 | * For stripes with data and parity, namely Data-Parity stripe |
| 2010 | * (STRIPE_R5C_CACHING == 0), we simply replay all the writes. |
| 2011 | * |
| 2012 | * For stripes with only data, namely Data-Only stripe |
| 2013 | * (STRIPE_R5C_CACHING == 1), we load them to stripe cache state machine. |
| 2014 | * |
| 2015 | * For a stripe, if we see data after parity, we should discard all previous |
| 2016 | * data and parity for this stripe, as these data are already flushed to |
| 2017 | * the array. |
| 2018 | * |
| 2019 | * At the end of the scan, we return the new journal_tail, which points to |
| 2020 | * first data-only stripe on the journal device, or next invalid meta block. |
| 2021 | */ |
| 2022 | static int r5c_recovery_flush_log(struct r5l_log *log, |
| 2023 | struct r5l_recovery_ctx *ctx) |
| 2024 | { |
JackieLiu | bc8f167 | 2016-11-28 16:19:20 +0800 | [diff] [blame] | 2025 | struct stripe_head *sh; |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 2026 | int ret = 0; |
| 2027 | |
| 2028 | /* scan through the log */ |
| 2029 | while (1) { |
| 2030 | if (r5l_recovery_read_meta_block(log, ctx)) |
| 2031 | break; |
| 2032 | |
| 2033 | ret = r5c_recovery_analyze_meta_block(log, ctx, |
| 2034 | &ctx->cached_list); |
| 2035 | /* |
| 2036 | * -EAGAIN means mismatch in data block, in this case, we still |
| 2037 | * try scan the next metablock |
| 2038 | */ |
| 2039 | if (ret && ret != -EAGAIN) |
| 2040 | break; /* ret == -EINVAL or -ENOMEM */ |
| 2041 | ctx->seq++; |
| 2042 | ctx->pos = r5l_ring_add(log, ctx->pos, ctx->meta_total_blocks); |
| 2043 | } |
| 2044 | |
| 2045 | if (ret == -ENOMEM) { |
| 2046 | r5c_recovery_drop_stripes(&ctx->cached_list, ctx); |
| 2047 | return ret; |
| 2048 | } |
| 2049 | |
| 2050 | /* replay data-parity stripes */ |
| 2051 | r5c_recovery_replay_stripes(&ctx->cached_list, ctx); |
| 2052 | |
| 2053 | /* load data-only stripes to stripe cache */ |
JackieLiu | bc8f167 | 2016-11-28 16:19:20 +0800 | [diff] [blame] | 2054 | list_for_each_entry(sh, &ctx->cached_list, lru) { |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 2055 | WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state)); |
| 2056 | r5c_recovery_load_one_stripe(log, sh); |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 2057 | ctx->data_only_stripes++; |
| 2058 | } |
| 2059 | |
| 2060 | return 0; |
| 2061 | } |
| 2062 | |
| 2063 | /* |
| 2064 | * we did a recovery. Now ctx.pos points to an invalid meta block. New |
| 2065 | * log will start here. but we can't let superblock point to last valid |
| 2066 | * meta block. The log might looks like: |
| 2067 | * | meta 1| meta 2| meta 3| |
| 2068 | * meta 1 is valid, meta 2 is invalid. meta 3 could be valid. If |
| 2069 | * superblock points to meta 1, we write a new valid meta 2n. if crash |
| 2070 | * happens again, new recovery will start from meta 1. Since meta 2n is |
| 2071 | * valid now, recovery will think meta 3 is valid, which is wrong. |
| 2072 | * The solution is we create a new meta in meta2 with its seq == meta |
Song Liu | 3c6edc6 | 2016-12-07 09:42:06 -0800 | [diff] [blame] | 2073 | * 1's seq + 10000 and let superblock points to meta2. The same recovery |
| 2074 | * will not think meta 3 is a valid meta, because its seq doesn't match |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 2075 | */ |
| 2076 | |
| 2077 | /* |
| 2078 | * Before recovery, the log looks like the following |
| 2079 | * |
| 2080 | * --------------------------------------------- |
| 2081 | * | valid log | invalid log | |
| 2082 | * --------------------------------------------- |
| 2083 | * ^ |
| 2084 | * |- log->last_checkpoint |
| 2085 | * |- log->last_cp_seq |
| 2086 | * |
| 2087 | * Now we scan through the log until we see invalid entry |
| 2088 | * |
| 2089 | * --------------------------------------------- |
| 2090 | * | valid log | invalid log | |
| 2091 | * --------------------------------------------- |
| 2092 | * ^ ^ |
| 2093 | * |- log->last_checkpoint |- ctx->pos |
| 2094 | * |- log->last_cp_seq |- ctx->seq |
| 2095 | * |
| 2096 | * From this point, we need to increase seq number by 10 to avoid |
| 2097 | * confusing next recovery. |
| 2098 | * |
| 2099 | * --------------------------------------------- |
| 2100 | * | valid log | invalid log | |
| 2101 | * --------------------------------------------- |
| 2102 | * ^ ^ |
| 2103 | * |- log->last_checkpoint |- ctx->pos+1 |
Song Liu | 3c6edc6 | 2016-12-07 09:42:06 -0800 | [diff] [blame] | 2104 | * |- log->last_cp_seq |- ctx->seq+10001 |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 2105 | * |
| 2106 | * However, it is not safe to start the state machine yet, because data only |
| 2107 | * parities are not yet secured in RAID. To save these data only parities, we |
| 2108 | * rewrite them from seq+11. |
| 2109 | * |
| 2110 | * ----------------------------------------------------------------- |
| 2111 | * | valid log | data only stripes | invalid log | |
| 2112 | * ----------------------------------------------------------------- |
| 2113 | * ^ ^ |
| 2114 | * |- log->last_checkpoint |- ctx->pos+n |
Song Liu | 3c6edc6 | 2016-12-07 09:42:06 -0800 | [diff] [blame] | 2115 | * |- log->last_cp_seq |- ctx->seq+10000+n |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 2116 | * |
| 2117 | * If failure happens again during this process, the recovery can safe start |
| 2118 | * again from log->last_checkpoint. |
| 2119 | * |
| 2120 | * Once data only stripes are rewritten to journal, we move log_tail |
| 2121 | * |
| 2122 | * ----------------------------------------------------------------- |
| 2123 | * | old log | data only stripes | invalid log | |
| 2124 | * ----------------------------------------------------------------- |
| 2125 | * ^ ^ |
| 2126 | * |- log->last_checkpoint |- ctx->pos+n |
Song Liu | 3c6edc6 | 2016-12-07 09:42:06 -0800 | [diff] [blame] | 2127 | * |- log->last_cp_seq |- ctx->seq+10000+n |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 2128 | * |
| 2129 | * Then we can safely start the state machine. If failure happens from this |
| 2130 | * point on, the recovery will start from new log->last_checkpoint. |
| 2131 | */ |
| 2132 | static int |
| 2133 | r5c_recovery_rewrite_data_only_stripes(struct r5l_log *log, |
| 2134 | struct r5l_recovery_ctx *ctx) |
| 2135 | { |
Song Liu | a85dd7b | 2017-01-23 17:12:57 -0800 | [diff] [blame] | 2136 | struct stripe_head *sh; |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 2137 | struct mddev *mddev = log->rdev->mddev; |
| 2138 | struct page *page; |
Song Liu | 3c66abb | 2016-12-14 15:38:01 -0800 | [diff] [blame] | 2139 | sector_t next_checkpoint = MaxSector; |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 2140 | |
| 2141 | page = alloc_page(GFP_KERNEL); |
| 2142 | if (!page) { |
| 2143 | pr_err("md/raid:%s: cannot allocate memory to rewrite data only stripes\n", |
| 2144 | mdname(mddev)); |
| 2145 | return -ENOMEM; |
| 2146 | } |
| 2147 | |
Song Liu | 3c66abb | 2016-12-14 15:38:01 -0800 | [diff] [blame] | 2148 | WARN_ON(list_empty(&ctx->cached_list)); |
| 2149 | |
Song Liu | a85dd7b | 2017-01-23 17:12:57 -0800 | [diff] [blame] | 2150 | list_for_each_entry(sh, &ctx->cached_list, lru) { |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 2151 | struct r5l_meta_block *mb; |
| 2152 | int i; |
| 2153 | int offset; |
| 2154 | sector_t write_pos; |
| 2155 | |
| 2156 | WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state)); |
| 2157 | r5l_recovery_create_empty_meta_block(log, page, |
| 2158 | ctx->pos, ctx->seq); |
| 2159 | mb = page_address(page); |
| 2160 | offset = le32_to_cpu(mb->meta_size); |
JackieLiu | fc833c2 | 2016-11-28 16:19:19 +0800 | [diff] [blame] | 2161 | write_pos = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS); |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 2162 | |
| 2163 | for (i = sh->disks; i--; ) { |
| 2164 | struct r5dev *dev = &sh->dev[i]; |
| 2165 | struct r5l_payload_data_parity *payload; |
| 2166 | void *addr; |
| 2167 | |
| 2168 | if (test_bit(R5_InJournal, &dev->flags)) { |
| 2169 | payload = (void *)mb + offset; |
| 2170 | payload->header.type = cpu_to_le16( |
| 2171 | R5LOG_PAYLOAD_DATA); |
| 2172 | payload->size = BLOCK_SECTORS; |
| 2173 | payload->location = cpu_to_le64( |
| 2174 | raid5_compute_blocknr(sh, i, 0)); |
| 2175 | addr = kmap_atomic(dev->page); |
| 2176 | payload->checksum[0] = cpu_to_le32( |
| 2177 | crc32c_le(log->uuid_checksum, addr, |
| 2178 | PAGE_SIZE)); |
| 2179 | kunmap_atomic(addr); |
| 2180 | sync_page_io(log->rdev, write_pos, PAGE_SIZE, |
| 2181 | dev->page, REQ_OP_WRITE, 0, false); |
| 2182 | write_pos = r5l_ring_add(log, write_pos, |
| 2183 | BLOCK_SECTORS); |
| 2184 | offset += sizeof(__le32) + |
| 2185 | sizeof(struct r5l_payload_data_parity); |
| 2186 | |
| 2187 | } |
| 2188 | } |
| 2189 | mb->meta_size = cpu_to_le32(offset); |
Song Liu | 5c88f40 | 2016-12-07 09:42:05 -0800 | [diff] [blame] | 2190 | mb->checksum = cpu_to_le32(crc32c_le(log->uuid_checksum, |
| 2191 | mb, PAGE_SIZE)); |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 2192 | sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page, |
Shaohua Li | 2073773 | 2016-12-13 12:40:15 -0800 | [diff] [blame] | 2193 | REQ_OP_WRITE, REQ_FUA, false); |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 2194 | sh->log_start = ctx->pos; |
Song Liu | 3c66abb | 2016-12-14 15:38:01 -0800 | [diff] [blame] | 2195 | list_add_tail(&sh->r5c, &log->stripe_in_journal_list); |
| 2196 | atomic_inc(&log->stripe_in_journal_count); |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 2197 | ctx->pos = write_pos; |
| 2198 | ctx->seq += 1; |
Song Liu | 3c66abb | 2016-12-14 15:38:01 -0800 | [diff] [blame] | 2199 | next_checkpoint = sh->log_start; |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 2200 | } |
Song Liu | 3c66abb | 2016-12-14 15:38:01 -0800 | [diff] [blame] | 2201 | log->next_checkpoint = next_checkpoint; |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 2202 | __free_page(page); |
| 2203 | return 0; |
| 2204 | } |
| 2205 | |
Song Liu | a85dd7b | 2017-01-23 17:12:57 -0800 | [diff] [blame] | 2206 | static void r5c_recovery_flush_data_only_stripes(struct r5l_log *log, |
| 2207 | struct r5l_recovery_ctx *ctx) |
| 2208 | { |
| 2209 | struct mddev *mddev = log->rdev->mddev; |
| 2210 | struct r5conf *conf = mddev->private; |
| 2211 | struct stripe_head *sh, *next; |
| 2212 | |
| 2213 | if (ctx->data_only_stripes == 0) |
| 2214 | return; |
| 2215 | |
| 2216 | log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_BACK; |
| 2217 | |
| 2218 | list_for_each_entry_safe(sh, next, &ctx->cached_list, lru) { |
| 2219 | r5c_make_stripe_write_out(sh); |
| 2220 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2221 | list_del_init(&sh->lru); |
| 2222 | raid5_release_stripe(sh); |
| 2223 | } |
| 2224 | |
| 2225 | md_wakeup_thread(conf->mddev->thread); |
| 2226 | /* reuse conf->wait_for_quiescent in recovery */ |
| 2227 | wait_event(conf->wait_for_quiescent, |
| 2228 | atomic_read(&conf->active_stripes) == 0); |
| 2229 | |
| 2230 | log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH; |
| 2231 | } |
| 2232 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2233 | static int r5l_recovery_log(struct r5l_log *log) |
| 2234 | { |
Song Liu | 5aabf7c | 2016-11-17 15:24:44 -0800 | [diff] [blame] | 2235 | struct mddev *mddev = log->rdev->mddev; |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 2236 | struct r5l_recovery_ctx ctx; |
Song Liu | 5aabf7c | 2016-11-17 15:24:44 -0800 | [diff] [blame] | 2237 | int ret; |
JackieLiu | 43b9674 | 2016-12-05 11:58:53 +0800 | [diff] [blame] | 2238 | sector_t pos; |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 2239 | |
| 2240 | ctx.pos = log->last_checkpoint; |
| 2241 | ctx.seq = log->last_cp_seq; |
| 2242 | ctx.meta_page = alloc_page(GFP_KERNEL); |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 2243 | ctx.data_only_stripes = 0; |
| 2244 | ctx.data_parity_stripes = 0; |
| 2245 | INIT_LIST_HEAD(&ctx.cached_list); |
| 2246 | |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 2247 | if (!ctx.meta_page) |
| 2248 | return -ENOMEM; |
| 2249 | |
Song Liu | 5aabf7c | 2016-11-17 15:24:44 -0800 | [diff] [blame] | 2250 | ret = r5c_recovery_flush_log(log, &ctx); |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 2251 | __free_page(ctx.meta_page); |
| 2252 | |
Song Liu | 5aabf7c | 2016-11-17 15:24:44 -0800 | [diff] [blame] | 2253 | if (ret) |
| 2254 | return ret; |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 2255 | |
Song Liu | 3c6edc6 | 2016-12-07 09:42:06 -0800 | [diff] [blame] | 2256 | pos = ctx.pos; |
| 2257 | ctx.seq += 10000; |
JackieLiu | 43b9674 | 2016-12-05 11:58:53 +0800 | [diff] [blame] | 2258 | |
JackieLiu | 43b9674 | 2016-12-05 11:58:53 +0800 | [diff] [blame] | 2259 | |
Song Liu | 5aabf7c | 2016-11-17 15:24:44 -0800 | [diff] [blame] | 2260 | if ((ctx.data_only_stripes == 0) && (ctx.data_parity_stripes == 0)) |
| 2261 | pr_debug("md/raid:%s: starting from clean shutdown\n", |
| 2262 | mdname(mddev)); |
Song Liu | a85dd7b | 2017-01-23 17:12:57 -0800 | [diff] [blame] | 2263 | else |
Colin Ian King | 99f1789 | 2016-12-23 00:52:30 +0000 | [diff] [blame] | 2264 | pr_debug("md/raid:%s: recovering %d data-only stripes and %d data-parity stripes\n", |
Song Liu | 5aabf7c | 2016-11-17 15:24:44 -0800 | [diff] [blame] | 2265 | mdname(mddev), ctx.data_only_stripes, |
| 2266 | ctx.data_parity_stripes); |
| 2267 | |
Song Liu | a85dd7b | 2017-01-23 17:12:57 -0800 | [diff] [blame] | 2268 | if (ctx.data_only_stripes == 0) { |
| 2269 | log->next_checkpoint = ctx.pos; |
| 2270 | r5l_log_write_empty_meta_block(log, ctx.pos, ctx.seq++); |
| 2271 | ctx.pos = r5l_ring_add(log, ctx.pos, BLOCK_SECTORS); |
| 2272 | } else if (r5c_recovery_rewrite_data_only_stripes(log, &ctx)) { |
| 2273 | pr_err("md/raid:%s: failed to rewrite stripes to journal\n", |
| 2274 | mdname(mddev)); |
| 2275 | return -EIO; |
Shaohua Li | 355810d | 2015-08-13 14:32:01 -0700 | [diff] [blame] | 2276 | } |
Song Liu | b4c625c | 2016-11-17 15:24:43 -0800 | [diff] [blame] | 2277 | |
Song Liu | 5aabf7c | 2016-11-17 15:24:44 -0800 | [diff] [blame] | 2278 | log->log_start = ctx.pos; |
Song Liu | 5aabf7c | 2016-11-17 15:24:44 -0800 | [diff] [blame] | 2279 | log->seq = ctx.seq; |
JackieLiu | 43b9674 | 2016-12-05 11:58:53 +0800 | [diff] [blame] | 2280 | log->last_checkpoint = pos; |
| 2281 | r5l_write_super(log, pos); |
Song Liu | a85dd7b | 2017-01-23 17:12:57 -0800 | [diff] [blame] | 2282 | |
| 2283 | r5c_recovery_flush_data_only_stripes(log, &ctx); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2284 | return 0; |
| 2285 | } |
| 2286 | |
| 2287 | static void r5l_write_super(struct r5l_log *log, sector_t cp) |
| 2288 | { |
| 2289 | struct mddev *mddev = log->rdev->mddev; |
| 2290 | |
| 2291 | log->rdev->journal_tail = cp; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2292 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2293 | } |
| 2294 | |
Song Liu | 2c7da14 | 2016-11-17 15:24:41 -0800 | [diff] [blame] | 2295 | static ssize_t r5c_journal_mode_show(struct mddev *mddev, char *page) |
| 2296 | { |
| 2297 | struct r5conf *conf = mddev->private; |
| 2298 | int ret; |
| 2299 | |
| 2300 | if (!conf->log) |
| 2301 | return 0; |
| 2302 | |
| 2303 | switch (conf->log->r5c_journal_mode) { |
| 2304 | case R5C_JOURNAL_MODE_WRITE_THROUGH: |
| 2305 | ret = snprintf( |
| 2306 | page, PAGE_SIZE, "[%s] %s\n", |
| 2307 | r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH], |
| 2308 | r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]); |
| 2309 | break; |
| 2310 | case R5C_JOURNAL_MODE_WRITE_BACK: |
| 2311 | ret = snprintf( |
| 2312 | page, PAGE_SIZE, "%s [%s]\n", |
| 2313 | r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH], |
| 2314 | r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]); |
| 2315 | break; |
| 2316 | default: |
| 2317 | ret = 0; |
| 2318 | } |
| 2319 | return ret; |
| 2320 | } |
| 2321 | |
| 2322 | static ssize_t r5c_journal_mode_store(struct mddev *mddev, |
| 2323 | const char *page, size_t length) |
| 2324 | { |
| 2325 | struct r5conf *conf = mddev->private; |
| 2326 | struct r5l_log *log = conf->log; |
| 2327 | int val = -1, i; |
| 2328 | int len = length; |
| 2329 | |
| 2330 | if (!log) |
| 2331 | return -ENODEV; |
| 2332 | |
| 2333 | if (len && page[len - 1] == '\n') |
| 2334 | len -= 1; |
| 2335 | for (i = 0; i < ARRAY_SIZE(r5c_journal_mode_str); i++) |
| 2336 | if (strlen(r5c_journal_mode_str[i]) == len && |
| 2337 | strncmp(page, r5c_journal_mode_str[i], len) == 0) { |
| 2338 | val = i; |
| 2339 | break; |
| 2340 | } |
| 2341 | if (val < R5C_JOURNAL_MODE_WRITE_THROUGH || |
| 2342 | val > R5C_JOURNAL_MODE_WRITE_BACK) |
| 2343 | return -EINVAL; |
| 2344 | |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 2345 | if (raid5_calc_degraded(conf) > 0 && |
| 2346 | val == R5C_JOURNAL_MODE_WRITE_BACK) |
| 2347 | return -EINVAL; |
| 2348 | |
Song Liu | 2c7da14 | 2016-11-17 15:24:41 -0800 | [diff] [blame] | 2349 | mddev_suspend(mddev); |
| 2350 | conf->log->r5c_journal_mode = val; |
| 2351 | mddev_resume(mddev); |
| 2352 | |
| 2353 | pr_debug("md/raid:%s: setting r5c cache mode to %d: %s\n", |
| 2354 | mdname(mddev), val, r5c_journal_mode_str[val]); |
| 2355 | return length; |
| 2356 | } |
| 2357 | |
| 2358 | struct md_sysfs_entry |
| 2359 | r5c_journal_mode = __ATTR(journal_mode, 0644, |
| 2360 | r5c_journal_mode_show, r5c_journal_mode_store); |
| 2361 | |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 2362 | /* |
| 2363 | * Try handle write operation in caching phase. This function should only |
| 2364 | * be called in write-back mode. |
| 2365 | * |
| 2366 | * If all outstanding writes can be handled in caching phase, returns 0 |
| 2367 | * If writes requires write-out phase, call r5c_make_stripe_write_out() |
| 2368 | * and returns -EAGAIN |
| 2369 | */ |
| 2370 | int r5c_try_caching_write(struct r5conf *conf, |
| 2371 | struct stripe_head *sh, |
| 2372 | struct stripe_head_state *s, |
| 2373 | int disks) |
| 2374 | { |
| 2375 | struct r5l_log *log = conf->log; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 2376 | int i; |
| 2377 | struct r5dev *dev; |
| 2378 | int to_cache = 0; |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 2379 | void **pslot; |
| 2380 | sector_t tree_index; |
| 2381 | int ret; |
| 2382 | uintptr_t refcount; |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 2383 | |
| 2384 | BUG_ON(!r5c_is_writeback(log)); |
| 2385 | |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 2386 | if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) { |
| 2387 | /* |
| 2388 | * There are two different scenarios here: |
| 2389 | * 1. The stripe has some data cached, and it is sent to |
| 2390 | * write-out phase for reclaim |
| 2391 | * 2. The stripe is clean, and this is the first write |
| 2392 | * |
| 2393 | * For 1, return -EAGAIN, so we continue with |
| 2394 | * handle_stripe_dirtying(). |
| 2395 | * |
| 2396 | * For 2, set STRIPE_R5C_CACHING and continue with caching |
| 2397 | * write. |
| 2398 | */ |
| 2399 | |
| 2400 | /* case 1: anything injournal or anything in written */ |
| 2401 | if (s->injournal > 0 || s->written > 0) |
| 2402 | return -EAGAIN; |
| 2403 | /* case 2 */ |
| 2404 | set_bit(STRIPE_R5C_CACHING, &sh->state); |
| 2405 | } |
| 2406 | |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 2407 | /* |
| 2408 | * When run in degraded mode, array is set to write-through mode. |
| 2409 | * This check helps drain pending write safely in the transition to |
| 2410 | * write-through mode. |
| 2411 | */ |
| 2412 | if (s->failed) { |
| 2413 | r5c_make_stripe_write_out(sh); |
| 2414 | return -EAGAIN; |
| 2415 | } |
| 2416 | |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 2417 | for (i = disks; i--; ) { |
| 2418 | dev = &sh->dev[i]; |
| 2419 | /* if non-overwrite, use writing-out phase */ |
| 2420 | if (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags) && |
| 2421 | !test_bit(R5_InJournal, &dev->flags)) { |
| 2422 | r5c_make_stripe_write_out(sh); |
| 2423 | return -EAGAIN; |
| 2424 | } |
| 2425 | } |
| 2426 | |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 2427 | /* if the stripe is not counted in big_stripe_tree, add it now */ |
| 2428 | if (!test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) && |
| 2429 | !test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) { |
| 2430 | tree_index = r5c_tree_index(conf, sh->sector); |
| 2431 | spin_lock(&log->tree_lock); |
| 2432 | pslot = radix_tree_lookup_slot(&log->big_stripe_tree, |
| 2433 | tree_index); |
| 2434 | if (pslot) { |
| 2435 | refcount = (uintptr_t)radix_tree_deref_slot_protected( |
| 2436 | pslot, &log->tree_lock) >> |
| 2437 | R5C_RADIX_COUNT_SHIFT; |
| 2438 | radix_tree_replace_slot( |
| 2439 | &log->big_stripe_tree, pslot, |
| 2440 | (void *)((refcount + 1) << R5C_RADIX_COUNT_SHIFT)); |
| 2441 | } else { |
| 2442 | /* |
| 2443 | * this radix_tree_insert can fail safely, so no |
| 2444 | * need to call radix_tree_preload() |
| 2445 | */ |
| 2446 | ret = radix_tree_insert( |
| 2447 | &log->big_stripe_tree, tree_index, |
| 2448 | (void *)(1 << R5C_RADIX_COUNT_SHIFT)); |
| 2449 | if (ret) { |
| 2450 | spin_unlock(&log->tree_lock); |
| 2451 | r5c_make_stripe_write_out(sh); |
| 2452 | return -EAGAIN; |
| 2453 | } |
| 2454 | } |
| 2455 | spin_unlock(&log->tree_lock); |
| 2456 | |
| 2457 | /* |
| 2458 | * set STRIPE_R5C_PARTIAL_STRIPE, this shows the stripe is |
| 2459 | * counted in the radix tree |
| 2460 | */ |
| 2461 | set_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state); |
| 2462 | atomic_inc(&conf->r5c_cached_partial_stripes); |
| 2463 | } |
| 2464 | |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 2465 | for (i = disks; i--; ) { |
| 2466 | dev = &sh->dev[i]; |
| 2467 | if (dev->towrite) { |
| 2468 | set_bit(R5_Wantwrite, &dev->flags); |
| 2469 | set_bit(R5_Wantdrain, &dev->flags); |
| 2470 | set_bit(R5_LOCKED, &dev->flags); |
| 2471 | to_cache++; |
| 2472 | } |
| 2473 | } |
| 2474 | |
| 2475 | if (to_cache) { |
| 2476 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 2477 | /* |
| 2478 | * set STRIPE_LOG_TRAPPED, which triggers r5c_cache_data() |
| 2479 | * in ops_run_io(). STRIPE_LOG_TRAPPED will be cleared in |
| 2480 | * r5c_handle_data_cached() |
| 2481 | */ |
| 2482 | set_bit(STRIPE_LOG_TRAPPED, &sh->state); |
| 2483 | } |
| 2484 | |
| 2485 | return 0; |
| 2486 | } |
| 2487 | |
| 2488 | /* |
| 2489 | * free extra pages (orig_page) we allocated for prexor |
| 2490 | */ |
| 2491 | void r5c_release_extra_page(struct stripe_head *sh) |
| 2492 | { |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 2493 | struct r5conf *conf = sh->raid_conf; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 2494 | int i; |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 2495 | bool using_disk_info_extra_page; |
| 2496 | |
| 2497 | using_disk_info_extra_page = |
| 2498 | sh->dev[0].orig_page == conf->disks[0].extra_page; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 2499 | |
| 2500 | for (i = sh->disks; i--; ) |
| 2501 | if (sh->dev[i].page != sh->dev[i].orig_page) { |
| 2502 | struct page *p = sh->dev[i].orig_page; |
| 2503 | |
| 2504 | sh->dev[i].orig_page = sh->dev[i].page; |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 2505 | clear_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags); |
| 2506 | |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 2507 | if (!using_disk_info_extra_page) |
| 2508 | put_page(p); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 2509 | } |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 2510 | |
| 2511 | if (using_disk_info_extra_page) { |
| 2512 | clear_bit(R5C_EXTRA_PAGE_IN_USE, &conf->cache_state); |
| 2513 | md_wakeup_thread(conf->mddev->thread); |
| 2514 | } |
| 2515 | } |
| 2516 | |
| 2517 | void r5c_use_extra_page(struct stripe_head *sh) |
| 2518 | { |
| 2519 | struct r5conf *conf = sh->raid_conf; |
| 2520 | int i; |
| 2521 | struct r5dev *dev; |
| 2522 | |
| 2523 | for (i = sh->disks; i--; ) { |
| 2524 | dev = &sh->dev[i]; |
| 2525 | if (dev->orig_page != dev->page) |
| 2526 | put_page(dev->orig_page); |
| 2527 | dev->orig_page = conf->disks[i].extra_page; |
| 2528 | } |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 2529 | } |
| 2530 | |
| 2531 | /* |
| 2532 | * clean up the stripe (clear R5_InJournal for dev[pd_idx] etc.) after the |
| 2533 | * stripe is committed to RAID disks. |
| 2534 | */ |
| 2535 | void r5c_finish_stripe_write_out(struct r5conf *conf, |
| 2536 | struct stripe_head *sh, |
| 2537 | struct stripe_head_state *s) |
| 2538 | { |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 2539 | struct r5l_log *log = conf->log; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 2540 | int i; |
| 2541 | int do_wakeup = 0; |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 2542 | sector_t tree_index; |
| 2543 | void **pslot; |
| 2544 | uintptr_t refcount; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 2545 | |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 2546 | if (!log || !test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags)) |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 2547 | return; |
| 2548 | |
| 2549 | WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state)); |
| 2550 | clear_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags); |
| 2551 | |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 2552 | if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 2553 | return; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 2554 | |
| 2555 | for (i = sh->disks; i--; ) { |
| 2556 | clear_bit(R5_InJournal, &sh->dev[i].flags); |
| 2557 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2558 | do_wakeup = 1; |
| 2559 | } |
| 2560 | |
| 2561 | /* |
| 2562 | * analyse_stripe() runs before r5c_finish_stripe_write_out(), |
| 2563 | * We updated R5_InJournal, so we also update s->injournal. |
| 2564 | */ |
| 2565 | s->injournal = 0; |
| 2566 | |
| 2567 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2568 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2569 | md_wakeup_thread(conf->mddev->thread); |
| 2570 | |
| 2571 | if (do_wakeup) |
| 2572 | wake_up(&conf->wait_for_overlap); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 2573 | |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 2574 | spin_lock_irq(&log->stripe_in_journal_lock); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 2575 | list_del_init(&sh->r5c); |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 2576 | spin_unlock_irq(&log->stripe_in_journal_lock); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 2577 | sh->log_start = MaxSector; |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 2578 | |
| 2579 | atomic_dec(&log->stripe_in_journal_count); |
| 2580 | r5c_update_log_state(log); |
| 2581 | |
| 2582 | /* stop counting this stripe in big_stripe_tree */ |
| 2583 | if (test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) || |
| 2584 | test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) { |
| 2585 | tree_index = r5c_tree_index(conf, sh->sector); |
| 2586 | spin_lock(&log->tree_lock); |
| 2587 | pslot = radix_tree_lookup_slot(&log->big_stripe_tree, |
| 2588 | tree_index); |
| 2589 | BUG_ON(pslot == NULL); |
| 2590 | refcount = (uintptr_t)radix_tree_deref_slot_protected( |
| 2591 | pslot, &log->tree_lock) >> |
| 2592 | R5C_RADIX_COUNT_SHIFT; |
| 2593 | if (refcount == 1) |
| 2594 | radix_tree_delete(&log->big_stripe_tree, tree_index); |
| 2595 | else |
| 2596 | radix_tree_replace_slot( |
| 2597 | &log->big_stripe_tree, pslot, |
| 2598 | (void *)((refcount - 1) << R5C_RADIX_COUNT_SHIFT)); |
| 2599 | spin_unlock(&log->tree_lock); |
| 2600 | } |
| 2601 | |
| 2602 | if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) { |
| 2603 | BUG_ON(atomic_read(&conf->r5c_cached_partial_stripes) == 0); |
| 2604 | atomic_dec(&conf->r5c_cached_partial_stripes); |
| 2605 | } |
| 2606 | |
| 2607 | if (test_and_clear_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) { |
| 2608 | BUG_ON(atomic_read(&conf->r5c_cached_full_stripes) == 0); |
| 2609 | atomic_dec(&conf->r5c_cached_full_stripes); |
| 2610 | } |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 2611 | } |
| 2612 | |
| 2613 | int |
| 2614 | r5c_cache_data(struct r5l_log *log, struct stripe_head *sh, |
| 2615 | struct stripe_head_state *s) |
| 2616 | { |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 2617 | struct r5conf *conf = sh->raid_conf; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 2618 | int pages = 0; |
| 2619 | int reserve; |
| 2620 | int i; |
| 2621 | int ret = 0; |
| 2622 | |
| 2623 | BUG_ON(!log); |
| 2624 | |
| 2625 | for (i = 0; i < sh->disks; i++) { |
| 2626 | void *addr; |
| 2627 | |
| 2628 | if (!test_bit(R5_Wantwrite, &sh->dev[i].flags)) |
| 2629 | continue; |
| 2630 | addr = kmap_atomic(sh->dev[i].page); |
| 2631 | sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum, |
| 2632 | addr, PAGE_SIZE); |
| 2633 | kunmap_atomic(addr); |
| 2634 | pages++; |
| 2635 | } |
| 2636 | WARN_ON(pages == 0); |
| 2637 | |
| 2638 | /* |
| 2639 | * The stripe must enter state machine again to call endio, so |
| 2640 | * don't delay. |
| 2641 | */ |
| 2642 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 2643 | atomic_inc(&sh->count); |
| 2644 | |
| 2645 | mutex_lock(&log->io_mutex); |
| 2646 | /* meta + data */ |
| 2647 | reserve = (1 + pages) << (PAGE_SHIFT - 9); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 2648 | |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 2649 | if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) && |
| 2650 | sh->log_start == MaxSector) |
| 2651 | r5l_add_no_space_stripe(log, sh); |
| 2652 | else if (!r5l_has_free_space(log, reserve)) { |
| 2653 | if (sh->log_start == log->last_checkpoint) |
| 2654 | BUG(); |
| 2655 | else |
| 2656 | r5l_add_no_space_stripe(log, sh); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 2657 | } else { |
| 2658 | ret = r5l_log_stripe(log, sh, pages, 0); |
| 2659 | if (ret) { |
| 2660 | spin_lock_irq(&log->io_list_lock); |
| 2661 | list_add_tail(&sh->log_list, &log->no_mem_stripes); |
| 2662 | spin_unlock_irq(&log->io_list_lock); |
| 2663 | } |
| 2664 | } |
| 2665 | |
| 2666 | mutex_unlock(&log->io_mutex); |
| 2667 | return 0; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2668 | } |
| 2669 | |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 2670 | /* check whether this big stripe is in write back cache. */ |
| 2671 | bool r5c_big_stripe_cached(struct r5conf *conf, sector_t sect) |
| 2672 | { |
| 2673 | struct r5l_log *log = conf->log; |
| 2674 | sector_t tree_index; |
| 2675 | void *slot; |
| 2676 | |
| 2677 | if (!log) |
| 2678 | return false; |
| 2679 | |
| 2680 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 2681 | tree_index = r5c_tree_index(conf, sect); |
| 2682 | slot = radix_tree_lookup(&log->big_stripe_tree, tree_index); |
| 2683 | return slot != NULL; |
| 2684 | } |
| 2685 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2686 | static int r5l_load_log(struct r5l_log *log) |
| 2687 | { |
| 2688 | struct md_rdev *rdev = log->rdev; |
| 2689 | struct page *page; |
| 2690 | struct r5l_meta_block *mb; |
| 2691 | sector_t cp = log->rdev->journal_tail; |
| 2692 | u32 stored_crc, expected_crc; |
| 2693 | bool create_super = false; |
JackieLiu | d30dfeb | 2016-12-08 08:47:39 +0800 | [diff] [blame] | 2694 | int ret = 0; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2695 | |
| 2696 | /* Make sure it's valid */ |
| 2697 | if (cp >= rdev->sectors || round_down(cp, BLOCK_SECTORS) != cp) |
| 2698 | cp = 0; |
| 2699 | page = alloc_page(GFP_KERNEL); |
| 2700 | if (!page) |
| 2701 | return -ENOMEM; |
| 2702 | |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 2703 | if (!sync_page_io(rdev, cp, PAGE_SIZE, page, REQ_OP_READ, 0, false)) { |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2704 | ret = -EIO; |
| 2705 | goto ioerr; |
| 2706 | } |
| 2707 | mb = page_address(page); |
| 2708 | |
| 2709 | if (le32_to_cpu(mb->magic) != R5LOG_MAGIC || |
| 2710 | mb->version != R5LOG_VERSION) { |
| 2711 | create_super = true; |
| 2712 | goto create; |
| 2713 | } |
| 2714 | stored_crc = le32_to_cpu(mb->checksum); |
| 2715 | mb->checksum = 0; |
Shaohua Li | 5cb2fbd | 2015-10-28 08:41:25 -0700 | [diff] [blame] | 2716 | expected_crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2717 | if (stored_crc != expected_crc) { |
| 2718 | create_super = true; |
| 2719 | goto create; |
| 2720 | } |
| 2721 | if (le64_to_cpu(mb->position) != cp) { |
| 2722 | create_super = true; |
| 2723 | goto create; |
| 2724 | } |
| 2725 | create: |
| 2726 | if (create_super) { |
| 2727 | log->last_cp_seq = prandom_u32(); |
| 2728 | cp = 0; |
Zhengyuan Liu | 56056c2 | 2016-10-24 16:15:59 +0800 | [diff] [blame] | 2729 | r5l_log_write_empty_meta_block(log, cp, log->last_cp_seq); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2730 | /* |
| 2731 | * Make sure super points to correct address. Log might have |
| 2732 | * data very soon. If super hasn't correct log tail address, |
| 2733 | * recovery can't find the log |
| 2734 | */ |
| 2735 | r5l_write_super(log, cp); |
| 2736 | } else |
| 2737 | log->last_cp_seq = le64_to_cpu(mb->seq); |
| 2738 | |
| 2739 | log->device_size = round_down(rdev->sectors, BLOCK_SECTORS); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 2740 | log->max_free_space = log->device_size >> RECLAIM_MAX_FREE_SPACE_SHIFT; |
| 2741 | if (log->max_free_space > RECLAIM_MAX_FREE_SPACE) |
| 2742 | log->max_free_space = RECLAIM_MAX_FREE_SPACE; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2743 | log->last_checkpoint = cp; |
| 2744 | |
| 2745 | __free_page(page); |
| 2746 | |
JackieLiu | d30dfeb | 2016-12-08 08:47:39 +0800 | [diff] [blame] | 2747 | if (create_super) { |
| 2748 | log->log_start = r5l_ring_add(log, cp, BLOCK_SECTORS); |
| 2749 | log->seq = log->last_cp_seq + 1; |
| 2750 | log->next_checkpoint = cp; |
| 2751 | } else |
| 2752 | ret = r5l_recovery_log(log); |
| 2753 | |
Zhengyuan Liu | 3d7e7e1 | 2016-12-04 16:49:44 +0800 | [diff] [blame] | 2754 | r5c_update_log_state(log); |
| 2755 | return ret; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2756 | ioerr: |
| 2757 | __free_page(page); |
| 2758 | return ret; |
| 2759 | } |
| 2760 | |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 2761 | void r5c_update_on_rdev_error(struct mddev *mddev) |
| 2762 | { |
| 2763 | struct r5conf *conf = mddev->private; |
| 2764 | struct r5l_log *log = conf->log; |
| 2765 | |
| 2766 | if (!log) |
| 2767 | return; |
| 2768 | |
| 2769 | if (raid5_calc_degraded(conf) > 0 && |
| 2770 | conf->log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK) |
| 2771 | schedule_work(&log->disable_writeback_work); |
| 2772 | } |
| 2773 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2774 | int r5l_init_log(struct r5conf *conf, struct md_rdev *rdev) |
| 2775 | { |
Jens Axboe | c888a8f9 | 2016-04-13 13:33:19 -0600 | [diff] [blame] | 2776 | struct request_queue *q = bdev_get_queue(rdev->bdev); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2777 | struct r5l_log *log; |
| 2778 | |
| 2779 | if (PAGE_SIZE != 4096) |
| 2780 | return -EINVAL; |
Song Liu | c757ec9 | 2016-11-17 15:24:36 -0800 | [diff] [blame] | 2781 | |
| 2782 | /* |
| 2783 | * The PAGE_SIZE must be big enough to hold 1 r5l_meta_block and |
| 2784 | * raid_disks r5l_payload_data_parity. |
| 2785 | * |
| 2786 | * Write journal and cache does not work for very big array |
| 2787 | * (raid_disks > 203) |
| 2788 | */ |
| 2789 | if (sizeof(struct r5l_meta_block) + |
| 2790 | ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32)) * |
| 2791 | conf->raid_disks) > PAGE_SIZE) { |
| 2792 | pr_err("md/raid:%s: write journal/cache doesn't work for array with %d disks\n", |
| 2793 | mdname(conf->mddev), conf->raid_disks); |
| 2794 | return -EINVAL; |
| 2795 | } |
| 2796 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2797 | log = kzalloc(sizeof(*log), GFP_KERNEL); |
| 2798 | if (!log) |
| 2799 | return -ENOMEM; |
| 2800 | log->rdev = rdev; |
| 2801 | |
Jens Axboe | c888a8f9 | 2016-04-13 13:33:19 -0600 | [diff] [blame] | 2802 | log->need_cache_flush = test_bit(QUEUE_FLAG_WC, &q->queue_flags) != 0; |
Christoph Hellwig | 56fef7c | 2015-10-05 09:31:09 +0200 | [diff] [blame] | 2803 | |
Shaohua Li | 5cb2fbd | 2015-10-28 08:41:25 -0700 | [diff] [blame] | 2804 | log->uuid_checksum = crc32c_le(~0, rdev->mddev->uuid, |
| 2805 | sizeof(rdev->mddev->uuid)); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2806 | |
| 2807 | mutex_init(&log->io_mutex); |
| 2808 | |
| 2809 | spin_lock_init(&log->io_list_lock); |
| 2810 | INIT_LIST_HEAD(&log->running_ios); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 2811 | INIT_LIST_HEAD(&log->io_end_ios); |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 2812 | INIT_LIST_HEAD(&log->flushing_ios); |
Christoph Hellwig | 04732f7 | 2015-10-05 09:31:07 +0200 | [diff] [blame] | 2813 | INIT_LIST_HEAD(&log->finished_ios); |
Ming Lei | 3a83f46 | 2016-11-22 08:57:21 -0700 | [diff] [blame] | 2814 | bio_init(&log->flush_bio, NULL, 0); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2815 | |
| 2816 | log->io_kc = KMEM_CACHE(r5l_io_unit, 0); |
| 2817 | if (!log->io_kc) |
| 2818 | goto io_kc; |
| 2819 | |
Christoph Hellwig | 5036c390 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2820 | log->io_pool = mempool_create_slab_pool(R5L_POOL_SIZE, log->io_kc); |
| 2821 | if (!log->io_pool) |
| 2822 | goto io_pool; |
| 2823 | |
Christoph Hellwig | c38d29b | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2824 | log->bs = bioset_create(R5L_POOL_SIZE, 0); |
| 2825 | if (!log->bs) |
| 2826 | goto io_bs; |
| 2827 | |
Christoph Hellwig | e8deb63 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2828 | log->meta_pool = mempool_create_page_pool(R5L_POOL_SIZE, 0); |
| 2829 | if (!log->meta_pool) |
| 2830 | goto out_mempool; |
| 2831 | |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 2832 | spin_lock_init(&log->tree_lock); |
| 2833 | INIT_RADIX_TREE(&log->big_stripe_tree, GFP_NOWAIT | __GFP_NOWARN); |
| 2834 | |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 2835 | log->reclaim_thread = md_register_thread(r5l_reclaim_thread, |
| 2836 | log->rdev->mddev, "reclaim"); |
| 2837 | if (!log->reclaim_thread) |
| 2838 | goto reclaim_thread; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 2839 | log->reclaim_thread->timeout = R5C_RECLAIM_WAKEUP_INTERVAL; |
| 2840 | |
Shaohua Li | 0fd22b4 | 2015-09-02 13:49:47 -0700 | [diff] [blame] | 2841 | init_waitqueue_head(&log->iounit_wait); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 2842 | |
Christoph Hellwig | 5036c390 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2843 | INIT_LIST_HEAD(&log->no_mem_stripes); |
| 2844 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2845 | INIT_LIST_HEAD(&log->no_space_stripes); |
| 2846 | spin_lock_init(&log->no_space_stripes_lock); |
| 2847 | |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 2848 | INIT_WORK(&log->deferred_io_work, r5l_submit_io_async); |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 2849 | INIT_WORK(&log->disable_writeback_work, r5c_disable_writeback_async); |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 2850 | |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 2851 | log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 2852 | INIT_LIST_HEAD(&log->stripe_in_journal_list); |
| 2853 | spin_lock_init(&log->stripe_in_journal_lock); |
| 2854 | atomic_set(&log->stripe_in_journal_count, 0); |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 2855 | |
Song Liu | d2250f1 | 2016-12-14 15:38:02 -0800 | [diff] [blame] | 2856 | rcu_assign_pointer(conf->log, log); |
| 2857 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2858 | if (r5l_load_log(log)) |
| 2859 | goto error; |
| 2860 | |
Shaohua Li | a62ab49 | 2016-01-06 14:37:13 -0800 | [diff] [blame] | 2861 | set_bit(MD_HAS_JOURNAL, &conf->mddev->flags); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2862 | return 0; |
Christoph Hellwig | e8deb63 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2863 | |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2864 | error: |
Song Liu | d2250f1 | 2016-12-14 15:38:02 -0800 | [diff] [blame] | 2865 | rcu_assign_pointer(conf->log, NULL); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 2866 | md_unregister_thread(&log->reclaim_thread); |
| 2867 | reclaim_thread: |
Christoph Hellwig | e8deb63 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2868 | mempool_destroy(log->meta_pool); |
| 2869 | out_mempool: |
Christoph Hellwig | c38d29b | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2870 | bioset_free(log->bs); |
| 2871 | io_bs: |
Christoph Hellwig | 5036c390 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2872 | mempool_destroy(log->io_pool); |
| 2873 | io_pool: |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2874 | kmem_cache_destroy(log->io_kc); |
| 2875 | io_kc: |
| 2876 | kfree(log); |
| 2877 | return -EINVAL; |
| 2878 | } |
| 2879 | |
| 2880 | void r5l_exit_log(struct r5l_log *log) |
| 2881 | { |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 2882 | flush_work(&log->disable_writeback_work); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 2883 | md_unregister_thread(&log->reclaim_thread); |
Christoph Hellwig | e8deb63 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2884 | mempool_destroy(log->meta_pool); |
Christoph Hellwig | c38d29b | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2885 | bioset_free(log->bs); |
Christoph Hellwig | 5036c390 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2886 | mempool_destroy(log->io_pool); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 2887 | kmem_cache_destroy(log->io_kc); |
| 2888 | kfree(log); |
| 2889 | } |