dm-mpath.c 28 KB

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