dm-mpath.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  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 DM_MSG_PREFIX "multipath"
  22. #define MESG_STR(x) x, sizeof(x)
  23. /* Path properties */
  24. struct pgpath {
  25. struct list_head list;
  26. struct priority_group *pg; /* Owning PG */
  27. unsigned fail_count; /* Cumulative failure count */
  28. struct path path;
  29. };
  30. #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
  31. /*
  32. * Paths are grouped into Priority Groups and numbered from 1 upwards.
  33. * Each has a path selector which controls which path gets used.
  34. */
  35. struct priority_group {
  36. struct list_head list;
  37. struct multipath *m; /* Owning multipath instance */
  38. struct path_selector ps;
  39. unsigned pg_num; /* Reference number */
  40. unsigned bypassed; /* Temporarily bypass this PG? */
  41. unsigned nr_pgpaths; /* Number of paths in PG */
  42. struct list_head pgpaths;
  43. };
  44. /* Multipath context */
  45. struct multipath {
  46. struct list_head list;
  47. struct dm_target *ti;
  48. spinlock_t lock;
  49. struct hw_handler hw_handler;
  50. unsigned nr_priority_groups;
  51. struct list_head priority_groups;
  52. unsigned pg_init_required; /* pg_init needs calling? */
  53. unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
  54. unsigned nr_valid_paths; /* Total number of usable paths */
  55. struct pgpath *current_pgpath;
  56. struct priority_group *current_pg;
  57. struct priority_group *next_pg; /* Switch to this PG if set */
  58. unsigned repeat_count; /* I/Os left before calling PS again */
  59. unsigned queue_io; /* Must we queue all I/O? */
  60. unsigned queue_if_no_path; /* Queue I/O if last path fails? */
  61. unsigned saved_queue_if_no_path;/* Saved state during suspension */
  62. struct work_struct process_queued_ios;
  63. struct bio_list queued_ios;
  64. unsigned queue_size;
  65. struct work_struct trigger_event;
  66. /*
  67. * We must use a mempool of mpath_io structs so that we
  68. * can resubmit bios on error.
  69. */
  70. mempool_t *mpio_pool;
  71. };
  72. /*
  73. * Context information attached to each bio we process.
  74. */
  75. struct mpath_io {
  76. struct pgpath *pgpath;
  77. struct dm_bio_details details;
  78. };
  79. typedef int (*action_fn) (struct pgpath *pgpath);
  80. #define MIN_IOS 256 /* Mempool size */
  81. static kmem_cache_t *_mpio_cache;
  82. struct workqueue_struct *kmultipathd;
  83. static void process_queued_ios(void *data);
  84. static void trigger_event(void *data);
  85. /*-----------------------------------------------
  86. * Allocation routines
  87. *-----------------------------------------------*/
  88. static struct pgpath *alloc_pgpath(void)
  89. {
  90. struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
  91. if (pgpath)
  92. pgpath->path.is_active = 1;
  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 = kzalloc(sizeof(*pg), GFP_KERNEL);
  103. if (pg)
  104. INIT_LIST_HEAD(&pg->pgpaths);
  105. return pg;
  106. }
  107. static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
  108. {
  109. struct pgpath *pgpath, *tmp;
  110. list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
  111. list_del(&pgpath->list);
  112. dm_put_device(ti, pgpath->path.dev);
  113. free_pgpath(pgpath);
  114. }
  115. }
  116. static void free_priority_group(struct priority_group *pg,
  117. struct dm_target *ti)
  118. {
  119. struct path_selector *ps = &pg->ps;
  120. if (ps->type) {
  121. ps->type->destroy(ps);
  122. dm_put_path_selector(ps->type);
  123. }
  124. free_pgpaths(&pg->pgpaths, ti);
  125. kfree(pg);
  126. }
  127. static struct multipath *alloc_multipath(struct dm_target *ti)
  128. {
  129. struct multipath *m;
  130. m = kzalloc(sizeof(*m), GFP_KERNEL);
  131. if (m) {
  132. INIT_LIST_HEAD(&m->priority_groups);
  133. spin_lock_init(&m->lock);
  134. m->queue_io = 1;
  135. INIT_WORK(&m->process_queued_ios, process_queued_ios, m);
  136. INIT_WORK(&m->trigger_event, trigger_event, m);
  137. m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
  138. if (!m->mpio_pool) {
  139. kfree(m);
  140. return NULL;
  141. }
  142. m->ti = ti;
  143. ti->private = m;
  144. }
  145. return m;
  146. }
  147. static void free_multipath(struct multipath *m)
  148. {
  149. struct priority_group *pg, *tmp;
  150. struct hw_handler *hwh = &m->hw_handler;
  151. list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
  152. list_del(&pg->list);
  153. free_priority_group(pg, m->ti);
  154. }
  155. if (hwh->type) {
  156. hwh->type->destroy(hwh);
  157. dm_put_hw_handler(hwh->type);
  158. }
  159. mempool_destroy(m->mpio_pool);
  160. kfree(m);
  161. }
  162. /*-----------------------------------------------
  163. * Path selection
  164. *-----------------------------------------------*/
  165. static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
  166. {
  167. struct hw_handler *hwh = &m->hw_handler;
  168. m->current_pg = pgpath->pg;
  169. /* Must we initialise the PG first, and queue I/O till it's ready? */
  170. if (hwh->type && hwh->type->pg_init) {
  171. m->pg_init_required = 1;
  172. m->queue_io = 1;
  173. } else {
  174. m->pg_init_required = 0;
  175. m->queue_io = 0;
  176. }
  177. }
  178. static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
  179. {
  180. struct path *path;
  181. path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
  182. if (!path)
  183. return -ENXIO;
  184. m->current_pgpath = path_to_pgpath(path);
  185. if (m->current_pg != pg)
  186. __switch_pg(m, m->current_pgpath);
  187. return 0;
  188. }
  189. static void __choose_pgpath(struct multipath *m)
  190. {
  191. struct priority_group *pg;
  192. unsigned bypassed = 1;
  193. if (!m->nr_valid_paths)
  194. goto failed;
  195. /* Were we instructed to switch PG? */
  196. if (m->next_pg) {
  197. pg = m->next_pg;
  198. m->next_pg = NULL;
  199. if (!__choose_path_in_pg(m, pg))
  200. return;
  201. }
  202. /* Don't change PG until it has no remaining paths */
  203. if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
  204. return;
  205. /*
  206. * Loop through priority groups until we find a valid path.
  207. * First time we skip PGs marked 'bypassed'.
  208. * Second time we only try the ones we skipped.
  209. */
  210. do {
  211. list_for_each_entry(pg, &m->priority_groups, list) {
  212. if (pg->bypassed == bypassed)
  213. continue;
  214. if (!__choose_path_in_pg(m, pg))
  215. return;
  216. }
  217. } while (bypassed--);
  218. failed:
  219. m->current_pgpath = NULL;
  220. m->current_pg = NULL;
  221. }
  222. static int map_io(struct multipath *m, struct bio *bio, struct mpath_io *mpio,
  223. unsigned was_queued)
  224. {
  225. int r = 1;
  226. unsigned long flags;
  227. struct pgpath *pgpath;
  228. spin_lock_irqsave(&m->lock, flags);
  229. /* Do we need to select a new pgpath? */
  230. if (!m->current_pgpath ||
  231. (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
  232. __choose_pgpath(m);
  233. pgpath = m->current_pgpath;
  234. if (was_queued)
  235. m->queue_size--;
  236. if ((pgpath && m->queue_io) ||
  237. (!pgpath && m->queue_if_no_path)) {
  238. /* Queue for the daemon to resubmit */
  239. bio_list_add(&m->queued_ios, bio);
  240. m->queue_size++;
  241. if ((m->pg_init_required && !m->pg_init_in_progress) ||
  242. !m->queue_io)
  243. queue_work(kmultipathd, &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. unsigned save_old_value)
  259. {
  260. unsigned long flags;
  261. spin_lock_irqsave(&m->lock, flags);
  262. if (save_old_value)
  263. m->saved_queue_if_no_path = m->queue_if_no_path;
  264. else
  265. m->saved_queue_if_no_path = queue_if_no_path;
  266. m->queue_if_no_path = queue_if_no_path;
  267. if (!m->queue_if_no_path && m->queue_size)
  268. queue_work(kmultipathd, &m->process_queued_ios);
  269. spin_unlock_irqrestore(&m->lock, flags);
  270. return 0;
  271. }
  272. /*-----------------------------------------------------------------
  273. * The multipath daemon is responsible for resubmitting queued ios.
  274. *---------------------------------------------------------------*/
  275. static void dispatch_queued_ios(struct multipath *m)
  276. {
  277. int r;
  278. unsigned long flags;
  279. struct bio *bio = NULL, *next;
  280. struct mpath_io *mpio;
  281. union map_info *info;
  282. spin_lock_irqsave(&m->lock, flags);
  283. bio = bio_list_get(&m->queued_ios);
  284. spin_unlock_irqrestore(&m->lock, flags);
  285. while (bio) {
  286. next = bio->bi_next;
  287. bio->bi_next = NULL;
  288. info = dm_get_mapinfo(bio);
  289. mpio = info->ptr;
  290. r = map_io(m, bio, mpio, 1);
  291. if (r < 0)
  292. bio_endio(bio, bio->bi_size, r);
  293. else if (r == 1)
  294. generic_make_request(bio);
  295. bio = next;
  296. }
  297. }
  298. static void process_queued_ios(void *data)
  299. {
  300. struct multipath *m = (struct multipath *) data;
  301. struct hw_handler *hwh = &m->hw_handler;
  302. struct pgpath *pgpath = NULL;
  303. unsigned init_required = 0, must_queue = 1;
  304. unsigned long flags;
  305. spin_lock_irqsave(&m->lock, flags);
  306. if (!m->queue_size)
  307. goto out;
  308. if (!m->current_pgpath)
  309. __choose_pgpath(m);
  310. pgpath = m->current_pgpath;
  311. if ((pgpath && !m->queue_io) ||
  312. (!pgpath && !m->queue_if_no_path))
  313. must_queue = 0;
  314. if (m->pg_init_required && !m->pg_init_in_progress) {
  315. m->pg_init_required = 0;
  316. m->pg_init_in_progress = 1;
  317. init_required = 1;
  318. }
  319. out:
  320. spin_unlock_irqrestore(&m->lock, flags);
  321. if (init_required)
  322. hwh->type->pg_init(hwh, pgpath->pg->bypassed, &pgpath->path);
  323. if (!must_queue)
  324. dispatch_queued_ios(m);
  325. }
  326. /*
  327. * An event is triggered whenever a path is taken out of use.
  328. * Includes path failure and PG bypass.
  329. */
  330. static void trigger_event(void *data)
  331. {
  332. struct multipath *m = (struct multipath *) data;
  333. dm_table_event(m->ti->table);
  334. }
  335. /*-----------------------------------------------------------------
  336. * Constructor/argument parsing:
  337. * <#multipath feature args> [<arg>]*
  338. * <#hw_handler args> [hw_handler [<arg>]*]
  339. * <#priority groups>
  340. * <initial priority group>
  341. * [<selector> <#selector args> [<arg>]*
  342. * <#paths> <#per-path selector args>
  343. * [<path> [<arg>]* ]+ ]+
  344. *---------------------------------------------------------------*/
  345. struct param {
  346. unsigned min;
  347. unsigned max;
  348. char *error;
  349. };
  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, "invalid number of path selector args"},
  390. };
  391. pst = dm_get_path_selector(shift(as));
  392. if (!pst) {
  393. ti->error = "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 = "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 = "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 = "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. {
  441. static struct param _params[] = {
  442. {1, 1024, "invalid number of paths"},
  443. {0, 1024, "invalid number of selector args"}
  444. };
  445. int r;
  446. unsigned i, nr_selector_args, nr_params;
  447. struct priority_group *pg;
  448. struct dm_target *ti = m->ti;
  449. if (as->argc < 2) {
  450. as->argc = 0;
  451. ti->error = "not enough priority group aruments";
  452. return NULL;
  453. }
  454. pg = alloc_priority_group();
  455. if (!pg) {
  456. ti->error = "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. {
  494. int r;
  495. struct hw_handler_type *hwht;
  496. unsigned hw_argc;
  497. struct dm_target *ti = m->ti;
  498. static struct param _params[] = {
  499. {0, 1024, "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 = "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 = "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. {
  523. int r;
  524. unsigned argc;
  525. struct dm_target *ti = m->ti;
  526. static struct param _params[] = {
  527. {0, 1, "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, 0);
  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, "invalid number of priority groups"},
  547. {1, 1024, "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(ti);
  557. if (!m) {
  558. ti->error = "can't allocate multipath";
  559. return -EINVAL;
  560. }
  561. r = parse_features(&as, m);
  562. if (r)
  563. goto bad;
  564. r = parse_hw_handler(&as, m);
  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);
  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 = "priority group count mismatch";
  590. r = -EINVAL;
  591. goto bad;
  592. }
  593. return 0;
  594. bad:
  595. free_multipath(m);
  596. return r;
  597. }
  598. static void multipath_dtr(struct dm_target *ti)
  599. {
  600. struct multipath *m = (struct multipath *) ti->private;
  601. flush_workqueue(kmultipathd);
  602. free_multipath(m);
  603. }
  604. /*
  605. * Map bios, recording original fields for later in case we have to resubmit
  606. */
  607. static int multipath_map(struct dm_target *ti, struct bio *bio,
  608. union map_info *map_context)
  609. {
  610. int r;
  611. struct mpath_io *mpio;
  612. struct multipath *m = (struct multipath *) ti->private;
  613. if (bio_barrier(bio))
  614. return -EOPNOTSUPP;
  615. mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
  616. dm_bio_record(&mpio->details, bio);
  617. map_context->ptr = mpio;
  618. bio->bi_rw |= (1 << BIO_RW_FAILFAST);
  619. r = map_io(m, bio, mpio, 0);
  620. if (r < 0)
  621. mempool_free(mpio, m->mpio_pool);
  622. return r;
  623. }
  624. /*
  625. * Take a path out of use.
  626. */
  627. static int fail_path(struct pgpath *pgpath)
  628. {
  629. unsigned long flags;
  630. struct multipath *m = pgpath->pg->m;
  631. spin_lock_irqsave(&m->lock, flags);
  632. if (!pgpath->path.is_active)
  633. goto out;
  634. DMWARN("Failing path %s.", pgpath->path.dev->name);
  635. pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
  636. pgpath->path.is_active = 0;
  637. pgpath->fail_count++;
  638. m->nr_valid_paths--;
  639. if (pgpath == m->current_pgpath)
  640. m->current_pgpath = NULL;
  641. queue_work(kmultipathd, &m->trigger_event);
  642. out:
  643. spin_unlock_irqrestore(&m->lock, flags);
  644. return 0;
  645. }
  646. /*
  647. * Reinstate a previously-failed path
  648. */
  649. static int reinstate_path(struct pgpath *pgpath)
  650. {
  651. int r = 0;
  652. unsigned long flags;
  653. struct multipath *m = pgpath->pg->m;
  654. spin_lock_irqsave(&m->lock, flags);
  655. if (pgpath->path.is_active)
  656. goto out;
  657. if (!pgpath->pg->ps.type) {
  658. DMWARN("Reinstate path not supported by path selector %s",
  659. pgpath->pg->ps.type->name);
  660. r = -EINVAL;
  661. goto out;
  662. }
  663. r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
  664. if (r)
  665. goto out;
  666. pgpath->path.is_active = 1;
  667. m->current_pgpath = NULL;
  668. if (!m->nr_valid_paths++ && m->queue_size)
  669. queue_work(kmultipathd, &m->process_queued_ios);
  670. queue_work(kmultipathd, &m->trigger_event);
  671. out:
  672. spin_unlock_irqrestore(&m->lock, flags);
  673. return r;
  674. }
  675. /*
  676. * Fail or reinstate all paths that match the provided struct dm_dev.
  677. */
  678. static int action_dev(struct multipath *m, struct dm_dev *dev,
  679. action_fn action)
  680. {
  681. int r = 0;
  682. struct pgpath *pgpath;
  683. struct priority_group *pg;
  684. list_for_each_entry(pg, &m->priority_groups, list) {
  685. list_for_each_entry(pgpath, &pg->pgpaths, list) {
  686. if (pgpath->path.dev == dev)
  687. r = action(pgpath);
  688. }
  689. }
  690. return r;
  691. }
  692. /*
  693. * Temporarily try to avoid having to use the specified PG
  694. */
  695. static void bypass_pg(struct multipath *m, struct priority_group *pg,
  696. int bypassed)
  697. {
  698. unsigned long flags;
  699. spin_lock_irqsave(&m->lock, flags);
  700. pg->bypassed = bypassed;
  701. m->current_pgpath = NULL;
  702. m->current_pg = NULL;
  703. spin_unlock_irqrestore(&m->lock, flags);
  704. queue_work(kmultipathd, &m->trigger_event);
  705. }
  706. /*
  707. * Switch to using the specified PG from the next I/O that gets mapped
  708. */
  709. static int switch_pg_num(struct multipath *m, const char *pgstr)
  710. {
  711. struct priority_group *pg;
  712. unsigned pgnum;
  713. unsigned long flags;
  714. if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
  715. (pgnum > m->nr_priority_groups)) {
  716. DMWARN("invalid PG number supplied to switch_pg_num");
  717. return -EINVAL;
  718. }
  719. spin_lock_irqsave(&m->lock, flags);
  720. list_for_each_entry(pg, &m->priority_groups, list) {
  721. pg->bypassed = 0;
  722. if (--pgnum)
  723. continue;
  724. m->current_pgpath = NULL;
  725. m->current_pg = NULL;
  726. m->next_pg = pg;
  727. }
  728. spin_unlock_irqrestore(&m->lock, flags);
  729. queue_work(kmultipathd, &m->trigger_event);
  730. return 0;
  731. }
  732. /*
  733. * Set/clear bypassed status of a PG.
  734. * PGs are numbered upwards from 1 in the order they were declared.
  735. */
  736. static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
  737. {
  738. struct priority_group *pg;
  739. unsigned pgnum;
  740. if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
  741. (pgnum > m->nr_priority_groups)) {
  742. DMWARN("invalid PG number supplied to bypass_pg");
  743. return -EINVAL;
  744. }
  745. list_for_each_entry(pg, &m->priority_groups, list) {
  746. if (!--pgnum)
  747. break;
  748. }
  749. bypass_pg(m, pg, bypassed);
  750. return 0;
  751. }
  752. /*
  753. * pg_init must call this when it has completed its initialisation
  754. */
  755. void dm_pg_init_complete(struct path *path, unsigned err_flags)
  756. {
  757. struct pgpath *pgpath = path_to_pgpath(path);
  758. struct priority_group *pg = pgpath->pg;
  759. struct multipath *m = pg->m;
  760. unsigned long flags;
  761. /* We insist on failing the path if the PG is already bypassed. */
  762. if (err_flags && pg->bypassed)
  763. err_flags |= MP_FAIL_PATH;
  764. if (err_flags & MP_FAIL_PATH)
  765. fail_path(pgpath);
  766. if (err_flags & MP_BYPASS_PG)
  767. bypass_pg(m, pg, 1);
  768. spin_lock_irqsave(&m->lock, flags);
  769. if (err_flags) {
  770. m->current_pgpath = NULL;
  771. m->current_pg = NULL;
  772. } else if (!m->pg_init_required)
  773. m->queue_io = 0;
  774. m->pg_init_in_progress = 0;
  775. queue_work(kmultipathd, &m->process_queued_ios);
  776. spin_unlock_irqrestore(&m->lock, flags);
  777. }
  778. /*
  779. * end_io handling
  780. */
  781. static int do_end_io(struct multipath *m, struct bio *bio,
  782. int error, struct mpath_io *mpio)
  783. {
  784. struct hw_handler *hwh = &m->hw_handler;
  785. unsigned err_flags = MP_FAIL_PATH; /* Default behavior */
  786. unsigned long flags;
  787. if (!error)
  788. return 0; /* I/O complete */
  789. if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
  790. return error;
  791. if (error == -EOPNOTSUPP)
  792. return error;
  793. spin_lock_irqsave(&m->lock, flags);
  794. if (!m->nr_valid_paths) {
  795. if (!m->queue_if_no_path) {
  796. spin_unlock_irqrestore(&m->lock, flags);
  797. return -EIO;
  798. } else {
  799. spin_unlock_irqrestore(&m->lock, flags);
  800. goto requeue;
  801. }
  802. }
  803. spin_unlock_irqrestore(&m->lock, flags);
  804. if (hwh->type && hwh->type->error)
  805. err_flags = hwh->type->error(hwh, bio);
  806. if (mpio->pgpath) {
  807. if (err_flags & MP_FAIL_PATH)
  808. fail_path(mpio->pgpath);
  809. if (err_flags & MP_BYPASS_PG)
  810. bypass_pg(m, mpio->pgpath->pg, 1);
  811. }
  812. if (err_flags & MP_ERROR_IO)
  813. return -EIO;
  814. requeue:
  815. dm_bio_restore(&mpio->details, bio);
  816. /* queue for the daemon to resubmit or fail */
  817. spin_lock_irqsave(&m->lock, flags);
  818. bio_list_add(&m->queued_ios, bio);
  819. m->queue_size++;
  820. if (!m->queue_io)
  821. queue_work(kmultipathd, &m->process_queued_ios);
  822. spin_unlock_irqrestore(&m->lock, flags);
  823. return 1; /* io not complete */
  824. }
  825. static int multipath_end_io(struct dm_target *ti, struct bio *bio,
  826. int error, union map_info *map_context)
  827. {
  828. struct multipath *m = (struct multipath *) ti->private;
  829. struct mpath_io *mpio = (struct mpath_io *) map_context->ptr;
  830. struct pgpath *pgpath = mpio->pgpath;
  831. struct path_selector *ps;
  832. int r;
  833. r = do_end_io(m, bio, error, mpio);
  834. if (pgpath) {
  835. ps = &pgpath->pg->ps;
  836. if (ps->type->end_io)
  837. ps->type->end_io(ps, &pgpath->path);
  838. }
  839. if (r <= 0)
  840. mempool_free(mpio, m->mpio_pool);
  841. return r;
  842. }
  843. /*
  844. * Suspend can't complete until all the I/O is processed so if
  845. * the last path fails we must error any remaining I/O.
  846. * Note that if the freeze_bdev fails while suspending, the
  847. * queue_if_no_path state is lost - userspace should reset it.
  848. */
  849. static void multipath_presuspend(struct dm_target *ti)
  850. {
  851. struct multipath *m = (struct multipath *) ti->private;
  852. queue_if_no_path(m, 0, 1);
  853. }
  854. /*
  855. * Restore the queue_if_no_path setting.
  856. */
  857. static void multipath_resume(struct dm_target *ti)
  858. {
  859. struct multipath *m = (struct multipath *) ti->private;
  860. unsigned long flags;
  861. spin_lock_irqsave(&m->lock, flags);
  862. m->queue_if_no_path = m->saved_queue_if_no_path;
  863. spin_unlock_irqrestore(&m->lock, flags);
  864. }
  865. /*
  866. * Info output has the following format:
  867. * num_multipath_feature_args [multipath_feature_args]*
  868. * num_handler_status_args [handler_status_args]*
  869. * num_groups init_group_number
  870. * [A|D|E num_ps_status_args [ps_status_args]*
  871. * num_paths num_selector_args
  872. * [path_dev A|F fail_count [selector_args]* ]+ ]+
  873. *
  874. * Table output has the following format (identical to the constructor string):
  875. * num_feature_args [features_args]*
  876. * num_handler_args hw_handler [hw_handler_args]*
  877. * num_groups init_group_number
  878. * [priority selector-name num_ps_args [ps_args]*
  879. * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
  880. */
  881. static int multipath_status(struct dm_target *ti, status_type_t type,
  882. char *result, unsigned int maxlen)
  883. {
  884. int sz = 0;
  885. unsigned long flags;
  886. struct multipath *m = (struct multipath *) ti->private;
  887. struct hw_handler *hwh = &m->hw_handler;
  888. struct priority_group *pg;
  889. struct pgpath *p;
  890. unsigned pg_num;
  891. char state;
  892. spin_lock_irqsave(&m->lock, flags);
  893. /* Features */
  894. if (type == STATUSTYPE_INFO)
  895. DMEMIT("1 %u ", m->queue_size);
  896. else if (m->queue_if_no_path)
  897. DMEMIT("1 queue_if_no_path ");
  898. else
  899. DMEMIT("0 ");
  900. if (hwh->type && hwh->type->status)
  901. sz += hwh->type->status(hwh, type, result + sz, maxlen - sz);
  902. else if (!hwh->type || type == STATUSTYPE_INFO)
  903. DMEMIT("0 ");
  904. else
  905. DMEMIT("1 %s ", hwh->type->name);
  906. DMEMIT("%u ", m->nr_priority_groups);
  907. if (m->next_pg)
  908. pg_num = m->next_pg->pg_num;
  909. else if (m->current_pg)
  910. pg_num = m->current_pg->pg_num;
  911. else
  912. pg_num = 1;
  913. DMEMIT("%u ", pg_num);
  914. switch (type) {
  915. case STATUSTYPE_INFO:
  916. list_for_each_entry(pg, &m->priority_groups, list) {
  917. if (pg->bypassed)
  918. state = 'D'; /* Disabled */
  919. else if (pg == m->current_pg)
  920. state = 'A'; /* Currently Active */
  921. else
  922. state = 'E'; /* Enabled */
  923. DMEMIT("%c ", state);
  924. if (pg->ps.type->status)
  925. sz += pg->ps.type->status(&pg->ps, NULL, type,
  926. result + sz,
  927. maxlen - sz);
  928. else
  929. DMEMIT("0 ");
  930. DMEMIT("%u %u ", pg->nr_pgpaths,
  931. pg->ps.type->info_args);
  932. list_for_each_entry(p, &pg->pgpaths, list) {
  933. DMEMIT("%s %s %u ", p->path.dev->name,
  934. p->path.is_active ? "A" : "F",
  935. p->fail_count);
  936. if (pg->ps.type->status)
  937. sz += pg->ps.type->status(&pg->ps,
  938. &p->path, type, result + sz,
  939. maxlen - sz);
  940. }
  941. }
  942. break;
  943. case STATUSTYPE_TABLE:
  944. list_for_each_entry(pg, &m->priority_groups, list) {
  945. DMEMIT("%s ", pg->ps.type->name);
  946. if (pg->ps.type->status)
  947. sz += pg->ps.type->status(&pg->ps, NULL, type,
  948. result + sz,
  949. maxlen - sz);
  950. else
  951. DMEMIT("0 ");
  952. DMEMIT("%u %u ", pg->nr_pgpaths,
  953. pg->ps.type->table_args);
  954. list_for_each_entry(p, &pg->pgpaths, list) {
  955. DMEMIT("%s ", p->path.dev->name);
  956. if (pg->ps.type->status)
  957. sz += pg->ps.type->status(&pg->ps,
  958. &p->path, type, result + sz,
  959. maxlen - sz);
  960. }
  961. }
  962. break;
  963. }
  964. spin_unlock_irqrestore(&m->lock, flags);
  965. return 0;
  966. }
  967. static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
  968. {
  969. int r;
  970. struct dm_dev *dev;
  971. struct multipath *m = (struct multipath *) ti->private;
  972. action_fn action;
  973. if (argc == 1) {
  974. if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
  975. return queue_if_no_path(m, 1, 0);
  976. else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
  977. return queue_if_no_path(m, 0, 0);
  978. }
  979. if (argc != 2)
  980. goto error;
  981. if (!strnicmp(argv[0], MESG_STR("disable_group")))
  982. return bypass_pg_num(m, argv[1], 1);
  983. else if (!strnicmp(argv[0], MESG_STR("enable_group")))
  984. return bypass_pg_num(m, argv[1], 0);
  985. else if (!strnicmp(argv[0], MESG_STR("switch_group")))
  986. return switch_pg_num(m, argv[1]);
  987. else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
  988. action = reinstate_path;
  989. else if (!strnicmp(argv[0], MESG_STR("fail_path")))
  990. action = fail_path;
  991. else
  992. goto error;
  993. r = dm_get_device(ti, argv[1], ti->begin, ti->len,
  994. dm_table_get_mode(ti->table), &dev);
  995. if (r) {
  996. DMWARN("message: error getting device %s",
  997. argv[1]);
  998. return -EINVAL;
  999. }
  1000. r = action_dev(m, dev, action);
  1001. dm_put_device(ti, dev);
  1002. return r;
  1003. error:
  1004. DMWARN("Unrecognised multipath message received.");
  1005. return -EINVAL;
  1006. }
  1007. static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
  1008. struct file *filp, unsigned int cmd,
  1009. unsigned long arg)
  1010. {
  1011. struct multipath *m = (struct multipath *) ti->private;
  1012. struct block_device *bdev = NULL;
  1013. unsigned long flags;
  1014. struct file fake_file = {};
  1015. struct dentry fake_dentry = {};
  1016. int r = 0;
  1017. fake_file.f_dentry = &fake_dentry;
  1018. spin_lock_irqsave(&m->lock, flags);
  1019. if (!m->current_pgpath)
  1020. __choose_pgpath(m);
  1021. if (m->current_pgpath) {
  1022. bdev = m->current_pgpath->path.dev->bdev;
  1023. fake_dentry.d_inode = bdev->bd_inode;
  1024. fake_file.f_mode = m->current_pgpath->path.dev->mode;
  1025. }
  1026. if (m->queue_io)
  1027. r = -EAGAIN;
  1028. else if (!bdev)
  1029. r = -EIO;
  1030. spin_unlock_irqrestore(&m->lock, flags);
  1031. return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
  1032. bdev->bd_disk, cmd, arg);
  1033. }
  1034. /*-----------------------------------------------------------------
  1035. * Module setup
  1036. *---------------------------------------------------------------*/
  1037. static struct target_type multipath_target = {
  1038. .name = "multipath",
  1039. .version = {1, 0, 5},
  1040. .module = THIS_MODULE,
  1041. .ctr = multipath_ctr,
  1042. .dtr = multipath_dtr,
  1043. .map = multipath_map,
  1044. .end_io = multipath_end_io,
  1045. .presuspend = multipath_presuspend,
  1046. .resume = multipath_resume,
  1047. .status = multipath_status,
  1048. .message = multipath_message,
  1049. .ioctl = multipath_ioctl,
  1050. };
  1051. static int __init dm_multipath_init(void)
  1052. {
  1053. int r;
  1054. /* allocate a slab for the dm_ios */
  1055. _mpio_cache = kmem_cache_create("dm_mpath", sizeof(struct mpath_io),
  1056. 0, 0, NULL, NULL);
  1057. if (!_mpio_cache)
  1058. return -ENOMEM;
  1059. r = dm_register_target(&multipath_target);
  1060. if (r < 0) {
  1061. DMERR("%s: register failed %d", multipath_target.name, r);
  1062. kmem_cache_destroy(_mpio_cache);
  1063. return -EINVAL;
  1064. }
  1065. kmultipathd = create_workqueue("kmpathd");
  1066. if (!kmultipathd) {
  1067. DMERR("%s: failed to create workqueue kmpathd",
  1068. multipath_target.name);
  1069. dm_unregister_target(&multipath_target);
  1070. kmem_cache_destroy(_mpio_cache);
  1071. return -ENOMEM;
  1072. }
  1073. DMINFO("version %u.%u.%u loaded",
  1074. multipath_target.version[0], multipath_target.version[1],
  1075. multipath_target.version[2]);
  1076. return r;
  1077. }
  1078. static void __exit dm_multipath_exit(void)
  1079. {
  1080. int r;
  1081. destroy_workqueue(kmultipathd);
  1082. r = dm_unregister_target(&multipath_target);
  1083. if (r < 0)
  1084. DMERR("%s: target unregister failed %d",
  1085. multipath_target.name, r);
  1086. kmem_cache_destroy(_mpio_cache);
  1087. }
  1088. EXPORT_SYMBOL_GPL(dm_pg_init_complete);
  1089. module_init(dm_multipath_init);
  1090. module_exit(dm_multipath_exit);
  1091. MODULE_DESCRIPTION(DM_NAME " multipath target");
  1092. MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
  1093. MODULE_LICENSE("GPL");