multipath.c 14 KB

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