1297 lines · 33.6 KB · C
RawDownload
/* * Copyright (C) 2010-2011 Neil Brown * Copyright (C) 2010-2011 Red Hat, Inc. All rights reserved. * * This file is released under the GPL. */ #include <linux/slab.h>#include <linux/module.h> #include "md.h"#include "raid1.h"#include "raid5.h"#include "bitmap.h" #include <linux/device-mapper.h> #define DM_MSG_PREFIX "raid" /* * The following flags are used by dm-raid.c to set up the array state. * They must be cleared before md_run is called. */#define FirstUse 10             /* rdev flag */ struct raid_dev {	/*	 * Two DM devices, one to hold metadata and one to hold the	 * actual data/parity.  The reason for this is to not confuse	 * ti->len and give more flexibility in altering size and	 * characteristics.	 *	 * While it is possible for this device to be associated	 * with a different physical device than the data_dev, it	 * is intended for it to be the same.	 *    |--------- Physical Device ---------|	 *    |- meta_dev -|------ data_dev ------|	 */	struct dm_dev *meta_dev;	struct dm_dev *data_dev;	struct md_rdev rdev;}; /* * Flags for rs->print_flags field. */#define DMPF_SYNC              0x1#define DMPF_NOSYNC            0x2#define DMPF_REBUILD           0x4#define DMPF_DAEMON_SLEEP      0x8#define DMPF_MIN_RECOVERY_RATE 0x10#define DMPF_MAX_RECOVERY_RATE 0x20#define DMPF_MAX_WRITE_BEHIND  0x40#define DMPF_STRIPE_CACHE      0x80#define DMPF_REGION_SIZE       0X100struct raid_set {	struct dm_target *ti; 	uint32_t bitmap_loaded;	uint32_t print_flags; 	struct mddev md;	struct raid_type *raid_type;	struct dm_target_callbacks callbacks; 	struct raid_dev dev[0];}; /* Supported raid types and properties. */static struct raid_type {	const char *name;		/* RAID algorithm. */	const char *descr;		/* Descriptor text for logging. */	const unsigned parity_devs;	/* # of parity devices. */	const unsigned minimal_devs;	/* minimal # of devices in set. */	const unsigned level;		/* RAID level. */	const unsigned algorithm;	/* RAID algorithm. */} raid_types[] = {	{"raid1",    "RAID1 (mirroring)",               0, 2, 1, 0 /* NONE */},	{"raid4",    "RAID4 (dedicated parity disk)",	1, 2, 5, ALGORITHM_PARITY_0},	{"raid5_la", "RAID5 (left asymmetric)",		1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC},	{"raid5_ra", "RAID5 (right asymmetric)",	1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC},	{"raid5_ls", "RAID5 (left symmetric)",		1, 2, 5, ALGORITHM_LEFT_SYMMETRIC},	{"raid5_rs", "RAID5 (right symmetric)",		1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC},	{"raid6_zr", "RAID6 (zero restart)",		2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART},	{"raid6_nr", "RAID6 (N restart)",		2, 4, 6, ALGORITHM_ROTATING_N_RESTART},	{"raid6_nc", "RAID6 (N continue)",		2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE}}; static struct raid_type *get_raid_type(char *name){	int i; 	for (i = 0; i < ARRAY_SIZE(raid_types); i++)		if (!strcmp(raid_types[i].name, name))			return &raid_types[i]; 	return NULL;} static struct raid_set *context_alloc(struct dm_target *ti, struct raid_type *raid_type, unsigned raid_devs){	unsigned i;	struct raid_set *rs;	sector_t sectors_per_dev; 	if (raid_devs <= raid_type->parity_devs) {		ti->error = "Insufficient number of devices";		return ERR_PTR(-EINVAL);	} 	sectors_per_dev = ti->len;	if ((raid_type->level > 1) &&	    sector_div(sectors_per_dev, (raid_devs - raid_type->parity_devs))) {		ti->error = "Target length not divisible by number of data devices";		return ERR_PTR(-EINVAL);	} 	rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);	if (!rs) {		ti->error = "Cannot allocate raid context";		return ERR_PTR(-ENOMEM);	} 	mddev_init(&rs->md); 	rs->ti = ti;	rs->raid_type = raid_type;	rs->md.raid_disks = raid_devs;	rs->md.level = raid_type->level;	rs->md.new_level = rs->md.level;	rs->md.dev_sectors = sectors_per_dev;	rs->md.layout = raid_type->algorithm;	rs->md.new_layout = rs->md.layout;	rs->md.delta_disks = 0;	rs->md.recovery_cp = 0; 	for (i = 0; i < raid_devs; i++)		md_rdev_init(&rs->dev[i].rdev); 	/*	 * Remaining items to be initialized by further RAID params:	 *  rs->md.persistent	 *  rs->md.external	 *  rs->md.chunk_sectors	 *  rs->md.new_chunk_sectors	 */ 	return rs;} static void context_free(struct raid_set *rs){	int i; 	for (i = 0; i < rs->md.raid_disks; i++) {		if (rs->dev[i].meta_dev)			dm_put_device(rs->ti, rs->dev[i].meta_dev);		md_rdev_clear(&rs->dev[i].rdev);		if (rs->dev[i].data_dev)			dm_put_device(rs->ti, rs->dev[i].data_dev);	} 	kfree(rs);} /* * For every device we have two words *  <meta_dev>: meta device name or '-' if missing *  <data_dev>: data device name or '-' if missing * * The following are permitted: *    - - *    - <data_dev> *    <meta_dev> <data_dev> * * The following is not allowed: *    <meta_dev> - * * This code parses those words.  If there is a failure, * the caller must use context_free to unwind the operations. */static int dev_parms(struct raid_set *rs, char **argv){	int i;	int rebuild = 0;	int metadata_available = 0;	int ret = 0; 	for (i = 0; i < rs->md.raid_disks; i++, argv += 2) {		rs->dev[i].rdev.raid_disk = i; 		rs->dev[i].meta_dev = NULL;		rs->dev[i].data_dev = NULL; 		/*		 * There are no offsets, since there is a separate device		 * for data and metadata.		 */		rs->dev[i].rdev.data_offset = 0;		rs->dev[i].rdev.mddev = &rs->md; 		if (strcmp(argv[0], "-")) {			ret = dm_get_device(rs->ti, argv[0],					    dm_table_get_mode(rs->ti->table),					    &rs->dev[i].meta_dev);			rs->ti->error = "RAID metadata device lookup failure";			if (ret)				return ret; 			rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);			if (!rs->dev[i].rdev.sb_page)				return -ENOMEM;		} 		if (!strcmp(argv[1], "-")) {			if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&			    (!rs->dev[i].rdev.recovery_offset)) {				rs->ti->error = "Drive designated for rebuild not specified";				return -EINVAL;			} 			rs->ti->error = "No data device supplied with metadata device";			if (rs->dev[i].meta_dev)				return -EINVAL; 			continue;		} 		ret = dm_get_device(rs->ti, argv[1],				    dm_table_get_mode(rs->ti->table),				    &rs->dev[i].data_dev);		if (ret) {			rs->ti->error = "RAID device lookup failure";			return ret;		} 		if (rs->dev[i].meta_dev) {			metadata_available = 1;			rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;		}		rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;		list_add(&rs->dev[i].rdev.same_set, &rs->md.disks);		if (!test_bit(In_sync, &rs->dev[i].rdev.flags))			rebuild++;	} 	if (metadata_available) {		rs->md.external = 0;		rs->md.persistent = 1;		rs->md.major_version = 2;	} else if (rebuild && !rs->md.recovery_cp) {		/*		 * Without metadata, we will not be able to tell if the array		 * is in-sync or not - we must assume it is not.  Therefore,		 * it is impossible to rebuild a drive.		 *		 * Even if there is metadata, the on-disk information may		 * indicate that the array is not in-sync and it will then		 * fail at that time.		 *		 * User could specify 'nosync' option if desperate.		 */		DMERR("Unable to rebuild drive while array is not in-sync");		rs->ti->error = "RAID device lookup failure";		return -EINVAL;	} 	return 0;} /* * validate_region_size * @rs * @region_size:  region size in sectors.  If 0, pick a size (4MiB default). * * Set rs->md.bitmap_info.chunksize (which really refers to 'region size'). * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap. * * Returns: 0 on success, -EINVAL on failure. */static int validate_region_size(struct raid_set *rs, unsigned long region_size){	unsigned long min_region_size = rs->ti->len / (1 << 21); 	if (!region_size) {		/*		 * Choose a reasonable default.  All figures in sectors.		 */		if (min_region_size > (1 << 13)) {			DMINFO("Choosing default region size of %lu sectors",			       region_size);			region_size = min_region_size;		} else {			DMINFO("Choosing default region size of 4MiB");			region_size = 1 << 13; /* sectors */		}	} else {		/*		 * Validate user-supplied value.		 */		if (region_size > rs->ti->len) {			rs->ti->error = "Supplied region size is too large";			return -EINVAL;		} 		if (region_size < min_region_size) {			DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",			      region_size, min_region_size);			rs->ti->error = "Supplied region size is too small";			return -EINVAL;		} 		if (!is_power_of_2(region_size)) {			rs->ti->error = "Region size is not a power of 2";			return -EINVAL;		} 		if (region_size < rs->md.chunk_sectors) {			rs->ti->error = "Region size is smaller than the chunk size";			return -EINVAL;		}	} 	/*	 * Convert sectors to bytes.	 */	rs->md.bitmap_info.chunksize = (region_size << 9); 	return 0;} /* * Possible arguments are... *	<chunk_size> [optional_args] * * Argument definitions *    <chunk_size>			The number of sectors per disk that *                                      will form the "stripe" *    [[no]sync]			Force or prevent recovery of the *                                      entire array *    [rebuild <idx>]			Rebuild the drive indicated by the index *    [daemon_sleep <ms>]		Time between bitmap daemon work to *                                      clear bits *    [min_recovery_rate <kB/sec/disk>]	Throttle RAID initialization *    [max_recovery_rate <kB/sec/disk>]	Throttle RAID initialization *    [write_mostly <idx>]		Indicate a write mostly drive via index *    [max_write_behind <sectors>]	See '-write-behind=' (man mdadm) *    [stripe_cache <sectors>]		Stripe cache size for higher RAIDs *    [region_size <sectors>]           Defines granularity of bitmap */static int parse_raid_params(struct raid_set *rs, char **argv,			     unsigned num_raid_params){	unsigned i, rebuild_cnt = 0;	unsigned long value, region_size = 0;	char *key; 	/*	 * First, parse the in-order required arguments	 * "chunk_size" is the only argument of this type.	 */	if ((strict_strtoul(argv[0], 10, &value) < 0)) {		rs->ti->error = "Bad chunk size";		return -EINVAL;	} else if (rs->raid_type->level == 1) {		if (value)			DMERR("Ignoring chunk size parameter for RAID 1");		value = 0;	} else if (!is_power_of_2(value)) {		rs->ti->error = "Chunk size must be a power of 2";		return -EINVAL;	} else if (value < 8) {		rs->ti->error = "Chunk size value is too small";		return -EINVAL;	} 	rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;	argv++;	num_raid_params--; 	/*	 * We set each individual device as In_sync with a completed	 * 'recovery_offset'.  If there has been a device failure or	 * replacement then one of the following cases applies:	 *	 *   1) User specifies 'rebuild'.	 *      - Device is reset when param is read.	 *   2) A new device is supplied.	 *      - No matching superblock found, resets device.	 *   3) Device failure was transient and returns on reload.	 *      - Failure noticed, resets device for bitmap replay.	 *   4) Device hadn't completed recovery after previous failure.	 *      - Superblock is read and overrides recovery_offset.	 *	 * What is found in the superblocks of the devices is always	 * authoritative, unless 'rebuild' or '[no]sync' was specified.	 */	for (i = 0; i < rs->md.raid_disks; i++) {		set_bit(In_sync, &rs->dev[i].rdev.flags);		rs->dev[i].rdev.recovery_offset = MaxSector;	} 	/*	 * Second, parse the unordered optional arguments	 */	for (i = 0; i < num_raid_params; i++) {		if (!strcasecmp(argv[i], "nosync")) {			rs->md.recovery_cp = MaxSector;			rs->print_flags |= DMPF_NOSYNC;			continue;		}		if (!strcasecmp(argv[i], "sync")) {			rs->md.recovery_cp = 0;			rs->print_flags |= DMPF_SYNC;			continue;		} 		/* The rest of the optional arguments come in key/value pairs */		if ((i + 1) >= num_raid_params) {			rs->ti->error = "Wrong number of raid parameters given";			return -EINVAL;		} 		key = argv[i++];		if (strict_strtoul(argv[i], 10, &value) < 0) {			rs->ti->error = "Bad numerical argument given in raid params";			return -EINVAL;		} 		if (!strcasecmp(key, "rebuild")) {			rebuild_cnt++;			if (((rs->raid_type->level != 1) &&			     (rebuild_cnt > rs->raid_type->parity_devs)) ||			    ((rs->raid_type->level == 1) &&			     (rebuild_cnt > (rs->md.raid_disks - 1)))) {				rs->ti->error = "Too many rebuild devices specified for given RAID type";				return -EINVAL;			}			if (value > rs->md.raid_disks) {				rs->ti->error = "Invalid rebuild index given";				return -EINVAL;			}			clear_bit(In_sync, &rs->dev[value].rdev.flags);			rs->dev[value].rdev.recovery_offset = 0;			rs->print_flags |= DMPF_REBUILD;		} else if (!strcasecmp(key, "write_mostly")) {			if (rs->raid_type->level != 1) {				rs->ti->error = "write_mostly option is only valid for RAID1";				return -EINVAL;			}			if (value >= rs->md.raid_disks) {				rs->ti->error = "Invalid write_mostly drive index given";				return -EINVAL;			}			set_bit(WriteMostly, &rs->dev[value].rdev.flags);		} else if (!strcasecmp(key, "max_write_behind")) {			if (rs->raid_type->level != 1) {				rs->ti->error = "max_write_behind option is only valid for RAID1";				return -EINVAL;			}			rs->print_flags |= DMPF_MAX_WRITE_BEHIND; 			/*			 * In device-mapper, we specify things in sectors, but			 * MD records this value in kB			 */			value /= 2;			if (value > COUNTER_MAX) {				rs->ti->error = "Max write-behind limit out of range";				return -EINVAL;			}			rs->md.bitmap_info.max_write_behind = value;		} else if (!strcasecmp(key, "daemon_sleep")) {			rs->print_flags |= DMPF_DAEMON_SLEEP;			if (!value || (value > MAX_SCHEDULE_TIMEOUT)) {				rs->ti->error = "daemon sleep period out of range";				return -EINVAL;			}			rs->md.bitmap_info.daemon_sleep = value;		} else if (!strcasecmp(key, "stripe_cache")) {			rs->print_flags |= DMPF_STRIPE_CACHE; 			/*			 * In device-mapper, we specify things in sectors, but			 * MD records this value in kB			 */			value /= 2; 			if (rs->raid_type->level < 5) {				rs->ti->error = "Inappropriate argument: stripe_cache";				return -EINVAL;			}			if (raid5_set_cache_size(&rs->md, (int)value)) {				rs->ti->error = "Bad stripe_cache size";				return -EINVAL;			}		} else if (!strcasecmp(key, "min_recovery_rate")) {			rs->print_flags |= DMPF_MIN_RECOVERY_RATE;			if (value > INT_MAX) {				rs->ti->error = "min_recovery_rate out of range";

Lines 1–500 of 1297.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t