dm-mpath.c 40 KB

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