raid0.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. raid0.c : Multiple Devices driver for Linux
  3. Copyright (C) 1994-96 Marc ZYNGIER
  4. <zyngier@ufr-info-p7.ibp.fr> or
  5. <maz@gloups.fdn.fr>
  6. Copyright (C) 1999, 2000 Ingo Molnar, Red Hat
  7. RAID-0 management functions.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12. You should have received a copy of the GNU General Public License
  13. (for example /usr/src/linux/COPYING); if not, write to the Free
  14. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <linux/blkdev.h>
  17. #include <linux/seq_file.h>
  18. #include "md.h"
  19. #include "raid0.h"
  20. static void raid0_unplug(struct request_queue *q)
  21. {
  22. mddev_t *mddev = q->queuedata;
  23. raid0_conf_t *conf = mddev_to_conf(mddev);
  24. mdk_rdev_t **devlist = conf->strip_zone[0].dev;
  25. int i;
  26. for (i=0; i<mddev->raid_disks; i++) {
  27. struct request_queue *r_queue = bdev_get_queue(devlist[i]->bdev);
  28. blk_unplug(r_queue);
  29. }
  30. }
  31. static int raid0_congested(void *data, int bits)
  32. {
  33. mddev_t *mddev = data;
  34. raid0_conf_t *conf = mddev_to_conf(mddev);
  35. mdk_rdev_t **devlist = conf->strip_zone[0].dev;
  36. int i, ret = 0;
  37. for (i = 0; i < mddev->raid_disks && !ret ; i++) {
  38. struct request_queue *q = bdev_get_queue(devlist[i]->bdev);
  39. ret |= bdi_congested(&q->backing_dev_info, bits);
  40. }
  41. return ret;
  42. }
  43. static int create_strip_zones (mddev_t *mddev)
  44. {
  45. int i, c, j;
  46. sector_t curr_zone_end;
  47. raid0_conf_t *conf = mddev_to_conf(mddev);
  48. mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev;
  49. struct strip_zone *zone;
  50. int cnt;
  51. char b[BDEVNAME_SIZE];
  52. /*
  53. * The number of 'same size groups'
  54. */
  55. conf->nr_strip_zones = 0;
  56. list_for_each_entry(rdev1, &mddev->disks, same_set) {
  57. printk(KERN_INFO "raid0: looking at %s\n",
  58. bdevname(rdev1->bdev,b));
  59. c = 0;
  60. list_for_each_entry(rdev2, &mddev->disks, same_set) {
  61. printk(KERN_INFO "raid0: comparing %s(%llu)",
  62. bdevname(rdev1->bdev,b),
  63. (unsigned long long)rdev1->sectors);
  64. printk(KERN_INFO " with %s(%llu)\n",
  65. bdevname(rdev2->bdev,b),
  66. (unsigned long long)rdev2->sectors);
  67. if (rdev2 == rdev1) {
  68. printk(KERN_INFO "raid0: END\n");
  69. break;
  70. }
  71. if (rdev2->sectors == rdev1->sectors) {
  72. /*
  73. * Not unique, don't count it as a new
  74. * group
  75. */
  76. printk(KERN_INFO "raid0: EQUAL\n");
  77. c = 1;
  78. break;
  79. }
  80. printk(KERN_INFO "raid0: NOT EQUAL\n");
  81. }
  82. if (!c) {
  83. printk(KERN_INFO "raid0: ==> UNIQUE\n");
  84. conf->nr_strip_zones++;
  85. printk(KERN_INFO "raid0: %d zones\n",
  86. conf->nr_strip_zones);
  87. }
  88. }
  89. printk(KERN_INFO "raid0: FINAL %d zones\n", conf->nr_strip_zones);
  90. conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
  91. conf->nr_strip_zones, GFP_KERNEL);
  92. if (!conf->strip_zone)
  93. return 1;
  94. conf->devlist = kzalloc(sizeof(mdk_rdev_t*)*
  95. conf->nr_strip_zones*mddev->raid_disks,
  96. GFP_KERNEL);
  97. if (!conf->devlist)
  98. return 1;
  99. /* The first zone must contain all devices, so here we check that
  100. * there is a proper alignment of slots to devices and find them all
  101. */
  102. zone = &conf->strip_zone[0];
  103. cnt = 0;
  104. smallest = NULL;
  105. zone->dev = conf->devlist;
  106. list_for_each_entry(rdev1, &mddev->disks, same_set) {
  107. int j = rdev1->raid_disk;
  108. if (j < 0 || j >= mddev->raid_disks) {
  109. printk(KERN_ERR "raid0: bad disk number %d - "
  110. "aborting!\n", j);
  111. goto abort;
  112. }
  113. if (zone->dev[j]) {
  114. printk(KERN_ERR "raid0: multiple devices for %d - "
  115. "aborting!\n", j);
  116. goto abort;
  117. }
  118. zone->dev[j] = rdev1;
  119. blk_queue_stack_limits(mddev->queue,
  120. rdev1->bdev->bd_disk->queue);
  121. /* as we don't honour merge_bvec_fn, we must never risk
  122. * violating it, so limit ->max_sector to one PAGE, as
  123. * a one page request is never in violation.
  124. */
  125. if (rdev1->bdev->bd_disk->queue->merge_bvec_fn &&
  126. queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
  127. blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
  128. if (!smallest || (rdev1->sectors < smallest->sectors))
  129. smallest = rdev1;
  130. cnt++;
  131. }
  132. if (cnt != mddev->raid_disks) {
  133. printk(KERN_ERR "raid0: too few disks (%d of %d) - "
  134. "aborting!\n", cnt, mddev->raid_disks);
  135. goto abort;
  136. }
  137. zone->nb_dev = cnt;
  138. zone->sectors = smallest->sectors * cnt;
  139. zone->zone_end = zone->sectors;
  140. curr_zone_end = zone->sectors;
  141. /* now do the other zones */
  142. for (i = 1; i < conf->nr_strip_zones; i++)
  143. {
  144. zone = conf->strip_zone + i;
  145. zone->dev = conf->strip_zone[i-1].dev + mddev->raid_disks;
  146. printk(KERN_INFO "raid0: zone %d\n", i);
  147. zone->dev_start = smallest->sectors;
  148. smallest = NULL;
  149. c = 0;
  150. for (j=0; j<cnt; j++) {
  151. char b[BDEVNAME_SIZE];
  152. rdev = conf->strip_zone[0].dev[j];
  153. printk(KERN_INFO "raid0: checking %s ...",
  154. bdevname(rdev->bdev, b));
  155. if (rdev->sectors <= zone->dev_start) {
  156. printk(KERN_INFO " nope.\n");
  157. continue;
  158. }
  159. printk(KERN_INFO " contained as device %d\n", c);
  160. zone->dev[c] = rdev;
  161. c++;
  162. if (!smallest || rdev->sectors < smallest->sectors) {
  163. smallest = rdev;
  164. printk(KERN_INFO " (%llu) is smallest!.\n",
  165. (unsigned long long)rdev->sectors);
  166. }
  167. }
  168. zone->nb_dev = c;
  169. zone->sectors = (smallest->sectors - zone->dev_start) * c;
  170. printk(KERN_INFO "raid0: zone->nb_dev: %d, sectors: %llu\n",
  171. zone->nb_dev, (unsigned long long)zone->sectors);
  172. curr_zone_end += zone->sectors;
  173. zone->zone_end = curr_zone_end;
  174. printk(KERN_INFO "raid0: current zone start: %llu\n",
  175. (unsigned long long)smallest->sectors);
  176. }
  177. mddev->queue->unplug_fn = raid0_unplug;
  178. mddev->queue->backing_dev_info.congested_fn = raid0_congested;
  179. mddev->queue->backing_dev_info.congested_data = mddev;
  180. printk(KERN_INFO "raid0: done.\n");
  181. return 0;
  182. abort:
  183. return 1;
  184. }
  185. /**
  186. * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
  187. * @q: request queue
  188. * @bvm: properties of new bio
  189. * @biovec: the request that could be merged to it.
  190. *
  191. * Return amount of bytes we can accept at this offset
  192. */
  193. static int raid0_mergeable_bvec(struct request_queue *q,
  194. struct bvec_merge_data *bvm,
  195. struct bio_vec *biovec)
  196. {
  197. mddev_t *mddev = q->queuedata;
  198. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  199. int max;
  200. unsigned int chunk_sectors = mddev->chunk_size >> 9;
  201. unsigned int bio_sectors = bvm->bi_size >> 9;
  202. max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
  203. if (max < 0) max = 0; /* bio_add cannot handle a negative return */
  204. if (max <= biovec->bv_len && bio_sectors == 0)
  205. return biovec->bv_len;
  206. else
  207. return max;
  208. }
  209. static sector_t raid0_size(mddev_t *mddev, sector_t sectors, int raid_disks)
  210. {
  211. sector_t array_sectors = 0;
  212. mdk_rdev_t *rdev;
  213. WARN_ONCE(sectors || raid_disks,
  214. "%s does not support generic reshape\n", __func__);
  215. list_for_each_entry(rdev, &mddev->disks, same_set)
  216. array_sectors += rdev->sectors;
  217. return array_sectors;
  218. }
  219. static int raid0_run(mddev_t *mddev)
  220. {
  221. raid0_conf_t *conf;
  222. if (mddev->chunk_size == 0) {
  223. printk(KERN_ERR "md/raid0: non-zero chunk size required.\n");
  224. return -EINVAL;
  225. }
  226. printk(KERN_INFO "%s: setting max_sectors to %d, segment boundary to %d\n",
  227. mdname(mddev),
  228. mddev->chunk_size >> 9,
  229. (mddev->chunk_size>>1)-1);
  230. blk_queue_max_sectors(mddev->queue, mddev->chunk_size >> 9);
  231. blk_queue_segment_boundary(mddev->queue, (mddev->chunk_size>>1) - 1);
  232. mddev->queue->queue_lock = &mddev->queue->__queue_lock;
  233. conf = kmalloc(sizeof (raid0_conf_t), GFP_KERNEL);
  234. if (!conf)
  235. goto out;
  236. mddev->private = (void *)conf;
  237. conf->strip_zone = NULL;
  238. conf->devlist = NULL;
  239. if (create_strip_zones (mddev))
  240. goto out_free_conf;
  241. /* calculate array device size */
  242. md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
  243. printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
  244. (unsigned long long)mddev->array_sectors);
  245. /* calculate the max read-ahead size.
  246. * For read-ahead of large files to be effective, we need to
  247. * readahead at least twice a whole stripe. i.e. number of devices
  248. * multiplied by chunk size times 2.
  249. * If an individual device has an ra_pages greater than the
  250. * chunk size, then we will not drive that device as hard as it
  251. * wants. We consider this a configuration error: a larger
  252. * chunksize should be used in that case.
  253. */
  254. {
  255. int stripe = mddev->raid_disks * mddev->chunk_size / PAGE_SIZE;
  256. if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
  257. mddev->queue->backing_dev_info.ra_pages = 2* stripe;
  258. }
  259. blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
  260. return 0;
  261. out_free_conf:
  262. kfree(conf->strip_zone);
  263. kfree(conf->devlist);
  264. kfree(conf);
  265. mddev->private = NULL;
  266. out:
  267. return -ENOMEM;
  268. }
  269. static int raid0_stop (mddev_t *mddev)
  270. {
  271. raid0_conf_t *conf = mddev_to_conf(mddev);
  272. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  273. kfree(conf->strip_zone);
  274. conf->strip_zone = NULL;
  275. kfree(conf);
  276. mddev->private = NULL;
  277. return 0;
  278. }
  279. /* Find the zone which holds a particular offset */
  280. static struct strip_zone *find_zone(struct raid0_private_data *conf,
  281. sector_t sector)
  282. {
  283. int i;
  284. struct strip_zone *z = conf->strip_zone;
  285. for (i = 0; i < conf->nr_strip_zones; i++)
  286. if (sector < z[i].zone_end)
  287. return z + i;
  288. BUG();
  289. }
  290. static int raid0_make_request (struct request_queue *q, struct bio *bio)
  291. {
  292. mddev_t *mddev = q->queuedata;
  293. unsigned int sect_in_chunk, chunksect_bits, chunk_sects;
  294. raid0_conf_t *conf = mddev_to_conf(mddev);
  295. struct strip_zone *zone;
  296. mdk_rdev_t *tmp_dev;
  297. sector_t chunk;
  298. sector_t sector, rsect;
  299. const int rw = bio_data_dir(bio);
  300. int cpu;
  301. if (unlikely(bio_barrier(bio))) {
  302. bio_endio(bio, -EOPNOTSUPP);
  303. return 0;
  304. }
  305. cpu = part_stat_lock();
  306. part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
  307. part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
  308. bio_sectors(bio));
  309. part_stat_unlock();
  310. chunk_sects = mddev->chunk_size >> 9;
  311. chunksect_bits = ffz(~chunk_sects);
  312. sector = bio->bi_sector;
  313. if (unlikely(chunk_sects < (bio->bi_sector & (chunk_sects - 1)) + (bio->bi_size >> 9))) {
  314. struct bio_pair *bp;
  315. /* Sanity check -- queue functions should prevent this happening */
  316. if (bio->bi_vcnt != 1 ||
  317. bio->bi_idx != 0)
  318. goto bad_map;
  319. /* This is a one page bio that upper layers
  320. * refuse to split for us, so we need to split it.
  321. */
  322. bp = bio_split(bio, chunk_sects - (bio->bi_sector & (chunk_sects - 1)));
  323. if (raid0_make_request(q, &bp->bio1))
  324. generic_make_request(&bp->bio1);
  325. if (raid0_make_request(q, &bp->bio2))
  326. generic_make_request(&bp->bio2);
  327. bio_pair_release(bp);
  328. return 0;
  329. }
  330. zone = find_zone(conf, sector);
  331. sect_in_chunk = bio->bi_sector & (chunk_sects - 1);
  332. {
  333. sector_t x = (zone->sectors + sector - zone->zone_end)
  334. >> chunksect_bits;
  335. sector_div(x, zone->nb_dev);
  336. chunk = x;
  337. x = sector >> chunksect_bits;
  338. tmp_dev = zone->dev[sector_div(x, zone->nb_dev)];
  339. }
  340. rsect = (chunk << chunksect_bits) + zone->dev_start + sect_in_chunk;
  341. bio->bi_bdev = tmp_dev->bdev;
  342. bio->bi_sector = rsect + tmp_dev->data_offset;
  343. /*
  344. * Let the main block layer submit the IO and resolve recursion:
  345. */
  346. return 1;
  347. bad_map:
  348. printk("raid0_make_request bug: can't convert block across chunks"
  349. " or bigger than %dk %llu %d\n", chunk_sects / 2,
  350. (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
  351. bio_io_error(bio);
  352. return 0;
  353. }
  354. static void raid0_status (struct seq_file *seq, mddev_t *mddev)
  355. {
  356. #undef MD_DEBUG
  357. #ifdef MD_DEBUG
  358. int j, k, h;
  359. char b[BDEVNAME_SIZE];
  360. raid0_conf_t *conf = mddev_to_conf(mddev);
  361. h = 0;
  362. for (j = 0; j < conf->nr_strip_zones; j++) {
  363. seq_printf(seq, " z%d", j);
  364. seq_printf(seq, "=[");
  365. for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
  366. seq_printf(seq, "%s/", bdevname(
  367. conf->strip_zone[j].dev[k]->bdev,b));
  368. seq_printf(seq, "] ze=%d ds=%d s=%d\n",
  369. conf->strip_zone[j].zone_end,
  370. conf->strip_zone[j].dev_start,
  371. conf->strip_zone[j].sectors);
  372. }
  373. #endif
  374. seq_printf(seq, " %dk chunks", mddev->chunk_size/1024);
  375. return;
  376. }
  377. static struct mdk_personality raid0_personality=
  378. {
  379. .name = "raid0",
  380. .level = 0,
  381. .owner = THIS_MODULE,
  382. .make_request = raid0_make_request,
  383. .run = raid0_run,
  384. .stop = raid0_stop,
  385. .status = raid0_status,
  386. .size = raid0_size,
  387. };
  388. static int __init raid0_init (void)
  389. {
  390. return register_md_personality (&raid0_personality);
  391. }
  392. static void raid0_exit (void)
  393. {
  394. unregister_md_personality (&raid0_personality);
  395. }
  396. module_init(raid0_init);
  397. module_exit(raid0_exit);
  398. MODULE_LICENSE("GPL");
  399. MODULE_ALIAS("md-personality-2"); /* RAID0 */
  400. MODULE_ALIAS("md-raid0");
  401. MODULE_ALIAS("md-level-0");