dm-mpath.c 40 KB

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