raid0.c 14 KB

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