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"
21 struct adapter_group *
22 alloc_adaptergroup(void)
24 struct adapter_group *agp;
26 agp = (struct adapter_group *)MALLOC(sizeof(struct adapter_group));
31 agp->host_groups = vector_alloc();
32 if (!agp->host_groups) {
39 void free_adaptergroup(vector adapters)
42 struct adapter_group *agp;
44 vector_foreach_slot(adapters, agp, i) {
45 free_hostgroup(agp->host_groups);
48 vector_free(adapters);
51 void free_hostgroup(vector hostgroups)
54 struct host_group *hgp;
59 vector_foreach_slot(hostgroups, hgp, i) {
60 vector_free(hgp->paths);
63 vector_free(hostgroups);
69 struct host_group *hgp;
71 hgp = (struct host_group *)MALLOC(sizeof(struct host_group));
76 hgp->paths = vector_alloc();
90 pp = (struct path *)MALLOC(sizeof(struct path));
93 pp->sg_id.host_no = -1;
94 pp->sg_id.channel = -1;
95 pp->sg_id.scsi_id = -1;
98 pp->priority = PRIO_UNDEF;
104 free_path (struct path * pp)
109 if (checker_selected(&pp->checker))
110 checker_put(&pp->checker);
112 if (prio_selected(&pp->prio))
119 udev_device_unref(pp->udev);
127 free_pathvec (vector vec, enum free_path_mode free_paths)
135 if (free_paths == FREE_PATHS)
136 vector_foreach_slot(vec, pp, i)
143 alloc_pathgroup (void)
145 struct pathgroup * pgp;
147 pgp = (struct pathgroup *)MALLOC(sizeof(struct pathgroup));
152 pgp->paths = vector_alloc();
163 free_pathgroup (struct pathgroup * pgp, enum free_path_mode free_paths)
168 free_pathvec(pgp->paths, free_paths);
173 free_pgvec (vector pgvec, enum free_path_mode free_paths)
176 struct pathgroup * pgp;
181 vector_foreach_slot(pgvec, pgp, i)
182 free_pathgroup(pgp, free_paths);
188 alloc_multipath (void)
190 struct multipath * mpp;
192 mpp = (struct multipath *)MALLOC(sizeof(struct multipath));
196 mpp->mpcontext = NULL;
197 mpp->no_path_retry = NO_PATH_RETRY_UNDEF;
198 mpp->fast_io_fail = MP_FAST_IO_FAIL_UNSET;
204 free_multipath_attributes (struct multipath * mpp)
211 mpp->selector = NULL;
216 mpp->features = NULL;
219 if (mpp->hwhandler) {
220 FREE(mpp->hwhandler);
221 mpp->hwhandler = NULL;
226 free_multipath (struct multipath * mpp, enum free_path_mode free_paths)
231 free_multipath_attributes(mpp);
243 free_pathvec(mpp->paths, free_paths);
244 free_pgvec(mpp->pg, free_paths);
245 FREE_PTR(mpp->mpcontext);
250 drop_multipath (vector mpvec, char * wwid, enum free_path_mode free_paths)
253 struct multipath * mpp;
258 vector_foreach_slot (mpvec, mpp, i) {
259 if (!strncmp(mpp->wwid, wwid, WWID_SIZE)) {
260 free_multipath(mpp, free_paths);
261 vector_del_slot(mpvec, i);
268 free_multipathvec (vector mpvec, enum free_path_mode free_paths)
271 struct multipath * mpp;
276 vector_foreach_slot (mpvec, mpp, i)
277 free_multipath(mpp, free_paths);
283 store_path (vector pathvec, struct path * pp)
285 if (!vector_alloc_slot(pathvec))
288 vector_set_slot(pathvec, pp);
294 store_pathgroup (vector pgvec, struct pathgroup * pgp)
296 if (!vector_alloc_slot(pgvec))
299 vector_set_slot(pgvec, pgp);
305 store_hostgroup(vector hostgroupvec, struct host_group * hgp)
307 if (!vector_alloc_slot(hostgroupvec))
310 vector_set_slot(hostgroupvec, hgp);
315 store_adaptergroup(vector adapters, struct adapter_group * agp)
317 if (!vector_alloc_slot(adapters))
320 vector_set_slot(adapters, agp);
325 find_mp_by_minor (vector mpvec, int minor)
328 struct multipath * mpp;
333 vector_foreach_slot (mpvec, mpp, i) {
337 if (mpp->dmi->minor == minor)
344 find_mp_by_wwid (vector mpvec, char * wwid)
347 struct multipath * mpp;
352 vector_foreach_slot (mpvec, mpp, i)
353 if (!strncmp(mpp->wwid, wwid, WWID_SIZE))
360 find_mp_by_alias (vector mpvec, char * alias)
364 struct multipath * mpp;
374 vector_foreach_slot (mpvec, mpp, i) {
375 if (strlen(mpp->alias) == len &&
376 !strncmp(mpp->alias, alias, len))
383 find_mp_by_str (vector mpvec, char * str)
387 if (sscanf(str, "dm-%d", &minor) == 1)
388 return find_mp_by_minor(mpvec, minor);
390 return find_mp_by_alias(mpvec, str);
394 find_path_by_dev (vector pathvec, char * dev)
402 vector_foreach_slot (pathvec, pp, i)
403 if (!strcmp(pp->dev, dev))
406 condlog(3, "%s: not found in pathvec", dev);
411 find_path_by_devt (vector pathvec, char * dev_t)
419 vector_foreach_slot (pathvec, pp, i)
420 if (!strcmp(pp->dev_t, dev_t))
423 condlog(3, "%s: not found in pathvec", dev_t);
428 pathcountgr (struct pathgroup * pgp, int state)
434 vector_foreach_slot (pgp->paths, pp, i)
435 if ((pp->state == state) || (state == PATH_WILD))
442 pathcount (struct multipath * mpp, int state)
444 struct pathgroup *pgp;
449 vector_foreach_slot (mpp->pg, pgp, i)
450 count += pathcountgr(pgp, state);
456 pathcmp (struct pathgroup *pgp, struct pathgroup *cpgp)
459 struct path *pp, *cpp;
460 int pnum = 0, found = 0;
462 vector_foreach_slot(pgp->paths, pp, i) {
464 vector_foreach_slot(cpgp->paths, cpp, j) {
465 if ((long)pp == (long)cpp) {
476 first_path (struct multipath * mpp)
478 struct pathgroup * pgp;
481 pgp = VECTOR_SLOT(mpp->pg, 0);
483 return pgp?VECTOR_SLOT(pgp->paths, 0):NULL;
487 setup_feature(struct multipath * mpp, char *feature)
489 if (!strncmp(feature, "queue_if_no_path", 16)) {
490 if (mpp->no_path_retry <= NO_PATH_RETRY_UNDEF)
491 mpp->no_path_retry = NO_PATH_RETRY_QUEUE;
496 add_feature (char **f, char *n)
508 /* Check if feature is already present */
512 /* Get feature count */
513 c = strtoul(*f, &e, 10);
518 /* Check if we need to increase feature count space */
519 l = strlen(*f) + strlen(n) + 1;
521 /* Count new features */
527 if (*p == ' ' && p[1] != '\0' && p[1] != ' ') {
541 /* Update feature count */
549 snprintf(p, l + 2, "%0d ", c);
551 /* Copy the feature string */
570 remove_feature(char **f, char *o)
579 if (!o || *o == '\0')
582 /* Check if not present */
586 /* Get feature count */
587 c = strtoul(*f, &e, 10);
592 /* Normalize features */
596 /* Just spaces, return */
604 /* Update feature count */
607 while (p[0] != '\0') {
608 if (p[0] == ' ' && p[1] != ' ' && p[1] != '\0')
613 /* Quick exit if all features have been removed */
622 /* Search feature to be removed */
625 /* Not found, return */
628 /* Update feature count space */
634 /* Copy the feature count */
635 sprintf(n, "%0d", c);
637 * Copy existing features up to the feature
638 * about to be removed
642 /* Internal error, feature string inconsistent */
653 strncat(n, p, (size_t)(e - p));
654 p += (size_t)(e - p);
656 /* Skip feature to be removed */
659 /* Copy remaining features */