linear.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. if (mddev_congested(mddev, bits))
  93. return 1;
  94. rcu_read_lock();
  95. conf = rcu_dereference(mddev->private);
  96. for (i = 0; i < mddev->raid_disks && !ret ; i++) {
  97. struct request_queue *q = bdev_get_queue(conf->disks[i].rdev->bdev);
  98. ret |= bdi_congested(&q->backing_dev_info, bits);
  99. }
  100. rcu_read_unlock();
  101. return ret;
  102. }
  103. static sector_t linear_size(mddev_t *mddev, sector_t sectors, int raid_disks)
  104. {
  105. linear_conf_t *conf;
  106. sector_t array_sectors;
  107. rcu_read_lock();
  108. conf = rcu_dereference(mddev->private);
  109. WARN_ONCE(sectors || raid_disks,
  110. "%s does not support generic reshape\n", __func__);
  111. array_sectors = conf->array_sectors;
  112. rcu_read_unlock();
  113. return array_sectors;
  114. }
  115. static linear_conf_t *linear_conf(mddev_t *mddev, int raid_disks)
  116. {
  117. linear_conf_t *conf;
  118. mdk_rdev_t *rdev;
  119. int i, cnt;
  120. conf = kzalloc (sizeof (*conf) + raid_disks*sizeof(dev_info_t),
  121. GFP_KERNEL);
  122. if (!conf)
  123. return NULL;
  124. cnt = 0;
  125. conf->array_sectors = 0;
  126. list_for_each_entry(rdev, &mddev->disks, same_set) {
  127. int j = rdev->raid_disk;
  128. dev_info_t *disk = conf->disks + j;
  129. sector_t sectors;
  130. if (j < 0 || j >= raid_disks || disk->rdev) {
  131. printk("linear: disk numbering problem. Aborting!\n");
  132. goto out;
  133. }
  134. disk->rdev = rdev;
  135. if (mddev->chunk_sectors) {
  136. sectors = rdev->sectors;
  137. sector_div(sectors, mddev->chunk_sectors);
  138. rdev->sectors = sectors * mddev->chunk_sectors;
  139. }
  140. disk_stack_limits(mddev->gendisk, rdev->bdev,
  141. rdev->data_offset << 9);
  142. /* as we don't honour merge_bvec_fn, we must never risk
  143. * violating it, so limit ->max_sector to one PAGE, as
  144. * a one page request is never in violation.
  145. */
  146. if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
  147. queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
  148. blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
  149. conf->array_sectors += rdev->sectors;
  150. cnt++;
  151. }
  152. if (cnt != raid_disks) {
  153. printk("linear: not enough drives present. Aborting!\n");
  154. goto out;
  155. }
  156. /*
  157. * Here we calculate the device offsets.
  158. */
  159. conf->disks[0].end_sector = conf->disks[0].rdev->sectors;
  160. for (i = 1; i < raid_disks; i++)
  161. conf->disks[i].end_sector =
  162. conf->disks[i-1].end_sector +
  163. conf->disks[i].rdev->sectors;
  164. return conf;
  165. out:
  166. kfree(conf);
  167. return NULL;
  168. }
  169. static int linear_run (mddev_t *mddev)
  170. {
  171. linear_conf_t *conf;
  172. if (md_check_no_bitmap(mddev))
  173. return -EINVAL;
  174. mddev->queue->queue_lock = &mddev->queue->__queue_lock;
  175. conf = linear_conf(mddev, mddev->raid_disks);
  176. if (!conf)
  177. return 1;
  178. mddev->private = conf;
  179. md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
  180. blk_queue_merge_bvec(mddev->queue, linear_mergeable_bvec);
  181. mddev->queue->unplug_fn = linear_unplug;
  182. mddev->queue->backing_dev_info.congested_fn = linear_congested;
  183. mddev->queue->backing_dev_info.congested_data = mddev;
  184. md_integrity_register(mddev);
  185. return 0;
  186. }
  187. static void free_conf(struct rcu_head *head)
  188. {
  189. linear_conf_t *conf = container_of(head, linear_conf_t, rcu);
  190. kfree(conf);
  191. }
  192. static int linear_add(mddev_t *mddev, mdk_rdev_t *rdev)
  193. {
  194. /* Adding a drive to a linear array allows the array to grow.
  195. * It is permitted if the new drive has a matching superblock
  196. * already on it, with raid_disk equal to raid_disks.
  197. * It is achieved by creating a new linear_private_data structure
  198. * and swapping it in in-place of the current one.
  199. * The current one is never freed until the array is stopped.
  200. * This avoids races.
  201. */
  202. linear_conf_t *newconf, *oldconf;
  203. if (rdev->saved_raid_disk != mddev->raid_disks)
  204. return -EINVAL;
  205. rdev->raid_disk = rdev->saved_raid_disk;
  206. newconf = linear_conf(mddev,mddev->raid_disks+1);
  207. if (!newconf)
  208. return -ENOMEM;
  209. oldconf = rcu_dereference(mddev->private);
  210. mddev->raid_disks++;
  211. rcu_assign_pointer(mddev->private, newconf);
  212. md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
  213. set_capacity(mddev->gendisk, mddev->array_sectors);
  214. revalidate_disk(mddev->gendisk);
  215. call_rcu(&oldconf->rcu, free_conf);
  216. return 0;
  217. }
  218. static int linear_stop (mddev_t *mddev)
  219. {
  220. linear_conf_t *conf = mddev->private;
  221. /*
  222. * We do not require rcu protection here since
  223. * we hold reconfig_mutex for both linear_add and
  224. * linear_stop, so they cannot race.
  225. * We should make sure any old 'conf's are properly
  226. * freed though.
  227. */
  228. rcu_barrier();
  229. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  230. kfree(conf);
  231. return 0;
  232. }
  233. static int linear_make_request (struct request_queue *q, struct bio *bio)
  234. {
  235. const int rw = bio_data_dir(bio);
  236. mddev_t *mddev = q->queuedata;
  237. dev_info_t *tmp_dev;
  238. sector_t start_sector;
  239. int cpu;
  240. if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
  241. md_barrier_request(mddev, bio);
  242. return 0;
  243. }
  244. cpu = part_stat_lock();
  245. part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
  246. part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
  247. bio_sectors(bio));
  248. part_stat_unlock();
  249. rcu_read_lock();
  250. tmp_dev = which_dev(mddev, bio->bi_sector);
  251. start_sector = tmp_dev->end_sector - tmp_dev->rdev->sectors;
  252. if (unlikely(bio->bi_sector >= (tmp_dev->end_sector)
  253. || (bio->bi_sector < start_sector))) {
  254. char b[BDEVNAME_SIZE];
  255. printk("linear_make_request: Sector %llu out of bounds on "
  256. "dev %s: %llu sectors, offset %llu\n",
  257. (unsigned long long)bio->bi_sector,
  258. bdevname(tmp_dev->rdev->bdev, b),
  259. (unsigned long long)tmp_dev->rdev->sectors,
  260. (unsigned long long)start_sector);
  261. rcu_read_unlock();
  262. bio_io_error(bio);
  263. return 0;
  264. }
  265. if (unlikely(bio->bi_sector + (bio->bi_size >> 9) >
  266. tmp_dev->end_sector)) {
  267. /* This bio crosses a device boundary, so we have to
  268. * split it.
  269. */
  270. struct bio_pair *bp;
  271. sector_t end_sector = tmp_dev->end_sector;
  272. rcu_read_unlock();
  273. bp = bio_split(bio, end_sector - bio->bi_sector);
  274. if (linear_make_request(q, &bp->bio1))
  275. generic_make_request(&bp->bio1);
  276. if (linear_make_request(q, &bp->bio2))
  277. generic_make_request(&bp->bio2);
  278. bio_pair_release(bp);
  279. return 0;
  280. }
  281. bio->bi_bdev = tmp_dev->rdev->bdev;
  282. bio->bi_sector = bio->bi_sector - start_sector
  283. + tmp_dev->rdev->data_offset;
  284. rcu_read_unlock();
  285. return 1;
  286. }
  287. static void linear_status (struct seq_file *seq, mddev_t *mddev)
  288. {
  289. seq_printf(seq, " %dk rounding", mddev->chunk_sectors / 2);
  290. }
  291. static struct mdk_personality linear_personality =
  292. {
  293. .name = "linear",
  294. .level = LEVEL_LINEAR,
  295. .owner = THIS_MODULE,
  296. .make_request = linear_make_request,
  297. .run = linear_run,
  298. .stop = linear_stop,
  299. .status = linear_status,
  300. .hot_add_disk = linear_add,
  301. .size = linear_size,
  302. };
  303. static int __init linear_init (void)
  304. {
  305. return register_md_personality (&linear_personality);
  306. }
  307. static void linear_exit (void)
  308. {
  309. unregister_md_personality (&linear_personality);
  310. }
  311. module_init(linear_init);
  312. module_exit(linear_exit);
  313. MODULE_LICENSE("GPL");
  314. MODULE_DESCRIPTION("Linear device concatenation personality for MD");
  315. MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
  316. MODULE_ALIAS("md-linear");
  317. MODULE_ALIAS("md-level--1");