dm-mpath.c 40 KB

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