dm-mpath.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  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. {
  262. unsigned long flags;
  263. spin_lock_irqsave(&m->lock, flags);
  264. m->saved_queue_if_no_path = m->queue_if_no_path;
  265. m->queue_if_no_path = queue_if_no_path;
  266. if (!m->queue_if_no_path && m->queue_size)
  267. queue_work(kmultipathd, &m->process_queued_ios);
  268. spin_unlock_irqrestore(&m->lock, flags);
  269. return 0;
  270. }
  271. /*-----------------------------------------------------------------
  272. * The multipath daemon is responsible for resubmitting queued ios.
  273. *---------------------------------------------------------------*/
  274. static void dispatch_queued_ios(struct multipath *m)
  275. {
  276. int r;
  277. unsigned long flags;
  278. struct bio *bio = NULL, *next;
  279. struct mpath_io *mpio;
  280. union map_info *info;
  281. spin_lock_irqsave(&m->lock, flags);
  282. bio = bio_list_get(&m->queued_ios);
  283. spin_unlock_irqrestore(&m->lock, flags);
  284. while (bio) {
  285. next = bio->bi_next;
  286. bio->bi_next = NULL;
  287. info = dm_get_mapinfo(bio);
  288. mpio = info->ptr;
  289. r = map_io(m, bio, mpio, 1);
  290. if (r < 0)
  291. bio_endio(bio, bio->bi_size, r);
  292. else if (r == 1)
  293. generic_make_request(bio);
  294. bio = next;
  295. }
  296. }
  297. static void process_queued_ios(void *data)
  298. {
  299. struct multipath *m = (struct multipath *) data;
  300. struct hw_handler *hwh = &m->hw_handler;
  301. struct pgpath *pgpath = NULL;
  302. unsigned init_required = 0, must_queue = 1;
  303. unsigned long flags;
  304. spin_lock_irqsave(&m->lock, flags);
  305. if (!m->queue_size)
  306. goto out;
  307. if (!m->current_pgpath)
  308. __choose_pgpath(m);
  309. pgpath = m->current_pgpath;
  310. if ((pgpath && !m->queue_io) ||
  311. (!pgpath && !m->queue_if_no_path))
  312. must_queue = 0;
  313. if (m->pg_init_required && !m->pg_init_in_progress) {
  314. m->pg_init_required = 0;
  315. m->pg_init_in_progress = 1;
  316. init_required = 1;
  317. }
  318. out:
  319. spin_unlock_irqrestore(&m->lock, flags);
  320. if (init_required)
  321. hwh->type->pg_init(hwh, pgpath->pg->bypassed, &pgpath->path);
  322. if (!must_queue)
  323. dispatch_queued_ios(m);
  324. }
  325. /*
  326. * An event is triggered whenever a path is taken out of use.
  327. * Includes path failure and PG bypass.
  328. */
  329. static void trigger_event(void *data)
  330. {
  331. struct multipath *m = (struct multipath *) data;
  332. dm_table_event(m->ti->table);
  333. }
  334. /*-----------------------------------------------------------------
  335. * Constructor/argument parsing:
  336. * <#multipath feature args> [<arg>]*
  337. * <#hw_handler args> [hw_handler [<arg>]*]
  338. * <#priority groups>
  339. * <initial priority group>
  340. * [<selector> <#selector args> [<arg>]*
  341. * <#paths> <#per-path selector args>
  342. * [<path> [<arg>]* ]+ ]+
  343. *---------------------------------------------------------------*/
  344. struct param {
  345. unsigned min;
  346. unsigned max;
  347. char *error;
  348. };
  349. #define ESTR(s) ("dm-multipath: " s)
  350. static int read_param(struct param *param, char *str, unsigned *v, char **error)
  351. {
  352. if (!str ||
  353. (sscanf(str, "%u", v) != 1) ||
  354. (*v < param->min) ||
  355. (*v > param->max)) {
  356. *error = param->error;
  357. return -EINVAL;
  358. }
  359. return 0;
  360. }
  361. struct arg_set {
  362. unsigned argc;
  363. char **argv;
  364. };
  365. static char *shift(struct arg_set *as)
  366. {
  367. char *r;
  368. if (as->argc) {
  369. as->argc--;
  370. r = *as->argv;
  371. as->argv++;
  372. return r;
  373. }
  374. return NULL;
  375. }
  376. static void consume(struct arg_set *as, unsigned n)
  377. {
  378. BUG_ON (as->argc < n);
  379. as->argc -= n;
  380. as->argv += n;
  381. }
  382. static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
  383. struct dm_target *ti)
  384. {
  385. int r;
  386. struct path_selector_type *pst;
  387. unsigned ps_argc;
  388. static struct param _params[] = {
  389. {0, 1024, ESTR("invalid number of path selector args")},
  390. };
  391. pst = dm_get_path_selector(shift(as));
  392. if (!pst) {
  393. ti->error = ESTR("unknown path selector type");
  394. return -EINVAL;
  395. }
  396. r = read_param(_params, shift(as), &ps_argc, &ti->error);
  397. if (r)
  398. return -EINVAL;
  399. r = pst->create(&pg->ps, ps_argc, as->argv);
  400. if (r) {
  401. dm_put_path_selector(pst);
  402. ti->error = ESTR("path selector constructor failed");
  403. return r;
  404. }
  405. pg->ps.type = pst;
  406. consume(as, ps_argc);
  407. return 0;
  408. }
  409. static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
  410. struct dm_target *ti)
  411. {
  412. int r;
  413. struct pgpath *p;
  414. /* we need at least a path arg */
  415. if (as->argc < 1) {
  416. ti->error = ESTR("no device given");
  417. return NULL;
  418. }
  419. p = alloc_pgpath();
  420. if (!p)
  421. return NULL;
  422. r = dm_get_device(ti, shift(as), ti->begin, ti->len,
  423. dm_table_get_mode(ti->table), &p->path.dev);
  424. if (r) {
  425. ti->error = ESTR("error getting device");
  426. goto bad;
  427. }
  428. r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
  429. if (r) {
  430. dm_put_device(ti, p->path.dev);
  431. goto bad;
  432. }
  433. return p;
  434. bad:
  435. free_pgpath(p);
  436. return NULL;
  437. }
  438. static struct priority_group *parse_priority_group(struct arg_set *as,
  439. struct multipath *m,
  440. struct dm_target *ti)
  441. {
  442. static struct param _params[] = {
  443. {1, 1024, ESTR("invalid number of paths")},
  444. {0, 1024, ESTR("invalid number of selector args")}
  445. };
  446. int r;
  447. unsigned i, nr_selector_args, nr_params;
  448. struct priority_group *pg;
  449. if (as->argc < 2) {
  450. as->argc = 0;
  451. ti->error = ESTR("not enough priority group aruments");
  452. return NULL;
  453. }
  454. pg = alloc_priority_group();
  455. if (!pg) {
  456. ti->error = ESTR("couldn't allocate priority group");
  457. return NULL;
  458. }
  459. pg->m = m;
  460. r = parse_path_selector(as, pg, ti);
  461. if (r)
  462. goto bad;
  463. /*
  464. * read the paths
  465. */
  466. r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
  467. if (r)
  468. goto bad;
  469. r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
  470. if (r)
  471. goto bad;
  472. nr_params = 1 + nr_selector_args;
  473. for (i = 0; i < pg->nr_pgpaths; i++) {
  474. struct pgpath *pgpath;
  475. struct arg_set path_args;
  476. if (as->argc < nr_params)
  477. goto bad;
  478. path_args.argc = nr_params;
  479. path_args.argv = as->argv;
  480. pgpath = parse_path(&path_args, &pg->ps, ti);
  481. if (!pgpath)
  482. goto bad;
  483. pgpath->pg = pg;
  484. list_add_tail(&pgpath->list, &pg->pgpaths);
  485. consume(as, nr_params);
  486. }
  487. return pg;
  488. bad:
  489. free_priority_group(pg, ti);
  490. return NULL;
  491. }
  492. static int parse_hw_handler(struct arg_set *as, struct multipath *m,
  493. struct dm_target *ti)
  494. {
  495. int r;
  496. struct hw_handler_type *hwht;
  497. unsigned hw_argc;
  498. static struct param _params[] = {
  499. {0, 1024, ESTR("invalid number of hardware handler args")},
  500. };
  501. r = read_param(_params, shift(as), &hw_argc, &ti->error);
  502. if (r)
  503. return -EINVAL;
  504. if (!hw_argc)
  505. return 0;
  506. hwht = dm_get_hw_handler(shift(as));
  507. if (!hwht) {
  508. ti->error = ESTR("unknown hardware handler type");
  509. return -EINVAL;
  510. }
  511. r = hwht->create(&m->hw_handler, hw_argc - 1, as->argv);
  512. if (r) {
  513. dm_put_hw_handler(hwht);
  514. ti->error = ESTR("hardware handler constructor failed");
  515. return r;
  516. }
  517. m->hw_handler.type = hwht;
  518. consume(as, hw_argc - 1);
  519. return 0;
  520. }
  521. static int parse_features(struct arg_set *as, struct multipath *m,
  522. struct dm_target *ti)
  523. {
  524. int r;
  525. unsigned argc;
  526. static struct param _params[] = {
  527. {0, 1, ESTR("invalid number of feature args")},
  528. };
  529. r = read_param(_params, shift(as), &argc, &ti->error);
  530. if (r)
  531. return -EINVAL;
  532. if (!argc)
  533. return 0;
  534. if (!strnicmp(shift(as), MESG_STR("queue_if_no_path")))
  535. return queue_if_no_path(m, 1);
  536. else {
  537. ti->error = "Unrecognised multipath feature request";
  538. return -EINVAL;
  539. }
  540. }
  541. static int multipath_ctr(struct dm_target *ti, unsigned int argc,
  542. char **argv)
  543. {
  544. /* target parameters */
  545. static struct param _params[] = {
  546. {1, 1024, ESTR("invalid number of priority groups")},
  547. {1, 1024, ESTR("invalid initial priority group number")},
  548. };
  549. int r;
  550. struct multipath *m;
  551. struct arg_set as;
  552. unsigned pg_count = 0;
  553. unsigned next_pg_num;
  554. as.argc = argc;
  555. as.argv = argv;
  556. m = alloc_multipath();
  557. if (!m) {
  558. ti->error = ESTR("can't allocate multipath");
  559. return -EINVAL;
  560. }
  561. r = parse_features(&as, m, ti);
  562. if (r)
  563. goto bad;
  564. r = parse_hw_handler(&as, m, ti);
  565. if (r)
  566. goto bad;
  567. r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
  568. if (r)
  569. goto bad;
  570. r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
  571. if (r)
  572. goto bad;
  573. /* parse the priority groups */
  574. while (as.argc) {
  575. struct priority_group *pg;
  576. pg = parse_priority_group(&as, m, ti);
  577. if (!pg) {
  578. r = -EINVAL;
  579. goto bad;
  580. }
  581. m->nr_valid_paths += pg->nr_pgpaths;
  582. list_add_tail(&pg->list, &m->priority_groups);
  583. pg_count++;
  584. pg->pg_num = pg_count;
  585. if (!--next_pg_num)
  586. m->next_pg = pg;
  587. }
  588. if (pg_count != m->nr_priority_groups) {
  589. ti->error = ESTR("priority group count mismatch");
  590. r = -EINVAL;
  591. goto bad;
  592. }
  593. ti->private = m;
  594. m->ti = ti;
  595. return 0;
  596. bad:
  597. free_multipath(m);
  598. return r;
  599. }
  600. static void multipath_dtr(struct dm_target *ti)
  601. {
  602. struct multipath *m = (struct multipath *) ti->private;
  603. flush_workqueue(kmultipathd);
  604. free_multipath(m);
  605. }
  606. /*
  607. * Map bios, recording original fields for later in case we have to resubmit
  608. */
  609. static int multipath_map(struct dm_target *ti, struct bio *bio,
  610. union map_info *map_context)
  611. {
  612. int r;
  613. struct mpath_io *mpio;
  614. struct multipath *m = (struct multipath *) ti->private;
  615. if (bio_barrier(bio))
  616. return -EOPNOTSUPP;
  617. mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
  618. dm_bio_record(&mpio->details, bio);
  619. map_context->ptr = mpio;
  620. bio->bi_rw |= (1 << BIO_RW_FAILFAST);
  621. r = map_io(m, bio, mpio, 0);
  622. if (r < 0)
  623. mempool_free(mpio, m->mpio_pool);
  624. return r;
  625. }
  626. /*
  627. * Take a path out of use.
  628. */
  629. static int fail_path(struct pgpath *pgpath)
  630. {
  631. unsigned long flags;
  632. struct multipath *m = pgpath->pg->m;
  633. spin_lock_irqsave(&m->lock, flags);
  634. if (!pgpath->path.is_active)
  635. goto out;
  636. DMWARN("dm-multipath: Failing path %s.", pgpath->path.dev->name);
  637. pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
  638. pgpath->path.is_active = 0;
  639. pgpath->fail_count++;
  640. m->nr_valid_paths--;
  641. if (pgpath == m->current_pgpath)
  642. m->current_pgpath = NULL;
  643. queue_work(kmultipathd, &m->trigger_event);
  644. out:
  645. spin_unlock_irqrestore(&m->lock, flags);
  646. return 0;
  647. }
  648. /*
  649. * Reinstate a previously-failed path
  650. */
  651. static int reinstate_path(struct pgpath *pgpath)
  652. {
  653. int r = 0;
  654. unsigned long flags;
  655. struct multipath *m = pgpath->pg->m;
  656. spin_lock_irqsave(&m->lock, flags);
  657. if (pgpath->path.is_active)
  658. goto out;
  659. if (!pgpath->pg->ps.type) {
  660. DMWARN("Reinstate path not supported by path selector %s",
  661. pgpath->pg->ps.type->name);
  662. r = -EINVAL;
  663. goto out;
  664. }
  665. r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
  666. if (r)
  667. goto out;
  668. pgpath->path.is_active = 1;
  669. m->current_pgpath = NULL;
  670. if (!m->nr_valid_paths++ && m->queue_size)
  671. queue_work(kmultipathd, &m->process_queued_ios);
  672. queue_work(kmultipathd, &m->trigger_event);
  673. out:
  674. spin_unlock_irqrestore(&m->lock, flags);
  675. return r;
  676. }
  677. /*
  678. * Fail or reinstate all paths that match the provided struct dm_dev.
  679. */
  680. static int action_dev(struct multipath *m, struct dm_dev *dev,
  681. action_fn action)
  682. {
  683. int r = 0;
  684. struct pgpath *pgpath;
  685. struct priority_group *pg;
  686. list_for_each_entry(pg, &m->priority_groups, list) {
  687. list_for_each_entry(pgpath, &pg->pgpaths, list) {
  688. if (pgpath->path.dev == dev)
  689. r = action(pgpath);
  690. }
  691. }
  692. return r;
  693. }
  694. /*
  695. * Temporarily try to avoid having to use the specified PG
  696. */
  697. static void bypass_pg(struct multipath *m, struct priority_group *pg,
  698. int bypassed)
  699. {
  700. unsigned long flags;
  701. spin_lock_irqsave(&m->lock, flags);
  702. pg->bypassed = bypassed;
  703. m->current_pgpath = NULL;
  704. m->current_pg = NULL;
  705. spin_unlock_irqrestore(&m->lock, flags);
  706. queue_work(kmultipathd, &m->trigger_event);
  707. }
  708. /*
  709. * Switch to using the specified PG from the next I/O that gets mapped
  710. */
  711. static int switch_pg_num(struct multipath *m, const char *pgstr)
  712. {
  713. struct priority_group *pg;
  714. unsigned pgnum;
  715. unsigned long flags;
  716. if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
  717. (pgnum > m->nr_priority_groups)) {
  718. DMWARN("invalid PG number supplied to switch_pg_num");
  719. return -EINVAL;
  720. }
  721. spin_lock_irqsave(&m->lock, flags);
  722. list_for_each_entry(pg, &m->priority_groups, list) {
  723. pg->bypassed = 0;
  724. if (--pgnum)
  725. continue;
  726. m->current_pgpath = NULL;
  727. m->current_pg = NULL;
  728. m->next_pg = pg;
  729. }
  730. spin_unlock_irqrestore(&m->lock, flags);
  731. queue_work(kmultipathd, &m->trigger_event);
  732. return 0;
  733. }
  734. /*
  735. * Set/clear bypassed status of a PG.
  736. * PGs are numbered upwards from 1 in the order they were declared.
  737. */
  738. static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
  739. {
  740. struct priority_group *pg;
  741. unsigned pgnum;
  742. if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
  743. (pgnum > m->nr_priority_groups)) {
  744. DMWARN("invalid PG number supplied to bypass_pg");
  745. return -EINVAL;
  746. }
  747. list_for_each_entry(pg, &m->priority_groups, list) {
  748. if (!--pgnum)
  749. break;
  750. }
  751. bypass_pg(m, pg, bypassed);
  752. return 0;
  753. }
  754. /*
  755. * pg_init must call this when it has completed its initialisation
  756. */
  757. void dm_pg_init_complete(struct path *path, unsigned err_flags)
  758. {
  759. struct pgpath *pgpath = path_to_pgpath(path);
  760. struct priority_group *pg = pgpath->pg;
  761. struct multipath *m = pg->m;
  762. unsigned long flags;
  763. /* We insist on failing the path if the PG is already bypassed. */
  764. if (err_flags && pg->bypassed)
  765. err_flags |= MP_FAIL_PATH;
  766. if (err_flags & MP_FAIL_PATH)
  767. fail_path(pgpath);
  768. if (err_flags & MP_BYPASS_PG)
  769. bypass_pg(m, pg, 1);
  770. spin_lock_irqsave(&m->lock, flags);
  771. if (err_flags) {
  772. m->current_pgpath = NULL;
  773. m->current_pg = NULL;
  774. } else if (!m->pg_init_required)
  775. m->queue_io = 0;
  776. m->pg_init_in_progress = 0;
  777. queue_work(kmultipathd, &m->process_queued_ios);
  778. spin_unlock_irqrestore(&m->lock, flags);
  779. }
  780. /*
  781. * end_io handling
  782. */
  783. static int do_end_io(struct multipath *m, struct bio *bio,
  784. int error, struct mpath_io *mpio)
  785. {
  786. struct hw_handler *hwh = &m->hw_handler;
  787. unsigned err_flags = MP_FAIL_PATH; /* Default behavior */
  788. if (!error)
  789. return 0; /* I/O complete */
  790. if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
  791. return error;
  792. if (error == -EOPNOTSUPP)
  793. return error;
  794. spin_lock(&m->lock);
  795. if (!m->nr_valid_paths) {
  796. if (!m->queue_if_no_path) {
  797. spin_unlock(&m->lock);
  798. return -EIO;
  799. } else {
  800. spin_unlock(&m->lock);
  801. goto requeue;
  802. }
  803. }
  804. spin_unlock(&m->lock);
  805. if (hwh->type && hwh->type->error)
  806. err_flags = hwh->type->error(hwh, bio);
  807. if (mpio->pgpath) {
  808. if (err_flags & MP_FAIL_PATH)
  809. fail_path(mpio->pgpath);
  810. if (err_flags & MP_BYPASS_PG)
  811. bypass_pg(m, mpio->pgpath->pg, 1);
  812. }
  813. if (err_flags & MP_ERROR_IO)
  814. return -EIO;
  815. requeue:
  816. dm_bio_restore(&mpio->details, bio);
  817. /* queue for the daemon to resubmit or fail */
  818. spin_lock(&m->lock);
  819. bio_list_add(&m->queued_ios, bio);
  820. m->queue_size++;
  821. if (!m->queue_io)
  822. queue_work(kmultipathd, &m->process_queued_ios);
  823. spin_unlock(&m->lock);
  824. return 1; /* io not complete */
  825. }
  826. static int multipath_end_io(struct dm_target *ti, struct bio *bio,
  827. int error, union map_info *map_context)
  828. {
  829. struct multipath *m = (struct multipath *) ti->private;
  830. struct mpath_io *mpio = (struct mpath_io *) map_context->ptr;
  831. struct pgpath *pgpath = mpio->pgpath;
  832. struct path_selector *ps;
  833. int r;
  834. r = do_end_io(m, bio, error, mpio);
  835. if (pgpath) {
  836. ps = &pgpath->pg->ps;
  837. if (ps->type->end_io)
  838. ps->type->end_io(ps, &pgpath->path);
  839. }
  840. if (r <= 0)
  841. mempool_free(mpio, m->mpio_pool);
  842. return r;
  843. }
  844. /*
  845. * Suspend can't complete until all the I/O is processed so if
  846. * the last path fails we must error any remaining I/O.
  847. * Note that if the freeze_bdev fails while suspending, the
  848. * queue_if_no_path state is lost - userspace should reset it.
  849. */
  850. static void multipath_presuspend(struct dm_target *ti)
  851. {
  852. struct multipath *m = (struct multipath *) ti->private;
  853. queue_if_no_path(m, 0);
  854. }
  855. /*
  856. * Restore the queue_if_no_path setting.
  857. */
  858. static void multipath_resume(struct dm_target *ti)
  859. {
  860. struct multipath *m = (struct multipath *) ti->private;
  861. unsigned long flags;
  862. spin_lock_irqsave(&m->lock, flags);
  863. m->queue_if_no_path = m->saved_queue_if_no_path;
  864. spin_unlock_irqrestore(&m->lock, flags);
  865. }
  866. /*
  867. * Info output has the following format:
  868. * num_multipath_feature_args [multipath_feature_args]*
  869. * num_handler_status_args [handler_status_args]*
  870. * num_groups init_group_number
  871. * [A|D|E num_ps_status_args [ps_status_args]*
  872. * num_paths num_selector_args
  873. * [path_dev A|F fail_count [selector_args]* ]+ ]+
  874. *
  875. * Table output has the following format (identical to the constructor string):
  876. * num_feature_args [features_args]*
  877. * num_handler_args hw_handler [hw_handler_args]*
  878. * num_groups init_group_number
  879. * [priority selector-name num_ps_args [ps_args]*
  880. * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
  881. */
  882. static int multipath_status(struct dm_target *ti, status_type_t type,
  883. char *result, unsigned int maxlen)
  884. {
  885. int sz = 0;
  886. unsigned long flags;
  887. struct multipath *m = (struct multipath *) ti->private;
  888. struct hw_handler *hwh = &m->hw_handler;
  889. struct priority_group *pg;
  890. struct pgpath *p;
  891. unsigned pg_num;
  892. char state;
  893. spin_lock_irqsave(&m->lock, flags);
  894. /* Features */
  895. if (type == STATUSTYPE_INFO)
  896. DMEMIT("1 %u ", m->queue_size);
  897. else if (m->queue_if_no_path)
  898. DMEMIT("1 queue_if_no_path ");
  899. else
  900. DMEMIT("0 ");
  901. if (hwh->type && hwh->type->status)
  902. sz += hwh->type->status(hwh, type, result + sz, maxlen - sz);
  903. else if (!hwh->type || type == STATUSTYPE_INFO)
  904. DMEMIT("0 ");
  905. else
  906. DMEMIT("1 %s ", hwh->type->name);
  907. DMEMIT("%u ", m->nr_priority_groups);
  908. if (m->next_pg)
  909. pg_num = m->next_pg->pg_num;
  910. else if (m->current_pg)
  911. pg_num = m->current_pg->pg_num;
  912. else
  913. pg_num = 1;
  914. DMEMIT("%u ", pg_num);
  915. switch (type) {
  916. case STATUSTYPE_INFO:
  917. list_for_each_entry(pg, &m->priority_groups, list) {
  918. if (pg->bypassed)
  919. state = 'D'; /* Disabled */
  920. else if (pg == m->current_pg)
  921. state = 'A'; /* Currently Active */
  922. else
  923. state = 'E'; /* Enabled */
  924. DMEMIT("%c ", state);
  925. if (pg->ps.type->status)
  926. sz += pg->ps.type->status(&pg->ps, NULL, type,
  927. result + sz,
  928. maxlen - sz);
  929. else
  930. DMEMIT("0 ");
  931. DMEMIT("%u %u ", pg->nr_pgpaths,
  932. pg->ps.type->info_args);
  933. list_for_each_entry(p, &pg->pgpaths, list) {
  934. DMEMIT("%s %s %u ", p->path.dev->name,
  935. p->path.is_active ? "A" : "F",
  936. p->fail_count);
  937. if (pg->ps.type->status)
  938. sz += pg->ps.type->status(&pg->ps,
  939. &p->path, type, result + sz,
  940. maxlen - sz);
  941. }
  942. }
  943. break;
  944. case STATUSTYPE_TABLE:
  945. list_for_each_entry(pg, &m->priority_groups, list) {
  946. DMEMIT("%s ", pg->ps.type->name);
  947. if (pg->ps.type->status)
  948. sz += pg->ps.type->status(&pg->ps, NULL, type,
  949. result + sz,
  950. maxlen - sz);
  951. else
  952. DMEMIT("0 ");
  953. DMEMIT("%u %u ", pg->nr_pgpaths,
  954. pg->ps.type->table_args);
  955. list_for_each_entry(p, &pg->pgpaths, list) {
  956. DMEMIT("%s ", p->path.dev->name);
  957. if (pg->ps.type->status)
  958. sz += pg->ps.type->status(&pg->ps,
  959. &p->path, type, result + sz,
  960. maxlen - sz);
  961. }
  962. }
  963. break;
  964. }
  965. spin_unlock_irqrestore(&m->lock, flags);
  966. return 0;
  967. }
  968. static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
  969. {
  970. int r;
  971. struct dm_dev *dev;
  972. struct multipath *m = (struct multipath *) ti->private;
  973. action_fn action;
  974. if (argc == 1) {
  975. if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
  976. return queue_if_no_path(m, 1);
  977. else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
  978. return queue_if_no_path(m, 0);
  979. }
  980. if (argc != 2)
  981. goto error;
  982. if (!strnicmp(argv[0], MESG_STR("disable_group")))
  983. return bypass_pg_num(m, argv[1], 1);
  984. else if (!strnicmp(argv[0], MESG_STR("enable_group")))
  985. return bypass_pg_num(m, argv[1], 0);
  986. else if (!strnicmp(argv[0], MESG_STR("switch_group")))
  987. return switch_pg_num(m, argv[1]);
  988. else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
  989. action = reinstate_path;
  990. else if (!strnicmp(argv[0], MESG_STR("fail_path")))
  991. action = fail_path;
  992. else
  993. goto error;
  994. r = dm_get_device(ti, argv[1], ti->begin, ti->len,
  995. dm_table_get_mode(ti->table), &dev);
  996. if (r) {
  997. DMWARN("dm-multipath message: error getting device %s",
  998. argv[1]);
  999. return -EINVAL;
  1000. }
  1001. r = action_dev(m, dev, action);
  1002. dm_put_device(ti, dev);
  1003. return r;
  1004. error:
  1005. DMWARN("Unrecognised multipath message received.");
  1006. return -EINVAL;
  1007. }
  1008. /*-----------------------------------------------------------------
  1009. * Module setup
  1010. *---------------------------------------------------------------*/
  1011. static struct target_type multipath_target = {
  1012. .name = "multipath",
  1013. .version = {1, 0, 4},
  1014. .module = THIS_MODULE,
  1015. .ctr = multipath_ctr,
  1016. .dtr = multipath_dtr,
  1017. .map = multipath_map,
  1018. .end_io = multipath_end_io,
  1019. .presuspend = multipath_presuspend,
  1020. .resume = multipath_resume,
  1021. .status = multipath_status,
  1022. .message = multipath_message,
  1023. };
  1024. static int __init dm_multipath_init(void)
  1025. {
  1026. int r;
  1027. /* allocate a slab for the dm_ios */
  1028. _mpio_cache = kmem_cache_create("dm_mpath", sizeof(struct mpath_io),
  1029. 0, 0, NULL, NULL);
  1030. if (!_mpio_cache)
  1031. return -ENOMEM;
  1032. r = dm_register_target(&multipath_target);
  1033. if (r < 0) {
  1034. DMERR("%s: register failed %d", multipath_target.name, r);
  1035. kmem_cache_destroy(_mpio_cache);
  1036. return -EINVAL;
  1037. }
  1038. kmultipathd = create_workqueue("kmpathd");
  1039. if (!kmultipathd) {
  1040. DMERR("%s: failed to create workqueue kmpathd",
  1041. multipath_target.name);
  1042. dm_unregister_target(&multipath_target);
  1043. kmem_cache_destroy(_mpio_cache);
  1044. return -ENOMEM;
  1045. }
  1046. DMINFO("dm-multipath version %u.%u.%u loaded",
  1047. multipath_target.version[0], multipath_target.version[1],
  1048. multipath_target.version[2]);
  1049. return r;
  1050. }
  1051. static void __exit dm_multipath_exit(void)
  1052. {
  1053. int r;
  1054. destroy_workqueue(kmultipathd);
  1055. r = dm_unregister_target(&multipath_target);
  1056. if (r < 0)
  1057. DMERR("%s: target unregister failed %d",
  1058. multipath_target.name, r);
  1059. kmem_cache_destroy(_mpio_cache);
  1060. }
  1061. EXPORT_SYMBOL_GPL(dm_pg_init_complete);
  1062. module_init(dm_multipath_init);
  1063. module_exit(dm_multipath_exit);
  1064. MODULE_DESCRIPTION(DM_NAME " multipath target");
  1065. MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
  1066. MODULE_LICENSE("GPL");