multipath.c 15 KB

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