md.c 177 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
   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.
*/

35
#include <linux/kthread.h>
36
#include <linux/blkdev.h>
Linus Torvalds's avatar
Linus Torvalds committed
37
#include <linux/sysctl.h>
38
#include <linux/seq_file.h>
Linus Torvalds's avatar
Linus Torvalds committed
39
#include <linux/buffer_head.h> /* for invalidate_bdev */
40
#include <linux/poll.h>
41
#include <linux/ctype.h>
42 43 44 45
#include <linux/hdreg.h>
#include <linux/proc_fs.h>
#include <linux/random.h>
#include <linux/reboot.h>
46
#include <linux/file.h>
47
#include <linux/delay.h>
48 49
#include <linux/raid/md_p.h>
#include <linux/raid/md_u.h>
50
#include "md.h"
51
#include "bitmap.h"
Linus Torvalds's avatar
Linus Torvalds committed
52 53 54 55 56 57

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


#ifndef MODULE
58
static void autostart_arrays(int part);
Linus Torvalds's avatar
Linus Torvalds committed
59 60
#endif

61
static LIST_HEAD(pers_list);
Linus Torvalds's avatar
Linus Torvalds committed
62 63
static DEFINE_SPINLOCK(pers_lock);

64 65
static void md_print_devices(void);

66 67
static DECLARE_WAIT_QUEUE_HEAD(resync_wait);

68 69
#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
70 71 72 73
/*
 * 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
74
 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds's avatar
Linus Torvalds committed
75 76 77 78 79
 * 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.
80
 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds's avatar
Linus Torvalds committed
81 82 83 84
 */

static int sysctl_speed_limit_min = 1000;
static int sysctl_speed_limit_max = 200000;
85 86 87 88 89 90 91 92 93 94 95
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
96 97 98 99 100 101 102 103 104

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),
105
		.mode		= S_IRUGO|S_IWUSR,
Linus Torvalds's avatar
Linus Torvalds committed
106 107 108 109 110 111 112
		.proc_handler	= &proc_dointvec,
	},
	{
		.ctl_name	= DEV_RAID_SPEED_LIMIT_MAX,
		.procname	= "speed_limit_max",
		.data		= &sysctl_speed_limit_max,
		.maxlen		= sizeof(int),
113
		.mode		= S_IRUGO|S_IWUSR,
Linus Torvalds's avatar
Linus Torvalds committed
114 115 116 117 118 119 120 121 122 123
		.proc_handler	= &proc_dointvec,
	},
	{ .ctl_name = 0 }
};

static ctl_table raid_dir_table[] = {
	{
		.ctl_name	= DEV_RAID,
		.procname	= "raid",
		.maxlen		= 0,
124
		.mode		= S_IRUGO|S_IXUGO,
Linus Torvalds's avatar
Linus Torvalds committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
		.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;

143 144
static int start_readonly;

145 146 147 148 149 150 151 152 153 154
/*
 * 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
 */
155
static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
156
static atomic_t md_event_count;
157
void md_new_event(mddev_t *mddev)
158 159 160 161
{
	atomic_inc(&md_event_count);
	wake_up(&md_event_waiters);
}
162
EXPORT_SYMBOL_GPL(md_new_event);
163

164 165 166
/* Alternate version that can be called from interrupts
 * when calling sysfs_notify isn't needed.
 */
167
static void md_new_event_inintr(mddev_t *mddev)
168 169 170 171 172
{
	atomic_inc(&md_event_count);
	wake_up(&md_event_waiters);
}

Linus Torvalds's avatar
Linus Torvalds committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
/*
 * 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.
 */
188
#define for_each_mddev(mddev,tmp)					\
Linus Torvalds's avatar
Linus Torvalds committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
									\
	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;})					\
		)


204 205 206 207 208 209 210 211
/* Rather than calling directly into the personality make_request function,
 * IO requests come here first so that we can check if the device is
 * being suspended pending a reconfiguration.
 * We hold a refcount over the call to ->make_request.  By the time that
 * call has finished, the bio has been linked into some internal structure
 * and so is visible to ->quiesce(), so we don't need the refcount any more.
 */
static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds's avatar
Linus Torvalds committed
212
{
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
	mddev_t *mddev = q->queuedata;
	int rv;
	if (mddev == NULL || mddev->pers == NULL) {
		bio_io_error(bio);
		return 0;
	}
	rcu_read_lock();
	if (mddev->suspended) {
		DEFINE_WAIT(__wait);
		for (;;) {
			prepare_to_wait(&mddev->sb_wait, &__wait,
					TASK_UNINTERRUPTIBLE);
			if (!mddev->suspended)
				break;
			rcu_read_unlock();
			schedule();
			rcu_read_lock();
		}
		finish_wait(&mddev->sb_wait, &__wait);
	}
	atomic_inc(&mddev->active_io);
	rcu_read_unlock();
	rv = mddev->pers->make_request(q, bio);
	if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
		wake_up(&mddev->sb_wait);

	return rv;
}

static void mddev_suspend(mddev_t *mddev)
{
	BUG_ON(mddev->suspended);
	mddev->suspended = 1;
	synchronize_rcu();
	wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
	mddev->pers->quiesce(mddev, 1);
	md_unregister_thread(mddev->thread);
	mddev->thread = NULL;
	/* we now know that no code is executing in the personality module,
	 * except possibly the tail end of a ->bi_end_io function, but that
	 * is certain to complete before the module has a chance to get
	 * unloaded
	 */
}

static void mddev_resume(mddev_t *mddev)
{
	mddev->suspended = 0;
	wake_up(&mddev->sb_wait);
	mddev->pers->quiesce(mddev, 0);
Linus Torvalds's avatar
Linus Torvalds committed
263 264
}

265

Linus Torvalds's avatar
Linus Torvalds committed
266 267 268 269 270 271
static inline mddev_t *mddev_get(mddev_t *mddev)
{
	atomic_inc(&mddev->active);
	return mddev;
}

272
static void mddev_delayed_delete(struct work_struct *ws);
273

Linus Torvalds's avatar
Linus Torvalds committed
274 275 276 277
static void mddev_put(mddev_t *mddev)
{
	if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
		return;
278 279
	if (!mddev->raid_disks && list_empty(&mddev->disks) &&
	    !mddev->hold_active) {
Linus Torvalds's avatar
Linus Torvalds committed
280
		list_del(&mddev->all_mddevs);
281 282 283 284 285 286 287 288 289 290 291 292 293
		if (mddev->gendisk) {
			/* we did a probe so need to clean up.
			 * Call schedule_work inside the spinlock
			 * so that flush_scheduled_work() after
			 * mddev_find will succeed in waiting for the
			 * work to be done.
			 */
			INIT_WORK(&mddev->del_work, mddev_delayed_delete);
			schedule_work(&mddev->del_work);
		} else
			kfree(mddev);
	}
	spin_unlock(&all_mddevs_lock);
Linus Torvalds's avatar
Linus Torvalds committed
294 295 296 297 298 299 300 301
}

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

 retry:
	spin_lock(&all_mddevs_lock);
302 303 304 305 306 307 308 309 310 311 312 313

	if (unit) {
		list_for_each_entry(mddev, &all_mddevs, all_mddevs)
			if (mddev->unit == unit) {
				mddev_get(mddev);
				spin_unlock(&all_mddevs_lock);
				kfree(new);
				return mddev;
			}

		if (new) {
			list_add(&new->all_mddevs, &all_mddevs);
Linus Torvalds's avatar
Linus Torvalds committed
314
			spin_unlock(&all_mddevs_lock);
315 316
			new->hold_active = UNTIL_IOCTL;
			return new;
Linus Torvalds's avatar
Linus Torvalds committed
317
		}
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
	} else if (new) {
		/* find an unused unit number */
		static int next_minor = 512;
		int start = next_minor;
		int is_free = 0;
		int dev = 0;
		while (!is_free) {
			dev = MKDEV(MD_MAJOR, next_minor);
			next_minor++;
			if (next_minor > MINORMASK)
				next_minor = 0;
			if (next_minor == start) {
				/* Oh dear, all in use. */
				spin_unlock(&all_mddevs_lock);
				kfree(new);
				return NULL;
			}
				
			is_free = 1;
			list_for_each_entry(mddev, &all_mddevs, all_mddevs)
				if (mddev->unit == dev) {
					is_free = 0;
					break;
				}
		}
		new->unit = dev;
		new->md_minor = MINOR(dev);
		new->hold_active = UNTIL_STOP;
Linus Torvalds's avatar
Linus Torvalds committed
346 347 348 349 350 351
		list_add(&new->all_mddevs, &all_mddevs);
		spin_unlock(&all_mddevs_lock);
		return new;
	}
	spin_unlock(&all_mddevs_lock);

352
	new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
353 354 355 356 357 358 359 360 361
	if (!new)
		return NULL;

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

362
	mutex_init(&new->reconfig_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
363 364 365 366
	INIT_LIST_HEAD(&new->disks);
	INIT_LIST_HEAD(&new->all_mddevs);
	init_timer(&new->safemode_timer);
	atomic_set(&new->active, 1);
367
	atomic_set(&new->openers, 0);
368
	atomic_set(&new->active_io, 0);
369
	spin_lock_init(&new->write_lock);
370
	init_waitqueue_head(&new->sb_wait);
371
	init_waitqueue_head(&new->recovery_wait);
372
	new->reshape_position = MaxSector;
373
	new->resync_min = 0;
374
	new->resync_max = MaxSector;
375
	new->level = LEVEL_NONE;
Linus Torvalds's avatar
Linus Torvalds committed
376 377 378 379 380 381

	goto retry;
}

static inline int mddev_lock(mddev_t * mddev)
{
382
	return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
383 384
}

385 386 387 388 389
static inline int mddev_is_locked(mddev_t *mddev)
{
	return mutex_is_locked(&mddev->reconfig_mutex);
}

Linus Torvalds's avatar
Linus Torvalds committed
390 391
static inline int mddev_trylock(mddev_t * mddev)
{
392
	return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
393 394 395 396
}

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

399
	md_wakeup_thread(mddev->thread);
Linus Torvalds's avatar
Linus Torvalds committed
400 401
}

402
static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds's avatar
Linus Torvalds committed
403
{
404
	mdk_rdev_t *rdev;
Linus Torvalds's avatar
Linus Torvalds committed
405

406
	list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds's avatar
Linus Torvalds committed
407 408
		if (rdev->desc_nr == nr)
			return rdev;
409

Linus Torvalds's avatar
Linus Torvalds committed
410 411 412 413 414 415 416
	return NULL;
}

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

417
	list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds's avatar
Linus Torvalds committed
418 419
		if (rdev->bdev->bd_dev == dev)
			return rdev;
420

Linus Torvalds's avatar
Linus Torvalds committed
421 422 423
	return NULL;
}

424
static struct mdk_personality *find_pers(int level, char *clevel)
425 426
{
	struct mdk_personality *pers;
427 428
	list_for_each_entry(pers, &pers_list, list) {
		if (level != LEVEL_NONE && pers->level == level)
429
			return pers;
430 431 432
		if (strcmp(pers->name, clevel)==0)
			return pers;
	}
433 434 435
	return NULL;
}

436
/* return the offset of the super block in 512byte sectors */
437
static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds's avatar
Linus Torvalds committed
438
{
439 440
	sector_t num_sectors = bdev->bd_inode->i_size / 512;
	return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds's avatar
Linus Torvalds committed
441 442
}

443
static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size)
Linus Torvalds's avatar
Linus Torvalds committed
444
{
445
	sector_t num_sectors = rdev->sb_start;
Linus Torvalds's avatar
Linus Torvalds committed
446 447

	if (chunk_size)
448 449
		num_sectors &= ~((sector_t)chunk_size/512 - 1);
	return num_sectors;
Linus Torvalds's avatar
Linus Torvalds committed
450 451 452 453 454 455 456 457 458 459
}

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");
460
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
461 462 463 464 465 466 467 468
	}

	return 0;
}

static void free_disk_sb(mdk_rdev_t * rdev)
{
	if (rdev->sb_page) {
469
		put_page(rdev->sb_page);
Linus Torvalds's avatar
Linus Torvalds committed
470 471
		rdev->sb_loaded = 0;
		rdev->sb_page = NULL;
472
		rdev->sb_start = 0;
473
		rdev->sectors = 0;
Linus Torvalds's avatar
Linus Torvalds committed
474 475 476 477
	}
}


478
static void super_written(struct bio *bio, int error)
479 480
{
	mdk_rdev_t *rdev = bio->bi_private;
481
	mddev_t *mddev = rdev->mddev;
482

483 484 485 486
	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));
487
		md_error(mddev, rdev);
488
	}
489

490 491
	if (atomic_dec_and_test(&mddev->pending_writes))
		wake_up(&mddev->sb_wait);
Neil Brown's avatar
Neil Brown committed
492
	bio_put(bio);
493 494
}

495
static void super_written_barrier(struct bio *bio, int error)
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
{
	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);
513 514 515 516
	} else {
		bio_put(bio2);
		bio->bi_private = rdev;
		super_written(bio, error);
517 518 519
	}
}

520 521 522 523 524 525 526 527
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
528 529 530
	 *
	 * As we might need to resubmit the request if BIO_RW_BARRIER
	 * causes ENOTSUPP, we allocate a spare bio...
531 532
	 */
	struct bio *bio = bio_alloc(GFP_NOIO, 1);
533
	int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
534 535 536 537 538 539

	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;
540 541
	bio->bi_rw = rw;

542
	atomic_inc(&mddev->pending_writes);
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
	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);
576 577
}

578
static void bi_complete(struct bio *bio, int error)
Linus Torvalds's avatar
Linus Torvalds committed
579 580 581 582
{
	complete((struct completion*)bio->bi_private);
}

583
int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds's avatar
Linus Torvalds committed
584 585
		   struct page *page, int rw)
{
586
	struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds's avatar
Linus Torvalds committed
587 588 589
	struct completion event;
	int ret;

590
	rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds's avatar
Linus Torvalds committed
591 592 593 594 595 596 597 598 599 600 601 602 603 604

	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;
}
605
EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds's avatar
Linus Torvalds committed
606

607
static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds's avatar
Linus Torvalds committed
608 609 610 611 612 613 614 615 616 617
{
	char b[BDEVNAME_SIZE];
	if (!rdev->sb_page) {
		MD_BUG();
		return -EINVAL;
	}
	if (rdev->sb_loaded)
		return 0;


618
	if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds's avatar
Linus Torvalds committed
619 620 621 622 623 624 625 626 627 628 629 630
		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
631 632 633 634
	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
635 636 637 638 639 640 641 642 643 644 645 646
}

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;
647
		printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds's avatar
Linus Torvalds committed
648 649 650 651 652 653 654 655 656 657 658 659
		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
660
	ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds's avatar
Linus Torvalds committed
661
abort:
662 663
	kfree(tmp1);
	kfree(tmp2);
Linus Torvalds's avatar
Linus Torvalds committed
664 665 666
	return ret;
}

667 668 669 670 671 672 673

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

Linus Torvalds's avatar
Linus Torvalds committed
674 675
static unsigned int calc_sb_csum(mdp_super_t * sb)
{
676 677 678
	u64 newcsum = 0;
	u32 *sb32 = (u32*)sb;
	int i;
Linus Torvalds's avatar
Linus Torvalds committed
679 680 681 682
	unsigned int disk_csum, csum;

	disk_csum = sb->sb_csum;
	sb->sb_csum = 0;
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699

	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
700
	sb->sb_csum = disk_csum;
701
#endif
Linus Torvalds's avatar
Linus Torvalds committed
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
	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  {
737 738 739 740 741 742 743
	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,
744
						sector_t num_sectors);
Linus Torvalds's avatar
Linus Torvalds committed
745 746 747 748 749 750 751 752 753 754 755 756
};

/*
 * 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;

	/*
757
	 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds's avatar
Linus Torvalds committed
758 759 760 761
	 * it's at the end of the disk.
	 *
	 * It also happens to be a multiple of 4Kb.
	 */
762
	rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds's avatar
Linus Torvalds committed
763

764
	ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds's avatar
Linus Torvalds committed
765 766 767 768 769 770 771 772 773 774 775 776 777 778
	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 ||
779 780
	    sb->minor_version < 90 ||
	    sb->minor_version > 91) {
Linus Torvalds's avatar
Linus Torvalds committed
781 782 783 784 785 786 787 788 789
		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;

790
	if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds's avatar
Linus Torvalds committed
791 792 793 794 795 796 797
		printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
			b);
		goto abort;
	}

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

800 801 802 803 804 805 806 807 808 809 810
	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
811 812 813 814 815
	if (sb->level == LEVEL_MULTIPATH)
		rdev->desc_nr = -1;
	else
		rdev->desc_nr = sb->this_disk.number;

816
	if (!refdev) {
Linus Torvalds's avatar
Linus Torvalds committed
817
		ret = 1;
818
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
		__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;
	}
839
	rdev->sectors = calc_num_sectors(rdev, sb->chunk_size);
Linus Torvalds's avatar
Linus Torvalds committed
840

841
	if (rdev->sectors < sb->size * 2 && sb->level > 1)
842 843 844
		/* "this cannot possibly happen" ... */
		ret = -EINVAL;

Linus Torvalds's avatar
Linus Torvalds committed
845 846 847 848 849 850 851 852 853 854 855
 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);
856
	__u64 ev1 = md_event(sb);
Linus Torvalds's avatar
Linus Torvalds committed
857

858
	rdev->raid_disk = -1;
859 860 861 862 863
	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
864 865 866 867
	if (mddev->raid_disks == 0) {
		mddev->major_version = 0;
		mddev->minor_version = sb->minor_version;
		mddev->patch_version = sb->patch_version;
868
		mddev->external = 0;
Linus Torvalds's avatar
Linus Torvalds committed
869 870 871 872
		mddev->chunk_size = sb->chunk_size;
		mddev->ctime = sb->ctime;
		mddev->utime = sb->utime;
		mddev->level = sb->level;
873
		mddev->clevel[0] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
874 875
		mddev->layout = sb->layout;
		mddev->raid_disks = sb->raid_disks;
876
		mddev->dev_sectors = sb->size * 2;
877
		mddev->events = ev1;
878
		mddev->bitmap_offset = 0;
879
		mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds's avatar
Linus Torvalds committed
880

881 882 883 884 885 886 887 888 889 890 891 892 893 894
		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
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
		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;
911 912

		if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
913
		    mddev->bitmap_file == NULL)
914
			mddev->bitmap_offset = mddev->default_bitmap_offset;
915

916 917
	} else if (mddev->pers == NULL) {
		/* Insist on good event counter while assembling */
Linus Torvalds's avatar
Linus Torvalds committed
918 919 920
		++ev1;
		if (ev1 < mddev->events) 
			return -EINVAL;
921 922 923 924 925 926
	} 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;
927 928 929 930 931
	} else {
		if (ev1 < mddev->events)
			/* just a hot-add of a new device, leave raid_disk at -1 */
			return 0;
	}
932

Linus Torvalds's avatar
Linus Torvalds committed
933 934 935 936
	if (mddev->level != LEVEL_MULTIPATH) {
		desc = sb->disks + rdev->desc_nr;

		if (desc->state & (1<<MD_DISK_FAULTY))
937
			set_bit(Faulty, &rdev->flags);
938 939
		else if (desc->state & (1<<MD_DISK_SYNC) /* &&
			    desc->raid_disk < mddev->raid_disks */) {
940
			set_bit(In_sync, &rdev->flags);
Linus Torvalds's avatar
Linus Torvalds committed
941 942
			rdev->raid_disk = desc->raid_disk;
		}
943 944
		if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
			set_bit(WriteMostly, &rdev->flags);
945
	} else /* MULTIPATH are always insync */
946
		set_bit(In_sync, &rdev->flags);
Linus Torvalds's avatar
Linus Torvalds committed
947 948 949 950 951 952 953 954 955 956 957
	return 0;
}

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

Linus Torvalds's avatar
Linus Torvalds committed
959 960 961 962 963 964 965 966 967 968 969 970 971 972

	/* 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;

973 974
	rdev->sb_size = MD_SB_BYTES;

Linus Torvalds's avatar
Linus Torvalds committed
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
	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;
990
	sb->size = mddev->dev_sectors / 2;
Linus Torvalds's avatar
Linus Torvalds committed
991 992
	sb->raid_disks = mddev->raid_disks;
	sb->md_minor = mddev->md_minor;
993
	sb->not_persistent = 0;
Linus Torvalds's avatar
Linus Torvalds committed
994 995 996 997 998
	sb->utime = mddev->utime;
	sb->state = 0;
	sb->events_hi = (mddev->events>>32);
	sb->events_lo = (u32)mddev->events;

999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
	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
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
	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;

1023 1024 1025
	if (mddev->bitmap && mddev->bitmap_file == NULL)
		sb->state |= (1<<MD_SB_BITMAP_PRESENT);

Linus Torvalds's avatar
Linus Torvalds committed
1026
	sb->disks[0].state = (1<<MD_DISK_REMOVED);
1027
	list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds's avatar
Linus Torvalds committed
1028
		mdp_disk_t *d;
1029
		int desc_nr;
1030 1031
		if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
		    && !test_bit(Faulty, &rdev2->flags))
1032
			desc_nr = rdev2->raid_disk;
Linus Torvalds's avatar
Linus Torvalds committed
1033
		else
1034
			desc_nr = next_spare++;
1035
		rdev2->desc_nr = desc_nr;
Linus Torvalds's avatar
Linus Torvalds committed
1036 1037 1038 1039 1040
		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);
1041 1042
		if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
		    && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds's avatar
Linus Torvalds committed
1043 1044 1045
			d->raid_disk = rdev2->raid_disk;
		else
			d->raid_disk = rdev2->desc_nr; /* compatibility */
1046
		if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds's avatar
Linus Torvalds committed
1047
			d->state = (1<<MD_DISK_FAULTY);
1048
		else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds's avatar
Linus Torvalds committed
1049 1050 1051 1052 1053 1054 1055 1056 1057
			d->state = (1<<MD_DISK_ACTIVE);
			d->state |= (1<<MD_DISK_SYNC);
			active++;
			working++;
		} else {
			d->state = 0;
			spare++;
			working++;
		}
1058 1059
		if (test_bit(WriteMostly, &rdev2->flags))
			d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds's avatar
Linus Torvalds committed
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
	}
	/* 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);
}

1082 1083 1084 1085
/*
 * rdev_size_change for 0.90.0
 */
static unsigned long long
1086
super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1087
{
1088
	if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1089 1090 1091
		return 0; /* component must fit device */
	if (rdev->mddev->bitmap_offset)
		return 0; /* can't move bitmap */
1092
	rdev->sb_start = calc_dev_sboffset(rdev->bdev);
1093 1094
	if (!num_sectors || num_sectors > rdev->sb_start)
		num_sectors = rdev->sb_start;
1095
	md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1096 1097
		       rdev->sb_page);
	md_super_wait(rdev->mddev);
1098
	return num_sectors / 2; /* kB for sysfs */
1099 1100 1101
}


Linus Torvalds's avatar
Linus Torvalds committed
1102 1103 1104 1105
/*
 * version 1 superblock
 */

1106
static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds's avatar
Linus Torvalds committed
1107
{
1108 1109
	__le32 disk_csum;
	u32 csum;
Linus Torvalds's avatar
Linus Torvalds committed
1110 1111
	unsigned long long newcsum;
	int size = 256 + le32_to_cpu(sb->max_dev)*2;
1112
	__le32 *isuper = (__le32*)sb;
Linus Torvalds's avatar
Linus Torvalds committed
1113 1114 1115 1116 1117 1118 1119 1120 1121
	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)
1122
		newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds's avatar
Linus Torvalds committed
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132

	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;
1133
	sector_t sb_start;
Linus Torvalds's avatar
Linus Torvalds committed
1134
	char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1135
	int bmask;
Linus Torvalds's avatar
Linus Torvalds committed
1136 1137

	/*
1138
	 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds's avatar
Linus Torvalds committed
1139 1140 1141 1142 1143 1144 1145 1146
	 * 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:
1147 1148 1149
		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
1150 1151
		break;
	case 1:
1152
		sb_start = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1153 1154
		break;
	case 2:
1155
		sb_start = 8;
Linus Torvalds's avatar
Linus Torvalds committed
1156 1157 1158 1159
		break;
	default:
		return -EINVAL;
	}
1160
	rdev->sb_start = sb_start;
Linus Torvalds's avatar
Linus Torvalds committed
1161

1162 1163 1164 1165
	/* 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
1166 1167 1168 1169 1170 1171 1172 1173
	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 ||
1174
	    le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1175
	    (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds's avatar
Linus Torvalds committed
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
		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;
	}
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
	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
1200 1201
	rdev->preferred_minor = 0xffff;
	rdev->data_offset = le64_to_cpu(sb->data_offset);
1202
	atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds's avatar
Linus Torvalds committed
1203

1204
	rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1205
	bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1206
	if (rdev->sb_size & bmask)