raid0.c 16 KB

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