dm-mpath.c 42 KB

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