adb1911e0b8c5b1355f4475945f5711c9cf19fd2
[multipath-tools/.git] / libmultipath / structs_vec.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <unistd.h>
4
5 #include "checkers.h"
6 #include "vector.h"
7 #include "defaults.h"
8 #include "debug.h"
9 #include "config.h"
10 #include "structs.h"
11 #include "structs_vec.h"
12 #include "sysfs.h"
13 #include "waiter.h"
14 #include "devmapper.h"
15 #include "dmparser.h"
16 #include "propsel.h"
17 #include "discovery.h"
18 #include "prio.h"
19
20 /*
21  * creates or updates mpp->paths reading mpp->pg
22  */
23 extern int
24 update_mpp_paths(struct multipath * mpp, vector pathvec)
25 {
26         struct pathgroup * pgp;
27         struct path * pp;
28         int i,j;
29
30         if (!mpp || !mpp->pg)
31                 return 0;
32
33         if (!mpp->paths &&
34             !(mpp->paths = vector_alloc()))
35                 return 1;
36
37         vector_foreach_slot (mpp->pg, pgp, i) {
38                 vector_foreach_slot (pgp->paths, pp, j) {
39                         if (!find_path_by_devt(mpp->paths, pp->dev_t) &&
40                             (find_path_by_devt(pathvec, pp->dev_t)) &&
41                             store_path(mpp->paths, pp))
42                                 return 1;
43                 }
44         }
45         return 0;
46 }
47
48 extern int
49 adopt_paths (vector pathvec, struct multipath * mpp)
50 {
51         int i, ret;
52         struct path * pp;
53         struct config *conf;
54
55         if (!mpp)
56                 return 0;
57
58         if (update_mpp_paths(mpp, pathvec))
59                 return 1;
60
61         vector_foreach_slot (pathvec, pp, i) {
62                 if (!strncmp(mpp->wwid, pp->wwid, WWID_SIZE)) {
63                         condlog(3, "%s: ownership set to %s",
64                                 pp->dev, mpp->alias);
65                         pp->mpp = mpp;
66
67                         if (!mpp->paths && !(mpp->paths = vector_alloc()))
68                                 return 1;
69
70                         if (!find_path_by_dev(mpp->paths, pp->dev) &&
71                             store_path(mpp->paths, pp))
72                                         return 1;
73                         conf = get_multipath_config();
74                         ret = pathinfo(pp, conf,
75                                        DI_PRIO | DI_CHECKER);
76                         put_multipath_config(conf);
77                         if (ret)
78                                 return 1;
79                 }
80         }
81         return 0;
82 }
83
84 extern void
85 orphan_path (struct path * pp, const char *reason)
86 {
87         condlog(3, "%s: orphan path, %s", pp->dev, reason);
88         pp->mpp = NULL;
89         pp->dmstate = PSTATE_UNDEF;
90         pp->uid_attribute = NULL;
91         pp->getuid = NULL;
92         prio_put(&pp->prio);
93         checker_put(&pp->checker);
94         if (pp->fd >= 0)
95                 close(pp->fd);
96         pp->fd = -1;
97 }
98
99 extern void
100 orphan_paths (vector pathvec, struct multipath * mpp)
101 {
102         int i;
103         struct path * pp;
104
105         vector_foreach_slot (pathvec, pp, i) {
106                 if (pp->mpp == mpp) {
107                         orphan_path(pp, "map flushed");
108                 }
109         }
110 }
111
112 static void
113 set_multipath_wwid (struct multipath * mpp)
114 {
115         if (strlen(mpp->wwid))
116                 return;
117
118         dm_get_uuid(mpp->alias, mpp->wwid);
119 }
120
121 #define KEEP_WAITER 0
122 #define STOP_WAITER 1
123 #define PURGE_VEC 1
124
125 static void
126 _remove_map (struct multipath * mpp, struct vectors * vecs,
127             int stop_waiter, int purge_vec)
128 {
129         int i;
130
131         condlog(4, "%s: remove multipath map", mpp->alias);
132
133         /*
134          * stop the DM event waiter thread
135          */
136         if (stop_waiter)
137                 stop_waiter_thread(mpp, vecs);
138
139         /*
140          * clear references to this map
141          */
142         orphan_paths(vecs->pathvec, mpp);
143
144         if (purge_vec &&
145             (i = find_slot(vecs->mpvec, (void *)mpp)) != -1)
146                 vector_del_slot(vecs->mpvec, i);
147
148         /*
149          * final free
150          */
151         free_multipath(mpp, KEEP_PATHS);
152 }
153
154 extern void
155 remove_map (struct multipath * mpp, struct vectors * vecs, int purge_vec)
156 {
157         _remove_map(mpp, vecs, KEEP_WAITER, purge_vec);
158 }
159
160 extern void
161 remove_map_and_stop_waiter (struct multipath * mpp, struct vectors * vecs,
162                             int purge_vec)
163 {
164         _remove_map(mpp, vecs, STOP_WAITER, purge_vec);
165 }
166
167 static void
168 _remove_maps (struct vectors * vecs, int stop_waiter)
169 {
170         int i;
171         struct multipath * mpp;
172
173         if (!vecs)
174                 return;
175
176         vector_foreach_slot (vecs->mpvec, mpp, i) {
177                 _remove_map(mpp, vecs, stop_waiter, 1);
178                 i--;
179         }
180
181         vector_free(vecs->mpvec);
182         vecs->mpvec = NULL;
183 }
184
185 extern void
186 remove_maps (struct vectors * vecs)
187 {
188         _remove_maps(vecs, KEEP_WAITER);
189 }
190
191 extern void
192 remove_maps_and_stop_waiters (struct vectors * vecs)
193 {
194         _remove_maps(vecs, STOP_WAITER);
195 }
196
197 static struct hwentry *
198 extract_hwe_from_path(struct multipath * mpp)
199 {
200         struct path * pp = NULL;
201         int pg_num = -1, p_num = -1, i;
202         struct pathgroup * pgp = NULL;
203
204         condlog(3, "%s: searching paths for valid hwe", mpp->alias);
205
206         if (mpp && mpp->pg) {
207                 vector_foreach_slot(mpp->pg, pgp, i) {
208                         if (pgp->status == PGSTATE_ACTIVE ||
209                             pgp->status == PGSTATE_ENABLED) {
210                                 pg_num = i;
211                                 break;
212                         }
213                 }
214                 if (pg_num >= 0)
215                         pgp = VECTOR_SLOT(mpp->pg, pg_num);
216         }
217
218         if (pgp && pgp->paths) {
219                 vector_foreach_slot(pgp->paths, pp, i) {
220                         if (pp->dmstate == PSTATE_FAILED)
221                                 continue;
222                         if (strlen(pp->vendor_id) > 0 &&
223                             strlen(pp->product_id) > 0 &&
224                             strlen(pp->rev) > 0) {
225                                 p_num = i;
226                                 break;
227                         }
228                 }
229                 if (p_num >= 0)
230                         pp = VECTOR_SLOT(pgp->paths, i);
231         }
232
233         if (pp) {
234                 if (!strlen(pp->vendor_id) ||
235                     !strlen(pp->product_id) ||
236                     !strlen(pp->rev)) {
237                         condlog(3, "%s: no device details available", pp->dev);
238                         return NULL;
239                 }
240                 condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
241                 condlog(3, "%s: product = %s", pp->dev, pp->product_id);
242                 condlog(3, "%s: rev = %s", pp->dev, pp->rev);
243                 if (!pp->hwe) {
244                         struct config *conf = get_multipath_config();
245
246                         condlog(3, "searching hwtable");
247                         pp->hwe = find_hwe(conf->hwtable, pp->vendor_id,
248                                            pp->product_id, pp->rev);
249                         put_multipath_config(conf);
250                 }
251         }
252
253         return pp?pp->hwe:NULL;
254 }
255
256 static int
257 update_multipath_table (struct multipath *mpp, vector pathvec, int is_daemon)
258 {
259         char params[PARAMS_SIZE] = {0};
260
261         if (!mpp)
262                 return 1;
263
264         if (dm_get_map(mpp->alias, &mpp->size, params)) {
265                 condlog(3, "%s: cannot get map", mpp->alias);
266                 return 1;
267         }
268
269         if (disassemble_map(pathvec, params, mpp, is_daemon)) {
270                 condlog(3, "%s: cannot disassemble map", mpp->alias);
271                 return 1;
272         }
273
274         return 0;
275 }
276
277 static int
278 update_multipath_status (struct multipath *mpp)
279 {
280         char status[PARAMS_SIZE] = {0};
281
282         if (!mpp)
283                 return 1;
284
285         if (dm_get_status(mpp->alias, status)) {
286                 condlog(3, "%s: cannot get status", mpp->alias);
287                 return 1;
288         }
289
290         if (disassemble_status(status, mpp)) {
291                 condlog(3, "%s: cannot disassemble status", mpp->alias);
292                 return 1;
293         }
294
295         return 0;
296 }
297
298 void sync_paths(struct multipath *mpp, vector pathvec)
299 {
300         struct path *pp;
301         struct pathgroup  *pgp;
302         int found, i, j;
303
304         vector_foreach_slot (mpp->paths, pp, i) {
305                 found = 0;
306                 vector_foreach_slot(mpp->pg, pgp, j) {
307                         if (find_slot(pgp->paths, (void *)pp) != -1) {
308                                 found = 1;
309                                 break;
310                         }
311                 }
312                 if (!found) {
313                         condlog(3, "%s dropped path %s", mpp->alias, pp->dev);
314                         vector_del_slot(mpp->paths, i--);
315                         orphan_path(pp, "path removed externally");
316                 }
317         }
318         update_mpp_paths(mpp, pathvec);
319         vector_foreach_slot (mpp->paths, pp, i)
320                 pp->mpp = mpp;
321 }
322
323 extern int
324 update_multipath_strings (struct multipath *mpp, vector pathvec, int is_daemon)
325 {
326         if (!mpp)
327                 return 1;
328
329         update_mpp_paths(mpp, pathvec);
330         condlog(4, "%s: %s", mpp->alias, __FUNCTION__);
331
332         free_multipath_attributes(mpp);
333         free_pgvec(mpp->pg, KEEP_PATHS);
334         mpp->pg = NULL;
335
336         if (update_multipath_table(mpp, pathvec, is_daemon))
337                 return 1;
338         sync_paths(mpp, pathvec);
339
340         if (update_multipath_status(mpp))
341                 return 1;
342
343         return 0;
344 }
345
346 extern void
347 set_no_path_retry(struct config *conf, struct multipath *mpp)
348 {
349         mpp->retry_tick = 0;
350         mpp->nr_active = pathcount(mpp, PATH_UP) + pathcount(mpp, PATH_GHOST);
351         select_no_path_retry(conf, mpp);
352
353         switch (mpp->no_path_retry) {
354         case NO_PATH_RETRY_UNDEF:
355                 break;
356         case NO_PATH_RETRY_FAIL:
357                 dm_queue_if_no_path(mpp->alias, 0);
358                 break;
359         case NO_PATH_RETRY_QUEUE:
360                 dm_queue_if_no_path(mpp->alias, 1);
361                 break;
362         default:
363                 dm_queue_if_no_path(mpp->alias, 1);
364                 if (mpp->nr_active == 0) {
365                         struct config *conf = get_multipath_config();
366                         /* Enter retry mode */
367                         mpp->retry_tick = mpp->no_path_retry * conf->checkint;
368                         condlog(1, "%s: Entering recovery mode: max_retries=%d",
369                                 mpp->alias, mpp->no_path_retry);
370                         put_multipath_config(conf);
371                 }
372                 break;
373         }
374 }
375
376 extern int
377 __setup_multipath (struct vectors * vecs, struct multipath * mpp,
378                    int reset, int is_daemon)
379 {
380         struct config *conf;
381
382         if (dm_get_info(mpp->alias, &mpp->dmi)) {
383                 /* Error accessing table */
384                 condlog(3, "%s: cannot access table", mpp->alias);
385                 goto out;
386         }
387
388         if (!dm_map_present(mpp->alias)) {
389                 /* Table has been removed */
390                 condlog(3, "%s: table does not exist", mpp->alias);
391                 goto out;
392         }
393
394         if (update_multipath_strings(mpp, vecs->pathvec, is_daemon)) {
395                 condlog(0, "%s: failed to setup multipath", mpp->alias);
396                 goto out;
397         }
398
399         set_multipath_wwid(mpp);
400         conf = get_multipath_config();
401         mpp->mpe = find_mpe(conf->mptable, mpp->wwid);
402         put_multipath_config(conf);
403         condlog(3, "%s: discover", mpp->alias);
404
405         if (!mpp->hwe)
406                 mpp->hwe = extract_hwe_from_path(mpp);
407         if (!mpp->hwe) {
408                 condlog(3, "%s: no hardware entry found, using defaults",
409                         mpp->alias);
410         }
411         if (reset) {
412                 select_rr_weight(conf, mpp);
413                 select_pgfailback(conf, mpp);
414                 set_no_path_retry(conf, mpp);
415                 select_flush_on_last_del(conf, mpp);
416                 if (VECTOR_SIZE(mpp->paths) != 0)
417                         dm_cancel_deferred_remove(mpp);
418         }
419
420         return 0;
421 out:
422         remove_map(mpp, vecs, PURGE_VEC);
423         return 1;
424 }
425
426 extern struct multipath *
427 add_map_without_path (struct vectors * vecs, char * alias)
428 {
429         struct multipath * mpp = alloc_multipath();
430
431         if (!mpp)
432                 return NULL;
433         if (!alias) {
434                 FREE(mpp);
435                 return NULL;
436         }
437
438         mpp->alias = STRDUP(alias);
439
440         if (setup_multipath(vecs, mpp))
441                 return NULL; /* mpp freed in setup_multipath */
442
443         if (adopt_paths(vecs->pathvec, mpp))
444                 goto out;
445
446         if (!vector_alloc_slot(vecs->mpvec))
447                 goto out;
448
449         vector_set_slot(vecs->mpvec, mpp);
450
451         if (start_waiter_thread(mpp, vecs))
452                 goto out;
453
454         return mpp;
455 out:
456         remove_map(mpp, vecs, PURGE_VEC);
457         return NULL;
458 }
459
460 static void
461 find_existing_alias (struct multipath * mpp,
462                      struct vectors *vecs)
463 {
464         struct multipath * mp;
465         int i;
466
467         vector_foreach_slot (vecs->mpvec, mp, i)
468                 if (strncmp(mp->wwid, mpp->wwid, WWID_SIZE - 1) == 0) {
469                         strncpy(mpp->alias_old, mp->alias, WWID_SIZE - 1);
470                         return;
471                 }
472 }
473
474 extern struct multipath *
475 add_map_with_path (struct vectors * vecs,
476                    struct path * pp, int add_vec)
477 {
478         struct multipath * mpp;
479         struct config *conf = NULL;
480
481         if (!strlen(pp->wwid))
482                 return NULL;
483
484         if (!(mpp = alloc_multipath()))
485                 return NULL;
486
487         conf = get_multipath_config();
488         mpp->mpe = find_mpe(conf->mptable, pp->wwid);
489         mpp->hwe = pp->hwe;
490         put_multipath_config(conf);
491
492         strcpy(mpp->wwid, pp->wwid);
493         find_existing_alias(mpp, vecs);
494         if (select_alias(conf, mpp))
495                 goto out;
496         mpp->size = pp->size;
497
498         if (adopt_paths(vecs->pathvec, mpp))
499                 goto out;
500
501         if (add_vec) {
502                 if (!vector_alloc_slot(vecs->mpvec))
503                         goto out;
504
505                 vector_set_slot(vecs->mpvec, mpp);
506         }
507
508         return mpp;
509
510 out:
511         remove_map(mpp, vecs, PURGE_VEC);
512         return NULL;
513 }
514
515 extern int
516 verify_paths(struct multipath * mpp, struct vectors * vecs)
517 {
518         struct path * pp;
519         int count = 0;
520         int i, j;
521
522         if (!mpp)
523                 return 0;
524
525         vector_foreach_slot (mpp->paths, pp, i) {
526                 /*
527                  * see if path is in sysfs
528                  */
529                 if (sysfs_attr_get_value(pp->udev, "dev",
530                                          pp->dev_t, BLK_DEV_SIZE) < 0) {
531                         if (pp->state != PATH_DOWN) {
532                                 condlog(1, "%s: removing valid path %s in state %d",
533                                         mpp->alias, pp->dev, pp->state);
534                         } else {
535                                 condlog(3, "%s: failed to access path %s",
536                                         mpp->alias, pp->dev);
537                         }
538                         count++;
539                         vector_del_slot(mpp->paths, i);
540                         i--;
541
542                         if ((j = find_slot(vecs->pathvec,
543                                            (void *)pp)) != -1)
544                                 vector_del_slot(vecs->pathvec, j);
545                         free_path(pp);
546                 } else {
547                         condlog(4, "%s: verified path %s dev_t %s",
548                                 mpp->alias, pp->dev, pp->dev_t);
549                 }
550         }
551         return count;
552 }
553
554 int update_multipath (struct vectors *vecs, char *mapname, int reset)
555 {
556         struct multipath *mpp;
557         struct pathgroup  *pgp;
558         struct path *pp;
559         int i, j;
560
561         mpp = find_mp_by_alias(vecs->mpvec, mapname);
562
563         if (!mpp) {
564                 condlog(3, "%s: multipath map not found", mapname);
565                 return 2;
566         }
567
568         if (__setup_multipath(vecs, mpp, reset, 1))
569                 return 1; /* mpp freed in setup_multipath */
570
571         /*
572          * compare checkers states with DM states
573          */
574         vector_foreach_slot (mpp->pg, pgp, i) {
575                 vector_foreach_slot (pgp->paths, pp, j) {
576                         if (pp->dmstate != PSTATE_FAILED)
577                                 continue;
578
579                         if (pp->state != PATH_DOWN) {
580                                 struct config *conf = get_multipath_config();
581                                 int oldstate = pp->state;
582                                 condlog(2, "%s: mark as failed", pp->dev);
583                                 mpp->stat_path_failures++;
584                                 pp->state = PATH_DOWN;
585                                 if (oldstate == PATH_UP ||
586                                     oldstate == PATH_GHOST)
587                                         update_queue_mode_del_path(mpp);
588
589                                 /*
590                                  * if opportune,
591                                  * schedule the next check earlier
592                                  */
593                                 if (pp->tick > conf->checkint)
594                                         pp->tick = conf->checkint;
595                                 put_multipath_config(conf);
596                         }
597                 }
598         }
599         return 0;
600 }
601
602 /*
603  * mpp->no_path_retry:
604  *   -2 (QUEUE) : queue_if_no_path enabled, never turned off
605  *   -1 (FAIL)  : fail_if_no_path
606  *    0 (UNDEF) : nothing
607  *   >0         : queue_if_no_path enabled, turned off after polling n times
608  */
609 void update_queue_mode_del_path(struct multipath *mpp)
610 {
611         if (--mpp->nr_active == 0 && mpp->no_path_retry > 0) {
612                 struct config *conf = get_multipath_config();
613
614                 /*
615                  * Enter retry mode.
616                  * meaning of +1: retry_tick may be decremented in
617                  *                checkerloop before starting retry.
618                  */
619                 mpp->stat_queueing_timeouts++;
620                 mpp->retry_tick = mpp->no_path_retry * conf->checkint + 1;
621                 condlog(1, "%s: Entering recovery mode: max_retries=%d",
622                         mpp->alias, mpp->no_path_retry);
623                 put_multipath_config(conf);
624         }
625         condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
626 }
627
628 void update_queue_mode_add_path(struct multipath *mpp)
629 {
630         if (mpp->nr_active++ == 0 && mpp->no_path_retry > 0) {
631                 /* come back to normal mode from retry mode */
632                 mpp->retry_tick = 0;
633                 dm_queue_if_no_path(mpp->alias, 1);
634                 condlog(2, "%s: queue_if_no_path enabled", mpp->alias);
635                 condlog(1, "%s: Recovered to normal mode", mpp->alias);
636         }
637         condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
638 }