dm-mpath.c 39 KB

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