dm-mpath.c 40 KB

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