dm-mpath.c 38 KB

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