raid0.c 15 KB

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