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 * This routine returns the disk from which the requested read should 4991da177e4SLinus Torvalds * be done. There is a per-array 'next expected sequential IO' sector 5001da177e4SLinus Torvalds * number - if this matches on the next IO then we use the last disk. 5011da177e4SLinus Torvalds * There is also a per-disk 'last know head position' sector that is 5021da177e4SLinus Torvalds * maintained from IRQ contexts, both the normal and the resync IO 5031da177e4SLinus Torvalds * completion handlers update this position correctly. If there is no 5041da177e4SLinus Torvalds * perfect sequential match then we pick the disk whose head is closest. 5051da177e4SLinus Torvalds * 5061da177e4SLinus Torvalds * If there are 2 mirrors in the same 2 devices, performance degrades 5071da177e4SLinus Torvalds * because position is mirror, not device based. 5081da177e4SLinus Torvalds * 5091da177e4SLinus Torvalds * The rdev for the device selected will have nr_pending incremented. 5101da177e4SLinus Torvalds */ 511e8096360SNeilBrown static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sectors) 5121da177e4SLinus Torvalds { 513af3a2cd6SNeilBrown const sector_t this_sector = r1_bio->sector; 514d2eb35acSNeilBrown int sectors; 515d2eb35acSNeilBrown int best_good_sectors; 5169dedf603SShaohua Li int best_disk, best_dist_disk, best_pending_disk; 5179dedf603SShaohua Li int has_nonrot_disk; 518be4d3280SShaohua Li int disk; 51976073054SNeilBrown sector_t best_dist; 5209dedf603SShaohua Li unsigned int min_pending; 5213cb03002SNeilBrown struct md_rdev *rdev; 522f3ac8bf7SNeilBrown int choose_first; 52312cee5a8SShaohua Li int choose_next_idle; 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds rcu_read_lock(); 5261da177e4SLinus Torvalds /* 5278ddf9efeSNeilBrown * Check if we can balance. We can balance on the whole 5281da177e4SLinus Torvalds * device if no resync is going on, or below the resync window. 5291da177e4SLinus Torvalds * We take the first readable disk when above the resync window. 5301da177e4SLinus Torvalds */ 5311da177e4SLinus Torvalds retry: 532d2eb35acSNeilBrown sectors = r1_bio->sectors; 53376073054SNeilBrown best_disk = -1; 5349dedf603SShaohua Li best_dist_disk = -1; 53576073054SNeilBrown best_dist = MaxSector; 5369dedf603SShaohua Li best_pending_disk = -1; 5379dedf603SShaohua Li min_pending = UINT_MAX; 538d2eb35acSNeilBrown best_good_sectors = 0; 5399dedf603SShaohua Li has_nonrot_disk = 0; 54012cee5a8SShaohua Li choose_next_idle = 0; 541d2eb35acSNeilBrown 542c6d119cfSNeilBrown choose_first = (conf->mddev->recovery_cp < this_sector + sectors); 5431da177e4SLinus Torvalds 544be4d3280SShaohua Li for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) { 54576073054SNeilBrown sector_t dist; 546d2eb35acSNeilBrown sector_t first_bad; 547d2eb35acSNeilBrown int bad_sectors; 5489dedf603SShaohua Li unsigned int pending; 54912cee5a8SShaohua Li bool nonrot; 550d2eb35acSNeilBrown 551f3ac8bf7SNeilBrown rdev = rcu_dereference(conf->mirrors[disk].rdev); 552f3ac8bf7SNeilBrown if (r1_bio->bios[disk] == IO_BLOCKED 553f3ac8bf7SNeilBrown || rdev == NULL 5546b740b8dSNeilBrown || test_bit(Unmerged, &rdev->flags) 55576073054SNeilBrown || test_bit(Faulty, &rdev->flags)) 556f3ac8bf7SNeilBrown continue; 55776073054SNeilBrown if (!test_bit(In_sync, &rdev->flags) && 55876073054SNeilBrown rdev->recovery_offset < this_sector + sectors) 55976073054SNeilBrown continue; 56076073054SNeilBrown if (test_bit(WriteMostly, &rdev->flags)) { 56176073054SNeilBrown /* Don't balance among write-mostly, just 56276073054SNeilBrown * use the first as a last resort */ 563307729c8SNeilBrown if (best_disk < 0) { 564307729c8SNeilBrown if (is_badblock(rdev, this_sector, sectors, 565307729c8SNeilBrown &first_bad, &bad_sectors)) { 566307729c8SNeilBrown if (first_bad < this_sector) 567307729c8SNeilBrown /* Cannot use this */ 568307729c8SNeilBrown continue; 569307729c8SNeilBrown best_good_sectors = first_bad - this_sector; 570307729c8SNeilBrown } else 571307729c8SNeilBrown best_good_sectors = sectors; 57276073054SNeilBrown best_disk = disk; 573307729c8SNeilBrown } 57476073054SNeilBrown continue; 5758ddf9efeSNeilBrown } 57676073054SNeilBrown /* This is a reasonable device to use. It might 57776073054SNeilBrown * even be best. 5781da177e4SLinus Torvalds */ 579d2eb35acSNeilBrown if (is_badblock(rdev, this_sector, sectors, 580d2eb35acSNeilBrown &first_bad, &bad_sectors)) { 581d2eb35acSNeilBrown if (best_dist < MaxSector) 582d2eb35acSNeilBrown /* already have a better device */ 583d2eb35acSNeilBrown continue; 584d2eb35acSNeilBrown if (first_bad <= this_sector) { 585d2eb35acSNeilBrown /* cannot read here. If this is the 'primary' 586d2eb35acSNeilBrown * device, then we must not read beyond 587d2eb35acSNeilBrown * bad_sectors from another device.. 588d2eb35acSNeilBrown */ 589d2eb35acSNeilBrown bad_sectors -= (this_sector - first_bad); 590d2eb35acSNeilBrown if (choose_first && sectors > bad_sectors) 591d2eb35acSNeilBrown sectors = bad_sectors; 592d2eb35acSNeilBrown if (best_good_sectors > sectors) 593d2eb35acSNeilBrown best_good_sectors = sectors; 594d2eb35acSNeilBrown 595d2eb35acSNeilBrown } else { 596d2eb35acSNeilBrown sector_t good_sectors = first_bad - this_sector; 597d2eb35acSNeilBrown if (good_sectors > best_good_sectors) { 598d2eb35acSNeilBrown best_good_sectors = good_sectors; 599d2eb35acSNeilBrown best_disk = disk; 600d2eb35acSNeilBrown } 601d2eb35acSNeilBrown if (choose_first) 602d2eb35acSNeilBrown break; 603d2eb35acSNeilBrown } 604d2eb35acSNeilBrown continue; 605d2eb35acSNeilBrown } else 606d2eb35acSNeilBrown best_good_sectors = sectors; 607d2eb35acSNeilBrown 60812cee5a8SShaohua Li nonrot = blk_queue_nonrot(bdev_get_queue(rdev->bdev)); 60912cee5a8SShaohua Li has_nonrot_disk |= nonrot; 6109dedf603SShaohua Li pending = atomic_read(&rdev->nr_pending); 61176073054SNeilBrown dist = abs(this_sector - conf->mirrors[disk].head_position); 61212cee5a8SShaohua Li if (choose_first) { 61376073054SNeilBrown best_disk = disk; 6141da177e4SLinus Torvalds break; 6151da177e4SLinus Torvalds } 61612cee5a8SShaohua Li /* Don't change to another disk for sequential reads */ 61712cee5a8SShaohua Li if (conf->mirrors[disk].next_seq_sect == this_sector 61812cee5a8SShaohua Li || dist == 0) { 61912cee5a8SShaohua Li int opt_iosize = bdev_io_opt(rdev->bdev) >> 9; 62012cee5a8SShaohua Li struct raid1_info *mirror = &conf->mirrors[disk]; 62112cee5a8SShaohua Li 62212cee5a8SShaohua Li best_disk = disk; 62312cee5a8SShaohua Li /* 62412cee5a8SShaohua Li * If buffered sequential IO size exceeds optimal 62512cee5a8SShaohua Li * iosize, check if there is idle disk. If yes, choose 62612cee5a8SShaohua Li * the idle disk. read_balance could already choose an 62712cee5a8SShaohua Li * idle disk before noticing it's a sequential IO in 62812cee5a8SShaohua Li * this disk. This doesn't matter because this disk 62912cee5a8SShaohua Li * will idle, next time it will be utilized after the 63012cee5a8SShaohua Li * first disk has IO size exceeds optimal iosize. In 63112cee5a8SShaohua Li * this way, iosize of the first disk will be optimal 63212cee5a8SShaohua Li * iosize at least. iosize of the second disk might be 63312cee5a8SShaohua Li * small, but not a big deal since when the second disk 63412cee5a8SShaohua Li * starts IO, the first disk is likely still busy. 63512cee5a8SShaohua Li */ 63612cee5a8SShaohua Li if (nonrot && opt_iosize > 0 && 63712cee5a8SShaohua Li mirror->seq_start != MaxSector && 63812cee5a8SShaohua Li mirror->next_seq_sect > opt_iosize && 63912cee5a8SShaohua Li mirror->next_seq_sect - opt_iosize >= 64012cee5a8SShaohua Li mirror->seq_start) { 64112cee5a8SShaohua Li choose_next_idle = 1; 64212cee5a8SShaohua Li continue; 64312cee5a8SShaohua Li } 64412cee5a8SShaohua Li break; 64512cee5a8SShaohua Li } 64612cee5a8SShaohua Li /* If device is idle, use it */ 64712cee5a8SShaohua Li if (pending == 0) { 64812cee5a8SShaohua Li best_disk = disk; 64912cee5a8SShaohua Li break; 65012cee5a8SShaohua Li } 65112cee5a8SShaohua Li 65212cee5a8SShaohua Li if (choose_next_idle) 65312cee5a8SShaohua Li continue; 6549dedf603SShaohua Li 6559dedf603SShaohua Li if (min_pending > pending) { 6569dedf603SShaohua Li min_pending = pending; 6579dedf603SShaohua Li best_pending_disk = disk; 6589dedf603SShaohua Li } 6599dedf603SShaohua Li 66076073054SNeilBrown if (dist < best_dist) { 66176073054SNeilBrown best_dist = dist; 6629dedf603SShaohua Li best_dist_disk = disk; 6631da177e4SLinus Torvalds } 664f3ac8bf7SNeilBrown } 6651da177e4SLinus Torvalds 6669dedf603SShaohua Li /* 6679dedf603SShaohua Li * If all disks are rotational, choose the closest disk. If any disk is 6689dedf603SShaohua Li * non-rotational, choose the disk with less pending request even the 6699dedf603SShaohua Li * disk is rotational, which might/might not be optimal for raids with 6709dedf603SShaohua Li * mixed ratation/non-rotational disks depending on workload. 6719dedf603SShaohua Li */ 6729dedf603SShaohua Li if (best_disk == -1) { 6739dedf603SShaohua Li if (has_nonrot_disk) 6749dedf603SShaohua Li best_disk = best_pending_disk; 6759dedf603SShaohua Li else 6769dedf603SShaohua Li best_disk = best_dist_disk; 6779dedf603SShaohua Li } 6789dedf603SShaohua Li 67976073054SNeilBrown if (best_disk >= 0) { 68076073054SNeilBrown rdev = rcu_dereference(conf->mirrors[best_disk].rdev); 6818ddf9efeSNeilBrown if (!rdev) 6828ddf9efeSNeilBrown goto retry; 6838ddf9efeSNeilBrown atomic_inc(&rdev->nr_pending); 68476073054SNeilBrown if (test_bit(Faulty, &rdev->flags)) { 6851da177e4SLinus Torvalds /* cannot risk returning a device that failed 6861da177e4SLinus Torvalds * before we inc'ed nr_pending 6871da177e4SLinus Torvalds */ 68803c902e1SNeilBrown rdev_dec_pending(rdev, conf->mddev); 6891da177e4SLinus Torvalds goto retry; 6901da177e4SLinus Torvalds } 691d2eb35acSNeilBrown sectors = best_good_sectors; 69212cee5a8SShaohua Li 69312cee5a8SShaohua Li if (conf->mirrors[best_disk].next_seq_sect != this_sector) 69412cee5a8SShaohua Li conf->mirrors[best_disk].seq_start = this_sector; 69512cee5a8SShaohua Li 696be4d3280SShaohua Li conf->mirrors[best_disk].next_seq_sect = this_sector + sectors; 6971da177e4SLinus Torvalds } 6981da177e4SLinus Torvalds rcu_read_unlock(); 699d2eb35acSNeilBrown *max_sectors = sectors; 7001da177e4SLinus Torvalds 70176073054SNeilBrown return best_disk; 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds 70464590f45SNeilBrown static int raid1_mergeable_bvec(struct mddev *mddev, 7056b740b8dSNeilBrown struct bvec_merge_data *bvm, 7066b740b8dSNeilBrown struct bio_vec *biovec) 7076b740b8dSNeilBrown { 7086b740b8dSNeilBrown struct r1conf *conf = mddev->private; 7096b740b8dSNeilBrown sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev); 7106b740b8dSNeilBrown int max = biovec->bv_len; 7116b740b8dSNeilBrown 7126b740b8dSNeilBrown if (mddev->merge_check_needed) { 7136b740b8dSNeilBrown int disk; 7146b740b8dSNeilBrown rcu_read_lock(); 7156b740b8dSNeilBrown for (disk = 0; disk < conf->raid_disks * 2; disk++) { 7166b740b8dSNeilBrown struct md_rdev *rdev = rcu_dereference( 7176b740b8dSNeilBrown conf->mirrors[disk].rdev); 7186b740b8dSNeilBrown if (rdev && !test_bit(Faulty, &rdev->flags)) { 7196b740b8dSNeilBrown struct request_queue *q = 7206b740b8dSNeilBrown bdev_get_queue(rdev->bdev); 7216b740b8dSNeilBrown if (q->merge_bvec_fn) { 7226b740b8dSNeilBrown bvm->bi_sector = sector + 7236b740b8dSNeilBrown rdev->data_offset; 7246b740b8dSNeilBrown bvm->bi_bdev = rdev->bdev; 7256b740b8dSNeilBrown max = min(max, q->merge_bvec_fn( 7266b740b8dSNeilBrown q, bvm, biovec)); 7276b740b8dSNeilBrown } 7286b740b8dSNeilBrown } 7296b740b8dSNeilBrown } 7306b740b8dSNeilBrown rcu_read_unlock(); 7316b740b8dSNeilBrown } 7326b740b8dSNeilBrown return max; 7336b740b8dSNeilBrown 7346b740b8dSNeilBrown } 7356b740b8dSNeilBrown 7365c675f83SNeilBrown static int raid1_congested(struct mddev *mddev, int bits) 7370d129228SNeilBrown { 738e8096360SNeilBrown struct r1conf *conf = mddev->private; 7390d129228SNeilBrown int i, ret = 0; 7400d129228SNeilBrown 74134db0cd6SNeilBrown if ((bits & (1 << BDI_async_congested)) && 74234db0cd6SNeilBrown conf->pending_count >= max_queued_requests) 74334db0cd6SNeilBrown return 1; 74434db0cd6SNeilBrown 7450d129228SNeilBrown rcu_read_lock(); 746f53e29fcSNeilBrown for (i = 0; i < conf->raid_disks * 2; i++) { 7473cb03002SNeilBrown struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); 7480d129228SNeilBrown if (rdev && !test_bit(Faulty, &rdev->flags)) { 749165125e1SJens Axboe struct request_queue *q = bdev_get_queue(rdev->bdev); 7500d129228SNeilBrown 7511ed7242eSJonathan Brassow BUG_ON(!q); 7521ed7242eSJonathan Brassow 7530d129228SNeilBrown /* Note the '|| 1' - when read_balance prefers 7540d129228SNeilBrown * non-congested targets, it can be removed 7550d129228SNeilBrown */ 75691a9e99dSAlexander Beregalov if ((bits & (1<<BDI_async_congested)) || 1) 7570d129228SNeilBrown ret |= bdi_congested(&q->backing_dev_info, bits); 7580d129228SNeilBrown else 7590d129228SNeilBrown ret &= bdi_congested(&q->backing_dev_info, bits); 7600d129228SNeilBrown } 7610d129228SNeilBrown } 7620d129228SNeilBrown rcu_read_unlock(); 7630d129228SNeilBrown return ret; 7640d129228SNeilBrown } 7650d129228SNeilBrown 766e8096360SNeilBrown static void flush_pending_writes(struct r1conf *conf) 767a35e63efSNeilBrown { 768a35e63efSNeilBrown /* Any writes that have been queued but are awaiting 769a35e63efSNeilBrown * bitmap updates get flushed here. 770a35e63efSNeilBrown */ 771a35e63efSNeilBrown spin_lock_irq(&conf->device_lock); 772a35e63efSNeilBrown 773a35e63efSNeilBrown if (conf->pending_bio_list.head) { 774a35e63efSNeilBrown struct bio *bio; 775a35e63efSNeilBrown bio = bio_list_get(&conf->pending_bio_list); 77634db0cd6SNeilBrown conf->pending_count = 0; 777a35e63efSNeilBrown spin_unlock_irq(&conf->device_lock); 778a35e63efSNeilBrown /* flush any pending bitmap writes to 779a35e63efSNeilBrown * disk before proceeding w/ I/O */ 780a35e63efSNeilBrown bitmap_unplug(conf->mddev->bitmap); 78134db0cd6SNeilBrown wake_up(&conf->wait_barrier); 782a35e63efSNeilBrown 783a35e63efSNeilBrown while (bio) { /* submit pending writes */ 784a35e63efSNeilBrown struct bio *next = bio->bi_next; 785a35e63efSNeilBrown bio->bi_next = NULL; 7862ff8cc2cSShaohua Li if (unlikely((bio->bi_rw & REQ_DISCARD) && 7872ff8cc2cSShaohua Li !blk_queue_discard(bdev_get_queue(bio->bi_bdev)))) 7882ff8cc2cSShaohua Li /* Just ignore it */ 7892ff8cc2cSShaohua Li bio_endio(bio, 0); 7902ff8cc2cSShaohua Li else 791a35e63efSNeilBrown generic_make_request(bio); 792a35e63efSNeilBrown bio = next; 793a35e63efSNeilBrown } 794a35e63efSNeilBrown } else 795a35e63efSNeilBrown spin_unlock_irq(&conf->device_lock); 7967eaceaccSJens Axboe } 7977eaceaccSJens Axboe 79817999be4SNeilBrown /* Barriers.... 79917999be4SNeilBrown * Sometimes we need to suspend IO while we do something else, 80017999be4SNeilBrown * either some resync/recovery, or reconfigure the array. 80117999be4SNeilBrown * To do this we raise a 'barrier'. 80217999be4SNeilBrown * The 'barrier' is a counter that can be raised multiple times 80317999be4SNeilBrown * to count how many activities are happening which preclude 80417999be4SNeilBrown * normal IO. 80517999be4SNeilBrown * We can only raise the barrier if there is no pending IO. 80617999be4SNeilBrown * i.e. if nr_pending == 0. 80717999be4SNeilBrown * We choose only to raise the barrier if no-one is waiting for the 80817999be4SNeilBrown * barrier to go down. This means that as soon as an IO request 80917999be4SNeilBrown * is ready, no other operations which require a barrier will start 81017999be4SNeilBrown * until the IO request has had a chance. 81117999be4SNeilBrown * 81217999be4SNeilBrown * So: regular IO calls 'wait_barrier'. When that returns there 81317999be4SNeilBrown * is no backgroup IO happening, It must arrange to call 81417999be4SNeilBrown * allow_barrier when it has finished its IO. 81517999be4SNeilBrown * backgroup IO calls must call raise_barrier. Once that returns 81617999be4SNeilBrown * there is no normal IO happeing. It must arrange to call 81717999be4SNeilBrown * lower_barrier when the particular background IO completes. 8181da177e4SLinus Torvalds */ 819c2fd4c94SNeilBrown static void raise_barrier(struct r1conf *conf, sector_t sector_nr) 8201da177e4SLinus Torvalds { 8211da177e4SLinus Torvalds spin_lock_irq(&conf->resync_lock); 8221da177e4SLinus Torvalds 82317999be4SNeilBrown /* Wait until no block IO is waiting */ 82417999be4SNeilBrown wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting, 825eed8c02eSLukas Czerner conf->resync_lock); 82617999be4SNeilBrown 82717999be4SNeilBrown /* block any new IO from starting */ 82817999be4SNeilBrown conf->barrier++; 829c2fd4c94SNeilBrown conf->next_resync = sector_nr; 83017999be4SNeilBrown 83179ef3a8aSmajianpeng /* For these conditions we must wait: 83279ef3a8aSmajianpeng * A: while the array is in frozen state 83379ef3a8aSmajianpeng * B: while barrier >= RESYNC_DEPTH, meaning resync reach 83479ef3a8aSmajianpeng * the max count which allowed. 83579ef3a8aSmajianpeng * C: next_resync + RESYNC_SECTORS > start_next_window, meaning 83679ef3a8aSmajianpeng * next resync will reach to the window which normal bios are 83779ef3a8aSmajianpeng * handling. 8382f73d3c5SNeilBrown * D: while there are any active requests in the current window. 83979ef3a8aSmajianpeng */ 84017999be4SNeilBrown wait_event_lock_irq(conf->wait_barrier, 841b364e3d0Smajianpeng !conf->array_frozen && 84279ef3a8aSmajianpeng conf->barrier < RESYNC_DEPTH && 8432f73d3c5SNeilBrown conf->current_window_requests == 0 && 84479ef3a8aSmajianpeng (conf->start_next_window >= 84579ef3a8aSmajianpeng conf->next_resync + RESYNC_SECTORS), 846eed8c02eSLukas Czerner conf->resync_lock); 84717999be4SNeilBrown 84834e97f17SNeilBrown conf->nr_pending++; 8491da177e4SLinus Torvalds spin_unlock_irq(&conf->resync_lock); 8501da177e4SLinus Torvalds } 8511da177e4SLinus Torvalds 852e8096360SNeilBrown static void lower_barrier(struct r1conf *conf) 85317999be4SNeilBrown { 85417999be4SNeilBrown unsigned long flags; 855709ae487SNeilBrown BUG_ON(conf->barrier <= 0); 85617999be4SNeilBrown spin_lock_irqsave(&conf->resync_lock, flags); 85717999be4SNeilBrown conf->barrier--; 85834e97f17SNeilBrown conf->nr_pending--; 85917999be4SNeilBrown spin_unlock_irqrestore(&conf->resync_lock, flags); 86017999be4SNeilBrown wake_up(&conf->wait_barrier); 86117999be4SNeilBrown } 86217999be4SNeilBrown 86379ef3a8aSmajianpeng static bool need_to_wait_for_sync(struct r1conf *conf, struct bio *bio) 86417999be4SNeilBrown { 86579ef3a8aSmajianpeng bool wait = false; 86679ef3a8aSmajianpeng 86779ef3a8aSmajianpeng if (conf->array_frozen || !bio) 86879ef3a8aSmajianpeng wait = true; 86979ef3a8aSmajianpeng else if (conf->barrier && bio_data_dir(bio) == WRITE) { 87023554960SNeilBrown if ((conf->mddev->curr_resync_completed 87179ef3a8aSmajianpeng >= bio_end_sector(bio)) || 87279ef3a8aSmajianpeng (conf->next_resync + NEXT_NORMALIO_DISTANCE 8734f024f37SKent Overstreet <= bio->bi_iter.bi_sector)) 87479ef3a8aSmajianpeng wait = false; 87579ef3a8aSmajianpeng else 87679ef3a8aSmajianpeng wait = true; 87779ef3a8aSmajianpeng } 87879ef3a8aSmajianpeng 87979ef3a8aSmajianpeng return wait; 88079ef3a8aSmajianpeng } 88179ef3a8aSmajianpeng 88279ef3a8aSmajianpeng static sector_t wait_barrier(struct r1conf *conf, struct bio *bio) 88379ef3a8aSmajianpeng { 88479ef3a8aSmajianpeng sector_t sector = 0; 88579ef3a8aSmajianpeng 88617999be4SNeilBrown spin_lock_irq(&conf->resync_lock); 88779ef3a8aSmajianpeng if (need_to_wait_for_sync(conf, bio)) { 88817999be4SNeilBrown conf->nr_waiting++; 889d6b42dcbSNeilBrown /* Wait for the barrier to drop. 890d6b42dcbSNeilBrown * However if there are already pending 891d6b42dcbSNeilBrown * requests (preventing the barrier from 892d6b42dcbSNeilBrown * rising completely), and the 8935965b642SNeilBrown * per-process bio queue isn't empty, 894d6b42dcbSNeilBrown * then don't wait, as we need to empty 8955965b642SNeilBrown * that queue to allow conf->start_next_window 8965965b642SNeilBrown * to increase. 897d6b42dcbSNeilBrown */ 898d6b42dcbSNeilBrown wait_event_lock_irq(conf->wait_barrier, 899b364e3d0Smajianpeng !conf->array_frozen && 900b364e3d0Smajianpeng (!conf->barrier || 90179ef3a8aSmajianpeng ((conf->start_next_window < 90279ef3a8aSmajianpeng conf->next_resync + RESYNC_SECTORS) && 903d6b42dcbSNeilBrown current->bio_list && 904b364e3d0Smajianpeng !bio_list_empty(current->bio_list))), 905eed8c02eSLukas Czerner conf->resync_lock); 90617999be4SNeilBrown conf->nr_waiting--; 90717999be4SNeilBrown } 90879ef3a8aSmajianpeng 90979ef3a8aSmajianpeng if (bio && bio_data_dir(bio) == WRITE) { 9102f73d3c5SNeilBrown if (bio->bi_iter.bi_sector >= 91123554960SNeilBrown conf->mddev->curr_resync_completed) { 91279ef3a8aSmajianpeng if (conf->start_next_window == MaxSector) 91379ef3a8aSmajianpeng conf->start_next_window = 91479ef3a8aSmajianpeng conf->next_resync + 91579ef3a8aSmajianpeng NEXT_NORMALIO_DISTANCE; 91679ef3a8aSmajianpeng 91779ef3a8aSmajianpeng if ((conf->start_next_window + NEXT_NORMALIO_DISTANCE) 9184f024f37SKent Overstreet <= bio->bi_iter.bi_sector) 91979ef3a8aSmajianpeng conf->next_window_requests++; 92079ef3a8aSmajianpeng else 92179ef3a8aSmajianpeng conf->current_window_requests++; 92279ef3a8aSmajianpeng sector = conf->start_next_window; 92317999be4SNeilBrown } 92441a336e0SNeilBrown } 92517999be4SNeilBrown 92679ef3a8aSmajianpeng conf->nr_pending++; 92779ef3a8aSmajianpeng spin_unlock_irq(&conf->resync_lock); 92879ef3a8aSmajianpeng return sector; 92979ef3a8aSmajianpeng } 93079ef3a8aSmajianpeng 93179ef3a8aSmajianpeng static void allow_barrier(struct r1conf *conf, sector_t start_next_window, 93279ef3a8aSmajianpeng sector_t bi_sector) 93317999be4SNeilBrown { 93417999be4SNeilBrown unsigned long flags; 93579ef3a8aSmajianpeng 93617999be4SNeilBrown spin_lock_irqsave(&conf->resync_lock, flags); 93717999be4SNeilBrown conf->nr_pending--; 93879ef3a8aSmajianpeng if (start_next_window) { 93979ef3a8aSmajianpeng if (start_next_window == conf->start_next_window) { 94079ef3a8aSmajianpeng if (conf->start_next_window + NEXT_NORMALIO_DISTANCE 94179ef3a8aSmajianpeng <= bi_sector) 94279ef3a8aSmajianpeng conf->next_window_requests--; 94379ef3a8aSmajianpeng else 94479ef3a8aSmajianpeng conf->current_window_requests--; 94579ef3a8aSmajianpeng } else 94679ef3a8aSmajianpeng conf->current_window_requests--; 94779ef3a8aSmajianpeng 94879ef3a8aSmajianpeng if (!conf->current_window_requests) { 94979ef3a8aSmajianpeng if (conf->next_window_requests) { 95079ef3a8aSmajianpeng conf->current_window_requests = 95179ef3a8aSmajianpeng conf->next_window_requests; 95279ef3a8aSmajianpeng conf->next_window_requests = 0; 95379ef3a8aSmajianpeng conf->start_next_window += 95479ef3a8aSmajianpeng NEXT_NORMALIO_DISTANCE; 95579ef3a8aSmajianpeng } else 95679ef3a8aSmajianpeng conf->start_next_window = MaxSector; 95779ef3a8aSmajianpeng } 95879ef3a8aSmajianpeng } 95917999be4SNeilBrown spin_unlock_irqrestore(&conf->resync_lock, flags); 96017999be4SNeilBrown wake_up(&conf->wait_barrier); 96117999be4SNeilBrown } 96217999be4SNeilBrown 963e2d59925SNeilBrown static void freeze_array(struct r1conf *conf, int extra) 964ddaf22abSNeilBrown { 965ddaf22abSNeilBrown /* stop syncio and normal IO and wait for everything to 966ddaf22abSNeilBrown * go quite. 967b364e3d0Smajianpeng * We wait until nr_pending match nr_queued+extra 9681c830532SNeilBrown * This is called in the context of one normal IO request 9691c830532SNeilBrown * that has failed. Thus any sync request that might be pending 9701c830532SNeilBrown * will be blocked by nr_pending, and we need to wait for 9711c830532SNeilBrown * pending IO requests to complete or be queued for re-try. 972e2d59925SNeilBrown * Thus the number queued (nr_queued) plus this request (extra) 9731c830532SNeilBrown * must match the number of pending IOs (nr_pending) before 9741c830532SNeilBrown * we continue. 975ddaf22abSNeilBrown */ 976ddaf22abSNeilBrown spin_lock_irq(&conf->resync_lock); 977b364e3d0Smajianpeng conf->array_frozen = 1; 978eed8c02eSLukas Czerner wait_event_lock_irq_cmd(conf->wait_barrier, 979e2d59925SNeilBrown conf->nr_pending == conf->nr_queued+extra, 980ddaf22abSNeilBrown conf->resync_lock, 981c3b328acSNeilBrown flush_pending_writes(conf)); 982ddaf22abSNeilBrown spin_unlock_irq(&conf->resync_lock); 983ddaf22abSNeilBrown } 984e8096360SNeilBrown static void unfreeze_array(struct r1conf *conf) 985ddaf22abSNeilBrown { 986ddaf22abSNeilBrown /* reverse the effect of the freeze */ 987ddaf22abSNeilBrown spin_lock_irq(&conf->resync_lock); 988b364e3d0Smajianpeng conf->array_frozen = 0; 989ddaf22abSNeilBrown wake_up(&conf->wait_barrier); 990ddaf22abSNeilBrown spin_unlock_irq(&conf->resync_lock); 991ddaf22abSNeilBrown } 992ddaf22abSNeilBrown 9934e78064fSNeilBrown /* duplicate the data pages for behind I/O 9944e78064fSNeilBrown */ 9959f2c9d12SNeilBrown static void alloc_behind_pages(struct bio *bio, struct r1bio *r1_bio) 9964b6d287fSNeilBrown { 9974b6d287fSNeilBrown int i; 9984b6d287fSNeilBrown struct bio_vec *bvec; 9992ca68f5eSNeilBrown struct bio_vec *bvecs = kzalloc(bio->bi_vcnt * sizeof(struct bio_vec), 10004b6d287fSNeilBrown GFP_NOIO); 10012ca68f5eSNeilBrown if (unlikely(!bvecs)) 1002af6d7b76SNeilBrown return; 10034b6d287fSNeilBrown 1004cb34e057SKent Overstreet bio_for_each_segment_all(bvec, bio, i) { 10052ca68f5eSNeilBrown bvecs[i] = *bvec; 10062ca68f5eSNeilBrown bvecs[i].bv_page = alloc_page(GFP_NOIO); 10072ca68f5eSNeilBrown if (unlikely(!bvecs[i].bv_page)) 10084b6d287fSNeilBrown goto do_sync_io; 10092ca68f5eSNeilBrown memcpy(kmap(bvecs[i].bv_page) + bvec->bv_offset, 10104b6d287fSNeilBrown kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len); 10112ca68f5eSNeilBrown kunmap(bvecs[i].bv_page); 10124b6d287fSNeilBrown kunmap(bvec->bv_page); 10134b6d287fSNeilBrown } 10142ca68f5eSNeilBrown r1_bio->behind_bvecs = bvecs; 1015af6d7b76SNeilBrown r1_bio->behind_page_count = bio->bi_vcnt; 1016af6d7b76SNeilBrown set_bit(R1BIO_BehindIO, &r1_bio->state); 1017af6d7b76SNeilBrown return; 10184b6d287fSNeilBrown 10194b6d287fSNeilBrown do_sync_io: 1020af6d7b76SNeilBrown for (i = 0; i < bio->bi_vcnt; i++) 10212ca68f5eSNeilBrown if (bvecs[i].bv_page) 10222ca68f5eSNeilBrown put_page(bvecs[i].bv_page); 10232ca68f5eSNeilBrown kfree(bvecs); 10244f024f37SKent Overstreet pr_debug("%dB behind alloc failed, doing sync I/O\n", 10254f024f37SKent Overstreet bio->bi_iter.bi_size); 10264b6d287fSNeilBrown } 10274b6d287fSNeilBrown 1028f54a9d0eSNeilBrown struct raid1_plug_cb { 1029f54a9d0eSNeilBrown struct blk_plug_cb cb; 1030f54a9d0eSNeilBrown struct bio_list pending; 1031f54a9d0eSNeilBrown int pending_cnt; 1032f54a9d0eSNeilBrown }; 1033f54a9d0eSNeilBrown 1034f54a9d0eSNeilBrown static void raid1_unplug(struct blk_plug_cb *cb, bool from_schedule) 1035f54a9d0eSNeilBrown { 1036f54a9d0eSNeilBrown struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb, 1037f54a9d0eSNeilBrown cb); 1038f54a9d0eSNeilBrown struct mddev *mddev = plug->cb.data; 1039f54a9d0eSNeilBrown struct r1conf *conf = mddev->private; 1040f54a9d0eSNeilBrown struct bio *bio; 1041f54a9d0eSNeilBrown 1042874807a8SNeilBrown if (from_schedule || current->bio_list) { 1043f54a9d0eSNeilBrown spin_lock_irq(&conf->device_lock); 1044f54a9d0eSNeilBrown bio_list_merge(&conf->pending_bio_list, &plug->pending); 1045f54a9d0eSNeilBrown conf->pending_count += plug->pending_cnt; 1046f54a9d0eSNeilBrown spin_unlock_irq(&conf->device_lock); 1047ee0b0244SNeilBrown wake_up(&conf->wait_barrier); 1048f54a9d0eSNeilBrown md_wakeup_thread(mddev->thread); 1049f54a9d0eSNeilBrown kfree(plug); 1050f54a9d0eSNeilBrown return; 1051f54a9d0eSNeilBrown } 1052f54a9d0eSNeilBrown 1053f54a9d0eSNeilBrown /* we aren't scheduling, so we can do the write-out directly. */ 1054f54a9d0eSNeilBrown bio = bio_list_get(&plug->pending); 1055f54a9d0eSNeilBrown bitmap_unplug(mddev->bitmap); 1056f54a9d0eSNeilBrown wake_up(&conf->wait_barrier); 1057f54a9d0eSNeilBrown 1058f54a9d0eSNeilBrown while (bio) { /* submit pending writes */ 1059f54a9d0eSNeilBrown struct bio *next = bio->bi_next; 1060f54a9d0eSNeilBrown bio->bi_next = NULL; 106132f9f570SShaohua Li if (unlikely((bio->bi_rw & REQ_DISCARD) && 106232f9f570SShaohua Li !blk_queue_discard(bdev_get_queue(bio->bi_bdev)))) 106332f9f570SShaohua Li /* Just ignore it */ 106432f9f570SShaohua Li bio_endio(bio, 0); 106532f9f570SShaohua Li else 1066f54a9d0eSNeilBrown generic_make_request(bio); 1067f54a9d0eSNeilBrown bio = next; 1068f54a9d0eSNeilBrown } 1069f54a9d0eSNeilBrown kfree(plug); 1070f54a9d0eSNeilBrown } 1071f54a9d0eSNeilBrown 1072b4fdcb02SLinus Torvalds static void make_request(struct mddev *mddev, struct bio * bio) 10731da177e4SLinus Torvalds { 1074e8096360SNeilBrown struct r1conf *conf = mddev->private; 10750eaf822cSJonathan Brassow struct raid1_info *mirror; 10769f2c9d12SNeilBrown struct r1bio *r1_bio; 10771da177e4SLinus Torvalds struct bio *read_bio; 10781f68f0c4SNeilBrown int i, disks; 107984255d10SNeilBrown struct bitmap *bitmap; 1080191ea9b2SNeilBrown unsigned long flags; 1081a362357bSJens Axboe const int rw = bio_data_dir(bio); 10822c7d46ecSNeilBrown const unsigned long do_sync = (bio->bi_rw & REQ_SYNC); 1083e9c7469bSTejun Heo const unsigned long do_flush_fua = (bio->bi_rw & (REQ_FLUSH | REQ_FUA)); 10842ff8cc2cSShaohua Li const unsigned long do_discard = (bio->bi_rw 10852ff8cc2cSShaohua Li & (REQ_DISCARD | REQ_SECURE)); 1086c8dc9c65SJoe Lawrence const unsigned long do_same = (bio->bi_rw & REQ_WRITE_SAME); 10873cb03002SNeilBrown struct md_rdev *blocked_rdev; 1088f54a9d0eSNeilBrown struct blk_plug_cb *cb; 1089f54a9d0eSNeilBrown struct raid1_plug_cb *plug = NULL; 10901f68f0c4SNeilBrown int first_clone; 10911f68f0c4SNeilBrown int sectors_handled; 10921f68f0c4SNeilBrown int max_sectors; 109379ef3a8aSmajianpeng sector_t start_next_window; 1094191ea9b2SNeilBrown 10951da177e4SLinus Torvalds /* 10961da177e4SLinus Torvalds * Register the new request and wait if the reconstruction 10971da177e4SLinus Torvalds * thread has put up a bar for new requests. 10981da177e4SLinus Torvalds * Continue immediately if no resync is active currently. 10991da177e4SLinus Torvalds */ 110062de608dSNeilBrown 11013d310eb7SNeilBrown md_write_start(mddev, bio); /* wait on superblock update early */ 11023d310eb7SNeilBrown 11036eef4b21SNeilBrown if (bio_data_dir(bio) == WRITE && 1104f73a1c7dSKent Overstreet bio_end_sector(bio) > mddev->suspend_lo && 11054f024f37SKent Overstreet bio->bi_iter.bi_sector < mddev->suspend_hi) { 11066eef4b21SNeilBrown /* As the suspend_* range is controlled by 11076eef4b21SNeilBrown * userspace, we want an interruptible 11086eef4b21SNeilBrown * wait. 11096eef4b21SNeilBrown */ 11106eef4b21SNeilBrown DEFINE_WAIT(w); 11116eef4b21SNeilBrown for (;;) { 11126eef4b21SNeilBrown flush_signals(current); 11136eef4b21SNeilBrown prepare_to_wait(&conf->wait_barrier, 11146eef4b21SNeilBrown &w, TASK_INTERRUPTIBLE); 1115f73a1c7dSKent Overstreet if (bio_end_sector(bio) <= mddev->suspend_lo || 11164f024f37SKent Overstreet bio->bi_iter.bi_sector >= mddev->suspend_hi) 11176eef4b21SNeilBrown break; 11186eef4b21SNeilBrown schedule(); 11196eef4b21SNeilBrown } 11206eef4b21SNeilBrown finish_wait(&conf->wait_barrier, &w); 11216eef4b21SNeilBrown } 112262de608dSNeilBrown 112379ef3a8aSmajianpeng start_next_window = wait_barrier(conf, bio); 11241da177e4SLinus Torvalds 112584255d10SNeilBrown bitmap = mddev->bitmap; 112684255d10SNeilBrown 11271da177e4SLinus Torvalds /* 11281da177e4SLinus Torvalds * make_request() can abort the operation when READA is being 11291da177e4SLinus Torvalds * used and no empty request is available. 11301da177e4SLinus Torvalds * 11311da177e4SLinus Torvalds */ 11321da177e4SLinus Torvalds r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO); 11331da177e4SLinus Torvalds 11341da177e4SLinus Torvalds r1_bio->master_bio = bio; 1135aa8b57aaSKent Overstreet r1_bio->sectors = bio_sectors(bio); 1136191ea9b2SNeilBrown r1_bio->state = 0; 11371da177e4SLinus Torvalds r1_bio->mddev = mddev; 11384f024f37SKent Overstreet r1_bio->sector = bio->bi_iter.bi_sector; 11391da177e4SLinus Torvalds 1140d2eb35acSNeilBrown /* We might need to issue multiple reads to different 1141d2eb35acSNeilBrown * devices if there are bad blocks around, so we keep 1142d2eb35acSNeilBrown * track of the number of reads in bio->bi_phys_segments. 1143d2eb35acSNeilBrown * If this is 0, there is only one r1_bio and no locking 1144d2eb35acSNeilBrown * will be needed when requests complete. If it is 1145d2eb35acSNeilBrown * non-zero, then it is the number of not-completed requests. 1146d2eb35acSNeilBrown */ 1147d2eb35acSNeilBrown bio->bi_phys_segments = 0; 1148d2eb35acSNeilBrown clear_bit(BIO_SEG_VALID, &bio->bi_flags); 1149d2eb35acSNeilBrown 1150a362357bSJens Axboe if (rw == READ) { 11511da177e4SLinus Torvalds /* 11521da177e4SLinus Torvalds * read balancing logic: 11531da177e4SLinus Torvalds */ 1154d2eb35acSNeilBrown int rdisk; 1155d2eb35acSNeilBrown 1156d2eb35acSNeilBrown read_again: 1157d2eb35acSNeilBrown rdisk = read_balance(conf, r1_bio, &max_sectors); 11581da177e4SLinus Torvalds 11591da177e4SLinus Torvalds if (rdisk < 0) { 11601da177e4SLinus Torvalds /* couldn't find anywhere to read from */ 11611da177e4SLinus Torvalds raid_end_bio_io(r1_bio); 11625a7bbad2SChristoph Hellwig return; 11631da177e4SLinus Torvalds } 11641da177e4SLinus Torvalds mirror = conf->mirrors + rdisk; 11651da177e4SLinus Torvalds 1166e555190dSNeilBrown if (test_bit(WriteMostly, &mirror->rdev->flags) && 1167e555190dSNeilBrown bitmap) { 1168e555190dSNeilBrown /* Reading from a write-mostly device must 1169e555190dSNeilBrown * take care not to over-take any writes 1170e555190dSNeilBrown * that are 'behind' 1171e555190dSNeilBrown */ 1172e555190dSNeilBrown wait_event(bitmap->behind_wait, 1173e555190dSNeilBrown atomic_read(&bitmap->behind_writes) == 0); 1174e555190dSNeilBrown } 11751da177e4SLinus Torvalds r1_bio->read_disk = rdisk; 1176f0cc9a05SNeilBrown r1_bio->start_next_window = 0; 11771da177e4SLinus Torvalds 1178a167f663SNeilBrown read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev); 11794f024f37SKent Overstreet bio_trim(read_bio, r1_bio->sector - bio->bi_iter.bi_sector, 1180d2eb35acSNeilBrown max_sectors); 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds r1_bio->bios[rdisk] = read_bio; 11831da177e4SLinus Torvalds 11844f024f37SKent Overstreet read_bio->bi_iter.bi_sector = r1_bio->sector + 11854f024f37SKent Overstreet mirror->rdev->data_offset; 11861da177e4SLinus Torvalds read_bio->bi_bdev = mirror->rdev->bdev; 11871da177e4SLinus Torvalds read_bio->bi_end_io = raid1_end_read_request; 11887b6d91daSChristoph Hellwig read_bio->bi_rw = READ | do_sync; 11891da177e4SLinus Torvalds read_bio->bi_private = r1_bio; 11901da177e4SLinus Torvalds 1191d2eb35acSNeilBrown if (max_sectors < r1_bio->sectors) { 1192d2eb35acSNeilBrown /* could not read all from this device, so we will 1193d2eb35acSNeilBrown * need another r1_bio. 1194d2eb35acSNeilBrown */ 1195d2eb35acSNeilBrown 1196d2eb35acSNeilBrown sectors_handled = (r1_bio->sector + max_sectors 11974f024f37SKent Overstreet - bio->bi_iter.bi_sector); 1198d2eb35acSNeilBrown r1_bio->sectors = max_sectors; 1199d2eb35acSNeilBrown spin_lock_irq(&conf->device_lock); 1200d2eb35acSNeilBrown if (bio->bi_phys_segments == 0) 1201d2eb35acSNeilBrown bio->bi_phys_segments = 2; 1202d2eb35acSNeilBrown else 1203d2eb35acSNeilBrown bio->bi_phys_segments++; 1204d2eb35acSNeilBrown spin_unlock_irq(&conf->device_lock); 1205d2eb35acSNeilBrown /* Cannot call generic_make_request directly 1206d2eb35acSNeilBrown * as that will be queued in __make_request 1207d2eb35acSNeilBrown * and subsequent mempool_alloc might block waiting 1208d2eb35acSNeilBrown * for it. So hand bio over to raid1d. 1209d2eb35acSNeilBrown */ 1210d2eb35acSNeilBrown reschedule_retry(r1_bio); 1211d2eb35acSNeilBrown 1212d2eb35acSNeilBrown r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO); 1213d2eb35acSNeilBrown 1214d2eb35acSNeilBrown r1_bio->master_bio = bio; 1215aa8b57aaSKent Overstreet r1_bio->sectors = bio_sectors(bio) - sectors_handled; 1216d2eb35acSNeilBrown r1_bio->state = 0; 1217d2eb35acSNeilBrown r1_bio->mddev = mddev; 12184f024f37SKent Overstreet r1_bio->sector = bio->bi_iter.bi_sector + 12194f024f37SKent Overstreet sectors_handled; 1220d2eb35acSNeilBrown goto read_again; 1221d2eb35acSNeilBrown } else 12221da177e4SLinus Torvalds generic_make_request(read_bio); 12235a7bbad2SChristoph Hellwig return; 12241da177e4SLinus Torvalds } 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds /* 12271da177e4SLinus Torvalds * WRITE: 12281da177e4SLinus Torvalds */ 122934db0cd6SNeilBrown if (conf->pending_count >= max_queued_requests) { 123034db0cd6SNeilBrown md_wakeup_thread(mddev->thread); 123134db0cd6SNeilBrown wait_event(conf->wait_barrier, 123234db0cd6SNeilBrown conf->pending_count < max_queued_requests); 123334db0cd6SNeilBrown } 12341f68f0c4SNeilBrown /* first select target devices under rcu_lock and 12351da177e4SLinus Torvalds * inc refcount on their rdev. Record them by setting 12361da177e4SLinus Torvalds * bios[x] to bio 12371f68f0c4SNeilBrown * If there are known/acknowledged bad blocks on any device on 12381f68f0c4SNeilBrown * which we have seen a write error, we want to avoid writing those 12391f68f0c4SNeilBrown * blocks. 12401f68f0c4SNeilBrown * This potentially requires several writes to write around 12411f68f0c4SNeilBrown * the bad blocks. Each set of writes gets it's own r1bio 12421f68f0c4SNeilBrown * with a set of bios attached. 12431da177e4SLinus Torvalds */ 1244c3b328acSNeilBrown 12458f19ccb2SNeilBrown disks = conf->raid_disks * 2; 12466bfe0b49SDan Williams retry_write: 124779ef3a8aSmajianpeng r1_bio->start_next_window = start_next_window; 12486bfe0b49SDan Williams blocked_rdev = NULL; 12491da177e4SLinus Torvalds rcu_read_lock(); 12501f68f0c4SNeilBrown max_sectors = r1_bio->sectors; 12511da177e4SLinus Torvalds for (i = 0; i < disks; i++) { 12523cb03002SNeilBrown struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); 12536bfe0b49SDan Williams if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) { 12546bfe0b49SDan Williams atomic_inc(&rdev->nr_pending); 12556bfe0b49SDan Williams blocked_rdev = rdev; 12566bfe0b49SDan Williams break; 12576bfe0b49SDan Williams } 12581da177e4SLinus Torvalds r1_bio->bios[i] = NULL; 12596b740b8dSNeilBrown if (!rdev || test_bit(Faulty, &rdev->flags) 12606b740b8dSNeilBrown || test_bit(Unmerged, &rdev->flags)) { 12618f19ccb2SNeilBrown if (i < conf->raid_disks) 12621f68f0c4SNeilBrown set_bit(R1BIO_Degraded, &r1_bio->state); 12631f68f0c4SNeilBrown continue; 1264964147d5SNeilBrown } 12651f68f0c4SNeilBrown 12661f68f0c4SNeilBrown atomic_inc(&rdev->nr_pending); 12671f68f0c4SNeilBrown if (test_bit(WriteErrorSeen, &rdev->flags)) { 12681f68f0c4SNeilBrown sector_t first_bad; 12691f68f0c4SNeilBrown int bad_sectors; 12701f68f0c4SNeilBrown int is_bad; 12711f68f0c4SNeilBrown 12721f68f0c4SNeilBrown is_bad = is_badblock(rdev, r1_bio->sector, 12731f68f0c4SNeilBrown max_sectors, 12741f68f0c4SNeilBrown &first_bad, &bad_sectors); 12751f68f0c4SNeilBrown if (is_bad < 0) { 12761f68f0c4SNeilBrown /* mustn't write here until the bad block is 12771f68f0c4SNeilBrown * acknowledged*/ 12781f68f0c4SNeilBrown set_bit(BlockedBadBlocks, &rdev->flags); 12791f68f0c4SNeilBrown blocked_rdev = rdev; 12801f68f0c4SNeilBrown break; 12811f68f0c4SNeilBrown } 12821f68f0c4SNeilBrown if (is_bad && first_bad <= r1_bio->sector) { 12831f68f0c4SNeilBrown /* Cannot write here at all */ 12841f68f0c4SNeilBrown bad_sectors -= (r1_bio->sector - first_bad); 12851f68f0c4SNeilBrown if (bad_sectors < max_sectors) 12861f68f0c4SNeilBrown /* mustn't write more than bad_sectors 12871f68f0c4SNeilBrown * to other devices yet 12881f68f0c4SNeilBrown */ 12891f68f0c4SNeilBrown max_sectors = bad_sectors; 12901f68f0c4SNeilBrown rdev_dec_pending(rdev, mddev); 12911f68f0c4SNeilBrown /* We don't set R1BIO_Degraded as that 12921f68f0c4SNeilBrown * only applies if the disk is 12931f68f0c4SNeilBrown * missing, so it might be re-added, 12941f68f0c4SNeilBrown * and we want to know to recover this 12951f68f0c4SNeilBrown * chunk. 12961f68f0c4SNeilBrown * In this case the device is here, 12971f68f0c4SNeilBrown * and the fact that this chunk is not 12981f68f0c4SNeilBrown * in-sync is recorded in the bad 12991f68f0c4SNeilBrown * block log 13001f68f0c4SNeilBrown */ 13011f68f0c4SNeilBrown continue; 13021f68f0c4SNeilBrown } 13031f68f0c4SNeilBrown if (is_bad) { 13041f68f0c4SNeilBrown int good_sectors = first_bad - r1_bio->sector; 13051f68f0c4SNeilBrown if (good_sectors < max_sectors) 13061f68f0c4SNeilBrown max_sectors = good_sectors; 13071f68f0c4SNeilBrown } 13081f68f0c4SNeilBrown } 13091f68f0c4SNeilBrown r1_bio->bios[i] = bio; 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds rcu_read_unlock(); 13121da177e4SLinus Torvalds 13136bfe0b49SDan Williams if (unlikely(blocked_rdev)) { 13146bfe0b49SDan Williams /* Wait for this device to become unblocked */ 13156bfe0b49SDan Williams int j; 131679ef3a8aSmajianpeng sector_t old = start_next_window; 13176bfe0b49SDan Williams 13186bfe0b49SDan Williams for (j = 0; j < i; j++) 13196bfe0b49SDan Williams if (r1_bio->bios[j]) 13206bfe0b49SDan Williams rdev_dec_pending(conf->mirrors[j].rdev, mddev); 13211f68f0c4SNeilBrown r1_bio->state = 0; 13224f024f37SKent Overstreet allow_barrier(conf, start_next_window, bio->bi_iter.bi_sector); 13236bfe0b49SDan Williams md_wait_for_blocked_rdev(blocked_rdev, mddev); 132479ef3a8aSmajianpeng start_next_window = wait_barrier(conf, bio); 132579ef3a8aSmajianpeng /* 132679ef3a8aSmajianpeng * We must make sure the multi r1bios of bio have 132779ef3a8aSmajianpeng * the same value of bi_phys_segments 132879ef3a8aSmajianpeng */ 132979ef3a8aSmajianpeng if (bio->bi_phys_segments && old && 133079ef3a8aSmajianpeng old != start_next_window) 133179ef3a8aSmajianpeng /* Wait for the former r1bio(s) to complete */ 133279ef3a8aSmajianpeng wait_event(conf->wait_barrier, 133379ef3a8aSmajianpeng bio->bi_phys_segments == 1); 13346bfe0b49SDan Williams goto retry_write; 13356bfe0b49SDan Williams } 13366bfe0b49SDan Williams 13371f68f0c4SNeilBrown if (max_sectors < r1_bio->sectors) { 13381f68f0c4SNeilBrown /* We are splitting this write into multiple parts, so 13391f68f0c4SNeilBrown * we need to prepare for allocating another r1_bio. 13401f68f0c4SNeilBrown */ 13411f68f0c4SNeilBrown r1_bio->sectors = max_sectors; 13421f68f0c4SNeilBrown spin_lock_irq(&conf->device_lock); 13431f68f0c4SNeilBrown if (bio->bi_phys_segments == 0) 13441f68f0c4SNeilBrown bio->bi_phys_segments = 2; 13451f68f0c4SNeilBrown else 13461f68f0c4SNeilBrown bio->bi_phys_segments++; 13471f68f0c4SNeilBrown spin_unlock_irq(&conf->device_lock); 1348191ea9b2SNeilBrown } 13494f024f37SKent Overstreet sectors_handled = r1_bio->sector + max_sectors - bio->bi_iter.bi_sector; 13504b6d287fSNeilBrown 13514e78064fSNeilBrown atomic_set(&r1_bio->remaining, 1); 13524b6d287fSNeilBrown atomic_set(&r1_bio->behind_remaining, 0); 1353191ea9b2SNeilBrown 13541f68f0c4SNeilBrown first_clone = 1; 13551da177e4SLinus Torvalds for (i = 0; i < disks; i++) { 13561da177e4SLinus Torvalds struct bio *mbio; 13571da177e4SLinus Torvalds if (!r1_bio->bios[i]) 13581da177e4SLinus Torvalds continue; 13591da177e4SLinus Torvalds 1360a167f663SNeilBrown mbio = bio_clone_mddev(bio, GFP_NOIO, mddev); 13614f024f37SKent Overstreet bio_trim(mbio, r1_bio->sector - bio->bi_iter.bi_sector, max_sectors); 13621da177e4SLinus Torvalds 13631f68f0c4SNeilBrown if (first_clone) { 13641f68f0c4SNeilBrown /* do behind I/O ? 13651f68f0c4SNeilBrown * Not if there are too many, or cannot 13661f68f0c4SNeilBrown * allocate memory, or a reader on WriteMostly 13671f68f0c4SNeilBrown * is waiting for behind writes to flush */ 13681f68f0c4SNeilBrown if (bitmap && 13691f68f0c4SNeilBrown (atomic_read(&bitmap->behind_writes) 13701f68f0c4SNeilBrown < mddev->bitmap_info.max_write_behind) && 13711f68f0c4SNeilBrown !waitqueue_active(&bitmap->behind_wait)) 13721f68f0c4SNeilBrown alloc_behind_pages(mbio, r1_bio); 13731da177e4SLinus Torvalds 13741f68f0c4SNeilBrown bitmap_startwrite(bitmap, r1_bio->sector, 13751f68f0c4SNeilBrown r1_bio->sectors, 13761f68f0c4SNeilBrown test_bit(R1BIO_BehindIO, 13771f68f0c4SNeilBrown &r1_bio->state)); 13781f68f0c4SNeilBrown first_clone = 0; 13791f68f0c4SNeilBrown } 13802ca68f5eSNeilBrown if (r1_bio->behind_bvecs) { 13814b6d287fSNeilBrown struct bio_vec *bvec; 13824b6d287fSNeilBrown int j; 13834b6d287fSNeilBrown 1384cb34e057SKent Overstreet /* 1385cb34e057SKent Overstreet * We trimmed the bio, so _all is legit 13864b6d287fSNeilBrown */ 1387d74c6d51SKent Overstreet bio_for_each_segment_all(bvec, mbio, j) 13882ca68f5eSNeilBrown bvec->bv_page = r1_bio->behind_bvecs[j].bv_page; 13894b6d287fSNeilBrown if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags)) 13904b6d287fSNeilBrown atomic_inc(&r1_bio->behind_remaining); 13914b6d287fSNeilBrown } 13924b6d287fSNeilBrown 13931f68f0c4SNeilBrown r1_bio->bios[i] = mbio; 13941f68f0c4SNeilBrown 13954f024f37SKent Overstreet mbio->bi_iter.bi_sector = (r1_bio->sector + 13961f68f0c4SNeilBrown conf->mirrors[i].rdev->data_offset); 13971f68f0c4SNeilBrown mbio->bi_bdev = conf->mirrors[i].rdev->bdev; 13981f68f0c4SNeilBrown mbio->bi_end_io = raid1_end_write_request; 1399c8dc9c65SJoe Lawrence mbio->bi_rw = 1400c8dc9c65SJoe Lawrence WRITE | do_flush_fua | do_sync | do_discard | do_same; 14011f68f0c4SNeilBrown mbio->bi_private = r1_bio; 14021f68f0c4SNeilBrown 14031da177e4SLinus Torvalds atomic_inc(&r1_bio->remaining); 1404f54a9d0eSNeilBrown 1405f54a9d0eSNeilBrown cb = blk_check_plugged(raid1_unplug, mddev, sizeof(*plug)); 1406f54a9d0eSNeilBrown if (cb) 1407f54a9d0eSNeilBrown plug = container_of(cb, struct raid1_plug_cb, cb); 1408f54a9d0eSNeilBrown else 1409f54a9d0eSNeilBrown plug = NULL; 1410191ea9b2SNeilBrown spin_lock_irqsave(&conf->device_lock, flags); 1411f54a9d0eSNeilBrown if (plug) { 1412f54a9d0eSNeilBrown bio_list_add(&plug->pending, mbio); 1413f54a9d0eSNeilBrown plug->pending_cnt++; 1414f54a9d0eSNeilBrown } else { 14154e78064fSNeilBrown bio_list_add(&conf->pending_bio_list, mbio); 141634db0cd6SNeilBrown conf->pending_count++; 1417f54a9d0eSNeilBrown } 1418191ea9b2SNeilBrown spin_unlock_irqrestore(&conf->device_lock, flags); 1419f54a9d0eSNeilBrown if (!plug) 1420b357f04aSNeilBrown md_wakeup_thread(mddev->thread); 14214e78064fSNeilBrown } 1422079fa166SNeilBrown /* Mustn't call r1_bio_write_done before this next test, 1423079fa166SNeilBrown * as it could result in the bio being freed. 1424079fa166SNeilBrown */ 1425aa8b57aaSKent Overstreet if (sectors_handled < bio_sectors(bio)) { 1426079fa166SNeilBrown r1_bio_write_done(r1_bio); 14271f68f0c4SNeilBrown /* We need another r1_bio. It has already been counted 14281f68f0c4SNeilBrown * in bio->bi_phys_segments 14291f68f0c4SNeilBrown */ 14301f68f0c4SNeilBrown r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO); 14311f68f0c4SNeilBrown r1_bio->master_bio = bio; 1432aa8b57aaSKent Overstreet r1_bio->sectors = bio_sectors(bio) - sectors_handled; 14331f68f0c4SNeilBrown r1_bio->state = 0; 14341f68f0c4SNeilBrown r1_bio->mddev = mddev; 14354f024f37SKent Overstreet r1_bio->sector = bio->bi_iter.bi_sector + sectors_handled; 14361f68f0c4SNeilBrown goto retry_write; 14371f68f0c4SNeilBrown } 14381f68f0c4SNeilBrown 1439079fa166SNeilBrown r1_bio_write_done(r1_bio); 1440079fa166SNeilBrown 1441079fa166SNeilBrown /* In case raid1d snuck in to freeze_array */ 1442079fa166SNeilBrown wake_up(&conf->wait_barrier); 14431da177e4SLinus Torvalds } 14441da177e4SLinus Torvalds 1445fd01b88cSNeilBrown static void status(struct seq_file *seq, struct mddev *mddev) 14461da177e4SLinus Torvalds { 1447e8096360SNeilBrown struct r1conf *conf = mddev->private; 14481da177e4SLinus Torvalds int i; 14491da177e4SLinus Torvalds 14501da177e4SLinus Torvalds seq_printf(seq, " [%d/%d] [", conf->raid_disks, 145111ce99e6SNeilBrown conf->raid_disks - mddev->degraded); 1452ddac7c7eSNeilBrown rcu_read_lock(); 1453ddac7c7eSNeilBrown for (i = 0; i < conf->raid_disks; i++) { 14543cb03002SNeilBrown struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); 14551da177e4SLinus Torvalds seq_printf(seq, "%s", 1456ddac7c7eSNeilBrown rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_"); 1457ddac7c7eSNeilBrown } 1458ddac7c7eSNeilBrown rcu_read_unlock(); 14591da177e4SLinus Torvalds seq_printf(seq, "]"); 14601da177e4SLinus Torvalds } 14611da177e4SLinus Torvalds 1462fd01b88cSNeilBrown static void error(struct mddev *mddev, struct md_rdev *rdev) 14631da177e4SLinus Torvalds { 14641da177e4SLinus Torvalds char b[BDEVNAME_SIZE]; 1465e8096360SNeilBrown struct r1conf *conf = mddev->private; 14661da177e4SLinus Torvalds 14671da177e4SLinus Torvalds /* 14681da177e4SLinus Torvalds * If it is not operational, then we have already marked it as dead 14691da177e4SLinus Torvalds * else if it is the last working disks, ignore the error, let the 14701da177e4SLinus Torvalds * next level up know. 14711da177e4SLinus Torvalds * else mark the drive as failed 14721da177e4SLinus Torvalds */ 1473b2d444d7SNeilBrown if (test_bit(In_sync, &rdev->flags) 14744044ba58SNeilBrown && (conf->raid_disks - mddev->degraded) == 1) { 14751da177e4SLinus Torvalds /* 14761da177e4SLinus Torvalds * Don't fail the drive, act as though we were just a 14774044ba58SNeilBrown * normal single drive. 14784044ba58SNeilBrown * However don't try a recovery from this drive as 14794044ba58SNeilBrown * it is very likely to fail. 14801da177e4SLinus Torvalds */ 14815389042fSNeilBrown conf->recovery_disabled = mddev->recovery_disabled; 14821da177e4SLinus Torvalds return; 14834044ba58SNeilBrown } 1484de393cdeSNeilBrown set_bit(Blocked, &rdev->flags); 1485c04be0aaSNeilBrown if (test_and_clear_bit(In_sync, &rdev->flags)) { 1486c04be0aaSNeilBrown unsigned long flags; 1487c04be0aaSNeilBrown spin_lock_irqsave(&conf->device_lock, flags); 14881da177e4SLinus Torvalds mddev->degraded++; 1489dd00a99eSNeilBrown set_bit(Faulty, &rdev->flags); 1490c04be0aaSNeilBrown spin_unlock_irqrestore(&conf->device_lock, flags); 14912446dba0SNeilBrown } else 14922446dba0SNeilBrown set_bit(Faulty, &rdev->flags); 14931da177e4SLinus Torvalds /* 14941da177e4SLinus Torvalds * if recovery is running, make sure it aborts. 14951da177e4SLinus Torvalds */ 1496dfc70645SNeilBrown set_bit(MD_RECOVERY_INTR, &mddev->recovery); 1497850b2b42SNeilBrown set_bit(MD_CHANGE_DEVS, &mddev->flags); 1498067032bcSJoe Perches printk(KERN_ALERT 1499067032bcSJoe Perches "md/raid1:%s: Disk failure on %s, disabling device.\n" 1500067032bcSJoe Perches "md/raid1:%s: Operation continuing on %d devices.\n", 15019dd1e2faSNeilBrown mdname(mddev), bdevname(rdev->bdev, b), 15029dd1e2faSNeilBrown mdname(mddev), conf->raid_disks - mddev->degraded); 15031da177e4SLinus Torvalds } 15041da177e4SLinus Torvalds 1505e8096360SNeilBrown static void print_conf(struct r1conf *conf) 15061da177e4SLinus Torvalds { 15071da177e4SLinus Torvalds int i; 15081da177e4SLinus Torvalds 15099dd1e2faSNeilBrown printk(KERN_DEBUG "RAID1 conf printout:\n"); 15101da177e4SLinus Torvalds if (!conf) { 15119dd1e2faSNeilBrown printk(KERN_DEBUG "(!conf)\n"); 15121da177e4SLinus Torvalds return; 15131da177e4SLinus Torvalds } 15149dd1e2faSNeilBrown printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded, 15151da177e4SLinus Torvalds conf->raid_disks); 15161da177e4SLinus Torvalds 1517ddac7c7eSNeilBrown rcu_read_lock(); 15181da177e4SLinus Torvalds for (i = 0; i < conf->raid_disks; i++) { 15191da177e4SLinus Torvalds char b[BDEVNAME_SIZE]; 15203cb03002SNeilBrown struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); 1521ddac7c7eSNeilBrown if (rdev) 15229dd1e2faSNeilBrown printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n", 1523ddac7c7eSNeilBrown i, !test_bit(In_sync, &rdev->flags), 1524ddac7c7eSNeilBrown !test_bit(Faulty, &rdev->flags), 1525ddac7c7eSNeilBrown bdevname(rdev->bdev,b)); 15261da177e4SLinus Torvalds } 1527ddac7c7eSNeilBrown rcu_read_unlock(); 15281da177e4SLinus Torvalds } 15291da177e4SLinus Torvalds 1530e8096360SNeilBrown static void close_sync(struct r1conf *conf) 15311da177e4SLinus Torvalds { 153279ef3a8aSmajianpeng wait_barrier(conf, NULL); 153379ef3a8aSmajianpeng allow_barrier(conf, 0, 0); 15341da177e4SLinus Torvalds 15351da177e4SLinus Torvalds mempool_destroy(conf->r1buf_pool); 15361da177e4SLinus Torvalds conf->r1buf_pool = NULL; 153779ef3a8aSmajianpeng 1538669cc7baSNeilBrown spin_lock_irq(&conf->resync_lock); 153979ef3a8aSmajianpeng conf->next_resync = 0; 154079ef3a8aSmajianpeng conf->start_next_window = MaxSector; 1541669cc7baSNeilBrown conf->current_window_requests += 1542669cc7baSNeilBrown conf->next_window_requests; 1543669cc7baSNeilBrown conf->next_window_requests = 0; 1544669cc7baSNeilBrown spin_unlock_irq(&conf->resync_lock); 15451da177e4SLinus Torvalds } 15461da177e4SLinus Torvalds 1547fd01b88cSNeilBrown static int raid1_spare_active(struct mddev *mddev) 15481da177e4SLinus Torvalds { 15491da177e4SLinus Torvalds int i; 1550e8096360SNeilBrown struct r1conf *conf = mddev->private; 15516b965620SNeilBrown int count = 0; 15526b965620SNeilBrown unsigned long flags; 15531da177e4SLinus Torvalds 15541da177e4SLinus Torvalds /* 15551da177e4SLinus Torvalds * Find all failed disks within the RAID1 configuration 1556ddac7c7eSNeilBrown * and mark them readable. 1557ddac7c7eSNeilBrown * Called under mddev lock, so rcu protection not needed. 15581da177e4SLinus Torvalds */ 15591da177e4SLinus Torvalds for (i = 0; i < conf->raid_disks; i++) { 15603cb03002SNeilBrown struct md_rdev *rdev = conf->mirrors[i].rdev; 15618c7a2c2bSNeilBrown struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev; 15628c7a2c2bSNeilBrown if (repl 15638c7a2c2bSNeilBrown && repl->recovery_offset == MaxSector 15648c7a2c2bSNeilBrown && !test_bit(Faulty, &repl->flags) 15658c7a2c2bSNeilBrown && !test_and_set_bit(In_sync, &repl->flags)) { 15668c7a2c2bSNeilBrown /* replacement has just become active */ 15678c7a2c2bSNeilBrown if (!rdev || 15688c7a2c2bSNeilBrown !test_and_clear_bit(In_sync, &rdev->flags)) 15698c7a2c2bSNeilBrown count++; 15708c7a2c2bSNeilBrown if (rdev) { 15718c7a2c2bSNeilBrown /* Replaced device not technically 15728c7a2c2bSNeilBrown * faulty, but we need to be sure 15738c7a2c2bSNeilBrown * it gets removed and never re-added 15748c7a2c2bSNeilBrown */ 15758c7a2c2bSNeilBrown set_bit(Faulty, &rdev->flags); 15768c7a2c2bSNeilBrown sysfs_notify_dirent_safe( 15778c7a2c2bSNeilBrown rdev->sysfs_state); 15788c7a2c2bSNeilBrown } 15798c7a2c2bSNeilBrown } 1580ddac7c7eSNeilBrown if (rdev 158161e4947cSLukasz Dorau && rdev->recovery_offset == MaxSector 1582ddac7c7eSNeilBrown && !test_bit(Faulty, &rdev->flags) 1583c04be0aaSNeilBrown && !test_and_set_bit(In_sync, &rdev->flags)) { 15846b965620SNeilBrown count++; 1585654e8b5aSJonathan Brassow sysfs_notify_dirent_safe(rdev->sysfs_state); 15861da177e4SLinus Torvalds } 15871da177e4SLinus Torvalds } 15886b965620SNeilBrown spin_lock_irqsave(&conf->device_lock, flags); 15896b965620SNeilBrown mddev->degraded -= count; 15906b965620SNeilBrown spin_unlock_irqrestore(&conf->device_lock, flags); 15911da177e4SLinus Torvalds 15921da177e4SLinus Torvalds print_conf(conf); 15936b965620SNeilBrown return count; 15941da177e4SLinus Torvalds } 15951da177e4SLinus Torvalds 1596fd01b88cSNeilBrown static int raid1_add_disk(struct mddev *mddev, struct md_rdev *rdev) 15971da177e4SLinus Torvalds { 1598e8096360SNeilBrown struct r1conf *conf = mddev->private; 1599199050eaSNeil Brown int err = -EEXIST; 160041158c7eSNeilBrown int mirror = 0; 16010eaf822cSJonathan Brassow struct raid1_info *p; 16026c2fce2eSNeil Brown int first = 0; 160330194636SNeilBrown int last = conf->raid_disks - 1; 16046b740b8dSNeilBrown struct request_queue *q = bdev_get_queue(rdev->bdev); 16051da177e4SLinus Torvalds 16065389042fSNeilBrown if (mddev->recovery_disabled == conf->recovery_disabled) 16075389042fSNeilBrown return -EBUSY; 16085389042fSNeilBrown 16096c2fce2eSNeil Brown if (rdev->raid_disk >= 0) 16106c2fce2eSNeil Brown first = last = rdev->raid_disk; 16116c2fce2eSNeil Brown 16126b740b8dSNeilBrown if (q->merge_bvec_fn) { 16136b740b8dSNeilBrown set_bit(Unmerged, &rdev->flags); 16146b740b8dSNeilBrown mddev->merge_check_needed = 1; 16156b740b8dSNeilBrown } 16166b740b8dSNeilBrown 16177ef449d1SNeilBrown for (mirror = first; mirror <= last; mirror++) { 16187ef449d1SNeilBrown p = conf->mirrors+mirror; 16197ef449d1SNeilBrown if (!p->rdev) { 16201da177e4SLinus Torvalds 16219092c02dSJonathan Brassow if (mddev->gendisk) 16228f6c2e4bSMartin K. Petersen disk_stack_limits(mddev->gendisk, rdev->bdev, 16238f6c2e4bSMartin K. Petersen rdev->data_offset << 9); 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds p->head_position = 0; 16261da177e4SLinus Torvalds rdev->raid_disk = mirror; 1627199050eaSNeil Brown err = 0; 16286aea114aSNeilBrown /* As all devices are equivalent, we don't need a full recovery 16296aea114aSNeilBrown * if this was recently any drive of the array 16306aea114aSNeilBrown */ 16316aea114aSNeilBrown if (rdev->saved_raid_disk < 0) 163241158c7eSNeilBrown conf->fullsync = 1; 1633d6065f7bSSuzanne Wood rcu_assign_pointer(p->rdev, rdev); 16341da177e4SLinus Torvalds break; 16351da177e4SLinus Torvalds } 16367ef449d1SNeilBrown if (test_bit(WantReplacement, &p->rdev->flags) && 16377ef449d1SNeilBrown p[conf->raid_disks].rdev == NULL) { 16387ef449d1SNeilBrown /* Add this device as a replacement */ 16397ef449d1SNeilBrown clear_bit(In_sync, &rdev->flags); 16407ef449d1SNeilBrown set_bit(Replacement, &rdev->flags); 16417ef449d1SNeilBrown rdev->raid_disk = mirror; 16427ef449d1SNeilBrown err = 0; 16437ef449d1SNeilBrown conf->fullsync = 1; 16447ef449d1SNeilBrown rcu_assign_pointer(p[conf->raid_disks].rdev, rdev); 16457ef449d1SNeilBrown break; 16467ef449d1SNeilBrown } 16477ef449d1SNeilBrown } 16486b740b8dSNeilBrown if (err == 0 && test_bit(Unmerged, &rdev->flags)) { 16496b740b8dSNeilBrown /* Some requests might not have seen this new 16506b740b8dSNeilBrown * merge_bvec_fn. We must wait for them to complete 16516b740b8dSNeilBrown * before merging the device fully. 16526b740b8dSNeilBrown * First we make sure any code which has tested 16536b740b8dSNeilBrown * our function has submitted the request, then 16546b740b8dSNeilBrown * we wait for all outstanding requests to complete. 16556b740b8dSNeilBrown */ 16566b740b8dSNeilBrown synchronize_sched(); 1657e2d59925SNeilBrown freeze_array(conf, 0); 1658e2d59925SNeilBrown unfreeze_array(conf); 16596b740b8dSNeilBrown clear_bit(Unmerged, &rdev->flags); 16606b740b8dSNeilBrown } 1661ac5e7113SAndre Noll md_integrity_add_rdev(rdev, mddev); 16629092c02dSJonathan Brassow if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev))) 16632ff8cc2cSShaohua Li queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue); 16641da177e4SLinus Torvalds print_conf(conf); 1665199050eaSNeil Brown return err; 16661da177e4SLinus Torvalds } 16671da177e4SLinus Torvalds 1668b8321b68SNeilBrown static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev) 16691da177e4SLinus Torvalds { 1670e8096360SNeilBrown struct r1conf *conf = mddev->private; 16711da177e4SLinus Torvalds int err = 0; 1672b8321b68SNeilBrown int number = rdev->raid_disk; 16730eaf822cSJonathan Brassow struct raid1_info *p = conf->mirrors + number; 16741da177e4SLinus Torvalds 1675b014f14cSNeilBrown if (rdev != p->rdev) 1676b014f14cSNeilBrown p = conf->mirrors + conf->raid_disks + number; 1677b014f14cSNeilBrown 16781da177e4SLinus Torvalds print_conf(conf); 1679b8321b68SNeilBrown if (rdev == p->rdev) { 1680b2d444d7SNeilBrown if (test_bit(In_sync, &rdev->flags) || 16811da177e4SLinus Torvalds atomic_read(&rdev->nr_pending)) { 16821da177e4SLinus Torvalds err = -EBUSY; 16831da177e4SLinus Torvalds goto abort; 16841da177e4SLinus Torvalds } 1685046abeedSNeilBrown /* Only remove non-faulty devices if recovery 1686dfc70645SNeilBrown * is not possible. 1687dfc70645SNeilBrown */ 1688dfc70645SNeilBrown if (!test_bit(Faulty, &rdev->flags) && 16895389042fSNeilBrown mddev->recovery_disabled != conf->recovery_disabled && 1690dfc70645SNeilBrown mddev->degraded < conf->raid_disks) { 1691dfc70645SNeilBrown err = -EBUSY; 1692dfc70645SNeilBrown goto abort; 1693dfc70645SNeilBrown } 16941da177e4SLinus Torvalds p->rdev = NULL; 1695fbd568a3SPaul E. McKenney synchronize_rcu(); 16961da177e4SLinus Torvalds if (atomic_read(&rdev->nr_pending)) { 16971da177e4SLinus Torvalds /* lost the race, try later */ 16981da177e4SLinus Torvalds err = -EBUSY; 16991da177e4SLinus Torvalds p->rdev = rdev; 1700ac5e7113SAndre Noll goto abort; 17018c7a2c2bSNeilBrown } else if (conf->mirrors[conf->raid_disks + number].rdev) { 17028c7a2c2bSNeilBrown /* We just removed a device that is being replaced. 17038c7a2c2bSNeilBrown * Move down the replacement. We drain all IO before 17048c7a2c2bSNeilBrown * doing this to avoid confusion. 17058c7a2c2bSNeilBrown */ 17068c7a2c2bSNeilBrown struct md_rdev *repl = 17078c7a2c2bSNeilBrown conf->mirrors[conf->raid_disks + number].rdev; 1708e2d59925SNeilBrown freeze_array(conf, 0); 17098c7a2c2bSNeilBrown clear_bit(Replacement, &repl->flags); 17108c7a2c2bSNeilBrown p->rdev = repl; 17118c7a2c2bSNeilBrown conf->mirrors[conf->raid_disks + number].rdev = NULL; 1712e2d59925SNeilBrown unfreeze_array(conf); 1713b014f14cSNeilBrown clear_bit(WantReplacement, &rdev->flags); 17148c7a2c2bSNeilBrown } else 17158c7a2c2bSNeilBrown clear_bit(WantReplacement, &rdev->flags); 1716a91a2785SMartin K. Petersen err = md_integrity_register(mddev); 17171da177e4SLinus Torvalds } 17181da177e4SLinus Torvalds abort: 17191da177e4SLinus Torvalds 17201da177e4SLinus Torvalds print_conf(conf); 17211da177e4SLinus Torvalds return err; 17221da177e4SLinus Torvalds } 17231da177e4SLinus Torvalds 17246712ecf8SNeilBrown static void end_sync_read(struct bio *bio, int error) 17251da177e4SLinus Torvalds { 17269f2c9d12SNeilBrown struct r1bio *r1_bio = bio->bi_private; 17271da177e4SLinus Torvalds 17280fc280f6SNeilBrown update_head_pos(r1_bio->read_disk, r1_bio); 1729ba3ae3beSNamhyung Kim 17301da177e4SLinus Torvalds /* 17311da177e4SLinus Torvalds * we have read a block, now it needs to be re-written, 17321da177e4SLinus Torvalds * or re-read if the read failed. 17331da177e4SLinus Torvalds * We don't do much here, just schedule handling by raid1d 17341da177e4SLinus Torvalds */ 173569382e85SNeilBrown if (test_bit(BIO_UPTODATE, &bio->bi_flags)) 17361da177e4SLinus Torvalds set_bit(R1BIO_Uptodate, &r1_bio->state); 1737d11c171eSNeilBrown 1738d11c171eSNeilBrown if (atomic_dec_and_test(&r1_bio->remaining)) 17391da177e4SLinus Torvalds reschedule_retry(r1_bio); 17401da177e4SLinus Torvalds } 17411da177e4SLinus Torvalds 17426712ecf8SNeilBrown static void end_sync_write(struct bio *bio, int error) 17431da177e4SLinus Torvalds { 17441da177e4SLinus Torvalds int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); 17459f2c9d12SNeilBrown struct r1bio *r1_bio = bio->bi_private; 1746fd01b88cSNeilBrown struct mddev *mddev = r1_bio->mddev; 1747e8096360SNeilBrown struct r1conf *conf = mddev->private; 17481da177e4SLinus Torvalds int mirror=0; 17494367af55SNeilBrown sector_t first_bad; 17504367af55SNeilBrown int bad_sectors; 17511da177e4SLinus Torvalds 1752ba3ae3beSNamhyung Kim mirror = find_bio_disk(r1_bio, bio); 1753ba3ae3beSNamhyung Kim 17546b1117d5SNeilBrown if (!uptodate) { 175557dab0bdSNeilBrown sector_t sync_blocks = 0; 17566b1117d5SNeilBrown sector_t s = r1_bio->sector; 17576b1117d5SNeilBrown long sectors_to_go = r1_bio->sectors; 17586b1117d5SNeilBrown /* make sure these bits doesn't get cleared. */ 17596b1117d5SNeilBrown do { 17605e3db645SNeilBrown bitmap_end_sync(mddev->bitmap, s, 17616b1117d5SNeilBrown &sync_blocks, 1); 17626b1117d5SNeilBrown s += sync_blocks; 17636b1117d5SNeilBrown sectors_to_go -= sync_blocks; 17646b1117d5SNeilBrown } while (sectors_to_go > 0); 1765d8f05d29SNeilBrown set_bit(WriteErrorSeen, 1766d8f05d29SNeilBrown &conf->mirrors[mirror].rdev->flags); 176719d67169SNeilBrown if (!test_and_set_bit(WantReplacement, 176819d67169SNeilBrown &conf->mirrors[mirror].rdev->flags)) 176919d67169SNeilBrown set_bit(MD_RECOVERY_NEEDED, & 177019d67169SNeilBrown mddev->recovery); 1771d8f05d29SNeilBrown set_bit(R1BIO_WriteError, &r1_bio->state); 17724367af55SNeilBrown } else if (is_badblock(conf->mirrors[mirror].rdev, 17734367af55SNeilBrown r1_bio->sector, 17744367af55SNeilBrown r1_bio->sectors, 17753a9f28a5SNeilBrown &first_bad, &bad_sectors) && 17763a9f28a5SNeilBrown !is_badblock(conf->mirrors[r1_bio->read_disk].rdev, 17773a9f28a5SNeilBrown r1_bio->sector, 17783a9f28a5SNeilBrown r1_bio->sectors, 17793a9f28a5SNeilBrown &first_bad, &bad_sectors) 17803a9f28a5SNeilBrown ) 17814367af55SNeilBrown set_bit(R1BIO_MadeGood, &r1_bio->state); 1782e3b9703eSNeilBrown 17831da177e4SLinus Torvalds if (atomic_dec_and_test(&r1_bio->remaining)) { 17844367af55SNeilBrown int s = r1_bio->sectors; 1785d8f05d29SNeilBrown if (test_bit(R1BIO_MadeGood, &r1_bio->state) || 1786d8f05d29SNeilBrown test_bit(R1BIO_WriteError, &r1_bio->state)) 17874367af55SNeilBrown reschedule_retry(r1_bio); 17884367af55SNeilBrown else { 17891da177e4SLinus Torvalds put_buf(r1_bio); 179073d5c38aSNeilBrown md_done_sync(mddev, s, uptodate); 17911da177e4SLinus Torvalds } 17921da177e4SLinus Torvalds } 17934367af55SNeilBrown } 17941da177e4SLinus Torvalds 17953cb03002SNeilBrown static int r1_sync_page_io(struct md_rdev *rdev, sector_t sector, 1796d8f05d29SNeilBrown int sectors, struct page *page, int rw) 1797d8f05d29SNeilBrown { 1798d8f05d29SNeilBrown if (sync_page_io(rdev, sector, sectors << 9, page, rw, false)) 1799d8f05d29SNeilBrown /* success */ 1800d8f05d29SNeilBrown return 1; 180119d67169SNeilBrown if (rw == WRITE) { 1802d8f05d29SNeilBrown set_bit(WriteErrorSeen, &rdev->flags); 180319d67169SNeilBrown if (!test_and_set_bit(WantReplacement, 180419d67169SNeilBrown &rdev->flags)) 180519d67169SNeilBrown set_bit(MD_RECOVERY_NEEDED, & 180619d67169SNeilBrown rdev->mddev->recovery); 180719d67169SNeilBrown } 1808d8f05d29SNeilBrown /* need to record an error - either for the block or the device */ 1809d8f05d29SNeilBrown if (!rdev_set_badblocks(rdev, sector, sectors, 0)) 1810d8f05d29SNeilBrown md_error(rdev->mddev, rdev); 1811d8f05d29SNeilBrown return 0; 1812d8f05d29SNeilBrown } 1813d8f05d29SNeilBrown 18149f2c9d12SNeilBrown static int fix_sync_read_error(struct r1bio *r1_bio) 18151da177e4SLinus Torvalds { 1816a68e5870SNeilBrown /* Try some synchronous reads of other devices to get 181769382e85SNeilBrown * good data, much like with normal read errors. Only 1818ddac7c7eSNeilBrown * read into the pages we already have so we don't 181969382e85SNeilBrown * need to re-issue the read request. 182069382e85SNeilBrown * We don't need to freeze the array, because being in an 182169382e85SNeilBrown * active sync request, there is no normal IO, and 182269382e85SNeilBrown * no overlapping syncs. 182306f60385SNeilBrown * We don't need to check is_badblock() again as we 182406f60385SNeilBrown * made sure that anything with a bad block in range 182506f60385SNeilBrown * will have bi_end_io clear. 18261da177e4SLinus Torvalds */ 1827fd01b88cSNeilBrown struct mddev *mddev = r1_bio->mddev; 1828e8096360SNeilBrown struct r1conf *conf = mddev->private; 1829a68e5870SNeilBrown struct bio *bio = r1_bio->bios[r1_bio->read_disk]; 183069382e85SNeilBrown sector_t sect = r1_bio->sector; 183169382e85SNeilBrown int sectors = r1_bio->sectors; 183269382e85SNeilBrown int idx = 0; 183369382e85SNeilBrown 183469382e85SNeilBrown while(sectors) { 183569382e85SNeilBrown int s = sectors; 183669382e85SNeilBrown int d = r1_bio->read_disk; 183769382e85SNeilBrown int success = 0; 18383cb03002SNeilBrown struct md_rdev *rdev; 183978d7f5f7SNeilBrown int start; 184069382e85SNeilBrown 184169382e85SNeilBrown if (s > (PAGE_SIZE>>9)) 184269382e85SNeilBrown s = PAGE_SIZE >> 9; 184369382e85SNeilBrown do { 184469382e85SNeilBrown if (r1_bio->bios[d]->bi_end_io == end_sync_read) { 1845ddac7c7eSNeilBrown /* No rcu protection needed here devices 1846ddac7c7eSNeilBrown * can only be removed when no resync is 1847ddac7c7eSNeilBrown * active, and resync is currently active 1848ddac7c7eSNeilBrown */ 184969382e85SNeilBrown rdev = conf->mirrors[d].rdev; 18509d3d8011SNamhyung Kim if (sync_page_io(rdev, sect, s<<9, 185169382e85SNeilBrown bio->bi_io_vec[idx].bv_page, 1852ccebd4c4SJonathan Brassow READ, false)) { 185369382e85SNeilBrown success = 1; 185469382e85SNeilBrown break; 185569382e85SNeilBrown } 185669382e85SNeilBrown } 185769382e85SNeilBrown d++; 18588f19ccb2SNeilBrown if (d == conf->raid_disks * 2) 185969382e85SNeilBrown d = 0; 186069382e85SNeilBrown } while (!success && d != r1_bio->read_disk); 186169382e85SNeilBrown 186278d7f5f7SNeilBrown if (!success) { 186378d7f5f7SNeilBrown char b[BDEVNAME_SIZE]; 18643a9f28a5SNeilBrown int abort = 0; 18653a9f28a5SNeilBrown /* Cannot read from anywhere, this block is lost. 18663a9f28a5SNeilBrown * Record a bad block on each device. If that doesn't 18673a9f28a5SNeilBrown * work just disable and interrupt the recovery. 18683a9f28a5SNeilBrown * Don't fail devices as that won't really help. 18693a9f28a5SNeilBrown */ 187078d7f5f7SNeilBrown printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O read error" 187178d7f5f7SNeilBrown " for block %llu\n", 187278d7f5f7SNeilBrown mdname(mddev), 187378d7f5f7SNeilBrown bdevname(bio->bi_bdev, b), 187478d7f5f7SNeilBrown (unsigned long long)r1_bio->sector); 18758f19ccb2SNeilBrown for (d = 0; d < conf->raid_disks * 2; d++) { 18763a9f28a5SNeilBrown rdev = conf->mirrors[d].rdev; 18773a9f28a5SNeilBrown if (!rdev || test_bit(Faulty, &rdev->flags)) 18783a9f28a5SNeilBrown continue; 18793a9f28a5SNeilBrown if (!rdev_set_badblocks(rdev, sect, s, 0)) 18803a9f28a5SNeilBrown abort = 1; 18813a9f28a5SNeilBrown } 18823a9f28a5SNeilBrown if (abort) { 1883d890fa2bSNeilBrown conf->recovery_disabled = 1884d890fa2bSNeilBrown mddev->recovery_disabled; 18853a9f28a5SNeilBrown set_bit(MD_RECOVERY_INTR, &mddev->recovery); 188678d7f5f7SNeilBrown md_done_sync(mddev, r1_bio->sectors, 0); 188778d7f5f7SNeilBrown put_buf(r1_bio); 188878d7f5f7SNeilBrown return 0; 188978d7f5f7SNeilBrown } 18903a9f28a5SNeilBrown /* Try next page */ 18913a9f28a5SNeilBrown sectors -= s; 18923a9f28a5SNeilBrown sect += s; 18933a9f28a5SNeilBrown idx++; 18943a9f28a5SNeilBrown continue; 18953a9f28a5SNeilBrown } 189678d7f5f7SNeilBrown 189778d7f5f7SNeilBrown start = d; 189869382e85SNeilBrown /* write it back and re-read */ 189969382e85SNeilBrown while (d != r1_bio->read_disk) { 190069382e85SNeilBrown if (d == 0) 19018f19ccb2SNeilBrown d = conf->raid_disks * 2; 190269382e85SNeilBrown d--; 190369382e85SNeilBrown if (r1_bio->bios[d]->bi_end_io != end_sync_read) 190469382e85SNeilBrown continue; 190569382e85SNeilBrown rdev = conf->mirrors[d].rdev; 1906d8f05d29SNeilBrown if (r1_sync_page_io(rdev, sect, s, 190769382e85SNeilBrown bio->bi_io_vec[idx].bv_page, 1908d8f05d29SNeilBrown WRITE) == 0) { 190978d7f5f7SNeilBrown r1_bio->bios[d]->bi_end_io = NULL; 191078d7f5f7SNeilBrown rdev_dec_pending(rdev, mddev); 19119d3d8011SNamhyung Kim } 1912097426f6SNeilBrown } 1913097426f6SNeilBrown d = start; 1914097426f6SNeilBrown while (d != r1_bio->read_disk) { 1915097426f6SNeilBrown if (d == 0) 19168f19ccb2SNeilBrown d = conf->raid_disks * 2; 1917097426f6SNeilBrown d--; 1918097426f6SNeilBrown if (r1_bio->bios[d]->bi_end_io != end_sync_read) 1919097426f6SNeilBrown continue; 1920097426f6SNeilBrown rdev = conf->mirrors[d].rdev; 1921d8f05d29SNeilBrown if (r1_sync_page_io(rdev, sect, s, 192269382e85SNeilBrown bio->bi_io_vec[idx].bv_page, 1923d8f05d29SNeilBrown READ) != 0) 19249d3d8011SNamhyung Kim atomic_add(s, &rdev->corrected_errors); 192569382e85SNeilBrown } 192669382e85SNeilBrown sectors -= s; 192769382e85SNeilBrown sect += s; 192869382e85SNeilBrown idx ++; 192969382e85SNeilBrown } 193078d7f5f7SNeilBrown set_bit(R1BIO_Uptodate, &r1_bio->state); 19317ca78d57SNeilBrown set_bit(BIO_UPTODATE, &bio->bi_flags); 1932a68e5870SNeilBrown return 1; 193369382e85SNeilBrown } 1934d11c171eSNeilBrown 1935c95e6385SNeilBrown static void process_checks(struct r1bio *r1_bio) 1936a68e5870SNeilBrown { 1937a68e5870SNeilBrown /* We have read all readable devices. If we haven't 1938a68e5870SNeilBrown * got the block, then there is no hope left. 1939a68e5870SNeilBrown * If we have, then we want to do a comparison 1940a68e5870SNeilBrown * and skip the write if everything is the same. 1941a68e5870SNeilBrown * If any blocks failed to read, then we need to 1942a68e5870SNeilBrown * attempt an over-write 1943a68e5870SNeilBrown */ 1944fd01b88cSNeilBrown struct mddev *mddev = r1_bio->mddev; 1945e8096360SNeilBrown struct r1conf *conf = mddev->private; 1946a68e5870SNeilBrown int primary; 1947a68e5870SNeilBrown int i; 1948f4380a91Smajianpeng int vcnt; 1949a68e5870SNeilBrown 195030bc9b53SNeilBrown /* Fix variable parts of all bios */ 195130bc9b53SNeilBrown vcnt = (r1_bio->sectors + PAGE_SIZE / 512 - 1) >> (PAGE_SHIFT - 9); 195230bc9b53SNeilBrown for (i = 0; i < conf->raid_disks * 2; i++) { 195330bc9b53SNeilBrown int j; 195430bc9b53SNeilBrown int size; 19551877db75SNeilBrown int uptodate; 195630bc9b53SNeilBrown struct bio *b = r1_bio->bios[i]; 195730bc9b53SNeilBrown if (b->bi_end_io != end_sync_read) 195830bc9b53SNeilBrown continue; 19591877db75SNeilBrown /* fixup the bio for reuse, but preserve BIO_UPTODATE */ 19601877db75SNeilBrown uptodate = test_bit(BIO_UPTODATE, &b->bi_flags); 196130bc9b53SNeilBrown bio_reset(b); 19621877db75SNeilBrown if (!uptodate) 19631877db75SNeilBrown clear_bit(BIO_UPTODATE, &b->bi_flags); 196430bc9b53SNeilBrown b->bi_vcnt = vcnt; 19654f024f37SKent Overstreet b->bi_iter.bi_size = r1_bio->sectors << 9; 19664f024f37SKent Overstreet b->bi_iter.bi_sector = r1_bio->sector + 196730bc9b53SNeilBrown conf->mirrors[i].rdev->data_offset; 196830bc9b53SNeilBrown b->bi_bdev = conf->mirrors[i].rdev->bdev; 196930bc9b53SNeilBrown b->bi_end_io = end_sync_read; 197030bc9b53SNeilBrown b->bi_private = r1_bio; 197130bc9b53SNeilBrown 19724f024f37SKent Overstreet size = b->bi_iter.bi_size; 197330bc9b53SNeilBrown for (j = 0; j < vcnt ; j++) { 197430bc9b53SNeilBrown struct bio_vec *bi; 197530bc9b53SNeilBrown bi = &b->bi_io_vec[j]; 197630bc9b53SNeilBrown bi->bv_offset = 0; 197730bc9b53SNeilBrown if (size > PAGE_SIZE) 197830bc9b53SNeilBrown bi->bv_len = PAGE_SIZE; 197930bc9b53SNeilBrown else 198030bc9b53SNeilBrown bi->bv_len = size; 198130bc9b53SNeilBrown size -= PAGE_SIZE; 198230bc9b53SNeilBrown } 198330bc9b53SNeilBrown } 19848f19ccb2SNeilBrown for (primary = 0; primary < conf->raid_disks * 2; primary++) 1985a68e5870SNeilBrown if (r1_bio->bios[primary]->bi_end_io == end_sync_read && 1986a68e5870SNeilBrown test_bit(BIO_UPTODATE, &r1_bio->bios[primary]->bi_flags)) { 1987a68e5870SNeilBrown r1_bio->bios[primary]->bi_end_io = NULL; 1988a68e5870SNeilBrown rdev_dec_pending(conf->mirrors[primary].rdev, mddev); 1989a68e5870SNeilBrown break; 1990a68e5870SNeilBrown } 1991a68e5870SNeilBrown r1_bio->read_disk = primary; 19928f19ccb2SNeilBrown for (i = 0; i < conf->raid_disks * 2; i++) { 1993a68e5870SNeilBrown int j; 1994a68e5870SNeilBrown struct bio *pbio = r1_bio->bios[primary]; 1995a68e5870SNeilBrown struct bio *sbio = r1_bio->bios[i]; 19961877db75SNeilBrown int uptodate = test_bit(BIO_UPTODATE, &sbio->bi_flags); 199778d7f5f7SNeilBrown 19982aabaa65SKent Overstreet if (sbio->bi_end_io != end_sync_read) 199978d7f5f7SNeilBrown continue; 20001877db75SNeilBrown /* Now we can 'fixup' the BIO_UPTODATE flag */ 20011877db75SNeilBrown set_bit(BIO_UPTODATE, &sbio->bi_flags); 2002a68e5870SNeilBrown 20031877db75SNeilBrown if (uptodate) { 2004a68e5870SNeilBrown for (j = vcnt; j-- ; ) { 2005a68e5870SNeilBrown struct page *p, *s; 2006a68e5870SNeilBrown p = pbio->bi_io_vec[j].bv_page; 2007a68e5870SNeilBrown s = sbio->bi_io_vec[j].bv_page; 2008a68e5870SNeilBrown if (memcmp(page_address(p), 2009a68e5870SNeilBrown page_address(s), 20105020ad7dSNeilBrown sbio->bi_io_vec[j].bv_len)) 2011a68e5870SNeilBrown break; 2012a68e5870SNeilBrown } 2013a68e5870SNeilBrown } else 2014a68e5870SNeilBrown j = 0; 2015a68e5870SNeilBrown if (j >= 0) 20167f7583d4SJianpeng Ma atomic64_add(r1_bio->sectors, &mddev->resync_mismatches); 2017a68e5870SNeilBrown if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery) 20181877db75SNeilBrown && uptodate)) { 201978d7f5f7SNeilBrown /* No need to write to this device. */ 2020a68e5870SNeilBrown sbio->bi_end_io = NULL; 2021a68e5870SNeilBrown rdev_dec_pending(conf->mirrors[i].rdev, mddev); 202278d7f5f7SNeilBrown continue; 202378d7f5f7SNeilBrown } 2024d3b45c2aSKent Overstreet 2025d3b45c2aSKent Overstreet bio_copy_data(sbio, pbio); 2026a68e5870SNeilBrown } 2027a68e5870SNeilBrown } 2028a68e5870SNeilBrown 20299f2c9d12SNeilBrown static void sync_request_write(struct mddev *mddev, struct r1bio *r1_bio) 2030a68e5870SNeilBrown { 2031e8096360SNeilBrown struct r1conf *conf = mddev->private; 2032a68e5870SNeilBrown int i; 20338f19ccb2SNeilBrown int disks = conf->raid_disks * 2; 2034a68e5870SNeilBrown struct bio *bio, *wbio; 2035a68e5870SNeilBrown 2036a68e5870SNeilBrown bio = r1_bio->bios[r1_bio->read_disk]; 2037a68e5870SNeilBrown 2038a68e5870SNeilBrown if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) 2039a68e5870SNeilBrown /* ouch - failed to read all of that. */ 2040a68e5870SNeilBrown if (!fix_sync_read_error(r1_bio)) 2041a68e5870SNeilBrown return; 20427ca78d57SNeilBrown 20437ca78d57SNeilBrown if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) 2044c95e6385SNeilBrown process_checks(r1_bio); 2045c95e6385SNeilBrown 2046d11c171eSNeilBrown /* 2047d11c171eSNeilBrown * schedule writes 2048d11c171eSNeilBrown */ 20491da177e4SLinus Torvalds atomic_set(&r1_bio->remaining, 1); 20501da177e4SLinus Torvalds for (i = 0; i < disks ; i++) { 20511da177e4SLinus Torvalds wbio = r1_bio->bios[i]; 20523e198f78SNeilBrown if (wbio->bi_end_io == NULL || 20533e198f78SNeilBrown (wbio->bi_end_io == end_sync_read && 20543e198f78SNeilBrown (i == r1_bio->read_disk || 20553e198f78SNeilBrown !test_bit(MD_RECOVERY_SYNC, &mddev->recovery)))) 20561da177e4SLinus Torvalds continue; 20571da177e4SLinus Torvalds 20583e198f78SNeilBrown wbio->bi_rw = WRITE; 20593e198f78SNeilBrown wbio->bi_end_io = end_sync_write; 20601da177e4SLinus Torvalds atomic_inc(&r1_bio->remaining); 2061aa8b57aaSKent Overstreet md_sync_acct(conf->mirrors[i].rdev->bdev, bio_sectors(wbio)); 2062191ea9b2SNeilBrown 20631da177e4SLinus Torvalds generic_make_request(wbio); 20641da177e4SLinus Torvalds } 20651da177e4SLinus Torvalds 20661da177e4SLinus Torvalds if (atomic_dec_and_test(&r1_bio->remaining)) { 2067191ea9b2SNeilBrown /* if we're here, all write(s) have completed, so clean up */ 206858e94ae1SNeilBrown int s = r1_bio->sectors; 206958e94ae1SNeilBrown if (test_bit(R1BIO_MadeGood, &r1_bio->state) || 207058e94ae1SNeilBrown test_bit(R1BIO_WriteError, &r1_bio->state)) 207158e94ae1SNeilBrown reschedule_retry(r1_bio); 207258e94ae1SNeilBrown else { 20731da177e4SLinus Torvalds put_buf(r1_bio); 207458e94ae1SNeilBrown md_done_sync(mddev, s, 1); 207558e94ae1SNeilBrown } 20761da177e4SLinus Torvalds } 20771da177e4SLinus Torvalds } 20781da177e4SLinus Torvalds 20791da177e4SLinus Torvalds /* 20801da177e4SLinus Torvalds * This is a kernel thread which: 20811da177e4SLinus Torvalds * 20821da177e4SLinus Torvalds * 1. Retries failed read operations on working mirrors. 20831da177e4SLinus Torvalds * 2. Updates the raid superblock when problems encounter. 2084d2eb35acSNeilBrown * 3. Performs writes following reads for array synchronising. 20851da177e4SLinus Torvalds */ 20861da177e4SLinus Torvalds 2087e8096360SNeilBrown static void fix_read_error(struct r1conf *conf, int read_disk, 2088867868fbSNeilBrown sector_t sect, int sectors) 2089867868fbSNeilBrown { 2090fd01b88cSNeilBrown struct mddev *mddev = conf->mddev; 2091867868fbSNeilBrown while(sectors) { 2092867868fbSNeilBrown int s = sectors; 2093867868fbSNeilBrown int d = read_disk; 2094867868fbSNeilBrown int success = 0; 2095867868fbSNeilBrown int start; 20963cb03002SNeilBrown struct md_rdev *rdev; 2097867868fbSNeilBrown 2098867868fbSNeilBrown if (s > (PAGE_SIZE>>9)) 2099867868fbSNeilBrown s = PAGE_SIZE >> 9; 2100867868fbSNeilBrown 2101867868fbSNeilBrown do { 2102867868fbSNeilBrown /* Note: no rcu protection needed here 2103867868fbSNeilBrown * as this is synchronous in the raid1d thread 2104867868fbSNeilBrown * which is the thread that might remove 2105867868fbSNeilBrown * a device. If raid1d ever becomes multi-threaded.... 2106867868fbSNeilBrown */ 2107d2eb35acSNeilBrown sector_t first_bad; 2108d2eb35acSNeilBrown int bad_sectors; 2109d2eb35acSNeilBrown 2110867868fbSNeilBrown rdev = conf->mirrors[d].rdev; 2111867868fbSNeilBrown if (rdev && 2112da8840a7Smajianpeng (test_bit(In_sync, &rdev->flags) || 2113da8840a7Smajianpeng (!test_bit(Faulty, &rdev->flags) && 2114da8840a7Smajianpeng rdev->recovery_offset >= sect + s)) && 2115d2eb35acSNeilBrown is_badblock(rdev, sect, s, 2116d2eb35acSNeilBrown &first_bad, &bad_sectors) == 0 && 2117ccebd4c4SJonathan Brassow sync_page_io(rdev, sect, s<<9, 2118ccebd4c4SJonathan Brassow conf->tmppage, READ, false)) 2119867868fbSNeilBrown success = 1; 2120867868fbSNeilBrown else { 2121867868fbSNeilBrown d++; 21228f19ccb2SNeilBrown if (d == conf->raid_disks * 2) 2123867868fbSNeilBrown d = 0; 2124867868fbSNeilBrown } 2125867868fbSNeilBrown } while (!success && d != read_disk); 2126867868fbSNeilBrown 2127867868fbSNeilBrown if (!success) { 2128d8f05d29SNeilBrown /* Cannot read from anywhere - mark it bad */ 21293cb03002SNeilBrown struct md_rdev *rdev = conf->mirrors[read_disk].rdev; 2130d8f05d29SNeilBrown if (!rdev_set_badblocks(rdev, sect, s, 0)) 2131d8f05d29SNeilBrown md_error(mddev, rdev); 2132867868fbSNeilBrown break; 2133867868fbSNeilBrown } 2134867868fbSNeilBrown /* write it back and re-read */ 2135867868fbSNeilBrown start = d; 2136867868fbSNeilBrown while (d != read_disk) { 2137867868fbSNeilBrown if (d==0) 21388f19ccb2SNeilBrown d = conf->raid_disks * 2; 2139867868fbSNeilBrown d--; 2140867868fbSNeilBrown rdev = conf->mirrors[d].rdev; 2141867868fbSNeilBrown if (rdev && 2142b8cb6b4cSNeilBrown !test_bit(Faulty, &rdev->flags)) 2143d8f05d29SNeilBrown r1_sync_page_io(rdev, sect, s, 2144d8f05d29SNeilBrown conf->tmppage, WRITE); 2145867868fbSNeilBrown } 2146867868fbSNeilBrown d = start; 2147867868fbSNeilBrown while (d != read_disk) { 2148867868fbSNeilBrown char b[BDEVNAME_SIZE]; 2149867868fbSNeilBrown if (d==0) 21508f19ccb2SNeilBrown d = conf->raid_disks * 2; 2151867868fbSNeilBrown d--; 2152867868fbSNeilBrown rdev = conf->mirrors[d].rdev; 2153867868fbSNeilBrown if (rdev && 2154b8cb6b4cSNeilBrown !test_bit(Faulty, &rdev->flags)) { 2155d8f05d29SNeilBrown if (r1_sync_page_io(rdev, sect, s, 2156d8f05d29SNeilBrown conf->tmppage, READ)) { 2157867868fbSNeilBrown atomic_add(s, &rdev->corrected_errors); 2158867868fbSNeilBrown printk(KERN_INFO 21599dd1e2faSNeilBrown "md/raid1:%s: read error corrected " 2160867868fbSNeilBrown "(%d sectors at %llu on %s)\n", 2161867868fbSNeilBrown mdname(mddev), s, 2162969b755aSRandy Dunlap (unsigned long long)(sect + 2163969b755aSRandy Dunlap rdev->data_offset), 2164867868fbSNeilBrown bdevname(rdev->bdev, b)); 2165867868fbSNeilBrown } 2166867868fbSNeilBrown } 2167867868fbSNeilBrown } 2168867868fbSNeilBrown sectors -= s; 2169867868fbSNeilBrown sect += s; 2170867868fbSNeilBrown } 2171867868fbSNeilBrown } 2172867868fbSNeilBrown 21739f2c9d12SNeilBrown static int narrow_write_error(struct r1bio *r1_bio, int i) 2174cd5ff9a1SNeilBrown { 2175fd01b88cSNeilBrown struct mddev *mddev = r1_bio->mddev; 2176e8096360SNeilBrown struct r1conf *conf = mddev->private; 21773cb03002SNeilBrown struct md_rdev *rdev = conf->mirrors[i].rdev; 2178cd5ff9a1SNeilBrown 2179cd5ff9a1SNeilBrown /* bio has the data to be written to device 'i' where 2180cd5ff9a1SNeilBrown * we just recently had a write error. 2181cd5ff9a1SNeilBrown * We repeatedly clone the bio and trim down to one block, 2182cd5ff9a1SNeilBrown * then try the write. Where the write fails we record 2183cd5ff9a1SNeilBrown * a bad block. 2184cd5ff9a1SNeilBrown * It is conceivable that the bio doesn't exactly align with 2185cd5ff9a1SNeilBrown * blocks. We must handle this somehow. 2186cd5ff9a1SNeilBrown * 2187cd5ff9a1SNeilBrown * We currently own a reference on the rdev. 2188cd5ff9a1SNeilBrown */ 2189cd5ff9a1SNeilBrown 2190cd5ff9a1SNeilBrown int block_sectors; 2191cd5ff9a1SNeilBrown sector_t sector; 2192cd5ff9a1SNeilBrown int sectors; 2193cd5ff9a1SNeilBrown int sect_to_write = r1_bio->sectors; 2194cd5ff9a1SNeilBrown int ok = 1; 2195cd5ff9a1SNeilBrown 2196cd5ff9a1SNeilBrown if (rdev->badblocks.shift < 0) 2197cd5ff9a1SNeilBrown return 0; 2198cd5ff9a1SNeilBrown 2199cd5ff9a1SNeilBrown block_sectors = 1 << rdev->badblocks.shift; 2200cd5ff9a1SNeilBrown sector = r1_bio->sector; 2201cd5ff9a1SNeilBrown sectors = ((sector + block_sectors) 2202cd5ff9a1SNeilBrown & ~(sector_t)(block_sectors - 1)) 2203cd5ff9a1SNeilBrown - sector; 2204cd5ff9a1SNeilBrown 2205cd5ff9a1SNeilBrown while (sect_to_write) { 2206cd5ff9a1SNeilBrown struct bio *wbio; 2207cd5ff9a1SNeilBrown if (sectors > sect_to_write) 2208cd5ff9a1SNeilBrown sectors = sect_to_write; 2209cd5ff9a1SNeilBrown /* Write at 'sector' for 'sectors'*/ 2210cd5ff9a1SNeilBrown 2211b783863fSKent Overstreet if (test_bit(R1BIO_BehindIO, &r1_bio->state)) { 2212b783863fSKent Overstreet unsigned vcnt = r1_bio->behind_page_count; 2213b783863fSKent Overstreet struct bio_vec *vec = r1_bio->behind_bvecs; 2214b783863fSKent Overstreet 2215b783863fSKent Overstreet while (!vec->bv_page) { 2216b783863fSKent Overstreet vec++; 2217b783863fSKent Overstreet vcnt--; 2218b783863fSKent Overstreet } 2219b783863fSKent Overstreet 2220cd5ff9a1SNeilBrown wbio = bio_alloc_mddev(GFP_NOIO, vcnt, mddev); 2221cd5ff9a1SNeilBrown memcpy(wbio->bi_io_vec, vec, vcnt * sizeof(struct bio_vec)); 2222b783863fSKent Overstreet 2223cd5ff9a1SNeilBrown wbio->bi_vcnt = vcnt; 2224b783863fSKent Overstreet } else { 2225b783863fSKent Overstreet wbio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev); 2226b783863fSKent Overstreet } 2227b783863fSKent Overstreet 2228b783863fSKent Overstreet wbio->bi_rw = WRITE; 22294f024f37SKent Overstreet wbio->bi_iter.bi_sector = r1_bio->sector; 22304f024f37SKent Overstreet wbio->bi_iter.bi_size = r1_bio->sectors << 9; 2231cd5ff9a1SNeilBrown 22326678d83fSKent Overstreet bio_trim(wbio, sector - r1_bio->sector, sectors); 22334f024f37SKent Overstreet wbio->bi_iter.bi_sector += rdev->data_offset; 2234cd5ff9a1SNeilBrown wbio->bi_bdev = rdev->bdev; 2235cd5ff9a1SNeilBrown if (submit_bio_wait(WRITE, wbio) == 0) 2236cd5ff9a1SNeilBrown /* failure! */ 2237cd5ff9a1SNeilBrown ok = rdev_set_badblocks(rdev, sector, 2238cd5ff9a1SNeilBrown sectors, 0) 2239cd5ff9a1SNeilBrown && ok; 2240cd5ff9a1SNeilBrown 2241cd5ff9a1SNeilBrown bio_put(wbio); 2242cd5ff9a1SNeilBrown sect_to_write -= sectors; 2243cd5ff9a1SNeilBrown sector += sectors; 2244cd5ff9a1SNeilBrown sectors = block_sectors; 2245cd5ff9a1SNeilBrown } 2246cd5ff9a1SNeilBrown return ok; 2247cd5ff9a1SNeilBrown } 2248cd5ff9a1SNeilBrown 2249e8096360SNeilBrown static void handle_sync_write_finished(struct r1conf *conf, struct r1bio *r1_bio) 225062096bceSNeilBrown { 225162096bceSNeilBrown int m; 225262096bceSNeilBrown int s = r1_bio->sectors; 22538f19ccb2SNeilBrown for (m = 0; m < conf->raid_disks * 2 ; m++) { 22543cb03002SNeilBrown struct md_rdev *rdev = conf->mirrors[m].rdev; 225562096bceSNeilBrown struct bio *bio = r1_bio->bios[m]; 225662096bceSNeilBrown if (bio->bi_end_io == NULL) 225762096bceSNeilBrown continue; 225862096bceSNeilBrown if (test_bit(BIO_UPTODATE, &bio->bi_flags) && 225962096bceSNeilBrown test_bit(R1BIO_MadeGood, &r1_bio->state)) { 2260c6563a8cSNeilBrown rdev_clear_badblocks(rdev, r1_bio->sector, s, 0); 226162096bceSNeilBrown } 226262096bceSNeilBrown if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && 226362096bceSNeilBrown test_bit(R1BIO_WriteError, &r1_bio->state)) { 226462096bceSNeilBrown if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0)) 226562096bceSNeilBrown md_error(conf->mddev, rdev); 226662096bceSNeilBrown } 226762096bceSNeilBrown } 226862096bceSNeilBrown put_buf(r1_bio); 226962096bceSNeilBrown md_done_sync(conf->mddev, s, 1); 227062096bceSNeilBrown } 227162096bceSNeilBrown 2272e8096360SNeilBrown static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio) 227362096bceSNeilBrown { 227462096bceSNeilBrown int m; 22758f19ccb2SNeilBrown for (m = 0; m < conf->raid_disks * 2 ; m++) 227662096bceSNeilBrown if (r1_bio->bios[m] == IO_MADE_GOOD) { 22773cb03002SNeilBrown struct md_rdev *rdev = conf->mirrors[m].rdev; 227862096bceSNeilBrown rdev_clear_badblocks(rdev, 227962096bceSNeilBrown r1_bio->sector, 2280c6563a8cSNeilBrown r1_bio->sectors, 0); 228162096bceSNeilBrown rdev_dec_pending(rdev, conf->mddev); 228262096bceSNeilBrown } else if (r1_bio->bios[m] != NULL) { 228362096bceSNeilBrown /* This drive got a write error. We need to 228462096bceSNeilBrown * narrow down and record precise write 228562096bceSNeilBrown * errors. 228662096bceSNeilBrown */ 228762096bceSNeilBrown if (!narrow_write_error(r1_bio, m)) { 228862096bceSNeilBrown md_error(conf->mddev, 228962096bceSNeilBrown conf->mirrors[m].rdev); 229062096bceSNeilBrown /* an I/O failed, we can't clear the bitmap */ 229162096bceSNeilBrown set_bit(R1BIO_Degraded, &r1_bio->state); 229262096bceSNeilBrown } 229362096bceSNeilBrown rdev_dec_pending(conf->mirrors[m].rdev, 229462096bceSNeilBrown conf->mddev); 229562096bceSNeilBrown } 229662096bceSNeilBrown if (test_bit(R1BIO_WriteError, &r1_bio->state)) 229762096bceSNeilBrown close_write(r1_bio); 229862096bceSNeilBrown raid_end_bio_io(r1_bio); 229962096bceSNeilBrown } 230062096bceSNeilBrown 2301e8096360SNeilBrown static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio) 230262096bceSNeilBrown { 230362096bceSNeilBrown int disk; 230462096bceSNeilBrown int max_sectors; 2305fd01b88cSNeilBrown struct mddev *mddev = conf->mddev; 230662096bceSNeilBrown struct bio *bio; 230762096bceSNeilBrown char b[BDEVNAME_SIZE]; 23083cb03002SNeilBrown struct md_rdev *rdev; 230962096bceSNeilBrown 231062096bceSNeilBrown clear_bit(R1BIO_ReadError, &r1_bio->state); 231162096bceSNeilBrown /* we got a read error. Maybe the drive is bad. Maybe just 231262096bceSNeilBrown * the block and we can fix it. 231362096bceSNeilBrown * We freeze all other IO, and try reading the block from 231462096bceSNeilBrown * other devices. When we find one, we re-write 231562096bceSNeilBrown * and check it that fixes the read error. 231662096bceSNeilBrown * This is all done synchronously while the array is 231762096bceSNeilBrown * frozen 231862096bceSNeilBrown */ 231962096bceSNeilBrown if (mddev->ro == 0) { 2320e2d59925SNeilBrown freeze_array(conf, 1); 232162096bceSNeilBrown fix_read_error(conf, r1_bio->read_disk, 232262096bceSNeilBrown r1_bio->sector, r1_bio->sectors); 232362096bceSNeilBrown unfreeze_array(conf); 232462096bceSNeilBrown } else 232562096bceSNeilBrown md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev); 23267ad4d4a6SNeilBrown rdev_dec_pending(conf->mirrors[r1_bio->read_disk].rdev, conf->mddev); 232762096bceSNeilBrown 232862096bceSNeilBrown bio = r1_bio->bios[r1_bio->read_disk]; 232962096bceSNeilBrown bdevname(bio->bi_bdev, b); 233062096bceSNeilBrown read_more: 233162096bceSNeilBrown disk = read_balance(conf, r1_bio, &max_sectors); 233262096bceSNeilBrown if (disk == -1) { 233362096bceSNeilBrown printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O" 233462096bceSNeilBrown " read error for block %llu\n", 233562096bceSNeilBrown mdname(mddev), b, (unsigned long long)r1_bio->sector); 233662096bceSNeilBrown raid_end_bio_io(r1_bio); 233762096bceSNeilBrown } else { 233862096bceSNeilBrown const unsigned long do_sync 233962096bceSNeilBrown = r1_bio->master_bio->bi_rw & REQ_SYNC; 234062096bceSNeilBrown if (bio) { 234162096bceSNeilBrown r1_bio->bios[r1_bio->read_disk] = 234262096bceSNeilBrown mddev->ro ? IO_BLOCKED : NULL; 234362096bceSNeilBrown bio_put(bio); 234462096bceSNeilBrown } 234562096bceSNeilBrown r1_bio->read_disk = disk; 234662096bceSNeilBrown bio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev); 23474f024f37SKent Overstreet bio_trim(bio, r1_bio->sector - bio->bi_iter.bi_sector, 23484f024f37SKent Overstreet max_sectors); 234962096bceSNeilBrown r1_bio->bios[r1_bio->read_disk] = bio; 235062096bceSNeilBrown rdev = conf->mirrors[disk].rdev; 235162096bceSNeilBrown printk_ratelimited(KERN_ERR 235262096bceSNeilBrown "md/raid1:%s: redirecting sector %llu" 235362096bceSNeilBrown " to other mirror: %s\n", 235462096bceSNeilBrown mdname(mddev), 235562096bceSNeilBrown (unsigned long long)r1_bio->sector, 235662096bceSNeilBrown bdevname(rdev->bdev, b)); 23574f024f37SKent Overstreet bio->bi_iter.bi_sector = r1_bio->sector + rdev->data_offset; 235862096bceSNeilBrown bio->bi_bdev = rdev->bdev; 235962096bceSNeilBrown bio->bi_end_io = raid1_end_read_request; 236062096bceSNeilBrown bio->bi_rw = READ | do_sync; 236162096bceSNeilBrown bio->bi_private = r1_bio; 236262096bceSNeilBrown if (max_sectors < r1_bio->sectors) { 236362096bceSNeilBrown /* Drat - have to split this up more */ 236462096bceSNeilBrown struct bio *mbio = r1_bio->master_bio; 236562096bceSNeilBrown int sectors_handled = (r1_bio->sector + max_sectors 23664f024f37SKent Overstreet - mbio->bi_iter.bi_sector); 236762096bceSNeilBrown r1_bio->sectors = max_sectors; 236862096bceSNeilBrown spin_lock_irq(&conf->device_lock); 236962096bceSNeilBrown if (mbio->bi_phys_segments == 0) 237062096bceSNeilBrown mbio->bi_phys_segments = 2; 237162096bceSNeilBrown else 237262096bceSNeilBrown mbio->bi_phys_segments++; 237362096bceSNeilBrown spin_unlock_irq(&conf->device_lock); 237462096bceSNeilBrown generic_make_request(bio); 237562096bceSNeilBrown bio = NULL; 237662096bceSNeilBrown 237762096bceSNeilBrown r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO); 237862096bceSNeilBrown 237962096bceSNeilBrown r1_bio->master_bio = mbio; 2380aa8b57aaSKent Overstreet r1_bio->sectors = bio_sectors(mbio) - sectors_handled; 238162096bceSNeilBrown r1_bio->state = 0; 238262096bceSNeilBrown set_bit(R1BIO_ReadError, &r1_bio->state); 238362096bceSNeilBrown r1_bio->mddev = mddev; 23844f024f37SKent Overstreet r1_bio->sector = mbio->bi_iter.bi_sector + 23854f024f37SKent Overstreet sectors_handled; 238662096bceSNeilBrown 238762096bceSNeilBrown goto read_more; 238862096bceSNeilBrown } else 238962096bceSNeilBrown generic_make_request(bio); 239062096bceSNeilBrown } 239162096bceSNeilBrown } 239262096bceSNeilBrown 23934ed8731dSShaohua Li static void raid1d(struct md_thread *thread) 23941da177e4SLinus Torvalds { 23954ed8731dSShaohua Li struct mddev *mddev = thread->mddev; 23969f2c9d12SNeilBrown struct r1bio *r1_bio; 23971da177e4SLinus Torvalds unsigned long flags; 2398e8096360SNeilBrown struct r1conf *conf = mddev->private; 23991da177e4SLinus Torvalds struct list_head *head = &conf->retry_list; 2400e1dfa0a2SNeilBrown struct blk_plug plug; 24011da177e4SLinus Torvalds 24021da177e4SLinus Torvalds md_check_recovery(mddev); 24031da177e4SLinus Torvalds 2404e1dfa0a2SNeilBrown blk_start_plug(&plug); 24051da177e4SLinus Torvalds for (;;) { 2406a35e63efSNeilBrown 24077eaceaccSJens Axboe flush_pending_writes(conf); 2408a35e63efSNeilBrown 24091da177e4SLinus Torvalds spin_lock_irqsave(&conf->device_lock, flags); 2410a35e63efSNeilBrown if (list_empty(head)) { 2411191ea9b2SNeilBrown spin_unlock_irqrestore(&conf->device_lock, flags); 24121da177e4SLinus Torvalds break; 2413a35e63efSNeilBrown } 24149f2c9d12SNeilBrown r1_bio = list_entry(head->prev, struct r1bio, retry_list); 24151da177e4SLinus Torvalds list_del(head->prev); 2416ddaf22abSNeilBrown conf->nr_queued--; 24171da177e4SLinus Torvalds spin_unlock_irqrestore(&conf->device_lock, flags); 24181da177e4SLinus Torvalds 24191da177e4SLinus Torvalds mddev = r1_bio->mddev; 2420070ec55dSNeilBrown conf = mddev->private; 24214367af55SNeilBrown if (test_bit(R1BIO_IsSync, &r1_bio->state)) { 2422d8f05d29SNeilBrown if (test_bit(R1BIO_MadeGood, &r1_bio->state) || 242362096bceSNeilBrown test_bit(R1BIO_WriteError, &r1_bio->state)) 242462096bceSNeilBrown handle_sync_write_finished(conf, r1_bio); 242562096bceSNeilBrown else 24261da177e4SLinus Torvalds sync_request_write(mddev, r1_bio); 2427cd5ff9a1SNeilBrown } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) || 242862096bceSNeilBrown test_bit(R1BIO_WriteError, &r1_bio->state)) 242962096bceSNeilBrown handle_write_finished(conf, r1_bio); 243062096bceSNeilBrown else if (test_bit(R1BIO_ReadError, &r1_bio->state)) 243162096bceSNeilBrown handle_read_error(conf, r1_bio); 2432d2eb35acSNeilBrown else 2433d2eb35acSNeilBrown /* just a partial read to be scheduled from separate 2434d2eb35acSNeilBrown * context 2435d2eb35acSNeilBrown */ 2436d2eb35acSNeilBrown generic_make_request(r1_bio->bios[r1_bio->read_disk]); 243762096bceSNeilBrown 24381d9d5241SNeilBrown cond_resched(); 2439de393cdeSNeilBrown if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) 2440de393cdeSNeilBrown md_check_recovery(mddev); 24411da177e4SLinus Torvalds } 2442e1dfa0a2SNeilBrown blk_finish_plug(&plug); 24431da177e4SLinus Torvalds } 24441da177e4SLinus Torvalds 2445e8096360SNeilBrown static int init_resync(struct r1conf *conf) 24461da177e4SLinus Torvalds { 24471da177e4SLinus Torvalds int buffs; 24481da177e4SLinus Torvalds 24491da177e4SLinus Torvalds buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE; 24509e77c485SEric Sesterhenn BUG_ON(conf->r1buf_pool); 24511da177e4SLinus Torvalds conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free, 24521da177e4SLinus Torvalds conf->poolinfo); 24531da177e4SLinus Torvalds if (!conf->r1buf_pool) 24541da177e4SLinus Torvalds return -ENOMEM; 24551da177e4SLinus Torvalds conf->next_resync = 0; 24561da177e4SLinus Torvalds return 0; 24571da177e4SLinus Torvalds } 24581da177e4SLinus Torvalds 24591da177e4SLinus Torvalds /* 24601da177e4SLinus Torvalds * perform a "sync" on one "block" 24611da177e4SLinus Torvalds * 24621da177e4SLinus Torvalds * We need to make sure that no normal I/O request - particularly write 24631da177e4SLinus Torvalds * requests - conflict with active sync requests. 24641da177e4SLinus Torvalds * 24651da177e4SLinus Torvalds * This is achieved by tracking pending requests and a 'barrier' concept 24661da177e4SLinus Torvalds * that can be installed to exclude normal IO requests. 24671da177e4SLinus Torvalds */ 24681da177e4SLinus Torvalds 2469fd01b88cSNeilBrown static sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster) 24701da177e4SLinus Torvalds { 2471e8096360SNeilBrown struct r1conf *conf = mddev->private; 24729f2c9d12SNeilBrown struct r1bio *r1_bio; 24731da177e4SLinus Torvalds struct bio *bio; 24741da177e4SLinus Torvalds sector_t max_sector, nr_sectors; 24753e198f78SNeilBrown int disk = -1; 24761da177e4SLinus Torvalds int i; 24773e198f78SNeilBrown int wonly = -1; 24783e198f78SNeilBrown int write_targets = 0, read_targets = 0; 247957dab0bdSNeilBrown sector_t sync_blocks; 2480e3b9703eSNeilBrown int still_degraded = 0; 248106f60385SNeilBrown int good_sectors = RESYNC_SECTORS; 248206f60385SNeilBrown int min_bad = 0; /* number of sectors that are bad in all devices */ 24831da177e4SLinus Torvalds 24841da177e4SLinus Torvalds if (!conf->r1buf_pool) 24851da177e4SLinus Torvalds if (init_resync(conf)) 248657afd89fSNeilBrown return 0; 24871da177e4SLinus Torvalds 248858c0fed4SAndre Noll max_sector = mddev->dev_sectors; 24891da177e4SLinus Torvalds if (sector_nr >= max_sector) { 2490191ea9b2SNeilBrown /* If we aborted, we need to abort the 2491191ea9b2SNeilBrown * sync on the 'current' bitmap chunk (there will 2492191ea9b2SNeilBrown * only be one in raid1 resync. 2493191ea9b2SNeilBrown * We can find the current addess in mddev->curr_resync 2494191ea9b2SNeilBrown */ 24956a806c51SNeilBrown if (mddev->curr_resync < max_sector) /* aborted */ 24966a806c51SNeilBrown bitmap_end_sync(mddev->bitmap, mddev->curr_resync, 2497191ea9b2SNeilBrown &sync_blocks, 1); 24986a806c51SNeilBrown else /* completed sync */ 2499191ea9b2SNeilBrown conf->fullsync = 0; 25006a806c51SNeilBrown 25016a806c51SNeilBrown bitmap_close_sync(mddev->bitmap); 25021da177e4SLinus Torvalds close_sync(conf); 25031da177e4SLinus Torvalds return 0; 25041da177e4SLinus Torvalds } 25051da177e4SLinus Torvalds 250607d84d10SNeilBrown if (mddev->bitmap == NULL && 250707d84d10SNeilBrown mddev->recovery_cp == MaxSector && 25086394cca5SNeilBrown !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && 250907d84d10SNeilBrown conf->fullsync == 0) { 251007d84d10SNeilBrown *skipped = 1; 251107d84d10SNeilBrown return max_sector - sector_nr; 251207d84d10SNeilBrown } 25136394cca5SNeilBrown /* before building a request, check if we can skip these blocks.. 25146394cca5SNeilBrown * This call the bitmap_start_sync doesn't actually record anything 25156394cca5SNeilBrown */ 2516e3b9703eSNeilBrown if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && 2517e5de485fSNeilBrown !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { 2518191ea9b2SNeilBrown /* We can skip this block, and probably several more */ 2519191ea9b2SNeilBrown *skipped = 1; 2520191ea9b2SNeilBrown return sync_blocks; 2521191ea9b2SNeilBrown } 25221da177e4SLinus Torvalds /* 252317999be4SNeilBrown * If there is non-resync activity waiting for a turn, 252417999be4SNeilBrown * and resync is going fast enough, 252517999be4SNeilBrown * then let it though before starting on this new sync request. 25261da177e4SLinus Torvalds */ 252717999be4SNeilBrown if (!go_faster && conf->nr_waiting) 25281da177e4SLinus Torvalds msleep_interruptible(1000); 252917999be4SNeilBrown 2530b47490c9SNeilBrown bitmap_cond_end_sync(mddev->bitmap, sector_nr); 25311c4588e9SNeilBrown r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO); 253217999be4SNeilBrown 2533c2fd4c94SNeilBrown raise_barrier(conf, sector_nr); 25341da177e4SLinus Torvalds 25353e198f78SNeilBrown rcu_read_lock(); 25363e198f78SNeilBrown /* 25373e198f78SNeilBrown * If we get a correctably read error during resync or recovery, 25383e198f78SNeilBrown * we might want to read from a different device. So we 25393e198f78SNeilBrown * flag all drives that could conceivably be read from for READ, 25403e198f78SNeilBrown * and any others (which will be non-In_sync devices) for WRITE. 25413e198f78SNeilBrown * If a read fails, we try reading from something else for which READ 25423e198f78SNeilBrown * is OK. 25433e198f78SNeilBrown */ 25441da177e4SLinus Torvalds 25451da177e4SLinus Torvalds r1_bio->mddev = mddev; 25461da177e4SLinus Torvalds r1_bio->sector = sector_nr; 2547191ea9b2SNeilBrown r1_bio->state = 0; 25481da177e4SLinus Torvalds set_bit(R1BIO_IsSync, &r1_bio->state); 25491da177e4SLinus Torvalds 25508f19ccb2SNeilBrown for (i = 0; i < conf->raid_disks * 2; i++) { 25513cb03002SNeilBrown struct md_rdev *rdev; 25521da177e4SLinus Torvalds bio = r1_bio->bios[i]; 25532aabaa65SKent Overstreet bio_reset(bio); 25541da177e4SLinus Torvalds 25553e198f78SNeilBrown rdev = rcu_dereference(conf->mirrors[i].rdev); 25563e198f78SNeilBrown if (rdev == NULL || 25573e198f78SNeilBrown test_bit(Faulty, &rdev->flags)) { 25588f19ccb2SNeilBrown if (i < conf->raid_disks) 2559e3b9703eSNeilBrown still_degraded = 1; 25603e198f78SNeilBrown } else if (!test_bit(In_sync, &rdev->flags)) { 25611da177e4SLinus Torvalds bio->bi_rw = WRITE; 25621da177e4SLinus Torvalds bio->bi_end_io = end_sync_write; 25631da177e4SLinus Torvalds write_targets ++; 25643e198f78SNeilBrown } else { 25653e198f78SNeilBrown /* may need to read from here */ 256606f60385SNeilBrown sector_t first_bad = MaxSector; 256706f60385SNeilBrown int bad_sectors; 256806f60385SNeilBrown 256906f60385SNeilBrown if (is_badblock(rdev, sector_nr, good_sectors, 257006f60385SNeilBrown &first_bad, &bad_sectors)) { 257106f60385SNeilBrown if (first_bad > sector_nr) 257206f60385SNeilBrown good_sectors = first_bad - sector_nr; 257306f60385SNeilBrown else { 257406f60385SNeilBrown bad_sectors -= (sector_nr - first_bad); 257506f60385SNeilBrown if (min_bad == 0 || 257606f60385SNeilBrown min_bad > bad_sectors) 257706f60385SNeilBrown min_bad = bad_sectors; 257806f60385SNeilBrown } 257906f60385SNeilBrown } 258006f60385SNeilBrown if (sector_nr < first_bad) { 25813e198f78SNeilBrown if (test_bit(WriteMostly, &rdev->flags)) { 25823e198f78SNeilBrown if (wonly < 0) 25833e198f78SNeilBrown wonly = i; 25843e198f78SNeilBrown } else { 25853e198f78SNeilBrown if (disk < 0) 25863e198f78SNeilBrown disk = i; 25873e198f78SNeilBrown } 258806f60385SNeilBrown bio->bi_rw = READ; 258906f60385SNeilBrown bio->bi_end_io = end_sync_read; 25903e198f78SNeilBrown read_targets++; 2591d57368afSAlexander Lyakas } else if (!test_bit(WriteErrorSeen, &rdev->flags) && 2592d57368afSAlexander Lyakas test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && 2593d57368afSAlexander Lyakas !test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { 2594d57368afSAlexander Lyakas /* 2595d57368afSAlexander Lyakas * The device is suitable for reading (InSync), 2596d57368afSAlexander Lyakas * but has bad block(s) here. Let's try to correct them, 2597d57368afSAlexander Lyakas * if we are doing resync or repair. Otherwise, leave 2598d57368afSAlexander Lyakas * this device alone for this sync request. 2599d57368afSAlexander Lyakas */ 2600d57368afSAlexander Lyakas bio->bi_rw = WRITE; 2601d57368afSAlexander Lyakas bio->bi_end_io = end_sync_write; 2602d57368afSAlexander Lyakas write_targets++; 26033e198f78SNeilBrown } 260406f60385SNeilBrown } 260506f60385SNeilBrown if (bio->bi_end_io) { 26063e198f78SNeilBrown atomic_inc(&rdev->nr_pending); 26074f024f37SKent Overstreet bio->bi_iter.bi_sector = sector_nr + rdev->data_offset; 26083e198f78SNeilBrown bio->bi_bdev = rdev->bdev; 26091da177e4SLinus Torvalds bio->bi_private = r1_bio; 26101da177e4SLinus Torvalds } 261106f60385SNeilBrown } 26123e198f78SNeilBrown rcu_read_unlock(); 26133e198f78SNeilBrown if (disk < 0) 26143e198f78SNeilBrown disk = wonly; 26153e198f78SNeilBrown r1_bio->read_disk = disk; 2616191ea9b2SNeilBrown 261706f60385SNeilBrown if (read_targets == 0 && min_bad > 0) { 261806f60385SNeilBrown /* These sectors are bad on all InSync devices, so we 261906f60385SNeilBrown * need to mark them bad on all write targets 262006f60385SNeilBrown */ 262106f60385SNeilBrown int ok = 1; 26228f19ccb2SNeilBrown for (i = 0 ; i < conf->raid_disks * 2 ; i++) 262306f60385SNeilBrown if (r1_bio->bios[i]->bi_end_io == end_sync_write) { 2624a42f9d83Smajianpeng struct md_rdev *rdev = conf->mirrors[i].rdev; 262506f60385SNeilBrown ok = rdev_set_badblocks(rdev, sector_nr, 262606f60385SNeilBrown min_bad, 0 262706f60385SNeilBrown ) && ok; 262806f60385SNeilBrown } 262906f60385SNeilBrown set_bit(MD_CHANGE_DEVS, &mddev->flags); 263006f60385SNeilBrown *skipped = 1; 263106f60385SNeilBrown put_buf(r1_bio); 263206f60385SNeilBrown 263306f60385SNeilBrown if (!ok) { 263406f60385SNeilBrown /* Cannot record the badblocks, so need to 263506f60385SNeilBrown * abort the resync. 263606f60385SNeilBrown * If there are multiple read targets, could just 263706f60385SNeilBrown * fail the really bad ones ??? 263806f60385SNeilBrown */ 263906f60385SNeilBrown conf->recovery_disabled = mddev->recovery_disabled; 264006f60385SNeilBrown set_bit(MD_RECOVERY_INTR, &mddev->recovery); 264106f60385SNeilBrown return 0; 264206f60385SNeilBrown } else 264306f60385SNeilBrown return min_bad; 264406f60385SNeilBrown 264506f60385SNeilBrown } 264606f60385SNeilBrown if (min_bad > 0 && min_bad < good_sectors) { 264706f60385SNeilBrown /* only resync enough to reach the next bad->good 264806f60385SNeilBrown * transition */ 264906f60385SNeilBrown good_sectors = min_bad; 265006f60385SNeilBrown } 265106f60385SNeilBrown 26523e198f78SNeilBrown if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0) 26533e198f78SNeilBrown /* extra read targets are also write targets */ 26543e198f78SNeilBrown write_targets += read_targets-1; 26553e198f78SNeilBrown 26563e198f78SNeilBrown if (write_targets == 0 || read_targets == 0) { 26571da177e4SLinus Torvalds /* There is nowhere to write, so all non-sync 26581da177e4SLinus Torvalds * drives must be failed - so we are finished 26591da177e4SLinus Torvalds */ 2660b7219ccbSNeilBrown sector_t rv; 2661b7219ccbSNeilBrown if (min_bad > 0) 2662b7219ccbSNeilBrown max_sector = sector_nr + min_bad; 2663b7219ccbSNeilBrown rv = max_sector - sector_nr; 266457afd89fSNeilBrown *skipped = 1; 26651da177e4SLinus Torvalds put_buf(r1_bio); 26661da177e4SLinus Torvalds return rv; 26671da177e4SLinus Torvalds } 26681da177e4SLinus Torvalds 2669c6207277SNeilBrown if (max_sector > mddev->resync_max) 2670c6207277SNeilBrown max_sector = mddev->resync_max; /* Don't do IO beyond here */ 267106f60385SNeilBrown if (max_sector > sector_nr + good_sectors) 267206f60385SNeilBrown max_sector = sector_nr + good_sectors; 26731da177e4SLinus Torvalds nr_sectors = 0; 2674289e99e8SNeilBrown sync_blocks = 0; 26751da177e4SLinus Torvalds do { 26761da177e4SLinus Torvalds struct page *page; 26771da177e4SLinus Torvalds int len = PAGE_SIZE; 26781da177e4SLinus Torvalds if (sector_nr + (len>>9) > max_sector) 26791da177e4SLinus Torvalds len = (max_sector - sector_nr) << 9; 26801da177e4SLinus Torvalds if (len == 0) 26811da177e4SLinus Torvalds break; 2682ab7a30c7SNeilBrown if (sync_blocks == 0) { 26836a806c51SNeilBrown if (!bitmap_start_sync(mddev->bitmap, sector_nr, 2684e3b9703eSNeilBrown &sync_blocks, still_degraded) && 2685e5de485fSNeilBrown !conf->fullsync && 2686e5de485fSNeilBrown !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) 2687191ea9b2SNeilBrown break; 26889e77c485SEric Sesterhenn BUG_ON(sync_blocks < (PAGE_SIZE>>9)); 26897571ae88SNeilBrown if ((len >> 9) > sync_blocks) 26906a806c51SNeilBrown len = sync_blocks<<9; 2691ab7a30c7SNeilBrown } 2692191ea9b2SNeilBrown 26938f19ccb2SNeilBrown for (i = 0 ; i < conf->raid_disks * 2; i++) { 26941da177e4SLinus Torvalds bio = r1_bio->bios[i]; 26951da177e4SLinus Torvalds if (bio->bi_end_io) { 2696d11c171eSNeilBrown page = bio->bi_io_vec[bio->bi_vcnt].bv_page; 26971da177e4SLinus Torvalds if (bio_add_page(bio, page, len, 0) == 0) { 26981da177e4SLinus Torvalds /* stop here */ 2699d11c171eSNeilBrown bio->bi_io_vec[bio->bi_vcnt].bv_page = page; 27001da177e4SLinus Torvalds while (i > 0) { 27011da177e4SLinus Torvalds i--; 27021da177e4SLinus Torvalds bio = r1_bio->bios[i]; 27036a806c51SNeilBrown if (bio->bi_end_io==NULL) 27046a806c51SNeilBrown continue; 27051da177e4SLinus Torvalds /* remove last page from this bio */ 27061da177e4SLinus Torvalds bio->bi_vcnt--; 27074f024f37SKent Overstreet bio->bi_iter.bi_size -= len; 27083fd83717SNeilBrown __clear_bit(BIO_SEG_VALID, &bio->bi_flags); 27091da177e4SLinus Torvalds } 27101da177e4SLinus Torvalds goto bio_full; 27111da177e4SLinus Torvalds } 27121da177e4SLinus Torvalds } 27131da177e4SLinus Torvalds } 27141da177e4SLinus Torvalds nr_sectors += len>>9; 27151da177e4SLinus Torvalds sector_nr += len>>9; 2716191ea9b2SNeilBrown sync_blocks -= (len>>9); 27171da177e4SLinus Torvalds } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES); 27181da177e4SLinus Torvalds bio_full: 27191da177e4SLinus Torvalds r1_bio->sectors = nr_sectors; 27201da177e4SLinus Torvalds 2721d11c171eSNeilBrown /* For a user-requested sync, we read all readable devices and do a 2722d11c171eSNeilBrown * compare 2723d11c171eSNeilBrown */ 2724d11c171eSNeilBrown if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { 2725d11c171eSNeilBrown atomic_set(&r1_bio->remaining, read_targets); 27262d4f4f33SNeilBrown for (i = 0; i < conf->raid_disks * 2 && read_targets; i++) { 2727d11c171eSNeilBrown bio = r1_bio->bios[i]; 2728d11c171eSNeilBrown if (bio->bi_end_io == end_sync_read) { 27292d4f4f33SNeilBrown read_targets--; 2730ddac7c7eSNeilBrown md_sync_acct(bio->bi_bdev, nr_sectors); 27311da177e4SLinus Torvalds generic_make_request(bio); 2732d11c171eSNeilBrown } 2733d11c171eSNeilBrown } 2734d11c171eSNeilBrown } else { 2735d11c171eSNeilBrown atomic_set(&r1_bio->remaining, 1); 2736d11c171eSNeilBrown bio = r1_bio->bios[r1_bio->read_disk]; 2737ddac7c7eSNeilBrown md_sync_acct(bio->bi_bdev, nr_sectors); 2738d11c171eSNeilBrown generic_make_request(bio); 2739d11c171eSNeilBrown 2740d11c171eSNeilBrown } 27411da177e4SLinus Torvalds return nr_sectors; 27421da177e4SLinus Torvalds } 27431da177e4SLinus Torvalds 2744fd01b88cSNeilBrown static sector_t raid1_size(struct mddev *mddev, sector_t sectors, int raid_disks) 274580c3a6ceSDan Williams { 274680c3a6ceSDan Williams if (sectors) 274780c3a6ceSDan Williams return sectors; 274880c3a6ceSDan Williams 274980c3a6ceSDan Williams return mddev->dev_sectors; 275080c3a6ceSDan Williams } 275180c3a6ceSDan Williams 2752e8096360SNeilBrown static struct r1conf *setup_conf(struct mddev *mddev) 27531da177e4SLinus Torvalds { 2754e8096360SNeilBrown struct r1conf *conf; 2755709ae487SNeilBrown int i; 27560eaf822cSJonathan Brassow struct raid1_info *disk; 27573cb03002SNeilBrown struct md_rdev *rdev; 2758709ae487SNeilBrown int err = -ENOMEM; 27591da177e4SLinus Torvalds 2760e8096360SNeilBrown conf = kzalloc(sizeof(struct r1conf), GFP_KERNEL); 27611da177e4SLinus Torvalds if (!conf) 2762709ae487SNeilBrown goto abort; 27631da177e4SLinus Torvalds 27640eaf822cSJonathan Brassow conf->mirrors = kzalloc(sizeof(struct raid1_info) 27658f19ccb2SNeilBrown * mddev->raid_disks * 2, 27661da177e4SLinus Torvalds GFP_KERNEL); 27671da177e4SLinus Torvalds if (!conf->mirrors) 2768709ae487SNeilBrown goto abort; 27691da177e4SLinus Torvalds 2770ddaf22abSNeilBrown conf->tmppage = alloc_page(GFP_KERNEL); 2771ddaf22abSNeilBrown if (!conf->tmppage) 2772709ae487SNeilBrown goto abort; 2773ddaf22abSNeilBrown 2774709ae487SNeilBrown conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL); 27751da177e4SLinus Torvalds if (!conf->poolinfo) 2776709ae487SNeilBrown goto abort; 27778f19ccb2SNeilBrown conf->poolinfo->raid_disks = mddev->raid_disks * 2; 27781da177e4SLinus Torvalds conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc, 27791da177e4SLinus Torvalds r1bio_pool_free, 27801da177e4SLinus Torvalds conf->poolinfo); 27811da177e4SLinus Torvalds if (!conf->r1bio_pool) 2782709ae487SNeilBrown goto abort; 2783709ae487SNeilBrown 2784ed9bfdf1SNeilBrown conf->poolinfo->mddev = mddev; 27851da177e4SLinus Torvalds 2786c19d5798SNeilBrown err = -EINVAL; 2787e7e72bf6SNeil Brown spin_lock_init(&conf->device_lock); 2788dafb20faSNeilBrown rdev_for_each(rdev, mddev) { 2789aba336bdSNeilBrown struct request_queue *q; 2790709ae487SNeilBrown int disk_idx = rdev->raid_disk; 27911da177e4SLinus Torvalds if (disk_idx >= mddev->raid_disks 27921da177e4SLinus Torvalds || disk_idx < 0) 27931da177e4SLinus Torvalds continue; 2794c19d5798SNeilBrown if (test_bit(Replacement, &rdev->flags)) 279502b898f2SNeilBrown disk = conf->mirrors + mddev->raid_disks + disk_idx; 2796c19d5798SNeilBrown else 27971da177e4SLinus Torvalds disk = conf->mirrors + disk_idx; 27981da177e4SLinus Torvalds 2799c19d5798SNeilBrown if (disk->rdev) 2800c19d5798SNeilBrown goto abort; 28011da177e4SLinus Torvalds disk->rdev = rdev; 2802aba336bdSNeilBrown q = bdev_get_queue(rdev->bdev); 2803aba336bdSNeilBrown if (q->merge_bvec_fn) 2804aba336bdSNeilBrown mddev->merge_check_needed = 1; 28051da177e4SLinus Torvalds 28061da177e4SLinus Torvalds disk->head_position = 0; 280712cee5a8SShaohua Li disk->seq_start = MaxSector; 28081da177e4SLinus Torvalds } 28091da177e4SLinus Torvalds conf->raid_disks = mddev->raid_disks; 28101da177e4SLinus Torvalds conf->mddev = mddev; 28111da177e4SLinus Torvalds INIT_LIST_HEAD(&conf->retry_list); 28121da177e4SLinus Torvalds 28131da177e4SLinus Torvalds spin_lock_init(&conf->resync_lock); 281417999be4SNeilBrown init_waitqueue_head(&conf->wait_barrier); 28151da177e4SLinus Torvalds 2816191ea9b2SNeilBrown bio_list_init(&conf->pending_bio_list); 281734db0cd6SNeilBrown conf->pending_count = 0; 2818d890fa2bSNeilBrown conf->recovery_disabled = mddev->recovery_disabled - 1; 2819191ea9b2SNeilBrown 282079ef3a8aSmajianpeng conf->start_next_window = MaxSector; 282179ef3a8aSmajianpeng conf->current_window_requests = conf->next_window_requests = 0; 282279ef3a8aSmajianpeng 2823c19d5798SNeilBrown err = -EIO; 28248f19ccb2SNeilBrown for (i = 0; i < conf->raid_disks * 2; i++) { 28251da177e4SLinus Torvalds 28261da177e4SLinus Torvalds disk = conf->mirrors + i; 28271da177e4SLinus Torvalds 2828c19d5798SNeilBrown if (i < conf->raid_disks && 2829c19d5798SNeilBrown disk[conf->raid_disks].rdev) { 2830c19d5798SNeilBrown /* This slot has a replacement. */ 2831c19d5798SNeilBrown if (!disk->rdev) { 2832c19d5798SNeilBrown /* No original, just make the replacement 2833c19d5798SNeilBrown * a recovering spare 2834c19d5798SNeilBrown */ 2835c19d5798SNeilBrown disk->rdev = 2836c19d5798SNeilBrown disk[conf->raid_disks].rdev; 2837c19d5798SNeilBrown disk[conf->raid_disks].rdev = NULL; 2838c19d5798SNeilBrown } else if (!test_bit(In_sync, &disk->rdev->flags)) 2839c19d5798SNeilBrown /* Original is not in_sync - bad */ 2840c19d5798SNeilBrown goto abort; 2841c19d5798SNeilBrown } 2842c19d5798SNeilBrown 28435fd6c1dcSNeilBrown if (!disk->rdev || 28445fd6c1dcSNeilBrown !test_bit(In_sync, &disk->rdev->flags)) { 28451da177e4SLinus Torvalds disk->head_position = 0; 28464f0a5e01SJonathan Brassow if (disk->rdev && 28474f0a5e01SJonathan Brassow (disk->rdev->saved_raid_disk < 0)) 284817571284SNeilBrown conf->fullsync = 1; 2849be4d3280SShaohua Li } 28501da177e4SLinus Torvalds } 2851709ae487SNeilBrown 2852709ae487SNeilBrown err = -ENOMEM; 28530232605dSNeilBrown conf->thread = md_register_thread(raid1d, mddev, "raid1"); 2854709ae487SNeilBrown if (!conf->thread) { 28551da177e4SLinus Torvalds printk(KERN_ERR 28569dd1e2faSNeilBrown "md/raid1:%s: couldn't allocate thread\n", 28571da177e4SLinus Torvalds mdname(mddev)); 2858709ae487SNeilBrown goto abort; 28591da177e4SLinus Torvalds } 2860191ea9b2SNeilBrown 2861709ae487SNeilBrown return conf; 2862709ae487SNeilBrown 2863709ae487SNeilBrown abort: 2864709ae487SNeilBrown if (conf) { 2865709ae487SNeilBrown if (conf->r1bio_pool) 2866709ae487SNeilBrown mempool_destroy(conf->r1bio_pool); 2867709ae487SNeilBrown kfree(conf->mirrors); 2868709ae487SNeilBrown safe_put_page(conf->tmppage); 2869709ae487SNeilBrown kfree(conf->poolinfo); 2870709ae487SNeilBrown kfree(conf); 2871709ae487SNeilBrown } 2872709ae487SNeilBrown return ERR_PTR(err); 2873709ae487SNeilBrown } 2874709ae487SNeilBrown 28755220ea1eSmajianpeng static int stop(struct mddev *mddev); 2876fd01b88cSNeilBrown static int run(struct mddev *mddev) 2877709ae487SNeilBrown { 2878e8096360SNeilBrown struct r1conf *conf; 2879709ae487SNeilBrown int i; 28803cb03002SNeilBrown struct md_rdev *rdev; 28815220ea1eSmajianpeng int ret; 28822ff8cc2cSShaohua Li bool discard_supported = false; 2883709ae487SNeilBrown 2884709ae487SNeilBrown if (mddev->level != 1) { 28859dd1e2faSNeilBrown printk(KERN_ERR "md/raid1:%s: raid level not set to mirroring (%d)\n", 2886709ae487SNeilBrown mdname(mddev), mddev->level); 2887709ae487SNeilBrown return -EIO; 2888709ae487SNeilBrown } 2889709ae487SNeilBrown if (mddev->reshape_position != MaxSector) { 28909dd1e2faSNeilBrown printk(KERN_ERR "md/raid1:%s: reshape_position set but not supported\n", 2891709ae487SNeilBrown mdname(mddev)); 2892709ae487SNeilBrown return -EIO; 2893709ae487SNeilBrown } 2894709ae487SNeilBrown /* 2895709ae487SNeilBrown * copy the already verified devices into our private RAID1 2896709ae487SNeilBrown * bookkeeping area. [whatever we allocate in run(), 2897709ae487SNeilBrown * should be freed in stop()] 2898709ae487SNeilBrown */ 2899709ae487SNeilBrown if (mddev->private == NULL) 2900709ae487SNeilBrown conf = setup_conf(mddev); 2901709ae487SNeilBrown else 2902709ae487SNeilBrown conf = mddev->private; 2903709ae487SNeilBrown 2904709ae487SNeilBrown if (IS_ERR(conf)) 2905709ae487SNeilBrown return PTR_ERR(conf); 2906709ae487SNeilBrown 2907c8dc9c65SJoe Lawrence if (mddev->queue) 29085026d7a9SH. Peter Anvin blk_queue_max_write_same_sectors(mddev->queue, 0); 29095026d7a9SH. Peter Anvin 2910dafb20faSNeilBrown rdev_for_each(rdev, mddev) { 29111ed7242eSJonathan Brassow if (!mddev->gendisk) 29121ed7242eSJonathan Brassow continue; 2913709ae487SNeilBrown disk_stack_limits(mddev->gendisk, rdev->bdev, 2914709ae487SNeilBrown rdev->data_offset << 9); 29152ff8cc2cSShaohua Li if (blk_queue_discard(bdev_get_queue(rdev->bdev))) 29162ff8cc2cSShaohua Li discard_supported = true; 2917709ae487SNeilBrown } 2918709ae487SNeilBrown 2919709ae487SNeilBrown mddev->degraded = 0; 2920709ae487SNeilBrown for (i=0; i < conf->raid_disks; i++) 2921709ae487SNeilBrown if (conf->mirrors[i].rdev == NULL || 2922709ae487SNeilBrown !test_bit(In_sync, &conf->mirrors[i].rdev->flags) || 2923709ae487SNeilBrown test_bit(Faulty, &conf->mirrors[i].rdev->flags)) 2924709ae487SNeilBrown mddev->degraded++; 2925709ae487SNeilBrown 2926709ae487SNeilBrown if (conf->raid_disks - mddev->degraded == 1) 2927709ae487SNeilBrown mddev->recovery_cp = MaxSector; 2928709ae487SNeilBrown 29298c6ac868SAndre Noll if (mddev->recovery_cp != MaxSector) 29309dd1e2faSNeilBrown printk(KERN_NOTICE "md/raid1:%s: not clean" 29318c6ac868SAndre Noll " -- starting background reconstruction\n", 29328c6ac868SAndre Noll mdname(mddev)); 29331da177e4SLinus Torvalds printk(KERN_INFO 29349dd1e2faSNeilBrown "md/raid1:%s: active with %d out of %d mirrors\n", 29351da177e4SLinus Torvalds mdname(mddev), mddev->raid_disks - mddev->degraded, 29361da177e4SLinus Torvalds mddev->raid_disks); 2937709ae487SNeilBrown 29381da177e4SLinus Torvalds /* 29391da177e4SLinus Torvalds * Ok, everything is just fine now 29401da177e4SLinus Torvalds */ 2941709ae487SNeilBrown mddev->thread = conf->thread; 2942709ae487SNeilBrown conf->thread = NULL; 2943709ae487SNeilBrown mddev->private = conf; 2944709ae487SNeilBrown 29451f403624SDan Williams md_set_array_sectors(mddev, raid1_size(mddev, 0, 0)); 29461da177e4SLinus Torvalds 29471ed7242eSJonathan Brassow if (mddev->queue) { 29482ff8cc2cSShaohua Li if (discard_supported) 29492ff8cc2cSShaohua Li queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, 29502ff8cc2cSShaohua Li mddev->queue); 29512ff8cc2cSShaohua Li else 29522ff8cc2cSShaohua Li queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, 29532ff8cc2cSShaohua Li mddev->queue); 29541ed7242eSJonathan Brassow } 29555220ea1eSmajianpeng 29565220ea1eSmajianpeng ret = md_integrity_register(mddev); 29575220ea1eSmajianpeng if (ret) 29585220ea1eSmajianpeng stop(mddev); 29595220ea1eSmajianpeng return ret; 29601da177e4SLinus Torvalds } 29611da177e4SLinus Torvalds 2962fd01b88cSNeilBrown static int stop(struct mddev *mddev) 29631da177e4SLinus Torvalds { 2964e8096360SNeilBrown struct r1conf *conf = mddev->private; 29654b6d287fSNeilBrown struct bitmap *bitmap = mddev->bitmap; 29664b6d287fSNeilBrown 29674b6d287fSNeilBrown /* wait for behind writes to complete */ 2968e555190dSNeilBrown if (bitmap && atomic_read(&bitmap->behind_writes) > 0) { 29699dd1e2faSNeilBrown printk(KERN_INFO "md/raid1:%s: behind writes in progress - waiting to stop.\n", 29709dd1e2faSNeilBrown mdname(mddev)); 29714b6d287fSNeilBrown /* need to kick something here to make sure I/O goes? */ 2972e555190dSNeilBrown wait_event(bitmap->behind_wait, 2973e555190dSNeilBrown atomic_read(&bitmap->behind_writes) == 0); 29744b6d287fSNeilBrown } 29751da177e4SLinus Torvalds 297607169fd4Smajianpeng freeze_array(conf, 0); 297707169fd4Smajianpeng unfreeze_array(conf); 2978409c57f3SNeilBrown 297901f96c0aSNeilBrown md_unregister_thread(&mddev->thread); 29801da177e4SLinus Torvalds if (conf->r1bio_pool) 29811da177e4SLinus Torvalds mempool_destroy(conf->r1bio_pool); 29821da177e4SLinus Torvalds kfree(conf->mirrors); 29830fea7ed8SHirokazu Takahashi safe_put_page(conf->tmppage); 29841da177e4SLinus Torvalds kfree(conf->poolinfo); 29851da177e4SLinus Torvalds kfree(conf); 29861da177e4SLinus Torvalds mddev->private = NULL; 29871da177e4SLinus Torvalds return 0; 29881da177e4SLinus Torvalds } 29891da177e4SLinus Torvalds 2990fd01b88cSNeilBrown static int raid1_resize(struct mddev *mddev, sector_t sectors) 29911da177e4SLinus Torvalds { 29921da177e4SLinus Torvalds /* no resync is happening, and there is enough space 29931da177e4SLinus Torvalds * on all devices, so we can resize. 29941da177e4SLinus Torvalds * We need to make sure resync covers any new space. 29951da177e4SLinus Torvalds * If the array is shrinking we should possibly wait until 29961da177e4SLinus Torvalds * any io in the removed space completes, but it hardly seems 29971da177e4SLinus Torvalds * worth it. 29981da177e4SLinus Torvalds */ 2999a4a6125aSNeilBrown sector_t newsize = raid1_size(mddev, sectors, 0); 3000a4a6125aSNeilBrown if (mddev->external_size && 3001a4a6125aSNeilBrown mddev->array_sectors > newsize) 3002b522adcdSDan Williams return -EINVAL; 3003a4a6125aSNeilBrown if (mddev->bitmap) { 3004a4a6125aSNeilBrown int ret = bitmap_resize(mddev->bitmap, newsize, 0, 0); 3005a4a6125aSNeilBrown if (ret) 3006a4a6125aSNeilBrown return ret; 3007a4a6125aSNeilBrown } 3008a4a6125aSNeilBrown md_set_array_sectors(mddev, newsize); 3009f233ea5cSAndre Noll set_capacity(mddev->gendisk, mddev->array_sectors); 3010449aad3eSNeilBrown revalidate_disk(mddev->gendisk); 3011b522adcdSDan Williams if (sectors > mddev->dev_sectors && 3012b098636cSNeilBrown mddev->recovery_cp > mddev->dev_sectors) { 301358c0fed4SAndre Noll mddev->recovery_cp = mddev->dev_sectors; 30141da177e4SLinus Torvalds set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 30151da177e4SLinus Torvalds } 3016b522adcdSDan Williams mddev->dev_sectors = sectors; 30174b5c7ae8SNeilBrown mddev->resync_max_sectors = sectors; 30181da177e4SLinus Torvalds return 0; 30191da177e4SLinus Torvalds } 30201da177e4SLinus Torvalds 3021fd01b88cSNeilBrown static int raid1_reshape(struct mddev *mddev) 30221da177e4SLinus Torvalds { 30231da177e4SLinus Torvalds /* We need to: 30241da177e4SLinus Torvalds * 1/ resize the r1bio_pool 30251da177e4SLinus Torvalds * 2/ resize conf->mirrors 30261da177e4SLinus Torvalds * 30271da177e4SLinus Torvalds * We allocate a new r1bio_pool if we can. 30281da177e4SLinus Torvalds * Then raise a device barrier and wait until all IO stops. 30291da177e4SLinus Torvalds * Then resize conf->mirrors and swap in the new r1bio pool. 30306ea9c07cSNeilBrown * 30316ea9c07cSNeilBrown * At the same time, we "pack" the devices so that all the missing 30326ea9c07cSNeilBrown * devices have the higher raid_disk numbers. 30331da177e4SLinus Torvalds */ 30341da177e4SLinus Torvalds mempool_t *newpool, *oldpool; 30351da177e4SLinus Torvalds struct pool_info *newpoolinfo; 30360eaf822cSJonathan Brassow struct raid1_info *newmirrors; 3037e8096360SNeilBrown struct r1conf *conf = mddev->private; 303863c70c4fSNeilBrown int cnt, raid_disks; 3039c04be0aaSNeilBrown unsigned long flags; 3040b5470dc5SDan Williams int d, d2, err; 30411da177e4SLinus Torvalds 304263c70c4fSNeilBrown /* Cannot change chunk_size, layout, or level */ 3043664e7c41SAndre Noll if (mddev->chunk_sectors != mddev->new_chunk_sectors || 304463c70c4fSNeilBrown mddev->layout != mddev->new_layout || 304563c70c4fSNeilBrown mddev->level != mddev->new_level) { 3046664e7c41SAndre Noll mddev->new_chunk_sectors = mddev->chunk_sectors; 304763c70c4fSNeilBrown mddev->new_layout = mddev->layout; 304863c70c4fSNeilBrown mddev->new_level = mddev->level; 304963c70c4fSNeilBrown return -EINVAL; 305063c70c4fSNeilBrown } 305163c70c4fSNeilBrown 3052b5470dc5SDan Williams err = md_allow_write(mddev); 3053b5470dc5SDan Williams if (err) 3054b5470dc5SDan Williams return err; 30552a2275d6SNeilBrown 305663c70c4fSNeilBrown raid_disks = mddev->raid_disks + mddev->delta_disks; 305763c70c4fSNeilBrown 30586ea9c07cSNeilBrown if (raid_disks < conf->raid_disks) { 30596ea9c07cSNeilBrown cnt=0; 30606ea9c07cSNeilBrown for (d= 0; d < conf->raid_disks; d++) 30611da177e4SLinus Torvalds if (conf->mirrors[d].rdev) 30626ea9c07cSNeilBrown cnt++; 30636ea9c07cSNeilBrown if (cnt > raid_disks) 30641da177e4SLinus Torvalds return -EBUSY; 30656ea9c07cSNeilBrown } 30661da177e4SLinus Torvalds 30671da177e4SLinus Torvalds newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL); 30681da177e4SLinus Torvalds if (!newpoolinfo) 30691da177e4SLinus Torvalds return -ENOMEM; 30701da177e4SLinus Torvalds newpoolinfo->mddev = mddev; 30718f19ccb2SNeilBrown newpoolinfo->raid_disks = raid_disks * 2; 30721da177e4SLinus Torvalds 30731da177e4SLinus Torvalds newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc, 30741da177e4SLinus Torvalds r1bio_pool_free, newpoolinfo); 30751da177e4SLinus Torvalds if (!newpool) { 30761da177e4SLinus Torvalds kfree(newpoolinfo); 30771da177e4SLinus Torvalds return -ENOMEM; 30781da177e4SLinus Torvalds } 30790eaf822cSJonathan Brassow newmirrors = kzalloc(sizeof(struct raid1_info) * raid_disks * 2, 30808f19ccb2SNeilBrown GFP_KERNEL); 30811da177e4SLinus Torvalds if (!newmirrors) { 30821da177e4SLinus Torvalds kfree(newpoolinfo); 30831da177e4SLinus Torvalds mempool_destroy(newpool); 30841da177e4SLinus Torvalds return -ENOMEM; 30851da177e4SLinus Torvalds } 30861da177e4SLinus Torvalds 3087e2d59925SNeilBrown freeze_array(conf, 0); 30881da177e4SLinus Torvalds 30891da177e4SLinus Torvalds /* ok, everything is stopped */ 30901da177e4SLinus Torvalds oldpool = conf->r1bio_pool; 30911da177e4SLinus Torvalds conf->r1bio_pool = newpool; 30926ea9c07cSNeilBrown 3093a88aa786SNeilBrown for (d = d2 = 0; d < conf->raid_disks; d++) { 30943cb03002SNeilBrown struct md_rdev *rdev = conf->mirrors[d].rdev; 3095a88aa786SNeilBrown if (rdev && rdev->raid_disk != d2) { 309636fad858SNamhyung Kim sysfs_unlink_rdev(mddev, rdev); 3097a88aa786SNeilBrown rdev->raid_disk = d2; 309836fad858SNamhyung Kim sysfs_unlink_rdev(mddev, rdev); 309936fad858SNamhyung Kim if (sysfs_link_rdev(mddev, rdev)) 3100a88aa786SNeilBrown printk(KERN_WARNING 310136fad858SNamhyung Kim "md/raid1:%s: cannot register rd%d\n", 310236fad858SNamhyung Kim mdname(mddev), rdev->raid_disk); 3103a88aa786SNeilBrown } 3104a88aa786SNeilBrown if (rdev) 3105a88aa786SNeilBrown newmirrors[d2++].rdev = rdev; 31066ea9c07cSNeilBrown } 31071da177e4SLinus Torvalds kfree(conf->mirrors); 31081da177e4SLinus Torvalds conf->mirrors = newmirrors; 31091da177e4SLinus Torvalds kfree(conf->poolinfo); 31101da177e4SLinus Torvalds conf->poolinfo = newpoolinfo; 31111da177e4SLinus Torvalds 3112c04be0aaSNeilBrown spin_lock_irqsave(&conf->device_lock, flags); 31131da177e4SLinus Torvalds mddev->degraded += (raid_disks - conf->raid_disks); 3114c04be0aaSNeilBrown spin_unlock_irqrestore(&conf->device_lock, flags); 31151da177e4SLinus Torvalds conf->raid_disks = mddev->raid_disks = raid_disks; 311663c70c4fSNeilBrown mddev->delta_disks = 0; 31171da177e4SLinus Torvalds 3118e2d59925SNeilBrown unfreeze_array(conf); 31191da177e4SLinus Torvalds 31201da177e4SLinus Torvalds set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 31211da177e4SLinus Torvalds md_wakeup_thread(mddev->thread); 31221da177e4SLinus Torvalds 31231da177e4SLinus Torvalds mempool_destroy(oldpool); 31241da177e4SLinus Torvalds return 0; 31251da177e4SLinus Torvalds } 31261da177e4SLinus Torvalds 3127fd01b88cSNeilBrown static void raid1_quiesce(struct mddev *mddev, int state) 312836fa3063SNeilBrown { 3129e8096360SNeilBrown struct r1conf *conf = mddev->private; 313036fa3063SNeilBrown 313136fa3063SNeilBrown switch(state) { 31326eef4b21SNeilBrown case 2: /* wake for suspend */ 31336eef4b21SNeilBrown wake_up(&conf->wait_barrier); 31346eef4b21SNeilBrown break; 31359e6603daSNeilBrown case 1: 313607169fd4Smajianpeng freeze_array(conf, 0); 313736fa3063SNeilBrown break; 31389e6603daSNeilBrown case 0: 313907169fd4Smajianpeng unfreeze_array(conf); 314036fa3063SNeilBrown break; 314136fa3063SNeilBrown } 314236fa3063SNeilBrown } 314336fa3063SNeilBrown 3144fd01b88cSNeilBrown static void *raid1_takeover(struct mddev *mddev) 3145709ae487SNeilBrown { 3146709ae487SNeilBrown /* raid1 can take over: 3147709ae487SNeilBrown * raid5 with 2 devices, any layout or chunk size 3148709ae487SNeilBrown */ 3149709ae487SNeilBrown if (mddev->level == 5 && mddev->raid_disks == 2) { 3150e8096360SNeilBrown struct r1conf *conf; 3151709ae487SNeilBrown mddev->new_level = 1; 3152709ae487SNeilBrown mddev->new_layout = 0; 3153709ae487SNeilBrown mddev->new_chunk_sectors = 0; 3154709ae487SNeilBrown conf = setup_conf(mddev); 3155709ae487SNeilBrown if (!IS_ERR(conf)) 315607169fd4Smajianpeng /* Array must appear to be quiesced */ 315707169fd4Smajianpeng conf->array_frozen = 1; 3158709ae487SNeilBrown return conf; 3159709ae487SNeilBrown } 3160709ae487SNeilBrown return ERR_PTR(-EINVAL); 3161709ae487SNeilBrown } 31621da177e4SLinus Torvalds 316384fc4b56SNeilBrown static struct md_personality raid1_personality = 31641da177e4SLinus Torvalds { 31651da177e4SLinus Torvalds .name = "raid1", 31662604b703SNeilBrown .level = 1, 31671da177e4SLinus Torvalds .owner = THIS_MODULE, 31681da177e4SLinus Torvalds .make_request = make_request, 31691da177e4SLinus Torvalds .run = run, 31701da177e4SLinus Torvalds .stop = stop, 31711da177e4SLinus Torvalds .status = status, 31721da177e4SLinus Torvalds .error_handler = error, 31731da177e4SLinus Torvalds .hot_add_disk = raid1_add_disk, 31741da177e4SLinus Torvalds .hot_remove_disk= raid1_remove_disk, 31751da177e4SLinus Torvalds .spare_active = raid1_spare_active, 31761da177e4SLinus Torvalds .sync_request = sync_request, 31771da177e4SLinus Torvalds .resize = raid1_resize, 317880c3a6ceSDan Williams .size = raid1_size, 317963c70c4fSNeilBrown .check_reshape = raid1_reshape, 318036fa3063SNeilBrown .quiesce = raid1_quiesce, 3181709ae487SNeilBrown .takeover = raid1_takeover, 31825c675f83SNeilBrown .congested = raid1_congested, 318364590f45SNeilBrown .mergeable_bvec = raid1_mergeable_bvec, 31841da177e4SLinus Torvalds }; 31851da177e4SLinus Torvalds 31861da177e4SLinus Torvalds static int __init raid_init(void) 31871da177e4SLinus Torvalds { 31882604b703SNeilBrown return register_md_personality(&raid1_personality); 31891da177e4SLinus Torvalds } 31901da177e4SLinus Torvalds 31911da177e4SLinus Torvalds static void raid_exit(void) 31921da177e4SLinus Torvalds { 31932604b703SNeilBrown unregister_md_personality(&raid1_personality); 31941da177e4SLinus Torvalds } 31951da177e4SLinus Torvalds 31961da177e4SLinus Torvalds module_init(raid_init); 31971da177e4SLinus Torvalds module_exit(raid_exit); 31981da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 31990efb9e61SNeilBrown MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD"); 32001da177e4SLinus Torvalds MODULE_ALIAS("md-personality-3"); /* RAID1 */ 3201d9d166c2SNeilBrown MODULE_ALIAS("md-raid1"); 32022604b703SNeilBrown MODULE_ALIAS("md-level-1"); 320334db0cd6SNeilBrown 320434db0cd6SNeilBrown module_param(max_queued_requests, int, S_IRUGO|S_IWUSR); 3205