linear.c 8.1 KB

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