dm-mpath.c 38 KB

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