raid0.c 22 KB

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