dm-mpath.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772
  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 <linux/device-mapper.h>
  8. #include "dm.h"
  9. #include "dm-path-selector.h"
  10. #include "dm-uevent.h"
  11. #include <linux/blkdev.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 <linux/delay.h>
  21. #include <scsi/scsi_dh.h>
  22. #include <linux/atomic.h>
  23. #define DM_MSG_PREFIX "multipath"
  24. #define DM_PG_INIT_DELAY_MSECS 2000
  25. #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
  26. /* Path properties */
  27. struct pgpath {
  28. struct list_head list;
  29. struct priority_group *pg; /* Owning PG */
  30. unsigned is_active; /* Path status */
  31. unsigned fail_count; /* Cumulative failure count */
  32. struct dm_path path;
  33. struct delayed_work activate_path;
  34. };
  35. #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
  36. /*
  37. * Paths are grouped into Priority Groups and numbered from 1 upwards.
  38. * Each has a path selector which controls which path gets used.
  39. */
  40. struct priority_group {
  41. struct list_head list;
  42. struct multipath *m; /* Owning multipath instance */
  43. struct path_selector ps;
  44. unsigned pg_num; /* Reference number */
  45. unsigned bypassed; /* Temporarily bypass this PG? */
  46. unsigned nr_pgpaths; /* Number of paths in PG */
  47. struct list_head pgpaths;
  48. };
  49. /* Multipath context */
  50. struct multipath {
  51. struct list_head list;
  52. struct dm_target *ti;
  53. const char *hw_handler_name;
  54. char *hw_handler_params;
  55. spinlock_t lock;
  56. unsigned nr_priority_groups;
  57. struct list_head priority_groups;
  58. wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
  59. unsigned pg_init_required; /* pg_init needs calling? */
  60. unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
  61. unsigned pg_init_delay_retry; /* Delay pg_init retry? */
  62. unsigned nr_valid_paths; /* Total number of usable paths */
  63. struct pgpath *current_pgpath;
  64. struct priority_group *current_pg;
  65. struct priority_group *next_pg; /* Switch to this PG if set */
  66. unsigned repeat_count; /* I/Os left before calling PS again */
  67. unsigned queue_io:1; /* Must we queue all I/O? */
  68. unsigned queue_if_no_path:1; /* Queue I/O if last path fails? */
  69. unsigned saved_queue_if_no_path:1; /* Saved state during suspension */
  70. unsigned retain_attached_hw_handler:1; /* If there's already a hw_handler present, don't change it. */
  71. unsigned pg_init_disabled:1; /* pg_init is not currently allowed */
  72. unsigned pg_init_retries; /* Number of times to retry pg_init */
  73. unsigned pg_init_count; /* Number of times pg_init called */
  74. unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */
  75. struct work_struct trigger_event;
  76. /*
  77. * We must use a mempool of dm_mpath_io structs so that we
  78. * can resubmit bios on error.
  79. */
  80. mempool_t *mpio_pool;
  81. struct mutex work_mutex;
  82. };
  83. /*
  84. * Context information attached to each bio we process.
  85. */
  86. struct dm_mpath_io {
  87. struct pgpath *pgpath;
  88. size_t nr_bytes;
  89. };
  90. typedef int (*action_fn) (struct pgpath *pgpath);
  91. static struct kmem_cache *_mpio_cache;
  92. static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
  93. static void trigger_event(struct work_struct *work);
  94. static void activate_path(struct work_struct *work);
  95. static int __pgpath_busy(struct pgpath *pgpath);
  96. /*-----------------------------------------------
  97. * Allocation routines
  98. *-----------------------------------------------*/
  99. static struct pgpath *alloc_pgpath(void)
  100. {
  101. struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
  102. if (pgpath) {
  103. pgpath->is_active = 1;
  104. INIT_DELAYED_WORK(&pgpath->activate_path, activate_path);
  105. }
  106. return pgpath;
  107. }
  108. static void free_pgpath(struct pgpath *pgpath)
  109. {
  110. kfree(pgpath);
  111. }
  112. static struct priority_group *alloc_priority_group(void)
  113. {
  114. struct priority_group *pg;
  115. pg = kzalloc(sizeof(*pg), GFP_KERNEL);
  116. if (pg)
  117. INIT_LIST_HEAD(&pg->pgpaths);
  118. return pg;
  119. }
  120. static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
  121. {
  122. struct pgpath *pgpath, *tmp;
  123. list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
  124. list_del(&pgpath->list);
  125. dm_put_device(ti, pgpath->path.dev);
  126. free_pgpath(pgpath);
  127. }
  128. }
  129. static void free_priority_group(struct priority_group *pg,
  130. struct dm_target *ti)
  131. {
  132. struct path_selector *ps = &pg->ps;
  133. if (ps->type) {
  134. ps->type->destroy(ps);
  135. dm_put_path_selector(ps->type);
  136. }
  137. free_pgpaths(&pg->pgpaths, ti);
  138. kfree(pg);
  139. }
  140. static struct multipath *alloc_multipath(struct dm_target *ti)
  141. {
  142. struct multipath *m;
  143. unsigned min_ios = dm_get_reserved_rq_based_ios();
  144. m = kzalloc(sizeof(*m), GFP_KERNEL);
  145. if (m) {
  146. INIT_LIST_HEAD(&m->priority_groups);
  147. spin_lock_init(&m->lock);
  148. m->queue_io = 1;
  149. m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
  150. INIT_WORK(&m->trigger_event, trigger_event);
  151. init_waitqueue_head(&m->pg_init_wait);
  152. mutex_init(&m->work_mutex);
  153. m->mpio_pool = mempool_create_slab_pool(min_ios, _mpio_cache);
  154. if (!m->mpio_pool) {
  155. kfree(m);
  156. return NULL;
  157. }
  158. m->ti = ti;
  159. ti->private = m;
  160. }
  161. return m;
  162. }
  163. static void free_multipath(struct multipath *m)
  164. {
  165. struct priority_group *pg, *tmp;
  166. list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
  167. list_del(&pg->list);
  168. free_priority_group(pg, m->ti);
  169. }
  170. kfree(m->hw_handler_name);
  171. kfree(m->hw_handler_params);
  172. mempool_destroy(m->mpio_pool);
  173. kfree(m);
  174. }
  175. static int set_mapinfo(struct multipath *m, union map_info *info)
  176. {
  177. struct dm_mpath_io *mpio;
  178. mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
  179. if (!mpio)
  180. return -ENOMEM;
  181. memset(mpio, 0, sizeof(*mpio));
  182. info->ptr = mpio;
  183. return 0;
  184. }
  185. static void clear_mapinfo(struct multipath *m, union map_info *info)
  186. {
  187. struct dm_mpath_io *mpio = info->ptr;
  188. info->ptr = NULL;
  189. mempool_free(mpio, m->mpio_pool);
  190. }
  191. /*-----------------------------------------------
  192. * Path selection
  193. *-----------------------------------------------*/
  194. static int __pg_init_all_paths(struct multipath *m)
  195. {
  196. struct pgpath *pgpath;
  197. unsigned long pg_init_delay = 0;
  198. if (m->pg_init_in_progress || m->pg_init_disabled)
  199. return 0;
  200. m->pg_init_count++;
  201. m->pg_init_required = 0;
  202. /* Check here to reset pg_init_required */
  203. if (!m->current_pg)
  204. return 0;
  205. if (m->pg_init_delay_retry)
  206. pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
  207. m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
  208. list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
  209. /* Skip failed paths */
  210. if (!pgpath->is_active)
  211. continue;
  212. if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
  213. pg_init_delay))
  214. m->pg_init_in_progress++;
  215. }
  216. return m->pg_init_in_progress;
  217. }
  218. static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
  219. {
  220. m->current_pg = pgpath->pg;
  221. /* Must we initialise the PG first, and queue I/O till it's ready? */
  222. if (m->hw_handler_name) {
  223. m->pg_init_required = 1;
  224. m->queue_io = 1;
  225. } else {
  226. m->pg_init_required = 0;
  227. m->queue_io = 0;
  228. }
  229. m->pg_init_count = 0;
  230. }
  231. static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
  232. size_t nr_bytes)
  233. {
  234. struct dm_path *path;
  235. path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
  236. if (!path)
  237. return -ENXIO;
  238. m->current_pgpath = path_to_pgpath(path);
  239. if (m->current_pg != pg)
  240. __switch_pg(m, m->current_pgpath);
  241. return 0;
  242. }
  243. static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
  244. {
  245. struct priority_group *pg;
  246. unsigned bypassed = 1;
  247. if (!m->nr_valid_paths) {
  248. m->queue_io = 0;
  249. goto failed;
  250. }
  251. /* Were we instructed to switch PG? */
  252. if (m->next_pg) {
  253. pg = m->next_pg;
  254. m->next_pg = NULL;
  255. if (!__choose_path_in_pg(m, pg, nr_bytes))
  256. return;
  257. }
  258. /* Don't change PG until it has no remaining paths */
  259. if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
  260. return;
  261. /*
  262. * Loop through priority groups until we find a valid path.
  263. * First time we skip PGs marked 'bypassed'.
  264. * Second time we only try the ones we skipped, but set
  265. * pg_init_delay_retry so we do not hammer controllers.
  266. */
  267. do {
  268. list_for_each_entry(pg, &m->priority_groups, list) {
  269. if (pg->bypassed == bypassed)
  270. continue;
  271. if (!__choose_path_in_pg(m, pg, nr_bytes)) {
  272. if (!bypassed)
  273. m->pg_init_delay_retry = 1;
  274. return;
  275. }
  276. }
  277. } while (bypassed--);
  278. failed:
  279. m->current_pgpath = NULL;
  280. m->current_pg = NULL;
  281. }
  282. /*
  283. * Check whether bios must be queued in the device-mapper core rather
  284. * than here in the target.
  285. *
  286. * m->lock must be held on entry.
  287. *
  288. * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
  289. * same value then we are not between multipath_presuspend()
  290. * and multipath_resume() calls and we have no need to check
  291. * for the DMF_NOFLUSH_SUSPENDING flag.
  292. */
  293. static int __must_push_back(struct multipath *m)
  294. {
  295. return (m->queue_if_no_path ||
  296. (m->queue_if_no_path != m->saved_queue_if_no_path &&
  297. dm_noflush_suspending(m->ti)));
  298. }
  299. /*
  300. * Map cloned requests
  301. */
  302. static int __multipath_map(struct dm_target *ti, struct request *clone,
  303. union map_info *map_context,
  304. struct request *rq, struct request **__clone)
  305. {
  306. struct multipath *m = (struct multipath *) ti->private;
  307. int r = DM_MAPIO_REQUEUE;
  308. size_t nr_bytes = clone ? blk_rq_bytes(clone) : blk_rq_bytes(rq);
  309. struct pgpath *pgpath;
  310. struct block_device *bdev;
  311. struct dm_mpath_io *mpio;
  312. spin_lock_irq(&m->lock);
  313. /* Do we need to select a new pgpath? */
  314. if (!m->current_pgpath ||
  315. (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
  316. __choose_pgpath(m, nr_bytes);
  317. pgpath = m->current_pgpath;
  318. if (!pgpath) {
  319. if (!__must_push_back(m))
  320. r = -EIO; /* Failed */
  321. goto out_unlock;
  322. } else if (m->queue_io || m->pg_init_required) {
  323. __pg_init_all_paths(m);
  324. goto out_unlock;
  325. }
  326. if (set_mapinfo(m, map_context) < 0)
  327. /* ENOMEM, requeue */
  328. goto out_unlock;
  329. mpio = map_context->ptr;
  330. mpio->pgpath = pgpath;
  331. mpio->nr_bytes = nr_bytes;
  332. bdev = pgpath->path.dev->bdev;
  333. spin_unlock_irq(&m->lock);
  334. if (clone) {
  335. /* Old request-based interface: allocated clone is passed in */
  336. clone->q = bdev_get_queue(bdev);
  337. clone->rq_disk = bdev->bd_disk;
  338. clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
  339. } else {
  340. /* blk-mq request-based interface */
  341. *__clone = blk_get_request(bdev_get_queue(bdev),
  342. rq_data_dir(rq), GFP_ATOMIC);
  343. if (IS_ERR(*__clone)) {
  344. /* ENOMEM, requeue */
  345. clear_mapinfo(m, map_context);
  346. return r;
  347. }
  348. (*__clone)->bio = (*__clone)->biotail = NULL;
  349. (*__clone)->rq_disk = bdev->bd_disk;
  350. (*__clone)->cmd_flags |= REQ_FAILFAST_TRANSPORT;
  351. }
  352. if (pgpath->pg->ps.type->start_io)
  353. pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
  354. &pgpath->path,
  355. nr_bytes);
  356. return DM_MAPIO_REMAPPED;
  357. out_unlock:
  358. spin_unlock_irq(&m->lock);
  359. return r;
  360. }
  361. static int multipath_map(struct dm_target *ti, struct request *clone,
  362. union map_info *map_context)
  363. {
  364. return __multipath_map(ti, clone, map_context, NULL, NULL);
  365. }
  366. static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
  367. union map_info *map_context,
  368. struct request **clone)
  369. {
  370. return __multipath_map(ti, NULL, map_context, rq, clone);
  371. }
  372. static void multipath_release_clone(struct request *clone)
  373. {
  374. blk_put_request(clone);
  375. }
  376. /*
  377. * If we run out of usable paths, should we queue I/O or error it?
  378. */
  379. static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
  380. unsigned save_old_value)
  381. {
  382. unsigned long flags;
  383. spin_lock_irqsave(&m->lock, flags);
  384. if (save_old_value)
  385. m->saved_queue_if_no_path = m->queue_if_no_path;
  386. else
  387. m->saved_queue_if_no_path = queue_if_no_path;
  388. m->queue_if_no_path = queue_if_no_path;
  389. spin_unlock_irqrestore(&m->lock, flags);
  390. if (!queue_if_no_path)
  391. dm_table_run_md_queue_async(m->ti->table);
  392. return 0;
  393. }
  394. /*
  395. * An event is triggered whenever a path is taken out of use.
  396. * Includes path failure and PG bypass.
  397. */
  398. static void trigger_event(struct work_struct *work)
  399. {
  400. struct multipath *m =
  401. container_of(work, struct multipath, trigger_event);
  402. dm_table_event(m->ti->table);
  403. }
  404. /*-----------------------------------------------------------------
  405. * Constructor/argument parsing:
  406. * <#multipath feature args> [<arg>]*
  407. * <#hw_handler args> [hw_handler [<arg>]*]
  408. * <#priority groups>
  409. * <initial priority group>
  410. * [<selector> <#selector args> [<arg>]*
  411. * <#paths> <#per-path selector args>
  412. * [<path> [<arg>]* ]+ ]+
  413. *---------------------------------------------------------------*/
  414. static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
  415. struct dm_target *ti)
  416. {
  417. int r;
  418. struct path_selector_type *pst;
  419. unsigned ps_argc;
  420. static struct dm_arg _args[] = {
  421. {0, 1024, "invalid number of path selector args"},
  422. };
  423. pst = dm_get_path_selector(dm_shift_arg(as));
  424. if (!pst) {
  425. ti->error = "unknown path selector type";
  426. return -EINVAL;
  427. }
  428. r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
  429. if (r) {
  430. dm_put_path_selector(pst);
  431. return -EINVAL;
  432. }
  433. r = pst->create(&pg->ps, ps_argc, as->argv);
  434. if (r) {
  435. dm_put_path_selector(pst);
  436. ti->error = "path selector constructor failed";
  437. return r;
  438. }
  439. pg->ps.type = pst;
  440. dm_consume_args(as, ps_argc);
  441. return 0;
  442. }
  443. static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
  444. struct dm_target *ti)
  445. {
  446. int r;
  447. struct pgpath *p;
  448. struct multipath *m = ti->private;
  449. struct request_queue *q = NULL;
  450. const char *attached_handler_name;
  451. /* we need at least a path arg */
  452. if (as->argc < 1) {
  453. ti->error = "no device given";
  454. return ERR_PTR(-EINVAL);
  455. }
  456. p = alloc_pgpath();
  457. if (!p)
  458. return ERR_PTR(-ENOMEM);
  459. r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
  460. &p->path.dev);
  461. if (r) {
  462. ti->error = "error getting device";
  463. goto bad;
  464. }
  465. if (m->retain_attached_hw_handler || m->hw_handler_name)
  466. q = bdev_get_queue(p->path.dev->bdev);
  467. if (m->retain_attached_hw_handler) {
  468. retain:
  469. attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
  470. if (attached_handler_name) {
  471. /*
  472. * Reset hw_handler_name to match the attached handler
  473. * and clear any hw_handler_params associated with the
  474. * ignored handler.
  475. *
  476. * NB. This modifies the table line to show the actual
  477. * handler instead of the original table passed in.
  478. */
  479. kfree(m->hw_handler_name);
  480. m->hw_handler_name = attached_handler_name;
  481. kfree(m->hw_handler_params);
  482. m->hw_handler_params = NULL;
  483. }
  484. }
  485. if (m->hw_handler_name) {
  486. r = scsi_dh_attach(q, m->hw_handler_name);
  487. if (r == -EBUSY) {
  488. char b[BDEVNAME_SIZE];
  489. printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
  490. bdevname(p->path.dev->bdev, b));
  491. goto retain;
  492. }
  493. if (r < 0) {
  494. ti->error = "error attaching hardware handler";
  495. dm_put_device(ti, p->path.dev);
  496. goto bad;
  497. }
  498. if (m->hw_handler_params) {
  499. r = scsi_dh_set_params(q, m->hw_handler_params);
  500. if (r < 0) {
  501. ti->error = "unable to set hardware "
  502. "handler parameters";
  503. dm_put_device(ti, p->path.dev);
  504. goto bad;
  505. }
  506. }
  507. }
  508. r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
  509. if (r) {
  510. dm_put_device(ti, p->path.dev);
  511. goto bad;
  512. }
  513. return p;
  514. bad:
  515. free_pgpath(p);
  516. return ERR_PTR(r);
  517. }
  518. static struct priority_group *parse_priority_group(struct dm_arg_set *as,
  519. struct multipath *m)
  520. {
  521. static struct dm_arg _args[] = {
  522. {1, 1024, "invalid number of paths"},
  523. {0, 1024, "invalid number of selector args"}
  524. };
  525. int r;
  526. unsigned i, nr_selector_args, nr_args;
  527. struct priority_group *pg;
  528. struct dm_target *ti = m->ti;
  529. if (as->argc < 2) {
  530. as->argc = 0;
  531. ti->error = "not enough priority group arguments";
  532. return ERR_PTR(-EINVAL);
  533. }
  534. pg = alloc_priority_group();
  535. if (!pg) {
  536. ti->error = "couldn't allocate priority group";
  537. return ERR_PTR(-ENOMEM);
  538. }
  539. pg->m = m;
  540. r = parse_path_selector(as, pg, ti);
  541. if (r)
  542. goto bad;
  543. /*
  544. * read the paths
  545. */
  546. r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
  547. if (r)
  548. goto bad;
  549. r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
  550. if (r)
  551. goto bad;
  552. nr_args = 1 + nr_selector_args;
  553. for (i = 0; i < pg->nr_pgpaths; i++) {
  554. struct pgpath *pgpath;
  555. struct dm_arg_set path_args;
  556. if (as->argc < nr_args) {
  557. ti->error = "not enough path parameters";
  558. r = -EINVAL;
  559. goto bad;
  560. }
  561. path_args.argc = nr_args;
  562. path_args.argv = as->argv;
  563. pgpath = parse_path(&path_args, &pg->ps, ti);
  564. if (IS_ERR(pgpath)) {
  565. r = PTR_ERR(pgpath);
  566. goto bad;
  567. }
  568. pgpath->pg = pg;
  569. list_add_tail(&pgpath->list, &pg->pgpaths);
  570. dm_consume_args(as, nr_args);
  571. }
  572. return pg;
  573. bad:
  574. free_priority_group(pg, ti);
  575. return ERR_PTR(r);
  576. }
  577. static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
  578. {
  579. unsigned hw_argc;
  580. int ret;
  581. struct dm_target *ti = m->ti;
  582. static struct dm_arg _args[] = {
  583. {0, 1024, "invalid number of hardware handler args"},
  584. };
  585. if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
  586. return -EINVAL;
  587. if (!hw_argc)
  588. return 0;
  589. m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
  590. if (hw_argc > 1) {
  591. char *p;
  592. int i, j, len = 4;
  593. for (i = 0; i <= hw_argc - 2; i++)
  594. len += strlen(as->argv[i]) + 1;
  595. p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
  596. if (!p) {
  597. ti->error = "memory allocation failed";
  598. ret = -ENOMEM;
  599. goto fail;
  600. }
  601. j = sprintf(p, "%d", hw_argc - 1);
  602. for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
  603. j = sprintf(p, "%s", as->argv[i]);
  604. }
  605. dm_consume_args(as, hw_argc - 1);
  606. return 0;
  607. fail:
  608. kfree(m->hw_handler_name);
  609. m->hw_handler_name = NULL;
  610. return ret;
  611. }
  612. static int parse_features(struct dm_arg_set *as, struct multipath *m)
  613. {
  614. int r;
  615. unsigned argc;
  616. struct dm_target *ti = m->ti;
  617. const char *arg_name;
  618. static struct dm_arg _args[] = {
  619. {0, 6, "invalid number of feature args"},
  620. {1, 50, "pg_init_retries must be between 1 and 50"},
  621. {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
  622. };
  623. r = dm_read_arg_group(_args, as, &argc, &ti->error);
  624. if (r)
  625. return -EINVAL;
  626. if (!argc)
  627. return 0;
  628. do {
  629. arg_name = dm_shift_arg(as);
  630. argc--;
  631. if (!strcasecmp(arg_name, "queue_if_no_path")) {
  632. r = queue_if_no_path(m, 1, 0);
  633. continue;
  634. }
  635. if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
  636. m->retain_attached_hw_handler = 1;
  637. continue;
  638. }
  639. if (!strcasecmp(arg_name, "pg_init_retries") &&
  640. (argc >= 1)) {
  641. r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
  642. argc--;
  643. continue;
  644. }
  645. if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
  646. (argc >= 1)) {
  647. r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
  648. argc--;
  649. continue;
  650. }
  651. ti->error = "Unrecognised multipath feature request";
  652. r = -EINVAL;
  653. } while (argc && !r);
  654. return r;
  655. }
  656. static int multipath_ctr(struct dm_target *ti, unsigned int argc,
  657. char **argv)
  658. {
  659. /* target arguments */
  660. static struct dm_arg _args[] = {
  661. {0, 1024, "invalid number of priority groups"},
  662. {0, 1024, "invalid initial priority group number"},
  663. };
  664. int r;
  665. struct multipath *m;
  666. struct dm_arg_set as;
  667. unsigned pg_count = 0;
  668. unsigned next_pg_num;
  669. as.argc = argc;
  670. as.argv = argv;
  671. m = alloc_multipath(ti);
  672. if (!m) {
  673. ti->error = "can't allocate multipath";
  674. return -EINVAL;
  675. }
  676. r = parse_features(&as, m);
  677. if (r)
  678. goto bad;
  679. r = parse_hw_handler(&as, m);
  680. if (r)
  681. goto bad;
  682. r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
  683. if (r)
  684. goto bad;
  685. r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
  686. if (r)
  687. goto bad;
  688. if ((!m->nr_priority_groups && next_pg_num) ||
  689. (m->nr_priority_groups && !next_pg_num)) {
  690. ti->error = "invalid initial priority group";
  691. r = -EINVAL;
  692. goto bad;
  693. }
  694. /* parse the priority groups */
  695. while (as.argc) {
  696. struct priority_group *pg;
  697. pg = parse_priority_group(&as, m);
  698. if (IS_ERR(pg)) {
  699. r = PTR_ERR(pg);
  700. goto bad;
  701. }
  702. m->nr_valid_paths += pg->nr_pgpaths;
  703. list_add_tail(&pg->list, &m->priority_groups);
  704. pg_count++;
  705. pg->pg_num = pg_count;
  706. if (!--next_pg_num)
  707. m->next_pg = pg;
  708. }
  709. if (pg_count != m->nr_priority_groups) {
  710. ti->error = "priority group count mismatch";
  711. r = -EINVAL;
  712. goto bad;
  713. }
  714. ti->num_flush_bios = 1;
  715. ti->num_discard_bios = 1;
  716. ti->num_write_same_bios = 1;
  717. return 0;
  718. bad:
  719. free_multipath(m);
  720. return r;
  721. }
  722. static void multipath_wait_for_pg_init_completion(struct multipath *m)
  723. {
  724. DECLARE_WAITQUEUE(wait, current);
  725. unsigned long flags;
  726. add_wait_queue(&m->pg_init_wait, &wait);
  727. while (1) {
  728. set_current_state(TASK_UNINTERRUPTIBLE);
  729. spin_lock_irqsave(&m->lock, flags);
  730. if (!m->pg_init_in_progress) {
  731. spin_unlock_irqrestore(&m->lock, flags);
  732. break;
  733. }
  734. spin_unlock_irqrestore(&m->lock, flags);
  735. io_schedule();
  736. }
  737. set_current_state(TASK_RUNNING);
  738. remove_wait_queue(&m->pg_init_wait, &wait);
  739. }
  740. static void flush_multipath_work(struct multipath *m)
  741. {
  742. unsigned long flags;
  743. spin_lock_irqsave(&m->lock, flags);
  744. m->pg_init_disabled = 1;
  745. spin_unlock_irqrestore(&m->lock, flags);
  746. flush_workqueue(kmpath_handlerd);
  747. multipath_wait_for_pg_init_completion(m);
  748. flush_workqueue(kmultipathd);
  749. flush_work(&m->trigger_event);
  750. spin_lock_irqsave(&m->lock, flags);
  751. m->pg_init_disabled = 0;
  752. spin_unlock_irqrestore(&m->lock, flags);
  753. }
  754. static void multipath_dtr(struct dm_target *ti)
  755. {
  756. struct multipath *m = ti->private;
  757. flush_multipath_work(m);
  758. free_multipath(m);
  759. }
  760. /*
  761. * Take a path out of use.
  762. */
  763. static int fail_path(struct pgpath *pgpath)
  764. {
  765. unsigned long flags;
  766. struct multipath *m = pgpath->pg->m;
  767. spin_lock_irqsave(&m->lock, flags);
  768. if (!pgpath->is_active)
  769. goto out;
  770. DMWARN("Failing path %s.", pgpath->path.dev->name);
  771. pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
  772. pgpath->is_active = 0;
  773. pgpath->fail_count++;
  774. m->nr_valid_paths--;
  775. if (pgpath == m->current_pgpath)
  776. m->current_pgpath = NULL;
  777. dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
  778. pgpath->path.dev->name, m->nr_valid_paths);
  779. schedule_work(&m->trigger_event);
  780. out:
  781. spin_unlock_irqrestore(&m->lock, flags);
  782. return 0;
  783. }
  784. /*
  785. * Reinstate a previously-failed path
  786. */
  787. static int reinstate_path(struct pgpath *pgpath)
  788. {
  789. int r = 0, run_queue = 0;
  790. unsigned long flags;
  791. struct multipath *m = pgpath->pg->m;
  792. spin_lock_irqsave(&m->lock, flags);
  793. if (pgpath->is_active)
  794. goto out;
  795. if (!pgpath->pg->ps.type->reinstate_path) {
  796. DMWARN("Reinstate path not supported by path selector %s",
  797. pgpath->pg->ps.type->name);
  798. r = -EINVAL;
  799. goto out;
  800. }
  801. r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
  802. if (r)
  803. goto out;
  804. pgpath->is_active = 1;
  805. if (!m->nr_valid_paths++) {
  806. m->current_pgpath = NULL;
  807. run_queue = 1;
  808. } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
  809. if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
  810. m->pg_init_in_progress++;
  811. }
  812. dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
  813. pgpath->path.dev->name, m->nr_valid_paths);
  814. schedule_work(&m->trigger_event);
  815. out:
  816. spin_unlock_irqrestore(&m->lock, flags);
  817. if (run_queue)
  818. dm_table_run_md_queue_async(m->ti->table);
  819. return r;
  820. }
  821. /*
  822. * Fail or reinstate all paths that match the provided struct dm_dev.
  823. */
  824. static int action_dev(struct multipath *m, struct dm_dev *dev,
  825. action_fn action)
  826. {
  827. int r = -EINVAL;
  828. struct pgpath *pgpath;
  829. struct priority_group *pg;
  830. list_for_each_entry(pg, &m->priority_groups, list) {
  831. list_for_each_entry(pgpath, &pg->pgpaths, list) {
  832. if (pgpath->path.dev == dev)
  833. r = action(pgpath);
  834. }
  835. }
  836. return r;
  837. }
  838. /*
  839. * Temporarily try to avoid having to use the specified PG
  840. */
  841. static void bypass_pg(struct multipath *m, struct priority_group *pg,
  842. int bypassed)
  843. {
  844. unsigned long flags;
  845. spin_lock_irqsave(&m->lock, flags);
  846. pg->bypassed = bypassed;
  847. m->current_pgpath = NULL;
  848. m->current_pg = NULL;
  849. spin_unlock_irqrestore(&m->lock, flags);
  850. schedule_work(&m->trigger_event);
  851. }
  852. /*
  853. * Switch to using the specified PG from the next I/O that gets mapped
  854. */
  855. static int switch_pg_num(struct multipath *m, const char *pgstr)
  856. {
  857. struct priority_group *pg;
  858. unsigned pgnum;
  859. unsigned long flags;
  860. char dummy;
  861. if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
  862. (pgnum > m->nr_priority_groups)) {
  863. DMWARN("invalid PG number supplied to switch_pg_num");
  864. return -EINVAL;
  865. }
  866. spin_lock_irqsave(&m->lock, flags);
  867. list_for_each_entry(pg, &m->priority_groups, list) {
  868. pg->bypassed = 0;
  869. if (--pgnum)
  870. continue;
  871. m->current_pgpath = NULL;
  872. m->current_pg = NULL;
  873. m->next_pg = pg;
  874. }
  875. spin_unlock_irqrestore(&m->lock, flags);
  876. schedule_work(&m->trigger_event);
  877. return 0;
  878. }
  879. /*
  880. * Set/clear bypassed status of a PG.
  881. * PGs are numbered upwards from 1 in the order they were declared.
  882. */
  883. static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
  884. {
  885. struct priority_group *pg;
  886. unsigned pgnum;
  887. char dummy;
  888. if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
  889. (pgnum > m->nr_priority_groups)) {
  890. DMWARN("invalid PG number supplied to bypass_pg");
  891. return -EINVAL;
  892. }
  893. list_for_each_entry(pg, &m->priority_groups, list) {
  894. if (!--pgnum)
  895. break;
  896. }
  897. bypass_pg(m, pg, bypassed);
  898. return 0;
  899. }
  900. /*
  901. * Should we retry pg_init immediately?
  902. */
  903. static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
  904. {
  905. unsigned long flags;
  906. int limit_reached = 0;
  907. spin_lock_irqsave(&m->lock, flags);
  908. if (m->pg_init_count <= m->pg_init_retries && !m->pg_init_disabled)
  909. m->pg_init_required = 1;
  910. else
  911. limit_reached = 1;
  912. spin_unlock_irqrestore(&m->lock, flags);
  913. return limit_reached;
  914. }
  915. static void pg_init_done(void *data, int errors)
  916. {
  917. struct pgpath *pgpath = data;
  918. struct priority_group *pg = pgpath->pg;
  919. struct multipath *m = pg->m;
  920. unsigned long flags;
  921. unsigned delay_retry = 0;
  922. /* device or driver problems */
  923. switch (errors) {
  924. case SCSI_DH_OK:
  925. break;
  926. case SCSI_DH_NOSYS:
  927. if (!m->hw_handler_name) {
  928. errors = 0;
  929. break;
  930. }
  931. DMERR("Could not failover the device: Handler scsi_dh_%s "
  932. "Error %d.", m->hw_handler_name, errors);
  933. /*
  934. * Fail path for now, so we do not ping pong
  935. */
  936. fail_path(pgpath);
  937. break;
  938. case SCSI_DH_DEV_TEMP_BUSY:
  939. /*
  940. * Probably doing something like FW upgrade on the
  941. * controller so try the other pg.
  942. */
  943. bypass_pg(m, pg, 1);
  944. break;
  945. case SCSI_DH_RETRY:
  946. /* Wait before retrying. */
  947. delay_retry = 1;
  948. case SCSI_DH_IMM_RETRY:
  949. case SCSI_DH_RES_TEMP_UNAVAIL:
  950. if (pg_init_limit_reached(m, pgpath))
  951. fail_path(pgpath);
  952. errors = 0;
  953. break;
  954. default:
  955. /*
  956. * We probably do not want to fail the path for a device
  957. * error, but this is what the old dm did. In future
  958. * patches we can do more advanced handling.
  959. */
  960. fail_path(pgpath);
  961. }
  962. spin_lock_irqsave(&m->lock, flags);
  963. if (errors) {
  964. if (pgpath == m->current_pgpath) {
  965. DMERR("Could not failover device. Error %d.", errors);
  966. m->current_pgpath = NULL;
  967. m->current_pg = NULL;
  968. }
  969. } else if (!m->pg_init_required)
  970. pg->bypassed = 0;
  971. if (--m->pg_init_in_progress)
  972. /* Activations of other paths are still on going */
  973. goto out;
  974. if (m->pg_init_required) {
  975. m->pg_init_delay_retry = delay_retry;
  976. if (__pg_init_all_paths(m))
  977. goto out;
  978. }
  979. m->queue_io = 0;
  980. /*
  981. * Wake up any thread waiting to suspend.
  982. */
  983. wake_up(&m->pg_init_wait);
  984. out:
  985. spin_unlock_irqrestore(&m->lock, flags);
  986. }
  987. static void activate_path(struct work_struct *work)
  988. {
  989. struct pgpath *pgpath =
  990. container_of(work, struct pgpath, activate_path.work);
  991. struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
  992. if (pgpath->is_active && !blk_queue_dying(q))
  993. scsi_dh_activate(q, pg_init_done, pgpath);
  994. else
  995. pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
  996. }
  997. static int noretry_error(int error)
  998. {
  999. switch (error) {
  1000. case -EOPNOTSUPP:
  1001. case -EREMOTEIO:
  1002. case -EILSEQ:
  1003. case -ENODATA:
  1004. case -ENOSPC:
  1005. return 1;
  1006. }
  1007. /* Anything else could be a path failure, so should be retried */
  1008. return 0;
  1009. }
  1010. /*
  1011. * end_io handling
  1012. */
  1013. static int do_end_io(struct multipath *m, struct request *clone,
  1014. int error, struct dm_mpath_io *mpio)
  1015. {
  1016. /*
  1017. * We don't queue any clone request inside the multipath target
  1018. * during end I/O handling, since those clone requests don't have
  1019. * bio clones. If we queue them inside the multipath target,
  1020. * we need to make bio clones, that requires memory allocation.
  1021. * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
  1022. * don't have bio clones.)
  1023. * Instead of queueing the clone request here, we queue the original
  1024. * request into dm core, which will remake a clone request and
  1025. * clone bios for it and resubmit it later.
  1026. */
  1027. int r = DM_ENDIO_REQUEUE;
  1028. unsigned long flags;
  1029. if (!error && !clone->errors)
  1030. return 0; /* I/O complete */
  1031. if (noretry_error(error))
  1032. return error;
  1033. if (mpio->pgpath)
  1034. fail_path(mpio->pgpath);
  1035. spin_lock_irqsave(&m->lock, flags);
  1036. if (!m->nr_valid_paths) {
  1037. if (!m->queue_if_no_path) {
  1038. if (!__must_push_back(m))
  1039. r = -EIO;
  1040. } else {
  1041. if (error == -EBADE)
  1042. r = error;
  1043. }
  1044. }
  1045. spin_unlock_irqrestore(&m->lock, flags);
  1046. return r;
  1047. }
  1048. static int multipath_end_io(struct dm_target *ti, struct request *clone,
  1049. int error, union map_info *map_context)
  1050. {
  1051. struct multipath *m = ti->private;
  1052. struct dm_mpath_io *mpio = map_context->ptr;
  1053. struct pgpath *pgpath;
  1054. struct path_selector *ps;
  1055. int r;
  1056. BUG_ON(!mpio);
  1057. r = do_end_io(m, clone, error, mpio);
  1058. pgpath = mpio->pgpath;
  1059. if (pgpath) {
  1060. ps = &pgpath->pg->ps;
  1061. if (ps->type->end_io)
  1062. ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
  1063. }
  1064. clear_mapinfo(m, map_context);
  1065. return r;
  1066. }
  1067. /*
  1068. * Suspend can't complete until all the I/O is processed so if
  1069. * the last path fails we must error any remaining I/O.
  1070. * Note that if the freeze_bdev fails while suspending, the
  1071. * queue_if_no_path state is lost - userspace should reset it.
  1072. */
  1073. static void multipath_presuspend(struct dm_target *ti)
  1074. {
  1075. struct multipath *m = (struct multipath *) ti->private;
  1076. queue_if_no_path(m, 0, 1);
  1077. }
  1078. static void multipath_postsuspend(struct dm_target *ti)
  1079. {
  1080. struct multipath *m = ti->private;
  1081. mutex_lock(&m->work_mutex);
  1082. flush_multipath_work(m);
  1083. mutex_unlock(&m->work_mutex);
  1084. }
  1085. /*
  1086. * Restore the queue_if_no_path setting.
  1087. */
  1088. static void multipath_resume(struct dm_target *ti)
  1089. {
  1090. struct multipath *m = (struct multipath *) ti->private;
  1091. unsigned long flags;
  1092. spin_lock_irqsave(&m->lock, flags);
  1093. m->queue_if_no_path = m->saved_queue_if_no_path;
  1094. spin_unlock_irqrestore(&m->lock, flags);
  1095. }
  1096. /*
  1097. * Info output has the following format:
  1098. * num_multipath_feature_args [multipath_feature_args]*
  1099. * num_handler_status_args [handler_status_args]*
  1100. * num_groups init_group_number
  1101. * [A|D|E num_ps_status_args [ps_status_args]*
  1102. * num_paths num_selector_args
  1103. * [path_dev A|F fail_count [selector_args]* ]+ ]+
  1104. *
  1105. * Table output has the following format (identical to the constructor string):
  1106. * num_feature_args [features_args]*
  1107. * num_handler_args hw_handler [hw_handler_args]*
  1108. * num_groups init_group_number
  1109. * [priority selector-name num_ps_args [ps_args]*
  1110. * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
  1111. */
  1112. static void multipath_status(struct dm_target *ti, status_type_t type,
  1113. unsigned status_flags, char *result, unsigned maxlen)
  1114. {
  1115. int sz = 0;
  1116. unsigned long flags;
  1117. struct multipath *m = (struct multipath *) ti->private;
  1118. struct priority_group *pg;
  1119. struct pgpath *p;
  1120. unsigned pg_num;
  1121. char state;
  1122. spin_lock_irqsave(&m->lock, flags);
  1123. /* Features */
  1124. if (type == STATUSTYPE_INFO)
  1125. DMEMIT("2 %u %u ", m->queue_io, m->pg_init_count);
  1126. else {
  1127. DMEMIT("%u ", m->queue_if_no_path +
  1128. (m->pg_init_retries > 0) * 2 +
  1129. (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
  1130. m->retain_attached_hw_handler);
  1131. if (m->queue_if_no_path)
  1132. DMEMIT("queue_if_no_path ");
  1133. if (m->pg_init_retries)
  1134. DMEMIT("pg_init_retries %u ", m->pg_init_retries);
  1135. if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
  1136. DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
  1137. if (m->retain_attached_hw_handler)
  1138. DMEMIT("retain_attached_hw_handler ");
  1139. }
  1140. if (!m->hw_handler_name || type == STATUSTYPE_INFO)
  1141. DMEMIT("0 ");
  1142. else
  1143. DMEMIT("1 %s ", m->hw_handler_name);
  1144. DMEMIT("%u ", m->nr_priority_groups);
  1145. if (m->next_pg)
  1146. pg_num = m->next_pg->pg_num;
  1147. else if (m->current_pg)
  1148. pg_num = m->current_pg->pg_num;
  1149. else
  1150. pg_num = (m->nr_priority_groups ? 1 : 0);
  1151. DMEMIT("%u ", pg_num);
  1152. switch (type) {
  1153. case STATUSTYPE_INFO:
  1154. list_for_each_entry(pg, &m->priority_groups, list) {
  1155. if (pg->bypassed)
  1156. state = 'D'; /* Disabled */
  1157. else if (pg == m->current_pg)
  1158. state = 'A'; /* Currently Active */
  1159. else
  1160. state = 'E'; /* Enabled */
  1161. DMEMIT("%c ", state);
  1162. if (pg->ps.type->status)
  1163. sz += pg->ps.type->status(&pg->ps, NULL, type,
  1164. result + sz,
  1165. maxlen - sz);
  1166. else
  1167. DMEMIT("0 ");
  1168. DMEMIT("%u %u ", pg->nr_pgpaths,
  1169. pg->ps.type->info_args);
  1170. list_for_each_entry(p, &pg->pgpaths, list) {
  1171. DMEMIT("%s %s %u ", p->path.dev->name,
  1172. p->is_active ? "A" : "F",
  1173. p->fail_count);
  1174. if (pg->ps.type->status)
  1175. sz += pg->ps.type->status(&pg->ps,
  1176. &p->path, type, result + sz,
  1177. maxlen - sz);
  1178. }
  1179. }
  1180. break;
  1181. case STATUSTYPE_TABLE:
  1182. list_for_each_entry(pg, &m->priority_groups, list) {
  1183. DMEMIT("%s ", pg->ps.type->name);
  1184. if (pg->ps.type->status)
  1185. sz += pg->ps.type->status(&pg->ps, NULL, type,
  1186. result + sz,
  1187. maxlen - sz);
  1188. else
  1189. DMEMIT("0 ");
  1190. DMEMIT("%u %u ", pg->nr_pgpaths,
  1191. pg->ps.type->table_args);
  1192. list_for_each_entry(p, &pg->pgpaths, list) {
  1193. DMEMIT("%s ", p->path.dev->name);
  1194. if (pg->ps.type->status)
  1195. sz += pg->ps.type->status(&pg->ps,
  1196. &p->path, type, result + sz,
  1197. maxlen - sz);
  1198. }
  1199. }
  1200. break;
  1201. }
  1202. spin_unlock_irqrestore(&m->lock, flags);
  1203. }
  1204. static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
  1205. {
  1206. int r = -EINVAL;
  1207. struct dm_dev *dev;
  1208. struct multipath *m = (struct multipath *) ti->private;
  1209. action_fn action;
  1210. mutex_lock(&m->work_mutex);
  1211. if (dm_suspended(ti)) {
  1212. r = -EBUSY;
  1213. goto out;
  1214. }
  1215. if (argc == 1) {
  1216. if (!strcasecmp(argv[0], "queue_if_no_path")) {
  1217. r = queue_if_no_path(m, 1, 0);
  1218. goto out;
  1219. } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
  1220. r = queue_if_no_path(m, 0, 0);
  1221. goto out;
  1222. }
  1223. }
  1224. if (argc != 2) {
  1225. DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
  1226. goto out;
  1227. }
  1228. if (!strcasecmp(argv[0], "disable_group")) {
  1229. r = bypass_pg_num(m, argv[1], 1);
  1230. goto out;
  1231. } else if (!strcasecmp(argv[0], "enable_group")) {
  1232. r = bypass_pg_num(m, argv[1], 0);
  1233. goto out;
  1234. } else if (!strcasecmp(argv[0], "switch_group")) {
  1235. r = switch_pg_num(m, argv[1]);
  1236. goto out;
  1237. } else if (!strcasecmp(argv[0], "reinstate_path"))
  1238. action = reinstate_path;
  1239. else if (!strcasecmp(argv[0], "fail_path"))
  1240. action = fail_path;
  1241. else {
  1242. DMWARN("Unrecognised multipath message received: %s", argv[0]);
  1243. goto out;
  1244. }
  1245. r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
  1246. if (r) {
  1247. DMWARN("message: error getting device %s",
  1248. argv[1]);
  1249. goto out;
  1250. }
  1251. r = action_dev(m, dev, action);
  1252. dm_put_device(ti, dev);
  1253. out:
  1254. mutex_unlock(&m->work_mutex);
  1255. return r;
  1256. }
  1257. static int multipath_prepare_ioctl(struct dm_target *ti,
  1258. struct block_device **bdev, fmode_t *mode)
  1259. {
  1260. struct multipath *m = ti->private;
  1261. unsigned long flags;
  1262. int r;
  1263. spin_lock_irqsave(&m->lock, flags);
  1264. if (!m->current_pgpath)
  1265. __choose_pgpath(m, 0);
  1266. if (m->current_pgpath) {
  1267. if (!m->queue_io) {
  1268. *bdev = m->current_pgpath->path.dev->bdev;
  1269. *mode = m->current_pgpath->path.dev->mode;
  1270. r = 0;
  1271. } else {
  1272. /* pg_init has not started or completed */
  1273. r = -ENOTCONN;
  1274. }
  1275. } else {
  1276. /* No path is available */
  1277. if (m->queue_if_no_path)
  1278. r = -ENOTCONN;
  1279. else
  1280. r = -EIO;
  1281. }
  1282. spin_unlock_irqrestore(&m->lock, flags);
  1283. if (r == -ENOTCONN) {
  1284. spin_lock_irqsave(&m->lock, flags);
  1285. if (!m->current_pg) {
  1286. /* Path status changed, redo selection */
  1287. __choose_pgpath(m, 0);
  1288. }
  1289. if (m->pg_init_required)
  1290. __pg_init_all_paths(m);
  1291. spin_unlock_irqrestore(&m->lock, flags);
  1292. dm_table_run_md_queue_async(m->ti->table);
  1293. }
  1294. /*
  1295. * Only pass ioctls through if the device sizes match exactly.
  1296. */
  1297. if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
  1298. return 1;
  1299. return r;
  1300. }
  1301. static int multipath_iterate_devices(struct dm_target *ti,
  1302. iterate_devices_callout_fn fn, void *data)
  1303. {
  1304. struct multipath *m = ti->private;
  1305. struct priority_group *pg;
  1306. struct pgpath *p;
  1307. int ret = 0;
  1308. list_for_each_entry(pg, &m->priority_groups, list) {
  1309. list_for_each_entry(p, &pg->pgpaths, list) {
  1310. ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
  1311. if (ret)
  1312. goto out;
  1313. }
  1314. }
  1315. out:
  1316. return ret;
  1317. }
  1318. static int __pgpath_busy(struct pgpath *pgpath)
  1319. {
  1320. struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
  1321. return blk_lld_busy(q);
  1322. }
  1323. /*
  1324. * We return "busy", only when we can map I/Os but underlying devices
  1325. * are busy (so even if we map I/Os now, the I/Os will wait on
  1326. * the underlying queue).
  1327. * In other words, if we want to kill I/Os or queue them inside us
  1328. * due to map unavailability, we don't return "busy". Otherwise,
  1329. * dm core won't give us the I/Os and we can't do what we want.
  1330. */
  1331. static int multipath_busy(struct dm_target *ti)
  1332. {
  1333. int busy = 0, has_active = 0;
  1334. struct multipath *m = ti->private;
  1335. struct priority_group *pg;
  1336. struct pgpath *pgpath;
  1337. unsigned long flags;
  1338. spin_lock_irqsave(&m->lock, flags);
  1339. /* pg_init in progress or no paths available */
  1340. if (m->pg_init_in_progress ||
  1341. (!m->nr_valid_paths && m->queue_if_no_path)) {
  1342. busy = 1;
  1343. goto out;
  1344. }
  1345. /* Guess which priority_group will be used at next mapping time */
  1346. if (unlikely(!m->current_pgpath && m->next_pg))
  1347. pg = m->next_pg;
  1348. else if (likely(m->current_pg))
  1349. pg = m->current_pg;
  1350. else
  1351. /*
  1352. * We don't know which pg will be used at next mapping time.
  1353. * We don't call __choose_pgpath() here to avoid to trigger
  1354. * pg_init just by busy checking.
  1355. * So we don't know whether underlying devices we will be using
  1356. * at next mapping time are busy or not. Just try mapping.
  1357. */
  1358. goto out;
  1359. /*
  1360. * If there is one non-busy active path at least, the path selector
  1361. * will be able to select it. So we consider such a pg as not busy.
  1362. */
  1363. busy = 1;
  1364. list_for_each_entry(pgpath, &pg->pgpaths, list)
  1365. if (pgpath->is_active) {
  1366. has_active = 1;
  1367. if (!__pgpath_busy(pgpath)) {
  1368. busy = 0;
  1369. break;
  1370. }
  1371. }
  1372. if (!has_active)
  1373. /*
  1374. * No active path in this pg, so this pg won't be used and
  1375. * the current_pg will be changed at next mapping time.
  1376. * We need to try mapping to determine it.
  1377. */
  1378. busy = 0;
  1379. out:
  1380. spin_unlock_irqrestore(&m->lock, flags);
  1381. return busy;
  1382. }
  1383. /*-----------------------------------------------------------------
  1384. * Module setup
  1385. *---------------------------------------------------------------*/
  1386. static struct target_type multipath_target = {
  1387. .name = "multipath",
  1388. .version = {1, 10, 0},
  1389. .module = THIS_MODULE,
  1390. .ctr = multipath_ctr,
  1391. .dtr = multipath_dtr,
  1392. .map_rq = multipath_map,
  1393. .clone_and_map_rq = multipath_clone_and_map,
  1394. .release_clone_rq = multipath_release_clone,
  1395. .rq_end_io = multipath_end_io,
  1396. .presuspend = multipath_presuspend,
  1397. .postsuspend = multipath_postsuspend,
  1398. .resume = multipath_resume,
  1399. .status = multipath_status,
  1400. .message = multipath_message,
  1401. .prepare_ioctl = multipath_prepare_ioctl,
  1402. .iterate_devices = multipath_iterate_devices,
  1403. .busy = multipath_busy,
  1404. };
  1405. static int __init dm_multipath_init(void)
  1406. {
  1407. int r;
  1408. /* allocate a slab for the dm_ios */
  1409. _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
  1410. if (!_mpio_cache)
  1411. return -ENOMEM;
  1412. r = dm_register_target(&multipath_target);
  1413. if (r < 0) {
  1414. DMERR("register failed %d", r);
  1415. r = -EINVAL;
  1416. goto bad_register_target;
  1417. }
  1418. kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
  1419. if (!kmultipathd) {
  1420. DMERR("failed to create workqueue kmpathd");
  1421. r = -ENOMEM;
  1422. goto bad_alloc_kmultipathd;
  1423. }
  1424. /*
  1425. * A separate workqueue is used to handle the device handlers
  1426. * to avoid overloading existing workqueue. Overloading the
  1427. * old workqueue would also create a bottleneck in the
  1428. * path of the storage hardware device activation.
  1429. */
  1430. kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
  1431. WQ_MEM_RECLAIM);
  1432. if (!kmpath_handlerd) {
  1433. DMERR("failed to create workqueue kmpath_handlerd");
  1434. r = -ENOMEM;
  1435. goto bad_alloc_kmpath_handlerd;
  1436. }
  1437. DMINFO("version %u.%u.%u loaded",
  1438. multipath_target.version[0], multipath_target.version[1],
  1439. multipath_target.version[2]);
  1440. return 0;
  1441. bad_alloc_kmpath_handlerd:
  1442. destroy_workqueue(kmultipathd);
  1443. bad_alloc_kmultipathd:
  1444. dm_unregister_target(&multipath_target);
  1445. bad_register_target:
  1446. kmem_cache_destroy(_mpio_cache);
  1447. return r;
  1448. }
  1449. static void __exit dm_multipath_exit(void)
  1450. {
  1451. destroy_workqueue(kmpath_handlerd);
  1452. destroy_workqueue(kmultipathd);
  1453. dm_unregister_target(&multipath_target);
  1454. kmem_cache_destroy(_mpio_cache);
  1455. }
  1456. module_init(dm_multipath_init);
  1457. module_exit(dm_multipath_exit);
  1458. MODULE_DESCRIPTION(DM_NAME " multipath target");
  1459. MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
  1460. MODULE_LICENSE("GPL");