raid0.c 19 KB

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