linear.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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/raid/linear.h>
  16. /*
  17. * find which device holds a particular offset
  18. */
  19. static inline dev_info_t *which_dev(mddev_t *mddev, sector_t sector)
  20. {
  21. dev_info_t *hash;
  22. linear_conf_t *conf = mddev_to_conf(mddev);
  23. /*
  24. * sector_div(a,b) returns the remainer and sets a to a/b
  25. */
  26. sector >>= conf->sector_shift;
  27. (void)sector_div(sector, conf->spacing);
  28. hash = conf->hash_table[sector];
  29. while (sector >= hash->num_sectors + hash->start_sector)
  30. hash++;
  31. return hash;
  32. }
  33. /**
  34. * linear_mergeable_bvec -- tell bio layer if two requests can be merged
  35. * @q: request queue
  36. * @bvm: properties of new bio
  37. * @biovec: the request that could be merged to it.
  38. *
  39. * Return amount of bytes we can take at this offset
  40. */
  41. static int linear_mergeable_bvec(struct request_queue *q,
  42. struct bvec_merge_data *bvm,
  43. struct bio_vec *biovec)
  44. {
  45. mddev_t *mddev = q->queuedata;
  46. dev_info_t *dev0;
  47. unsigned long maxsectors, bio_sectors = bvm->bi_size >> 9;
  48. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  49. dev0 = which_dev(mddev, sector);
  50. maxsectors = dev0->num_sectors - (sector - dev0->start_sector);
  51. if (maxsectors < bio_sectors)
  52. maxsectors = 0;
  53. else
  54. maxsectors -= bio_sectors;
  55. if (maxsectors <= (PAGE_SIZE >> 9 ) && bio_sectors == 0)
  56. return biovec->bv_len;
  57. /* The bytes available at this offset could be really big,
  58. * so we cap at 2^31 to avoid overflow */
  59. if (maxsectors > (1 << (31-9)))
  60. return 1<<31;
  61. return maxsectors << 9;
  62. }
  63. static void linear_unplug(struct request_queue *q)
  64. {
  65. mddev_t *mddev = q->queuedata;
  66. linear_conf_t *conf = mddev_to_conf(mddev);
  67. int i;
  68. for (i=0; i < mddev->raid_disks; i++) {
  69. struct request_queue *r_queue = bdev_get_queue(conf->disks[i].rdev->bdev);
  70. blk_unplug(r_queue);
  71. }
  72. }
  73. static int linear_congested(void *data, int bits)
  74. {
  75. mddev_t *mddev = data;
  76. linear_conf_t *conf = mddev_to_conf(mddev);
  77. int i, ret = 0;
  78. for (i = 0; i < mddev->raid_disks && !ret ; i++) {
  79. struct request_queue *q = bdev_get_queue(conf->disks[i].rdev->bdev);
  80. ret |= bdi_congested(&q->backing_dev_info, bits);
  81. }
  82. return ret;
  83. }
  84. static linear_conf_t *linear_conf(mddev_t *mddev, int raid_disks)
  85. {
  86. linear_conf_t *conf;
  87. dev_info_t **table;
  88. mdk_rdev_t *rdev;
  89. int i, nb_zone, cnt;
  90. sector_t min_sectors;
  91. sector_t curr_sector;
  92. struct list_head *tmp;
  93. conf = kzalloc (sizeof (*conf) + raid_disks*sizeof(dev_info_t),
  94. GFP_KERNEL);
  95. if (!conf)
  96. return NULL;
  97. cnt = 0;
  98. conf->array_sectors = 0;
  99. rdev_for_each(rdev, tmp, mddev) {
  100. int j = rdev->raid_disk;
  101. dev_info_t *disk = conf->disks + j;
  102. if (j < 0 || j >= raid_disks || disk->rdev) {
  103. printk("linear: disk numbering problem. Aborting!\n");
  104. goto out;
  105. }
  106. disk->rdev = rdev;
  107. blk_queue_stack_limits(mddev->queue,
  108. rdev->bdev->bd_disk->queue);
  109. /* as we don't honour merge_bvec_fn, we must never risk
  110. * violating it, so limit ->max_sector to one PAGE, as
  111. * a one page request is never in violation.
  112. */
  113. if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
  114. mddev->queue->max_sectors > (PAGE_SIZE>>9))
  115. blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
  116. disk->num_sectors = rdev->size * 2;
  117. conf->array_sectors += rdev->size * 2;
  118. cnt++;
  119. }
  120. if (cnt != raid_disks) {
  121. printk("linear: not enough drives present. Aborting!\n");
  122. goto out;
  123. }
  124. min_sectors = conf->array_sectors;
  125. sector_div(min_sectors, PAGE_SIZE/sizeof(struct dev_info *));
  126. if (min_sectors == 0)
  127. min_sectors = 1;
  128. /* min_sectors is the minimum spacing that will fit the hash
  129. * table in one PAGE. This may be much smaller than needed.
  130. * We find the smallest non-terminal set of consecutive devices
  131. * that is larger than min_sectors and use the size of that as
  132. * the actual spacing
  133. */
  134. conf->spacing = conf->array_sectors;
  135. for (i=0; i < cnt-1 ; i++) {
  136. sector_t tmp = 0;
  137. int j;
  138. for (j = i; j < cnt - 1 && tmp < min_sectors; j++)
  139. tmp += conf->disks[j].num_sectors;
  140. if (tmp >= min_sectors && tmp < conf->spacing)
  141. conf->spacing = tmp;
  142. }
  143. /* spacing may be too large for sector_div to work with,
  144. * so we might need to pre-shift
  145. */
  146. conf->sector_shift = 0;
  147. if (sizeof(sector_t) > sizeof(u32)) {
  148. sector_t space = conf->spacing;
  149. while (space > (sector_t)(~(u32)0)) {
  150. space >>= 1;
  151. conf->sector_shift++;
  152. }
  153. }
  154. /*
  155. * This code was restructured to work around a gcc-2.95.3 internal
  156. * compiler error. Alter it with care.
  157. */
  158. {
  159. sector_t sz;
  160. unsigned round;
  161. unsigned long base;
  162. sz = conf->array_sectors >> conf->sector_shift;
  163. sz += 1; /* force round-up */
  164. base = conf->spacing >> conf->sector_shift;
  165. round = sector_div(sz, base);
  166. nb_zone = sz + (round ? 1 : 0);
  167. }
  168. BUG_ON(nb_zone > PAGE_SIZE / sizeof(struct dev_info *));
  169. conf->hash_table = kmalloc (sizeof (struct dev_info *) * nb_zone,
  170. GFP_KERNEL);
  171. if (!conf->hash_table)
  172. goto out;
  173. /*
  174. * Here we generate the linear hash table
  175. * First calculate the device offsets.
  176. */
  177. conf->disks[0].start_sector = 0;
  178. for (i = 1; i < raid_disks; i++)
  179. conf->disks[i].start_sector =
  180. conf->disks[i-1].start_sector +
  181. conf->disks[i-1].num_sectors;
  182. table = conf->hash_table;
  183. i = 0;
  184. for (curr_sector = 0;
  185. curr_sector < conf->array_sectors;
  186. curr_sector += conf->spacing) {
  187. while (i < raid_disks-1 &&
  188. curr_sector >= conf->disks[i+1].start_sector)
  189. i++;
  190. *table ++ = conf->disks + i;
  191. }
  192. if (conf->sector_shift) {
  193. conf->spacing >>= conf->sector_shift;
  194. /* round spacing up so that when we divide by it,
  195. * we err on the side of "too-low", which is safest.
  196. */
  197. conf->spacing++;
  198. }
  199. BUG_ON(table - conf->hash_table > nb_zone);
  200. return conf;
  201. out:
  202. kfree(conf);
  203. return NULL;
  204. }
  205. static int linear_run (mddev_t *mddev)
  206. {
  207. linear_conf_t *conf;
  208. mddev->queue->queue_lock = &mddev->queue->__queue_lock;
  209. conf = linear_conf(mddev, mddev->raid_disks);
  210. if (!conf)
  211. return 1;
  212. mddev->private = conf;
  213. mddev->array_sectors = conf->array_sectors;
  214. blk_queue_merge_bvec(mddev->queue, linear_mergeable_bvec);
  215. mddev->queue->unplug_fn = linear_unplug;
  216. mddev->queue->backing_dev_info.congested_fn = linear_congested;
  217. mddev->queue->backing_dev_info.congested_data = mddev;
  218. return 0;
  219. }
  220. static int linear_add(mddev_t *mddev, mdk_rdev_t *rdev)
  221. {
  222. /* Adding a drive to a linear array allows the array to grow.
  223. * It is permitted if the new drive has a matching superblock
  224. * already on it, with raid_disk equal to raid_disks.
  225. * It is achieved by creating a new linear_private_data structure
  226. * and swapping it in in-place of the current one.
  227. * The current one is never freed until the array is stopped.
  228. * This avoids races.
  229. */
  230. linear_conf_t *newconf;
  231. if (rdev->saved_raid_disk != mddev->raid_disks)
  232. return -EINVAL;
  233. rdev->raid_disk = rdev->saved_raid_disk;
  234. newconf = linear_conf(mddev,mddev->raid_disks+1);
  235. if (!newconf)
  236. return -ENOMEM;
  237. newconf->prev = mddev_to_conf(mddev);
  238. mddev->private = newconf;
  239. mddev->raid_disks++;
  240. mddev->array_sectors = newconf->array_sectors;
  241. set_capacity(mddev->gendisk, mddev->array_sectors);
  242. return 0;
  243. }
  244. static int linear_stop (mddev_t *mddev)
  245. {
  246. linear_conf_t *conf = mddev_to_conf(mddev);
  247. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  248. do {
  249. linear_conf_t *t = conf->prev;
  250. kfree(conf->hash_table);
  251. kfree(conf);
  252. conf = t;
  253. } while (conf);
  254. return 0;
  255. }
  256. static int linear_make_request (struct request_queue *q, struct bio *bio)
  257. {
  258. const int rw = bio_data_dir(bio);
  259. mddev_t *mddev = q->queuedata;
  260. dev_info_t *tmp_dev;
  261. int cpu;
  262. if (unlikely(bio_barrier(bio))) {
  263. bio_endio(bio, -EOPNOTSUPP);
  264. return 0;
  265. }
  266. cpu = part_stat_lock();
  267. part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
  268. part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
  269. bio_sectors(bio));
  270. part_stat_unlock();
  271. tmp_dev = which_dev(mddev, bio->bi_sector);
  272. if (unlikely(bio->bi_sector >= (tmp_dev->num_sectors +
  273. tmp_dev->start_sector)
  274. || (bio->bi_sector <
  275. tmp_dev->start_sector))) {
  276. char b[BDEVNAME_SIZE];
  277. printk("linear_make_request: Sector %llu out of bounds on "
  278. "dev %s: %llu sectors, offset %llu\n",
  279. (unsigned long long)bio->bi_sector,
  280. bdevname(tmp_dev->rdev->bdev, b),
  281. (unsigned long long)tmp_dev->num_sectors,
  282. (unsigned long long)tmp_dev->start_sector);
  283. bio_io_error(bio);
  284. return 0;
  285. }
  286. if (unlikely(bio->bi_sector + (bio->bi_size >> 9) >
  287. tmp_dev->start_sector + tmp_dev->num_sectors)) {
  288. /* This bio crosses a device boundary, so we have to
  289. * split it.
  290. */
  291. struct bio_pair *bp;
  292. bp = bio_split(bio,
  293. tmp_dev->start_sector + tmp_dev->num_sectors
  294. - bio->bi_sector);
  295. if (linear_make_request(q, &bp->bio1))
  296. generic_make_request(&bp->bio1);
  297. if (linear_make_request(q, &bp->bio2))
  298. generic_make_request(&bp->bio2);
  299. bio_pair_release(bp);
  300. return 0;
  301. }
  302. bio->bi_bdev = tmp_dev->rdev->bdev;
  303. bio->bi_sector = bio->bi_sector - tmp_dev->start_sector
  304. + tmp_dev->rdev->data_offset;
  305. return 1;
  306. }
  307. static void linear_status (struct seq_file *seq, mddev_t *mddev)
  308. {
  309. seq_printf(seq, " %dk rounding", mddev->chunk_size/1024);
  310. }
  311. static struct mdk_personality linear_personality =
  312. {
  313. .name = "linear",
  314. .level = LEVEL_LINEAR,
  315. .owner = THIS_MODULE,
  316. .make_request = linear_make_request,
  317. .run = linear_run,
  318. .stop = linear_stop,
  319. .status = linear_status,
  320. .hot_add_disk = linear_add,
  321. };
  322. static int __init linear_init (void)
  323. {
  324. return register_md_personality (&linear_personality);
  325. }
  326. static void linear_exit (void)
  327. {
  328. unregister_md_personality (&linear_personality);
  329. }
  330. module_init(linear_init);
  331. module_exit(linear_exit);
  332. MODULE_LICENSE("GPL");
  333. MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
  334. MODULE_ALIAS("md-linear");
  335. MODULE_ALIAS("md-level--1");