dm-mpath.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  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. ti->num_write_same_bios = 1;
  739. return 0;
  740. bad:
  741. free_multipath(m);
  742. return r;
  743. }
  744. static void multipath_wait_for_pg_init_completion(struct multipath *m)
  745. {
  746. DECLARE_WAITQUEUE(wait, current);
  747. unsigned long flags;
  748. add_wait_queue(&m->pg_init_wait, &wait);
  749. while (1) {
  750. set_current_state(TASK_UNINTERRUPTIBLE);
  751. spin_lock_irqsave(&m->lock, flags);
  752. if (!m->pg_init_in_progress) {
  753. spin_unlock_irqrestore(&m->lock, flags);
  754. break;
  755. }
  756. spin_unlock_irqrestore(&m->lock, flags);
  757. io_schedule();
  758. }
  759. set_current_state(TASK_RUNNING);
  760. remove_wait_queue(&m->pg_init_wait, &wait);
  761. }
  762. static void flush_multipath_work(struct multipath *m)
  763. {
  764. flush_workqueue(kmpath_handlerd);
  765. multipath_wait_for_pg_init_completion(m);
  766. flush_workqueue(kmultipathd);
  767. flush_work(&m->trigger_event);
  768. }
  769. static void multipath_dtr(struct dm_target *ti)
  770. {
  771. struct multipath *m = ti->private;
  772. flush_multipath_work(m);
  773. free_multipath(m);
  774. }
  775. /*
  776. * Map cloned requests
  777. */
  778. static int multipath_map(struct dm_target *ti, struct request *clone,
  779. union map_info *map_context)
  780. {
  781. int r;
  782. struct multipath *m = (struct multipath *) ti->private;
  783. if (set_mapinfo(m, map_context) < 0)
  784. /* ENOMEM, requeue */
  785. return DM_MAPIO_REQUEUE;
  786. clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
  787. r = map_io(m, clone, map_context, 0);
  788. if (r < 0 || r == DM_MAPIO_REQUEUE)
  789. clear_mapinfo(m, map_context);
  790. return r;
  791. }
  792. /*
  793. * Take a path out of use.
  794. */
  795. static int fail_path(struct pgpath *pgpath)
  796. {
  797. unsigned long flags;
  798. struct multipath *m = pgpath->pg->m;
  799. spin_lock_irqsave(&m->lock, flags);
  800. if (!pgpath->is_active)
  801. goto out;
  802. DMWARN("Failing path %s.", pgpath->path.dev->name);
  803. pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
  804. pgpath->is_active = 0;
  805. pgpath->fail_count++;
  806. m->nr_valid_paths--;
  807. if (pgpath == m->current_pgpath)
  808. m->current_pgpath = NULL;
  809. dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
  810. pgpath->path.dev->name, m->nr_valid_paths);
  811. schedule_work(&m->trigger_event);
  812. out:
  813. spin_unlock_irqrestore(&m->lock, flags);
  814. return 0;
  815. }
  816. /*
  817. * Reinstate a previously-failed path
  818. */
  819. static int reinstate_path(struct pgpath *pgpath)
  820. {
  821. int r = 0;
  822. unsigned long flags;
  823. struct multipath *m = pgpath->pg->m;
  824. spin_lock_irqsave(&m->lock, flags);
  825. if (pgpath->is_active)
  826. goto out;
  827. if (!pgpath->pg->ps.type->reinstate_path) {
  828. DMWARN("Reinstate path not supported by path selector %s",
  829. pgpath->pg->ps.type->name);
  830. r = -EINVAL;
  831. goto out;
  832. }
  833. r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
  834. if (r)
  835. goto out;
  836. pgpath->is_active = 1;
  837. if (!m->nr_valid_paths++ && m->queue_size) {
  838. m->current_pgpath = NULL;
  839. queue_work(kmultipathd, &m->process_queued_ios);
  840. } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
  841. if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
  842. m->pg_init_in_progress++;
  843. }
  844. dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
  845. pgpath->path.dev->name, m->nr_valid_paths);
  846. schedule_work(&m->trigger_event);
  847. out:
  848. spin_unlock_irqrestore(&m->lock, flags);
  849. return r;
  850. }
  851. /*
  852. * Fail or reinstate all paths that match the provided struct dm_dev.
  853. */
  854. static int action_dev(struct multipath *m, struct dm_dev *dev,
  855. action_fn action)
  856. {
  857. int r = -EINVAL;
  858. struct pgpath *pgpath;
  859. struct priority_group *pg;
  860. list_for_each_entry(pg, &m->priority_groups, list) {
  861. list_for_each_entry(pgpath, &pg->pgpaths, list) {
  862. if (pgpath->path.dev == dev)
  863. r = action(pgpath);
  864. }
  865. }
  866. return r;
  867. }
  868. /*
  869. * Temporarily try to avoid having to use the specified PG
  870. */
  871. static void bypass_pg(struct multipath *m, struct priority_group *pg,
  872. int bypassed)
  873. {
  874. unsigned long flags;
  875. spin_lock_irqsave(&m->lock, flags);
  876. pg->bypassed = bypassed;
  877. m->current_pgpath = NULL;
  878. m->current_pg = NULL;
  879. spin_unlock_irqrestore(&m->lock, flags);
  880. schedule_work(&m->trigger_event);
  881. }
  882. /*
  883. * Switch to using the specified PG from the next I/O that gets mapped
  884. */
  885. static int switch_pg_num(struct multipath *m, const char *pgstr)
  886. {
  887. struct priority_group *pg;
  888. unsigned pgnum;
  889. unsigned long flags;
  890. char dummy;
  891. if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
  892. (pgnum > m->nr_priority_groups)) {
  893. DMWARN("invalid PG number supplied to switch_pg_num");
  894. return -EINVAL;
  895. }
  896. spin_lock_irqsave(&m->lock, flags);
  897. list_for_each_entry(pg, &m->priority_groups, list) {
  898. pg->bypassed = 0;
  899. if (--pgnum)
  900. continue;
  901. m->current_pgpath = NULL;
  902. m->current_pg = NULL;
  903. m->next_pg = pg;
  904. }
  905. spin_unlock_irqrestore(&m->lock, flags);
  906. schedule_work(&m->trigger_event);
  907. return 0;
  908. }
  909. /*
  910. * Set/clear bypassed status of a PG.
  911. * PGs are numbered upwards from 1 in the order they were declared.
  912. */
  913. static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
  914. {
  915. struct priority_group *pg;
  916. unsigned pgnum;
  917. char dummy;
  918. if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
  919. (pgnum > m->nr_priority_groups)) {
  920. DMWARN("invalid PG number supplied to bypass_pg");
  921. return -EINVAL;
  922. }
  923. list_for_each_entry(pg, &m->priority_groups, list) {
  924. if (!--pgnum)
  925. break;
  926. }
  927. bypass_pg(m, pg, bypassed);
  928. return 0;
  929. }
  930. /*
  931. * Should we retry pg_init immediately?
  932. */
  933. static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
  934. {
  935. unsigned long flags;
  936. int limit_reached = 0;
  937. spin_lock_irqsave(&m->lock, flags);
  938. if (m->pg_init_count <= m->pg_init_retries)
  939. m->pg_init_required = 1;
  940. else
  941. limit_reached = 1;
  942. spin_unlock_irqrestore(&m->lock, flags);
  943. return limit_reached;
  944. }
  945. static void pg_init_done(void *data, int errors)
  946. {
  947. struct pgpath *pgpath = data;
  948. struct priority_group *pg = pgpath->pg;
  949. struct multipath *m = pg->m;
  950. unsigned long flags;
  951. unsigned delay_retry = 0;
  952. /* device or driver problems */
  953. switch (errors) {
  954. case SCSI_DH_OK:
  955. break;
  956. case SCSI_DH_NOSYS:
  957. if (!m->hw_handler_name) {
  958. errors = 0;
  959. break;
  960. }
  961. DMERR("Could not failover the device: Handler scsi_dh_%s "
  962. "Error %d.", m->hw_handler_name, errors);
  963. /*
  964. * Fail path for now, so we do not ping pong
  965. */
  966. fail_path(pgpath);
  967. break;
  968. case SCSI_DH_DEV_TEMP_BUSY:
  969. /*
  970. * Probably doing something like FW upgrade on the
  971. * controller so try the other pg.
  972. */
  973. bypass_pg(m, pg, 1);
  974. break;
  975. case SCSI_DH_RETRY:
  976. /* Wait before retrying. */
  977. delay_retry = 1;
  978. case SCSI_DH_IMM_RETRY:
  979. case SCSI_DH_RES_TEMP_UNAVAIL:
  980. if (pg_init_limit_reached(m, pgpath))
  981. fail_path(pgpath);
  982. errors = 0;
  983. break;
  984. default:
  985. /*
  986. * We probably do not want to fail the path for a device
  987. * error, but this is what the old dm did. In future
  988. * patches we can do more advanced handling.
  989. */
  990. fail_path(pgpath);
  991. }
  992. spin_lock_irqsave(&m->lock, flags);
  993. if (errors) {
  994. if (pgpath == m->current_pgpath) {
  995. DMERR("Could not failover device. Error %d.", errors);
  996. m->current_pgpath = NULL;
  997. m->current_pg = NULL;
  998. }
  999. } else if (!m->pg_init_required)
  1000. pg->bypassed = 0;
  1001. if (--m->pg_init_in_progress)
  1002. /* Activations of other paths are still on going */
  1003. goto out;
  1004. if (!m->pg_init_required)
  1005. m->queue_io = 0;
  1006. m->pg_init_delay_retry = delay_retry;
  1007. queue_work(kmultipathd, &m->process_queued_ios);
  1008. /*
  1009. * Wake up any thread waiting to suspend.
  1010. */
  1011. wake_up(&m->pg_init_wait);
  1012. out:
  1013. spin_unlock_irqrestore(&m->lock, flags);
  1014. }
  1015. static void activate_path(struct work_struct *work)
  1016. {
  1017. struct pgpath *pgpath =
  1018. container_of(work, struct pgpath, activate_path.work);
  1019. scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
  1020. pg_init_done, pgpath);
  1021. }
  1022. /*
  1023. * end_io handling
  1024. */
  1025. static int do_end_io(struct multipath *m, struct request *clone,
  1026. int error, struct dm_mpath_io *mpio)
  1027. {
  1028. /*
  1029. * We don't queue any clone request inside the multipath target
  1030. * during end I/O handling, since those clone requests don't have
  1031. * bio clones. If we queue them inside the multipath target,
  1032. * we need to make bio clones, that requires memory allocation.
  1033. * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
  1034. * don't have bio clones.)
  1035. * Instead of queueing the clone request here, we queue the original
  1036. * request into dm core, which will remake a clone request and
  1037. * clone bios for it and resubmit it later.
  1038. */
  1039. int r = DM_ENDIO_REQUEUE;
  1040. unsigned long flags;
  1041. if (!error && !clone->errors)
  1042. return 0; /* I/O complete */
  1043. if (error == -EOPNOTSUPP || error == -EREMOTEIO || error == -EILSEQ)
  1044. return error;
  1045. if (mpio->pgpath)
  1046. fail_path(mpio->pgpath);
  1047. spin_lock_irqsave(&m->lock, flags);
  1048. if (!m->nr_valid_paths) {
  1049. if (!m->queue_if_no_path) {
  1050. if (!__must_push_back(m))
  1051. r = -EIO;
  1052. } else {
  1053. if (error == -EBADE)
  1054. r = error;
  1055. }
  1056. }
  1057. spin_unlock_irqrestore(&m->lock, flags);
  1058. return r;
  1059. }
  1060. static int multipath_end_io(struct dm_target *ti, struct request *clone,
  1061. int error, union map_info *map_context)
  1062. {
  1063. struct multipath *m = ti->private;
  1064. struct dm_mpath_io *mpio = map_context->ptr;
  1065. struct pgpath *pgpath;
  1066. struct path_selector *ps;
  1067. int r;
  1068. BUG_ON(!mpio);
  1069. r = do_end_io(m, clone, error, mpio);
  1070. pgpath = mpio->pgpath;
  1071. if (pgpath) {
  1072. ps = &pgpath->pg->ps;
  1073. if (ps->type->end_io)
  1074. ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
  1075. }
  1076. clear_mapinfo(m, map_context);
  1077. return r;
  1078. }
  1079. /*
  1080. * Suspend can't complete until all the I/O is processed so if
  1081. * the last path fails we must error any remaining I/O.
  1082. * Note that if the freeze_bdev fails while suspending, the
  1083. * queue_if_no_path state is lost - userspace should reset it.
  1084. */
  1085. static void multipath_presuspend(struct dm_target *ti)
  1086. {
  1087. struct multipath *m = (struct multipath *) ti->private;
  1088. queue_if_no_path(m, 0, 1);
  1089. }
  1090. static void multipath_postsuspend(struct dm_target *ti)
  1091. {
  1092. struct multipath *m = ti->private;
  1093. mutex_lock(&m->work_mutex);
  1094. flush_multipath_work(m);
  1095. mutex_unlock(&m->work_mutex);
  1096. }
  1097. /*
  1098. * Restore the queue_if_no_path setting.
  1099. */
  1100. static void multipath_resume(struct dm_target *ti)
  1101. {
  1102. struct multipath *m = (struct multipath *) ti->private;
  1103. unsigned long flags;
  1104. spin_lock_irqsave(&m->lock, flags);
  1105. m->queue_if_no_path = m->saved_queue_if_no_path;
  1106. spin_unlock_irqrestore(&m->lock, flags);
  1107. }
  1108. /*
  1109. * Info output has the following format:
  1110. * num_multipath_feature_args [multipath_feature_args]*
  1111. * num_handler_status_args [handler_status_args]*
  1112. * num_groups init_group_number
  1113. * [A|D|E num_ps_status_args [ps_status_args]*
  1114. * num_paths num_selector_args
  1115. * [path_dev A|F fail_count [selector_args]* ]+ ]+
  1116. *
  1117. * Table output has the following format (identical to the constructor string):
  1118. * num_feature_args [features_args]*
  1119. * num_handler_args hw_handler [hw_handler_args]*
  1120. * num_groups init_group_number
  1121. * [priority selector-name num_ps_args [ps_args]*
  1122. * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
  1123. */
  1124. static void multipath_status(struct dm_target *ti, status_type_t type,
  1125. unsigned status_flags, char *result, unsigned maxlen)
  1126. {
  1127. int sz = 0;
  1128. unsigned long flags;
  1129. struct multipath *m = (struct multipath *) ti->private;
  1130. struct priority_group *pg;
  1131. struct pgpath *p;
  1132. unsigned pg_num;
  1133. char state;
  1134. spin_lock_irqsave(&m->lock, flags);
  1135. /* Features */
  1136. if (type == STATUSTYPE_INFO)
  1137. DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
  1138. else {
  1139. DMEMIT("%u ", m->queue_if_no_path +
  1140. (m->pg_init_retries > 0) * 2 +
  1141. (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
  1142. m->retain_attached_hw_handler);
  1143. if (m->queue_if_no_path)
  1144. DMEMIT("queue_if_no_path ");
  1145. if (m->pg_init_retries)
  1146. DMEMIT("pg_init_retries %u ", m->pg_init_retries);
  1147. if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
  1148. DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
  1149. if (m->retain_attached_hw_handler)
  1150. DMEMIT("retain_attached_hw_handler ");
  1151. }
  1152. if (!m->hw_handler_name || type == STATUSTYPE_INFO)
  1153. DMEMIT("0 ");
  1154. else
  1155. DMEMIT("1 %s ", m->hw_handler_name);
  1156. DMEMIT("%u ", m->nr_priority_groups);
  1157. if (m->next_pg)
  1158. pg_num = m->next_pg->pg_num;
  1159. else if (m->current_pg)
  1160. pg_num = m->current_pg->pg_num;
  1161. else
  1162. pg_num = (m->nr_priority_groups ? 1 : 0);
  1163. DMEMIT("%u ", pg_num);
  1164. switch (type) {
  1165. case STATUSTYPE_INFO:
  1166. list_for_each_entry(pg, &m->priority_groups, list) {
  1167. if (pg->bypassed)
  1168. state = 'D'; /* Disabled */
  1169. else if (pg == m->current_pg)
  1170. state = 'A'; /* Currently Active */
  1171. else
  1172. state = 'E'; /* Enabled */
  1173. DMEMIT("%c ", state);
  1174. if (pg->ps.type->status)
  1175. sz += pg->ps.type->status(&pg->ps, NULL, type,
  1176. result + sz,
  1177. maxlen - sz);
  1178. else
  1179. DMEMIT("0 ");
  1180. DMEMIT("%u %u ", pg->nr_pgpaths,
  1181. pg->ps.type->info_args);
  1182. list_for_each_entry(p, &pg->pgpaths, list) {
  1183. DMEMIT("%s %s %u ", p->path.dev->name,
  1184. p->is_active ? "A" : "F",
  1185. p->fail_count);
  1186. if (pg->ps.type->status)
  1187. sz += pg->ps.type->status(&pg->ps,
  1188. &p->path, type, result + sz,
  1189. maxlen - sz);
  1190. }
  1191. }
  1192. break;
  1193. case STATUSTYPE_TABLE:
  1194. list_for_each_entry(pg, &m->priority_groups, list) {
  1195. DMEMIT("%s ", pg->ps.type->name);
  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->table_args);
  1204. list_for_each_entry(p, &pg->pgpaths, list) {
  1205. DMEMIT("%s ", p->path.dev->name);
  1206. if (pg->ps.type->status)
  1207. sz += pg->ps.type->status(&pg->ps,
  1208. &p->path, type, result + sz,
  1209. maxlen - sz);
  1210. }
  1211. }
  1212. break;
  1213. }
  1214. spin_unlock_irqrestore(&m->lock, flags);
  1215. }
  1216. static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
  1217. {
  1218. int r = -EINVAL;
  1219. struct dm_dev *dev;
  1220. struct multipath *m = (struct multipath *) ti->private;
  1221. action_fn action;
  1222. mutex_lock(&m->work_mutex);
  1223. if (dm_suspended(ti)) {
  1224. r = -EBUSY;
  1225. goto out;
  1226. }
  1227. if (argc == 1) {
  1228. if (!strcasecmp(argv[0], "queue_if_no_path")) {
  1229. r = queue_if_no_path(m, 1, 0);
  1230. goto out;
  1231. } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
  1232. r = queue_if_no_path(m, 0, 0);
  1233. goto out;
  1234. }
  1235. }
  1236. if (argc != 2) {
  1237. DMWARN("Unrecognised multipath message received.");
  1238. goto out;
  1239. }
  1240. if (!strcasecmp(argv[0], "disable_group")) {
  1241. r = bypass_pg_num(m, argv[1], 1);
  1242. goto out;
  1243. } else if (!strcasecmp(argv[0], "enable_group")) {
  1244. r = bypass_pg_num(m, argv[1], 0);
  1245. goto out;
  1246. } else if (!strcasecmp(argv[0], "switch_group")) {
  1247. r = switch_pg_num(m, argv[1]);
  1248. goto out;
  1249. } else if (!strcasecmp(argv[0], "reinstate_path"))
  1250. action = reinstate_path;
  1251. else if (!strcasecmp(argv[0], "fail_path"))
  1252. action = fail_path;
  1253. else {
  1254. DMWARN("Unrecognised multipath message received.");
  1255. goto out;
  1256. }
  1257. r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
  1258. if (r) {
  1259. DMWARN("message: error getting device %s",
  1260. argv[1]);
  1261. goto out;
  1262. }
  1263. r = action_dev(m, dev, action);
  1264. dm_put_device(ti, dev);
  1265. out:
  1266. mutex_unlock(&m->work_mutex);
  1267. return r;
  1268. }
  1269. static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
  1270. unsigned long arg)
  1271. {
  1272. struct multipath *m = ti->private;
  1273. struct pgpath *pgpath;
  1274. struct block_device *bdev;
  1275. fmode_t mode;
  1276. unsigned long flags;
  1277. int r;
  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 = -ENOTCONN;
  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 == -ENOTCONN && !fatal_signal_pending(current))
  1300. queue_work(kmultipathd, &m->process_queued_ios);
  1301. return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
  1302. }
  1303. static int multipath_iterate_devices(struct dm_target *ti,
  1304. iterate_devices_callout_fn fn, void *data)
  1305. {
  1306. struct multipath *m = ti->private;
  1307. struct priority_group *pg;
  1308. struct pgpath *p;
  1309. int ret = 0;
  1310. list_for_each_entry(pg, &m->priority_groups, list) {
  1311. list_for_each_entry(p, &pg->pgpaths, list) {
  1312. ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
  1313. if (ret)
  1314. goto out;
  1315. }
  1316. }
  1317. out:
  1318. return ret;
  1319. }
  1320. static int __pgpath_busy(struct pgpath *pgpath)
  1321. {
  1322. struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
  1323. return dm_underlying_device_busy(q);
  1324. }
  1325. /*
  1326. * We return "busy", only when we can map I/Os but underlying devices
  1327. * are busy (so even if we map I/Os now, the I/Os will wait on
  1328. * the underlying queue).
  1329. * In other words, if we want to kill I/Os or queue them inside us
  1330. * due to map unavailability, we don't return "busy". Otherwise,
  1331. * dm core won't give us the I/Os and we can't do what we want.
  1332. */
  1333. static int multipath_busy(struct dm_target *ti)
  1334. {
  1335. int busy = 0, has_active = 0;
  1336. struct multipath *m = ti->private;
  1337. struct priority_group *pg;
  1338. struct pgpath *pgpath;
  1339. unsigned long flags;
  1340. spin_lock_irqsave(&m->lock, flags);
  1341. /* Guess which priority_group will be used at next mapping time */
  1342. if (unlikely(!m->current_pgpath && m->next_pg))
  1343. pg = m->next_pg;
  1344. else if (likely(m->current_pg))
  1345. pg = m->current_pg;
  1346. else
  1347. /*
  1348. * We don't know which pg will be used at next mapping time.
  1349. * We don't call __choose_pgpath() here to avoid to trigger
  1350. * pg_init just by busy checking.
  1351. * So we don't know whether underlying devices we will be using
  1352. * at next mapping time are busy or not. Just try mapping.
  1353. */
  1354. goto out;
  1355. /*
  1356. * If there is one non-busy active path at least, the path selector
  1357. * will be able to select it. So we consider such a pg as not busy.
  1358. */
  1359. busy = 1;
  1360. list_for_each_entry(pgpath, &pg->pgpaths, list)
  1361. if (pgpath->is_active) {
  1362. has_active = 1;
  1363. if (!__pgpath_busy(pgpath)) {
  1364. busy = 0;
  1365. break;
  1366. }
  1367. }
  1368. if (!has_active)
  1369. /*
  1370. * No active path in this pg, so this pg won't be used and
  1371. * the current_pg will be changed at next mapping time.
  1372. * We need to try mapping to determine it.
  1373. */
  1374. busy = 0;
  1375. out:
  1376. spin_unlock_irqrestore(&m->lock, flags);
  1377. return busy;
  1378. }
  1379. /*-----------------------------------------------------------------
  1380. * Module setup
  1381. *---------------------------------------------------------------*/
  1382. static struct target_type multipath_target = {
  1383. .name = "multipath",
  1384. .version = {1, 5, 1},
  1385. .module = THIS_MODULE,
  1386. .ctr = multipath_ctr,
  1387. .dtr = multipath_dtr,
  1388. .map_rq = multipath_map,
  1389. .rq_end_io = multipath_end_io,
  1390. .presuspend = multipath_presuspend,
  1391. .postsuspend = multipath_postsuspend,
  1392. .resume = multipath_resume,
  1393. .status = multipath_status,
  1394. .message = multipath_message,
  1395. .ioctl = multipath_ioctl,
  1396. .iterate_devices = multipath_iterate_devices,
  1397. .busy = multipath_busy,
  1398. };
  1399. static int __init dm_multipath_init(void)
  1400. {
  1401. int r;
  1402. /* allocate a slab for the dm_ios */
  1403. _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
  1404. if (!_mpio_cache)
  1405. return -ENOMEM;
  1406. r = dm_register_target(&multipath_target);
  1407. if (r < 0) {
  1408. DMERR("register failed %d", r);
  1409. kmem_cache_destroy(_mpio_cache);
  1410. return -EINVAL;
  1411. }
  1412. kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
  1413. if (!kmultipathd) {
  1414. DMERR("failed to create workqueue kmpathd");
  1415. dm_unregister_target(&multipath_target);
  1416. kmem_cache_destroy(_mpio_cache);
  1417. return -ENOMEM;
  1418. }
  1419. /*
  1420. * A separate workqueue is used to handle the device handlers
  1421. * to avoid overloading existing workqueue. Overloading the
  1422. * old workqueue would also create a bottleneck in the
  1423. * path of the storage hardware device activation.
  1424. */
  1425. kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
  1426. WQ_MEM_RECLAIM);
  1427. if (!kmpath_handlerd) {
  1428. DMERR("failed to create workqueue kmpath_handlerd");
  1429. destroy_workqueue(kmultipathd);
  1430. dm_unregister_target(&multipath_target);
  1431. kmem_cache_destroy(_mpio_cache);
  1432. return -ENOMEM;
  1433. }
  1434. DMINFO("version %u.%u.%u loaded",
  1435. multipath_target.version[0], multipath_target.version[1],
  1436. multipath_target.version[2]);
  1437. return r;
  1438. }
  1439. static void __exit dm_multipath_exit(void)
  1440. {
  1441. destroy_workqueue(kmpath_handlerd);
  1442. destroy_workqueue(kmultipathd);
  1443. dm_unregister_target(&multipath_target);
  1444. kmem_cache_destroy(_mpio_cache);
  1445. }
  1446. module_init(dm_multipath_init);
  1447. module_exit(dm_multipath_exit);
  1448. MODULE_DESCRIPTION(DM_NAME " multipath target");
  1449. MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
  1450. MODULE_LICENSE("GPL");