md-linear.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. linear.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. Linear mode management functions.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. You should have received a copy of the GNU General Public License
  12. (for example /usr/src/linux/COPYING); if not, write to the Free
  13. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #include <linux/blkdev.h>
  16. #include <linux/raid/md_u.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 "md-linear.h"
  23. /*
  24. * find which device holds a particular offset
  25. */
  26. static inline struct dev_info *which_dev(struct mddev *mddev, sector_t sector)
  27. {
  28. int lo, mid, hi;
  29. struct linear_conf *conf;
  30. lo = 0;
  31. hi = mddev->raid_disks - 1;
  32. conf = mddev->private;
  33. /*
  34. * Binary Search
  35. */
  36. while (hi > lo) {
  37. mid = (hi + lo) / 2;
  38. if (sector < conf->disks[mid].end_sector)
  39. hi = mid;
  40. else
  41. lo = mid + 1;
  42. }
  43. return conf->disks + lo;
  44. }
  45. /*
  46. * In linear_congested() conf->raid_disks is used as a copy of
  47. * mddev->raid_disks to iterate conf->disks[], because conf->raid_disks
  48. * and conf->disks[] are created in linear_conf(), they are always
  49. * consitent with each other, but mddev->raid_disks does not.
  50. */
  51. static int linear_congested(struct mddev *mddev, int bits)
  52. {
  53. struct linear_conf *conf;
  54. int i, ret = 0;
  55. rcu_read_lock();
  56. conf = rcu_dereference(mddev->private);
  57. for (i = 0; i < conf->raid_disks && !ret ; i++) {
  58. struct request_queue *q = bdev_get_queue(conf->disks[i].rdev->bdev);
  59. ret |= bdi_congested(q->backing_dev_info, bits);
  60. }
  61. rcu_read_unlock();
  62. return ret;
  63. }
  64. static sector_t linear_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  65. {
  66. struct linear_conf *conf;
  67. sector_t array_sectors;
  68. conf = mddev->private;
  69. WARN_ONCE(sectors || raid_disks,
  70. "%s does not support generic reshape\n", __func__);
  71. array_sectors = conf->array_sectors;
  72. return array_sectors;
  73. }
  74. static struct linear_conf *linear_conf(struct mddev *mddev, int raid_disks)
  75. {
  76. struct linear_conf *conf;
  77. struct md_rdev *rdev;
  78. int i, cnt;
  79. bool discard_supported = false;
  80. conf = kzalloc (sizeof (*conf) + raid_disks*sizeof(struct dev_info),
  81. GFP_KERNEL);
  82. if (!conf)
  83. return NULL;
  84. cnt = 0;
  85. conf->array_sectors = 0;
  86. rdev_for_each(rdev, mddev) {
  87. int j = rdev->raid_disk;
  88. struct dev_info *disk = conf->disks + j;
  89. sector_t sectors;
  90. if (j < 0 || j >= raid_disks || disk->rdev) {
  91. pr_warn("md/linear:%s: disk numbering problem. Aborting!\n",
  92. mdname(mddev));
  93. goto out;
  94. }
  95. disk->rdev = rdev;
  96. if (mddev->chunk_sectors) {
  97. sectors = rdev->sectors;
  98. sector_div(sectors, mddev->chunk_sectors);
  99. rdev->sectors = sectors * mddev->chunk_sectors;
  100. }
  101. disk_stack_limits(mddev->gendisk, rdev->bdev,
  102. rdev->data_offset << 9);
  103. conf->array_sectors += rdev->sectors;
  104. cnt++;
  105. if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
  106. discard_supported = true;
  107. }
  108. if (cnt != raid_disks) {
  109. pr_warn("md/linear:%s: not enough drives present. Aborting!\n",
  110. mdname(mddev));
  111. goto out;
  112. }
  113. if (!discard_supported)
  114. blk_queue_flag_clear(QUEUE_FLAG_DISCARD, mddev->queue);
  115. else
  116. blk_queue_flag_set(QUEUE_FLAG_DISCARD, mddev->queue);
  117. /*
  118. * Here we calculate the device offsets.
  119. */
  120. conf->disks[0].end_sector = conf->disks[0].rdev->sectors;
  121. for (i = 1; i < raid_disks; i++)
  122. conf->disks[i].end_sector =
  123. conf->disks[i-1].end_sector +
  124. conf->disks[i].rdev->sectors;
  125. /*
  126. * conf->raid_disks is copy of mddev->raid_disks. The reason to
  127. * keep a copy of mddev->raid_disks in struct linear_conf is,
  128. * mddev->raid_disks may not be consistent with pointers number of
  129. * conf->disks[] when it is updated in linear_add() and used to
  130. * iterate old conf->disks[] earray in linear_congested().
  131. * Here conf->raid_disks is always consitent with number of
  132. * pointers in conf->disks[] array, and mddev->private is updated
  133. * with rcu_assign_pointer() in linear_addr(), such race can be
  134. * avoided.
  135. */
  136. conf->raid_disks = raid_disks;
  137. return conf;
  138. out:
  139. kfree(conf);
  140. return NULL;
  141. }
  142. static int linear_run (struct mddev *mddev)
  143. {
  144. struct linear_conf *conf;
  145. int ret;
  146. if (md_check_no_bitmap(mddev))
  147. return -EINVAL;
  148. conf = linear_conf(mddev, mddev->raid_disks);
  149. if (!conf)
  150. return 1;
  151. mddev->private = conf;
  152. md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
  153. ret = md_integrity_register(mddev);
  154. if (ret) {
  155. kfree(conf);
  156. mddev->private = NULL;
  157. }
  158. return ret;
  159. }
  160. static int linear_add(struct mddev *mddev, struct md_rdev *rdev)
  161. {
  162. /* Adding a drive to a linear array allows the array to grow.
  163. * It is permitted if the new drive has a matching superblock
  164. * already on it, with raid_disk equal to raid_disks.
  165. * It is achieved by creating a new linear_private_data structure
  166. * and swapping it in in-place of the current one.
  167. * The current one is never freed until the array is stopped.
  168. * This avoids races.
  169. */
  170. struct linear_conf *newconf, *oldconf;
  171. if (rdev->saved_raid_disk != mddev->raid_disks)
  172. return -EINVAL;
  173. rdev->raid_disk = rdev->saved_raid_disk;
  174. rdev->saved_raid_disk = -1;
  175. newconf = linear_conf(mddev,mddev->raid_disks+1);
  176. if (!newconf)
  177. return -ENOMEM;
  178. /* newconf->raid_disks already keeps a copy of * the increased
  179. * value of mddev->raid_disks, WARN_ONCE() is just used to make
  180. * sure of this. It is possible that oldconf is still referenced
  181. * in linear_congested(), therefore kfree_rcu() is used to free
  182. * oldconf until no one uses it anymore.
  183. */
  184. mddev_suspend(mddev);
  185. oldconf = rcu_dereference_protected(mddev->private,
  186. lockdep_is_held(&mddev->reconfig_mutex));
  187. mddev->raid_disks++;
  188. WARN_ONCE(mddev->raid_disks != newconf->raid_disks,
  189. "copied raid_disks doesn't match mddev->raid_disks");
  190. rcu_assign_pointer(mddev->private, newconf);
  191. md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
  192. set_capacity(mddev->gendisk, mddev->array_sectors);
  193. mddev_resume(mddev);
  194. revalidate_disk(mddev->gendisk);
  195. kfree_rcu(oldconf, rcu);
  196. return 0;
  197. }
  198. static void linear_free(struct mddev *mddev, void *priv)
  199. {
  200. struct linear_conf *conf = priv;
  201. kfree(conf);
  202. }
  203. static bool linear_make_request(struct mddev *mddev, struct bio *bio)
  204. {
  205. char b[BDEVNAME_SIZE];
  206. struct dev_info *tmp_dev;
  207. sector_t start_sector, end_sector, data_offset;
  208. sector_t bio_sector = bio->bi_iter.bi_sector;
  209. if (unlikely(bio->bi_opf & REQ_PREFLUSH)
  210. && md_flush_request(mddev, bio))
  211. return true;
  212. tmp_dev = which_dev(mddev, bio_sector);
  213. start_sector = tmp_dev->end_sector - tmp_dev->rdev->sectors;
  214. end_sector = tmp_dev->end_sector;
  215. data_offset = tmp_dev->rdev->data_offset;
  216. if (unlikely(bio_sector >= end_sector ||
  217. bio_sector < start_sector))
  218. goto out_of_bounds;
  219. if (unlikely(bio_end_sector(bio) > end_sector)) {
  220. /* This bio crosses a device boundary, so we have to split it */
  221. struct bio *split = bio_split(bio, end_sector - bio_sector,
  222. GFP_NOIO, &mddev->bio_set);
  223. bio_chain(split, bio);
  224. generic_make_request(bio);
  225. bio = split;
  226. }
  227. bio_set_dev(bio, tmp_dev->rdev->bdev);
  228. bio->bi_iter.bi_sector = bio->bi_iter.bi_sector -
  229. start_sector + data_offset;
  230. if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
  231. !blk_queue_discard(bio->bi_disk->queue))) {
  232. /* Just ignore it */
  233. bio_endio(bio);
  234. } else {
  235. if (mddev->gendisk)
  236. trace_block_bio_remap(bio->bi_disk->queue,
  237. bio, disk_devt(mddev->gendisk),
  238. bio_sector);
  239. mddev_check_writesame(mddev, bio);
  240. mddev_check_write_zeroes(mddev, bio);
  241. generic_make_request(bio);
  242. }
  243. return true;
  244. out_of_bounds:
  245. pr_err("md/linear:%s: make_request: Sector %llu out of bounds on dev %s: %llu sectors, offset %llu\n",
  246. mdname(mddev),
  247. (unsigned long long)bio->bi_iter.bi_sector,
  248. bdevname(tmp_dev->rdev->bdev, b),
  249. (unsigned long long)tmp_dev->rdev->sectors,
  250. (unsigned long long)start_sector);
  251. bio_io_error(bio);
  252. return true;
  253. }
  254. static void linear_status (struct seq_file *seq, struct mddev *mddev)
  255. {
  256. seq_printf(seq, " %dk rounding", mddev->chunk_sectors / 2);
  257. }
  258. static void linear_quiesce(struct mddev *mddev, int state)
  259. {
  260. }
  261. static struct md_personality linear_personality =
  262. {
  263. .name = "linear",
  264. .level = LEVEL_LINEAR,
  265. .owner = THIS_MODULE,
  266. .make_request = linear_make_request,
  267. .run = linear_run,
  268. .free = linear_free,
  269. .status = linear_status,
  270. .hot_add_disk = linear_add,
  271. .size = linear_size,
  272. .quiesce = linear_quiesce,
  273. .congested = linear_congested,
  274. };
  275. static int __init linear_init (void)
  276. {
  277. return register_md_personality (&linear_personality);
  278. }
  279. static void linear_exit (void)
  280. {
  281. unregister_md_personality (&linear_personality);
  282. }
  283. module_init(linear_init);
  284. module_exit(linear_exit);
  285. MODULE_LICENSE("GPL");
  286. MODULE_DESCRIPTION("Linear device concatenation personality for MD");
  287. MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
  288. MODULE_ALIAS("md-linear");
  289. MODULE_ALIAS("md-level--1");