raid0.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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->private;
  24. mdk_rdev_t **devlist = conf->devlist;
  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->private;
  35. mdk_rdev_t **devlist = conf->devlist;
  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. /*
  44. * inform the user of the raid configuration
  45. */
  46. static void dump_zones(mddev_t *mddev)
  47. {
  48. int j, k, h;
  49. sector_t zone_size = 0;
  50. sector_t zone_start = 0;
  51. char b[BDEVNAME_SIZE];
  52. raid0_conf_t *conf = mddev->private;
  53. printk(KERN_INFO "******* %s configuration *********\n",
  54. mdname(mddev));
  55. h = 0;
  56. for (j = 0; j < conf->nr_strip_zones; j++) {
  57. printk(KERN_INFO "zone%d=[", j);
  58. for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
  59. printk("%s/",
  60. bdevname(conf->devlist[j*mddev->raid_disks
  61. + k]->bdev, b));
  62. printk("]\n");
  63. zone_size = conf->strip_zone[j].zone_end - zone_start;
  64. printk(KERN_INFO " zone offset=%llukb "
  65. "device offset=%llukb size=%llukb\n",
  66. (unsigned long long)zone_start>>1,
  67. (unsigned long long)conf->strip_zone[j].dev_start>>1,
  68. (unsigned long long)zone_size>>1);
  69. zone_start = conf->strip_zone[j].zone_end;
  70. }
  71. printk(KERN_INFO "**********************************\n\n");
  72. }
  73. static int create_strip_zones(mddev_t *mddev)
  74. {
  75. int i, c, j, err;
  76. sector_t curr_zone_end, sectors;
  77. mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev, **dev;
  78. struct strip_zone *zone;
  79. int cnt;
  80. char b[BDEVNAME_SIZE];
  81. raid0_conf_t *conf = kzalloc(sizeof(*conf), GFP_KERNEL);
  82. if (!conf)
  83. return -ENOMEM;
  84. list_for_each_entry(rdev1, &mddev->disks, same_set) {
  85. printk(KERN_INFO "raid0: looking at %s\n",
  86. bdevname(rdev1->bdev,b));
  87. c = 0;
  88. list_for_each_entry(rdev2, &mddev->disks, same_set) {
  89. printk(KERN_INFO "raid0: comparing %s(%llu)",
  90. bdevname(rdev1->bdev,b),
  91. (unsigned long long)rdev1->sectors);
  92. printk(KERN_INFO " with %s(%llu)\n",
  93. bdevname(rdev2->bdev,b),
  94. (unsigned long long)rdev2->sectors);
  95. if (rdev2 == rdev1) {
  96. printk(KERN_INFO "raid0: END\n");
  97. break;
  98. }
  99. if (rdev2->sectors == rdev1->sectors) {
  100. /*
  101. * Not unique, don't count it as a new
  102. * group
  103. */
  104. printk(KERN_INFO "raid0: EQUAL\n");
  105. c = 1;
  106. break;
  107. }
  108. printk(KERN_INFO "raid0: NOT EQUAL\n");
  109. }
  110. if (!c) {
  111. printk(KERN_INFO "raid0: ==> UNIQUE\n");
  112. conf->nr_strip_zones++;
  113. printk(KERN_INFO "raid0: %d zones\n",
  114. conf->nr_strip_zones);
  115. }
  116. }
  117. printk(KERN_INFO "raid0: FINAL %d zones\n", conf->nr_strip_zones);
  118. err = -ENOMEM;
  119. conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
  120. conf->nr_strip_zones, GFP_KERNEL);
  121. if (!conf->strip_zone)
  122. goto abort;
  123. conf->devlist = kzalloc(sizeof(mdk_rdev_t*)*
  124. conf->nr_strip_zones*mddev->raid_disks,
  125. GFP_KERNEL);
  126. if (!conf->devlist)
  127. goto abort;
  128. /* The first zone must contain all devices, so here we check that
  129. * there is a proper alignment of slots to devices and find them all
  130. */
  131. zone = &conf->strip_zone[0];
  132. cnt = 0;
  133. smallest = NULL;
  134. dev = conf->devlist;
  135. err = -EINVAL;
  136. list_for_each_entry(rdev1, &mddev->disks, same_set) {
  137. int j = rdev1->raid_disk;
  138. if (j < 0 || j >= mddev->raid_disks) {
  139. printk(KERN_ERR "raid0: bad disk number %d - "
  140. "aborting!\n", j);
  141. goto abort;
  142. }
  143. if (dev[j]) {
  144. printk(KERN_ERR "raid0: multiple devices for %d - "
  145. "aborting!\n", j);
  146. goto abort;
  147. }
  148. dev[j] = rdev1;
  149. blk_queue_stack_limits(mddev->queue,
  150. rdev1->bdev->bd_disk->queue);
  151. /* as we don't honour merge_bvec_fn, we must never risk
  152. * violating it, so limit ->max_sector to one PAGE, as
  153. * a one page request is never in violation.
  154. */
  155. if (rdev1->bdev->bd_disk->queue->merge_bvec_fn &&
  156. queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
  157. blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
  158. if (!smallest || (rdev1->sectors < smallest->sectors))
  159. smallest = rdev1;
  160. cnt++;
  161. }
  162. if (cnt != mddev->raid_disks) {
  163. printk(KERN_ERR "raid0: too few disks (%d of %d) - "
  164. "aborting!\n", cnt, mddev->raid_disks);
  165. goto abort;
  166. }
  167. zone->nb_dev = cnt;
  168. zone->zone_end = smallest->sectors * cnt;
  169. curr_zone_end = zone->zone_end;
  170. /* now do the other zones */
  171. for (i = 1; i < conf->nr_strip_zones; i++)
  172. {
  173. zone = conf->strip_zone + i;
  174. dev = conf->devlist + i * mddev->raid_disks;
  175. printk(KERN_INFO "raid0: zone %d\n", i);
  176. zone->dev_start = smallest->sectors;
  177. smallest = NULL;
  178. c = 0;
  179. for (j=0; j<cnt; j++) {
  180. char b[BDEVNAME_SIZE];
  181. rdev = conf->devlist[j];
  182. printk(KERN_INFO "raid0: checking %s ...",
  183. bdevname(rdev->bdev, b));
  184. if (rdev->sectors <= zone->dev_start) {
  185. printk(KERN_INFO " nope.\n");
  186. continue;
  187. }
  188. printk(KERN_INFO " contained as device %d\n", c);
  189. dev[c] = rdev;
  190. c++;
  191. if (!smallest || rdev->sectors < smallest->sectors) {
  192. smallest = rdev;
  193. printk(KERN_INFO " (%llu) is smallest!.\n",
  194. (unsigned long long)rdev->sectors);
  195. }
  196. }
  197. zone->nb_dev = c;
  198. sectors = (smallest->sectors - zone->dev_start) * c;
  199. printk(KERN_INFO "raid0: zone->nb_dev: %d, sectors: %llu\n",
  200. zone->nb_dev, (unsigned long long)sectors);
  201. curr_zone_end += sectors;
  202. zone->zone_end = curr_zone_end;
  203. printk(KERN_INFO "raid0: current zone start: %llu\n",
  204. (unsigned long long)smallest->sectors);
  205. }
  206. mddev->queue->unplug_fn = raid0_unplug;
  207. mddev->queue->backing_dev_info.congested_fn = raid0_congested;
  208. mddev->queue->backing_dev_info.congested_data = mddev;
  209. /*
  210. * now since we have the hard sector sizes, we can make sure
  211. * chunk size is a multiple of that sector size
  212. */
  213. if (mddev->chunk_size % queue_logical_block_size(mddev->queue)) {
  214. printk(KERN_ERR "%s chunk_size of %d not valid\n",
  215. mdname(mddev),
  216. mddev->chunk_size);
  217. goto abort;
  218. }
  219. printk(KERN_INFO "raid0: done.\n");
  220. mddev->private = conf;
  221. return 0;
  222. abort:
  223. kfree(conf->strip_zone);
  224. kfree(conf->devlist);
  225. kfree(conf);
  226. mddev->private = NULL;
  227. return err;
  228. }
  229. /**
  230. * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
  231. * @q: request queue
  232. * @bvm: properties of new bio
  233. * @biovec: the request that could be merged to it.
  234. *
  235. * Return amount of bytes we can accept at this offset
  236. */
  237. static int raid0_mergeable_bvec(struct request_queue *q,
  238. struct bvec_merge_data *bvm,
  239. struct bio_vec *biovec)
  240. {
  241. mddev_t *mddev = q->queuedata;
  242. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  243. int max;
  244. unsigned int chunk_sectors = mddev->chunk_size >> 9;
  245. unsigned int bio_sectors = bvm->bi_size >> 9;
  246. max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
  247. if (max < 0) max = 0; /* bio_add cannot handle a negative return */
  248. if (max <= biovec->bv_len && bio_sectors == 0)
  249. return biovec->bv_len;
  250. else
  251. return max;
  252. }
  253. static sector_t raid0_size(mddev_t *mddev, sector_t sectors, int raid_disks)
  254. {
  255. sector_t array_sectors = 0;
  256. mdk_rdev_t *rdev;
  257. WARN_ONCE(sectors || raid_disks,
  258. "%s does not support generic reshape\n", __func__);
  259. list_for_each_entry(rdev, &mddev->disks, same_set)
  260. array_sectors += rdev->sectors;
  261. return array_sectors;
  262. }
  263. static int raid0_run(mddev_t *mddev)
  264. {
  265. int ret;
  266. if (mddev->chunk_size == 0 ||
  267. !is_power_of_2(mddev->chunk_size)) {
  268. printk(KERN_ERR "md/raid0: chunk size must be a power of 2.\n");
  269. return -EINVAL;
  270. }
  271. blk_queue_max_sectors(mddev->queue, mddev->chunk_size >> 9);
  272. mddev->queue->queue_lock = &mddev->queue->__queue_lock;
  273. ret = create_strip_zones(mddev);
  274. if (ret < 0)
  275. return ret;
  276. /* calculate array device size */
  277. md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
  278. printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
  279. (unsigned long long)mddev->array_sectors);
  280. /* calculate the max read-ahead size.
  281. * For read-ahead of large files to be effective, we need to
  282. * readahead at least twice a whole stripe. i.e. number of devices
  283. * multiplied by chunk size times 2.
  284. * If an individual device has an ra_pages greater than the
  285. * chunk size, then we will not drive that device as hard as it
  286. * wants. We consider this a configuration error: a larger
  287. * chunksize should be used in that case.
  288. */
  289. {
  290. int stripe = mddev->raid_disks * mddev->chunk_size / PAGE_SIZE;
  291. if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
  292. mddev->queue->backing_dev_info.ra_pages = 2* stripe;
  293. }
  294. blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
  295. dump_zones(mddev);
  296. return 0;
  297. }
  298. static int raid0_stop(mddev_t *mddev)
  299. {
  300. raid0_conf_t *conf = mddev->private;
  301. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  302. kfree(conf->strip_zone);
  303. kfree(conf->devlist);
  304. kfree(conf);
  305. mddev->private = NULL;
  306. return 0;
  307. }
  308. /* Find the zone which holds a particular offset
  309. * Update *sectorp to be an offset in that zone
  310. */
  311. static struct strip_zone *find_zone(struct raid0_private_data *conf,
  312. sector_t *sectorp)
  313. {
  314. int i;
  315. struct strip_zone *z = conf->strip_zone;
  316. sector_t sector = *sectorp;
  317. for (i = 0; i < conf->nr_strip_zones; i++)
  318. if (sector < z[i].zone_end) {
  319. if (i)
  320. *sectorp = sector - z[i-1].zone_end;
  321. return z + i;
  322. }
  323. BUG();
  324. }
  325. static int raid0_make_request (struct request_queue *q, struct bio *bio)
  326. {
  327. mddev_t *mddev = q->queuedata;
  328. unsigned int sect_in_chunk, chunksect_bits, chunk_sects;
  329. raid0_conf_t *conf = mddev->private;
  330. struct strip_zone *zone;
  331. mdk_rdev_t *tmp_dev;
  332. sector_t chunk;
  333. sector_t sector, rsect, sector_offset;
  334. const int rw = bio_data_dir(bio);
  335. int cpu;
  336. if (unlikely(bio_barrier(bio))) {
  337. bio_endio(bio, -EOPNOTSUPP);
  338. return 0;
  339. }
  340. cpu = part_stat_lock();
  341. part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
  342. part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
  343. bio_sectors(bio));
  344. part_stat_unlock();
  345. chunk_sects = mddev->chunk_size >> 9;
  346. chunksect_bits = ffz(~chunk_sects);
  347. sector = bio->bi_sector;
  348. if (unlikely(chunk_sects < (bio->bi_sector & (chunk_sects - 1)) + (bio->bi_size >> 9))) {
  349. struct bio_pair *bp;
  350. /* Sanity check -- queue functions should prevent this happening */
  351. if (bio->bi_vcnt != 1 ||
  352. bio->bi_idx != 0)
  353. goto bad_map;
  354. /* This is a one page bio that upper layers
  355. * refuse to split for us, so we need to split it.
  356. */
  357. bp = bio_split(bio, chunk_sects - (bio->bi_sector & (chunk_sects - 1)));
  358. if (raid0_make_request(q, &bp->bio1))
  359. generic_make_request(&bp->bio1);
  360. if (raid0_make_request(q, &bp->bio2))
  361. generic_make_request(&bp->bio2);
  362. bio_pair_release(bp);
  363. return 0;
  364. }
  365. sector_offset = sector;
  366. zone = find_zone(conf, &sector_offset);
  367. sect_in_chunk = bio->bi_sector & (chunk_sects - 1);
  368. {
  369. sector_t x = sector_offset >> chunksect_bits;
  370. sector_div(x, zone->nb_dev);
  371. chunk = x;
  372. x = sector >> chunksect_bits;
  373. tmp_dev = conf->devlist[(zone - conf->strip_zone)*mddev->raid_disks
  374. + sector_div(x, zone->nb_dev)];
  375. }
  376. rsect = (chunk << chunksect_bits) + zone->dev_start + sect_in_chunk;
  377. bio->bi_bdev = tmp_dev->bdev;
  378. bio->bi_sector = rsect + tmp_dev->data_offset;
  379. /*
  380. * Let the main block layer submit the IO and resolve recursion:
  381. */
  382. return 1;
  383. bad_map:
  384. printk("raid0_make_request bug: can't convert block across chunks"
  385. " or bigger than %dk %llu %d\n", chunk_sects / 2,
  386. (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
  387. bio_io_error(bio);
  388. return 0;
  389. }
  390. static void raid0_status(struct seq_file *seq, mddev_t *mddev)
  391. {
  392. #undef MD_DEBUG
  393. #ifdef MD_DEBUG
  394. int j, k, h;
  395. char b[BDEVNAME_SIZE];
  396. raid0_conf_t *conf = mddev->private;
  397. sector_t zone_size;
  398. sector_t zone_start = 0;
  399. h = 0;
  400. for (j = 0; j < conf->nr_strip_zones; j++) {
  401. seq_printf(seq, " z%d", j);
  402. seq_printf(seq, "=[");
  403. for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
  404. seq_printf(seq, "%s/", bdevname(
  405. conf->devlist[j*mddev->raid_disks + k]
  406. ->bdev, b));
  407. zone_size = conf->strip_zone[j].zone_end - zone_start;
  408. seq_printf(seq, "] ze=%lld ds=%lld s=%lld\n",
  409. (unsigned long long)zone_start>>1,
  410. (unsigned long long)conf->strip_zone[j].dev_start>>1,
  411. (unsigned long long)zone_size>>1);
  412. zone_start = conf->strip_zone[j].zone_end;
  413. }
  414. #endif
  415. seq_printf(seq, " %dk chunks", mddev->chunk_size/1024);
  416. return;
  417. }
  418. static struct mdk_personality raid0_personality=
  419. {
  420. .name = "raid0",
  421. .level = 0,
  422. .owner = THIS_MODULE,
  423. .make_request = raid0_make_request,
  424. .run = raid0_run,
  425. .stop = raid0_stop,
  426. .status = raid0_status,
  427. .size = raid0_size,
  428. };
  429. static int __init raid0_init (void)
  430. {
  431. return register_md_personality (&raid0_personality);
  432. }
  433. static void raid0_exit (void)
  434. {
  435. unregister_md_personality (&raid0_personality);
  436. }
  437. module_init(raid0_init);
  438. module_exit(raid0_exit);
  439. MODULE_LICENSE("GPL");
  440. MODULE_ALIAS("md-personality-2"); /* RAID0 */
  441. MODULE_ALIAS("md-raid0");
  442. MODULE_ALIAS("md-level-0");