linear.c 11 KB

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