multipath.c 15 KB

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