dm-mpath.c 41 KB

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