13 #include <linux/ctype.h>
16 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/time.h>
25 #define DM_MSG_PREFIX "multipath"
26 #define DM_PG_INIT_DELAY_MSECS 2000
27 #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
41 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
133 static struct pgpath *alloc_pgpath(
void)
145 static void free_pgpath(
struct pgpath *pgpath)
164 struct pgpath *pgpath, *
tmp;
182 ps->
type->destroy(ps);
186 free_pgpaths(&pg->
pgpaths, ti);
217 static void free_multipath(
struct multipath *m)
223 free_priority_group(pg, m->
ti);
240 memset(mpio, 0,
sizeof(*mpio));
258 static void __pg_init_all_paths(
struct multipath *m)
260 struct pgpath *pgpath;
261 unsigned long pg_init_delay = 0;
278 static void __switch_pg(
struct multipath *m,
struct pgpath *pgpath)
311 static void __choose_pgpath(
struct multipath *m,
size_t nr_bytes)
323 if (!__choose_path_in_pg(m, pg, nr_bytes))
341 if (!__choose_path_in_pg(m, pg, nr_bytes)) {
347 }
while (bypassed--);
365 static int __must_push_back(
struct multipath *m)
372 union map_info *map_context,
unsigned was_queued)
375 size_t nr_bytes = blk_rq_bytes(clone);
377 struct pgpath *pgpath;
386 __choose_pgpath(m, nr_bytes);
404 bdev = pgpath->
path.dev->bdev;
405 clone->q = bdev_get_queue(bdev);
406 clone->rq_disk = bdev->
bd_disk;
407 }
else if (__must_push_back(m))
416 pgpath->
pg->ps.type->start_io(&pgpath->
pg->ps, &pgpath->
path,
419 spin_unlock_irqrestore(&m->
lock, flags);
427 static int queue_if_no_path(
struct multipath *m,
unsigned queue_if_no_path,
428 unsigned save_old_value)
442 spin_unlock_irqrestore(&m->
lock, flags);
451 static void dispatch_queued_ios(
struct multipath *m)
461 spin_unlock_irqrestore(&m->
lock, flags);
464 list_del_init(&clone->queuelist);
468 r =
map_io(m, clone, info, 1);
470 clear_mapinfo(m, info);
475 clear_mapinfo(m, info);
485 struct pgpath *pgpath =
NULL;
486 unsigned must_queue = 1;
492 __choose_pgpath(m, 0);
501 __pg_init_all_paths(m);
503 spin_unlock_irqrestore(&m->
lock, flags);
505 dispatch_queued_ios(m);
512 static void trigger_event(
struct work_struct *work)
537 static struct dm_arg _args[] = {
538 {0, 1024,
"invalid number of path selector args"},
543 ti->
error =
"unknown path selector type";
556 ti->
error =
"path selector constructor failed";
573 const char *attached_handler_name;
577 ti->
error =
"no device given";
588 ti->
error =
"error getting device";
593 q = bdev_get_queue(p->
path.dev->bdev);
597 if (attached_handler_name) {
630 ti->
error =
"error attaching hardware handler";
638 ti->
error =
"unable to set hardware "
639 "handler parameters";
663 static struct dm_arg _args[] = {
664 {1, 1024,
"invalid number of paths"},
665 {0, 1024,
"invalid number of selector args"}
669 unsigned i, nr_selector_args,
nr_args;
675 ti->
error =
"not enough priority group arguments";
679 pg = alloc_priority_group();
681 ti->
error =
"couldn't allocate priority group";
686 r = parse_path_selector(as, pg, ti);
701 nr_args = 1 + nr_selector_args;
703 struct pgpath *pgpath;
706 if (as->
argc < nr_args) {
707 ti->
error =
"not enough path parameters";
713 path_args.argv = as->
argv;
715 pgpath = parse_path(&path_args, &pg->
ps, ti);
716 if (IS_ERR(pgpath)) {
729 free_priority_group(pg, ti);
739 static struct dm_arg _args[] = {
740 {0, 1024,
"invalid number of hardware handler args"},
752 ti->
error =
"unknown hardware handler type";
761 for (i = 0; i <= hw_argc - 2; i++)
765 ti->
error =
"memory allocation failed";
769 j =
sprintf(p,
"%d", hw_argc - 1);
770 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
787 const char *arg_name;
789 static struct dm_arg _args[] = {
790 {0, 6,
"invalid number of feature args"},
791 {1, 50,
"pg_init_retries must be between 1 and 50"},
792 {0, 60000,
"pg_init_delay_msecs must be between 0 and 60000"},
806 if (!
strcasecmp(arg_name,
"queue_if_no_path")) {
807 r = queue_if_no_path(m, 1, 0);
811 if (!
strcasecmp(arg_name,
"retain_attached_hw_handler")) {
816 if (!
strcasecmp(arg_name,
"pg_init_retries") &&
823 if (!
strcasecmp(arg_name,
"pg_init_delay_msecs") &&
830 ti->
error =
"Unrecognised multipath feature request";
832 }
while (argc && !r);
837 static int multipath_ctr(
struct dm_target *ti,
unsigned int argc,
841 static struct dm_arg _args[] = {
842 {0, 1024,
"invalid number of priority groups"},
843 {0, 1024,
"invalid initial priority group number"},
849 unsigned pg_count = 0;
850 unsigned next_pg_num;
855 m = alloc_multipath(ti);
857 ti->
error =
"can't allocate multipath";
861 r = parse_features(&as, m);
865 r = parse_hw_handler(&as, m);
879 ti->
error =
"invalid initial priority group";
888 pg = parse_priority_group(&as, m);
903 ti->
error =
"priority group count mismatch";
918 static void multipath_wait_for_pg_init_completion(
struct multipath *m)
930 spin_unlock_irqrestore(&m->
lock, flags);
933 spin_unlock_irqrestore(&m->
lock, flags);
942 static void flush_multipath_work(
struct multipath *m)
945 multipath_wait_for_pg_init_completion(m);
950 static void multipath_dtr(
struct dm_target *ti)
954 flush_multipath_work(m);
967 if (set_mapinfo(m, map_context) < 0)
972 r =
map_io(m, clone, map_context, 0);
974 clear_mapinfo(m, map_context);
982 static int fail_path(
struct pgpath *pgpath)
992 DMWARN(
"Failing path %s.", pgpath->
path.dev->name);
994 pgpath->
pg->ps.type->fail_path(&pgpath->
pg->ps, &pgpath->
path);
1009 spin_unlock_irqrestore(&m->
lock, flags);
1017 static int reinstate_path(
struct pgpath *pgpath)
1020 unsigned long flags;
1028 if (!pgpath->
pg->ps.type->reinstate_path) {
1029 DMWARN(
"Reinstate path not supported by path selector %s",
1030 pgpath->
pg->ps.type->name);
1035 r = pgpath->
pg->ps.type->reinstate_path(&pgpath->
pg->ps, &pgpath->
path);
1055 spin_unlock_irqrestore(&m->
lock, flags);
1067 struct pgpath *pgpath;
1072 if (pgpath->
path.dev == dev)
1086 unsigned long flags;
1094 spin_unlock_irqrestore(&m->
lock, flags);
1102 static int switch_pg_num(
struct multipath *m,
const char *pgstr)
1106 unsigned long flags;
1109 if (!pgstr || (
sscanf(pgstr,
"%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1111 DMWARN(
"invalid PG number supplied to switch_pg_num");
1125 spin_unlock_irqrestore(&m->
lock, flags);
1135 static int bypass_pg_num(
struct multipath *m,
const char *pgstr,
int bypassed)
1141 if (!pgstr || (
sscanf(pgstr,
"%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1143 DMWARN(
"invalid PG number supplied to bypass_pg");
1152 bypass_pg(m, pg, bypassed);
1159 static int pg_init_limit_reached(
struct multipath *m,
struct pgpath *pgpath)
1161 unsigned long flags;
1162 int limit_reached = 0;
1171 spin_unlock_irqrestore(&m->
lock, flags);
1173 return limit_reached;
1176 static void pg_init_done(
void *
data,
int errors)
1178 struct pgpath *pgpath =
data;
1181 unsigned long flags;
1182 unsigned delay_retry = 0;
1193 DMERR(
"Could not failover the device: Handler scsi_dh_%s "
1205 bypass_pg(m, pg, 1);
1212 if (pg_init_limit_reached(m, pgpath))
1228 DMERR(
"Could not failover device. Error %d.", errors);
1251 spin_unlock_irqrestore(&m->
lock, flags);
1254 static void activate_path(
struct work_struct *work)
1256 struct pgpath *pgpath =
1260 pg_init_done, pgpath);
1281 unsigned long flags;
1283 if (!error && !clone->errors)
1295 if (!__must_push_back(m))
1298 if (error == -
EBADE)
1302 spin_unlock_irqrestore(&m->
lock, flags);
1308 int error,
union map_info *map_context)
1312 struct pgpath *pgpath;
1318 r = do_end_io(m, clone, error, mpio);
1321 ps = &pgpath->
pg->ps;
1322 if (ps->
type->end_io)
1325 clear_mapinfo(m, map_context);
1336 static void multipath_presuspend(
struct dm_target *ti)
1340 queue_if_no_path(m, 0, 1);
1343 static void multipath_postsuspend(
struct dm_target *ti)
1348 flush_multipath_work(m);
1355 static void multipath_resume(
struct dm_target *ti)
1358 unsigned long flags;
1362 spin_unlock_irqrestore(&m->
lock, flags);
1382 unsigned status_flags,
char *
result,
unsigned maxlen)
1385 unsigned long flags;
1403 DMEMIT(
"queue_if_no_path ");
1409 DMEMIT(
"retain_attached_hw_handler ");
1440 if (pg->
ps.type->status)
1441 sz += pg->
ps.type->status(&pg->
ps,
NULL, type,
1448 pg->
ps.type->info_args);
1451 DMEMIT(
"%s %s %u ", p->path.dev->name,
1452 p->is_active ?
"A" :
"F",
1454 if (pg->
ps.type->status)
1455 sz += pg->
ps.type->status(&pg->
ps,
1456 &p->path, type, result + sz,
1466 if (pg->
ps.type->status)
1467 sz += pg->
ps.type->status(&pg->
ps,
NULL, type,
1474 pg->
ps.type->table_args);
1477 DMEMIT(
"%s ", p->path.dev->name);
1478 if (pg->
ps.type->status)
1479 sz += pg->
ps.type->status(&pg->
ps,
1480 &p->path, type, result + sz,
1487 spin_unlock_irqrestore(&m->
lock, flags);
1492 static int multipath_message(
struct dm_target *ti,
unsigned argc,
char **argv)
1507 if (!
strcasecmp(argv[0],
"queue_if_no_path")) {
1510 }
else if (!
strcasecmp(argv[0],
"fail_if_no_path")) {
1517 DMWARN(
"Unrecognised multipath message received.");
1522 r = bypass_pg_num(m, argv[1], 1);
1524 }
else if (!
strcasecmp(argv[0],
"enable_group")) {
1525 r = bypass_pg_num(m, argv[1], 0);
1527 }
else if (!
strcasecmp(argv[0],
"switch_group")) {
1528 r = switch_pg_num(m, argv[1]);
1530 }
else if (!
strcasecmp(argv[0],
"reinstate_path"))
1531 action = reinstate_path;
1535 DMWARN(
"Unrecognised multipath message received.");
1541 DMWARN(
"message: error getting device %s",
1546 r = action_dev(m, dev, action);
1555 static int multipath_ioctl(
struct dm_target *ti,
unsigned int cmd,
1559 struct pgpath *pgpath;
1562 unsigned long flags;
1573 __choose_pgpath(m, 0);
1578 bdev = pgpath->
path.dev->bdev;
1579 mode = pgpath->
path.dev->mode;
1587 spin_unlock_irqrestore(&m->
lock, flags);
1604 static int multipath_iterate_devices(
struct dm_target *ti,
1624 static int __pgpath_busy(
struct pgpath *pgpath)
1639 static int multipath_busy(
struct dm_target *ti)
1641 int busy = 0, has_active = 0;
1644 struct pgpath *pgpath;
1645 unsigned long flags;
1673 if (!__pgpath_busy(pgpath)) {
1688 spin_unlock_irqrestore(&m->
lock, flags);
1697 .name =
"multipath",
1698 .version = {1, 5, 0},
1700 .ctr = multipath_ctr,
1701 .dtr = multipath_dtr,
1702 .map_rq = multipath_map,
1703 .rq_end_io = multipath_end_io,
1704 .presuspend = multipath_presuspend,
1705 .postsuspend = multipath_postsuspend,
1706 .resume = multipath_resume,
1707 .status = multipath_status,
1708 .message = multipath_message,
1709 .ioctl = multipath_ioctl,
1710 .iterate_devices = multipath_iterate_devices,
1711 .busy = multipath_busy,
1714 static int __init dm_multipath_init(
void)
1725 DMERR(
"register failed %d", r);
1732 DMERR(
"failed to create workqueue kmpathd");
1746 if (!kmpath_handlerd) {
1747 DMERR(
"failed to create workqueue kmpath_handlerd");
1754 DMINFO(
"version %u.%u.%u loaded",
1761 static void __exit dm_multipath_exit(
void)