raid0.c 14 KB

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