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