dm-mpath.c 41 KB

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