multipath.c 13 KB

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