raid0.c 15 KB

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