4762 lines · 131.1 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 <linux/kthread.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 ) *    use_far_sets (stored in bit 17 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 there * 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 being very far * apart on disk, there are adjacent stripes. * * The far and offset algorithms are handled slightly differently if * 'use_far_sets' is true.  In this case, the array's devices are grouped into * sets that are (near_copies * far_copies) in size.  The far copied stripes * are still shifted by 'near_copies' devices, but this shifting stays confined * to the set rather than the entire array.  This is done to improve the number * of device combinations that can fail without causing the array to fail. * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk * on a device): *    A B C D    A B C D E *      ...         ... *    D A B C    E A B C D * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s): *    [A B] [C D]    [A B] [C D E] *    |...| |...|    |...| | ... | *    [B A] [D C]    [B A] [E C D] */ /* * Number of guaranteed r10bios in case of extreme VM load: */#define	NR_RAID10_BIOS 256 /* when we get a read error on a read-only array, we redirect to another * device without failing the first device, or trying to over-write to * correct the read error.  To keep track of bad blocks on a per-bio * level, we store IO_BLOCKED in the appropriate 'bios' pointer */#define IO_BLOCKED ((struct bio *)1)/* When we successfully write to a known bad-block, we need to remove the * bad-block marking which must be done from process context.  So we record * the success by setting devs[n].bio to IO_MADE_GOOD */#define IO_MADE_GOOD ((struct bio *)2) #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2) /* When there are this many requests queued 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 int _enough(struct r10conf *conf, int previous, int ignore);static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,				int *skipped);static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);static void end_reshape_write(struct bio *bio, int error);static void end_reshape(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) ||	    test_bit(MD_RECOVERY_RESHAPE, &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;		if (!conf->have_replacement)			continue;		bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);		if (!bio)			goto out_free_bio;		r10_bio->devs[j].repl_bio = bio;	}	/*	 * Allocate RESYNC_PAGES data pages and attach them	 * where needed.	 */	for (j = 0 ; j < nalloc; j++) {		struct bio *rbio = r10_bio->devs[j].repl_bio;		bio = r10_bio->devs[j].bio;		for (i = 0; i < RESYNC_PAGES; i++) {			if (j > 0 && !test_bit(MD_RECOVERY_SYNC,					       &conf->mddev->recovery)) {				/* we can share bv_page's during recovery				 * and reshape */				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;			if (rbio)				rbio->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 = 0;out_free_bio:	for ( ; j < nalloc; j++) {		if (r10_bio->devs[j].bio)			bio_put(r10_bio->devs[j].bio);		if (r10_bio->devs[j].repl_bio)			bio_put(r10_bio->devs[j].repl_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);		}		bio = r10bio->devs[j].repl_bio;		if (bio)			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;		bio = &r10_bio->devs[i].repl_bio;		if (r10_bio->read_slot < 0 && !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 *replp){	int slot;	int repl = 0; 	for (slot = 0; slot < conf->copies; slot++) {		if (r10_bio->devs[slot].bio == bio)			break;		if (r10_bio->devs[slot].repl_bio == bio) {			repl = 1;			break;		}	} 	BUG_ON(slot == conf->copies);	update_head_pos(slot, r10_bio); 	if (slotp)		*slotp = slot;	if (replp)		*replp = repl;	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 md_rdev *rdev;	struct r10conf *conf = r10_bio->mddev->private;  	slot = r10_bio->read_slot;	dev = r10_bio->devs[slot].devnum;	rdev = r10_bio->devs[slot].rdev;	/*	 * 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);	} else {		/* If all other devices that store this block have		 * failed, we want to return the error upwards rather		 * than fail the last device.  Here we redefine		 * "uptodate" to mean "Don't want to retry"		 */		if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),			     rdev->raid_disk))			uptodate = 1;	}	if (uptodate) {		raid_end_bio_io(r10_bio);		rdev_dec_pending(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(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, repl;	struct md_rdev *rdev = NULL; 	dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl); 	if (repl)		rdev = conf->mirrors[dev].replacement;	if (!rdev) {		smp_rmb();		repl = 0;		rdev = conf->mirrors[dev].rdev;	}	/*	 * this branch is our 'one mirror IO has finished' event handler:	 */	if (!uptodate) {		if (repl)			/* Never record new bad blocks to replacement,			 * just fail it.			 */			md_error(rdev->mddev, rdev);		else {			set_bit(WriteErrorSeen,	&rdev->flags);			if (!test_and_set_bit(WantReplacement, &rdev->flags))				set_bit(MD_RECOVERY_NEEDED,					&rdev->mddev->recovery);			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; 		/*		 * Do not set R10BIO_Uptodate if the current device is		 * rebuilding or Faulty. This is because we cannot use		 * such device for properly reading the data back (we could		 * potentially use it, if the current write would have felt		 * before rdev->recovery_offset, but for simplicity we don't		 * check this here.		 */		if (test_bit(In_sync, &rdev->flags) &&		    !test_bit(Faulty, &rdev->flags))

Lines 1–500 of 4762.