linear.c 11 KB

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