| 1 | |
| 2 | |
| 3 | * raid1.c : Multiple Devices driver for Linux
|
| 4 | *
|
| 5 | * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
|
| 6 | *
|
| 7 | * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
|
| 8 | *
|
| 9 | * RAID-1 management functions.
|
| 10 | *
|
| 11 | * Better read-balancing code written by Mika Kuoppala <[email protected]>, 2000
|
| 12 | *
|
| 13 | * Fixes to reconstruction by Jakob Østergaard" <[email protected]>
|
| 14 | * Various fixes by Neil Brown <[email protected]>
|
| 15 | *
|
| 16 | * Changes by Peter T. Breuer <[email protected]> 31/1/2003 to support
|
| 17 | * bitmapped intelligence in resync:
|
| 18 | *
|
| 19 | * - bitmap marked during normal i/o
|
| 20 | * - bitmap used to skip nondirty blocks during sync
|
| 21 | *
|
| 22 | * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology:
|
| 23 | * - persistent bitmap code
|
| 24 |
|
| 25 |
|
| 26 | #include <linux/slab.h>
|
| 27 | #include <linux/delay.h>
|
| 28 | #include <linux/blkdev.h>
|
| 29 | #include <linux/module.h>
|
| 30 | #include <linux/seq_file.h>
|
| 31 | #include <linux/ratelimit.h>
|
| 32 | #include <linux/interval_tree_generic.h>
|
| 33 |
|
| 34 | #include <trace/events/block.h>
|
| 35 |
|
| 36 | #include "md.h"
|
| 37 | #include "raid1.h"
|
| 38 | #include "md-bitmap.h"
|
| 39 | #include "md-cluster.h"
|
| 40 |
|
| 41 | #define UNSUPPORTED_MDDEV_FLAGS \
|
| 42 | ((1L << MD_HAS_JOURNAL) | \
|
| 43 | (1L << MD_JOURNAL_CLEAN) | \
|
| 44 | (1L << MD_HAS_PPL) | \
|
| 45 | (1L << MD_HAS_MULTIPLE_PPLS))
|
| 46 |
|
| 47 | static void allow_barrier(struct r1conf *conf, sector_t sector_nr);
|
| 48 | static void lower_barrier(struct r1conf *conf, sector_t sector_nr);
|
| 49 | static void raid1_free(struct mddev *mddev, void *priv);
|
| 50 |
|
| 51 | #define RAID_1_10_NAME "raid1"
|
| 52 | #include "raid1-10.c"
|
| 53 |
|
| 54 | #define START(node) ((node)->start)
|
| 55 | #define LAST(node) ((node)->last)
|
| 56 | INTERVAL_TREE_DEFINE(struct serial_info, node, sector_t, _subtree_last,
|
| 57 | START, LAST, static inline, raid1_rb);
|
| 58 |
|
| 59 | static int check_and_add_serial(struct md_rdev *rdev, struct r1bio *r1_bio,
|
| 60 | struct serial_info *si)
|
| 61 | {
|
| 62 | unsigned long flags;
|
| 63 | int ret = 0;
|
| 64 | sector_t lo = r1_bio->sector;
|
| 65 | sector_t hi = lo + r1_bio->sectors - 1;
|
| 66 | int idx = sector_to_idx(r1_bio->sector);
|
| 67 | struct serial_in_rdev *serial = &rdev->serial[idx];
|
| 68 | struct serial_info *head_si;
|
| 69 |
|
| 70 | spin_lock_irqsave(&serial->serial_lock, flags);
|
| 71 |
|
| 72 | head_si = raid1_rb_iter_first(&serial->serial_rb, lo, hi);
|
| 73 | if (head_si && head_si != si) {
|
| 74 | si->start = lo;
|
| 75 | si->last = hi;
|
| 76 | si->wnode_start = head_si->wnode_start;
|
| 77 | list_add_tail(&si->list_node, &head_si->waiters);
|
| 78 | ret = -EBUSY;
|
| 79 | } else if (!head_si) {
|
| 80 | si->start = lo;
|
| 81 | si->last = hi;
|
| 82 | si->wnode_start = si->start;
|
| 83 | raid1_rb_insert(si, &serial->serial_rb);
|
| 84 | }
|
| 85 | spin_unlock_irqrestore(&serial->serial_lock, flags);
|
| 86 |
|
| 87 | return ret;
|
| 88 | }
|
| 89 |
|
| 90 | static void wait_for_serialization(struct md_rdev *rdev, struct r1bio *r1_bio)
|
| 91 | {
|
| 92 | struct mddev *mddev = rdev->mddev;
|
| 93 | struct serial_info *si;
|
| 94 |
|
| 95 | if (WARN_ON(!mddev->serial_info_pool))
|
| 96 | return;
|
| 97 | si = mempool_alloc(mddev->serial_info_pool, GFP_NOIO);
|
| 98 | INIT_LIST_HEAD(&si->waiters);
|
| 99 | INIT_LIST_HEAD(&si->list_node);
|
| 100 | init_completion(&si->ready);
|
| 101 | while (check_and_add_serial(rdev, r1_bio, si)) {
|
| 102 | wait_for_completion(&si->ready);
|
| 103 | reinit_completion(&si->ready);
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | static void remove_serial(struct md_rdev *rdev, sector_t lo, sector_t hi)
|
| 108 | {
|
| 109 | struct serial_info *si, *iter_si;
|
| 110 | unsigned long flags;
|
| 111 | int found = 0;
|
| 112 | struct mddev *mddev = rdev->mddev;
|
| 113 | int idx = sector_to_idx(lo);
|
| 114 | struct serial_in_rdev *serial = &rdev->serial[idx];
|
| 115 |
|
| 116 | spin_lock_irqsave(&serial->serial_lock, flags);
|
| 117 | for (si = raid1_rb_iter_first(&serial->serial_rb, lo, hi);
|
| 118 | si; si = raid1_rb_iter_next(si, lo, hi)) {
|
| 119 | if (si->start == lo && si->last == hi) {
|
| 120 | found = 1;
|
| 121 | break;
|
| 122 | }
|
| 123 | }
|
| 124 | if (found) {
|
| 125 | raid1_rb_remove(si, &serial->serial_rb);
|
| 126 | if (!list_empty(&si->waiters)) {
|
| 127 | list_for_each_entry(iter_si, &si->waiters, list_node) {
|
| 128 | if (iter_si->wnode_start == si->wnode_start) {
|
| 129 | list_del_init(&iter_si->list_node);
|
| 130 | list_splice_init(&si->waiters, &iter_si->waiters);
|
| 131 | raid1_rb_insert(iter_si, &serial->serial_rb);
|
| 132 | complete(&iter_si->ready);
|
| 133 | break;
|
| 134 | }
|
| 135 | }
|
| 136 | }
|
| 137 | mempool_free(si, mddev->serial_info_pool);
|
| 138 | } else {
|
| 139 | WARN(1, "The write IO is not recorded for serialization\n");
|
| 140 | }
|
| 141 | spin_unlock_irqrestore(&serial->serial_lock, flags);
|
| 142 | }
|
| 143 |
|
| 144 | |
| 145 | * for resync bio, r1bio pointer can be retrieved from the per-bio
|
| 146 | * 'struct resync_pages'.
|
| 147 |
|
| 148 | static inline struct r1bio *get_resync_r1bio(struct bio *bio)
|
| 149 | {
|
| 150 | return get_resync_pages(bio)->raid_bio;
|
| 151 | }
|
| 152 |
|
| 153 | static void *r1bio_pool_alloc(gfp_t gfp_flags, struct r1conf *conf)
|
| 154 | {
|
| 155 | int size = offsetof(struct r1bio, bios[conf->raid_disks * 2]);
|
| 156 |
|
| 157 |
|
| 158 | return kzalloc(size, gfp_flags);
|
| 159 | }
|
| 160 |
|
| 161 | #define RESYNC_DEPTH 32
|
| 162 | #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
|
| 163 | #define RESYNC_WINDOW (RESYNC_BLOCK_SIZE * RESYNC_DEPTH)
|
| 164 | #define RESYNC_WINDOW_SECTORS (RESYNC_WINDOW >> 9)
|
| 165 | #define CLUSTER_RESYNC_WINDOW (16 * RESYNC_WINDOW)
|
| 166 | #define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
|
| 167 |
|
| 168 | static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
|
| 169 | {
|
| 170 | struct r1conf *conf = data;
|
| 171 | struct r1bio *r1_bio;
|
| 172 | struct bio *bio;
|
| 173 | int need_pages;
|
| 174 | int j;
|
| 175 | struct resync_pages *rps;
|
| 176 |
|
| 177 | r1_bio = r1bio_pool_alloc(gfp_flags, conf);
|
| 178 | if (!r1_bio)
|
| 179 | return NULL;
|
| 180 |
|
| 181 | rps = kmalloc_objs(struct resync_pages, conf->raid_disks * 2, gfp_flags);
|
| 182 | if (!rps)
|
| 183 | goto out_free_r1bio;
|
| 184 |
|
| 185 | |
| 186 | * Allocate bios : 1 for reading, n-1 for writing
|
| 187 |
|
| 188 | for (j = conf->raid_disks * 2; j-- ; ) {
|
| 189 | bio = bio_kmalloc(RESYNC_PAGES, gfp_flags);
|
| 190 | if (!bio)
|
| 191 | goto out_free_bio;
|
| 192 | bio_init_inline(bio, NULL, RESYNC_PAGES, 0);
|
| 193 | r1_bio->bios[j] = bio;
|
| 194 | }
|
| 195 | |
| 196 | * Allocate RESYNC_PAGES data pages and attach them to
|
| 197 | * the first bio.
|
| 198 | * If this is a user-requested check/repair, allocate
|
| 199 | * RESYNC_PAGES for each bio.
|
| 200 |
|
| 201 | if (test_bit(MD_RECOVERY_REQUESTED, &conf->mddev->recovery))
|
| 202 | need_pages = conf->raid_disks * 2;
|
| 203 | else
|
| 204 | need_pages = 1;
|
| 205 | for (j = 0; j < conf->raid_disks * 2; j++) {
|
| 206 | struct resync_pages *rp = &rps[j];
|
| 207 |
|
| 208 | bio = r1_bio->bios[j];
|
| 209 |
|
| 210 | if (j < need_pages) {
|
| 211 | if (resync_alloc_pages(rp, gfp_flags))
|
| 212 | goto out_free_pages;
|
| 213 | } else {
|
| 214 | memcpy(rp, &rps[0], sizeof(*rp));
|
| 215 | resync_get_all_pages(rp);
|
| 216 | }
|
| 217 |
|
| 218 | rp->raid_bio = r1_bio;
|
| 219 | bio->bi_private = rp;
|
| 220 | }
|
| 221 |
|
| 222 | r1_bio->master_bio = NULL;
|
| 223 |
|
| 224 | return r1_bio;
|
| 225 |
|
| 226 | out_free_pages:
|
| 227 | while (--j >= 0)
|
| 228 | resync_free_pages(&rps[j]);
|
| 229 |
|
| 230 | out_free_bio:
|
| 231 | while (++j < conf->raid_disks * 2) {
|
| 232 | bio_uninit(r1_bio->bios[j]);
|
| 233 | kfree(r1_bio->bios[j]);
|
| 234 | }
|
| 235 | kfree(rps);
|
| 236 |
|
| 237 | out_free_r1bio:
|
| 238 | rbio_pool_free(r1_bio, data);
|
| 239 | return NULL;
|
| 240 | }
|
| 241 |
|
| 242 | static void r1buf_pool_free(void *__r1_bio, void *data)
|
| 243 | {
|
| 244 | struct r1conf *conf = data;
|
| 245 | int i;
|
| 246 | struct r1bio *r1bio = __r1_bio;
|
| 247 | struct resync_pages *rp = NULL;
|
| 248 |
|
| 249 | for (i = conf->raid_disks * 2; i--; ) {
|
| 250 | rp = get_resync_pages(r1bio->bios[i]);
|
| 251 | resync_free_pages(rp);
|
| 252 | bio_uninit(r1bio->bios[i]);
|
| 253 | kfree(r1bio->bios[i]);
|
| 254 | }
|
| 255 |
|
| 256 |
|
| 257 | kfree(rp);
|
| 258 |
|
| 259 | rbio_pool_free(r1bio, data);
|
| 260 | }
|
| 261 |
|
| 262 | static void put_all_bios(struct r1conf *conf, struct r1bio *r1_bio)
|
| 263 | {
|
| 264 | int i;
|
| 265 |
|
| 266 | for (i = 0; i < conf->raid_disks * 2; i++) {
|
| 267 | struct bio **bio = r1_bio->bios + i;
|
| 268 | if (!BIO_SPECIAL(*bio))
|
| 269 | bio_put(*bio);
|
| 270 | *bio = NULL;
|
| 271 | }
|
| 272 | }
|
| 273 |
|
| 274 | static void free_r1bio(struct r1bio *r1_bio)
|
| 275 | {
|
| 276 | struct r1conf *conf = r1_bio->mddev->private;
|
| 277 |
|
| 278 | put_all_bios(conf, r1_bio);
|
| 279 | mempool_free(r1_bio, conf->r1bio_pool);
|
| 280 | }
|
| 281 |
|
| 282 | static void put_buf(struct r1bio *r1_bio)
|
| 283 | {
|
| 284 | struct r1conf *conf = r1_bio->mddev->private;
|
| 285 | sector_t sect = r1_bio->sector;
|
| 286 | int i;
|
| 287 |
|
| 288 | for (i = 0; i < conf->raid_disks * 2; i++) {
|
| 289 | struct bio *bio = r1_bio->bios[i];
|
| 290 | if (bio->bi_end_io)
|
| 291 | rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
|
| 292 | }
|
| 293 |
|
| 294 | mempool_free(r1_bio, &conf->r1buf_pool);
|
| 295 |
|
| 296 | lower_barrier(conf, sect);
|
| 297 | }
|
| 298 |
|
| 299 | static void reschedule_retry(struct r1bio *r1_bio)
|
| 300 | {
|
| 301 | unsigned long flags;
|
| 302 | struct mddev *mddev = r1_bio->mddev;
|
| 303 | struct r1conf *conf = mddev->private;
|
| 304 | int idx;
|
| 305 |
|
| 306 | idx = sector_to_idx(r1_bio->sector);
|
| 307 | spin_lock_irqsave(&conf->device_lock, flags);
|
| 308 | list_add(&r1_bio->retry_list, &conf->retry_list);
|
| 309 | atomic_inc(&conf->nr_queued[idx]);
|
| 310 | spin_unlock_irqrestore(&conf->device_lock, flags);
|
| 311 |
|
| 312 | wake_up(&conf->wait_barrier);
|
| 313 | md_wakeup_thread(mddev->thread);
|
| 314 | }
|
| 315 |
|
| 316 | |
| 317 | * raid_end_bio_io() is called when we have finished servicing a mirrored
|
| 318 | * operation and are ready to return a success/failure code to the buffer
|
| 319 | * cache layer.
|
| 320 |
|
| 321 | static void call_bio_endio(struct r1bio *r1_bio)
|
| 322 | {
|
| 323 | struct bio *bio = r1_bio->master_bio;
|
| 324 |
|
| 325 | if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
|
| 326 | bio->bi_status = BLK_STS_IOERR;
|
| 327 |
|
| 328 | bio_endio(bio);
|
| 329 | }
|
| 330 |
|
| 331 | static void raid_end_bio_io(struct r1bio *r1_bio)
|
| 332 | {
|
| 333 | struct bio *bio = r1_bio->master_bio;
|
| 334 | struct r1conf *conf = r1_bio->mddev->private;
|
| 335 | sector_t sector = r1_bio->sector;
|
| 336 |
|
| 337 |
|
| 338 | if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
|
| 339 | pr_debug("raid1: sync end %s on sectors %llu-%llu\n",
|
| 340 | (bio_data_dir(bio) == WRITE) ? "write" : "read",
|
| 341 | (unsigned long long) bio->bi_iter.bi_sector,
|
| 342 | (unsigned long long) bio_end_sector(bio) - 1);
|
| 343 |
|
| 344 | call_bio_endio(r1_bio);
|
| 345 | }
|
| 346 |
|
| 347 | free_r1bio(r1_bio);
|
| 348 | |
| 349 | * Wake up any possible resync thread that waits for the device
|
| 350 | * to go idle. All I/Os, even write-behind writes, are done.
|
| 351 |
|
| 352 | allow_barrier(conf, sector);
|
| 353 | }
|
| 354 |
|
| 355 | |
| 356 | * Update disk head position estimator based on IRQ completion info.
|
| 357 |
|
| 358 | static inline void update_head_pos(int disk, struct r1bio *r1_bio)
|
| 359 | {
|
| 360 | struct r1conf *conf = r1_bio->mddev->private;
|
| 361 |
|
| 362 | WRITE_ONCE(conf->mirrors[disk].head_position,
|
| 363 | r1_bio->sector + r1_bio->sectors);
|
| 364 | }
|
| 365 |
|
| 366 | |
| 367 | * Find the disk number which triggered given bio
|
| 368 |
|
| 369 | static int find_bio_disk(struct r1bio *r1_bio, struct bio *bio)
|
| 370 | {
|
| 371 | int mirror;
|
| 372 | struct r1conf *conf = r1_bio->mddev->private;
|
| 373 | int raid_disks = conf->raid_disks;
|
| 374 |
|
| 375 | for (mirror = 0; mirror < raid_disks * 2; mirror++)
|
| 376 | if (r1_bio->bios[mirror] == bio)
|
| 377 | break;
|
| 378 |
|
| 379 | BUG_ON(mirror == raid_disks * 2);
|
| 380 | update_head_pos(mirror, r1_bio);
|
| 381 |
|
| 382 | return mirror;
|
| 383 | }
|
| 384 |
|
| 385 | static void raid1_end_read_request(struct bio *bio)
|
| 386 | {
|
| 387 | int uptodate = !bio->bi_status;
|
| 388 | struct r1bio *r1_bio = bio->bi_private;
|
| 389 | struct r1conf *conf = r1_bio->mddev->private;
|
| 390 | struct md_rdev *rdev = conf->mirrors[r1_bio->read_disk].rdev;
|
| 391 |
|
| 392 | |
| 393 | * this branch is our 'one mirror IO has finished' event handler:
|
| 394 |
|
| 395 | update_head_pos(r1_bio->read_disk, r1_bio);
|
| 396 |
|
| 397 | if (uptodate) {
|
| 398 | set_bit(R1BIO_Uptodate, &r1_bio->state);
|
| 399 | } else if (test_bit(FailFast, &rdev->flags) &&
|
| 400 | test_bit(R1BIO_FailFast, &r1_bio->state)) {
|
| 401 | |
| 402 | * want to retry
|
| 403 | ;
|
| 404 | } else if (!raid1_should_handle_error(bio)) {
|
| 405 | uptodate = 1;
|
| 406 | } else {
|
| 407 | |
| 408 | * the error upwards rather than fail the last device.
|
| 409 | * Here we redefine "uptodate" to mean "Don't want to retry"
|
| 410 |
|
| 411 | unsigned long flags;
|
| 412 | spin_lock_irqsave(&conf->device_lock, flags);
|
| 413 | if (r1_bio->mddev->degraded == conf->raid_disks ||
|
| 414 | (r1_bio->mddev->degraded == conf->raid_disks-1 &&
|
| 415 | test_bit(In_sync, &rdev->flags)))
|
| 416 | uptodate = 1;
|
| 417 | spin_unlock_irqrestore(&conf->device_lock, flags);
|
| 418 | }
|
| 419 |
|
| 420 | if (uptodate) {
|
| 421 | raid_end_bio_io(r1_bio);
|
| 422 | rdev_dec_pending(rdev, conf->mddev);
|
| 423 | } else {
|
| 424 | |
| 425 | * oops, read error:
|
| 426 |
|
| 427 | pr_err_ratelimited("md/raid1:%s: %pg: rescheduling sector %llu\n",
|
| 428 | mdname(conf->mddev),
|
| 429 | rdev->bdev,
|
| 430 | (unsigned long long)r1_bio->sector);
|
| 431 | set_bit(R1BIO_ReadError, &r1_bio->state);
|
| 432 | reschedule_retry(r1_bio);
|
| 433 |
|
| 434 | }
|
| 435 | }
|
| 436 |
|
| 437 | static void close_write(struct r1bio *r1_bio)
|
| 438 | {
|
| 439 | struct mddev *mddev = r1_bio->mddev;
|
| 440 |
|
| 441 |
|
| 442 | if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
|
| 443 | bio_free_pages(r1_bio->behind_master_bio);
|
| 444 | bio_put(r1_bio->behind_master_bio);
|
| 445 | r1_bio->behind_master_bio = NULL;
|
| 446 | }
|
| 447 |
|
| 448 | if (test_bit(R1BIO_BehindIO, &r1_bio->state))
|
| 449 | mddev->bitmap_ops->end_behind_write(mddev);
|
| 450 | md_write_end(mddev);
|
| 451 | }
|
| 452 |
|
| 453 | static void r1_bio_write_done(struct r1bio *r1_bio)
|
| 454 | {
|
| 455 | if (!atomic_dec_and_test(&r1_bio->remaining))
|
| 456 | return;
|
| 457 |
|
| 458 | if (test_bit(R1BIO_WriteError, &r1_bio->state))
|
| 459 | reschedule_retry(r1_bio);
|
| 460 | else {
|
| 461 | close_write(r1_bio);
|
| 462 | if (test_bit(R1BIO_MadeGood, &r1_bio->state))
|
| 463 | reschedule_retry(r1_bio);
|
| 464 | else
|
| 465 | raid_end_bio_io(r1_bio);
|
| 466 | }
|
| 467 | }
|
| 468 |
|
| 469 | static void raid1_end_write_request(struct bio *bio)
|
| 470 | {
|
| 471 | struct r1bio *r1_bio = bio->bi_private;
|
| 472 | int behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
|
| 473 | struct r1conf *conf = r1_bio->mddev->private;
|
| 474 | struct bio *to_put = NULL;
|
| 475 | int mirror = find_bio_disk(r1_bio, bio);
|
| 476 | struct md_rdev *rdev = conf->mirrors[mirror].rdev;
|
| 477 | sector_t lo = r1_bio->sector;
|
| 478 | sector_t hi = r1_bio->sector + r1_bio->sectors - 1;
|
| 479 | bool ignore_error = !raid1_should_handle_error(bio) ||
|
| 480 | (bio->bi_status && bio_op(bio) == REQ_OP_DISCARD);
|
| 481 |
|
| 482 | |
| 483 | * 'one mirror IO has finished' event handler:
|
| 484 |
|
| 485 | if (bio->bi_status && !ignore_error) {
|
| 486 | set_bit(WriteErrorSeen, &rdev->flags);
|
| 487 | if (!test_and_set_bit(WantReplacement, &rdev->flags))
|
| 488 | set_bit(MD_RECOVERY_NEEDED, &
|
| 489 | conf->mddev->recovery);
|
| 490 |
|
| 491 | if (test_bit(FailFast, &rdev->flags) &&
|
| 492 | (bio->bi_opf & MD_FAILFAST) &&
|
| 493 |
|
| 494 | !test_bit(WriteMostly, &rdev->flags)) {
|
| 495 | md_error(r1_bio->mddev, rdev);
|
| 496 | }
|
| 497 |
|
| 498 | |
| 499 | * When the device is faulty, it is not necessary to
|
| 500 | * handle write error.
|
| 501 |
|
| 502 | if (!test_bit(Faulty, &rdev->flags))
|
| 503 | set_bit(R1BIO_WriteError, &r1_bio->state);
|
| 504 | else {
|
| 505 |
|
| 506 | r1_bio->bios[mirror] = NULL;
|
| 507 | to_put = bio;
|
| 508 | }
|
| 509 | } else {
|
| 510 | |
| 511 | * Set R1BIO_Uptodate in our master bio, so that we
|
| 512 | * will return a good error code for to the higher
|
| 513 | * levels even if IO on some other mirrored buffer
|
| 514 | * fails.
|
| 515 | *
|
| 516 | * The 'master' represents the composite IO operation
|
| 517 | * to user-side. So if something waits for IO, then it
|
| 518 | * will wait for the 'master' bio.
|
| 519 |
|
| 520 | r1_bio->bios[mirror] = NULL;
|
| 521 | to_put = bio;
|
| 522 | |
| 523 | * Do not set R1BIO_Uptodate if the current device is
|
| 524 | * rebuilding or Faulty. This is because we cannot use
|
| 525 | * such device for properly reading the data back (we could
|
| 526 | * potentially use it, if the current write would have felt
|
| 527 | * before rdev->recovery_offset, but for simplicity we don't
|
| 528 | * check this here.
|
| 529 |
|
| 530 | if (test_bit(In_sync, &rdev->flags) &&
|
| 531 | !test_bit(Faulty, &rdev->flags))
|
| 532 | set_bit(R1BIO_Uptodate, &r1_bio->state);
|
| 533 |
|
| 534 |
|
| 535 | if (rdev_has_badblock(rdev, r1_bio->sector, r1_bio->sectors) &&
|
| 536 | !ignore_error) {
|
| 537 | r1_bio->bios[mirror] = IO_MADE_GOOD;
|
| 538 | set_bit(R1BIO_MadeGood, &r1_bio->state);
|
| 539 | }
|
| 540 | }
|
| 541 |
|
| 542 | if (behind) {
|
| 543 | if (test_bit(CollisionCheck, &rdev->flags))
|
| 544 | remove_serial(rdev, lo, hi);
|
| 545 | if (test_bit(WriteMostly, &rdev->flags))
|
| 546 | atomic_dec(&r1_bio->behind_remaining);
|
| 547 |
|
| 548 | |
| 549 | * In behind mode, we ACK the master bio once the I/O
|
| 550 | * has safely reached all non-writemostly
|
| 551 | * disks. Setting the Returned bit ensures that this
|
| 552 | * gets done only once -- we don't ever want to return
|
| 553 | * -EIO here, instead we'll wait
|
| 554 |
|
| 555 | if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
|
| 556 | test_bit(R1BIO_Uptodate, &r1_bio->state)) {
|
| 557 |
|
| 558 | if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
|
| 559 | struct bio *mbio = r1_bio->master_bio;
|
| 560 | pr_debug("raid1: behind end write sectors"
|
| 561 | " %llu-%llu\n",
|
| 562 | (unsigned long long) mbio->bi_iter.bi_sector,
|
| 563 | (unsigned long long) bio_end_sector(mbio) - 1);
|
| 564 | call_bio_endio(r1_bio);
|
| 565 | }
|
| 566 | }
|
| 567 | } else if (test_bit(MD_SERIALIZE_POLICY, &rdev->mddev->flags))
|
| 568 | remove_serial(rdev, lo, hi);
|
| 569 | if (r1_bio->bios[mirror] == NULL)
|
| 570 | rdev_dec_pending(rdev, conf->mddev);
|
| 571 |
|
| 572 | |
| 573 | * Let's see if all mirrored write operations have finished
|
| 574 | * already.
|
| 575 |
|
| 576 | r1_bio_write_done(r1_bio);
|
| 577 |
|
| 578 | if (to_put)
|
| 579 | bio_put(to_put);
|
| 580 | }
|
| 581 |
|
| 582 | static sector_t align_to_barrier_unit_end(sector_t start_sector,
|
| 583 | sector_t sectors)
|
| 584 | {
|
| 585 | sector_t len;
|
| 586 |
|
| 587 | WARN_ON(sectors == 0);
|
| 588 | |
| 589 | * len is the number of sectors from start_sector to end of the
|
| 590 | * barrier unit which start_sector belongs to.
|
| 591 |
|
| 592 | len = round_up(start_sector + 1, BARRIER_UNIT_SECTOR_SIZE) -
|
| 593 | start_sector;
|
| 594 |
|
| 595 | if (len > sectors)
|
| 596 | len = sectors;
|
| 597 |
|
| 598 | return len;
|
| 599 | }
|
| 600 |
|
| 601 | static void update_read_sectors(struct r1conf *conf, int disk,
|
| 602 | sector_t this_sector, int len)
|
| 603 | {
|
| 604 | struct raid1_info *info = &conf->mirrors[disk];
|
| 605 |
|
| 606 | atomic_inc(&info->rdev->nr_pending);
|
| 607 | if (READ_ONCE(info->next_seq_sect) != this_sector)
|
| 608 | WRITE_ONCE(info->seq_start, this_sector);
|
| 609 | WRITE_ONCE(info->next_seq_sect, this_sector + len);
|
| 610 | }
|
| 611 |
|
| 612 | static int choose_first_rdev(struct r1conf *conf, struct r1bio *r1_bio,
|
| 613 | int *max_sectors)
|
| 614 | {
|
| 615 | sector_t this_sector = r1_bio->sector;
|
| 616 | int len = r1_bio->sectors;
|
| 617 | int disk;
|
| 618 |
|
| 619 | for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
|
| 620 | struct md_rdev *rdev;
|
| 621 | int read_len;
|
| 622 |
|
| 623 | if (r1_bio->bios[disk] == IO_BLOCKED)
|
| 624 | continue;
|
| 625 |
|
| 626 | rdev = conf->mirrors[disk].rdev;
|
| 627 | if (!rdev || test_bit(Faulty, &rdev->flags))
|
| 628 | continue;
|
| 629 |
|
| 630 |
|
| 631 | read_len = raid1_check_read_range(rdev, this_sector, &len);
|
| 632 | if (read_len > 0) {
|
| 633 | update_read_sectors(conf, disk, this_sector, read_len);
|
| 634 | *max_sectors = read_len;
|
| 635 | return disk;
|
| 636 | }
|
| 637 | }
|
| 638 |
|
| 639 | return -1;
|
| 640 | }
|
| 641 |
|
| 642 | static bool rdev_in_recovery(struct md_rdev *rdev, struct r1bio *r1_bio)
|
| 643 | {
|
| 644 | return !test_bit(In_sync, &rdev->flags) &&
|
| 645 | rdev->recovery_offset < r1_bio->sector + r1_bio->sectors;
|
| 646 | }
|
| 647 |
|
| 648 | static int choose_bb_rdev(struct r1conf *conf, struct r1bio *r1_bio,
|
| 649 | int *max_sectors)
|
| 650 | {
|
| 651 | sector_t this_sector = r1_bio->sector;
|
| 652 | int best_disk = -1;
|
| 653 | int best_len = 0;
|
| 654 | int disk;
|
| 655 |
|
| 656 | for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
|
| 657 | struct md_rdev *rdev;
|
| 658 | int len;
|
| 659 | int read_len;
|
| 660 |
|
| 661 | if (r1_bio->bios[disk] == IO_BLOCKED)
|
| 662 | continue;
|
| 663 |
|
| 664 | rdev = conf->mirrors[disk].rdev;
|
| 665 | if (!rdev || test_bit(Faulty, &rdev->flags) ||
|
| 666 | rdev_in_recovery(rdev, r1_bio) ||
|
| 667 | test_bit(WriteMostly, &rdev->flags))
|
| 668 | continue;
|
| 669 |
|
| 670 |
|
| 671 | len = r1_bio->sectors;
|
| 672 | read_len = raid1_check_read_range(rdev, this_sector, &len);
|
| 673 | if (read_len > best_len) {
|
| 674 | best_disk = disk;
|
| 675 | best_len = read_len;
|
| 676 | }
|
| 677 | }
|
| 678 |
|
| 679 | if (best_disk != -1) {
|
| 680 | *max_sectors = best_len;
|
| 681 | update_read_sectors(conf, best_disk, this_sector, best_len);
|
| 682 | }
|
| 683 |
|
| 684 | return best_disk;
|
| 685 | }
|
| 686 |
|
| 687 | static int choose_slow_rdev(struct r1conf *conf, struct r1bio *r1_bio,
|
| 688 | int *max_sectors)
|
| 689 | {
|
| 690 | sector_t this_sector = r1_bio->sector;
|
| 691 | int bb_disk = -1;
|
| 692 | int bb_read_len = 0;
|
| 693 | int disk;
|
| 694 |
|
| 695 | for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
|
| 696 | struct md_rdev *rdev;
|
| 697 | int len;
|
| 698 | int read_len;
|
| 699 |
|
| 700 | if (r1_bio->bios[disk] == IO_BLOCKED)
|
| 701 | continue;
|
| 702 |
|
| 703 | rdev = conf->mirrors[disk].rdev;
|
| 704 | if (!rdev || test_bit(Faulty, &rdev->flags) ||
|
| 705 | !test_bit(WriteMostly, &rdev->flags) ||
|
| 706 | rdev_in_recovery(rdev, r1_bio))
|
| 707 | continue;
|
| 708 |
|
| 709 |
|
| 710 | len = r1_bio->sectors;
|
| 711 | read_len = raid1_check_read_range(rdev, this_sector, &len);
|
| 712 | if (read_len == r1_bio->sectors) {
|
| 713 | *max_sectors = read_len;
|
| 714 | update_read_sectors(conf, disk, this_sector, read_len);
|
| 715 | return disk;
|
| 716 | }
|
| 717 |
|
| 718 | |
| 719 | * there are partial bad blocks, choose the rdev with largest
|
| 720 | * read length.
|
| 721 |
|
| 722 | if (read_len > bb_read_len) {
|
| 723 | bb_disk = disk;
|
| 724 | bb_read_len = read_len;
|
| 725 | }
|
| 726 | }
|
| 727 |
|
| 728 | if (bb_disk != -1) {
|
| 729 | *max_sectors = bb_read_len;
|
| 730 | update_read_sectors(conf, bb_disk, this_sector, bb_read_len);
|
| 731 | }
|
| 732 |
|
| 733 | return bb_disk;
|
| 734 | }
|
| 735 |
|
| 736 | static bool is_sequential(struct r1conf *conf, int disk, struct r1bio *r1_bio)
|
| 737 | {
|
| 738 | return READ_ONCE(conf->mirrors[disk].next_seq_sect) == r1_bio->sector ||
|
| 739 | READ_ONCE(conf->mirrors[disk].head_position) == r1_bio->sector;
|
| 740 | }
|
| 741 |
|
| 742 | |
| 743 | * If buffered sequential IO size exceeds optimal iosize, check if there is idle
|
| 744 | * disk. If yes, choose the idle disk.
|
| 745 |
|
| 746 | static bool should_choose_next(struct r1conf *conf, int disk)
|
| 747 | {
|
| 748 | struct raid1_info *mirror = &conf->mirrors[disk];
|
| 749 | sector_t seq_start, next_seq_sect;
|
| 750 | int opt_iosize;
|
| 751 |
|
| 752 | if (!test_bit(Nonrot, &mirror->rdev->flags))
|
| 753 | return false;
|
| 754 |
|
| 755 | opt_iosize = bdev_io_opt(mirror->rdev->bdev) >> 9;
|
| 756 | seq_start = READ_ONCE(mirror->seq_start);
|
| 757 | next_seq_sect = READ_ONCE(mirror->next_seq_sect);
|
| 758 | return opt_iosize > 0 && seq_start != MaxSector &&
|
| 759 | next_seq_sect > opt_iosize &&
|
| 760 | next_seq_sect - opt_iosize >= seq_start;
|
| 761 | }
|
| 762 |
|
| 763 | static bool rdev_readable(struct md_rdev *rdev, struct r1bio *r1_bio)
|
| 764 | {
|
| 765 | if (!rdev || test_bit(Faulty, &rdev->flags))
|
| 766 | return false;
|
| 767 |
|
| 768 | if (rdev_in_recovery(rdev, r1_bio))
|
| 769 | return false;
|
| 770 |
|
| 771 |
|
| 772 | if (test_bit(WriteMostly, &rdev->flags))
|
| 773 | return false;
|
| 774 |
|
| 775 |
|
| 776 | if (rdev_has_badblock(rdev, r1_bio->sector, r1_bio->sectors))
|
| 777 | return false;
|
| 778 |
|
| 779 | return true;
|
| 780 | }
|
| 781 |
|
| 782 | struct read_balance_ctl {
|
| 783 | sector_t closest_dist;
|
| 784 | int closest_dist_disk;
|
| 785 | int min_pending;
|
| 786 | int min_pending_disk;
|
| 787 | int sequential_disk;
|
| 788 | int readable_disks;
|
| 789 | };
|
| 790 |
|
| 791 | static int choose_best_rdev(struct r1conf *conf, struct r1bio *r1_bio)
|
| 792 | {
|
| 793 | int disk;
|
| 794 | struct read_balance_ctl ctl = {
|
| 795 | .closest_dist_disk = -1,
|
| 796 | .closest_dist = MaxSector,
|
| 797 | .min_pending_disk = -1,
|
| 798 | .min_pending = UINT_MAX,
|
| 799 | .sequential_disk = -1,
|
| 800 | };
|
| 801 |
|
| 802 | for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
|
| 803 | struct md_rdev *rdev;
|
| 804 | sector_t dist;
|
| 805 | unsigned int pending;
|
| 806 |
|
| 807 | if (r1_bio->bios[disk] == IO_BLOCKED)
|
| 808 | continue;
|
| 809 |
|
| 810 | rdev = conf->mirrors[disk].rdev;
|
| 811 | if (!rdev_readable(rdev, r1_bio))
|
| 812 | continue;
|
| 813 |
|
| 814 |
|
| 815 | if (ctl.readable_disks++ == 1)
|
| 816 | set_bit(R1BIO_FailFast, &r1_bio->state);
|
| 817 |
|
| 818 | pending = atomic_read(&rdev->nr_pending);
|
| 819 | dist = abs(r1_bio->sector -
|
| 820 | READ_ONCE(conf->mirrors[disk].head_position));
|
| 821 |
|
| 822 |
|
| 823 | if (is_sequential(conf, disk, r1_bio)) {
|
| 824 | if (!should_choose_next(conf, disk))
|
| 825 | return disk;
|
| 826 |
|
| 827 | |
| 828 | * Add 'pending' to avoid choosing this disk if
|
| 829 | * there is other idle disk.
|
| 830 |
|
| 831 | pending++;
|
| 832 | |
| 833 | * If there is no other idle disk, this disk
|
| 834 | * will be chosen.
|
| 835 |
|
| 836 | ctl.sequential_disk = disk;
|
| 837 | }
|
| 838 |
|
| 839 | if (ctl.min_pending > pending) {
|
| 840 | ctl.min_pending = pending;
|
| 841 | ctl.min_pending_disk = disk;
|
| 842 | }
|
| 843 |
|
| 844 | if (ctl.closest_dist > dist) {
|
| 845 | ctl.closest_dist = dist;
|
| 846 | ctl.closest_dist_disk = disk;
|
| 847 | }
|
| 848 | }
|
| 849 |
|
| 850 | |
| 851 | * sequential IO size exceeds optimal iosize, however, there is no other
|
| 852 | * idle disk, so choose the sequential disk.
|
| 853 |
|
| 854 | if (ctl.sequential_disk != -1 && ctl.min_pending != 0)
|
| 855 | return ctl.sequential_disk;
|
| 856 |
|
| 857 | |
| 858 | * If all disks are rotational, choose the closest disk. If any disk is
|
| 859 | * non-rotational, choose the disk with less pending request even the
|
| 860 | * disk is rotational, which might/might not be optimal for raids with
|
| 861 | * mixed ratation/non-rotational disks depending on workload.
|
| 862 |
|
| 863 | if (ctl.min_pending_disk != -1 &&
|
| 864 | (READ_ONCE(conf->nonrot_disks) || ctl.min_pending == 0))
|
| 865 | return ctl.min_pending_disk;
|
| 866 | else
|
| 867 | return ctl.closest_dist_disk;
|
| 868 | }
|
| 869 |
|
| 870 | |
| 871 | * This routine returns the disk from which the requested read should be done.
|
| 872 | *
|
| 873 | * 1) If resync is in progress, find the first usable disk and use it even if it
|
| 874 | * has some bad blocks.
|
| 875 | *
|
| 876 | * 2) Now that there is no resync, loop through all disks and skipping slow
|
| 877 | * disks and disks with bad blocks for now. Only pay attention to key disk
|
| 878 | * choice.
|
| 879 | *
|
| 880 | * 3) If we've made it this far, now look for disks with bad blocks and choose
|
| 881 | * the one with most number of sectors.
|
| 882 | *
|
| 883 | * 4) If we are all the way at the end, we have no choice but to use a disk even
|
| 884 | * if it is write mostly.
|
| 885 | *
|
| 886 | * The rdev for the device selected will have nr_pending incremented.
|
| 887 |
|
| 888 | static int read_balance(struct r1conf *conf, struct r1bio *r1_bio,
|
| 889 | int *max_sectors)
|
| 890 | {
|
| 891 | int disk;
|
| 892 |
|
| 893 | clear_bit(R1BIO_FailFast, &r1_bio->state);
|
| 894 |
|
| 895 | if (raid1_should_read_first(conf->mddev, r1_bio->sector,
|
| 896 | r1_bio->sectors))
|
| 897 | return choose_first_rdev(conf, r1_bio, max_sectors);
|
| 898 |
|
| 899 | disk = choose_best_rdev(conf, r1_bio);
|
| 900 | if (disk >= 0) {
|
| 901 | *max_sectors = r1_bio->sectors;
|
| 902 | update_read_sectors(conf, disk, r1_bio->sector,
|
| 903 | r1_bio->sectors);
|
| 904 | return disk;
|
| 905 | }
|
| 906 |
|
| 907 | |
| 908 | * If we are here it means we didn't find a perfectly good disk so
|
| 909 | * now spend a bit more time trying to find one with the most good
|
| 910 | * sectors.
|
| 911 |
|
| 912 | disk = choose_bb_rdev(conf, r1_bio, max_sectors);
|
| 913 | if (disk >= 0)
|
| 914 | return disk;
|
| 915 |
|
| 916 | return choose_slow_rdev(conf, r1_bio, max_sectors);
|
| 917 | }
|
| 918 |
|
| 919 | static void wake_up_barrier(struct r1conf *conf)
|
| 920 | {
|
| 921 | if (wq_has_sleeper(&conf->wait_barrier))
|
| 922 | wake_up(&conf->wait_barrier);
|
| 923 | }
|
| 924 |
|
| 925 | static void flush_bio_list(struct r1conf *conf, struct bio *bio)
|
| 926 | {
|
| 927 |
|
| 928 | raid1_prepare_flush_writes(conf->mddev);
|
| 929 | wake_up_barrier(conf);
|
| 930 |
|
| 931 | while (bio) {
|
| 932 | struct bio *next = bio->bi_next;
|
| 933 |
|
| 934 | raid1_submit_write(bio);
|
| 935 | bio = next;
|
| 936 | cond_resched();
|
| 937 | }
|
| 938 | }
|
| 939 |
|
| 940 | static void flush_pending_writes(struct r1conf *conf)
|
| 941 | {
|
| 942 | |
| 943 | * bitmap updates get flushed here.
|
| 944 |
|
| 945 | spin_lock_irq(&conf->device_lock);
|
| 946 |
|
| 947 | if (conf->pending_bio_list.head) {
|
| 948 | struct blk_plug plug;
|
| 949 | struct bio *bio;
|
| 950 |
|
| 951 | bio = bio_list_get(&conf->pending_bio_list);
|
| 952 | spin_unlock_irq(&conf->device_lock);
|
| 953 |
|
| 954 | |
| 955 | * As this is called in a wait_event() loop (see freeze_array),
|
| 956 | * current->state might be TASK_UNINTERRUPTIBLE which will
|
| 957 | * cause a warning when we prepare to wait again. As it is
|
| 958 | * rare that this path is taken, it is perfectly safe to force
|
| 959 | * us to go around the wait_event() loop again, so the warning
|
| 960 | * is a false-positive. Silence the warning by resetting
|
| 961 | * thread state
|
| 962 |
|
| 963 | __set_current_state(TASK_RUNNING);
|
| 964 | blk_start_plug(&plug);
|
| 965 | flush_bio_list(conf, bio);
|
| 966 | blk_finish_plug(&plug);
|
| 967 | } else
|
| 968 | spin_unlock_irq(&conf->device_lock);
|
| 969 | }
|
| 970 |
|
| 971 | |
| 972 | * Sometimes we need to suspend IO while we do something else,
|
| 973 | * either some resync/recovery, or reconfigure the array.
|
| 974 | * To do this we raise a 'barrier'.
|
| 975 | * The 'barrier' is a counter that can be raised multiple times
|
| 976 | * to count how many activities are happening which preclude
|
| 977 | * normal IO.
|
| 978 | * We can only raise the barrier if there is no pending IO.
|
| 979 | * i.e. if nr_pending == 0.
|
| 980 | * We choose only to raise the barrier if no-one is waiting for the
|
| 981 | * barrier to go down. This means that as soon as an IO request
|
| 982 | * is ready, no other operations which require a barrier will start
|
| 983 | * until the IO request has had a chance.
|
| 984 | *
|
| 985 | * So: regular IO calls 'wait_barrier'. When that returns there
|
| 986 | * is no backgroup IO happening, It must arrange to call
|
| 987 | * allow_barrier when it has finished its IO.
|
| 988 | * backgroup IO calls must call raise_barrier. Once that returns
|
| 989 | * there is no normal IO happeing. It must arrange to call
|
| 990 | * lower_barrier when the particular background IO completes.
|
| 991 | *
|
| 992 | * If resync/recovery is interrupted, returns -EINTR;
|
| 993 | * Otherwise, returns 0.
|
| 994 |
|
| 995 | static int raise_barrier(struct r1conf *conf, sector_t sector_nr)
|
| 996 | {
|
| 997 | int idx = sector_to_idx(sector_nr);
|
| 998 |
|
| 999 | spin_lock_irq(&conf->resync_lock);
|
| 1000 |
|
| 1001 |
|
| 1002 | wait_event_lock_irq(conf->wait_barrier,
|
| 1003 | !atomic_read(&conf->nr_waiting[idx]),
|
| 1004 | conf->resync_lock);
|
| 1005 |
|
| 1006 |
|
| 1007 | atomic_inc(&conf->barrier[idx]);
|
| 1008 | |
| 1009 | * In raise_barrier() we firstly increase conf->barrier[idx] then
|
| 1010 | * check conf->nr_pending[idx]. In _wait_barrier() we firstly
|
| 1011 | * increase conf->nr_pending[idx] then check conf->barrier[idx].
|
| 1012 | * A memory barrier here to make sure conf->nr_pending[idx] won't
|
| 1013 | * be fetched before conf->barrier[idx] is increased. Otherwise
|
| 1014 | * there will be a race between raise_barrier() and _wait_barrier().
|
| 1015 |
|
| 1016 | smp_mb__after_atomic();
|
| 1017 |
|
| 1018 | |
| 1019 | * A: while the array is in frozen state
|
| 1020 | * B: while conf->nr_pending[idx] is not 0, meaning regular I/O
|
| 1021 | * existing in corresponding I/O barrier bucket.
|
| 1022 | * C: while conf->barrier[idx] >= RESYNC_DEPTH, meaning reaches
|
| 1023 | * max resync count which allowed on current I/O barrier bucket.
|
| 1024 |
|
| 1025 | wait_event_lock_irq(conf->wait_barrier,
|
| 1026 | (!conf->array_frozen &&
|
| 1027 | !atomic_read(&conf->nr_pending[idx]) &&
|
| 1028 | atomic_read(&conf->barrier[idx]) < RESYNC_DEPTH) ||
|
| 1029 | test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery),
|
| 1030 | conf->resync_lock);
|
| 1031 |
|
| 1032 | if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
|
| 1033 | atomic_dec(&conf->barrier[idx]);
|
| 1034 | spin_unlock_irq(&conf->resync_lock);
|
| 1035 | wake_up(&conf->wait_barrier);
|
| 1036 | return -EINTR;
|
| 1037 | }
|
| 1038 |
|
| 1039 | atomic_inc(&conf->nr_sync_pending);
|
| 1040 | spin_unlock_irq(&conf->resync_lock);
|
| 1041 |
|
| 1042 | return 0;
|
| 1043 | }
|
| 1044 |
|
| 1045 | static void lower_barrier(struct r1conf *conf, sector_t sector_nr)
|
| 1046 | {
|
| 1047 | int idx = sector_to_idx(sector_nr);
|
| 1048 |
|
| 1049 | BUG_ON(atomic_read(&conf->barrier[idx]) <= 0);
|
| 1050 |
|
| 1051 | atomic_dec(&conf->barrier[idx]);
|
| 1052 | atomic_dec(&conf->nr_sync_pending);
|
| 1053 | wake_up(&conf->wait_barrier);
|
| 1054 | }
|
| 1055 |
|
| 1056 | static void _wait_barrier(struct r1conf *conf, int idx)
|
| 1057 | {
|
| 1058 | |
| 1059 | * We need to increase conf->nr_pending[idx] very early here,
|
| 1060 | * then raise_barrier() can be blocked when it waits for
|
| 1061 | * conf->nr_pending[idx] to be 0. Then we can avoid holding
|
| 1062 | * conf->resync_lock when there is no barrier raised in same
|
| 1063 | * barrier unit bucket. Also if the array is frozen, I/O
|
| 1064 | * should be blocked until array is unfrozen.
|
| 1065 |
|
| 1066 | atomic_inc(&conf->nr_pending[idx]);
|
| 1067 | |
| 1068 | * In _wait_barrier() we firstly increase conf->nr_pending[idx], then
|
| 1069 | * check conf->barrier[idx]. In raise_barrier() we firstly increase
|
| 1070 | * conf->barrier[idx], then check conf->nr_pending[idx]. A memory
|
| 1071 | * barrier is necessary here to make sure conf->barrier[idx] won't be
|
| 1072 | * fetched before conf->nr_pending[idx] is increased. Otherwise there
|
| 1073 | * will be a race between _wait_barrier() and raise_barrier().
|
| 1074 |
|
| 1075 | smp_mb__after_atomic();
|
| 1076 |
|
| 1077 | |
| 1078 | * Don't worry about checking two atomic_t variables at same time
|
| 1079 | * here. If during we check conf->barrier[idx], the array is
|
| 1080 | * frozen (conf->array_frozen is 1), and chonf->barrier[idx] is
|
| 1081 | * 0, it is safe to return and make the I/O continue. Because the
|
| 1082 | * array is frozen, all I/O returned here will eventually complete
|
| 1083 | * or be queued, no race will happen. See code comment in
|
| 1084 | * frozen_array().
|
| 1085 |
|
| 1086 | if (!READ_ONCE(conf->array_frozen) &&
|
| 1087 | !atomic_read(&conf->barrier[idx]))
|
| 1088 | return;
|
| 1089 |
|
| 1090 | |
| 1091 | * After holding conf->resync_lock, conf->nr_pending[idx]
|
| 1092 | * should be decreased before waiting for barrier to drop.
|
| 1093 | * Otherwise, we may encounter a race condition because
|
| 1094 | * raise_barrer() might be waiting for conf->nr_pending[idx]
|
| 1095 | * to be 0 at same time.
|
| 1096 |
|
| 1097 | spin_lock_irq(&conf->resync_lock);
|
| 1098 | atomic_inc(&conf->nr_waiting[idx]);
|
| 1099 | atomic_dec(&conf->nr_pending[idx]);
|
| 1100 | |
| 1101 | * In case freeze_array() is waiting for
|
| 1102 | * get_unqueued_pending() == extra
|
| 1103 |
|
| 1104 | wake_up_barrier(conf);
|
| 1105 |
|
| 1106 |
|
| 1107 | wait_event_lock_irq(conf->wait_barrier, !conf->array_frozen &&
|
| 1108 | !atomic_read(&conf->barrier[idx]),
|
| 1109 | conf->resync_lock);
|
| 1110 |
|
| 1111 | atomic_inc(&conf->nr_pending[idx]);
|
| 1112 | atomic_dec(&conf->nr_waiting[idx]);
|
| 1113 | spin_unlock_irq(&conf->resync_lock);
|
| 1114 | }
|
| 1115 |
|
| 1116 | static void wait_read_barrier(struct r1conf *conf, sector_t sector_nr)
|
| 1117 | {
|
| 1118 | int idx = sector_to_idx(sector_nr);
|
| 1119 |
|
| 1120 | |
| 1121 | * Very similar to _wait_barrier(). The difference is, for read
|
| 1122 | * I/O we don't need wait for sync I/O, but if the whole array
|
| 1123 | * is frozen, the read I/O still has to wait until the array is
|
| 1124 | * unfrozen. Since there is no ordering requirement with
|
| 1125 | * conf->barrier[idx] here, memory barrier is unnecessary as well.
|
| 1126 |
|
| 1127 | atomic_inc(&conf->nr_pending[idx]);
|
| 1128 |
|
| 1129 | if (!READ_ONCE(conf->array_frozen))
|
| 1130 | return;
|
| 1131 |
|
| 1132 | spin_lock_irq(&conf->resync_lock);
|
| 1133 | atomic_inc(&conf->nr_waiting[idx]);
|
| 1134 | atomic_dec(&conf->nr_pending[idx]);
|
| 1135 | |
| 1136 | * In case freeze_array() is waiting for
|
| 1137 | * get_unqueued_pending() == extra
|
| 1138 |
|
| 1139 | wake_up_barrier(conf);
|
| 1140 |
|
| 1141 |
|
| 1142 | wait_event_lock_irq(conf->wait_barrier, !conf->array_frozen,
|
| 1143 | conf->resync_lock);
|
| 1144 |
|
| 1145 | atomic_inc(&conf->nr_pending[idx]);
|
| 1146 | atomic_dec(&conf->nr_waiting[idx]);
|
| 1147 | spin_unlock_irq(&conf->resync_lock);
|
| 1148 | }
|
| 1149 |
|
| 1150 | static void wait_barrier(struct r1conf *conf, sector_t sector_nr)
|
| 1151 | {
|
| 1152 | int idx = sector_to_idx(sector_nr);
|
| 1153 |
|
| 1154 | _wait_barrier(conf, idx);
|
| 1155 | }
|
| 1156 |
|
| 1157 | static void _allow_barrier(struct r1conf *conf, int idx)
|
| 1158 | {
|
| 1159 | atomic_dec(&conf->nr_pending[idx]);
|
| 1160 | wake_up_barrier(conf);
|
| 1161 | }
|
| 1162 |
|
| 1163 | static void allow_barrier(struct r1conf *conf, sector_t sector_nr)
|
| 1164 | {
|
| 1165 | int idx = sector_to_idx(sector_nr);
|
| 1166 |
|
| 1167 | _allow_barrier(conf, idx);
|
| 1168 | }
|
| 1169 |
|
| 1170 |
|
| 1171 | static int get_unqueued_pending(struct r1conf *conf)
|
| 1172 | {
|
| 1173 | int idx, ret;
|
| 1174 |
|
| 1175 | ret = atomic_read(&conf->nr_sync_pending);
|
| 1176 | for (idx = 0; idx < BARRIER_BUCKETS_NR; idx++)
|
| 1177 | ret += atomic_read(&conf->nr_pending[idx]) -
|
| 1178 | atomic_read(&conf->nr_queued[idx]);
|
| 1179 |
|
| 1180 | return ret;
|
| 1181 | }
|
| 1182 |
|
| 1183 | static void freeze_array(struct r1conf *conf, int extra)
|
| 1184 | {
|
| 1185 | |
| 1186 | * go quiet.
|
| 1187 | * This is called in two situations:
|
| 1188 | * 1) management command handlers (reshape, remove disk, quiesce).
|
| 1189 | * 2) one normal I/O request failed.
|
| 1190 |
|
| 1191 | * After array_frozen is set to 1, new sync IO will be blocked at
|
| 1192 | * raise_barrier(), and new normal I/O will blocked at _wait_barrier()
|
| 1193 | * or wait_read_barrier(). The flying I/Os will either complete or be
|
| 1194 | * queued. When everything goes quite, there are only queued I/Os left.
|
| 1195 |
|
| 1196 | * Every flying I/O contributes to a conf->nr_pending[idx], idx is the
|
| 1197 | * barrier bucket index which this I/O request hits. When all sync and
|
| 1198 | * normal I/O are queued, sum of all conf->nr_pending[] will match sum
|
| 1199 | * of all conf->nr_queued[]. But normal I/O failure is an exception,
|
| 1200 | * in handle_read_error(), we may call freeze_array() before trying to
|
| 1201 | * fix the read error. In this case, the error read I/O is not queued,
|
| 1202 | * so get_unqueued_pending() == 1.
|
| 1203 | *
|
| 1204 | * Therefore before this function returns, we need to wait until
|
| 1205 | * get_unqueued_pendings(conf) gets equal to extra. For
|
| 1206 | * normal I/O context, extra is 1, in rested situations extra is 0.
|
| 1207 |
|
| 1208 | spin_lock_irq(&conf->resync_lock);
|
| 1209 | conf->array_frozen = 1;
|
| 1210 | mddev_add_trace_msg(conf->mddev, "raid1 wait freeze");
|
| 1211 | wait_event_lock_irq_cmd(
|
| 1212 | conf->wait_barrier,
|
| 1213 | get_unqueued_pending(conf) == extra,
|
| 1214 | conf->resync_lock,
|
| 1215 | flush_pending_writes(conf));
|
| 1216 | spin_unlock_irq(&conf->resync_lock);
|
| 1217 | }
|
| 1218 | static void unfreeze_array(struct r1conf *conf)
|
| 1219 | {
|
| 1220 |
|
| 1221 | spin_lock_irq(&conf->resync_lock);
|
| 1222 | conf->array_frozen = 0;
|
| 1223 | spin_unlock_irq(&conf->resync_lock);
|
| 1224 | wake_up(&conf->wait_barrier);
|
| 1225 | }
|
| 1226 |
|
| 1227 | static void alloc_behind_master_bio(struct r1bio *r1_bio,
|
| 1228 | struct bio *bio)
|
| 1229 | {
|
| 1230 | int size = bio->bi_iter.bi_size;
|
| 1231 | unsigned vcnt = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
|
| 1232 | int i = 0;
|
| 1233 | struct bio *behind_bio = NULL;
|
| 1234 |
|
| 1235 | behind_bio = bio_alloc_bioset(NULL, vcnt, bio->bi_opf, GFP_NOIO,
|
| 1236 | &r1_bio->mddev->bio_set);
|
| 1237 |
|
| 1238 |
|
| 1239 | if (!bio_has_data(bio)) {
|
| 1240 | behind_bio->bi_iter.bi_size = size;
|
| 1241 | goto skip_copy;
|
| 1242 | }
|
| 1243 |
|
| 1244 | while (i < vcnt && size) {
|
| 1245 | struct page *page;
|
| 1246 | int len = min_t(int, PAGE_SIZE, size);
|
| 1247 |
|
| 1248 | page = alloc_page(GFP_NOIO);
|
| 1249 | if (unlikely(!page))
|
| 1250 | goto free_pages;
|
| 1251 |
|
| 1252 | if (!bio_add_page(behind_bio, page, len, 0)) {
|
| 1253 | put_page(page);
|
| 1254 | goto free_pages;
|
| 1255 | }
|
| 1256 |
|
| 1257 | size -= len;
|
| 1258 | i++;
|
| 1259 | }
|
| 1260 |
|
| 1261 | bio_copy_data(behind_bio, bio);
|
| 1262 | skip_copy:
|
| 1263 | r1_bio->behind_master_bio = behind_bio;
|
| 1264 | set_bit(R1BIO_BehindIO, &r1_bio->state);
|
| 1265 |
|
| 1266 | return;
|
| 1267 |
|
| 1268 | free_pages:
|
| 1269 | pr_debug("%dB behind alloc failed, doing sync I/O\n",
|
| 1270 | bio->bi_iter.bi_size);
|
| 1271 | bio_free_pages(behind_bio);
|
| 1272 | bio_put(behind_bio);
|
| 1273 | }
|
| 1274 |
|
| 1275 | static void raid1_unplug(struct blk_plug_cb *cb, bool from_schedule)
|
| 1276 | {
|
| 1277 | struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb,
|
| 1278 | cb);
|
| 1279 | struct mddev *mddev = plug->cb.data;
|
| 1280 | struct r1conf *conf = mddev->private;
|
| 1281 | struct bio *bio;
|
| 1282 |
|
| 1283 | if (from_schedule) {
|
| 1284 | spin_lock_irq(&conf->device_lock);
|
| 1285 | bio_list_merge(&conf->pending_bio_list, &plug->pending);
|
| 1286 | spin_unlock_irq(&conf->device_lock);
|
| 1287 | wake_up_barrier(conf);
|
| 1288 | md_wakeup_thread(mddev->thread);
|
| 1289 | kfree(plug);
|
| 1290 | return;
|
| 1291 | }
|
| 1292 |
|
| 1293 |
|
| 1294 | bio = bio_list_get(&plug->pending);
|
| 1295 | flush_bio_list(conf, bio);
|
| 1296 | kfree(plug);
|
| 1297 | }
|
| 1298 |
|
| 1299 | static void init_r1bio(struct r1bio *r1_bio, struct mddev *mddev, struct bio *bio)
|
| 1300 | {
|
| 1301 | r1_bio->master_bio = bio;
|
| 1302 | r1_bio->sectors = bio_sectors(bio);
|
| 1303 | r1_bio->state = 0;
|
| 1304 | r1_bio->mddev = mddev;
|
| 1305 | r1_bio->sector = bio->bi_iter.bi_sector;
|
| 1306 | }
|
| 1307 |
|
| 1308 | static inline struct r1bio *
|
| 1309 | alloc_r1bio(struct mddev *mddev, struct bio *bio)
|
| 1310 | {
|
| 1311 | struct r1conf *conf = mddev->private;
|
| 1312 | struct r1bio *r1_bio;
|
| 1313 |
|
| 1314 | r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
|
| 1315 | memset(r1_bio, 0, offsetof(struct r1bio, bios[conf->raid_disks * 2]));
|
| 1316 | init_r1bio(r1_bio, mddev, bio);
|
| 1317 | return r1_bio;
|
| 1318 | }
|
| 1319 |
|
| 1320 | static void raid1_read_request(struct mddev *mddev, struct bio *bio,
|
| 1321 | int max_read_sectors, struct r1bio *r1_bio)
|
| 1322 | {
|
| 1323 | struct r1conf *conf = mddev->private;
|
| 1324 | struct raid1_info *mirror;
|
| 1325 | struct bio *read_bio;
|
| 1326 | int max_sectors;
|
| 1327 | int rdisk;
|
| 1328 | bool r1bio_existed = !!r1_bio;
|
| 1329 |
|
| 1330 | |
| 1331 | * An md cloned bio indicates we are in the error path.
|
| 1332 | * This is more reliable than checking r1_bio, which might
|
| 1333 | * be NULL even in the error path if a failed bio was split.
|
| 1334 |
|
| 1335 | bool err_path = md_cloned_bio(mddev, bio);
|
| 1336 |
|
| 1337 | |
| 1338 | * If we are in the error path, we are blocking the raid1d
|
| 1339 | * thread so there is a tiny risk of deadlock. So ask for
|
| 1340 | * emergency memory if needed.
|
| 1341 |
|
| 1342 | gfp_t gfp = err_path ? (GFP_NOIO | __GFP_HIGH) : GFP_NOIO;
|
| 1343 |
|
| 1344 | |
| 1345 | * Still need barrier for READ in case that whole
|
| 1346 | * array is frozen.
|
| 1347 |
|
| 1348 | wait_read_barrier(conf, bio->bi_iter.bi_sector);
|
| 1349 |
|
| 1350 | if (!r1_bio)
|
| 1351 | r1_bio = alloc_r1bio(mddev, bio);
|
| 1352 | else
|
| 1353 | init_r1bio(r1_bio, mddev, bio);
|
| 1354 | r1_bio->sectors = max_read_sectors;
|
| 1355 |
|
| 1356 | |
| 1357 | * make_request() can abort the operation when read-ahead is being
|
| 1358 | * used and no empty request is available.
|
| 1359 |
|
| 1360 | rdisk = read_balance(conf, r1_bio, &max_sectors);
|
| 1361 | if (rdisk < 0) {
|
| 1362 |
|
| 1363 | if (r1bio_existed)
|
| 1364 | pr_crit_ratelimited("md/raid1:%s: %pg: unrecoverable I/O read error for block %llu\n",
|
| 1365 | mdname(mddev),
|
| 1366 | conf->mirrors[r1_bio->read_disk].rdev->bdev,
|
| 1367 | r1_bio->sector);
|
| 1368 | raid_end_bio_io(r1_bio);
|
| 1369 | return;
|
| 1370 | }
|
| 1371 | mirror = conf->mirrors + rdisk;
|
| 1372 |
|
| 1373 | if (r1bio_existed)
|
| 1374 | pr_info_ratelimited("md/raid1:%s: redirecting sector %llu to other mirror: %pg\n",
|
| 1375 | mdname(mddev),
|
| 1376 | (unsigned long long)r1_bio->sector,
|
| 1377 | mirror->rdev->bdev);
|
| 1378 |
|
| 1379 | if (test_bit(WriteMostly, &mirror->rdev->flags) &&
|
| 1380 | md_bitmap_enabled(mddev, false)) {
|
| 1381 | |
| 1382 | * Reading from a write-mostly device must take care not to
|
| 1383 | * over-take any writes that are 'behind'
|
| 1384 |
|
| 1385 | mddev_add_trace_msg(mddev, "raid1 wait behind writes");
|
| 1386 | mddev->bitmap_ops->wait_behind_writes(mddev);
|
| 1387 | }
|
| 1388 |
|
| 1389 | if (max_sectors < bio_sectors(bio)) {
|
| 1390 | bio = bio_submit_split_bioset(bio, max_sectors,
|
| 1391 | &conf->bio_split);
|
| 1392 | if (!bio) {
|
| 1393 | set_bit(R1BIO_Returned, &r1_bio->state);
|
| 1394 | goto err_handle;
|
| 1395 | }
|
| 1396 |
|
| 1397 | r1_bio->master_bio = bio;
|
| 1398 | r1_bio->sectors = max_sectors;
|
| 1399 | }
|
| 1400 |
|
| 1401 | r1_bio->read_disk = rdisk;
|
| 1402 | if (likely(!md_cloned_bio(mddev, bio))) {
|
| 1403 | md_account_bio(mddev, &bio);
|
| 1404 | r1_bio->master_bio = bio;
|
| 1405 | }
|
| 1406 | read_bio = bio_alloc_clone(mirror->rdev->bdev, bio, gfp,
|
| 1407 | &mddev->bio_set);
|
| 1408 | r1_bio->bios[rdisk] = read_bio;
|
| 1409 |
|
| 1410 | read_bio->bi_iter.bi_sector = r1_bio->sector +
|
| 1411 | mirror->rdev->data_offset;
|
| 1412 | read_bio->bi_end_io = raid1_end_read_request;
|
| 1413 | if (test_bit(FailFast, &mirror->rdev->flags) &&
|
| 1414 | test_bit(R1BIO_FailFast, &r1_bio->state))
|
| 1415 | read_bio->bi_opf |= MD_FAILFAST;
|
| 1416 | read_bio->bi_private = r1_bio;
|
| 1417 | mddev_trace_remap(mddev, read_bio, r1_bio->sector);
|
| 1418 | submit_bio_noacct(read_bio);
|
| 1419 | return;
|
| 1420 |
|
| 1421 | err_handle:
|
| 1422 | atomic_dec(&mirror->rdev->nr_pending);
|
| 1423 | raid_end_bio_io(r1_bio);
|
| 1424 | }
|
| 1425 |
|
| 1426 | static void wait_blocked_rdev(struct mddev *mddev, struct bio *bio)
|
| 1427 | {
|
| 1428 | struct r1conf *conf = mddev->private;
|
| 1429 | int disks = conf->raid_disks * 2;
|
| 1430 | int i;
|
| 1431 |
|
| 1432 | retry:
|
| 1433 | for (i = 0; i < disks; i++) {
|
| 1434 | struct md_rdev *rdev = conf->mirrors[i].rdev;
|
| 1435 |
|
| 1436 | if (!rdev)
|
| 1437 | continue;
|
| 1438 |
|
| 1439 |
|
| 1440 | if (test_bit(WriteErrorSeen, &rdev->flags) &&
|
| 1441 | rdev_has_badblock(rdev, bio->bi_iter.bi_sector,
|
| 1442 | bio_sectors(bio)) < 0)
|
| 1443 | set_bit(BlockedBadBlocks, &rdev->flags);
|
| 1444 |
|
| 1445 | if (rdev_blocked(rdev)) {
|
| 1446 | mddev_add_trace_msg(rdev->mddev, "raid1 wait rdev %d blocked",
|
| 1447 | rdev->raid_disk);
|
| 1448 | atomic_inc(&rdev->nr_pending);
|
| 1449 | md_wait_for_blocked_rdev(rdev, rdev->mddev);
|
| 1450 | goto retry;
|
| 1451 | }
|
| 1452 | }
|
| 1453 | }
|
| 1454 |
|
| 1455 | static void raid1_start_write_behind(struct mddev *mddev, struct r1bio *r1_bio,
|
| 1456 | struct bio *bio)
|
| 1457 | {
|
| 1458 | unsigned long max_write_behind = mddev->bitmap_info.max_write_behind;
|
| 1459 | struct md_bitmap_stats stats;
|
| 1460 | int err;
|
| 1461 |
|
| 1462 |
|
| 1463 | if (!md_bitmap_enabled(mddev, false))
|
| 1464 | return;
|
| 1465 |
|
| 1466 | err = mddev->bitmap_ops->get_stats(mddev->bitmap, &stats);
|
| 1467 | if (err)
|
| 1468 | return;
|
| 1469 |
|
| 1470 |
|
| 1471 | if (!stats.behind_wait && stats.behind_writes < max_write_behind)
|
| 1472 | alloc_behind_master_bio(r1_bio, bio);
|
| 1473 |
|
| 1474 | if (test_bit(R1BIO_BehindIO, &r1_bio->state))
|
| 1475 | mddev->bitmap_ops->start_behind_write(mddev);
|
| 1476 |
|
| 1477 | }
|
| 1478 |
|
| 1479 | static bool raid1_write_request(struct mddev *mddev, struct bio *bio,
|
| 1480 | int max_sectors)
|
| 1481 | {
|
| 1482 | struct r1conf *conf = mddev->private;
|
| 1483 | struct r1bio *r1_bio;
|
| 1484 | int i, disks, k;
|
| 1485 | unsigned long flags;
|
| 1486 | int first_clone;
|
| 1487 | bool write_behind = false;
|
| 1488 | bool atomic = bio->bi_opf & REQ_ATOMIC;
|
| 1489 | bool is_discard = op_is_discard(bio->bi_opf);
|
| 1490 | sector_t sector = bio->bi_iter.bi_sector;
|
| 1491 |
|
| 1492 | if (mddev_is_clustered(mddev) &&
|
| 1493 | mddev->cluster_ops->area_resyncing(mddev, WRITE, sector,
|
| 1494 | bio_end_sector(bio))) {
|
| 1495 | wait_event_idle(conf->wait_barrier,
|
| 1496 | !mddev->cluster_ops->area_resyncing(mddev, WRITE,
|
| 1497 | sector,
|
| 1498 | bio_end_sector(bio)));
|
| 1499 | }
|
| 1500 |
|
| 1501 | |
| 1502 | * Register the new request and wait if the reconstruction
|
| 1503 | * thread has put up a bar for new requests.
|
| 1504 | * Continue immediately if no resync is active currently.
|
| 1505 |
|
| 1506 | wait_barrier(conf, sector);
|
| 1507 |
|
| 1508 | wait_blocked_rdev(mddev, bio);
|
| 1509 |
|
| 1510 | r1_bio = alloc_r1bio(mddev, bio);
|
| 1511 | r1_bio->sectors = max_sectors;
|
| 1512 |
|
| 1513 | |
| 1514 | * inc refcount on their rdev. Record them by setting
|
| 1515 | * bios[x] to bio
|
| 1516 | * If there are known/acknowledged bad blocks on any device on
|
| 1517 | * which we have seen a write error, we want to avoid writing those
|
| 1518 | * blocks.
|
| 1519 | * This potentially requires several writes to write around
|
| 1520 | * the bad blocks. Each set of writes gets it's own r1bio
|
| 1521 | * with a set of bios attached.
|
| 1522 |
|
| 1523 |
|
| 1524 | disks = conf->raid_disks * 2;
|
| 1525 | for (i = 0; i < disks; i++) {
|
| 1526 | struct md_rdev *rdev = conf->mirrors[i].rdev;
|
| 1527 |
|
| 1528 | |
| 1529 | * The write-behind io is only attempted on drives marked as
|
| 1530 | * write-mostly, which means we could allocate write behind
|
| 1531 | * bio later.
|
| 1532 |
|
| 1533 | if (!is_discard && rdev && test_bit(WriteMostly, &rdev->flags))
|
| 1534 | write_behind = true;
|
| 1535 | if (atomic && max_sectors > BIO_MAX_VECS * (PAGE_SIZE >> 9))
|
| 1536 | write_behind = false;
|
| 1537 |
|
| 1538 | r1_bio->bios[i] = NULL;
|
| 1539 | if (!rdev || test_bit(Faulty, &rdev->flags))
|
| 1540 | continue;
|
| 1541 |
|
| 1542 | if (test_bit(WriteErrorSeen, &rdev->flags)) {
|
| 1543 | sector_t first_bad;
|
| 1544 | sector_t bad_sectors;
|
| 1545 | int is_bad;
|
| 1546 |
|
| 1547 | is_bad = is_badblock(rdev, sector, max_sectors,
|
| 1548 | &first_bad, &bad_sectors);
|
| 1549 | if (is_bad && first_bad <= sector) {
|
| 1550 |
|
| 1551 | bad_sectors -= (sector - first_bad);
|
| 1552 | if (bad_sectors < max_sectors)
|
| 1553 | |
| 1554 | * to other devices yet
|
| 1555 |
|
| 1556 | max_sectors = bad_sectors;
|
| 1557 | continue;
|
| 1558 | }
|
| 1559 | if (is_bad) {
|
| 1560 | int good_sectors;
|
| 1561 |
|
| 1562 | good_sectors = first_bad - sector;
|
| 1563 | if (good_sectors < max_sectors)
|
| 1564 | max_sectors = good_sectors;
|
| 1565 | }
|
| 1566 | }
|
| 1567 |
|
| 1568 | atomic_inc(&rdev->nr_pending);
|
| 1569 | r1_bio->bios[i] = bio;
|
| 1570 | }
|
| 1571 |
|
| 1572 | |
| 1573 | * When using a bitmap, we may call alloc_behind_master_bio below.
|
| 1574 | * alloc_behind_master_bio allocates a copy of the data payload a page
|
| 1575 | * at a time and thus needs a new bio that can fit the whole payload
|
| 1576 | * this bio in page sized chunks.
|
| 1577 |
|
| 1578 | if (write_behind && mddev->bitmap)
|
| 1579 | max_sectors = min_t(int, max_sectors,
|
| 1580 | BIO_MAX_VECS * (PAGE_SIZE >> 9));
|
| 1581 | if (max_sectors < bio_sectors(bio)) {
|
| 1582 | if (atomic) {
|
| 1583 | bio_io_error(bio);
|
| 1584 | goto err_dec_pending;
|
| 1585 | }
|
| 1586 |
|
| 1587 | bio = bio_submit_split_bioset(bio, max_sectors,
|
| 1588 | &conf->bio_split);
|
| 1589 | if (!bio)
|
| 1590 | goto err_dec_pending;
|
| 1591 |
|
| 1592 | r1_bio->master_bio = bio;
|
| 1593 | r1_bio->sectors = max_sectors;
|
| 1594 | }
|
| 1595 |
|
| 1596 | md_account_bio(mddev, &bio);
|
| 1597 | r1_bio->master_bio = bio;
|
| 1598 | atomic_set(&r1_bio->remaining, 1);
|
| 1599 | atomic_set(&r1_bio->behind_remaining, 0);
|
| 1600 |
|
| 1601 | first_clone = 1;
|
| 1602 |
|
| 1603 | for (i = 0; i < disks; i++) {
|
| 1604 | struct bio *mbio = NULL;
|
| 1605 | struct md_rdev *rdev = conf->mirrors[i].rdev;
|
| 1606 | if (!r1_bio->bios[i])
|
| 1607 | continue;
|
| 1608 |
|
| 1609 | if (first_clone) {
|
| 1610 | if (write_behind)
|
| 1611 | raid1_start_write_behind(mddev, r1_bio, bio);
|
| 1612 | first_clone = 0;
|
| 1613 | }
|
| 1614 |
|
| 1615 | if (r1_bio->behind_master_bio) {
|
| 1616 | mbio = bio_alloc_clone(rdev->bdev,
|
| 1617 | r1_bio->behind_master_bio,
|
| 1618 | GFP_NOIO, &mddev->bio_set);
|
| 1619 | if (test_bit(CollisionCheck, &rdev->flags))
|
| 1620 | wait_for_serialization(rdev, r1_bio);
|
| 1621 | if (test_bit(WriteMostly, &rdev->flags))
|
| 1622 | atomic_inc(&r1_bio->behind_remaining);
|
| 1623 | } else {
|
| 1624 | mbio = bio_alloc_clone(rdev->bdev, bio, GFP_NOIO,
|
| 1625 | &mddev->bio_set);
|
| 1626 |
|
| 1627 | if (test_bit(MD_SERIALIZE_POLICY, &mddev->flags))
|
| 1628 | wait_for_serialization(rdev, r1_bio);
|
| 1629 | }
|
| 1630 |
|
| 1631 | r1_bio->bios[i] = mbio;
|
| 1632 |
|
| 1633 | mbio->bi_iter.bi_sector = sector + rdev->data_offset;
|
| 1634 | mbio->bi_end_io = raid1_end_write_request;
|
| 1635 | if (test_bit(FailFast, &rdev->flags) &&
|
| 1636 | !test_bit(WriteMostly, &rdev->flags) &&
|
| 1637 | conf->raid_disks - mddev->degraded > 1)
|
| 1638 | mbio->bi_opf |= MD_FAILFAST;
|
| 1639 | mbio->bi_private = r1_bio;
|
| 1640 |
|
| 1641 | atomic_inc(&r1_bio->remaining);
|
| 1642 | mddev_trace_remap(mddev, mbio, sector);
|
| 1643 |
|
| 1644 | mbio->bi_bdev = (void *)rdev;
|
| 1645 | if (!raid1_add_bio_to_plug(mddev, mbio, raid1_unplug, disks)) {
|
| 1646 | spin_lock_irqsave(&conf->device_lock, flags);
|
| 1647 | bio_list_add(&conf->pending_bio_list, mbio);
|
| 1648 | spin_unlock_irqrestore(&conf->device_lock, flags);
|
| 1649 | md_wakeup_thread(mddev->thread);
|
| 1650 | }
|
| 1651 | }
|
| 1652 |
|
| 1653 | r1_bio_write_done(r1_bio);
|
| 1654 |
|
| 1655 |
|
| 1656 | wake_up_barrier(conf);
|
| 1657 |
|
| 1658 | return true;
|
| 1659 |
|
| 1660 | err_dec_pending:
|
| 1661 | for (k = 0; k < i; k++) {
|
| 1662 | if (r1_bio->bios[k]) {
|
| 1663 | rdev_dec_pending(conf->mirrors[k].rdev, mddev);
|
| 1664 | r1_bio->bios[k] = NULL;
|
| 1665 | }
|
| 1666 | }
|
| 1667 |
|
| 1668 | free_r1bio(r1_bio);
|
| 1669 | allow_barrier(conf, sector);
|
| 1670 |
|
| 1671 | return false;
|
| 1672 | }
|
| 1673 |
|
| 1674 | static bool raid1_make_request(struct mddev *mddev, struct bio *bio)
|
| 1675 | {
|
| 1676 | sector_t sectors;
|
| 1677 |
|
| 1678 | if (unlikely(bio->bi_opf & REQ_PREFLUSH)
|
| 1679 | && md_flush_request(mddev, bio))
|
| 1680 | return true;
|
| 1681 |
|
| 1682 | |
| 1683 | * There is a limit to the maximum size, but
|
| 1684 | * the read/write handler might find a lower limit
|
| 1685 | * due to bad blocks. To avoid multiple splits,
|
| 1686 | * we pass the maximum number of sectors down
|
| 1687 | * and let the lower level perform the split.
|
| 1688 |
|
| 1689 | sectors = align_to_barrier_unit_end(
|
| 1690 | bio->bi_iter.bi_sector, bio_sectors(bio));
|
| 1691 |
|
| 1692 | if (bio_data_dir(bio) == READ)
|
| 1693 | raid1_read_request(mddev, bio, sectors, NULL);
|
| 1694 | else {
|
| 1695 | md_write_start(mddev, bio);
|
| 1696 | if (!raid1_write_request(mddev, bio, sectors))
|
| 1697 | md_write_end(mddev);
|
| 1698 | }
|
| 1699 | return true;
|
| 1700 | }
|
| 1701 |
|
| 1702 | static void raid1_status(struct seq_file *seq, struct mddev *mddev)
|
| 1703 | {
|
| 1704 | struct r1conf *conf = mddev->private;
|
| 1705 | int i;
|
| 1706 |
|
| 1707 | lockdep_assert_held(&mddev->lock);
|
| 1708 |
|
| 1709 | seq_printf(seq, " [%d/%d] [", conf->raid_disks,
|
| 1710 | conf->raid_disks - mddev->degraded);
|
| 1711 | for (i = 0; i < conf->raid_disks; i++) {
|
| 1712 | struct md_rdev *rdev = READ_ONCE(conf->mirrors[i].rdev);
|
| 1713 |
|
| 1714 | seq_printf(seq, "%s",
|
| 1715 | rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
|
| 1716 | }
|
| 1717 | seq_printf(seq, "]");
|
| 1718 | }
|
| 1719 |
|
| 1720 | |
| 1721 | * raid1_error() - RAID1 error handler.
|
| 1722 | * @mddev: affected md device.
|
| 1723 | * @rdev: member device to fail.
|
| 1724 | *
|
| 1725 | * The routine acknowledges &rdev failure and determines new @mddev state.
|
| 1726 | * If it failed, then:
|
| 1727 | * - &MD_BROKEN flag is set in &mddev->flags.
|
| 1728 | * - recovery is disabled.
|
| 1729 | * Otherwise, it must be degraded:
|
| 1730 | * - recovery is interrupted.
|
| 1731 | * - &mddev->degraded is bumped.
|
| 1732 | *
|
| 1733 | * @rdev is marked as &Faulty excluding case when array is failed and
|
| 1734 | * MD_FAILLAST_DEV is not set.
|
| 1735 |
|
| 1736 | static void raid1_error(struct mddev *mddev, struct md_rdev *rdev)
|
| 1737 | {
|
| 1738 | struct r1conf *conf = mddev->private;
|
| 1739 | unsigned long flags;
|
| 1740 |
|
| 1741 | spin_lock_irqsave(&conf->device_lock, flags);
|
| 1742 |
|
| 1743 | if (test_bit(In_sync, &rdev->flags) &&
|
| 1744 | (conf->raid_disks - mddev->degraded) == 1) {
|
| 1745 | set_bit(MD_BROKEN, &mddev->flags);
|
| 1746 |
|
| 1747 | if (!test_bit(MD_FAILLAST_DEV, &mddev->flags)) {
|
| 1748 | spin_unlock_irqrestore(&conf->device_lock, flags);
|
| 1749 | return;
|
| 1750 | }
|
| 1751 | }
|
| 1752 | set_bit(Blocked, &rdev->flags);
|
| 1753 | if (test_and_clear_bit(In_sync, &rdev->flags))
|
| 1754 | mddev->degraded++;
|
| 1755 | set_bit(Faulty, &rdev->flags);
|
| 1756 | spin_unlock_irqrestore(&conf->device_lock, flags);
|
| 1757 | |
| 1758 | * if recovery is running, make sure it aborts.
|
| 1759 |
|
| 1760 | set_bit(MD_RECOVERY_INTR, &mddev->recovery);
|
| 1761 | set_mask_bits(&mddev->sb_flags, 0,
|
| 1762 | BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
|
| 1763 | pr_crit("md/raid1:%s: Disk failure on %pg, disabling device.\n"
|
| 1764 | "md/raid1:%s: Operation continuing on %d devices.\n",
|
| 1765 | mdname(mddev), rdev->bdev,
|
| 1766 | mdname(mddev), conf->raid_disks - mddev->degraded);
|
| 1767 | }
|
| 1768 |
|
| 1769 | static void print_conf(struct r1conf *conf)
|
| 1770 | {
|
| 1771 | int i;
|
| 1772 |
|
| 1773 | pr_debug("RAID1 conf printout:\n");
|
| 1774 | if (!conf) {
|
| 1775 | pr_debug("(!conf)\n");
|
| 1776 | return;
|
| 1777 | }
|
| 1778 | pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
|
| 1779 | conf->raid_disks);
|
| 1780 |
|
| 1781 | lockdep_assert_held(&conf->mddev->reconfig_mutex);
|
| 1782 | for (i = 0; i < conf->raid_disks; i++) {
|
| 1783 | struct md_rdev *rdev = conf->mirrors[i].rdev;
|
| 1784 | if (rdev)
|
| 1785 | pr_debug(" disk %d, wo:%d, o:%d, dev:%pg\n",
|
| 1786 | i, !test_bit(In_sync, &rdev->flags),
|
| 1787 | !test_bit(Faulty, &rdev->flags),
|
| 1788 | rdev->bdev);
|
| 1789 | }
|
| 1790 | }
|
| 1791 |
|
| 1792 | static void close_sync(struct r1conf *conf)
|
| 1793 | {
|
| 1794 | int idx;
|
| 1795 |
|
| 1796 | for (idx = 0; idx < BARRIER_BUCKETS_NR; idx++) {
|
| 1797 | _wait_barrier(conf, idx);
|
| 1798 | _allow_barrier(conf, idx);
|
| 1799 | }
|
| 1800 |
|
| 1801 | mempool_exit(&conf->r1buf_pool);
|
| 1802 | }
|
| 1803 |
|
| 1804 | static int raid1_spare_active(struct mddev *mddev)
|
| 1805 | {
|
| 1806 | int i;
|
| 1807 | struct r1conf *conf = mddev->private;
|
| 1808 | int count = 0;
|
| 1809 | unsigned long flags;
|
| 1810 |
|
| 1811 | |
| 1812 | * Find all failed disks within the RAID1 configuration
|
| 1813 | * and mark them readable.
|
| 1814 | * Called under mddev lock, so rcu protection not needed.
|
| 1815 | * device_lock used to avoid races with raid1_end_read_request
|
| 1816 | * which expects 'In_sync' flags and ->degraded to be consistent.
|
| 1817 |
|
| 1818 | spin_lock_irqsave(&conf->device_lock, flags);
|
| 1819 | for (i = 0; i < conf->raid_disks; i++) {
|
| 1820 | struct md_rdev *rdev = conf->mirrors[i].rdev;
|
| 1821 | struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev;
|
| 1822 | if (repl
|
| 1823 | && !test_bit(Candidate, &repl->flags)
|
| 1824 | && repl->recovery_offset == MaxSector
|
| 1825 | && !test_bit(Faulty, &repl->flags)
|
| 1826 | && !test_and_set_bit(In_sync, &repl->flags)) {
|
| 1827 |
|
| 1828 | if (!rdev ||
|
| 1829 | !test_and_clear_bit(In_sync, &rdev->flags))
|
| 1830 | count++;
|
| 1831 | if (rdev) {
|
| 1832 | |
| 1833 | * faulty, but we need to be sure
|
| 1834 | * it gets removed and never re-added
|
| 1835 |
|
| 1836 | set_bit(Faulty, &rdev->flags);
|
| 1837 | sysfs_notify_dirent_safe(
|
| 1838 | rdev->sysfs_state);
|
| 1839 | }
|
| 1840 | }
|
| 1841 | if (rdev
|
| 1842 | && rdev->recovery_offset == MaxSector
|
| 1843 | && !test_bit(Faulty, &rdev->flags)
|
| 1844 | && !test_and_set_bit(In_sync, &rdev->flags)) {
|
| 1845 | count++;
|
| 1846 | sysfs_notify_dirent_safe(rdev->sysfs_state);
|
| 1847 | }
|
| 1848 | }
|
| 1849 | mddev->degraded -= count;
|
| 1850 | spin_unlock_irqrestore(&conf->device_lock, flags);
|
| 1851 |
|
| 1852 | print_conf(conf);
|
| 1853 | return count;
|
| 1854 | }
|
| 1855 |
|
| 1856 | static bool raid1_add_conf(struct r1conf *conf, struct md_rdev *rdev, int disk,
|
| 1857 | bool replacement)
|
| 1858 | {
|
| 1859 | struct raid1_info *info = conf->mirrors + disk;
|
| 1860 |
|
| 1861 | if (replacement)
|
| 1862 | info += conf->raid_disks;
|
| 1863 |
|
| 1864 | if (info->rdev)
|
| 1865 | return false;
|
| 1866 |
|
| 1867 | if (!bdev_rot(rdev->bdev)) {
|
| 1868 | set_bit(Nonrot, &rdev->flags);
|
| 1869 | WRITE_ONCE(conf->nonrot_disks, conf->nonrot_disks + 1);
|
| 1870 | }
|
| 1871 |
|
| 1872 | rdev->raid_disk = disk;
|
| 1873 | info->head_position = 0;
|
| 1874 | info->seq_start = MaxSector;
|
| 1875 | WRITE_ONCE(info->rdev, rdev);
|
| 1876 |
|
| 1877 | return true;
|
| 1878 | }
|
| 1879 |
|
| 1880 | static bool raid1_remove_conf(struct r1conf *conf, int disk)
|
| 1881 | {
|
| 1882 | struct raid1_info *info = conf->mirrors + disk;
|
| 1883 | struct md_rdev *rdev = info->rdev;
|
| 1884 |
|
| 1885 | if (!rdev || test_bit(In_sync, &rdev->flags) ||
|
| 1886 | atomic_read(&rdev->nr_pending))
|
| 1887 | return false;
|
| 1888 |
|
| 1889 |
|
| 1890 | if (!test_bit(Faulty, &rdev->flags) &&
|
| 1891 | rdev->mddev->degraded < conf->raid_disks)
|
| 1892 | return false;
|
| 1893 |
|
| 1894 | if (test_and_clear_bit(Nonrot, &rdev->flags))
|
| 1895 | WRITE_ONCE(conf->nonrot_disks, conf->nonrot_disks - 1);
|
| 1896 |
|
| 1897 | WRITE_ONCE(info->rdev, NULL);
|
| 1898 | return true;
|
| 1899 | }
|
| 1900 |
|
| 1901 | static int raid1_add_disk(struct mddev *mddev, struct md_rdev *rdev)
|
| 1902 | {
|
| 1903 | struct r1conf *conf = mddev->private;
|
| 1904 | int err = -EEXIST;
|
| 1905 | int mirror = 0, repl_slot = -1;
|
| 1906 | struct raid1_info *p;
|
| 1907 | int first = 0;
|
| 1908 | int last = conf->raid_disks - 1;
|
| 1909 |
|
| 1910 | if (rdev->raid_disk >= 0)
|
| 1911 | first = last = rdev->raid_disk;
|
| 1912 |
|
| 1913 | |
| 1914 | * find the disk ... but prefer rdev->saved_raid_disk
|
| 1915 | * if possible.
|
| 1916 |
|
| 1917 | if (rdev->saved_raid_disk >= 0 &&
|
| 1918 | rdev->saved_raid_disk >= first &&
|
| 1919 | rdev->saved_raid_disk < conf->raid_disks &&
|
| 1920 | conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
|
| 1921 | first = last = rdev->saved_raid_disk;
|
| 1922 |
|
| 1923 | for (mirror = first; mirror <= last; mirror++) {
|
| 1924 | p = conf->mirrors + mirror;
|
| 1925 | if (!p->rdev) {
|
| 1926 | err = mddev_stack_new_rdev(mddev, rdev);
|
| 1927 | if (err)
|
| 1928 | return err;
|
| 1929 |
|
| 1930 | raid1_add_conf(conf, rdev, mirror, false);
|
| 1931 | |
| 1932 | * if this was recently any drive of the array
|
| 1933 |
|
| 1934 | if (rdev->saved_raid_disk < 0)
|
| 1935 | conf->fullsync = 1;
|
| 1936 | break;
|
| 1937 | }
|
| 1938 | if (test_bit(WantReplacement, &p->rdev->flags) &&
|
| 1939 | p[conf->raid_disks].rdev == NULL && repl_slot < 0)
|
| 1940 | repl_slot = mirror;
|
| 1941 | }
|
| 1942 |
|
| 1943 | if (err && repl_slot >= 0) {
|
| 1944 |
|
| 1945 | clear_bit(In_sync, &rdev->flags);
|
| 1946 | set_bit(Replacement, &rdev->flags);
|
| 1947 | raid1_add_conf(conf, rdev, repl_slot, true);
|
| 1948 | err = 0;
|
| 1949 | conf->fullsync = 1;
|
| 1950 | }
|
| 1951 |
|
| 1952 | print_conf(conf);
|
| 1953 | return err;
|
| 1954 | }
|
| 1955 |
|
| 1956 | static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
|
| 1957 | {
|
| 1958 | struct r1conf *conf = mddev->private;
|
| 1959 | int err = 0;
|
| 1960 | int number = rdev->raid_disk;
|
| 1961 | struct raid1_info *p = conf->mirrors + number;
|
| 1962 |
|
| 1963 | if (unlikely(number >= conf->raid_disks))
|
| 1964 | goto abort;
|
| 1965 |
|
| 1966 | if (rdev != p->rdev) {
|
| 1967 | number += conf->raid_disks;
|
| 1968 | p = conf->mirrors + number;
|
| 1969 | }
|
| 1970 |
|
| 1971 | print_conf(conf);
|
| 1972 | if (rdev == p->rdev) {
|
| 1973 | if (!raid1_remove_conf(conf, number)) {
|
| 1974 | err = -EBUSY;
|
| 1975 | goto abort;
|
| 1976 | }
|
| 1977 |
|
| 1978 | if (number < conf->raid_disks &&
|
| 1979 | conf->mirrors[conf->raid_disks + number].rdev) {
|
| 1980 | |
| 1981 | * Move down the replacement. We drain all IO before
|
| 1982 | * doing this to avoid confusion.
|
| 1983 |
|
| 1984 | struct md_rdev *repl =
|
| 1985 | conf->mirrors[conf->raid_disks + number].rdev;
|
| 1986 | freeze_array(conf, 0);
|
| 1987 | if (atomic_read(&repl->nr_pending)) {
|
| 1988 | |
| 1989 | * hold repl. Thus, we cannot set replacement
|
| 1990 | * as NULL, avoiding rdev NULL pointer
|
| 1991 | * dereference in sync_request_write and
|
| 1992 | * handle_write_finished.
|
| 1993 |
|
| 1994 | err = -EBUSY;
|
| 1995 | unfreeze_array(conf);
|
| 1996 | goto abort;
|
| 1997 | }
|
| 1998 | clear_bit(Replacement, &repl->flags);
|
| 1999 | WRITE_ONCE(p->rdev, repl);
|
| 2000 | conf->mirrors[conf->raid_disks + number].rdev = NULL;
|
| 2001 | unfreeze_array(conf);
|
| 2002 | }
|
| 2003 |
|
| 2004 | clear_bit(WantReplacement, &rdev->flags);
|
| 2005 | err = md_integrity_register(mddev);
|
| 2006 | }
|
| 2007 | abort:
|
| 2008 |
|
| 2009 | print_conf(conf);
|
| 2010 | return err;
|
| 2011 | }
|
| 2012 |
|
| 2013 | static void end_sync_read(struct bio *bio)
|
| 2014 | {
|
| 2015 | struct r1bio *r1_bio = get_resync_r1bio(bio);
|
| 2016 |
|
| 2017 | update_head_pos(r1_bio->read_disk, r1_bio);
|
| 2018 |
|
| 2019 | |
| 2020 | * we have read a block, now it needs to be re-written,
|
| 2021 | * or re-read if the read failed.
|
| 2022 | * We don't do much here, just schedule handling by raid1d
|
| 2023 |
|
| 2024 | if (!bio->bi_status)
|
| 2025 | set_bit(R1BIO_Uptodate, &r1_bio->state);
|
| 2026 |
|
| 2027 | if (atomic_dec_and_test(&r1_bio->remaining))
|
| 2028 | reschedule_retry(r1_bio);
|
| 2029 | }
|
| 2030 |
|
| 2031 | static void abort_sync_write(struct mddev *mddev, struct r1bio *r1_bio)
|
| 2032 | {
|
| 2033 | sector_t sync_blocks = 0;
|
| 2034 | sector_t s = r1_bio->sector;
|
| 2035 | long sectors_to_go = r1_bio->sectors;
|
| 2036 |
|
| 2037 |
|
| 2038 | do {
|
| 2039 | md_bitmap_end_sync(mddev, s, &sync_blocks);
|
| 2040 | s += sync_blocks;
|
| 2041 | sectors_to_go -= sync_blocks;
|
| 2042 | } while (sectors_to_go > 0);
|
| 2043 | }
|
| 2044 |
|
| 2045 | static void put_sync_write_buf(struct r1bio *r1_bio)
|
| 2046 | {
|
| 2047 | if (atomic_dec_and_test(&r1_bio->remaining)) {
|
| 2048 | struct mddev *mddev = r1_bio->mddev;
|
| 2049 | int s = r1_bio->sectors;
|
| 2050 |
|
| 2051 | if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
|
| 2052 | test_bit(R1BIO_WriteError, &r1_bio->state))
|
| 2053 | reschedule_retry(r1_bio);
|
| 2054 | else {
|
| 2055 | put_buf(r1_bio);
|
| 2056 | md_done_sync(mddev, s);
|
| 2057 | }
|
| 2058 | }
|
| 2059 | }
|
| 2060 |
|
| 2061 | static void end_sync_write(struct bio *bio)
|
| 2062 | {
|
| 2063 | struct r1bio *r1_bio = get_resync_r1bio(bio);
|
| 2064 | struct mddev *mddev = r1_bio->mddev;
|
| 2065 | struct r1conf *conf = mddev->private;
|
| 2066 | struct md_rdev *rdev = conf->mirrors[find_bio_disk(r1_bio, bio)].rdev;
|
| 2067 |
|
| 2068 | if (bio->bi_status) {
|
| 2069 | abort_sync_write(mddev, r1_bio);
|
| 2070 | set_bit(WriteErrorSeen, &rdev->flags);
|
| 2071 | if (!test_and_set_bit(WantReplacement, &rdev->flags))
|
| 2072 | set_bit(MD_RECOVERY_NEEDED, &
|
| 2073 | mddev->recovery);
|
| 2074 | set_bit(R1BIO_WriteError, &r1_bio->state);
|
| 2075 | } else if (rdev_has_badblock(rdev, r1_bio->sector, r1_bio->sectors) &&
|
| 2076 | !rdev_has_badblock(conf->mirrors[r1_bio->read_disk].rdev,
|
| 2077 | r1_bio->sector, r1_bio->sectors)) {
|
| 2078 | set_bit(R1BIO_MadeGood, &r1_bio->state);
|
| 2079 | }
|
| 2080 |
|
| 2081 | put_sync_write_buf(r1_bio);
|
| 2082 | }
|
| 2083 |
|
| 2084 | static int r1_sync_page_io(struct md_rdev *rdev, sector_t sector,
|
| 2085 | int sectors, struct page *page, blk_opf_t rw)
|
| 2086 | {
|
| 2087 | if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
|
| 2088 |
|
| 2089 | return 1;
|
| 2090 | if (rw == REQ_OP_WRITE) {
|
| 2091 | set_bit(WriteErrorSeen, &rdev->flags);
|
| 2092 | if (!test_and_set_bit(WantReplacement,
|
| 2093 | &rdev->flags))
|
| 2094 | set_bit(MD_RECOVERY_NEEDED, &
|
| 2095 | rdev->mddev->recovery);
|
| 2096 | }
|
| 2097 |
|
| 2098 | rdev_set_badblocks(rdev, sector, sectors, 0);
|
| 2099 | return 0;
|
| 2100 | }
|
| 2101 |
|
| 2102 | static int fix_sync_read_error(struct r1bio *r1_bio)
|
| 2103 | {
|
| 2104 | |
| 2105 | * good data, much like with normal read errors. Only
|
| 2106 | * read into the pages we already have so we don't
|
| 2107 | * need to re-issue the read request.
|
| 2108 | * We don't need to freeze the array, because being in an
|
| 2109 | * active sync request, there is no normal IO, and
|
| 2110 | * no overlapping syncs.
|
| 2111 | * We don't need to check is_badblock() again as we
|
| 2112 | * made sure that anything with a bad block in range
|
| 2113 | * will have bi_end_io clear.
|
| 2114 |
|
| 2115 | struct mddev *mddev = r1_bio->mddev;
|
| 2116 | struct r1conf *conf = mddev->private;
|
| 2117 | struct bio *bio = r1_bio->bios[r1_bio->read_disk];
|
| 2118 | struct page **pages = get_resync_pages(bio)->pages;
|
| 2119 | sector_t sect = r1_bio->sector;
|
| 2120 | int sectors = r1_bio->sectors;
|
| 2121 | int idx = 0;
|
| 2122 | struct md_rdev *rdev;
|
| 2123 |
|
| 2124 | rdev = conf->mirrors[r1_bio->read_disk].rdev;
|
| 2125 | if (test_bit(FailFast, &rdev->flags)) {
|
| 2126 | |
| 2127 | * ... unless it is the last working device of course
|
| 2128 | md_error(mddev, rdev);
|
| 2129 | if (test_bit(Faulty, &rdev->flags))
|
| 2130 | |
| 2131 | * put_buf does it's thing
|
| 2132 |
|
| 2133 | bio->bi_end_io = end_sync_write;
|
| 2134 | }
|
| 2135 |
|
| 2136 | while(sectors) {
|
| 2137 | int s = sectors;
|
| 2138 | int d = r1_bio->read_disk;
|
| 2139 | int success = 0;
|
| 2140 | int start;
|
| 2141 |
|
| 2142 | if (s > (PAGE_SIZE>>9))
|
| 2143 | s = PAGE_SIZE >> 9;
|
| 2144 | do {
|
| 2145 | if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
|
| 2146 | |
| 2147 | * can only be removed when no resync is
|
| 2148 | * active, and resync is currently active
|
| 2149 |
|
| 2150 | rdev = conf->mirrors[d].rdev;
|
| 2151 | if (sync_page_io(rdev, sect, s<<9,
|
| 2152 | pages[idx],
|
| 2153 | REQ_OP_READ, false)) {
|
| 2154 | success = 1;
|
| 2155 | break;
|
| 2156 | }
|
| 2157 | }
|
| 2158 | d++;
|
| 2159 | if (d == conf->raid_disks * 2)
|
| 2160 | d = 0;
|
| 2161 | } while (!success && d != r1_bio->read_disk);
|
| 2162 |
|
| 2163 | if (!success) {
|
| 2164 | int abort = 0;
|
| 2165 | |
| 2166 | * Record a bad block on each device. If that doesn't
|
| 2167 | * work just disable and interrupt the recovery.
|
| 2168 | * Don't fail devices as that won't really help.
|
| 2169 |
|
| 2170 | pr_crit_ratelimited("md/raid1:%s: %pg: unrecoverable I/O read error for block %llu\n",
|
| 2171 | mdname(mddev), bio->bi_bdev,
|
| 2172 | (unsigned long long)r1_bio->sector);
|
| 2173 | for (d = 0; d < conf->raid_disks * 2; d++) {
|
| 2174 | rdev = conf->mirrors[d].rdev;
|
| 2175 | if (!rdev || test_bit(Faulty, &rdev->flags))
|
| 2176 | continue;
|
| 2177 | if (!rdev_set_badblocks(rdev, sect, s, 0))
|
| 2178 | abort = 1;
|
| 2179 | }
|
| 2180 | if (abort)
|
| 2181 | return 0;
|
| 2182 |
|
| 2183 |
|
| 2184 | sectors -= s;
|
| 2185 | sect += s;
|
| 2186 | idx++;
|
| 2187 | continue;
|
| 2188 | }
|
| 2189 |
|
| 2190 | start = d;
|
| 2191 |
|
| 2192 | while (d != r1_bio->read_disk) {
|
| 2193 | if (d == 0)
|
| 2194 | d = conf->raid_disks * 2;
|
| 2195 | d--;
|
| 2196 | if (r1_bio->bios[d]->bi_end_io != end_sync_read)
|
| 2197 | continue;
|
| 2198 | rdev = conf->mirrors[d].rdev;
|
| 2199 | if (r1_sync_page_io(rdev, sect, s,
|
| 2200 | pages[idx],
|
| 2201 | REQ_OP_WRITE) == 0) {
|
| 2202 | r1_bio->bios[d]->bi_end_io = NULL;
|
| 2203 | rdev_dec_pending(rdev, mddev);
|
| 2204 | }
|
| 2205 | }
|
| 2206 | d = start;
|
| 2207 | while (d != r1_bio->read_disk) {
|
| 2208 | if (d == 0)
|
| 2209 | d = conf->raid_disks * 2;
|
| 2210 | d--;
|
| 2211 | if (r1_bio->bios[d]->bi_end_io != end_sync_read)
|
| 2212 | continue;
|
| 2213 | rdev = conf->mirrors[d].rdev;
|
| 2214 | if (r1_sync_page_io(rdev, sect, s,
|
| 2215 | pages[idx],
|
| 2216 | REQ_OP_READ) != 0)
|
| 2217 | atomic_add(s, &rdev->corrected_errors);
|
| 2218 | }
|
| 2219 | sectors -= s;
|
| 2220 | sect += s;
|
| 2221 | idx ++;
|
| 2222 | }
|
| 2223 | set_bit(R1BIO_Uptodate, &r1_bio->state);
|
| 2224 | bio->bi_status = 0;
|
| 2225 | return 1;
|
| 2226 | }
|
| 2227 |
|
| 2228 | static void process_checks(struct r1bio *r1_bio)
|
| 2229 | {
|
| 2230 | |
| 2231 | * got the block, then there is no hope left.
|
| 2232 | * If we have, then we want to do a comparison
|
| 2233 | * and skip the write if everything is the same.
|
| 2234 | * If any blocks failed to read, then we need to
|
| 2235 | * attempt an over-write
|
| 2236 |
|
| 2237 | struct mddev *mddev = r1_bio->mddev;
|
| 2238 | struct r1conf *conf = mddev->private;
|
| 2239 | int primary;
|
| 2240 | int i;
|
| 2241 | int vcnt;
|
| 2242 |
|
| 2243 |
|
| 2244 | vcnt = (r1_bio->sectors + PAGE_SIZE / 512 - 1) >> (PAGE_SHIFT - 9);
|
| 2245 | for (i = 0; i < conf->raid_disks * 2; i++) {
|
| 2246 | blk_status_t status;
|
| 2247 | struct bio *b = r1_bio->bios[i];
|
| 2248 | struct resync_pages *rp = get_resync_pages(b);
|
| 2249 | if (b->bi_end_io != end_sync_read)
|
| 2250 | continue;
|
| 2251 |
|
| 2252 | status = b->bi_status;
|
| 2253 | bio_reset(b, conf->mirrors[i].rdev->bdev, REQ_OP_READ);
|
| 2254 | b->bi_status = status;
|
| 2255 | b->bi_iter.bi_sector = r1_bio->sector +
|
| 2256 | conf->mirrors[i].rdev->data_offset;
|
| 2257 | b->bi_end_io = end_sync_read;
|
| 2258 | rp->raid_bio = r1_bio;
|
| 2259 | b->bi_private = rp;
|
| 2260 |
|
| 2261 |
|
| 2262 | md_bio_reset_resync_pages(b, rp, r1_bio->sectors << 9);
|
| 2263 | }
|
| 2264 | for (primary = 0; primary < conf->raid_disks * 2; primary++)
|
| 2265 | if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
|
| 2266 | !r1_bio->bios[primary]->bi_status) {
|
| 2267 | r1_bio->bios[primary]->bi_end_io = NULL;
|
| 2268 | rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
|
| 2269 | break;
|
| 2270 | }
|
| 2271 | r1_bio->read_disk = primary;
|
| 2272 | for (i = 0; i < conf->raid_disks * 2; i++) {
|
| 2273 | int j = 0;
|
| 2274 | struct bio *pbio = r1_bio->bios[primary];
|
| 2275 | struct bio *sbio = r1_bio->bios[i];
|
| 2276 | blk_status_t status = sbio->bi_status;
|
| 2277 | struct page **ppages = get_resync_pages(pbio)->pages;
|
| 2278 | struct page **spages = get_resync_pages(sbio)->pages;
|
| 2279 | struct bio_vec *bi;
|
| 2280 | int page_len[RESYNC_PAGES] = { 0 };
|
| 2281 | struct bvec_iter_all iter_all;
|
| 2282 |
|
| 2283 | if (sbio->bi_end_io != end_sync_read)
|
| 2284 | continue;
|
| 2285 |
|
| 2286 | sbio->bi_status = 0;
|
| 2287 |
|
| 2288 | bio_for_each_segment_all(bi, sbio, iter_all)
|
| 2289 | page_len[j++] = bi->bv_len;
|
| 2290 |
|
| 2291 | if (!status) {
|
| 2292 | for (j = vcnt; j-- ; ) {
|
| 2293 | if (memcmp(page_address(ppages[j]),
|
| 2294 | page_address(spages[j]),
|
| 2295 | page_len[j]))
|
| 2296 | break;
|
| 2297 | }
|
| 2298 | } else
|
| 2299 | j = 0;
|
| 2300 | if (j >= 0)
|
| 2301 | atomic64_add(r1_bio->sectors, &mddev->resync_mismatches);
|
| 2302 | if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
|
| 2303 | && !status)) {
|
| 2304 |
|
| 2305 | sbio->bi_end_io = NULL;
|
| 2306 | rdev_dec_pending(conf->mirrors[i].rdev, mddev);
|
| 2307 | continue;
|
| 2308 | }
|
| 2309 |
|
| 2310 | bio_copy_data(sbio, pbio);
|
| 2311 | }
|
| 2312 | }
|
| 2313 |
|
| 2314 | static void sync_request_write(struct mddev *mddev, struct r1bio *r1_bio)
|
| 2315 | {
|
| 2316 | struct r1conf *conf = mddev->private;
|
| 2317 | int i;
|
| 2318 | int disks = conf->raid_disks * 2;
|
| 2319 | struct bio *wbio;
|
| 2320 |
|
| 2321 | if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
|
| 2322 | |
| 2323 | * ouch - failed to read all of that.
|
| 2324 | * No need to fix read error for check/repair
|
| 2325 | * because all member disks are read.
|
| 2326 |
|
| 2327 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) ||
|
| 2328 | !fix_sync_read_error(r1_bio)) {
|
| 2329 | md_done_sync(mddev, r1_bio->sectors);
|
| 2330 | md_sync_error(mddev);
|
| 2331 | put_buf(r1_bio);
|
| 2332 | return;
|
| 2333 | }
|
| 2334 | }
|
| 2335 |
|
| 2336 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
|
| 2337 | process_checks(r1_bio);
|
| 2338 |
|
| 2339 | |
| 2340 | * schedule writes
|
| 2341 |
|
| 2342 | atomic_set(&r1_bio->remaining, 1);
|
| 2343 | for (i = 0; i < disks ; i++) {
|
| 2344 | wbio = r1_bio->bios[i];
|
| 2345 | if (wbio->bi_end_io == NULL ||
|
| 2346 | (wbio->bi_end_io == end_sync_read &&
|
| 2347 | (i == r1_bio->read_disk ||
|
| 2348 | !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
|
| 2349 | continue;
|
| 2350 | if (test_bit(Faulty, &conf->mirrors[i].rdev->flags)) {
|
| 2351 | abort_sync_write(mddev, r1_bio);
|
| 2352 | continue;
|
| 2353 | }
|
| 2354 |
|
| 2355 | wbio->bi_opf = REQ_OP_WRITE;
|
| 2356 | if (test_bit(FailFast, &conf->mirrors[i].rdev->flags))
|
| 2357 | wbio->bi_opf |= MD_FAILFAST;
|
| 2358 |
|
| 2359 | wbio->bi_end_io = end_sync_write;
|
| 2360 | atomic_inc(&r1_bio->remaining);
|
| 2361 |
|
| 2362 | submit_bio_noacct(wbio);
|
| 2363 | }
|
| 2364 |
|
| 2365 | put_sync_write_buf(r1_bio);
|
| 2366 | }
|
| 2367 |
|
| 2368 | |
| 2369 | * This is a kernel thread which:
|
| 2370 | *
|
| 2371 | * 1. Retries failed read operations on working mirrors.
|
| 2372 | * 2. Updates the raid superblock when problems encounter.
|
| 2373 | * 3. Performs writes following reads for array synchronising.
|
| 2374 |
|
| 2375 |
|
| 2376 | static void fix_read_error(struct r1conf *conf, struct r1bio *r1_bio)
|
| 2377 | {
|
| 2378 | sector_t sect = r1_bio->sector;
|
| 2379 | int sectors = r1_bio->sectors;
|
| 2380 | int read_disk = r1_bio->read_disk;
|
| 2381 | struct mddev *mddev = conf->mddev;
|
| 2382 | struct md_rdev *rdev = conf->mirrors[read_disk].rdev;
|
| 2383 |
|
| 2384 | while(sectors) {
|
| 2385 | int s = sectors;
|
| 2386 | int d = read_disk;
|
| 2387 | int success = 0;
|
| 2388 | int start;
|
| 2389 |
|
| 2390 | if (s > (PAGE_SIZE>>9))
|
| 2391 | s = PAGE_SIZE >> 9;
|
| 2392 |
|
| 2393 | do {
|
| 2394 | rdev = conf->mirrors[d].rdev;
|
| 2395 | if (rdev &&
|
| 2396 | (test_bit(In_sync, &rdev->flags) ||
|
| 2397 | (!test_bit(Faulty, &rdev->flags) &&
|
| 2398 | rdev->recovery_offset >= sect + s)) &&
|
| 2399 | rdev_has_badblock(rdev, sect, s) == 0) {
|
| 2400 | atomic_inc(&rdev->nr_pending);
|
| 2401 | if (sync_page_io(rdev, sect, s<<9,
|
| 2402 | conf->tmppage, REQ_OP_READ, false))
|
| 2403 | success = 1;
|
| 2404 | rdev_dec_pending(rdev, mddev);
|
| 2405 | if (success)
|
| 2406 | break;
|
| 2407 | }
|
| 2408 |
|
| 2409 | d++;
|
| 2410 | if (d == conf->raid_disks * 2)
|
| 2411 | d = 0;
|
| 2412 | } while (d != read_disk);
|
| 2413 |
|
| 2414 | if (!success) {
|
| 2415 |
|
| 2416 | struct md_rdev *rdev = conf->mirrors[read_disk].rdev;
|
| 2417 | rdev_set_badblocks(rdev, sect, s, 0);
|
| 2418 | break;
|
| 2419 | }
|
| 2420 |
|
| 2421 | start = d;
|
| 2422 | while (d != read_disk) {
|
| 2423 | if (d==0)
|
| 2424 | d = conf->raid_disks * 2;
|
| 2425 | d--;
|
| 2426 | rdev = conf->mirrors[d].rdev;
|
| 2427 | if (rdev &&
|
| 2428 | !test_bit(Faulty, &rdev->flags)) {
|
| 2429 | atomic_inc(&rdev->nr_pending);
|
| 2430 | r1_sync_page_io(rdev, sect, s,
|
| 2431 | conf->tmppage, REQ_OP_WRITE);
|
| 2432 | rdev_dec_pending(rdev, mddev);
|
| 2433 | }
|
| 2434 | }
|
| 2435 | d = start;
|
| 2436 | while (d != read_disk) {
|
| 2437 | if (d==0)
|
| 2438 | d = conf->raid_disks * 2;
|
| 2439 | d--;
|
| 2440 | rdev = conf->mirrors[d].rdev;
|
| 2441 | if (rdev &&
|
| 2442 | !test_bit(Faulty, &rdev->flags)) {
|
| 2443 | atomic_inc(&rdev->nr_pending);
|
| 2444 | if (r1_sync_page_io(rdev, sect, s,
|
| 2445 | conf->tmppage, REQ_OP_READ)) {
|
| 2446 | atomic_add(s, &rdev->corrected_errors);
|
| 2447 | pr_info("md/raid1:%s: read error corrected (%d sectors at %llu on %pg)\n",
|
| 2448 | mdname(mddev), s,
|
| 2449 | (unsigned long long)(sect +
|
| 2450 | rdev->data_offset),
|
| 2451 | rdev->bdev);
|
| 2452 | }
|
| 2453 | rdev_dec_pending(rdev, mddev);
|
| 2454 | }
|
| 2455 | }
|
| 2456 | sectors -= s;
|
| 2457 | sect += s;
|
| 2458 | }
|
| 2459 | }
|
| 2460 |
|
| 2461 | static void narrow_write_error(struct r1bio *r1_bio, int i)
|
| 2462 | {
|
| 2463 | struct mddev *mddev = r1_bio->mddev;
|
| 2464 | struct r1conf *conf = mddev->private;
|
| 2465 | struct md_rdev *rdev = conf->mirrors[i].rdev;
|
| 2466 |
|
| 2467 | |
| 2468 | * we just recently had a write error.
|
| 2469 | * We repeatedly clone the bio and trim down to one block,
|
| 2470 | * then try the write. Where the write fails we record
|
| 2471 | * a bad block.
|
| 2472 | * It is conceivable that the bio doesn't exactly align with
|
| 2473 | * blocks. We must handle this somehow.
|
| 2474 | *
|
| 2475 | * We currently own a reference on the rdev.
|
| 2476 |
|
| 2477 |
|
| 2478 | int block_sectors, lbs = bdev_logical_block_size(rdev->bdev) >> 9;
|
| 2479 | sector_t sector;
|
| 2480 | int sectors;
|
| 2481 | int sect_to_write = r1_bio->sectors;
|
| 2482 |
|
| 2483 | if (rdev->badblocks.shift < 0)
|
| 2484 | block_sectors = lbs;
|
| 2485 | else
|
| 2486 | block_sectors = roundup(1 << rdev->badblocks.shift, lbs);
|
| 2487 |
|
| 2488 | sector = r1_bio->sector;
|
| 2489 | sectors = ((sector + block_sectors)
|
| 2490 | & ~(sector_t)(block_sectors - 1))
|
| 2491 | - sector;
|
| 2492 |
|
| 2493 | while (sect_to_write) {
|
| 2494 | struct bio *wbio;
|
| 2495 | if (sectors > sect_to_write)
|
| 2496 | sectors = sect_to_write;
|
| 2497 |
|
| 2498 |
|
| 2499 | if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
|
| 2500 | wbio = bio_alloc_clone(rdev->bdev,
|
| 2501 | r1_bio->behind_master_bio,
|
| 2502 | GFP_NOIO, &mddev->bio_set);
|
| 2503 | } else {
|
| 2504 | wbio = bio_alloc_clone(rdev->bdev, r1_bio->master_bio,
|
| 2505 | GFP_NOIO, &mddev->bio_set);
|
| 2506 | }
|
| 2507 |
|
| 2508 | wbio->bi_opf = REQ_OP_WRITE;
|
| 2509 | wbio->bi_iter.bi_sector = r1_bio->sector;
|
| 2510 | wbio->bi_iter.bi_size = r1_bio->sectors << 9;
|
| 2511 |
|
| 2512 | bio_trim(wbio, sector - r1_bio->sector, sectors);
|
| 2513 | wbio->bi_iter.bi_sector += rdev->data_offset;
|
| 2514 |
|
| 2515 | if (submit_bio_wait(wbio) &&
|
| 2516 | !rdev_set_badblocks(rdev, sector, sectors, 0)) {
|
| 2517 | |
| 2518 | * Badblocks set failed, disk marked Faulty.
|
| 2519 | * No further operations needed.
|
| 2520 |
|
| 2521 | bio_put(wbio);
|
| 2522 | break;
|
| 2523 | }
|
| 2524 |
|
| 2525 | bio_put(wbio);
|
| 2526 | sect_to_write -= sectors;
|
| 2527 | sector += sectors;
|
| 2528 | sectors = block_sectors;
|
| 2529 | }
|
| 2530 | }
|
| 2531 |
|
| 2532 | static void handle_sync_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
|
| 2533 | {
|
| 2534 | int m;
|
| 2535 | int s = r1_bio->sectors;
|
| 2536 | for (m = 0; m < conf->raid_disks * 2 ; m++) {
|
| 2537 | struct md_rdev *rdev = conf->mirrors[m].rdev;
|
| 2538 | struct bio *bio = r1_bio->bios[m];
|
| 2539 | if (bio->bi_end_io == NULL)
|
| 2540 | continue;
|
| 2541 | if (!bio->bi_status &&
|
| 2542 | test_bit(R1BIO_MadeGood, &r1_bio->state))
|
| 2543 | rdev_clear_badblocks(rdev, r1_bio->sector, s, 0);
|
| 2544 | if (bio->bi_status &&
|
| 2545 | test_bit(R1BIO_WriteError, &r1_bio->state))
|
| 2546 | rdev_set_badblocks(rdev, r1_bio->sector, s, 0);
|
| 2547 | }
|
| 2548 | put_buf(r1_bio);
|
| 2549 | md_done_sync(conf->mddev, s);
|
| 2550 | }
|
| 2551 |
|
| 2552 | static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
|
| 2553 | {
|
| 2554 | int m, idx;
|
| 2555 | bool fail = false;
|
| 2556 |
|
| 2557 | for (m = 0; m < conf->raid_disks * 2 ; m++)
|
| 2558 | if (r1_bio->bios[m] == IO_MADE_GOOD) {
|
| 2559 | struct md_rdev *rdev = conf->mirrors[m].rdev;
|
| 2560 | rdev_clear_badblocks(rdev,
|
| 2561 | r1_bio->sector,
|
| 2562 | r1_bio->sectors, 0);
|
| 2563 | rdev_dec_pending(rdev, conf->mddev);
|
| 2564 | } else if (r1_bio->bios[m] != NULL) {
|
| 2565 | |
| 2566 | * narrow down and record precise write
|
| 2567 | * errors.
|
| 2568 |
|
| 2569 | fail = true;
|
| 2570 | narrow_write_error(r1_bio, m);
|
| 2571 | rdev_dec_pending(conf->mirrors[m].rdev,
|
| 2572 | conf->mddev);
|
| 2573 | }
|
| 2574 | if (fail) {
|
| 2575 | spin_lock_irq(&conf->device_lock);
|
| 2576 | list_add(&r1_bio->retry_list, &conf->bio_end_io_list);
|
| 2577 | idx = sector_to_idx(r1_bio->sector);
|
| 2578 | atomic_inc(&conf->nr_queued[idx]);
|
| 2579 | spin_unlock_irq(&conf->device_lock);
|
| 2580 | |
| 2581 | * In case freeze_array() is waiting for condition
|
| 2582 | * get_unqueued_pending() == extra to be true.
|
| 2583 |
|
| 2584 | wake_up(&conf->wait_barrier);
|
| 2585 | md_wakeup_thread(conf->mddev->thread);
|
| 2586 | } else {
|
| 2587 | if (test_bit(R1BIO_WriteError, &r1_bio->state))
|
| 2588 | close_write(r1_bio);
|
| 2589 | raid_end_bio_io(r1_bio);
|
| 2590 | }
|
| 2591 | }
|
| 2592 |
|
| 2593 | static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio)
|
| 2594 | {
|
| 2595 | struct md_rdev *rdev = conf->mirrors[r1_bio->read_disk].rdev;
|
| 2596 | struct bio *bio = r1_bio->bios[r1_bio->read_disk];
|
| 2597 | struct mddev *mddev = conf->mddev;
|
| 2598 | sector_t sector;
|
| 2599 |
|
| 2600 | clear_bit(R1BIO_ReadError, &r1_bio->state);
|
| 2601 |
|
| 2602 | bio_put(bio);
|
| 2603 | r1_bio->bios[r1_bio->read_disk] = NULL;
|
| 2604 |
|
| 2605 | |
| 2606 | * We got a read error. Maybe the drive is bad. Maybe just the block
|
| 2607 | * and we can fix it.
|
| 2608 | *
|
| 2609 | * If allowed, freeze all other IO, and try reading the block from other
|
| 2610 | * devices. If we find one, we re-write and check it that fixes the
|
| 2611 | * read error. This is all done synchronously while the array is
|
| 2612 | * frozen.
|
| 2613 |
|
| 2614 | if (mddev->ro) {
|
| 2615 | r1_bio->bios[r1_bio->read_disk] = IO_BLOCKED;
|
| 2616 | } else if (test_bit(FailFast, &rdev->flags)) {
|
| 2617 | md_error(mddev, rdev);
|
| 2618 | } else {
|
| 2619 | freeze_array(conf, 1);
|
| 2620 | if (exceed_read_errors(mddev, rdev))
|
| 2621 | r1_bio->bios[r1_bio->read_disk] = IO_BLOCKED;
|
| 2622 | else
|
| 2623 | fix_read_error(conf, r1_bio);
|
| 2624 | unfreeze_array(conf);
|
| 2625 | }
|
| 2626 |
|
| 2627 | rdev_dec_pending(rdev, conf->mddev);
|
| 2628 | sector = r1_bio->sector;
|
| 2629 | bio = r1_bio->master_bio;
|
| 2630 |
|
| 2631 |
|
| 2632 | r1_bio->state = 0;
|
| 2633 | raid1_read_request(mddev, bio, r1_bio->sectors, r1_bio);
|
| 2634 | allow_barrier(conf, sector);
|
| 2635 | }
|
| 2636 |
|
| 2637 | static void raid1d(struct md_thread *thread)
|
| 2638 | {
|
| 2639 | struct mddev *mddev = thread->mddev;
|
| 2640 | struct r1bio *r1_bio;
|
| 2641 | unsigned long flags;
|
| 2642 | struct r1conf *conf = mddev->private;
|
| 2643 | struct list_head *head = &conf->retry_list;
|
| 2644 | struct blk_plug plug;
|
| 2645 | int idx;
|
| 2646 |
|
| 2647 | md_check_recovery(mddev);
|
| 2648 |
|
| 2649 | if (!list_empty_careful(&conf->bio_end_io_list) &&
|
| 2650 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
|
| 2651 | LIST_HEAD(tmp);
|
| 2652 | spin_lock_irqsave(&conf->device_lock, flags);
|
| 2653 | if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
|
| 2654 | list_splice_init(&conf->bio_end_io_list, &tmp);
|
| 2655 | spin_unlock_irqrestore(&conf->device_lock, flags);
|
| 2656 | while (!list_empty(&tmp)) {
|
| 2657 | r1_bio = list_first_entry(&tmp, struct r1bio,
|
| 2658 | retry_list);
|
| 2659 | list_del(&r1_bio->retry_list);
|
| 2660 | idx = sector_to_idx(r1_bio->sector);
|
| 2661 | atomic_dec(&conf->nr_queued[idx]);
|
| 2662 | if (test_bit(R1BIO_WriteError, &r1_bio->state))
|
| 2663 | close_write(r1_bio);
|
| 2664 | raid_end_bio_io(r1_bio);
|
| 2665 | }
|
| 2666 | }
|
| 2667 |
|
| 2668 | blk_start_plug(&plug);
|
| 2669 | for (;;) {
|
| 2670 |
|
| 2671 | flush_pending_writes(conf);
|
| 2672 |
|
| 2673 | spin_lock_irqsave(&conf->device_lock, flags);
|
| 2674 | if (list_empty(head)) {
|
| 2675 | spin_unlock_irqrestore(&conf->device_lock, flags);
|
| 2676 | break;
|
| 2677 | }
|
| 2678 | r1_bio = list_entry(head->prev, struct r1bio, retry_list);
|
| 2679 | list_del(head->prev);
|
| 2680 | idx = sector_to_idx(r1_bio->sector);
|
| 2681 | atomic_dec(&conf->nr_queued[idx]);
|
| 2682 | spin_unlock_irqrestore(&conf->device_lock, flags);
|
| 2683 |
|
| 2684 | mddev = r1_bio->mddev;
|
| 2685 | conf = mddev->private;
|
| 2686 | if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
|
| 2687 | if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
|
| 2688 | test_bit(R1BIO_WriteError, &r1_bio->state))
|
| 2689 | handle_sync_write_finished(conf, r1_bio);
|
| 2690 | else
|
| 2691 | sync_request_write(mddev, r1_bio);
|
| 2692 | } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
|
| 2693 | test_bit(R1BIO_WriteError, &r1_bio->state))
|
| 2694 | handle_write_finished(conf, r1_bio);
|
| 2695 | else if (test_bit(R1BIO_ReadError, &r1_bio->state))
|
| 2696 | handle_read_error(conf, r1_bio);
|
| 2697 | else
|
| 2698 | WARN_ON_ONCE(1);
|
| 2699 |
|
| 2700 | cond_resched();
|
| 2701 | if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
|
| 2702 | md_check_recovery(mddev);
|
| 2703 | }
|
| 2704 | blk_finish_plug(&plug);
|
| 2705 | }
|
| 2706 |
|
| 2707 | static int init_resync(struct r1conf *conf)
|
| 2708 | {
|
| 2709 | int buffs;
|
| 2710 |
|
| 2711 | buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
|
| 2712 | BUG_ON(mempool_initialized(&conf->r1buf_pool));
|
| 2713 |
|
| 2714 | return mempool_init(&conf->r1buf_pool, buffs, r1buf_pool_alloc,
|
| 2715 | r1buf_pool_free, conf);
|
| 2716 | }
|
| 2717 |
|
| 2718 | static struct r1bio *raid1_alloc_init_r1buf(struct r1conf *conf)
|
| 2719 | {
|
| 2720 | struct r1bio *r1bio = mempool_alloc(&conf->r1buf_pool, GFP_NOIO);
|
| 2721 | struct resync_pages *rps;
|
| 2722 | struct bio *bio;
|
| 2723 | int i;
|
| 2724 |
|
| 2725 | for (i = conf->raid_disks * 2; i--; ) {
|
| 2726 | bio = r1bio->bios[i];
|
| 2727 | rps = bio->bi_private;
|
| 2728 | bio_reset(bio, NULL, 0);
|
| 2729 | bio->bi_private = rps;
|
| 2730 | }
|
| 2731 | r1bio->master_bio = NULL;
|
| 2732 | return r1bio;
|
| 2733 | }
|
| 2734 |
|
| 2735 | |
| 2736 | * perform a "sync" on one "block"
|
| 2737 | *
|
| 2738 | * We need to make sure that no normal I/O request - particularly write
|
| 2739 | * requests - conflict with active sync requests.
|
| 2740 | *
|
| 2741 | * This is achieved by tracking pending requests and a 'barrier' concept
|
| 2742 | * that can be installed to exclude normal IO requests.
|
| 2743 |
|
| 2744 |
|
| 2745 | static sector_t raid1_sync_request(struct mddev *mddev, sector_t sector_nr,
|
| 2746 | sector_t max_sector, int *skipped)
|
| 2747 | {
|
| 2748 | struct r1conf *conf = mddev->private;
|
| 2749 | struct r1bio *r1_bio;
|
| 2750 | struct bio *bio;
|
| 2751 | sector_t nr_sectors;
|
| 2752 | int disk = -1;
|
| 2753 | int i;
|
| 2754 | int wonly = -1;
|
| 2755 | int write_targets = 0, read_targets = 0;
|
| 2756 | sector_t sync_blocks;
|
| 2757 | bool still_degraded = false;
|
| 2758 | int good_sectors = RESYNC_SECTORS;
|
| 2759 | int min_bad = 0;
|
| 2760 | int idx = sector_to_idx(sector_nr);
|
| 2761 | int page_idx = 0;
|
| 2762 |
|
| 2763 | if (!mempool_initialized(&conf->r1buf_pool))
|
| 2764 | if (init_resync(conf))
|
| 2765 | return 0;
|
| 2766 |
|
| 2767 | if (sector_nr >= max_sector) {
|
| 2768 | |
| 2769 | * sync on the 'current' bitmap chunk (there will
|
| 2770 | * only be one in raid1 resync.
|
| 2771 | * We can find the current addess in mddev->curr_resync
|
| 2772 |
|
| 2773 | if (mddev->curr_resync < max_sector)
|
| 2774 | md_bitmap_end_sync(mddev, mddev->curr_resync,
|
| 2775 | &sync_blocks);
|
| 2776 | else
|
| 2777 | conf->fullsync = 0;
|
| 2778 |
|
| 2779 | if (md_bitmap_enabled(mddev, false))
|
| 2780 | mddev->bitmap_ops->close_sync(mddev);
|
| 2781 | close_sync(conf);
|
| 2782 |
|
| 2783 | if (mddev_is_clustered(mddev)) {
|
| 2784 | conf->cluster_sync_low = 0;
|
| 2785 | conf->cluster_sync_high = 0;
|
| 2786 | }
|
| 2787 | return 0;
|
| 2788 | }
|
| 2789 |
|
| 2790 | if (mddev->bitmap == NULL &&
|
| 2791 | mddev->resync_offset == MaxSector &&
|
| 2792 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
|
| 2793 | conf->fullsync == 0) {
|
| 2794 | *skipped = 1;
|
| 2795 | return max_sector - sector_nr;
|
| 2796 | }
|
| 2797 | |
| 2798 | * This call the bitmap_start_sync doesn't actually record anything
|
| 2799 |
|
| 2800 | if (!md_bitmap_start_sync(mddev, sector_nr, &sync_blocks, true) &&
|
| 2801 | !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
|
| 2802 |
|
| 2803 | *skipped = 1;
|
| 2804 | return sync_blocks;
|
| 2805 | }
|
| 2806 |
|
| 2807 | |
| 2808 | * If there is non-resync activity waiting for a turn, then let it
|
| 2809 | * though before starting on this new sync request.
|
| 2810 |
|
| 2811 | if (atomic_read(&conf->nr_waiting[idx]))
|
| 2812 | schedule_timeout_uninterruptible(1);
|
| 2813 |
|
| 2814 | |
| 2815 | * sector_nr + two times RESYNC_SECTORS
|
| 2816 |
|
| 2817 | if (md_bitmap_enabled(mddev, false))
|
| 2818 | mddev->bitmap_ops->cond_end_sync(mddev, sector_nr,
|
| 2819 | mddev_is_clustered(mddev) &&
|
| 2820 | (sector_nr + 2 * RESYNC_SECTORS >
|
| 2821 | conf->cluster_sync_high));
|
| 2822 |
|
| 2823 | if (raise_barrier(conf, sector_nr))
|
| 2824 | return 0;
|
| 2825 |
|
| 2826 | r1_bio = raid1_alloc_init_r1buf(conf);
|
| 2827 |
|
| 2828 | |
| 2829 | * If we get a correctably read error during resync or recovery,
|
| 2830 | * we might want to read from a different device. So we
|
| 2831 | * flag all drives that could conceivably be read from for READ,
|
| 2832 | * and any others (which will be non-In_sync devices) for WRITE.
|
| 2833 | * If a read fails, we try reading from something else for which READ
|
| 2834 | * is OK.
|
| 2835 |
|
| 2836 |
|
| 2837 | r1_bio->mddev = mddev;
|
| 2838 | r1_bio->sector = sector_nr;
|
| 2839 | r1_bio->state = 0;
|
| 2840 | set_bit(R1BIO_IsSync, &r1_bio->state);
|
| 2841 |
|
| 2842 | good_sectors = align_to_barrier_unit_end(sector_nr, good_sectors);
|
| 2843 |
|
| 2844 | for (i = 0; i < conf->raid_disks * 2; i++) {
|
| 2845 | struct md_rdev *rdev;
|
| 2846 | bio = r1_bio->bios[i];
|
| 2847 |
|
| 2848 | rdev = conf->mirrors[i].rdev;
|
| 2849 | if (rdev == NULL ||
|
| 2850 | test_bit(Faulty, &rdev->flags)) {
|
| 2851 | if (i < conf->raid_disks)
|
| 2852 | still_degraded = true;
|
| 2853 | } else if (!test_bit(In_sync, &rdev->flags)) {
|
| 2854 | bio->bi_opf = REQ_OP_WRITE;
|
| 2855 | bio->bi_end_io = end_sync_write;
|
| 2856 | write_targets ++;
|
| 2857 | } else {
|
| 2858 |
|
| 2859 | sector_t first_bad = MaxSector;
|
| 2860 | sector_t bad_sectors;
|
| 2861 |
|
| 2862 | if (is_badblock(rdev, sector_nr, good_sectors,
|
| 2863 | &first_bad, &bad_sectors)) {
|
| 2864 | if (first_bad > sector_nr)
|
| 2865 | good_sectors = first_bad - sector_nr;
|
| 2866 | else {
|
| 2867 | bad_sectors -= (sector_nr - first_bad);
|
| 2868 | if (min_bad == 0 ||
|
| 2869 | min_bad > bad_sectors)
|
| 2870 | min_bad = bad_sectors;
|
| 2871 | }
|
| 2872 | }
|
| 2873 | if (sector_nr < first_bad) {
|
| 2874 | if (test_bit(WriteMostly, &rdev->flags)) {
|
| 2875 | if (wonly < 0)
|
| 2876 | wonly = i;
|
| 2877 | } else {
|
| 2878 | if (disk < 0)
|
| 2879 | disk = i;
|
| 2880 | }
|
| 2881 | bio->bi_opf = REQ_OP_READ;
|
| 2882 | bio->bi_end_io = end_sync_read;
|
| 2883 | read_targets++;
|
| 2884 | } else if (!test_bit(WriteErrorSeen, &rdev->flags) &&
|
| 2885 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
|
| 2886 | !test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
|
| 2887 | |
| 2888 | * The device is suitable for reading (InSync),
|
| 2889 | * but has bad block(s) here. Let's try to correct them,
|
| 2890 | * if we are doing resync or repair. Otherwise, leave
|
| 2891 | * this device alone for this sync request.
|
| 2892 |
|
| 2893 | bio->bi_opf = REQ_OP_WRITE;
|
| 2894 | bio->bi_end_io = end_sync_write;
|
| 2895 | write_targets++;
|
| 2896 | }
|
| 2897 | }
|
| 2898 | if (rdev && bio->bi_end_io) {
|
| 2899 | atomic_inc(&rdev->nr_pending);
|
| 2900 | bio->bi_iter.bi_sector = sector_nr + rdev->data_offset;
|
| 2901 | bio_set_dev(bio, rdev->bdev);
|
| 2902 | if (test_bit(FailFast, &rdev->flags))
|
| 2903 | bio->bi_opf |= MD_FAILFAST;
|
| 2904 | }
|
| 2905 | }
|
| 2906 | if (disk < 0)
|
| 2907 | disk = wonly;
|
| 2908 | r1_bio->read_disk = disk;
|
| 2909 |
|
| 2910 | if (read_targets == 0 && min_bad > 0) {
|
| 2911 | |
| 2912 | * need to mark them bad on all write targets
|
| 2913 |
|
| 2914 | int ok = 1;
|
| 2915 | for (i = 0 ; i < conf->raid_disks * 2 ; i++)
|
| 2916 | if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
|
| 2917 | struct md_rdev *rdev = conf->mirrors[i].rdev;
|
| 2918 | ok = rdev_set_badblocks(rdev, sector_nr,
|
| 2919 | min_bad, 0
|
| 2920 | ) && ok;
|
| 2921 | }
|
| 2922 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
|
| 2923 | *skipped = 1;
|
| 2924 | put_buf(r1_bio);
|
| 2925 |
|
| 2926 | if (!ok)
|
| 2927 | |
| 2928 | * abort the resync.
|
| 2929 |
|
| 2930 | return 0;
|
| 2931 | else
|
| 2932 | return min_bad;
|
| 2933 |
|
| 2934 | }
|
| 2935 | if (min_bad > 0 && min_bad < good_sectors) {
|
| 2936 | |
| 2937 | * transition
|
| 2938 | good_sectors = min_bad;
|
| 2939 | }
|
| 2940 |
|
| 2941 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
|
| 2942 |
|
| 2943 | write_targets += read_targets-1;
|
| 2944 |
|
| 2945 | if (write_targets == 0 || read_targets == 0) {
|
| 2946 | |
| 2947 | * drives must be failed - so we are finished
|
| 2948 |
|
| 2949 | sector_t rv;
|
| 2950 | if (min_bad > 0)
|
| 2951 | max_sector = sector_nr + min_bad;
|
| 2952 | rv = max_sector - sector_nr;
|
| 2953 | *skipped = 1;
|
| 2954 | put_buf(r1_bio);
|
| 2955 | return rv;
|
| 2956 | }
|
| 2957 |
|
| 2958 | if (max_sector > mddev->resync_max)
|
| 2959 | max_sector = mddev->resync_max;
|
| 2960 | if (max_sector > sector_nr + good_sectors)
|
| 2961 | max_sector = sector_nr + good_sectors;
|
| 2962 | nr_sectors = 0;
|
| 2963 | sync_blocks = 0;
|
| 2964 | do {
|
| 2965 | struct page *page;
|
| 2966 | int len = PAGE_SIZE;
|
| 2967 | if (sector_nr + (len>>9) > max_sector)
|
| 2968 | len = (max_sector - sector_nr) << 9;
|
| 2969 | if (len == 0)
|
| 2970 | break;
|
| 2971 | if (sync_blocks == 0) {
|
| 2972 | if (!md_bitmap_start_sync(mddev, sector_nr,
|
| 2973 | &sync_blocks, still_degraded) &&
|
| 2974 | !conf->fullsync &&
|
| 2975 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
|
| 2976 | break;
|
| 2977 | if ((len >> 9) > sync_blocks)
|
| 2978 | len = sync_blocks<<9;
|
| 2979 | }
|
| 2980 |
|
| 2981 | for (i = 0 ; i < conf->raid_disks * 2; i++) {
|
| 2982 | struct resync_pages *rp;
|
| 2983 |
|
| 2984 | bio = r1_bio->bios[i];
|
| 2985 | rp = get_resync_pages(bio);
|
| 2986 | if (bio->bi_end_io) {
|
| 2987 | page = resync_fetch_page(rp, page_idx);
|
| 2988 |
|
| 2989 | |
| 2990 | * won't fail because the vec table is big
|
| 2991 | * enough to hold all these pages
|
| 2992 |
|
| 2993 | __bio_add_page(bio, page, len, 0);
|
| 2994 | }
|
| 2995 | }
|
| 2996 | nr_sectors += len>>9;
|
| 2997 | sector_nr += len>>9;
|
| 2998 | sync_blocks -= (len>>9);
|
| 2999 | } while (++page_idx < RESYNC_PAGES);
|
| 3000 |
|
| 3001 | r1_bio->sectors = nr_sectors;
|
| 3002 |
|
| 3003 | if (mddev_is_clustered(mddev) &&
|
| 3004 | conf->cluster_sync_high < sector_nr + nr_sectors) {
|
| 3005 | conf->cluster_sync_low = mddev->curr_resync_completed;
|
| 3006 | conf->cluster_sync_high = conf->cluster_sync_low + CLUSTER_RESYNC_WINDOW_SECTORS;
|
| 3007 |
|
| 3008 | mddev->cluster_ops->resync_info_update(mddev,
|
| 3009 | conf->cluster_sync_low,
|
| 3010 | conf->cluster_sync_high);
|
| 3011 | }
|
| 3012 |
|
| 3013 | |
| 3014 | * compare
|
| 3015 |
|
| 3016 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
|
| 3017 | atomic_set(&r1_bio->remaining, read_targets);
|
| 3018 | for (i = 0; i < conf->raid_disks * 2 && read_targets; i++) {
|
| 3019 | bio = r1_bio->bios[i];
|
| 3020 | if (bio->bi_end_io == end_sync_read) {
|
| 3021 | read_targets--;
|
| 3022 | if (read_targets == 1)
|
| 3023 | bio->bi_opf &= ~MD_FAILFAST;
|
| 3024 | submit_bio_noacct(bio);
|
| 3025 | }
|
| 3026 | }
|
| 3027 | } else {
|
| 3028 | atomic_set(&r1_bio->remaining, 1);
|
| 3029 | bio = r1_bio->bios[r1_bio->read_disk];
|
| 3030 | if (read_targets == 1)
|
| 3031 | bio->bi_opf &= ~MD_FAILFAST;
|
| 3032 | submit_bio_noacct(bio);
|
| 3033 | }
|
| 3034 | return nr_sectors;
|
| 3035 | }
|
| 3036 |
|
| 3037 | static sector_t raid1_size(struct mddev *mddev, sector_t sectors, int raid_disks)
|
| 3038 | {
|
| 3039 | if (sectors)
|
| 3040 | return sectors;
|
| 3041 |
|
| 3042 | return mddev->dev_sectors;
|
| 3043 | }
|
| 3044 |
|
| 3045 | static struct r1conf *setup_conf(struct mddev *mddev)
|
| 3046 | {
|
| 3047 | struct r1conf *conf;
|
| 3048 | int i;
|
| 3049 | struct raid1_info *disk;
|
| 3050 | struct md_rdev *rdev;
|
| 3051 | size_t r1bio_size;
|
| 3052 | int err = -ENOMEM;
|
| 3053 |
|
| 3054 | conf = kzalloc_obj(struct r1conf);
|
| 3055 | if (!conf)
|
| 3056 | goto abort;
|
| 3057 |
|
| 3058 | conf->nr_pending = kzalloc_objs(atomic_t, BARRIER_BUCKETS_NR);
|
| 3059 | if (!conf->nr_pending)
|
| 3060 | goto abort;
|
| 3061 |
|
| 3062 | conf->nr_waiting = kzalloc_objs(atomic_t, BARRIER_BUCKETS_NR);
|
| 3063 | if (!conf->nr_waiting)
|
| 3064 | goto abort;
|
| 3065 |
|
| 3066 | conf->nr_queued = kzalloc_objs(atomic_t, BARRIER_BUCKETS_NR);
|
| 3067 | if (!conf->nr_queued)
|
| 3068 | goto abort;
|
| 3069 |
|
| 3070 | conf->barrier = kzalloc_objs(atomic_t, BARRIER_BUCKETS_NR);
|
| 3071 | if (!conf->barrier)
|
| 3072 | goto abort;
|
| 3073 |
|
| 3074 | conf->mirrors = kzalloc(array3_size(sizeof(struct raid1_info),
|
| 3075 | mddev->raid_disks, 2),
|
| 3076 | GFP_KERNEL);
|
| 3077 | if (!conf->mirrors)
|
| 3078 | goto abort;
|
| 3079 |
|
| 3080 | conf->tmppage = alloc_page(GFP_KERNEL);
|
| 3081 | if (!conf->tmppage)
|
| 3082 | goto abort;
|
| 3083 |
|
| 3084 | r1bio_size = offsetof(struct r1bio, bios[mddev->raid_disks * 2]);
|
| 3085 | conf->r1bio_pool = mempool_create_kmalloc_pool(NR_RAID_BIOS, r1bio_size);
|
| 3086 | if (!conf->r1bio_pool)
|
| 3087 | goto abort;
|
| 3088 |
|
| 3089 | err = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
|
| 3090 | if (err)
|
| 3091 | goto abort;
|
| 3092 |
|
| 3093 | err = -EINVAL;
|
| 3094 | spin_lock_init(&conf->device_lock);
|
| 3095 | conf->raid_disks = mddev->raid_disks;
|
| 3096 | rdev_for_each(rdev, mddev) {
|
| 3097 | int disk_idx = rdev->raid_disk;
|
| 3098 |
|
| 3099 | if (disk_idx >= conf->raid_disks || disk_idx < 0)
|
| 3100 | continue;
|
| 3101 |
|
| 3102 | if (!raid1_add_conf(conf, rdev, disk_idx,
|
| 3103 | test_bit(Replacement, &rdev->flags)))
|
| 3104 | goto abort;
|
| 3105 | }
|
| 3106 | conf->mddev = mddev;
|
| 3107 | INIT_LIST_HEAD(&conf->retry_list);
|
| 3108 | INIT_LIST_HEAD(&conf->bio_end_io_list);
|
| 3109 |
|
| 3110 | spin_lock_init(&conf->resync_lock);
|
| 3111 | init_waitqueue_head(&conf->wait_barrier);
|
| 3112 |
|
| 3113 | bio_list_init(&conf->pending_bio_list);
|
| 3114 |
|
| 3115 | err = -EIO;
|
| 3116 | for (i = 0; i < conf->raid_disks * 2; i++) {
|
| 3117 |
|
| 3118 | disk = conf->mirrors + i;
|
| 3119 |
|
| 3120 | if (i < conf->raid_disks &&
|
| 3121 | disk[conf->raid_disks].rdev) {
|
| 3122 |
|
| 3123 | if (!disk->rdev) {
|
| 3124 | |
| 3125 | * a recovering spare
|
| 3126 |
|
| 3127 | disk->rdev =
|
| 3128 | disk[conf->raid_disks].rdev;
|
| 3129 | disk[conf->raid_disks].rdev = NULL;
|
| 3130 | } else if (!test_bit(In_sync, &disk->rdev->flags))
|
| 3131 |
|
| 3132 | goto abort;
|
| 3133 | }
|
| 3134 |
|
| 3135 | if (!disk->rdev ||
|
| 3136 | !test_bit(In_sync, &disk->rdev->flags)) {
|
| 3137 | disk->head_position = 0;
|
| 3138 | if (disk->rdev &&
|
| 3139 | (disk->rdev->saved_raid_disk < 0))
|
| 3140 | conf->fullsync = 1;
|
| 3141 | }
|
| 3142 | }
|
| 3143 |
|
| 3144 | err = -ENOMEM;
|
| 3145 | rcu_assign_pointer(conf->thread,
|
| 3146 | md_register_thread(raid1d, mddev, "raid1"));
|
| 3147 | if (!conf->thread)
|
| 3148 | goto abort;
|
| 3149 |
|
| 3150 | return conf;
|
| 3151 |
|
| 3152 | abort:
|
| 3153 | if (conf) {
|
| 3154 | mempool_destroy(conf->r1bio_pool);
|
| 3155 | kfree(conf->mirrors);
|
| 3156 | safe_put_page(conf->tmppage);
|
| 3157 | kfree(conf->nr_pending);
|
| 3158 | kfree(conf->nr_waiting);
|
| 3159 | kfree(conf->nr_queued);
|
| 3160 | kfree(conf->barrier);
|
| 3161 | bioset_exit(&conf->bio_split);
|
| 3162 | kfree(conf);
|
| 3163 | }
|
| 3164 | return ERR_PTR(err);
|
| 3165 | }
|
| 3166 |
|
| 3167 | static int raid1_set_limits(struct mddev *mddev)
|
| 3168 | {
|
| 3169 | struct queue_limits lim;
|
| 3170 | int err;
|
| 3171 |
|
| 3172 | md_init_stacking_limits(&lim);
|
| 3173 | lim.max_write_zeroes_sectors = 0;
|
| 3174 | lim.max_hw_wzeroes_unmap_sectors = 0;
|
| 3175 | lim.chunk_sectors = BARRIER_UNIT_SECTOR_SIZE;
|
| 3176 | lim.logical_block_size = mddev->logical_block_size;
|
| 3177 | lim.features |= BLK_FEAT_ATOMIC_WRITES;
|
| 3178 | lim.features |= BLK_FEAT_PCI_P2PDMA;
|
| 3179 | err = mddev_stack_rdev_limits(mddev, &lim, MDDEV_STACK_INTEGRITY);
|
| 3180 | if (err)
|
| 3181 | return err;
|
| 3182 | return queue_limits_set(mddev->gendisk->queue, &lim);
|
| 3183 | }
|
| 3184 |
|
| 3185 | static int raid1_run(struct mddev *mddev)
|
| 3186 | {
|
| 3187 | struct r1conf *conf;
|
| 3188 | int i;
|
| 3189 | int ret;
|
| 3190 |
|
| 3191 | if (mddev->level != 1) {
|
| 3192 | pr_warn("md/raid1:%s: raid level not set to mirroring (%d)\n",
|
| 3193 | mdname(mddev), mddev->level);
|
| 3194 | return -EIO;
|
| 3195 | }
|
| 3196 | if (mddev->reshape_position != MaxSector) {
|
| 3197 | pr_warn("md/raid1:%s: reshape_position set but not supported\n",
|
| 3198 | mdname(mddev));
|
| 3199 | return -EIO;
|
| 3200 | }
|
| 3201 |
|
| 3202 | |
| 3203 | * copy the already verified devices into our private RAID1
|
| 3204 | * bookkeeping area. [whatever we allocate in run(),
|
| 3205 | * should be freed in raid1_free()]
|
| 3206 |
|
| 3207 | if (mddev->private == NULL)
|
| 3208 | conf = setup_conf(mddev);
|
| 3209 | else
|
| 3210 | conf = mddev->private;
|
| 3211 |
|
| 3212 | if (IS_ERR(conf))
|
| 3213 | return PTR_ERR(conf);
|
| 3214 |
|
| 3215 | if (!mddev_is_dm(mddev)) {
|
| 3216 | ret = raid1_set_limits(mddev);
|
| 3217 | if (ret) {
|
| 3218 | md_unregister_thread(mddev, &conf->thread);
|
| 3219 | if (!mddev->private)
|
| 3220 | raid1_free(mddev, conf);
|
| 3221 | return ret;
|
| 3222 | }
|
| 3223 | }
|
| 3224 |
|
| 3225 | mddev->degraded = 0;
|
| 3226 | for (i = 0; i < conf->raid_disks; i++)
|
| 3227 | if (conf->mirrors[i].rdev == NULL ||
|
| 3228 | !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
|
| 3229 | test_bit(Faulty, &conf->mirrors[i].rdev->flags))
|
| 3230 | mddev->degraded++;
|
| 3231 | |
| 3232 | * RAID1 needs at least one disk in active
|
| 3233 |
|
| 3234 | if (conf->raid_disks - mddev->degraded < 1) {
|
| 3235 | md_unregister_thread(mddev, &conf->thread);
|
| 3236 | if (!mddev->private)
|
| 3237 | raid1_free(mddev, conf);
|
| 3238 | return -EINVAL;
|
| 3239 | }
|
| 3240 |
|
| 3241 | if (conf->raid_disks - mddev->degraded == 1)
|
| 3242 | mddev->resync_offset = MaxSector;
|
| 3243 |
|
| 3244 | if (mddev->resync_offset != MaxSector)
|
| 3245 | pr_info("md/raid1:%s: not clean -- starting background reconstruction\n",
|
| 3246 | mdname(mddev));
|
| 3247 | pr_info("md/raid1:%s: active with %d out of %d mirrors\n",
|
| 3248 | mdname(mddev), mddev->raid_disks - mddev->degraded,
|
| 3249 | mddev->raid_disks);
|
| 3250 |
|
| 3251 | |
| 3252 | * Ok, everything is just fine now
|
| 3253 |
|
| 3254 | rcu_assign_pointer(mddev->thread, conf->thread);
|
| 3255 | rcu_assign_pointer(conf->thread, NULL);
|
| 3256 | mddev->private = conf;
|
| 3257 | set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
|
| 3258 |
|
| 3259 | md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
|
| 3260 |
|
| 3261 | ret = md_integrity_register(mddev);
|
| 3262 | if (ret)
|
| 3263 | md_unregister_thread(mddev, &mddev->thread);
|
| 3264 | return ret;
|
| 3265 | }
|
| 3266 |
|
| 3267 | static void raid1_free(struct mddev *mddev, void *priv)
|
| 3268 | {
|
| 3269 | struct r1conf *conf = priv;
|
| 3270 |
|
| 3271 | mempool_destroy(conf->r1bio_pool);
|
| 3272 | kfree(conf->mirrors);
|
| 3273 | safe_put_page(conf->tmppage);
|
| 3274 | kfree(conf->nr_pending);
|
| 3275 | kfree(conf->nr_waiting);
|
| 3276 | kfree(conf->nr_queued);
|
| 3277 | kfree(conf->barrier);
|
| 3278 | bioset_exit(&conf->bio_split);
|
| 3279 | kfree(conf);
|
| 3280 | }
|
| 3281 |
|
| 3282 | static int raid1_resize(struct mddev *mddev, sector_t sectors)
|
| 3283 | {
|
| 3284 | |
| 3285 | * on all devices, so we can resize.
|
| 3286 | * We need to make sure resync covers any new space.
|
| 3287 | * If the array is shrinking we should possibly wait until
|
| 3288 | * any io in the removed space completes, but it hardly seems
|
| 3289 | * worth it.
|
| 3290 |
|
| 3291 | sector_t newsize = raid1_size(mddev, sectors, 0);
|
| 3292 |
|
| 3293 | if (mddev->external_size &&
|
| 3294 | mddev->array_sectors > newsize)
|
| 3295 | return -EINVAL;
|
| 3296 |
|
| 3297 | if (md_bitmap_enabled(mddev, false)) {
|
| 3298 | int ret = mddev->bitmap_ops->resize(mddev, newsize, 0);
|
| 3299 |
|
| 3300 | if (ret)
|
| 3301 | return ret;
|
| 3302 | }
|
| 3303 |
|
| 3304 | md_set_array_sectors(mddev, newsize);
|
| 3305 | if (sectors > mddev->dev_sectors &&
|
| 3306 | mddev->resync_offset > mddev->dev_sectors) {
|
| 3307 | mddev->resync_offset = mddev->dev_sectors;
|
| 3308 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
|
| 3309 | }
|
| 3310 | mddev->dev_sectors = sectors;
|
| 3311 | mddev->resync_max_sectors = sectors;
|
| 3312 | return 0;
|
| 3313 | }
|
| 3314 |
|
| 3315 | static int raid1_reshape(struct mddev *mddev)
|
| 3316 | {
|
| 3317 | |
| 3318 | * 1/ resize the r1bio_pool
|
| 3319 | * 2/ resize conf->mirrors
|
| 3320 | *
|
| 3321 | * We allocate a new r1bio_pool if we can.
|
| 3322 | * Then raise a device barrier and wait until all IO stops.
|
| 3323 | * Then resize conf->mirrors and swap in the new r1bio pool.
|
| 3324 | *
|
| 3325 | * At the same time, we "pack" the devices so that all the missing
|
| 3326 | * devices have the higher raid_disk numbers.
|
| 3327 |
|
| 3328 | mempool_t *newpool, *oldpool;
|
| 3329 | size_t new_r1bio_size;
|
| 3330 | struct raid1_info *newmirrors;
|
| 3331 | struct r1conf *conf = mddev->private;
|
| 3332 | int cnt, raid_disks;
|
| 3333 | unsigned long flags;
|
| 3334 | int d, d2;
|
| 3335 |
|
| 3336 |
|
| 3337 | if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
|
| 3338 | mddev->layout != mddev->new_layout ||
|
| 3339 | mddev->level != mddev->new_level) {
|
| 3340 | mddev->new_chunk_sectors = mddev->chunk_sectors;
|
| 3341 | mddev->new_layout = mddev->layout;
|
| 3342 | mddev->new_level = mddev->level;
|
| 3343 | return -EINVAL;
|
| 3344 | }
|
| 3345 |
|
| 3346 | if (!mddev_is_clustered(mddev))
|
| 3347 | md_allow_write(mddev);
|
| 3348 |
|
| 3349 | raid_disks = mddev->raid_disks + mddev->delta_disks;
|
| 3350 |
|
| 3351 | if (raid_disks < conf->raid_disks) {
|
| 3352 | cnt=0;
|
| 3353 | for (d= 0; d < conf->raid_disks; d++)
|
| 3354 | if (conf->mirrors[d].rdev)
|
| 3355 | cnt++;
|
| 3356 | if (cnt > raid_disks)
|
| 3357 | return -EBUSY;
|
| 3358 | }
|
| 3359 |
|
| 3360 | new_r1bio_size = offsetof(struct r1bio, bios[raid_disks * 2]);
|
| 3361 | newpool = mempool_create_kmalloc_pool(NR_RAID_BIOS, new_r1bio_size);
|
| 3362 | if (!newpool) {
|
| 3363 | return -ENOMEM;
|
| 3364 | }
|
| 3365 | newmirrors = kzalloc(array3_size(sizeof(struct raid1_info),
|
| 3366 | raid_disks, 2),
|
| 3367 | GFP_KERNEL);
|
| 3368 | if (!newmirrors) {
|
| 3369 | mempool_destroy(newpool);
|
| 3370 | return -ENOMEM;
|
| 3371 | }
|
| 3372 |
|
| 3373 | freeze_array(conf, 0);
|
| 3374 |
|
| 3375 |
|
| 3376 | oldpool = conf->r1bio_pool;
|
| 3377 | conf->r1bio_pool = newpool;
|
| 3378 |
|
| 3379 | for (d = d2 = 0; d < conf->raid_disks; d++) {
|
| 3380 | struct md_rdev *rdev = conf->mirrors[d].rdev;
|
| 3381 | if (rdev && rdev->raid_disk != d2) {
|
| 3382 | sysfs_unlink_rdev(mddev, rdev);
|
| 3383 | rdev->raid_disk = d2;
|
| 3384 | sysfs_unlink_rdev(mddev, rdev);
|
| 3385 | if (sysfs_link_rdev(mddev, rdev))
|
| 3386 | pr_warn("md/raid1:%s: cannot register rd%d\n",
|
| 3387 | mdname(mddev), rdev->raid_disk);
|
| 3388 | }
|
| 3389 | if (rdev)
|
| 3390 | newmirrors[d2++].rdev = rdev;
|
| 3391 | }
|
| 3392 | kfree(conf->mirrors);
|
| 3393 | conf->mirrors = newmirrors;
|
| 3394 |
|
| 3395 | spin_lock_irqsave(&conf->device_lock, flags);
|
| 3396 | mddev->degraded += (raid_disks - conf->raid_disks);
|
| 3397 | spin_unlock_irqrestore(&conf->device_lock, flags);
|
| 3398 | conf->raid_disks = mddev->raid_disks = raid_disks;
|
| 3399 | mddev->delta_disks = 0;
|
| 3400 |
|
| 3401 | unfreeze_array(conf);
|
| 3402 |
|
| 3403 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
|
| 3404 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
|
| 3405 | md_wakeup_thread(mddev->thread);
|
| 3406 |
|
| 3407 | mempool_destroy(oldpool);
|
| 3408 | return 0;
|
| 3409 | }
|
| 3410 |
|
| 3411 | static void raid1_quiesce(struct mddev *mddev, int quiesce)
|
| 3412 | {
|
| 3413 | struct r1conf *conf = mddev->private;
|
| 3414 |
|
| 3415 | if (quiesce)
|
| 3416 | freeze_array(conf, 0);
|
| 3417 | else
|
| 3418 | unfreeze_array(conf);
|
| 3419 | }
|
| 3420 |
|
| 3421 | static void *raid1_takeover(struct mddev *mddev)
|
| 3422 | {
|
| 3423 | |
| 3424 | * raid5 with 2 devices, any layout or chunk size
|
| 3425 |
|
| 3426 | if (mddev->level == 5 && mddev->raid_disks == 2) {
|
| 3427 | struct r1conf *conf;
|
| 3428 | mddev->new_level = 1;
|
| 3429 | mddev->new_layout = 0;
|
| 3430 | mddev->new_chunk_sectors = 0;
|
| 3431 | conf = setup_conf(mddev);
|
| 3432 | if (!IS_ERR(conf)) {
|
| 3433 | mddev_clear_unsupported_flags(mddev,
|
| 3434 | UNSUPPORTED_MDDEV_FLAGS);
|
| 3435 | }
|
| 3436 | return conf;
|
| 3437 | }
|
| 3438 | return ERR_PTR(-EINVAL);
|
| 3439 | }
|
| 3440 |
|
| 3441 | static struct md_personality raid1_personality =
|
| 3442 | {
|
| 3443 | .head = {
|
| 3444 | .type = MD_PERSONALITY,
|
| 3445 | .id = ID_RAID1,
|
| 3446 | .name = "raid1",
|
| 3447 | .owner = THIS_MODULE,
|
| 3448 | },
|
| 3449 |
|
| 3450 | .make_request = raid1_make_request,
|
| 3451 | .run = raid1_run,
|
| 3452 | .free = raid1_free,
|
| 3453 | .status = raid1_status,
|
| 3454 | .error_handler = raid1_error,
|
| 3455 | .hot_add_disk = raid1_add_disk,
|
| 3456 | .hot_remove_disk= raid1_remove_disk,
|
| 3457 | .spare_active = raid1_spare_active,
|
| 3458 | .sync_request = raid1_sync_request,
|
| 3459 | .resize = raid1_resize,
|
| 3460 | .size = raid1_size,
|
| 3461 | .check_reshape = raid1_reshape,
|
| 3462 | .quiesce = raid1_quiesce,
|
| 3463 | .takeover = raid1_takeover,
|
| 3464 | };
|
| 3465 |
|
| 3466 | static int __init raid1_init(void)
|
| 3467 | {
|
| 3468 | return register_md_submodule(&raid1_personality.head);
|
| 3469 | }
|
| 3470 |
|
| 3471 | static void __exit raid1_exit(void)
|
| 3472 | {
|
| 3473 | unregister_md_submodule(&raid1_personality.head);
|
| 3474 | }
|
| 3475 |
|
| 3476 | module_init(raid1_init);
|
| 3477 | module_exit(raid1_exit);
|
| 3478 | MODULE_LICENSE("GPL");
|
| 3479 | MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
|
| 3480 | MODULE_ALIAS("md-personality-3");
|
| 3481 | MODULE_ALIAS("md-raid1");
|
| 3482 | MODULE_ALIAS("md-level-1");
|