raid0.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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(struct mddev *mddev, int bits)
  24. {
  25. struct r0conf *conf = mddev->private;
  26. struct md_rdev **devlist = conf->devlist;
  27. int raid_disks = conf->strip_zone[0].nb_dev;
  28. int i, ret = 0;
  29. for (i = 0; i < raid_disks && !ret ; i++) {
  30. struct request_queue *q = bdev_get_queue(devlist[i]->bdev);
  31. ret |= bdi_congested(&q->backing_dev_info, bits);
  32. }
  33. return ret;
  34. }
  35. /*
  36. * inform the user of the raid configuration
  37. */
  38. static void dump_zones(struct mddev *mddev)
  39. {
  40. int j, k;
  41. sector_t zone_size = 0;
  42. sector_t zone_start = 0;
  43. char b[BDEVNAME_SIZE];
  44. struct r0conf *conf = mddev->private;
  45. int raid_disks = conf->strip_zone[0].nb_dev;
  46. printk(KERN_INFO "md: RAID0 configuration for %s - %d zone%s\n",
  47. mdname(mddev),
  48. conf->nr_strip_zones, conf->nr_strip_zones==1?"":"s");
  49. for (j = 0; j < conf->nr_strip_zones; j++) {
  50. printk(KERN_INFO "md: zone%d=[", j);
  51. for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
  52. printk(KERN_CONT "%s%s", k?"/":"",
  53. bdevname(conf->devlist[j*raid_disks
  54. + k]->bdev, b));
  55. printk(KERN_CONT "]\n");
  56. zone_size = conf->strip_zone[j].zone_end - zone_start;
  57. printk(KERN_INFO " zone-offset=%10lluKB, "
  58. "device-offset=%10lluKB, size=%10lluKB\n",
  59. (unsigned long long)zone_start>>1,
  60. (unsigned long long)conf->strip_zone[j].dev_start>>1,
  61. (unsigned long long)zone_size>>1);
  62. zone_start = conf->strip_zone[j].zone_end;
  63. }
  64. printk(KERN_INFO "\n");
  65. }
  66. static int create_strip_zones(struct mddev *mddev, struct r0conf **private_conf)
  67. {
  68. int i, c, err;
  69. sector_t curr_zone_end, sectors;
  70. struct md_rdev *smallest, *rdev1, *rdev2, *rdev, **dev;
  71. struct strip_zone *zone;
  72. int cnt;
  73. char b[BDEVNAME_SIZE];
  74. char b2[BDEVNAME_SIZE];
  75. struct r0conf *conf = kzalloc(sizeof(*conf), GFP_KERNEL);
  76. bool discard_supported = false;
  77. if (!conf)
  78. return -ENOMEM;
  79. rdev_for_each(rdev1, mddev) {
  80. pr_debug("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. rdev_for_each(rdev2, mddev) {
  89. pr_debug("md/raid0:%s: comparing %s(%llu)"
  90. " with %s(%llu)\n",
  91. mdname(mddev),
  92. bdevname(rdev1->bdev,b),
  93. (unsigned long long)rdev1->sectors,
  94. bdevname(rdev2->bdev,b2),
  95. (unsigned long long)rdev2->sectors);
  96. if (rdev2 == rdev1) {
  97. pr_debug("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. pr_debug("md/raid0:%s: EQUAL\n",
  107. mdname(mddev));
  108. c = 1;
  109. break;
  110. }
  111. pr_debug("md/raid0:%s: NOT EQUAL\n",
  112. mdname(mddev));
  113. }
  114. if (!c) {
  115. pr_debug("md/raid0:%s: ==> UNIQUE\n",
  116. mdname(mddev));
  117. conf->nr_strip_zones++;
  118. pr_debug("md/raid0:%s: %d zones\n",
  119. mdname(mddev), conf->nr_strip_zones);
  120. }
  121. }
  122. pr_debug("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(struct md_rdev*)*
  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. rdev_for_each(rdev1, mddev) {
  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) {
  157. printk(KERN_ERR
  158. "md/raid0:%s: remove inactive devices before converting to RAID0\n",
  159. mdname(mddev));
  160. goto abort;
  161. }
  162. if (j >= mddev->raid_disks) {
  163. printk(KERN_ERR "md/raid0:%s: bad disk number %d - "
  164. "aborting!\n", mdname(mddev), j);
  165. goto abort;
  166. }
  167. if (dev[j]) {
  168. printk(KERN_ERR "md/raid0:%s: multiple devices for %d - "
  169. "aborting!\n", mdname(mddev), j);
  170. goto abort;
  171. }
  172. dev[j] = rdev1;
  173. if (mddev->queue)
  174. disk_stack_limits(mddev->gendisk, rdev1->bdev,
  175. rdev1->data_offset << 9);
  176. if (rdev1->bdev->bd_disk->queue->merge_bvec_fn)
  177. conf->has_merge_bvec = 1;
  178. if (!smallest || (rdev1->sectors < smallest->sectors))
  179. smallest = rdev1;
  180. cnt++;
  181. if (blk_queue_discard(bdev_get_queue(rdev1->bdev)))
  182. discard_supported = true;
  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. /*
  235. * now since we have the hard sector sizes, we can make sure
  236. * chunk size is a multiple of that sector size
  237. */
  238. if ((mddev->chunk_sectors << 9) % queue_logical_block_size(mddev->queue)) {
  239. printk(KERN_ERR "md/raid0:%s: chunk_size of %d not valid\n",
  240. mdname(mddev),
  241. mddev->chunk_sectors << 9);
  242. goto abort;
  243. }
  244. if (mddev->queue) {
  245. blk_queue_io_min(mddev->queue, mddev->chunk_sectors << 9);
  246. blk_queue_io_opt(mddev->queue,
  247. (mddev->chunk_sectors << 9) * mddev->raid_disks);
  248. if (!discard_supported)
  249. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
  250. else
  251. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
  252. }
  253. pr_debug("md/raid0:%s: done.\n", mdname(mddev));
  254. *private_conf = conf;
  255. return 0;
  256. abort:
  257. kfree(conf->strip_zone);
  258. kfree(conf->devlist);
  259. kfree(conf);
  260. *private_conf = ERR_PTR(err);
  261. return err;
  262. }
  263. /* Find the zone which holds a particular offset
  264. * Update *sectorp to be an offset in that zone
  265. */
  266. static struct strip_zone *find_zone(struct r0conf *conf,
  267. sector_t *sectorp)
  268. {
  269. int i;
  270. struct strip_zone *z = conf->strip_zone;
  271. sector_t sector = *sectorp;
  272. for (i = 0; i < conf->nr_strip_zones; i++)
  273. if (sector < z[i].zone_end) {
  274. if (i)
  275. *sectorp = sector - z[i-1].zone_end;
  276. return z + i;
  277. }
  278. BUG();
  279. }
  280. /*
  281. * remaps the bio to the target device. we separate two flows.
  282. * power 2 flow and a general flow for the sake of performance
  283. */
  284. static struct md_rdev *map_sector(struct mddev *mddev, struct strip_zone *zone,
  285. sector_t sector, sector_t *sector_offset)
  286. {
  287. unsigned int sect_in_chunk;
  288. sector_t chunk;
  289. struct r0conf *conf = mddev->private;
  290. int raid_disks = conf->strip_zone[0].nb_dev;
  291. unsigned int chunk_sects = mddev->chunk_sectors;
  292. if (is_power_of_2(chunk_sects)) {
  293. int chunksect_bits = ffz(~chunk_sects);
  294. /* find the sector offset inside the chunk */
  295. sect_in_chunk = sector & (chunk_sects - 1);
  296. sector >>= chunksect_bits;
  297. /* chunk in zone */
  298. chunk = *sector_offset;
  299. /* quotient is the chunk in real device*/
  300. sector_div(chunk, zone->nb_dev << chunksect_bits);
  301. } else{
  302. sect_in_chunk = sector_div(sector, chunk_sects);
  303. chunk = *sector_offset;
  304. sector_div(chunk, chunk_sects * zone->nb_dev);
  305. }
  306. /*
  307. * position the bio over the real device
  308. * real sector = chunk in device + starting of zone
  309. * + the position in the chunk
  310. */
  311. *sector_offset = (chunk * chunk_sects) + sect_in_chunk;
  312. return conf->devlist[(zone - conf->strip_zone)*raid_disks
  313. + sector_div(sector, zone->nb_dev)];
  314. }
  315. /**
  316. * raid0_mergeable_bvec -- tell bio layer if two requests can be merged
  317. * @mddev: the md device
  318. * @bvm: properties of new bio
  319. * @biovec: the request that could be merged to it.
  320. *
  321. * Return amount of bytes we can accept at this offset
  322. */
  323. static int raid0_mergeable_bvec(struct mddev *mddev,
  324. struct bvec_merge_data *bvm,
  325. struct bio_vec *biovec)
  326. {
  327. struct r0conf *conf = mddev->private;
  328. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  329. sector_t sector_offset = sector;
  330. int max;
  331. unsigned int chunk_sectors = mddev->chunk_sectors;
  332. unsigned int bio_sectors = bvm->bi_size >> 9;
  333. struct strip_zone *zone;
  334. struct md_rdev *rdev;
  335. struct request_queue *subq;
  336. if (is_power_of_2(chunk_sectors))
  337. max = (chunk_sectors - ((sector & (chunk_sectors-1))
  338. + bio_sectors)) << 9;
  339. else
  340. max = (chunk_sectors - (sector_div(sector, chunk_sectors)
  341. + bio_sectors)) << 9;
  342. if (max < 0)
  343. max = 0; /* bio_add cannot handle a negative return */
  344. if (max <= biovec->bv_len && bio_sectors == 0)
  345. return biovec->bv_len;
  346. if (max < biovec->bv_len)
  347. /* too small already, no need to check further */
  348. return max;
  349. if (!conf->has_merge_bvec)
  350. return max;
  351. /* May need to check subordinate device */
  352. sector = sector_offset;
  353. zone = find_zone(mddev->private, &sector_offset);
  354. rdev = map_sector(mddev, zone, sector, &sector_offset);
  355. subq = bdev_get_queue(rdev->bdev);
  356. if (subq->merge_bvec_fn) {
  357. bvm->bi_bdev = rdev->bdev;
  358. bvm->bi_sector = sector_offset + zone->dev_start +
  359. rdev->data_offset;
  360. return min(max, subq->merge_bvec_fn(subq, bvm, biovec));
  361. } else
  362. return max;
  363. }
  364. static sector_t raid0_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  365. {
  366. sector_t array_sectors = 0;
  367. struct md_rdev *rdev;
  368. WARN_ONCE(sectors || raid_disks,
  369. "%s does not support generic reshape\n", __func__);
  370. rdev_for_each(rdev, mddev)
  371. array_sectors += (rdev->sectors &
  372. ~(sector_t)(mddev->chunk_sectors-1));
  373. return array_sectors;
  374. }
  375. static void raid0_free(struct mddev *mddev, void *priv);
  376. static int raid0_run(struct mddev *mddev)
  377. {
  378. struct r0conf *conf;
  379. int ret;
  380. if (mddev->chunk_sectors == 0) {
  381. printk(KERN_ERR "md/raid0:%s: chunk size must be set.\n",
  382. mdname(mddev));
  383. return -EINVAL;
  384. }
  385. if (md_check_no_bitmap(mddev))
  386. return -EINVAL;
  387. if (mddev->queue) {
  388. blk_queue_max_hw_sectors(mddev->queue, mddev->chunk_sectors);
  389. blk_queue_max_write_same_sectors(mddev->queue, mddev->chunk_sectors);
  390. blk_queue_max_discard_sectors(mddev->queue, mddev->chunk_sectors);
  391. }
  392. /* if private is not null, we are here after takeover */
  393. if (mddev->private == NULL) {
  394. ret = create_strip_zones(mddev, &conf);
  395. if (ret < 0)
  396. return ret;
  397. mddev->private = conf;
  398. }
  399. conf = mddev->private;
  400. /* calculate array device size */
  401. md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
  402. printk(KERN_INFO "md/raid0:%s: md_size is %llu sectors.\n",
  403. mdname(mddev),
  404. (unsigned long long)mddev->array_sectors);
  405. if (mddev->queue) {
  406. /* calculate the max read-ahead size.
  407. * For read-ahead of large files to be effective, we need to
  408. * readahead at least twice a whole stripe. i.e. number of devices
  409. * multiplied by chunk size times 2.
  410. * If an individual device has an ra_pages greater than the
  411. * chunk size, then we will not drive that device as hard as it
  412. * wants. We consider this a configuration error: a larger
  413. * chunksize should be used in that case.
  414. */
  415. int stripe = mddev->raid_disks *
  416. (mddev->chunk_sectors << 9) / PAGE_SIZE;
  417. if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
  418. mddev->queue->backing_dev_info.ra_pages = 2* stripe;
  419. }
  420. dump_zones(mddev);
  421. ret = md_integrity_register(mddev);
  422. return ret;
  423. }
  424. static void raid0_free(struct mddev *mddev, void *priv)
  425. {
  426. struct r0conf *conf = priv;
  427. kfree(conf->strip_zone);
  428. kfree(conf->devlist);
  429. kfree(conf);
  430. }
  431. /*
  432. * Is io distribute over 1 or more chunks ?
  433. */
  434. static inline int is_io_in_chunk_boundary(struct mddev *mddev,
  435. unsigned int chunk_sects, struct bio *bio)
  436. {
  437. if (likely(is_power_of_2(chunk_sects))) {
  438. return chunk_sects >=
  439. ((bio->bi_iter.bi_sector & (chunk_sects-1))
  440. + bio_sectors(bio));
  441. } else{
  442. sector_t sector = bio->bi_iter.bi_sector;
  443. return chunk_sects >= (sector_div(sector, chunk_sects)
  444. + bio_sectors(bio));
  445. }
  446. }
  447. static void raid0_make_request(struct mddev *mddev, struct bio *bio)
  448. {
  449. struct strip_zone *zone;
  450. struct md_rdev *tmp_dev;
  451. struct bio *split;
  452. if (unlikely(bio->bi_rw & REQ_FLUSH)) {
  453. md_flush_request(mddev, bio);
  454. return;
  455. }
  456. do {
  457. sector_t sector = bio->bi_iter.bi_sector;
  458. unsigned chunk_sects = mddev->chunk_sectors;
  459. unsigned sectors = chunk_sects -
  460. (likely(is_power_of_2(chunk_sects))
  461. ? (sector & (chunk_sects-1))
  462. : sector_div(sector, chunk_sects));
  463. /* Restore due to sector_div */
  464. sector = bio->bi_iter.bi_sector;
  465. if (sectors < bio_sectors(bio)) {
  466. split = bio_split(bio, sectors, GFP_NOIO, fs_bio_set);
  467. bio_chain(split, bio);
  468. } else {
  469. split = bio;
  470. }
  471. zone = find_zone(mddev->private, &sector);
  472. tmp_dev = map_sector(mddev, zone, sector, &sector);
  473. split->bi_bdev = tmp_dev->bdev;
  474. split->bi_iter.bi_sector = sector + zone->dev_start +
  475. tmp_dev->data_offset;
  476. if (unlikely((split->bi_rw & REQ_DISCARD) &&
  477. !blk_queue_discard(bdev_get_queue(split->bi_bdev)))) {
  478. /* Just ignore it */
  479. bio_endio(split, 0);
  480. } else
  481. generic_make_request(split);
  482. } while (split != bio);
  483. }
  484. static void raid0_status(struct seq_file *seq, struct mddev *mddev)
  485. {
  486. seq_printf(seq, " %dk chunks", mddev->chunk_sectors / 2);
  487. return;
  488. }
  489. static void *raid0_takeover_raid45(struct mddev *mddev)
  490. {
  491. struct md_rdev *rdev;
  492. struct r0conf *priv_conf;
  493. if (mddev->degraded != 1) {
  494. printk(KERN_ERR "md/raid0:%s: raid5 must be degraded! Degraded disks: %d\n",
  495. mdname(mddev),
  496. mddev->degraded);
  497. return ERR_PTR(-EINVAL);
  498. }
  499. rdev_for_each(rdev, mddev) {
  500. /* check slot number for a disk */
  501. if (rdev->raid_disk == mddev->raid_disks-1) {
  502. printk(KERN_ERR "md/raid0:%s: raid5 must have missing parity disk!\n",
  503. mdname(mddev));
  504. return ERR_PTR(-EINVAL);
  505. }
  506. rdev->sectors = mddev->dev_sectors;
  507. }
  508. /* Set new parameters */
  509. mddev->new_level = 0;
  510. mddev->new_layout = 0;
  511. mddev->new_chunk_sectors = mddev->chunk_sectors;
  512. mddev->raid_disks--;
  513. mddev->delta_disks = -1;
  514. /* make sure it will be not marked as dirty */
  515. mddev->recovery_cp = MaxSector;
  516. create_strip_zones(mddev, &priv_conf);
  517. return priv_conf;
  518. }
  519. static void *raid0_takeover_raid10(struct mddev *mddev)
  520. {
  521. struct r0conf *priv_conf;
  522. /* Check layout:
  523. * - far_copies must be 1
  524. * - near_copies must be 2
  525. * - disks number must be even
  526. * - all mirrors must be already degraded
  527. */
  528. if (mddev->layout != ((1 << 8) + 2)) {
  529. printk(KERN_ERR "md/raid0:%s:: Raid0 cannot takover layout: 0x%x\n",
  530. mdname(mddev),
  531. mddev->layout);
  532. return ERR_PTR(-EINVAL);
  533. }
  534. if (mddev->raid_disks & 1) {
  535. printk(KERN_ERR "md/raid0:%s: Raid0 cannot takover Raid10 with odd disk number.\n",
  536. mdname(mddev));
  537. return ERR_PTR(-EINVAL);
  538. }
  539. if (mddev->degraded != (mddev->raid_disks>>1)) {
  540. printk(KERN_ERR "md/raid0:%s: All mirrors must be already degraded!\n",
  541. mdname(mddev));
  542. return ERR_PTR(-EINVAL);
  543. }
  544. /* Set new parameters */
  545. mddev->new_level = 0;
  546. mddev->new_layout = 0;
  547. mddev->new_chunk_sectors = mddev->chunk_sectors;
  548. mddev->delta_disks = - mddev->raid_disks / 2;
  549. mddev->raid_disks += mddev->delta_disks;
  550. mddev->degraded = 0;
  551. /* make sure it will be not marked as dirty */
  552. mddev->recovery_cp = MaxSector;
  553. create_strip_zones(mddev, &priv_conf);
  554. return priv_conf;
  555. }
  556. static void *raid0_takeover_raid1(struct mddev *mddev)
  557. {
  558. struct r0conf *priv_conf;
  559. int chunksect;
  560. /* Check layout:
  561. * - (N - 1) mirror drives must be already faulty
  562. */
  563. if ((mddev->raid_disks - 1) != mddev->degraded) {
  564. printk(KERN_ERR "md/raid0:%s: (N - 1) mirrors drives must be already faulty!\n",
  565. mdname(mddev));
  566. return ERR_PTR(-EINVAL);
  567. }
  568. /*
  569. * a raid1 doesn't have the notion of chunk size, so
  570. * figure out the largest suitable size we can use.
  571. */
  572. chunksect = 64 * 2; /* 64K by default */
  573. /* The array must be an exact multiple of chunksize */
  574. while (chunksect && (mddev->array_sectors & (chunksect - 1)))
  575. chunksect >>= 1;
  576. if ((chunksect << 9) < PAGE_SIZE)
  577. /* array size does not allow a suitable chunk size */
  578. return ERR_PTR(-EINVAL);
  579. /* Set new parameters */
  580. mddev->new_level = 0;
  581. mddev->new_layout = 0;
  582. mddev->new_chunk_sectors = chunksect;
  583. mddev->chunk_sectors = chunksect;
  584. mddev->delta_disks = 1 - mddev->raid_disks;
  585. mddev->raid_disks = 1;
  586. /* make sure it will be not marked as dirty */
  587. mddev->recovery_cp = MaxSector;
  588. create_strip_zones(mddev, &priv_conf);
  589. return priv_conf;
  590. }
  591. static void *raid0_takeover(struct mddev *mddev)
  592. {
  593. /* raid0 can take over:
  594. * raid4 - if all data disks are active.
  595. * raid5 - providing it is Raid4 layout and one disk is faulty
  596. * raid10 - assuming we have all necessary active disks
  597. * raid1 - with (N -1) mirror drives faulty
  598. */
  599. if (mddev->bitmap) {
  600. printk(KERN_ERR "md/raid0: %s: cannot takeover array with bitmap\n",
  601. mdname(mddev));
  602. return ERR_PTR(-EBUSY);
  603. }
  604. if (mddev->level == 4)
  605. return raid0_takeover_raid45(mddev);
  606. if (mddev->level == 5) {
  607. if (mddev->layout == ALGORITHM_PARITY_N)
  608. return raid0_takeover_raid45(mddev);
  609. printk(KERN_ERR "md/raid0:%s: Raid can only takeover Raid5 with layout: %d\n",
  610. mdname(mddev), ALGORITHM_PARITY_N);
  611. }
  612. if (mddev->level == 10)
  613. return raid0_takeover_raid10(mddev);
  614. if (mddev->level == 1)
  615. return raid0_takeover_raid1(mddev);
  616. printk(KERN_ERR "Takeover from raid%i to raid0 not supported\n",
  617. mddev->level);
  618. return ERR_PTR(-EINVAL);
  619. }
  620. static void raid0_quiesce(struct mddev *mddev, int state)
  621. {
  622. }
  623. static struct md_personality raid0_personality=
  624. {
  625. .name = "raid0",
  626. .level = 0,
  627. .owner = THIS_MODULE,
  628. .make_request = raid0_make_request,
  629. .run = raid0_run,
  630. .free = raid0_free,
  631. .status = raid0_status,
  632. .size = raid0_size,
  633. .takeover = raid0_takeover,
  634. .quiesce = raid0_quiesce,
  635. .congested = raid0_congested,
  636. .mergeable_bvec = raid0_mergeable_bvec,
  637. };
  638. static int __init raid0_init (void)
  639. {
  640. return register_md_personality (&raid0_personality);
  641. }
  642. static void raid0_exit (void)
  643. {
  644. unregister_md_personality (&raid0_personality);
  645. }
  646. module_init(raid0_init);
  647. module_exit(raid0_exit);
  648. MODULE_LICENSE("GPL");
  649. MODULE_DESCRIPTION("RAID0 (striping) personality for MD");
  650. MODULE_ALIAS("md-personality-2"); /* RAID0 */
  651. MODULE_ALIAS("md-raid0");
  652. MODULE_ALIAS("md-level-0");