dm-mpath.c 30 KB

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