linear.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. return 0;
  183. }
  184. static void free_conf(struct rcu_head *head)
  185. {
  186. linear_conf_t *conf = container_of(head, linear_conf_t, rcu);
  187. kfree(conf);
  188. }
  189. static int linear_add(mddev_t *mddev, mdk_rdev_t *rdev)
  190. {
  191. /* Adding a drive to a linear array allows the array to grow.
  192. * It is permitted if the new drive has a matching superblock
  193. * already on it, with raid_disk equal to raid_disks.
  194. * It is achieved by creating a new linear_private_data structure
  195. * and swapping it in in-place of the current one.
  196. * The current one is never freed until the array is stopped.
  197. * This avoids races.
  198. */
  199. linear_conf_t *newconf, *oldconf;
  200. if (rdev->saved_raid_disk != mddev->raid_disks)
  201. return -EINVAL;
  202. rdev->raid_disk = rdev->saved_raid_disk;
  203. newconf = linear_conf(mddev,mddev->raid_disks+1);
  204. if (!newconf)
  205. return -ENOMEM;
  206. oldconf = rcu_dereference(mddev->private);
  207. mddev->raid_disks++;
  208. rcu_assign_pointer(mddev->private, newconf);
  209. md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
  210. set_capacity(mddev->gendisk, mddev->array_sectors);
  211. call_rcu(&oldconf->rcu, free_conf);
  212. return 0;
  213. }
  214. static int linear_stop (mddev_t *mddev)
  215. {
  216. linear_conf_t *conf = mddev->private;
  217. /*
  218. * We do not require rcu protection here since
  219. * we hold reconfig_mutex for both linear_add and
  220. * linear_stop, so they cannot race.
  221. * We should make sure any old 'conf's are properly
  222. * freed though.
  223. */
  224. rcu_barrier();
  225. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  226. kfree(conf);
  227. return 0;
  228. }
  229. static int linear_make_request (struct request_queue *q, struct bio *bio)
  230. {
  231. const int rw = bio_data_dir(bio);
  232. mddev_t *mddev = q->queuedata;
  233. dev_info_t *tmp_dev;
  234. sector_t start_sector;
  235. int cpu;
  236. if (unlikely(bio_barrier(bio))) {
  237. bio_endio(bio, -EOPNOTSUPP);
  238. return 0;
  239. }
  240. cpu = part_stat_lock();
  241. part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
  242. part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
  243. bio_sectors(bio));
  244. part_stat_unlock();
  245. rcu_read_lock();
  246. tmp_dev = which_dev(mddev, bio->bi_sector);
  247. start_sector = tmp_dev->end_sector - tmp_dev->rdev->sectors;
  248. if (unlikely(bio->bi_sector >= (tmp_dev->end_sector)
  249. || (bio->bi_sector < start_sector))) {
  250. char b[BDEVNAME_SIZE];
  251. printk("linear_make_request: Sector %llu out of bounds on "
  252. "dev %s: %llu sectors, offset %llu\n",
  253. (unsigned long long)bio->bi_sector,
  254. bdevname(tmp_dev->rdev->bdev, b),
  255. (unsigned long long)tmp_dev->rdev->sectors,
  256. (unsigned long long)start_sector);
  257. rcu_read_unlock();
  258. bio_io_error(bio);
  259. return 0;
  260. }
  261. if (unlikely(bio->bi_sector + (bio->bi_size >> 9) >
  262. tmp_dev->end_sector)) {
  263. /* This bio crosses a device boundary, so we have to
  264. * split it.
  265. */
  266. struct bio_pair *bp;
  267. sector_t end_sector = tmp_dev->end_sector;
  268. rcu_read_unlock();
  269. bp = bio_split(bio, end_sector - bio->bi_sector);
  270. if (linear_make_request(q, &bp->bio1))
  271. generic_make_request(&bp->bio1);
  272. if (linear_make_request(q, &bp->bio2))
  273. generic_make_request(&bp->bio2);
  274. bio_pair_release(bp);
  275. return 0;
  276. }
  277. bio->bi_bdev = tmp_dev->rdev->bdev;
  278. bio->bi_sector = bio->bi_sector - start_sector
  279. + tmp_dev->rdev->data_offset;
  280. rcu_read_unlock();
  281. return 1;
  282. }
  283. static void linear_status (struct seq_file *seq, mddev_t *mddev)
  284. {
  285. seq_printf(seq, " %dk rounding", mddev->chunk_sectors / 2);
  286. }
  287. static struct mdk_personality linear_personality =
  288. {
  289. .name = "linear",
  290. .level = LEVEL_LINEAR,
  291. .owner = THIS_MODULE,
  292. .make_request = linear_make_request,
  293. .run = linear_run,
  294. .stop = linear_stop,
  295. .status = linear_status,
  296. .hot_add_disk = linear_add,
  297. .size = linear_size,
  298. };
  299. static int __init linear_init (void)
  300. {
  301. return register_md_personality (&linear_personality);
  302. }
  303. static void linear_exit (void)
  304. {
  305. unregister_md_personality (&linear_personality);
  306. }
  307. module_init(linear_init);
  308. module_exit(linear_exit);
  309. MODULE_LICENSE("GPL");
  310. MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
  311. MODULE_ALIAS("md-linear");
  312. MODULE_ALIAS("md-level--1");