PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/md/linear.c

https://github.com/mpalmer/linux-2.6
C | 401 lines | 268 code | 65 blank | 68 comment | 39 complexity | fdd1e2408667e4c2581855b49899ae9d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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. /* min_sectors is the minimum spacing that will fit the hash
  127. * table in one PAGE. This may be much smaller than needed.
  128. * We find the smallest non-terminal set of consecutive devices
  129. * that is larger than min_sectors and use the size of that as
  130. * the actual spacing
  131. */
  132. conf->spacing = conf->array_sectors;
  133. for (i=0; i < cnt-1 ; i++) {
  134. sector_t tmp = 0;
  135. int j;
  136. for (j = i; j < cnt - 1 && tmp < min_sectors; j++)
  137. tmp += conf->disks[j].num_sectors;
  138. if (tmp >= min_sectors && tmp < conf->spacing)
  139. conf->spacing = tmp;
  140. }
  141. /* spacing may be too large for sector_div to work with,
  142. * so we might need to pre-shift
  143. */
  144. conf->sector_shift = 0;
  145. if (sizeof(sector_t) > sizeof(u32)) {
  146. sector_t space = conf->spacing;
  147. while (space > (sector_t)(~(u32)0)) {
  148. space >>= 1;
  149. conf->sector_shift++;
  150. }
  151. }
  152. /*
  153. * This code was restructured to work around a gcc-2.95.3 internal
  154. * compiler error. Alter it with care.
  155. */
  156. {
  157. sector_t sz;
  158. unsigned round;
  159. unsigned long base;
  160. sz = conf->array_sectors >> conf->sector_shift;
  161. sz += 1; /* force round-up */
  162. base = conf->spacing >> conf->sector_shift;
  163. round = sector_div(sz, base);
  164. nb_zone = sz + (round ? 1 : 0);
  165. }
  166. BUG_ON(nb_zone > PAGE_SIZE / sizeof(struct dev_info *));
  167. conf->hash_table = kmalloc (sizeof (struct dev_info *) * nb_zone,
  168. GFP_KERNEL);
  169. if (!conf->hash_table)
  170. goto out;
  171. /*
  172. * Here we generate the linear hash table
  173. * First calculate the device offsets.
  174. */
  175. conf->disks[0].start_sector = 0;
  176. for (i = 1; i < raid_disks; i++)
  177. conf->disks[i].start_sector =
  178. conf->disks[i-1].start_sector +
  179. conf->disks[i-1].num_sectors;
  180. table = conf->hash_table;
  181. i = 0;
  182. for (curr_sector = 0;
  183. curr_sector < conf->array_sectors;
  184. curr_sector += conf->spacing) {
  185. while (i < raid_disks-1 &&
  186. curr_sector >= conf->disks[i+1].start_sector)
  187. i++;
  188. *table ++ = conf->disks + i;
  189. }
  190. if (conf->sector_shift) {
  191. conf->spacing >>= conf->sector_shift;
  192. /* round spacing up so that when we divide by it,
  193. * we err on the side of "too-low", which is safest.
  194. */
  195. conf->spacing++;
  196. }
  197. BUG_ON(table - conf->hash_table > nb_zone);
  198. return conf;
  199. out:
  200. kfree(conf);
  201. return NULL;
  202. }
  203. static int linear_run (mddev_t *mddev)
  204. {
  205. linear_conf_t *conf;
  206. mddev->queue->queue_lock = &mddev->queue->__queue_lock;
  207. conf = linear_conf(mddev, mddev->raid_disks);
  208. if (!conf)
  209. return 1;
  210. mddev->private = conf;
  211. mddev->array_sectors = conf->array_sectors;
  212. blk_queue_merge_bvec(mddev->queue, linear_mergeable_bvec);
  213. mddev->queue->unplug_fn = linear_unplug;
  214. mddev->queue->backing_dev_info.congested_fn = linear_congested;
  215. mddev->queue->backing_dev_info.congested_data = mddev;
  216. return 0;
  217. }
  218. static int linear_add(mddev_t *mddev, mdk_rdev_t *rdev)
  219. {
  220. /* Adding a drive to a linear array allows the array to grow.
  221. * It is permitted if the new drive has a matching superblock
  222. * already on it, with raid_disk equal to raid_disks.
  223. * It is achieved by creating a new linear_private_data structure
  224. * and swapping it in in-place of the current one.
  225. * The current one is never freed until the array is stopped.
  226. * This avoids races.
  227. */
  228. linear_conf_t *newconf;
  229. if (rdev->saved_raid_disk != mddev->raid_disks)
  230. return -EINVAL;
  231. rdev->raid_disk = rdev->saved_raid_disk;
  232. newconf = linear_conf(mddev,mddev->raid_disks+1);
  233. if (!newconf)
  234. return -ENOMEM;
  235. newconf->prev = mddev_to_conf(mddev);
  236. mddev->private = newconf;
  237. mddev->raid_disks++;
  238. mddev->array_sectors = newconf->array_sectors;
  239. set_capacity(mddev->gendisk, mddev->array_sectors);
  240. return 0;
  241. }
  242. static int linear_stop (mddev_t *mddev)
  243. {
  244. linear_conf_t *conf = mddev_to_conf(mddev);
  245. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  246. do {
  247. linear_conf_t *t = conf->prev;
  248. kfree(conf->hash_table);
  249. kfree(conf);
  250. conf = t;
  251. } while (conf);
  252. return 0;
  253. }
  254. static int linear_make_request (struct request_queue *q, struct bio *bio)
  255. {
  256. const int rw = bio_data_dir(bio);
  257. mddev_t *mddev = q->queuedata;
  258. dev_info_t *tmp_dev;
  259. int cpu;
  260. if (unlikely(bio_barrier(bio))) {
  261. bio_endio(bio, -EOPNOTSUPP);
  262. return 0;
  263. }
  264. cpu = part_stat_lock();
  265. part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
  266. part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
  267. bio_sectors(bio));
  268. part_stat_unlock();
  269. tmp_dev = which_dev(mddev, bio->bi_sector);
  270. if (unlikely(bio->bi_sector >= (tmp_dev->num_sectors +
  271. tmp_dev->start_sector)
  272. || (bio->bi_sector <
  273. tmp_dev->start_sector))) {
  274. char b[BDEVNAME_SIZE];
  275. printk("linear_make_request: Sector %llu out of bounds on "
  276. "dev %s: %llu sectors, offset %llu\n",
  277. (unsigned long long)bio->bi_sector,
  278. bdevname(tmp_dev->rdev->bdev, b),
  279. (unsigned long long)tmp_dev->num_sectors,
  280. (unsigned long long)tmp_dev->start_sector);
  281. bio_io_error(bio);
  282. return 0;
  283. }
  284. if (unlikely(bio->bi_sector + (bio->bi_size >> 9) >
  285. tmp_dev->start_sector + tmp_dev->num_sectors)) {
  286. /* This bio crosses a device boundary, so we have to
  287. * split it.
  288. */
  289. struct bio_pair *bp;
  290. bp = bio_split(bio,
  291. tmp_dev->start_sector + tmp_dev->num_sectors
  292. - bio->bi_sector);
  293. if (linear_make_request(q, &bp->bio1))
  294. generic_make_request(&bp->bio1);
  295. if (linear_make_request(q, &bp->bio2))
  296. generic_make_request(&bp->bio2);
  297. bio_pair_release(bp);
  298. return 0;
  299. }
  300. bio->bi_bdev = tmp_dev->rdev->bdev;
  301. bio->bi_sector = bio->bi_sector - tmp_dev->start_sector
  302. + tmp_dev->rdev->data_offset;
  303. return 1;
  304. }
  305. static void linear_status (struct seq_file *seq, mddev_t *mddev)
  306. {
  307. seq_printf(seq, " %dk rounding", mddev->chunk_size/1024);
  308. }
  309. static struct mdk_personality linear_personality =
  310. {
  311. .name = "linear",
  312. .level = LEVEL_LINEAR,
  313. .owner = THIS_MODULE,
  314. .make_request = linear_make_request,
  315. .run = linear_run,
  316. .stop = linear_stop,
  317. .status = linear_status,
  318. .hot_add_disk = linear_add,
  319. };
  320. static int __init linear_init (void)
  321. {
  322. return register_md_personality (&linear_personality);
  323. }
  324. static void linear_exit (void)
  325. {
  326. unregister_md_personality (&linear_personality);
  327. }
  328. module_init(linear_init);
  329. module_exit(linear_exit);
  330. MODULE_LICENSE("GPL");
  331. MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
  332. MODULE_ALIAS("md-linear");
  333. MODULE_ALIAS("md-level--1");