linear.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 "md.h"
  19. #include "linear.h"
  20. /*
  21. * find which device holds a particular offset
  22. */
  23. static inline dev_info_t *which_dev(mddev_t *mddev, sector_t sector)
  24. {
  25. int lo, mid, hi;
  26. linear_conf_t *conf;
  27. lo = 0;
  28. hi = mddev->raid_disks - 1;
  29. conf = rcu_dereference(mddev->private);
  30. /*
  31. * Binary Search
  32. */
  33. while (hi > lo) {
  34. mid = (hi + lo) / 2;
  35. if (sector < conf->disks[mid].end_sector)
  36. hi = mid;
  37. else
  38. lo = mid + 1;
  39. }
  40. return conf->disks + lo;
  41. }
  42. /**
  43. * linear_mergeable_bvec -- tell bio layer if two requests can be merged
  44. * @q: request queue
  45. * @bvm: properties of new bio
  46. * @biovec: the request that could be merged to it.
  47. *
  48. * Return amount of bytes we can take at this offset
  49. */
  50. static int linear_mergeable_bvec(struct request_queue *q,
  51. struct bvec_merge_data *bvm,
  52. struct bio_vec *biovec)
  53. {
  54. mddev_t *mddev = q->queuedata;
  55. dev_info_t *dev0;
  56. unsigned long maxsectors, bio_sectors = bvm->bi_size >> 9;
  57. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  58. rcu_read_lock();
  59. dev0 = which_dev(mddev, sector);
  60. maxsectors = dev0->end_sector - sector;
  61. rcu_read_unlock();
  62. if (maxsectors < bio_sectors)
  63. maxsectors = 0;
  64. else
  65. maxsectors -= bio_sectors;
  66. if (maxsectors <= (PAGE_SIZE >> 9 ) && bio_sectors == 0)
  67. return biovec->bv_len;
  68. /* The bytes available at this offset could be really big,
  69. * so we cap at 2^31 to avoid overflow */
  70. if (maxsectors > (1 << (31-9)))
  71. return 1<<31;
  72. return maxsectors << 9;
  73. }
  74. static void linear_unplug(struct request_queue *q)
  75. {
  76. mddev_t *mddev = q->queuedata;
  77. linear_conf_t *conf;
  78. int i;
  79. rcu_read_lock();
  80. conf = rcu_dereference(mddev->private);
  81. for (i=0; i < mddev->raid_disks; i++) {
  82. struct request_queue *r_queue = bdev_get_queue(conf->disks[i].rdev->bdev);
  83. blk_unplug(r_queue);
  84. }
  85. rcu_read_unlock();
  86. }
  87. static int linear_congested(void *data, int bits)
  88. {
  89. mddev_t *mddev = data;
  90. linear_conf_t *conf;
  91. int i, ret = 0;
  92. rcu_read_lock();
  93. conf = rcu_dereference(mddev->private);
  94. for (i = 0; i < mddev->raid_disks && !ret ; i++) {
  95. struct request_queue *q = bdev_get_queue(conf->disks[i].rdev->bdev);
  96. ret |= bdi_congested(&q->backing_dev_info, bits);
  97. }
  98. rcu_read_unlock();
  99. return ret;
  100. }
  101. static sector_t linear_size(mddev_t *mddev, sector_t sectors, int raid_disks)
  102. {
  103. linear_conf_t *conf;
  104. sector_t array_sectors;
  105. rcu_read_lock();
  106. conf = rcu_dereference(mddev->private);
  107. WARN_ONCE(sectors || raid_disks,
  108. "%s does not support generic reshape\n", __func__);
  109. array_sectors = conf->array_sectors;
  110. rcu_read_unlock();
  111. return array_sectors;
  112. }
  113. static linear_conf_t *linear_conf(mddev_t *mddev, int raid_disks)
  114. {
  115. linear_conf_t *conf;
  116. mdk_rdev_t *rdev;
  117. int i, cnt;
  118. conf = kzalloc (sizeof (*conf) + raid_disks*sizeof(dev_info_t),
  119. GFP_KERNEL);
  120. if (!conf)
  121. return NULL;
  122. cnt = 0;
  123. conf->array_sectors = 0;
  124. list_for_each_entry(rdev, &mddev->disks, same_set) {
  125. int j = rdev->raid_disk;
  126. dev_info_t *disk = conf->disks + j;
  127. sector_t sectors;
  128. if (j < 0 || j >= raid_disks || disk->rdev) {
  129. printk("linear: disk numbering problem. Aborting!\n");
  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. /* as we don't honour merge_bvec_fn, we must never risk
  141. * violating it, so limit ->max_sector to one PAGE, as
  142. * a one page request is never in violation.
  143. */
  144. if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
  145. queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
  146. blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
  147. conf->array_sectors += rdev->sectors;
  148. cnt++;
  149. }
  150. if (cnt != raid_disks) {
  151. printk("linear: not enough drives present. Aborting!\n");
  152. goto out;
  153. }
  154. /*
  155. * Here we calculate the device offsets.
  156. */
  157. conf->disks[0].end_sector = conf->disks[0].rdev->sectors;
  158. for (i = 1; i < raid_disks; i++)
  159. conf->disks[i].end_sector =
  160. conf->disks[i-1].end_sector +
  161. conf->disks[i].rdev->sectors;
  162. return conf;
  163. out:
  164. kfree(conf);
  165. return NULL;
  166. }
  167. static int linear_run (mddev_t *mddev)
  168. {
  169. linear_conf_t *conf;
  170. if (md_check_no_bitmap(mddev))
  171. return -EINVAL;
  172. mddev->queue->queue_lock = &mddev->queue->__queue_lock;
  173. conf = linear_conf(mddev, mddev->raid_disks);
  174. if (!conf)
  175. return 1;
  176. mddev->private = conf;
  177. md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
  178. blk_queue_merge_bvec(mddev->queue, linear_mergeable_bvec);
  179. mddev->queue->unplug_fn = linear_unplug;
  180. mddev->queue->backing_dev_info.congested_fn = linear_congested;
  181. mddev->queue->backing_dev_info.congested_data = mddev;
  182. md_integrity_register(mddev);
  183. return 0;
  184. }
  185. static void free_conf(struct rcu_head *head)
  186. {
  187. linear_conf_t *conf = container_of(head, linear_conf_t, rcu);
  188. kfree(conf);
  189. }
  190. static int linear_add(mddev_t *mddev, mdk_rdev_t *rdev)
  191. {
  192. /* Adding a drive to a linear array allows the array to grow.
  193. * It is permitted if the new drive has a matching superblock
  194. * already on it, with raid_disk equal to raid_disks.
  195. * It is achieved by creating a new linear_private_data structure
  196. * and swapping it in in-place of the current one.
  197. * The current one is never freed until the array is stopped.
  198. * This avoids races.
  199. */
  200. linear_conf_t *newconf, *oldconf;
  201. if (rdev->saved_raid_disk != mddev->raid_disks)
  202. return -EINVAL;
  203. rdev->raid_disk = rdev->saved_raid_disk;
  204. newconf = linear_conf(mddev,mddev->raid_disks+1);
  205. if (!newconf)
  206. return -ENOMEM;
  207. oldconf = rcu_dereference(mddev->private);
  208. mddev->raid_disks++;
  209. rcu_assign_pointer(mddev->private, newconf);
  210. md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
  211. set_capacity(mddev->gendisk, mddev->array_sectors);
  212. revalidate_disk(mddev->gendisk);
  213. call_rcu(&oldconf->rcu, free_conf);
  214. return 0;
  215. }
  216. static int linear_stop (mddev_t *mddev)
  217. {
  218. linear_conf_t *conf = mddev->private;
  219. /*
  220. * We do not require rcu protection here since
  221. * we hold reconfig_mutex for both linear_add and
  222. * linear_stop, so they cannot race.
  223. * We should make sure any old 'conf's are properly
  224. * freed though.
  225. */
  226. rcu_barrier();
  227. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  228. kfree(conf);
  229. return 0;
  230. }
  231. static int linear_make_request (struct request_queue *q, struct bio *bio)
  232. {
  233. const int rw = bio_data_dir(bio);
  234. mddev_t *mddev = q->queuedata;
  235. dev_info_t *tmp_dev;
  236. sector_t start_sector;
  237. int cpu;
  238. if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
  239. bio_endio(bio, -EOPNOTSUPP);
  240. return 0;
  241. }
  242. cpu = part_stat_lock();
  243. part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
  244. part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
  245. bio_sectors(bio));
  246. part_stat_unlock();
  247. rcu_read_lock();
  248. tmp_dev = which_dev(mddev, bio->bi_sector);
  249. start_sector = tmp_dev->end_sector - tmp_dev->rdev->sectors;
  250. if (unlikely(bio->bi_sector >= (tmp_dev->end_sector)
  251. || (bio->bi_sector < start_sector))) {
  252. char b[BDEVNAME_SIZE];
  253. printk("linear_make_request: Sector %llu out of bounds on "
  254. "dev %s: %llu sectors, offset %llu\n",
  255. (unsigned long long)bio->bi_sector,
  256. bdevname(tmp_dev->rdev->bdev, b),
  257. (unsigned long long)tmp_dev->rdev->sectors,
  258. (unsigned long long)start_sector);
  259. rcu_read_unlock();
  260. bio_io_error(bio);
  261. return 0;
  262. }
  263. if (unlikely(bio->bi_sector + (bio->bi_size >> 9) >
  264. tmp_dev->end_sector)) {
  265. /* This bio crosses a device boundary, so we have to
  266. * split it.
  267. */
  268. struct bio_pair *bp;
  269. sector_t end_sector = tmp_dev->end_sector;
  270. rcu_read_unlock();
  271. bp = bio_split(bio, end_sector - bio->bi_sector);
  272. if (linear_make_request(q, &bp->bio1))
  273. generic_make_request(&bp->bio1);
  274. if (linear_make_request(q, &bp->bio2))
  275. generic_make_request(&bp->bio2);
  276. bio_pair_release(bp);
  277. return 0;
  278. }
  279. bio->bi_bdev = tmp_dev->rdev->bdev;
  280. bio->bi_sector = bio->bi_sector - start_sector
  281. + tmp_dev->rdev->data_offset;
  282. rcu_read_unlock();
  283. return 1;
  284. }
  285. static void linear_status (struct seq_file *seq, mddev_t *mddev)
  286. {
  287. seq_printf(seq, " %dk rounding", mddev->chunk_sectors / 2);
  288. }
  289. static struct mdk_personality linear_personality =
  290. {
  291. .name = "linear",
  292. .level = LEVEL_LINEAR,
  293. .owner = THIS_MODULE,
  294. .make_request = linear_make_request,
  295. .run = linear_run,
  296. .stop = linear_stop,
  297. .status = linear_status,
  298. .hot_add_disk = linear_add,
  299. .size = linear_size,
  300. };
  301. static int __init linear_init (void)
  302. {
  303. return register_md_personality (&linear_personality);
  304. }
  305. static void linear_exit (void)
  306. {
  307. unregister_md_personality (&linear_personality);
  308. }
  309. module_init(linear_init);
  310. module_exit(linear_exit);
  311. MODULE_LICENSE("GPL");
  312. MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
  313. MODULE_ALIAS("md-linear");
  314. MODULE_ALIAS("md-level--1");