multipathd: move recovery mode code to function
[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 enter_recovery_mode(struct multipath *mpp)
311 {
312         struct config *conf = get_multipath_config();
313
314         /*
315          * Enter retry mode.
316          * meaning of +1: retry_tick may be decremented in checkerloop before
317          * starting retry.
318          */
319         mpp->stat_queueing_timeouts++;
320         mpp->retry_tick = mpp->no_path_retry * conf->checkint + 1;
321         condlog(1, "%s: Entering recovery mode: max_retries=%d",
322                 mpp->alias, mpp->no_path_retry);
323         put_multipath_config(conf);
324 }
325
326 static void set_no_path_retry(struct multipath *mpp)
327 {
328         mpp->retry_tick = 0;
329         mpp->nr_active = pathcount(mpp, PATH_UP) + pathcount(mpp, PATH_GHOST);
330
331         switch (mpp->no_path_retry) {
332         case NO_PATH_RETRY_UNDEF:
333                 break;
334         case NO_PATH_RETRY_FAIL:
335                 dm_queue_if_no_path(mpp->alias, 0);
336                 break;
337         case NO_PATH_RETRY_QUEUE:
338                 dm_queue_if_no_path(mpp->alias, 1);
339                 break;
340         default:
341                 dm_queue_if_no_path(mpp->alias, 1);
342                 if (mpp->nr_active == 0)
343                         enter_recovery_mode(mpp);
344                 break;
345         }
346 }
347
348 int __setup_multipath(struct vectors *vecs, struct multipath *mpp,
349                       int reset)
350 {
351         if (dm_get_info(mpp->alias, &mpp->dmi)) {
352                 /* Error accessing table */
353                 condlog(3, "%s: cannot access table", mpp->alias);
354                 goto out;
355         }
356
357         if (!dm_map_present(mpp->alias)) {
358                 /* Table has been removed */
359                 condlog(3, "%s: table does not exist", mpp->alias);
360                 goto out;
361         }
362
363         if (update_multipath_strings(mpp, vecs->pathvec, 1)) {
364                 condlog(0, "%s: failed to setup multipath", mpp->alias);
365                 goto out;
366         }
367
368         if (reset) {
369                 set_no_path_retry(mpp);
370                 if (VECTOR_SIZE(mpp->paths) != 0)
371                         dm_cancel_deferred_remove(mpp);
372         }
373
374         return 0;
375 out:
376         remove_map(mpp, vecs, PURGE_VEC);
377         return 1;
378 }
379
380 void
381 sync_map_state(struct multipath *mpp)
382 {
383         struct pathgroup *pgp;
384         struct path *pp;
385         unsigned int i, j;
386
387         if (!mpp->pg)
388                 return;
389
390         vector_foreach_slot (mpp->pg, pgp, i){
391                 vector_foreach_slot (pgp->paths, pp, j){
392                         if (pp->state == PATH_UNCHECKED ||
393                             pp->state == PATH_WILD ||
394                             pp->state == PATH_DELAYED)
395                                 continue;
396                         if (mpp->ghost_delay_tick > 0)
397                                 continue;
398                         if ((pp->dmstate == PSTATE_FAILED ||
399                              pp->dmstate == PSTATE_UNDEF) &&
400                             (pp->state == PATH_UP || pp->state == PATH_GHOST))
401                                 dm_reinstate_path(mpp->alias, pp->dev_t);
402                         else if ((pp->dmstate == PSTATE_ACTIVE ||
403                                   pp->dmstate == PSTATE_UNDEF) &&
404                                  (pp->state == PATH_DOWN ||
405                                   pp->state == PATH_SHAKY))
406                                 dm_fail_path(mpp->alias, pp->dev_t);
407                 }
408         }
409 }
410
411 int
412 update_map (struct multipath *mpp, struct vectors *vecs)
413 {
414         int retries = 3;
415         char params[PARAMS_SIZE] = {0};
416
417 retry:
418         condlog(4, "%s: updating new map", mpp->alias);
419         if (adopt_paths(vecs->pathvec, mpp)) {
420                 condlog(0, "%s: failed to adopt paths for new map update",
421                         mpp->alias);
422                 retries = -1;
423                 goto fail;
424         }
425         verify_paths(mpp, vecs);
426         mpp->action = ACT_RELOAD;
427
428         extract_hwe_from_path(mpp);
429         if (setup_map(mpp, params, PARAMS_SIZE)) {
430                 condlog(0, "%s: failed to setup new map in update", mpp->alias);
431                 retries = -1;
432                 goto fail;
433         }
434         if (domap(mpp, params, 1) <= 0 && retries-- > 0) {
435                 condlog(0, "%s: map_udate sleep", mpp->alias);
436                 sleep(1);
437                 goto retry;
438         }
439         dm_lib_release();
440
441 fail:
442         if (setup_multipath(vecs, mpp))
443                 return 1;
444
445         sync_map_state(mpp);
446
447         if (retries < 0)
448                 condlog(0, "%s: failed reload in new map update", mpp->alias);
449         return 0;
450 }
451
452 struct multipath *add_map_without_path (struct vectors *vecs, char *alias)
453 {
454         struct multipath * mpp = alloc_multipath();
455         struct config *conf;
456
457         if (!mpp)
458                 return NULL;
459         if (!alias) {
460                 FREE(mpp);
461                 return NULL;
462         }
463
464         mpp->alias = STRDUP(alias);
465
466         if (dm_get_info(mpp->alias, &mpp->dmi)) {
467                 condlog(3, "%s: cannot access table", mpp->alias);
468                 goto out;
469         }
470         set_multipath_wwid(mpp);
471         conf = get_multipath_config();
472         mpp->mpe = find_mpe(conf->mptable, mpp->wwid);
473         put_multipath_config(conf);
474
475         if (update_multipath_table(mpp, vecs->pathvec, 1))
476                 goto out;
477         if (update_multipath_status(mpp))
478                 goto out;
479
480         if (!vector_alloc_slot(vecs->mpvec))
481                 goto out;
482
483         vector_set_slot(vecs->mpvec, mpp);
484
485         if (update_map(mpp, vecs) != 0) /* map removed */
486                 return NULL;
487
488         if (start_waiter_thread(mpp, vecs))
489                 goto out;
490
491         return mpp;
492 out:
493         remove_map(mpp, vecs, PURGE_VEC);
494         return NULL;
495 }
496
497 static void
498 find_existing_alias (struct multipath * mpp,
499                      struct vectors *vecs)
500 {
501         struct multipath * mp;
502         int i;
503
504         vector_foreach_slot (vecs->mpvec, mp, i)
505                 if (strncmp(mp->wwid, mpp->wwid, WWID_SIZE - 1) == 0) {
506                         strncpy(mpp->alias_old, mp->alias, WWID_SIZE - 1);
507                         return;
508                 }
509 }
510
511 struct multipath *add_map_with_path(struct vectors *vecs, struct path *pp,
512                                     int add_vec)
513 {
514         struct multipath * mpp;
515         struct config *conf = NULL;
516
517         if (!strlen(pp->wwid))
518                 return NULL;
519
520         if (!(mpp = alloc_multipath()))
521                 return NULL;
522
523         conf = get_multipath_config();
524         mpp->mpe = find_mpe(conf->mptable, pp->wwid);
525         mpp->hwe = pp->hwe;
526         put_multipath_config(conf);
527
528         strcpy(mpp->wwid, pp->wwid);
529         find_existing_alias(mpp, vecs);
530         if (select_alias(conf, mpp))
531                 goto out;
532         mpp->size = pp->size;
533
534         if (adopt_paths(vecs->pathvec, mpp))
535                 goto out;
536
537         if (add_vec) {
538                 if (!vector_alloc_slot(vecs->mpvec))
539                         goto out;
540
541                 vector_set_slot(vecs->mpvec, mpp);
542         }
543
544         return mpp;
545
546 out:
547         remove_map(mpp, vecs, PURGE_VEC);
548         return NULL;
549 }
550
551 int verify_paths(struct multipath *mpp, struct vectors *vecs)
552 {
553         struct path * pp;
554         int count = 0;
555         int i, j;
556
557         if (!mpp)
558                 return 0;
559
560         vector_foreach_slot (mpp->paths, pp, i) {
561                 /*
562                  * see if path is in sysfs
563                  */
564                 if (sysfs_attr_get_value(pp->udev, "dev",
565                                          pp->dev_t, BLK_DEV_SIZE) < 0) {
566                         if (pp->state != PATH_DOWN) {
567                                 condlog(1, "%s: removing valid path %s in state %d",
568                                         mpp->alias, pp->dev, pp->state);
569                         } else {
570                                 condlog(3, "%s: failed to access path %s",
571                                         mpp->alias, pp->dev);
572                         }
573                         count++;
574                         vector_del_slot(mpp->paths, i);
575                         i--;
576
577                         if ((j = find_slot(vecs->pathvec,
578                                            (void *)pp)) != -1)
579                                 vector_del_slot(vecs->pathvec, j);
580                         free_path(pp);
581                 } else {
582                         condlog(4, "%s: verified path %s dev_t %s",
583                                 mpp->alias, pp->dev, pp->dev_t);
584                 }
585         }
586         return count;
587 }
588
589 int update_multipath (struct vectors *vecs, char *mapname, int reset)
590 {
591         struct multipath *mpp;
592         struct pathgroup  *pgp;
593         struct path *pp;
594         int i, j;
595
596         mpp = find_mp_by_alias(vecs->mpvec, mapname);
597
598         if (!mpp) {
599                 condlog(3, "%s: multipath map not found", mapname);
600                 return 2;
601         }
602
603         if (__setup_multipath(vecs, mpp, reset))
604                 return 1; /* mpp freed in setup_multipath */
605
606         /*
607          * compare checkers states with DM states
608          */
609         vector_foreach_slot (mpp->pg, pgp, i) {
610                 vector_foreach_slot (pgp->paths, pp, j) {
611                         if (pp->dmstate != PSTATE_FAILED)
612                                 continue;
613
614                         if (pp->state != PATH_DOWN) {
615                                 struct config *conf = get_multipath_config();
616                                 int oldstate = pp->state;
617                                 condlog(2, "%s: mark as failed", pp->dev);
618                                 mpp->stat_path_failures++;
619                                 pp->state = PATH_DOWN;
620                                 if (oldstate == PATH_UP ||
621                                     oldstate == PATH_GHOST)
622                                         update_queue_mode_del_path(mpp);
623
624                                 /*
625                                  * if opportune,
626                                  * schedule the next check earlier
627                                  */
628                                 if (pp->tick > conf->checkint)
629                                         pp->tick = conf->checkint;
630                                 put_multipath_config(conf);
631                         }
632                 }
633         }
634         return 0;
635 }
636
637 /*
638  * mpp->no_path_retry:
639  *   -2 (QUEUE) : queue_if_no_path enabled, never turned off
640  *   -1 (FAIL)  : fail_if_no_path
641  *    0 (UNDEF) : nothing
642  *   >0         : queue_if_no_path enabled, turned off after polling n times
643  */
644 void update_queue_mode_del_path(struct multipath *mpp)
645 {
646         if (--mpp->nr_active == 0) {
647                 if (mpp->no_path_retry > 0)
648                         enter_recovery_mode(mpp);
649                 else if (mpp->no_path_retry != NO_PATH_RETRY_QUEUE)
650                         mpp->stat_map_failures++;
651         }
652         condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
653 }
654
655 void update_queue_mode_add_path(struct multipath *mpp)
656 {
657         if (mpp->nr_active++ == 0 && mpp->no_path_retry > 0) {
658                 /* come back to normal mode from retry mode */
659                 mpp->retry_tick = 0;
660                 dm_queue_if_no_path(mpp->alias, 1);
661                 condlog(2, "%s: queue_if_no_path enabled", mpp->alias);
662                 condlog(1, "%s: Recovered to normal mode", mpp->alias);
663         }
664         condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
665 }