dm-mpath.c 39 KB

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