raid0.c 15 KB

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