ataraid.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:7k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.    ataraid.c  Copyright (C) 2001 Red Hat, Inc. All rights reserved.
  3.    This program is free software; you can redistribute it and/or modify
  4.    it under the terms of the GNU General Public License as published by
  5.    the Free Software Foundation; either version 2, or (at your option)
  6.    any later version.
  7.    
  8.    You should have received a copy of the GNU General Public License
  9.    (for example /usr/src/linux/COPYING); if not, write to the Free
  10.    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
  11.    
  12.    Authors:  Arjan van de Ven <arjanv@redhat.com>
  13.    
  14.    
  15. */
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mm.h>
  20. #include <asm/semaphore.h>
  21. #include <linux/sched.h>
  22. #include <linux/smp_lock.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/blkpg.h>
  25. #include <linux/genhd.h>
  26. #include <linux/ioctl.h>
  27. #include <linux/kdev_t.h>
  28. #include <linux/swap.h>
  29. #include <linux/ide.h>
  30. #include <asm/uaccess.h>
  31. #include "ataraid.h"
  32.                                         
  33. static int ataraid_hardsect_size[256];
  34. static int ataraid_blksize_size[256];
  35. static struct raid_device_operations* ataraid_ops[16];
  36. static int ataraid_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
  37. static int ataraid_open(struct inode * inode, struct file * filp);
  38. static int ataraid_release(struct inode * inode, struct file * filp);
  39. static void ataraid_split_request(request_queue_t *q, int rw, struct buffer_head * bh);
  40. struct gendisk ataraid_gendisk;
  41. static int ataraid_gendisk_sizes[256];
  42. static int ataraid_readahead[256];
  43. static struct block_device_operations ataraid_fops = {
  44. owner: THIS_MODULE,
  45. open:                   ataraid_open,
  46. release:                ataraid_release,
  47. ioctl:                  ataraid_ioctl,
  48. };
  49.                 
  50. static DECLARE_MUTEX(ataraid_sem);
  51. /* Bitmap for the devices currently in use */
  52. static unsigned int ataraiduse;
  53. /* stub fops functions */
  54. static int ataraid_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)  
  55. {
  56. int minor;
  57. minor = MINOR(inode->i_rdev)>>SHIFT;
  58. if ((ataraid_ops[minor])&&(ataraid_ops[minor]->ioctl))
  59. return (ataraid_ops[minor]->ioctl)(inode,file,cmd,arg);
  60. return -EINVAL;
  61. }
  62. static int ataraid_open(struct inode * inode, struct file * filp)
  63. {
  64. int minor;
  65. minor = MINOR(inode->i_rdev)>>SHIFT;
  66. if ((ataraid_ops[minor])&&(ataraid_ops[minor]->open))
  67. return (ataraid_ops[minor]->open)(inode,filp);
  68. return -EINVAL;
  69. }
  70. static int ataraid_release(struct inode * inode, struct file * filp)
  71. {
  72. int minor;
  73. minor = MINOR(inode->i_rdev)>>SHIFT;
  74. if ((ataraid_ops[minor])&&(ataraid_ops[minor]->release))
  75. return (ataraid_ops[minor]->release)(inode,filp);
  76. return -EINVAL;
  77. }
  78. static int ataraid_make_request (request_queue_t *q, int rw, struct buffer_head * bh)
  79. {
  80. int minor;
  81. int retval;
  82. minor = MINOR(bh->b_rdev)>>SHIFT;
  83. if ((ataraid_ops[minor])&&(ataraid_ops[minor]->make_request)) {
  84. retval= (ataraid_ops[minor]->make_request)(q,rw,bh);
  85. if (retval == -1) {
  86. ataraid_split_request(q,rw,bh);
  87. return 0;
  88. } else
  89. return retval;
  90. }
  91. return -EINVAL;
  92. }
  93. struct buffer_head *ataraid_get_bhead(void)
  94. {
  95. void *ptr = NULL;
  96. while (!ptr) {
  97. ptr=kmalloc(sizeof(struct buffer_head),GFP_NOIO);
  98. if (!ptr)
  99. yield();
  100. }
  101. return ptr;
  102. }
  103. EXPORT_SYMBOL(ataraid_get_bhead);
  104. struct ataraid_bh_private *ataraid_get_private(void)
  105. {
  106. void *ptr = NULL;
  107. while (!ptr) {
  108. ptr=kmalloc(sizeof(struct ataraid_bh_private),GFP_NOIO);
  109. if (!ptr)
  110. yield();
  111. }
  112. return ptr;
  113. }
  114. EXPORT_SYMBOL(ataraid_get_private);
  115. void ataraid_end_request(struct buffer_head *bh, int uptodate)
  116. {
  117. struct ataraid_bh_private *private = bh->b_private;
  118. if (private==NULL)
  119. BUG();
  120. if (atomic_dec_and_test(&private->count)) {
  121. private->parent->b_end_io(private->parent,uptodate);
  122. private->parent = NULL;
  123. kfree(private);
  124. }
  125. kfree(bh);
  126. }
  127. EXPORT_SYMBOL(ataraid_end_request);
  128. static void ataraid_split_request(request_queue_t *q, int rw, struct buffer_head * bh)
  129. {
  130. struct buffer_head *bh1,*bh2;
  131. struct ataraid_bh_private *private;
  132. bh1=ataraid_get_bhead();
  133. bh2=ataraid_get_bhead();
  134. /* If either of those ever fails we're doomed */
  135. if ((!bh1)||(!bh2))
  136. BUG();
  137. private = ataraid_get_private();
  138. if (private==NULL)
  139. BUG();
  140. memcpy(bh1, bh, sizeof(*bh));
  141. memcpy(bh2, bh, sizeof(*bh));
  142. bh1->b_end_io = ataraid_end_request;
  143. bh2->b_end_io = ataraid_end_request;
  144. bh2->b_rsector += bh->b_size >> 10;
  145. bh1->b_size /= 2;
  146. bh2->b_size /= 2;
  147. private->parent = bh;
  148. bh1->b_private = private;
  149. bh2->b_private = private;
  150. atomic_set(&private->count,2);
  151. bh2->b_data +=  bh->b_size/2;
  152. generic_make_request(rw,bh1);
  153. generic_make_request(rw,bh2);
  154. }
  155. /* device register / release functions */
  156. int ataraid_get_device(struct raid_device_operations *fops)
  157. {
  158. int bit;
  159. down(&ataraid_sem);
  160. if (ataraiduse==~0U) {
  161. up(&ataraid_sem);
  162. return -ENODEV;
  163. }
  164. bit=ffz(ataraiduse); 
  165. ataraiduse |= 1<<bit;
  166. ataraid_ops[bit] = fops;
  167. up(&ataraid_sem);
  168. return bit;
  169. }
  170. void ataraid_release_device(int device)
  171. {
  172. down(&ataraid_sem);
  173. if ((ataraiduse & (1<<device))==0)
  174. BUG(); /* device wasn't registered at all */
  175. ataraiduse &= ~(1<<device);
  176. ataraid_ops[device] = NULL;
  177. up(&ataraid_sem);
  178. }
  179. void ataraid_register_disk(int device,long size)
  180. {
  181. register_disk(&ataraid_gendisk, MKDEV(ATAMAJOR,16*device),16,
  182. &ataraid_fops,size);
  183. }
  184. static __init int ataraid_init(void) 
  185. {
  186. int i;
  187.         for(i=0;i<256;i++)
  188. {
  189.          ataraid_hardsect_size[i] = 512;
  190. ataraid_blksize_size[i] = 1024;  
  191. ataraid_readahead[i] = 1023;
  192. }
  193. if (blksize_size[ATAMAJOR]==NULL)
  194. blksize_size[ATAMAJOR] = ataraid_blksize_size;
  195. if (hardsect_size[ATAMAJOR]==NULL)
  196. hardsect_size[ATAMAJOR] = ataraid_hardsect_size;
  197. /* setup the gendisk structure */
  198. ataraid_gendisk.part = kmalloc(256 * sizeof(struct hd_struct),GFP_KERNEL);
  199. if (ataraid_gendisk.part==NULL) {
  200. printk(KERN_ERR "ataraid: Couldn't allocate memory, aborting n");
  201. return -1;
  202. }
  203. memset(&ataraid_gendisk.part[0],0,256*sizeof(struct hd_struct));
  204. ataraid_gendisk.major       = ATAMAJOR;
  205. ataraid_gendisk.major_name  = "ataraid";
  206. ataraid_gendisk.minor_shift = 4;
  207. ataraid_gendisk.max_p     = 15;
  208. ataraid_gendisk.sizes     = &ataraid_gendisk_sizes[0];
  209. ataraid_gendisk.nr_real     = 16;
  210. ataraid_gendisk.fops        = &ataraid_fops;
  211. add_gendisk(&ataraid_gendisk);
  212. if (register_blkdev(ATAMAJOR, "ataraid", &ataraid_fops)) {
  213. printk(KERN_ERR "ataraid: Could not get major %d n",ATAMAJOR);
  214. return -1;
  215. }
  216.                 
  217. blk_queue_make_request(BLK_DEFAULT_QUEUE(ATAMAJOR),ataraid_make_request);
  218.                                                                                      
  219. return 0;                                                        
  220. }
  221. static void __exit ataraid_exit(void)
  222. {
  223. unregister_blkdev(ATAMAJOR, "ataraid");
  224. hardsect_size[ATAMAJOR] = NULL;
  225. blk_size[ATAMAJOR] = NULL;
  226. blksize_size[ATAMAJOR] = NULL;                       
  227. max_readahead[ATAMAJOR] = NULL;
  228. del_gendisk(&ataraid_gendisk);
  229.         
  230. if (ataraid_gendisk.part) {
  231. kfree(ataraid_gendisk.part);
  232. ataraid_gendisk.part = NULL;
  233. }
  234. }
  235. module_init(ataraid_init);
  236. module_exit(ataraid_exit);
  237. EXPORT_SYMBOL(ataraid_get_device);
  238. EXPORT_SYMBOL(ataraid_release_device);
  239. EXPORT_SYMBOL(ataraid_gendisk);
  240. EXPORT_SYMBOL(ataraid_register_disk);
  241. MODULE_LICENSE("GPL");