raid0.c 15 KB

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