raid5.c

来自「linux 内核源代码」· C语言 代码 · 共 2,325 行 · 第 1/5 页

C
2,325
字号
	case 5:		switch (conf->algorithm) {		case ALGORITHM_LEFT_ASYMMETRIC:		case ALGORITHM_RIGHT_ASYMMETRIC:			if (i > sh->pd_idx)				i--;			break;		case ALGORITHM_LEFT_SYMMETRIC:		case ALGORITHM_RIGHT_SYMMETRIC:			if (i < sh->pd_idx)				i += raid_disks;			i -= (sh->pd_idx + 1);			break;		default:			printk(KERN_ERR "raid5: unsupported algorithm %d\n",			       conf->algorithm);		}		break;	case 6:		if (i == raid6_next_disk(sh->pd_idx, raid_disks))			return 0; /* It is the Q disk */		switch (conf->algorithm) {		case ALGORITHM_LEFT_ASYMMETRIC:		case ALGORITHM_RIGHT_ASYMMETRIC:		  	if (sh->pd_idx == raid_disks-1)				i--; 	/* Q D D D P */			else if (i > sh->pd_idx)				i -= 2; /* D D P Q D */			break;		case ALGORITHM_LEFT_SYMMETRIC:		case ALGORITHM_RIGHT_SYMMETRIC:			if (sh->pd_idx == raid_disks-1)				i--; /* Q D D D P */			else {				/* D D P Q D */				if (i < sh->pd_idx)					i += raid_disks;				i -= (sh->pd_idx + 2);			}			break;		default:			printk (KERN_CRIT "raid6: unsupported algorithm %d\n",				conf->algorithm);		}		break;	}	chunk_number = stripe * data_disks + i;	r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;	check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);	if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {		printk(KERN_ERR "compute_blocknr: map not correct\n");		return 0;	}	return r_sector;}/* * Copy data between a page in the stripe cache, and one or more bion * The page could align with the middle of the bio, or there could be * several bion, each with several bio_vecs, which cover part of the page * Multiple bion are linked together on bi_next.  There may be extras * at the end of this list.  We ignore them. */static void copy_data(int frombio, struct bio *bio,		     struct page *page,		     sector_t sector){	char *pa = page_address(page);	struct bio_vec *bvl;	int i;	int page_offset;	if (bio->bi_sector >= sector)		page_offset = (signed)(bio->bi_sector - sector) * 512;	else		page_offset = (signed)(sector - bio->bi_sector) * -512;	bio_for_each_segment(bvl, bio, i) {		int len = bio_iovec_idx(bio,i)->bv_len;		int clen;		int b_offset = 0;		if (page_offset < 0) {			b_offset = -page_offset;			page_offset += b_offset;			len -= b_offset;		}		if (len > 0 && page_offset + len > STRIPE_SIZE)			clen = STRIPE_SIZE - page_offset;		else clen = len;		if (clen > 0) {			char *ba = __bio_kmap_atomic(bio, i, KM_USER0);			if (frombio)				memcpy(pa+page_offset, ba+b_offset, clen);			else				memcpy(ba+b_offset, pa+page_offset, clen);			__bio_kunmap_atomic(ba, KM_USER0);		}		if (clen < len) /* hit end of page */			break;		page_offset +=  len;	}}#define check_xor()	do {						  \				if (count == MAX_XOR_BLOCKS) {		  \				xor_blocks(count, STRIPE_SIZE, dest, ptr);\				count = 0;				  \			   }						  \			} while(0)static void compute_parity6(struct stripe_head *sh, int method){	raid6_conf_t *conf = sh->raid_conf;	int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = sh->disks, count;	struct bio *chosen;	/**** FIX THIS: This could be very bad if disks is close to 256 ****/	void *ptrs[disks];	qd_idx = raid6_next_disk(pd_idx, disks);	d0_idx = raid6_next_disk(qd_idx, disks);	pr_debug("compute_parity, stripe %llu, method %d\n",		(unsigned long long)sh->sector, method);	switch(method) {	case READ_MODIFY_WRITE:		BUG();		/* READ_MODIFY_WRITE N/A for RAID-6 */	case RECONSTRUCT_WRITE:		for (i= disks; i-- ;)			if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {				chosen = sh->dev[i].towrite;				sh->dev[i].towrite = NULL;				if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))					wake_up(&conf->wait_for_overlap);				BUG_ON(sh->dev[i].written);				sh->dev[i].written = chosen;			}		break;	case CHECK_PARITY:		BUG();		/* Not implemented yet */	}	for (i = disks; i--;)		if (sh->dev[i].written) {			sector_t sector = sh->dev[i].sector;			struct bio *wbi = sh->dev[i].written;			while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {				copy_data(1, wbi, sh->dev[i].page, sector);				wbi = r5_next_bio(wbi, sector);			}			set_bit(R5_LOCKED, &sh->dev[i].flags);			set_bit(R5_UPTODATE, &sh->dev[i].flags);		}//	switch(method) {//	case RECONSTRUCT_WRITE://	case CHECK_PARITY://	case UPDATE_PARITY:		/* Note that unlike RAID-5, the ordering of the disks matters greatly. */		/* FIX: Is this ordering of drives even remotely optimal? */		count = 0;		i = d0_idx;		do {			ptrs[count++] = page_address(sh->dev[i].page);			if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))				printk("block %d/%d not uptodate on parity calc\n", i,count);			i = raid6_next_disk(i, disks);		} while ( i != d0_idx );//		break;//	}	raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);	switch(method) {	case RECONSTRUCT_WRITE:		set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);		set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);		set_bit(R5_LOCKED,   &sh->dev[pd_idx].flags);		set_bit(R5_LOCKED,   &sh->dev[qd_idx].flags);		break;	case UPDATE_PARITY:		set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);		set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);		break;	}}/* Compute one missing block */static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero){	int i, count, disks = sh->disks;	void *ptr[MAX_XOR_BLOCKS], *dest, *p;	int pd_idx = sh->pd_idx;	int qd_idx = raid6_next_disk(pd_idx, disks);	pr_debug("compute_block_1, stripe %llu, idx %d\n",		(unsigned long long)sh->sector, dd_idx);	if ( dd_idx == qd_idx ) {		/* We're actually computing the Q drive */		compute_parity6(sh, UPDATE_PARITY);	} else {		dest = page_address(sh->dev[dd_idx].page);		if (!nozero) memset(dest, 0, STRIPE_SIZE);		count = 0;		for (i = disks ; i--; ) {			if (i == dd_idx || i == qd_idx)				continue;			p = page_address(sh->dev[i].page);			if (test_bit(R5_UPTODATE, &sh->dev[i].flags))				ptr[count++] = p;			else				printk("compute_block() %d, stripe %llu, %d"				       " not present\n", dd_idx,				       (unsigned long long)sh->sector, i);			check_xor();		}		if (count)			xor_blocks(count, STRIPE_SIZE, dest, ptr);		if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);		else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);	}}/* Compute two missing blocks */static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2){	int i, count, disks = sh->disks;	int pd_idx = sh->pd_idx;	int qd_idx = raid6_next_disk(pd_idx, disks);	int d0_idx = raid6_next_disk(qd_idx, disks);	int faila, failb;	/* faila and failb are disk numbers relative to d0_idx */	/* pd_idx become disks-2 and qd_idx become disks-1 */	faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;	failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;	BUG_ON(faila == failb);	if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }	pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",	       (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);	if ( failb == disks-1 ) {		/* Q disk is one of the missing disks */		if ( faila == disks-2 ) {			/* Missing P+Q, just recompute */			compute_parity6(sh, UPDATE_PARITY);			return;		} else {			/* We're missing D+Q; recompute D from P */			compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);			compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */			return;		}	}	/* We're missing D+P or D+D; build pointer table */	{		/**** FIX THIS: This could be very bad if disks is close to 256 ****/		void *ptrs[disks];		count = 0;		i = d0_idx;		do {			ptrs[count++] = page_address(sh->dev[i].page);			i = raid6_next_disk(i, disks);			if (i != dd_idx1 && i != dd_idx2 &&			    !test_bit(R5_UPTODATE, &sh->dev[i].flags))				printk("compute_2 with missing block %d/%d\n", count, i);		} while ( i != d0_idx );		if ( failb == disks-2 ) {			/* We're missing D+P. */			raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);		} else {			/* We're missing D+D. */			raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);		}		/* Both the above update both missing blocks */		set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);		set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);	}}static inthandle_write_operations5(struct stripe_head *sh, int rcw, int expand){	int i, pd_idx = sh->pd_idx, disks = sh->disks;	int locked = 0;	if (rcw) {		/* if we are not expanding this is a proper write request, and		 * there will be bios with new data to be drained into the		 * stripe cache		 */		if (!expand) {			set_bit(STRIPE_OP_BIODRAIN, &sh->ops.pending);			sh->ops.count++;		}		set_bit(STRIPE_OP_POSTXOR, &sh->ops.pending);		sh->ops.count++;		for (i = disks; i--; ) {			struct r5dev *dev = &sh->dev[i];			if (dev->towrite) {				set_bit(R5_LOCKED, &dev->flags);				if (!expand)					clear_bit(R5_UPTODATE, &dev->flags);				locked++;			}		}	} else {		BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||			test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));		set_bit(STRIPE_OP_PREXOR, &sh->ops.pending);		set_bit(STRIPE_OP_BIODRAIN, &sh->ops.pending);		set_bit(STRIPE_OP_POSTXOR, &sh->ops.pending);		sh->ops.count += 3;		for (i = disks; i--; ) {			struct r5dev *dev = &sh->dev[i];			if (i == pd_idx)				continue;			/* For a read-modify write there may be blocks that are			 * locked for reading while others are ready to be			 * written so we distinguish these blocks by the			 * R5_Wantprexor bit			 */			if (dev->towrite &&			    (test_bit(R5_UPTODATE, &dev->flags) ||			    test_bit(R5_Wantcompute, &dev->flags))) {				set_bit(R5_Wantprexor, &dev->flags);				set_bit(R5_LOCKED, &dev->flags);				clear_bit(R5_UPTODATE, &dev->flags);				locked++;			}		}	}	/* keep the parity disk locked while asynchronous operations	 * are in flight	 */	set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);	clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);	locked++;	pr_debug("%s: stripe %llu locked: %d pending: %lx\n",		__FUNCTION__, (unsigned long long)sh->sector,		locked, sh->ops.pending);	return locked;}/* * Each stripe/dev can have one or more bion attached. * toread/towrite point to the first in a chain. * The bi_next chain must be in order. */static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite){	struct bio **bip;	raid5_conf_t *conf = sh->raid_conf;	int firstwrite=0;	pr_debug("adding bh b#%llu to stripe s#%llu\n",		(unsigned long long)bi->bi_sector,		(unsigned long long)sh->sector);	spin_lock(&sh->lock);	spin_lock_irq(&conf->device_lock);	if (forwrite) {		bip = &sh->dev[dd_idx].towrite;		if (*bip == NULL && sh->dev[dd_idx].written == NULL)			firstwrite = 1;	} else		bip = &sh->dev[dd_idx].toread;	while (*bip && (*bip)->bi_sector < bi->bi_sector) {		if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)			goto overlap;		bip = & (*bip)->bi_next;	}	if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))		goto overlap;	BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);	if (*bip)		bi->bi_next = *bip;	*bip = bi;	bi->bi_phys_segments ++;	spin_unlock_irq(&conf->device_lock);	spin_unlock(&sh->lock);	pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",		(unsigned long long)bi->bi_sector,		(unsigned long long)sh->sector, dd_idx);	if (conf->mddev->bitmap && firstwrite) {		bitmap_startwrite(conf->mddev->bitmap, sh->sector,				  STRIPE_SECTORS, 0);		sh->bm_seq = conf->seq_flush+1;		set_bit(STRIPE_BIT_DELAY, &sh->state);	}	if (forwrite) {		/* check if page is covered */		sector_t sector = sh->dev[dd_idx].sector;		for (bi=sh->dev[dd_idx].towrite;		     sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&			     bi && bi->bi_sector <= sector;		     bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {			if (bi->bi_sector + (bi->bi_size>>9) >= sector)				sector = bi->bi_sector + (bi->bi_size>>9);		}		if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)			set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);	}	return 1; overlap:	set_bit(R5_Overlap, &sh->dev[dd_idx].flags);	spin_unlock_irq(&conf->device_lock);	spin_unlock(&sh->lock);	return 0;}static void end_reshape(raid5_conf_t *conf);static int page_is_zero(struct page *p){	char *a = page_address(p);	return ((*(u32*)a) == 0 &&		memcmp(a, a+4, STRIPE_SIZE-4)==0);}static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks){	int sectors_per_chunk = conf->chunk_size >> 9;	int pd_idx, dd_idx;	int chunk_offset = sector_div(stripe, sectors_per_chunk);	raid5_compute_sector(stripe * (disks - conf->max_degraded)			     *sectors_per_chunk + chunk_offset,			     disks, disks - conf->max_degraded,			     &dd_idx, &pd_idx, conf);	return pd_idx;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?