md.c 162 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
   md.c : Multiple Devices driver for Linux
	  Copyright (C) 1998, 1999, 2000 Ingo Molnar

     completely rewritten, based on the MD driver code from Marc Zyngier

   Changes:

   - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
   - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
   - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
   - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
   - kmod support by: Cyrus Durgin
   - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
   - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>

   - lots of fixes and improvements to the RAID1/RAID5 and generic
     RAID code (such as request based resynchronization):

     Neil Brown <neilb@cse.unsw.edu.au>.

22 23 24
   - persistent bitmap code
     Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.

Linus Torvalds's avatar
Linus Torvalds committed
25 26 27 28 29 30 31 32 33 34 35
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   You should have received a copy of the GNU General Public License
   (for example /usr/src/linux/COPYING); if not, write to the Free
   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <linux/module.h>
36
#include <linux/kernel.h>
37
#include <linux/kthread.h>
Linus Torvalds's avatar
Linus Torvalds committed
38 39
#include <linux/linkage.h>
#include <linux/raid/md.h>
40
#include <linux/raid/bitmap.h>
Linus Torvalds's avatar
Linus Torvalds committed
41 42
#include <linux/sysctl.h>
#include <linux/buffer_head.h> /* for invalidate_bdev */
43
#include <linux/poll.h>
44
#include <linux/mutex.h>
45
#include <linux/ctype.h>
46
#include <linux/freezer.h>
Linus Torvalds's avatar
Linus Torvalds committed
47 48 49

#include <linux/init.h>

50 51
#include <linux/file.h>

Linus Torvalds's avatar
Linus Torvalds committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#ifdef CONFIG_KMOD
#include <linux/kmod.h>
#endif

#include <asm/unaligned.h>

#define MAJOR_NR MD_MAJOR
#define MD_DRIVER

/* 63 partitions with the alternate major number (mdp) */
#define MdpMinorShift 6

#define DEBUG 0
#define dprintk(x...) ((void)(DEBUG && printk(x)))


#ifndef MODULE
static void autostart_arrays (int part);
#endif

72
static LIST_HEAD(pers_list);
Linus Torvalds's avatar
Linus Torvalds committed
73 74
static DEFINE_SPINLOCK(pers_lock);

75 76
static void md_print_devices(void);

77 78
static DECLARE_WAIT_QUEUE_HEAD(resync_wait);

79 80
#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }

Linus Torvalds's avatar
Linus Torvalds committed
81 82 83 84
/*
 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
 * is 1000 KB/sec, so the extra system load does not show up that much.
 * Increase it if you want to have more _guaranteed_ speed. Note that
85
 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds's avatar
Linus Torvalds committed
86 87 88 89 90
 * subsystem is idle. There is also an 'absolute maximum' reconstruction
 * speed limit - in case reconstruction slows down your system despite
 * idle IO detection.
 *
 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
91
 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds's avatar
Linus Torvalds committed
92 93 94 95
 */

static int sysctl_speed_limit_min = 1000;
static int sysctl_speed_limit_max = 200000;
96 97 98 99 100 101 102 103 104 105 106
static inline int speed_min(mddev_t *mddev)
{
	return mddev->sync_speed_min ?
		mddev->sync_speed_min : sysctl_speed_limit_min;
}

static inline int speed_max(mddev_t *mddev)
{
	return mddev->sync_speed_max ?
		mddev->sync_speed_max : sysctl_speed_limit_max;
}
Linus Torvalds's avatar
Linus Torvalds committed
107 108 109 110 111 112 113 114 115

static struct ctl_table_header *raid_table_header;

static ctl_table raid_table[] = {
	{
		.ctl_name	= DEV_RAID_SPEED_LIMIT_MIN,
		.procname	= "speed_limit_min",
		.data		= &sysctl_speed_limit_min,
		.maxlen		= sizeof(int),
116
		.mode		= S_IRUGO|S_IWUSR,
Linus Torvalds's avatar
Linus Torvalds committed
117 118 119 120 121 122 123
		.proc_handler	= &proc_dointvec,
	},
	{
		.ctl_name	= DEV_RAID_SPEED_LIMIT_MAX,
		.procname	= "speed_limit_max",
		.data		= &sysctl_speed_limit_max,
		.maxlen		= sizeof(int),
124
		.mode		= S_IRUGO|S_IWUSR,
Linus Torvalds's avatar
Linus Torvalds committed
125 126 127 128 129 130 131 132 133 134
		.proc_handler	= &proc_dointvec,
	},
	{ .ctl_name = 0 }
};

static ctl_table raid_dir_table[] = {
	{
		.ctl_name	= DEV_RAID,
		.procname	= "raid",
		.maxlen		= 0,
135
		.mode		= S_IRUGO|S_IXUGO,
Linus Torvalds's avatar
Linus Torvalds committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
		.child		= raid_table,
	},
	{ .ctl_name = 0 }
};

static ctl_table raid_root_table[] = {
	{
		.ctl_name	= CTL_DEV,
		.procname	= "dev",
		.maxlen		= 0,
		.mode		= 0555,
		.child		= raid_dir_table,
	},
	{ .ctl_name = 0 }
};

static struct block_device_operations md_fops;

154 155
static int start_readonly;

156 157 158 159 160 161 162 163 164 165
/*
 * We have a system wide 'event count' that is incremented
 * on any 'interesting' event, and readers of /proc/mdstat
 * can use 'poll' or 'select' to find out when the event
 * count increases.
 *
 * Events are:
 *  start array, stop array, error, add device, remove device,
 *  start build, activate spare
 */
166
static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
167
static atomic_t md_event_count;
168
void md_new_event(mddev_t *mddev)
169 170 171 172
{
	atomic_inc(&md_event_count);
	wake_up(&md_event_waiters);
}
173
EXPORT_SYMBOL_GPL(md_new_event);
174

175 176 177
/* Alternate version that can be called from interrupts
 * when calling sysfs_notify isn't needed.
 */
178
static void md_new_event_inintr(mddev_t *mddev)
179 180 181 182 183
{
	atomic_inc(&md_event_count);
	wake_up(&md_event_waiters);
}

Linus Torvalds's avatar
Linus Torvalds committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
/*
 * Enables to iterate over all existing md arrays
 * all_mddevs_lock protects this list.
 */
static LIST_HEAD(all_mddevs);
static DEFINE_SPINLOCK(all_mddevs_lock);


/*
 * iterates through all used mddevs in the system.
 * We take care to grab the all_mddevs_lock whenever navigating
 * the list, and to always hold a refcount when unlocked.
 * Any code which breaks out of this loop while own
 * a reference to the current mddev and must mddev_put it.
 */
199
#define for_each_mddev(mddev,tmp)					\
Linus Torvalds's avatar
Linus Torvalds committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
									\
	for (({ spin_lock(&all_mddevs_lock); 				\
		tmp = all_mddevs.next;					\
		mddev = NULL;});					\
	     ({ if (tmp != &all_mddevs)					\
			mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
		spin_unlock(&all_mddevs_lock);				\
		if (mddev) mddev_put(mddev);				\
		mddev = list_entry(tmp, mddev_t, all_mddevs);		\
		tmp != &all_mddevs;});					\
	     ({ spin_lock(&all_mddevs_lock);				\
		tmp = tmp->next;})					\
		)


215
static int md_fail_request (struct request_queue *q, struct bio *bio)
Linus Torvalds's avatar
Linus Torvalds committed
216
{
217
	bio_io_error(bio);
Linus Torvalds's avatar
Linus Torvalds committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	return 0;
}

static inline mddev_t *mddev_get(mddev_t *mddev)
{
	atomic_inc(&mddev->active);
	return mddev;
}

static void mddev_put(mddev_t *mddev)
{
	if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
		return;
	if (!mddev->raid_disks && list_empty(&mddev->disks)) {
		list_del(&mddev->all_mddevs);
233
		spin_unlock(&all_mddevs_lock);
234
		blk_cleanup_queue(mddev->queue);
235
		kobject_put(&mddev->kobj);
236 237
	} else
		spin_unlock(&all_mddevs_lock);
Linus Torvalds's avatar
Linus Torvalds committed
238 239 240 241 242 243 244 245 246 247 248 249
}

static mddev_t * mddev_find(dev_t unit)
{
	mddev_t *mddev, *new = NULL;

 retry:
	spin_lock(&all_mddevs_lock);
	list_for_each_entry(mddev, &all_mddevs, all_mddevs)
		if (mddev->unit == unit) {
			mddev_get(mddev);
			spin_unlock(&all_mddevs_lock);
250
			kfree(new);
Linus Torvalds's avatar
Linus Torvalds committed
251 252 253 254 255 256 257 258 259 260
			return mddev;
		}

	if (new) {
		list_add(&new->all_mddevs, &all_mddevs);
		spin_unlock(&all_mddevs_lock);
		return new;
	}
	spin_unlock(&all_mddevs_lock);

261
	new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
262 263 264 265 266 267 268 269 270
	if (!new)
		return NULL;

	new->unit = unit;
	if (MAJOR(unit) == MD_MAJOR)
		new->md_minor = MINOR(unit);
	else
		new->md_minor = MINOR(unit) >> MdpMinorShift;

271
	mutex_init(&new->reconfig_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
272 273 274 275
	INIT_LIST_HEAD(&new->disks);
	INIT_LIST_HEAD(&new->all_mddevs);
	init_timer(&new->safemode_timer);
	atomic_set(&new->active, 1);
276
	atomic_set(&new->openers, 0);
277
	spin_lock_init(&new->write_lock);
278
	init_waitqueue_head(&new->sb_wait);
279
	init_waitqueue_head(&new->recovery_wait);
280
	new->reshape_position = MaxSector;
281
	new->resync_min = 0;
282
	new->resync_max = MaxSector;
283
	new->level = LEVEL_NONE;
Linus Torvalds's avatar
Linus Torvalds committed
284 285 286 287 288 289

	new->queue = blk_alloc_queue(GFP_KERNEL);
	if (!new->queue) {
		kfree(new);
		return NULL;
	}
290 291
	/* Can be unlocked because the queue is new: no concurrency */
	queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, new->queue);
Linus Torvalds's avatar
Linus Torvalds committed
292 293 294 295 296 297 298 299

	blk_queue_make_request(new->queue, md_fail_request);

	goto retry;
}

static inline int mddev_lock(mddev_t * mddev)
{
300
	return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
301 302 303 304
}

static inline int mddev_trylock(mddev_t * mddev)
{
305
	return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
306 307 308 309
}

static inline void mddev_unlock(mddev_t * mddev)
{
310
	mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
311

312
	md_wakeup_thread(mddev->thread);
Linus Torvalds's avatar
Linus Torvalds committed
313 314
}

315
static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds's avatar
Linus Torvalds committed
316 317 318 319
{
	mdk_rdev_t * rdev;
	struct list_head *tmp;

320
	rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds's avatar
Linus Torvalds committed
321 322 323 324 325 326 327 328 329 330 331
		if (rdev->desc_nr == nr)
			return rdev;
	}
	return NULL;
}

static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
{
	struct list_head *tmp;
	mdk_rdev_t *rdev;

332
	rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds's avatar
Linus Torvalds committed
333 334 335 336 337 338
		if (rdev->bdev->bd_dev == dev)
			return rdev;
	}
	return NULL;
}

339
static struct mdk_personality *find_pers(int level, char *clevel)
340 341
{
	struct mdk_personality *pers;
342 343
	list_for_each_entry(pers, &pers_list, list) {
		if (level != LEVEL_NONE && pers->level == level)
344
			return pers;
345 346 347
		if (strcmp(pers->name, clevel)==0)
			return pers;
	}
348 349 350
	return NULL;
}

351
/* return the offset of the super block in 512byte sectors */
352
static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds's avatar
Linus Torvalds committed
353
{
354 355
	sector_t num_sectors = bdev->bd_inode->i_size / 512;
	return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds's avatar
Linus Torvalds committed
356 357
}

358
static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size)
Linus Torvalds's avatar
Linus Torvalds committed
359
{
360
	sector_t num_sectors = rdev->sb_start;
Linus Torvalds's avatar
Linus Torvalds committed
361 362

	if (chunk_size)
363 364
		num_sectors &= ~((sector_t)chunk_size/512 - 1);
	return num_sectors;
Linus Torvalds's avatar
Linus Torvalds committed
365 366 367 368 369 370 371 372 373 374
}

static int alloc_disk_sb(mdk_rdev_t * rdev)
{
	if (rdev->sb_page)
		MD_BUG();

	rdev->sb_page = alloc_page(GFP_KERNEL);
	if (!rdev->sb_page) {
		printk(KERN_ALERT "md: out of memory.\n");
375
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
376 377 378 379 380 381 382 383
	}

	return 0;
}

static void free_disk_sb(mdk_rdev_t * rdev)
{
	if (rdev->sb_page) {
384
		put_page(rdev->sb_page);
Linus Torvalds's avatar
Linus Torvalds committed
385 386
		rdev->sb_loaded = 0;
		rdev->sb_page = NULL;
387
		rdev->sb_start = 0;
Linus Torvalds's avatar
Linus Torvalds committed
388 389 390 391 392
		rdev->size = 0;
	}
}


393
static void super_written(struct bio *bio, int error)
394 395
{
	mdk_rdev_t *rdev = bio->bi_private;
396
	mddev_t *mddev = rdev->mddev;
397

398 399 400 401
	if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
		printk("md: super_written gets error=%d, uptodate=%d\n",
		       error, test_bit(BIO_UPTODATE, &bio->bi_flags));
		WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
402
		md_error(mddev, rdev);
403
	}
404

405 406
	if (atomic_dec_and_test(&mddev->pending_writes))
		wake_up(&mddev->sb_wait);
Neil Brown's avatar
Neil Brown committed
407
	bio_put(bio);
408 409
}

410
static void super_written_barrier(struct bio *bio, int error)
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
{
	struct bio *bio2 = bio->bi_private;
	mdk_rdev_t *rdev = bio2->bi_private;
	mddev_t *mddev = rdev->mddev;

	if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
	    error == -EOPNOTSUPP) {
		unsigned long flags;
		/* barriers don't appear to be supported :-( */
		set_bit(BarriersNotsupp, &rdev->flags);
		mddev->barriers_work = 0;
		spin_lock_irqsave(&mddev->write_lock, flags);
		bio2->bi_next = mddev->biolist;
		mddev->biolist = bio2;
		spin_unlock_irqrestore(&mddev->write_lock, flags);
		wake_up(&mddev->sb_wait);
		bio_put(bio);
428 429 430 431
	} else {
		bio_put(bio2);
		bio->bi_private = rdev;
		super_written(bio, error);
432 433 434
	}
}

435 436 437 438 439 440 441 442
void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
		   sector_t sector, int size, struct page *page)
{
	/* write first size bytes of page to sector of rdev
	 * Increment mddev->pending_writes before returning
	 * and decrement it on completion, waking up sb_wait
	 * if zero is reached.
	 * If an error occurred, call md_error
443 444 445
	 *
	 * As we might need to resubmit the request if BIO_RW_BARRIER
	 * causes ENOTSUPP, we allocate a spare bio...
446 447
	 */
	struct bio *bio = bio_alloc(GFP_NOIO, 1);
448
	int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
449 450 451 452 453 454

	bio->bi_bdev = rdev->bdev;
	bio->bi_sector = sector;
	bio_add_page(bio, page, size, 0);
	bio->bi_private = rdev;
	bio->bi_end_io = super_written;
455 456
	bio->bi_rw = rw;

457
	atomic_inc(&mddev->pending_writes);
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
	if (!test_bit(BarriersNotsupp, &rdev->flags)) {
		struct bio *rbio;
		rw |= (1<<BIO_RW_BARRIER);
		rbio = bio_clone(bio, GFP_NOIO);
		rbio->bi_private = bio;
		rbio->bi_end_io = super_written_barrier;
		submit_bio(rw, rbio);
	} else
		submit_bio(rw, bio);
}

void md_super_wait(mddev_t *mddev)
{
	/* wait for all superblock writes that were scheduled to complete.
	 * if any had to be retried (due to BARRIER problems), retry them
	 */
	DEFINE_WAIT(wq);
	for(;;) {
		prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
		if (atomic_read(&mddev->pending_writes)==0)
			break;
		while (mddev->biolist) {
			struct bio *bio;
			spin_lock_irq(&mddev->write_lock);
			bio = mddev->biolist;
			mddev->biolist = bio->bi_next ;
			bio->bi_next = NULL;
			spin_unlock_irq(&mddev->write_lock);
			submit_bio(bio->bi_rw, bio);
		}
		schedule();
	}
	finish_wait(&mddev->sb_wait, &wq);
491 492
}

493
static void bi_complete(struct bio *bio, int error)
Linus Torvalds's avatar
Linus Torvalds committed
494 495 496 497
{
	complete((struct completion*)bio->bi_private);
}

498
int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds's avatar
Linus Torvalds committed
499 500
		   struct page *page, int rw)
{
501
	struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds's avatar
Linus Torvalds committed
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
	struct completion event;
	int ret;

	rw |= (1 << BIO_RW_SYNC);

	bio->bi_bdev = bdev;
	bio->bi_sector = sector;
	bio_add_page(bio, page, size, 0);
	init_completion(&event);
	bio->bi_private = &event;
	bio->bi_end_io = bi_complete;
	submit_bio(rw, bio);
	wait_for_completion(&event);

	ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
	bio_put(bio);
	return ret;
}
520
EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds's avatar
Linus Torvalds committed
521

522
static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds's avatar
Linus Torvalds committed
523 524 525 526 527 528 529 530 531 532
{
	char b[BDEVNAME_SIZE];
	if (!rdev->sb_page) {
		MD_BUG();
		return -EINVAL;
	}
	if (rdev->sb_loaded)
		return 0;


533
	if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds's avatar
Linus Torvalds committed
534 535 536 537 538 539 540 541 542 543 544 545
		goto fail;
	rdev->sb_loaded = 1;
	return 0;

fail:
	printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
		bdevname(rdev->bdev,b));
	return -EINVAL;
}

static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
{
Andre Noll's avatar
Andre Noll committed
546 547 548 549
	return 	sb1->set_uuid0 == sb2->set_uuid0 &&
		sb1->set_uuid1 == sb2->set_uuid1 &&
		sb1->set_uuid2 == sb2->set_uuid2 &&
		sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds's avatar
Linus Torvalds committed
550 551 552 553 554 555 556 557 558 559 560 561
}

static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
{
	int ret;
	mdp_super_t *tmp1, *tmp2;

	tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
	tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);

	if (!tmp1 || !tmp2) {
		ret = 0;
562
		printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds's avatar
Linus Torvalds committed
563 564 565 566 567 568 569 570 571 572 573 574
		goto abort;
	}

	*tmp1 = *sb1;
	*tmp2 = *sb2;

	/*
	 * nr_disks is not constant
	 */
	tmp1->nr_disks = 0;
	tmp2->nr_disks = 0;

Andre Noll's avatar
Andre Noll committed
575
	ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds's avatar
Linus Torvalds committed
576
abort:
577 578
	kfree(tmp1);
	kfree(tmp2);
Linus Torvalds's avatar
Linus Torvalds committed
579 580 581
	return ret;
}

582 583 584 585 586 587 588

static u32 md_csum_fold(u32 csum)
{
	csum = (csum & 0xffff) + (csum >> 16);
	return (csum & 0xffff) + (csum >> 16);
}

Linus Torvalds's avatar
Linus Torvalds committed
589 590
static unsigned int calc_sb_csum(mdp_super_t * sb)
{
591 592 593
	u64 newcsum = 0;
	u32 *sb32 = (u32*)sb;
	int i;
Linus Torvalds's avatar
Linus Torvalds committed
594 595 596 597
	unsigned int disk_csum, csum;

	disk_csum = sb->sb_csum;
	sb->sb_csum = 0;
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614

	for (i = 0; i < MD_SB_BYTES/4 ; i++)
		newcsum += sb32[i];
	csum = (newcsum & 0xffffffff) + (newcsum>>32);


#ifdef CONFIG_ALPHA
	/* This used to use csum_partial, which was wrong for several
	 * reasons including that different results are returned on
	 * different architectures.  It isn't critical that we get exactly
	 * the same return value as before (we always csum_fold before
	 * testing, and that removes any differences).  However as we
	 * know that csum_partial always returned a 16bit value on
	 * alphas, do a fold to maximise conformity to previous behaviour.
	 */
	sb->sb_csum = md_csum_fold(disk_csum);
#else
Linus Torvalds's avatar
Linus Torvalds committed
615
	sb->sb_csum = disk_csum;
616
#endif
Linus Torvalds's avatar
Linus Torvalds committed
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
	return csum;
}


/*
 * Handle superblock details.
 * We want to be able to handle multiple superblock formats
 * so we have a common interface to them all, and an array of
 * different handlers.
 * We rely on user-space to write the initial superblock, and support
 * reading and updating of superblocks.
 * Interface methods are:
 *   int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
 *      loads and validates a superblock on dev.
 *      if refdev != NULL, compare superblocks on both devices
 *    Return:
 *      0 - dev has a superblock that is compatible with refdev
 *      1 - dev has a superblock that is compatible and newer than refdev
 *          so dev should be used as the refdev in future
 *     -EINVAL superblock incompatible or invalid
 *     -othererror e.g. -EIO
 *
 *   int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
 *      Verify that dev is acceptable into mddev.
 *       The first time, mddev->raid_disks will be 0, and data from
 *       dev should be merged in.  Subsequent calls check that dev
 *       is new enough.  Return 0 or -EINVAL
 *
 *   void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
 *     Update the superblock for rdev with data in mddev
 *     This does not write to disc.
 *
 */

struct super_type  {
652 653 654 655 656 657 658
	char		    *name;
	struct module	    *owner;
	int		    (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
					  int minor_version);
	int		    (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
	void		    (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
	unsigned long long  (*rdev_size_change)(mdk_rdev_t *rdev,
659
						sector_t num_sectors);
Linus Torvalds's avatar
Linus Torvalds committed
660 661 662 663 664 665 666 667 668 669 670 671
};

/*
 * load_super for 0.90.0 
 */
static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
{
	char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
	mdp_super_t *sb;
	int ret;

	/*
672
	 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds's avatar
Linus Torvalds committed
673 674 675 676
	 * it's at the end of the disk.
	 *
	 * It also happens to be a multiple of 4Kb.
	 */
677
	rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds's avatar
Linus Torvalds committed
678

679
	ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds's avatar
Linus Torvalds committed
680 681 682 683 684 685 686 687 688 689 690 691 692 693
	if (ret) return ret;

	ret = -EINVAL;

	bdevname(rdev->bdev, b);
	sb = (mdp_super_t*)page_address(rdev->sb_page);

	if (sb->md_magic != MD_SB_MAGIC) {
		printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
		       b);
		goto abort;
	}

	if (sb->major_version != 0 ||
694 695
	    sb->minor_version < 90 ||
	    sb->minor_version > 91) {
Linus Torvalds's avatar
Linus Torvalds committed
696 697 698 699 700 701 702 703 704
		printk(KERN_WARNING "Bad version number %d.%d on %s\n",
			sb->major_version, sb->minor_version,
			b);
		goto abort;
	}

	if (sb->raid_disks <= 0)
		goto abort;

705
	if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds's avatar
Linus Torvalds committed
706 707 708 709 710 711 712
		printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
			b);
		goto abort;
	}

	rdev->preferred_minor = sb->md_minor;
	rdev->data_offset = 0;
713
	rdev->sb_size = MD_SB_BYTES;
Linus Torvalds's avatar
Linus Torvalds committed
714

715 716 717 718 719 720 721 722 723 724 725
	if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
		if (sb->level != 1 && sb->level != 4
		    && sb->level != 5 && sb->level != 6
		    && sb->level != 10) {
			/* FIXME use a better test */
			printk(KERN_WARNING
			       "md: bitmaps not supported for this level.\n");
			goto abort;
		}
	}

Linus Torvalds's avatar
Linus Torvalds committed
726 727 728 729 730
	if (sb->level == LEVEL_MULTIPATH)
		rdev->desc_nr = -1;
	else
		rdev->desc_nr = sb->this_disk.number;

731
	if (!refdev) {
Linus Torvalds's avatar
Linus Torvalds committed
732
		ret = 1;
733
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
		__u64 ev1, ev2;
		mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
		if (!uuid_equal(refsb, sb)) {
			printk(KERN_WARNING "md: %s has different UUID to %s\n",
				b, bdevname(refdev->bdev,b2));
			goto abort;
		}
		if (!sb_equal(refsb, sb)) {
			printk(KERN_WARNING "md: %s has same UUID"
			       " but different superblock to %s\n",
			       b, bdevname(refdev->bdev, b2));
			goto abort;
		}
		ev1 = md_event(sb);
		ev2 = md_event(refsb);
		if (ev1 > ev2)
			ret = 1;
		else 
			ret = 0;
	}
754
	rdev->size = calc_num_sectors(rdev, sb->chunk_size) / 2;
Linus Torvalds's avatar
Linus Torvalds committed
755

756 757 758 759
	if (rdev->size < sb->size && sb->level > 1)
		/* "this cannot possibly happen" ... */
		ret = -EINVAL;

Linus Torvalds's avatar
Linus Torvalds committed
760 761 762 763 764 765 766 767 768 769 770
 abort:
	return ret;
}

/*
 * validate_super for 0.90.0
 */
static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
{
	mdp_disk_t *desc;
	mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
771
	__u64 ev1 = md_event(sb);
Linus Torvalds's avatar
Linus Torvalds committed
772

773
	rdev->raid_disk = -1;
774 775 776 777 778
	clear_bit(Faulty, &rdev->flags);
	clear_bit(In_sync, &rdev->flags);
	clear_bit(WriteMostly, &rdev->flags);
	clear_bit(BarriersNotsupp, &rdev->flags);

Linus Torvalds's avatar
Linus Torvalds committed
779 780 781 782
	if (mddev->raid_disks == 0) {
		mddev->major_version = 0;
		mddev->minor_version = sb->minor_version;
		mddev->patch_version = sb->patch_version;
783
		mddev->external = 0;
Linus Torvalds's avatar
Linus Torvalds committed
784 785 786 787
		mddev->chunk_size = sb->chunk_size;
		mddev->ctime = sb->ctime;
		mddev->utime = sb->utime;
		mddev->level = sb->level;
788
		mddev->clevel[0] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
789 790 791
		mddev->layout = sb->layout;
		mddev->raid_disks = sb->raid_disks;
		mddev->size = sb->size;
792
		mddev->events = ev1;
793
		mddev->bitmap_offset = 0;
794
		mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds's avatar
Linus Torvalds committed
795

796 797 798 799 800 801 802 803 804 805 806 807 808 809
		if (mddev->minor_version >= 91) {
			mddev->reshape_position = sb->reshape_position;
			mddev->delta_disks = sb->delta_disks;
			mddev->new_level = sb->new_level;
			mddev->new_layout = sb->new_layout;
			mddev->new_chunk = sb->new_chunk;
		} else {
			mddev->reshape_position = MaxSector;
			mddev->delta_disks = 0;
			mddev->new_level = mddev->level;
			mddev->new_layout = mddev->layout;
			mddev->new_chunk = mddev->chunk_size;
		}

Linus Torvalds's avatar
Linus Torvalds committed
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
		if (sb->state & (1<<MD_SB_CLEAN))
			mddev->recovery_cp = MaxSector;
		else {
			if (sb->events_hi == sb->cp_events_hi && 
				sb->events_lo == sb->cp_events_lo) {
				mddev->recovery_cp = sb->recovery_cp;
			} else
				mddev->recovery_cp = 0;
		}

		memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
		memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
		memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
		memcpy(mddev->uuid+12,&sb->set_uuid3, 4);

		mddev->max_disks = MD_SB_DISKS;
826 827

		if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
828
		    mddev->bitmap_file == NULL)
829
			mddev->bitmap_offset = mddev->default_bitmap_offset;
830

831 832
	} else if (mddev->pers == NULL) {
		/* Insist on good event counter while assembling */
Linus Torvalds's avatar
Linus Torvalds committed
833 834 835
		++ev1;
		if (ev1 < mddev->events) 
			return -EINVAL;
836 837 838 839 840 841
	} else if (mddev->bitmap) {
		/* if adding to array with a bitmap, then we can accept an
		 * older device ... but not too old.
		 */
		if (ev1 < mddev->bitmap->events_cleared)
			return 0;
842 843 844 845 846
	} else {
		if (ev1 < mddev->events)
			/* just a hot-add of a new device, leave raid_disk at -1 */
			return 0;
	}
847

Linus Torvalds's avatar
Linus Torvalds committed
848 849 850 851
	if (mddev->level != LEVEL_MULTIPATH) {
		desc = sb->disks + rdev->desc_nr;

		if (desc->state & (1<<MD_DISK_FAULTY))
852
			set_bit(Faulty, &rdev->flags);
853 854
		else if (desc->state & (1<<MD_DISK_SYNC) /* &&
			    desc->raid_disk < mddev->raid_disks */) {
855
			set_bit(In_sync, &rdev->flags);
Linus Torvalds's avatar
Linus Torvalds committed
856 857
			rdev->raid_disk = desc->raid_disk;
		}
858 859
		if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
			set_bit(WriteMostly, &rdev->flags);
860
	} else /* MULTIPATH are always insync */
861
		set_bit(In_sync, &rdev->flags);
Linus Torvalds's avatar
Linus Torvalds committed
862 863 864 865 866 867 868 869 870 871 872 873
	return 0;
}

/*
 * sync_super for 0.90.0
 */
static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
{
	mdp_super_t *sb;
	struct list_head *tmp;
	mdk_rdev_t *rdev2;
	int next_spare = mddev->raid_disks;
874

Linus Torvalds's avatar
Linus Torvalds committed
875 876 877 878 879 880 881 882 883 884 885 886 887 888

	/* make rdev->sb match mddev data..
	 *
	 * 1/ zero out disks
	 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
	 * 3/ any empty disks < next_spare become removed
	 *
	 * disks[0] gets initialised to REMOVED because
	 * we cannot be sure from other fields if it has
	 * been initialised or not.
	 */
	int i;
	int active=0, working=0,failed=0,spare=0,nr_disks=0;

889 890
	rdev->sb_size = MD_SB_BYTES;

Linus Torvalds's avatar
Linus Torvalds committed
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
	sb = (mdp_super_t*)page_address(rdev->sb_page);

	memset(sb, 0, sizeof(*sb));

	sb->md_magic = MD_SB_MAGIC;
	sb->major_version = mddev->major_version;
	sb->patch_version = mddev->patch_version;
	sb->gvalid_words  = 0; /* ignored */
	memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
	memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
	memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
	memcpy(&sb->set_uuid3, mddev->uuid+12,4);

	sb->ctime = mddev->ctime;
	sb->level = mddev->level;
	sb->size  = mddev->size;
	sb->raid_disks = mddev->raid_disks;
	sb->md_minor = mddev->md_minor;
909
	sb->not_persistent = 0;
Linus Torvalds's avatar
Linus Torvalds committed
910 911 912 913 914
	sb->utime = mddev->utime;
	sb->state = 0;
	sb->events_hi = (mddev->events>>32);
	sb->events_lo = (u32)mddev->events;

915 916 917 918 919 920 921 922 923 924 925
	if (mddev->reshape_position == MaxSector)
		sb->minor_version = 90;
	else {
		sb->minor_version = 91;
		sb->reshape_position = mddev->reshape_position;
		sb->new_level = mddev->new_level;
		sb->delta_disks = mddev->delta_disks;
		sb->new_layout = mddev->new_layout;
		sb->new_chunk = mddev->new_chunk;
	}
	mddev->minor_version = sb->minor_version;
Linus Torvalds's avatar
Linus Torvalds committed
926 927 928 929 930 931 932 933 934 935 936 937 938
	if (mddev->in_sync)
	{
		sb->recovery_cp = mddev->recovery_cp;
		sb->cp_events_hi = (mddev->events>>32);
		sb->cp_events_lo = (u32)mddev->events;
		if (mddev->recovery_cp == MaxSector)
			sb->state = (1<< MD_SB_CLEAN);
	} else
		sb->recovery_cp = 0;

	sb->layout = mddev->layout;
	sb->chunk_size = mddev->chunk_size;

939 940 941
	if (mddev->bitmap && mddev->bitmap_file == NULL)
		sb->state |= (1<<MD_SB_BITMAP_PRESENT);

Linus Torvalds's avatar
Linus Torvalds committed
942
	sb->disks[0].state = (1<<MD_DISK_REMOVED);
943
	rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds's avatar
Linus Torvalds committed
944
		mdp_disk_t *d;
945
		int desc_nr;
946 947
		if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
		    && !test_bit(Faulty, &rdev2->flags))
948
			desc_nr = rdev2->raid_disk;
Linus Torvalds's avatar
Linus Torvalds committed
949
		else
950
			desc_nr = next_spare++;
951
		rdev2->desc_nr = desc_nr;
Linus Torvalds's avatar
Linus Torvalds committed
952 953 954 955 956
		d = &sb->disks[rdev2->desc_nr];
		nr_disks++;
		d->number = rdev2->desc_nr;
		d->major = MAJOR(rdev2->bdev->bd_dev);
		d->minor = MINOR(rdev2->bdev->bd_dev);
957 958
		if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
		    && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds's avatar
Linus Torvalds committed
959 960 961
			d->raid_disk = rdev2->raid_disk;
		else
			d->raid_disk = rdev2->desc_nr; /* compatibility */
962
		if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds's avatar
Linus Torvalds committed
963
			d->state = (1<<MD_DISK_FAULTY);
964
		else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds's avatar
Linus Torvalds committed
965 966 967 968 969 970 971 972 973
			d->state = (1<<MD_DISK_ACTIVE);
			d->state |= (1<<MD_DISK_SYNC);
			active++;
			working++;
		} else {
			d->state = 0;
			spare++;
			working++;
		}
974 975
		if (test_bit(WriteMostly, &rdev2->flags))
			d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds's avatar
Linus Torvalds committed
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
	}
	/* now set the "removed" and "faulty" bits on any missing devices */
	for (i=0 ; i < mddev->raid_disks ; i++) {
		mdp_disk_t *d = &sb->disks[i];
		if (d->state == 0 && d->number == 0) {
			d->number = i;
			d->raid_disk = i;
			d->state = (1<<MD_DISK_REMOVED);
			d->state |= (1<<MD_DISK_FAULTY);
			failed++;
		}
	}
	sb->nr_disks = nr_disks;
	sb->active_disks = active;
	sb->working_disks = working;
	sb->failed_disks = failed;
	sb->spare_disks = spare;

	sb->this_disk = sb->disks[rdev->desc_nr];
	sb->sb_csum = calc_sb_csum(sb);
}

998 999 1000 1001
/*
 * rdev_size_change for 0.90.0
 */
static unsigned long long
1002
super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1003
{
1004
	if (num_sectors && num_sectors < rdev->mddev->size * 2)
1005 1006 1007
		return 0; /* component must fit device */
	if (rdev->mddev->bitmap_offset)
		return 0; /* can't move bitmap */
1008
	rdev->sb_start = calc_dev_sboffset(rdev->bdev);
1009 1010
	if (!num_sectors || num_sectors > rdev->sb_start)
		num_sectors = rdev->sb_start;
1011
	md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1012 1013
		       rdev->sb_page);
	md_super_wait(rdev->mddev);
1014
	return num_sectors / 2; /* kB for sysfs */
1015 1016 1017
}


Linus Torvalds's avatar
Linus Torvalds committed
1018 1019 1020 1021
/*
 * version 1 superblock
 */

1022
static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds's avatar
Linus Torvalds committed
1023
{
1024 1025
	__le32 disk_csum;
	u32 csum;
Linus Torvalds's avatar
Linus Torvalds committed
1026 1027
	unsigned long long newcsum;
	int size = 256 + le32_to_cpu(sb->max_dev)*2;
1028
	__le32 *isuper = (__le32*)sb;
Linus Torvalds's avatar
Linus Torvalds committed
1029 1030 1031 1032 1033 1034 1035 1036 1037
	int i;

	disk_csum = sb->sb_csum;
	sb->sb_csum = 0;
	newcsum = 0;
	for (i=0; size>=4; size -= 4 )
		newcsum += le32_to_cpu(*isuper++);

	if (size == 2)
1038
		newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds's avatar
Linus Torvalds committed
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048

	csum = (newcsum & 0xffffffff) + (newcsum >> 32);
	sb->sb_csum = disk_csum;
	return cpu_to_le32(csum);
}

static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
{
	struct mdp_superblock_1 *sb;
	int ret;
1049
	sector_t sb_start;
Linus Torvalds's avatar
Linus Torvalds committed
1050
	char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1051
	int bmask;
Linus Torvalds's avatar
Linus Torvalds committed
1052 1053

	/*
1054
	 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds's avatar
Linus Torvalds committed
1055 1056 1057 1058 1059 1060 1061 1062
	 * It is always aligned to a 4K boundary and
	 * depeding on minor_version, it can be:
	 * 0: At least 8K, but less than 12K, from end of device
	 * 1: At start of device
	 * 2: 4K from start of device.
	 */
	switch(minor_version) {
	case 0:
1063 1064 1065
		sb_start = rdev->bdev->bd_inode->i_size >> 9;
		sb_start -= 8*2;
		sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds's avatar
Linus Torvalds committed
1066 1067
		break;
	case 1:
1068
		sb_start = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1069 1070
		break;
	case 2:
1071
		sb_start = 8;
Linus Torvalds's avatar
Linus Torvalds committed
1072 1073 1074 1075
		break;
	default:
		return -EINVAL;
	}
1076
	rdev->sb_start = sb_start;
Linus Torvalds's avatar
Linus Torvalds committed
1077

1078 1079 1080 1081
	/* superblock is rarely larger than 1K, but it can be larger,
	 * and it is safe to read 4k, so we do that
	 */
	ret = read_disk_sb(rdev, 4096);
Linus Torvalds's avatar
Linus Torvalds committed
1082 1083 1084 1085 1086 1087 1088 1089
	if (ret) return ret;


	sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);

	if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
	    sb->major_version != cpu_to_le32(1) ||
	    le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1090
	    le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1091
	    (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds's avatar
Linus Torvalds committed
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
		return -EINVAL;

	if (calc_sb_1_csum(sb) != sb->sb_csum) {
		printk("md: invalid superblock checksum on %s\n",
			bdevname(rdev->bdev,b));
		return -EINVAL;
	}
	if (le64_to_cpu(sb->data_size) < 10) {
		printk("md: data_size too small on %s\n",
		       bdevname(rdev->bdev,b));
		return -EINVAL;
	}
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
	if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
		if (sb->level != cpu_to_le32(1) &&
		    sb->level != cpu_to_le32(4) &&
		    sb->level != cpu_to_le32(5) &&
		    sb->level != cpu_to_le32(6) &&
		    sb->level != cpu_to_le32(10)) {
			printk(KERN_WARNING
			       "md: bitmaps not supported for this level.\n");
			return -EINVAL;
		}
	}

Linus Torvalds's avatar
Linus Torvalds committed
1116 1117
	rdev->preferred_minor = 0xffff;
	rdev->data_offset = le64_to_cpu(sb->data_offset);
1118
	atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds's avatar
Linus Torvalds committed
1119

1120
	rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1121
	bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
1122
	if (rdev->sb_size & bmask)
1123 1124 1125
		rdev->sb_size = (rdev->sb_size | bmask) + 1;

	if (minor_version
1126
	    && rdev->data_offset < sb_start + (rdev->sb_size/512))
1127
		return -EINVAL;
1128

1129 1130 1131 1132 1133
	if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
		rdev->desc_nr = -1;
	else
		rdev->desc_nr = le32_to_cpu(sb->dev_number);

1134
	if (!refdev) {
1135
		ret = 1;
1136
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
		__u64 ev1, ev2;
		struct mdp_superblock_1 *refsb = 
			(struct mdp_superblock_1*)page_address(refdev->sb_page);

		if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
		    sb->level != refsb->level ||
		    sb->layout != refsb->layout ||
		    sb->chunksize != refsb->chunksize) {
			printk(KERN_WARNING "md: %s has strangely different"
				" superblock to %s\n",
				bdevname(rdev->bdev,b),
				bdevname(refdev->bdev,b2));
			return -EINVAL;
		}
		ev1 = le64_to_cpu(sb->events);
		ev2 = le64_to_cpu(refsb->events);

		if (ev1 > ev2)
1155 1156 1157
			ret = 1;
		else
			ret = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1158
	}
1159
	if (minor_version)
Linus Torvalds's avatar
Linus Torvalds committed
1160 1161
		rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
	else
1162
		rdev->size = rdev->sb_start / 2;
Linus Torvalds's avatar
Linus Torvalds committed
1163 1164 1165 1166 1167
	if (rdev->size < le64_to_cpu(sb->data_size)/2)
		return -EINVAL;
	rdev->size = le64_to_cpu(sb->data_size)/2;
	if (le32_to_cpu(sb->chunksize))
		rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1168

1169
	if (le64_to_cpu(sb->size) > rdev->size*2)
1170
		return -EINVAL;
1171
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
1172 1173 1174 1175 1176
}

static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
{
	struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1177
	__u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds's avatar
Linus Torvalds committed
1178

1179
	rdev->raid_disk = -1;
1180 1181 1182 1183 1184
	clear_bit(Faulty, &rdev->flags);
	clear_bit(In_sync, &rdev->flags);
	clear_bit(WriteMostly, &rdev->flags);
	clear_bit(BarriersNotsupp, &rdev->flags);

Linus Torvalds's avatar
Linus Torvalds committed
1185 1186 1187
	if (mddev->raid_disks == 0) {
		mddev->major_version = 1;
		mddev->patch_version = 0;
1188
		mddev->external = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1189 1190 1191 1192
		mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
		mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
		mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
		mddev->level = le32_to_cpu(sb->level);
1193
		mddev->clevel[0] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1194 1195 1196
		mddev->layout = le32_to_cpu(sb->layout);
		mddev->raid_disks = le32_to_cpu(sb->raid_disks);
		mddev->size = le64_to_cpu(sb->size)/2;
1197
		mddev->events = ev1;
1198
		mddev->bitmap_offset = 0;
1199
		mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds's avatar
Linus Torvalds committed
1200 1201 1202 1203 1204
		
		mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
		memcpy(mddev->uuid, sb->set_uuid, 16);

		mddev->max_disks =  (4096-256)/2;
1205

1206
		if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1207
		    mddev->bitmap_file == NULL )
1208
			mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1209

1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
		if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
			mddev->reshape_position = le64_to_cpu(sb->reshape_position);
			mddev->delta_disks = le32_to_cpu(sb->delta_disks);
			mddev->new_level = le32_to_cpu(sb->new_level);
			mddev->new_layout = le32_to_cpu(sb->new_layout);
			mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
		} else {
			mddev->reshape_position = MaxSector;
			mddev->delta_disks = 0;
			mddev->new_level = mddev->level;
			mddev->new_layout = mddev->layout;
			mddev->new_chunk = mddev->chunk_size;
		}

1224 1225
	} else if (mddev->pers == NULL) {
		/* Insist of good event counter while assembling */
Linus Torvalds's avatar
Linus Torvalds committed
1226 1227 1228
		++ev1;
		if (ev1 < mddev->events)
			return -EINVAL;
1229 1230 1231 1232 1233 1234
	} else if (mddev->bitmap) {
		/* If adding to array with a bitmap, then we can accept an
		 * older device, but not too old.
		 */
		if (ev1 < mddev->bitmap->events_cleared)
			return 0;
1235 1236 1237 1238 1239
	} else {
		if (ev1 < mddev->events)
			/* just a hot-add of a new device, leave raid_disk at -1 */
			return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1240 1241 1242 1243 1244 1245 1246
	if (mddev->level != LEVEL_MULTIPATH) {
		int role;
		role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
		switch(role) {
		case 0xffff: /* spare */
			break;
		case 0xfffe: /* faulty */
1247
			set_bit(Faulty, &rdev->flags);
Linus Torvalds's avatar
Linus Torvalds committed
1248 1249
			break;
		default:
1250 1251 1252 1253 1254
			if ((le32_to_cpu(sb->feature_map) &
			     MD_FEATURE_RECOVERY_OFFSET))
				rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
			else
				set_bit(In_sync, &rdev->flags);
Linus Torvalds's avatar
Linus Torvalds committed
1255 1256 1257
			rdev->raid_disk = role;
			break;
		}
1258 1259
		if (sb->devflags & WriteMostly1)
			set_bit(WriteMostly, &rdev->flags);
1260
	} else /* MULTIPATH are always insync */
1261
		set_bit(In_sync, &rdev->flags);
1262

Linus Torvalds's avatar
Linus Torvalds committed
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
	return 0;
}

static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
{
	struct mdp_superblock_1 *sb;
	struct list_head *tmp;
	mdk_rdev_t *rdev2;
	int max_dev, i;
	/* make rdev->sb match mddev and rdev data. */

	sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);

	sb->feature_map = 0;
	sb->pad0 = 0;
1278
	sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds's avatar
Linus Torvalds committed
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
	memset(sb->pad1, 0, sizeof(sb->pad1));
	memset(sb->pad2, 0, sizeof(sb->pad2));
	memset(sb->pad3, 0, sizeof(sb->pad3));

	sb->utime = cpu_to_le64((__u64)mddev->utime);
	sb->events = cpu_to_le64(mddev->events);
	if (mddev->in_sync)
		sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
	else
		sb->resync_offset = cpu_to_le64(0);

1290
	sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1291

NeilBrown's avatar