dm-mpath.c 41 KB

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