multipath.c 15 KB

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