dm-mpath.c 41 KB

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