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"
22 struct adapter_group *
23 alloc_adaptergroup(void)
25 struct adapter_group *agp;
27 agp = (struct adapter_group *)MALLOC(sizeof(struct adapter_group));
32 agp->host_groups = vector_alloc();
33 if (!agp->host_groups) {
40 void free_adaptergroup(vector adapters)
43 struct adapter_group *agp;
45 vector_foreach_slot(adapters, agp, i) {
46 free_hostgroup(agp->host_groups);
49 vector_free(adapters);
52 void free_hostgroup(vector hostgroups)
55 struct host_group *hgp;
60 vector_foreach_slot(hostgroups, hgp, i) {
61 vector_free(hgp->paths);
64 vector_free(hostgroups);
70 struct host_group *hgp;
72 hgp = (struct host_group *)MALLOC(sizeof(struct host_group));
77 hgp->paths = vector_alloc();
91 pp = (struct path *)MALLOC(sizeof(struct path));
94 pp->sg_id.host_no = -1;
95 pp->sg_id.channel = -1;
96 pp->sg_id.scsi_id = -1;
98 pp->sg_id.proto_id = SCSI_PROTOCOL_UNSPEC;
100 pp->tpgs = TPGS_UNDEF;
101 pp->priority = PRIO_UNDEF;
107 free_path (struct path * pp)
112 if (checker_selected(&pp->checker))
113 checker_put(&pp->checker);
115 if (prio_selected(&pp->prio))
122 udev_device_unref(pp->udev);
130 free_pathvec (vector vec, enum free_path_mode free_paths)
138 if (free_paths == FREE_PATHS)
139 vector_foreach_slot(vec, pp, i)
146 alloc_pathgroup (void)
148 struct pathgroup * pgp;
150 pgp = (struct pathgroup *)MALLOC(sizeof(struct pathgroup));
155 pgp->paths = vector_alloc();
166 free_pathgroup (struct pathgroup * pgp, enum free_path_mode free_paths)
171 free_pathvec(pgp->paths, free_paths);
176 free_pgvec (vector pgvec, enum free_path_mode free_paths)
179 struct pathgroup * pgp;
184 vector_foreach_slot(pgvec, pgp, i)
185 free_pathgroup(pgp, free_paths);
191 alloc_multipath (void)
193 struct multipath * mpp;
195 mpp = (struct multipath *)MALLOC(sizeof(struct multipath));
199 mpp->mpcontext = NULL;
200 mpp->no_path_retry = NO_PATH_RETRY_UNDEF;
201 mpp->fast_io_fail = MP_FAST_IO_FAIL_UNSET;
206 void free_multipath_attributes(struct multipath *mpp)
213 mpp->selector = NULL;
218 mpp->features = NULL;
221 if (mpp->hwhandler) {
222 FREE(mpp->hwhandler);
223 mpp->hwhandler = NULL;
228 free_multipath (struct multipath * mpp, enum free_path_mode free_paths)
233 free_multipath_attributes(mpp);
245 free_pathvec(mpp->paths, free_paths);
246 free_pgvec(mpp->pg, free_paths);
247 FREE_PTR(mpp->mpcontext);
252 drop_multipath (vector mpvec, char * wwid, enum free_path_mode free_paths)
255 struct multipath * mpp;
260 vector_foreach_slot (mpvec, mpp, i) {
261 if (!strncmp(mpp->wwid, wwid, WWID_SIZE)) {
262 free_multipath(mpp, free_paths);
263 vector_del_slot(mpvec, i);
270 free_multipathvec (vector mpvec, enum free_path_mode free_paths)
273 struct multipath * mpp;
278 vector_foreach_slot (mpvec, mpp, i)
279 free_multipath(mpp, free_paths);
285 store_path (vector pathvec, struct path * pp)
289 if (!strlen(pp->dev_t)) {
290 condlog(2, "%s: Empty device number", pp->dev);
293 if (!strlen(pp->dev)) {
294 condlog(2, "%s: Empty device name", pp->dev_t);
301 if (!vector_alloc_slot(pathvec))
304 vector_set_slot(pathvec, pp);
310 store_pathgroup (vector pgvec, struct pathgroup * pgp)
312 if (!vector_alloc_slot(pgvec))
315 vector_set_slot(pgvec, pgp);
321 store_hostgroup(vector hostgroupvec, struct host_group * hgp)
323 if (!vector_alloc_slot(hostgroupvec))
326 vector_set_slot(hostgroupvec, hgp);
331 store_adaptergroup(vector adapters, struct adapter_group * agp)
333 if (!vector_alloc_slot(adapters))
336 vector_set_slot(adapters, agp);
341 find_mp_by_minor (vector mpvec, int minor)
344 struct multipath * mpp;
349 vector_foreach_slot (mpvec, mpp, i) {
353 if (mpp->dmi->minor == minor)
360 find_mp_by_wwid (vector mpvec, char * wwid)
363 struct multipath * mpp;
368 vector_foreach_slot (mpvec, mpp, i)
369 if (!strncmp(mpp->wwid, wwid, WWID_SIZE))
376 find_mp_by_alias (vector mpvec, char * alias)
380 struct multipath * mpp;
390 vector_foreach_slot (mpvec, mpp, i) {
391 if (strlen(mpp->alias) == len &&
392 !strncmp(mpp->alias, alias, len))
399 find_mp_by_str (vector mpvec, char * str)
403 if (sscanf(str, "dm-%d", &minor) == 1)
404 return find_mp_by_minor(mpvec, minor);
406 return find_mp_by_alias(mpvec, str);
410 find_path_by_dev (vector pathvec, char * dev)
418 vector_foreach_slot (pathvec, pp, i)
419 if (!strcmp(pp->dev, dev))
422 condlog(4, "%s: dev not found in pathvec", dev);
427 find_path_by_devt (vector pathvec, char * dev_t)
435 vector_foreach_slot (pathvec, pp, i)
436 if (!strcmp(pp->dev_t, dev_t))
439 condlog(4, "%s: dev_t not found in pathvec", dev_t);
443 int pathcountgr(struct pathgroup *pgp, int state)
449 vector_foreach_slot (pgp->paths, pp, i)
450 if ((pp->state == state) || (state == PATH_WILD))
456 int pathcount(struct multipath *mpp, int state)
458 struct pathgroup *pgp;
463 vector_foreach_slot (mpp->pg, pgp, i)
464 count += pathcountgr(pgp, state);
469 int pathcmp(struct pathgroup *pgp, struct pathgroup *cpgp)
472 struct path *pp, *cpp;
473 int pnum = 0, found = 0;
475 vector_foreach_slot(pgp->paths, pp, i) {
477 vector_foreach_slot(cpgp->paths, cpp, j) {
478 if ((long)pp == (long)cpp) {
489 first_path (struct multipath * mpp)
491 struct pathgroup * pgp;
494 pgp = VECTOR_SLOT(mpp->pg, 0);
496 return pgp?VECTOR_SLOT(pgp->paths, 0):NULL;
499 void setup_feature(struct multipath *mpp, char *feature)
501 if (!strncmp(feature, "queue_if_no_path", 16)) {
502 if (mpp->no_path_retry <= NO_PATH_RETRY_UNDEF)
503 mpp->no_path_retry = NO_PATH_RETRY_QUEUE;
505 condlog(1, "%s: ignoring feature queue_if_no_path because no_path_retry = %d",
506 mpp->alias, mpp->no_path_retry);
507 } else if (!strcmp(feature, "retain_attached_hw_handler")) {
508 if (mpp->retain_hwhandler != RETAIN_HWHANDLER_OFF)
509 mpp->retain_hwhandler = RETAIN_HWHANDLER_ON;
511 condlog(1, "%s: ignoring feature 'retain_attached_hw_handler'",
516 int add_feature(char **f, char *n)
528 /* default feature is null */
531 l = asprintf(&t, "1 %s", n);
539 /* Check if feature is already present */
544 /* Get feature count */
545 c = strtoul(*f, &e, 10);
550 /* Check if we need to increase feature count space */
551 l = strlen(*f) + strlen(n) + 1;
553 /* Count new features */
559 if (*p == ' ' && p[1] != '\0' && p[1] != ' ') {
573 /* Update feature count */
581 snprintf(p, l + 2, "%0d ", c);
583 /* Copy the feature string */
604 int remove_feature(char **f, char *o)
613 if (!o || *o == '\0')
616 /* Check if not present */
620 /* Get feature count */
621 c = strtoul(*f, &e, 10);
626 /* Normalize features */
630 /* Just spaces, return */
638 /* Update feature count */
641 while (p[0] != '\0') {
642 if (p[0] == ' ' && p[1] != ' ' && p[1] != '\0')
647 /* Quick exit if all features have been removed */
656 /* Search feature to be removed */
659 /* Not found, return */
662 /* Update feature count space */
668 /* Copy the feature count */
669 sprintf(n, "%0d", c);
671 * Copy existing features up to the feature
672 * about to be removed
676 /* Internal error, feature string inconsistent */
689 strncat(n, p, (size_t)(e - p));
690 p += (size_t)(e - p);
692 /* Skip feature to be removed */
695 /* Copy remaining features */