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