dm-mpath.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. /*
  2. * Copyright (C) 2003 Sistina Software Limited.
  3. * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm.h"
  8. #include "dm-path-selector.h"
  9. #include "dm-hw-handler.h"
  10. #include "dm-bio-list.h"
  11. #include "dm-bio-record.h"
  12. #include <linux/ctype.h>
  13. #include <linux/init.h>
  14. #include <linux/mempool.h>
  15. #include <linux/module.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/slab.h>
  18. #include <linux/time.h>
  19. #include <linux/workqueue.h>
  20. #include <asm/atomic.h>
  21. #define DM_MSG_PREFIX "multipath"
  22. #define MESG_STR(x) x, sizeof(x)
  23. /* Path properties */
  24. struct pgpath {
  25. struct list_head list;
  26. struct priority_group *pg; /* Owning PG */
  27. unsigned fail_count; /* Cumulative failure count */
  28. struct path path;
  29. };
  30. #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
  31. /*
  32. * Paths are grouped into Priority Groups and numbered from 1 upwards.
  33. * Each has a path selector which controls which path gets used.
  34. */
  35. struct priority_group {
  36. struct list_head list;
  37. struct multipath *m; /* Owning multipath instance */
  38. struct path_selector ps;
  39. unsigned pg_num; /* Reference number */
  40. unsigned bypassed; /* Temporarily bypass this PG? */
  41. unsigned nr_pgpaths; /* Number of paths in PG */
  42. struct list_head pgpaths;
  43. };
  44. /* Multipath context */
  45. struct multipath {
  46. struct list_head list;
  47. struct dm_target *ti;
  48. spinlock_t lock;
  49. struct hw_handler hw_handler;
  50. unsigned nr_priority_groups;
  51. struct list_head priority_groups;
  52. unsigned pg_init_required; /* pg_init needs calling? */
  53. unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
  54. unsigned nr_valid_paths; /* Total number of usable paths */
  55. struct pgpath *current_pgpath;
  56. struct priority_group *current_pg;
  57. struct priority_group *next_pg; /* Switch to this PG if set */
  58. unsigned repeat_count; /* I/Os left before calling PS again */
  59. unsigned queue_io; /* Must we queue all I/O? */
  60. unsigned queue_if_no_path; /* Queue I/O if last path fails? */
  61. unsigned saved_queue_if_no_path;/* Saved state during suspension */
  62. struct work_struct process_queued_ios;
  63. struct bio_list queued_ios;
  64. unsigned queue_size;
  65. struct work_struct trigger_event;
  66. /*
  67. * We must use a mempool of mpath_io structs so that we
  68. * can resubmit bios on error.
  69. */
  70. mempool_t *mpio_pool;
  71. };
  72. /*
  73. * Context information attached to each bio we process.
  74. */
  75. struct mpath_io {
  76. struct pgpath *pgpath;
  77. struct dm_bio_details details;
  78. };
  79. typedef int (*action_fn) (struct pgpath *pgpath);
  80. #define MIN_IOS 256 /* Mempool size */
  81. static kmem_cache_t *_mpio_cache;
  82. struct workqueue_struct *kmultipathd;
  83. static void process_queued_ios(void *data);
  84. static void trigger_event(void *data);
  85. /*-----------------------------------------------
  86. * Allocation routines
  87. *-----------------------------------------------*/
  88. static struct pgpath *alloc_pgpath(void)
  89. {
  90. struct pgpath *pgpath = kmalloc(sizeof(*pgpath), GFP_KERNEL);
  91. if (pgpath) {
  92. memset(pgpath, 0, sizeof(*pgpath));
  93. pgpath->path.is_active = 1;
  94. }
  95. return pgpath;
  96. }
  97. static inline void free_pgpath(struct pgpath *pgpath)
  98. {
  99. kfree(pgpath);
  100. }
  101. static struct priority_group *alloc_priority_group(void)
  102. {
  103. struct priority_group *pg;
  104. pg = kmalloc(sizeof(*pg), GFP_KERNEL);
  105. if (!pg)
  106. return NULL;
  107. memset(pg, 0, sizeof(*pg));
  108. INIT_LIST_HEAD(&pg->pgpaths);
  109. return pg;
  110. }
  111. static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
  112. {
  113. struct pgpath *pgpath, *tmp;
  114. list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
  115. list_del(&pgpath->list);
  116. dm_put_device(ti, pgpath->path.dev);
  117. free_pgpath(pgpath);
  118. }
  119. }
  120. static void free_priority_group(struct priority_group *pg,
  121. struct dm_target *ti)
  122. {
  123. struct path_selector *ps = &pg->ps;
  124. if (ps->type) {
  125. ps->type->destroy(ps);
  126. dm_put_path_selector(ps->type);
  127. }
  128. free_pgpaths(&pg->pgpaths, ti);
  129. kfree(pg);
  130. }
  131. static struct multipath *alloc_multipath(void)
  132. {
  133. struct multipath *m;
  134. m = kmalloc(sizeof(*m), GFP_KERNEL);
  135. if (m) {
  136. memset(m, 0, sizeof(*m));
  137. INIT_LIST_HEAD(&m->priority_groups);
  138. spin_lock_init(&m->lock);
  139. m->queue_io = 1;
  140. INIT_WORK(&m->process_queued_ios, process_queued_ios, m);
  141. INIT_WORK(&m->trigger_event, trigger_event, m);
  142. m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
  143. if (!m->mpio_pool) {
  144. kfree(m);
  145. return NULL;
  146. }
  147. }
  148. return m;
  149. }
  150. static void free_multipath(struct multipath *m)
  151. {
  152. struct priority_group *pg, *tmp;
  153. struct hw_handler *hwh = &m->hw_handler;
  154. list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
  155. list_del(&pg->list);
  156. free_priority_group(pg, m->ti);
  157. }
  158. if (hwh->type) {
  159. hwh->type->destroy(hwh);
  160. dm_put_hw_handler(hwh->type);
  161. }
  162. mempool_destroy(m->mpio_pool);
  163. kfree(m);
  164. }
  165. /*-----------------------------------------------
  166. * Path selection
  167. *-----------------------------------------------*/
  168. static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
  169. {
  170. struct hw_handler *hwh = &m->hw_handler;
  171. m->current_pg = pgpath->pg;
  172. /* Must we initialise the PG first, and queue I/O till it's ready? */
  173. if (hwh->type && hwh->type->pg_init) {
  174. m->pg_init_required = 1;
  175. m->queue_io = 1;
  176. } else {
  177. m->pg_init_required = 0;
  178. m->queue_io = 0;
  179. }
  180. }
  181. static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
  182. {
  183. struct path *path;
  184. path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
  185. if (!path)
  186. return -ENXIO;
  187. m->current_pgpath = path_to_pgpath(path);
  188. if (m->current_pg != pg)
  189. __switch_pg(m, m->current_pgpath);
  190. return 0;
  191. }
  192. static void __choose_pgpath(struct multipath *m)
  193. {
  194. struct priority_group *pg;
  195. unsigned bypassed = 1;
  196. if (!m->nr_valid_paths)
  197. goto failed;
  198. /* Were we instructed to switch PG? */
  199. if (m->next_pg) {
  200. pg = m->next_pg;
  201. m->next_pg = NULL;
  202. if (!__choose_path_in_pg(m, pg))
  203. return;
  204. }
  205. /* Don't change PG until it has no remaining paths */
  206. if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
  207. return;
  208. /*
  209. * Loop through priority groups until we find a valid path.
  210. * First time we skip PGs marked 'bypassed'.
  211. * Second time we only try the ones we skipped.
  212. */
  213. do {
  214. list_for_each_entry(pg, &m->priority_groups, list) {
  215. if (pg->bypassed == bypassed)
  216. continue;
  217. if (!__choose_path_in_pg(m, pg))
  218. return;
  219. }
  220. } while (bypassed--);
  221. failed:
  222. m->current_pgpath = NULL;
  223. m->current_pg = NULL;
  224. }
  225. static int map_io(struct multipath *m, struct bio *bio, struct mpath_io *mpio,
  226. unsigned was_queued)
  227. {
  228. int r = 1;
  229. unsigned long flags;
  230. struct pgpath *pgpath;
  231. spin_lock_irqsave(&m->lock, flags);
  232. /* Do we need to select a new pgpath? */
  233. if (!m->current_pgpath ||
  234. (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
  235. __choose_pgpath(m);
  236. pgpath = m->current_pgpath;
  237. if (was_queued)
  238. m->queue_size--;
  239. if ((pgpath && m->queue_io) ||
  240. (!pgpath && m->queue_if_no_path)) {
  241. /* Queue for the daemon to resubmit */
  242. bio_list_add(&m->queued_ios, bio);
  243. m->queue_size++;
  244. if ((m->pg_init_required && !m->pg_init_in_progress) ||
  245. !m->queue_io)
  246. queue_work(kmultipathd, &m->process_queued_ios);
  247. pgpath = NULL;
  248. r = 0;
  249. } else if (!pgpath)
  250. r = -EIO; /* Failed */
  251. else
  252. bio->bi_bdev = pgpath->path.dev->bdev;
  253. mpio->pgpath = pgpath;
  254. spin_unlock_irqrestore(&m->lock, flags);
  255. return r;
  256. }
  257. /*
  258. * If we run out of usable paths, should we queue I/O or error it?
  259. */
  260. static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
  261. unsigned save_old_value)
  262. {
  263. unsigned long flags;
  264. spin_lock_irqsave(&m->lock, flags);
  265. if (save_old_value)
  266. m->saved_queue_if_no_path = m->queue_if_no_path;
  267. else
  268. m->saved_queue_if_no_path = queue_if_no_path;
  269. m->queue_if_no_path = queue_if_no_path;
  270. if (!m->queue_if_no_path && m->queue_size)
  271. queue_work(kmultipathd, &m->process_queued_ios);
  272. spin_unlock_irqrestore(&m->lock, flags);
  273. return 0;
  274. }
  275. /*-----------------------------------------------------------------
  276. * The multipath daemon is responsible for resubmitting queued ios.
  277. *---------------------------------------------------------------*/
  278. static void dispatch_queued_ios(struct multipath *m)
  279. {
  280. int r;
  281. unsigned long flags;
  282. struct bio *bio = NULL, *next;
  283. struct mpath_io *mpio;
  284. union map_info *info;
  285. spin_lock_irqsave(&m->lock, flags);
  286. bio = bio_list_get(&m->queued_ios);
  287. spin_unlock_irqrestore(&m->lock, flags);
  288. while (bio) {
  289. next = bio->bi_next;
  290. bio->bi_next = NULL;
  291. info = dm_get_mapinfo(bio);
  292. mpio = info->ptr;
  293. r = map_io(m, bio, mpio, 1);
  294. if (r < 0)
  295. bio_endio(bio, bio->bi_size, r);
  296. else if (r == 1)
  297. generic_make_request(bio);
  298. bio = next;
  299. }
  300. }
  301. static void process_queued_ios(void *data)
  302. {
  303. struct multipath *m = (struct multipath *) data;
  304. struct hw_handler *hwh = &m->hw_handler;
  305. struct pgpath *pgpath = NULL;
  306. unsigned init_required = 0, must_queue = 1;
  307. unsigned long flags;
  308. spin_lock_irqsave(&m->lock, flags);
  309. if (!m->queue_size)
  310. goto out;
  311. if (!m->current_pgpath)
  312. __choose_pgpath(m);
  313. pgpath = m->current_pgpath;
  314. if ((pgpath && !m->queue_io) ||
  315. (!pgpath && !m->queue_if_no_path))
  316. must_queue = 0;
  317. if (m->pg_init_required && !m->pg_init_in_progress) {
  318. m->pg_init_required = 0;
  319. m->pg_init_in_progress = 1;
  320. init_required = 1;
  321. }
  322. out:
  323. spin_unlock_irqrestore(&m->lock, flags);
  324. if (init_required)
  325. hwh->type->pg_init(hwh, pgpath->pg->bypassed, &pgpath->path);
  326. if (!must_queue)
  327. dispatch_queued_ios(m);
  328. }
  329. /*
  330. * An event is triggered whenever a path is taken out of use.
  331. * Includes path failure and PG bypass.
  332. */
  333. static void trigger_event(void *data)
  334. {
  335. struct multipath *m = (struct multipath *) data;
  336. dm_table_event(m->ti->table);
  337. }
  338. /*-----------------------------------------------------------------
  339. * Constructor/argument parsing:
  340. * <#multipath feature args> [<arg>]*
  341. * <#hw_handler args> [hw_handler [<arg>]*]
  342. * <#priority groups>
  343. * <initial priority group>
  344. * [<selector> <#selector args> [<arg>]*
  345. * <#paths> <#per-path selector args>
  346. * [<path> [<arg>]* ]+ ]+
  347. *---------------------------------------------------------------*/
  348. struct param {
  349. unsigned min;
  350. unsigned max;
  351. char *error;
  352. };
  353. static int read_param(struct param *param, char *str, unsigned *v, char **error)
  354. {
  355. if (!str ||
  356. (sscanf(str, "%u", v) != 1) ||
  357. (*v < param->min) ||
  358. (*v > param->max)) {
  359. *error = param->error;
  360. return -EINVAL;
  361. }
  362. return 0;
  363. }
  364. struct arg_set {
  365. unsigned argc;
  366. char **argv;
  367. };
  368. static char *shift(struct arg_set *as)
  369. {
  370. char *r;
  371. if (as->argc) {
  372. as->argc--;
  373. r = *as->argv;
  374. as->argv++;
  375. return r;
  376. }
  377. return NULL;
  378. }
  379. static void consume(struct arg_set *as, unsigned n)
  380. {
  381. BUG_ON (as->argc < n);
  382. as->argc -= n;
  383. as->argv += n;
  384. }
  385. static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
  386. struct dm_target *ti)
  387. {
  388. int r;
  389. struct path_selector_type *pst;
  390. unsigned ps_argc;
  391. static struct param _params[] = {
  392. {0, 1024, "invalid number of path selector args"},
  393. };
  394. pst = dm_get_path_selector(shift(as));
  395. if (!pst) {
  396. ti->error = "unknown path selector type";
  397. return -EINVAL;
  398. }
  399. r = read_param(_params, shift(as), &ps_argc, &ti->error);
  400. if (r)
  401. return -EINVAL;
  402. r = pst->create(&pg->ps, ps_argc, as->argv);
  403. if (r) {
  404. dm_put_path_selector(pst);
  405. ti->error = "path selector constructor failed";
  406. return r;
  407. }
  408. pg->ps.type = pst;
  409. consume(as, ps_argc);
  410. return 0;
  411. }
  412. static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
  413. struct dm_target *ti)
  414. {
  415. int r;
  416. struct pgpath *p;
  417. /* we need at least a path arg */
  418. if (as->argc < 1) {
  419. ti->error = "no device given";
  420. return NULL;
  421. }
  422. p = alloc_pgpath();
  423. if (!p)
  424. return NULL;
  425. r = dm_get_device(ti, shift(as), ti->begin, ti->len,
  426. dm_table_get_mode(ti->table), &p->path.dev);
  427. if (r) {
  428. ti->error = "error getting device";
  429. goto bad;
  430. }
  431. r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
  432. if (r) {
  433. dm_put_device(ti, p->path.dev);
  434. goto bad;
  435. }
  436. return p;
  437. bad:
  438. free_pgpath(p);
  439. return NULL;
  440. }
  441. static struct priority_group *parse_priority_group(struct arg_set *as,
  442. struct multipath *m,
  443. struct dm_target *ti)
  444. {
  445. static struct param _params[] = {
  446. {1, 1024, "invalid number of paths"},
  447. {0, 1024, "invalid number of selector args"}
  448. };
  449. int r;
  450. unsigned i, nr_selector_args, nr_params;
  451. struct priority_group *pg;
  452. if (as->argc < 2) {
  453. as->argc = 0;
  454. ti->error = "not enough priority group aruments";
  455. return NULL;
  456. }
  457. pg = alloc_priority_group();
  458. if (!pg) {
  459. ti->error = "couldn't allocate priority group";
  460. return NULL;
  461. }
  462. pg->m = m;
  463. r = parse_path_selector(as, pg, ti);
  464. if (r)
  465. goto bad;
  466. /*
  467. * read the paths
  468. */
  469. r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
  470. if (r)
  471. goto bad;
  472. r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
  473. if (r)
  474. goto bad;
  475. nr_params = 1 + nr_selector_args;
  476. for (i = 0; i < pg->nr_pgpaths; i++) {
  477. struct pgpath *pgpath;
  478. struct arg_set path_args;
  479. if (as->argc < nr_params)
  480. goto bad;
  481. path_args.argc = nr_params;
  482. path_args.argv = as->argv;
  483. pgpath = parse_path(&path_args, &pg->ps, ti);
  484. if (!pgpath)
  485. goto bad;
  486. pgpath->pg = pg;
  487. list_add_tail(&pgpath->list, &pg->pgpaths);
  488. consume(as, nr_params);
  489. }
  490. return pg;
  491. bad:
  492. free_priority_group(pg, ti);
  493. return NULL;
  494. }
  495. static int parse_hw_handler(struct arg_set *as, struct multipath *m,
  496. struct dm_target *ti)
  497. {
  498. int r;
  499. struct hw_handler_type *hwht;
  500. unsigned hw_argc;
  501. static struct param _params[] = {
  502. {0, 1024, "invalid number of hardware handler args"},
  503. };
  504. r = read_param(_params, shift(as), &hw_argc, &ti->error);
  505. if (r)
  506. return -EINVAL;
  507. if (!hw_argc)
  508. return 0;
  509. hwht = dm_get_hw_handler(shift(as));
  510. if (!hwht) {
  511. ti->error = "unknown hardware handler type";
  512. return -EINVAL;
  513. }
  514. r = hwht->create(&m->hw_handler, hw_argc - 1, as->argv);
  515. if (r) {
  516. dm_put_hw_handler(hwht);
  517. ti->error = "hardware handler constructor failed";
  518. return r;
  519. }
  520. m->hw_handler.type = hwht;
  521. consume(as, hw_argc - 1);
  522. return 0;
  523. }
  524. static int parse_features(struct arg_set *as, struct multipath *m,
  525. struct dm_target *ti)
  526. {
  527. int r;
  528. unsigned argc;
  529. static struct param _params[] = {
  530. {0, 1, "invalid number of feature args"},
  531. };
  532. r = read_param(_params, shift(as), &argc, &ti->error);
  533. if (r)
  534. return -EINVAL;
  535. if (!argc)
  536. return 0;
  537. if (!strnicmp(shift(as), MESG_STR("queue_if_no_path")))
  538. return queue_if_no_path(m, 1, 0);
  539. else {
  540. ti->error = "Unrecognised multipath feature request";
  541. return -EINVAL;
  542. }
  543. }
  544. static int multipath_ctr(struct dm_target *ti, unsigned int argc,
  545. char **argv)
  546. {
  547. /* target parameters */
  548. static struct param _params[] = {
  549. {1, 1024, "invalid number of priority groups"},
  550. {1, 1024, "invalid initial priority group number"},
  551. };
  552. int r;
  553. struct multipath *m;
  554. struct arg_set as;
  555. unsigned pg_count = 0;
  556. unsigned next_pg_num;
  557. as.argc = argc;
  558. as.argv = argv;
  559. m = alloc_multipath();
  560. if (!m) {
  561. ti->error = "can't allocate multipath";
  562. return -EINVAL;
  563. }
  564. m->ti = ti;
  565. r = parse_features(&as, m, ti);
  566. if (r)
  567. goto bad;
  568. r = parse_hw_handler(&as, m, ti);
  569. if (r)
  570. goto bad;
  571. r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
  572. if (r)
  573. goto bad;
  574. r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
  575. if (r)
  576. goto bad;
  577. /* parse the priority groups */
  578. while (as.argc) {
  579. struct priority_group *pg;
  580. pg = parse_priority_group(&as, m, ti);
  581. if (!pg) {
  582. r = -EINVAL;
  583. goto bad;
  584. }
  585. m->nr_valid_paths += pg->nr_pgpaths;
  586. list_add_tail(&pg->list, &m->priority_groups);
  587. pg_count++;
  588. pg->pg_num = pg_count;
  589. if (!--next_pg_num)
  590. m->next_pg = pg;
  591. }
  592. if (pg_count != m->nr_priority_groups) {
  593. ti->error = "priority group count mismatch";
  594. r = -EINVAL;
  595. goto bad;
  596. }
  597. ti->private = m;
  598. return 0;
  599. bad:
  600. free_multipath(m);
  601. return r;
  602. }
  603. static void multipath_dtr(struct dm_target *ti)
  604. {
  605. struct multipath *m = (struct multipath *) ti->private;
  606. flush_workqueue(kmultipathd);
  607. free_multipath(m);
  608. }
  609. /*
  610. * Map bios, recording original fields for later in case we have to resubmit
  611. */
  612. static int multipath_map(struct dm_target *ti, struct bio *bio,
  613. union map_info *map_context)
  614. {
  615. int r;
  616. struct mpath_io *mpio;
  617. struct multipath *m = (struct multipath *) ti->private;
  618. if (bio_barrier(bio))
  619. return -EOPNOTSUPP;
  620. mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
  621. dm_bio_record(&mpio->details, bio);
  622. map_context->ptr = mpio;
  623. bio->bi_rw |= (1 << BIO_RW_FAILFAST);
  624. r = map_io(m, bio, mpio, 0);
  625. if (r < 0)
  626. mempool_free(mpio, m->mpio_pool);
  627. return r;
  628. }
  629. /*
  630. * Take a path out of use.
  631. */
  632. static int fail_path(struct pgpath *pgpath)
  633. {
  634. unsigned long flags;
  635. struct multipath *m = pgpath->pg->m;
  636. spin_lock_irqsave(&m->lock, flags);
  637. if (!pgpath->path.is_active)
  638. goto out;
  639. DMWARN("Failing path %s.", pgpath->path.dev->name);
  640. pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
  641. pgpath->path.is_active = 0;
  642. pgpath->fail_count++;
  643. m->nr_valid_paths--;
  644. if (pgpath == m->current_pgpath)
  645. m->current_pgpath = NULL;
  646. queue_work(kmultipathd, &m->trigger_event);
  647. out:
  648. spin_unlock_irqrestore(&m->lock, flags);
  649. return 0;
  650. }
  651. /*
  652. * Reinstate a previously-failed path
  653. */
  654. static int reinstate_path(struct pgpath *pgpath)
  655. {
  656. int r = 0;
  657. unsigned long flags;
  658. struct multipath *m = pgpath->pg->m;
  659. spin_lock_irqsave(&m->lock, flags);
  660. if (pgpath->path.is_active)
  661. goto out;
  662. if (!pgpath->pg->ps.type) {
  663. DMWARN("Reinstate path not supported by path selector %s",
  664. pgpath->pg->ps.type->name);
  665. r = -EINVAL;
  666. goto out;
  667. }
  668. r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
  669. if (r)
  670. goto out;
  671. pgpath->path.is_active = 1;
  672. m->current_pgpath = NULL;
  673. if (!m->nr_valid_paths++ && m->queue_size)
  674. queue_work(kmultipathd, &m->process_queued_ios);
  675. queue_work(kmultipathd, &m->trigger_event);
  676. out:
  677. spin_unlock_irqrestore(&m->lock, flags);
  678. return r;
  679. }
  680. /*
  681. * Fail or reinstate all paths that match the provided struct dm_dev.
  682. */
  683. static int action_dev(struct multipath *m, struct dm_dev *dev,
  684. action_fn action)
  685. {
  686. int r = 0;
  687. struct pgpath *pgpath;
  688. struct priority_group *pg;
  689. list_for_each_entry(pg, &m->priority_groups, list) {
  690. list_for_each_entry(pgpath, &pg->pgpaths, list) {
  691. if (pgpath->path.dev == dev)
  692. r = action(pgpath);
  693. }
  694. }
  695. return r;
  696. }
  697. /*
  698. * Temporarily try to avoid having to use the specified PG
  699. */
  700. static void bypass_pg(struct multipath *m, struct priority_group *pg,
  701. int bypassed)
  702. {
  703. unsigned long flags;
  704. spin_lock_irqsave(&m->lock, flags);
  705. pg->bypassed = bypassed;
  706. m->current_pgpath = NULL;
  707. m->current_pg = NULL;
  708. spin_unlock_irqrestore(&m->lock, flags);
  709. queue_work(kmultipathd, &m->trigger_event);
  710. }
  711. /*
  712. * Switch to using the specified PG from the next I/O that gets mapped
  713. */
  714. static int switch_pg_num(struct multipath *m, const char *pgstr)
  715. {
  716. struct priority_group *pg;
  717. unsigned pgnum;
  718. unsigned long flags;
  719. if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
  720. (pgnum > m->nr_priority_groups)) {
  721. DMWARN("invalid PG number supplied to switch_pg_num");
  722. return -EINVAL;
  723. }
  724. spin_lock_irqsave(&m->lock, flags);
  725. list_for_each_entry(pg, &m->priority_groups, list) {
  726. pg->bypassed = 0;
  727. if (--pgnum)
  728. continue;
  729. m->current_pgpath = NULL;
  730. m->current_pg = NULL;
  731. m->next_pg = pg;
  732. }
  733. spin_unlock_irqrestore(&m->lock, flags);
  734. queue_work(kmultipathd, &m->trigger_event);
  735. return 0;
  736. }
  737. /*
  738. * Set/clear bypassed status of a PG.
  739. * PGs are numbered upwards from 1 in the order they were declared.
  740. */
  741. static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
  742. {
  743. struct priority_group *pg;
  744. unsigned pgnum;
  745. if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
  746. (pgnum > m->nr_priority_groups)) {
  747. DMWARN("invalid PG number supplied to bypass_pg");
  748. return -EINVAL;
  749. }
  750. list_for_each_entry(pg, &m->priority_groups, list) {
  751. if (!--pgnum)
  752. break;
  753. }
  754. bypass_pg(m, pg, bypassed);
  755. return 0;
  756. }
  757. /*
  758. * pg_init must call this when it has completed its initialisation
  759. */
  760. void dm_pg_init_complete(struct path *path, unsigned err_flags)
  761. {
  762. struct pgpath *pgpath = path_to_pgpath(path);
  763. struct priority_group *pg = pgpath->pg;
  764. struct multipath *m = pg->m;
  765. unsigned long flags;
  766. /* We insist on failing the path if the PG is already bypassed. */
  767. if (err_flags && pg->bypassed)
  768. err_flags |= MP_FAIL_PATH;
  769. if (err_flags & MP_FAIL_PATH)
  770. fail_path(pgpath);
  771. if (err_flags & MP_BYPASS_PG)
  772. bypass_pg(m, pg, 1);
  773. spin_lock_irqsave(&m->lock, flags);
  774. if (err_flags) {
  775. m->current_pgpath = NULL;
  776. m->current_pg = NULL;
  777. } else if (!m->pg_init_required)
  778. m->queue_io = 0;
  779. m->pg_init_in_progress = 0;
  780. queue_work(kmultipathd, &m->process_queued_ios);
  781. spin_unlock_irqrestore(&m->lock, flags);
  782. }
  783. /*
  784. * end_io handling
  785. */
  786. static int do_end_io(struct multipath *m, struct bio *bio,
  787. int error, struct mpath_io *mpio)
  788. {
  789. struct hw_handler *hwh = &m->hw_handler;
  790. unsigned err_flags = MP_FAIL_PATH; /* Default behavior */
  791. unsigned long flags;
  792. if (!error)
  793. return 0; /* I/O complete */
  794. if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
  795. return error;
  796. if (error == -EOPNOTSUPP)
  797. return error;
  798. spin_lock_irqsave(&m->lock, flags);
  799. if (!m->nr_valid_paths) {
  800. if (!m->queue_if_no_path) {
  801. spin_unlock_irqrestore(&m->lock, flags);
  802. return -EIO;
  803. } else {
  804. spin_unlock_irqrestore(&m->lock, flags);
  805. goto requeue;
  806. }
  807. }
  808. spin_unlock_irqrestore(&m->lock, flags);
  809. if (hwh->type && hwh->type->error)
  810. err_flags = hwh->type->error(hwh, bio);
  811. if (mpio->pgpath) {
  812. if (err_flags & MP_FAIL_PATH)
  813. fail_path(mpio->pgpath);
  814. if (err_flags & MP_BYPASS_PG)
  815. bypass_pg(m, mpio->pgpath->pg, 1);
  816. }
  817. if (err_flags & MP_ERROR_IO)
  818. return -EIO;
  819. requeue:
  820. dm_bio_restore(&mpio->details, bio);
  821. /* queue for the daemon to resubmit or fail */
  822. spin_lock_irqsave(&m->lock, flags);
  823. bio_list_add(&m->queued_ios, bio);
  824. m->queue_size++;
  825. if (!m->queue_io)
  826. queue_work(kmultipathd, &m->process_queued_ios);
  827. spin_unlock_irqrestore(&m->lock, flags);
  828. return 1; /* io not complete */
  829. }
  830. static int multipath_end_io(struct dm_target *ti, struct bio *bio,
  831. int error, union map_info *map_context)
  832. {
  833. struct multipath *m = (struct multipath *) ti->private;
  834. struct mpath_io *mpio = (struct mpath_io *) map_context->ptr;
  835. struct pgpath *pgpath = mpio->pgpath;
  836. struct path_selector *ps;
  837. int r;
  838. r = do_end_io(m, bio, error, mpio);
  839. if (pgpath) {
  840. ps = &pgpath->pg->ps;
  841. if (ps->type->end_io)
  842. ps->type->end_io(ps, &pgpath->path);
  843. }
  844. if (r <= 0)
  845. mempool_free(mpio, m->mpio_pool);
  846. return r;
  847. }
  848. /*
  849. * Suspend can't complete until all the I/O is processed so if
  850. * the last path fails we must error any remaining I/O.
  851. * Note that if the freeze_bdev fails while suspending, the
  852. * queue_if_no_path state is lost - userspace should reset it.
  853. */
  854. static void multipath_presuspend(struct dm_target *ti)
  855. {
  856. struct multipath *m = (struct multipath *) ti->private;
  857. queue_if_no_path(m, 0, 1);
  858. }
  859. /*
  860. * Restore the queue_if_no_path setting.
  861. */
  862. static void multipath_resume(struct dm_target *ti)
  863. {
  864. struct multipath *m = (struct multipath *) ti->private;
  865. unsigned long flags;
  866. spin_lock_irqsave(&m->lock, flags);
  867. m->queue_if_no_path = m->saved_queue_if_no_path;
  868. spin_unlock_irqrestore(&m->lock, flags);
  869. }
  870. /*
  871. * Info output has the following format:
  872. * num_multipath_feature_args [multipath_feature_args]*
  873. * num_handler_status_args [handler_status_args]*
  874. * num_groups init_group_number
  875. * [A|D|E num_ps_status_args [ps_status_args]*
  876. * num_paths num_selector_args
  877. * [path_dev A|F fail_count [selector_args]* ]+ ]+
  878. *
  879. * Table output has the following format (identical to the constructor string):
  880. * num_feature_args [features_args]*
  881. * num_handler_args hw_handler [hw_handler_args]*
  882. * num_groups init_group_number
  883. * [priority selector-name num_ps_args [ps_args]*
  884. * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
  885. */
  886. static int multipath_status(struct dm_target *ti, status_type_t type,
  887. char *result, unsigned int maxlen)
  888. {
  889. int sz = 0;
  890. unsigned long flags;
  891. struct multipath *m = (struct multipath *) ti->private;
  892. struct hw_handler *hwh = &m->hw_handler;
  893. struct priority_group *pg;
  894. struct pgpath *p;
  895. unsigned pg_num;
  896. char state;
  897. spin_lock_irqsave(&m->lock, flags);
  898. /* Features */
  899. if (type == STATUSTYPE_INFO)
  900. DMEMIT("1 %u ", m->queue_size);
  901. else if (m->queue_if_no_path)
  902. DMEMIT("1 queue_if_no_path ");
  903. else
  904. DMEMIT("0 ");
  905. if (hwh->type && hwh->type->status)
  906. sz += hwh->type->status(hwh, type, result + sz, maxlen - sz);
  907. else if (!hwh->type || type == STATUSTYPE_INFO)
  908. DMEMIT("0 ");
  909. else
  910. DMEMIT("1 %s ", hwh->type->name);
  911. DMEMIT("%u ", m->nr_priority_groups);
  912. if (m->next_pg)
  913. pg_num = m->next_pg->pg_num;
  914. else if (m->current_pg)
  915. pg_num = m->current_pg->pg_num;
  916. else
  917. pg_num = 1;
  918. DMEMIT("%u ", pg_num);
  919. switch (type) {
  920. case STATUSTYPE_INFO:
  921. list_for_each_entry(pg, &m->priority_groups, list) {
  922. if (pg->bypassed)
  923. state = 'D'; /* Disabled */
  924. else if (pg == m->current_pg)
  925. state = 'A'; /* Currently Active */
  926. else
  927. state = 'E'; /* Enabled */
  928. DMEMIT("%c ", state);
  929. if (pg->ps.type->status)
  930. sz += pg->ps.type->status(&pg->ps, NULL, type,
  931. result + sz,
  932. maxlen - sz);
  933. else
  934. DMEMIT("0 ");
  935. DMEMIT("%u %u ", pg->nr_pgpaths,
  936. pg->ps.type->info_args);
  937. list_for_each_entry(p, &pg->pgpaths, list) {
  938. DMEMIT("%s %s %u ", p->path.dev->name,
  939. p->path.is_active ? "A" : "F",
  940. p->fail_count);
  941. if (pg->ps.type->status)
  942. sz += pg->ps.type->status(&pg->ps,
  943. &p->path, type, result + sz,
  944. maxlen - sz);
  945. }
  946. }
  947. break;
  948. case STATUSTYPE_TABLE:
  949. list_for_each_entry(pg, &m->priority_groups, list) {
  950. DMEMIT("%s ", pg->ps.type->name);
  951. if (pg->ps.type->status)
  952. sz += pg->ps.type->status(&pg->ps, NULL, type,
  953. result + sz,
  954. maxlen - sz);
  955. else
  956. DMEMIT("0 ");
  957. DMEMIT("%u %u ", pg->nr_pgpaths,
  958. pg->ps.type->table_args);
  959. list_for_each_entry(p, &pg->pgpaths, list) {
  960. DMEMIT("%s ", p->path.dev->name);
  961. if (pg->ps.type->status)
  962. sz += pg->ps.type->status(&pg->ps,
  963. &p->path, type, result + sz,
  964. maxlen - sz);
  965. }
  966. }
  967. break;
  968. }
  969. spin_unlock_irqrestore(&m->lock, flags);
  970. return 0;
  971. }
  972. static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
  973. {
  974. int r;
  975. struct dm_dev *dev;
  976. struct multipath *m = (struct multipath *) ti->private;
  977. action_fn action;
  978. if (argc == 1) {
  979. if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
  980. return queue_if_no_path(m, 1, 0);
  981. else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
  982. return queue_if_no_path(m, 0, 0);
  983. }
  984. if (argc != 2)
  985. goto error;
  986. if (!strnicmp(argv[0], MESG_STR("disable_group")))
  987. return bypass_pg_num(m, argv[1], 1);
  988. else if (!strnicmp(argv[0], MESG_STR("enable_group")))
  989. return bypass_pg_num(m, argv[1], 0);
  990. else if (!strnicmp(argv[0], MESG_STR("switch_group")))
  991. return switch_pg_num(m, argv[1]);
  992. else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
  993. action = reinstate_path;
  994. else if (!strnicmp(argv[0], MESG_STR("fail_path")))
  995. action = fail_path;
  996. else
  997. goto error;
  998. r = dm_get_device(ti, argv[1], ti->begin, ti->len,
  999. dm_table_get_mode(ti->table), &dev);
  1000. if (r) {
  1001. DMWARN("message: error getting device %s",
  1002. argv[1]);
  1003. return -EINVAL;
  1004. }
  1005. r = action_dev(m, dev, action);
  1006. dm_put_device(ti, dev);
  1007. return r;
  1008. error:
  1009. DMWARN("Unrecognised multipath message received.");
  1010. return -EINVAL;
  1011. }
  1012. /*-----------------------------------------------------------------
  1013. * Module setup
  1014. *---------------------------------------------------------------*/
  1015. static struct target_type multipath_target = {
  1016. .name = "multipath",
  1017. .version = {1, 0, 4},
  1018. .module = THIS_MODULE,
  1019. .ctr = multipath_ctr,
  1020. .dtr = multipath_dtr,
  1021. .map = multipath_map,
  1022. .end_io = multipath_end_io,
  1023. .presuspend = multipath_presuspend,
  1024. .resume = multipath_resume,
  1025. .status = multipath_status,
  1026. .message = multipath_message,
  1027. };
  1028. static int __init dm_multipath_init(void)
  1029. {
  1030. int r;
  1031. /* allocate a slab for the dm_ios */
  1032. _mpio_cache = kmem_cache_create("dm_mpath", sizeof(struct mpath_io),
  1033. 0, 0, NULL, NULL);
  1034. if (!_mpio_cache)
  1035. return -ENOMEM;
  1036. r = dm_register_target(&multipath_target);
  1037. if (r < 0) {
  1038. DMERR("%s: register failed %d", multipath_target.name, r);
  1039. kmem_cache_destroy(_mpio_cache);
  1040. return -EINVAL;
  1041. }
  1042. kmultipathd = create_workqueue("kmpathd");
  1043. if (!kmultipathd) {
  1044. DMERR("%s: failed to create workqueue kmpathd",
  1045. multipath_target.name);
  1046. dm_unregister_target(&multipath_target);
  1047. kmem_cache_destroy(_mpio_cache);
  1048. return -ENOMEM;
  1049. }
  1050. DMINFO("version %u.%u.%u loaded",
  1051. multipath_target.version[0], multipath_target.version[1],
  1052. multipath_target.version[2]);
  1053. return r;
  1054. }
  1055. static void __exit dm_multipath_exit(void)
  1056. {
  1057. int r;
  1058. destroy_workqueue(kmultipathd);
  1059. r = dm_unregister_target(&multipath_target);
  1060. if (r < 0)
  1061. DMERR("%s: target unregister failed %d",
  1062. multipath_target.name, r);
  1063. kmem_cache_destroy(_mpio_cache);
  1064. }
  1065. EXPORT_SYMBOL_GPL(dm_pg_init_complete);
  1066. module_init(dm_multipath_init);
  1067. module_exit(dm_multipath_exit);
  1068. MODULE_DESCRIPTION(DM_NAME " multipath target");
  1069. MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
  1070. MODULE_LICENSE("GPL");