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