multipath.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. /*
  196. * Careful, this can execute in IRQ contexts as well!
  197. */
  198. static void multipath_error (mddev_t *mddev, mdk_rdev_t *rdev)
  199. {
  200. multipath_conf_t *conf = mddev_to_conf(mddev);
  201. if (conf->working_disks <= 1) {
  202. /*
  203. * Uh oh, we can do nothing if this is our last path, but
  204. * first check if this is a queued request for a device
  205. * which has just failed.
  206. */
  207. printk(KERN_ALERT
  208. "multipath: only one IO path left and IO error.\n");
  209. /* leave it active... it's all we have */
  210. } else {
  211. /*
  212. * Mark disk as unusable
  213. */
  214. if (!test_bit(Faulty, &rdev->flags)) {
  215. char b[BDEVNAME_SIZE];
  216. clear_bit(In_sync, &rdev->flags);
  217. set_bit(Faulty, &rdev->flags);
  218. mddev->sb_dirty = 1;
  219. conf->working_disks--;
  220. printk(KERN_ALERT "multipath: IO failure on %s,"
  221. " disabling IO path. \n Operation continuing"
  222. " on %d IO paths.\n",
  223. bdevname (rdev->bdev,b),
  224. conf->working_disks);
  225. }
  226. }
  227. }
  228. static void print_multipath_conf (multipath_conf_t *conf)
  229. {
  230. int i;
  231. struct multipath_info *tmp;
  232. printk("MULTIPATH conf printout:\n");
  233. if (!conf) {
  234. printk("(conf==NULL)\n");
  235. return;
  236. }
  237. printk(" --- wd:%d rd:%d\n", conf->working_disks,
  238. conf->raid_disks);
  239. for (i = 0; i < conf->raid_disks; i++) {
  240. char b[BDEVNAME_SIZE];
  241. tmp = conf->multipaths + i;
  242. if (tmp->rdev)
  243. printk(" disk%d, o:%d, dev:%s\n",
  244. i,!test_bit(Faulty, &tmp->rdev->flags),
  245. bdevname(tmp->rdev->bdev,b));
  246. }
  247. }
  248. static int multipath_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  249. {
  250. multipath_conf_t *conf = mddev->private;
  251. struct request_queue *q;
  252. int found = 0;
  253. int path;
  254. struct multipath_info *p;
  255. print_multipath_conf(conf);
  256. for (path=0; path<mddev->raid_disks; path++)
  257. if ((p=conf->multipaths+path)->rdev == NULL) {
  258. q = rdev->bdev->bd_disk->queue;
  259. blk_queue_stack_limits(mddev->queue, q);
  260. /* as we don't honour merge_bvec_fn, we must never risk
  261. * violating it, so limit ->max_sector to one PAGE, as
  262. * a one page request is never in violation.
  263. * (Note: it is very unlikely that a device with
  264. * merge_bvec_fn will be involved in multipath.)
  265. */
  266. if (q->merge_bvec_fn &&
  267. mddev->queue->max_sectors > (PAGE_SIZE>>9))
  268. blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
  269. conf->working_disks++;
  270. rdev->raid_disk = path;
  271. set_bit(In_sync, &rdev->flags);
  272. rcu_assign_pointer(p->rdev, rdev);
  273. found = 1;
  274. }
  275. print_multipath_conf(conf);
  276. return found;
  277. }
  278. static int multipath_remove_disk(mddev_t *mddev, int number)
  279. {
  280. multipath_conf_t *conf = mddev->private;
  281. int err = 0;
  282. mdk_rdev_t *rdev;
  283. struct multipath_info *p = conf->multipaths + number;
  284. print_multipath_conf(conf);
  285. rdev = p->rdev;
  286. if (rdev) {
  287. if (test_bit(In_sync, &rdev->flags) ||
  288. atomic_read(&rdev->nr_pending)) {
  289. printk(KERN_ERR "hot-remove-disk, slot %d is identified" " but is still operational!\n", number);
  290. err = -EBUSY;
  291. goto abort;
  292. }
  293. p->rdev = NULL;
  294. synchronize_rcu();
  295. if (atomic_read(&rdev->nr_pending)) {
  296. /* lost the race, try later */
  297. err = -EBUSY;
  298. p->rdev = rdev;
  299. }
  300. }
  301. abort:
  302. print_multipath_conf(conf);
  303. return err;
  304. }
  305. /*
  306. * This is a kernel thread which:
  307. *
  308. * 1. Retries failed read operations on working multipaths.
  309. * 2. Updates the raid superblock when problems encounter.
  310. * 3. Performs writes following reads for array syncronising.
  311. */
  312. static void multipathd (mddev_t *mddev)
  313. {
  314. struct multipath_bh *mp_bh;
  315. struct bio *bio;
  316. unsigned long flags;
  317. multipath_conf_t *conf = mddev_to_conf(mddev);
  318. struct list_head *head = &conf->retry_list;
  319. md_check_recovery(mddev);
  320. for (;;) {
  321. char b[BDEVNAME_SIZE];
  322. spin_lock_irqsave(&conf->device_lock, flags);
  323. if (list_empty(head))
  324. break;
  325. mp_bh = list_entry(head->prev, struct multipath_bh, retry_list);
  326. list_del(head->prev);
  327. spin_unlock_irqrestore(&conf->device_lock, flags);
  328. bio = &mp_bh->bio;
  329. bio->bi_sector = mp_bh->master_bio->bi_sector;
  330. if ((mp_bh->path = multipath_map (conf))<0) {
  331. printk(KERN_ALERT "multipath: %s: unrecoverable IO read"
  332. " error for block %llu\n",
  333. bdevname(bio->bi_bdev,b),
  334. (unsigned long long)bio->bi_sector);
  335. multipath_end_bh_io(mp_bh, -EIO);
  336. } else {
  337. printk(KERN_ERR "multipath: %s: redirecting sector %llu"
  338. " to another IO path\n",
  339. bdevname(bio->bi_bdev,b),
  340. (unsigned long long)bio->bi_sector);
  341. *bio = *(mp_bh->master_bio);
  342. bio->bi_sector += conf->multipaths[mp_bh->path].rdev->data_offset;
  343. bio->bi_bdev = conf->multipaths[mp_bh->path].rdev->bdev;
  344. bio->bi_rw |= (1 << BIO_RW_FAILFAST);
  345. bio->bi_end_io = multipath_end_request;
  346. bio->bi_private = mp_bh;
  347. generic_make_request(bio);
  348. }
  349. }
  350. spin_unlock_irqrestore(&conf->device_lock, flags);
  351. }
  352. static int multipath_run (mddev_t *mddev)
  353. {
  354. multipath_conf_t *conf;
  355. int disk_idx;
  356. struct multipath_info *disk;
  357. mdk_rdev_t *rdev;
  358. struct list_head *tmp;
  359. if (mddev->level != LEVEL_MULTIPATH) {
  360. printk("multipath: %s: raid level not set to multipath IO (%d)\n",
  361. mdname(mddev), mddev->level);
  362. goto out;
  363. }
  364. /*
  365. * copy the already verified devices into our private MULTIPATH
  366. * bookkeeping area. [whatever we allocate in multipath_run(),
  367. * should be freed in multipath_stop()]
  368. */
  369. conf = kzalloc(sizeof(multipath_conf_t), GFP_KERNEL);
  370. mddev->private = conf;
  371. if (!conf) {
  372. printk(KERN_ERR
  373. "multipath: couldn't allocate memory for %s\n",
  374. mdname(mddev));
  375. goto out;
  376. }
  377. conf->multipaths = kzalloc(sizeof(struct multipath_info)*mddev->raid_disks,
  378. GFP_KERNEL);
  379. if (!conf->multipaths) {
  380. printk(KERN_ERR
  381. "multipath: couldn't allocate memory for %s\n",
  382. mdname(mddev));
  383. goto out_free_conf;
  384. }
  385. conf->working_disks = 0;
  386. ITERATE_RDEV(mddev,rdev,tmp) {
  387. disk_idx = rdev->raid_disk;
  388. if (disk_idx < 0 ||
  389. disk_idx >= mddev->raid_disks)
  390. continue;
  391. disk = conf->multipaths + disk_idx;
  392. disk->rdev = rdev;
  393. blk_queue_stack_limits(mddev->queue,
  394. rdev->bdev->bd_disk->queue);
  395. /* as we don't honour merge_bvec_fn, we must never risk
  396. * violating it, not that we ever expect a device with
  397. * a merge_bvec_fn to be involved in multipath */
  398. if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
  399. mddev->queue->max_sectors > (PAGE_SIZE>>9))
  400. blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
  401. if (!test_bit(Faulty, &rdev->flags))
  402. conf->working_disks++;
  403. }
  404. conf->raid_disks = mddev->raid_disks;
  405. mddev->sb_dirty = 1;
  406. conf->mddev = mddev;
  407. spin_lock_init(&conf->device_lock);
  408. INIT_LIST_HEAD(&conf->retry_list);
  409. if (!conf->working_disks) {
  410. printk(KERN_ERR "multipath: no operational IO paths for %s\n",
  411. mdname(mddev));
  412. goto out_free_conf;
  413. }
  414. mddev->degraded = conf->raid_disks = conf->working_disks;
  415. conf->pool = mempool_create_kzalloc_pool(NR_RESERVED_BUFS,
  416. sizeof(struct multipath_bh));
  417. if (conf->pool == NULL) {
  418. printk(KERN_ERR
  419. "multipath: couldn't allocate memory for %s\n",
  420. mdname(mddev));
  421. goto out_free_conf;
  422. }
  423. {
  424. mddev->thread = md_register_thread(multipathd, mddev, "%s_multipath");
  425. if (!mddev->thread) {
  426. printk(KERN_ERR "multipath: couldn't allocate thread"
  427. " for %s\n", mdname(mddev));
  428. goto out_free_conf;
  429. }
  430. }
  431. printk(KERN_INFO
  432. "multipath: array %s active with %d out of %d IO paths\n",
  433. mdname(mddev), conf->working_disks, mddev->raid_disks);
  434. /*
  435. * Ok, everything is just fine now
  436. */
  437. mddev->array_size = mddev->size;
  438. mddev->queue->unplug_fn = multipath_unplug;
  439. mddev->queue->issue_flush_fn = multipath_issue_flush;
  440. return 0;
  441. out_free_conf:
  442. if (conf->pool)
  443. mempool_destroy(conf->pool);
  444. kfree(conf->multipaths);
  445. kfree(conf);
  446. mddev->private = NULL;
  447. out:
  448. return -EIO;
  449. }
  450. static int multipath_stop (mddev_t *mddev)
  451. {
  452. multipath_conf_t *conf = mddev_to_conf(mddev);
  453. md_unregister_thread(mddev->thread);
  454. mddev->thread = NULL;
  455. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  456. mempool_destroy(conf->pool);
  457. kfree(conf->multipaths);
  458. kfree(conf);
  459. mddev->private = NULL;
  460. return 0;
  461. }
  462. static struct mdk_personality multipath_personality =
  463. {
  464. .name = "multipath",
  465. .level = LEVEL_MULTIPATH,
  466. .owner = THIS_MODULE,
  467. .make_request = multipath_make_request,
  468. .run = multipath_run,
  469. .stop = multipath_stop,
  470. .status = multipath_status,
  471. .error_handler = multipath_error,
  472. .hot_add_disk = multipath_add_disk,
  473. .hot_remove_disk= multipath_remove_disk,
  474. };
  475. static int __init multipath_init (void)
  476. {
  477. return register_md_personality (&multipath_personality);
  478. }
  479. static void __exit multipath_exit (void)
  480. {
  481. unregister_md_personality (&multipath_personality);
  482. }
  483. module_init(multipath_init);
  484. module_exit(multipath_exit);
  485. MODULE_LICENSE("GPL");
  486. MODULE_ALIAS("md-personality-7"); /* MULTIPATH */
  487. MODULE_ALIAS("md-multipath");
  488. MODULE_ALIAS("md-level--4");