raid0.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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/raid/raid0.h>
  17. static void raid0_unplug(struct request_queue *q)
  18. {
  19. mddev_t *mddev = q->queuedata;
  20. raid0_conf_t *conf = mddev_to_conf(mddev);
  21. mdk_rdev_t **devlist = conf->strip_zone[0].dev;
  22. int i;
  23. for (i=0; i<mddev->raid_disks; i++) {
  24. struct request_queue *r_queue = bdev_get_queue(devlist[i]->bdev);
  25. blk_unplug(r_queue);
  26. }
  27. }
  28. static int raid0_congested(void *data, int bits)
  29. {
  30. mddev_t *mddev = data;
  31. raid0_conf_t *conf = mddev_to_conf(mddev);
  32. mdk_rdev_t **devlist = conf->strip_zone[0].dev;
  33. int i, ret = 0;
  34. for (i = 0; i < mddev->raid_disks && !ret ; i++) {
  35. struct request_queue *q = bdev_get_queue(devlist[i]->bdev);
  36. ret |= bdi_congested(&q->backing_dev_info, bits);
  37. }
  38. return ret;
  39. }
  40. static int create_strip_zones (mddev_t *mddev)
  41. {
  42. int i, c, j;
  43. sector_t current_start, curr_zone_start;
  44. sector_t min_spacing;
  45. raid0_conf_t *conf = mddev_to_conf(mddev);
  46. mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev;
  47. struct strip_zone *zone;
  48. int cnt;
  49. char b[BDEVNAME_SIZE];
  50. /*
  51. * The number of 'same size groups'
  52. */
  53. conf->nr_strip_zones = 0;
  54. list_for_each_entry(rdev1, &mddev->disks, same_set) {
  55. printk(KERN_INFO "raid0: looking at %s\n",
  56. bdevname(rdev1->bdev,b));
  57. c = 0;
  58. list_for_each_entry(rdev2, &mddev->disks, same_set) {
  59. printk(KERN_INFO "raid0: comparing %s(%llu)",
  60. bdevname(rdev1->bdev,b),
  61. (unsigned long long)rdev1->size);
  62. printk(KERN_INFO " with %s(%llu)\n",
  63. bdevname(rdev2->bdev,b),
  64. (unsigned long long)rdev2->size);
  65. if (rdev2 == rdev1) {
  66. printk(KERN_INFO "raid0: END\n");
  67. break;
  68. }
  69. if (rdev2->size == rdev1->size)
  70. {
  71. /*
  72. * Not unique, don't count it as a new
  73. * group
  74. */
  75. printk(KERN_INFO "raid0: EQUAL\n");
  76. c = 1;
  77. break;
  78. }
  79. printk(KERN_INFO "raid0: NOT EQUAL\n");
  80. }
  81. if (!c) {
  82. printk(KERN_INFO "raid0: ==> UNIQUE\n");
  83. conf->nr_strip_zones++;
  84. printk(KERN_INFO "raid0: %d zones\n",
  85. conf->nr_strip_zones);
  86. }
  87. }
  88. printk(KERN_INFO "raid0: FINAL %d zones\n", conf->nr_strip_zones);
  89. conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
  90. conf->nr_strip_zones, GFP_KERNEL);
  91. if (!conf->strip_zone)
  92. return 1;
  93. conf->devlist = kzalloc(sizeof(mdk_rdev_t*)*
  94. conf->nr_strip_zones*mddev->raid_disks,
  95. GFP_KERNEL);
  96. if (!conf->devlist)
  97. return 1;
  98. /* The first zone must contain all devices, so here we check that
  99. * there is a proper alignment of slots to devices and find them all
  100. */
  101. zone = &conf->strip_zone[0];
  102. cnt = 0;
  103. smallest = NULL;
  104. zone->dev = conf->devlist;
  105. list_for_each_entry(rdev1, &mddev->disks, same_set) {
  106. int j = rdev1->raid_disk;
  107. if (j < 0 || j >= mddev->raid_disks) {
  108. printk(KERN_ERR "raid0: bad disk number %d - "
  109. "aborting!\n", j);
  110. goto abort;
  111. }
  112. if (zone->dev[j]) {
  113. printk(KERN_ERR "raid0: multiple devices for %d - "
  114. "aborting!\n", j);
  115. goto abort;
  116. }
  117. zone->dev[j] = rdev1;
  118. blk_queue_stack_limits(mddev->queue,
  119. rdev1->bdev->bd_disk->queue);
  120. /* as we don't honour merge_bvec_fn, we must never risk
  121. * violating it, so limit ->max_sector to one PAGE, as
  122. * a one page request is never in violation.
  123. */
  124. if (rdev1->bdev->bd_disk->queue->merge_bvec_fn &&
  125. mddev->queue->max_sectors > (PAGE_SIZE>>9))
  126. blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
  127. if (!smallest || (rdev1->size <smallest->size))
  128. smallest = rdev1;
  129. cnt++;
  130. }
  131. if (cnt != mddev->raid_disks) {
  132. printk(KERN_ERR "raid0: too few disks (%d of %d) - "
  133. "aborting!\n", cnt, mddev->raid_disks);
  134. goto abort;
  135. }
  136. zone->nb_dev = cnt;
  137. zone->sectors = smallest->size * cnt * 2;
  138. zone->zone_start = 0;
  139. current_start = smallest->size * 2;
  140. curr_zone_start = zone->sectors;
  141. /* now do the other zones */
  142. for (i = 1; i < conf->nr_strip_zones; i++)
  143. {
  144. zone = conf->strip_zone + i;
  145. zone->dev = conf->strip_zone[i-1].dev + mddev->raid_disks;
  146. printk(KERN_INFO "raid0: zone %d\n", i);
  147. zone->dev_start = current_start;
  148. smallest = NULL;
  149. c = 0;
  150. for (j=0; j<cnt; j++) {
  151. char b[BDEVNAME_SIZE];
  152. rdev = conf->strip_zone[0].dev[j];
  153. printk(KERN_INFO "raid0: checking %s ...",
  154. bdevname(rdev->bdev, b));
  155. if (rdev->size > current_start / 2) {
  156. printk(KERN_INFO " contained as device %d\n",
  157. c);
  158. zone->dev[c] = rdev;
  159. c++;
  160. if (!smallest || (rdev->size <smallest->size)) {
  161. smallest = rdev;
  162. printk(KERN_INFO " (%llu) is smallest!.\n",
  163. (unsigned long long)rdev->size);
  164. }
  165. } else
  166. printk(KERN_INFO " nope.\n");
  167. }
  168. zone->nb_dev = c;
  169. zone->sectors = (smallest->size * 2 - current_start) * c;
  170. printk(KERN_INFO "raid0: zone->nb_dev: %d, sectors: %llu\n",
  171. zone->nb_dev, (unsigned long long)zone->sectors);
  172. zone->zone_start = curr_zone_start;
  173. curr_zone_start += zone->sectors;
  174. current_start = smallest->size * 2;
  175. printk(KERN_INFO "raid0: current zone start: %llu\n",
  176. (unsigned long long)current_start);
  177. }
  178. /* Now find appropriate hash spacing.
  179. * We want a number which causes most hash entries to cover
  180. * at most two strips, but the hash table must be at most
  181. * 1 PAGE. We choose the smallest strip, or contiguous collection
  182. * of strips, that has big enough size. We never consider the last
  183. * strip though as it's size has no bearing on the efficacy of the hash
  184. * table.
  185. */
  186. conf->spacing = curr_zone_start;
  187. min_spacing = curr_zone_start;
  188. sector_div(min_spacing, PAGE_SIZE/sizeof(struct strip_zone*));
  189. for (i=0; i < conf->nr_strip_zones-1; i++) {
  190. sector_t s = 0;
  191. for (j = i; j < conf->nr_strip_zones - 1 &&
  192. s < min_spacing; j++)
  193. s += conf->strip_zone[j].sectors;
  194. if (s >= min_spacing && s < conf->spacing)
  195. conf->spacing = s;
  196. }
  197. mddev->queue->unplug_fn = raid0_unplug;
  198. mddev->queue->backing_dev_info.congested_fn = raid0_congested;
  199. mddev->queue->backing_dev_info.congested_data = mddev;
  200. printk(KERN_INFO "raid0: done.\n");
  201. return 0;
  202. abort:
  203. return 1;
  204. }
  205. /**
  206. * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
  207. * @q: request queue
  208. * @bvm: properties of new bio
  209. * @biovec: the request that could be merged to it.
  210. *
  211. * Return amount of bytes we can accept at this offset
  212. */
  213. static int raid0_mergeable_bvec(struct request_queue *q,
  214. struct bvec_merge_data *bvm,
  215. struct bio_vec *biovec)
  216. {
  217. mddev_t *mddev = q->queuedata;
  218. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  219. int max;
  220. unsigned int chunk_sectors = mddev->chunk_size >> 9;
  221. unsigned int bio_sectors = bvm->bi_size >> 9;
  222. max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
  223. if (max < 0) max = 0; /* bio_add cannot handle a negative return */
  224. if (max <= biovec->bv_len && bio_sectors == 0)
  225. return biovec->bv_len;
  226. else
  227. return max;
  228. }
  229. static int raid0_run (mddev_t *mddev)
  230. {
  231. unsigned cur=0, i=0, nb_zone;
  232. s64 sectors;
  233. raid0_conf_t *conf;
  234. mdk_rdev_t *rdev;
  235. if (mddev->chunk_size == 0) {
  236. printk(KERN_ERR "md/raid0: non-zero chunk size required.\n");
  237. return -EINVAL;
  238. }
  239. printk(KERN_INFO "%s: setting max_sectors to %d, segment boundary to %d\n",
  240. mdname(mddev),
  241. mddev->chunk_size >> 9,
  242. (mddev->chunk_size>>1)-1);
  243. blk_queue_max_sectors(mddev->queue, mddev->chunk_size >> 9);
  244. blk_queue_segment_boundary(mddev->queue, (mddev->chunk_size>>1) - 1);
  245. mddev->queue->queue_lock = &mddev->queue->__queue_lock;
  246. conf = kmalloc(sizeof (raid0_conf_t), GFP_KERNEL);
  247. if (!conf)
  248. goto out;
  249. mddev->private = (void *)conf;
  250. conf->strip_zone = NULL;
  251. conf->devlist = NULL;
  252. if (create_strip_zones (mddev))
  253. goto out_free_conf;
  254. /* calculate array device size */
  255. mddev->array_sectors = 0;
  256. list_for_each_entry(rdev, &mddev->disks, same_set)
  257. mddev->array_sectors += rdev->size * 2;
  258. printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
  259. (unsigned long long)mddev->array_sectors);
  260. printk(KERN_INFO "raid0 : conf->spacing is %llu sectors.\n",
  261. (unsigned long long)conf->spacing);
  262. {
  263. sector_t s = mddev->array_sectors;
  264. sector_t space = conf->spacing;
  265. int round;
  266. conf->sector_shift = 0;
  267. if (sizeof(sector_t) > sizeof(u32)) {
  268. /*shift down space and s so that sector_div will work */
  269. while (space > (sector_t) (~(u32)0)) {
  270. s >>= 1;
  271. space >>= 1;
  272. s += 1; /* force round-up */
  273. conf->sector_shift++;
  274. }
  275. }
  276. round = sector_div(s, (u32)space) ? 1 : 0;
  277. nb_zone = s + round;
  278. }
  279. printk(KERN_INFO "raid0 : nb_zone is %d.\n", nb_zone);
  280. printk(KERN_INFO "raid0 : Allocating %zu bytes for hash.\n",
  281. nb_zone*sizeof(struct strip_zone*));
  282. conf->hash_table = kmalloc (sizeof (struct strip_zone *)*nb_zone, GFP_KERNEL);
  283. if (!conf->hash_table)
  284. goto out_free_conf;
  285. sectors = conf->strip_zone[cur].sectors;
  286. conf->hash_table[0] = conf->strip_zone + cur;
  287. for (i=1; i< nb_zone; i++) {
  288. while (sectors <= conf->spacing) {
  289. cur++;
  290. sectors += conf->strip_zone[cur].sectors;
  291. }
  292. sectors -= conf->spacing;
  293. conf->hash_table[i] = conf->strip_zone + cur;
  294. }
  295. if (conf->sector_shift) {
  296. conf->spacing >>= conf->sector_shift;
  297. /* round spacing up so when we divide by it, we
  298. * err on the side of too-low, which is safest
  299. */
  300. conf->spacing++;
  301. }
  302. /* calculate the max read-ahead size.
  303. * For read-ahead of large files to be effective, we need to
  304. * readahead at least twice a whole stripe. i.e. number of devices
  305. * multiplied by chunk size times 2.
  306. * If an individual device has an ra_pages greater than the
  307. * chunk size, then we will not drive that device as hard as it
  308. * wants. We consider this a configuration error: a larger
  309. * chunksize should be used in that case.
  310. */
  311. {
  312. int stripe = mddev->raid_disks * mddev->chunk_size / PAGE_SIZE;
  313. if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
  314. mddev->queue->backing_dev_info.ra_pages = 2* stripe;
  315. }
  316. blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
  317. return 0;
  318. out_free_conf:
  319. kfree(conf->strip_zone);
  320. kfree(conf->devlist);
  321. kfree(conf);
  322. mddev->private = NULL;
  323. out:
  324. return -ENOMEM;
  325. }
  326. static int raid0_stop (mddev_t *mddev)
  327. {
  328. raid0_conf_t *conf = mddev_to_conf(mddev);
  329. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  330. kfree(conf->hash_table);
  331. conf->hash_table = NULL;
  332. kfree(conf->strip_zone);
  333. conf->strip_zone = NULL;
  334. kfree(conf);
  335. mddev->private = NULL;
  336. return 0;
  337. }
  338. static int raid0_make_request (struct request_queue *q, struct bio *bio)
  339. {
  340. mddev_t *mddev = q->queuedata;
  341. unsigned int sect_in_chunk, chunksect_bits, chunk_sects;
  342. raid0_conf_t *conf = mddev_to_conf(mddev);
  343. struct strip_zone *zone;
  344. mdk_rdev_t *tmp_dev;
  345. sector_t chunk;
  346. sector_t sector, rsect;
  347. const int rw = bio_data_dir(bio);
  348. int cpu;
  349. if (unlikely(bio_barrier(bio))) {
  350. bio_endio(bio, -EOPNOTSUPP);
  351. return 0;
  352. }
  353. cpu = part_stat_lock();
  354. part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
  355. part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
  356. bio_sectors(bio));
  357. part_stat_unlock();
  358. chunk_sects = mddev->chunk_size >> 9;
  359. chunksect_bits = ffz(~chunk_sects);
  360. sector = bio->bi_sector;
  361. if (unlikely(chunk_sects < (bio->bi_sector & (chunk_sects - 1)) + (bio->bi_size >> 9))) {
  362. struct bio_pair *bp;
  363. /* Sanity check -- queue functions should prevent this happening */
  364. if (bio->bi_vcnt != 1 ||
  365. bio->bi_idx != 0)
  366. goto bad_map;
  367. /* This is a one page bio that upper layers
  368. * refuse to split for us, so we need to split it.
  369. */
  370. bp = bio_split(bio, chunk_sects - (bio->bi_sector & (chunk_sects - 1)));
  371. if (raid0_make_request(q, &bp->bio1))
  372. generic_make_request(&bp->bio1);
  373. if (raid0_make_request(q, &bp->bio2))
  374. generic_make_request(&bp->bio2);
  375. bio_pair_release(bp);
  376. return 0;
  377. }
  378. {
  379. sector_t x = sector >> conf->sector_shift;
  380. sector_div(x, (u32)conf->spacing);
  381. zone = conf->hash_table[x];
  382. }
  383. while (sector >= zone->zone_start + zone->sectors)
  384. zone++;
  385. sect_in_chunk = bio->bi_sector & (chunk_sects - 1);
  386. {
  387. sector_t x = (sector - zone->zone_start) >> chunksect_bits;
  388. sector_div(x, zone->nb_dev);
  389. chunk = x;
  390. x = sector >> chunksect_bits;
  391. tmp_dev = zone->dev[sector_div(x, zone->nb_dev)];
  392. }
  393. rsect = (chunk << chunksect_bits) + zone->dev_start + sect_in_chunk;
  394. bio->bi_bdev = tmp_dev->bdev;
  395. bio->bi_sector = rsect + tmp_dev->data_offset;
  396. /*
  397. * Let the main block layer submit the IO and resolve recursion:
  398. */
  399. return 1;
  400. bad_map:
  401. printk("raid0_make_request bug: can't convert block across chunks"
  402. " or bigger than %dk %llu %d\n", chunk_sects / 2,
  403. (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
  404. bio_io_error(bio);
  405. return 0;
  406. }
  407. static void raid0_status (struct seq_file *seq, mddev_t *mddev)
  408. {
  409. #undef MD_DEBUG
  410. #ifdef MD_DEBUG
  411. int j, k, h;
  412. char b[BDEVNAME_SIZE];
  413. raid0_conf_t *conf = mddev_to_conf(mddev);
  414. h = 0;
  415. for (j = 0; j < conf->nr_strip_zones; j++) {
  416. seq_printf(seq, " z%d", j);
  417. if (conf->hash_table[h] == conf->strip_zone+j)
  418. seq_printf(seq, "(h%d)", h++);
  419. seq_printf(seq, "=[");
  420. for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
  421. seq_printf(seq, "%s/", bdevname(
  422. conf->strip_zone[j].dev[k]->bdev,b));
  423. seq_printf(seq, "] zs=%d ds=%d s=%d\n",
  424. conf->strip_zone[j].zone_start,
  425. conf->strip_zone[j].dev_start,
  426. conf->strip_zone[j].sectors);
  427. }
  428. #endif
  429. seq_printf(seq, " %dk chunks", mddev->chunk_size/1024);
  430. return;
  431. }
  432. static struct mdk_personality raid0_personality=
  433. {
  434. .name = "raid0",
  435. .level = 0,
  436. .owner = THIS_MODULE,
  437. .make_request = raid0_make_request,
  438. .run = raid0_run,
  439. .stop = raid0_stop,
  440. .status = raid0_status,
  441. };
  442. static int __init raid0_init (void)
  443. {
  444. return register_md_personality (&raid0_personality);
  445. }
  446. static void raid0_exit (void)
  447. {
  448. unregister_md_personality (&raid0_personality);
  449. }
  450. module_init(raid0_init);
  451. module_exit(raid0_exit);
  452. MODULE_LICENSE("GPL");
  453. MODULE_ALIAS("md-personality-2"); /* RAID0 */
  454. MODULE_ALIAS("md-raid0");
  455. MODULE_ALIAS("md-level-0");