dm-mpath.c 28 KB

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