File: mpdev.c

package info (click to toggle)
evms 2.5.2-1.sarge2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 14,248 kB
  • ctags: 15,488
  • sloc: ansic: 201,340; perl: 12,421; sh: 4,262; makefile: 1,516; yacc: 316; sed: 16
file content (291 lines) | stat: -rw-r--r-- 6,106 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * mpdev.c - Multipath Device - RAM disk driver 
 * 
 * This code is based on lots of ramdisk code like ... 
 * drivers/block/rd.c
 *
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/blkdev.h>
#include <linux/bio.h>
#include <linux/devfs_fs_kernel.h>
#include <linux/hdreg.h>
#include <asm/uaccess.h>

#include "mpdev.h"

#define MP_NUM_DISKS		16
#define MP_MAJOR		252
#define MP_SECTORS		2048
#define MP_SECTOR_SIZE		512

#define MP_NAME			"mp"
#define MP_HEADS		16
#define MP_CYLINDERS		2

static struct gendisk *rd_disks[MP_NUM_DISKS];
static struct request_queue *rd_queue[MP_NUM_DISKS];
static int mpdev_indexes[MP_NUM_DISKS];
static int mpdev_device_status[MP_NUM_DISKS]; 

static void *mpdev_data;

static unsigned int major = MP_MAJOR;
static unsigned int paths = MP_NUM_DISKS;
static unsigned int size = MP_SECTORS / 2;
static unsigned int sectors;


static int request_should_fail(int dev)
{       
	if (dev >= 0 && dev < paths) {
		return mpdev_device_status[dev];
	}
	return 1;
}


static int mpdev_make_request(request_queue_t * q, struct bio *bio)
{
	sector_t lsn = bio->bi_sector;
	long   len = bio->bi_size >> 9;
	struct bio_vec *bvec;
	int rw = bio_data_dir(bio);
	int i;          
	int dev=0;
	char *src,*dst;
	int vsectors;
	int bytes;


	if (lsn + len > sectors)
		goto fail;

	if (rw==READA)
		rw=READ;

	dev = bio->bi_bdev->bd_dev & 0xff;

	if ( request_should_fail(dev) )
		goto fail;

	if (rw==READ)
		printk(KERN_INFO "mpdev: read(%d) lsn = %ld count = %ld (SUCCESS)\n",
		       dev, (unsigned long)lsn, len);
	else
		printk(KERN_INFO "mpdev: write(%d) lsn = %ld  count = %ld (SUCCESS)\n",
		       dev, (unsigned long)lsn, len);     

	bio_for_each_segment(bvec, bio, i) {            

		bytes    = bvec->bv_len;
		vsectors = bvec->bv_len>>9;                             

		if (rw == READ) {
			src = mpdev_data + (lsn<<9);
			dst = bio_data(bio); 
		} else {
			src = bio_data(bio); 
			dst = mpdev_data + (lsn<<9);
		}

		memcpy(dst, src, bytes);

		lsn += vsectors;
		++bio->bi_idx;
	}

	bio_endio(bio, bio->bi_size, 0);
	return 0;

	fail:
	if (rw==READ)
		printk(KERN_INFO "mpdev: read(%d) lsn = %ld  count = %ld (FAILED)\n",
		       dev, (unsigned long)lsn, len);
	else
		printk(KERN_INFO "mpdev: write(%d) lsn = %ld  count = %ld (FAILED)\n",
		       dev, (unsigned long)lsn, len);

	bio_io_error(bio, bio->bi_size);
	return 0;
} 


static int mpdev_open(struct inode * inode, struct file * filp)
{
	return 0;
}


static int mpdev_ioctl(struct inode * inode, struct file *file,
		       unsigned int cmd, unsigned long arg)
{
	int error;
	int unit_number;

	switch (cmd) {
	case HDIO_GETGEO:
		{
			struct hd_geometry g;
			struct hd_geometry *geometry = (struct hd_geometry *)arg;

			printk(KERN_INFO "HDIO GETGEO cmd\n");  

			g.heads = MP_HEADS;
			g.cylinders = MP_CYLINDERS;
			g.sectors = sectors / (MP_HEADS * MP_CYLINDERS);
			g.start = 0;
			error = copy_to_user(geometry, &g, sizeof g) ? -EFAULT : 0;
			break;
		}

	case MPDEV_CMD:  
		{    
			mpdev_cmd_t mpdev_cmd;

			unit_number = *((int*)inode->i_bdev->bd_disk->private_data);

			if (copy_from_user(&mpdev_cmd, (mpdev_cmd_t *)arg, sizeof(mpdev_cmd_t)))
				return -EFAULT;

			if (unit_number >= 0  && unit_number < paths) {
				mpdev_device_status[unit_number] = mpdev_cmd.cmd;
				if (mpdev_cmd.cmd == 1) {
					printk(KERN_INFO "mpdev: failing unit device %d\n", unit_number);
				} else {
					printk(KERN_INFO "mpdev: recovering unit device %d\n", unit_number);
				}
				error = 0;
			} else {
				error = -EINVAL;
			}                    

			mpdev_cmd.status = error;
			copy_to_user((mpdev_cmd_t *)arg, &mpdev_cmd, sizeof(mpdev_cmd));
			return 0;
		}


	default:
		error = -EINVAL;
	}

	return error;
}

static struct block_device_operations rd_bd_op = {
	.owner =        THIS_MODULE,
	.open =         mpdev_open,
	.ioctl = mpdev_ioctl,
};

static void __exit mpdev_cleanup(void)
{
	int i;

	for (i = 0 ; i < paths; i++) {
		del_gendisk(rd_disks[i]);
		put_disk(rd_disks[i]);
		blk_put_queue(rd_queue[i]);
	}
	vfree(mpdev_data);
	devfs_remove(MP_NAME);
	unregister_blkdev(major, MP_NAME);

	printk("mpdev driver unloaded\n");
}

static int __init mpdev_init(void)
{
	int i, rc;

	if (paths > MP_NUM_DISKS) {
		rc = -EINVAL;
		goto err1;
	}

	sectors = size * 2;

	for (i = 0; i < paths; i++) {
		mpdev_device_status[i] = 0; 
	}

	rc = register_blkdev(major, MP_NAME);
	if (rc)
		goto err1;

	devfs_mk_dir(MP_NAME);

	mpdev_data = vmalloc(sectors * MP_SECTOR_SIZE);
	if (!mpdev_data) {
		printk(KERN_ERR "mpdev: failed to malloc ramdisk area\n");
		rc = -ENOMEM;
		goto err2;
	}
	memset(mpdev_data, 0, sectors * MP_SECTOR_SIZE);

	for (i = 0; i < paths; i++) {
		rd_disks[i] = alloc_disk(1);
		if (!rd_disks[i])
			goto err3;

		rd_queue[i] = blk_alloc_queue(GFP_KERNEL);
		if (!rd_queue[i])
			goto err3;
	}

	for (i = 0; i < paths; i++) {
		struct gendisk *disk = rd_disks[i];

		mpdev_indexes[i] = i;

		blk_queue_make_request(rd_queue[i], &mpdev_make_request);
		rd_queue[i]->queuedata = &mpdev_indexes[i];

		disk->major = major;
		disk->first_minor = i;
		disk->fops = &rd_bd_op;
		disk->queue = rd_queue[i];
		disk->private_data = &mpdev_indexes[i];
		sprintf(disk->disk_name, "%s%d", MP_NAME, i);
		sprintf(disk->devfs_name, "%s/%d", MP_NAME, i);
		set_capacity(disk, sectors);
		add_disk(disk);
	}

	printk("mpdev driver initialized: major %d. "
	       "%d multipath disks of %d kB\n", major, paths, size);

	return 0;

err3:
	for (i = 0; i < paths; i++) {
		if (rd_disks[i])
			put_disk(rd_disks[i]);
		if (rd_queue[i])
			blk_put_queue(rd_queue[i]);
	}
	vfree(mpdev_data);
err2:
	devfs_remove(MP_NAME);
	unregister_blkdev(major, MP_NAME);
err1:
	return rc;
}

module_param(major, uint, 0);
MODULE_PARM_DESC(major, "The major number of the mp driver");

module_param(paths, uint, 0);
MODULE_PARM_DESC(major, "The number of paths to create to the ram-disk");

module_param(size, uint, 0);
MODULE_PARM_DESC(major, "The size of the ram-disk in kB");

module_init(mpdev_init);
module_exit(mpdev_cleanup);
MODULE_LICENSE("GPL");