dm-mpath.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794
  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_ATOMIC);
  346. if (IS_ERR(*__clone)) {
  347. /* ENOMEM, requeue */
  348. clear_mapinfo(m, map_context);
  349. return r;
  350. }
  351. (*__clone)->bio = (*__clone)->biotail = NULL;
  352. (*__clone)->rq_disk = bdev->bd_disk;
  353. (*__clone)->cmd_flags |= REQ_FAILFAST_TRANSPORT;
  354. }
  355. if (pgpath->pg->ps.type->start_io)
  356. pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
  357. &pgpath->path,
  358. nr_bytes);
  359. return DM_MAPIO_REMAPPED;
  360. out_unlock:
  361. spin_unlock_irq(&m->lock);
  362. return r;
  363. }
  364. static int multipath_map(struct dm_target *ti, struct request *clone,
  365. union map_info *map_context)
  366. {
  367. return __multipath_map(ti, clone, map_context, NULL, NULL);
  368. }
  369. static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
  370. union map_info *map_context,
  371. struct request **clone)
  372. {
  373. return __multipath_map(ti, NULL, map_context, rq, clone);
  374. }
  375. static void multipath_release_clone(struct request *clone)
  376. {
  377. blk_put_request(clone);
  378. }
  379. /*
  380. * If we run out of usable paths, should we queue I/O or error it?
  381. */
  382. static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
  383. unsigned save_old_value)
  384. {
  385. unsigned long flags;
  386. spin_lock_irqsave(&m->lock, flags);
  387. if (save_old_value)
  388. m->saved_queue_if_no_path = m->queue_if_no_path;
  389. else
  390. m->saved_queue_if_no_path = queue_if_no_path;
  391. m->queue_if_no_path = queue_if_no_path;
  392. spin_unlock_irqrestore(&m->lock, flags);
  393. if (!queue_if_no_path)
  394. dm_table_run_md_queue_async(m->ti->table);
  395. return 0;
  396. }
  397. /*
  398. * An event is triggered whenever a path is taken out of use.
  399. * Includes path failure and PG bypass.
  400. */
  401. static void trigger_event(struct work_struct *work)
  402. {
  403. struct multipath *m =
  404. container_of(work, struct multipath, trigger_event);
  405. dm_table_event(m->ti->table);
  406. }
  407. /*-----------------------------------------------------------------
  408. * Constructor/argument parsing:
  409. * <#multipath feature args> [<arg>]*
  410. * <#hw_handler args> [hw_handler [<arg>]*]
  411. * <#priority groups>
  412. * <initial priority group>
  413. * [<selector> <#selector args> [<arg>]*
  414. * <#paths> <#per-path selector args>
  415. * [<path> [<arg>]* ]+ ]+
  416. *---------------------------------------------------------------*/
  417. static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
  418. struct dm_target *ti)
  419. {
  420. int r;
  421. struct path_selector_type *pst;
  422. unsigned ps_argc;
  423. static struct dm_arg _args[] = {
  424. {0, 1024, "invalid number of path selector args"},
  425. };
  426. pst = dm_get_path_selector(dm_shift_arg(as));
  427. if (!pst) {
  428. ti->error = "unknown path selector type";
  429. return -EINVAL;
  430. }
  431. r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
  432. if (r) {
  433. dm_put_path_selector(pst);
  434. return -EINVAL;
  435. }
  436. r = pst->create(&pg->ps, ps_argc, as->argv);
  437. if (r) {
  438. dm_put_path_selector(pst);
  439. ti->error = "path selector constructor failed";
  440. return r;
  441. }
  442. pg->ps.type = pst;
  443. dm_consume_args(as, ps_argc);
  444. return 0;
  445. }
  446. static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
  447. struct dm_target *ti)
  448. {
  449. int r;
  450. struct pgpath *p;
  451. struct multipath *m = ti->private;
  452. struct request_queue *q = NULL;
  453. const char *attached_handler_name;
  454. /* we need at least a path arg */
  455. if (as->argc < 1) {
  456. ti->error = "no device given";
  457. return ERR_PTR(-EINVAL);
  458. }
  459. p = alloc_pgpath();
  460. if (!p)
  461. return ERR_PTR(-ENOMEM);
  462. r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
  463. &p->path.dev);
  464. if (r) {
  465. ti->error = "error getting device";
  466. goto bad;
  467. }
  468. if (m->retain_attached_hw_handler || m->hw_handler_name)
  469. q = bdev_get_queue(p->path.dev->bdev);
  470. if (m->retain_attached_hw_handler) {
  471. attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
  472. if (attached_handler_name) {
  473. /*
  474. * Reset hw_handler_name to match the attached handler
  475. * and clear any hw_handler_params associated with the
  476. * ignored handler.
  477. *
  478. * NB. This modifies the table line to show the actual
  479. * handler instead of the original table passed in.
  480. */
  481. kfree(m->hw_handler_name);
  482. m->hw_handler_name = attached_handler_name;
  483. kfree(m->hw_handler_params);
  484. m->hw_handler_params = NULL;
  485. }
  486. }
  487. if (m->hw_handler_name) {
  488. /*
  489. * Increments scsi_dh reference, even when using an
  490. * already-attached handler.
  491. */
  492. r = scsi_dh_attach(q, m->hw_handler_name);
  493. if (r == -EBUSY) {
  494. /*
  495. * Already attached to different hw_handler:
  496. * try to reattach with correct one.
  497. */
  498. scsi_dh_detach(q);
  499. r = scsi_dh_attach(q, m->hw_handler_name);
  500. }
  501. if (r < 0) {
  502. ti->error = "error attaching hardware handler";
  503. dm_put_device(ti, p->path.dev);
  504. goto bad;
  505. }
  506. if (m->hw_handler_params) {
  507. r = scsi_dh_set_params(q, m->hw_handler_params);
  508. if (r < 0) {
  509. ti->error = "unable to set hardware "
  510. "handler parameters";
  511. scsi_dh_detach(q);
  512. dm_put_device(ti, p->path.dev);
  513. goto bad;
  514. }
  515. }
  516. }
  517. r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
  518. if (r) {
  519. dm_put_device(ti, p->path.dev);
  520. goto bad;
  521. }
  522. return p;
  523. bad:
  524. free_pgpath(p);
  525. return ERR_PTR(r);
  526. }
  527. static struct priority_group *parse_priority_group(struct dm_arg_set *as,
  528. struct multipath *m)
  529. {
  530. static struct dm_arg _args[] = {
  531. {1, 1024, "invalid number of paths"},
  532. {0, 1024, "invalid number of selector args"}
  533. };
  534. int r;
  535. unsigned i, nr_selector_args, nr_args;
  536. struct priority_group *pg;
  537. struct dm_target *ti = m->ti;
  538. if (as->argc < 2) {
  539. as->argc = 0;
  540. ti->error = "not enough priority group arguments";
  541. return ERR_PTR(-EINVAL);
  542. }
  543. pg = alloc_priority_group();
  544. if (!pg) {
  545. ti->error = "couldn't allocate priority group";
  546. return ERR_PTR(-ENOMEM);
  547. }
  548. pg->m = m;
  549. r = parse_path_selector(as, pg, ti);
  550. if (r)
  551. goto bad;
  552. /*
  553. * read the paths
  554. */
  555. r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
  556. if (r)
  557. goto bad;
  558. r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
  559. if (r)
  560. goto bad;
  561. nr_args = 1 + nr_selector_args;
  562. for (i = 0; i < pg->nr_pgpaths; i++) {
  563. struct pgpath *pgpath;
  564. struct dm_arg_set path_args;
  565. if (as->argc < nr_args) {
  566. ti->error = "not enough path parameters";
  567. r = -EINVAL;
  568. goto bad;
  569. }
  570. path_args.argc = nr_args;
  571. path_args.argv = as->argv;
  572. pgpath = parse_path(&path_args, &pg->ps, ti);
  573. if (IS_ERR(pgpath)) {
  574. r = PTR_ERR(pgpath);
  575. goto bad;
  576. }
  577. pgpath->pg = pg;
  578. list_add_tail(&pgpath->list, &pg->pgpaths);
  579. dm_consume_args(as, nr_args);
  580. }
  581. return pg;
  582. bad:
  583. free_priority_group(pg, ti);
  584. return ERR_PTR(r);
  585. }
  586. static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
  587. {
  588. unsigned hw_argc;
  589. int ret;
  590. struct dm_target *ti = m->ti;
  591. static struct dm_arg _args[] = {
  592. {0, 1024, "invalid number of hardware handler args"},
  593. };
  594. if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
  595. return -EINVAL;
  596. if (!hw_argc)
  597. return 0;
  598. m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
  599. if (!try_then_request_module(scsi_dh_handler_exist(m->hw_handler_name),
  600. "scsi_dh_%s", m->hw_handler_name)) {
  601. ti->error = "unknown hardware handler type";
  602. ret = -EINVAL;
  603. goto fail;
  604. }
  605. if (hw_argc > 1) {
  606. char *p;
  607. int i, j, len = 4;
  608. for (i = 0; i <= hw_argc - 2; i++)
  609. len += strlen(as->argv[i]) + 1;
  610. p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
  611. if (!p) {
  612. ti->error = "memory allocation failed";
  613. ret = -ENOMEM;
  614. goto fail;
  615. }
  616. j = sprintf(p, "%d", hw_argc - 1);
  617. for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
  618. j = sprintf(p, "%s", as->argv[i]);
  619. }
  620. dm_consume_args(as, hw_argc - 1);
  621. return 0;
  622. fail:
  623. kfree(m->hw_handler_name);
  624. m->hw_handler_name = NULL;
  625. return ret;
  626. }
  627. static int parse_features(struct dm_arg_set *as, struct multipath *m)
  628. {
  629. int r;
  630. unsigned argc;
  631. struct dm_target *ti = m->ti;
  632. const char *arg_name;
  633. static struct dm_arg _args[] = {
  634. {0, 6, "invalid number of feature args"},
  635. {1, 50, "pg_init_retries must be between 1 and 50"},
  636. {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
  637. };
  638. r = dm_read_arg_group(_args, as, &argc, &ti->error);
  639. if (r)
  640. return -EINVAL;
  641. if (!argc)
  642. return 0;
  643. do {
  644. arg_name = dm_shift_arg(as);
  645. argc--;
  646. if (!strcasecmp(arg_name, "queue_if_no_path")) {
  647. r = queue_if_no_path(m, 1, 0);
  648. continue;
  649. }
  650. if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
  651. m->retain_attached_hw_handler = 1;
  652. continue;
  653. }
  654. if (!strcasecmp(arg_name, "pg_init_retries") &&
  655. (argc >= 1)) {
  656. r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
  657. argc--;
  658. continue;
  659. }
  660. if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
  661. (argc >= 1)) {
  662. r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
  663. argc--;
  664. continue;
  665. }
  666. ti->error = "Unrecognised multipath feature request";
  667. r = -EINVAL;
  668. } while (argc && !r);
  669. return r;
  670. }
  671. static int multipath_ctr(struct dm_target *ti, unsigned int argc,
  672. char **argv)
  673. {
  674. /* target arguments */
  675. static struct dm_arg _args[] = {
  676. {0, 1024, "invalid number of priority groups"},
  677. {0, 1024, "invalid initial priority group number"},
  678. };
  679. int r;
  680. struct multipath *m;
  681. struct dm_arg_set as;
  682. unsigned pg_count = 0;
  683. unsigned next_pg_num;
  684. as.argc = argc;
  685. as.argv = argv;
  686. m = alloc_multipath(ti);
  687. if (!m) {
  688. ti->error = "can't allocate multipath";
  689. return -EINVAL;
  690. }
  691. r = parse_features(&as, m);
  692. if (r)
  693. goto bad;
  694. r = parse_hw_handler(&as, m);
  695. if (r)
  696. goto bad;
  697. r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
  698. if (r)
  699. goto bad;
  700. r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
  701. if (r)
  702. goto bad;
  703. if ((!m->nr_priority_groups && next_pg_num) ||
  704. (m->nr_priority_groups && !next_pg_num)) {
  705. ti->error = "invalid initial priority group";
  706. r = -EINVAL;
  707. goto bad;
  708. }
  709. /* parse the priority groups */
  710. while (as.argc) {
  711. struct priority_group *pg;
  712. pg = parse_priority_group(&as, m);
  713. if (IS_ERR(pg)) {
  714. r = PTR_ERR(pg);
  715. goto bad;
  716. }
  717. m->nr_valid_paths += pg->nr_pgpaths;
  718. list_add_tail(&pg->list, &m->priority_groups);
  719. pg_count++;
  720. pg->pg_num = pg_count;
  721. if (!--next_pg_num)
  722. m->next_pg = pg;
  723. }
  724. if (pg_count != m->nr_priority_groups) {
  725. ti->error = "priority group count mismatch";
  726. r = -EINVAL;
  727. goto bad;
  728. }
  729. ti->num_flush_bios = 1;
  730. ti->num_discard_bios = 1;
  731. ti->num_write_same_bios = 1;
  732. return 0;
  733. bad:
  734. free_multipath(m);
  735. return r;
  736. }
  737. static void multipath_wait_for_pg_init_completion(struct multipath *m)
  738. {
  739. DECLARE_WAITQUEUE(wait, current);
  740. unsigned long flags;
  741. add_wait_queue(&m->pg_init_wait, &wait);
  742. while (1) {
  743. set_current_state(TASK_UNINTERRUPTIBLE);
  744. spin_lock_irqsave(&m->lock, flags);
  745. if (!m->pg_init_in_progress) {
  746. spin_unlock_irqrestore(&m->lock, flags);
  747. break;
  748. }
  749. spin_unlock_irqrestore(&m->lock, flags);
  750. io_schedule();
  751. }
  752. set_current_state(TASK_RUNNING);
  753. remove_wait_queue(&m->pg_init_wait, &wait);
  754. }
  755. static void flush_multipath_work(struct multipath *m)
  756. {
  757. unsigned long flags;
  758. spin_lock_irqsave(&m->lock, flags);
  759. m->pg_init_disabled = 1;
  760. spin_unlock_irqrestore(&m->lock, flags);
  761. flush_workqueue(kmpath_handlerd);
  762. multipath_wait_for_pg_init_completion(m);
  763. flush_workqueue(kmultipathd);
  764. flush_work(&m->trigger_event);
  765. spin_lock_irqsave(&m->lock, flags);
  766. m->pg_init_disabled = 0;
  767. spin_unlock_irqrestore(&m->lock, flags);
  768. }
  769. static void multipath_dtr(struct dm_target *ti)
  770. {
  771. struct multipath *m = ti->private;
  772. flush_multipath_work(m);
  773. free_multipath(m);
  774. }
  775. /*
  776. * Take a path out of use.
  777. */
  778. static int fail_path(struct pgpath *pgpath)
  779. {
  780. unsigned long flags;
  781. struct multipath *m = pgpath->pg->m;
  782. spin_lock_irqsave(&m->lock, flags);
  783. if (!pgpath->is_active)
  784. goto out;
  785. DMWARN("Failing path %s.", pgpath->path.dev->name);
  786. pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
  787. pgpath->is_active = 0;
  788. pgpath->fail_count++;
  789. m->nr_valid_paths--;
  790. if (pgpath == m->current_pgpath)
  791. m->current_pgpath = NULL;
  792. dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
  793. pgpath->path.dev->name, m->nr_valid_paths);
  794. schedule_work(&m->trigger_event);
  795. out:
  796. spin_unlock_irqrestore(&m->lock, flags);
  797. return 0;
  798. }
  799. /*
  800. * Reinstate a previously-failed path
  801. */
  802. static int reinstate_path(struct pgpath *pgpath)
  803. {
  804. int r = 0, run_queue = 0;
  805. unsigned long flags;
  806. struct multipath *m = pgpath->pg->m;
  807. spin_lock_irqsave(&m->lock, flags);
  808. if (pgpath->is_active)
  809. goto out;
  810. if (!pgpath->pg->ps.type->reinstate_path) {
  811. DMWARN("Reinstate path not supported by path selector %s",
  812. pgpath->pg->ps.type->name);
  813. r = -EINVAL;
  814. goto out;
  815. }
  816. r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
  817. if (r)
  818. goto out;
  819. pgpath->is_active = 1;
  820. if (!m->nr_valid_paths++) {
  821. m->current_pgpath = NULL;
  822. run_queue = 1;
  823. } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
  824. if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
  825. m->pg_init_in_progress++;
  826. }
  827. dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
  828. pgpath->path.dev->name, m->nr_valid_paths);
  829. schedule_work(&m->trigger_event);
  830. out:
  831. spin_unlock_irqrestore(&m->lock, flags);
  832. if (run_queue)
  833. dm_table_run_md_queue_async(m->ti->table);
  834. return r;
  835. }
  836. /*
  837. * Fail or reinstate all paths that match the provided struct dm_dev.
  838. */
  839. static int action_dev(struct multipath *m, struct dm_dev *dev,
  840. action_fn action)
  841. {
  842. int r = -EINVAL;
  843. struct pgpath *pgpath;
  844. struct priority_group *pg;
  845. list_for_each_entry(pg, &m->priority_groups, list) {
  846. list_for_each_entry(pgpath, &pg->pgpaths, list) {
  847. if (pgpath->path.dev == dev)
  848. r = action(pgpath);
  849. }
  850. }
  851. return r;
  852. }
  853. /*
  854. * Temporarily try to avoid having to use the specified PG
  855. */
  856. static void bypass_pg(struct multipath *m, struct priority_group *pg,
  857. int bypassed)
  858. {
  859. unsigned long flags;
  860. spin_lock_irqsave(&m->lock, flags);
  861. pg->bypassed = bypassed;
  862. m->current_pgpath = NULL;
  863. m->current_pg = NULL;
  864. spin_unlock_irqrestore(&m->lock, flags);
  865. schedule_work(&m->trigger_event);
  866. }
  867. /*
  868. * Switch to using the specified PG from the next I/O that gets mapped
  869. */
  870. static int switch_pg_num(struct multipath *m, const char *pgstr)
  871. {
  872. struct priority_group *pg;
  873. unsigned pgnum;
  874. unsigned long flags;
  875. char dummy;
  876. if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
  877. (pgnum > m->nr_priority_groups)) {
  878. DMWARN("invalid PG number supplied to switch_pg_num");
  879. return -EINVAL;
  880. }
  881. spin_lock_irqsave(&m->lock, flags);
  882. list_for_each_entry(pg, &m->priority_groups, list) {
  883. pg->bypassed = 0;
  884. if (--pgnum)
  885. continue;
  886. m->current_pgpath = NULL;
  887. m->current_pg = NULL;
  888. m->next_pg = pg;
  889. }
  890. spin_unlock_irqrestore(&m->lock, flags);
  891. schedule_work(&m->trigger_event);
  892. return 0;
  893. }
  894. /*
  895. * Set/clear bypassed status of a PG.
  896. * PGs are numbered upwards from 1 in the order they were declared.
  897. */
  898. static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
  899. {
  900. struct priority_group *pg;
  901. unsigned pgnum;
  902. char dummy;
  903. if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
  904. (pgnum > m->nr_priority_groups)) {
  905. DMWARN("invalid PG number supplied to bypass_pg");
  906. return -EINVAL;
  907. }
  908. list_for_each_entry(pg, &m->priority_groups, list) {
  909. if (!--pgnum)
  910. break;
  911. }
  912. bypass_pg(m, pg, bypassed);
  913. return 0;
  914. }
  915. /*
  916. * Should we retry pg_init immediately?
  917. */
  918. static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
  919. {
  920. unsigned long flags;
  921. int limit_reached = 0;
  922. spin_lock_irqsave(&m->lock, flags);
  923. if (m->pg_init_count <= m->pg_init_retries && !m->pg_init_disabled)
  924. m->pg_init_required = 1;
  925. else
  926. limit_reached = 1;
  927. spin_unlock_irqrestore(&m->lock, flags);
  928. return limit_reached;
  929. }
  930. static void pg_init_done(void *data, int errors)
  931. {
  932. struct pgpath *pgpath = data;
  933. struct priority_group *pg = pgpath->pg;
  934. struct multipath *m = pg->m;
  935. unsigned long flags;
  936. unsigned delay_retry = 0;
  937. /* device or driver problems */
  938. switch (errors) {
  939. case SCSI_DH_OK:
  940. break;
  941. case SCSI_DH_NOSYS:
  942. if (!m->hw_handler_name) {
  943. errors = 0;
  944. break;
  945. }
  946. DMERR("Could not failover the device: Handler scsi_dh_%s "
  947. "Error %d.", m->hw_handler_name, errors);
  948. /*
  949. * Fail path for now, so we do not ping pong
  950. */
  951. fail_path(pgpath);
  952. break;
  953. case SCSI_DH_DEV_TEMP_BUSY:
  954. /*
  955. * Probably doing something like FW upgrade on the
  956. * controller so try the other pg.
  957. */
  958. bypass_pg(m, pg, 1);
  959. break;
  960. case SCSI_DH_RETRY:
  961. /* Wait before retrying. */
  962. delay_retry = 1;
  963. case SCSI_DH_IMM_RETRY:
  964. case SCSI_DH_RES_TEMP_UNAVAIL:
  965. if (pg_init_limit_reached(m, pgpath))
  966. fail_path(pgpath);
  967. errors = 0;
  968. break;
  969. default:
  970. /*
  971. * We probably do not want to fail the path for a device
  972. * error, but this is what the old dm did. In future
  973. * patches we can do more advanced handling.
  974. */
  975. fail_path(pgpath);
  976. }
  977. spin_lock_irqsave(&m->lock, flags);
  978. if (errors) {
  979. if (pgpath == m->current_pgpath) {
  980. DMERR("Could not failover device. Error %d.", errors);
  981. m->current_pgpath = NULL;
  982. m->current_pg = NULL;
  983. }
  984. } else if (!m->pg_init_required)
  985. pg->bypassed = 0;
  986. if (--m->pg_init_in_progress)
  987. /* Activations of other paths are still on going */
  988. goto out;
  989. if (m->pg_init_required) {
  990. m->pg_init_delay_retry = delay_retry;
  991. if (__pg_init_all_paths(m))
  992. goto out;
  993. }
  994. m->queue_io = 0;
  995. /*
  996. * Wake up any thread waiting to suspend.
  997. */
  998. wake_up(&m->pg_init_wait);
  999. out:
  1000. spin_unlock_irqrestore(&m->lock, flags);
  1001. }
  1002. static void activate_path(struct work_struct *work)
  1003. {
  1004. struct pgpath *pgpath =
  1005. container_of(work, struct pgpath, activate_path.work);
  1006. if (pgpath->is_active)
  1007. scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
  1008. pg_init_done, pgpath);
  1009. else
  1010. pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
  1011. }
  1012. static int noretry_error(int error)
  1013. {
  1014. switch (error) {
  1015. case -EOPNOTSUPP:
  1016. case -EREMOTEIO:
  1017. case -EILSEQ:
  1018. case -ENODATA:
  1019. case -ENOSPC:
  1020. return 1;
  1021. }
  1022. /* Anything else could be a path failure, so should be retried */
  1023. return 0;
  1024. }
  1025. /*
  1026. * end_io handling
  1027. */
  1028. static int do_end_io(struct multipath *m, struct request *clone,
  1029. int error, struct dm_mpath_io *mpio)
  1030. {
  1031. /*
  1032. * We don't queue any clone request inside the multipath target
  1033. * during end I/O handling, since those clone requests don't have
  1034. * bio clones. If we queue them inside the multipath target,
  1035. * we need to make bio clones, that requires memory allocation.
  1036. * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
  1037. * don't have bio clones.)
  1038. * Instead of queueing the clone request here, we queue the original
  1039. * request into dm core, which will remake a clone request and
  1040. * clone bios for it and resubmit it later.
  1041. */
  1042. int r = DM_ENDIO_REQUEUE;
  1043. unsigned long flags;
  1044. if (!error && !clone->errors)
  1045. return 0; /* I/O complete */
  1046. if (noretry_error(error))
  1047. return error;
  1048. if (mpio->pgpath)
  1049. fail_path(mpio->pgpath);
  1050. spin_lock_irqsave(&m->lock, flags);
  1051. if (!m->nr_valid_paths) {
  1052. if (!m->queue_if_no_path) {
  1053. if (!__must_push_back(m))
  1054. r = -EIO;
  1055. } else {
  1056. if (error == -EBADE)
  1057. r = error;
  1058. }
  1059. }
  1060. spin_unlock_irqrestore(&m->lock, flags);
  1061. return r;
  1062. }
  1063. static int multipath_end_io(struct dm_target *ti, struct request *clone,
  1064. int error, union map_info *map_context)
  1065. {
  1066. struct multipath *m = ti->private;
  1067. struct dm_mpath_io *mpio = map_context->ptr;
  1068. struct pgpath *pgpath;
  1069. struct path_selector *ps;
  1070. int r;
  1071. BUG_ON(!mpio);
  1072. r = do_end_io(m, clone, error, mpio);
  1073. pgpath = mpio->pgpath;
  1074. if (pgpath) {
  1075. ps = &pgpath->pg->ps;
  1076. if (ps->type->end_io)
  1077. ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
  1078. }
  1079. clear_mapinfo(m, map_context);
  1080. return r;
  1081. }
  1082. /*
  1083. * Suspend can't complete until all the I/O is processed so if
  1084. * the last path fails we must error any remaining I/O.
  1085. * Note that if the freeze_bdev fails while suspending, the
  1086. * queue_if_no_path state is lost - userspace should reset it.
  1087. */
  1088. static void multipath_presuspend(struct dm_target *ti)
  1089. {
  1090. struct multipath *m = (struct multipath *) ti->private;
  1091. queue_if_no_path(m, 0, 1);
  1092. }
  1093. static void multipath_postsuspend(struct dm_target *ti)
  1094. {
  1095. struct multipath *m = ti->private;
  1096. mutex_lock(&m->work_mutex);
  1097. flush_multipath_work(m);
  1098. mutex_unlock(&m->work_mutex);
  1099. }
  1100. /*
  1101. * Restore the queue_if_no_path setting.
  1102. */
  1103. static void multipath_resume(struct dm_target *ti)
  1104. {
  1105. struct multipath *m = (struct multipath *) ti->private;
  1106. unsigned long flags;
  1107. spin_lock_irqsave(&m->lock, flags);
  1108. m->queue_if_no_path = m->saved_queue_if_no_path;
  1109. spin_unlock_irqrestore(&m->lock, flags);
  1110. }
  1111. /*
  1112. * Info output has the following format:
  1113. * num_multipath_feature_args [multipath_feature_args]*
  1114. * num_handler_status_args [handler_status_args]*
  1115. * num_groups init_group_number
  1116. * [A|D|E num_ps_status_args [ps_status_args]*
  1117. * num_paths num_selector_args
  1118. * [path_dev A|F fail_count [selector_args]* ]+ ]+
  1119. *
  1120. * Table output has the following format (identical to the constructor string):
  1121. * num_feature_args [features_args]*
  1122. * num_handler_args hw_handler [hw_handler_args]*
  1123. * num_groups init_group_number
  1124. * [priority selector-name num_ps_args [ps_args]*
  1125. * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
  1126. */
  1127. static void multipath_status(struct dm_target *ti, status_type_t type,
  1128. unsigned status_flags, char *result, unsigned maxlen)
  1129. {
  1130. int sz = 0;
  1131. unsigned long flags;
  1132. struct multipath *m = (struct multipath *) ti->private;
  1133. struct priority_group *pg;
  1134. struct pgpath *p;
  1135. unsigned pg_num;
  1136. char state;
  1137. spin_lock_irqsave(&m->lock, flags);
  1138. /* Features */
  1139. if (type == STATUSTYPE_INFO)
  1140. DMEMIT("2 %u %u ", m->queue_io, m->pg_init_count);
  1141. else {
  1142. DMEMIT("%u ", m->queue_if_no_path +
  1143. (m->pg_init_retries > 0) * 2 +
  1144. (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
  1145. m->retain_attached_hw_handler);
  1146. if (m->queue_if_no_path)
  1147. DMEMIT("queue_if_no_path ");
  1148. if (m->pg_init_retries)
  1149. DMEMIT("pg_init_retries %u ", m->pg_init_retries);
  1150. if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
  1151. DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
  1152. if (m->retain_attached_hw_handler)
  1153. DMEMIT("retain_attached_hw_handler ");
  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. }
  1219. static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
  1220. {
  1221. int r = -EINVAL;
  1222. struct dm_dev *dev;
  1223. struct multipath *m = (struct multipath *) ti->private;
  1224. action_fn action;
  1225. mutex_lock(&m->work_mutex);
  1226. if (dm_suspended(ti)) {
  1227. r = -EBUSY;
  1228. goto out;
  1229. }
  1230. if (argc == 1) {
  1231. if (!strcasecmp(argv[0], "queue_if_no_path")) {
  1232. r = queue_if_no_path(m, 1, 0);
  1233. goto out;
  1234. } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
  1235. r = queue_if_no_path(m, 0, 0);
  1236. goto out;
  1237. }
  1238. }
  1239. if (argc != 2) {
  1240. DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
  1241. goto out;
  1242. }
  1243. if (!strcasecmp(argv[0], "disable_group")) {
  1244. r = bypass_pg_num(m, argv[1], 1);
  1245. goto out;
  1246. } else if (!strcasecmp(argv[0], "enable_group")) {
  1247. r = bypass_pg_num(m, argv[1], 0);
  1248. goto out;
  1249. } else if (!strcasecmp(argv[0], "switch_group")) {
  1250. r = switch_pg_num(m, argv[1]);
  1251. goto out;
  1252. } else if (!strcasecmp(argv[0], "reinstate_path"))
  1253. action = reinstate_path;
  1254. else if (!strcasecmp(argv[0], "fail_path"))
  1255. action = fail_path;
  1256. else {
  1257. DMWARN("Unrecognised multipath message received: %s", argv[0]);
  1258. goto out;
  1259. }
  1260. r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
  1261. if (r) {
  1262. DMWARN("message: error getting device %s",
  1263. argv[1]);
  1264. goto out;
  1265. }
  1266. r = action_dev(m, dev, action);
  1267. dm_put_device(ti, dev);
  1268. out:
  1269. mutex_unlock(&m->work_mutex);
  1270. return r;
  1271. }
  1272. static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
  1273. unsigned long arg)
  1274. {
  1275. struct multipath *m = ti->private;
  1276. struct pgpath *pgpath;
  1277. struct block_device *bdev;
  1278. fmode_t mode;
  1279. unsigned long flags;
  1280. int r;
  1281. bdev = NULL;
  1282. mode = 0;
  1283. r = 0;
  1284. spin_lock_irqsave(&m->lock, flags);
  1285. if (!m->current_pgpath)
  1286. __choose_pgpath(m, 0);
  1287. pgpath = m->current_pgpath;
  1288. if (pgpath) {
  1289. bdev = pgpath->path.dev->bdev;
  1290. mode = pgpath->path.dev->mode;
  1291. }
  1292. if ((pgpath && m->queue_io) || (!pgpath && m->queue_if_no_path))
  1293. r = -ENOTCONN;
  1294. else if (!bdev)
  1295. r = -EIO;
  1296. spin_unlock_irqrestore(&m->lock, flags);
  1297. /*
  1298. * Only pass ioctls through if the device sizes match exactly.
  1299. */
  1300. if (!bdev || ti->len != i_size_read(bdev->bd_inode) >> SECTOR_SHIFT) {
  1301. int err = scsi_verify_blk_ioctl(NULL, cmd);
  1302. if (err)
  1303. r = err;
  1304. }
  1305. if (r == -ENOTCONN && !fatal_signal_pending(current)) {
  1306. spin_lock_irqsave(&m->lock, flags);
  1307. if (!m->current_pg) {
  1308. /* Path status changed, redo selection */
  1309. __choose_pgpath(m, 0);
  1310. }
  1311. if (m->pg_init_required)
  1312. __pg_init_all_paths(m);
  1313. spin_unlock_irqrestore(&m->lock, flags);
  1314. dm_table_run_md_queue_async(m->ti->table);
  1315. }
  1316. return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
  1317. }
  1318. static int multipath_iterate_devices(struct dm_target *ti,
  1319. iterate_devices_callout_fn fn, void *data)
  1320. {
  1321. struct multipath *m = ti->private;
  1322. struct priority_group *pg;
  1323. struct pgpath *p;
  1324. int ret = 0;
  1325. list_for_each_entry(pg, &m->priority_groups, list) {
  1326. list_for_each_entry(p, &pg->pgpaths, list) {
  1327. ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
  1328. if (ret)
  1329. goto out;
  1330. }
  1331. }
  1332. out:
  1333. return ret;
  1334. }
  1335. static int __pgpath_busy(struct pgpath *pgpath)
  1336. {
  1337. struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
  1338. return blk_lld_busy(q);
  1339. }
  1340. /*
  1341. * We return "busy", only when we can map I/Os but underlying devices
  1342. * are busy (so even if we map I/Os now, the I/Os will wait on
  1343. * the underlying queue).
  1344. * In other words, if we want to kill I/Os or queue them inside us
  1345. * due to map unavailability, we don't return "busy". Otherwise,
  1346. * dm core won't give us the I/Os and we can't do what we want.
  1347. */
  1348. static int multipath_busy(struct dm_target *ti)
  1349. {
  1350. int busy = 0, has_active = 0;
  1351. struct multipath *m = ti->private;
  1352. struct priority_group *pg;
  1353. struct pgpath *pgpath;
  1354. unsigned long flags;
  1355. spin_lock_irqsave(&m->lock, flags);
  1356. /* pg_init in progress or no paths available */
  1357. if (m->pg_init_in_progress ||
  1358. (!m->nr_valid_paths && m->queue_if_no_path)) {
  1359. busy = 1;
  1360. goto out;
  1361. }
  1362. /* Guess which priority_group will be used at next mapping time */
  1363. if (unlikely(!m->current_pgpath && m->next_pg))
  1364. pg = m->next_pg;
  1365. else if (likely(m->current_pg))
  1366. pg = m->current_pg;
  1367. else
  1368. /*
  1369. * We don't know which pg will be used at next mapping time.
  1370. * We don't call __choose_pgpath() here to avoid to trigger
  1371. * pg_init just by busy checking.
  1372. * So we don't know whether underlying devices we will be using
  1373. * at next mapping time are busy or not. Just try mapping.
  1374. */
  1375. goto out;
  1376. /*
  1377. * If there is one non-busy active path at least, the path selector
  1378. * will be able to select it. So we consider such a pg as not busy.
  1379. */
  1380. busy = 1;
  1381. list_for_each_entry(pgpath, &pg->pgpaths, list)
  1382. if (pgpath->is_active) {
  1383. has_active = 1;
  1384. if (!__pgpath_busy(pgpath)) {
  1385. busy = 0;
  1386. break;
  1387. }
  1388. }
  1389. if (!has_active)
  1390. /*
  1391. * No active path in this pg, so this pg won't be used and
  1392. * the current_pg will be changed at next mapping time.
  1393. * We need to try mapping to determine it.
  1394. */
  1395. busy = 0;
  1396. out:
  1397. spin_unlock_irqrestore(&m->lock, flags);
  1398. return busy;
  1399. }
  1400. /*-----------------------------------------------------------------
  1401. * Module setup
  1402. *---------------------------------------------------------------*/
  1403. static struct target_type multipath_target = {
  1404. .name = "multipath",
  1405. .version = {1, 9, 0},
  1406. .module = THIS_MODULE,
  1407. .ctr = multipath_ctr,
  1408. .dtr = multipath_dtr,
  1409. .map_rq = multipath_map,
  1410. .clone_and_map_rq = multipath_clone_and_map,
  1411. .release_clone_rq = multipath_release_clone,
  1412. .rq_end_io = multipath_end_io,
  1413. .presuspend = multipath_presuspend,
  1414. .postsuspend = multipath_postsuspend,
  1415. .resume = multipath_resume,
  1416. .status = multipath_status,
  1417. .message = multipath_message,
  1418. .ioctl = multipath_ioctl,
  1419. .iterate_devices = multipath_iterate_devices,
  1420. .busy = multipath_busy,
  1421. };
  1422. static int __init dm_multipath_init(void)
  1423. {
  1424. int r;
  1425. /* allocate a slab for the dm_ios */
  1426. _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
  1427. if (!_mpio_cache)
  1428. return -ENOMEM;
  1429. r = dm_register_target(&multipath_target);
  1430. if (r < 0) {
  1431. DMERR("register failed %d", r);
  1432. r = -EINVAL;
  1433. goto bad_register_target;
  1434. }
  1435. kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
  1436. if (!kmultipathd) {
  1437. DMERR("failed to create workqueue kmpathd");
  1438. r = -ENOMEM;
  1439. goto bad_alloc_kmultipathd;
  1440. }
  1441. /*
  1442. * A separate workqueue is used to handle the device handlers
  1443. * to avoid overloading existing workqueue. Overloading the
  1444. * old workqueue would also create a bottleneck in the
  1445. * path of the storage hardware device activation.
  1446. */
  1447. kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
  1448. WQ_MEM_RECLAIM);
  1449. if (!kmpath_handlerd) {
  1450. DMERR("failed to create workqueue kmpath_handlerd");
  1451. r = -ENOMEM;
  1452. goto bad_alloc_kmpath_handlerd;
  1453. }
  1454. DMINFO("version %u.%u.%u loaded",
  1455. multipath_target.version[0], multipath_target.version[1],
  1456. multipath_target.version[2]);
  1457. return 0;
  1458. bad_alloc_kmpath_handlerd:
  1459. destroy_workqueue(kmultipathd);
  1460. bad_alloc_kmultipathd:
  1461. dm_unregister_target(&multipath_target);
  1462. bad_register_target:
  1463. kmem_cache_destroy(_mpio_cache);
  1464. return r;
  1465. }
  1466. static void __exit dm_multipath_exit(void)
  1467. {
  1468. destroy_workqueue(kmpath_handlerd);
  1469. destroy_workqueue(kmultipathd);
  1470. dm_unregister_target(&multipath_target);
  1471. kmem_cache_destroy(_mpio_cache);
  1472. }
  1473. module_init(dm_multipath_init);
  1474. module_exit(dm_multipath_exit);
  1475. MODULE_DESCRIPTION(DM_NAME " multipath target");
  1476. MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
  1477. MODULE_LICENSE("GPL");