linear.c 8.9 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/slab.h>
  19. #include "md.h"
  20. #include "linear.h"
  21. /*
  22. * find which device holds a particular offset
  23. */
  24. static inline dev_info_t *which_dev(mddev_t *mddev, sector_t sector)
  25. {
  26. int lo, mid, hi;
  27. linear_conf_t *conf;
  28. lo = 0;
  29. hi = mddev->raid_disks - 1;
  30. conf = rcu_dereference(mddev->private);
  31. /*
  32. * Binary Search
  33. */
  34. while (hi > lo) {
  35. mid = (hi + lo) / 2;
  36. if (sector < conf->disks[mid].end_sector)
  37. hi = mid;
  38. else
  39. lo = mid + 1;
  40. }
  41. return conf->disks + lo;
  42. }
  43. /**
  44. * linear_mergeable_bvec -- tell bio layer if two requests can be merged
  45. * @q: request queue
  46. * @bvm: properties of new bio
  47. * @biovec: the request that could be merged to it.
  48. *
  49. * Return amount of bytes we can take at this offset
  50. */
  51. static int linear_mergeable_bvec(struct request_queue *q,
  52. struct bvec_merge_data *bvm,
  53. struct bio_vec *biovec)
  54. {
  55. mddev_t *mddev = q->queuedata;
  56. dev_info_t *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. rcu_read_lock();
  60. dev0 = which_dev(mddev, sector);
  61. maxsectors = dev0->end_sector - sector;
  62. rcu_read_unlock();
  63. if (maxsectors < bio_sectors)
  64. maxsectors = 0;
  65. else
  66. maxsectors -= bio_sectors;
  67. if (maxsectors <= (PAGE_SIZE >> 9 ) && bio_sectors == 0)
  68. return biovec->bv_len;
  69. /* The bytes available at this offset could be really big,
  70. * so we cap at 2^31 to avoid overflow */
  71. if (maxsectors > (1 << (31-9)))
  72. return 1<<31;
  73. return maxsectors << 9;
  74. }
  75. static int linear_congested(void *data, int bits)
  76. {
  77. mddev_t *mddev = data;
  78. linear_conf_t *conf;
  79. int i, ret = 0;
  80. if (mddev_congested(mddev, bits))
  81. return 1;
  82. rcu_read_lock();
  83. conf = rcu_dereference(mddev->private);
  84. for (i = 0; i < mddev->raid_disks && !ret ; i++) {
  85. struct request_queue *q = bdev_get_queue(conf->disks[i].rdev->bdev);
  86. ret |= bdi_congested(&q->backing_dev_info, bits);
  87. }
  88. rcu_read_unlock();
  89. return ret;
  90. }
  91. static sector_t linear_size(mddev_t *mddev, sector_t sectors, int raid_disks)
  92. {
  93. linear_conf_t *conf;
  94. sector_t array_sectors;
  95. rcu_read_lock();
  96. conf = rcu_dereference(mddev->private);
  97. WARN_ONCE(sectors || raid_disks,
  98. "%s does not support generic reshape\n", __func__);
  99. array_sectors = conf->array_sectors;
  100. rcu_read_unlock();
  101. return array_sectors;
  102. }
  103. static linear_conf_t *linear_conf(mddev_t *mddev, int raid_disks)
  104. {
  105. linear_conf_t *conf;
  106. mdk_rdev_t *rdev;
  107. int i, cnt;
  108. conf = kzalloc (sizeof (*conf) + raid_disks*sizeof(dev_info_t),
  109. GFP_KERNEL);
  110. if (!conf)
  111. return NULL;
  112. cnt = 0;
  113. conf->array_sectors = 0;
  114. list_for_each_entry(rdev, &mddev->disks, same_set) {
  115. int j = rdev->raid_disk;
  116. dev_info_t *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. /* as we don't honour merge_bvec_fn, we must never risk
  132. * violating it, so limit max_segments to 1 lying within
  133. * a single page.
  134. */
  135. if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
  136. blk_queue_max_segments(mddev->queue, 1);
  137. blk_queue_segment_boundary(mddev->queue,
  138. PAGE_CACHE_SIZE - 1);
  139. }
  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 (mddev_t *mddev)
  162. {
  163. linear_conf_t *conf;
  164. if (md_check_no_bitmap(mddev))
  165. return -EINVAL;
  166. conf = linear_conf(mddev, mddev->raid_disks);
  167. if (!conf)
  168. return 1;
  169. mddev->private = conf;
  170. md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
  171. blk_queue_merge_bvec(mddev->queue, linear_mergeable_bvec);
  172. mddev->queue->backing_dev_info.congested_fn = linear_congested;
  173. mddev->queue->backing_dev_info.congested_data = mddev;
  174. return md_integrity_register(mddev);
  175. }
  176. static void free_conf(struct rcu_head *head)
  177. {
  178. linear_conf_t *conf = container_of(head, linear_conf_t, rcu);
  179. kfree(conf);
  180. }
  181. static int linear_add(mddev_t *mddev, mdk_rdev_t *rdev)
  182. {
  183. /* Adding a drive to a linear array allows the array to grow.
  184. * It is permitted if the new drive has a matching superblock
  185. * already on it, with raid_disk equal to raid_disks.
  186. * It is achieved by creating a new linear_private_data structure
  187. * and swapping it in in-place of the current one.
  188. * The current one is never freed until the array is stopped.
  189. * This avoids races.
  190. */
  191. linear_conf_t *newconf, *oldconf;
  192. if (rdev->saved_raid_disk != mddev->raid_disks)
  193. return -EINVAL;
  194. rdev->raid_disk = rdev->saved_raid_disk;
  195. newconf = linear_conf(mddev,mddev->raid_disks+1);
  196. if (!newconf)
  197. return -ENOMEM;
  198. oldconf = rcu_dereference(mddev->private);
  199. mddev->raid_disks++;
  200. rcu_assign_pointer(mddev->private, newconf);
  201. md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
  202. set_capacity(mddev->gendisk, mddev->array_sectors);
  203. revalidate_disk(mddev->gendisk);
  204. call_rcu(&oldconf->rcu, free_conf);
  205. return 0;
  206. }
  207. static int linear_stop (mddev_t *mddev)
  208. {
  209. linear_conf_t *conf = mddev->private;
  210. /*
  211. * We do not require rcu protection here since
  212. * we hold reconfig_mutex for both linear_add and
  213. * linear_stop, so they cannot race.
  214. * We should make sure any old 'conf's are properly
  215. * freed though.
  216. */
  217. rcu_barrier();
  218. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  219. kfree(conf);
  220. mddev->private = NULL;
  221. return 0;
  222. }
  223. static int linear_make_request (mddev_t *mddev, struct bio *bio)
  224. {
  225. dev_info_t *tmp_dev;
  226. sector_t start_sector;
  227. if (unlikely(bio->bi_rw & REQ_FLUSH)) {
  228. md_flush_request(mddev, bio);
  229. return 0;
  230. }
  231. rcu_read_lock();
  232. tmp_dev = which_dev(mddev, bio->bi_sector);
  233. start_sector = tmp_dev->end_sector - tmp_dev->rdev->sectors;
  234. if (unlikely(bio->bi_sector >= (tmp_dev->end_sector)
  235. || (bio->bi_sector < start_sector))) {
  236. char b[BDEVNAME_SIZE];
  237. printk(KERN_ERR
  238. "md/linear:%s: make_request: Sector %llu out of bounds on "
  239. "dev %s: %llu sectors, offset %llu\n",
  240. mdname(mddev),
  241. (unsigned long long)bio->bi_sector,
  242. bdevname(tmp_dev->rdev->bdev, b),
  243. (unsigned long long)tmp_dev->rdev->sectors,
  244. (unsigned long long)start_sector);
  245. rcu_read_unlock();
  246. bio_io_error(bio);
  247. return 0;
  248. }
  249. if (unlikely(bio->bi_sector + (bio->bi_size >> 9) >
  250. tmp_dev->end_sector)) {
  251. /* This bio crosses a device boundary, so we have to
  252. * split it.
  253. */
  254. struct bio_pair *bp;
  255. sector_t end_sector = tmp_dev->end_sector;
  256. rcu_read_unlock();
  257. bp = bio_split(bio, end_sector - bio->bi_sector);
  258. if (linear_make_request(mddev, &bp->bio1))
  259. generic_make_request(&bp->bio1);
  260. if (linear_make_request(mddev, &bp->bio2))
  261. generic_make_request(&bp->bio2);
  262. bio_pair_release(bp);
  263. return 0;
  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. return 1;
  270. }
  271. static void linear_status (struct seq_file *seq, mddev_t *mddev)
  272. {
  273. seq_printf(seq, " %dk rounding", mddev->chunk_sectors / 2);
  274. }
  275. static struct mdk_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");