Imported Upstream version 0.8.3
[platform/upstream/multipath-tools.git] / libmultipath / structs.c
1 /*
2  * Copyright (c) 2004, 2005 Christophe Varoqui
3  * Copyright (c) 2004 Stefan Bader, IBM
4  */
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <libdevmapper.h>
8 #include <libudev.h>
9
10 #include "checkers.h"
11 #include "memory.h"
12 #include "vector.h"
13 #include "util.h"
14 #include "structs.h"
15 #include "config.h"
16 #include "debug.h"
17 #include "structs_vec.h"
18 #include "blacklist.h"
19 #include "prio.h"
20 #include "prioritizers/alua_spc3.h"
21 #include "dm-generic.h"
22
23 struct adapter_group *
24 alloc_adaptergroup(void)
25 {
26         struct adapter_group *agp;
27
28         agp = (struct adapter_group *)MALLOC(sizeof(struct adapter_group));
29
30         if (!agp)
31                 return NULL;
32
33         agp->host_groups = vector_alloc();
34         if (!agp->host_groups) {
35                 FREE(agp);
36                 agp = NULL;
37         }
38         return agp;
39 }
40
41 void free_adaptergroup(vector adapters)
42 {
43         int i;
44         struct adapter_group *agp;
45
46         vector_foreach_slot(adapters, agp, i) {
47                 free_hostgroup(agp->host_groups);
48                 FREE(agp);
49         }
50         vector_free(adapters);
51 }
52
53 void free_hostgroup(vector hostgroups)
54 {
55         int i;
56         struct host_group *hgp;
57
58         if (!hostgroups)
59                 return;
60
61         vector_foreach_slot(hostgroups, hgp, i) {
62                 vector_free(hgp->paths);
63                 FREE(hgp);
64         }
65         vector_free(hostgroups);
66 }
67
68 struct host_group *
69 alloc_hostgroup(void)
70 {
71         struct host_group *hgp;
72
73         hgp = (struct host_group *)MALLOC(sizeof(struct host_group));
74
75         if (!hgp)
76                 return NULL;
77
78         hgp->paths = vector_alloc();
79
80         if (!hgp->paths) {
81                 FREE(hgp);
82                 hgp = NULL;
83         }
84         return hgp;
85 }
86
87 struct path *
88 alloc_path (void)
89 {
90         struct path * pp;
91
92         pp = (struct path *)MALLOC(sizeof(struct path));
93
94         if (pp) {
95                 pp->sg_id.host_no = -1;
96                 pp->sg_id.channel = -1;
97                 pp->sg_id.scsi_id = -1;
98                 pp->sg_id.lun = -1;
99                 pp->sg_id.proto_id = SCSI_PROTOCOL_UNSPEC;
100                 pp->fd = -1;
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) {
108                         free(pp);
109                         return NULL;
110                 }
111         }
112         return pp;
113 }
114
115 void
116 free_path (struct path * pp)
117 {
118         if (!pp)
119                 return;
120
121         if (checker_selected(&pp->checker))
122                 checker_put(&pp->checker);
123
124         if (prio_selected(&pp->prio))
125                 prio_put(&pp->prio);
126
127         if (pp->fd >= 0)
128                 close(pp->fd);
129
130         if (pp->udev) {
131                 udev_device_unref(pp->udev);
132                 pp->udev = NULL;
133         }
134         vector_free(pp->hwe);
135
136         FREE(pp);
137 }
138
139 void
140 free_pathvec (vector vec, enum free_path_mode free_paths)
141 {
142         int i;
143         struct path * pp;
144
145         if (!vec)
146                 return;
147
148         if (free_paths == FREE_PATHS)
149                 vector_foreach_slot(vec, pp, i)
150                         free_path(pp);
151
152         vector_free(vec);
153 }
154
155 struct pathgroup *
156 alloc_pathgroup (void)
157 {
158         struct pathgroup * pgp;
159
160         pgp = (struct pathgroup *)MALLOC(sizeof(struct pathgroup));
161
162         if (!pgp)
163                 return NULL;
164
165         pgp->paths = vector_alloc();
166
167         if (!pgp->paths) {
168                 FREE(pgp);
169                 return NULL;
170         }
171
172         dm_pathgroup_to_gen(pgp)->ops = &dm_gen_pathgroup_ops;
173         return pgp;
174 }
175
176 void
177 free_pathgroup (struct pathgroup * pgp, enum free_path_mode free_paths)
178 {
179         if (!pgp)
180                 return;
181
182         free_pathvec(pgp->paths, free_paths);
183         FREE(pgp);
184 }
185
186 void
187 free_pgvec (vector pgvec, enum free_path_mode free_paths)
188 {
189         int i;
190         struct pathgroup * pgp;
191
192         if (!pgvec)
193                 return;
194
195         vector_foreach_slot(pgvec, pgp, i)
196                 free_pathgroup(pgp, free_paths);
197
198         vector_free(pgvec);
199 }
200
201 struct multipath *
202 alloc_multipath (void)
203 {
204         struct multipath * mpp;
205
206         mpp = (struct multipath *)MALLOC(sizeof(struct multipath));
207
208         if (mpp) {
209                 mpp->bestpg = 1;
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;
214         }
215         return mpp;
216 }
217
218 void free_multipath_attributes(struct multipath *mpp)
219 {
220         if (!mpp)
221                 return;
222
223         if (mpp->selector) {
224                 FREE(mpp->selector);
225                 mpp->selector = NULL;
226         }
227
228         if (mpp->features) {
229                 FREE(mpp->features);
230                 mpp->features = NULL;
231         }
232
233         if (mpp->hwhandler) {
234                 FREE(mpp->hwhandler);
235                 mpp->hwhandler = NULL;
236         }
237 }
238
239 void
240 free_multipath (struct multipath * mpp, enum free_path_mode free_paths)
241 {
242         if (!mpp)
243                 return;
244
245         free_multipath_attributes(mpp);
246
247         if (mpp->alias) {
248                 FREE(mpp->alias);
249                 mpp->alias = NULL;
250         }
251
252         if (mpp->dmi) {
253                 FREE(mpp->dmi);
254                 mpp->dmi = NULL;
255         }
256
257         free_pathvec(mpp->paths, free_paths);
258         free_pgvec(mpp->pg, free_paths);
259         FREE_PTR(mpp->mpcontext);
260         FREE(mpp);
261 }
262
263 void
264 drop_multipath (vector mpvec, char * wwid, enum free_path_mode free_paths)
265 {
266         int i;
267         struct multipath * mpp;
268
269         if (!mpvec)
270                 return;
271
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);
276                         return;
277                 }
278         }
279 }
280
281 void
282 free_multipathvec (vector mpvec, enum free_path_mode free_paths)
283 {
284         int i;
285         struct multipath * mpp;
286
287         if (!mpvec)
288                 return;
289
290         vector_foreach_slot (mpvec, mpp, i)
291                 free_multipath(mpp, free_paths);
292
293         vector_free(mpvec);
294 }
295
296 int
297 store_path (vector pathvec, struct path * pp)
298 {
299         int err = 0;
300
301         if (!strlen(pp->dev_t)) {
302                 condlog(2, "%s: Empty device number", pp->dev);
303                 err++;
304         }
305         if (!strlen(pp->dev)) {
306                 condlog(2, "%s: Empty device name", pp->dev_t);
307                 err++;
308         }
309
310         if (err > 1)
311                 return 1;
312
313         if (!vector_alloc_slot(pathvec))
314                 return 1;
315
316         vector_set_slot(pathvec, pp);
317
318         return 0;
319 }
320
321 int add_pathgroup(struct multipath *mpp, struct pathgroup *pgp)
322 {
323         if (!vector_alloc_slot(mpp->pg))
324                 return 1;
325
326         vector_set_slot(mpp->pg, pgp);
327
328         pgp->mpp = mpp;
329         return 0;
330 }
331
332 int
333 store_hostgroup(vector hostgroupvec, struct host_group * hgp)
334 {
335         if (!vector_alloc_slot(hostgroupvec))
336                 return 1;
337
338         vector_set_slot(hostgroupvec, hgp);
339         return 0;
340 }
341
342 int
343 store_adaptergroup(vector adapters, struct adapter_group * agp)
344 {
345         if (!vector_alloc_slot(adapters))
346                 return 1;
347
348         vector_set_slot(adapters, agp);
349         return 0;
350 }
351
352 struct multipath *
353 find_mp_by_minor (const struct _vector *mpvec, int minor)
354 {
355         int i;
356         struct multipath * mpp;
357
358         if (!mpvec)
359                 return NULL;
360
361         vector_foreach_slot (mpvec, mpp, i) {
362                 if (!mpp->dmi)
363                         continue;
364
365                 if (mpp->dmi->minor == minor)
366                         return mpp;
367         }
368         return NULL;
369 }
370
371 struct multipath *
372 find_mp_by_wwid (const struct _vector *mpvec, const char * wwid)
373 {
374         int i;
375         struct multipath * mpp;
376
377         if (!mpvec)
378                 return NULL;
379
380         vector_foreach_slot (mpvec, mpp, i)
381                 if (!strncmp(mpp->wwid, wwid, WWID_SIZE))
382                         return mpp;
383
384         return NULL;
385 }
386
387 struct multipath *
388 find_mp_by_alias (const struct _vector *mpvec, const char * alias)
389 {
390         int i;
391         int len;
392         struct multipath * mpp;
393
394         if (!mpvec)
395                 return NULL;
396
397         len = strlen(alias);
398
399         if (!len)
400                 return NULL;
401
402         vector_foreach_slot (mpvec, mpp, i) {
403                 if (strlen(mpp->alias) == len &&
404                     !strncmp(mpp->alias, alias, len))
405                         return mpp;
406         }
407         return NULL;
408 }
409
410 struct multipath *
411 find_mp_by_str (const struct _vector *mpvec, const char * str)
412 {
413         int minor;
414
415         if (sscanf(str, "dm-%d", &minor) == 1)
416                 return find_mp_by_minor(mpvec, minor);
417         else
418                 return find_mp_by_alias(mpvec, str);
419 }
420
421 struct path *
422 find_path_by_dev (const struct _vector *pathvec, const char * dev)
423 {
424         int i;
425         struct path * pp;
426
427         if (!pathvec)
428                 return NULL;
429
430         vector_foreach_slot (pathvec, pp, i)
431                 if (!strcmp(pp->dev, dev))
432                         return pp;
433
434         condlog(4, "%s: dev not found in pathvec", dev);
435         return NULL;
436 }
437
438 struct path *
439 find_path_by_devt (const struct _vector *pathvec, const char * dev_t)
440 {
441         int i;
442         struct path * pp;
443
444         if (!pathvec)
445                 return NULL;
446
447         vector_foreach_slot (pathvec, pp, i)
448                 if (!strcmp(pp->dev_t, dev_t))
449                         return pp;
450
451         condlog(4, "%s: dev_t not found in pathvec", dev_t);
452         return NULL;
453 }
454
455 int pathcountgr(const struct pathgroup *pgp, int state)
456 {
457         struct path *pp;
458         int count = 0;
459         int i;
460
461         vector_foreach_slot (pgp->paths, pp, i)
462                 if ((pp->state == state) || (state == PATH_WILD))
463                         count++;
464
465         return count;
466 }
467
468 int pathcount(const struct multipath *mpp, int state)
469 {
470         struct pathgroup *pgp;
471         int count = 0;
472         int i;
473
474         if (mpp->pg) {
475                 vector_foreach_slot (mpp->pg, pgp, i)
476                         count += pathcountgr(pgp, state);
477         }
478         return count;
479 }
480
481 int pathcmp(const struct pathgroup *pgp, const struct pathgroup *cpgp)
482 {
483         int i, j;
484         struct path *pp, *cpp;
485         int pnum = 0, found = 0;
486
487         vector_foreach_slot(pgp->paths, pp, i) {
488                 pnum++;
489                 vector_foreach_slot(cpgp->paths, cpp, j) {
490                         if ((long)pp == (long)cpp) {
491                                 found++;
492                                 break;
493                         }
494                 }
495         }
496
497         return pnum - found;
498 }
499
500 struct path *
501 first_path (const struct multipath * mpp)
502 {
503         struct pathgroup * pgp;
504         if (!mpp->pg)
505                 return NULL;
506         pgp = VECTOR_SLOT(mpp->pg, 0);
507
508         return pgp?VECTOR_SLOT(pgp->paths, 0):NULL;
509 }
510
511 int add_feature(char **f, const char *n)
512 {
513         int c = 0, d, l;
514         char *e, *t;
515
516         if (!f)
517                 return 1;
518
519         /* Nothing to do */
520         if (!n || *n == '0')
521                 return 0;
522
523         if (strchr(n, ' ') != NULL) {
524                 condlog(0, "internal error: feature \"%s\" contains spaces", n);
525                 return 1;
526         }
527
528         /* default feature is null */
529         if(!*f)
530         {
531                 l = asprintf(&t, "1 %s", n);
532                 if(l == -1)
533                         return 1;
534
535                 *f = t;
536                 return 0;
537         }
538
539         /* Check if feature is already present */
540         if (strstr(*f, n))
541                 return 0;
542
543         /* Get feature count */
544         c = strtoul(*f, &e, 10);
545         if (*f == e || (*e != ' ' && *e != '\0')) {
546                 condlog(0, "parse error in feature string \"%s\"", *f);
547                 return 1;
548         }
549
550         /* Add 1 digit and 1 space */
551         l = strlen(e) + strlen(n) + 2;
552
553         c++;
554         /* Check if we need more digits for feature count */
555         for (d = c; d >= 10; d /= 10)
556                 l++;
557
558         t = MALLOC(l + 1);
559         if (!t)
560                 return 1;
561
562         /* e: old feature string with leading space, or "" */
563         if (*e == ' ')
564                 while (*(e + 1) == ' ')
565                         e++;
566
567         snprintf(t, l + 1, "%0d%s %s", c, e, n);
568
569         FREE(*f);
570         *f = t;
571
572         return 0;
573 }
574
575 int remove_feature(char **f, const char *o)
576 {
577         int c = 0, d, l;
578         char *e, *p, *n;
579         const char *q;
580
581         if (!f || !*f)
582                 return 1;
583
584         /* Nothing to do */
585         if (!o || *o == '\0')
586                 return 0;
587
588         /* Check if not present */
589         if (!strstr(*f, o))
590                 return 0;
591
592         /* Get feature count */
593         c = strtoul(*f, &e, 10);
594         if (*f == e)
595                 /* parse error */
596                 return 1;
597
598         /* Normalize features */
599         while (*o == ' ') {
600                 o++;
601         }
602         /* Just spaces, return */
603         if (*o == '\0')
604                 return 0;
605         q = o + strlen(o);
606         while (*q == ' ')
607                 q--;
608         d = (int)(q - o);
609
610         /* Update feature count */
611         c--;
612         q = o;
613         while (q[0] != '\0') {
614                 if (q[0] == ' ' && q[1] != ' ' && q[1] != '\0')
615                         c--;
616                 q++;
617         }
618
619         /* Quick exit if all features have been removed */
620         if (c == 0) {
621                 n = MALLOC(2);
622                 if (!n)
623                         return 1;
624                 strcpy(n, "0");
625                 goto out;
626         }
627
628         /* Search feature to be removed */
629         e = strstr(*f, o);
630         if (!e)
631                 /* Not found, return */
632                 return 0;
633
634         /* Update feature count space */
635         l = strlen(*f) - d;
636         n =  MALLOC(l + 1);
637         if (!n)
638                 return 1;
639
640         /* Copy the feature count */
641         sprintf(n, "%0d", c);
642         /*
643          * Copy existing features up to the feature
644          * about to be removed
645          */
646         p = strchr(*f, ' ');
647         if (!p) {
648                 /* Internal error, feature string inconsistent */
649                 FREE(n);
650                 return 1;
651         }
652         while (*p == ' ')
653                 p++;
654         p--;
655         if (e != p) {
656                 do {
657                         e--;
658                         d++;
659                 } while (*e == ' ');
660                 e++; d--;
661                 strncat(n, p, (size_t)(e - p));
662                 p += (size_t)(e - p);
663         }
664         /* Skip feature to be removed */
665         p += d;
666
667         /* Copy remaining features */
668         if (strlen(p)) {
669                 while (*p == ' ')
670                         p++;
671                 if (strlen(p)) {
672                         p--;
673                         strcat(n, p);
674                 }
675         }
676
677 out:
678         FREE(*f);
679         *f = n;
680
681         return 0;
682 }