dm-mpath.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  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. m->hw_handler.md = dm_table_get_md(ti->table);
  534. dm_put(m->hw_handler.md);
  535. r = hwht->create(&m->hw_handler, hw_argc - 1, as->argv);
  536. if (r) {
  537. dm_put_hw_handler(hwht);
  538. ti->error = "hardware handler constructor failed";
  539. return r;
  540. }
  541. m->hw_handler.type = hwht;
  542. consume(as, hw_argc - 1);
  543. return 0;
  544. }
  545. static int parse_features(struct arg_set *as, struct multipath *m)
  546. {
  547. int r;
  548. unsigned argc;
  549. struct dm_target *ti = m->ti;
  550. static struct param _params[] = {
  551. {0, 1, "invalid number of feature args"},
  552. };
  553. r = read_param(_params, shift(as), &argc, &ti->error);
  554. if (r)
  555. return -EINVAL;
  556. if (!argc)
  557. return 0;
  558. if (!strnicmp(shift(as), MESG_STR("queue_if_no_path")))
  559. return queue_if_no_path(m, 1, 0);
  560. else {
  561. ti->error = "Unrecognised multipath feature request";
  562. return -EINVAL;
  563. }
  564. }
  565. static int multipath_ctr(struct dm_target *ti, unsigned int argc,
  566. char **argv)
  567. {
  568. /* target parameters */
  569. static struct param _params[] = {
  570. {1, 1024, "invalid number of priority groups"},
  571. {1, 1024, "invalid initial priority group number"},
  572. };
  573. int r;
  574. struct multipath *m;
  575. struct arg_set as;
  576. unsigned pg_count = 0;
  577. unsigned next_pg_num;
  578. as.argc = argc;
  579. as.argv = argv;
  580. m = alloc_multipath(ti);
  581. if (!m) {
  582. ti->error = "can't allocate multipath";
  583. return -EINVAL;
  584. }
  585. r = parse_features(&as, m);
  586. if (r)
  587. goto bad;
  588. r = parse_hw_handler(&as, m);
  589. if (r)
  590. goto bad;
  591. r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
  592. if (r)
  593. goto bad;
  594. r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
  595. if (r)
  596. goto bad;
  597. /* parse the priority groups */
  598. while (as.argc) {
  599. struct priority_group *pg;
  600. pg = parse_priority_group(&as, m);
  601. if (!pg) {
  602. r = -EINVAL;
  603. goto bad;
  604. }
  605. m->nr_valid_paths += pg->nr_pgpaths;
  606. list_add_tail(&pg->list, &m->priority_groups);
  607. pg_count++;
  608. pg->pg_num = pg_count;
  609. if (!--next_pg_num)
  610. m->next_pg = pg;
  611. }
  612. if (pg_count != m->nr_priority_groups) {
  613. ti->error = "priority group count mismatch";
  614. r = -EINVAL;
  615. goto bad;
  616. }
  617. return 0;
  618. bad:
  619. free_multipath(m);
  620. return r;
  621. }
  622. static void multipath_dtr(struct dm_target *ti)
  623. {
  624. struct multipath *m = (struct multipath *) ti->private;
  625. flush_workqueue(kmultipathd);
  626. free_multipath(m);
  627. }
  628. /*
  629. * Map bios, recording original fields for later in case we have to resubmit
  630. */
  631. static int multipath_map(struct dm_target *ti, struct bio *bio,
  632. union map_info *map_context)
  633. {
  634. int r;
  635. struct mpath_io *mpio;
  636. struct multipath *m = (struct multipath *) ti->private;
  637. if (bio_barrier(bio))
  638. return -EOPNOTSUPP;
  639. mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
  640. dm_bio_record(&mpio->details, bio);
  641. map_context->ptr = mpio;
  642. bio->bi_rw |= (1 << BIO_RW_FAILFAST);
  643. r = map_io(m, bio, mpio, 0);
  644. if (r < 0 || r == DM_MAPIO_REQUEUE)
  645. mempool_free(mpio, m->mpio_pool);
  646. return r;
  647. }
  648. /*
  649. * Take a path out of use.
  650. */
  651. static int fail_path(struct pgpath *pgpath)
  652. {
  653. unsigned long flags;
  654. struct multipath *m = pgpath->pg->m;
  655. spin_lock_irqsave(&m->lock, flags);
  656. if (!pgpath->path.is_active)
  657. goto out;
  658. DMWARN("Failing path %s.", pgpath->path.dev->name);
  659. pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
  660. pgpath->path.is_active = 0;
  661. pgpath->fail_count++;
  662. m->nr_valid_paths--;
  663. if (pgpath == m->current_pgpath)
  664. m->current_pgpath = NULL;
  665. queue_work(kmultipathd, &m->trigger_event);
  666. out:
  667. spin_unlock_irqrestore(&m->lock, flags);
  668. return 0;
  669. }
  670. /*
  671. * Reinstate a previously-failed path
  672. */
  673. static int reinstate_path(struct pgpath *pgpath)
  674. {
  675. int r = 0;
  676. unsigned long flags;
  677. struct multipath *m = pgpath->pg->m;
  678. spin_lock_irqsave(&m->lock, flags);
  679. if (pgpath->path.is_active)
  680. goto out;
  681. if (!pgpath->pg->ps.type) {
  682. DMWARN("Reinstate path not supported by path selector %s",
  683. pgpath->pg->ps.type->name);
  684. r = -EINVAL;
  685. goto out;
  686. }
  687. r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
  688. if (r)
  689. goto out;
  690. pgpath->path.is_active = 1;
  691. m->current_pgpath = NULL;
  692. if (!m->nr_valid_paths++ && m->queue_size)
  693. queue_work(kmultipathd, &m->process_queued_ios);
  694. queue_work(kmultipathd, &m->trigger_event);
  695. out:
  696. spin_unlock_irqrestore(&m->lock, flags);
  697. return r;
  698. }
  699. /*
  700. * Fail or reinstate all paths that match the provided struct dm_dev.
  701. */
  702. static int action_dev(struct multipath *m, struct dm_dev *dev,
  703. action_fn action)
  704. {
  705. int r = 0;
  706. struct pgpath *pgpath;
  707. struct priority_group *pg;
  708. list_for_each_entry(pg, &m->priority_groups, list) {
  709. list_for_each_entry(pgpath, &pg->pgpaths, list) {
  710. if (pgpath->path.dev == dev)
  711. r = action(pgpath);
  712. }
  713. }
  714. return r;
  715. }
  716. /*
  717. * Temporarily try to avoid having to use the specified PG
  718. */
  719. static void bypass_pg(struct multipath *m, struct priority_group *pg,
  720. int bypassed)
  721. {
  722. unsigned long flags;
  723. spin_lock_irqsave(&m->lock, flags);
  724. pg->bypassed = bypassed;
  725. m->current_pgpath = NULL;
  726. m->current_pg = NULL;
  727. spin_unlock_irqrestore(&m->lock, flags);
  728. queue_work(kmultipathd, &m->trigger_event);
  729. }
  730. /*
  731. * Switch to using the specified PG from the next I/O that gets mapped
  732. */
  733. static int switch_pg_num(struct multipath *m, const char *pgstr)
  734. {
  735. struct priority_group *pg;
  736. unsigned pgnum;
  737. unsigned long flags;
  738. if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
  739. (pgnum > m->nr_priority_groups)) {
  740. DMWARN("invalid PG number supplied to switch_pg_num");
  741. return -EINVAL;
  742. }
  743. spin_lock_irqsave(&m->lock, flags);
  744. list_for_each_entry(pg, &m->priority_groups, list) {
  745. pg->bypassed = 0;
  746. if (--pgnum)
  747. continue;
  748. m->current_pgpath = NULL;
  749. m->current_pg = NULL;
  750. m->next_pg = pg;
  751. }
  752. spin_unlock_irqrestore(&m->lock, flags);
  753. queue_work(kmultipathd, &m->trigger_event);
  754. return 0;
  755. }
  756. /*
  757. * Set/clear bypassed status of a PG.
  758. * PGs are numbered upwards from 1 in the order they were declared.
  759. */
  760. static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
  761. {
  762. struct priority_group *pg;
  763. unsigned pgnum;
  764. if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
  765. (pgnum > m->nr_priority_groups)) {
  766. DMWARN("invalid PG number supplied to bypass_pg");
  767. return -EINVAL;
  768. }
  769. list_for_each_entry(pg, &m->priority_groups, list) {
  770. if (!--pgnum)
  771. break;
  772. }
  773. bypass_pg(m, pg, bypassed);
  774. return 0;
  775. }
  776. /*
  777. * pg_init must call this when it has completed its initialisation
  778. */
  779. void dm_pg_init_complete(struct dm_path *path, unsigned err_flags)
  780. {
  781. struct pgpath *pgpath = path_to_pgpath(path);
  782. struct priority_group *pg = pgpath->pg;
  783. struct multipath *m = pg->m;
  784. unsigned long flags;
  785. /* We insist on failing the path if the PG is already bypassed. */
  786. if (err_flags && pg->bypassed)
  787. err_flags |= MP_FAIL_PATH;
  788. if (err_flags & MP_FAIL_PATH)
  789. fail_path(pgpath);
  790. if (err_flags & MP_BYPASS_PG)
  791. bypass_pg(m, pg, 1);
  792. spin_lock_irqsave(&m->lock, flags);
  793. if (err_flags) {
  794. m->current_pgpath = NULL;
  795. m->current_pg = NULL;
  796. } else if (!m->pg_init_required)
  797. m->queue_io = 0;
  798. m->pg_init_in_progress = 0;
  799. queue_work(kmultipathd, &m->process_queued_ios);
  800. spin_unlock_irqrestore(&m->lock, flags);
  801. }
  802. /*
  803. * end_io handling
  804. */
  805. static int do_end_io(struct multipath *m, struct bio *bio,
  806. int error, struct mpath_io *mpio)
  807. {
  808. struct hw_handler *hwh = &m->hw_handler;
  809. unsigned err_flags = MP_FAIL_PATH; /* Default behavior */
  810. unsigned long flags;
  811. if (!error)
  812. return 0; /* I/O complete */
  813. if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
  814. return error;
  815. if (error == -EOPNOTSUPP)
  816. return error;
  817. spin_lock_irqsave(&m->lock, flags);
  818. if (!m->nr_valid_paths) {
  819. if (__must_push_back(m)) {
  820. spin_unlock_irqrestore(&m->lock, flags);
  821. return DM_ENDIO_REQUEUE;
  822. } else if (!m->queue_if_no_path) {
  823. spin_unlock_irqrestore(&m->lock, flags);
  824. return -EIO;
  825. } else {
  826. spin_unlock_irqrestore(&m->lock, flags);
  827. goto requeue;
  828. }
  829. }
  830. spin_unlock_irqrestore(&m->lock, flags);
  831. if (hwh->type && hwh->type->error)
  832. err_flags = hwh->type->error(hwh, bio);
  833. if (mpio->pgpath) {
  834. if (err_flags & MP_FAIL_PATH)
  835. fail_path(mpio->pgpath);
  836. if (err_flags & MP_BYPASS_PG)
  837. bypass_pg(m, mpio->pgpath->pg, 1);
  838. }
  839. if (err_flags & MP_ERROR_IO)
  840. return -EIO;
  841. requeue:
  842. dm_bio_restore(&mpio->details, bio);
  843. /* queue for the daemon to resubmit or fail */
  844. spin_lock_irqsave(&m->lock, flags);
  845. bio_list_add(&m->queued_ios, bio);
  846. m->queue_size++;
  847. if (!m->queue_io)
  848. queue_work(kmultipathd, &m->process_queued_ios);
  849. spin_unlock_irqrestore(&m->lock, flags);
  850. return DM_ENDIO_INCOMPLETE; /* io not complete */
  851. }
  852. static int multipath_end_io(struct dm_target *ti, struct bio *bio,
  853. int error, union map_info *map_context)
  854. {
  855. struct multipath *m = (struct multipath *) ti->private;
  856. struct mpath_io *mpio = (struct mpath_io *) map_context->ptr;
  857. struct pgpath *pgpath = mpio->pgpath;
  858. struct path_selector *ps;
  859. int r;
  860. r = do_end_io(m, bio, error, mpio);
  861. if (pgpath) {
  862. ps = &pgpath->pg->ps;
  863. if (ps->type->end_io)
  864. ps->type->end_io(ps, &pgpath->path);
  865. }
  866. if (r != DM_ENDIO_INCOMPLETE)
  867. mempool_free(mpio, m->mpio_pool);
  868. return r;
  869. }
  870. /*
  871. * Suspend can't complete until all the I/O is processed so if
  872. * the last path fails we must error any remaining I/O.
  873. * Note that if the freeze_bdev fails while suspending, the
  874. * queue_if_no_path state is lost - userspace should reset it.
  875. */
  876. static void multipath_presuspend(struct dm_target *ti)
  877. {
  878. struct multipath *m = (struct multipath *) ti->private;
  879. queue_if_no_path(m, 0, 1);
  880. }
  881. /*
  882. * Restore the queue_if_no_path setting.
  883. */
  884. static void multipath_resume(struct dm_target *ti)
  885. {
  886. struct multipath *m = (struct multipath *) ti->private;
  887. unsigned long flags;
  888. spin_lock_irqsave(&m->lock, flags);
  889. m->queue_if_no_path = m->saved_queue_if_no_path;
  890. spin_unlock_irqrestore(&m->lock, flags);
  891. }
  892. /*
  893. * Info output has the following format:
  894. * num_multipath_feature_args [multipath_feature_args]*
  895. * num_handler_status_args [handler_status_args]*
  896. * num_groups init_group_number
  897. * [A|D|E num_ps_status_args [ps_status_args]*
  898. * num_paths num_selector_args
  899. * [path_dev A|F fail_count [selector_args]* ]+ ]+
  900. *
  901. * Table output has the following format (identical to the constructor string):
  902. * num_feature_args [features_args]*
  903. * num_handler_args hw_handler [hw_handler_args]*
  904. * num_groups init_group_number
  905. * [priority selector-name num_ps_args [ps_args]*
  906. * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
  907. */
  908. static int multipath_status(struct dm_target *ti, status_type_t type,
  909. char *result, unsigned int maxlen)
  910. {
  911. int sz = 0;
  912. unsigned long flags;
  913. struct multipath *m = (struct multipath *) ti->private;
  914. struct hw_handler *hwh = &m->hw_handler;
  915. struct priority_group *pg;
  916. struct pgpath *p;
  917. unsigned pg_num;
  918. char state;
  919. spin_lock_irqsave(&m->lock, flags);
  920. /* Features */
  921. if (type == STATUSTYPE_INFO)
  922. DMEMIT("1 %u ", m->queue_size);
  923. else if (m->queue_if_no_path)
  924. DMEMIT("1 queue_if_no_path ");
  925. else
  926. DMEMIT("0 ");
  927. if (hwh->type && hwh->type->status)
  928. sz += hwh->type->status(hwh, type, result + sz, maxlen - sz);
  929. else if (!hwh->type || type == STATUSTYPE_INFO)
  930. DMEMIT("0 ");
  931. else
  932. DMEMIT("1 %s ", hwh->type->name);
  933. DMEMIT("%u ", m->nr_priority_groups);
  934. if (m->next_pg)
  935. pg_num = m->next_pg->pg_num;
  936. else if (m->current_pg)
  937. pg_num = m->current_pg->pg_num;
  938. else
  939. pg_num = 1;
  940. DMEMIT("%u ", pg_num);
  941. switch (type) {
  942. case STATUSTYPE_INFO:
  943. list_for_each_entry(pg, &m->priority_groups, list) {
  944. if (pg->bypassed)
  945. state = 'D'; /* Disabled */
  946. else if (pg == m->current_pg)
  947. state = 'A'; /* Currently Active */
  948. else
  949. state = 'E'; /* Enabled */
  950. DMEMIT("%c ", state);
  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->info_args);
  959. list_for_each_entry(p, &pg->pgpaths, list) {
  960. DMEMIT("%s %s %u ", p->path.dev->name,
  961. p->path.is_active ? "A" : "F",
  962. p->fail_count);
  963. if (pg->ps.type->status)
  964. sz += pg->ps.type->status(&pg->ps,
  965. &p->path, type, result + sz,
  966. maxlen - sz);
  967. }
  968. }
  969. break;
  970. case STATUSTYPE_TABLE:
  971. list_for_each_entry(pg, &m->priority_groups, list) {
  972. DMEMIT("%s ", pg->ps.type->name);
  973. if (pg->ps.type->status)
  974. sz += pg->ps.type->status(&pg->ps, NULL, type,
  975. result + sz,
  976. maxlen - sz);
  977. else
  978. DMEMIT("0 ");
  979. DMEMIT("%u %u ", pg->nr_pgpaths,
  980. pg->ps.type->table_args);
  981. list_for_each_entry(p, &pg->pgpaths, list) {
  982. DMEMIT("%s ", p->path.dev->name);
  983. if (pg->ps.type->status)
  984. sz += pg->ps.type->status(&pg->ps,
  985. &p->path, type, result + sz,
  986. maxlen - sz);
  987. }
  988. }
  989. break;
  990. }
  991. spin_unlock_irqrestore(&m->lock, flags);
  992. return 0;
  993. }
  994. static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
  995. {
  996. int r;
  997. struct dm_dev *dev;
  998. struct multipath *m = (struct multipath *) ti->private;
  999. action_fn action;
  1000. if (argc == 1) {
  1001. if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
  1002. return queue_if_no_path(m, 1, 0);
  1003. else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
  1004. return queue_if_no_path(m, 0, 0);
  1005. }
  1006. if (argc != 2)
  1007. goto error;
  1008. if (!strnicmp(argv[0], MESG_STR("disable_group")))
  1009. return bypass_pg_num(m, argv[1], 1);
  1010. else if (!strnicmp(argv[0], MESG_STR("enable_group")))
  1011. return bypass_pg_num(m, argv[1], 0);
  1012. else if (!strnicmp(argv[0], MESG_STR("switch_group")))
  1013. return switch_pg_num(m, argv[1]);
  1014. else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
  1015. action = reinstate_path;
  1016. else if (!strnicmp(argv[0], MESG_STR("fail_path")))
  1017. action = fail_path;
  1018. else
  1019. goto error;
  1020. r = dm_get_device(ti, argv[1], ti->begin, ti->len,
  1021. dm_table_get_mode(ti->table), &dev);
  1022. if (r) {
  1023. DMWARN("message: error getting device %s",
  1024. argv[1]);
  1025. return -EINVAL;
  1026. }
  1027. r = action_dev(m, dev, action);
  1028. dm_put_device(ti, dev);
  1029. return r;
  1030. error:
  1031. DMWARN("Unrecognised multipath message received.");
  1032. return -EINVAL;
  1033. }
  1034. static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
  1035. struct file *filp, unsigned int cmd,
  1036. unsigned long arg)
  1037. {
  1038. struct multipath *m = (struct multipath *) ti->private;
  1039. struct block_device *bdev = NULL;
  1040. unsigned long flags;
  1041. struct file fake_file = {};
  1042. struct dentry fake_dentry = {};
  1043. int r = 0;
  1044. fake_file.f_path.dentry = &fake_dentry;
  1045. spin_lock_irqsave(&m->lock, flags);
  1046. if (!m->current_pgpath)
  1047. __choose_pgpath(m);
  1048. if (m->current_pgpath) {
  1049. bdev = m->current_pgpath->path.dev->bdev;
  1050. fake_dentry.d_inode = bdev->bd_inode;
  1051. fake_file.f_mode = m->current_pgpath->path.dev->mode;
  1052. }
  1053. if (m->queue_io)
  1054. r = -EAGAIN;
  1055. else if (!bdev)
  1056. r = -EIO;
  1057. spin_unlock_irqrestore(&m->lock, flags);
  1058. return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
  1059. bdev->bd_disk, cmd, arg);
  1060. }
  1061. /*-----------------------------------------------------------------
  1062. * Module setup
  1063. *---------------------------------------------------------------*/
  1064. static struct target_type multipath_target = {
  1065. .name = "multipath",
  1066. .version = {1, 0, 5},
  1067. .module = THIS_MODULE,
  1068. .ctr = multipath_ctr,
  1069. .dtr = multipath_dtr,
  1070. .map = multipath_map,
  1071. .end_io = multipath_end_io,
  1072. .presuspend = multipath_presuspend,
  1073. .resume = multipath_resume,
  1074. .status = multipath_status,
  1075. .message = multipath_message,
  1076. .ioctl = multipath_ioctl,
  1077. };
  1078. static int __init dm_multipath_init(void)
  1079. {
  1080. int r;
  1081. /* allocate a slab for the dm_ios */
  1082. _mpio_cache = kmem_cache_create("dm_mpath", sizeof(struct mpath_io),
  1083. 0, 0, NULL, NULL);
  1084. if (!_mpio_cache)
  1085. return -ENOMEM;
  1086. r = dm_register_target(&multipath_target);
  1087. if (r < 0) {
  1088. DMERR("%s: register failed %d", multipath_target.name, r);
  1089. kmem_cache_destroy(_mpio_cache);
  1090. return -EINVAL;
  1091. }
  1092. kmultipathd = create_workqueue("kmpathd");
  1093. if (!kmultipathd) {
  1094. DMERR("%s: failed to create workqueue kmpathd",
  1095. multipath_target.name);
  1096. dm_unregister_target(&multipath_target);
  1097. kmem_cache_destroy(_mpio_cache);
  1098. return -ENOMEM;
  1099. }
  1100. DMINFO("version %u.%u.%u loaded",
  1101. multipath_target.version[0], multipath_target.version[1],
  1102. multipath_target.version[2]);
  1103. return r;
  1104. }
  1105. static void __exit dm_multipath_exit(void)
  1106. {
  1107. int r;
  1108. destroy_workqueue(kmultipathd);
  1109. r = dm_unregister_target(&multipath_target);
  1110. if (r < 0)
  1111. DMERR("%s: target unregister failed %d",
  1112. multipath_target.name, r);
  1113. kmem_cache_destroy(_mpio_cache);
  1114. }
  1115. EXPORT_SYMBOL_GPL(dm_pg_init_complete);
  1116. module_init(dm_multipath_init);
  1117. module_exit(dm_multipath_exit);
  1118. MODULE_DESCRIPTION(DM_NAME " multipath target");
  1119. MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
  1120. MODULE_LICENSE("GPL");