dm-mpath.c 32 KB

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