multipath.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * multipath.c : Multiple Devices driver for Linux
  3. *
  4. * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
  5. *
  6. * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
  7. *
  8. * MULTIPATH management functions.
  9. *
  10. * derived from raid1.c.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * (for example /usr/src/linux/COPYING); if not, write to the Free
  19. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/blkdev.h>
  22. #include <linux/raid/md_u.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/slab.h>
  25. #include "md.h"
  26. #include "multipath.h"
  27. #define MAX_WORK_PER_DISK 128
  28. #define NR_RESERVED_BUFS 32
  29. static int multipath_map (multipath_conf_t *conf)
  30. {
  31. int i, disks = conf->raid_disks;
  32. /*
  33. * Later we do read balancing on the read side
  34. * now we use the first available disk.
  35. */
  36. rcu_read_lock();
  37. for (i = 0; i < disks; i++) {
  38. mdk_rdev_t *rdev = rcu_dereference(conf->multipaths[i].rdev);
  39. if (rdev && test_bit(In_sync, &rdev->flags)) {
  40. atomic_inc(&rdev->nr_pending);
  41. rcu_read_unlock();
  42. return i;
  43. }
  44. }
  45. rcu_read_unlock();
  46. printk(KERN_ERR "multipath_map(): no more operational IO paths?\n");
  47. return (-1);
  48. }
  49. static void multipath_reschedule_retry (struct multipath_bh *mp_bh)
  50. {
  51. unsigned long flags;
  52. mddev_t *mddev = mp_bh->mddev;
  53. multipath_conf_t *conf = mddev->private;
  54. spin_lock_irqsave(&conf->device_lock, flags);
  55. list_add(&mp_bh->retry_list, &conf->retry_list);
  56. spin_unlock_irqrestore(&conf->device_lock, flags);
  57. md_wakeup_thread(mddev->thread);
  58. }
  59. /*
  60. * multipath_end_bh_io() is called when we have finished servicing a multipathed
  61. * operation and are ready to return a success/failure code to the buffer
  62. * cache layer.
  63. */
  64. static void multipath_end_bh_io (struct multipath_bh *mp_bh, int err)
  65. {
  66. struct bio *bio = mp_bh->master_bio;
  67. multipath_conf_t *conf = mp_bh->mddev->private;
  68. bio_endio(bio, err);
  69. mempool_free(mp_bh, conf->pool);
  70. }
  71. static void multipath_end_request(struct bio *bio, int error)
  72. {
  73. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  74. struct multipath_bh * mp_bh = (struct multipath_bh *)(bio->bi_private);
  75. multipath_conf_t *conf = mp_bh->mddev->private;
  76. mdk_rdev_t *rdev = conf->multipaths[mp_bh->path].rdev;
  77. if (uptodate)
  78. multipath_end_bh_io(mp_bh, 0);
  79. else if (!bio_rw_flagged(bio, BIO_RW_AHEAD)) {
  80. /*
  81. * oops, IO error:
  82. */
  83. char b[BDEVNAME_SIZE];
  84. md_error (mp_bh->mddev, rdev);
  85. printk(KERN_ERR "multipath: %s: rescheduling sector %llu\n",
  86. bdevname(rdev->bdev,b),
  87. (unsigned long long)bio->bi_sector);
  88. multipath_reschedule_retry(mp_bh);
  89. } else
  90. multipath_end_bh_io(mp_bh, error);
  91. rdev_dec_pending(rdev, conf->mddev);
  92. }
  93. static void unplug_slaves(mddev_t *mddev)
  94. {
  95. multipath_conf_t *conf = mddev->private;
  96. int i;
  97. rcu_read_lock();
  98. for (i=0; i<mddev->raid_disks; i++) {
  99. mdk_rdev_t *rdev = rcu_dereference(conf->multipaths[i].rdev);
  100. if (rdev && !test_bit(Faulty, &rdev->flags)
  101. && atomic_read(&rdev->nr_pending)) {
  102. struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
  103. atomic_inc(&rdev->nr_pending);
  104. rcu_read_unlock();
  105. blk_unplug(r_queue);
  106. rdev_dec_pending(rdev, mddev);
  107. rcu_read_lock();
  108. }
  109. }
  110. rcu_read_unlock();
  111. }
  112. static void multipath_unplug(struct request_queue *q)
  113. {
  114. unplug_slaves(q->queuedata);
  115. }
  116. static int multipath_make_request (struct request_queue *q, struct bio * bio)
  117. {
  118. mddev_t *mddev = q->queuedata;
  119. multipath_conf_t *conf = mddev->private;
  120. struct multipath_bh * mp_bh;
  121. struct multipath_info *multipath;
  122. const int rw = bio_data_dir(bio);
  123. int cpu;
  124. if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
  125. md_barrier_request(mddev, bio);
  126. return 0;
  127. }
  128. mp_bh = mempool_alloc(conf->pool, GFP_NOIO);
  129. mp_bh->master_bio = bio;
  130. mp_bh->mddev = mddev;
  131. cpu = part_stat_lock();
  132. part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
  133. part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
  134. bio_sectors(bio));
  135. part_stat_unlock();
  136. mp_bh->path = multipath_map(conf);
  137. if (mp_bh->path < 0) {
  138. bio_endio(bio, -EIO);
  139. mempool_free(mp_bh, conf->pool);
  140. return 0;
  141. }
  142. multipath = conf->multipaths + mp_bh->path;
  143. mp_bh->bio = *bio;
  144. mp_bh->bio.bi_sector += multipath->rdev->data_offset;
  145. mp_bh->bio.bi_bdev = multipath->rdev->bdev;
  146. mp_bh->bio.bi_rw |= (1 << BIO_RW_FAILFAST_TRANSPORT);
  147. mp_bh->bio.bi_end_io = multipath_end_request;
  148. mp_bh->bio.bi_private = mp_bh;
  149. generic_make_request(&mp_bh->bio);
  150. return 0;
  151. }
  152. static void multipath_status (struct seq_file *seq, mddev_t *mddev)
  153. {
  154. multipath_conf_t *conf = mddev->private;
  155. int i;
  156. seq_printf (seq, " [%d/%d] [", conf->raid_disks,
  157. conf->working_disks);
  158. for (i = 0; i < conf->raid_disks; i++)
  159. seq_printf (seq, "%s",
  160. conf->multipaths[i].rdev &&
  161. test_bit(In_sync, &conf->multipaths[i].rdev->flags) ? "U" : "_");
  162. seq_printf (seq, "]");
  163. }
  164. static int multipath_congested(void *data, int bits)
  165. {
  166. mddev_t *mddev = data;
  167. multipath_conf_t *conf = mddev->private;
  168. int i, ret = 0;
  169. if (mddev_congested(mddev, bits))
  170. return 1;
  171. rcu_read_lock();
  172. for (i = 0; i < mddev->raid_disks ; i++) {
  173. mdk_rdev_t *rdev = rcu_dereference(conf->multipaths[i].rdev);
  174. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  175. struct request_queue *q = bdev_get_queue(rdev->bdev);
  176. ret |= bdi_congested(&q->backing_dev_info, bits);
  177. /* Just like multipath_map, we just check the
  178. * first available device
  179. */
  180. break;
  181. }
  182. }
  183. rcu_read_unlock();
  184. return ret;
  185. }
  186. /*
  187. * Careful, this can execute in IRQ contexts as well!
  188. */
  189. static void multipath_error (mddev_t *mddev, mdk_rdev_t *rdev)
  190. {
  191. multipath_conf_t *conf = mddev->private;
  192. if (conf->working_disks <= 1) {
  193. /*
  194. * Uh oh, we can do nothing if this is our last path, but
  195. * first check if this is a queued request for a device
  196. * which has just failed.
  197. */
  198. printk(KERN_ALERT
  199. "multipath: only one IO path left and IO error.\n");
  200. /* leave it active... it's all we have */
  201. } else {
  202. /*
  203. * Mark disk as unusable
  204. */
  205. if (!test_bit(Faulty, &rdev->flags)) {
  206. char b[BDEVNAME_SIZE];
  207. clear_bit(In_sync, &rdev->flags);
  208. set_bit(Faulty, &rdev->flags);
  209. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  210. conf->working_disks--;
  211. mddev->degraded++;
  212. printk(KERN_ALERT "multipath: IO failure on %s,"
  213. " disabling IO path.\n"
  214. "multipath: Operation continuing"
  215. " on %d IO paths.\n",
  216. bdevname (rdev->bdev,b),
  217. conf->working_disks);
  218. }
  219. }
  220. }
  221. static void print_multipath_conf (multipath_conf_t *conf)
  222. {
  223. int i;
  224. struct multipath_info *tmp;
  225. printk("MULTIPATH conf printout:\n");
  226. if (!conf) {
  227. printk("(conf==NULL)\n");
  228. return;
  229. }
  230. printk(" --- wd:%d rd:%d\n", conf->working_disks,
  231. conf->raid_disks);
  232. for (i = 0; i < conf->raid_disks; i++) {
  233. char b[BDEVNAME_SIZE];
  234. tmp = conf->multipaths + i;
  235. if (tmp->rdev)
  236. printk(" disk%d, o:%d, dev:%s\n",
  237. i,!test_bit(Faulty, &tmp->rdev->flags),
  238. bdevname(tmp->rdev->bdev,b));
  239. }
  240. }
  241. static int multipath_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  242. {
  243. multipath_conf_t *conf = mddev->private;
  244. struct request_queue *q;
  245. int err = -EEXIST;
  246. int path;
  247. struct multipath_info *p;
  248. int first = 0;
  249. int last = mddev->raid_disks - 1;
  250. if (rdev->raid_disk >= 0)
  251. first = last = rdev->raid_disk;
  252. print_multipath_conf(conf);
  253. for (path = first; path <= last; path++)
  254. if ((p=conf->multipaths+path)->rdev == NULL) {
  255. q = rdev->bdev->bd_disk->queue;
  256. disk_stack_limits(mddev->gendisk, rdev->bdev,
  257. rdev->data_offset << 9);
  258. /* as we don't honour merge_bvec_fn, we must never risk
  259. * violating it, so limit ->max_segments to one, lying
  260. * within a single page.
  261. * (Note: it is very unlikely that a device with
  262. * merge_bvec_fn will be involved in multipath.)
  263. */
  264. if (q->merge_bvec_fn) {
  265. blk_queue_max_segments(mddev->queue, 1);
  266. blk_queue_segment_boundary(mddev->queue,
  267. PAGE_CACHE_SIZE - 1);
  268. }
  269. conf->working_disks++;
  270. mddev->degraded--;
  271. rdev->raid_disk = path;
  272. set_bit(In_sync, &rdev->flags);
  273. rcu_assign_pointer(p->rdev, rdev);
  274. err = 0;
  275. md_integrity_add_rdev(rdev, mddev);
  276. break;
  277. }
  278. print_multipath_conf(conf);
  279. return err;
  280. }
  281. static int multipath_remove_disk(mddev_t *mddev, int number)
  282. {
  283. multipath_conf_t *conf = mddev->private;
  284. int err = 0;
  285. mdk_rdev_t *rdev;
  286. struct multipath_info *p = conf->multipaths + number;
  287. print_multipath_conf(conf);
  288. rdev = p->rdev;
  289. if (rdev) {
  290. if (test_bit(In_sync, &rdev->flags) ||
  291. atomic_read(&rdev->nr_pending)) {
  292. printk(KERN_ERR "hot-remove-disk, slot %d is identified"
  293. " but is still operational!\n", number);
  294. err = -EBUSY;
  295. goto abort;
  296. }
  297. p->rdev = NULL;
  298. synchronize_rcu();
  299. if (atomic_read(&rdev->nr_pending)) {
  300. /* lost the race, try later */
  301. err = -EBUSY;
  302. p->rdev = rdev;
  303. goto abort;
  304. }
  305. md_integrity_register(mddev);
  306. }
  307. abort:
  308. print_multipath_conf(conf);
  309. return err;
  310. }
  311. /*
  312. * This is a kernel thread which:
  313. *
  314. * 1. Retries failed read operations on working multipaths.
  315. * 2. Updates the raid superblock when problems encounter.
  316. * 3. Performs writes following reads for array syncronising.
  317. */
  318. static void multipathd (mddev_t *mddev)
  319. {
  320. struct multipath_bh *mp_bh;
  321. struct bio *bio;
  322. unsigned long flags;
  323. multipath_conf_t *conf = mddev->private;
  324. struct list_head *head = &conf->retry_list;
  325. md_check_recovery(mddev);
  326. for (;;) {
  327. char b[BDEVNAME_SIZE];
  328. spin_lock_irqsave(&conf->device_lock, flags);
  329. if (list_empty(head))
  330. break;
  331. mp_bh = list_entry(head->prev, struct multipath_bh, retry_list);
  332. list_del(head->prev);
  333. spin_unlock_irqrestore(&conf->device_lock, flags);
  334. bio = &mp_bh->bio;
  335. bio->bi_sector = mp_bh->master_bio->bi_sector;
  336. if ((mp_bh->path = multipath_map (conf))<0) {
  337. printk(KERN_ALERT "multipath: %s: unrecoverable IO read"
  338. " error for block %llu\n",
  339. bdevname(bio->bi_bdev,b),
  340. (unsigned long long)bio->bi_sector);
  341. multipath_end_bh_io(mp_bh, -EIO);
  342. } else {
  343. printk(KERN_ERR "multipath: %s: redirecting sector %llu"
  344. " to another IO path\n",
  345. bdevname(bio->bi_bdev,b),
  346. (unsigned long long)bio->bi_sector);
  347. *bio = *(mp_bh->master_bio);
  348. bio->bi_sector += conf->multipaths[mp_bh->path].rdev->data_offset;
  349. bio->bi_bdev = conf->multipaths[mp_bh->path].rdev->bdev;
  350. bio->bi_rw |= (1 << BIO_RW_FAILFAST_TRANSPORT);
  351. bio->bi_end_io = multipath_end_request;
  352. bio->bi_private = mp_bh;
  353. generic_make_request(bio);
  354. }
  355. }
  356. spin_unlock_irqrestore(&conf->device_lock, flags);
  357. }
  358. static sector_t multipath_size(mddev_t *mddev, sector_t sectors, int raid_disks)
  359. {
  360. WARN_ONCE(sectors || raid_disks,
  361. "%s does not support generic reshape\n", __func__);
  362. return mddev->dev_sectors;
  363. }
  364. static int multipath_run (mddev_t *mddev)
  365. {
  366. multipath_conf_t *conf;
  367. int disk_idx;
  368. struct multipath_info *disk;
  369. mdk_rdev_t *rdev;
  370. if (md_check_no_bitmap(mddev))
  371. return -EINVAL;
  372. if (mddev->level != LEVEL_MULTIPATH) {
  373. printk("multipath: %s: raid level not set to multipath IO (%d)\n",
  374. mdname(mddev), mddev->level);
  375. goto out;
  376. }
  377. /*
  378. * copy the already verified devices into our private MULTIPATH
  379. * bookkeeping area. [whatever we allocate in multipath_run(),
  380. * should be freed in multipath_stop()]
  381. */
  382. mddev->queue->queue_lock = &mddev->queue->__queue_lock;
  383. conf = kzalloc(sizeof(multipath_conf_t), GFP_KERNEL);
  384. mddev->private = conf;
  385. if (!conf) {
  386. printk(KERN_ERR
  387. "multipath: couldn't allocate memory for %s\n",
  388. mdname(mddev));
  389. goto out;
  390. }
  391. conf->multipaths = kzalloc(sizeof(struct multipath_info)*mddev->raid_disks,
  392. GFP_KERNEL);
  393. if (!conf->multipaths) {
  394. printk(KERN_ERR
  395. "multipath: couldn't allocate memory for %s\n",
  396. mdname(mddev));
  397. goto out_free_conf;
  398. }
  399. conf->working_disks = 0;
  400. list_for_each_entry(rdev, &mddev->disks, same_set) {
  401. disk_idx = rdev->raid_disk;
  402. if (disk_idx < 0 ||
  403. disk_idx >= mddev->raid_disks)
  404. continue;
  405. disk = conf->multipaths + disk_idx;
  406. disk->rdev = rdev;
  407. disk_stack_limits(mddev->gendisk, rdev->bdev,
  408. rdev->data_offset << 9);
  409. /* as we don't honour merge_bvec_fn, we must never risk
  410. * violating it, not that we ever expect a device with
  411. * a merge_bvec_fn to be involved in multipath */
  412. if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
  413. blk_queue_max_segments(mddev->queue, 1);
  414. blk_queue_segment_boundary(mddev->queue,
  415. PAGE_CACHE_SIZE - 1);
  416. }
  417. if (!test_bit(Faulty, &rdev->flags))
  418. conf->working_disks++;
  419. }
  420. conf->raid_disks = mddev->raid_disks;
  421. conf->mddev = mddev;
  422. spin_lock_init(&conf->device_lock);
  423. INIT_LIST_HEAD(&conf->retry_list);
  424. if (!conf->working_disks) {
  425. printk(KERN_ERR "multipath: no operational IO paths for %s\n",
  426. mdname(mddev));
  427. goto out_free_conf;
  428. }
  429. mddev->degraded = conf->raid_disks - conf->working_disks;
  430. conf->pool = mempool_create_kmalloc_pool(NR_RESERVED_BUFS,
  431. sizeof(struct multipath_bh));
  432. if (conf->pool == NULL) {
  433. printk(KERN_ERR
  434. "multipath: couldn't allocate memory for %s\n",
  435. mdname(mddev));
  436. goto out_free_conf;
  437. }
  438. {
  439. mddev->thread = md_register_thread(multipathd, mddev, NULL);
  440. if (!mddev->thread) {
  441. printk(KERN_ERR "multipath: couldn't allocate thread"
  442. " for %s\n", mdname(mddev));
  443. goto out_free_conf;
  444. }
  445. }
  446. printk(KERN_INFO
  447. "multipath: array %s active with %d out of %d IO paths\n",
  448. mdname(mddev), conf->working_disks, mddev->raid_disks);
  449. /*
  450. * Ok, everything is just fine now
  451. */
  452. md_set_array_sectors(mddev, multipath_size(mddev, 0, 0));
  453. mddev->queue->unplug_fn = multipath_unplug;
  454. mddev->queue->backing_dev_info.congested_fn = multipath_congested;
  455. mddev->queue->backing_dev_info.congested_data = mddev;
  456. md_integrity_register(mddev);
  457. return 0;
  458. out_free_conf:
  459. if (conf->pool)
  460. mempool_destroy(conf->pool);
  461. kfree(conf->multipaths);
  462. kfree(conf);
  463. mddev->private = NULL;
  464. out:
  465. return -EIO;
  466. }
  467. static int multipath_stop (mddev_t *mddev)
  468. {
  469. multipath_conf_t *conf = mddev->private;
  470. md_unregister_thread(mddev->thread);
  471. mddev->thread = NULL;
  472. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  473. mempool_destroy(conf->pool);
  474. kfree(conf->multipaths);
  475. kfree(conf);
  476. mddev->private = NULL;
  477. return 0;
  478. }
  479. static struct mdk_personality multipath_personality =
  480. {
  481. .name = "multipath",
  482. .level = LEVEL_MULTIPATH,
  483. .owner = THIS_MODULE,
  484. .make_request = multipath_make_request,
  485. .run = multipath_run,
  486. .stop = multipath_stop,
  487. .status = multipath_status,
  488. .error_handler = multipath_error,
  489. .hot_add_disk = multipath_add_disk,
  490. .hot_remove_disk= multipath_remove_disk,
  491. .size = multipath_size,
  492. };
  493. static int __init multipath_init (void)
  494. {
  495. return register_md_personality (&multipath_personality);
  496. }
  497. static void __exit multipath_exit (void)
  498. {
  499. unregister_md_personality (&multipath_personality);
  500. }
  501. module_init(multipath_init);
  502. module_exit(multipath_exit);
  503. MODULE_LICENSE("GPL");
  504. MODULE_DESCRIPTION("simple multi-path personality for MD");
  505. MODULE_ALIAS("md-personality-7"); /* MULTIPATH */
  506. MODULE_ALIAS("md-multipath");
  507. MODULE_ALIAS("md-level--4");