raid0.c 20 KB

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