dm-mpath.c 40 KB

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