dm-mpath.c 41 KB

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