linear.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. * linear_mergeable_bvec -- tell bio layer if two requests can be merged
  46. * @q: request queue
  47. * @bvm: properties of new bio
  48. * @biovec: the request that could be merged to it.
  49. *
  50. * Return amount of bytes we can take at this offset
  51. */
  52. static int linear_mergeable_bvec(struct mddev *mddev,
  53. struct bvec_merge_data *bvm,
  54. struct bio_vec *biovec)
  55. {
  56. struct dev_info *dev0;
  57. unsigned long maxsectors, bio_sectors = bvm->bi_size >> 9;
  58. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  59. int maxbytes = biovec->bv_len;
  60. struct request_queue *subq;
  61. dev0 = which_dev(mddev, sector);
  62. maxsectors = dev0->end_sector - sector;
  63. subq = bdev_get_queue(dev0->rdev->bdev);
  64. if (subq->merge_bvec_fn) {
  65. bvm->bi_bdev = dev0->rdev->bdev;
  66. bvm->bi_sector -= dev0->end_sector - dev0->rdev->sectors;
  67. maxbytes = min(maxbytes, subq->merge_bvec_fn(subq, bvm,
  68. biovec));
  69. }
  70. if (maxsectors < bio_sectors)
  71. maxsectors = 0;
  72. else
  73. maxsectors -= bio_sectors;
  74. if (maxsectors <= (PAGE_SIZE >> 9 ) && bio_sectors == 0)
  75. return maxbytes;
  76. if (maxsectors > (maxbytes >> 9))
  77. return maxbytes;
  78. else
  79. return maxsectors << 9;
  80. }
  81. static int linear_congested(struct mddev *mddev, int bits)
  82. {
  83. struct linear_conf *conf;
  84. int i, ret = 0;
  85. conf = mddev->private;
  86. for (i = 0; i < mddev->raid_disks && !ret ; i++) {
  87. struct request_queue *q = bdev_get_queue(conf->disks[i].rdev->bdev);
  88. ret |= bdi_congested(&q->backing_dev_info, bits);
  89. }
  90. return ret;
  91. }
  92. static sector_t linear_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  93. {
  94. struct linear_conf *conf;
  95. sector_t array_sectors;
  96. conf = mddev->private;
  97. WARN_ONCE(sectors || raid_disks,
  98. "%s does not support generic reshape\n", __func__);
  99. array_sectors = conf->array_sectors;
  100. return array_sectors;
  101. }
  102. static struct linear_conf *linear_conf(struct mddev *mddev, int raid_disks)
  103. {
  104. struct linear_conf *conf;
  105. struct md_rdev *rdev;
  106. int i, cnt;
  107. bool discard_supported = false;
  108. conf = kzalloc (sizeof (*conf) + raid_disks*sizeof(struct dev_info),
  109. GFP_KERNEL);
  110. if (!conf)
  111. return NULL;
  112. cnt = 0;
  113. conf->array_sectors = 0;
  114. rdev_for_each(rdev, mddev) {
  115. int j = rdev->raid_disk;
  116. struct dev_info *disk = conf->disks + j;
  117. sector_t sectors;
  118. if (j < 0 || j >= raid_disks || disk->rdev) {
  119. printk(KERN_ERR "md/linear:%s: disk numbering problem. Aborting!\n",
  120. mdname(mddev));
  121. goto out;
  122. }
  123. disk->rdev = rdev;
  124. if (mddev->chunk_sectors) {
  125. sectors = rdev->sectors;
  126. sector_div(sectors, mddev->chunk_sectors);
  127. rdev->sectors = sectors * mddev->chunk_sectors;
  128. }
  129. disk_stack_limits(mddev->gendisk, rdev->bdev,
  130. rdev->data_offset << 9);
  131. conf->array_sectors += rdev->sectors;
  132. cnt++;
  133. if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
  134. discard_supported = true;
  135. }
  136. if (cnt != raid_disks) {
  137. printk(KERN_ERR "md/linear:%s: not enough drives present. Aborting!\n",
  138. mdname(mddev));
  139. goto out;
  140. }
  141. if (!discard_supported)
  142. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
  143. else
  144. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
  145. /*
  146. * Here we calculate the device offsets.
  147. */
  148. conf->disks[0].end_sector = conf->disks[0].rdev->sectors;
  149. for (i = 1; i < raid_disks; i++)
  150. conf->disks[i].end_sector =
  151. conf->disks[i-1].end_sector +
  152. conf->disks[i].rdev->sectors;
  153. return conf;
  154. out:
  155. kfree(conf);
  156. return NULL;
  157. }
  158. static int linear_run (struct mddev *mddev)
  159. {
  160. struct linear_conf *conf;
  161. int ret;
  162. if (md_check_no_bitmap(mddev))
  163. return -EINVAL;
  164. conf = linear_conf(mddev, mddev->raid_disks);
  165. if (!conf)
  166. return 1;
  167. mddev->private = conf;
  168. md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
  169. ret = md_integrity_register(mddev);
  170. if (ret) {
  171. kfree(conf);
  172. mddev->private = NULL;
  173. }
  174. return ret;
  175. }
  176. static int linear_add(struct mddev *mddev, struct md_rdev *rdev)
  177. {
  178. /* Adding a drive to a linear array allows the array to grow.
  179. * It is permitted if the new drive has a matching superblock
  180. * already on it, with raid_disk equal to raid_disks.
  181. * It is achieved by creating a new linear_private_data structure
  182. * and swapping it in in-place of the current one.
  183. * The current one is never freed until the array is stopped.
  184. * This avoids races.
  185. */
  186. struct linear_conf *newconf, *oldconf;
  187. if (rdev->saved_raid_disk != mddev->raid_disks)
  188. return -EINVAL;
  189. rdev->raid_disk = rdev->saved_raid_disk;
  190. rdev->saved_raid_disk = -1;
  191. newconf = linear_conf(mddev,mddev->raid_disks+1);
  192. if (!newconf)
  193. return -ENOMEM;
  194. mddev_suspend(mddev);
  195. oldconf = mddev->private;
  196. mddev->raid_disks++;
  197. mddev->private = newconf;
  198. md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
  199. set_capacity(mddev->gendisk, mddev->array_sectors);
  200. mddev_resume(mddev);
  201. revalidate_disk(mddev->gendisk);
  202. kfree(oldconf);
  203. return 0;
  204. }
  205. static void linear_free(struct mddev *mddev, void *priv)
  206. {
  207. struct linear_conf *conf = priv;
  208. kfree(conf);
  209. }
  210. static void linear_make_request(struct mddev *mddev, struct bio *bio)
  211. {
  212. char b[BDEVNAME_SIZE];
  213. struct dev_info *tmp_dev;
  214. struct bio *split;
  215. sector_t start_sector, end_sector, data_offset;
  216. if (unlikely(bio->bi_rw & REQ_FLUSH)) {
  217. md_flush_request(mddev, bio);
  218. return;
  219. }
  220. do {
  221. tmp_dev = which_dev(mddev, bio->bi_iter.bi_sector);
  222. start_sector = tmp_dev->end_sector - tmp_dev->rdev->sectors;
  223. end_sector = tmp_dev->end_sector;
  224. data_offset = tmp_dev->rdev->data_offset;
  225. bio->bi_bdev = tmp_dev->rdev->bdev;
  226. if (unlikely(bio->bi_iter.bi_sector >= end_sector ||
  227. bio->bi_iter.bi_sector < start_sector))
  228. goto out_of_bounds;
  229. if (unlikely(bio_end_sector(bio) > end_sector)) {
  230. /* This bio crosses a device boundary, so we have to
  231. * split it.
  232. */
  233. split = bio_split(bio, end_sector -
  234. bio->bi_iter.bi_sector,
  235. GFP_NOIO, fs_bio_set);
  236. bio_chain(split, bio);
  237. } else {
  238. split = bio;
  239. }
  240. split->bi_iter.bi_sector = split->bi_iter.bi_sector -
  241. start_sector + data_offset;
  242. if (unlikely((split->bi_rw & REQ_DISCARD) &&
  243. !blk_queue_discard(bdev_get_queue(split->bi_bdev)))) {
  244. /* Just ignore it */
  245. bio_endio(split, 0);
  246. } else
  247. generic_make_request(split);
  248. } while (split != bio);
  249. return;
  250. out_of_bounds:
  251. printk(KERN_ERR
  252. "md/linear:%s: make_request: Sector %llu out of bounds on "
  253. "dev %s: %llu sectors, offset %llu\n",
  254. mdname(mddev),
  255. (unsigned long long)bio->bi_iter.bi_sector,
  256. bdevname(tmp_dev->rdev->bdev, b),
  257. (unsigned long long)tmp_dev->rdev->sectors,
  258. (unsigned long long)start_sector);
  259. bio_io_error(bio);
  260. }
  261. static void linear_status (struct seq_file *seq, struct mddev *mddev)
  262. {
  263. seq_printf(seq, " %dk rounding", mddev->chunk_sectors / 2);
  264. }
  265. static void linear_quiesce(struct mddev *mddev, int state)
  266. {
  267. }
  268. static struct md_personality linear_personality =
  269. {
  270. .name = "linear",
  271. .level = LEVEL_LINEAR,
  272. .owner = THIS_MODULE,
  273. .make_request = linear_make_request,
  274. .run = linear_run,
  275. .free = linear_free,
  276. .status = linear_status,
  277. .hot_add_disk = linear_add,
  278. .size = linear_size,
  279. .quiesce = linear_quiesce,
  280. .congested = linear_congested,
  281. .mergeable_bvec = linear_mergeable_bvec,
  282. };
  283. static int __init linear_init (void)
  284. {
  285. return register_md_personality (&linear_personality);
  286. }
  287. static void linear_exit (void)
  288. {
  289. unregister_md_personality (&linear_personality);
  290. }
  291. module_init(linear_init);
  292. module_exit(linear_exit);
  293. MODULE_LICENSE("GPL");
  294. MODULE_DESCRIPTION("Linear device concatenation personality for MD");
  295. MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
  296. MODULE_ALIAS("md-linear");
  297. MODULE_ALIAS("md-level--1");