raid0.c 15 KB

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