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_slab_pool(MIN_IOS, _mpio_cache);
  142. if (!m->mpio_pool) {
  143. kfree(m);
  144. return NULL;
  145. }
  146. }
  147. return m;
  148. }
  149. static void free_multipath(struct multipath *m)
  150. {
  151. struct priority_group *pg, *tmp;
  152. struct hw_handler *hwh = &m->hw_handler;
  153. list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
  154. list_del(&pg->list);
  155. free_priority_group(pg, m->ti);
  156. }
  157. if (hwh->type) {
  158. hwh->type->destroy(hwh);
  159. dm_put_hw_handler(hwh->type);
  160. }
  161. mempool_destroy(m->mpio_pool);
  162. kfree(m);
  163. }
  164. /*-----------------------------------------------
  165. * Path selection
  166. *-----------------------------------------------*/
  167. static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
  168. {
  169. struct hw_handler *hwh = &m->hw_handler;
  170. m->current_pg = pgpath->pg;
  171. /* Must we initialise the PG first, and queue I/O till it's ready? */
  172. if (hwh->type && hwh->type->pg_init) {
  173. m->pg_init_required = 1;
  174. m->queue_io = 1;
  175. } else {
  176. m->pg_init_required = 0;
  177. m->queue_io = 0;
  178. }
  179. }
  180. static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
  181. {
  182. struct path *path;
  183. path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
  184. if (!path)
  185. return -ENXIO;
  186. m->current_pgpath = path_to_pgpath(path);
  187. if (m->current_pg != pg)
  188. __switch_pg(m, m->current_pgpath);
  189. return 0;
  190. }
  191. static void __choose_pgpath(struct multipath *m)
  192. {
  193. struct priority_group *pg;
  194. unsigned bypassed = 1;
  195. if (!m->nr_valid_paths)
  196. goto failed;
  197. /* Were we instructed to switch PG? */
  198. if (m->next_pg) {
  199. pg = m->next_pg;
  200. m->next_pg = NULL;
  201. if (!__choose_path_in_pg(m, pg))
  202. return;
  203. }
  204. /* Don't change PG until it has no remaining paths */
  205. if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
  206. return;
  207. /*
  208. * Loop through priority groups until we find a valid path.
  209. * First time we skip PGs marked 'bypassed'.
  210. * Second time we only try the ones we skipped.
  211. */
  212. do {
  213. list_for_each_entry(pg, &m->priority_groups, list) {
  214. if (pg->bypassed == bypassed)
  215. continue;
  216. if (!__choose_path_in_pg(m, pg))
  217. return;
  218. }
  219. } while (bypassed--);
  220. failed:
  221. m->current_pgpath = NULL;
  222. m->current_pg = NULL;
  223. }
  224. static int map_io(struct multipath *m, struct bio *bio, struct mpath_io *mpio,
  225. unsigned was_queued)
  226. {
  227. int r = 1;
  228. unsigned long flags;
  229. struct pgpath *pgpath;
  230. spin_lock_irqsave(&m->lock, flags);
  231. /* Do we need to select a new pgpath? */
  232. if (!m->current_pgpath ||
  233. (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
  234. __choose_pgpath(m);
  235. pgpath = m->current_pgpath;
  236. if (was_queued)
  237. m->queue_size--;
  238. if ((pgpath && m->queue_io) ||
  239. (!pgpath && m->queue_if_no_path)) {
  240. /* Queue for the daemon to resubmit */
  241. bio_list_add(&m->queued_ios, bio);
  242. m->queue_size++;
  243. if ((m->pg_init_required && !m->pg_init_in_progress) ||
  244. !m->queue_io)
  245. queue_work(kmultipathd, &m->process_queued_ios);
  246. pgpath = NULL;
  247. r = 0;
  248. } else if (!pgpath)
  249. r = -EIO; /* Failed */
  250. else
  251. bio->bi_bdev = pgpath->path.dev->bdev;
  252. mpio->pgpath = pgpath;
  253. spin_unlock_irqrestore(&m->lock, flags);
  254. return r;
  255. }
  256. /*
  257. * If we run out of usable paths, should we queue I/O or error it?
  258. */
  259. static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
  260. unsigned save_old_value)
  261. {
  262. unsigned long flags;
  263. spin_lock_irqsave(&m->lock, flags);
  264. if (save_old_value)
  265. m->saved_queue_if_no_path = m->queue_if_no_path;
  266. else
  267. m->saved_queue_if_no_path = queue_if_no_path;
  268. m->queue_if_no_path = queue_if_no_path;
  269. if (!m->queue_if_no_path && m->queue_size)
  270. queue_work(kmultipathd, &m->process_queued_ios);
  271. spin_unlock_irqrestore(&m->lock, flags);
  272. return 0;
  273. }
  274. /*-----------------------------------------------------------------
  275. * The multipath daemon is responsible for resubmitting queued ios.
  276. *---------------------------------------------------------------*/
  277. static void dispatch_queued_ios(struct multipath *m)
  278. {
  279. int r;
  280. unsigned long flags;
  281. struct bio *bio = NULL, *next;
  282. struct mpath_io *mpio;
  283. union map_info *info;
  284. spin_lock_irqsave(&m->lock, flags);
  285. bio = bio_list_get(&m->queued_ios);
  286. spin_unlock_irqrestore(&m->lock, flags);
  287. while (bio) {
  288. next = bio->bi_next;
  289. bio->bi_next = NULL;
  290. info = dm_get_mapinfo(bio);
  291. mpio = info->ptr;
  292. r = map_io(m, bio, mpio, 1);
  293. if (r < 0)
  294. bio_endio(bio, bio->bi_size, r);
  295. else if (r == 1)
  296. generic_make_request(bio);
  297. bio = next;
  298. }
  299. }
  300. static void process_queued_ios(void *data)
  301. {
  302. struct multipath *m = (struct multipath *) data;
  303. struct hw_handler *hwh = &m->hw_handler;
  304. struct pgpath *pgpath = NULL;
  305. unsigned init_required = 0, must_queue = 1;
  306. unsigned long flags;
  307. spin_lock_irqsave(&m->lock, flags);
  308. if (!m->queue_size)
  309. goto out;
  310. if (!m->current_pgpath)
  311. __choose_pgpath(m);
  312. pgpath = m->current_pgpath;
  313. if ((pgpath && !m->queue_io) ||
  314. (!pgpath && !m->queue_if_no_path))
  315. must_queue = 0;
  316. if (m->pg_init_required && !m->pg_init_in_progress) {
  317. m->pg_init_required = 0;
  318. m->pg_init_in_progress = 1;
  319. init_required = 1;
  320. }
  321. out:
  322. spin_unlock_irqrestore(&m->lock, flags);
  323. if (init_required)
  324. hwh->type->pg_init(hwh, pgpath->pg->bypassed, &pgpath->path);
  325. if (!must_queue)
  326. dispatch_queued_ios(m);
  327. }
  328. /*
  329. * An event is triggered whenever a path is taken out of use.
  330. * Includes path failure and PG bypass.
  331. */
  332. static void trigger_event(void *data)
  333. {
  334. struct multipath *m = (struct multipath *) data;
  335. dm_table_event(m->ti->table);
  336. }
  337. /*-----------------------------------------------------------------
  338. * Constructor/argument parsing:
  339. * <#multipath feature args> [<arg>]*
  340. * <#hw_handler args> [hw_handler [<arg>]*]
  341. * <#priority groups>
  342. * <initial priority group>
  343. * [<selector> <#selector args> [<arg>]*
  344. * <#paths> <#per-path selector args>
  345. * [<path> [<arg>]* ]+ ]+
  346. *---------------------------------------------------------------*/
  347. struct param {
  348. unsigned min;
  349. unsigned max;
  350. char *error;
  351. };
  352. #define ESTR(s) ("dm-multipath: " s)
  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, ESTR("invalid number of path selector args")},
  393. };
  394. pst = dm_get_path_selector(shift(as));
  395. if (!pst) {
  396. ti->error = ESTR("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 = ESTR("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 = ESTR("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 = ESTR("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, ESTR("invalid number of paths")},
  447. {0, 1024, ESTR("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 = ESTR("not enough priority group aruments");
  455. return NULL;
  456. }
  457. pg = alloc_priority_group();
  458. if (!pg) {
  459. ti->error = ESTR("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, ESTR("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 = ESTR("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 = ESTR("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, ESTR("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, ESTR("invalid number of priority groups")},
  550. {1, 1024, ESTR("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 = ESTR("can't allocate multipath");
  562. return -EINVAL;
  563. }
  564. r = parse_features(&as, m, ti);
  565. if (r)
  566. goto bad;
  567. r = parse_hw_handler(&as, m, ti);
  568. if (r)
  569. goto bad;
  570. r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
  571. if (r)
  572. goto bad;
  573. r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
  574. if (r)
  575. goto bad;
  576. /* parse the priority groups */
  577. while (as.argc) {
  578. struct priority_group *pg;
  579. pg = parse_priority_group(&as, m, ti);
  580. if (!pg) {
  581. r = -EINVAL;
  582. goto bad;
  583. }
  584. m->nr_valid_paths += pg->nr_pgpaths;
  585. list_add_tail(&pg->list, &m->priority_groups);
  586. pg_count++;
  587. pg->pg_num = pg_count;
  588. if (!--next_pg_num)
  589. m->next_pg = pg;
  590. }
  591. if (pg_count != m->nr_priority_groups) {
  592. ti->error = ESTR("priority group count mismatch");
  593. r = -EINVAL;
  594. goto bad;
  595. }
  596. ti->private = m;
  597. m->ti = ti;
  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("dm-multipath: 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("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");