raid0.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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/module.h>
  19. #include <linux/slab.h>
  20. #include "md.h"
  21. #include "raid0.h"
  22. #include "raid5.h"
  23. static int raid0_congested(void *data, int bits)
  24. {
  25. struct mddev *mddev = data;
  26. struct r0conf *conf = mddev->private;
  27. struct md_rdev **devlist = conf->devlist;
  28. int raid_disks = conf->strip_zone[0].nb_dev;
  29. int i, ret = 0;
  30. if (mddev_congested(mddev, bits))
  31. return 1;
  32. for (i = 0; i < raid_disks && !ret ; i++) {
  33. struct request_queue *q = bdev_get_queue(devlist[i]->bdev);
  34. ret |= bdi_congested(&q->backing_dev_info, bits);
  35. }
  36. return ret;
  37. }
  38. /*
  39. * inform the user of the raid configuration
  40. */
  41. static void dump_zones(struct mddev *mddev)
  42. {
  43. int j, k;
  44. sector_t zone_size = 0;
  45. sector_t zone_start = 0;
  46. char b[BDEVNAME_SIZE];
  47. struct r0conf *conf = mddev->private;
  48. int raid_disks = conf->strip_zone[0].nb_dev;
  49. printk(KERN_INFO "md: RAID0 configuration for %s - %d zone%s\n",
  50. mdname(mddev),
  51. conf->nr_strip_zones, conf->nr_strip_zones==1?"":"s");
  52. for (j = 0; j < conf->nr_strip_zones; j++) {
  53. printk(KERN_INFO "md: zone%d=[", j);
  54. for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
  55. printk(KERN_CONT "%s%s", k?"/":"",
  56. bdevname(conf->devlist[j*raid_disks
  57. + k]->bdev, b));
  58. printk(KERN_CONT "]\n");
  59. zone_size = conf->strip_zone[j].zone_end - zone_start;
  60. printk(KERN_INFO " zone-offset=%10lluKB, "
  61. "device-offset=%10lluKB, size=%10lluKB\n",
  62. (unsigned long long)zone_start>>1,
  63. (unsigned long long)conf->strip_zone[j].dev_start>>1,
  64. (unsigned long long)zone_size>>1);
  65. zone_start = conf->strip_zone[j].zone_end;
  66. }
  67. printk(KERN_INFO "\n");
  68. }
  69. static int create_strip_zones(struct mddev *mddev, struct r0conf **private_conf)
  70. {
  71. int i, c, err;
  72. sector_t curr_zone_end, sectors;
  73. struct md_rdev *smallest, *rdev1, *rdev2, *rdev, **dev;
  74. struct strip_zone *zone;
  75. int cnt;
  76. char b[BDEVNAME_SIZE];
  77. char b2[BDEVNAME_SIZE];
  78. struct r0conf *conf = kzalloc(sizeof(*conf), GFP_KERNEL);
  79. if (!conf)
  80. return -ENOMEM;
  81. list_for_each_entry(rdev1, &mddev->disks, same_set) {
  82. pr_debug("md/raid0:%s: looking at %s\n",
  83. mdname(mddev),
  84. bdevname(rdev1->bdev, b));
  85. c = 0;
  86. /* round size to chunk_size */
  87. sectors = rdev1->sectors;
  88. sector_div(sectors, mddev->chunk_sectors);
  89. rdev1->sectors = sectors * mddev->chunk_sectors;
  90. list_for_each_entry(rdev2, &mddev->disks, same_set) {
  91. pr_debug("md/raid0:%s: comparing %s(%llu)"
  92. " with %s(%llu)\n",
  93. mdname(mddev),
  94. bdevname(rdev1->bdev,b),
  95. (unsigned long long)rdev1->sectors,
  96. bdevname(rdev2->bdev,b2),
  97. (unsigned long long)rdev2->sectors);
  98. if (rdev2 == rdev1) {
  99. pr_debug("md/raid0:%s: END\n",
  100. mdname(mddev));
  101. break;
  102. }
  103. if (rdev2->sectors == rdev1->sectors) {
  104. /*
  105. * Not unique, don't count it as a new
  106. * group
  107. */
  108. pr_debug("md/raid0:%s: EQUAL\n",
  109. mdname(mddev));
  110. c = 1;
  111. break;
  112. }
  113. pr_debug("md/raid0:%s: NOT EQUAL\n",
  114. mdname(mddev));
  115. }
  116. if (!c) {
  117. pr_debug("md/raid0:%s: ==> UNIQUE\n",
  118. mdname(mddev));
  119. conf->nr_strip_zones++;
  120. pr_debug("md/raid0:%s: %d zones\n",
  121. mdname(mddev), conf->nr_strip_zones);
  122. }
  123. }
  124. pr_debug("md/raid0:%s: FINAL %d zones\n",
  125. mdname(mddev), conf->nr_strip_zones);
  126. err = -ENOMEM;
  127. conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
  128. conf->nr_strip_zones, GFP_KERNEL);
  129. if (!conf->strip_zone)
  130. goto abort;
  131. conf->devlist = kzalloc(sizeof(struct md_rdev*)*
  132. conf->nr_strip_zones*mddev->raid_disks,
  133. GFP_KERNEL);
  134. if (!conf->devlist)
  135. goto abort;
  136. /* The first zone must contain all devices, so here we check that
  137. * there is a proper alignment of slots to devices and find them all
  138. */
  139. zone = &conf->strip_zone[0];
  140. cnt = 0;
  141. smallest = NULL;
  142. dev = conf->devlist;
  143. err = -EINVAL;
  144. list_for_each_entry(rdev1, &mddev->disks, same_set) {
  145. int j = rdev1->raid_disk;
  146. if (mddev->level == 10) {
  147. /* taking over a raid10-n2 array */
  148. j /= 2;
  149. rdev1->new_raid_disk = j;
  150. }
  151. if (mddev->level == 1) {
  152. /* taiking over a raid1 array-
  153. * we have only one active disk
  154. */
  155. j = 0;
  156. rdev1->new_raid_disk = j;
  157. }
  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. pr_debug("md/raid0:%s: zone %d\n", mdname(mddev), i);
  199. zone->dev_start = smallest->sectors;
  200. smallest = NULL;
  201. c = 0;
  202. for (j=0; j<cnt; j++) {
  203. rdev = conf->devlist[j];
  204. if (rdev->sectors <= zone->dev_start) {
  205. pr_debug("md/raid0:%s: checking %s ... nope\n",
  206. mdname(mddev),
  207. bdevname(rdev->bdev, b));
  208. continue;
  209. }
  210. pr_debug("md/raid0:%s: checking %s ..."
  211. " contained as device %d\n",
  212. mdname(mddev),
  213. bdevname(rdev->bdev, b), c);
  214. dev[c] = rdev;
  215. c++;
  216. if (!smallest || rdev->sectors < smallest->sectors) {
  217. smallest = rdev;
  218. pr_debug("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. pr_debug("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. pr_debug("md/raid0:%s: current zone start: %llu\n",
  231. mdname(mddev),
  232. (unsigned long long)smallest->sectors);
  233. }
  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. pr_debug("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. struct mddev *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(struct mddev *mddev, sector_t sectors, int raid_disks)
  289. {
  290. sector_t array_sectors = 0;
  291. struct md_rdev *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(struct mddev *mddev)
  299. {
  300. struct r0conf *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. /* if private is not null, we are here after takeover */
  311. if (mddev->private == NULL) {
  312. ret = create_strip_zones(mddev, &conf);
  313. if (ret < 0)
  314. return ret;
  315. mddev->private = conf;
  316. }
  317. conf = mddev->private;
  318. /* calculate array device size */
  319. md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
  320. printk(KERN_INFO "md/raid0:%s: md_size is %llu sectors.\n",
  321. mdname(mddev),
  322. (unsigned long long)mddev->array_sectors);
  323. /* calculate the max read-ahead size.
  324. * For read-ahead of large files to be effective, we need to
  325. * readahead at least twice a whole stripe. i.e. number of devices
  326. * multiplied by chunk size times 2.
  327. * If an individual device has an ra_pages greater than the
  328. * chunk size, then we will not drive that device as hard as it
  329. * wants. We consider this a configuration error: a larger
  330. * chunksize should be used in that case.
  331. */
  332. {
  333. int stripe = mddev->raid_disks *
  334. (mddev->chunk_sectors << 9) / PAGE_SIZE;
  335. if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
  336. mddev->queue->backing_dev_info.ra_pages = 2* stripe;
  337. }
  338. blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
  339. dump_zones(mddev);
  340. return md_integrity_register(mddev);
  341. }
  342. static int raid0_stop(struct mddev *mddev)
  343. {
  344. struct r0conf *conf = mddev->private;
  345. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  346. kfree(conf->strip_zone);
  347. kfree(conf->devlist);
  348. kfree(conf);
  349. mddev->private = NULL;
  350. return 0;
  351. }
  352. /* Find the zone which holds a particular offset
  353. * Update *sectorp to be an offset in that zone
  354. */
  355. static struct strip_zone *find_zone(struct r0conf *conf,
  356. sector_t *sectorp)
  357. {
  358. int i;
  359. struct strip_zone *z = conf->strip_zone;
  360. sector_t sector = *sectorp;
  361. for (i = 0; i < conf->nr_strip_zones; i++)
  362. if (sector < z[i].zone_end) {
  363. if (i)
  364. *sectorp = sector - z[i-1].zone_end;
  365. return z + i;
  366. }
  367. BUG();
  368. }
  369. /*
  370. * remaps the bio to the target device. we separate two flows.
  371. * power 2 flow and a general flow for the sake of perfromance
  372. */
  373. static struct md_rdev *map_sector(struct mddev *mddev, struct strip_zone *zone,
  374. sector_t sector, sector_t *sector_offset)
  375. {
  376. unsigned int sect_in_chunk;
  377. sector_t chunk;
  378. struct r0conf *conf = mddev->private;
  379. int raid_disks = conf->strip_zone[0].nb_dev;
  380. unsigned int chunk_sects = mddev->chunk_sectors;
  381. if (is_power_of_2(chunk_sects)) {
  382. int chunksect_bits = ffz(~chunk_sects);
  383. /* find the sector offset inside the chunk */
  384. sect_in_chunk = sector & (chunk_sects - 1);
  385. sector >>= chunksect_bits;
  386. /* chunk in zone */
  387. chunk = *sector_offset;
  388. /* quotient is the chunk in real device*/
  389. sector_div(chunk, zone->nb_dev << chunksect_bits);
  390. } else{
  391. sect_in_chunk = sector_div(sector, chunk_sects);
  392. chunk = *sector_offset;
  393. sector_div(chunk, chunk_sects * zone->nb_dev);
  394. }
  395. /*
  396. * position the bio over the real device
  397. * real sector = chunk in device + starting of zone
  398. * + the position in the chunk
  399. */
  400. *sector_offset = (chunk * chunk_sects) + sect_in_chunk;
  401. return conf->devlist[(zone - conf->strip_zone)*raid_disks
  402. + sector_div(sector, zone->nb_dev)];
  403. }
  404. /*
  405. * Is io distribute over 1 or more chunks ?
  406. */
  407. static inline int is_io_in_chunk_boundary(struct mddev *mddev,
  408. unsigned int chunk_sects, struct bio *bio)
  409. {
  410. if (likely(is_power_of_2(chunk_sects))) {
  411. return chunk_sects >= ((bio->bi_sector & (chunk_sects-1))
  412. + (bio->bi_size >> 9));
  413. } else{
  414. sector_t sector = bio->bi_sector;
  415. return chunk_sects >= (sector_div(sector, chunk_sects)
  416. + (bio->bi_size >> 9));
  417. }
  418. }
  419. static void raid0_make_request(struct mddev *mddev, struct bio *bio)
  420. {
  421. unsigned int chunk_sects;
  422. sector_t sector_offset;
  423. struct strip_zone *zone;
  424. struct md_rdev *tmp_dev;
  425. if (unlikely(bio->bi_rw & REQ_FLUSH)) {
  426. md_flush_request(mddev, bio);
  427. return;
  428. }
  429. chunk_sects = mddev->chunk_sectors;
  430. if (unlikely(!is_io_in_chunk_boundary(mddev, chunk_sects, bio))) {
  431. sector_t sector = bio->bi_sector;
  432. struct bio_pair *bp;
  433. /* Sanity check -- queue functions should prevent this happening */
  434. if (bio->bi_vcnt != 1 ||
  435. bio->bi_idx != 0)
  436. goto bad_map;
  437. /* This is a one page bio that upper layers
  438. * refuse to split for us, so we need to split it.
  439. */
  440. if (likely(is_power_of_2(chunk_sects)))
  441. bp = bio_split(bio, chunk_sects - (sector &
  442. (chunk_sects-1)));
  443. else
  444. bp = bio_split(bio, chunk_sects -
  445. sector_div(sector, chunk_sects));
  446. raid0_make_request(mddev, &bp->bio1);
  447. raid0_make_request(mddev, &bp->bio2);
  448. bio_pair_release(bp);
  449. return;
  450. }
  451. sector_offset = bio->bi_sector;
  452. zone = find_zone(mddev->private, &sector_offset);
  453. tmp_dev = map_sector(mddev, zone, bio->bi_sector,
  454. &sector_offset);
  455. bio->bi_bdev = tmp_dev->bdev;
  456. bio->bi_sector = sector_offset + zone->dev_start +
  457. tmp_dev->data_offset;
  458. generic_make_request(bio);
  459. return;
  460. bad_map:
  461. printk("md/raid0:%s: make_request bug: can't convert block across chunks"
  462. " or bigger than %dk %llu %d\n",
  463. mdname(mddev), chunk_sects / 2,
  464. (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
  465. bio_io_error(bio);
  466. return;
  467. }
  468. static void raid0_status(struct seq_file *seq, struct mddev *mddev)
  469. {
  470. seq_printf(seq, " %dk chunks", mddev->chunk_sectors / 2);
  471. return;
  472. }
  473. static void *raid0_takeover_raid45(struct mddev *mddev)
  474. {
  475. struct md_rdev *rdev;
  476. struct r0conf *priv_conf;
  477. if (mddev->degraded != 1) {
  478. printk(KERN_ERR "md/raid0:%s: raid5 must be degraded! Degraded disks: %d\n",
  479. mdname(mddev),
  480. mddev->degraded);
  481. return ERR_PTR(-EINVAL);
  482. }
  483. list_for_each_entry(rdev, &mddev->disks, same_set) {
  484. /* check slot number for a disk */
  485. if (rdev->raid_disk == mddev->raid_disks-1) {
  486. printk(KERN_ERR "md/raid0:%s: raid5 must have missing parity disk!\n",
  487. mdname(mddev));
  488. return ERR_PTR(-EINVAL);
  489. }
  490. }
  491. /* Set new parameters */
  492. mddev->new_level = 0;
  493. mddev->new_layout = 0;
  494. mddev->new_chunk_sectors = mddev->chunk_sectors;
  495. mddev->raid_disks--;
  496. mddev->delta_disks = -1;
  497. /* make sure it will be not marked as dirty */
  498. mddev->recovery_cp = MaxSector;
  499. create_strip_zones(mddev, &priv_conf);
  500. return priv_conf;
  501. }
  502. static void *raid0_takeover_raid10(struct mddev *mddev)
  503. {
  504. struct r0conf *priv_conf;
  505. /* Check layout:
  506. * - far_copies must be 1
  507. * - near_copies must be 2
  508. * - disks number must be even
  509. * - all mirrors must be already degraded
  510. */
  511. if (mddev->layout != ((1 << 8) + 2)) {
  512. printk(KERN_ERR "md/raid0:%s:: Raid0 cannot takover layout: 0x%x\n",
  513. mdname(mddev),
  514. mddev->layout);
  515. return ERR_PTR(-EINVAL);
  516. }
  517. if (mddev->raid_disks & 1) {
  518. printk(KERN_ERR "md/raid0:%s: Raid0 cannot takover Raid10 with odd disk number.\n",
  519. mdname(mddev));
  520. return ERR_PTR(-EINVAL);
  521. }
  522. if (mddev->degraded != (mddev->raid_disks>>1)) {
  523. printk(KERN_ERR "md/raid0:%s: All mirrors must be already degraded!\n",
  524. mdname(mddev));
  525. return ERR_PTR(-EINVAL);
  526. }
  527. /* Set new parameters */
  528. mddev->new_level = 0;
  529. mddev->new_layout = 0;
  530. mddev->new_chunk_sectors = mddev->chunk_sectors;
  531. mddev->delta_disks = - mddev->raid_disks / 2;
  532. mddev->raid_disks += mddev->delta_disks;
  533. mddev->degraded = 0;
  534. /* make sure it will be not marked as dirty */
  535. mddev->recovery_cp = MaxSector;
  536. create_strip_zones(mddev, &priv_conf);
  537. return priv_conf;
  538. }
  539. static void *raid0_takeover_raid1(struct mddev *mddev)
  540. {
  541. struct r0conf *priv_conf;
  542. /* Check layout:
  543. * - (N - 1) mirror drives must be already faulty
  544. */
  545. if ((mddev->raid_disks - 1) != mddev->degraded) {
  546. printk(KERN_ERR "md/raid0:%s: (N - 1) mirrors drives must be already faulty!\n",
  547. mdname(mddev));
  548. return ERR_PTR(-EINVAL);
  549. }
  550. /* Set new parameters */
  551. mddev->new_level = 0;
  552. mddev->new_layout = 0;
  553. mddev->new_chunk_sectors = 128; /* by default set chunk size to 64k */
  554. mddev->delta_disks = 1 - mddev->raid_disks;
  555. mddev->raid_disks = 1;
  556. /* make sure it will be not marked as dirty */
  557. mddev->recovery_cp = MaxSector;
  558. create_strip_zones(mddev, &priv_conf);
  559. return priv_conf;
  560. }
  561. static void *raid0_takeover(struct mddev *mddev)
  562. {
  563. /* raid0 can take over:
  564. * raid4 - if all data disks are active.
  565. * raid5 - providing it is Raid4 layout and one disk is faulty
  566. * raid10 - assuming we have all necessary active disks
  567. * raid1 - with (N -1) mirror drives faulty
  568. */
  569. if (mddev->level == 4)
  570. return raid0_takeover_raid45(mddev);
  571. if (mddev->level == 5) {
  572. if (mddev->layout == ALGORITHM_PARITY_N)
  573. return raid0_takeover_raid45(mddev);
  574. printk(KERN_ERR "md/raid0:%s: Raid can only takeover Raid5 with layout: %d\n",
  575. mdname(mddev), ALGORITHM_PARITY_N);
  576. }
  577. if (mddev->level == 10)
  578. return raid0_takeover_raid10(mddev);
  579. if (mddev->level == 1)
  580. return raid0_takeover_raid1(mddev);
  581. printk(KERN_ERR "Takeover from raid%i to raid0 not supported\n",
  582. mddev->level);
  583. return ERR_PTR(-EINVAL);
  584. }
  585. static void raid0_quiesce(struct mddev *mddev, int state)
  586. {
  587. }
  588. static struct md_personality raid0_personality=
  589. {
  590. .name = "raid0",
  591. .level = 0,
  592. .owner = THIS_MODULE,
  593. .make_request = raid0_make_request,
  594. .run = raid0_run,
  595. .stop = raid0_stop,
  596. .status = raid0_status,
  597. .size = raid0_size,
  598. .takeover = raid0_takeover,
  599. .quiesce = raid0_quiesce,
  600. };
  601. static int __init raid0_init (void)
  602. {
  603. return register_md_personality (&raid0_personality);
  604. }
  605. static void raid0_exit (void)
  606. {
  607. unregister_md_personality (&raid0_personality);
  608. }
  609. module_init(raid0_init);
  610. module_exit(raid0_exit);
  611. MODULE_LICENSE("GPL");
  612. MODULE_DESCRIPTION("RAID0 (striping) personality for MD");
  613. MODULE_ALIAS("md-personality-2"); /* RAID0 */
  614. MODULE_ALIAS("md-raid0");
  615. MODULE_ALIAS("md-level-0");