linear.c 8.8 KB

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