hptraid.c

来自「linux-2.4.29操作系统的源码」· C语言 代码 · 共 921 行 · 第 1/2 页

C
921
字号
/*   hptraid.c  Copyright (C) 2001 Red Hat, Inc. All rights reserved.   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.        Authors: 	Arjan van de Ven <arjanv@redhat.com>   Based on work   	Copyleft  (C) 2001 by Wilfried Weissmann <wweissmann@gmx.at>	Copyright (C) 1994-96 Marc ZYNGIER <zyngier@ufr-info-p7.ibp.fr>   Based on work done by S鴕en Schmidt for FreeBSD   Changelog:   19.08.2003 v0.03 wweissmann@gmx.at   	* register the raid volume only if all disks are available	* print a warning that raid-(0+)1 failover is not supported   15.06.2003 v0.02 wweissmann@gmx.at   	* correct values of raid-1 superbock	* re-add check for availability of all disks	* fix offset bug in raid-1 (introduced in raid 0+1 implementation)   14.06.2003 wweissmann@gmx.at   	* superblock has wrong "disks" value on raid-1   	* fixup for raid-1 disknumbering	* do _NOT_ align size to 255*63 boundary		I WILL NOT USE FDISK TO DETERMINE THE VOLUME SIZE.		I WILL NOT USE FDISK TO DETERMINE THE VOLUME SIZE.		I WILL NOT USE FDISK TO DETERMINE THE VOLUME SIZE.		I WILL NOT ...   13.06.2003 wweissmann@gmx.at   	* raid 0+1 support	* check if all disks of an array are available	* bump version number   29.05.2003 wweissmann@gmx.at   	* release no more devices than available on unload	* remove static variables in raid-1 read path*/#include <linux/module.h>#include <linux/init.h>#include <linux/sched.h>#include <linux/smp_lock.h>#include <linux/kernel.h>#include <linux/blkdev.h>#include <linux/blkpg.h>#include <linux/genhd.h>#include <linux/ioctl.h>#include <linux/ide.h>#include <asm/uaccess.h>#include "ataraid.h"#include "hptraid.h"static int hptraid_open(struct inode * inode, struct file * filp);static int hptraid_release(struct inode * inode, struct file * filp);static int hptraid_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);static int hptraidspan_make_request (request_queue_t *q, int rw, struct buffer_head * bh);static int hptraid0_make_request (request_queue_t *q, int rw, struct buffer_head * bh);static int hptraid1_make_request (request_queue_t *q, int rw, struct buffer_head * bh);static int hptraid01_make_request (request_queue_t *q, int rw, struct buffer_head * bh);struct hptdisk {	kdev_t	device;		/* disk-ID/raid 0+1 volume-ID */	unsigned long sectors;	struct block_device *bdev;	unsigned long last_pos;};struct hptraid {	unsigned int stride;	/* stripesize */	unsigned int disks;	/* number of disks in array */	unsigned long sectors;	/* disksize in sectors */        u_int32_t magic_0;        u_int32_t magic_1;	struct geom geom;		int previous;		/* most recently accessed disk in mirror */	struct hptdisk disk[8];	unsigned long cutoff[8];	/* raid 0 cutoff */	unsigned int cutoff_disks[8];		struct hptraid * raid01;	/* sub arrays for raid 0+1 */};struct hptraid_dev {	int major;	int minor;	int device;};static struct hptraid_dev devlist[]={	{IDE0_MAJOR,  0, -1},	{IDE0_MAJOR, 64, -1},	{IDE1_MAJOR,  0, -1},	{IDE1_MAJOR, 64, -1},	{IDE2_MAJOR,  0, -1},	{IDE2_MAJOR, 64, -1},	{IDE3_MAJOR,  0, -1},	{IDE3_MAJOR, 64, -1},	{IDE4_MAJOR,  0, -1},	{IDE4_MAJOR, 64, -1},	{IDE5_MAJOR,  0, -1},	{IDE5_MAJOR, 64, -1},	{IDE6_MAJOR,  0, -1},	{IDE6_MAJOR, 64, -1}};static struct raid_device_operations hptraidspan_ops = {	open:                   hptraid_open,	release:                hptraid_release,	ioctl:			hptraid_ioctl,	make_request:		hptraidspan_make_request};static struct raid_device_operations hptraid0_ops = {	open:                   hptraid_open,	release:                hptraid_release,	ioctl:			hptraid_ioctl,	make_request:		hptraid0_make_request};static struct raid_device_operations hptraid1_ops = {	open:                   hptraid_open,	release:                hptraid_release,	ioctl:			hptraid_ioctl,	make_request:		hptraid1_make_request};static struct raid_device_operations hptraid01_ops = {	open:                   hptraid_open,	release:                hptraid_release,	ioctl:			hptraid_ioctl,	make_request:		hptraid01_make_request};static __initdata struct {	struct raid_device_operations *op;	u_int8_t type;	char label[8];} oplist[] = {	{&hptraid0_ops, HPT_T_RAID_0, "RAID 0"},	{&hptraid1_ops, HPT_T_RAID_1, "RAID 1"},	{&hptraidspan_ops, HPT_T_SPAN, "SPAN"},	{&hptraid01_ops, HPT_T_RAID_01_RAID_0, "RAID 0+1"},	{0, 0}};static struct hptraid raid[14];static int hptraid_ioctl(struct inode *inode, struct file *file,		unsigned int cmd, unsigned long arg){	unsigned int minor;	unsigned char val;	unsigned long sectors;		if (!inode || !inode->i_rdev) 			return -EINVAL;	minor = MINOR(inode->i_rdev)>>SHIFT;		switch (cmd) {         	case BLKGETSIZE:   /* Return device size */			if (!arg)  return -EINVAL;			sectors = ataraid_gendisk.part[MINOR(inode->i_rdev)].nr_sects;			if (MINOR(inode->i_rdev)&15)				return put_user(sectors, (unsigned long *) arg);			return put_user(raid[minor].sectors , (unsigned long *) arg);			break;					case HDIO_GETGEO:		{			struct hd_geometry *loc = (struct hd_geometry *) arg;			unsigned short bios_cyl;						if (!loc) return -EINVAL;			val = 255;			if (put_user(val, (byte *) &loc->heads)) return -EFAULT;			val=63;			if (put_user(val, (byte *) &loc->sectors)) return -EFAULT;			bios_cyl = raid[minor].sectors/63/255;			if (put_user(bios_cyl, (unsigned short *) &loc->cylinders)) return -EFAULT;			if (put_user((unsigned)ataraid_gendisk.part[MINOR(inode->i_rdev)].start_sect,				(unsigned long *) &loc->start)) return -EFAULT;			return 0;		}		case HDIO_GETGEO_BIG:		{			struct hd_big_geometry *loc = (struct hd_big_geometry *) arg;			unsigned int bios_cyl;			if (!loc) return -EINVAL;			val = 255;			if (put_user(val, (byte *) &loc->heads)) return -EFAULT;			val = 63;			if (put_user(val, (byte *) &loc->sectors)) return -EFAULT;			bios_cyl = raid[minor].sectors/63/255;			if (put_user(bios_cyl, (unsigned int *) &loc->cylinders)) return -EFAULT;			if (put_user((unsigned)ataraid_gendisk.part[MINOR(inode->i_rdev)].start_sect,				(unsigned long *) &loc->start)) return -EFAULT;			return 0;		}					default:			return blk_ioctl(inode->i_rdev, cmd, arg);	};	return 0;}static int hptraidspan_make_request (request_queue_t *q, int rw, struct buffer_head * bh){	unsigned long rsect;	unsigned int disk;	int device;	struct hptraid *thisraid;	rsect = bh->b_rsector;	device = (bh->b_rdev >> SHIFT)&MAJOR_MASK;	thisraid = &raid[device];	/*	 * Partitions need adding of the start sector of the partition to the	 * requested sector	 */		rsect += ataraid_gendisk.part[MINOR(bh->b_rdev)].start_sect;	for (disk=0;disk<thisraid->disks;disk++) {		if (disk==1)			rsect+=10;			// the "on next disk" contition check is a bit odd		if (thisraid->disk[disk].sectors > rsect+1)			break;		rsect-=thisraid->disk[disk].sectors-(disk?11:1);	}		// request spans over 2 disks => request must be split	if(rsect+bh->b_size/512 >= thisraid->disk[disk].sectors)		return -1;		/*	 * The new BH_Lock semantics in ll_rw_blk.c guarantee that this	 * is the only IO operation happening on this bh.	 */	 	bh->b_rdev = thisraid->disk[disk].device;	bh->b_rsector = rsect;	/*	 * Let the main block layer submit the IO and resolve recursion:	 */	return 1;}static int hptraid0_compute_request (struct hptraid *thisraid,		request_queue_t *q,		int rw, struct buffer_head * bh){	unsigned long rsect_left,rsect_accum = 0;	unsigned long block;	unsigned int disk=0,real_disk=0;	int i;	/* Ok. We need to modify this sector number to a new disk + new sector	 * number. 	 * If there are disks of different sizes, this gets tricky. 	 * Example with 3 disks (1Gb, 4Gb and 5 GB):	 * The first 3 Gb of the "RAID" are evenly spread over the 3 disks.	 * Then things get interesting. The next 2Gb (RAID view) are spread	 * across disk 2 and 3 and the last 1Gb is disk 3 only.	 *	 * the way this is solved is like this: We have a list of "cutoff"	 * points where everytime a disk falls out of the "higher" count, we	 * mark the max sector. So once we pass a cutoff point, we have to	 * divide by one less.	 */		if (thisraid->stride==0)		thisraid->stride=1;	/*	 * Woops we need to split the request to avoid crossing a stride	 * barrier	 */	if ((bh->b_rsector/thisraid->stride) !=			((bh->b_rsector+(bh->b_size/512)-1)/thisraid->stride)) {		return -1;	}				rsect_left = bh->b_rsector;;		for (i=0;i<8;i++) {		if (thisraid->cutoff_disks[i]==0)			break;		if (bh->b_rsector > thisraid->cutoff[i]) {			/* we're in the wrong area so far */			rsect_left -= thisraid->cutoff[i];			rsect_accum += thisraid->cutoff[i] /				thisraid->cutoff_disks[i];		} else {			block = rsect_left / thisraid->stride;			disk = block % thisraid->cutoff_disks[i];			block = (block / thisraid->cutoff_disks[i]) *				thisraid->stride;			bh->b_rsector = rsect_accum +				(rsect_left % thisraid->stride) + block;			break;		}	}		for (i=0;i<8;i++) {		if ((disk==0) && (thisraid->disk[i].sectors > rsect_accum)) {			real_disk = i;			break;		}		if ((disk>0) && (thisraid->disk[i].sectors >= rsect_accum)) {			disk--;		}			}	disk = real_disk;		/* All but the first disk have a 10 sector offset */	if (i>0)		bh->b_rsector+=10;				/*	 * The new BH_Lock semantics in ll_rw_blk.c guarantee that this	 * is the only IO operation happening on this bh.	 */	 	bh->b_rdev = thisraid->disk[disk].device;	/*	 * Let the main block layer submit the IO and resolve recursion:	 */	return 1;}static int hptraid0_make_request (request_queue_t *q, int rw, struct buffer_head * bh){	unsigned long rsect;	int device;	/*	 * save the sector, it must be restored before a request-split	 * is performed	 */	rsect = bh->b_rsector;	/*	 * Partitions need adding of the start sector of the partition to the	 * requested sector	 */		bh->b_rsector += ataraid_gendisk.part[MINOR(bh->b_rdev)].start_sect;	device = (bh->b_rdev >> SHIFT)&MAJOR_MASK;	if( hptraid0_compute_request(raid+device, q, rw, bh) != 1 ) {			/* request must be split => restore sector */		bh->b_rsector = rsect;		return -1;	}	return 1;}static int hptraid1_read_request (request_queue_t *q, int rw, struct buffer_head * bh){	int device;	int dist;	int bestsofar,bestdist,i;	/* Reads are simple in principle. Pick a disk and go. 	   Initially I cheat by just picking the one which the last known	   head position is closest by.	   Later on, online/offline checking and performance needs adding */		device = (bh->b_rdev >> SHIFT)&MAJOR_MASK;	bh->b_rsector += ataraid_gendisk.part[MINOR(bh->b_rdev)].start_sect;	bestsofar = 0; 	bestdist = raid[device].disk[0].last_pos - bh->b_rsector;	if (bestdist<0) 		bestdist=-bestdist;	if (bestdist>4095)		bestdist=4095;	for (i=1 ; i<raid[device].disks; i++) {		dist = raid[device].disk[i].last_pos - bh->b_rsector;		if (dist<0) 			dist = -dist;		if (dist>4095)			dist=4095;		  /* it's a tie; try to do some read balancing */		if (bestdist==dist) {			if ( (raid[device].previous>bestsofar) &&					(raid[device].previous<=i) )  				bestsofar = i;			raid[device].previous =				(raid[device].previous + 1) %				raid[device].disks;		} else if (bestdist>dist) {			bestdist = dist;			bestsofar = i;		}		}		bh->b_rdev = raid[device].disk[bestsofar].device; 	raid[device].disk[bestsofar].last_pos = bh->b_rsector+(bh->b_size>>9);	/*	 * Let the main block layer submit the IO and resolve recursion:	 */                          		return 1;}static int hptraid1_write_request(request_queue_t *q, int rw, struct buffer_head * bh){	struct buffer_head *bh1;	struct ataraid_bh_private *private;	int device;	int i;	device = (bh->b_rdev >> SHIFT)&MAJOR_MASK;	private = ataraid_get_private();	if (private==NULL)		BUG();	private->parent = bh;		atomic_set(&private->count,raid[device].disks);	for (i = 0; i< raid[device].disks; i++) { 		bh1=ataraid_get_bhead();

⌨️ 快捷键说明

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