raid0.c 14 KB

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