dm-mpath.c 30 KB

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