dm-mpath.c 41 KB

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