multipath: add "ghost_delay" parameter
[multipath-tools/.git] / libmultipath / configure.c
1 /*
2  * Copyright (c) 2003, 2004, 2005 Christophe Varoqui
3  * Copyright (c) 2005 Benjamin Marzinski, Redhat
4  * Copyright (c) 2005 Kiyoshi Ueda, NEC
5  * Copyright (c) 2005 Patrick Caulfield, Redhat
6  * Copyright (c) 2005 Edward Goggin, EMC
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <string.h>
13 #include <sys/file.h>
14 #include <errno.h>
15 #include <libdevmapper.h>
16 #include <libudev.h>
17 #include "mpath_cmd.h"
18
19 #include "checkers.h"
20 #include "vector.h"
21 #include "memory.h"
22 #include "devmapper.h"
23 #include "defaults.h"
24 #include "structs.h"
25 #include "structs_vec.h"
26 #include "dmparser.h"
27 #include "config.h"
28 #include "blacklist.h"
29 #include "propsel.h"
30 #include "discovery.h"
31 #include "debug.h"
32 #include "switchgroup.h"
33 #include "print.h"
34 #include "configure.h"
35 #include "pgpolicies.h"
36 #include "dict.h"
37 #include "alias.h"
38 #include "prio.h"
39 #include "util.h"
40 #include "uxsock.h"
41 #include "wwids.h"
42 #include "sysfs.h"
43
44 /* group paths in pg by host adapter
45  */
46 int group_by_host_adapter(struct pathgroup *pgp, vector adapters)
47 {
48         struct adapter_group *agp;
49         struct host_group *hgp;
50         struct path *pp, *pp1;
51         char adapter_name1[SLOT_NAME_SIZE];
52         char adapter_name2[SLOT_NAME_SIZE];
53         int i, j;
54         int found_hostgroup = 0;
55
56         while (VECTOR_SIZE(pgp->paths) > 0) {
57
58                 pp = VECTOR_SLOT(pgp->paths, 0);
59
60                 if (sysfs_get_host_adapter_name(pp, adapter_name1))
61                         goto out;
62                 /* create a new host adapter group
63                  */
64                 agp = alloc_adaptergroup();
65                 if (!agp)
66                         goto out;
67                 agp->pgp = pgp;
68
69                 strncpy(agp->adapter_name, adapter_name1, SLOT_NAME_SIZE - 1);
70                 store_adaptergroup(adapters, agp);
71
72                 /* create a new host port group
73                  */
74                 hgp = alloc_hostgroup();
75                 if (!hgp)
76                         goto out;
77                 if (store_hostgroup(agp->host_groups, hgp))
78                         goto out;
79
80                 hgp->host_no = pp->sg_id.host_no;
81                 agp->num_hosts++;
82                 if (store_path(hgp->paths, pp))
83                         goto out;
84
85                 hgp->num_paths++;
86                 /* delete path from path group
87                  */
88                 vector_del_slot(pgp->paths, 0);
89
90                 /* add all paths belonging to same host adapter
91                  */
92                 vector_foreach_slot(pgp->paths, pp1, i) {
93                         if (sysfs_get_host_adapter_name(pp1, adapter_name2))
94                                 goto out;
95                         if (strcmp(adapter_name1, adapter_name2) == 0) {
96                                 found_hostgroup = 0;
97                                 vector_foreach_slot(agp->host_groups, hgp, j) {
98                                         if (hgp->host_no == pp1->sg_id.host_no) {
99                                                 if (store_path(hgp->paths, pp1))
100                                                         goto out;
101                                                 hgp->num_paths++;
102                                                 found_hostgroup = 1;
103                                                 break;
104                                         }
105                                 }
106                                 if (!found_hostgroup) {
107                                         /* this path belongs to new host port
108                                          * within this adapter
109                                          */
110                                         hgp = alloc_hostgroup();
111                                         if (!hgp)
112                                                 goto out;
113
114                                         if (store_hostgroup(agp->host_groups, hgp))
115                                                 goto out;
116
117                                         agp->num_hosts++;
118                                         if (store_path(hgp->paths, pp1))
119                                                 goto out;
120
121                                         hgp->host_no = pp1->sg_id.host_no;
122                                         hgp->num_paths++;
123                                 }
124                                 /* delete paths from original path_group
125                                  * as they are added into adapter group now
126                                  */
127                                 vector_del_slot(pgp->paths, i);
128                                 i--;
129                         }
130                 }
131         }
132         return 0;
133
134 out:    /* add back paths into pg as re-ordering failed
135          */
136         vector_foreach_slot(adapters, agp, i) {
137                         vector_foreach_slot(agp->host_groups, hgp, j) {
138                                 while (VECTOR_SIZE(hgp->paths) > 0) {
139                                         pp = VECTOR_SLOT(hgp->paths, 0);
140                                         if (store_path(pgp->paths, pp))
141                                                 condlog(3, "failed to restore "
142                                                 "path %s into path group",
143                                                  pp->dev);
144                                         vector_del_slot(hgp->paths, 0);
145                                 }
146                         }
147                 }
148         free_adaptergroup(adapters);
149         return 1;
150 }
151
152 /* re-order paths in pg by alternating adapters and host ports
153  * for optimized selection
154  */
155 int order_paths_in_pg_by_alt_adapters(struct pathgroup *pgp, vector adapters,
156                  int total_paths)
157 {
158         int next_adapter_index = 0;
159         struct adapter_group *agp;
160         struct host_group *hgp;
161         struct path *pp;
162
163         while (total_paths > 0) {
164                 agp = VECTOR_SLOT(adapters, next_adapter_index);
165                 if (!agp) {
166                         condlog(0, "can't get adapter group %d", next_adapter_index);
167                         return 1;
168                 }
169
170                 hgp = VECTOR_SLOT(agp->host_groups, agp->next_host_index);
171                 if (!hgp) {
172                         condlog(0, "can't get host group %d of adapter group %d", next_adapter_index, agp->next_host_index);
173                         return 1;
174                 }
175
176                 if (!hgp->num_paths) {
177                         agp->next_host_index++;
178                         agp->next_host_index %= agp->num_hosts;
179                         next_adapter_index++;
180                         next_adapter_index %= VECTOR_SIZE(adapters);
181                         continue;
182                 }
183
184                 pp  = VECTOR_SLOT(hgp->paths, 0);
185
186                 if (store_path(pgp->paths, pp))
187                         return 1;
188
189                 total_paths--;
190
191                 vector_del_slot(hgp->paths, 0);
192
193                 hgp->num_paths--;
194
195                 agp->next_host_index++;
196                 agp->next_host_index %= agp->num_hosts;
197                 next_adapter_index++;
198                 next_adapter_index %= VECTOR_SIZE(adapters);
199         }
200
201         /* all paths are added into path_group
202          * in crafted child order
203          */
204         return 0;
205 }
206
207 /* round-robin: order paths in path group to alternate
208  * between all host adapters
209  */
210 int rr_optimize_path_order(struct pathgroup *pgp)
211 {
212         vector adapters;
213         struct path *pp;
214         int total_paths;
215         int i;
216
217         total_paths = VECTOR_SIZE(pgp->paths);
218         vector_foreach_slot(pgp->paths, pp, i) {
219                 if (pp->sg_id.proto_id != SCSI_PROTOCOL_FCP &&
220                         pp->sg_id.proto_id != SCSI_PROTOCOL_SAS &&
221                         pp->sg_id.proto_id != SCSI_PROTOCOL_ISCSI &&
222                         pp->sg_id.proto_id != SCSI_PROTOCOL_SRP) {
223                         /* return success as default path order
224                          * is maintained in path group
225                          */
226                         return 0;
227                 }
228         }
229         adapters = vector_alloc();
230         if (!adapters)
231                 return 0;
232
233         /* group paths in path group by host adapters
234          */
235         if (group_by_host_adapter(pgp, adapters)) {
236                 /* already freed adapters */
237                 condlog(3, "Failed to group paths by adapters");
238                 return 0;
239         }
240
241         /* re-order paths in pg to alternate between adapters and host ports
242          */
243         if (order_paths_in_pg_by_alt_adapters(pgp, adapters, total_paths)) {
244                 condlog(3, "Failed to re-order paths in pg by adapters "
245                         "and host ports");
246                 free_adaptergroup(adapters);
247                 /* return failure as original paths are
248                  * removed form pgp
249                  */
250                 return 1;
251         }
252
253         free_adaptergroup(adapters);
254         return 0;
255 }
256
257 int setup_map(struct multipath *mpp, char *params, int params_size)
258 {
259         struct pathgroup * pgp;
260         struct config *conf;
261         int i;
262
263         /*
264          * don't bother if devmap size is unknown
265          */
266         if (mpp->size <= 0) {
267                 condlog(3, "%s: devmap size is unknown", mpp->alias);
268                 return 1;
269         }
270
271         /*
272          * free features, selector, and hwhandler properties if they are being reused
273          */
274         free_multipath_attributes(mpp);
275
276         /*
277          * properties selectors
278          */
279         conf = get_multipath_config();
280         select_pgfailback(conf, mpp);
281         select_pgpolicy(conf, mpp);
282         select_selector(conf, mpp);
283         select_hwhandler(conf, mpp);
284         select_no_path_retry(conf, mpp);
285         select_retain_hwhandler(conf, mpp);
286         select_features(conf, mpp);
287         select_rr_weight(conf, mpp);
288         select_minio(conf, mpp);
289         select_mode(conf, mpp);
290         select_uid(conf, mpp);
291         select_gid(conf, mpp);
292         select_fast_io_fail(conf, mpp);
293         select_dev_loss(conf, mpp);
294         select_reservation_key(conf, mpp);
295         select_deferred_remove(conf, mpp);
296         select_delay_watch_checks(conf, mpp);
297         select_delay_wait_checks(conf, mpp);
298         select_marginal_path_err_sample_time(conf, mpp);
299         select_marginal_path_err_rate_threshold(conf, mpp);
300         select_marginal_path_err_recheck_gap_time(conf, mpp);
301         select_marginal_path_double_failed_time(conf, mpp);
302         select_skip_kpartx(conf, mpp);
303         select_max_sectors_kb(conf, mpp);
304         select_ghost_delay(conf, mpp);
305
306         sysfs_set_scsi_tmo(mpp, conf->checkint);
307         put_multipath_config(conf);
308         /*
309          * assign paths to path groups -- start with no groups and all paths
310          * in mpp->paths
311          */
312         if (mpp->pg) {
313                 vector_foreach_slot (mpp->pg, pgp, i)
314                         free_pathgroup(pgp, KEEP_PATHS);
315
316                 vector_free(mpp->pg);
317                 mpp->pg = NULL;
318         }
319         if (mpp->pgpolicyfn && mpp->pgpolicyfn(mpp))
320                 return 1;
321
322         mpp->nr_active = pathcount(mpp, PATH_UP) + pathcount(mpp, PATH_GHOST);
323
324         /*
325          * ponders each path group and determine highest prio pg
326          * to switch over (default to first)
327          */
328         mpp->bestpg = select_path_group(mpp);
329
330         /* re-order paths in all path groups in an optimized way
331          * for round-robin path selectors to get maximum throughput.
332          */
333         if (!strncmp(mpp->selector, "round-robin", 11)) {
334                 vector_foreach_slot(mpp->pg, pgp, i) {
335                         if (VECTOR_SIZE(pgp->paths) <= 2)
336                                 continue;
337                         if (rr_optimize_path_order(pgp)) {
338                                 condlog(2, "cannot re-order paths for "
339                                         "optimization: %s",
340                                         mpp->alias);
341                                 return 1;
342                         }
343                 }
344         }
345
346         /*
347          * transform the mp->pg vector of vectors of paths
348          * into a mp->params strings to feed the device-mapper
349          */
350         if (assemble_map(mpp, params, params_size)) {
351                 condlog(0, "%s: problem assembing map", mpp->alias);
352                 return 1;
353         }
354         return 0;
355 }
356
357 static void
358 compute_pgid(struct pathgroup * pgp)
359 {
360         struct path * pp;
361         int i;
362
363         vector_foreach_slot (pgp->paths, pp, i)
364                 pgp->id ^= (long)pp;
365 }
366
367 static int
368 pgcmp (struct multipath * mpp, struct multipath * cmpp)
369 {
370         int i, j;
371         struct pathgroup * pgp;
372         struct pathgroup * cpgp;
373         int r = 0;
374
375         if (!mpp)
376                 return 0;
377
378         vector_foreach_slot (mpp->pg, pgp, i) {
379                 compute_pgid(pgp);
380
381                 vector_foreach_slot (cmpp->pg, cpgp, j) {
382                         if (pgp->id == cpgp->id &&
383                             !pathcmp(pgp, cpgp)) {
384                                 r = 0;
385                                 break;
386                         }
387                         r++;
388                 }
389                 if (r)
390                         return r;
391         }
392         return r;
393 }
394
395 static struct udev_device *
396 get_udev_for_mpp(const struct multipath *mpp)
397 {
398         dev_t devnum;
399         struct udev_device *udd;
400
401         if (!mpp || !mpp->dmi) {
402                 condlog(1, "%s called with empty mpp", __func__);
403                 return NULL;
404         }
405
406         devnum = makedev(mpp->dmi->major, mpp->dmi->minor);
407         udd = udev_device_new_from_devnum(udev, 'b', devnum);
408         if (!udd) {
409                 condlog(1, "failed to get udev device for %s", mpp->alias);
410                 return NULL;
411         }
412         return udd;
413 }
414
415 static void
416 trigger_udev_change(const struct multipath *mpp)
417 {
418         static const char change[] = "change";
419         struct udev_device *udd = get_udev_for_mpp(mpp);
420         if (!udd)
421                 return;
422         condlog(3, "triggering %s uevent for %s", change, mpp->alias);
423         sysfs_attr_set_value(udd, "uevent", change, sizeof(change)-1);
424         udev_device_unref(udd);
425 }
426
427 static int
428 is_mpp_known_to_udev(const struct multipath *mpp)
429 {
430         struct udev_device *udd = get_udev_for_mpp(mpp);
431         int ret = (udd != NULL);
432         udev_device_unref(udd);
433         return ret;
434 }
435
436 static int
437 sysfs_set_max_sectors_kb(struct multipath *mpp, int is_reload)
438 {
439         struct pathgroup * pgp;
440         struct path *pp;
441         char buff[11];
442         int i, j, ret, err = 0;
443         struct udev_device *udd;
444         int max_sectors_kb;
445
446         if (mpp->max_sectors_kb == MAX_SECTORS_KB_UNDEF)
447                 return 0;
448         max_sectors_kb = mpp->max_sectors_kb;
449         if (is_reload) {
450                 if (!mpp->dmi && dm_get_info(mpp->alias, &mpp->dmi) != 0) {
451                         condlog(1, "failed to get dm info for %s", mpp->alias);
452                         return 1;
453                 }
454                 udd = get_udev_for_mpp(mpp);
455                 if (!udd) {
456                         condlog(1, "failed to get udev device to set max_sectors_kb for %s", mpp->alias);
457                         return 1;
458                 }
459                 ret = sysfs_attr_get_value(udd, "queue/max_sectors_kb", buff,
460                                            sizeof(buff));
461                 udev_device_unref(udd);
462                 if (ret <= 0) {
463                         condlog(1, "failed to get current max_sectors_kb from %s", mpp->alias);
464                         return 1;
465                 }
466                 if (sscanf(buff, "%u\n", &max_sectors_kb) != 1) {
467                         condlog(1, "can't parse current max_sectors_kb from %s",
468                                 mpp->alias);
469                         return 1;
470                 }
471         }
472         snprintf(buff, 11, "%d", max_sectors_kb);
473
474         vector_foreach_slot (mpp->pg, pgp, i) {
475                 vector_foreach_slot(pgp->paths, pp, j) {
476                         ret = sysfs_attr_set_value(pp->udev,
477                                                    "queue/max_sectors_kb",
478                                                    buff, strlen(buff));
479                         if (ret < 0) {
480                                 condlog(1, "failed setting max_sectors_kb on %s : %s", pp->dev, strerror(-ret));
481                                 err = 1;
482                         }
483                 }
484         }
485         return err;
486 }
487
488 static void
489 select_action (struct multipath * mpp, vector curmp, int force_reload)
490 {
491         struct multipath * cmpp;
492         struct multipath * cmpp_by_name;
493         char * mpp_feat, * cmpp_feat;
494
495         cmpp = find_mp_by_wwid(curmp, mpp->wwid);
496         cmpp_by_name = find_mp_by_alias(curmp, mpp->alias);
497
498         if (!cmpp_by_name) {
499                 if (cmpp) {
500                         condlog(2, "%s: rename %s to %s", mpp->wwid,
501                                 cmpp->alias, mpp->alias);
502                         strncpy(mpp->alias_old, cmpp->alias, WWID_SIZE - 1);
503                         mpp->action = ACT_RENAME;
504                         if (force_reload) {
505                                 mpp->force_udev_reload = 1;
506                                 mpp->action = ACT_FORCERENAME;
507                         }
508                         return;
509                 }
510                 mpp->action = ACT_CREATE;
511                 condlog(3, "%s: set ACT_CREATE (map does not exist)",
512                         mpp->alias);
513                 return;
514         }
515
516         if (!cmpp) {
517                 condlog(2, "%s: remove (wwid changed)", mpp->alias);
518                 dm_flush_map(mpp->alias);
519                 strncpy(cmpp_by_name->wwid, mpp->wwid, WWID_SIZE - 1);
520                 drop_multipath(curmp, cmpp_by_name->wwid, KEEP_PATHS);
521                 mpp->action = ACT_CREATE;
522                 condlog(3, "%s: set ACT_CREATE (map wwid change)",
523                         mpp->alias);
524                 return;
525         }
526
527         if (cmpp != cmpp_by_name) {
528                 condlog(2, "%s: unable to rename %s to %s (%s is used by %s)",
529                         mpp->wwid, cmpp->alias, mpp->alias,
530                         mpp->alias, cmpp_by_name->wwid);
531                 /* reset alias to existing alias */
532                 FREE(mpp->alias);
533                 mpp->alias = STRDUP(cmpp->alias);
534                 mpp->action = ACT_IMPOSSIBLE;
535                 return;
536         }
537
538         if (pathcount(mpp, PATH_UP) == 0) {
539                 mpp->action = ACT_IMPOSSIBLE;
540                 condlog(3, "%s: set ACT_IMPOSSIBLE (no usable path)",
541                         mpp->alias);
542                 return;
543         }
544         if (force_reload) {
545                 mpp->force_udev_reload = 1;
546                 mpp->action = ACT_RELOAD;
547                 condlog(3, "%s: set ACT_RELOAD (forced by user)",
548                         mpp->alias);
549                 return;
550         }
551         if (cmpp->size != mpp->size) {
552                 mpp->force_udev_reload = 1;
553                 mpp->action = ACT_RESIZE;
554                 condlog(3, "%s: set ACT_RESIZE (size change)",
555                         mpp->alias);
556                 return;
557         }
558
559         if (mpp->no_path_retry != NO_PATH_RETRY_UNDEF &&
560             mpp->no_path_retry != cmpp->no_path_retry) {
561                 mpp->action =  ACT_RELOAD;
562                 condlog(3, "%s: set ACT_RELOAD (no_path_retry change)",
563                         mpp->alias);
564                 return;
565         }
566         if (mpp->retain_hwhandler != RETAIN_HWHANDLER_ON &&
567             (strlen(cmpp->hwhandler) != strlen(mpp->hwhandler) ||
568              strncmp(cmpp->hwhandler, mpp->hwhandler,
569                     strlen(mpp->hwhandler)))) {
570                 mpp->action = ACT_RELOAD;
571                 condlog(3, "%s: set ACT_RELOAD (hwhandler change)",
572                         mpp->alias);
573                 return;
574         }
575
576         if (mpp->retain_hwhandler != RETAIN_HWHANDLER_UNDEF &&
577             mpp->retain_hwhandler != cmpp->retain_hwhandler &&
578             get_linux_version_code() < KERNEL_VERSION(4, 3, 0)) {
579                 mpp->action = ACT_RELOAD;
580                 condlog(3, "%s: set ACT_RELOAD (retain_hwhandler change)",
581                         mpp->alias);
582                 return;
583         }
584
585         cmpp_feat = STRDUP(cmpp->features);
586         mpp_feat = STRDUP(mpp->features);
587         if (cmpp_feat && mpp_feat) {
588                 remove_feature(&mpp_feat, "queue_if_no_path");
589                 remove_feature(&mpp_feat, "retain_attached_hw_handler");
590                 remove_feature(&cmpp_feat, "queue_if_no_path");
591                 remove_feature(&cmpp_feat, "retain_attached_hw_handler");
592                 if (strncmp(mpp_feat, cmpp_feat, PARAMS_SIZE)) {
593                         mpp->action =  ACT_RELOAD;
594                         condlog(3, "%s: set ACT_RELOAD (features change)",
595                                 mpp->alias);
596                 }
597         }
598         FREE(cmpp_feat);
599         FREE(mpp_feat);
600
601         if (!cmpp->selector || strncmp(cmpp->selector, mpp->selector,
602                     strlen(mpp->selector))) {
603                 mpp->action = ACT_RELOAD;
604                 condlog(3, "%s: set ACT_RELOAD (selector change)",
605                         mpp->alias);
606                 return;
607         }
608         if (cmpp->minio != mpp->minio) {
609                 mpp->action = ACT_RELOAD;
610                 condlog(3, "%s: set ACT_RELOAD (minio change, %u->%u)",
611                         mpp->alias, cmpp->minio, mpp->minio);
612                 return;
613         }
614         if (!cmpp->pg || VECTOR_SIZE(cmpp->pg) != VECTOR_SIZE(mpp->pg)) {
615                 mpp->action = ACT_RELOAD;
616                 condlog(3, "%s: set ACT_RELOAD (path group number change)",
617                         mpp->alias);
618                 return;
619         }
620         if (pgcmp(mpp, cmpp)) {
621                 mpp->action = ACT_RELOAD;
622                 condlog(3, "%s: set ACT_RELOAD (path group topology change)",
623                         mpp->alias);
624                 return;
625         }
626         if (cmpp->nextpg != mpp->bestpg) {
627                 mpp->action = ACT_SWITCHPG;
628                 condlog(3, "%s: set ACT_SWITCHPG (next path group change)",
629                         mpp->alias);
630                 return;
631         }
632         if (!is_mpp_known_to_udev(cmpp)) {
633                 mpp->action = ACT_RELOAD;
634                 condlog(3, "%s: set ACT_RELOAD (udev device not initialized)",
635                         mpp->alias);
636                 return;
637         }
638         mpp->action = ACT_NOTHING;
639         condlog(3, "%s: set ACT_NOTHING (map unchanged)",
640                 mpp->alias);
641         return;
642 }
643
644 int reinstate_paths(struct multipath *mpp)
645 {
646         int i, j;
647         struct pathgroup * pgp;
648         struct path * pp;
649
650         if (!mpp->pg)
651                 return 0;
652
653         vector_foreach_slot (mpp->pg, pgp, i) {
654                 if (!pgp->paths)
655                         continue;
656
657                 vector_foreach_slot (pgp->paths, pp, j) {
658                         if (pp->state != PATH_UP &&
659                             (pgp->status == PGSTATE_DISABLED ||
660                              pgp->status == PGSTATE_ACTIVE))
661                                 continue;
662
663                         if (pp->dmstate == PSTATE_FAILED) {
664                                 if (dm_reinstate_path(mpp->alias, pp->dev_t))
665                                         condlog(0, "%s: error reinstating",
666                                                 pp->dev);
667                         }
668                 }
669         }
670         return 0;
671 }
672
673 static int
674 lock_multipath (struct multipath * mpp, int lock)
675 {
676         struct pathgroup * pgp;
677         struct path * pp;
678         int i, j;
679         int x, y;
680
681         if (!mpp || !mpp->pg)
682                 return 0;
683
684         vector_foreach_slot (mpp->pg, pgp, i) {
685                 if (!pgp->paths)
686                         continue;
687                 vector_foreach_slot(pgp->paths, pp, j) {
688                         if (lock && flock(pp->fd, LOCK_SH | LOCK_NB) &&
689                             errno == EWOULDBLOCK)
690                                 goto fail;
691                         else if (!lock)
692                                 flock(pp->fd, LOCK_UN);
693                 }
694         }
695         return 0;
696 fail:
697         vector_foreach_slot (mpp->pg, pgp, x) {
698                 if (x > i)
699                         return 1;
700                 if (!pgp->paths)
701                         continue;
702                 vector_foreach_slot(pgp->paths, pp, y) {
703                         if (x == i && y >= j)
704                                 return 1;
705                         flock(pp->fd, LOCK_UN);
706                 }
707         }
708         return 1;
709 }
710
711 /*
712  * Return value:
713  */
714 #define DOMAP_RETRY     -1
715 #define DOMAP_FAIL      0
716 #define DOMAP_OK        1
717 #define DOMAP_EXIST     2
718 #define DOMAP_DRY       3
719
720 int domap(struct multipath *mpp, char *params, int is_daemon)
721 {
722         int r = DOMAP_FAIL;
723         struct config *conf;
724
725         /*
726          * last chance to quit before touching the devmaps
727          */
728         if (mpp->action == ACT_DRY_RUN) {
729                 conf = get_multipath_config();
730                 print_multipath_topology(mpp, conf->verbosity);
731                 put_multipath_config(conf);
732                 return DOMAP_DRY;
733         }
734
735         if (mpp->action == ACT_CREATE &&
736             dm_map_present(mpp->alias)) {
737                 condlog(3, "%s: map already present", mpp->alias);
738                 mpp->action = ACT_RELOAD;
739         }
740
741         switch (mpp->action) {
742         case ACT_REJECT:
743         case ACT_NOTHING:
744         case ACT_IMPOSSIBLE:
745                 return DOMAP_EXIST;
746
747         case ACT_SWITCHPG:
748                 dm_switchgroup(mpp->alias, mpp->bestpg);
749                 /*
750                  * we may have avoided reinstating paths because there where in
751                  * active or disabled PG. Now that the topology has changed,
752                  * retry.
753                  */
754                 reinstate_paths(mpp);
755                 return DOMAP_EXIST;
756
757         case ACT_CREATE:
758                 if (lock_multipath(mpp, 1)) {
759                         condlog(3, "%s: failed to create map (in use)",
760                                 mpp->alias);
761                         return DOMAP_RETRY;
762                 }
763
764                 sysfs_set_max_sectors_kb(mpp, 0);
765                 if (is_daemon && mpp->ghost_delay > 0 && mpp->nr_active &&
766                     pathcount(mpp, PATH_GHOST) == mpp->nr_active)
767                         mpp->ghost_delay_tick = mpp->ghost_delay;
768                 r = dm_addmap_create(mpp, params);
769
770                 lock_multipath(mpp, 0);
771                 break;
772
773         case ACT_RELOAD:
774                 sysfs_set_max_sectors_kb(mpp, 1);
775                 if (mpp->ghost_delay_tick > 0 && pathcount(mpp, PATH_UP))
776                         mpp->ghost_delay_tick = 0;
777                 r = dm_addmap_reload(mpp, params, 0);
778                 break;
779
780         case ACT_RESIZE:
781                 sysfs_set_max_sectors_kb(mpp, 1);
782                 if (mpp->ghost_delay_tick > 0 && pathcount(mpp, PATH_UP))
783                         mpp->ghost_delay_tick = 0;
784                 r = dm_addmap_reload(mpp, params, 1);
785                 break;
786
787         case ACT_RENAME:
788                 conf = get_multipath_config();
789                 r = dm_rename(mpp->alias_old, mpp->alias,
790                               conf->partition_delim, mpp->skip_kpartx);
791                 put_multipath_config(conf);
792                 break;
793
794         case ACT_FORCERENAME:
795                 conf = get_multipath_config();
796                 r = dm_rename(mpp->alias_old, mpp->alias,
797                               conf->partition_delim, mpp->skip_kpartx);
798                 put_multipath_config(conf);
799                 if (r) {
800                         sysfs_set_max_sectors_kb(mpp, 1);
801                         if (mpp->ghost_delay_tick > 0 &&
802                             pathcount(mpp, PATH_UP))
803                                 mpp->ghost_delay_tick = 0;
804                         r = dm_addmap_reload(mpp, params, 0);
805                 }
806                 break;
807
808         default:
809                 break;
810         }
811
812         if (r == DOMAP_OK) {
813                 /*
814                  * DM_DEVICE_CREATE, DM_DEVICE_RENAME, or DM_DEVICE_RELOAD
815                  * succeeded
816                  */
817                 mpp->force_udev_reload = 0;
818                 if (mpp->action == ACT_CREATE)
819                         remember_wwid(mpp->wwid);
820                 if (!is_daemon) {
821                         /* multipath client mode */
822                         dm_switchgroup(mpp->alias, mpp->bestpg);
823                 } else  {
824                         /* multipath daemon mode */
825                         mpp->stat_map_loads++;
826                         condlog(2, "%s: load table [0 %llu %s %s]", mpp->alias,
827                                 mpp->size, TGT_MPATH, params);
828                         /*
829                          * Required action is over, reset for the stateful daemon.
830                          * But don't do it for creation as we use in the caller the
831                          * mpp->action to figure out whether to start the watievent checker.
832                          */
833                         if (mpp->action != ACT_CREATE)
834                                 mpp->action = ACT_NOTHING;
835                         else {
836                                 conf = get_multipath_config();
837                                 mpp->wait_for_udev = 1;
838                                 mpp->uev_wait_tick = conf->uev_wait_timeout;
839                                 put_multipath_config(conf);
840                         }
841                 }
842                 dm_setgeometry(mpp);
843                 return DOMAP_OK;
844         }
845         return DOMAP_FAIL;
846 }
847
848 static int
849 deadmap (struct multipath * mpp)
850 {
851         int i, j;
852         struct pathgroup * pgp;
853         struct path * pp;
854
855         if (!mpp->pg)
856                 return 1;
857
858         vector_foreach_slot (mpp->pg, pgp, i) {
859                 if (!pgp->paths)
860                         continue;
861
862                 vector_foreach_slot (pgp->paths, pp, j)
863                         if (strlen(pp->dev))
864                                 return 0; /* alive */
865         }
866
867         return 1; /* dead */
868 }
869
870 int check_daemon(void)
871 {
872         int fd;
873         char *reply;
874         int ret = 0;
875         unsigned int timeout;
876         struct config *conf;
877
878         fd = mpath_connect();
879         if (fd == -1)
880                 return 0;
881
882         if (send_packet(fd, "show daemon") != 0)
883                 goto out;
884         conf = get_multipath_config();
885         timeout = conf->uxsock_timeout;
886         put_multipath_config(conf);
887         if (recv_packet(fd, &reply, timeout) != 0)
888                 goto out;
889
890         if (strstr(reply, "shutdown"))
891                 goto out_free;
892
893         ret = 1;
894
895 out_free:
896         FREE(reply);
897 out:
898         mpath_disconnect(fd);
899         return ret;
900 }
901
902 /*
903  * The force_reload parameter determines how coalesce_paths treats existing maps.
904  * FORCE_RELOAD_NONE: existing maps aren't touched at all
905  * FORCE_RELOAD_YES: all maps are rebuilt from scratch and (re)loaded in DM
906  * FORCE_RELOAD_WEAK: existing maps are compared to the current conf and only
907  * reloaded in DM if there's a difference. This is useful during startup.
908  */
909 int coalesce_paths (struct vectors * vecs, vector newmp, char * refwwid,
910                     int force_reload, enum mpath_cmds cmd)
911 {
912         int r = 1;
913         int k, i;
914         int is_daemon = (cmd == CMD_NONE) ? 1 : 0;
915         char params[PARAMS_SIZE];
916         struct multipath * mpp;
917         struct path * pp1;
918         struct path * pp2;
919         vector curmp = vecs->mpvec;
920         vector pathvec = vecs->pathvec;
921         struct config *conf;
922         int allow_queueing;
923
924         /* ignore refwwid if it's empty */
925         if (refwwid && !strlen(refwwid))
926                 refwwid = NULL;
927
928         if (force_reload != FORCE_RELOAD_NONE) {
929                 vector_foreach_slot (pathvec, pp1, k) {
930                         pp1->mpp = NULL;
931                 }
932         }
933         vector_foreach_slot (pathvec, pp1, k) {
934                 /* skip this path for some reason */
935
936                 /* 1. if path has no unique id or wwid blacklisted */
937                 conf = get_multipath_config();
938                 if (strlen(pp1->wwid) == 0 ||
939                     filter_path(conf, pp1) > 0) {
940                         put_multipath_config(conf);
941                         orphan_path(pp1, "wwid blacklisted");
942                         continue;
943                 }
944                 put_multipath_config(conf);
945
946                 /* 2. if path already coalesced */
947                 if (pp1->mpp)
948                         continue;
949
950                 /* 3. if path has disappeared */
951                 if (pp1->state == PATH_REMOVED) {
952                         orphan_path(pp1, "path removed");
953                         continue;
954                 }
955
956                 /* 4. path is out of scope */
957                 if (refwwid && strncmp(pp1->wwid, refwwid, WWID_SIZE - 1))
958                         continue;
959
960                 /* If find_multipaths was selected check if the path is valid */
961                 if (!refwwid && !should_multipath(pp1, pathvec)) {
962                         orphan_path(pp1, "only one path");
963                         continue;
964                 }
965
966                 /*
967                  * at this point, we know we really got a new mp
968                  */
969                 mpp = add_map_with_path(vecs, pp1, 0);
970                 if (!mpp) {
971                         orphan_path(pp1, "failed to create multipath device");
972                         continue;
973                 }
974
975                 if (pp1->priority == PRIO_UNDEF)
976                         mpp->action = ACT_REJECT;
977
978                 if (!mpp->paths) {
979                         condlog(0, "%s: skip coalesce (no paths)", mpp->alias);
980                         remove_map(mpp, vecs, 0);
981                         continue;
982                 }
983
984                 for (i = k + 1; i < VECTOR_SIZE(pathvec); i++) {
985                         pp2 = VECTOR_SLOT(pathvec, i);
986
987                         if (strcmp(pp1->wwid, pp2->wwid))
988                                 continue;
989
990                         if (!mpp->size && pp2->size)
991                                 mpp->size = pp2->size;
992
993                         if (mpp->size && pp2->size &&
994                             pp2->size != mpp->size) {
995                                 /*
996                                  * ouch, avoid feeding that to the DM
997                                  */
998                                 condlog(0, "%s: size %llu, expected %llu. "
999                                         "Discard", pp2->dev_t, pp2->size,
1000                                         mpp->size);
1001                                 mpp->action = ACT_REJECT;
1002                         }
1003                         if (pp2->priority == PRIO_UNDEF)
1004                                 mpp->action = ACT_REJECT;
1005                 }
1006                 verify_paths(mpp, vecs);
1007
1008                 params[0] = '\0';
1009                 if (setup_map(mpp, params, PARAMS_SIZE)) {
1010                         remove_map(mpp, vecs, 0);
1011                         continue;
1012                 }
1013
1014                 if (cmd == CMD_DRY_RUN)
1015                         mpp->action = ACT_DRY_RUN;
1016                 if (mpp->action == ACT_UNDEF)
1017                         select_action(mpp, curmp,
1018                                       force_reload == FORCE_RELOAD_YES ? 1 : 0);
1019
1020                 r = domap(mpp, params, is_daemon);
1021
1022                 if (r == DOMAP_FAIL || r == DOMAP_RETRY) {
1023                         condlog(3, "%s: domap (%u) failure "
1024                                    "for create/reload map",
1025                                 mpp->alias, r);
1026                         if (r == DOMAP_FAIL || is_daemon) {
1027                                 condlog(2, "%s: %s map",
1028                                         mpp->alias, (mpp->action == ACT_CREATE)?
1029                                         "ignoring" : "removing");
1030                                 remove_map(mpp, vecs, 0);
1031                                 continue;
1032                         } else /* if (r == DOMAP_RETRY) */
1033                                 return r;
1034                 }
1035                 if (r == DOMAP_DRY)
1036                         continue;
1037
1038                 if (r == DOMAP_EXIST && mpp->action == ACT_NOTHING &&
1039                     force_reload == FORCE_RELOAD_WEAK)
1040                         /*
1041                          * First time we're called, and no changes applied.
1042                          * domap() was a noop. But we can't be sure that
1043                          * udev has already finished setting up this device
1044                          * (udev in initrd may have been shut down while
1045                          * processing this device or its children).
1046                          * Trigger a change event, just in case.
1047                          */
1048                         trigger_udev_change(find_mp_by_wwid(curmp, mpp->wwid));
1049
1050                 conf = get_multipath_config();
1051                 allow_queueing = conf->allow_queueing;
1052                 put_multipath_config(conf);
1053                 if (!is_daemon && !allow_queueing && !check_daemon()) {
1054                         if (mpp->no_path_retry != NO_PATH_RETRY_UNDEF &&
1055                             mpp->no_path_retry != NO_PATH_RETRY_FAIL)
1056                                 condlog(3, "%s: multipathd not running, unset "
1057                                         "queue_if_no_path feature", mpp->alias);
1058                         if (!dm_queue_if_no_path(mpp->alias, 0))
1059                                 remove_feature(&mpp->features,
1060                                                "queue_if_no_path");
1061                 }
1062                 else if (mpp->no_path_retry != NO_PATH_RETRY_UNDEF) {
1063                         if (mpp->no_path_retry == NO_PATH_RETRY_FAIL) {
1064                                 condlog(3, "%s: unset queue_if_no_path feature",
1065                                         mpp->alias);
1066                                 if (!dm_queue_if_no_path(mpp->alias, 0))
1067                                         remove_feature(&mpp->features,
1068                                                        "queue_if_no_path");
1069                         } else {
1070                                 condlog(3, "%s: set queue_if_no_path feature",
1071                                         mpp->alias);
1072                                 if (!dm_queue_if_no_path(mpp->alias, 1))
1073                                         add_feature(&mpp->features,
1074                                                     "queue_if_no_path");
1075                         }
1076                 }
1077
1078                 if (!is_daemon && mpp->action != ACT_NOTHING) {
1079                         conf = get_multipath_config();
1080                         print_multipath_topology(mpp, conf->verbosity);
1081                         put_multipath_config(conf);
1082                 }
1083
1084                 if (newmp) {
1085                         if (mpp->action != ACT_REJECT) {
1086                                 if (!vector_alloc_slot(newmp))
1087                                         return 1;
1088                                 vector_set_slot(newmp, mpp);
1089                         }
1090                         else
1091                                 remove_map(mpp, vecs, 0);
1092                 }
1093         }
1094         /*
1095          * Flush maps with only dead paths (ie not in sysfs)
1096          * Keep maps with only failed paths
1097          */
1098         if (newmp) {
1099                 vector_foreach_slot (newmp, mpp, i) {
1100                         char alias[WWID_SIZE];
1101
1102                         if (!deadmap(mpp))
1103                                 continue;
1104
1105                         strncpy(alias, mpp->alias, WWID_SIZE - 1);
1106
1107                         vector_del_slot(newmp, i);
1108                         i--;
1109                         remove_map(mpp, vecs, 0);
1110
1111                         if (dm_flush_map(alias))
1112                                 condlog(2, "%s: remove failed (dead)",
1113                                         alias);
1114                         else
1115                                 condlog(2, "%s: remove (dead)", alias);
1116                 }
1117         }
1118         return 0;
1119 }
1120
1121 struct udev_device *get_udev_device(const char *dev, enum devtypes dev_type)
1122 {
1123         struct udev_device *ud = NULL;
1124         const char *base;
1125
1126         if (dev == NULL || *dev == '\0')
1127                 return NULL;
1128
1129         switch (dev_type) {
1130         case DEV_DEVNODE:
1131         case DEV_DEVMAP:
1132                 /* This should be GNU basename, compiler will warn if not */
1133                 base = basename(dev);
1134                 if (*base == '\0')
1135                         break;
1136                 ud = udev_device_new_from_subsystem_sysname(udev, "block",
1137                                                             base);
1138                 break;
1139         case DEV_DEVT:
1140                 ud = udev_device_new_from_devnum(udev, 'b', parse_devt(dev));
1141                 break;
1142         case DEV_UEVENT:
1143                 ud = udev_device_new_from_environment(udev);
1144                 break;
1145         default:
1146                 condlog(0, "Internal error: get_udev_device called with invalid type %d\n",
1147                         dev_type);
1148                 break;
1149         }
1150         if (ud == NULL)
1151                 condlog(2, "get_udev_device: failed to look up %s with type %d",
1152                         dev, dev_type);
1153         return ud;
1154 }
1155
1156 /*
1157  * returns:
1158  * 0 - success
1159  * 1 - failure
1160  * 2 - blacklist
1161  */
1162 int get_refwwid(enum mpath_cmds cmd, char *dev, enum devtypes dev_type,
1163                 vector pathvec, char **wwid)
1164 {
1165         int ret = 1;
1166         struct path * pp;
1167         char buff[FILE_NAME_SIZE];
1168         char * refwwid = NULL, tmpwwid[WWID_SIZE];
1169         int flags = DI_SYSFS | DI_WWID;
1170         struct config *conf;
1171
1172         if (!wwid)
1173                 return 1;
1174         *wwid = NULL;
1175
1176         if (dev_type == DEV_NONE)
1177                 return 1;
1178
1179         if (cmd != CMD_REMOVE_WWID)
1180                 flags |= DI_BLACKLIST;
1181
1182         if (dev_type == DEV_DEVNODE) {
1183                 if (basenamecpy(dev, buff, FILE_NAME_SIZE) == 0) {
1184                         condlog(1, "basename failed for '%s' (%s)",
1185                                 dev, buff);
1186                         return 1;
1187                 }
1188
1189                 pp = find_path_by_dev(pathvec, buff);
1190                 if (!pp) {
1191                         struct udev_device *udevice =
1192                                 get_udev_device(buff, dev_type);
1193
1194                         if (!udevice)
1195                                 return 1;
1196
1197                         conf = get_multipath_config();
1198                         ret = store_pathinfo(pathvec, conf, udevice,
1199                                              flags, &pp);
1200                         put_multipath_config(conf);
1201                         udev_device_unref(udevice);
1202                         if (!pp) {
1203                                 if (ret == 1)
1204                                         condlog(0, "%s: can't store path info",
1205                                                 dev);
1206                                 return ret;
1207                         }
1208                 }
1209                 conf = get_multipath_config();
1210                 if (pp->udev && pp->uid_attribute &&
1211                     filter_property(conf, pp->udev) > 0) {
1212                         put_multipath_config(conf);
1213                         return 2;
1214                 }
1215                 put_multipath_config(conf);
1216
1217                 refwwid = pp->wwid;
1218                 goto out;
1219         }
1220
1221         if (dev_type == DEV_DEVT) {
1222                 strchop(dev);
1223                 if (devt2devname(buff, FILE_NAME_SIZE, dev)) {
1224                         condlog(0, "%s: cannot find block device\n", dev);
1225                         return 1;
1226                 }
1227                 pp = find_path_by_dev(pathvec, buff);
1228                 if (!pp) {
1229                         struct udev_device *udevice =
1230                                 get_udev_device(dev, dev_type);
1231
1232                         if (!udevice)
1233                                 return 1;
1234
1235                         conf = get_multipath_config();
1236                         ret = store_pathinfo(pathvec, conf, udevice,
1237                                              flags, &pp);
1238                         put_multipath_config(conf);
1239                         udev_device_unref(udevice);
1240                         if (!pp) {
1241                                 if (ret == 1)
1242                                         condlog(0, "%s can't store path info",
1243                                                 buff);
1244                                 return ret;
1245                         }
1246                 }
1247                 conf = get_multipath_config();
1248                 if (pp->udev && pp->uid_attribute &&
1249                     filter_property(conf, pp->udev) > 0) {
1250                         put_multipath_config(conf);
1251                         return 2;
1252                 }
1253                 put_multipath_config(conf);
1254                 refwwid = pp->wwid;
1255                 goto out;
1256         }
1257
1258         if (dev_type == DEV_UEVENT) {
1259                 struct udev_device *udevice = get_udev_device(dev, dev_type);
1260
1261                 if (!udevice)
1262                         return 1;
1263
1264                 conf = get_multipath_config();
1265                 ret = store_pathinfo(pathvec, conf, udevice,
1266                                      flags, &pp);
1267                 udev_device_unref(udevice);
1268                 if (!pp) {
1269                         if (ret == 1)
1270                                 condlog(0, "%s: can't store path info",
1271                                         dev);
1272                         put_multipath_config(conf);
1273                         return ret;
1274                 }
1275                 if (pp->udev && pp->uid_attribute &&
1276                     filter_property(conf, pp->udev) > 0) {
1277                         put_multipath_config(conf);
1278                         return 2;
1279                 }
1280                 put_multipath_config(conf);
1281                 refwwid = pp->wwid;
1282                 goto out;
1283         }
1284
1285         if (dev_type == DEV_DEVMAP) {
1286
1287                 conf = get_multipath_config();
1288                 if (((dm_get_uuid(dev, tmpwwid)) == 0) && (strlen(tmpwwid))) {
1289                         refwwid = tmpwwid;
1290                         goto check;
1291                 }
1292
1293                 /*
1294                  * may be a binding
1295                  */
1296                 if (get_user_friendly_wwid(dev, tmpwwid,
1297                                            conf->bindings_file) == 0) {
1298                         refwwid = tmpwwid;
1299                         put_multipath_config(conf);
1300                         goto check;
1301                 }
1302
1303                 /*
1304                  * or may be an alias
1305                  */
1306                 refwwid = get_mpe_wwid(conf->mptable, dev);
1307
1308                 /*
1309                  * or directly a wwid
1310                  */
1311                 if (!refwwid)
1312                         refwwid = dev;
1313
1314 check:
1315                 if (refwwid && strlen(refwwid)) {
1316                         if (filter_wwid(conf->blist_wwid, conf->elist_wwid,
1317                                         refwwid, NULL) > 0) {
1318                                 put_multipath_config(conf);
1319                                 return 2;
1320                         }
1321                 }
1322                 put_multipath_config(conf);
1323         }
1324 out:
1325         if (refwwid && strlen(refwwid)) {
1326                 *wwid = STRDUP(refwwid);
1327                 return 0;
1328         }
1329
1330         return 1;
1331 }
1332
1333 int reload_map(struct vectors *vecs, struct multipath *mpp, int refresh,
1334                int is_daemon)
1335 {
1336         char params[PARAMS_SIZE] = {0};
1337         struct path *pp;
1338         int i, r;
1339
1340         update_mpp_paths(mpp, vecs->pathvec);
1341         if (refresh) {
1342                 vector_foreach_slot (mpp->paths, pp, i) {
1343                         struct config *conf = get_multipath_config();
1344                         r = pathinfo(pp, conf, DI_PRIO);
1345                         put_multipath_config(conf);
1346                         if (r) {
1347                                 condlog(2, "%s: failed to refresh pathinfo",
1348                                         mpp->alias);
1349                                 return 1;
1350                         }
1351                 }
1352         }
1353         if (setup_map(mpp, params, PARAMS_SIZE)) {
1354                 condlog(0, "%s: failed to setup map", mpp->alias);
1355                 return 1;
1356         }
1357         select_action(mpp, vecs->mpvec, 1);
1358
1359         r = domap(mpp, params, is_daemon);
1360         if (r == DOMAP_FAIL || r == DOMAP_RETRY) {
1361                 condlog(3, "%s: domap (%u) failure "
1362                         "for reload map", mpp->alias, r);
1363                 return 1;
1364         }
1365         if (mpp->no_path_retry != NO_PATH_RETRY_UNDEF) {
1366                 if (mpp->no_path_retry == NO_PATH_RETRY_FAIL)
1367                         dm_queue_if_no_path(mpp->alias, 0);
1368                 else
1369                         dm_queue_if_no_path(mpp->alias, 1);
1370         }
1371
1372         return 0;
1373 }