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->sg_id.host_no = -1;
96 pp->sg_id.channel = -1;
97 pp->sg_id.scsi_id = -1;
99 pp->sg_id.proto_id = SCSI_PROTOCOL_UNSPEC;
101 pp->tpgs = TPGS_UNDEF;
102 pp->priority = PRIO_UNDEF;
103 pp->checkint = CHECKINT_UNDEF;
104 checker_clear(&pp->checker);
105 dm_path_to_gen(pp)->ops = &dm_gen_path_ops;
106 pp->hwe = vector_alloc();
107 if (pp->hwe == NULL) {
116 free_path (struct path * pp)
121 if (checker_selected(&pp->checker))
122 checker_put(&pp->checker);
124 if (prio_selected(&pp->prio))
131 udev_device_unref(pp->udev);
134 vector_free(pp->hwe);
140 free_pathvec (vector vec, enum free_path_mode free_paths)
148 if (free_paths == FREE_PATHS)
149 vector_foreach_slot(vec, pp, i)
156 alloc_pathgroup (void)
158 struct pathgroup * pgp;
160 pgp = (struct pathgroup *)MALLOC(sizeof(struct pathgroup));
165 pgp->paths = vector_alloc();
172 dm_pathgroup_to_gen(pgp)->ops = &dm_gen_pathgroup_ops;
177 free_pathgroup (struct pathgroup * pgp, enum free_path_mode free_paths)
182 free_pathvec(pgp->paths, free_paths);
187 free_pgvec (vector pgvec, enum free_path_mode free_paths)
190 struct pathgroup * pgp;
195 vector_foreach_slot(pgvec, pgp, i)
196 free_pathgroup(pgp, free_paths);
202 alloc_multipath (void)
204 struct multipath * mpp;
206 mpp = (struct multipath *)MALLOC(sizeof(struct multipath));
210 mpp->mpcontext = NULL;
211 mpp->no_path_retry = NO_PATH_RETRY_UNDEF;
212 mpp->fast_io_fail = MP_FAST_IO_FAIL_UNSET;
213 dm_multipath_to_gen(mpp)->ops = &dm_gen_multipath_ops;
218 void free_multipath_attributes(struct multipath *mpp)
225 mpp->selector = NULL;
230 mpp->features = NULL;
233 if (mpp->hwhandler) {
234 FREE(mpp->hwhandler);
235 mpp->hwhandler = NULL;
240 free_multipath (struct multipath * mpp, enum free_path_mode free_paths)
245 free_multipath_attributes(mpp);
257 free_pathvec(mpp->paths, free_paths);
258 free_pgvec(mpp->pg, free_paths);
259 FREE_PTR(mpp->mpcontext);
264 drop_multipath (vector mpvec, char * wwid, enum free_path_mode free_paths)
267 struct multipath * mpp;
272 vector_foreach_slot (mpvec, mpp, i) {
273 if (!strncmp(mpp->wwid, wwid, WWID_SIZE)) {
274 free_multipath(mpp, free_paths);
275 vector_del_slot(mpvec, i);
282 free_multipathvec (vector mpvec, enum free_path_mode free_paths)
285 struct multipath * mpp;
290 vector_foreach_slot (mpvec, mpp, i)
291 free_multipath(mpp, free_paths);
297 store_path (vector pathvec, struct path * pp)
301 if (!strlen(pp->dev_t)) {
302 condlog(2, "%s: Empty device number", pp->dev);
305 if (!strlen(pp->dev)) {
306 condlog(2, "%s: Empty device name", pp->dev_t);
313 if (!vector_alloc_slot(pathvec))
316 vector_set_slot(pathvec, pp);
322 store_pathgroup (vector pgvec, struct pathgroup * pgp)
324 if (!vector_alloc_slot(pgvec))
327 vector_set_slot(pgvec, pgp);
332 int add_pathgroup(struct multipath *mpp, struct pathgroup *pgp)
334 int ret = store_pathgroup(mpp->pg, pgp);
343 store_hostgroup(vector hostgroupvec, struct host_group * hgp)
345 if (!vector_alloc_slot(hostgroupvec))
348 vector_set_slot(hostgroupvec, hgp);
353 store_adaptergroup(vector adapters, struct adapter_group * agp)
355 if (!vector_alloc_slot(adapters))
358 vector_set_slot(adapters, agp);
363 find_mp_by_minor (const struct _vector *mpvec, int minor)
366 struct multipath * mpp;
371 vector_foreach_slot (mpvec, mpp, i) {
375 if (mpp->dmi->minor == minor)
382 find_mp_by_wwid (const struct _vector *mpvec, const char * wwid)
385 struct multipath * mpp;
390 vector_foreach_slot (mpvec, mpp, i)
391 if (!strncmp(mpp->wwid, wwid, WWID_SIZE))
398 find_mp_by_alias (const struct _vector *mpvec, const char * alias)
402 struct multipath * mpp;
412 vector_foreach_slot (mpvec, mpp, i) {
413 if (strlen(mpp->alias) == len &&
414 !strncmp(mpp->alias, alias, len))
421 find_mp_by_str (const struct _vector *mpvec, const char * str)
425 if (sscanf(str, "dm-%d", &minor) == 1)
426 return find_mp_by_minor(mpvec, minor);
428 return find_mp_by_alias(mpvec, str);
432 find_path_by_dev (const struct _vector *pathvec, const char * dev)
440 vector_foreach_slot (pathvec, pp, i)
441 if (!strcmp(pp->dev, dev))
444 condlog(4, "%s: dev not found in pathvec", dev);
449 find_path_by_devt (const struct _vector *pathvec, const char * dev_t)
457 vector_foreach_slot (pathvec, pp, i)
458 if (!strcmp(pp->dev_t, dev_t))
461 condlog(4, "%s: dev_t not found in pathvec", dev_t);
465 int pathcountgr(const struct pathgroup *pgp, int state)
471 vector_foreach_slot (pgp->paths, pp, i)
472 if ((pp->state == state) || (state == PATH_WILD))
478 int pathcount(const struct multipath *mpp, int state)
480 struct pathgroup *pgp;
485 vector_foreach_slot (mpp->pg, pgp, i)
486 count += pathcountgr(pgp, state);
491 int pathcmp(const struct pathgroup *pgp, const struct pathgroup *cpgp)
494 struct path *pp, *cpp;
495 int pnum = 0, found = 0;
497 vector_foreach_slot(pgp->paths, pp, i) {
499 vector_foreach_slot(cpgp->paths, cpp, j) {
500 if ((long)pp == (long)cpp) {
511 first_path (const struct multipath * mpp)
513 struct pathgroup * pgp;
516 pgp = VECTOR_SLOT(mpp->pg, 0);
518 return pgp?VECTOR_SLOT(pgp->paths, 0):NULL;
521 int add_feature(char **f, const char *n)
533 if (strchr(n, ' ') != NULL) {
534 condlog(0, "internal error: feature \"%s\" contains spaces", n);
538 /* default feature is null */
541 l = asprintf(&t, "1 %s", n);
549 /* Check if feature is already present */
553 /* Get feature count */
554 c = strtoul(*f, &e, 10);
555 if (*f == e || (*e != ' ' && *e != '\0')) {
556 condlog(0, "parse error in feature string \"%s\"", *f);
560 /* Add 1 digit and 1 space */
561 l = strlen(e) + strlen(n) + 2;
564 /* Check if we need more digits for feature count */
565 for (d = c; d >= 10; d /= 10)
572 /* e: old feature string with leading space, or "" */
574 while (*(e + 1) == ' ')
577 snprintf(t, l + 1, "%0d%s %s", c, e, n);
585 int remove_feature(char **f, const char *o)
595 if (!o || *o == '\0')
598 /* Check if not present */
602 /* Get feature count */
603 c = strtoul(*f, &e, 10);
608 /* Normalize features */
612 /* Just spaces, return */
620 /* Update feature count */
623 while (q[0] != '\0') {
624 if (q[0] == ' ' && q[1] != ' ' && q[1] != '\0')
629 /* Quick exit if all features have been removed */
638 /* Search feature to be removed */
641 /* Not found, return */
644 /* Update feature count space */
650 /* Copy the feature count */
651 sprintf(n, "%0d", c);
653 * Copy existing features up to the feature
654 * about to be removed
658 /* Internal error, feature string inconsistent */
671 strncat(n, p, (size_t)(e - p));
672 p += (size_t)(e - p);
674 /* Skip feature to be removed */
677 /* Copy remaining features */