dm-mpath.c 30 KB

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