dm-mpath.c 29 KB

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