raid0.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. #include "raid5.h"
  21. static void raid0_unplug(struct request_queue *q)
  22. {
  23. mddev_t *mddev = q->queuedata;
  24. raid0_conf_t *conf = mddev->private;
  25. mdk_rdev_t **devlist = conf->devlist;
  26. int raid_disks = conf->strip_zone[0].nb_dev;
  27. int i;
  28. for (i=0; i < raid_disks; i++) {
  29. struct request_queue *r_queue = bdev_get_queue(devlist[i]->bdev);
  30. blk_unplug(r_queue);
  31. }
  32. }
  33. static int raid0_congested(void *data, int bits)
  34. {
  35. mddev_t *mddev = data;
  36. raid0_conf_t *conf = mddev->private;
  37. mdk_rdev_t **devlist = conf->devlist;
  38. int raid_disks = conf->strip_zone[0].nb_dev;
  39. int i, ret = 0;
  40. if (mddev_congested(mddev, bits))
  41. return 1;
  42. for (i = 0; i < raid_disks && !ret ; i++) {
  43. struct request_queue *q = bdev_get_queue(devlist[i]->bdev);
  44. ret |= bdi_congested(&q->backing_dev_info, bits);
  45. }
  46. return ret;
  47. }
  48. /*
  49. * inform the user of the raid configuration
  50. */
  51. static void dump_zones(mddev_t *mddev)
  52. {
  53. int j, k, h;
  54. sector_t zone_size = 0;
  55. sector_t zone_start = 0;
  56. char b[BDEVNAME_SIZE];
  57. raid0_conf_t *conf = mddev->private;
  58. int raid_disks = conf->strip_zone[0].nb_dev;
  59. printk(KERN_INFO "******* %s configuration *********\n",
  60. mdname(mddev));
  61. h = 0;
  62. for (j = 0; j < conf->nr_strip_zones; j++) {
  63. printk(KERN_INFO "zone%d=[", j);
  64. for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
  65. printk(KERN_CONT "%s/",
  66. bdevname(conf->devlist[j*raid_disks
  67. + k]->bdev, b));
  68. printk(KERN_CONT "]\n");
  69. zone_size = conf->strip_zone[j].zone_end - zone_start;
  70. printk(KERN_INFO " zone offset=%llukb "
  71. "device offset=%llukb size=%llukb\n",
  72. (unsigned long long)zone_start>>1,
  73. (unsigned long long)conf->strip_zone[j].dev_start>>1,
  74. (unsigned long long)zone_size>>1);
  75. zone_start = conf->strip_zone[j].zone_end;
  76. }
  77. printk(KERN_INFO "**********************************\n\n");
  78. }
  79. static int create_strip_zones(mddev_t *mddev, raid0_conf_t **private_conf)
  80. {
  81. int i, c, err;
  82. sector_t curr_zone_end, sectors;
  83. mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev, **dev;
  84. struct strip_zone *zone;
  85. int cnt;
  86. char b[BDEVNAME_SIZE];
  87. raid0_conf_t *conf = kzalloc(sizeof(*conf), GFP_KERNEL);
  88. if (!conf)
  89. return -ENOMEM;
  90. list_for_each_entry(rdev1, &mddev->disks, same_set) {
  91. printk(KERN_INFO "md/raid0:%s: looking at %s\n",
  92. mdname(mddev),
  93. bdevname(rdev1->bdev, b));
  94. c = 0;
  95. /* round size to chunk_size */
  96. sectors = rdev1->sectors;
  97. sector_div(sectors, mddev->chunk_sectors);
  98. rdev1->sectors = sectors * mddev->chunk_sectors;
  99. list_for_each_entry(rdev2, &mddev->disks, same_set) {
  100. printk(KERN_INFO "md/raid0:%s: comparing %s(%llu)",
  101. mdname(mddev),
  102. bdevname(rdev1->bdev,b),
  103. (unsigned long long)rdev1->sectors);
  104. printk(KERN_CONT " with %s(%llu)\n",
  105. bdevname(rdev2->bdev,b),
  106. (unsigned long long)rdev2->sectors);
  107. if (rdev2 == rdev1) {
  108. printk(KERN_INFO "md/raid0:%s: END\n",
  109. mdname(mddev));
  110. break;
  111. }
  112. if (rdev2->sectors == rdev1->sectors) {
  113. /*
  114. * Not unique, don't count it as a new
  115. * group
  116. */
  117. printk(KERN_INFO "md/raid0:%s: EQUAL\n",
  118. mdname(mddev));
  119. c = 1;
  120. break;
  121. }
  122. printk(KERN_INFO "md/raid0:%s: NOT EQUAL\n",
  123. mdname(mddev));
  124. }
  125. if (!c) {
  126. printk(KERN_INFO "md/raid0:%s: ==> UNIQUE\n",
  127. mdname(mddev));
  128. conf->nr_strip_zones++;
  129. printk(KERN_INFO "md/raid0:%s: %d zones\n",
  130. mdname(mddev), conf->nr_strip_zones);
  131. }
  132. }
  133. printk(KERN_INFO "md/raid0:%s: FINAL %d zones\n",
  134. mdname(mddev), conf->nr_strip_zones);
  135. err = -ENOMEM;
  136. conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
  137. conf->nr_strip_zones, GFP_KERNEL);
  138. if (!conf->strip_zone)
  139. goto abort;
  140. conf->devlist = kzalloc(sizeof(mdk_rdev_t*)*
  141. conf->nr_strip_zones*mddev->raid_disks,
  142. GFP_KERNEL);
  143. if (!conf->devlist)
  144. goto abort;
  145. /* The first zone must contain all devices, so here we check that
  146. * there is a proper alignment of slots to devices and find them all
  147. */
  148. zone = &conf->strip_zone[0];
  149. cnt = 0;
  150. smallest = NULL;
  151. dev = conf->devlist;
  152. err = -EINVAL;
  153. list_for_each_entry(rdev1, &mddev->disks, same_set) {
  154. int j = rdev1->raid_disk;
  155. if (mddev->level == 10)
  156. /* taking over a raid10-n2 array */
  157. j /= 2;
  158. if (j < 0 || j >= mddev->raid_disks) {
  159. printk(KERN_ERR "md/raid0:%s: bad disk number %d - "
  160. "aborting!\n", mdname(mddev), j);
  161. goto abort;
  162. }
  163. if (dev[j]) {
  164. printk(KERN_ERR "md/raid0:%s: multiple devices for %d - "
  165. "aborting!\n", mdname(mddev), j);
  166. goto abort;
  167. }
  168. dev[j] = rdev1;
  169. disk_stack_limits(mddev->gendisk, rdev1->bdev,
  170. rdev1->data_offset << 9);
  171. /* as we don't honour merge_bvec_fn, we must never risk
  172. * violating it, so limit ->max_segments to 1, lying within
  173. * a single page.
  174. */
  175. if (rdev1->bdev->bd_disk->queue->merge_bvec_fn) {
  176. blk_queue_max_segments(mddev->queue, 1);
  177. blk_queue_segment_boundary(mddev->queue,
  178. PAGE_CACHE_SIZE - 1);
  179. }
  180. if (!smallest || (rdev1->sectors < smallest->sectors))
  181. smallest = rdev1;
  182. cnt++;
  183. }
  184. if (cnt != mddev->raid_disks) {
  185. printk(KERN_ERR "md/raid0:%s: too few disks (%d of %d) - "
  186. "aborting!\n", mdname(mddev), cnt, mddev->raid_disks);
  187. goto abort;
  188. }
  189. zone->nb_dev = cnt;
  190. zone->zone_end = smallest->sectors * cnt;
  191. curr_zone_end = zone->zone_end;
  192. /* now do the other zones */
  193. for (i = 1; i < conf->nr_strip_zones; i++)
  194. {
  195. int j;
  196. zone = conf->strip_zone + i;
  197. dev = conf->devlist + i * mddev->raid_disks;
  198. printk(KERN_INFO "md/raid0:%s: zone %d\n",
  199. mdname(mddev), i);
  200. zone->dev_start = smallest->sectors;
  201. smallest = NULL;
  202. c = 0;
  203. for (j=0; j<cnt; j++) {
  204. rdev = conf->devlist[j];
  205. printk(KERN_INFO "md/raid0:%s: checking %s ...",
  206. mdname(mddev),
  207. bdevname(rdev->bdev, b));
  208. if (rdev->sectors <= zone->dev_start) {
  209. printk(KERN_CONT " nope.\n");
  210. continue;
  211. }
  212. printk(KERN_CONT " contained as device %d\n", c);
  213. dev[c] = rdev;
  214. c++;
  215. if (!smallest || rdev->sectors < smallest->sectors) {
  216. smallest = rdev;
  217. printk(KERN_INFO "md/raid0:%s: (%llu) is smallest!.\n",
  218. mdname(mddev),
  219. (unsigned long long)rdev->sectors);
  220. }
  221. }
  222. zone->nb_dev = c;
  223. sectors = (smallest->sectors - zone->dev_start) * c;
  224. printk(KERN_INFO "md/raid0:%s: zone->nb_dev: %d, sectors: %llu\n",
  225. mdname(mddev),
  226. zone->nb_dev, (unsigned long long)sectors);
  227. curr_zone_end += sectors;
  228. zone->zone_end = curr_zone_end;
  229. printk(KERN_INFO "md/raid0:%s: current zone start: %llu\n",
  230. mdname(mddev),
  231. (unsigned long long)smallest->sectors);
  232. }
  233. mddev->queue->unplug_fn = raid0_unplug;
  234. mddev->queue->backing_dev_info.congested_fn = raid0_congested;
  235. mddev->queue->backing_dev_info.congested_data = mddev;
  236. /*
  237. * now since we have the hard sector sizes, we can make sure
  238. * chunk size is a multiple of that sector size
  239. */
  240. if ((mddev->chunk_sectors << 9) % queue_logical_block_size(mddev->queue)) {
  241. printk(KERN_ERR "md/raid0:%s: chunk_size of %d not valid\n",
  242. mdname(mddev),
  243. mddev->chunk_sectors << 9);
  244. goto abort;
  245. }
  246. blk_queue_io_min(mddev->queue, mddev->chunk_sectors << 9);
  247. blk_queue_io_opt(mddev->queue,
  248. (mddev->chunk_sectors << 9) * mddev->raid_disks);
  249. printk(KERN_INFO "md/raid0:%s: done.\n", mdname(mddev));
  250. *private_conf = conf;
  251. return 0;
  252. abort:
  253. kfree(conf->strip_zone);
  254. kfree(conf->devlist);
  255. kfree(conf);
  256. *private_conf = NULL;
  257. return err;
  258. }
  259. /**
  260. * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
  261. * @q: request queue
  262. * @bvm: properties of new bio
  263. * @biovec: the request that could be merged to it.
  264. *
  265. * Return amount of bytes we can accept at this offset
  266. */
  267. static int raid0_mergeable_bvec(struct request_queue *q,
  268. struct bvec_merge_data *bvm,
  269. struct bio_vec *biovec)
  270. {
  271. mddev_t *mddev = q->queuedata;
  272. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  273. int max;
  274. unsigned int chunk_sectors = mddev->chunk_sectors;
  275. unsigned int bio_sectors = bvm->bi_size >> 9;
  276. if (is_power_of_2(chunk_sectors))
  277. max = (chunk_sectors - ((sector & (chunk_sectors-1))
  278. + bio_sectors)) << 9;
  279. else
  280. max = (chunk_sectors - (sector_div(sector, chunk_sectors)
  281. + bio_sectors)) << 9;
  282. if (max < 0) max = 0; /* bio_add cannot handle a negative return */
  283. if (max <= biovec->bv_len && bio_sectors == 0)
  284. return biovec->bv_len;
  285. else
  286. return max;
  287. }
  288. static sector_t raid0_size(mddev_t *mddev, sector_t sectors, int raid_disks)
  289. {
  290. sector_t array_sectors = 0;
  291. mdk_rdev_t *rdev;
  292. WARN_ONCE(sectors || raid_disks,
  293. "%s does not support generic reshape\n", __func__);
  294. list_for_each_entry(rdev, &mddev->disks, same_set)
  295. array_sectors += rdev->sectors;
  296. return array_sectors;
  297. }
  298. static int raid0_run(mddev_t *mddev)
  299. {
  300. raid0_conf_t *conf;
  301. int ret;
  302. if (mddev->chunk_sectors == 0) {
  303. printk(KERN_ERR "md/raid0:%s: chunk size must be set.\n",
  304. mdname(mddev));
  305. return -EINVAL;
  306. }
  307. if (md_check_no_bitmap(mddev))
  308. return -EINVAL;
  309. blk_queue_max_hw_sectors(mddev->queue, mddev->chunk_sectors);
  310. mddev->queue->queue_lock = &mddev->queue->__queue_lock;
  311. /* if private is not null, we are here after takeover */
  312. if (mddev->private == NULL) {
  313. ret = create_strip_zones(mddev, &conf);
  314. if (ret < 0)
  315. return ret;
  316. mddev->private = conf;
  317. }
  318. conf = mddev->private;
  319. if (conf->scale_raid_disks) {
  320. int i;
  321. for (i=0; i < conf->strip_zone[0].nb_dev; i++)
  322. conf->devlist[i]->raid_disk /= conf->scale_raid_disks;
  323. /* FIXME update sysfs rd links */
  324. }
  325. /* calculate array device size */
  326. md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
  327. printk(KERN_INFO "md/raid0:%s: md_size is %llu sectors.\n",
  328. mdname(mddev),
  329. (unsigned long long)mddev->array_sectors);
  330. /* calculate the max read-ahead size.
  331. * For read-ahead of large files to be effective, we need to
  332. * readahead at least twice a whole stripe. i.e. number of devices
  333. * multiplied by chunk size times 2.
  334. * If an individual device has an ra_pages greater than the
  335. * chunk size, then we will not drive that device as hard as it
  336. * wants. We consider this a configuration error: a larger
  337. * chunksize should be used in that case.
  338. */
  339. {
  340. int stripe = mddev->raid_disks *
  341. (mddev->chunk_sectors << 9) / PAGE_SIZE;
  342. if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
  343. mddev->queue->backing_dev_info.ra_pages = 2* stripe;
  344. }
  345. blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
  346. dump_zones(mddev);
  347. md_integrity_register(mddev);
  348. return 0;
  349. }
  350. static int raid0_stop(mddev_t *mddev)
  351. {
  352. raid0_conf_t *conf = mddev->private;
  353. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  354. kfree(conf->strip_zone);
  355. kfree(conf->devlist);
  356. kfree(conf);
  357. mddev->private = NULL;
  358. return 0;
  359. }
  360. /* Find the zone which holds a particular offset
  361. * Update *sectorp to be an offset in that zone
  362. */
  363. static struct strip_zone *find_zone(struct raid0_private_data *conf,
  364. sector_t *sectorp)
  365. {
  366. int i;
  367. struct strip_zone *z = conf->strip_zone;
  368. sector_t sector = *sectorp;
  369. for (i = 0; i < conf->nr_strip_zones; i++)
  370. if (sector < z[i].zone_end) {
  371. if (i)
  372. *sectorp = sector - z[i-1].zone_end;
  373. return z + i;
  374. }
  375. BUG();
  376. }
  377. /*
  378. * remaps the bio to the target device. we separate two flows.
  379. * power 2 flow and a general flow for the sake of perfromance
  380. */
  381. static mdk_rdev_t *map_sector(mddev_t *mddev, struct strip_zone *zone,
  382. sector_t sector, sector_t *sector_offset)
  383. {
  384. unsigned int sect_in_chunk;
  385. sector_t chunk;
  386. raid0_conf_t *conf = mddev->private;
  387. int raid_disks = conf->strip_zone[0].nb_dev;
  388. unsigned int chunk_sects = mddev->chunk_sectors;
  389. if (is_power_of_2(chunk_sects)) {
  390. int chunksect_bits = ffz(~chunk_sects);
  391. /* find the sector offset inside the chunk */
  392. sect_in_chunk = sector & (chunk_sects - 1);
  393. sector >>= chunksect_bits;
  394. /* chunk in zone */
  395. chunk = *sector_offset;
  396. /* quotient is the chunk in real device*/
  397. sector_div(chunk, zone->nb_dev << chunksect_bits);
  398. } else{
  399. sect_in_chunk = sector_div(sector, chunk_sects);
  400. chunk = *sector_offset;
  401. sector_div(chunk, chunk_sects * zone->nb_dev);
  402. }
  403. /*
  404. * position the bio over the real device
  405. * real sector = chunk in device + starting of zone
  406. * + the position in the chunk
  407. */
  408. *sector_offset = (chunk * chunk_sects) + sect_in_chunk;
  409. return conf->devlist[(zone - conf->strip_zone)*raid_disks
  410. + sector_div(sector, zone->nb_dev)];
  411. }
  412. /*
  413. * Is io distribute over 1 or more chunks ?
  414. */
  415. static inline int is_io_in_chunk_boundary(mddev_t *mddev,
  416. unsigned int chunk_sects, struct bio *bio)
  417. {
  418. if (likely(is_power_of_2(chunk_sects))) {
  419. return chunk_sects >= ((bio->bi_sector & (chunk_sects-1))
  420. + (bio->bi_size >> 9));
  421. } else{
  422. sector_t sector = bio->bi_sector;
  423. return chunk_sects >= (sector_div(sector, chunk_sects)
  424. + (bio->bi_size >> 9));
  425. }
  426. }
  427. static int raid0_make_request(mddev_t *mddev, struct bio *bio)
  428. {
  429. unsigned int chunk_sects;
  430. sector_t sector_offset;
  431. struct strip_zone *zone;
  432. mdk_rdev_t *tmp_dev;
  433. if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
  434. md_barrier_request(mddev, bio);
  435. return 0;
  436. }
  437. chunk_sects = mddev->chunk_sectors;
  438. if (unlikely(!is_io_in_chunk_boundary(mddev, chunk_sects, bio))) {
  439. sector_t sector = bio->bi_sector;
  440. struct bio_pair *bp;
  441. /* Sanity check -- queue functions should prevent this happening */
  442. if (bio->bi_vcnt != 1 ||
  443. bio->bi_idx != 0)
  444. goto bad_map;
  445. /* This is a one page bio that upper layers
  446. * refuse to split for us, so we need to split it.
  447. */
  448. if (likely(is_power_of_2(chunk_sects)))
  449. bp = bio_split(bio, chunk_sects - (sector &
  450. (chunk_sects-1)));
  451. else
  452. bp = bio_split(bio, chunk_sects -
  453. sector_div(sector, chunk_sects));
  454. if (raid0_make_request(mddev, &bp->bio1))
  455. generic_make_request(&bp->bio1);
  456. if (raid0_make_request(mddev, &bp->bio2))
  457. generic_make_request(&bp->bio2);
  458. bio_pair_release(bp);
  459. return 0;
  460. }
  461. sector_offset = bio->bi_sector;
  462. zone = find_zone(mddev->private, &sector_offset);
  463. tmp_dev = map_sector(mddev, zone, bio->bi_sector,
  464. &sector_offset);
  465. bio->bi_bdev = tmp_dev->bdev;
  466. bio->bi_sector = sector_offset + zone->dev_start +
  467. tmp_dev->data_offset;
  468. /*
  469. * Let the main block layer submit the IO and resolve recursion:
  470. */
  471. return 1;
  472. bad_map:
  473. printk("md/raid0:%s: make_request bug: can't convert block across chunks"
  474. " or bigger than %dk %llu %d\n",
  475. mdname(mddev), chunk_sects / 2,
  476. (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
  477. bio_io_error(bio);
  478. return 0;
  479. }
  480. static void raid0_status(struct seq_file *seq, mddev_t *mddev)
  481. {
  482. #undef MD_DEBUG
  483. #ifdef MD_DEBUG
  484. int j, k, h;
  485. char b[BDEVNAME_SIZE];
  486. raid0_conf_t *conf = mddev->private;
  487. int raid_disks = conf->strip_zone[0].nb_dev;
  488. sector_t zone_size;
  489. sector_t zone_start = 0;
  490. h = 0;
  491. for (j = 0; j < conf->nr_strip_zones; j++) {
  492. seq_printf(seq, " z%d", j);
  493. seq_printf(seq, "=[");
  494. for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
  495. seq_printf(seq, "%s/", bdevname(
  496. conf->devlist[j*raid_disks + k]
  497. ->bdev, b));
  498. zone_size = conf->strip_zone[j].zone_end - zone_start;
  499. seq_printf(seq, "] ze=%lld ds=%lld s=%lld\n",
  500. (unsigned long long)zone_start>>1,
  501. (unsigned long long)conf->strip_zone[j].dev_start>>1,
  502. (unsigned long long)zone_size>>1);
  503. zone_start = conf->strip_zone[j].zone_end;
  504. }
  505. #endif
  506. seq_printf(seq, " %dk chunks", mddev->chunk_sectors / 2);
  507. return;
  508. }
  509. static void *raid0_takeover_raid5(mddev_t *mddev)
  510. {
  511. mdk_rdev_t *rdev;
  512. raid0_conf_t *priv_conf;
  513. if (mddev->degraded != 1) {
  514. printk(KERN_ERR "md/raid0:%s: raid5 must be degraded! Degraded disks: %d\n",
  515. mdname(mddev),
  516. mddev->degraded);
  517. return ERR_PTR(-EINVAL);
  518. }
  519. list_for_each_entry(rdev, &mddev->disks, same_set) {
  520. /* check slot number for a disk */
  521. if (rdev->raid_disk == mddev->raid_disks-1) {
  522. printk(KERN_ERR "md/raid0:%s: raid5 must have missing parity disk!\n",
  523. mdname(mddev));
  524. return ERR_PTR(-EINVAL);
  525. }
  526. }
  527. /* Set new parameters */
  528. mddev->new_level = 0;
  529. mddev->new_chunk_sectors = mddev->chunk_sectors;
  530. mddev->raid_disks--;
  531. mddev->delta_disks = -1;
  532. /* make sure it will be not marked as dirty */
  533. mddev->recovery_cp = MaxSector;
  534. create_strip_zones(mddev, &priv_conf);
  535. return priv_conf;
  536. }
  537. static void *raid0_takeover_raid10(mddev_t *mddev)
  538. {
  539. raid0_conf_t *priv_conf;
  540. /* Check layout:
  541. * - far_copies must be 1
  542. * - near_copies must be 2
  543. * - disks number must be even
  544. * - all mirrors must be already degraded
  545. */
  546. if (mddev->layout != ((1 << 8) + 2)) {
  547. printk(KERN_ERR "md/raid0:%s:: Raid0 cannot takover layout: 0x%x\n",
  548. mdname(mddev),
  549. mddev->layout);
  550. return ERR_PTR(-EINVAL);
  551. }
  552. if (mddev->raid_disks & 1) {
  553. printk(KERN_ERR "md/raid0:%s: Raid0 cannot takover Raid10 with odd disk number.\n",
  554. mdname(mddev));
  555. return ERR_PTR(-EINVAL);
  556. }
  557. if (mddev->degraded != (mddev->raid_disks>>1)) {
  558. printk(KERN_ERR "md/raid0:%s: All mirrors must be already degraded!\n",
  559. mdname(mddev));
  560. return ERR_PTR(-EINVAL);
  561. }
  562. /* Set new parameters */
  563. mddev->new_level = 0;
  564. mddev->new_chunk_sectors = mddev->chunk_sectors;
  565. mddev->delta_disks = - mddev->raid_disks / 2;
  566. mddev->raid_disks += mddev->delta_disks;
  567. mddev->degraded = 0;
  568. /* make sure it will be not marked as dirty */
  569. mddev->recovery_cp = MaxSector;
  570. create_strip_zones(mddev, &priv_conf);
  571. priv_conf->scale_raid_disks = 2;
  572. return priv_conf;
  573. }
  574. static void *raid0_takeover(mddev_t *mddev)
  575. {
  576. /* raid0 can take over:
  577. * raid5 - providing it is Raid4 layout and one disk is faulty
  578. * raid10 - assuming we have all necessary active disks
  579. */
  580. if (mddev->level == 5) {
  581. if (mddev->layout == ALGORITHM_PARITY_N)
  582. return raid0_takeover_raid5(mddev);
  583. printk(KERN_ERR "md/raid0:%s: Raid can only takeover Raid5 with layout: %d\n",
  584. mdname(mddev), ALGORITHM_PARITY_N);
  585. }
  586. if (mddev->level == 10)
  587. return raid0_takeover_raid10(mddev);
  588. return ERR_PTR(-EINVAL);
  589. }
  590. static void raid0_quiesce(mddev_t *mddev, int state)
  591. {
  592. }
  593. static struct mdk_personality raid0_personality=
  594. {
  595. .name = "raid0",
  596. .level = 0,
  597. .owner = THIS_MODULE,
  598. .make_request = raid0_make_request,
  599. .run = raid0_run,
  600. .stop = raid0_stop,
  601. .status = raid0_status,
  602. .size = raid0_size,
  603. .takeover = raid0_takeover,
  604. .quiesce = raid0_quiesce,
  605. };
  606. static int __init raid0_init (void)
  607. {
  608. return register_md_personality (&raid0_personality);
  609. }
  610. static void raid0_exit (void)
  611. {
  612. unregister_md_personality (&raid0_personality);
  613. }
  614. module_init(raid0_init);
  615. module_exit(raid0_exit);
  616. MODULE_LICENSE("GPL");
  617. MODULE_DESCRIPTION("RAID0 (striping) personality for MD");
  618. MODULE_ALIAS("md-personality-2"); /* RAID0 */
  619. MODULE_ALIAS("md-raid0");
  620. MODULE_ALIAS("md-level-0");