3108 lines · 82.9 KB · C
RawDownload
/* * raid10.c : Multiple Devices driver for Linux * * Copyright (C) 2000-2004 Neil Brown * * RAID-10 support for md. * * Base on code in raid1.c.  See raid1.c for further copyright information. * * * 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/slab.h>#include <linux/delay.h>#include <linux/blkdev.h>#include <linux/module.h>#include <linux/seq_file.h>#include <linux/ratelimit.h>#include "md.h"#include "raid10.h"#include "raid0.h"#include "bitmap.h" /* * RAID10 provides a combination of RAID0 and RAID1 functionality. * The layout of data is defined by *    chunk_size *    raid_disks *    near_copies (stored in low byte of layout) *    far_copies (stored in second byte of layout) *    far_offset (stored in bit 16 of layout ) * * The data to be stored is divided into chunks using chunksize. * Each device is divided into far_copies sections. * In each section, chunks are laid out in a style similar to raid0, but * near_copies copies of each chunk is stored (each on a different drive). * The starting device for each section is offset near_copies from the starting * device of the previous section. * Thus they are (near_copies*far_copies) of each chunk, and each is on a different * drive. * near_copies and far_copies must be at least one, and their product is at most * raid_disks. * * If far_offset is true, then the far_copies are handled a bit differently. * The copies are still in different stripes, but instead of be very far apart * on disk, there are adjacent stripes. */ /* * Number of guaranteed r10bios in case of extreme VM load: */#define	NR_RAID10_BIOS 256 /* When there are this many requests queue to be written by * the raid10 thread, we become 'congested' to provide back-pressure * for writeback. */static int max_queued_requests = 1024; static void allow_barrier(struct r10conf *conf);static void lower_barrier(struct r10conf *conf); static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data){	struct r10conf *conf = data;	int size = offsetof(struct r10bio, devs[conf->copies]); 	/* allocate a r10bio with room for raid_disks entries in the bios array */	return kzalloc(size, gfp_flags);} static void r10bio_pool_free(void *r10_bio, void *data){	kfree(r10_bio);} /* Maximum size of each resync request */#define RESYNC_BLOCK_SIZE (64*1024)#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)/* amount of memory to reserve for resync requests */#define RESYNC_WINDOW (1024*1024)/* maximum number of concurrent requests, memory permitting */#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE) /* * When performing a resync, we need to read and compare, so * we need as many pages are there are copies. * When performing a recovery, we need 2 bios, one for read, * one for write (we recover only one drive per r10buf) * */static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data){	struct r10conf *conf = data;	struct page *page;	struct r10bio *r10_bio;	struct bio *bio;	int i, j;	int nalloc; 	r10_bio = r10bio_pool_alloc(gfp_flags, conf);	if (!r10_bio)		return NULL; 	if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))		nalloc = conf->copies; /* resync */	else		nalloc = 2; /* recovery */ 	/*	 * Allocate bios.	 */	for (j = nalloc ; j-- ; ) {		bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);		if (!bio)			goto out_free_bio;		r10_bio->devs[j].bio = bio;	}	/*	 * Allocate RESYNC_PAGES data pages and attach them	 * where needed.	 */	for (j = 0 ; j < nalloc; j++) {		bio = r10_bio->devs[j].bio;		for (i = 0; i < RESYNC_PAGES; i++) {			if (j == 1 && !test_bit(MD_RECOVERY_SYNC,						&conf->mddev->recovery)) {				/* we can share bv_page's during recovery */				struct bio *rbio = r10_bio->devs[0].bio;				page = rbio->bi_io_vec[i].bv_page;				get_page(page);			} else				page = alloc_page(gfp_flags);			if (unlikely(!page))				goto out_free_pages; 			bio->bi_io_vec[i].bv_page = page;		}	} 	return r10_bio; out_free_pages:	for ( ; i > 0 ; i--)		safe_put_page(bio->bi_io_vec[i-1].bv_page);	while (j--)		for (i = 0; i < RESYNC_PAGES ; i++)			safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);	j = -1;out_free_bio:	while ( ++j < nalloc )		bio_put(r10_bio->devs[j].bio);	r10bio_pool_free(r10_bio, conf);	return NULL;} static void r10buf_pool_free(void *__r10_bio, void *data){	int i;	struct r10conf *conf = data;	struct r10bio *r10bio = __r10_bio;	int j; 	for (j=0; j < conf->copies; j++) {		struct bio *bio = r10bio->devs[j].bio;		if (bio) {			for (i = 0; i < RESYNC_PAGES; i++) {				safe_put_page(bio->bi_io_vec[i].bv_page);				bio->bi_io_vec[i].bv_page = NULL;			}			bio_put(bio);		}	}	r10bio_pool_free(r10bio, conf);} static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio){	int i; 	for (i = 0; i < conf->copies; i++) {		struct bio **bio = & r10_bio->devs[i].bio;		if (!BIO_SPECIAL(*bio))			bio_put(*bio);		*bio = NULL;	}} static void free_r10bio(struct r10bio *r10_bio){	struct r10conf *conf = r10_bio->mddev->private; 	put_all_bios(conf, r10_bio);	mempool_free(r10_bio, conf->r10bio_pool);} static void put_buf(struct r10bio *r10_bio){	struct r10conf *conf = r10_bio->mddev->private; 	mempool_free(r10_bio, conf->r10buf_pool); 	lower_barrier(conf);} static void reschedule_retry(struct r10bio *r10_bio){	unsigned long flags;	struct mddev *mddev = r10_bio->mddev;	struct r10conf *conf = mddev->private; 	spin_lock_irqsave(&conf->device_lock, flags);	list_add(&r10_bio->retry_list, &conf->retry_list);	conf->nr_queued ++;	spin_unlock_irqrestore(&conf->device_lock, flags); 	/* wake up frozen array... */	wake_up(&conf->wait_barrier); 	md_wakeup_thread(mddev->thread);} /* * raid_end_bio_io() is called when we have finished servicing a mirrored * operation and are ready to return a success/failure code to the buffer * cache layer. */static void raid_end_bio_io(struct r10bio *r10_bio){	struct bio *bio = r10_bio->master_bio;	int done;	struct r10conf *conf = r10_bio->mddev->private; 	if (bio->bi_phys_segments) {		unsigned long flags;		spin_lock_irqsave(&conf->device_lock, flags);		bio->bi_phys_segments--;		done = (bio->bi_phys_segments == 0);		spin_unlock_irqrestore(&conf->device_lock, flags);	} else		done = 1;	if (!test_bit(R10BIO_Uptodate, &r10_bio->state))		clear_bit(BIO_UPTODATE, &bio->bi_flags);	if (done) {		bio_endio(bio, 0);		/*		 * Wake up any possible resync thread that waits for the device		 * to go idle.		 */		allow_barrier(conf);	}	free_r10bio(r10_bio);} /* * Update disk head position estimator based on IRQ completion info. */static inline void update_head_pos(int slot, struct r10bio *r10_bio){	struct r10conf *conf = r10_bio->mddev->private; 	conf->mirrors[r10_bio->devs[slot].devnum].head_position =		r10_bio->devs[slot].addr + (r10_bio->sectors);} /* * Find the disk number which triggered given bio */static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,			 struct bio *bio, int *slotp){	int slot; 	for (slot = 0; slot < conf->copies; slot++)		if (r10_bio->devs[slot].bio == bio)			break; 	BUG_ON(slot == conf->copies);	update_head_pos(slot, r10_bio); 	if (slotp)		*slotp = slot;	return r10_bio->devs[slot].devnum;} static void raid10_end_read_request(struct bio *bio, int error){	int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);	struct r10bio *r10_bio = bio->bi_private;	int slot, dev;	struct r10conf *conf = r10_bio->mddev->private;  	slot = r10_bio->read_slot;	dev = r10_bio->devs[slot].devnum;	/*	 * this branch is our 'one mirror IO has finished' event handler:	 */	update_head_pos(slot, r10_bio); 	if (uptodate) {		/*		 * Set R10BIO_Uptodate in our master bio, so that		 * we will return a good error code to the higher		 * levels even if IO on some other mirrored buffer fails.		 *		 * The 'master' represents the composite IO operation to		 * user-side. So if something waits for IO, then it will		 * wait for the 'master' bio.		 */		set_bit(R10BIO_Uptodate, &r10_bio->state);		raid_end_bio_io(r10_bio);		rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);	} else {		/*		 * oops, read error - keep the refcount on the rdev		 */		char b[BDEVNAME_SIZE];		printk_ratelimited(KERN_ERR				   "md/raid10:%s: %s: rescheduling sector %llu\n",				   mdname(conf->mddev),				   bdevname(conf->mirrors[dev].rdev->bdev, b),				   (unsigned long long)r10_bio->sector);		set_bit(R10BIO_ReadError, &r10_bio->state);		reschedule_retry(r10_bio);	}} static void close_write(struct r10bio *r10_bio){	/* clear the bitmap if all writes complete successfully */	bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,			r10_bio->sectors,			!test_bit(R10BIO_Degraded, &r10_bio->state),			0);	md_write_end(r10_bio->mddev);} static void one_write_done(struct r10bio *r10_bio){	if (atomic_dec_and_test(&r10_bio->remaining)) {		if (test_bit(R10BIO_WriteError, &r10_bio->state))			reschedule_retry(r10_bio);		else {			close_write(r10_bio);			if (test_bit(R10BIO_MadeGood, &r10_bio->state))				reschedule_retry(r10_bio);			else				raid_end_bio_io(r10_bio);		}	}} static void raid10_end_write_request(struct bio *bio, int error){	int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);	struct r10bio *r10_bio = bio->bi_private;	int dev;	int dec_rdev = 1;	struct r10conf *conf = r10_bio->mddev->private;	int slot; 	dev = find_bio_disk(conf, r10_bio, bio, &slot); 	/*	 * this branch is our 'one mirror IO has finished' event handler:	 */	if (!uptodate) {		set_bit(WriteErrorSeen,	&conf->mirrors[dev].rdev->flags);		set_bit(R10BIO_WriteError, &r10_bio->state);		dec_rdev = 0;	} else {		/*		 * Set R10BIO_Uptodate in our master bio, so that		 * we will return a good error code for to the higher		 * levels even if IO on some other mirrored buffer fails.		 *		 * The 'master' represents the composite IO operation to		 * user-side. So if something waits for IO, then it will		 * wait for the 'master' bio.		 */		sector_t first_bad;		int bad_sectors; 		set_bit(R10BIO_Uptodate, &r10_bio->state); 		/* Maybe we can clear some bad blocks. */		if (is_badblock(conf->mirrors[dev].rdev,				r10_bio->devs[slot].addr,				r10_bio->sectors,				&first_bad, &bad_sectors)) {			bio_put(bio);			r10_bio->devs[slot].bio = IO_MADE_GOOD;			dec_rdev = 0;			set_bit(R10BIO_MadeGood, &r10_bio->state);		}	} 	/*	 *	 * Let's see if all mirrored write operations have finished	 * already.	 */	one_write_done(r10_bio);	if (dec_rdev)		rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);}  /* * RAID10 layout manager * As well as the chunksize and raid_disks count, there are two * parameters: near_copies and far_copies. * near_copies * far_copies must be <= raid_disks. * Normally one of these will be 1. * If both are 1, we get raid0. * If near_copies == raid_disks, we get raid1. * * Chunks are laid out in raid0 style with near_copies copies of the * first chunk, followed by near_copies copies of the next chunk and * so on. * If far_copies > 1, then after 1/far_copies of the array has been assigned * as described above, we start again with a device offset of near_copies. * So we effectively have another copy of the whole array further down all * the drives, but with blocks on different drives. * With this layout, and block is never stored twice on the one device. * * raid10_find_phys finds the sector offset of a given virtual sector * on each device that it is on. * * raid10_find_virt does the reverse mapping, from a device and a * sector offset to a virtual address */ static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio){	int n,f;	sector_t sector;	sector_t chunk;	sector_t stripe;	int dev; 	int slot = 0; 	/* now calculate first sector/dev */	chunk = r10bio->sector >> conf->chunk_shift;	sector = r10bio->sector & conf->chunk_mask; 	chunk *= conf->near_copies;	stripe = chunk;	dev = sector_div(stripe, conf->raid_disks);	if (conf->far_offset)		stripe *= conf->far_copies; 	sector += stripe << conf->chunk_shift; 	/* and calculate all the others */	for (n=0; n < conf->near_copies; n++) {		int d = dev;		sector_t s = sector;		r10bio->devs[slot].addr = sector;		r10bio->devs[slot].devnum = d;		slot++; 		for (f = 1; f < conf->far_copies; f++) {			d += conf->near_copies;			if (d >= conf->raid_disks)				d -= conf->raid_disks;			s += conf->stride;			r10bio->devs[slot].devnum = d;			r10bio->devs[slot].addr = s;			slot++;		}		dev++;		if (dev >= conf->raid_disks) {			dev = 0;			sector += (conf->chunk_mask + 1);		}	}	BUG_ON(slot != conf->copies);} static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev){	sector_t offset, chunk, vchunk; 	offset = sector & conf->chunk_mask;	if (conf->far_offset) {		int fc;		chunk = sector >> conf->chunk_shift;		fc = sector_div(chunk, conf->far_copies);		dev -= fc * conf->near_copies;

Lines 1–500 of 3108.