raid0.c 15 KB

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