linear.c 8.8 KB

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