dm-mpath.c 39 KB

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