linear.c 11 KB

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