raid0.c 15 KB

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