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 MESG_STR(x) x, sizeof(x)
  22. /* Path properties */
  23. struct pgpath {
  24. struct list_head list;
  25. struct priority_group *pg; /* Owning PG */
  26. unsigned fail_count; /* Cumulative failure count */
  27. struct path path;
  28. };
  29. #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
  30. /*
  31. * Paths are grouped into Priority Groups and numbered from 1 upwards.
  32. * Each has a path selector which controls which path gets used.
  33. */
  34. struct priority_group {
  35. struct list_head list;
  36. struct multipath *m; /* Owning multipath instance */
  37. struct path_selector ps;
  38. unsigned pg_num; /* Reference number */
  39. unsigned bypassed; /* Temporarily bypass this PG? */
  40. unsigned nr_pgpaths; /* Number of paths in PG */
  41. struct list_head pgpaths;
  42. };
  43. /* Multipath context */
  44. struct multipath {
  45. struct list_head list;
  46. struct dm_target *ti;
  47. spinlock_t lock;
  48. struct hw_handler hw_handler;
  49. unsigned nr_priority_groups;
  50. struct list_head priority_groups;
  51. unsigned pg_init_required; /* pg_init needs calling? */
  52. unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
  53. unsigned nr_valid_paths; /* Total number of usable paths */
  54. struct pgpath *current_pgpath;
  55. struct priority_group *current_pg;
  56. struct priority_group *next_pg; /* Switch to this PG if set */
  57. unsigned repeat_count; /* I/Os left before calling PS again */
  58. unsigned queue_io; /* Must we queue all I/O? */
  59. unsigned queue_if_no_path; /* Queue I/O if last path fails? */
  60. unsigned saved_queue_if_no_path;/* Saved state during suspension */
  61. struct work_struct process_queued_ios;
  62. struct bio_list queued_ios;
  63. unsigned queue_size;
  64. struct work_struct trigger_event;
  65. /*
  66. * We must use a mempool of mpath_io structs so that we
  67. * can resubmit bios on error.
  68. */
  69. mempool_t *mpio_pool;
  70. };
  71. /*
  72. * Context information attached to each bio we process.
  73. */
  74. struct mpath_io {
  75. struct pgpath *pgpath;
  76. struct dm_bio_details details;
  77. };
  78. typedef int (*action_fn) (struct pgpath *pgpath);
  79. #define MIN_IOS 256 /* Mempool size */
  80. static kmem_cache_t *_mpio_cache;
  81. struct workqueue_struct *kmultipathd;
  82. static void process_queued_ios(void *data);
  83. static void trigger_event(void *data);
  84. /*-----------------------------------------------
  85. * Allocation routines
  86. *-----------------------------------------------*/
  87. static struct pgpath *alloc_pgpath(void)
  88. {
  89. struct pgpath *pgpath = kmalloc(sizeof(*pgpath), GFP_KERNEL);
  90. if (pgpath) {
  91. memset(pgpath, 0, sizeof(*pgpath));
  92. pgpath->path.is_active = 1;
  93. }
  94. return pgpath;
  95. }
  96. static inline void free_pgpath(struct pgpath *pgpath)
  97. {
  98. kfree(pgpath);
  99. }
  100. static struct priority_group *alloc_priority_group(void)
  101. {
  102. struct priority_group *pg;
  103. pg = kmalloc(sizeof(*pg), GFP_KERNEL);
  104. if (!pg)
  105. return NULL;
  106. memset(pg, 0, sizeof(*pg));
  107. INIT_LIST_HEAD(&pg->pgpaths);
  108. return pg;
  109. }
  110. static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
  111. {
  112. struct pgpath *pgpath, *tmp;
  113. list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
  114. list_del(&pgpath->list);
  115. dm_put_device(ti, pgpath->path.dev);
  116. free_pgpath(pgpath);
  117. }
  118. }
  119. static void free_priority_group(struct priority_group *pg,
  120. struct dm_target *ti)
  121. {
  122. struct path_selector *ps = &pg->ps;
  123. if (ps->type) {
  124. ps->type->destroy(ps);
  125. dm_put_path_selector(ps->type);
  126. }
  127. free_pgpaths(&pg->pgpaths, ti);
  128. kfree(pg);
  129. }
  130. static struct multipath *alloc_multipath(void)
  131. {
  132. struct multipath *m;
  133. m = kmalloc(sizeof(*m), GFP_KERNEL);
  134. if (m) {
  135. memset(m, 0, sizeof(*m));
  136. INIT_LIST_HEAD(&m->priority_groups);
  137. spin_lock_init(&m->lock);
  138. m->queue_io = 1;
  139. INIT_WORK(&m->process_queued_ios, process_queued_ios, m);
  140. INIT_WORK(&m->trigger_event, trigger_event, m);
  141. m->mpio_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
  142. mempool_free_slab, _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. #define ESTR(s) ("dm-multipath: " s)
  354. static int read_param(struct param *param, char *str, unsigned *v, char **error)
  355. {
  356. if (!str ||
  357. (sscanf(str, "%u", v) != 1) ||
  358. (*v < param->min) ||
  359. (*v > param->max)) {
  360. *error = param->error;
  361. return -EINVAL;
  362. }
  363. return 0;
  364. }
  365. struct arg_set {
  366. unsigned argc;
  367. char **argv;
  368. };
  369. static char *shift(struct arg_set *as)
  370. {
  371. char *r;
  372. if (as->argc) {
  373. as->argc--;
  374. r = *as->argv;
  375. as->argv++;
  376. return r;
  377. }
  378. return NULL;
  379. }
  380. static void consume(struct arg_set *as, unsigned n)
  381. {
  382. BUG_ON (as->argc < n);
  383. as->argc -= n;
  384. as->argv += n;
  385. }
  386. static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
  387. struct dm_target *ti)
  388. {
  389. int r;
  390. struct path_selector_type *pst;
  391. unsigned ps_argc;
  392. static struct param _params[] = {
  393. {0, 1024, ESTR("invalid number of path selector args")},
  394. };
  395. pst = dm_get_path_selector(shift(as));
  396. if (!pst) {
  397. ti->error = ESTR("unknown path selector type");
  398. return -EINVAL;
  399. }
  400. r = read_param(_params, shift(as), &ps_argc, &ti->error);
  401. if (r)
  402. return -EINVAL;
  403. r = pst->create(&pg->ps, ps_argc, as->argv);
  404. if (r) {
  405. dm_put_path_selector(pst);
  406. ti->error = ESTR("path selector constructor failed");
  407. return r;
  408. }
  409. pg->ps.type = pst;
  410. consume(as, ps_argc);
  411. return 0;
  412. }
  413. static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
  414. struct dm_target *ti)
  415. {
  416. int r;
  417. struct pgpath *p;
  418. /* we need at least a path arg */
  419. if (as->argc < 1) {
  420. ti->error = ESTR("no device given");
  421. return NULL;
  422. }
  423. p = alloc_pgpath();
  424. if (!p)
  425. return NULL;
  426. r = dm_get_device(ti, shift(as), ti->begin, ti->len,
  427. dm_table_get_mode(ti->table), &p->path.dev);
  428. if (r) {
  429. ti->error = ESTR("error getting device");
  430. goto bad;
  431. }
  432. r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
  433. if (r) {
  434. dm_put_device(ti, p->path.dev);
  435. goto bad;
  436. }
  437. return p;
  438. bad:
  439. free_pgpath(p);
  440. return NULL;
  441. }
  442. static struct priority_group *parse_priority_group(struct arg_set *as,
  443. struct multipath *m,
  444. struct dm_target *ti)
  445. {
  446. static struct param _params[] = {
  447. {1, 1024, ESTR("invalid number of paths")},
  448. {0, 1024, ESTR("invalid number of selector args")}
  449. };
  450. int r;
  451. unsigned i, nr_selector_args, nr_params;
  452. struct priority_group *pg;
  453. if (as->argc < 2) {
  454. as->argc = 0;
  455. ti->error = ESTR("not enough priority group aruments");
  456. return NULL;
  457. }
  458. pg = alloc_priority_group();
  459. if (!pg) {
  460. ti->error = ESTR("couldn't allocate priority group");
  461. return NULL;
  462. }
  463. pg->m = m;
  464. r = parse_path_selector(as, pg, ti);
  465. if (r)
  466. goto bad;
  467. /*
  468. * read the paths
  469. */
  470. r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
  471. if (r)
  472. goto bad;
  473. r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
  474. if (r)
  475. goto bad;
  476. nr_params = 1 + nr_selector_args;
  477. for (i = 0; i < pg->nr_pgpaths; i++) {
  478. struct pgpath *pgpath;
  479. struct arg_set path_args;
  480. if (as->argc < nr_params)
  481. goto bad;
  482. path_args.argc = nr_params;
  483. path_args.argv = as->argv;
  484. pgpath = parse_path(&path_args, &pg->ps, ti);
  485. if (!pgpath)
  486. goto bad;
  487. pgpath->pg = pg;
  488. list_add_tail(&pgpath->list, &pg->pgpaths);
  489. consume(as, nr_params);
  490. }
  491. return pg;
  492. bad:
  493. free_priority_group(pg, ti);
  494. return NULL;
  495. }
  496. static int parse_hw_handler(struct arg_set *as, struct multipath *m,
  497. struct dm_target *ti)
  498. {
  499. int r;
  500. struct hw_handler_type *hwht;
  501. unsigned hw_argc;
  502. static struct param _params[] = {
  503. {0, 1024, ESTR("invalid number of hardware handler args")},
  504. };
  505. r = read_param(_params, shift(as), &hw_argc, &ti->error);
  506. if (r)
  507. return -EINVAL;
  508. if (!hw_argc)
  509. return 0;
  510. hwht = dm_get_hw_handler(shift(as));
  511. if (!hwht) {
  512. ti->error = ESTR("unknown hardware handler type");
  513. return -EINVAL;
  514. }
  515. r = hwht->create(&m->hw_handler, hw_argc - 1, as->argv);
  516. if (r) {
  517. dm_put_hw_handler(hwht);
  518. ti->error = ESTR("hardware handler constructor failed");
  519. return r;
  520. }
  521. m->hw_handler.type = hwht;
  522. consume(as, hw_argc - 1);
  523. return 0;
  524. }
  525. static int parse_features(struct arg_set *as, struct multipath *m,
  526. struct dm_target *ti)
  527. {
  528. int r;
  529. unsigned argc;
  530. static struct param _params[] = {
  531. {0, 1, ESTR("invalid number of feature args")},
  532. };
  533. r = read_param(_params, shift(as), &argc, &ti->error);
  534. if (r)
  535. return -EINVAL;
  536. if (!argc)
  537. return 0;
  538. if (!strnicmp(shift(as), MESG_STR("queue_if_no_path")))
  539. return queue_if_no_path(m, 1, 0);
  540. else {
  541. ti->error = "Unrecognised multipath feature request";
  542. return -EINVAL;
  543. }
  544. }
  545. static int multipath_ctr(struct dm_target *ti, unsigned int argc,
  546. char **argv)
  547. {
  548. /* target parameters */
  549. static struct param _params[] = {
  550. {1, 1024, ESTR("invalid number of priority groups")},
  551. {1, 1024, ESTR("invalid initial priority group number")},
  552. };
  553. int r;
  554. struct multipath *m;
  555. struct arg_set as;
  556. unsigned pg_count = 0;
  557. unsigned next_pg_num;
  558. as.argc = argc;
  559. as.argv = argv;
  560. m = alloc_multipath();
  561. if (!m) {
  562. ti->error = ESTR("can't allocate multipath");
  563. return -EINVAL;
  564. }
  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 = ESTR("priority group count mismatch");
  594. r = -EINVAL;
  595. goto bad;
  596. }
  597. ti->private = m;
  598. m->ti = ti;
  599. return 0;
  600. bad:
  601. free_multipath(m);
  602. return r;
  603. }
  604. static void multipath_dtr(struct dm_target *ti)
  605. {
  606. struct multipath *m = (struct multipath *) ti->private;
  607. flush_workqueue(kmultipathd);
  608. free_multipath(m);
  609. }
  610. /*
  611. * Map bios, recording original fields for later in case we have to resubmit
  612. */
  613. static int multipath_map(struct dm_target *ti, struct bio *bio,
  614. union map_info *map_context)
  615. {
  616. int r;
  617. struct mpath_io *mpio;
  618. struct multipath *m = (struct multipath *) ti->private;
  619. if (bio_barrier(bio))
  620. return -EOPNOTSUPP;
  621. mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
  622. dm_bio_record(&mpio->details, bio);
  623. map_context->ptr = mpio;
  624. bio->bi_rw |= (1 << BIO_RW_FAILFAST);
  625. r = map_io(m, bio, mpio, 0);
  626. if (r < 0)
  627. mempool_free(mpio, m->mpio_pool);
  628. return r;
  629. }
  630. /*
  631. * Take a path out of use.
  632. */
  633. static int fail_path(struct pgpath *pgpath)
  634. {
  635. unsigned long flags;
  636. struct multipath *m = pgpath->pg->m;
  637. spin_lock_irqsave(&m->lock, flags);
  638. if (!pgpath->path.is_active)
  639. goto out;
  640. DMWARN("dm-multipath: Failing path %s.", pgpath->path.dev->name);
  641. pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
  642. pgpath->path.is_active = 0;
  643. pgpath->fail_count++;
  644. m->nr_valid_paths--;
  645. if (pgpath == m->current_pgpath)
  646. m->current_pgpath = NULL;
  647. queue_work(kmultipathd, &m->trigger_event);
  648. out:
  649. spin_unlock_irqrestore(&m->lock, flags);
  650. return 0;
  651. }
  652. /*
  653. * Reinstate a previously-failed path
  654. */
  655. static int reinstate_path(struct pgpath *pgpath)
  656. {
  657. int r = 0;
  658. unsigned long flags;
  659. struct multipath *m = pgpath->pg->m;
  660. spin_lock_irqsave(&m->lock, flags);
  661. if (pgpath->path.is_active)
  662. goto out;
  663. if (!pgpath->pg->ps.type) {
  664. DMWARN("Reinstate path not supported by path selector %s",
  665. pgpath->pg->ps.type->name);
  666. r = -EINVAL;
  667. goto out;
  668. }
  669. r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
  670. if (r)
  671. goto out;
  672. pgpath->path.is_active = 1;
  673. m->current_pgpath = NULL;
  674. if (!m->nr_valid_paths++ && m->queue_size)
  675. queue_work(kmultipathd, &m->process_queued_ios);
  676. queue_work(kmultipathd, &m->trigger_event);
  677. out:
  678. spin_unlock_irqrestore(&m->lock, flags);
  679. return r;
  680. }
  681. /*
  682. * Fail or reinstate all paths that match the provided struct dm_dev.
  683. */
  684. static int action_dev(struct multipath *m, struct dm_dev *dev,
  685. action_fn action)
  686. {
  687. int r = 0;
  688. struct pgpath *pgpath;
  689. struct priority_group *pg;
  690. list_for_each_entry(pg, &m->priority_groups, list) {
  691. list_for_each_entry(pgpath, &pg->pgpaths, list) {
  692. if (pgpath->path.dev == dev)
  693. r = action(pgpath);
  694. }
  695. }
  696. return r;
  697. }
  698. /*
  699. * Temporarily try to avoid having to use the specified PG
  700. */
  701. static void bypass_pg(struct multipath *m, struct priority_group *pg,
  702. int bypassed)
  703. {
  704. unsigned long flags;
  705. spin_lock_irqsave(&m->lock, flags);
  706. pg->bypassed = bypassed;
  707. m->current_pgpath = NULL;
  708. m->current_pg = NULL;
  709. spin_unlock_irqrestore(&m->lock, flags);
  710. queue_work(kmultipathd, &m->trigger_event);
  711. }
  712. /*
  713. * Switch to using the specified PG from the next I/O that gets mapped
  714. */
  715. static int switch_pg_num(struct multipath *m, const char *pgstr)
  716. {
  717. struct priority_group *pg;
  718. unsigned pgnum;
  719. unsigned long flags;
  720. if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
  721. (pgnum > m->nr_priority_groups)) {
  722. DMWARN("invalid PG number supplied to switch_pg_num");
  723. return -EINVAL;
  724. }
  725. spin_lock_irqsave(&m->lock, flags);
  726. list_for_each_entry(pg, &m->priority_groups, list) {
  727. pg->bypassed = 0;
  728. if (--pgnum)
  729. continue;
  730. m->current_pgpath = NULL;
  731. m->current_pg = NULL;
  732. m->next_pg = pg;
  733. }
  734. spin_unlock_irqrestore(&m->lock, flags);
  735. queue_work(kmultipathd, &m->trigger_event);
  736. return 0;
  737. }
  738. /*
  739. * Set/clear bypassed status of a PG.
  740. * PGs are numbered upwards from 1 in the order they were declared.
  741. */
  742. static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
  743. {
  744. struct priority_group *pg;
  745. unsigned pgnum;
  746. if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
  747. (pgnum > m->nr_priority_groups)) {
  748. DMWARN("invalid PG number supplied to bypass_pg");
  749. return -EINVAL;
  750. }
  751. list_for_each_entry(pg, &m->priority_groups, list) {
  752. if (!--pgnum)
  753. break;
  754. }
  755. bypass_pg(m, pg, bypassed);
  756. return 0;
  757. }
  758. /*
  759. * pg_init must call this when it has completed its initialisation
  760. */
  761. void dm_pg_init_complete(struct path *path, unsigned err_flags)
  762. {
  763. struct pgpath *pgpath = path_to_pgpath(path);
  764. struct priority_group *pg = pgpath->pg;
  765. struct multipath *m = pg->m;
  766. unsigned long flags;
  767. /* We insist on failing the path if the PG is already bypassed. */
  768. if (err_flags && pg->bypassed)
  769. err_flags |= MP_FAIL_PATH;
  770. if (err_flags & MP_FAIL_PATH)
  771. fail_path(pgpath);
  772. if (err_flags & MP_BYPASS_PG)
  773. bypass_pg(m, pg, 1);
  774. spin_lock_irqsave(&m->lock, flags);
  775. if (err_flags) {
  776. m->current_pgpath = NULL;
  777. m->current_pg = NULL;
  778. } else if (!m->pg_init_required)
  779. m->queue_io = 0;
  780. m->pg_init_in_progress = 0;
  781. queue_work(kmultipathd, &m->process_queued_ios);
  782. spin_unlock_irqrestore(&m->lock, flags);
  783. }
  784. /*
  785. * end_io handling
  786. */
  787. static int do_end_io(struct multipath *m, struct bio *bio,
  788. int error, struct mpath_io *mpio)
  789. {
  790. struct hw_handler *hwh = &m->hw_handler;
  791. unsigned err_flags = MP_FAIL_PATH; /* Default behavior */
  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(&m->lock);
  799. if (!m->nr_valid_paths) {
  800. if (!m->queue_if_no_path) {
  801. spin_unlock(&m->lock);
  802. return -EIO;
  803. } else {
  804. spin_unlock(&m->lock);
  805. goto requeue;
  806. }
  807. }
  808. spin_unlock(&m->lock);
  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(&m->lock);
  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(&m->lock);
  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("dm-multipath 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("dm-multipath 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");