dm-mpath.c 39 KB

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