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