dm-mpath.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771
  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_requests = 1;
  737. ti->num_discard_requests = 1;
  738. return 0;
  739. bad:
  740. free_multipath(m);
  741. return r;
  742. }
  743. static void multipath_wait_for_pg_init_completion(struct multipath *m)
  744. {
  745. DECLARE_WAITQUEUE(wait, current);
  746. unsigned long flags;
  747. add_wait_queue(&m->pg_init_wait, &wait);
  748. while (1) {
  749. set_current_state(TASK_UNINTERRUPTIBLE);
  750. spin_lock_irqsave(&m->lock, flags);
  751. if (!m->pg_init_in_progress) {
  752. spin_unlock_irqrestore(&m->lock, flags);
  753. break;
  754. }
  755. spin_unlock_irqrestore(&m->lock, flags);
  756. io_schedule();
  757. }
  758. set_current_state(TASK_RUNNING);
  759. remove_wait_queue(&m->pg_init_wait, &wait);
  760. }
  761. static void flush_multipath_work(struct multipath *m)
  762. {
  763. flush_workqueue(kmpath_handlerd);
  764. multipath_wait_for_pg_init_completion(m);
  765. flush_workqueue(kmultipathd);
  766. flush_work_sync(&m->trigger_event);
  767. }
  768. static void multipath_dtr(struct dm_target *ti)
  769. {
  770. struct multipath *m = ti->private;
  771. flush_multipath_work(m);
  772. free_multipath(m);
  773. }
  774. /*
  775. * Map cloned requests
  776. */
  777. static int multipath_map(struct dm_target *ti, struct request *clone,
  778. union map_info *map_context)
  779. {
  780. int r;
  781. struct multipath *m = (struct multipath *) ti->private;
  782. if (set_mapinfo(m, map_context) < 0)
  783. /* ENOMEM, requeue */
  784. return DM_MAPIO_REQUEUE;
  785. clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
  786. r = map_io(m, clone, map_context, 0);
  787. if (r < 0 || r == DM_MAPIO_REQUEUE)
  788. clear_mapinfo(m, map_context);
  789. return r;
  790. }
  791. /*
  792. * Take a path out of use.
  793. */
  794. static int fail_path(struct pgpath *pgpath)
  795. {
  796. unsigned long flags;
  797. struct multipath *m = pgpath->pg->m;
  798. spin_lock_irqsave(&m->lock, flags);
  799. if (!pgpath->is_active)
  800. goto out;
  801. DMWARN("Failing path %s.", pgpath->path.dev->name);
  802. pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
  803. pgpath->is_active = 0;
  804. pgpath->fail_count++;
  805. m->nr_valid_paths--;
  806. if (pgpath == m->current_pgpath)
  807. m->current_pgpath = NULL;
  808. dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
  809. pgpath->path.dev->name, m->nr_valid_paths);
  810. schedule_work(&m->trigger_event);
  811. out:
  812. spin_unlock_irqrestore(&m->lock, flags);
  813. return 0;
  814. }
  815. /*
  816. * Reinstate a previously-failed path
  817. */
  818. static int reinstate_path(struct pgpath *pgpath)
  819. {
  820. int r = 0;
  821. unsigned long flags;
  822. struct multipath *m = pgpath->pg->m;
  823. spin_lock_irqsave(&m->lock, flags);
  824. if (pgpath->is_active)
  825. goto out;
  826. if (!pgpath->pg->ps.type->reinstate_path) {
  827. DMWARN("Reinstate path not supported by path selector %s",
  828. pgpath->pg->ps.type->name);
  829. r = -EINVAL;
  830. goto out;
  831. }
  832. r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
  833. if (r)
  834. goto out;
  835. pgpath->is_active = 1;
  836. if (!m->nr_valid_paths++ && m->queue_size) {
  837. m->current_pgpath = NULL;
  838. queue_work(kmultipathd, &m->process_queued_ios);
  839. } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
  840. if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
  841. m->pg_init_in_progress++;
  842. }
  843. dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
  844. pgpath->path.dev->name, m->nr_valid_paths);
  845. schedule_work(&m->trigger_event);
  846. out:
  847. spin_unlock_irqrestore(&m->lock, flags);
  848. return r;
  849. }
  850. /*
  851. * Fail or reinstate all paths that match the provided struct dm_dev.
  852. */
  853. static int action_dev(struct multipath *m, struct dm_dev *dev,
  854. action_fn action)
  855. {
  856. int r = -EINVAL;
  857. struct pgpath *pgpath;
  858. struct priority_group *pg;
  859. list_for_each_entry(pg, &m->priority_groups, list) {
  860. list_for_each_entry(pgpath, &pg->pgpaths, list) {
  861. if (pgpath->path.dev == dev)
  862. r = action(pgpath);
  863. }
  864. }
  865. return r;
  866. }
  867. /*
  868. * Temporarily try to avoid having to use the specified PG
  869. */
  870. static void bypass_pg(struct multipath *m, struct priority_group *pg,
  871. int bypassed)
  872. {
  873. unsigned long flags;
  874. spin_lock_irqsave(&m->lock, flags);
  875. pg->bypassed = bypassed;
  876. m->current_pgpath = NULL;
  877. m->current_pg = NULL;
  878. spin_unlock_irqrestore(&m->lock, flags);
  879. schedule_work(&m->trigger_event);
  880. }
  881. /*
  882. * Switch to using the specified PG from the next I/O that gets mapped
  883. */
  884. static int switch_pg_num(struct multipath *m, const char *pgstr)
  885. {
  886. struct priority_group *pg;
  887. unsigned pgnum;
  888. unsigned long flags;
  889. char dummy;
  890. if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
  891. (pgnum > m->nr_priority_groups)) {
  892. DMWARN("invalid PG number supplied to switch_pg_num");
  893. return -EINVAL;
  894. }
  895. spin_lock_irqsave(&m->lock, flags);
  896. list_for_each_entry(pg, &m->priority_groups, list) {
  897. pg->bypassed = 0;
  898. if (--pgnum)
  899. continue;
  900. m->current_pgpath = NULL;
  901. m->current_pg = NULL;
  902. m->next_pg = pg;
  903. }
  904. spin_unlock_irqrestore(&m->lock, flags);
  905. schedule_work(&m->trigger_event);
  906. return 0;
  907. }
  908. /*
  909. * Set/clear bypassed status of a PG.
  910. * PGs are numbered upwards from 1 in the order they were declared.
  911. */
  912. static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
  913. {
  914. struct priority_group *pg;
  915. unsigned pgnum;
  916. char dummy;
  917. if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
  918. (pgnum > m->nr_priority_groups)) {
  919. DMWARN("invalid PG number supplied to bypass_pg");
  920. return -EINVAL;
  921. }
  922. list_for_each_entry(pg, &m->priority_groups, list) {
  923. if (!--pgnum)
  924. break;
  925. }
  926. bypass_pg(m, pg, bypassed);
  927. return 0;
  928. }
  929. /*
  930. * Should we retry pg_init immediately?
  931. */
  932. static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
  933. {
  934. unsigned long flags;
  935. int limit_reached = 0;
  936. spin_lock_irqsave(&m->lock, flags);
  937. if (m->pg_init_count <= m->pg_init_retries)
  938. m->pg_init_required = 1;
  939. else
  940. limit_reached = 1;
  941. spin_unlock_irqrestore(&m->lock, flags);
  942. return limit_reached;
  943. }
  944. static void pg_init_done(void *data, int errors)
  945. {
  946. struct pgpath *pgpath = data;
  947. struct priority_group *pg = pgpath->pg;
  948. struct multipath *m = pg->m;
  949. unsigned long flags;
  950. unsigned delay_retry = 0;
  951. /* device or driver problems */
  952. switch (errors) {
  953. case SCSI_DH_OK:
  954. break;
  955. case SCSI_DH_NOSYS:
  956. if (!m->hw_handler_name) {
  957. errors = 0;
  958. break;
  959. }
  960. DMERR("Could not failover the device: Handler scsi_dh_%s "
  961. "Error %d.", m->hw_handler_name, errors);
  962. /*
  963. * Fail path for now, so we do not ping pong
  964. */
  965. fail_path(pgpath);
  966. break;
  967. case SCSI_DH_DEV_TEMP_BUSY:
  968. /*
  969. * Probably doing something like FW upgrade on the
  970. * controller so try the other pg.
  971. */
  972. bypass_pg(m, pg, 1);
  973. break;
  974. case SCSI_DH_RETRY:
  975. /* Wait before retrying. */
  976. delay_retry = 1;
  977. case SCSI_DH_IMM_RETRY:
  978. case SCSI_DH_RES_TEMP_UNAVAIL:
  979. if (pg_init_limit_reached(m, pgpath))
  980. fail_path(pgpath);
  981. errors = 0;
  982. break;
  983. default:
  984. /*
  985. * We probably do not want to fail the path for a device
  986. * error, but this is what the old dm did. In future
  987. * patches we can do more advanced handling.
  988. */
  989. fail_path(pgpath);
  990. }
  991. spin_lock_irqsave(&m->lock, flags);
  992. if (errors) {
  993. if (pgpath == m->current_pgpath) {
  994. DMERR("Could not failover device. Error %d.", errors);
  995. m->current_pgpath = NULL;
  996. m->current_pg = NULL;
  997. }
  998. } else if (!m->pg_init_required)
  999. pg->bypassed = 0;
  1000. if (--m->pg_init_in_progress)
  1001. /* Activations of other paths are still on going */
  1002. goto out;
  1003. if (!m->pg_init_required)
  1004. m->queue_io = 0;
  1005. m->pg_init_delay_retry = delay_retry;
  1006. queue_work(kmultipathd, &m->process_queued_ios);
  1007. /*
  1008. * Wake up any thread waiting to suspend.
  1009. */
  1010. wake_up(&m->pg_init_wait);
  1011. out:
  1012. spin_unlock_irqrestore(&m->lock, flags);
  1013. }
  1014. static void activate_path(struct work_struct *work)
  1015. {
  1016. struct pgpath *pgpath =
  1017. container_of(work, struct pgpath, activate_path.work);
  1018. scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
  1019. pg_init_done, pgpath);
  1020. }
  1021. /*
  1022. * end_io handling
  1023. */
  1024. static int do_end_io(struct multipath *m, struct request *clone,
  1025. int error, struct dm_mpath_io *mpio)
  1026. {
  1027. /*
  1028. * We don't queue any clone request inside the multipath target
  1029. * during end I/O handling, since those clone requests don't have
  1030. * bio clones. If we queue them inside the multipath target,
  1031. * we need to make bio clones, that requires memory allocation.
  1032. * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
  1033. * don't have bio clones.)
  1034. * Instead of queueing the clone request here, we queue the original
  1035. * request into dm core, which will remake a clone request and
  1036. * clone bios for it and resubmit it later.
  1037. */
  1038. int r = DM_ENDIO_REQUEUE;
  1039. unsigned long flags;
  1040. if (!error && !clone->errors)
  1041. return 0; /* I/O complete */
  1042. if (error == -EOPNOTSUPP || error == -EREMOTEIO || error == -EILSEQ)
  1043. return error;
  1044. if (mpio->pgpath)
  1045. fail_path(mpio->pgpath);
  1046. spin_lock_irqsave(&m->lock, flags);
  1047. if (!m->nr_valid_paths) {
  1048. if (!m->queue_if_no_path) {
  1049. if (!__must_push_back(m))
  1050. r = -EIO;
  1051. } else {
  1052. if (error == -EBADE)
  1053. r = error;
  1054. }
  1055. }
  1056. spin_unlock_irqrestore(&m->lock, flags);
  1057. return r;
  1058. }
  1059. static int multipath_end_io(struct dm_target *ti, struct request *clone,
  1060. int error, union map_info *map_context)
  1061. {
  1062. struct multipath *m = ti->private;
  1063. struct dm_mpath_io *mpio = map_context->ptr;
  1064. struct pgpath *pgpath = mpio->pgpath;
  1065. struct path_selector *ps;
  1066. int r;
  1067. BUG_ON(!mpio);
  1068. r = do_end_io(m, clone, error, mpio);
  1069. if (pgpath) {
  1070. ps = &pgpath->pg->ps;
  1071. if (ps->type->end_io)
  1072. ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
  1073. }
  1074. clear_mapinfo(m, map_context);
  1075. return r;
  1076. }
  1077. /*
  1078. * Suspend can't complete until all the I/O is processed so if
  1079. * the last path fails we must error any remaining I/O.
  1080. * Note that if the freeze_bdev fails while suspending, the
  1081. * queue_if_no_path state is lost - userspace should reset it.
  1082. */
  1083. static void multipath_presuspend(struct dm_target *ti)
  1084. {
  1085. struct multipath *m = (struct multipath *) ti->private;
  1086. queue_if_no_path(m, 0, 1);
  1087. }
  1088. static void multipath_postsuspend(struct dm_target *ti)
  1089. {
  1090. struct multipath *m = ti->private;
  1091. mutex_lock(&m->work_mutex);
  1092. flush_multipath_work(m);
  1093. mutex_unlock(&m->work_mutex);
  1094. }
  1095. /*
  1096. * Restore the queue_if_no_path setting.
  1097. */
  1098. static void multipath_resume(struct dm_target *ti)
  1099. {
  1100. struct multipath *m = (struct multipath *) ti->private;
  1101. unsigned long flags;
  1102. spin_lock_irqsave(&m->lock, flags);
  1103. m->queue_if_no_path = m->saved_queue_if_no_path;
  1104. spin_unlock_irqrestore(&m->lock, flags);
  1105. }
  1106. /*
  1107. * Info output has the following format:
  1108. * num_multipath_feature_args [multipath_feature_args]*
  1109. * num_handler_status_args [handler_status_args]*
  1110. * num_groups init_group_number
  1111. * [A|D|E num_ps_status_args [ps_status_args]*
  1112. * num_paths num_selector_args
  1113. * [path_dev A|F fail_count [selector_args]* ]+ ]+
  1114. *
  1115. * Table output has the following format (identical to the constructor string):
  1116. * num_feature_args [features_args]*
  1117. * num_handler_args hw_handler [hw_handler_args]*
  1118. * num_groups init_group_number
  1119. * [priority selector-name num_ps_args [ps_args]*
  1120. * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
  1121. */
  1122. static int multipath_status(struct dm_target *ti, status_type_t type,
  1123. unsigned status_flags, char *result, unsigned maxlen)
  1124. {
  1125. int sz = 0;
  1126. unsigned long flags;
  1127. struct multipath *m = (struct multipath *) ti->private;
  1128. struct priority_group *pg;
  1129. struct pgpath *p;
  1130. unsigned pg_num;
  1131. char state;
  1132. spin_lock_irqsave(&m->lock, flags);
  1133. /* Features */
  1134. if (type == STATUSTYPE_INFO)
  1135. DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
  1136. else {
  1137. DMEMIT("%u ", m->queue_if_no_path +
  1138. (m->pg_init_retries > 0) * 2 +
  1139. (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
  1140. m->retain_attached_hw_handler);
  1141. if (m->queue_if_no_path)
  1142. DMEMIT("queue_if_no_path ");
  1143. if (m->pg_init_retries)
  1144. DMEMIT("pg_init_retries %u ", m->pg_init_retries);
  1145. if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
  1146. DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
  1147. if (m->retain_attached_hw_handler)
  1148. DMEMIT("retain_attached_hw_handler ");
  1149. }
  1150. if (!m->hw_handler_name || type == STATUSTYPE_INFO)
  1151. DMEMIT("0 ");
  1152. else
  1153. DMEMIT("1 %s ", m->hw_handler_name);
  1154. DMEMIT("%u ", m->nr_priority_groups);
  1155. if (m->next_pg)
  1156. pg_num = m->next_pg->pg_num;
  1157. else if (m->current_pg)
  1158. pg_num = m->current_pg->pg_num;
  1159. else
  1160. pg_num = (m->nr_priority_groups ? 1 : 0);
  1161. DMEMIT("%u ", pg_num);
  1162. switch (type) {
  1163. case STATUSTYPE_INFO:
  1164. list_for_each_entry(pg, &m->priority_groups, list) {
  1165. if (pg->bypassed)
  1166. state = 'D'; /* Disabled */
  1167. else if (pg == m->current_pg)
  1168. state = 'A'; /* Currently Active */
  1169. else
  1170. state = 'E'; /* Enabled */
  1171. DMEMIT("%c ", state);
  1172. if (pg->ps.type->status)
  1173. sz += pg->ps.type->status(&pg->ps, NULL, type,
  1174. result + sz,
  1175. maxlen - sz);
  1176. else
  1177. DMEMIT("0 ");
  1178. DMEMIT("%u %u ", pg->nr_pgpaths,
  1179. pg->ps.type->info_args);
  1180. list_for_each_entry(p, &pg->pgpaths, list) {
  1181. DMEMIT("%s %s %u ", p->path.dev->name,
  1182. p->is_active ? "A" : "F",
  1183. p->fail_count);
  1184. if (pg->ps.type->status)
  1185. sz += pg->ps.type->status(&pg->ps,
  1186. &p->path, type, result + sz,
  1187. maxlen - sz);
  1188. }
  1189. }
  1190. break;
  1191. case STATUSTYPE_TABLE:
  1192. list_for_each_entry(pg, &m->priority_groups, list) {
  1193. DMEMIT("%s ", pg->ps.type->name);
  1194. if (pg->ps.type->status)
  1195. sz += pg->ps.type->status(&pg->ps, NULL, type,
  1196. result + sz,
  1197. maxlen - sz);
  1198. else
  1199. DMEMIT("0 ");
  1200. DMEMIT("%u %u ", pg->nr_pgpaths,
  1201. pg->ps.type->table_args);
  1202. list_for_each_entry(p, &pg->pgpaths, list) {
  1203. DMEMIT("%s ", p->path.dev->name);
  1204. if (pg->ps.type->status)
  1205. sz += pg->ps.type->status(&pg->ps,
  1206. &p->path, type, result + sz,
  1207. maxlen - sz);
  1208. }
  1209. }
  1210. break;
  1211. }
  1212. spin_unlock_irqrestore(&m->lock, flags);
  1213. return 0;
  1214. }
  1215. static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
  1216. {
  1217. int r = -EINVAL;
  1218. struct dm_dev *dev;
  1219. struct multipath *m = (struct multipath *) ti->private;
  1220. action_fn action;
  1221. mutex_lock(&m->work_mutex);
  1222. if (dm_suspended(ti)) {
  1223. r = -EBUSY;
  1224. goto out;
  1225. }
  1226. if (argc == 1) {
  1227. if (!strcasecmp(argv[0], "queue_if_no_path")) {
  1228. r = queue_if_no_path(m, 1, 0);
  1229. goto out;
  1230. } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
  1231. r = queue_if_no_path(m, 0, 0);
  1232. goto out;
  1233. }
  1234. }
  1235. if (argc != 2) {
  1236. DMWARN("Unrecognised multipath message received.");
  1237. goto out;
  1238. }
  1239. if (!strcasecmp(argv[0], "disable_group")) {
  1240. r = bypass_pg_num(m, argv[1], 1);
  1241. goto out;
  1242. } else if (!strcasecmp(argv[0], "enable_group")) {
  1243. r = bypass_pg_num(m, argv[1], 0);
  1244. goto out;
  1245. } else if (!strcasecmp(argv[0], "switch_group")) {
  1246. r = switch_pg_num(m, argv[1]);
  1247. goto out;
  1248. } else if (!strcasecmp(argv[0], "reinstate_path"))
  1249. action = reinstate_path;
  1250. else if (!strcasecmp(argv[0], "fail_path"))
  1251. action = fail_path;
  1252. else {
  1253. DMWARN("Unrecognised multipath message received.");
  1254. goto out;
  1255. }
  1256. r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
  1257. if (r) {
  1258. DMWARN("message: error getting device %s",
  1259. argv[1]);
  1260. goto out;
  1261. }
  1262. r = action_dev(m, dev, action);
  1263. dm_put_device(ti, dev);
  1264. out:
  1265. mutex_unlock(&m->work_mutex);
  1266. return r;
  1267. }
  1268. static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
  1269. unsigned long arg)
  1270. {
  1271. struct multipath *m = ti->private;
  1272. struct block_device *bdev;
  1273. fmode_t mode;
  1274. unsigned long flags;
  1275. int r;
  1276. again:
  1277. bdev = NULL;
  1278. mode = 0;
  1279. r = 0;
  1280. spin_lock_irqsave(&m->lock, flags);
  1281. if (!m->current_pgpath)
  1282. __choose_pgpath(m, 0);
  1283. if (m->current_pgpath) {
  1284. bdev = m->current_pgpath->path.dev->bdev;
  1285. mode = m->current_pgpath->path.dev->mode;
  1286. }
  1287. if (m->queue_io)
  1288. r = -EAGAIN;
  1289. else if (!bdev)
  1290. r = -EIO;
  1291. spin_unlock_irqrestore(&m->lock, flags);
  1292. /*
  1293. * Only pass ioctls through if the device sizes match exactly.
  1294. */
  1295. if (!r && ti->len != i_size_read(bdev->bd_inode) >> SECTOR_SHIFT)
  1296. r = scsi_verify_blk_ioctl(NULL, cmd);
  1297. if (r == -EAGAIN && !fatal_signal_pending(current)) {
  1298. queue_work(kmultipathd, &m->process_queued_ios);
  1299. msleep(10);
  1300. goto again;
  1301. }
  1302. return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
  1303. }
  1304. static int multipath_iterate_devices(struct dm_target *ti,
  1305. iterate_devices_callout_fn fn, void *data)
  1306. {
  1307. struct multipath *m = ti->private;
  1308. struct priority_group *pg;
  1309. struct pgpath *p;
  1310. int ret = 0;
  1311. list_for_each_entry(pg, &m->priority_groups, list) {
  1312. list_for_each_entry(p, &pg->pgpaths, list) {
  1313. ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
  1314. if (ret)
  1315. goto out;
  1316. }
  1317. }
  1318. out:
  1319. return ret;
  1320. }
  1321. static int __pgpath_busy(struct pgpath *pgpath)
  1322. {
  1323. struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
  1324. return dm_underlying_device_busy(q);
  1325. }
  1326. /*
  1327. * We return "busy", only when we can map I/Os but underlying devices
  1328. * are busy (so even if we map I/Os now, the I/Os will wait on
  1329. * the underlying queue).
  1330. * In other words, if we want to kill I/Os or queue them inside us
  1331. * due to map unavailability, we don't return "busy". Otherwise,
  1332. * dm core won't give us the I/Os and we can't do what we want.
  1333. */
  1334. static int multipath_busy(struct dm_target *ti)
  1335. {
  1336. int busy = 0, has_active = 0;
  1337. struct multipath *m = ti->private;
  1338. struct priority_group *pg;
  1339. struct pgpath *pgpath;
  1340. unsigned long flags;
  1341. spin_lock_irqsave(&m->lock, flags);
  1342. /* Guess which priority_group will be used at next mapping time */
  1343. if (unlikely(!m->current_pgpath && m->next_pg))
  1344. pg = m->next_pg;
  1345. else if (likely(m->current_pg))
  1346. pg = m->current_pg;
  1347. else
  1348. /*
  1349. * We don't know which pg will be used at next mapping time.
  1350. * We don't call __choose_pgpath() here to avoid to trigger
  1351. * pg_init just by busy checking.
  1352. * So we don't know whether underlying devices we will be using
  1353. * at next mapping time are busy or not. Just try mapping.
  1354. */
  1355. goto out;
  1356. /*
  1357. * If there is one non-busy active path at least, the path selector
  1358. * will be able to select it. So we consider such a pg as not busy.
  1359. */
  1360. busy = 1;
  1361. list_for_each_entry(pgpath, &pg->pgpaths, list)
  1362. if (pgpath->is_active) {
  1363. has_active = 1;
  1364. if (!__pgpath_busy(pgpath)) {
  1365. busy = 0;
  1366. break;
  1367. }
  1368. }
  1369. if (!has_active)
  1370. /*
  1371. * No active path in this pg, so this pg won't be used and
  1372. * the current_pg will be changed at next mapping time.
  1373. * We need to try mapping to determine it.
  1374. */
  1375. busy = 0;
  1376. out:
  1377. spin_unlock_irqrestore(&m->lock, flags);
  1378. return busy;
  1379. }
  1380. /*-----------------------------------------------------------------
  1381. * Module setup
  1382. *---------------------------------------------------------------*/
  1383. static struct target_type multipath_target = {
  1384. .name = "multipath",
  1385. .version = {1, 5, 0},
  1386. .module = THIS_MODULE,
  1387. .ctr = multipath_ctr,
  1388. .dtr = multipath_dtr,
  1389. .map_rq = multipath_map,
  1390. .rq_end_io = multipath_end_io,
  1391. .presuspend = multipath_presuspend,
  1392. .postsuspend = multipath_postsuspend,
  1393. .resume = multipath_resume,
  1394. .status = multipath_status,
  1395. .message = multipath_message,
  1396. .ioctl = multipath_ioctl,
  1397. .iterate_devices = multipath_iterate_devices,
  1398. .busy = multipath_busy,
  1399. };
  1400. static int __init dm_multipath_init(void)
  1401. {
  1402. int r;
  1403. /* allocate a slab for the dm_ios */
  1404. _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
  1405. if (!_mpio_cache)
  1406. return -ENOMEM;
  1407. r = dm_register_target(&multipath_target);
  1408. if (r < 0) {
  1409. DMERR("register failed %d", r);
  1410. kmem_cache_destroy(_mpio_cache);
  1411. return -EINVAL;
  1412. }
  1413. kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
  1414. if (!kmultipathd) {
  1415. DMERR("failed to create workqueue kmpathd");
  1416. dm_unregister_target(&multipath_target);
  1417. kmem_cache_destroy(_mpio_cache);
  1418. return -ENOMEM;
  1419. }
  1420. /*
  1421. * A separate workqueue is used to handle the device handlers
  1422. * to avoid overloading existing workqueue. Overloading the
  1423. * old workqueue would also create a bottleneck in the
  1424. * path of the storage hardware device activation.
  1425. */
  1426. kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
  1427. WQ_MEM_RECLAIM);
  1428. if (!kmpath_handlerd) {
  1429. DMERR("failed to create workqueue kmpath_handlerd");
  1430. destroy_workqueue(kmultipathd);
  1431. dm_unregister_target(&multipath_target);
  1432. kmem_cache_destroy(_mpio_cache);
  1433. return -ENOMEM;
  1434. }
  1435. DMINFO("version %u.%u.%u loaded",
  1436. multipath_target.version[0], multipath_target.version[1],
  1437. multipath_target.version[2]);
  1438. return r;
  1439. }
  1440. static void __exit dm_multipath_exit(void)
  1441. {
  1442. destroy_workqueue(kmpath_handlerd);
  1443. destroy_workqueue(kmultipathd);
  1444. dm_unregister_target(&multipath_target);
  1445. kmem_cache_destroy(_mpio_cache);
  1446. }
  1447. module_init(dm_multipath_init);
  1448. module_exit(dm_multipath_exit);
  1449. MODULE_DESCRIPTION(DM_NAME " multipath target");
  1450. MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
  1451. MODULE_LICENSE("GPL");