dm-mpath.c 39 KB

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