multipath.c 15 KB

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