dm-mpath.c 40 KB

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