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