File: ataraid.c

package info (click to toggle)
kernel-source-2.4.18 2.4.18-14.3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 144,648 kB
  • ctags: 443,980
  • sloc: ansic: 2,548,117; asm: 142,436; makefile: 8,411; sh: 3,097; perl: 2,561; yacc: 1,177; cpp: 755; tcl: 577; lex: 352; awk: 251; lisp: 218; sed: 72
file content (319 lines) | stat: -rw-r--r-- 7,633 bytes parent folder | download | duplicates (7)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
   ataraid.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>
   		
   
*/

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <asm/semaphore.h>
#include <linux/sched.h>
#include <linux/smp_lock.h>
#include <linux/blkdev.h>
#include <linux/blkpg.h>
#include <linux/genhd.h>
#include <linux/ioctl.h>
#include <linux/kdev_t.h>
#include <linux/swap.h>

#include <linux/ide.h>
#include <asm/uaccess.h>

#include "ataraid.h"

                                        
static int ataraid_hardsect_size[256];
static int ataraid_blksize_size[256];

static struct raid_device_operations* ataraid_ops[16];

static int ataraid_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
static int ataraid_open(struct inode * inode, struct file * filp);
static int ataraid_release(struct inode * inode, struct file * filp);
static void ataraid_split_request(request_queue_t *q, int rw, struct buffer_head * bh);


struct gendisk ataraid_gendisk;
static int ataraid_gendisk_sizes[256];
static int ataraid_readahead[256];

static struct block_device_operations ataraid_fops = {
	owner:			THIS_MODULE,
	open:                   ataraid_open,
	release:                ataraid_release,
	ioctl:                  ataraid_ioctl,
};
                


static DECLARE_MUTEX(ataraid_sem);

/* Bitmap for the devices currently in use */
static unsigned int ataraiduse;


/* stub fops functions */

static int ataraid_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)  
{
	int minor;
	minor = MINOR(inode->i_rdev)>>SHIFT;
	
	if ((ataraid_ops[minor])&&(ataraid_ops[minor]->ioctl))
		return (ataraid_ops[minor]->ioctl)(inode,file,cmd,arg);
	return -EINVAL;
}

static int ataraid_open(struct inode * inode, struct file * filp)
{
	int minor;
	minor = MINOR(inode->i_rdev)>>SHIFT;

	if ((ataraid_ops[minor])&&(ataraid_ops[minor]->open))
		return (ataraid_ops[minor]->open)(inode,filp);
	return -EINVAL;
}


static int ataraid_release(struct inode * inode, struct file * filp)
{
	int minor;
	minor = MINOR(inode->i_rdev)>>SHIFT;

	if ((ataraid_ops[minor])&&(ataraid_ops[minor]->release))
		return (ataraid_ops[minor]->release)(inode,filp);
	return -EINVAL;
}

static int ataraid_make_request (request_queue_t *q, int rw, struct buffer_head * bh)
{
	int minor;
	int retval;
	minor = MINOR(bh->b_rdev)>>SHIFT;

	if ((ataraid_ops[minor])&&(ataraid_ops[minor]->make_request)) {
		
		retval= (ataraid_ops[minor]->make_request)(q,rw,bh);
		if (retval == -1) {
			ataraid_split_request(q,rw,bh);		
			return 0;
		} else
			return retval;
	}
	return -EINVAL;
}

struct buffer_head *ataraid_get_bhead(void)
{
	void *ptr = NULL;
	while (!ptr) {
		ptr=kmalloc(sizeof(struct buffer_head),GFP_NOIO);
		if (!ptr) {
			__set_current_state(TASK_RUNNING);
	                current->policy |= SCHED_YIELD;
	                schedule();             
		}
	}
	return ptr;
}

EXPORT_SYMBOL(ataraid_get_bhead);

struct ataraid_bh_private *ataraid_get_private(void)
{
	void *ptr = NULL;
	while (!ptr) {
		ptr=kmalloc(sizeof(struct ataraid_bh_private),GFP_NOIO);
		if (!ptr) {
			__set_current_state(TASK_RUNNING);
	                current->policy |= SCHED_YIELD;
	                schedule();             
		}
	}
	return ptr;
}

EXPORT_SYMBOL(ataraid_get_private);

void ataraid_end_request(struct buffer_head *bh, int uptodate)
{
	struct ataraid_bh_private *private = bh->b_private;

	if (private==NULL)
		BUG();

	if (atomic_dec_and_test(&private->count)) {
		private->parent->b_end_io(private->parent,uptodate);
		private->parent = NULL;
		kfree(private);
	}
	kfree(bh);
}

EXPORT_SYMBOL(ataraid_end_request);

static void ataraid_split_request(request_queue_t *q, int rw, struct buffer_head * bh)
{
	struct buffer_head *bh1,*bh2;
	struct ataraid_bh_private *private;
	bh1=ataraid_get_bhead();
	bh2=ataraid_get_bhead();

	/* If either of those ever fails we're doomed */
	if ((!bh1)||(!bh2))
		BUG();
	private = ataraid_get_private();
	if (private==NULL)
		BUG();
	
	memcpy(bh1, bh, sizeof(*bh));
	memcpy(bh2, bh, sizeof(*bh));
	
	bh1->b_end_io = ataraid_end_request;
	bh2->b_end_io = ataraid_end_request;

	bh2->b_rsector += bh->b_size >> 10;
	bh1->b_size /= 2;
	bh2->b_size /= 2;
	private->parent = bh;

	bh1->b_private = private;
	bh2->b_private = private;
	atomic_set(&private->count,2);

	bh2->b_data +=  bh->b_size/2;

	generic_make_request(rw,bh1);
	generic_make_request(rw,bh2);
}




/* device register / release functions */


int ataraid_get_device(struct raid_device_operations *fops)
{
	int bit;
	down(&ataraid_sem);
	if (ataraiduse==~0U) {
		up(&ataraid_sem);
		return -ENODEV;
	}
	bit=ffz(ataraiduse); 
	ataraiduse |= 1<<bit;
	ataraid_ops[bit] = fops;
	up(&ataraid_sem);
	return bit;
}

void ataraid_release_device(int device)
{
	down(&ataraid_sem);
	
	if ((ataraiduse & (1<<device))==0)
		BUG();	/* device wasn't registered at all */
	
	ataraiduse &= ~(1<<device);
	ataraid_ops[device] = NULL;
	up(&ataraid_sem);
}

void ataraid_register_disk(int device,long size)
{
	register_disk(&ataraid_gendisk, MKDEV(ATAMAJOR,16*device),16,
		&ataraid_fops,size);

}

static __init int ataraid_init(void) 
{
	int i;
        for(i=0;i<256;i++)
	{
        	ataraid_hardsect_size[i] = 512;
		ataraid_blksize_size[i] = 1024;  
		ataraid_readahead[i] = 1023;
	}
	
	if (blksize_size[ATAMAJOR]==NULL)
		blksize_size[ATAMAJOR] = ataraid_blksize_size;
	if (hardsect_size[ATAMAJOR]==NULL)
		hardsect_size[ATAMAJOR] = ataraid_hardsect_size;
	
	
	/* setup the gendisk structure */	
	ataraid_gendisk.part = kmalloc(256 * sizeof(struct hd_struct),GFP_KERNEL);
	if (ataraid_gendisk.part==NULL) {
		printk(KERN_ERR "ataraid: Couldn't allocate memory, aborting \n");
		return -1;
	}
	
	memset(&ataraid_gendisk.part[0],0,256*sizeof(struct hd_struct));
	
	
	ataraid_gendisk.major       = ATAMAJOR;
	ataraid_gendisk.major_name  = "ataraid";
	ataraid_gendisk.minor_shift = 4;
	ataraid_gendisk.max_p	    = 15;
	ataraid_gendisk.sizes	    = &ataraid_gendisk_sizes[0];
	ataraid_gendisk.nr_real	    = 16;
	ataraid_gendisk.fops        = &ataraid_fops;
	
	
	add_gendisk(&ataraid_gendisk);
			
	if (register_blkdev(ATAMAJOR, "ataraid", &ataraid_fops)) {
		printk(KERN_ERR "ataraid: Could not get major %d \n",ATAMAJOR);
		return -1;
	}
	
	                
	
	blk_queue_make_request(BLK_DEFAULT_QUEUE(ATAMAJOR),ataraid_make_request);
                                                                                     	
	return 0;                                                        	
}


static void __exit ataraid_exit(void)
{
	unregister_blkdev(ATAMAJOR, "ataraid");
	hardsect_size[ATAMAJOR] = NULL;
	blk_size[ATAMAJOR] = NULL;
	blksize_size[ATAMAJOR] = NULL;                       
	max_readahead[ATAMAJOR] = NULL;

	del_gendisk(&ataraid_gendisk);
        
	if (ataraid_gendisk.part) {
		kfree(ataraid_gendisk.part);
		ataraid_gendisk.part = NULL;
	}
}

module_init(ataraid_init);
module_exit(ataraid_exit);



EXPORT_SYMBOL(ataraid_get_device);
EXPORT_SYMBOL(ataraid_release_device);
EXPORT_SYMBOL(ataraid_gendisk);
EXPORT_SYMBOL(ataraid_register_disk);
MODULE_LICENSE("GPL");