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