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