raid0.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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 <trace/events/block.h>
  21. #include "md.h"
  22. #include "raid0.h"
  23. #include "raid5.h"
  24. #define UNSUPPORTED_MDDEV_FLAGS \
  25. ((1L << MD_HAS_JOURNAL) | \
  26. (1L << MD_JOURNAL_CLEAN) | \
  27. (1L << MD_FAILFAST_SUPPORTED) |\
  28. (1L << MD_HAS_PPL) | \
  29. (1L << MD_HAS_MULTIPLE_PPLS))
  30. static int raid0_congested(struct mddev *mddev, int bits)
  31. {
  32. struct r0conf *conf = mddev->private;
  33. struct md_rdev **devlist = conf->devlist;
  34. int raid_disks = conf->strip_zone[0].nb_dev;
  35. int i, ret = 0;
  36. for (i = 0; i < raid_disks && !ret ; i++) {
  37. struct request_queue *q = bdev_get_queue(devlist[i]->bdev);
  38. ret |= bdi_congested(q->backing_dev_info, bits);
  39. }
  40. return ret;
  41. }
  42. /*
  43. * inform the user of the raid configuration
  44. */
  45. static void dump_zones(struct mddev *mddev)
  46. {
  47. int j, k;
  48. sector_t zone_size = 0;
  49. sector_t zone_start = 0;
  50. char b[BDEVNAME_SIZE];
  51. struct r0conf *conf = mddev->private;
  52. int raid_disks = conf->strip_zone[0].nb_dev;
  53. pr_debug("md: RAID0 configuration for %s - %d zone%s\n",
  54. mdname(mddev),
  55. conf->nr_strip_zones, conf->nr_strip_zones==1?"":"s");
  56. for (j = 0; j < conf->nr_strip_zones; j++) {
  57. char line[200];
  58. int len = 0;
  59. for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
  60. len += snprintf(line+len, 200-len, "%s%s", k?"/":"",
  61. bdevname(conf->devlist[j*raid_disks
  62. + k]->bdev, b));
  63. pr_debug("md: zone%d=[%s]\n", j, line);
  64. zone_size = conf->strip_zone[j].zone_end - zone_start;
  65. pr_debug(" zone-offset=%10lluKB, device-offset=%10lluKB, size=%10lluKB\n",
  66. (unsigned long long)zone_start>>1,
  67. (unsigned long long)conf->strip_zone[j].dev_start>>1,
  68. (unsigned long long)zone_size>>1);
  69. zone_start = conf->strip_zone[j].zone_end;
  70. }
  71. }
  72. static int create_strip_zones(struct mddev *mddev, struct r0conf **private_conf)
  73. {
  74. int i, c, err;
  75. sector_t curr_zone_end, sectors;
  76. struct md_rdev *smallest, *rdev1, *rdev2, *rdev, **dev;
  77. struct strip_zone *zone;
  78. int cnt;
  79. char b[BDEVNAME_SIZE];
  80. char b2[BDEVNAME_SIZE];
  81. struct r0conf *conf = kzalloc(sizeof(*conf), GFP_KERNEL);
  82. unsigned short blksize = 512;
  83. *private_conf = ERR_PTR(-ENOMEM);
  84. if (!conf)
  85. return -ENOMEM;
  86. rdev_for_each(rdev1, mddev) {
  87. pr_debug("md/raid0:%s: looking at %s\n",
  88. mdname(mddev),
  89. bdevname(rdev1->bdev, b));
  90. c = 0;
  91. /* round size to chunk_size */
  92. sectors = rdev1->sectors;
  93. sector_div(sectors, mddev->chunk_sectors);
  94. rdev1->sectors = sectors * mddev->chunk_sectors;
  95. blksize = max(blksize, queue_logical_block_size(
  96. rdev1->bdev->bd_disk->queue));
  97. rdev_for_each(rdev2, mddev) {
  98. pr_debug("md/raid0:%s: comparing %s(%llu)"
  99. " with %s(%llu)\n",
  100. mdname(mddev),
  101. bdevname(rdev1->bdev,b),
  102. (unsigned long long)rdev1->sectors,
  103. bdevname(rdev2->bdev,b2),
  104. (unsigned long long)rdev2->sectors);
  105. if (rdev2 == rdev1) {
  106. pr_debug("md/raid0:%s: END\n",
  107. mdname(mddev));
  108. break;
  109. }
  110. if (rdev2->sectors == rdev1->sectors) {
  111. /*
  112. * Not unique, don't count it as a new
  113. * group
  114. */
  115. pr_debug("md/raid0:%s: EQUAL\n",
  116. mdname(mddev));
  117. c = 1;
  118. break;
  119. }
  120. pr_debug("md/raid0:%s: NOT EQUAL\n",
  121. mdname(mddev));
  122. }
  123. if (!c) {
  124. pr_debug("md/raid0:%s: ==> UNIQUE\n",
  125. mdname(mddev));
  126. conf->nr_strip_zones++;
  127. pr_debug("md/raid0:%s: %d zones\n",
  128. mdname(mddev), conf->nr_strip_zones);
  129. }
  130. }
  131. pr_debug("md/raid0:%s: FINAL %d zones\n",
  132. mdname(mddev), conf->nr_strip_zones);
  133. /*
  134. * now since we have the hard sector sizes, we can make sure
  135. * chunk size is a multiple of that sector size
  136. */
  137. if ((mddev->chunk_sectors << 9) % blksize) {
  138. pr_warn("md/raid0:%s: chunk_size of %d not multiple of block size %d\n",
  139. mdname(mddev),
  140. mddev->chunk_sectors << 9, blksize);
  141. err = -EINVAL;
  142. goto abort;
  143. }
  144. err = -ENOMEM;
  145. conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
  146. conf->nr_strip_zones, GFP_KERNEL);
  147. if (!conf->strip_zone)
  148. goto abort;
  149. conf->devlist = kzalloc(sizeof(struct md_rdev*)*
  150. conf->nr_strip_zones*mddev->raid_disks,
  151. GFP_KERNEL);
  152. if (!conf->devlist)
  153. goto abort;
  154. /* The first zone must contain all devices, so here we check that
  155. * there is a proper alignment of slots to devices and find them all
  156. */
  157. zone = &conf->strip_zone[0];
  158. cnt = 0;
  159. smallest = NULL;
  160. dev = conf->devlist;
  161. err = -EINVAL;
  162. rdev_for_each(rdev1, mddev) {
  163. int j = rdev1->raid_disk;
  164. if (mddev->level == 10) {
  165. /* taking over a raid10-n2 array */
  166. j /= 2;
  167. rdev1->new_raid_disk = j;
  168. }
  169. if (mddev->level == 1) {
  170. /* taiking over a raid1 array-
  171. * we have only one active disk
  172. */
  173. j = 0;
  174. rdev1->new_raid_disk = j;
  175. }
  176. if (j < 0) {
  177. pr_warn("md/raid0:%s: remove inactive devices before converting to RAID0\n",
  178. mdname(mddev));
  179. goto abort;
  180. }
  181. if (j >= mddev->raid_disks) {
  182. pr_warn("md/raid0:%s: bad disk number %d - aborting!\n",
  183. mdname(mddev), j);
  184. goto abort;
  185. }
  186. if (dev[j]) {
  187. pr_warn("md/raid0:%s: multiple devices for %d - aborting!\n",
  188. mdname(mddev), j);
  189. goto abort;
  190. }
  191. dev[j] = rdev1;
  192. if (!smallest || (rdev1->sectors < smallest->sectors))
  193. smallest = rdev1;
  194. cnt++;
  195. }
  196. if (cnt != mddev->raid_disks) {
  197. pr_warn("md/raid0:%s: too few disks (%d of %d) - aborting!\n",
  198. mdname(mddev), cnt, mddev->raid_disks);
  199. goto abort;
  200. }
  201. zone->nb_dev = cnt;
  202. zone->zone_end = smallest->sectors * cnt;
  203. curr_zone_end = zone->zone_end;
  204. /* now do the other zones */
  205. for (i = 1; i < conf->nr_strip_zones; i++)
  206. {
  207. int j;
  208. zone = conf->strip_zone + i;
  209. dev = conf->devlist + i * mddev->raid_disks;
  210. pr_debug("md/raid0:%s: zone %d\n", mdname(mddev), i);
  211. zone->dev_start = smallest->sectors;
  212. smallest = NULL;
  213. c = 0;
  214. for (j=0; j<cnt; j++) {
  215. rdev = conf->devlist[j];
  216. if (rdev->sectors <= zone->dev_start) {
  217. pr_debug("md/raid0:%s: checking %s ... nope\n",
  218. mdname(mddev),
  219. bdevname(rdev->bdev, b));
  220. continue;
  221. }
  222. pr_debug("md/raid0:%s: checking %s ..."
  223. " contained as device %d\n",
  224. mdname(mddev),
  225. bdevname(rdev->bdev, b), c);
  226. dev[c] = rdev;
  227. c++;
  228. if (!smallest || rdev->sectors < smallest->sectors) {
  229. smallest = rdev;
  230. pr_debug("md/raid0:%s: (%llu) is smallest!.\n",
  231. mdname(mddev),
  232. (unsigned long long)rdev->sectors);
  233. }
  234. }
  235. zone->nb_dev = c;
  236. sectors = (smallest->sectors - zone->dev_start) * c;
  237. pr_debug("md/raid0:%s: zone->nb_dev: %d, sectors: %llu\n",
  238. mdname(mddev),
  239. zone->nb_dev, (unsigned long long)sectors);
  240. curr_zone_end += sectors;
  241. zone->zone_end = curr_zone_end;
  242. pr_debug("md/raid0:%s: current zone start: %llu\n",
  243. mdname(mddev),
  244. (unsigned long long)smallest->sectors);
  245. }
  246. pr_debug("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 = ERR_PTR(err);
  254. return err;
  255. }
  256. /* Find the zone which holds a particular offset
  257. * Update *sectorp to be an offset in that zone
  258. */
  259. static struct strip_zone *find_zone(struct r0conf *conf,
  260. sector_t *sectorp)
  261. {
  262. int i;
  263. struct strip_zone *z = conf->strip_zone;
  264. sector_t sector = *sectorp;
  265. for (i = 0; i < conf->nr_strip_zones; i++)
  266. if (sector < z[i].zone_end) {
  267. if (i)
  268. *sectorp = sector - z[i-1].zone_end;
  269. return z + i;
  270. }
  271. BUG();
  272. }
  273. /*
  274. * remaps the bio to the target device. we separate two flows.
  275. * power 2 flow and a general flow for the sake of performance
  276. */
  277. static struct md_rdev *map_sector(struct mddev *mddev, struct strip_zone *zone,
  278. sector_t sector, sector_t *sector_offset)
  279. {
  280. unsigned int sect_in_chunk;
  281. sector_t chunk;
  282. struct r0conf *conf = mddev->private;
  283. int raid_disks = conf->strip_zone[0].nb_dev;
  284. unsigned int chunk_sects = mddev->chunk_sectors;
  285. if (is_power_of_2(chunk_sects)) {
  286. int chunksect_bits = ffz(~chunk_sects);
  287. /* find the sector offset inside the chunk */
  288. sect_in_chunk = sector & (chunk_sects - 1);
  289. sector >>= chunksect_bits;
  290. /* chunk in zone */
  291. chunk = *sector_offset;
  292. /* quotient is the chunk in real device*/
  293. sector_div(chunk, zone->nb_dev << chunksect_bits);
  294. } else{
  295. sect_in_chunk = sector_div(sector, chunk_sects);
  296. chunk = *sector_offset;
  297. sector_div(chunk, chunk_sects * zone->nb_dev);
  298. }
  299. /*
  300. * position the bio over the real device
  301. * real sector = chunk in device + starting of zone
  302. * + the position in the chunk
  303. */
  304. *sector_offset = (chunk * chunk_sects) + sect_in_chunk;
  305. return conf->devlist[(zone - conf->strip_zone)*raid_disks
  306. + sector_div(sector, zone->nb_dev)];
  307. }
  308. static sector_t raid0_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  309. {
  310. sector_t array_sectors = 0;
  311. struct md_rdev *rdev;
  312. WARN_ONCE(sectors || raid_disks,
  313. "%s does not support generic reshape\n", __func__);
  314. rdev_for_each(rdev, mddev)
  315. array_sectors += (rdev->sectors &
  316. ~(sector_t)(mddev->chunk_sectors-1));
  317. return array_sectors;
  318. }
  319. static void raid0_free(struct mddev *mddev, void *priv);
  320. static int raid0_run(struct mddev *mddev)
  321. {
  322. struct r0conf *conf;
  323. int ret;
  324. if (mddev->chunk_sectors == 0) {
  325. pr_warn("md/raid0:%s: chunk size must be set.\n", mdname(mddev));
  326. return -EINVAL;
  327. }
  328. if (md_check_no_bitmap(mddev))
  329. return -EINVAL;
  330. /* if private is not null, we are here after takeover */
  331. if (mddev->private == NULL) {
  332. ret = create_strip_zones(mddev, &conf);
  333. if (ret < 0)
  334. return ret;
  335. mddev->private = conf;
  336. }
  337. conf = mddev->private;
  338. if (mddev->queue) {
  339. struct md_rdev *rdev;
  340. bool discard_supported = false;
  341. blk_queue_max_hw_sectors(mddev->queue, mddev->chunk_sectors);
  342. blk_queue_max_write_same_sectors(mddev->queue, mddev->chunk_sectors);
  343. blk_queue_max_write_zeroes_sectors(mddev->queue, mddev->chunk_sectors);
  344. blk_queue_max_discard_sectors(mddev->queue, UINT_MAX);
  345. blk_queue_io_min(mddev->queue, mddev->chunk_sectors << 9);
  346. blk_queue_io_opt(mddev->queue,
  347. (mddev->chunk_sectors << 9) * mddev->raid_disks);
  348. rdev_for_each(rdev, mddev) {
  349. disk_stack_limits(mddev->gendisk, rdev->bdev,
  350. rdev->data_offset << 9);
  351. if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
  352. discard_supported = true;
  353. }
  354. if (!discard_supported)
  355. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
  356. else
  357. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
  358. }
  359. /* calculate array device size */
  360. md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
  361. pr_debug("md/raid0:%s: md_size is %llu sectors.\n",
  362. mdname(mddev),
  363. (unsigned long long)mddev->array_sectors);
  364. if (mddev->queue) {
  365. /* calculate the max read-ahead size.
  366. * For read-ahead of large files to be effective, we need to
  367. * readahead at least twice a whole stripe. i.e. number of devices
  368. * multiplied by chunk size times 2.
  369. * If an individual device has an ra_pages greater than the
  370. * chunk size, then we will not drive that device as hard as it
  371. * wants. We consider this a configuration error: a larger
  372. * chunksize should be used in that case.
  373. */
  374. int stripe = mddev->raid_disks *
  375. (mddev->chunk_sectors << 9) / PAGE_SIZE;
  376. if (mddev->queue->backing_dev_info->ra_pages < 2* stripe)
  377. mddev->queue->backing_dev_info->ra_pages = 2* stripe;
  378. }
  379. dump_zones(mddev);
  380. ret = md_integrity_register(mddev);
  381. return ret;
  382. }
  383. static void raid0_free(struct mddev *mddev, void *priv)
  384. {
  385. struct r0conf *conf = priv;
  386. kfree(conf->strip_zone);
  387. kfree(conf->devlist);
  388. kfree(conf);
  389. }
  390. /*
  391. * Is io distribute over 1 or more chunks ?
  392. */
  393. static inline int is_io_in_chunk_boundary(struct mddev *mddev,
  394. unsigned int chunk_sects, struct bio *bio)
  395. {
  396. if (likely(is_power_of_2(chunk_sects))) {
  397. return chunk_sects >=
  398. ((bio->bi_iter.bi_sector & (chunk_sects-1))
  399. + bio_sectors(bio));
  400. } else{
  401. sector_t sector = bio->bi_iter.bi_sector;
  402. return chunk_sects >= (sector_div(sector, chunk_sects)
  403. + bio_sectors(bio));
  404. }
  405. }
  406. static void raid0_handle_discard(struct mddev *mddev, struct bio *bio)
  407. {
  408. struct r0conf *conf = mddev->private;
  409. struct strip_zone *zone;
  410. sector_t start = bio->bi_iter.bi_sector;
  411. sector_t end;
  412. unsigned int stripe_size;
  413. sector_t first_stripe_index, last_stripe_index;
  414. sector_t start_disk_offset;
  415. unsigned int start_disk_index;
  416. sector_t end_disk_offset;
  417. unsigned int end_disk_index;
  418. unsigned int disk;
  419. zone = find_zone(conf, &start);
  420. if (bio_end_sector(bio) > zone->zone_end) {
  421. struct bio *split = bio_split(bio,
  422. zone->zone_end - bio->bi_iter.bi_sector, GFP_NOIO,
  423. mddev->bio_set);
  424. bio_chain(split, bio);
  425. generic_make_request(bio);
  426. bio = split;
  427. end = zone->zone_end;
  428. } else
  429. end = bio_end_sector(bio);
  430. if (zone != conf->strip_zone)
  431. end = end - zone[-1].zone_end;
  432. /* Now start and end is the offset in zone */
  433. stripe_size = zone->nb_dev * mddev->chunk_sectors;
  434. first_stripe_index = start;
  435. sector_div(first_stripe_index, stripe_size);
  436. last_stripe_index = end;
  437. sector_div(last_stripe_index, stripe_size);
  438. start_disk_index = (int)(start - first_stripe_index * stripe_size) /
  439. mddev->chunk_sectors;
  440. start_disk_offset = ((int)(start - first_stripe_index * stripe_size) %
  441. mddev->chunk_sectors) +
  442. first_stripe_index * mddev->chunk_sectors;
  443. end_disk_index = (int)(end - last_stripe_index * stripe_size) /
  444. mddev->chunk_sectors;
  445. end_disk_offset = ((int)(end - last_stripe_index * stripe_size) %
  446. mddev->chunk_sectors) +
  447. last_stripe_index * mddev->chunk_sectors;
  448. for (disk = 0; disk < zone->nb_dev; disk++) {
  449. sector_t dev_start, dev_end;
  450. struct bio *discard_bio = NULL;
  451. struct md_rdev *rdev;
  452. if (disk < start_disk_index)
  453. dev_start = (first_stripe_index + 1) *
  454. mddev->chunk_sectors;
  455. else if (disk > start_disk_index)
  456. dev_start = first_stripe_index * mddev->chunk_sectors;
  457. else
  458. dev_start = start_disk_offset;
  459. if (disk < end_disk_index)
  460. dev_end = (last_stripe_index + 1) * mddev->chunk_sectors;
  461. else if (disk > end_disk_index)
  462. dev_end = last_stripe_index * mddev->chunk_sectors;
  463. else
  464. dev_end = end_disk_offset;
  465. if (dev_end <= dev_start)
  466. continue;
  467. rdev = conf->devlist[(zone - conf->strip_zone) *
  468. conf->strip_zone[0].nb_dev + disk];
  469. if (__blkdev_issue_discard(rdev->bdev,
  470. dev_start + zone->dev_start + rdev->data_offset,
  471. dev_end - dev_start, GFP_NOIO, 0, &discard_bio) ||
  472. !discard_bio)
  473. continue;
  474. bio_chain(discard_bio, bio);
  475. bio_clone_blkcg_association(discard_bio, bio);
  476. if (mddev->gendisk)
  477. trace_block_bio_remap(bdev_get_queue(rdev->bdev),
  478. discard_bio, disk_devt(mddev->gendisk),
  479. bio->bi_iter.bi_sector);
  480. generic_make_request(discard_bio);
  481. }
  482. bio_endio(bio);
  483. }
  484. static bool raid0_make_request(struct mddev *mddev, struct bio *bio)
  485. {
  486. struct strip_zone *zone;
  487. struct md_rdev *tmp_dev;
  488. sector_t bio_sector;
  489. sector_t sector;
  490. unsigned chunk_sects;
  491. unsigned sectors;
  492. if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
  493. md_flush_request(mddev, bio);
  494. return true;
  495. }
  496. if (unlikely((bio_op(bio) == REQ_OP_DISCARD))) {
  497. raid0_handle_discard(mddev, bio);
  498. return true;
  499. }
  500. bio_sector = bio->bi_iter.bi_sector;
  501. sector = bio_sector;
  502. chunk_sects = mddev->chunk_sectors;
  503. sectors = chunk_sects -
  504. (likely(is_power_of_2(chunk_sects))
  505. ? (sector & (chunk_sects-1))
  506. : sector_div(sector, chunk_sects));
  507. /* Restore due to sector_div */
  508. sector = bio_sector;
  509. if (sectors < bio_sectors(bio)) {
  510. struct bio *split = bio_split(bio, sectors, GFP_NOIO, mddev->bio_set);
  511. bio_chain(split, bio);
  512. generic_make_request(bio);
  513. bio = split;
  514. }
  515. zone = find_zone(mddev->private, &sector);
  516. tmp_dev = map_sector(mddev, zone, sector, &sector);
  517. bio_set_dev(bio, tmp_dev->bdev);
  518. bio->bi_iter.bi_sector = sector + zone->dev_start +
  519. tmp_dev->data_offset;
  520. if (mddev->gendisk)
  521. trace_block_bio_remap(bio->bi_disk->queue, bio,
  522. disk_devt(mddev->gendisk), bio_sector);
  523. mddev_check_writesame(mddev, bio);
  524. mddev_check_write_zeroes(mddev, bio);
  525. generic_make_request(bio);
  526. return true;
  527. }
  528. static void raid0_status(struct seq_file *seq, struct mddev *mddev)
  529. {
  530. seq_printf(seq, " %dk chunks", mddev->chunk_sectors / 2);
  531. return;
  532. }
  533. static void *raid0_takeover_raid45(struct mddev *mddev)
  534. {
  535. struct md_rdev *rdev;
  536. struct r0conf *priv_conf;
  537. if (mddev->degraded != 1) {
  538. pr_warn("md/raid0:%s: raid5 must be degraded! Degraded disks: %d\n",
  539. mdname(mddev),
  540. mddev->degraded);
  541. return ERR_PTR(-EINVAL);
  542. }
  543. rdev_for_each(rdev, mddev) {
  544. /* check slot number for a disk */
  545. if (rdev->raid_disk == mddev->raid_disks-1) {
  546. pr_warn("md/raid0:%s: raid5 must have missing parity disk!\n",
  547. mdname(mddev));
  548. return ERR_PTR(-EINVAL);
  549. }
  550. rdev->sectors = mddev->dev_sectors;
  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->raid_disks--;
  557. mddev->delta_disks = -1;
  558. /* make sure it will be not marked as dirty */
  559. mddev->recovery_cp = MaxSector;
  560. mddev_clear_unsupported_flags(mddev, UNSUPPORTED_MDDEV_FLAGS);
  561. create_strip_zones(mddev, &priv_conf);
  562. return priv_conf;
  563. }
  564. static void *raid0_takeover_raid10(struct mddev *mddev)
  565. {
  566. struct r0conf *priv_conf;
  567. /* Check layout:
  568. * - far_copies must be 1
  569. * - near_copies must be 2
  570. * - disks number must be even
  571. * - all mirrors must be already degraded
  572. */
  573. if (mddev->layout != ((1 << 8) + 2)) {
  574. pr_warn("md/raid0:%s:: Raid0 cannot takeover layout: 0x%x\n",
  575. mdname(mddev),
  576. mddev->layout);
  577. return ERR_PTR(-EINVAL);
  578. }
  579. if (mddev->raid_disks & 1) {
  580. pr_warn("md/raid0:%s: Raid0 cannot takeover Raid10 with odd disk number.\n",
  581. mdname(mddev));
  582. return ERR_PTR(-EINVAL);
  583. }
  584. if (mddev->degraded != (mddev->raid_disks>>1)) {
  585. pr_warn("md/raid0:%s: All mirrors must be already degraded!\n",
  586. mdname(mddev));
  587. return ERR_PTR(-EINVAL);
  588. }
  589. /* Set new parameters */
  590. mddev->new_level = 0;
  591. mddev->new_layout = 0;
  592. mddev->new_chunk_sectors = mddev->chunk_sectors;
  593. mddev->delta_disks = - mddev->raid_disks / 2;
  594. mddev->raid_disks += mddev->delta_disks;
  595. mddev->degraded = 0;
  596. /* make sure it will be not marked as dirty */
  597. mddev->recovery_cp = MaxSector;
  598. mddev_clear_unsupported_flags(mddev, UNSUPPORTED_MDDEV_FLAGS);
  599. create_strip_zones(mddev, &priv_conf);
  600. return priv_conf;
  601. }
  602. static void *raid0_takeover_raid1(struct mddev *mddev)
  603. {
  604. struct r0conf *priv_conf;
  605. int chunksect;
  606. /* Check layout:
  607. * - (N - 1) mirror drives must be already faulty
  608. */
  609. if ((mddev->raid_disks - 1) != mddev->degraded) {
  610. pr_err("md/raid0:%s: (N - 1) mirrors drives must be already faulty!\n",
  611. mdname(mddev));
  612. return ERR_PTR(-EINVAL);
  613. }
  614. /*
  615. * a raid1 doesn't have the notion of chunk size, so
  616. * figure out the largest suitable size we can use.
  617. */
  618. chunksect = 64 * 2; /* 64K by default */
  619. /* The array must be an exact multiple of chunksize */
  620. while (chunksect && (mddev->array_sectors & (chunksect - 1)))
  621. chunksect >>= 1;
  622. if ((chunksect << 9) < PAGE_SIZE)
  623. /* array size does not allow a suitable chunk size */
  624. return ERR_PTR(-EINVAL);
  625. /* Set new parameters */
  626. mddev->new_level = 0;
  627. mddev->new_layout = 0;
  628. mddev->new_chunk_sectors = chunksect;
  629. mddev->chunk_sectors = chunksect;
  630. mddev->delta_disks = 1 - mddev->raid_disks;
  631. mddev->raid_disks = 1;
  632. /* make sure it will be not marked as dirty */
  633. mddev->recovery_cp = MaxSector;
  634. mddev_clear_unsupported_flags(mddev, UNSUPPORTED_MDDEV_FLAGS);
  635. create_strip_zones(mddev, &priv_conf);
  636. return priv_conf;
  637. }
  638. static void *raid0_takeover(struct mddev *mddev)
  639. {
  640. /* raid0 can take over:
  641. * raid4 - if all data disks are active.
  642. * raid5 - providing it is Raid4 layout and one disk is faulty
  643. * raid10 - assuming we have all necessary active disks
  644. * raid1 - with (N -1) mirror drives faulty
  645. */
  646. if (mddev->bitmap) {
  647. pr_warn("md/raid0: %s: cannot takeover array with bitmap\n",
  648. mdname(mddev));
  649. return ERR_PTR(-EBUSY);
  650. }
  651. if (mddev->level == 4)
  652. return raid0_takeover_raid45(mddev);
  653. if (mddev->level == 5) {
  654. if (mddev->layout == ALGORITHM_PARITY_N)
  655. return raid0_takeover_raid45(mddev);
  656. pr_warn("md/raid0:%s: Raid can only takeover Raid5 with layout: %d\n",
  657. mdname(mddev), ALGORITHM_PARITY_N);
  658. }
  659. if (mddev->level == 10)
  660. return raid0_takeover_raid10(mddev);
  661. if (mddev->level == 1)
  662. return raid0_takeover_raid1(mddev);
  663. pr_warn("Takeover from raid%i to raid0 not supported\n",
  664. mddev->level);
  665. return ERR_PTR(-EINVAL);
  666. }
  667. static void raid0_quiesce(struct mddev *mddev, int state)
  668. {
  669. }
  670. static struct md_personality raid0_personality=
  671. {
  672. .name = "raid0",
  673. .level = 0,
  674. .owner = THIS_MODULE,
  675. .make_request = raid0_make_request,
  676. .run = raid0_run,
  677. .free = raid0_free,
  678. .status = raid0_status,
  679. .size = raid0_size,
  680. .takeover = raid0_takeover,
  681. .quiesce = raid0_quiesce,
  682. .congested = raid0_congested,
  683. };
  684. static int __init raid0_init (void)
  685. {
  686. return register_md_personality (&raid0_personality);
  687. }
  688. static void raid0_exit (void)
  689. {
  690. unregister_md_personality (&raid0_personality);
  691. }
  692. module_init(raid0_init);
  693. module_exit(raid0_exit);
  694. MODULE_LICENSE("GPL");
  695. MODULE_DESCRIPTION("RAID0 (striping) personality for MD");
  696. MODULE_ALIAS("md-personality-2"); /* RAID0 */
  697. MODULE_ALIAS("md-raid0");
  698. MODULE_ALIAS("md-level-0");