raid0.c 22 KB

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