2 * Copyright (c) 2004, 2005 Christophe Varoqui
3 * Copyright (c) 2004 Stefan Bader, IBM
7 #include <libdevmapper.h>
17 #include "structs_vec.h"
18 #include "blacklist.h"
20 #include "prioritizers/alua_spc3.h"
21 #include "dm-generic.h"
23 struct adapter_group *
24 alloc_adaptergroup(void)
26 struct adapter_group *agp;
28 agp = (struct adapter_group *)MALLOC(sizeof(struct adapter_group));
33 agp->host_groups = vector_alloc();
34 if (!agp->host_groups) {
41 void free_adaptergroup(vector adapters)
44 struct adapter_group *agp;
46 vector_foreach_slot(adapters, agp, i) {
47 free_hostgroup(agp->host_groups);
50 vector_free(adapters);
53 void free_hostgroup(vector hostgroups)
56 struct host_group *hgp;
61 vector_foreach_slot(hostgroups, hgp, i) {
62 vector_free(hgp->paths);
65 vector_free(hostgroups);
71 struct host_group *hgp;
73 hgp = (struct host_group *)MALLOC(sizeof(struct host_group));
78 hgp->paths = vector_alloc();
92 pp = (struct path *)MALLOC(sizeof(struct path));
95 pp->initialized = INIT_NEW;
96 pp->sg_id.host_no = -1;
97 pp->sg_id.channel = -1;
98 pp->sg_id.scsi_id = -1;
100 pp->sg_id.proto_id = SCSI_PROTOCOL_UNSPEC;
102 pp->tpgs = TPGS_UNDEF;
103 pp->priority = PRIO_UNDEF;
104 pp->checkint = CHECKINT_UNDEF;
105 checker_clear(&pp->checker);
106 dm_path_to_gen(pp)->ops = &dm_gen_path_ops;
107 pp->hwe = vector_alloc();
108 if (pp->hwe == NULL) {
117 uninitialize_path(struct path *pp)
122 pp->dmstate = PSTATE_UNDEF;
123 pp->uid_attribute = NULL;
126 if (checker_selected(&pp->checker))
127 checker_put(&pp->checker);
129 if (prio_selected(&pp->prio))
139 free_path (struct path * pp)
144 uninitialize_path(pp);
147 udev_device_unref(pp->udev);
153 vector_free(pp->hwe);
159 free_pathvec (vector vec, enum free_path_mode free_paths)
167 if (free_paths == FREE_PATHS)
168 vector_foreach_slot(vec, pp, i)
175 alloc_pathgroup (void)
177 struct pathgroup * pgp;
179 pgp = (struct pathgroup *)MALLOC(sizeof(struct pathgroup));
184 pgp->paths = vector_alloc();
191 dm_pathgroup_to_gen(pgp)->ops = &dm_gen_pathgroup_ops;
196 free_pathgroup (struct pathgroup * pgp, enum free_path_mode free_paths)
201 free_pathvec(pgp->paths, free_paths);
206 free_pgvec (vector pgvec, enum free_path_mode free_paths)
209 struct pathgroup * pgp;
214 vector_foreach_slot(pgvec, pgp, i)
215 free_pathgroup(pgp, free_paths);
221 alloc_multipath (void)
223 struct multipath * mpp;
225 mpp = (struct multipath *)MALLOC(sizeof(struct multipath));
229 mpp->mpcontext = NULL;
230 mpp->no_path_retry = NO_PATH_RETRY_UNDEF;
231 mpp->fast_io_fail = MP_FAST_IO_FAIL_UNSET;
232 dm_multipath_to_gen(mpp)->ops = &dm_gen_multipath_ops;
237 void *set_mpp_hwe(struct multipath *mpp, const struct path *pp)
239 if (!mpp || !pp || !pp->hwe)
243 mpp->hwe = vector_convert(NULL, pp->hwe,
244 struct hwentry, identity);
248 void free_multipath_attributes(struct multipath *mpp)
255 mpp->selector = NULL;
260 mpp->features = NULL;
263 if (mpp->hwhandler) {
264 FREE(mpp->hwhandler);
265 mpp->hwhandler = NULL;
270 free_multipath (struct multipath * mpp, enum free_path_mode free_paths)
275 free_multipath_attributes(mpp);
287 if (!free_paths && mpp->pg) {
288 struct pathgroup *pgp;
293 * Make sure paths carry no reference to this mpp any more
295 vector_foreach_slot(mpp->pg, pgp, i) {
296 vector_foreach_slot(pgp->paths, pp, j)
302 free_pathvec(mpp->paths, free_paths);
303 free_pgvec(mpp->pg, free_paths);
305 vector_free(mpp->hwe);
308 FREE_PTR(mpp->mpcontext);
313 drop_multipath (vector mpvec, char * wwid, enum free_path_mode free_paths)
316 struct multipath * mpp;
321 vector_foreach_slot (mpvec, mpp, i) {
322 if (!strncmp(mpp->wwid, wwid, WWID_SIZE)) {
323 free_multipath(mpp, free_paths);
324 vector_del_slot(mpvec, i);
331 free_multipathvec (vector mpvec, enum free_path_mode free_paths)
334 struct multipath * mpp;
339 vector_foreach_slot (mpvec, mpp, i)
340 free_multipath(mpp, free_paths);
346 store_path (vector pathvec, struct path * pp)
350 if (!strlen(pp->dev_t)) {
351 condlog(2, "%s: Empty device number", pp->dev);
354 if (!strlen(pp->dev)) {
355 condlog(3, "%s: Empty device name", pp->dev_t);
362 if (!vector_alloc_slot(pathvec))
365 vector_set_slot(pathvec, pp);
370 int add_pathgroup(struct multipath *mpp, struct pathgroup *pgp)
372 if (!vector_alloc_slot(mpp->pg))
375 vector_set_slot(mpp->pg, pgp);
382 store_hostgroup(vector hostgroupvec, struct host_group * hgp)
384 if (!vector_alloc_slot(hostgroupvec))
387 vector_set_slot(hostgroupvec, hgp);
392 store_adaptergroup(vector adapters, struct adapter_group * agp)
394 if (!vector_alloc_slot(adapters))
397 vector_set_slot(adapters, agp);
402 find_mp_by_minor (const struct _vector *mpvec, unsigned int minor)
405 struct multipath * mpp;
410 vector_foreach_slot (mpvec, mpp, i) {
414 if (mpp->dmi->minor == minor)
421 find_mp_by_wwid (const struct _vector *mpvec, const char * wwid)
424 struct multipath * mpp;
429 vector_foreach_slot (mpvec, mpp, i)
430 if (!strncmp(mpp->wwid, wwid, WWID_SIZE))
437 find_mp_by_alias (const struct _vector *mpvec, const char * alias)
441 struct multipath * mpp;
451 vector_foreach_slot (mpvec, mpp, i) {
452 if (strlen(mpp->alias) == len &&
453 !strncmp(mpp->alias, alias, len))
460 find_mp_by_str (const struct _vector *mpvec, const char * str)
464 if (sscanf(str, "dm-%d", &minor) == 1)
465 return find_mp_by_minor(mpvec, minor);
467 return find_mp_by_alias(mpvec, str);
471 find_path_by_dev (const struct _vector *pathvec, const char *dev)
476 if (!pathvec || !dev)
479 vector_foreach_slot (pathvec, pp, i)
480 if (!strcmp(pp->dev, dev))
483 condlog(4, "%s: dev not found in pathvec", dev);
488 find_path_by_devt (const struct _vector *pathvec, const char * dev_t)
496 vector_foreach_slot (pathvec, pp, i)
497 if (!strcmp(pp->dev_t, dev_t))
500 condlog(4, "%s: dev_t not found in pathvec", dev_t);
504 static int do_pathcount(const struct multipath *mpp, const int *states,
505 unsigned int nr_states)
507 struct pathgroup *pgp;
510 unsigned int i, j, k;
512 if (!mpp->pg || !nr_states)
515 vector_foreach_slot (mpp->pg, pgp, i) {
516 vector_foreach_slot (pgp->paths, pp, j) {
517 for (k = 0; k < nr_states; k++) {
518 if (pp->state == states[k]) {
528 int pathcount(const struct multipath *mpp, int state)
530 return do_pathcount(mpp, &state, 1);
533 int count_active_paths(const struct multipath *mpp)
535 struct pathgroup *pgp;
543 vector_foreach_slot (mpp->pg, pgp, i) {
544 vector_foreach_slot (pgp->paths, pp, j) {
545 if (pp->state == PATH_UP || pp->state == PATH_GHOST)
552 int count_active_pending_paths(const struct multipath *mpp)
554 int states[] = {PATH_UP, PATH_GHOST, PATH_PENDING};
556 return do_pathcount(mpp, states, 3);
559 int pathcmp(const struct pathgroup *pgp, const struct pathgroup *cpgp)
562 struct path *pp, *cpp;
563 int pnum = 0, found = 0;
565 vector_foreach_slot(pgp->paths, pp, i) {
567 vector_foreach_slot(cpgp->paths, cpp, j) {
568 if ((long)pp == (long)cpp) {
579 first_path (const struct multipath * mpp)
581 struct pathgroup * pgp;
584 pgp = VECTOR_SLOT(mpp->pg, 0);
586 return pgp?VECTOR_SLOT(pgp->paths, 0):NULL;
589 int add_feature(char **f, const char *n)
601 if (strchr(n, ' ') != NULL) {
602 condlog(0, "internal error: feature \"%s\" contains spaces", n);
606 /* default feature is null */
609 l = asprintf(&t, "1 %s", n);
617 /* Check if feature is already present */
621 /* Get feature count */
622 c = strtoul(*f, &e, 10);
623 if (*f == e || (*e != ' ' && *e != '\0')) {
624 condlog(0, "parse error in feature string \"%s\"", *f);
628 /* Add 1 digit and 1 space */
629 l = strlen(e) + strlen(n) + 2;
632 /* Check if we need more digits for feature count */
633 for (d = c; d >= 10; d /= 10)
640 /* e: old feature string with leading space, or "" */
642 while (*(e + 1) == ' ')
645 snprintf(t, l + 1, "%0d%s %s", c, e, n);
653 int remove_feature(char **f, const char *o)
663 if (!o || *o == '\0')
666 /* Check if not present */
670 /* Get feature count */
671 c = strtoul(*f, &e, 10);
676 /* Normalize features */
680 /* Just spaces, return */
688 /* Update feature count */
691 while (q[0] != '\0') {
692 if (q[0] == ' ' && q[1] != ' ' && q[1] != '\0')
697 /* Quick exit if all features have been removed */
706 /* Search feature to be removed */
709 /* Not found, return */
712 /* Update feature count space */
718 /* Copy the feature count */
719 sprintf(n, "%0d", c);
721 * Copy existing features up to the feature
722 * about to be removed
726 /* Internal error, feature string inconsistent */
739 strncat(n, p, (size_t)(e - p));
740 p += (size_t)(e - p);
742 /* Skip feature to be removed */
745 /* Copy remaining features */