Skip to content

Commit 709ae48

Browse files
committed
md/raid1: add takeover support for raid5->raid1
A 2-device raid5 array can now be converted to raid1. Signed-off-by: NeilBrown <neilb@suse.de>
1 parent 6eef4b2 commit 709ae48

2 files changed

Lines changed: 120 additions & 76 deletions

File tree

‎drivers/md/raid1.c‎

Lines changed: 115 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ static void raise_barrier(conf_t *conf)
677677
static void lower_barrier(conf_t *conf)
678678
{
679679
unsigned long flags;
680+
BUG_ON(conf->barrier <= 0);
680681
spin_lock_irqsave(&conf->resync_lock, flags);
681682
conf->barrier--;
682683
spin_unlock_irqrestore(&conf->resync_lock, flags);
@@ -1960,74 +1961,48 @@ static sector_t raid1_size(mddev_t *mddev, sector_t sectors, int raid_disks)
19601961
return mddev->dev_sectors;
19611962
}
19621963

1963-
static int run(mddev_t *mddev)
1964+
static conf_t *setup_conf(mddev_t *mddev)
19641965
{
19651966
conf_t *conf;
1966-
int i, j, disk_idx;
1967+
int i;
19671968
mirror_info_t *disk;
19681969
mdk_rdev_t *rdev;
1970+
int err = -ENOMEM;
19691971

1970-
if (mddev->level != 1) {
1971-
printk("raid1: %s: raid level not set to mirroring (%d)\n",
1972-
mdname(mddev), mddev->level);
1973-
goto out;
1974-
}
1975-
if (mddev->reshape_position != MaxSector) {
1976-
printk("raid1: %s: reshape_position set but not supported\n",
1977-
mdname(mddev));
1978-
goto out;
1979-
}
1980-
/*
1981-
* copy the already verified devices into our private RAID1
1982-
* bookkeeping area. [whatever we allocate in run(),
1983-
* should be freed in stop()]
1984-
*/
19851972
conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
1986-
mddev->private = conf;
19871973
if (!conf)
1988-
goto out_no_mem;
1974+
goto abort;
19891975

19901976
conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
19911977
GFP_KERNEL);
19921978
if (!conf->mirrors)
1993-
goto out_no_mem;
1979+
goto abort;
19941980

19951981
conf->tmppage = alloc_page(GFP_KERNEL);
19961982
if (!conf->tmppage)
1997-
goto out_no_mem;
1983+
goto abort;
19981984

1999-
conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
1985+
conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
20001986
if (!conf->poolinfo)
2001-
goto out_no_mem;
2002-
conf->poolinfo->mddev = NULL;
1987+
goto abort;
20031988
conf->poolinfo->raid_disks = mddev->raid_disks;
20041989
conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
20051990
r1bio_pool_free,
20061991
conf->poolinfo);
20071992
if (!conf->r1bio_pool)
2008-
goto out_no_mem;
1993+
goto abort;
1994+
20091995
conf->poolinfo->mddev = mddev;
20101996

20111997
spin_lock_init(&conf->device_lock);
2012-
mddev->queue->queue_lock = &conf->device_lock;
2013-
20141998
list_for_each_entry(rdev, &mddev->disks, same_set) {
2015-
disk_idx = rdev->raid_disk;
1999+
int disk_idx = rdev->raid_disk;
20162000
if (disk_idx >= mddev->raid_disks
20172001
|| disk_idx < 0)
20182002
continue;
20192003
disk = conf->mirrors + disk_idx;
20202004

20212005
disk->rdev = rdev;
2022-
disk_stack_limits(mddev->gendisk, rdev->bdev,
2023-
rdev->data_offset << 9);
2024-
/* as we don't honour merge_bvec_fn, we must never risk
2025-
* violating it, so limit ->max_sector to one PAGE, as
2026-
* a one page request is never in violation.
2027-
*/
2028-
if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
2029-
queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
2030-
blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
20312006

20322007
disk->head_position = 0;
20332008
}
@@ -2041,47 +2016,105 @@ static int run(mddev_t *mddev)
20412016
bio_list_init(&conf->pending_bio_list);
20422017
bio_list_init(&conf->flushing_bio_list);
20432018

2044-
2045-
mddev->degraded = 0;
2019+
conf->last_used = -1;
20462020
for (i = 0; i < conf->raid_disks; i++) {
20472021

20482022
disk = conf->mirrors + i;
20492023

20502024
if (!disk->rdev ||
20512025
!test_bit(In_sync, &disk->rdev->flags)) {
20522026
disk->head_position = 0;
2053-
mddev->degraded++;
20542027
if (disk->rdev)
20552028
conf->fullsync = 1;
2056-
}
2029+
} else if (conf->last_used < 0)
2030+
/*
2031+
* The first working device is used as a
2032+
* starting point to read balancing.
2033+
*/
2034+
conf->last_used = i;
20572035
}
2058-
if (mddev->degraded == conf->raid_disks) {
2036+
2037+
err = -EIO;
2038+
if (conf->last_used < 0) {
20592039
printk(KERN_ERR "raid1: no operational mirrors for %s\n",
2060-
mdname(mddev));
2061-
goto out_free_conf;
2040+
mdname(mddev));
2041+
goto abort;
2042+
}
2043+
err = -ENOMEM;
2044+
conf->thread = md_register_thread(raid1d, mddev, NULL);
2045+
if (!conf->thread) {
2046+
printk(KERN_ERR
2047+
"raid1: couldn't allocate thread for %s\n",
2048+
mdname(mddev));
2049+
goto abort;
20622050
}
2063-
if (conf->raid_disks - mddev->degraded == 1)
2064-
mddev->recovery_cp = MaxSector;
20652051

2052+
return conf;
2053+
2054+
abort:
2055+
if (conf) {
2056+
if (conf->r1bio_pool)
2057+
mempool_destroy(conf->r1bio_pool);
2058+
kfree(conf->mirrors);
2059+
safe_put_page(conf->tmppage);
2060+
kfree(conf->poolinfo);
2061+
kfree(conf);
2062+
}
2063+
return ERR_PTR(err);
2064+
}
2065+
2066+
static int run(mddev_t *mddev)
2067+
{
2068+
conf_t *conf;
2069+
int i;
2070+
mdk_rdev_t *rdev;
2071+
2072+
if (mddev->level != 1) {
2073+
printk("raid1: %s: raid level not set to mirroring (%d)\n",
2074+
mdname(mddev), mddev->level);
2075+
return -EIO;
2076+
}
2077+
if (mddev->reshape_position != MaxSector) {
2078+
printk("raid1: %s: reshape_position set but not supported\n",
2079+
mdname(mddev));
2080+
return -EIO;
2081+
}
20662082
/*
2067-
* find the first working one and use it as a starting point
2068-
* to read balancing.
2083+
* copy the already verified devices into our private RAID1
2084+
* bookkeeping area. [whatever we allocate in run(),
2085+
* should be freed in stop()]
20692086
*/
2070-
for (j = 0; j < conf->raid_disks &&
2071-
(!conf->mirrors[j].rdev ||
2072-
!test_bit(In_sync, &conf->mirrors[j].rdev->flags)) ; j++)
2073-
/* nothing */;
2074-
conf->last_used = j;
2087+
if (mddev->private == NULL)
2088+
conf = setup_conf(mddev);
2089+
else
2090+
conf = mddev->private;
20752091

2092+
if (IS_ERR(conf))
2093+
return PTR_ERR(conf);
20762094

2077-
mddev->thread = md_register_thread(raid1d, mddev, NULL);
2078-
if (!mddev->thread) {
2079-
printk(KERN_ERR
2080-
"raid1: couldn't allocate thread for %s\n",
2081-
mdname(mddev));
2082-
goto out_free_conf;
2095+
mddev->queue->queue_lock = &conf->device_lock;
2096+
list_for_each_entry(rdev, &mddev->disks, same_set) {
2097+
disk_stack_limits(mddev->gendisk, rdev->bdev,
2098+
rdev->data_offset << 9);
2099+
/* as we don't honour merge_bvec_fn, we must never risk
2100+
* violating it, so limit ->max_sector to one PAGE, as
2101+
* a one page request is never in violation.
2102+
*/
2103+
if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
2104+
queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
2105+
blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
20832106
}
20842107

2108+
mddev->degraded = 0;
2109+
for (i=0; i < conf->raid_disks; i++)
2110+
if (conf->mirrors[i].rdev == NULL ||
2111+
!test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
2112+
test_bit(Faulty, &conf->mirrors[i].rdev->flags))
2113+
mddev->degraded++;
2114+
2115+
if (conf->raid_disks - mddev->degraded == 1)
2116+
mddev->recovery_cp = MaxSector;
2117+
20852118
if (mddev->recovery_cp != MaxSector)
20862119
printk(KERN_NOTICE "raid1: %s is not clean"
20872120
" -- starting background reconstruction\n",
@@ -2090,33 +2123,21 @@ static int run(mddev_t *mddev)
20902123
"raid1: raid set %s active with %d out of %d mirrors\n",
20912124
mdname(mddev), mddev->raid_disks - mddev->degraded,
20922125
mddev->raid_disks);
2126+
20932127
/*
20942128
* Ok, everything is just fine now
20952129
*/
2130+
mddev->thread = conf->thread;
2131+
conf->thread = NULL;
2132+
mddev->private = conf;
2133+
20962134
md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
20972135

20982136
mddev->queue->unplug_fn = raid1_unplug;
20992137
mddev->queue->backing_dev_info.congested_fn = raid1_congested;
21002138
mddev->queue->backing_dev_info.congested_data = mddev;
21012139
md_integrity_register(mddev);
21022140
return 0;
2103-
2104-
out_no_mem:
2105-
printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
2106-
mdname(mddev));
2107-
2108-
out_free_conf:
2109-
if (conf) {
2110-
if (conf->r1bio_pool)
2111-
mempool_destroy(conf->r1bio_pool);
2112-
kfree(conf->mirrors);
2113-
safe_put_page(conf->tmppage);
2114-
kfree(conf->poolinfo);
2115-
kfree(conf);
2116-
mddev->private = NULL;
2117-
}
2118-
out:
2119-
return -EIO;
21202141
}
21212142

21222143
static int stop(mddev_t *mddev)
@@ -2302,6 +2323,23 @@ static void raid1_quiesce(mddev_t *mddev, int state)
23022323
}
23032324
}
23042325

2326+
static void *raid1_takeover(mddev_t *mddev)
2327+
{
2328+
/* raid1 can take over:
2329+
* raid5 with 2 devices, any layout or chunk size
2330+
*/
2331+
if (mddev->level == 5 && mddev->raid_disks == 2) {
2332+
conf_t *conf;
2333+
mddev->new_level = 1;
2334+
mddev->new_layout = 0;
2335+
mddev->new_chunk_sectors = 0;
2336+
conf = setup_conf(mddev);
2337+
if (!IS_ERR(conf))
2338+
conf->barrier = 1;
2339+
return conf;
2340+
}
2341+
return ERR_PTR(-EINVAL);
2342+
}
23052343

23062344
static struct mdk_personality raid1_personality =
23072345
{
@@ -2321,6 +2359,7 @@ static struct mdk_personality raid1_personality =
23212359
.size = raid1_size,
23222360
.check_reshape = raid1_reshape,
23232361
.quiesce = raid1_quiesce,
2362+
.takeover = raid1_takeover,
23242363
};
23252364

23262365
static int __init raid_init(void)

‎drivers/md/raid1.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ struct r1_private_data_s {
5959

6060
mempool_t *r1bio_pool;
6161
mempool_t *r1buf_pool;
62+
63+
/* When taking over an array from a different personality, we store
64+
* the new thread here until we fully activate the array.
65+
*/
66+
struct mdk_thread_s *thread;
6267
};
6368

6469
typedef struct r1_private_data_s conf_t;

0 commit comments

Comments
 (0)