11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * raid1.c : Multiple Devices driver for Linux 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * RAID-1 management functions. 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000 111da177e4SLinus Torvalds * 1296de0e25SJan Engelhardt * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk> 131da177e4SLinus Torvalds * Various fixes by Neil Brown <neilb@cse.unsw.edu.au> 141da177e4SLinus Torvalds * 15191ea9b2SNeilBrown * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support 16191ea9b2SNeilBrown * bitmapped intelligence in resync: 17191ea9b2SNeilBrown * 18191ea9b2SNeilBrown * - bitmap marked during normal i/o 19191ea9b2SNeilBrown * - bitmap used to skip nondirty blocks during sync 20191ea9b2SNeilBrown * 21191ea9b2SNeilBrown * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology: 22191ea9b2SNeilBrown * - persistent bitmap code 23191ea9b2SNeilBrown * 241da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 251da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 261da177e4SLinus Torvalds * the Free Software Foundation; either version 2, or (at your option) 271da177e4SLinus Torvalds * any later version. 281da177e4SLinus Torvalds * 291da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License 301da177e4SLinus Torvalds * (for example /usr/src/linux/COPYING); if not, write to the Free 311da177e4SLinus Torvalds * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 321da177e4SLinus Torvalds */ 331da177e4SLinus Torvalds 345a0e3ad6STejun Heo #include <linux/slab.h> 3525570727SStephen Rothwell #include <linux/delay.h> 36bff61975SNeilBrown #include <linux/blkdev.h> 37056075c7SPaul Gortmaker #include <linux/module.h> 38bff61975SNeilBrown #include <linux/seq_file.h> 398bda470eSChristian Dietrich #include <linux/ratelimit.h> 4043b2e5d8SNeilBrown #include "md.h" 41ef740c37SChristoph Hellwig #include "raid1.h" 42ef740c37SChristoph Hellwig #include "bitmap.h" 43191ea9b2SNeilBrown 441da177e4SLinus Torvalds /* 451da177e4SLinus Torvalds * Number of guaranteed r1bios in case of extreme VM load: 461da177e4SLinus Torvalds */ 471da177e4SLinus Torvalds #define NR_RAID1_BIOS 256 481da177e4SLinus Torvalds 49473e87ceSJonathan Brassow /* when we get a read error on a read-only array, we redirect to another 50473e87ceSJonathan Brassow * device without failing the first device, or trying to over-write to 51473e87ceSJonathan Brassow * correct the read error. To keep track of bad blocks on a per-bio 52473e87ceSJonathan Brassow * level, we store IO_BLOCKED in the appropriate 'bios' pointer 53473e87ceSJonathan Brassow */ 54473e87ceSJonathan Brassow #define IO_BLOCKED ((struct bio *)1) 55473e87ceSJonathan Brassow /* When we successfully write to a known bad-block, we need to remove the 56473e87ceSJonathan Brassow * bad-block marking which must be done from process context. So we record 57473e87ceSJonathan Brassow * the success by setting devs[n].bio to IO_MADE_GOOD 58473e87ceSJonathan Brassow */ 59473e87ceSJonathan Brassow #define IO_MADE_GOOD ((struct bio *)2) 60473e87ceSJonathan Brassow 61473e87ceSJonathan Brassow #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2) 62473e87ceSJonathan Brassow 6334db0cd6SNeilBrown /* When there are this many requests queue to be written by 6434db0cd6SNeilBrown * the raid1 thread, we become 'congested' to provide back-pressure 6534db0cd6SNeilBrown * for writeback. 6634db0cd6SNeilBrown */ 6734db0cd6SNeilBrown static int max_queued_requests = 1024; 681da177e4SLinus Torvalds 6979ef3a8aSmajianpeng static void allow_barrier(struct r1conf *conf, sector_t start_next_window, 7079ef3a8aSmajianpeng sector_t bi_sector); 71e8096360SNeilBrown static void lower_barrier(struct r1conf *conf); 721da177e4SLinus Torvalds 73dd0fc66fSAl Viro static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data) 741da177e4SLinus Torvalds { 751da177e4SLinus Torvalds struct pool_info *pi = data; 769f2c9d12SNeilBrown int size = offsetof(struct r1bio, bios[pi->raid_disks]); 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds /* allocate a r1bio with room for raid_disks entries in the bios array */ 797eaceaccSJens Axboe return kzalloc(size, gfp_flags); 801da177e4SLinus Torvalds } 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds static void r1bio_pool_free(void *r1_bio, void *data) 831da177e4SLinus Torvalds { 841da177e4SLinus Torvalds kfree(r1_bio); 851da177e4SLinus Torvalds } 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds #define RESYNC_BLOCK_SIZE (64*1024) 888e005f7cSmajianpeng #define RESYNC_DEPTH 32 891da177e4SLinus Torvalds #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9) 901da177e4SLinus Torvalds #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE) 918e005f7cSmajianpeng #define RESYNC_WINDOW (RESYNC_BLOCK_SIZE * RESYNC_DEPTH) 928e005f7cSmajianpeng #define RESYNC_WINDOW_SECTORS (RESYNC_WINDOW >> 9) 938e005f7cSmajianpeng #define NEXT_NORMALIO_DISTANCE (3 * RESYNC_WINDOW_SECTORS) 941da177e4SLinus Torvalds 95dd0fc66fSAl Viro static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data) 961da177e4SLinus Torvalds { 971da177e4SLinus Torvalds struct pool_info *pi = data; 989f2c9d12SNeilBrown struct r1bio *r1_bio; 991da177e4SLinus Torvalds struct bio *bio; 100da1aab3dSNeilBrown int need_pages; 1011da177e4SLinus Torvalds int i, j; 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds r1_bio = r1bio_pool_alloc(gfp_flags, pi); 1047eaceaccSJens Axboe if (!r1_bio) 1051da177e4SLinus Torvalds return NULL; 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds /* 1081da177e4SLinus Torvalds * Allocate bios : 1 for reading, n-1 for writing 1091da177e4SLinus Torvalds */ 1101da177e4SLinus Torvalds for (j = pi->raid_disks ; j-- ; ) { 1116746557fSNeilBrown bio = bio_kmalloc(gfp_flags, RESYNC_PAGES); 1121da177e4SLinus Torvalds if (!bio) 1131da177e4SLinus Torvalds goto out_free_bio; 1141da177e4SLinus Torvalds r1_bio->bios[j] = bio; 1151da177e4SLinus Torvalds } 1161da177e4SLinus Torvalds /* 1171da177e4SLinus Torvalds * Allocate RESYNC_PAGES data pages and attach them to 118d11c171eSNeilBrown * the first bio. 119d11c171eSNeilBrown * If this is a user-requested check/repair, allocate 120d11c171eSNeilBrown * RESYNC_PAGES for each bio. 1211da177e4SLinus Torvalds */ 122d11c171eSNeilBrown if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) 123da1aab3dSNeilBrown need_pages = pi->raid_disks; 124d11c171eSNeilBrown else 125da1aab3dSNeilBrown need_pages = 1; 126da1aab3dSNeilBrown for (j = 0; j < need_pages; j++) { 127d11c171eSNeilBrown bio = r1_bio->bios[j]; 128a0787606SKent Overstreet bio->bi_vcnt = RESYNC_PAGES; 1291da177e4SLinus Torvalds 130a0787606SKent Overstreet if (bio_alloc_pages(bio, gfp_flags)) 131da1aab3dSNeilBrown goto out_free_pages; 132d11c171eSNeilBrown } 133d11c171eSNeilBrown /* If not user-requests, copy the page pointers to all bios */ 134d11c171eSNeilBrown if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) { 135d11c171eSNeilBrown for (i=0; i<RESYNC_PAGES ; i++) 136d11c171eSNeilBrown for (j=1; j<pi->raid_disks; j++) 137d11c171eSNeilBrown r1_bio->bios[j]->bi_io_vec[i].bv_page = 138d11c171eSNeilBrown r1_bio->bios[0]->bi_io_vec[i].bv_page; 139d11c171eSNeilBrown } 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds r1_bio->master_bio = NULL; 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds return r1_bio; 1441da177e4SLinus Torvalds 145da1aab3dSNeilBrown out_free_pages: 146da1aab3dSNeilBrown while (--j >= 0) { 147da1aab3dSNeilBrown struct bio_vec *bv; 148da1aab3dSNeilBrown 149da1aab3dSNeilBrown bio_for_each_segment_all(bv, r1_bio->bios[j], i) 150da1aab3dSNeilBrown __free_page(bv->bv_page); 151da1aab3dSNeilBrown } 152da1aab3dSNeilBrown 1531da177e4SLinus Torvalds out_free_bio: 1541da177e4SLinus Torvalds while (++j < pi->raid_disks) 1551da177e4SLinus Torvalds bio_put(r1_bio->bios[j]); 1561da177e4SLinus Torvalds r1bio_pool_free(r1_bio, data); 1571da177e4SLinus Torvalds return NULL; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds static void r1buf_pool_free(void *__r1_bio, void *data) 1611da177e4SLinus Torvalds { 1621da177e4SLinus Torvalds struct pool_info *pi = data; 163d11c171eSNeilBrown int i,j; 1649f2c9d12SNeilBrown struct r1bio *r1bio = __r1_bio; 1651da177e4SLinus Torvalds 166d11c171eSNeilBrown for (i = 0; i < RESYNC_PAGES; i++) 167d11c171eSNeilBrown for (j = pi->raid_disks; j-- ;) { 168d11c171eSNeilBrown if (j == 0 || 169d11c171eSNeilBrown r1bio->bios[j]->bi_io_vec[i].bv_page != 170d11c171eSNeilBrown r1bio->bios[0]->bi_io_vec[i].bv_page) 1711345b1d8SNeilBrown safe_put_page(r1bio->bios[j]->bi_io_vec[i].bv_page); 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds for (i=0 ; i < pi->raid_disks; i++) 1741da177e4SLinus Torvalds bio_put(r1bio->bios[i]); 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds r1bio_pool_free(r1bio, data); 1771da177e4SLinus Torvalds } 1781da177e4SLinus Torvalds 179e8096360SNeilBrown static void put_all_bios(struct r1conf *conf, struct r1bio *r1_bio) 1801da177e4SLinus Torvalds { 1811da177e4SLinus Torvalds int i; 1821da177e4SLinus Torvalds 1838f19ccb2SNeilBrown for (i = 0; i < conf->raid_disks * 2; i++) { 1841da177e4SLinus Torvalds struct bio **bio = r1_bio->bios + i; 1854367af55SNeilBrown if (!BIO_SPECIAL(*bio)) 1861da177e4SLinus Torvalds bio_put(*bio); 1871da177e4SLinus Torvalds *bio = NULL; 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds 1919f2c9d12SNeilBrown static void free_r1bio(struct r1bio *r1_bio) 1921da177e4SLinus Torvalds { 193e8096360SNeilBrown struct r1conf *conf = r1_bio->mddev->private; 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds put_all_bios(conf, r1_bio); 1961da177e4SLinus Torvalds mempool_free(r1_bio, conf->r1bio_pool); 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 1999f2c9d12SNeilBrown static void put_buf(struct r1bio *r1_bio) 2001da177e4SLinus Torvalds { 201e8096360SNeilBrown struct r1conf *conf = r1_bio->mddev->private; 2023e198f78SNeilBrown int i; 2033e198f78SNeilBrown 2048f19ccb2SNeilBrown for (i = 0; i < conf->raid_disks * 2; i++) { 2053e198f78SNeilBrown struct bio *bio = r1_bio->bios[i]; 2063e198f78SNeilBrown if (bio->bi_end_io) 2073e198f78SNeilBrown rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev); 2083e198f78SNeilBrown } 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds mempool_free(r1_bio, conf->r1buf_pool); 2111da177e4SLinus Torvalds 21217999be4SNeilBrown lower_barrier(conf); 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds 2159f2c9d12SNeilBrown static void reschedule_retry(struct r1bio *r1_bio) 2161da177e4SLinus Torvalds { 2171da177e4SLinus Torvalds unsigned long flags; 218fd01b88cSNeilBrown struct mddev *mddev = r1_bio->mddev; 219e8096360SNeilBrown struct r1conf *conf = mddev->private; 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds spin_lock_irqsave(&conf->device_lock, flags); 2221da177e4SLinus Torvalds list_add(&r1_bio->retry_list, &conf->retry_list); 223ddaf22abSNeilBrown conf->nr_queued ++; 2241da177e4SLinus Torvalds spin_unlock_irqrestore(&conf->device_lock, flags); 2251da177e4SLinus Torvalds 22617999be4SNeilBrown wake_up(&conf->wait_barrier); 2271da177e4SLinus Torvalds md_wakeup_thread(mddev->thread); 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds /* 2311da177e4SLinus Torvalds * raid_end_bio_io() is called when we have finished servicing a mirrored 2321da177e4SLinus Torvalds * operation and are ready to return a success/failure code to the buffer 2331da177e4SLinus Torvalds * cache layer. 2341da177e4SLinus Torvalds */ 2359f2c9d12SNeilBrown static void call_bio_endio(struct r1bio *r1_bio) 236d2eb35acSNeilBrown { 237d2eb35acSNeilBrown struct bio *bio = r1_bio->master_bio; 238d2eb35acSNeilBrown int done; 239e8096360SNeilBrown struct r1conf *conf = r1_bio->mddev->private; 24079ef3a8aSmajianpeng sector_t start_next_window = r1_bio->start_next_window; 2414f024f37SKent Overstreet sector_t bi_sector = bio->bi_iter.bi_sector; 242d2eb35acSNeilBrown 243d2eb35acSNeilBrown if (bio->bi_phys_segments) { 244d2eb35acSNeilBrown unsigned long flags; 245d2eb35acSNeilBrown spin_lock_irqsave(&conf->device_lock, flags); 246d2eb35acSNeilBrown bio->bi_phys_segments--; 247d2eb35acSNeilBrown done = (bio->bi_phys_segments == 0); 248d2eb35acSNeilBrown spin_unlock_irqrestore(&conf->device_lock, flags); 24979ef3a8aSmajianpeng /* 25079ef3a8aSmajianpeng * make_request() might be waiting for 25179ef3a8aSmajianpeng * bi_phys_segments to decrease 25279ef3a8aSmajianpeng */ 25379ef3a8aSmajianpeng wake_up(&conf->wait_barrier); 254d2eb35acSNeilBrown } else 255d2eb35acSNeilBrown done = 1; 256d2eb35acSNeilBrown 257d2eb35acSNeilBrown if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) 258d2eb35acSNeilBrown clear_bit(BIO_UPTODATE, &bio->bi_flags); 259d2eb35acSNeilBrown if (done) { 260d2eb35acSNeilBrown bio_endio(bio, 0); 261d2eb35acSNeilBrown /* 262d2eb35acSNeilBrown * Wake up any possible resync thread that waits for the device 263d2eb35acSNeilBrown * to go idle. 264d2eb35acSNeilBrown */ 26579ef3a8aSmajianpeng allow_barrier(conf, start_next_window, bi_sector); 266d2eb35acSNeilBrown } 267d2eb35acSNeilBrown } 268d2eb35acSNeilBrown 2699f2c9d12SNeilBrown static void raid_end_bio_io(struct r1bio *r1_bio) 2701da177e4SLinus Torvalds { 2711da177e4SLinus Torvalds struct bio *bio = r1_bio->master_bio; 2721da177e4SLinus Torvalds 2734b6d287fSNeilBrown /* if nobody has done the final endio yet, do it now */ 2744b6d287fSNeilBrown if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) { 27536a4e1feSNeilBrown pr_debug("raid1: sync end %s on sectors %llu-%llu\n", 2764b6d287fSNeilBrown (bio_data_dir(bio) == WRITE) ? "write" : "read", 2774f024f37SKent Overstreet (unsigned long long) bio->bi_iter.bi_sector, 2784f024f37SKent Overstreet (unsigned long long) bio_end_sector(bio) - 1); 2794b6d287fSNeilBrown 280d2eb35acSNeilBrown call_bio_endio(r1_bio); 2814b6d287fSNeilBrown } 2821da177e4SLinus Torvalds free_r1bio(r1_bio); 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds /* 2861da177e4SLinus Torvalds * Update disk head position estimator based on IRQ completion info. 2871da177e4SLinus Torvalds */ 2889f2c9d12SNeilBrown static inline void update_head_pos(int disk, struct r1bio *r1_bio) 2891da177e4SLinus Torvalds { 290e8096360SNeilBrown struct r1conf *conf = r1_bio->mddev->private; 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds conf->mirrors[disk].head_position = 2931da177e4SLinus Torvalds r1_bio->sector + (r1_bio->sectors); 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds 296ba3ae3beSNamhyung Kim /* 297ba3ae3beSNamhyung Kim * Find the disk number which triggered given bio 298ba3ae3beSNamhyung Kim */ 2999f2c9d12SNeilBrown static int find_bio_disk(struct r1bio *r1_bio, struct bio *bio) 300ba3ae3beSNamhyung Kim { 301ba3ae3beSNamhyung Kim int mirror; 30230194636SNeilBrown struct r1conf *conf = r1_bio->mddev->private; 30330194636SNeilBrown int raid_disks = conf->raid_disks; 304ba3ae3beSNamhyung Kim 3058f19ccb2SNeilBrown for (mirror = 0; mirror < raid_disks * 2; mirror++) 306ba3ae3beSNamhyung Kim if (r1_bio->bios[mirror] == bio) 307ba3ae3beSNamhyung Kim break; 308ba3ae3beSNamhyung Kim 3098f19ccb2SNeilBrown BUG_ON(mirror == raid_disks * 2); 310ba3ae3beSNamhyung Kim update_head_pos(mirror, r1_bio); 311ba3ae3beSNamhyung Kim 312ba3ae3beSNamhyung Kim return mirror; 313ba3ae3beSNamhyung Kim } 314ba3ae3beSNamhyung Kim 3156712ecf8SNeilBrown static void raid1_end_read_request(struct bio *bio, int error) 3161da177e4SLinus Torvalds { 3171da177e4SLinus Torvalds int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); 3189f2c9d12SNeilBrown struct r1bio *r1_bio = bio->bi_private; 3191da177e4SLinus Torvalds int mirror; 320e8096360SNeilBrown struct r1conf *conf = r1_bio->mddev->private; 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds mirror = r1_bio->read_disk; 3231da177e4SLinus Torvalds /* 3241da177e4SLinus Torvalds * this branch is our 'one mirror IO has finished' event handler: 3251da177e4SLinus Torvalds */ 326ddaf22abSNeilBrown update_head_pos(mirror, r1_bio); 327ddaf22abSNeilBrown 328220946c9SNeilBrown if (uptodate) 3291da177e4SLinus Torvalds set_bit(R1BIO_Uptodate, &r1_bio->state); 330dd00a99eSNeilBrown else { 331dd00a99eSNeilBrown /* If all other devices have failed, we want to return 332dd00a99eSNeilBrown * the error upwards rather than fail the last device. 333dd00a99eSNeilBrown * Here we redefine "uptodate" to mean "Don't want to retry" 334dd00a99eSNeilBrown */ 335dd00a99eSNeilBrown unsigned long flags; 336dd00a99eSNeilBrown spin_lock_irqsave(&conf->device_lock, flags); 337dd00a99eSNeilBrown if (r1_bio->mddev->degraded == conf->raid_disks || 338dd00a99eSNeilBrown (r1_bio->mddev->degraded == conf->raid_disks-1 && 339dd00a99eSNeilBrown !test_bit(Faulty, &conf->mirrors[mirror].rdev->flags))) 340dd00a99eSNeilBrown uptodate = 1; 341dd00a99eSNeilBrown spin_unlock_irqrestore(&conf->device_lock, flags); 342dd00a99eSNeilBrown } 3431da177e4SLinus Torvalds 3447ad4d4a6SNeilBrown if (uptodate) { 3451da177e4SLinus Torvalds raid_end_bio_io(r1_bio); 3467ad4d4a6SNeilBrown rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev); 3477ad4d4a6SNeilBrown } else { 3481da177e4SLinus Torvalds /* 3491da177e4SLinus Torvalds * oops, read error: 3501da177e4SLinus Torvalds */ 3511da177e4SLinus Torvalds char b[BDEVNAME_SIZE]; 3528bda470eSChristian Dietrich printk_ratelimited( 3538bda470eSChristian Dietrich KERN_ERR "md/raid1:%s: %s: " 3548bda470eSChristian Dietrich "rescheduling sector %llu\n", 3559dd1e2faSNeilBrown mdname(conf->mddev), 3568bda470eSChristian Dietrich bdevname(conf->mirrors[mirror].rdev->bdev, 3578bda470eSChristian Dietrich b), 3588bda470eSChristian Dietrich (unsigned long long)r1_bio->sector); 359d2eb35acSNeilBrown set_bit(R1BIO_ReadError, &r1_bio->state); 3601da177e4SLinus Torvalds reschedule_retry(r1_bio); 3617ad4d4a6SNeilBrown /* don't drop the reference on read_disk yet */ 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds } 3641da177e4SLinus Torvalds 3659f2c9d12SNeilBrown static void close_write(struct r1bio *r1_bio) 3664e78064fSNeilBrown { 3674e78064fSNeilBrown /* it really is the end of this request */ 3684e78064fSNeilBrown if (test_bit(R1BIO_BehindIO, &r1_bio->state)) { 3694e78064fSNeilBrown /* free extra copy of the data pages */ 370af6d7b76SNeilBrown int i = r1_bio->behind_page_count; 3714e78064fSNeilBrown while (i--) 3722ca68f5eSNeilBrown safe_put_page(r1_bio->behind_bvecs[i].bv_page); 3732ca68f5eSNeilBrown kfree(r1_bio->behind_bvecs); 3742ca68f5eSNeilBrown r1_bio->behind_bvecs = NULL; 3754e78064fSNeilBrown } 3764e78064fSNeilBrown /* clear the bitmap if all writes complete successfully */ 3774e78064fSNeilBrown bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector, 3784e78064fSNeilBrown r1_bio->sectors, 3794e78064fSNeilBrown !test_bit(R1BIO_Degraded, &r1_bio->state), 380af6d7b76SNeilBrown test_bit(R1BIO_BehindIO, &r1_bio->state)); 3814e78064fSNeilBrown md_write_end(r1_bio->mddev); 382cd5ff9a1SNeilBrown } 383cd5ff9a1SNeilBrown 3849f2c9d12SNeilBrown static void r1_bio_write_done(struct r1bio *r1_bio) 385cd5ff9a1SNeilBrown { 386cd5ff9a1SNeilBrown if (!atomic_dec_and_test(&r1_bio->remaining)) 387cd5ff9a1SNeilBrown return; 388cd5ff9a1SNeilBrown 389cd5ff9a1SNeilBrown if (test_bit(R1BIO_WriteError, &r1_bio->state)) 390cd5ff9a1SNeilBrown reschedule_retry(r1_bio); 391cd5ff9a1SNeilBrown else { 392cd5ff9a1SNeilBrown close_write(r1_bio); 3934367af55SNeilBrown if (test_bit(R1BIO_MadeGood, &r1_bio->state)) 3944367af55SNeilBrown reschedule_retry(r1_bio); 3954367af55SNeilBrown else 3964e78064fSNeilBrown raid_end_bio_io(r1_bio); 3974e78064fSNeilBrown } 3984e78064fSNeilBrown } 3994e78064fSNeilBrown 4006712ecf8SNeilBrown static void raid1_end_write_request(struct bio *bio, int error) 4011da177e4SLinus Torvalds { 4021da177e4SLinus Torvalds int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); 4039f2c9d12SNeilBrown struct r1bio *r1_bio = bio->bi_private; 404a9701a30SNeilBrown int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state); 405e8096360SNeilBrown struct r1conf *conf = r1_bio->mddev->private; 40604b857f7SNeilBrown struct bio *to_put = NULL; 4071da177e4SLinus Torvalds 408ba3ae3beSNamhyung Kim mirror = find_bio_disk(r1_bio, bio); 4091da177e4SLinus Torvalds 4101da177e4SLinus Torvalds /* 411e9c7469bSTejun Heo * 'one mirror IO has finished' event handler: 4121da177e4SLinus Torvalds */ 413191ea9b2SNeilBrown if (!uptodate) { 414cd5ff9a1SNeilBrown set_bit(WriteErrorSeen, 415cd5ff9a1SNeilBrown &conf->mirrors[mirror].rdev->flags); 41619d67169SNeilBrown if (!test_and_set_bit(WantReplacement, 41719d67169SNeilBrown &conf->mirrors[mirror].rdev->flags)) 41819d67169SNeilBrown set_bit(MD_RECOVERY_NEEDED, & 41919d67169SNeilBrown conf->mddev->recovery); 42019d67169SNeilBrown 421cd5ff9a1SNeilBrown set_bit(R1BIO_WriteError, &r1_bio->state); 4224367af55SNeilBrown } else { 4231da177e4SLinus Torvalds /* 424e9c7469bSTejun Heo * Set R1BIO_Uptodate in our master bio, so that we 425e9c7469bSTejun Heo * will return a good error code for to the higher 426e9c7469bSTejun Heo * levels even if IO on some other mirrored buffer 427e9c7469bSTejun Heo * fails. 4281da177e4SLinus Torvalds * 429e9c7469bSTejun Heo * The 'master' represents the composite IO operation 430e9c7469bSTejun Heo * to user-side. So if something waits for IO, then it 431e9c7469bSTejun Heo * will wait for the 'master' bio. 4321da177e4SLinus Torvalds */ 4334367af55SNeilBrown sector_t first_bad; 4344367af55SNeilBrown int bad_sectors; 4354367af55SNeilBrown 436cd5ff9a1SNeilBrown r1_bio->bios[mirror] = NULL; 437cd5ff9a1SNeilBrown to_put = bio; 4383056e3aeSAlex Lyakas /* 4393056e3aeSAlex Lyakas * Do not set R1BIO_Uptodate if the current device is 4403056e3aeSAlex Lyakas * rebuilding or Faulty. This is because we cannot use 4413056e3aeSAlex Lyakas * such device for properly reading the data back (we could 4423056e3aeSAlex Lyakas * potentially use it, if the current write would have felt 4433056e3aeSAlex Lyakas * before rdev->recovery_offset, but for simplicity we don't 4443056e3aeSAlex Lyakas * check this here. 4453056e3aeSAlex Lyakas */ 4463056e3aeSAlex Lyakas if (test_bit(In_sync, &conf->mirrors[mirror].rdev->flags) && 4473056e3aeSAlex Lyakas !test_bit(Faulty, &conf->mirrors[mirror].rdev->flags)) 4481da177e4SLinus Torvalds set_bit(R1BIO_Uptodate, &r1_bio->state); 4491da177e4SLinus Torvalds 4504367af55SNeilBrown /* Maybe we can clear some bad blocks. */ 4514367af55SNeilBrown if (is_badblock(conf->mirrors[mirror].rdev, 4524367af55SNeilBrown r1_bio->sector, r1_bio->sectors, 4534367af55SNeilBrown &first_bad, &bad_sectors)) { 4544367af55SNeilBrown r1_bio->bios[mirror] = IO_MADE_GOOD; 4554367af55SNeilBrown set_bit(R1BIO_MadeGood, &r1_bio->state); 4564367af55SNeilBrown } 4574367af55SNeilBrown } 4584367af55SNeilBrown 4594b6d287fSNeilBrown if (behind) { 4604b6d287fSNeilBrown if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags)) 4614b6d287fSNeilBrown atomic_dec(&r1_bio->behind_remaining); 4624b6d287fSNeilBrown 463e9c7469bSTejun Heo /* 464e9c7469bSTejun Heo * In behind mode, we ACK the master bio once the I/O 465e9c7469bSTejun Heo * has safely reached all non-writemostly 466e9c7469bSTejun Heo * disks. Setting the Returned bit ensures that this 467e9c7469bSTejun Heo * gets done only once -- we don't ever want to return 468e9c7469bSTejun Heo * -EIO here, instead we'll wait 469e9c7469bSTejun Heo */ 4704b6d287fSNeilBrown if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) && 4714b6d287fSNeilBrown test_bit(R1BIO_Uptodate, &r1_bio->state)) { 4724b6d287fSNeilBrown /* Maybe we can return now */ 4734b6d287fSNeilBrown if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) { 4744b6d287fSNeilBrown struct bio *mbio = r1_bio->master_bio; 47536a4e1feSNeilBrown pr_debug("raid1: behind end write sectors" 47636a4e1feSNeilBrown " %llu-%llu\n", 4774f024f37SKent Overstreet (unsigned long long) mbio->bi_iter.bi_sector, 4784f024f37SKent Overstreet (unsigned long long) bio_end_sector(mbio) - 1); 479d2eb35acSNeilBrown call_bio_endio(r1_bio); 4804b6d287fSNeilBrown } 4814b6d287fSNeilBrown } 4824b6d287fSNeilBrown } 4834367af55SNeilBrown if (r1_bio->bios[mirror] == NULL) 4844367af55SNeilBrown rdev_dec_pending(conf->mirrors[mirror].rdev, 4854367af55SNeilBrown conf->mddev); 486e9c7469bSTejun Heo 4871da177e4SLinus Torvalds /* 4881da177e4SLinus Torvalds * Let's see if all mirrored write operations have finished 4891da177e4SLinus Torvalds * already. 4901da177e4SLinus Torvalds */ 491af6d7b76SNeilBrown r1_bio_write_done(r1_bio); 492c70810b3SNeilBrown 49304b857f7SNeilBrown if (to_put) 49404b857f7SNeilBrown bio_put(to_put); 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds /* 4991da177e4SLinus Torvalds * This routine returns the disk from which the requested read should 5001da177e4SLinus Torvalds * be done. There is a per-array 'next expected sequential IO' sector 5011da177e4SLinus Torvalds * number - if this matches on the next IO then we use the last disk. 5021da177e4SLinus Torvalds * There is also a per-disk 'last know head position' sector that is 5031da177e4SLinus Torvalds * maintained from IRQ contexts, both the normal and the resync IO 5041da177e4SLinus Torvalds * completion handlers update this position correctly. If there is no 5051da177e4SLinus Torvalds * perfect sequential match then we pick the disk whose head is closest. 5061da177e4SLinus Torvalds * 5071da177e4SLinus Torvalds * If there are 2 mirrors in the same 2 devices, performance degrades 5081da177e4SLinus Torvalds * because position is mirror, not device based. 5091da177e4SLinus Torvalds * 5101da177e4SLinus Torvalds * The rdev for the device selected will have nr_pending incremented. 5111da177e4SLinus Torvalds */ 512e8096360SNeilBrown static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sectors) 5131da177e4SLinus Torvalds { 514af3a2cd6SNeilBrown const sector_t this_sector = r1_bio->sector; 515d2eb35acSNeilBrown int sectors; 516d2eb35acSNeilBrown int best_good_sectors; 5179dedf603SShaohua Li int best_disk, best_dist_disk, best_pending_disk; 5189dedf603SShaohua Li int has_nonrot_disk; 519be4d3280SShaohua Li int disk; 52076073054SNeilBrown sector_t best_dist; 5219dedf603SShaohua Li unsigned int min_pending; 5223cb03002SNeilBrown struct md_rdev *rdev; 523f3ac8bf7SNeilBrown int choose_first; 52412cee5a8SShaohua Li int choose_next_idle; 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds rcu_read_lock(); 5271da177e4SLinus Torvalds /* 5288ddf9efeSNeilBrown * Check if we can balance. We can balance on the whole 5291da177e4SLinus Torvalds * device if no resync is going on, or below the resync window. 5301da177e4SLinus Torvalds * We take the first readable disk when above the resync window. 5311da177e4SLinus Torvalds */ 5321da177e4SLinus Torvalds retry: 533d2eb35acSNeilBrown sectors = r1_bio->sectors; 53476073054SNeilBrown best_disk = -1; 5359dedf603SShaohua Li best_dist_disk = -1; 53676073054SNeilBrown best_dist = MaxSector; 5379dedf603SShaohua Li best_pending_disk = -1; 5389dedf603SShaohua Li min_pending = UINT_MAX; 539d2eb35acSNeilBrown best_good_sectors = 0; 5409dedf603SShaohua Li has_nonrot_disk = 0; 54112cee5a8SShaohua Li choose_next_idle = 0; 542d2eb35acSNeilBrown 543c6d119cfSNeilBrown choose_first = (conf->mddev->recovery_cp < this_sector + sectors); 5441da177e4SLinus Torvalds 545be4d3280SShaohua Li for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) { 54676073054SNeilBrown sector_t dist; 547d2eb35acSNeilBrown sector_t first_bad; 548d2eb35acSNeilBrown int bad_sectors; 5499dedf603SShaohua Li unsigned int pending; 55012cee5a8SShaohua Li bool nonrot; 551d2eb35acSNeilBrown 552f3ac8bf7SNeilBrown rdev = rcu_dereference(conf->mirrors[disk].rdev); 553f3ac8bf7SNeilBrown if (r1_bio->bios[disk] == IO_BLOCKED 554f3ac8bf7SNeilBrown || rdev == NULL 5556b740b8dSNeilBrown || test_bit(Unmerged, &rdev->flags) 55676073054SNeilBrown || test_bit(Faulty, &rdev->flags)) 557f3ac8bf7SNeilBrown continue; 55876073054SNeilBrown if (!test_bit(In_sync, &rdev->flags) && 55976073054SNeilBrown rdev->recovery_offset < this_sector + sectors) 56076073054SNeilBrown continue; 56176073054SNeilBrown if (test_bit(WriteMostly, &rdev->flags)) { 56276073054SNeilBrown /* Don't balance among write-mostly, just 56376073054SNeilBrown * use the first as a last resort */ 564307729c8SNeilBrown if (best_disk < 0) { 565307729c8SNeilBrown if (is_badblock(rdev, this_sector, sectors, 566307729c8SNeilBrown &first_bad, &bad_sectors)) { 567307729c8SNeilBrown if (first_bad < this_sector) 568307729c8SNeilBrown /* Cannot use this */ 569307729c8SNeilBrown continue; 570307729c8SNeilBrown best_good_sectors = first_bad - this_sector; 571307729c8SNeilBrown } else 572307729c8SNeilBrown best_good_sectors = sectors; 57376073054SNeilBrown best_disk = disk; 574307729c8SNeilBrown } 57576073054SNeilBrown continue; 5768ddf9efeSNeilBrown } 57776073054SNeilBrown /* This is a reasonable device to use. It might 57876073054SNeilBrown * even be best. 5791da177e4SLinus Torvalds */ 580d2eb35acSNeilBrown if (is_badblock(rdev, this_sector, sectors, 581d2eb35acSNeilBrown &first_bad, &bad_sectors)) { 582d2eb35acSNeilBrown if (best_dist < MaxSector) 583d2eb35acSNeilBrown /* already have a better device */ 584d2eb35acSNeilBrown continue; 585d2eb35acSNeilBrown if (first_bad <= this_sector) { 586d2eb35acSNeilBrown /* cannot read here. If this is the 'primary' 587d2eb35acSNeilBrown * device, then we must not read beyond 588d2eb35acSNeilBrown * bad_sectors from another device.. 589d2eb35acSNeilBrown */ 590d2eb35acSNeilBrown bad_sectors -= (this_sector - first_bad); 591d2eb35acSNeilBrown if (choose_first && sectors > bad_sectors) 592d2eb35acSNeilBrown sectors = bad_sectors; 593d2eb35acSNeilBrown if (best_good_sectors > sectors) 594d2eb35acSNeilBrown best_good_sectors = sectors; 595d2eb35acSNeilBrown 596d2eb35acSNeilBrown } else { 597d2eb35acSNeilBrown sector_t good_sectors = first_bad - this_sector; 598d2eb35acSNeilBrown if (good_sectors > best_good_sectors) { 599d2eb35acSNeilBrown best_good_sectors = good_sectors; 600d2eb35acSNeilBrown best_disk = disk; 601d2eb35acSNeilBrown } 602d2eb35acSNeilBrown if (choose_first) 603d2eb35acSNeilBrown break; 604d2eb35acSNeilBrown } 605d2eb35acSNeilBrown continue; 606d2eb35acSNeilBrown } else 607d2eb35acSNeilBrown best_good_sectors = sectors; 608d2eb35acSNeilBrown 60912cee5a8SShaohua Li nonrot = blk_queue_nonrot(bdev_get_queue(rdev->bdev)); 61012cee5a8SShaohua Li has_nonrot_disk |= nonrot; 6119dedf603SShaohua Li pending = atomic_read(&rdev->nr_pending); 61276073054SNeilBrown dist = abs(this_sector - conf->mirrors[disk].head_position); 61312cee5a8SShaohua Li if (choose_first) { 61476073054SNeilBrown best_disk = disk; 6151da177e4SLinus Torvalds break; 6161da177e4SLinus Torvalds } 61712cee5a8SShaohua Li /* Don't change to another disk for sequential reads */ 61812cee5a8SShaohua Li if (conf->mirrors[disk].next_seq_sect == this_sector 61912cee5a8SShaohua Li || dist == 0) { 62012cee5a8SShaohua Li int opt_iosize = bdev_io_opt(rdev->bdev) >> 9; 62112cee5a8SShaohua Li struct raid1_info *mirror = &conf->mirrors[disk]; 62212cee5a8SShaohua Li 62312cee5a8SShaohua Li best_disk = disk; 62412cee5a8SShaohua Li /* 62512cee5a8SShaohua Li * If buffered sequential IO size exceeds optimal 62612cee5a8SShaohua Li * iosize, check if there is idle disk. If yes, choose 62712cee5a8SShaohua Li * the idle disk. read_balance could already choose an 62812cee5a8SShaohua Li * idle disk before noticing it's a sequential IO in 62912cee5a8SShaohua Li * this disk. This doesn't matter because this disk 63012cee5a8SShaohua Li * will idle, next time it will be utilized after the 63112cee5a8SShaohua Li * first disk has IO size exceeds optimal iosize. In 63212cee5a8SShaohua Li * this way, iosize of the first disk will be optimal 63312cee5a8SShaohua Li * iosize at least. iosize of the second disk might be 63412cee5a8SShaohua Li * small, but not a big deal since when the second disk 63512cee5a8SShaohua Li * starts IO, the first disk is likely still busy. 63612cee5a8SShaohua Li */ 63712cee5a8SShaohua Li if (nonrot && opt_iosize > 0 && 63812cee5a8SShaohua Li mirror->seq_start != MaxSector && 63912cee5a8SShaohua Li mirror->next_seq_sect > opt_iosize && 64012cee5a8SShaohua Li mirror->next_seq_sect - opt_iosize >= 64112cee5a8SShaohua Li mirror->seq_start) { 64212cee5a8SShaohua Li choose_next_idle = 1; 64312cee5a8SShaohua Li continue; 64412cee5a8SShaohua Li } 64512cee5a8SShaohua Li break; 64612cee5a8SShaohua Li } 64712cee5a8SShaohua Li /* If device is idle, use it */ 64812cee5a8SShaohua Li if (pending == 0) { 64912cee5a8SShaohua Li best_disk = disk; 65012cee5a8SShaohua Li break; 65112cee5a8SShaohua Li } 65212cee5a8SShaohua Li 65312cee5a8SShaohua Li if (choose_next_idle) 65412cee5a8SShaohua Li continue; 6559dedf603SShaohua Li 6569dedf603SShaohua Li if (min_pending > pending) { 6579dedf603SShaohua Li min_pending = pending; 6589dedf603SShaohua Li best_pending_disk = disk; 6599dedf603SShaohua Li } 6609dedf603SShaohua Li 66176073054SNeilBrown if (dist < best_dist) { 66276073054SNeilBrown best_dist = dist; 6639dedf603SShaohua Li best_dist_disk = disk; 6641da177e4SLinus Torvalds } 665f3ac8bf7SNeilBrown } 6661da177e4SLinus Torvalds 6679dedf603SShaohua Li /* 6689dedf603SShaohua Li * If all disks are rotational, choose the closest disk. If any disk is 6699dedf603SShaohua Li * non-rotational, choose the disk with less pending request even the 6709dedf603SShaohua Li * disk is rotational, which might/might not be optimal for raids with 6719dedf603SShaohua Li * mixed ratation/non-rotational disks depending on workload. 6729dedf603SShaohua Li */ 6739dedf603SShaohua Li if (best_disk == -1) { 6749dedf603SShaohua Li if (has_nonrot_disk) 6759dedf603SShaohua Li best_disk = best_pending_disk; 6769dedf603SShaohua Li else 6779dedf603SShaohua Li best_disk = best_dist_disk; 6789dedf603SShaohua Li } 6799dedf603SShaohua Li 68076073054SNeilBrown if (best_disk >= 0) { 68176073054SNeilBrown rdev = rcu_dereference(conf->mirrors[best_disk].rdev); 6828ddf9efeSNeilBrown if (!rdev) 6838ddf9efeSNeilBrown goto retry; 6848ddf9efeSNeilBrown atomic_inc(&rdev->nr_pending); 68576073054SNeilBrown if (test_bit(Faulty, &rdev->flags)) { 6861da177e4SLinus Torvalds /* cannot risk returning a device that failed 6871da177e4SLinus Torvalds * before we inc'ed nr_pending 6881da177e4SLinus Torvalds */ 68903c902e1SNeilBrown rdev_dec_pending(rdev, conf->mddev); 6901da177e4SLinus Torvalds goto retry; 6911da177e4SLinus Torvalds } 692d2eb35acSNeilBrown sectors = best_good_sectors; 69312cee5a8SShaohua Li 69412cee5a8SShaohua Li if (conf->mirrors[best_disk].next_seq_sect != this_sector) 69512cee5a8SShaohua Li conf->mirrors[best_disk].seq_start = this_sector; 69612cee5a8SShaohua Li 697be4d3280SShaohua Li conf->mirrors[best_disk].next_seq_sect = this_sector + sectors; 6981da177e4SLinus Torvalds } 6991da177e4SLinus Torvalds rcu_read_unlock(); 700d2eb35acSNeilBrown *max_sectors = sectors; 7011da177e4SLinus Torvalds 70276073054SNeilBrown return best_disk; 7031da177e4SLinus Torvalds } 7041da177e4SLinus Torvalds 7056b740b8dSNeilBrown static int raid1_mergeable_bvec(struct request_queue *q, 7066b740b8dSNeilBrown struct bvec_merge_data *bvm, 7076b740b8dSNeilBrown struct bio_vec *biovec) 7086b740b8dSNeilBrown { 7096b740b8dSNeilBrown struct mddev *mddev = q->queuedata; 7106b740b8dSNeilBrown struct r1conf *conf = mddev->private; 7116b740b8dSNeilBrown sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev); 7126b740b8dSNeilBrown int max = biovec->bv_len; 7136b740b8dSNeilBrown 7146b740b8dSNeilBrown if (mddev->merge_check_needed) { 7156b740b8dSNeilBrown int disk; 7166b740b8dSNeilBrown rcu_read_lock(); 7176b740b8dSNeilBrown for (disk = 0; disk < conf->raid_disks * 2; disk++) { 7186b740b8dSNeilBrown struct md_rdev *rdev = rcu_dereference( 7196b740b8dSNeilBrown conf->mirrors[disk].rdev); 7206b740b8dSNeilBrown if (rdev && !test_bit(Faulty, &rdev->flags)) { 7216b740b8dSNeilBrown struct request_queue *q = 7226b740b8dSNeilBrown bdev_get_queue(rdev->bdev); 7236b740b8dSNeilBrown if (q->merge_bvec_fn) { 7246b740b8dSNeilBrown bvm->bi_sector = sector + 7256b740b8dSNeilBrown rdev->data_offset; 7266b740b8dSNeilBrown bvm->bi_bdev = rdev->bdev; 7276b740b8dSNeilBrown max = min(max, q->merge_bvec_fn( 7286b740b8dSNeilBrown q, bvm, biovec)); 7296b740b8dSNeilBrown } 7306b740b8dSNeilBrown } 7316b740b8dSNeilBrown } 7326b740b8dSNeilBrown rcu_read_unlock(); 7336b740b8dSNeilBrown } 7346b740b8dSNeilBrown return max; 7356b740b8dSNeilBrown 7366b740b8dSNeilBrown } 7376b740b8dSNeilBrown 738fd01b88cSNeilBrown int md_raid1_congested(struct mddev *mddev, int bits) 7390d129228SNeilBrown { 740e8096360SNeilBrown struct r1conf *conf = mddev->private; 7410d129228SNeilBrown int i, ret = 0; 7420d129228SNeilBrown 74334db0cd6SNeilBrown if ((bits & (1 << BDI_async_congested)) && 74434db0cd6SNeilBrown conf->pending_count >= max_queued_requests) 74534db0cd6SNeilBrown return 1; 74634db0cd6SNeilBrown 7470d129228SNeilBrown rcu_read_lock(); 748f53e29fcSNeilBrown for (i = 0; i < conf->raid_disks * 2; i++) { 7493cb03002SNeilBrown struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); 7500d129228SNeilBrown if (rdev && !test_bit(Faulty, &rdev->flags)) { 751165125e1SJens Axboe struct request_queue *q = bdev_get_queue(rdev->bdev); 7520d129228SNeilBrown 7531ed7242eSJonathan Brassow BUG_ON(!q); 7541ed7242eSJonathan Brassow 7550d129228SNeilBrown /* Note the '|| 1' - when read_balance prefers 7560d129228SNeilBrown * non-congested targets, it can be removed 7570d129228SNeilBrown */ 75891a9e99dSAlexander Beregalov if ((bits & (1<<BDI_async_congested)) || 1) 7590d129228SNeilBrown ret |= bdi_congested(&q->backing_dev_info, bits); 7600d129228SNeilBrown else 7610d129228SNeilBrown ret &= bdi_congested(&q->backing_dev_info, bits); 7620d129228SNeilBrown } 7630d129228SNeilBrown } 7640d129228SNeilBrown rcu_read_unlock(); 7650d129228SNeilBrown return ret; 7660d129228SNeilBrown } 7671ed7242eSJonathan Brassow EXPORT_SYMBOL_GPL(md_raid1_congested); 7680d129228SNeilBrown 7691ed7242eSJonathan Brassow static int raid1_congested(void *data, int bits) 7701ed7242eSJonathan Brassow { 771fd01b88cSNeilBrown struct mddev *mddev = data; 7721ed7242eSJonathan Brassow 7731ed7242eSJonathan Brassow return mddev_congested(mddev, bits) || 7741ed7242eSJonathan Brassow md_raid1_congested(mddev, bits); 7751ed7242eSJonathan Brassow } 7760d129228SNeilBrown 777e8096360SNeilBrown static void flush_pending_writes(struct r1conf *conf) 778a35e63efSNeilBrown { 779a35e63efSNeilBrown /* Any writes that have been queued but are awaiting 780a35e63efSNeilBrown * bitmap updates get flushed here. 781a35e63efSNeilBrown */ 782a35e63efSNeilBrown spin_lock_irq(&conf->device_lock); 783a35e63efSNeilBrown 784a35e63efSNeilBrown if (conf->pending_bio_list.head) { 785a35e63efSNeilBrown struct bio *bio; 786a35e63efSNeilBrown bio = bio_list_get(&conf->pending_bio_list); 78734db0cd6SNeilBrown conf->pending_count = 0; 788a35e63efSNeilBrown spin_unlock_irq(&conf->device_lock); 789a35e63efSNeilBrown /* flush any pending bitmap writes to 790a35e63efSNeilBrown * disk before proceeding w/ I/O */ 791a35e63efSNeilBrown bitmap_unplug(conf->mddev->bitmap); 79234db0cd6SNeilBrown wake_up(&conf->wait_barrier); 793a35e63efSNeilBrown 794a35e63efSNeilBrown while (bio) { /* submit pending writes */ 795a35e63efSNeilBrown struct bio *next = bio->bi_next; 796a35e63efSNeilBrown bio->bi_next = NULL; 7972ff8cc2cSShaohua Li if (unlikely((bio->bi_rw & REQ_DISCARD) && 7982ff8cc2cSShaohua Li !blk_queue_discard(bdev_get_queue(bio->bi_bdev)))) 7992ff8cc2cSShaohua Li /* Just ignore it */ 8002ff8cc2cSShaohua Li bio_endio(bio, 0); 8012ff8cc2cSShaohua Li else 802a35e63efSNeilBrown generic_make_request(bio); 803a35e63efSNeilBrown bio = next; 804a35e63efSNeilBrown } 805a35e63efSNeilBrown } else 806a35e63efSNeilBrown spin_unlock_irq(&conf->device_lock); 8077eaceaccSJens Axboe } 8087eaceaccSJens Axboe 80917999be4SNeilBrown /* Barriers.... 81017999be4SNeilBrown * Sometimes we need to suspend IO while we do something else, 81117999be4SNeilBrown * either some resync/recovery, or reconfigure the array. 81217999be4SNeilBrown * To do this we raise a 'barrier'. 81317999be4SNeilBrown * The 'barrier' is a counter that can be raised multiple times 81417999be4SNeilBrown * to count how many activities are happening which preclude 81517999be4SNeilBrown * normal IO. 81617999be4SNeilBrown * We can only raise the barrier if there is no pending IO. 81717999be4SNeilBrown * i.e. if nr_pending == 0. 81817999be4SNeilBrown * We choose only to raise the barrier if no-one is waiting for the 81917999be4SNeilBrown * barrier to go down. This means that as soon as an IO request 82017999be4SNeilBrown * is ready, no other operations which require a barrier will start 82117999be4SNeilBrown * until the IO request has had a chance. 82217999be4SNeilBrown * 82317999be4SNeilBrown * So: regular IO calls 'wait_barrier'. When that returns there 82417999be4SNeilBrown * is no backgroup IO happening, It must arrange to call 82517999be4SNeilBrown * allow_barrier when it has finished its IO. 82617999be4SNeilBrown * backgroup IO calls must call raise_barrier. Once that returns 82717999be4SNeilBrown * there is no normal IO happeing. It must arrange to call 82817999be4SNeilBrown * lower_barrier when the particular background IO completes. 8291da177e4SLinus Torvalds */ 830c2fd4c94SNeilBrown static void raise_barrier(struct r1conf *conf, sector_t sector_nr) 8311da177e4SLinus Torvalds { 8321da177e4SLinus Torvalds spin_lock_irq(&conf->resync_lock); 8331da177e4SLinus Torvalds 83417999be4SNeilBrown /* Wait until no block IO is waiting */ 83517999be4SNeilBrown wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting, 836eed8c02eSLukas Czerner conf->resync_lock); 83717999be4SNeilBrown 83817999be4SNeilBrown /* block any new IO from starting */ 83917999be4SNeilBrown conf->barrier++; 840c2fd4c94SNeilBrown conf->next_resync = sector_nr; 84117999be4SNeilBrown 84279ef3a8aSmajianpeng /* For these conditions we must wait: 84379ef3a8aSmajianpeng * A: while the array is in frozen state 84479ef3a8aSmajianpeng * B: while barrier >= RESYNC_DEPTH, meaning resync reach 84579ef3a8aSmajianpeng * the max count which allowed. 84679ef3a8aSmajianpeng * C: next_resync + RESYNC_SECTORS > start_next_window, meaning 84779ef3a8aSmajianpeng * next resync will reach to the window which normal bios are 84879ef3a8aSmajianpeng * handling. 8492f73d3c5SNeilBrown * D: while there are any active requests in the current window. 85079ef3a8aSmajianpeng */ 85117999be4SNeilBrown wait_event_lock_irq(conf->wait_barrier, 852b364e3d0Smajianpeng !conf->array_frozen && 85379ef3a8aSmajianpeng conf->barrier < RESYNC_DEPTH && 8542f73d3c5SNeilBrown conf->current_window_requests == 0 && 85579ef3a8aSmajianpeng (conf->start_next_window >= 85679ef3a8aSmajianpeng conf->next_resync + RESYNC_SECTORS), 857eed8c02eSLukas Czerner conf->resync_lock); 85817999be4SNeilBrown 8591da177e4SLinus Torvalds spin_unlock_irq(&conf->resync_lock); 8601da177e4SLinus Torvalds } 8611da177e4SLinus Torvalds 862e8096360SNeilBrown static void lower_barrier(struct r1conf *conf) 86317999be4SNeilBrown { 86417999be4SNeilBrown unsigned long flags; 865709ae487SNeilBrown BUG_ON(conf->barrier <= 0); 86617999be4SNeilBrown spin_lock_irqsave(&conf->resync_lock, flags); 86717999be4SNeilBrown conf->barrier--; 86817999be4SNeilBrown spin_unlock_irqrestore(&conf->resync_lock, flags); 86917999be4SNeilBrown wake_up(&conf->wait_barrier); 87017999be4SNeilBrown } 87117999be4SNeilBrown 87279ef3a8aSmajianpeng static bool need_to_wait_for_sync(struct r1conf *conf, struct bio *bio) 87317999be4SNeilBrown { 87479ef3a8aSmajianpeng bool wait = false; 87579ef3a8aSmajianpeng 87679ef3a8aSmajianpeng if (conf->array_frozen || !bio) 87779ef3a8aSmajianpeng wait = true; 87879ef3a8aSmajianpeng else if (conf->barrier && bio_data_dir(bio) == WRITE) { 87923554960SNeilBrown if ((conf->mddev->curr_resync_completed 88079ef3a8aSmajianpeng >= bio_end_sector(bio)) || 88179ef3a8aSmajianpeng (conf->next_resync + NEXT_NORMALIO_DISTANCE 8824f024f37SKent Overstreet <= bio->bi_iter.bi_sector)) 88379ef3a8aSmajianpeng wait = false; 88479ef3a8aSmajianpeng else 88579ef3a8aSmajianpeng wait = true; 88679ef3a8aSmajianpeng } 88779ef3a8aSmajianpeng 88879ef3a8aSmajianpeng return wait; 88979ef3a8aSmajianpeng } 89079ef3a8aSmajianpeng 89179ef3a8aSmajianpeng static sector_t wait_barrier(struct r1conf *conf, struct bio *bio) 89279ef3a8aSmajianpeng { 89379ef3a8aSmajianpeng sector_t sector = 0; 89479ef3a8aSmajianpeng 89517999be4SNeilBrown spin_lock_irq(&conf->resync_lock); 89679ef3a8aSmajianpeng if (need_to_wait_for_sync(conf, bio)) { 89717999be4SNeilBrown conf->nr_waiting++; 898d6b42dcbSNeilBrown /* Wait for the barrier to drop. 899d6b42dcbSNeilBrown * However if there are already pending 900d6b42dcbSNeilBrown * requests (preventing the barrier from 901d6b42dcbSNeilBrown * rising completely), and the 902d6b42dcbSNeilBrown * pre-process bio queue isn't empty, 903d6b42dcbSNeilBrown * then don't wait, as we need to empty 904d6b42dcbSNeilBrown * that queue to get the nr_pending 905d6b42dcbSNeilBrown * count down. 906d6b42dcbSNeilBrown */ 907d6b42dcbSNeilBrown wait_event_lock_irq(conf->wait_barrier, 908b364e3d0Smajianpeng !conf->array_frozen && 909b364e3d0Smajianpeng (!conf->barrier || 91079ef3a8aSmajianpeng ((conf->start_next_window < 91179ef3a8aSmajianpeng conf->next_resync + RESYNC_SECTORS) && 912d6b42dcbSNeilBrown current->bio_list && 913b364e3d0Smajianpeng !bio_list_empty(current->bio_list))), 914eed8c02eSLukas Czerner conf->resync_lock); 91517999be4SNeilBrown conf->nr_waiting--; 91617999be4SNeilBrown } 91779ef3a8aSmajianpeng 91879ef3a8aSmajianpeng if (bio && bio_data_dir(bio) == WRITE) { 9192f73d3c5SNeilBrown if (bio->bi_iter.bi_sector >= 92023554960SNeilBrown conf->mddev->curr_resync_completed) { 92179ef3a8aSmajianpeng if (conf->start_next_window == MaxSector) 92279ef3a8aSmajianpeng conf->start_next_window = 92379ef3a8aSmajianpeng conf->next_resync + 92479ef3a8aSmajianpeng NEXT_NORMALIO_DISTANCE; 92579ef3a8aSmajianpeng 92679ef3a8aSmajianpeng if ((conf->start_next_window + NEXT_NORMALIO_DISTANCE) 9274f024f37SKent Overstreet <= bio->bi_iter.bi_sector) 92879ef3a8aSmajianpeng conf->next_window_requests++; 92979ef3a8aSmajianpeng else 93079ef3a8aSmajianpeng conf->current_window_requests++; 93179ef3a8aSmajianpeng sector = conf->start_next_window; 93217999be4SNeilBrown } 93341a336e0SNeilBrown } 93417999be4SNeilBrown 93579ef3a8aSmajianpeng conf->nr_pending++; 93679ef3a8aSmajianpeng spin_unlock_irq(&conf->resync_lock); 93779ef3a8aSmajianpeng return sector; 93879ef3a8aSmajianpeng } 93979ef3a8aSmajianpeng 94079ef3a8aSmajianpeng static void allow_barrier(struct r1conf *conf, sector_t start_next_window, 94179ef3a8aSmajianpeng sector_t bi_sector) 94217999be4SNeilBrown { 94317999be4SNeilBrown unsigned long flags; 94479ef3a8aSmajianpeng 94517999be4SNeilBrown spin_lock_irqsave(&conf->resync_lock, flags); 94617999be4SNeilBrown conf->nr_pending--; 94779ef3a8aSmajianpeng if (start_next_window) { 94879ef3a8aSmajianpeng if (start_next_window == conf->start_next_window) { 94979ef3a8aSmajianpeng if (conf->start_next_window + NEXT_NORMALIO_DISTANCE 95079ef3a8aSmajianpeng <= bi_sector) 95179ef3a8aSmajianpeng conf->next_window_requests--; 95279ef3a8aSmajianpeng else 95379ef3a8aSmajianpeng conf->current_window_requests--; 95479ef3a8aSmajianpeng } else 95579ef3a8aSmajianpeng conf->current_window_requests--; 95679ef3a8aSmajianpeng 95779ef3a8aSmajianpeng if (!conf->current_window_requests) { 95879ef3a8aSmajianpeng if (conf->next_window_requests) { 95979ef3a8aSmajianpeng conf->current_window_requests = 96079ef3a8aSmajianpeng conf->next_window_requests; 96179ef3a8aSmajianpeng conf->next_window_requests = 0; 96279ef3a8aSmajianpeng conf->start_next_window += 96379ef3a8aSmajianpeng NEXT_NORMALIO_DISTANCE; 96479ef3a8aSmajianpeng } else 96579ef3a8aSmajianpeng conf->start_next_window = MaxSector; 96679ef3a8aSmajianpeng } 96779ef3a8aSmajianpeng } 96817999be4SNeilBrown spin_unlock_irqrestore(&conf->resync_lock, flags); 96917999be4SNeilBrown wake_up(&conf->wait_barrier); 97017999be4SNeilBrown } 97117999be4SNeilBrown 972e2d59925SNeilBrown static void freeze_array(struct r1conf *conf, int extra) 973ddaf22abSNeilBrown { 974ddaf22abSNeilBrown /* stop syncio and normal IO and wait for everything to 975ddaf22abSNeilBrown * go quite. 976b364e3d0Smajianpeng * We wait until nr_pending match nr_queued+extra 9771c830532SNeilBrown * This is called in the context of one normal IO request 9781c830532SNeilBrown * that has failed. Thus any sync request that might be pending 9791c830532SNeilBrown * will be blocked by nr_pending, and we need to wait for 9801c830532SNeilBrown * pending IO requests to complete or be queued for re-try. 981e2d59925SNeilBrown * Thus the number queued (nr_queued) plus this request (extra) 9821c830532SNeilBrown * must match the number of pending IOs (nr_pending) before 9831c830532SNeilBrown * we continue. 984ddaf22abSNeilBrown */ 985ddaf22abSNeilBrown spin_lock_irq(&conf->resync_lock); 986b364e3d0Smajianpeng conf->array_frozen = 1; 987eed8c02eSLukas Czerner wait_event_lock_irq_cmd(conf->wait_barrier, 988e2d59925SNeilBrown conf->nr_pending == conf->nr_queued+extra, 989ddaf22abSNeilBrown conf->resync_lock, 990c3b328acSNeilBrown flush_pending_writes(conf)); 991ddaf22abSNeilBrown spin_unlock_irq(&conf->resync_lock); 992ddaf22abSNeilBrown } 993e8096360SNeilBrown static void unfreeze_array(struct r1conf *conf) 994ddaf22abSNeilBrown { 995ddaf22abSNeilBrown /* reverse the effect of the freeze */ 996ddaf22abSNeilBrown spin_lock_irq(&conf->resync_lock); 997b364e3d0Smajianpeng conf->array_frozen = 0; 998ddaf22abSNeilBrown wake_up(&conf->wait_barrier); 999ddaf22abSNeilBrown spin_unlock_irq(&conf->resync_lock); 1000ddaf22abSNeilBrown } 1001ddaf22abSNeilBrown 100217999be4SNeilBrown 10034e78064fSNeilBrown /* duplicate the data pages for behind I/O 10044e78064fSNeilBrown */ 10059f2c9d12SNeilBrown static void alloc_behind_pages(struct bio *bio, struct r1bio *r1_bio) 10064b6d287fSNeilBrown { 10074b6d287fSNeilBrown int i; 10084b6d287fSNeilBrown struct bio_vec *bvec; 10092ca68f5eSNeilBrown struct bio_vec *bvecs = kzalloc(bio->bi_vcnt * sizeof(struct bio_vec), 10104b6d287fSNeilBrown GFP_NOIO); 10112ca68f5eSNeilBrown if (unlikely(!bvecs)) 1012af6d7b76SNeilBrown return; 10134b6d287fSNeilBrown 1014cb34e057SKent Overstreet bio_for_each_segment_all(bvec, bio, i) { 10152ca68f5eSNeilBrown bvecs[i] = *bvec; 10162ca68f5eSNeilBrown bvecs[i].bv_page = alloc_page(GFP_NOIO); 10172ca68f5eSNeilBrown if (unlikely(!bvecs[i].bv_page)) 10184b6d287fSNeilBrown goto do_sync_io; 10192ca68f5eSNeilBrown memcpy(kmap(bvecs[i].bv_page) + bvec->bv_offset, 10204b6d287fSNeilBrown kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len); 10212ca68f5eSNeilBrown kunmap(bvecs[i].bv_page); 10224b6d287fSNeilBrown kunmap(bvec->bv_page); 10234b6d287fSNeilBrown } 10242ca68f5eSNeilBrown r1_bio->behind_bvecs = bvecs; 1025af6d7b76SNeilBrown r1_bio->behind_page_count = bio->bi_vcnt; 1026af6d7b76SNeilBrown set_bit(R1BIO_BehindIO, &r1_bio->state); 1027af6d7b76SNeilBrown return; 10284b6d287fSNeilBrown 10294b6d287fSNeilBrown do_sync_io: 1030af6d7b76SNeilBrown for (i = 0; i < bio->bi_vcnt; i++) 10312ca68f5eSNeilBrown if (bvecs[i].bv_page) 10322ca68f5eSNeilBrown put_page(bvecs[i].bv_page); 10332ca68f5eSNeilBrown kfree(bvecs); 10344f024f37SKent Overstreet pr_debug("%dB behind alloc failed, doing sync I/O\n", 10354f024f37SKent Overstreet bio->bi_iter.bi_size); 10364b6d287fSNeilBrown } 10374b6d287fSNeilBrown 1038f54a9d0eSNeilBrown struct raid1_plug_cb { 1039f54a9d0eSNeilBrown struct blk_plug_cb cb; 1040f54a9d0eSNeilBrown struct bio_list pending; 1041f54a9d0eSNeilBrown int pending_cnt; 1042f54a9d0eSNeilBrown }; 1043f54a9d0eSNeilBrown 1044f54a9d0eSNeilBrown static void raid1_unplug(struct blk_plug_cb *cb, bool from_schedule) 1045f54a9d0eSNeilBrown { 1046f54a9d0eSNeilBrown struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb, 1047f54a9d0eSNeilBrown cb); 1048f54a9d0eSNeilBrown struct mddev *mddev = plug->cb.data; 1049f54a9d0eSNeilBrown struct r1conf *conf = mddev->private; 1050f54a9d0eSNeilBrown struct bio *bio; 1051f54a9d0eSNeilBrown 1052874807a8SNeilBrown if (from_schedule || current->bio_list) { 1053f54a9d0eSNeilBrown spin_lock_irq(&conf->device_lock); 1054f54a9d0eSNeilBrown bio_list_merge(&conf->pending_bio_list, &plug->pending); 1055f54a9d0eSNeilBrown conf->pending_count += plug->pending_cnt; 1056f54a9d0eSNeilBrown spin_unlock_irq(&conf->device_lock); 1057ee0b0244SNeilBrown wake_up(&conf->wait_barrier); 1058f54a9d0eSNeilBrown md_wakeup_thread(mddev->thread); 1059f54a9d0eSNeilBrown kfree(plug); 1060f54a9d0eSNeilBrown return; 1061f54a9d0eSNeilBrown } 1062f54a9d0eSNeilBrown 1063f54a9d0eSNeilBrown /* we aren't scheduling, so we can do the write-out directly. */ 1064f54a9d0eSNeilBrown bio = bio_list_get(&plug->pending); 1065f54a9d0eSNeilBrown bitmap_unplug(mddev->bitmap); 1066f54a9d0eSNeilBrown wake_up(&conf->wait_barrier); 1067f54a9d0eSNeilBrown 1068f54a9d0eSNeilBrown while (bio) { /* submit pending writes */ 1069f54a9d0eSNeilBrown struct bio *next = bio->bi_next; 1070f54a9d0eSNeilBrown bio->bi_next = NULL; 107132f9f570SShaohua Li if (unlikely((bio->bi_rw & REQ_DISCARD) && 107232f9f570SShaohua Li !blk_queue_discard(bdev_get_queue(bio->bi_bdev)))) 107332f9f570SShaohua Li /* Just ignore it */ 107432f9f570SShaohua Li bio_endio(bio, 0); 107532f9f570SShaohua Li else 1076f54a9d0eSNeilBrown generic_make_request(bio); 1077f54a9d0eSNeilBrown bio = next; 1078f54a9d0eSNeilBrown } 1079f54a9d0eSNeilBrown kfree(plug); 1080f54a9d0eSNeilBrown } 1081f54a9d0eSNeilBrown 1082b4fdcb02SLinus Torvalds static void make_request(struct mddev *mddev, struct bio * bio) 10831da177e4SLinus Torvalds { 1084e8096360SNeilBrown struct r1conf *conf = mddev->private; 10850eaf822cSJonathan Brassow struct raid1_info *mirror; 10869f2c9d12SNeilBrown struct r1bio *r1_bio; 10871da177e4SLinus Torvalds struct bio *read_bio; 10881f68f0c4SNeilBrown int i, disks; 108984255d10SNeilBrown struct bitmap *bitmap; 1090191ea9b2SNeilBrown unsigned long flags; 1091a362357bSJens Axboe const int rw = bio_data_dir(bio); 10922c7d46ecSNeilBrown const unsigned long do_sync = (bio->bi_rw & REQ_SYNC); 1093e9c7469bSTejun Heo const unsigned long do_flush_fua = (bio->bi_rw & (REQ_FLUSH | REQ_FUA)); 10942ff8cc2cSShaohua Li const unsigned long do_discard = (bio->bi_rw 10952ff8cc2cSShaohua Li & (REQ_DISCARD | REQ_SECURE)); 1096c8dc9c65SJoe Lawrence const unsigned long do_same = (bio->bi_rw & REQ_WRITE_SAME); 10973cb03002SNeilBrown struct md_rdev *blocked_rdev; 1098f54a9d0eSNeilBrown struct blk_plug_cb *cb; 1099f54a9d0eSNeilBrown struct raid1_plug_cb *plug = NULL; 11001f68f0c4SNeilBrown int first_clone; 11011f68f0c4SNeilBrown int sectors_handled; 11021f68f0c4SNeilBrown int max_sectors; 110379ef3a8aSmajianpeng sector_t start_next_window; 1104191ea9b2SNeilBrown 11051da177e4SLinus Torvalds /* 11061da177e4SLinus Torvalds * Register the new request and wait if the reconstruction 11071da177e4SLinus Torvalds * thread has put up a bar for new requests. 11081da177e4SLinus Torvalds * Continue immediately if no resync is active currently. 11091da177e4SLinus Torvalds */ 111062de608dSNeilBrown 11113d310eb7SNeilBrown md_write_start(mddev, bio); /* wait on superblock update early */ 11123d310eb7SNeilBrown 11136eef4b21SNeilBrown if (bio_data_dir(bio) == WRITE && 1114f73a1c7dSKent Overstreet bio_end_sector(bio) > mddev->suspend_lo && 11154f024f37SKent Overstreet bio->bi_iter.bi_sector < mddev->suspend_hi) { 11166eef4b21SNeilBrown /* As the suspend_* range is controlled by 11176eef4b21SNeilBrown * userspace, we want an interruptible 11186eef4b21SNeilBrown * wait. 11196eef4b21SNeilBrown */ 11206eef4b21SNeilBrown DEFINE_WAIT(w); 11216eef4b21SNeilBrown for (;;) { 11226eef4b21SNeilBrown flush_signals(current); 11236eef4b21SNeilBrown prepare_to_wait(&conf->wait_barrier, 11246eef4b21SNeilBrown &w, TASK_INTERRUPTIBLE); 1125f73a1c7dSKent Overstreet if (bio_end_sector(bio) <= mddev->suspend_lo || 11264f024f37SKent Overstreet bio->bi_iter.bi_sector >= mddev->suspend_hi) 11276eef4b21SNeilBrown break; 11286eef4b21SNeilBrown schedule(); 11296eef4b21SNeilBrown } 11306eef4b21SNeilBrown finish_wait(&conf->wait_barrier, &w); 11316eef4b21SNeilBrown } 113262de608dSNeilBrown 113379ef3a8aSmajianpeng start_next_window = wait_barrier(conf, bio); 11341da177e4SLinus Torvalds 113584255d10SNeilBrown bitmap = mddev->bitmap; 113684255d10SNeilBrown 11371da177e4SLinus Torvalds /* 11381da177e4SLinus Torvalds * make_request() can abort the operation when READA is being 11391da177e4SLinus Torvalds * used and no empty request is available. 11401da177e4SLinus Torvalds * 11411da177e4SLinus Torvalds */ 11421da177e4SLinus Torvalds r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO); 11431da177e4SLinus Torvalds 11441da177e4SLinus Torvalds r1_bio->master_bio = bio; 1145aa8b57aaSKent Overstreet r1_bio->sectors = bio_sectors(bio); 1146191ea9b2SNeilBrown r1_bio->state = 0; 11471da177e4SLinus Torvalds r1_bio->mddev = mddev; 11484f024f37SKent Overstreet r1_bio->sector = bio->bi_iter.bi_sector; 11491da177e4SLinus Torvalds 1150d2eb35acSNeilBrown /* We might need to issue multiple reads to different 1151d2eb35acSNeilBrown * devices if there are bad blocks around, so we keep 1152d2eb35acSNeilBrown * track of the number of reads in bio->bi_phys_segments. 1153d2eb35acSNeilBrown * If this is 0, there is only one r1_bio and no locking 1154d2eb35acSNeilBrown * will be needed when requests complete. If it is 1155d2eb35acSNeilBrown * non-zero, then it is the number of not-completed requests. 1156d2eb35acSNeilBrown */ 1157d2eb35acSNeilBrown bio->bi_phys_segments = 0; 1158d2eb35acSNeilBrown clear_bit(BIO_SEG_VALID, &bio->bi_flags); 1159d2eb35acSNeilBrown 1160a362357bSJens Axboe if (rw == READ) { 11611da177e4SLinus Torvalds /* 11621da177e4SLinus Torvalds * read balancing logic: 11631da177e4SLinus Torvalds */ 1164d2eb35acSNeilBrown int rdisk; 1165d2eb35acSNeilBrown 1166d2eb35acSNeilBrown read_again: 1167d2eb35acSNeilBrown rdisk = read_balance(conf, r1_bio, &max_sectors); 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds if (rdisk < 0) { 11701da177e4SLinus Torvalds /* couldn't find anywhere to read from */ 11711da177e4SLinus Torvalds raid_end_bio_io(r1_bio); 11725a7bbad2SChristoph Hellwig return; 11731da177e4SLinus Torvalds } 11741da177e4SLinus Torvalds mirror = conf->mirrors + rdisk; 11751da177e4SLinus Torvalds 1176e555190dSNeilBrown if (test_bit(WriteMostly, &mirror->rdev->flags) && 1177e555190dSNeilBrown bitmap) { 1178e555190dSNeilBrown /* Reading from a write-mostly device must 1179e555190dSNeilBrown * take care not to over-take any writes 1180e555190dSNeilBrown * that are 'behind' 1181e555190dSNeilBrown */ 1182e555190dSNeilBrown wait_event(bitmap->behind_wait, 1183e555190dSNeilBrown atomic_read(&bitmap->behind_writes) == 0); 1184e555190dSNeilBrown } 11851da177e4SLinus Torvalds r1_bio->read_disk = rdisk; 1186f0cc9a05SNeilBrown r1_bio->start_next_window = 0; 11871da177e4SLinus Torvalds 1188a167f663SNeilBrown read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev); 11894f024f37SKent Overstreet bio_trim(read_bio, r1_bio->sector - bio->bi_iter.bi_sector, 1190d2eb35acSNeilBrown max_sectors); 11911da177e4SLinus Torvalds 11921da177e4SLinus Torvalds r1_bio->bios[rdisk] = read_bio; 11931da177e4SLinus Torvalds 11944f024f37SKent Overstreet read_bio->bi_iter.bi_sector = r1_bio->sector + 11954f024f37SKent Overstreet mirror->rdev->data_offset; 11961da177e4SLinus Torvalds read_bio->bi_bdev = mirror->rdev->bdev; 11971da177e4SLinus Torvalds read_bio->bi_end_io = raid1_end_read_request; 11987b6d91daSChristoph Hellwig read_bio->bi_rw = READ | do_sync; 11991da177e4SLinus Torvalds read_bio->bi_private = r1_bio; 12001da177e4SLinus Torvalds 1201d2eb35acSNeilBrown if (max_sectors < r1_bio->sectors) { 1202d2eb35acSNeilBrown /* could not read all from this device, so we will 1203d2eb35acSNeilBrown * need another r1_bio. 1204d2eb35acSNeilBrown */ 1205d2eb35acSNeilBrown 1206d2eb35acSNeilBrown sectors_handled = (r1_bio->sector + max_sectors 12074f024f37SKent Overstreet - bio->bi_iter.bi_sector); 1208d2eb35acSNeilBrown r1_bio->sectors = max_sectors; 1209d2eb35acSNeilBrown spin_lock_irq(&conf->device_lock); 1210d2eb35acSNeilBrown if (bio->bi_phys_segments == 0) 1211d2eb35acSNeilBrown bio->bi_phys_segments = 2; 1212d2eb35acSNeilBrown else 1213d2eb35acSNeilBrown bio->bi_phys_segments++; 1214d2eb35acSNeilBrown spin_unlock_irq(&conf->device_lock); 1215d2eb35acSNeilBrown /* Cannot call generic_make_request directly 1216d2eb35acSNeilBrown * as that will be queued in __make_request 1217d2eb35acSNeilBrown * and subsequent mempool_alloc might block waiting 1218d2eb35acSNeilBrown * for it. So hand bio over to raid1d. 1219d2eb35acSNeilBrown */ 1220d2eb35acSNeilBrown reschedule_retry(r1_bio); 1221d2eb35acSNeilBrown 1222d2eb35acSNeilBrown r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO); 1223d2eb35acSNeilBrown 1224d2eb35acSNeilBrown r1_bio->master_bio = bio; 1225aa8b57aaSKent Overstreet r1_bio->sectors = bio_sectors(bio) - sectors_handled; 1226d2eb35acSNeilBrown r1_bio->state = 0; 1227d2eb35acSNeilBrown r1_bio->mddev = mddev; 12284f024f37SKent Overstreet r1_bio->sector = bio->bi_iter.bi_sector + 12294f024f37SKent Overstreet sectors_handled; 1230d2eb35acSNeilBrown goto read_again; 1231d2eb35acSNeilBrown } else 12321da177e4SLinus Torvalds generic_make_request(read_bio); 12335a7bbad2SChristoph Hellwig return; 12341da177e4SLinus Torvalds } 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds /* 12371da177e4SLinus Torvalds * WRITE: 12381da177e4SLinus Torvalds */ 123934db0cd6SNeilBrown if (conf->pending_count >= max_queued_requests) { 124034db0cd6SNeilBrown md_wakeup_thread(mddev->thread); 124134db0cd6SNeilBrown wait_event(conf->wait_barrier, 124234db0cd6SNeilBrown conf->pending_count < max_queued_requests); 124334db0cd6SNeilBrown } 12441f68f0c4SNeilBrown /* first select target devices under rcu_lock and 12451da177e4SLinus Torvalds * inc refcount on their rdev. Record them by setting 12461da177e4SLinus Torvalds * bios[x] to bio 12471f68f0c4SNeilBrown * If there are known/acknowledged bad blocks on any device on 12481f68f0c4SNeilBrown * which we have seen a write error, we want to avoid writing those 12491f68f0c4SNeilBrown * blocks. 12501f68f0c4SNeilBrown * This potentially requires several writes to write around 12511f68f0c4SNeilBrown * the bad blocks. Each set of writes gets it's own r1bio 12521f68f0c4SNeilBrown * with a set of bios attached. 12531da177e4SLinus Torvalds */ 1254c3b328acSNeilBrown 12558f19ccb2SNeilBrown disks = conf->raid_disks * 2; 12566bfe0b49SDan Williams retry_write: 125779ef3a8aSmajianpeng r1_bio->start_next_window = start_next_window; 12586bfe0b49SDan Williams blocked_rdev = NULL; 12591da177e4SLinus Torvalds rcu_read_lock(); 12601f68f0c4SNeilBrown max_sectors = r1_bio->sectors; 12611da177e4SLinus Torvalds for (i = 0; i < disks; i++) { 12623cb03002SNeilBrown struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); 12636bfe0b49SDan Williams if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) { 12646bfe0b49SDan Williams atomic_inc(&rdev->nr_pending); 12656bfe0b49SDan Williams blocked_rdev = rdev; 12666bfe0b49SDan Williams break; 12676bfe0b49SDan Williams } 12681da177e4SLinus Torvalds r1_bio->bios[i] = NULL; 12696b740b8dSNeilBrown if (!rdev || test_bit(Faulty, &rdev->flags) 12706b740b8dSNeilBrown || test_bit(Unmerged, &rdev->flags)) { 12718f19ccb2SNeilBrown if (i < conf->raid_disks) 12721f68f0c4SNeilBrown set_bit(R1BIO_Degraded, &r1_bio->state); 12731f68f0c4SNeilBrown continue; 1274964147d5SNeilBrown } 12751f68f0c4SNeilBrown 12761f68f0c4SNeilBrown atomic_inc(&rdev->nr_pending); 12771f68f0c4SNeilBrown if (test_bit(WriteErrorSeen, &rdev->flags)) { 12781f68f0c4SNeilBrown sector_t first_bad; 12791f68f0c4SNeilBrown int bad_sectors; 12801f68f0c4SNeilBrown int is_bad; 12811f68f0c4SNeilBrown 12821f68f0c4SNeilBrown is_bad = is_badblock(rdev, r1_bio->sector, 12831f68f0c4SNeilBrown max_sectors, 12841f68f0c4SNeilBrown &first_bad, &bad_sectors); 12851f68f0c4SNeilBrown if (is_bad < 0) { 12861f68f0c4SNeilBrown /* mustn't write here until the bad block is 12871f68f0c4SNeilBrown * acknowledged*/ 12881f68f0c4SNeilBrown set_bit(BlockedBadBlocks, &rdev->flags); 12891f68f0c4SNeilBrown blocked_rdev = rdev; 12901f68f0c4SNeilBrown break; 12911f68f0c4SNeilBrown } 12921f68f0c4SNeilBrown if (is_bad && first_bad <= r1_bio->sector) { 12931f68f0c4SNeilBrown /* Cannot write here at all */ 12941f68f0c4SNeilBrown bad_sectors -= (r1_bio->sector - first_bad); 12951f68f0c4SNeilBrown if (bad_sectors < max_sectors) 12961f68f0c4SNeilBrown /* mustn't write more than bad_sectors 12971f68f0c4SNeilBrown * to other devices yet 12981f68f0c4SNeilBrown */ 12991f68f0c4SNeilBrown max_sectors = bad_sectors; 13001f68f0c4SNeilBrown rdev_dec_pending(rdev, mddev); 13011f68f0c4SNeilBrown /* We don't set R1BIO_Degraded as that 13021f68f0c4SNeilBrown * only applies if the disk is 13031f68f0c4SNeilBrown * missing, so it might be re-added, 13041f68f0c4SNeilBrown * and we want to know to recover this 13051f68f0c4SNeilBrown * chunk. 13061f68f0c4SNeilBrown * In this case the device is here, 13071f68f0c4SNeilBrown * and the fact that this chunk is not 13081f68f0c4SNeilBrown * in-sync is recorded in the bad 13091f68f0c4SNeilBrown * block log 13101f68f0c4SNeilBrown */ 13111f68f0c4SNeilBrown continue; 13121f68f0c4SNeilBrown } 13131f68f0c4SNeilBrown if (is_bad) { 13141f68f0c4SNeilBrown int good_sectors = first_bad - r1_bio->sector; 13151f68f0c4SNeilBrown if (good_sectors < max_sectors) 13161f68f0c4SNeilBrown max_sectors = good_sectors; 13171f68f0c4SNeilBrown } 13181f68f0c4SNeilBrown } 13191f68f0c4SNeilBrown r1_bio->bios[i] = bio; 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds rcu_read_unlock(); 13221da177e4SLinus Torvalds 13236bfe0b49SDan Williams if (unlikely(blocked_rdev)) { 13246bfe0b49SDan Williams /* Wait for this device to become unblocked */ 13256bfe0b49SDan Williams int j; 132679ef3a8aSmajianpeng sector_t old = start_next_window; 13276bfe0b49SDan Williams 13286bfe0b49SDan Williams for (j = 0; j < i; j++) 13296bfe0b49SDan Williams if (r1_bio->bios[j]) 13306bfe0b49SDan Williams rdev_dec_pending(conf->mirrors[j].rdev, mddev); 13311f68f0c4SNeilBrown r1_bio->state = 0; 13324f024f37SKent Overstreet allow_barrier(conf, start_next_window, bio->bi_iter.bi_sector); 13336bfe0b49SDan Williams md_wait_for_blocked_rdev(blocked_rdev, mddev); 133479ef3a8aSmajianpeng start_next_window = wait_barrier(conf, bio); 133579ef3a8aSmajianpeng /* 133679ef3a8aSmajianpeng * We must make sure the multi r1bios of bio have 133779ef3a8aSmajianpeng * the same value of bi_phys_segments 133879ef3a8aSmajianpeng */ 133979ef3a8aSmajianpeng if (bio->bi_phys_segments && old && 134079ef3a8aSmajianpeng old != start_next_window) 134179ef3a8aSmajianpeng /* Wait for the former r1bio(s) to complete */ 134279ef3a8aSmajianpeng wait_event(conf->wait_barrier, 134379ef3a8aSmajianpeng bio->bi_phys_segments == 1); 13446bfe0b49SDan Williams goto retry_write; 13456bfe0b49SDan Williams } 13466bfe0b49SDan Williams 13471f68f0c4SNeilBrown if (max_sectors < r1_bio->sectors) { 13481f68f0c4SNeilBrown /* We are splitting this write into multiple parts, so 13491f68f0c4SNeilBrown * we need to prepare for allocating another r1_bio. 13501f68f0c4SNeilBrown */ 13511f68f0c4SNeilBrown r1_bio->sectors = max_sectors; 13521f68f0c4SNeilBrown spin_lock_irq(&conf->device_lock); 13531f68f0c4SNeilBrown if (bio->bi_phys_segments == 0) 13541f68f0c4SNeilBrown bio->bi_phys_segments = 2; 13551f68f0c4SNeilBrown else 13561f68f0c4SNeilBrown bio->bi_phys_segments++; 13571f68f0c4SNeilBrown spin_unlock_irq(&conf->device_lock); 1358191ea9b2SNeilBrown } 13594f024f37SKent Overstreet sectors_handled = r1_bio->sector + max_sectors - bio->bi_iter.bi_sector; 13604b6d287fSNeilBrown 13614e78064fSNeilBrown atomic_set(&r1_bio->remaining, 1); 13624b6d287fSNeilBrown atomic_set(&r1_bio->behind_remaining, 0); 1363191ea9b2SNeilBrown 13641f68f0c4SNeilBrown first_clone = 1; 13651da177e4SLinus Torvalds for (i = 0; i < disks; i++) { 13661da177e4SLinus Torvalds struct bio *mbio; 13671da177e4SLinus Torvalds if (!r1_bio->bios[i]) 13681da177e4SLinus Torvalds continue; 13691da177e4SLinus Torvalds 1370a167f663SNeilBrown mbio = bio_clone_mddev(bio, GFP_NOIO, mddev); 13714f024f37SKent Overstreet bio_trim(mbio, r1_bio->sector - bio->bi_iter.bi_sector, max_sectors); 13721da177e4SLinus Torvalds 13731f68f0c4SNeilBrown if (first_clone) { 13741f68f0c4SNeilBrown /* do behind I/O ? 13751f68f0c4SNeilBrown * Not if there are too many, or cannot 13761f68f0c4SNeilBrown * allocate memory, or a reader on WriteMostly 13771f68f0c4SNeilBrown * is waiting for behind writes to flush */ 13781f68f0c4SNeilBrown if (bitmap && 13791f68f0c4SNeilBrown (atomic_read(&bitmap->behind_writes) 13801f68f0c4SNeilBrown < mddev->bitmap_info.max_write_behind) && 13811f68f0c4SNeilBrown !waitqueue_active(&bitmap->behind_wait)) 13821f68f0c4SNeilBrown alloc_behind_pages(mbio, r1_bio); 13831da177e4SLinus Torvalds 13841f68f0c4SNeilBrown bitmap_startwrite(bitmap, r1_bio->sector, 13851f68f0c4SNeilBrown r1_bio->sectors, 13861f68f0c4SNeilBrown test_bit(R1BIO_BehindIO, 13871f68f0c4SNeilBrown &r1_bio->state)); 13881f68f0c4SNeilBrown first_clone = 0; 13891f68f0c4SNeilBrown } 13902ca68f5eSNeilBrown if (r1_bio->behind_bvecs) { 13914b6d287fSNeilBrown struct bio_vec *bvec; 13924b6d287fSNeilBrown int j; 13934b6d287fSNeilBrown 1394cb34e057SKent Overstreet /* 1395cb34e057SKent Overstreet * We trimmed the bio, so _all is legit 13964b6d287fSNeilBrown */ 1397d74c6d51SKent Overstreet bio_for_each_segment_all(bvec, mbio, j) 13982ca68f5eSNeilBrown bvec->bv_page = r1_bio->behind_bvecs[j].bv_page; 13994b6d287fSNeilBrown if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags)) 14004b6d287fSNeilBrown atomic_inc(&r1_bio->behind_remaining); 14014b6d287fSNeilBrown } 14024b6d287fSNeilBrown 14031f68f0c4SNeilBrown r1_bio->bios[i] = mbio; 14041f68f0c4SNeilBrown 14054f024f37SKent Overstreet mbio->bi_iter.bi_sector = (r1_bio->sector + 14061f68f0c4SNeilBrown conf->mirrors[i].rdev->data_offset); 14071f68f0c4SNeilBrown mbio->bi_bdev = conf->mirrors[i].rdev->bdev; 14081f68f0c4SNeilBrown mbio->bi_end_io = raid1_end_write_request; 1409c8dc9c65SJoe Lawrence mbio->bi_rw = 1410c8dc9c65SJoe Lawrence WRITE | do_flush_fua | do_sync | do_discard | do_same; 14111f68f0c4SNeilBrown mbio->bi_private = r1_bio; 14121f68f0c4SNeilBrown 14131da177e4SLinus Torvalds atomic_inc(&r1_bio->remaining); 1414f54a9d0eSNeilBrown 1415f54a9d0eSNeilBrown cb = blk_check_plugged(raid1_unplug, mddev, sizeof(*plug)); 1416f54a9d0eSNeilBrown if (cb) 1417f54a9d0eSNeilBrown plug = container_of(cb, struct raid1_plug_cb, cb); 1418f54a9d0eSNeilBrown else 1419f54a9d0eSNeilBrown plug = NULL; 1420191ea9b2SNeilBrown spin_lock_irqsave(&conf->device_lock, flags); 1421f54a9d0eSNeilBrown if (plug) { 1422f54a9d0eSNeilBrown bio_list_add(&plug->pending, mbio); 1423f54a9d0eSNeilBrown plug->pending_cnt++; 1424f54a9d0eSNeilBrown } else { 14254e78064fSNeilBrown bio_list_add(&conf->pending_bio_list, mbio); 142634db0cd6SNeilBrown conf->pending_count++; 1427f54a9d0eSNeilBrown } 1428191ea9b2SNeilBrown spin_unlock_irqrestore(&conf->device_lock, flags); 1429f54a9d0eSNeilBrown if (!plug) 1430b357f04aSNeilBrown md_wakeup_thread(mddev->thread); 14314e78064fSNeilBrown } 1432079fa166SNeilBrown /* Mustn't call r1_bio_write_done before this next test, 1433079fa166SNeilBrown * as it could result in the bio being freed. 1434079fa166SNeilBrown */ 1435aa8b57aaSKent Overstreet if (sectors_handled < bio_sectors(bio)) { 1436079fa166SNeilBrown r1_bio_write_done(r1_bio); 14371f68f0c4SNeilBrown /* We need another r1_bio. It has already been counted 14381f68f0c4SNeilBrown * in bio->bi_phys_segments 14391f68f0c4SNeilBrown */ 14401f68f0c4SNeilBrown r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO); 14411f68f0c4SNeilBrown r1_bio->master_bio = bio; 1442aa8b57aaSKent Overstreet r1_bio->sectors = bio_sectors(bio) - sectors_handled; 14431f68f0c4SNeilBrown r1_bio->state = 0; 14441f68f0c4SNeilBrown r1_bio->mddev = mddev; 14454f024f37SKent Overstreet r1_bio->sector = bio->bi_iter.bi_sector + sectors_handled; 14461f68f0c4SNeilBrown goto retry_write; 14471f68f0c4SNeilBrown } 14481f68f0c4SNeilBrown 1449079fa166SNeilBrown r1_bio_write_done(r1_bio); 1450079fa166SNeilBrown 1451079fa166SNeilBrown /* In case raid1d snuck in to freeze_array */ 1452079fa166SNeilBrown wake_up(&conf->wait_barrier); 14531da177e4SLinus Torvalds } 14541da177e4SLinus Torvalds 1455fd01b88cSNeilBrown static void status(struct seq_file *seq, struct mddev *mddev) 14561da177e4SLinus Torvalds { 1457e8096360SNeilBrown struct r1conf *conf = mddev->private; 14581da177e4SLinus Torvalds int i; 14591da177e4SLinus Torvalds 14601da177e4SLinus Torvalds seq_printf(seq, " [%d/%d] [", conf->raid_disks, 146111ce99e6SNeilBrown conf->raid_disks - mddev->degraded); 1462ddac7c7eSNeilBrown rcu_read_lock(); 1463ddac7c7eSNeilBrown for (i = 0; i < conf->raid_disks; i++) { 14643cb03002SNeilBrown struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); 14651da177e4SLinus Torvalds seq_printf(seq, "%s", 1466ddac7c7eSNeilBrown rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_"); 1467ddac7c7eSNeilBrown } 1468ddac7c7eSNeilBrown rcu_read_unlock(); 14691da177e4SLinus Torvalds seq_printf(seq, "]"); 14701da177e4SLinus Torvalds } 14711da177e4SLinus Torvalds 14721da177e4SLinus Torvalds 1473fd01b88cSNeilBrown static void error(struct mddev *mddev, struct md_rdev *rdev) 14741da177e4SLinus Torvalds { 14751da177e4SLinus Torvalds char b[BDEVNAME_SIZE]; 1476e8096360SNeilBrown struct r1conf *conf = mddev->private; 14771da177e4SLinus Torvalds 14781da177e4SLinus Torvalds /* 14791da177e4SLinus Torvalds * If it is not operational, then we have already marked it as dead 14801da177e4SLinus Torvalds * else if it is the last working disks, ignore the error, let the 14811da177e4SLinus Torvalds * next level up know. 14821da177e4SLinus Torvalds * else mark the drive as failed 14831da177e4SLinus Torvalds */ 1484b2d444d7SNeilBrown if (test_bit(In_sync, &rdev->flags) 14854044ba58SNeilBrown && (conf->raid_disks - mddev->degraded) == 1) { 14861da177e4SLinus Torvalds /* 14871da177e4SLinus Torvalds * Don't fail the drive, act as though we were just a 14884044ba58SNeilBrown * normal single drive. 14894044ba58SNeilBrown * However don't try a recovery from this drive as 14904044ba58SNeilBrown * it is very likely to fail. 14911da177e4SLinus Torvalds */ 14925389042fSNeilBrown conf->recovery_disabled = mddev->recovery_disabled; 14931da177e4SLinus Torvalds return; 14944044ba58SNeilBrown } 1495de393cdeSNeilBrown set_bit(Blocked, &rdev->flags); 1496c04be0aaSNeilBrown if (test_and_clear_bit(In_sync, &rdev->flags)) { 1497c04be0aaSNeilBrown unsigned long flags; 1498c04be0aaSNeilBrown spin_lock_irqsave(&conf->device_lock, flags); 14991da177e4SLinus Torvalds mddev->degraded++; 1500dd00a99eSNeilBrown set_bit(Faulty, &rdev->flags); 1501c04be0aaSNeilBrown spin_unlock_irqrestore(&conf->device_lock, flags); 15022446dba0SNeilBrown } else 15032446dba0SNeilBrown set_bit(Faulty, &rdev->flags); 15041da177e4SLinus Torvalds /* 15051da177e4SLinus Torvalds * if recovery is running, make sure it aborts. 15061da177e4SLinus Torvalds */ 1507dfc70645SNeilBrown set_bit(MD_RECOVERY_INTR, &mddev->recovery); 1508850b2b42SNeilBrown set_bit(MD_CHANGE_DEVS, &mddev->flags); 1509067032bcSJoe Perches printk(KERN_ALERT 1510067032bcSJoe Perches "md/raid1:%s: Disk failure on %s, disabling device.\n" 1511067032bcSJoe Perches "md/raid1:%s: Operation continuing on %d devices.\n", 15129dd1e2faSNeilBrown mdname(mddev), bdevname(rdev->bdev, b), 15139dd1e2faSNeilBrown mdname(mddev), conf->raid_disks - mddev->degraded); 15141da177e4SLinus Torvalds } 15151da177e4SLinus Torvalds 1516e8096360SNeilBrown static void print_conf(struct r1conf *conf) 15171da177e4SLinus Torvalds { 15181da177e4SLinus Torvalds int i; 15191da177e4SLinus Torvalds 15209dd1e2faSNeilBrown printk(KERN_DEBUG "RAID1 conf printout:\n"); 15211da177e4SLinus Torvalds if (!conf) { 15229dd1e2faSNeilBrown printk(KERN_DEBUG "(!conf)\n"); 15231da177e4SLinus Torvalds return; 15241da177e4SLinus Torvalds } 15259dd1e2faSNeilBrown printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded, 15261da177e4SLinus Torvalds conf->raid_disks); 15271da177e4SLinus Torvalds 1528ddac7c7eSNeilBrown rcu_read_lock(); 15291da177e4SLinus Torvalds for (i = 0; i < conf->raid_disks; i++) { 15301da177e4SLinus Torvalds char b[BDEVNAME_SIZE]; 15313cb03002SNeilBrown struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); 1532ddac7c7eSNeilBrown if (rdev) 15339dd1e2faSNeilBrown printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n", 1534ddac7c7eSNeilBrown i, !test_bit(In_sync, &rdev->flags), 1535ddac7c7eSNeilBrown !test_bit(Faulty, &rdev->flags), 1536ddac7c7eSNeilBrown bdevname(rdev->bdev,b)); 15371da177e4SLinus Torvalds } 1538ddac7c7eSNeilBrown rcu_read_unlock(); 15391da177e4SLinus Torvalds } 15401da177e4SLinus Torvalds 1541e8096360SNeilBrown static void close_sync(struct r1conf *conf) 15421da177e4SLinus Torvalds { 154379ef3a8aSmajianpeng wait_barrier(conf, NULL); 154479ef3a8aSmajianpeng allow_barrier(conf, 0, 0); 15451da177e4SLinus Torvalds 15461da177e4SLinus Torvalds mempool_destroy(conf->r1buf_pool); 15471da177e4SLinus Torvalds conf->r1buf_pool = NULL; 154879ef3a8aSmajianpeng 1549669cc7baSNeilBrown spin_lock_irq(&conf->resync_lock); 155079ef3a8aSmajianpeng conf->next_resync = 0; 155179ef3a8aSmajianpeng conf->start_next_window = MaxSector; 1552669cc7baSNeilBrown conf->current_window_requests += 1553669cc7baSNeilBrown conf->next_window_requests; 1554669cc7baSNeilBrown conf->next_window_requests = 0; 1555669cc7baSNeilBrown spin_unlock_irq(&conf->resync_lock); 15561da177e4SLinus Torvalds } 15571da177e4SLinus Torvalds 1558fd01b88cSNeilBrown static int raid1_spare_active(struct mddev *mddev) 15591da177e4SLinus Torvalds { 15601da177e4SLinus Torvalds int i; 1561e8096360SNeilBrown struct r1conf *conf = mddev->private; 15626b965620SNeilBrown int count = 0; 15636b965620SNeilBrown unsigned long flags; 15641da177e4SLinus Torvalds 15651da177e4SLinus Torvalds /* 15661da177e4SLinus Torvalds * Find all failed disks within the RAID1 configuration 1567ddac7c7eSNeilBrown * and mark them readable. 1568ddac7c7eSNeilBrown * Called under mddev lock, so rcu protection not needed. 15691da177e4SLinus Torvalds */ 15701da177e4SLinus Torvalds for (i = 0; i < conf->raid_disks; i++) { 15713cb03002SNeilBrown struct md_rdev *rdev = conf->mirrors[i].rdev; 15728c7a2c2bSNeilBrown struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev; 15738c7a2c2bSNeilBrown if (repl 15748c7a2c2bSNeilBrown && repl->recovery_offset == MaxSector 15758c7a2c2bSNeilBrown && !test_bit(Faulty, &repl->flags) 15768c7a2c2bSNeilBrown && !test_and_set_bit(In_sync, &repl->flags)) { 15778c7a2c2bSNeilBrown /* replacement has just become active */ 15788c7a2c2bSNeilBrown if (!rdev || 15798c7a2c2bSNeilBrown !test_and_clear_bit(In_sync, &rdev->flags)) 15808c7a2c2bSNeilBrown count++; 15818c7a2c2bSNeilBrown if (rdev) { 15828c7a2c2bSNeilBrown /* Replaced device not technically 15838c7a2c2bSNeilBrown * faulty, but we need to be sure 15848c7a2c2bSNeilBrown * it gets removed and never re-added 15858c7a2c2bSNeilBrown */ 15868c7a2c2bSNeilBrown set_bit(Faulty, &rdev->flags); 15878c7a2c2bSNeilBrown sysfs_notify_dirent_safe( 15888c7a2c2bSNeilBrown rdev->sysfs_state); 15898c7a2c2bSNeilBrown } 15908c7a2c2bSNeilBrown } 1591ddac7c7eSNeilBrown if (rdev 159261e4947cSLukasz Dorau && rdev->recovery_offset == MaxSector 1593ddac7c7eSNeilBrown && !test_bit(Faulty, &rdev->flags) 1594c04be0aaSNeilBrown && !test_and_set_bit(In_sync, &rdev->flags)) { 15956b965620SNeilBrown count++; 1596654e8b5aSJonathan Brassow sysfs_notify_dirent_safe(rdev->sysfs_state); 15971da177e4SLinus Torvalds } 15981da177e4SLinus Torvalds } 15996b965620SNeilBrown spin_lock_irqsave(&conf->device_lock, flags); 16006b965620SNeilBrown mddev->degraded -= count; 16016b965620SNeilBrown spin_unlock_irqrestore(&conf->device_lock, flags); 16021da177e4SLinus Torvalds 16031da177e4SLinus Torvalds print_conf(conf); 16046b965620SNeilBrown return count; 16051da177e4SLinus Torvalds } 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds 1608fd01b88cSNeilBrown static int raid1_add_disk(struct mddev *mddev, struct md_rdev *rdev) 16091da177e4SLinus Torvalds { 1610e8096360SNeilBrown struct r1conf *conf = mddev->private; 1611199050eaSNeil Brown int err = -EEXIST; 161241158c7eSNeilBrown int mirror = 0; 16130eaf822cSJonathan Brassow struct raid1_info *p; 16146c2fce2eSNeil Brown int first = 0; 161530194636SNeilBrown int last = conf->raid_disks - 1; 16166b740b8dSNeilBrown struct request_queue *q = bdev_get_queue(rdev->bdev); 16171da177e4SLinus Torvalds 16185389042fSNeilBrown if (mddev->recovery_disabled == conf->recovery_disabled) 16195389042fSNeilBrown return -EBUSY; 16205389042fSNeilBrown 16216c2fce2eSNeil Brown if (rdev->raid_disk >= 0) 16226c2fce2eSNeil Brown first = last = rdev->raid_disk; 16236c2fce2eSNeil Brown 16246b740b8dSNeilBrown if (q->merge_bvec_fn) { 16256b740b8dSNeilBrown set_bit(Unmerged, &rdev->flags); 16266b740b8dSNeilBrown mddev->merge_check_needed = 1; 16276b740b8dSNeilBrown } 16286b740b8dSNeilBrown 16297ef449d1SNeilBrown for (mirror = first; mirror <= last; mirror++) { 16307ef449d1SNeilBrown p = conf->mirrors+mirror; 16317ef449d1SNeilBrown if (!p->rdev) { 16321da177e4SLinus Torvalds 16339092c02dSJonathan Brassow if (mddev->gendisk) 16348f6c2e4bSMartin K. Petersen disk_stack_limits(mddev->gendisk, rdev->bdev, 16358f6c2e4bSMartin K. Petersen rdev->data_offset << 9); 16361da177e4SLinus Torvalds 16371da177e4SLinus Torvalds p->head_position = 0; 16381da177e4SLinus Torvalds rdev->raid_disk = mirror; 1639199050eaSNeil Brown err = 0; 16406aea114aSNeilBrown /* As all devices are equivalent, we don't need a full recovery 16416aea114aSNeilBrown * if this was recently any drive of the array 16426aea114aSNeilBrown */ 16436aea114aSNeilBrown if (rdev->saved_raid_disk < 0) 164441158c7eSNeilBrown conf->fullsync = 1; 1645d6065f7bSSuzanne Wood rcu_assign_pointer(p->rdev, rdev); 16461da177e4SLinus Torvalds break; 16471da177e4SLinus Torvalds } 16487ef449d1SNeilBrown if (test_bit(WantReplacement, &p->rdev->flags) && 16497ef449d1SNeilBrown p[conf->raid_disks].rdev == NULL) { 16507ef449d1SNeilBrown /* Add this device as a replacement */ 16517ef449d1SNeilBrown clear_bit(In_sync, &rdev->flags); 16527ef449d1SNeilBrown set_bit(Replacement, &rdev->flags); 16537ef449d1SNeilBrown rdev->raid_disk = mirror; 16547ef449d1SNeilBrown err = 0; 16557ef449d1SNeilBrown conf->fullsync = 1; 16567ef449d1SNeilBrown rcu_assign_pointer(p[conf->raid_disks].rdev, rdev); 16577ef449d1SNeilBrown break; 16587ef449d1SNeilBrown } 16597ef449d1SNeilBrown } 16606b740b8dSNeilBrown if (err == 0 && test_bit(Unmerged, &rdev->flags)) { 16616b740b8dSNeilBrown /* Some requests might not have seen this new 16626b740b8dSNeilBrown * merge_bvec_fn. We must wait for them to complete 16636b740b8dSNeilBrown * before merging the device fully. 16646b740b8dSNeilBrown * First we make sure any code which has tested 16656b740b8dSNeilBrown * our function has submitted the request, then 16666b740b8dSNeilBrown * we wait for all outstanding requests to complete. 16676b740b8dSNeilBrown */ 16686b740b8dSNeilBrown synchronize_sched(); 1669e2d59925SNeilBrown freeze_array(conf, 0); 1670e2d59925SNeilBrown unfreeze_array(conf); 16716b740b8dSNeilBrown clear_bit(Unmerged, &rdev->flags); 16726b740b8dSNeilBrown } 1673ac5e7113SAndre Noll md_integrity_add_rdev(rdev, mddev); 16749092c02dSJonathan Brassow if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev))) 16752ff8cc2cSShaohua Li queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue); 16761da177e4SLinus Torvalds print_conf(conf); 1677199050eaSNeil Brown return err; 16781da177e4SLinus Torvalds } 16791da177e4SLinus Torvalds 1680b8321b68SNeilBrown static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev) 16811da177e4SLinus Torvalds { 1682e8096360SNeilBrown struct r1conf *conf = mddev->private; 16831da177e4SLinus Torvalds int err = 0; 1684b8321b68SNeilBrown int number = rdev->raid_disk; 16850eaf822cSJonathan Brassow struct raid1_info *p = conf->mirrors + number; 16861da177e4SLinus Torvalds 1687b014f14cSNeilBrown if (rdev != p->rdev) 1688b014f14cSNeilBrown p = conf->mirrors + conf->raid_disks + number; 1689b014f14cSNeilBrown 16901da177e4SLinus Torvalds print_conf(conf); 1691b8321b68SNeilBrown if (rdev == p->rdev) { 1692b2d444d7SNeilBrown if (test_bit(In_sync, &rdev->flags) || 16931da177e4SLinus Torvalds atomic_read(&rdev->nr_pending)) { 16941da177e4SLinus Torvalds err = -EBUSY; 16951da177e4SLinus Torvalds goto abort; 16961da177e4SLinus Torvalds } 1697046abeedSNeilBrown /* Only remove non-faulty devices if recovery 1698dfc70645SNeilBrown * is not possible. 1699dfc70645SNeilBrown */ 1700dfc70645SNeilBrown if (!test_bit(Faulty, &rdev->flags) && 17015389042fSNeilBrown mddev->recovery_disabled != conf->recovery_disabled && 1702dfc70645SNeilBrown mddev->degraded < conf->raid_disks) { 1703dfc70645SNeilBrown err = -EBUSY; 1704dfc70645SNeilBrown goto abort; 1705dfc70645SNeilBrown } 17061da177e4SLinus Torvalds p->rdev = NULL; 1707fbd568a3SPaul E. McKenney synchronize_rcu(); 17081da177e4SLinus Torvalds if (atomic_read(&rdev->nr_pending)) { 17091da177e4SLinus Torvalds /* lost the race, try later */ 17101da177e4SLinus Torvalds err = -EBUSY; 17111da177e4SLinus Torvalds p->rdev = rdev; 1712ac5e7113SAndre Noll goto abort; 17138c7a2c2bSNeilBrown } else if (conf->mirrors[conf->raid_disks + number].rdev) { 17148c7a2c2bSNeilBrown /* We just removed a device that is being replaced. 17158c7a2c2bSNeilBrown * Move down the replacement. We drain all IO before 17168c7a2c2bSNeilBrown * doing this to avoid confusion. 17178c7a2c2bSNeilBrown */ 17188c7a2c2bSNeilBrown struct md_rdev *repl = 17198c7a2c2bSNeilBrown conf->mirrors[conf->raid_disks + number].rdev; 1720e2d59925SNeilBrown freeze_array(conf, 0); 17218c7a2c2bSNeilBrown clear_bit(Replacement, &repl->flags); 17228c7a2c2bSNeilBrown p->rdev = repl; 17238c7a2c2bSNeilBrown conf->mirrors[conf->raid_disks + number].rdev = NULL; 1724e2d59925SNeilBrown unfreeze_array(conf); 1725b014f14cSNeilBrown clear_bit(WantReplacement, &rdev->flags); 17268c7a2c2bSNeilBrown } else 17278c7a2c2bSNeilBrown clear_bit(WantReplacement, &rdev->flags); 1728a91a2785SMartin K. Petersen err = md_integrity_register(mddev); 17291da177e4SLinus Torvalds } 17301da177e4SLinus Torvalds abort: 17311da177e4SLinus Torvalds 17321da177e4SLinus Torvalds print_conf(conf); 17331da177e4SLinus Torvalds return err; 17341da177e4SLinus Torvalds } 17351da177e4SLinus Torvalds 17361da177e4SLinus Torvalds 17376712ecf8SNeilBrown static void end_sync_read(struct bio *bio, int error) 17381da177e4SLinus Torvalds { 17399f2c9d12SNeilBrown struct r1bio *r1_bio = bio->bi_private; 17401da177e4SLinus Torvalds 17410fc280f6SNeilBrown update_head_pos(r1_bio->read_disk, r1_bio); 1742ba3ae3beSNamhyung Kim 17431da177e4SLinus Torvalds /* 17441da177e4SLinus Torvalds * we have read a block, now it needs to be re-written, 17451da177e4SLinus Torvalds * or re-read if the read failed. 17461da177e4SLinus Torvalds * We don't do much here, just schedule handling by raid1d 17471da177e4SLinus Torvalds */ 174869382e85SNeilBrown if (test_bit(BIO_UPTODATE, &bio->bi_flags)) 17491da177e4SLinus Torvalds set_bit(R1BIO_Uptodate, &r1_bio->state); 1750d11c171eSNeilBrown 1751d11c171eSNeilBrown if (atomic_dec_and_test(&r1_bio->remaining)) 17521da177e4SLinus Torvalds reschedule_retry(r1_bio); 17531da177e4SLinus Torvalds } 17541da177e4SLinus Torvalds 17556712ecf8SNeilBrown static void end_sync_write(struct bio *bio, int error) 17561da177e4SLinus Torvalds { 17571da177e4SLinus Torvalds int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); 17589f2c9d12SNeilBrown struct r1bio *r1_bio = bio->bi_private; 1759fd01b88cSNeilBrown struct mddev *mddev = r1_bio->mddev; 1760e8096360SNeilBrown struct r1conf *conf = mddev->private; 17611da177e4SLinus Torvalds int mirror=0; 17624367af55SNeilBrown sector_t first_bad; 17634367af55SNeilBrown int bad_sectors; 17641da177e4SLinus Torvalds 1765ba3ae3beSNamhyung Kim mirror = find_bio_disk(r1_bio, bio); 1766ba3ae3beSNamhyung Kim 17676b1117d5SNeilBrown if (!uptodate) { 176857dab0bdSNeilBrown sector_t sync_blocks = 0; 17696b1117d5SNeilBrown sector_t s = r1_bio->sector; 17706b1117d5SNeilBrown long sectors_to_go = r1_bio->sectors; 17716b1117d5SNeilBrown /* make sure these bits doesn't get cleared. */ 17726b1117d5SNeilBrown do { 17735e3db645SNeilBrown bitmap_end_sync(mddev->bitmap, s, 17746b1117d5SNeilBrown &sync_blocks, 1); 17756b1117d5SNeilBrown s += sync_blocks; 17766b1117d5SNeilBrown sectors_to_go -= sync_blocks; 17776b1117d5SNeilBrown } while (sectors_to_go > 0); 1778d8f05d29SNeilBrown set_bit(WriteErrorSeen, 1779d8f05d29SNeilBrown &conf->mirrors[mirror].rdev->flags); 178019d67169SNeilBrown if (!test_and_set_bit(WantReplacement, 178119d67169SNeilBrown &conf->mirrors[mirror].rdev->flags)) 178219d67169SNeilBrown set_bit(MD_RECOVERY_NEEDED, & 178319d67169SNeilBrown mddev->recovery); 1784d8f05d29SNeilBrown set_bit(R1BIO_WriteError, &r1_bio->state); 17854367af55SNeilBrown } else if (is_badblock(conf->mirrors[mirror].rdev, 17864367af55SNeilBrown r1_bio->sector, 17874367af55SNeilBrown r1_bio->sectors, 17883a9f28a5SNeilBrown &first_bad, &bad_sectors) && 17893a9f28a5SNeilBrown !is_badblock(conf->mirrors[r1_bio->read_disk].rdev, 17903a9f28a5SNeilBrown r1_bio->sector, 17913a9f28a5SNeilBrown r1_bio->sectors, 17923a9f28a5SNeilBrown &first_bad, &bad_sectors) 17933a9f28a5SNeilBrown ) 17944367af55SNeilBrown set_bit(R1BIO_MadeGood, &r1_bio->state); 1795e3b9703eSNeilBrown 17961da177e4SLinus Torvalds if (atomic_dec_and_test(&r1_bio->remaining)) { 17974367af55SNeilBrown int s = r1_bio->sectors; 1798d8f05d29SNeilBrown if (test_bit(R1BIO_MadeGood, &r1_bio->state) || 1799d8f05d29SNeilBrown test_bit(R1BIO_WriteError, &r1_bio->state)) 18004367af55SNeilBrown reschedule_retry(r1_bio); 18014367af55SNeilBrown else { 18021da177e4SLinus Torvalds put_buf(r1_bio); 180373d5c38aSNeilBrown md_done_sync(mddev, s, uptodate); 18041da177e4SLinus Torvalds } 18051da177e4SLinus Torvalds } 18064367af55SNeilBrown } 18071da177e4SLinus Torvalds 18083cb03002SNeilBrown static int r1_sync_page_io(struct md_rdev *rdev, sector_t sector, 1809d8f05d29SNeilBrown int sectors, struct page *page, int rw) 1810d8f05d29SNeilBrown { 1811d8f05d29SNeilBrown if (sync_page_io(rdev, sector, sectors << 9, page, rw, false)) 1812d8f05d29SNeilBrown /* success */ 1813d8f05d29SNeilBrown return 1; 181419d67169SNeilBrown if (rw == WRITE) { 1815d8f05d29SNeilBrown set_bit(WriteErrorSeen, &rdev->flags); 181619d67169SNeilBrown if (!test_and_set_bit(WantReplacement, 181719d67169SNeilBrown &rdev->flags)) 181819d67169SNeilBrown set_bit(MD_RECOVERY_NEEDED, & 181919d67169SNeilBrown rdev->mddev->recovery); 182019d67169SNeilBrown } 1821d8f05d29SNeilBrown /* need to record an error - either for the block or the device */ 1822d8f05d29SNeilBrown if (!rdev_set_badblocks(rdev, sector, sectors, 0)) 1823d8f05d29SNeilBrown md_error(rdev->mddev, rdev); 1824d8f05d29SNeilBrown return 0; 1825d8f05d29SNeilBrown } 1826d8f05d29SNeilBrown 18279f2c9d12SNeilBrown static int fix_sync_read_error(struct r1bio *r1_bio) 18281da177e4SLinus Torvalds { 1829a68e5870SNeilBrown /* Try some synchronous reads of other devices to get 183069382e85SNeilBrown * good data, much like with normal read errors. Only 1831ddac7c7eSNeilBrown * read into the pages we already have so we don't 183269382e85SNeilBrown * need to re-issue the read request. 183369382e85SNeilBrown * We don't need to freeze the array, because being in an 183469382e85SNeilBrown * active sync request, there is no normal IO, and 183569382e85SNeilBrown * no overlapping syncs. 183606f60385SNeilBrown * We don't need to check is_badblock() again as we 183706f60385SNeilBrown * made sure that anything with a bad block in range 183806f60385SNeilBrown * will have bi_end_io clear. 18391da177e4SLinus Torvalds */ 1840fd01b88cSNeilBrown struct mddev *mddev = r1_bio->mddev; 1841e8096360SNeilBrown struct r1conf *conf = mddev->private; 1842a68e5870SNeilBrown struct bio *bio = r1_bio->bios[r1_bio->read_disk]; 184369382e85SNeilBrown sector_t sect = r1_bio->sector; 184469382e85SNeilBrown int sectors = r1_bio->sectors; 184569382e85SNeilBrown int idx = 0; 184669382e85SNeilBrown 184769382e85SNeilBrown while(sectors) { 184869382e85SNeilBrown int s = sectors; 184969382e85SNeilBrown int d = r1_bio->read_disk; 185069382e85SNeilBrown int success = 0; 18513cb03002SNeilBrown struct md_rdev *rdev; 185278d7f5f7SNeilBrown int start; 185369382e85SNeilBrown 185469382e85SNeilBrown if (s > (PAGE_SIZE>>9)) 185569382e85SNeilBrown s = PAGE_SIZE >> 9; 185669382e85SNeilBrown do { 185769382e85SNeilBrown if (r1_bio->bios[d]->bi_end_io == end_sync_read) { 1858ddac7c7eSNeilBrown /* No rcu protection needed here devices 1859ddac7c7eSNeilBrown * can only be removed when no resync is 1860ddac7c7eSNeilBrown * active, and resync is currently active 1861ddac7c7eSNeilBrown */ 186269382e85SNeilBrown rdev = conf->mirrors[d].rdev; 18639d3d8011SNamhyung Kim if (sync_page_io(rdev, sect, s<<9, 186469382e85SNeilBrown bio->bi_io_vec[idx].bv_page, 1865ccebd4c4SJonathan Brassow READ, false)) { 186669382e85SNeilBrown success = 1; 186769382e85SNeilBrown break; 186869382e85SNeilBrown } 186969382e85SNeilBrown } 187069382e85SNeilBrown d++; 18718f19ccb2SNeilBrown if (d == conf->raid_disks * 2) 187269382e85SNeilBrown d = 0; 187369382e85SNeilBrown } while (!success && d != r1_bio->read_disk); 187469382e85SNeilBrown 187578d7f5f7SNeilBrown if (!success) { 187678d7f5f7SNeilBrown char b[BDEVNAME_SIZE]; 18773a9f28a5SNeilBrown int abort = 0; 18783a9f28a5SNeilBrown /* Cannot read from anywhere, this block is lost. 18793a9f28a5SNeilBrown * Record a bad block on each device. If that doesn't 18803a9f28a5SNeilBrown * work just disable and interrupt the recovery. 18813a9f28a5SNeilBrown * Don't fail devices as that won't really help. 18823a9f28a5SNeilBrown */ 188378d7f5f7SNeilBrown printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O read error" 188478d7f5f7SNeilBrown " for block %llu\n", 188578d7f5f7SNeilBrown mdname(mddev), 188678d7f5f7SNeilBrown bdevname(bio->bi_bdev, b), 188778d7f5f7SNeilBrown (unsigned long long)r1_bio->sector); 18888f19ccb2SNeilBrown for (d = 0; d < conf->raid_disks * 2; d++) { 18893a9f28a5SNeilBrown rdev = conf->mirrors[d].rdev; 18903a9f28a5SNeilBrown if (!rdev || test_bit(Faulty, &rdev->flags)) 18913a9f28a5SNeilBrown continue; 18923a9f28a5SNeilBrown if (!rdev_set_badblocks(rdev, sect, s, 0)) 18933a9f28a5SNeilBrown abort = 1; 18943a9f28a5SNeilBrown } 18953a9f28a5SNeilBrown if (abort) { 1896d890fa2bSNeilBrown conf->recovery_disabled = 1897d890fa2bSNeilBrown mddev->recovery_disabled; 18983a9f28a5SNeilBrown set_bit(MD_RECOVERY_INTR, &mddev->recovery); 189978d7f5f7SNeilBrown md_done_sync(mddev, r1_bio->sectors, 0); 190078d7f5f7SNeilBrown put_buf(r1_bio); 190178d7f5f7SNeilBrown return 0; 190278d7f5f7SNeilBrown } 19033a9f28a5SNeilBrown /* Try next page */ 19043a9f28a5SNeilBrown sectors -= s; 19053a9f28a5SNeilBrown sect += s; 19063a9f28a5SNeilBrown idx++; 19073a9f28a5SNeilBrown continue; 19083a9f28a5SNeilBrown } 190978d7f5f7SNeilBrown 191078d7f5f7SNeilBrown start = d; 191169382e85SNeilBrown /* write it back and re-read */ 191269382e85SNeilBrown while (d != r1_bio->read_disk) { 191369382e85SNeilBrown if (d == 0) 19148f19ccb2SNeilBrown d = conf->raid_disks * 2; 191569382e85SNeilBrown d--; 191669382e85SNeilBrown if (r1_bio->bios[d]->bi_end_io != end_sync_read) 191769382e85SNeilBrown continue; 191869382e85SNeilBrown rdev = conf->mirrors[d].rdev; 1919d8f05d29SNeilBrown if (r1_sync_page_io(rdev, sect, s, 192069382e85SNeilBrown bio->bi_io_vec[idx].bv_page, 1921d8f05d29SNeilBrown WRITE) == 0) { 192278d7f5f7SNeilBrown r1_bio->bios[d]->bi_end_io = NULL; 192378d7f5f7SNeilBrown rdev_dec_pending(rdev, mddev); 19249d3d8011SNamhyung Kim } 1925097426f6SNeilBrown } 1926097426f6SNeilBrown d = start; 1927097426f6SNeilBrown while (d != r1_bio->read_disk) { 1928097426f6SNeilBrown if (d == 0) 19298f19ccb2SNeilBrown d = conf->raid_disks * 2; 1930097426f6SNeilBrown d--; 1931097426f6SNeilBrown if (r1_bio->bios[d]->bi_end_io != end_sync_read) 1932097426f6SNeilBrown continue; 1933097426f6SNeilBrown rdev = conf->mirrors[d].rdev; 1934d8f05d29SNeilBrown if (r1_sync_page_io(rdev, sect, s, 193569382e85SNeilBrown bio->bi_io_vec[idx].bv_page, 1936d8f05d29SNeilBrown READ) != 0) 19379d3d8011SNamhyung Kim atomic_add(s, &rdev->corrected_errors); 193869382e85SNeilBrown } 193969382e85SNeilBrown sectors -= s; 194069382e85SNeilBrown sect += s; 194169382e85SNeilBrown idx ++; 194269382e85SNeilBrown } 194378d7f5f7SNeilBrown set_bit(R1BIO_Uptodate, &r1_bio->state); 19447ca78d57SNeilBrown set_bit(BIO_UPTODATE, &bio->bi_flags); 1945a68e5870SNeilBrown return 1; 194669382e85SNeilBrown } 1947d11c171eSNeilBrown 19489f2c9d12SNeilBrown static int process_checks(struct r1bio *r1_bio) 1949a68e5870SNeilBrown { 1950a68e5870SNeilBrown /* We have read all readable devices. If we haven't 1951a68e5870SNeilBrown * got the block, then there is no hope left. 1952a68e5870SNeilBrown * If we have, then we want to do a comparison 1953a68e5870SNeilBrown * and skip the write if everything is the same. 1954a68e5870SNeilBrown * If any blocks failed to read, then we need to 1955a68e5870SNeilBrown * attempt an over-write 1956a68e5870SNeilBrown */ 1957fd01b88cSNeilBrown struct mddev *mddev = r1_bio->mddev; 1958e8096360SNeilBrown struct r1conf *conf = mddev->private; 1959a68e5870SNeilBrown int primary; 1960a68e5870SNeilBrown int i; 1961f4380a91Smajianpeng int vcnt; 1962a68e5870SNeilBrown 196330bc9b53SNeilBrown /* Fix variable parts of all bios */ 196430bc9b53SNeilBrown vcnt = (r1_bio->sectors + PAGE_SIZE / 512 - 1) >> (PAGE_SHIFT - 9); 196530bc9b53SNeilBrown for (i = 0; i < conf->raid_disks * 2; i++) { 196630bc9b53SNeilBrown int j; 196730bc9b53SNeilBrown int size; 19681877db75SNeilBrown int uptodate; 196930bc9b53SNeilBrown struct bio *b = r1_bio->bios[i]; 197030bc9b53SNeilBrown if (b->bi_end_io != end_sync_read) 197130bc9b53SNeilBrown continue; 19721877db75SNeilBrown /* fixup the bio for reuse, but preserve BIO_UPTODATE */ 19731877db75SNeilBrown uptodate = test_bit(BIO_UPTODATE, &b->bi_flags); 197430bc9b53SNeilBrown bio_reset(b); 19751877db75SNeilBrown if (!uptodate) 19761877db75SNeilBrown clear_bit(BIO_UPTODATE, &b->bi_flags); 197730bc9b53SNeilBrown b->bi_vcnt = vcnt; 19784f024f37SKent Overstreet b->bi_iter.bi_size = r1_bio->sectors << 9; 19794f024f37SKent Overstreet b->bi_iter.bi_sector = r1_bio->sector + 198030bc9b53SNeilBrown conf->mirrors[i].rdev->data_offset; 198130bc9b53SNeilBrown b->bi_bdev = conf->mirrors[i].rdev->bdev; 198230bc9b53SNeilBrown b->bi_end_io = end_sync_read; 198330bc9b53SNeilBrown b->bi_private = r1_bio; 198430bc9b53SNeilBrown 19854f024f37SKent Overstreet size = b->bi_iter.bi_size; 198630bc9b53SNeilBrown for (j = 0; j < vcnt ; j++) { 198730bc9b53SNeilBrown struct bio_vec *bi; 198830bc9b53SNeilBrown bi = &b->bi_io_vec[j]; 198930bc9b53SNeilBrown bi->bv_offset = 0; 199030bc9b53SNeilBrown if (size > PAGE_SIZE) 199130bc9b53SNeilBrown bi->bv_len = PAGE_SIZE; 199230bc9b53SNeilBrown else 199330bc9b53SNeilBrown bi->bv_len = size; 199430bc9b53SNeilBrown size -= PAGE_SIZE; 199530bc9b53SNeilBrown } 199630bc9b53SNeilBrown } 19978f19ccb2SNeilBrown for (primary = 0; primary < conf->raid_disks * 2; primary++) 1998a68e5870SNeilBrown if (r1_bio->bios[primary]->bi_end_io == end_sync_read && 1999a68e5870SNeilBrown test_bit(BIO_UPTODATE, &r1_bio->bios[primary]->bi_flags)) { 2000a68e5870SNeilBrown r1_bio->bios[primary]->bi_end_io = NULL; 2001a68e5870SNeilBrown rdev_dec_pending(conf->mirrors[primary].rdev, mddev); 2002a68e5870SNeilBrown break; 2003a68e5870SNeilBrown } 2004a68e5870SNeilBrown r1_bio->read_disk = primary; 20058f19ccb2SNeilBrown for (i = 0; i < conf->raid_disks * 2; i++) { 2006a68e5870SNeilBrown int j; 2007a68e5870SNeilBrown struct bio *pbio = r1_bio->bios[primary]; 2008a68e5870SNeilBrown struct bio *sbio = r1_bio->bios[i]; 20091877db75SNeilBrown int uptodate = test_bit(BIO_UPTODATE, &sbio->bi_flags); 201078d7f5f7SNeilBrown 20112aabaa65SKent Overstreet if (sbio->bi_end_io != end_sync_read) 201278d7f5f7SNeilBrown continue; 20131877db75SNeilBrown /* Now we can 'fixup' the BIO_UPTODATE flag */ 20141877db75SNeilBrown set_bit(BIO_UPTODATE, &sbio->bi_flags); 2015a68e5870SNeilBrown 20161877db75SNeilBrown if (uptodate) { 2017a68e5870SNeilBrown for (j = vcnt; j-- ; ) { 2018a68e5870SNeilBrown struct page *p, *s; 2019a68e5870SNeilBrown p = pbio->bi_io_vec[j].bv_page; 2020a68e5870SNeilBrown s = sbio->bi_io_vec[j].bv_page; 2021a68e5870SNeilBrown if (memcmp(page_address(p), 2022a68e5870SNeilBrown page_address(s), 20235020ad7dSNeilBrown sbio->bi_io_vec[j].bv_len)) 2024a68e5870SNeilBrown break; 2025a68e5870SNeilBrown } 2026a68e5870SNeilBrown } else 2027a68e5870SNeilBrown j = 0; 2028a68e5870SNeilBrown if (j >= 0) 20297f7583d4SJianpeng Ma atomic64_add(r1_bio->sectors, &mddev->resync_mismatches); 2030a68e5870SNeilBrown if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery) 20311877db75SNeilBrown && uptodate)) { 203278d7f5f7SNeilBrown /* No need to write to this device. */ 2033a68e5870SNeilBrown sbio->bi_end_io = NULL; 2034a68e5870SNeilBrown rdev_dec_pending(conf->mirrors[i].rdev, mddev); 203578d7f5f7SNeilBrown continue; 203678d7f5f7SNeilBrown } 2037d3b45c2aSKent Overstreet 2038d3b45c2aSKent Overstreet bio_copy_data(sbio, pbio); 2039a68e5870SNeilBrown } 2040a68e5870SNeilBrown return 0; 2041a68e5870SNeilBrown } 2042a68e5870SNeilBrown 20439f2c9d12SNeilBrown static void sync_request_write(struct mddev *mddev, struct r1bio *r1_bio) 2044a68e5870SNeilBrown { 2045e8096360SNeilBrown struct r1conf *conf = mddev->private; 2046a68e5870SNeilBrown int i; 20478f19ccb2SNeilBrown int disks = conf->raid_disks * 2; 2048a68e5870SNeilBrown struct bio *bio, *wbio; 2049a68e5870SNeilBrown 2050a68e5870SNeilBrown bio = r1_bio->bios[r1_bio->read_disk]; 2051a68e5870SNeilBrown 2052a68e5870SNeilBrown if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) 2053a68e5870SNeilBrown /* ouch - failed to read all of that. */ 2054a68e5870SNeilBrown if (!fix_sync_read_error(r1_bio)) 2055a68e5870SNeilBrown return; 20567ca78d57SNeilBrown 20577ca78d57SNeilBrown if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) 20587ca78d57SNeilBrown if (process_checks(r1_bio) < 0) 20597ca78d57SNeilBrown return; 2060d11c171eSNeilBrown /* 2061d11c171eSNeilBrown * schedule writes 2062d11c171eSNeilBrown */ 20631da177e4SLinus Torvalds atomic_set(&r1_bio->remaining, 1); 20641da177e4SLinus Torvalds for (i = 0; i < disks ; i++) { 20651da177e4SLinus Torvalds wbio = r1_bio->bios[i]; 20663e198f78SNeilBrown if (wbio->bi_end_io == NULL || 20673e198f78SNeilBrown (wbio->bi_end_io == end_sync_read && 20683e198f78SNeilBrown (i == r1_bio->read_disk || 20693e198f78SNeilBrown !test_bit(MD_RECOVERY_SYNC, &mddev->recovery)))) 20701da177e4SLinus Torvalds continue; 20711da177e4SLinus Torvalds 20723e198f78SNeilBrown wbio->bi_rw = WRITE; 20733e198f78SNeilBrown wbio->bi_end_io = end_sync_write; 20741da177e4SLinus Torvalds atomic_inc(&r1_bio->remaining); 2075aa8b57aaSKent Overstreet md_sync_acct(conf->mirrors[i].rdev->bdev, bio_sectors(wbio)); 2076191ea9b2SNeilBrown 20771da177e4SLinus Torvalds generic_make_request(wbio); 20781da177e4SLinus Torvalds } 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds if (atomic_dec_and_test(&r1_bio->remaining)) { 2081191ea9b2SNeilBrown /* if we're here, all write(s) have completed, so clean up */ 208258e94ae1SNeilBrown int s = r1_bio->sectors; 208358e94ae1SNeilBrown if (test_bit(R1BIO_MadeGood, &r1_bio->state) || 208458e94ae1SNeilBrown test_bit(R1BIO_WriteError, &r1_bio->state)) 208558e94ae1SNeilBrown reschedule_retry(r1_bio); 208658e94ae1SNeilBrown else { 20871da177e4SLinus Torvalds put_buf(r1_bio); 208858e94ae1SNeilBrown md_done_sync(mddev, s, 1); 208958e94ae1SNeilBrown } 20901da177e4SLinus Torvalds } 20911da177e4SLinus Torvalds } 20921da177e4SLinus Torvalds 20931da177e4SLinus Torvalds /* 20941da177e4SLinus Torvalds * This is a kernel thread which: 20951da177e4SLinus Torvalds * 20961da177e4SLinus Torvalds * 1. Retries failed read operations on working mirrors. 20971da177e4SLinus Torvalds * 2. Updates the raid superblock when problems encounter. 2098d2eb35acSNeilBrown * 3. Performs writes following reads for array synchronising. 20991da177e4SLinus Torvalds */ 21001da177e4SLinus Torvalds 2101e8096360SNeilBrown static void fix_read_error(struct r1conf *conf, int read_disk, 2102867868fbSNeilBrown sector_t sect, int sectors) 2103867868fbSNeilBrown { 2104fd01b88cSNeilBrown struct mddev *mddev = conf->mddev; 2105867868fbSNeilBrown while(sectors) { 2106867868fbSNeilBrown int s = sectors; 2107867868fbSNeilBrown int d = read_disk; 2108867868fbSNeilBrown int success = 0; 2109867868fbSNeilBrown int start; 21103cb03002SNeilBrown struct md_rdev *rdev; 2111867868fbSNeilBrown 2112867868fbSNeilBrown if (s > (PAGE_SIZE>>9)) 2113867868fbSNeilBrown s = PAGE_SIZE >> 9; 2114867868fbSNeilBrown 2115867868fbSNeilBrown do { 2116867868fbSNeilBrown /* Note: no rcu protection needed here 2117867868fbSNeilBrown * as this is synchronous in the raid1d thread 2118867868fbSNeilBrown * which is the thread that might remove 2119867868fbSNeilBrown * a device. If raid1d ever becomes multi-threaded.... 2120867868fbSNeilBrown */ 2121d2eb35acSNeilBrown sector_t first_bad; 2122d2eb35acSNeilBrown int bad_sectors; 2123d2eb35acSNeilBrown 2124867868fbSNeilBrown rdev = conf->mirrors[d].rdev; 2125867868fbSNeilBrown if (rdev && 2126da8840a7Smajianpeng (test_bit(In_sync, &rdev->flags) || 2127da8840a7Smajianpeng (!test_bit(Faulty, &rdev->flags) && 2128da8840a7Smajianpeng rdev->recovery_offset >= sect + s)) && 2129d2eb35acSNeilBrown is_badblock(rdev, sect, s, 2130d2eb35acSNeilBrown &first_bad, &bad_sectors) == 0 && 2131ccebd4c4SJonathan Brassow sync_page_io(rdev, sect, s<<9, 2132ccebd4c4SJonathan Brassow conf->tmppage, READ, false)) 2133867868fbSNeilBrown success = 1; 2134867868fbSNeilBrown else { 2135867868fbSNeilBrown d++; 21368f19ccb2SNeilBrown if (d == conf->raid_disks * 2) 2137867868fbSNeilBrown d = 0; 2138867868fbSNeilBrown } 2139867868fbSNeilBrown } while (!success && d != read_disk); 2140867868fbSNeilBrown 2141867868fbSNeilBrown if (!success) { 2142d8f05d29SNeilBrown /* Cannot read from anywhere - mark it bad */ 21433cb03002SNeilBrown struct md_rdev *rdev = conf->mirrors[read_disk].rdev; 2144d8f05d29SNeilBrown if (!rdev_set_badblocks(rdev, sect, s, 0)) 2145d8f05d29SNeilBrown md_error(mddev, rdev); 2146867868fbSNeilBrown break; 2147867868fbSNeilBrown } 2148867868fbSNeilBrown /* write it back and re-read */ 2149867868fbSNeilBrown start = d; 2150867868fbSNeilBrown while (d != read_disk) { 2151867868fbSNeilBrown if (d==0) 21528f19ccb2SNeilBrown d = conf->raid_disks * 2; 2153867868fbSNeilBrown d--; 2154867868fbSNeilBrown rdev = conf->mirrors[d].rdev; 2155867868fbSNeilBrown if (rdev && 2156d8f05d29SNeilBrown test_bit(In_sync, &rdev->flags)) 2157d8f05d29SNeilBrown r1_sync_page_io(rdev, sect, s, 2158d8f05d29SNeilBrown conf->tmppage, WRITE); 2159867868fbSNeilBrown } 2160867868fbSNeilBrown d = start; 2161867868fbSNeilBrown while (d != read_disk) { 2162867868fbSNeilBrown char b[BDEVNAME_SIZE]; 2163867868fbSNeilBrown if (d==0) 21648f19ccb2SNeilBrown d = conf->raid_disks * 2; 2165867868fbSNeilBrown d--; 2166867868fbSNeilBrown rdev = conf->mirrors[d].rdev; 2167867868fbSNeilBrown if (rdev && 2168867868fbSNeilBrown test_bit(In_sync, &rdev->flags)) { 2169d8f05d29SNeilBrown if (r1_sync_page_io(rdev, sect, s, 2170d8f05d29SNeilBrown conf->tmppage, READ)) { 2171867868fbSNeilBrown atomic_add(s, &rdev->corrected_errors); 2172867868fbSNeilBrown printk(KERN_INFO 21739dd1e2faSNeilBrown "md/raid1:%s: read error corrected " 2174867868fbSNeilBrown "(%d sectors at %llu on %s)\n", 2175867868fbSNeilBrown mdname(mddev), s, 2176969b755aSRandy Dunlap (unsigned long long)(sect + 2177969b755aSRandy Dunlap rdev->data_offset), 2178867868fbSNeilBrown bdevname(rdev->bdev, b)); 2179867868fbSNeilBrown } 2180867868fbSNeilBrown } 2181867868fbSNeilBrown } 2182867868fbSNeilBrown sectors -= s; 2183867868fbSNeilBrown sect += s; 2184867868fbSNeilBrown } 2185867868fbSNeilBrown } 2186867868fbSNeilBrown 21879f2c9d12SNeilBrown static int narrow_write_error(struct r1bio *r1_bio, int i) 2188cd5ff9a1SNeilBrown { 2189fd01b88cSNeilBrown struct mddev *mddev = r1_bio->mddev; 2190e8096360SNeilBrown struct r1conf *conf = mddev->private; 21913cb03002SNeilBrown struct md_rdev *rdev = conf->mirrors[i].rdev; 2192cd5ff9a1SNeilBrown 2193cd5ff9a1SNeilBrown /* bio has the data to be written to device 'i' where 2194cd5ff9a1SNeilBrown * we just recently had a write error. 2195cd5ff9a1SNeilBrown * We repeatedly clone the bio and trim down to one block, 2196cd5ff9a1SNeilBrown * then try the write. Where the write fails we record 2197cd5ff9a1SNeilBrown * a bad block. 2198cd5ff9a1SNeilBrown * It is conceivable that the bio doesn't exactly align with 2199cd5ff9a1SNeilBrown * blocks. We must handle this somehow. 2200cd5ff9a1SNeilBrown * 2201cd5ff9a1SNeilBrown * We currently own a reference on the rdev. 2202cd5ff9a1SNeilBrown */ 2203cd5ff9a1SNeilBrown 2204cd5ff9a1SNeilBrown int block_sectors; 2205cd5ff9a1SNeilBrown sector_t sector; 2206cd5ff9a1SNeilBrown int sectors; 2207cd5ff9a1SNeilBrown int sect_to_write = r1_bio->sectors; 2208cd5ff9a1SNeilBrown int ok = 1; 2209cd5ff9a1SNeilBrown 2210cd5ff9a1SNeilBrown if (rdev->badblocks.shift < 0) 2211cd5ff9a1SNeilBrown return 0; 2212cd5ff9a1SNeilBrown 2213cd5ff9a1SNeilBrown block_sectors = 1 << rdev->badblocks.shift; 2214cd5ff9a1SNeilBrown sector = r1_bio->sector; 2215cd5ff9a1SNeilBrown sectors = ((sector + block_sectors) 2216cd5ff9a1SNeilBrown & ~(sector_t)(block_sectors - 1)) 2217cd5ff9a1SNeilBrown - sector; 2218cd5ff9a1SNeilBrown 2219cd5ff9a1SNeilBrown while (sect_to_write) { 2220cd5ff9a1SNeilBrown struct bio *wbio; 2221cd5ff9a1SNeilBrown if (sectors > sect_to_write) 2222cd5ff9a1SNeilBrown sectors = sect_to_write; 2223cd5ff9a1SNeilBrown /* Write at 'sector' for 'sectors'*/ 2224cd5ff9a1SNeilBrown 2225b783863fSKent Overstreet if (test_bit(R1BIO_BehindIO, &r1_bio->state)) { 2226b783863fSKent Overstreet unsigned vcnt = r1_bio->behind_page_count; 2227b783863fSKent Overstreet struct bio_vec *vec = r1_bio->behind_bvecs; 2228b783863fSKent Overstreet 2229b783863fSKent Overstreet while (!vec->bv_page) { 2230b783863fSKent Overstreet vec++; 2231b783863fSKent Overstreet vcnt--; 2232b783863fSKent Overstreet } 2233b783863fSKent Overstreet 2234cd5ff9a1SNeilBrown wbio = bio_alloc_mddev(GFP_NOIO, vcnt, mddev); 2235cd5ff9a1SNeilBrown memcpy(wbio->bi_io_vec, vec, vcnt * sizeof(struct bio_vec)); 2236b783863fSKent Overstreet 2237cd5ff9a1SNeilBrown wbio->bi_vcnt = vcnt; 2238b783863fSKent Overstreet } else { 2239b783863fSKent Overstreet wbio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev); 2240b783863fSKent Overstreet } 2241b783863fSKent Overstreet 2242b783863fSKent Overstreet wbio->bi_rw = WRITE; 22434f024f37SKent Overstreet wbio->bi_iter.bi_sector = r1_bio->sector; 22444f024f37SKent Overstreet wbio->bi_iter.bi_size = r1_bio->sectors << 9; 2245cd5ff9a1SNeilBrown 22466678d83fSKent Overstreet bio_trim(wbio, sector - r1_bio->sector, sectors); 22474f024f37SKent Overstreet wbio->bi_iter.bi_sector += rdev->data_offset; 2248cd5ff9a1SNeilBrown wbio->bi_bdev = rdev->bdev; 2249cd5ff9a1SNeilBrown if (submit_bio_wait(WRITE, wbio) == 0) 2250cd5ff9a1SNeilBrown /* failure! */ 2251cd5ff9a1SNeilBrown ok = rdev_set_badblocks(rdev, sector, 2252cd5ff9a1SNeilBrown sectors, 0) 2253cd5ff9a1SNeilBrown && ok; 2254cd5ff9a1SNeilBrown 2255cd5ff9a1SNeilBrown bio_put(wbio); 2256cd5ff9a1SNeilBrown sect_to_write -= sectors; 2257cd5ff9a1SNeilBrown sector += sectors; 2258cd5ff9a1SNeilBrown sectors = block_sectors; 2259cd5ff9a1SNeilBrown } 2260cd5ff9a1SNeilBrown return ok; 2261cd5ff9a1SNeilBrown } 2262cd5ff9a1SNeilBrown 2263e8096360SNeilBrown static void handle_sync_write_finished(struct r1conf *conf, struct r1bio *r1_bio) 226462096bceSNeilBrown { 226562096bceSNeilBrown int m; 226662096bceSNeilBrown int s = r1_bio->sectors; 22678f19ccb2SNeilBrown for (m = 0; m < conf->raid_disks * 2 ; m++) { 22683cb03002SNeilBrown struct md_rdev *rdev = conf->mirrors[m].rdev; 226962096bceSNeilBrown struct bio *bio = r1_bio->bios[m]; 227062096bceSNeilBrown if (bio->bi_end_io == NULL) 227162096bceSNeilBrown continue; 227262096bceSNeilBrown if (test_bit(BIO_UPTODATE, &bio->bi_flags) && 227362096bceSNeilBrown test_bit(R1BIO_MadeGood, &r1_bio->state)) { 2274c6563a8cSNeilBrown rdev_clear_badblocks(rdev, r1_bio->sector, s, 0); 227562096bceSNeilBrown } 227662096bceSNeilBrown if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && 227762096bceSNeilBrown test_bit(R1BIO_WriteError, &r1_bio->state)) { 227862096bceSNeilBrown if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0)) 227962096bceSNeilBrown md_error(conf->mddev, rdev); 228062096bceSNeilBrown } 228162096bceSNeilBrown } 228262096bceSNeilBrown put_buf(r1_bio); 228362096bceSNeilBrown md_done_sync(conf->mddev, s, 1); 228462096bceSNeilBrown } 228562096bceSNeilBrown 2286e8096360SNeilBrown static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio) 228762096bceSNeilBrown { 228862096bceSNeilBrown int m; 22898f19ccb2SNeilBrown for (m = 0; m < conf->raid_disks * 2 ; m++) 229062096bceSNeilBrown if (r1_bio->bios[m] == IO_MADE_GOOD) { 22913cb03002SNeilBrown struct md_rdev *rdev = conf->mirrors[m].rdev; 229262096bceSNeilBrown rdev_clear_badblocks(rdev, 229362096bceSNeilBrown r1_bio->sector, 2294c6563a8cSNeilBrown r1_bio->sectors, 0); 229562096bceSNeilBrown rdev_dec_pending(rdev, conf->mddev); 229662096bceSNeilBrown } else if (r1_bio->bios[m] != NULL) { 229762096bceSNeilBrown /* This drive got a write error. We need to 229862096bceSNeilBrown * narrow down and record precise write 229962096bceSNeilBrown * errors. 230062096bceSNeilBrown */ 230162096bceSNeilBrown if (!narrow_write_error(r1_bio, m)) { 230262096bceSNeilBrown md_error(conf->mddev, 230362096bceSNeilBrown conf->mirrors[m].rdev); 230462096bceSNeilBrown /* an I/O failed, we can't clear the bitmap */ 230562096bceSNeilBrown set_bit(R1BIO_Degraded, &r1_bio->state); 230662096bceSNeilBrown } 230762096bceSNeilBrown rdev_dec_pending(conf->mirrors[m].rdev, 230862096bceSNeilBrown conf->mddev); 230962096bceSNeilBrown } 231062096bceSNeilBrown if (test_bit(R1BIO_WriteError, &r1_bio->state)) 231162096bceSNeilBrown close_write(r1_bio); 231262096bceSNeilBrown raid_end_bio_io(r1_bio); 231362096bceSNeilBrown } 231462096bceSNeilBrown 2315e8096360SNeilBrown static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio) 231662096bceSNeilBrown { 231762096bceSNeilBrown int disk; 231862096bceSNeilBrown int max_sectors; 2319fd01b88cSNeilBrown struct mddev *mddev = conf->mddev; 232062096bceSNeilBrown struct bio *bio; 232162096bceSNeilBrown char b[BDEVNAME_SIZE]; 23223cb03002SNeilBrown struct md_rdev *rdev; 232362096bceSNeilBrown 232462096bceSNeilBrown clear_bit(R1BIO_ReadError, &r1_bio->state); 232562096bceSNeilBrown /* we got a read error. Maybe the drive is bad. Maybe just 232662096bceSNeilBrown * the block and we can fix it. 232762096bceSNeilBrown * We freeze all other IO, and try reading the block from 232862096bceSNeilBrown * other devices. When we find one, we re-write 232962096bceSNeilBrown * and check it that fixes the read error. 233062096bceSNeilBrown * This is all done synchronously while the array is 233162096bceSNeilBrown * frozen 233262096bceSNeilBrown */ 233362096bceSNeilBrown if (mddev->ro == 0) { 2334e2d59925SNeilBrown freeze_array(conf, 1); 233562096bceSNeilBrown fix_read_error(conf, r1_bio->read_disk, 233662096bceSNeilBrown r1_bio->sector, r1_bio->sectors); 233762096bceSNeilBrown unfreeze_array(conf); 233862096bceSNeilBrown } else 233962096bceSNeilBrown md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev); 23407ad4d4a6SNeilBrown rdev_dec_pending(conf->mirrors[r1_bio->read_disk].rdev, conf->mddev); 234162096bceSNeilBrown 234262096bceSNeilBrown bio = r1_bio->bios[r1_bio->read_disk]; 234362096bceSNeilBrown bdevname(bio->bi_bdev, b); 234462096bceSNeilBrown read_more: 234562096bceSNeilBrown disk = read_balance(conf, r1_bio, &max_sectors); 234662096bceSNeilBrown if (disk == -1) { 234762096bceSNeilBrown printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O" 234862096bceSNeilBrown " read error for block %llu\n", 234962096bceSNeilBrown mdname(mddev), b, (unsigned long long)r1_bio->sector); 235062096bceSNeilBrown raid_end_bio_io(r1_bio); 235162096bceSNeilBrown } else { 235262096bceSNeilBrown const unsigned long do_sync 235362096bceSNeilBrown = r1_bio->master_bio->bi_rw & REQ_SYNC; 235462096bceSNeilBrown if (bio) { 235562096bceSNeilBrown r1_bio->bios[r1_bio->read_disk] = 235662096bceSNeilBrown mddev->ro ? IO_BLOCKED : NULL; 235762096bceSNeilBrown bio_put(bio); 235862096bceSNeilBrown } 235962096bceSNeilBrown r1_bio->read_disk = disk; 236062096bceSNeilBrown bio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev); 23614f024f37SKent Overstreet bio_trim(bio, r1_bio->sector - bio->bi_iter.bi_sector, 23624f024f37SKent Overstreet max_sectors); 236362096bceSNeilBrown r1_bio->bios[r1_bio->read_disk] = bio; 236462096bceSNeilBrown rdev = conf->mirrors[disk].rdev; 236562096bceSNeilBrown printk_ratelimited(KERN_ERR 236662096bceSNeilBrown "md/raid1:%s: redirecting sector %llu" 236762096bceSNeilBrown " to other mirror: %s\n", 236862096bceSNeilBrown mdname(mddev), 236962096bceSNeilBrown (unsigned long long)r1_bio->sector, 237062096bceSNeilBrown bdevname(rdev->bdev, b)); 23714f024f37SKent Overstreet bio->bi_iter.bi_sector = r1_bio->sector + rdev->data_offset; 237262096bceSNeilBrown bio->bi_bdev = rdev->bdev; 237362096bceSNeilBrown bio->bi_end_io = raid1_end_read_request; 237462096bceSNeilBrown bio->bi_rw = READ | do_sync; 237562096bceSNeilBrown bio->bi_private = r1_bio; 237662096bceSNeilBrown if (max_sectors < r1_bio->sectors) { 237762096bceSNeilBrown /* Drat - have to split this up more */ 237862096bceSNeilBrown struct bio *mbio = r1_bio->master_bio; 237962096bceSNeilBrown int sectors_handled = (r1_bio->sector + max_sectors 23804f024f37SKent Overstreet - mbio->bi_iter.bi_sector); 238162096bceSNeilBrown r1_bio->sectors = max_sectors; 238262096bceSNeilBrown spin_lock_irq(&conf->device_lock); 238362096bceSNeilBrown if (mbio->bi_phys_segments == 0) 238462096bceSNeilBrown mbio->bi_phys_segments = 2; 238562096bceSNeilBrown else 238662096bceSNeilBrown mbio->bi_phys_segments++; 238762096bceSNeilBrown spin_unlock_irq(&conf->device_lock); 238862096bceSNeilBrown generic_make_request(bio); 238962096bceSNeilBrown bio = NULL; 239062096bceSNeilBrown 239162096bceSNeilBrown r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO); 239262096bceSNeilBrown 239362096bceSNeilBrown r1_bio->master_bio = mbio; 2394aa8b57aaSKent Overstreet r1_bio->sectors = bio_sectors(mbio) - sectors_handled; 239562096bceSNeilBrown r1_bio->state = 0; 239662096bceSNeilBrown set_bit(R1BIO_ReadError, &r1_bio->state); 239762096bceSNeilBrown r1_bio->mddev = mddev; 23984f024f37SKent Overstreet r1_bio->sector = mbio->bi_iter.bi_sector + 23994f024f37SKent Overstreet sectors_handled; 240062096bceSNeilBrown 240162096bceSNeilBrown goto read_more; 240262096bceSNeilBrown } else 240362096bceSNeilBrown generic_make_request(bio); 240462096bceSNeilBrown } 240562096bceSNeilBrown } 240662096bceSNeilBrown 24074ed8731dSShaohua Li static void raid1d(struct md_thread *thread) 24081da177e4SLinus Torvalds { 24094ed8731dSShaohua Li struct mddev *mddev = thread->mddev; 24109f2c9d12SNeilBrown struct r1bio *r1_bio; 24111da177e4SLinus Torvalds unsigned long flags; 2412e8096360SNeilBrown struct r1conf *conf = mddev->private; 24131da177e4SLinus Torvalds struct list_head *head = &conf->retry_list; 2414e1dfa0a2SNeilBrown struct blk_plug plug; 24151da177e4SLinus Torvalds 24161da177e4SLinus Torvalds md_check_recovery(mddev); 24171da177e4SLinus Torvalds 2418e1dfa0a2SNeilBrown blk_start_plug(&plug); 24191da177e4SLinus Torvalds for (;;) { 2420a35e63efSNeilBrown 24217eaceaccSJens Axboe flush_pending_writes(conf); 2422a35e63efSNeilBrown 24231da177e4SLinus Torvalds spin_lock_irqsave(&conf->device_lock, flags); 2424a35e63efSNeilBrown if (list_empty(head)) { 2425191ea9b2SNeilBrown spin_unlock_irqrestore(&conf->device_lock, flags); 24261da177e4SLinus Torvalds break; 2427a35e63efSNeilBrown } 24289f2c9d12SNeilBrown r1_bio = list_entry(head->prev, struct r1bio, retry_list); 24291da177e4SLinus Torvalds list_del(head->prev); 2430ddaf22abSNeilBrown conf->nr_queued--; 24311da177e4SLinus Torvalds spin_unlock_irqrestore(&conf->device_lock, flags); 24321da177e4SLinus Torvalds 24331da177e4SLinus Torvalds mddev = r1_bio->mddev; 2434070ec55dSNeilBrown conf = mddev->private; 24354367af55SNeilBrown if (test_bit(R1BIO_IsSync, &r1_bio->state)) { 2436d8f05d29SNeilBrown if (test_bit(R1BIO_MadeGood, &r1_bio->state) || 243762096bceSNeilBrown test_bit(R1BIO_WriteError, &r1_bio->state)) 243862096bceSNeilBrown handle_sync_write_finished(conf, r1_bio); 243962096bceSNeilBrown else 24401da177e4SLinus Torvalds sync_request_write(mddev, r1_bio); 2441cd5ff9a1SNeilBrown } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) || 244262096bceSNeilBrown test_bit(R1BIO_WriteError, &r1_bio->state)) 244362096bceSNeilBrown handle_write_finished(conf, r1_bio); 244462096bceSNeilBrown else if (test_bit(R1BIO_ReadError, &r1_bio->state)) 244562096bceSNeilBrown handle_read_error(conf, r1_bio); 2446d2eb35acSNeilBrown else 2447d2eb35acSNeilBrown /* just a partial read to be scheduled from separate 2448d2eb35acSNeilBrown * context 2449d2eb35acSNeilBrown */ 2450d2eb35acSNeilBrown generic_make_request(r1_bio->bios[r1_bio->read_disk]); 245162096bceSNeilBrown 24521d9d5241SNeilBrown cond_resched(); 2453de393cdeSNeilBrown if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) 2454de393cdeSNeilBrown md_check_recovery(mddev); 24551da177e4SLinus Torvalds } 2456e1dfa0a2SNeilBrown blk_finish_plug(&plug); 24571da177e4SLinus Torvalds } 24581da177e4SLinus Torvalds 24591da177e4SLinus Torvalds 2460e8096360SNeilBrown static int init_resync(struct r1conf *conf) 24611da177e4SLinus Torvalds { 24621da177e4SLinus Torvalds int buffs; 24631da177e4SLinus Torvalds 24641da177e4SLinus Torvalds buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE; 24659e77c485SEric Sesterhenn BUG_ON(conf->r1buf_pool); 24661da177e4SLinus Torvalds conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free, 24671da177e4SLinus Torvalds conf->poolinfo); 24681da177e4SLinus Torvalds if (!conf->r1buf_pool) 24691da177e4SLinus Torvalds return -ENOMEM; 24701da177e4SLinus Torvalds conf->next_resync = 0; 24711da177e4SLinus Torvalds return 0; 24721da177e4SLinus Torvalds } 24731da177e4SLinus Torvalds 24741da177e4SLinus Torvalds /* 24751da177e4SLinus Torvalds * perform a "sync" on one "block" 24761da177e4SLinus Torvalds * 24771da177e4SLinus Torvalds * We need to make sure that no normal I/O request - particularly write 24781da177e4SLinus Torvalds * requests - conflict with active sync requests. 24791da177e4SLinus Torvalds * 24801da177e4SLinus Torvalds * This is achieved by tracking pending requests and a 'barrier' concept 24811da177e4SLinus Torvalds * that can be installed to exclude normal IO requests. 24821da177e4SLinus Torvalds */ 24831da177e4SLinus Torvalds 2484fd01b88cSNeilBrown static sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster) 24851da177e4SLinus Torvalds { 2486e8096360SNeilBrown struct r1conf *conf = mddev->private; 24879f2c9d12SNeilBrown struct r1bio *r1_bio; 24881da177e4SLinus Torvalds struct bio *bio; 24891da177e4SLinus Torvalds sector_t max_sector, nr_sectors; 24903e198f78SNeilBrown int disk = -1; 24911da177e4SLinus Torvalds int i; 24923e198f78SNeilBrown int wonly = -1; 24933e198f78SNeilBrown int write_targets = 0, read_targets = 0; 249457dab0bdSNeilBrown sector_t sync_blocks; 2495e3b9703eSNeilBrown int still_degraded = 0; 249606f60385SNeilBrown int good_sectors = RESYNC_SECTORS; 249706f60385SNeilBrown int min_bad = 0; /* number of sectors that are bad in all devices */ 24981da177e4SLinus Torvalds 24991da177e4SLinus Torvalds if (!conf->r1buf_pool) 25001da177e4SLinus Torvalds if (init_resync(conf)) 250157afd89fSNeilBrown return 0; 25021da177e4SLinus Torvalds 250358c0fed4SAndre Noll max_sector = mddev->dev_sectors; 25041da177e4SLinus Torvalds if (sector_nr >= max_sector) { 2505191ea9b2SNeilBrown /* If we aborted, we need to abort the 2506191ea9b2SNeilBrown * sync on the 'current' bitmap chunk (there will 2507191ea9b2SNeilBrown * only be one in raid1 resync. 2508191ea9b2SNeilBrown * We can find the current addess in mddev->curr_resync 2509191ea9b2SNeilBrown */ 25106a806c51SNeilBrown if (mddev->curr_resync < max_sector) /* aborted */ 25116a806c51SNeilBrown bitmap_end_sync(mddev->bitmap, mddev->curr_resync, 2512191ea9b2SNeilBrown &sync_blocks, 1); 25136a806c51SNeilBrown else /* completed sync */ 2514191ea9b2SNeilBrown conf->fullsync = 0; 25156a806c51SNeilBrown 25166a806c51SNeilBrown bitmap_close_sync(mddev->bitmap); 25171da177e4SLinus Torvalds close_sync(conf); 25181da177e4SLinus Torvalds return 0; 25191da177e4SLinus Torvalds } 25201da177e4SLinus Torvalds 252107d84d10SNeilBrown if (mddev->bitmap == NULL && 252207d84d10SNeilBrown mddev->recovery_cp == MaxSector && 25236394cca5SNeilBrown !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && 252407d84d10SNeilBrown conf->fullsync == 0) { 252507d84d10SNeilBrown *skipped = 1; 252607d84d10SNeilBrown return max_sector - sector_nr; 252707d84d10SNeilBrown } 25286394cca5SNeilBrown /* before building a request, check if we can skip these blocks.. 25296394cca5SNeilBrown * This call the bitmap_start_sync doesn't actually record anything 25306394cca5SNeilBrown */ 2531e3b9703eSNeilBrown if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && 2532e5de485fSNeilBrown !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { 2533191ea9b2SNeilBrown /* We can skip this block, and probably several more */ 2534191ea9b2SNeilBrown *skipped = 1; 2535191ea9b2SNeilBrown return sync_blocks; 2536191ea9b2SNeilBrown } 25371da177e4SLinus Torvalds /* 253817999be4SNeilBrown * If there is non-resync activity waiting for a turn, 253917999be4SNeilBrown * and resync is going fast enough, 254017999be4SNeilBrown * then let it though before starting on this new sync request. 25411da177e4SLinus Torvalds */ 254217999be4SNeilBrown if (!go_faster && conf->nr_waiting) 25431da177e4SLinus Torvalds msleep_interruptible(1000); 254417999be4SNeilBrown 2545b47490c9SNeilBrown bitmap_cond_end_sync(mddev->bitmap, sector_nr); 25461c4588e9SNeilBrown r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO); 254717999be4SNeilBrown 2548c2fd4c94SNeilBrown raise_barrier(conf, sector_nr); 25491da177e4SLinus Torvalds 25503e198f78SNeilBrown rcu_read_lock(); 25513e198f78SNeilBrown /* 25523e198f78SNeilBrown * If we get a correctably read error during resync or recovery, 25533e198f78SNeilBrown * we might want to read from a different device. So we 25543e198f78SNeilBrown * flag all drives that could conceivably be read from for READ, 25553e198f78SNeilBrown * and any others (which will be non-In_sync devices) for WRITE. 25563e198f78SNeilBrown * If a read fails, we try reading from something else for which READ 25573e198f78SNeilBrown * is OK. 25583e198f78SNeilBrown */ 25591da177e4SLinus Torvalds 25601da177e4SLinus Torvalds r1_bio->mddev = mddev; 25611da177e4SLinus Torvalds r1_bio->sector = sector_nr; 2562191ea9b2SNeilBrown r1_bio->state = 0; 25631da177e4SLinus Torvalds set_bit(R1BIO_IsSync, &r1_bio->state); 25641da177e4SLinus Torvalds 25658f19ccb2SNeilBrown for (i = 0; i < conf->raid_disks * 2; i++) { 25663cb03002SNeilBrown struct md_rdev *rdev; 25671da177e4SLinus Torvalds bio = r1_bio->bios[i]; 25682aabaa65SKent Overstreet bio_reset(bio); 25691da177e4SLinus Torvalds 25703e198f78SNeilBrown rdev = rcu_dereference(conf->mirrors[i].rdev); 25713e198f78SNeilBrown if (rdev == NULL || 25723e198f78SNeilBrown test_bit(Faulty, &rdev->flags)) { 25738f19ccb2SNeilBrown if (i < conf->raid_disks) 2574e3b9703eSNeilBrown still_degraded = 1; 25753e198f78SNeilBrown } else if (!test_bit(In_sync, &rdev->flags)) { 25761da177e4SLinus Torvalds bio->bi_rw = WRITE; 25771da177e4SLinus Torvalds bio->bi_end_io = end_sync_write; 25781da177e4SLinus Torvalds write_targets ++; 25793e198f78SNeilBrown } else { 25803e198f78SNeilBrown /* may need to read from here */ 258106f60385SNeilBrown sector_t first_bad = MaxSector; 258206f60385SNeilBrown int bad_sectors; 258306f60385SNeilBrown 258406f60385SNeilBrown if (is_badblock(rdev, sector_nr, good_sectors, 258506f60385SNeilBrown &first_bad, &bad_sectors)) { 258606f60385SNeilBrown if (first_bad > sector_nr) 258706f60385SNeilBrown good_sectors = first_bad - sector_nr; 258806f60385SNeilBrown else { 258906f60385SNeilBrown bad_sectors -= (sector_nr - first_bad); 259006f60385SNeilBrown if (min_bad == 0 || 259106f60385SNeilBrown min_bad > bad_sectors) 259206f60385SNeilBrown min_bad = bad_sectors; 259306f60385SNeilBrown } 259406f60385SNeilBrown } 259506f60385SNeilBrown if (sector_nr < first_bad) { 25963e198f78SNeilBrown if (test_bit(WriteMostly, &rdev->flags)) { 25973e198f78SNeilBrown if (wonly < 0) 25983e198f78SNeilBrown wonly = i; 25993e198f78SNeilBrown } else { 26003e198f78SNeilBrown if (disk < 0) 26013e198f78SNeilBrown disk = i; 26023e198f78SNeilBrown } 260306f60385SNeilBrown bio->bi_rw = READ; 260406f60385SNeilBrown bio->bi_end_io = end_sync_read; 26053e198f78SNeilBrown read_targets++; 2606d57368afSAlexander Lyakas } else if (!test_bit(WriteErrorSeen, &rdev->flags) && 2607d57368afSAlexander Lyakas test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && 2608d57368afSAlexander Lyakas !test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { 2609d57368afSAlexander Lyakas /* 2610d57368afSAlexander Lyakas * The device is suitable for reading (InSync), 2611d57368afSAlexander Lyakas * but has bad block(s) here. Let's try to correct them, 2612d57368afSAlexander Lyakas * if we are doing resync or repair. Otherwise, leave 2613d57368afSAlexander Lyakas * this device alone for this sync request. 2614d57368afSAlexander Lyakas */ 2615d57368afSAlexander Lyakas bio->bi_rw = WRITE; 2616d57368afSAlexander Lyakas bio->bi_end_io = end_sync_write; 2617d57368afSAlexander Lyakas write_targets++; 26183e198f78SNeilBrown } 261906f60385SNeilBrown } 262006f60385SNeilBrown if (bio->bi_end_io) { 26213e198f78SNeilBrown atomic_inc(&rdev->nr_pending); 26224f024f37SKent Overstreet bio->bi_iter.bi_sector = sector_nr + rdev->data_offset; 26233e198f78SNeilBrown bio->bi_bdev = rdev->bdev; 26241da177e4SLinus Torvalds bio->bi_private = r1_bio; 26251da177e4SLinus Torvalds } 262606f60385SNeilBrown } 26273e198f78SNeilBrown rcu_read_unlock(); 26283e198f78SNeilBrown if (disk < 0) 26293e198f78SNeilBrown disk = wonly; 26303e198f78SNeilBrown r1_bio->read_disk = disk; 2631191ea9b2SNeilBrown 263206f60385SNeilBrown if (read_targets == 0 && min_bad > 0) { 263306f60385SNeilBrown /* These sectors are bad on all InSync devices, so we 263406f60385SNeilBrown * need to mark them bad on all write targets 263506f60385SNeilBrown */ 263606f60385SNeilBrown int ok = 1; 26378f19ccb2SNeilBrown for (i = 0 ; i < conf->raid_disks * 2 ; i++) 263806f60385SNeilBrown if (r1_bio->bios[i]->bi_end_io == end_sync_write) { 2639a42f9d83Smajianpeng struct md_rdev *rdev = conf->mirrors[i].rdev; 264006f60385SNeilBrown ok = rdev_set_badblocks(rdev, sector_nr, 264106f60385SNeilBrown min_bad, 0 264206f60385SNeilBrown ) && ok; 264306f60385SNeilBrown } 264406f60385SNeilBrown set_bit(MD_CHANGE_DEVS, &mddev->flags); 264506f60385SNeilBrown *skipped = 1; 264606f60385SNeilBrown put_buf(r1_bio); 264706f60385SNeilBrown 264806f60385SNeilBrown if (!ok) { 264906f60385SNeilBrown /* Cannot record the badblocks, so need to 265006f60385SNeilBrown * abort the resync. 265106f60385SNeilBrown * If there are multiple read targets, could just 265206f60385SNeilBrown * fail the really bad ones ??? 265306f60385SNeilBrown */ 265406f60385SNeilBrown conf->recovery_disabled = mddev->recovery_disabled; 265506f60385SNeilBrown set_bit(MD_RECOVERY_INTR, &mddev->recovery); 265606f60385SNeilBrown return 0; 265706f60385SNeilBrown } else 265806f60385SNeilBrown return min_bad; 265906f60385SNeilBrown 266006f60385SNeilBrown } 266106f60385SNeilBrown if (min_bad > 0 && min_bad < good_sectors) { 266206f60385SNeilBrown /* only resync enough to reach the next bad->good 266306f60385SNeilBrown * transition */ 266406f60385SNeilBrown good_sectors = min_bad; 266506f60385SNeilBrown } 266606f60385SNeilBrown 26673e198f78SNeilBrown if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0) 26683e198f78SNeilBrown /* extra read targets are also write targets */ 26693e198f78SNeilBrown write_targets += read_targets-1; 26703e198f78SNeilBrown 26713e198f78SNeilBrown if (write_targets == 0 || read_targets == 0) { 26721da177e4SLinus Torvalds /* There is nowhere to write, so all non-sync 26731da177e4SLinus Torvalds * drives must be failed - so we are finished 26741da177e4SLinus Torvalds */ 2675b7219ccbSNeilBrown sector_t rv; 2676b7219ccbSNeilBrown if (min_bad > 0) 2677b7219ccbSNeilBrown max_sector = sector_nr + min_bad; 2678b7219ccbSNeilBrown rv = max_sector - sector_nr; 267957afd89fSNeilBrown *skipped = 1; 26801da177e4SLinus Torvalds put_buf(r1_bio); 26811da177e4SLinus Torvalds return rv; 26821da177e4SLinus Torvalds } 26831da177e4SLinus Torvalds 2684c6207277SNeilBrown if (max_sector > mddev->resync_max) 2685c6207277SNeilBrown max_sector = mddev->resync_max; /* Don't do IO beyond here */ 268606f60385SNeilBrown if (max_sector > sector_nr + good_sectors) 268706f60385SNeilBrown max_sector = sector_nr + good_sectors; 26881da177e4SLinus Torvalds nr_sectors = 0; 2689289e99e8SNeilBrown sync_blocks = 0; 26901da177e4SLinus Torvalds do { 26911da177e4SLinus Torvalds struct page *page; 26921da177e4SLinus Torvalds int len = PAGE_SIZE; 26931da177e4SLinus Torvalds if (sector_nr + (len>>9) > max_sector) 26941da177e4SLinus Torvalds len = (max_sector - sector_nr) << 9; 26951da177e4SLinus Torvalds if (len == 0) 26961da177e4SLinus Torvalds break; 2697ab7a30c7SNeilBrown if (sync_blocks == 0) { 26986a806c51SNeilBrown if (!bitmap_start_sync(mddev->bitmap, sector_nr, 2699e3b9703eSNeilBrown &sync_blocks, still_degraded) && 2700e5de485fSNeilBrown !conf->fullsync && 2701e5de485fSNeilBrown !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) 2702191ea9b2SNeilBrown break; 27039e77c485SEric Sesterhenn BUG_ON(sync_blocks < (PAGE_SIZE>>9)); 27047571ae88SNeilBrown if ((len >> 9) > sync_blocks) 27056a806c51SNeilBrown len = sync_blocks<<9; 2706ab7a30c7SNeilBrown } 2707191ea9b2SNeilBrown 27088f19ccb2SNeilBrown for (i = 0 ; i < conf->raid_disks * 2; i++) { 27091da177e4SLinus Torvalds bio = r1_bio->bios[i]; 27101da177e4SLinus Torvalds if (bio->bi_end_io) { 2711d11c171eSNeilBrown page = bio->bi_io_vec[bio->bi_vcnt].bv_page; 27121da177e4SLinus Torvalds if (bio_add_page(bio, page, len, 0) == 0) { 27131da177e4SLinus Torvalds /* stop here */ 2714d11c171eSNeilBrown bio->bi_io_vec[bio->bi_vcnt].bv_page = page; 27151da177e4SLinus Torvalds while (i > 0) { 27161da177e4SLinus Torvalds i--; 27171da177e4SLinus Torvalds bio = r1_bio->bios[i]; 27186a806c51SNeilBrown if (bio->bi_end_io==NULL) 27196a806c51SNeilBrown continue; 27201da177e4SLinus Torvalds /* remove last page from this bio */ 27211da177e4SLinus Torvalds bio->bi_vcnt--; 27224f024f37SKent Overstreet bio->bi_iter.bi_size -= len; 27231da177e4SLinus Torvalds bio->bi_flags &= ~(1<< BIO_SEG_VALID); 27241da177e4SLinus Torvalds } 27251da177e4SLinus Torvalds goto bio_full; 27261da177e4SLinus Torvalds } 27271da177e4SLinus Torvalds } 27281da177e4SLinus Torvalds } 27291da177e4SLinus Torvalds nr_sectors += len>>9; 27301da177e4SLinus Torvalds sector_nr += len>>9; 2731191ea9b2SNeilBrown sync_blocks -= (len>>9); 27321da177e4SLinus Torvalds } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES); 27331da177e4SLinus Torvalds bio_full: 27341da177e4SLinus Torvalds r1_bio->sectors = nr_sectors; 27351da177e4SLinus Torvalds 2736d11c171eSNeilBrown /* For a user-requested sync, we read all readable devices and do a 2737d11c171eSNeilBrown * compare 2738d11c171eSNeilBrown */ 2739d11c171eSNeilBrown if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { 2740d11c171eSNeilBrown atomic_set(&r1_bio->remaining, read_targets); 27412d4f4f33SNeilBrown for (i = 0; i < conf->raid_disks * 2 && read_targets; i++) { 2742d11c171eSNeilBrown bio = r1_bio->bios[i]; 2743d11c171eSNeilBrown if (bio->bi_end_io == end_sync_read) { 27442d4f4f33SNeilBrown read_targets--; 2745ddac7c7eSNeilBrown md_sync_acct(bio->bi_bdev, nr_sectors); 27461da177e4SLinus Torvalds generic_make_request(bio); 2747d11c171eSNeilBrown } 2748d11c171eSNeilBrown } 2749d11c171eSNeilBrown } else { 2750d11c171eSNeilBrown atomic_set(&r1_bio->remaining, 1); 2751d11c171eSNeilBrown bio = r1_bio->bios[r1_bio->read_disk]; 2752ddac7c7eSNeilBrown md_sync_acct(bio->bi_bdev, nr_sectors); 2753d11c171eSNeilBrown generic_make_request(bio); 2754d11c171eSNeilBrown 2755d11c171eSNeilBrown } 27561da177e4SLinus Torvalds return nr_sectors; 27571da177e4SLinus Torvalds } 27581da177e4SLinus Torvalds 2759fd01b88cSNeilBrown static sector_t raid1_size(struct mddev *mddev, sector_t sectors, int raid_disks) 276080c3a6ceSDan Williams { 276180c3a6ceSDan Williams if (sectors) 276280c3a6ceSDan Williams return sectors; 276380c3a6ceSDan Williams 276480c3a6ceSDan Williams return mddev->dev_sectors; 276580c3a6ceSDan Williams } 276680c3a6ceSDan Williams 2767e8096360SNeilBrown static struct r1conf *setup_conf(struct mddev *mddev) 27681da177e4SLinus Torvalds { 2769e8096360SNeilBrown struct r1conf *conf; 2770709ae487SNeilBrown int i; 27710eaf822cSJonathan Brassow struct raid1_info *disk; 27723cb03002SNeilBrown struct md_rdev *rdev; 2773709ae487SNeilBrown int err = -ENOMEM; 27741da177e4SLinus Torvalds 2775e8096360SNeilBrown conf = kzalloc(sizeof(struct r1conf), GFP_KERNEL); 27761da177e4SLinus Torvalds if (!conf) 2777709ae487SNeilBrown goto abort; 27781da177e4SLinus Torvalds 27790eaf822cSJonathan Brassow conf->mirrors = kzalloc(sizeof(struct raid1_info) 27808f19ccb2SNeilBrown * mddev->raid_disks * 2, 27811da177e4SLinus Torvalds GFP_KERNEL); 27821da177e4SLinus Torvalds if (!conf->mirrors) 2783709ae487SNeilBrown goto abort; 27841da177e4SLinus Torvalds 2785ddaf22abSNeilBrown conf->tmppage = alloc_page(GFP_KERNEL); 2786ddaf22abSNeilBrown if (!conf->tmppage) 2787709ae487SNeilBrown goto abort; 2788ddaf22abSNeilBrown 2789709ae487SNeilBrown conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL); 27901da177e4SLinus Torvalds if (!conf->poolinfo) 2791709ae487SNeilBrown goto abort; 27928f19ccb2SNeilBrown conf->poolinfo->raid_disks = mddev->raid_disks * 2; 27931da177e4SLinus Torvalds conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc, 27941da177e4SLinus Torvalds r1bio_pool_free, 27951da177e4SLinus Torvalds conf->poolinfo); 27961da177e4SLinus Torvalds if (!conf->r1bio_pool) 2797709ae487SNeilBrown goto abort; 2798709ae487SNeilBrown 2799ed9bfdf1SNeilBrown conf->poolinfo->mddev = mddev; 28001da177e4SLinus Torvalds 2801c19d5798SNeilBrown err = -EINVAL; 2802e7e72bf6SNeil Brown spin_lock_init(&conf->device_lock); 2803dafb20faSNeilBrown rdev_for_each(rdev, mddev) { 2804aba336bdSNeilBrown struct request_queue *q; 2805709ae487SNeilBrown int disk_idx = rdev->raid_disk; 28061da177e4SLinus Torvalds if (disk_idx >= mddev->raid_disks 28071da177e4SLinus Torvalds || disk_idx < 0) 28081da177e4SLinus Torvalds continue; 2809c19d5798SNeilBrown if (test_bit(Replacement, &rdev->flags)) 281002b898f2SNeilBrown disk = conf->mirrors + mddev->raid_disks + disk_idx; 2811c19d5798SNeilBrown else 28121da177e4SLinus Torvalds disk = conf->mirrors + disk_idx; 28131da177e4SLinus Torvalds 2814c19d5798SNeilBrown if (disk->rdev) 2815c19d5798SNeilBrown goto abort; 28161da177e4SLinus Torvalds disk->rdev = rdev; 2817aba336bdSNeilBrown q = bdev_get_queue(rdev->bdev); 2818aba336bdSNeilBrown if (q->merge_bvec_fn) 2819aba336bdSNeilBrown mddev->merge_check_needed = 1; 28201da177e4SLinus Torvalds 28211da177e4SLinus Torvalds disk->head_position = 0; 282212cee5a8SShaohua Li disk->seq_start = MaxSector; 28231da177e4SLinus Torvalds } 28241da177e4SLinus Torvalds conf->raid_disks = mddev->raid_disks; 28251da177e4SLinus Torvalds conf->mddev = mddev; 28261da177e4SLinus Torvalds INIT_LIST_HEAD(&conf->retry_list); 28271da177e4SLinus Torvalds 28281da177e4SLinus Torvalds spin_lock_init(&conf->resync_lock); 282917999be4SNeilBrown init_waitqueue_head(&conf->wait_barrier); 28301da177e4SLinus Torvalds 2831191ea9b2SNeilBrown bio_list_init(&conf->pending_bio_list); 283234db0cd6SNeilBrown conf->pending_count = 0; 2833d890fa2bSNeilBrown conf->recovery_disabled = mddev->recovery_disabled - 1; 2834191ea9b2SNeilBrown 283579ef3a8aSmajianpeng conf->start_next_window = MaxSector; 283679ef3a8aSmajianpeng conf->current_window_requests = conf->next_window_requests = 0; 283779ef3a8aSmajianpeng 2838c19d5798SNeilBrown err = -EIO; 28398f19ccb2SNeilBrown for (i = 0; i < conf->raid_disks * 2; i++) { 28401da177e4SLinus Torvalds 28411da177e4SLinus Torvalds disk = conf->mirrors + i; 28421da177e4SLinus Torvalds 2843c19d5798SNeilBrown if (i < conf->raid_disks && 2844c19d5798SNeilBrown disk[conf->raid_disks].rdev) { 2845c19d5798SNeilBrown /* This slot has a replacement. */ 2846c19d5798SNeilBrown if (!disk->rdev) { 2847c19d5798SNeilBrown /* No original, just make the replacement 2848c19d5798SNeilBrown * a recovering spare 2849c19d5798SNeilBrown */ 2850c19d5798SNeilBrown disk->rdev = 2851c19d5798SNeilBrown disk[conf->raid_disks].rdev; 2852c19d5798SNeilBrown disk[conf->raid_disks].rdev = NULL; 2853c19d5798SNeilBrown } else if (!test_bit(In_sync, &disk->rdev->flags)) 2854c19d5798SNeilBrown /* Original is not in_sync - bad */ 2855c19d5798SNeilBrown goto abort; 2856c19d5798SNeilBrown } 2857c19d5798SNeilBrown 28585fd6c1dcSNeilBrown if (!disk->rdev || 28595fd6c1dcSNeilBrown !test_bit(In_sync, &disk->rdev->flags)) { 28601da177e4SLinus Torvalds disk->head_position = 0; 28614f0a5e01SJonathan Brassow if (disk->rdev && 28624f0a5e01SJonathan Brassow (disk->rdev->saved_raid_disk < 0)) 286317571284SNeilBrown conf->fullsync = 1; 2864be4d3280SShaohua Li } 28651da177e4SLinus Torvalds } 2866709ae487SNeilBrown 2867709ae487SNeilBrown err = -ENOMEM; 28680232605dSNeilBrown conf->thread = md_register_thread(raid1d, mddev, "raid1"); 2869709ae487SNeilBrown if (!conf->thread) { 28701da177e4SLinus Torvalds printk(KERN_ERR 28719dd1e2faSNeilBrown "md/raid1:%s: couldn't allocate thread\n", 28721da177e4SLinus Torvalds mdname(mddev)); 2873709ae487SNeilBrown goto abort; 28741da177e4SLinus Torvalds } 2875191ea9b2SNeilBrown 2876709ae487SNeilBrown return conf; 2877709ae487SNeilBrown 2878709ae487SNeilBrown abort: 2879709ae487SNeilBrown if (conf) { 2880709ae487SNeilBrown if (conf->r1bio_pool) 2881709ae487SNeilBrown mempool_destroy(conf->r1bio_pool); 2882709ae487SNeilBrown kfree(conf->mirrors); 2883709ae487SNeilBrown safe_put_page(conf->tmppage); 2884709ae487SNeilBrown kfree(conf->poolinfo); 2885709ae487SNeilBrown kfree(conf); 2886709ae487SNeilBrown } 2887709ae487SNeilBrown return ERR_PTR(err); 2888709ae487SNeilBrown } 2889709ae487SNeilBrown 28905220ea1eSmajianpeng static int stop(struct mddev *mddev); 2891fd01b88cSNeilBrown static int run(struct mddev *mddev) 2892709ae487SNeilBrown { 2893e8096360SNeilBrown struct r1conf *conf; 2894709ae487SNeilBrown int i; 28953cb03002SNeilBrown struct md_rdev *rdev; 28965220ea1eSmajianpeng int ret; 28972ff8cc2cSShaohua Li bool discard_supported = false; 2898709ae487SNeilBrown 2899709ae487SNeilBrown if (mddev->level != 1) { 29009dd1e2faSNeilBrown printk(KERN_ERR "md/raid1:%s: raid level not set to mirroring (%d)\n", 2901709ae487SNeilBrown mdname(mddev), mddev->level); 2902709ae487SNeilBrown return -EIO; 2903709ae487SNeilBrown } 2904709ae487SNeilBrown if (mddev->reshape_position != MaxSector) { 29059dd1e2faSNeilBrown printk(KERN_ERR "md/raid1:%s: reshape_position set but not supported\n", 2906709ae487SNeilBrown mdname(mddev)); 2907709ae487SNeilBrown return -EIO; 2908709ae487SNeilBrown } 2909709ae487SNeilBrown /* 2910709ae487SNeilBrown * copy the already verified devices into our private RAID1 2911709ae487SNeilBrown * bookkeeping area. [whatever we allocate in run(), 2912709ae487SNeilBrown * should be freed in stop()] 2913709ae487SNeilBrown */ 2914709ae487SNeilBrown if (mddev->private == NULL) 2915709ae487SNeilBrown conf = setup_conf(mddev); 2916709ae487SNeilBrown else 2917709ae487SNeilBrown conf = mddev->private; 2918709ae487SNeilBrown 2919709ae487SNeilBrown if (IS_ERR(conf)) 2920709ae487SNeilBrown return PTR_ERR(conf); 2921709ae487SNeilBrown 2922c8dc9c65SJoe Lawrence if (mddev->queue) 29235026d7a9SH. Peter Anvin blk_queue_max_write_same_sectors(mddev->queue, 0); 29245026d7a9SH. Peter Anvin 2925dafb20faSNeilBrown rdev_for_each(rdev, mddev) { 29261ed7242eSJonathan Brassow if (!mddev->gendisk) 29271ed7242eSJonathan Brassow continue; 2928709ae487SNeilBrown disk_stack_limits(mddev->gendisk, rdev->bdev, 2929709ae487SNeilBrown rdev->data_offset << 9); 29302ff8cc2cSShaohua Li if (blk_queue_discard(bdev_get_queue(rdev->bdev))) 29312ff8cc2cSShaohua Li discard_supported = true; 2932709ae487SNeilBrown } 2933709ae487SNeilBrown 2934709ae487SNeilBrown mddev->degraded = 0; 2935709ae487SNeilBrown for (i=0; i < conf->raid_disks; i++) 2936709ae487SNeilBrown if (conf->mirrors[i].rdev == NULL || 2937709ae487SNeilBrown !test_bit(In_sync, &conf->mirrors[i].rdev->flags) || 2938709ae487SNeilBrown test_bit(Faulty, &conf->mirrors[i].rdev->flags)) 2939709ae487SNeilBrown mddev->degraded++; 2940709ae487SNeilBrown 2941709ae487SNeilBrown if (conf->raid_disks - mddev->degraded == 1) 2942709ae487SNeilBrown mddev->recovery_cp = MaxSector; 2943709ae487SNeilBrown 29448c6ac868SAndre Noll if (mddev->recovery_cp != MaxSector) 29459dd1e2faSNeilBrown printk(KERN_NOTICE "md/raid1:%s: not clean" 29468c6ac868SAndre Noll " -- starting background reconstruction\n", 29478c6ac868SAndre Noll mdname(mddev)); 29481da177e4SLinus Torvalds printk(KERN_INFO 29499dd1e2faSNeilBrown "md/raid1:%s: active with %d out of %d mirrors\n", 29501da177e4SLinus Torvalds mdname(mddev), mddev->raid_disks - mddev->degraded, 29511da177e4SLinus Torvalds mddev->raid_disks); 2952709ae487SNeilBrown 29531da177e4SLinus Torvalds /* 29541da177e4SLinus Torvalds * Ok, everything is just fine now 29551da177e4SLinus Torvalds */ 2956709ae487SNeilBrown mddev->thread = conf->thread; 2957709ae487SNeilBrown conf->thread = NULL; 2958709ae487SNeilBrown mddev->private = conf; 2959709ae487SNeilBrown 29601f403624SDan Williams md_set_array_sectors(mddev, raid1_size(mddev, 0, 0)); 29611da177e4SLinus Torvalds 29621ed7242eSJonathan Brassow if (mddev->queue) { 29630d129228SNeilBrown mddev->queue->backing_dev_info.congested_fn = raid1_congested; 29640d129228SNeilBrown mddev->queue->backing_dev_info.congested_data = mddev; 29656b740b8dSNeilBrown blk_queue_merge_bvec(mddev->queue, raid1_mergeable_bvec); 29662ff8cc2cSShaohua Li 29672ff8cc2cSShaohua Li if (discard_supported) 29682ff8cc2cSShaohua Li queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, 29692ff8cc2cSShaohua Li mddev->queue); 29702ff8cc2cSShaohua Li else 29712ff8cc2cSShaohua Li queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, 29722ff8cc2cSShaohua Li mddev->queue); 29731ed7242eSJonathan Brassow } 29745220ea1eSmajianpeng 29755220ea1eSmajianpeng ret = md_integrity_register(mddev); 29765220ea1eSmajianpeng if (ret) 29775220ea1eSmajianpeng stop(mddev); 29785220ea1eSmajianpeng return ret; 29791da177e4SLinus Torvalds } 29801da177e4SLinus Torvalds 2981fd01b88cSNeilBrown static int stop(struct mddev *mddev) 29821da177e4SLinus Torvalds { 2983e8096360SNeilBrown struct r1conf *conf = mddev->private; 29844b6d287fSNeilBrown struct bitmap *bitmap = mddev->bitmap; 29854b6d287fSNeilBrown 29864b6d287fSNeilBrown /* wait for behind writes to complete */ 2987e555190dSNeilBrown if (bitmap && atomic_read(&bitmap->behind_writes) > 0) { 29889dd1e2faSNeilBrown printk(KERN_INFO "md/raid1:%s: behind writes in progress - waiting to stop.\n", 29899dd1e2faSNeilBrown mdname(mddev)); 29904b6d287fSNeilBrown /* need to kick something here to make sure I/O goes? */ 2991e555190dSNeilBrown wait_event(bitmap->behind_wait, 2992e555190dSNeilBrown atomic_read(&bitmap->behind_writes) == 0); 29934b6d287fSNeilBrown } 29941da177e4SLinus Torvalds 299507169fd4Smajianpeng freeze_array(conf, 0); 299607169fd4Smajianpeng unfreeze_array(conf); 2997409c57f3SNeilBrown 299801f96c0aSNeilBrown md_unregister_thread(&mddev->thread); 29991da177e4SLinus Torvalds if (conf->r1bio_pool) 30001da177e4SLinus Torvalds mempool_destroy(conf->r1bio_pool); 30011da177e4SLinus Torvalds kfree(conf->mirrors); 30020fea7ed8SHirokazu Takahashi safe_put_page(conf->tmppage); 30031da177e4SLinus Torvalds kfree(conf->poolinfo); 30041da177e4SLinus Torvalds kfree(conf); 30051da177e4SLinus Torvalds mddev->private = NULL; 30061da177e4SLinus Torvalds return 0; 30071da177e4SLinus Torvalds } 30081da177e4SLinus Torvalds 3009fd01b88cSNeilBrown static int raid1_resize(struct mddev *mddev, sector_t sectors) 30101da177e4SLinus Torvalds { 30111da177e4SLinus Torvalds /* no resync is happening, and there is enough space 30121da177e4SLinus Torvalds * on all devices, so we can resize. 30131da177e4SLinus Torvalds * We need to make sure resync covers any new space. 30141da177e4SLinus Torvalds * If the array is shrinking we should possibly wait until 30151da177e4SLinus Torvalds * any io in the removed space completes, but it hardly seems 30161da177e4SLinus Torvalds * worth it. 30171da177e4SLinus Torvalds */ 3018a4a6125aSNeilBrown sector_t newsize = raid1_size(mddev, sectors, 0); 3019a4a6125aSNeilBrown if (mddev->external_size && 3020a4a6125aSNeilBrown mddev->array_sectors > newsize) 3021b522adcdSDan Williams return -EINVAL; 3022a4a6125aSNeilBrown if (mddev->bitmap) { 3023a4a6125aSNeilBrown int ret = bitmap_resize(mddev->bitmap, newsize, 0, 0); 3024a4a6125aSNeilBrown if (ret) 3025a4a6125aSNeilBrown return ret; 3026a4a6125aSNeilBrown } 3027a4a6125aSNeilBrown md_set_array_sectors(mddev, newsize); 3028f233ea5cSAndre Noll set_capacity(mddev->gendisk, mddev->array_sectors); 3029449aad3eSNeilBrown revalidate_disk(mddev->gendisk); 3030b522adcdSDan Williams if (sectors > mddev->dev_sectors && 3031b098636cSNeilBrown mddev->recovery_cp > mddev->dev_sectors) { 303258c0fed4SAndre Noll mddev->recovery_cp = mddev->dev_sectors; 30331da177e4SLinus Torvalds set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 30341da177e4SLinus Torvalds } 3035b522adcdSDan Williams mddev->dev_sectors = sectors; 30364b5c7ae8SNeilBrown mddev->resync_max_sectors = sectors; 30371da177e4SLinus Torvalds return 0; 30381da177e4SLinus Torvalds } 30391da177e4SLinus Torvalds 3040fd01b88cSNeilBrown static int raid1_reshape(struct mddev *mddev) 30411da177e4SLinus Torvalds { 30421da177e4SLinus Torvalds /* We need to: 30431da177e4SLinus Torvalds * 1/ resize the r1bio_pool 30441da177e4SLinus Torvalds * 2/ resize conf->mirrors 30451da177e4SLinus Torvalds * 30461da177e4SLinus Torvalds * We allocate a new r1bio_pool if we can. 30471da177e4SLinus Torvalds * Then raise a device barrier and wait until all IO stops. 30481da177e4SLinus Torvalds * Then resize conf->mirrors and swap in the new r1bio pool. 30496ea9c07cSNeilBrown * 30506ea9c07cSNeilBrown * At the same time, we "pack" the devices so that all the missing 30516ea9c07cSNeilBrown * devices have the higher raid_disk numbers. 30521da177e4SLinus Torvalds */ 30531da177e4SLinus Torvalds mempool_t *newpool, *oldpool; 30541da177e4SLinus Torvalds struct pool_info *newpoolinfo; 30550eaf822cSJonathan Brassow struct raid1_info *newmirrors; 3056e8096360SNeilBrown struct r1conf *conf = mddev->private; 305763c70c4fSNeilBrown int cnt, raid_disks; 3058c04be0aaSNeilBrown unsigned long flags; 3059b5470dc5SDan Williams int d, d2, err; 30601da177e4SLinus Torvalds 306163c70c4fSNeilBrown /* Cannot change chunk_size, layout, or level */ 3062664e7c41SAndre Noll if (mddev->chunk_sectors != mddev->new_chunk_sectors || 306363c70c4fSNeilBrown mddev->layout != mddev->new_layout || 306463c70c4fSNeilBrown mddev->level != mddev->new_level) { 3065664e7c41SAndre Noll mddev->new_chunk_sectors = mddev->chunk_sectors; 306663c70c4fSNeilBrown mddev->new_layout = mddev->layout; 306763c70c4fSNeilBrown mddev->new_level = mddev->level; 306863c70c4fSNeilBrown return -EINVAL; 306963c70c4fSNeilBrown } 307063c70c4fSNeilBrown 3071b5470dc5SDan Williams err = md_allow_write(mddev); 3072b5470dc5SDan Williams if (err) 3073b5470dc5SDan Williams return err; 30742a2275d6SNeilBrown 307563c70c4fSNeilBrown raid_disks = mddev->raid_disks + mddev->delta_disks; 307663c70c4fSNeilBrown 30776ea9c07cSNeilBrown if (raid_disks < conf->raid_disks) { 30786ea9c07cSNeilBrown cnt=0; 30796ea9c07cSNeilBrown for (d= 0; d < conf->raid_disks; d++) 30801da177e4SLinus Torvalds if (conf->mirrors[d].rdev) 30816ea9c07cSNeilBrown cnt++; 30826ea9c07cSNeilBrown if (cnt > raid_disks) 30831da177e4SLinus Torvalds return -EBUSY; 30846ea9c07cSNeilBrown } 30851da177e4SLinus Torvalds 30861da177e4SLinus Torvalds newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL); 30871da177e4SLinus Torvalds if (!newpoolinfo) 30881da177e4SLinus Torvalds return -ENOMEM; 30891da177e4SLinus Torvalds newpoolinfo->mddev = mddev; 30908f19ccb2SNeilBrown newpoolinfo->raid_disks = raid_disks * 2; 30911da177e4SLinus Torvalds 30921da177e4SLinus Torvalds newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc, 30931da177e4SLinus Torvalds r1bio_pool_free, newpoolinfo); 30941da177e4SLinus Torvalds if (!newpool) { 30951da177e4SLinus Torvalds kfree(newpoolinfo); 30961da177e4SLinus Torvalds return -ENOMEM; 30971da177e4SLinus Torvalds } 30980eaf822cSJonathan Brassow newmirrors = kzalloc(sizeof(struct raid1_info) * raid_disks * 2, 30998f19ccb2SNeilBrown GFP_KERNEL); 31001da177e4SLinus Torvalds if (!newmirrors) { 31011da177e4SLinus Torvalds kfree(newpoolinfo); 31021da177e4SLinus Torvalds mempool_destroy(newpool); 31031da177e4SLinus Torvalds return -ENOMEM; 31041da177e4SLinus Torvalds } 31051da177e4SLinus Torvalds 3106e2d59925SNeilBrown freeze_array(conf, 0); 31071da177e4SLinus Torvalds 31081da177e4SLinus Torvalds /* ok, everything is stopped */ 31091da177e4SLinus Torvalds oldpool = conf->r1bio_pool; 31101da177e4SLinus Torvalds conf->r1bio_pool = newpool; 31116ea9c07cSNeilBrown 3112a88aa786SNeilBrown for (d = d2 = 0; d < conf->raid_disks; d++) { 31133cb03002SNeilBrown struct md_rdev *rdev = conf->mirrors[d].rdev; 3114a88aa786SNeilBrown if (rdev && rdev->raid_disk != d2) { 311536fad858SNamhyung Kim sysfs_unlink_rdev(mddev, rdev); 3116a88aa786SNeilBrown rdev->raid_disk = d2; 311736fad858SNamhyung Kim sysfs_unlink_rdev(mddev, rdev); 311836fad858SNamhyung Kim if (sysfs_link_rdev(mddev, rdev)) 3119a88aa786SNeilBrown printk(KERN_WARNING 312036fad858SNamhyung Kim "md/raid1:%s: cannot register rd%d\n", 312136fad858SNamhyung Kim mdname(mddev), rdev->raid_disk); 3122a88aa786SNeilBrown } 3123a88aa786SNeilBrown if (rdev) 3124a88aa786SNeilBrown newmirrors[d2++].rdev = rdev; 31256ea9c07cSNeilBrown } 31261da177e4SLinus Torvalds kfree(conf->mirrors); 31271da177e4SLinus Torvalds conf->mirrors = newmirrors; 31281da177e4SLinus Torvalds kfree(conf->poolinfo); 31291da177e4SLinus Torvalds conf->poolinfo = newpoolinfo; 31301da177e4SLinus Torvalds 3131c04be0aaSNeilBrown spin_lock_irqsave(&conf->device_lock, flags); 31321da177e4SLinus Torvalds mddev->degraded += (raid_disks - conf->raid_disks); 3133c04be0aaSNeilBrown spin_unlock_irqrestore(&conf->device_lock, flags); 31341da177e4SLinus Torvalds conf->raid_disks = mddev->raid_disks = raid_disks; 313563c70c4fSNeilBrown mddev->delta_disks = 0; 31361da177e4SLinus Torvalds 3137e2d59925SNeilBrown unfreeze_array(conf); 31381da177e4SLinus Torvalds 31391da177e4SLinus Torvalds set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 31401da177e4SLinus Torvalds md_wakeup_thread(mddev->thread); 31411da177e4SLinus Torvalds 31421da177e4SLinus Torvalds mempool_destroy(oldpool); 31431da177e4SLinus Torvalds return 0; 31441da177e4SLinus Torvalds } 31451da177e4SLinus Torvalds 3146fd01b88cSNeilBrown static void raid1_quiesce(struct mddev *mddev, int state) 314736fa3063SNeilBrown { 3148e8096360SNeilBrown struct r1conf *conf = mddev->private; 314936fa3063SNeilBrown 315036fa3063SNeilBrown switch(state) { 31516eef4b21SNeilBrown case 2: /* wake for suspend */ 31526eef4b21SNeilBrown wake_up(&conf->wait_barrier); 31536eef4b21SNeilBrown break; 31549e6603daSNeilBrown case 1: 315507169fd4Smajianpeng freeze_array(conf, 0); 315636fa3063SNeilBrown break; 31579e6603daSNeilBrown case 0: 315807169fd4Smajianpeng unfreeze_array(conf); 315936fa3063SNeilBrown break; 316036fa3063SNeilBrown } 316136fa3063SNeilBrown } 316236fa3063SNeilBrown 3163fd01b88cSNeilBrown static void *raid1_takeover(struct mddev *mddev) 3164709ae487SNeilBrown { 3165709ae487SNeilBrown /* raid1 can take over: 3166709ae487SNeilBrown * raid5 with 2 devices, any layout or chunk size 3167709ae487SNeilBrown */ 3168709ae487SNeilBrown if (mddev->level == 5 && mddev->raid_disks == 2) { 3169e8096360SNeilBrown struct r1conf *conf; 3170709ae487SNeilBrown mddev->new_level = 1; 3171709ae487SNeilBrown mddev->new_layout = 0; 3172709ae487SNeilBrown mddev->new_chunk_sectors = 0; 3173709ae487SNeilBrown conf = setup_conf(mddev); 3174709ae487SNeilBrown if (!IS_ERR(conf)) 317507169fd4Smajianpeng /* Array must appear to be quiesced */ 317607169fd4Smajianpeng conf->array_frozen = 1; 3177709ae487SNeilBrown return conf; 3178709ae487SNeilBrown } 3179709ae487SNeilBrown return ERR_PTR(-EINVAL); 3180709ae487SNeilBrown } 31811da177e4SLinus Torvalds 318284fc4b56SNeilBrown static struct md_personality raid1_personality = 31831da177e4SLinus Torvalds { 31841da177e4SLinus Torvalds .name = "raid1", 31852604b703SNeilBrown .level = 1, 31861da177e4SLinus Torvalds .owner = THIS_MODULE, 31871da177e4SLinus Torvalds .make_request = make_request, 31881da177e4SLinus Torvalds .run = run, 31891da177e4SLinus Torvalds .stop = stop, 31901da177e4SLinus Torvalds .status = status, 31911da177e4SLinus Torvalds .error_handler = error, 31921da177e4SLinus Torvalds .hot_add_disk = raid1_add_disk, 31931da177e4SLinus Torvalds .hot_remove_disk= raid1_remove_disk, 31941da177e4SLinus Torvalds .spare_active = raid1_spare_active, 31951da177e4SLinus Torvalds .sync_request = sync_request, 31961da177e4SLinus Torvalds .resize = raid1_resize, 319780c3a6ceSDan Williams .size = raid1_size, 319863c70c4fSNeilBrown .check_reshape = raid1_reshape, 319936fa3063SNeilBrown .quiesce = raid1_quiesce, 3200709ae487SNeilBrown .takeover = raid1_takeover, 32011da177e4SLinus Torvalds }; 32021da177e4SLinus Torvalds 32031da177e4SLinus Torvalds static int __init raid_init(void) 32041da177e4SLinus Torvalds { 32052604b703SNeilBrown return register_md_personality(&raid1_personality); 32061da177e4SLinus Torvalds } 32071da177e4SLinus Torvalds 32081da177e4SLinus Torvalds static void raid_exit(void) 32091da177e4SLinus Torvalds { 32102604b703SNeilBrown unregister_md_personality(&raid1_personality); 32111da177e4SLinus Torvalds } 32121da177e4SLinus Torvalds 32131da177e4SLinus Torvalds module_init(raid_init); 32141da177e4SLinus Torvalds module_exit(raid_exit); 32151da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 32160efb9e61SNeilBrown MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD"); 32171da177e4SLinus Torvalds MODULE_ALIAS("md-personality-3"); /* RAID1 */ 3218d9d166c2SNeilBrown MODULE_ALIAS("md-raid1"); 32192604b703SNeilBrown MODULE_ALIAS("md-level-1"); 322034db0cd6SNeilBrown 322134db0cd6SNeilBrown module_param(max_queued_requests, int, S_IRUGO|S_IWUSR); 3222