Imported Upstream version 0.8.9
[platform/upstream/multipath-tools.git] / libmultipath / discovery.c
1 /*
2  * Copyright (c) 2004, 2005, 2006 Christophe Varoqui
3  * Copyright (c) 2005 Stefan Bader, IBM
4  * Copyright (c) 2005 Mike Anderson
5  */
6 #include <stdio.h>
7 #include <ctype.h>
8 #include <unistd.h>
9 #include <limits.h>
10 #include <fcntl.h>
11 #include <sys/ioctl.h>
12 #include <sys/stat.h>
13 #include <dirent.h>
14 #include <errno.h>
15 #include <libgen.h>
16 #include <libudev.h>
17
18 #include "checkers.h"
19 #include "vector.h"
20 #include "util.h"
21 #include "structs.h"
22 #include "config.h"
23 #include "blacklist.h"
24 #include "callout.h"
25 #include "debug.h"
26 #include "propsel.h"
27 #include "sg_include.h"
28 #include "sysfs.h"
29 #include "discovery.h"
30 #include "prio.h"
31 #include "defaults.h"
32 #include "unaligned.h"
33 #include "prioritizers/alua_rtpg.h"
34 #include "foreign.h"
35 #include "configure.h"
36 #include "print.h"
37 #include "strbuf.h"
38
39 #define VPD_BUFLEN 4096
40
41 struct vpd_vendor_page vpd_vendor_pages[VPD_VP_ARRAY_SIZE] = {
42         [VPD_VP_UNDEF]  = { 0x00, "undef" },
43         [VPD_VP_HP3PAR] = { 0xc0, "hp3par" },
44 };
45
46 int
47 alloc_path_with_pathinfo (struct config *conf, struct udev_device *udevice,
48                           const char *wwid, int flag, struct path **pp_ptr)
49 {
50         int err = PATHINFO_FAILED;
51         struct path * pp;
52         const char * devname;
53
54         if (pp_ptr)
55                 *pp_ptr = NULL;
56
57         devname = udev_device_get_sysname(udevice);
58         if (!devname)
59                 return PATHINFO_FAILED;
60
61         pp = alloc_path();
62
63         if (!pp)
64                 return PATHINFO_FAILED;
65
66         if (wwid)
67                 strlcpy(pp->wwid, wwid, sizeof(pp->wwid));
68
69         if (safe_sprintf(pp->dev, "%s", devname)) {
70                 condlog(0, "pp->dev too small");
71                 err = 1;
72         } else {
73                 pp->udev = udev_device_ref(udevice);
74                 err = pathinfo(pp, conf, flag | DI_BLACKLIST);
75         }
76
77         if (err || !pp_ptr)
78                 free_path(pp);
79         else if (pp_ptr)
80                 *pp_ptr = pp;
81         return err;
82 }
83
84 int
85 store_pathinfo (vector pathvec, struct config *conf,
86                 struct udev_device *udevice, int flag, struct path **pp_ptr)
87 {
88         int err = PATHINFO_FAILED;
89         struct path * pp;
90         const char * devname;
91
92         if (pp_ptr)
93                 *pp_ptr = NULL;
94
95         devname = udev_device_get_sysname(udevice);
96         if (!devname)
97                 return PATHINFO_FAILED;
98
99         pp = alloc_path();
100
101         if (!pp)
102                 return PATHINFO_FAILED;
103
104         if(safe_sprintf(pp->dev, "%s", devname)) {
105                 condlog(0, "pp->dev too small");
106                 goto out;
107         }
108         pp->udev = udev_device_ref(udevice);
109         err = pathinfo(pp, conf, flag);
110         if (err)
111                 goto out;
112
113         err = store_path(pathvec, pp);
114         if (err)
115                 goto out;
116         pp->checkint = conf->checkint;
117
118 out:
119         if (err)
120                 free_path(pp);
121         else if (pp_ptr)
122                 *pp_ptr = pp;
123         return err;
124 }
125
126 static int
127 path_discover (vector pathvec, struct config * conf,
128                struct udev_device *udevice, int flag)
129 {
130         struct path *pp;
131         char devt[BLK_DEV_SIZE];
132         dev_t devnum = udev_device_get_devnum(udevice);
133
134         snprintf(devt, BLK_DEV_SIZE, "%d:%d",
135                  major(devnum), minor(devnum));
136         pp = find_path_by_devt(pathvec, devt);
137         if (!pp)
138                 return store_pathinfo(pathvec, conf,
139                                       udevice, flag | DI_BLACKLIST,
140                                       NULL);
141         else
142                 /*
143                  * Don't use DI_BLACKLIST on paths already in pathvec. We rely
144                  * on the caller to pre-populate the pathvec with valid paths
145                  * only.
146                  */
147                 return pathinfo(pp, conf, flag);
148 }
149
150 static void cleanup_udev_enumerate_ptr(void *arg)
151 {
152         struct udev_enumerate *ue;
153
154         if (!arg)
155                 return;
156         ue = *((struct udev_enumerate**) arg);
157         if (ue)
158                 (void)udev_enumerate_unref(ue);
159 }
160
161 static void cleanup_udev_device_ptr(void *arg)
162 {
163         struct udev_device *ud;
164
165         if (!arg)
166                 return;
167         ud = *((struct udev_device**) arg);
168         if (ud)
169                 (void)udev_device_unref(ud);
170 }
171
172 int
173 path_discovery (vector pathvec, int flag)
174 {
175         struct udev_enumerate *udev_iter = NULL;
176         struct udev_list_entry *entry;
177         struct udev_device *udevice = NULL;
178         struct config *conf;
179         int num_paths = 0, total_paths = 0, ret;
180
181         pthread_cleanup_push(cleanup_udev_enumerate_ptr, &udev_iter);
182         pthread_cleanup_push(cleanup_udev_device_ptr, &udevice);
183         conf = get_multipath_config();
184         pthread_cleanup_push(put_multipath_config, conf);
185
186         udev_iter = udev_enumerate_new(udev);
187         if (!udev_iter) {
188                 ret = -ENOMEM;
189                 goto out;
190         }
191
192         if (udev_enumerate_add_match_subsystem(udev_iter, "block") < 0 ||
193             udev_enumerate_add_match_is_initialized(udev_iter) < 0 ||
194             udev_enumerate_scan_devices(udev_iter) < 0) {
195                 condlog(1, "%s: error setting up udev_enumerate: %m", __func__);
196                 ret = -1;
197                 goto out;
198         }
199
200         udev_list_entry_foreach(entry,
201                                 udev_enumerate_get_list_entry(udev_iter)) {
202                 const char *devtype;
203                 const char *devpath;
204
205                 if (should_exit())
206                         break;
207
208                 devpath = udev_list_entry_get_name(entry);
209                 condlog(4, "Discover device %s", devpath);
210                 udevice = udev_device_new_from_syspath(udev, devpath);
211                 if (!udevice) {
212                         condlog(4, "%s: no udev information", devpath);
213                         continue;
214                 }
215                 devtype = udev_device_get_devtype(udevice);
216                 if(devtype && !strncmp(devtype, "disk", 4)) {
217                         total_paths++;
218                         if (path_discover(pathvec, conf,
219                                           udevice, flag) == PATHINFO_OK)
220                                 num_paths++;
221                 }
222                 udevice = udev_device_unref(udevice);
223         }
224         ret = total_paths - num_paths;
225         condlog(4, "Discovered %d/%d paths", num_paths, total_paths);
226 out:
227         pthread_cleanup_pop(1);
228         pthread_cleanup_pop(1);
229         pthread_cleanup_pop(1);
230         return ret;
231 }
232
233 #define declare_sysfs_get_str(fname)                                    \
234 ssize_t                                                                 \
235 sysfs_get_##fname (struct udev_device * udev, char * buff, size_t len)  \
236 {                                                                       \
237         size_t l;                                                       \
238         const char * attr;                                              \
239         const char * devname;                                           \
240                                                                         \
241         if (!udev)                                                      \
242                 return -ENOSYS;                                         \
243                                                                         \
244         devname = udev_device_get_sysname(udev);                        \
245                                                                         \
246         attr = udev_device_get_sysattr_value(udev, #fname);             \
247         if (!attr) {                                                    \
248                 condlog(3, "%s: attribute %s not found in sysfs",       \
249                         devname, #fname);                               \
250                 return -ENXIO;                                          \
251         }                                                               \
252         for (l = strlen(attr); l >= 1 && isspace(attr[l-1]); l--);      \
253         if (l > len) {                                                  \
254                 condlog(3, "%s: overflow in attribute %s",              \
255                         devname, #fname);                               \
256                 return -EINVAL;                                         \
257         }                                                               \
258         strlcpy(buff, attr, len);                                       \
259         return strchop(buff);                                           \
260 }
261
262 declare_sysfs_get_str(devtype);
263 declare_sysfs_get_str(vendor);
264 declare_sysfs_get_str(model);
265 declare_sysfs_get_str(rev);
266
267 static ssize_t
268 sysfs_get_binary (struct udev_device * udev, const char *attrname,
269                   unsigned char *buff, size_t len)
270 {
271         ssize_t attr_len;
272         const char * devname;
273
274         if (!udev) {
275                 condlog(3, "No udev device given\n");
276                 return -ENOSYS;
277         }
278
279         devname = udev_device_get_sysname(udev);
280         attr_len = sysfs_bin_attr_get_value(udev, attrname, buff, len);
281         if (attr_len < 0) {
282                 condlog(3, "%s: attribute %s not found in sysfs",
283                         devname, attrname);
284                 return attr_len;
285         }
286         return attr_len;
287 }
288
289 ssize_t sysfs_get_vpd(struct udev_device * udev, unsigned char pg,
290                       unsigned char *buff, size_t len)
291 {
292         char attrname[9];
293
294         snprintf(attrname, sizeof(attrname), "vpd_pg%02x", pg);
295         return sysfs_get_binary(udev, attrname, buff, len);
296 }
297
298 ssize_t sysfs_get_inquiry(struct udev_device * udev,
299                           unsigned char *buff, size_t len)
300 {
301         return sysfs_get_binary(udev, "inquiry", buff, len);
302 }
303
304 int
305 sysfs_get_timeout(const struct path *pp, unsigned int *timeout)
306 {
307         const char *attr = NULL;
308         const char *subsys;
309         struct udev_device *parent;
310         char *eptr;
311         unsigned long t;
312
313         if (!pp->udev || pp->bus != SYSFS_BUS_SCSI)
314                 return -ENOSYS;
315
316         parent = pp->udev;
317         while (parent) {
318                 subsys = udev_device_get_subsystem(parent);
319                 attr = udev_device_get_sysattr_value(parent, "timeout");
320                 if (subsys && attr)
321                         break;
322                 parent = udev_device_get_parent(parent);
323         }
324         if (!attr) {
325                 condlog(3, "%s: No timeout value in sysfs", pp->dev);
326                 return -ENXIO;
327         }
328
329         t = strtoul(attr, &eptr, 0);
330         if (attr == eptr || t == ULONG_MAX) {
331                 condlog(3, "%s: Cannot parse timeout attribute '%s'",
332                         pp->dev, attr);
333                 return -EINVAL;
334         }
335         if (t > UINT_MAX) {
336                 condlog(3, "%s: Overflow in timeout value '%s'",
337                         pp->dev, attr);
338                 return -ERANGE;
339         }
340         *timeout = t;
341
342         return 1;
343 }
344
345 static int
346 sysfs_get_tgt_nodename(struct path *pp, char *node)
347 {
348         const char *tgtname, *value;
349         struct udev_device *parent, *tgtdev;
350         int host, channel, tgtid = -1;
351
352         if (!pp->udev)
353                 return 1;
354         parent = udev_device_get_parent_with_subsystem_devtype(pp->udev,
355                                                          "scsi", "scsi_device");
356         if (!parent)
357                 return 1;
358         /* Check for SAS */
359         value = udev_device_get_sysattr_value(parent, "sas_address");
360         if (value) {
361                 tgtdev = udev_device_get_parent(parent);
362                 while (tgtdev) {
363                         char c;
364
365                         tgtname = udev_device_get_sysname(tgtdev);
366                         if (tgtname) {
367                                 if (sscanf(tgtname, "end_device-%d:%d:%d%c",
368                                            &host, &channel, &tgtid, &c) == 3)
369                                         break;
370                                 if (sscanf(tgtname, "end_device-%d:%d%c",
371                                            &host, &tgtid, &c) == 2)
372                                         break;
373                         }
374                         tgtdev = udev_device_get_parent(tgtdev);
375                         tgtid = -1;
376                 }
377                 if (tgtid >= 0) {
378                         pp->sg_id.proto_id = SCSI_PROTOCOL_SAS;
379                         pp->sg_id.transport_id = tgtid;
380                         strlcpy(node, value, NODE_NAME_SIZE);
381                         return 0;
382                 }
383         }
384
385         /* Check for USB */
386         tgtdev = udev_device_get_parent(parent);
387         while (tgtdev) {
388                 value = udev_device_get_subsystem(tgtdev);
389                 if (value && !strcmp(value, "usb")) {
390                         pp->sg_id.proto_id = SCSI_PROTOCOL_USB;
391                         tgtname = udev_device_get_sysname(tgtdev);
392                         if (tgtname) {
393                                 strlcpy(node, tgtname, NODE_NAME_SIZE);
394                                 return 0;
395                         }
396                 }
397                 tgtdev = udev_device_get_parent(tgtdev);
398         }
399         parent = udev_device_get_parent_with_subsystem_devtype(pp->udev, "scsi", "scsi_target");
400         if (!parent)
401                 return 1;
402         /* Check for FibreChannel */
403         tgtdev = udev_device_get_parent(parent);
404         value = udev_device_get_sysname(tgtdev);
405         if (value && sscanf(value, "rport-%d:%d-%d",
406                    &host, &channel, &tgtid) == 3) {
407                 tgtdev = udev_device_new_from_subsystem_sysname(udev,
408                                 "fc_remote_ports", value);
409                 if (tgtdev) {
410                         condlog(4, "SCSI target %d:%d:%d -> "
411                                 "FC rport %d:%d-%d",
412                                 pp->sg_id.host_no, pp->sg_id.channel,
413                                 pp->sg_id.scsi_id, host, channel,
414                                 tgtid);
415                         value = udev_device_get_sysattr_value(tgtdev,
416                                                               "node_name");
417                         if (value) {
418                                 pp->sg_id.proto_id = SCSI_PROTOCOL_FCP;
419                                 pp->sg_id.transport_id = tgtid;
420                                 strlcpy(node, value, NODE_NAME_SIZE);
421                                 udev_device_unref(tgtdev);
422                                 return 0;
423                         } else
424                                 udev_device_unref(tgtdev);
425                 }
426         }
427
428         /* Check for iSCSI */
429         parent = pp->udev;
430         tgtname = NULL;
431         while (parent) {
432                 tgtname = udev_device_get_sysname(parent);
433                 if (tgtname && sscanf(tgtname , "session%d", &tgtid) == 1)
434                         break;
435                 parent = udev_device_get_parent(parent);
436                 tgtname = NULL;
437                 tgtid = -1;
438         }
439         if (parent && tgtname) {
440                 tgtdev = udev_device_new_from_subsystem_sysname(udev,
441                                 "iscsi_session", tgtname);
442                 if (tgtdev) {
443                         const char *value;
444
445                         value = udev_device_get_sysattr_value(tgtdev, "targetname");
446                         if (value) {
447                                 pp->sg_id.proto_id = SCSI_PROTOCOL_ISCSI;
448                                 pp->sg_id.transport_id = tgtid;
449                                 strlcpy(node, value, NODE_NAME_SIZE);
450                                 udev_device_unref(tgtdev);
451                                 return 0;
452                         }
453                         else
454                                 udev_device_unref(tgtdev);
455                 }
456         }
457         /* Check for libata */
458         parent = pp->udev;
459         tgtname = NULL;
460         while (parent) {
461                 tgtname = udev_device_get_sysname(parent);
462                 if (tgtname && sscanf(tgtname, "ata%d", &tgtid) == 1)
463                         break;
464                 parent = udev_device_get_parent(parent);
465                 tgtname = NULL;
466         }
467         if (tgtname) {
468                 pp->sg_id.proto_id = SCSI_PROTOCOL_ATA;
469                 pp->sg_id.transport_id = tgtid;
470                 snprintf(node, NODE_NAME_SIZE, "ata-%d.00", tgtid);
471                 return 0;
472         }
473         /* Unknown SCSI transport. Keep fingers crossed */
474         pp->sg_id.proto_id = SCSI_PROTOCOL_UNSPEC;
475         return 0;
476 }
477
478 static int sysfs_get_host_bus_id(const struct path *pp, char *bus_id)
479 {
480         struct udev_device *hostdev, *parent;
481         char host_name[HOST_NAME_LEN];
482         const char *driver_name, *subsystem_name, *value;
483
484         if (!pp || !bus_id)
485                 return 1;
486
487         snprintf(host_name, sizeof(host_name), "host%d", pp->sg_id.host_no);
488         hostdev = udev_device_new_from_subsystem_sysname(udev,
489                         "scsi_host", host_name);
490         if (!hostdev)
491                 return 1;
492
493         for (parent = udev_device_get_parent(hostdev);
494              parent;
495              parent = udev_device_get_parent(parent)) {
496                 driver_name = udev_device_get_driver(parent);
497                 subsystem_name = udev_device_get_subsystem(parent);
498                 if (driver_name && !strcmp(driver_name, "pcieport"))
499                         break;
500                 if (subsystem_name && !strcmp(subsystem_name, "ccw"))
501                         break;
502         }
503         if (parent) {
504                 /* pci_device or ccw fcp device found
505                  */
506                 value = udev_device_get_sysname(parent);
507
508                 if (!value) {
509                         udev_device_unref(hostdev);
510                         return 1;
511                 }
512
513                 strlcpy(bus_id, value, SLOT_NAME_SIZE);
514                 udev_device_unref(hostdev);
515                 return 0;
516         }
517         udev_device_unref(hostdev);
518         return 1;
519 }
520
521 int sysfs_get_host_adapter_name(const struct path *pp, char *adapter_name)
522 {
523         int proto_id;
524
525         if (!pp || !adapter_name)
526                 return 1;
527
528         proto_id = pp->sg_id.proto_id;
529
530         if (proto_id != SCSI_PROTOCOL_FCP &&
531             proto_id != SCSI_PROTOCOL_SAS &&
532             proto_id != SCSI_PROTOCOL_ISCSI &&
533             proto_id != SCSI_PROTOCOL_SRP) {
534                 return 1;
535         }
536         /* iscsi doesn't have adapter info in sysfs
537          * get ip_address for grouping paths
538          */
539         if (pp->sg_id.proto_id == SCSI_PROTOCOL_ISCSI)
540                 return sysfs_get_iscsi_ip_address(pp, adapter_name);
541
542         /* fetch adapter bus-ID for other protocols
543          */
544         return sysfs_get_host_bus_id(pp, adapter_name);
545 }
546
547 int sysfs_get_iscsi_ip_address(const struct path *pp, char *ip_address)
548 {
549         struct udev_device *hostdev;
550         char host_name[HOST_NAME_LEN];
551         const char *value;
552
553         sprintf(host_name, "host%d", pp->sg_id.host_no);
554         hostdev = udev_device_new_from_subsystem_sysname(udev,
555                         "iscsi_host", host_name);
556         if (hostdev) {
557                 value = udev_device_get_sysattr_value(hostdev,
558                                 "ipaddress");
559                 if (value) {
560                         strncpy(ip_address, value, SLOT_NAME_SIZE);
561                         udev_device_unref(hostdev);
562                         return 0;
563                 } else
564                         udev_device_unref(hostdev);
565         }
566         return 1;
567 }
568
569 int
570 sysfs_get_asymmetric_access_state(struct path *pp, char *buff, int buflen)
571 {
572         struct udev_device *parent = pp->udev;
573         char value[16], *eptr;
574         unsigned long preferred;
575
576         while (parent) {
577                 const char *subsys = udev_device_get_subsystem(parent);
578                 if (subsys && !strncmp(subsys, "scsi", 4))
579                         break;
580                 parent = udev_device_get_parent(parent);
581         }
582
583         if (!parent)
584                 return -1;
585
586         if (sysfs_attr_get_value(parent, "access_state", buff, buflen) <= 0)
587                 return -1;
588
589         if (sysfs_attr_get_value(parent, "preferred_path", value, 16) <= 0)
590                 return 0;
591
592         preferred = strtoul(value, &eptr, 0);
593         if (value == eptr || preferred == ULONG_MAX) {
594                 /* Parse error, ignore */
595                 return 0;
596         }
597         return !!preferred;
598 }
599
600 static int
601 sysfs_set_eh_deadline(struct multipath *mpp, struct path *pp)
602 {
603         struct udev_device *hostdev;
604         char host_name[HOST_NAME_LEN], value[16];
605         int ret, len;
606
607         if (mpp->eh_deadline == EH_DEADLINE_UNSET)
608                 return 0;
609
610         sprintf(host_name, "host%d", pp->sg_id.host_no);
611         hostdev = udev_device_new_from_subsystem_sysname(udev,
612                         "scsi_host", host_name);
613         if (!hostdev)
614                 return 1;
615
616         if (mpp->eh_deadline == EH_DEADLINE_OFF)
617                 len = sprintf(value, "off");
618         else if (mpp->eh_deadline == EH_DEADLINE_ZERO)
619                 len = sprintf(value, "0");
620         else
621                 len = sprintf(value, "%d", mpp->eh_deadline);
622
623         ret = sysfs_attr_set_value(hostdev, "eh_deadline",
624                                    value, len + 1);
625         /*
626          * not all scsi drivers support setting eh_deadline, so failing
627          * is totally reasonable
628          */
629         if (ret <= 0)
630                 condlog(3, "%s: failed to set eh_deadline to %s, error %d", udev_device_get_sysname(hostdev), value, -ret);
631
632         udev_device_unref(hostdev);
633         return (ret <= 0);
634 }
635
636 static void
637 sysfs_set_rport_tmo(struct multipath *mpp, struct path *pp)
638 {
639         struct udev_device *rport_dev = NULL;
640         char value[16], *eptr;
641         char rport_id[42];
642         unsigned int tmo;
643         int ret;
644
645         if (mpp->dev_loss == DEV_LOSS_TMO_UNSET &&
646             mpp->fast_io_fail == MP_FAST_IO_FAIL_UNSET)
647                 return;
648
649         sprintf(rport_id, "rport-%d:%d-%d",
650                 pp->sg_id.host_no, pp->sg_id.channel, pp->sg_id.transport_id);
651         rport_dev = udev_device_new_from_subsystem_sysname(udev,
652                                 "fc_remote_ports", rport_id);
653         if (!rport_dev) {
654                 condlog(1, "%s: No fc_remote_port device for '%s'", pp->dev,
655                         rport_id);
656                 return;
657         }
658         condlog(4, "target%d:%d:%d -> %s", pp->sg_id.host_no,
659                 pp->sg_id.channel, pp->sg_id.scsi_id, rport_id);
660
661         /*
662          * read the current dev_loss_tmo value from sysfs
663          */
664         ret = sysfs_attr_get_value(rport_dev, "dev_loss_tmo", value, 16);
665         if (ret <= 0) {
666                 condlog(0, "%s: failed to read dev_loss_tmo value, "
667                         "error %d", rport_id, -ret);
668                 goto out;
669         }
670         tmo = strtoul(value, &eptr, 0);
671         if (value == eptr) {
672                 condlog(0, "%s: Cannot parse dev_loss_tmo "
673                         "attribute '%s'", rport_id, value);
674                 goto out;
675         }
676
677         /*
678          * This is tricky.
679          * dev_loss_tmo will be limited to 600 if fast_io_fail
680          * is _not_ set.
681          * fast_io_fail will be limited by the current dev_loss_tmo
682          * setting.
683          * So to get everything right we first need to increase
684          * dev_loss_tmo to the fast_io_fail setting (if present),
685          * then set fast_io_fail, and _then_ set dev_loss_tmo
686          * to the correct value.
687          */
688         if (mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET &&
689             mpp->fast_io_fail != MP_FAST_IO_FAIL_ZERO &&
690             mpp->fast_io_fail != MP_FAST_IO_FAIL_OFF) {
691                 /* Check if we need to temporarily increase dev_loss_tmo */
692                 if ((unsigned int)mpp->fast_io_fail >= tmo) {
693                         /* Increase dev_loss_tmo temporarily */
694                         snprintf(value, sizeof(value), "%u",
695                                  (unsigned int)mpp->fast_io_fail + 1);
696                         ret = sysfs_attr_set_value(rport_dev, "dev_loss_tmo",
697                                                    value, strlen(value));
698                         if (ret <= 0) {
699                                 if (ret == -EBUSY)
700                                         condlog(3, "%s: rport blocked",
701                                                 rport_id);
702                                 else
703                                         condlog(0, "%s: failed to set "
704                                                 "dev_loss_tmo to %s, error %d",
705                                                 rport_id, value, -ret);
706                                 goto out;
707                         }
708                 }
709         } else if (mpp->dev_loss > DEFAULT_DEV_LOSS_TMO &&
710                 mpp->no_path_retry != NO_PATH_RETRY_QUEUE) {
711                 condlog(2, "%s: limiting dev_loss_tmo to %d, since "
712                         "fast_io_fail is not set",
713                         rport_id, DEFAULT_DEV_LOSS_TMO);
714                 mpp->dev_loss = DEFAULT_DEV_LOSS_TMO;
715         }
716         if (mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET) {
717                 if (mpp->fast_io_fail == MP_FAST_IO_FAIL_OFF)
718                         sprintf(value, "off");
719                 else if (mpp->fast_io_fail == MP_FAST_IO_FAIL_ZERO)
720                         sprintf(value, "0");
721                 else
722                         snprintf(value, 16, "%u", mpp->fast_io_fail);
723                 ret = sysfs_attr_set_value(rport_dev, "fast_io_fail_tmo",
724                                            value, strlen(value));
725                 if (ret <= 0) {
726                         if (ret == -EBUSY)
727                                 condlog(3, "%s: rport blocked", rport_id);
728                         else
729                                 condlog(0, "%s: failed to set fast_io_fail_tmo to %s, error %d",
730                                         rport_id, value, -ret);
731                 }
732         }
733         if (mpp->dev_loss != DEV_LOSS_TMO_UNSET) {
734                 snprintf(value, 16, "%u", mpp->dev_loss);
735                 ret = sysfs_attr_set_value(rport_dev, "dev_loss_tmo",
736                                            value, strlen(value));
737                 if (ret <= 0) {
738                         if (ret == -EBUSY)
739                                 condlog(3, "%s: rport blocked", rport_id);
740                         else
741                                 condlog(0, "%s: failed to set dev_loss_tmo to %s, error %d",
742                                         rport_id, value, -ret);
743                 }
744         }
745 out:
746         udev_device_unref(rport_dev);
747 }
748
749 static void
750 sysfs_set_session_tmo(struct multipath *mpp, struct path *pp)
751 {
752         struct udev_device *session_dev = NULL;
753         char session_id[64];
754         char value[11];
755
756         if (mpp->dev_loss != DEV_LOSS_TMO_UNSET)
757                 condlog(3, "%s: ignoring dev_loss_tmo on iSCSI", pp->dev);
758         if (mpp->fast_io_fail == MP_FAST_IO_FAIL_UNSET)
759                 return;
760
761         sprintf(session_id, "session%d", pp->sg_id.transport_id);
762         session_dev = udev_device_new_from_subsystem_sysname(udev,
763                                 "iscsi_session", session_id);
764         if (!session_dev) {
765                 condlog(1, "%s: No iscsi session for '%s'", pp->dev,
766                         session_id);
767                 return;
768         }
769         condlog(4, "target%d:%d:%d -> %s", pp->sg_id.host_no,
770                 pp->sg_id.channel, pp->sg_id.scsi_id, session_id);
771
772         if (mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET) {
773                 if (mpp->fast_io_fail == MP_FAST_IO_FAIL_OFF) {
774                         condlog(3, "%s: can't switch off fast_io_fail_tmo "
775                                 "on iSCSI", pp->dev);
776                 } else if (mpp->fast_io_fail == MP_FAST_IO_FAIL_ZERO) {
777                         condlog(3, "%s: can't set fast_io_fail_tmo to '0'"
778                                 "on iSCSI", pp->dev);
779                 } else {
780                         snprintf(value, 11, "%u", mpp->fast_io_fail);
781                         if (sysfs_attr_set_value(session_dev, "recovery_tmo",
782                                                  value, strlen(value)) <= 0) {
783                                 condlog(3, "%s: Failed to set recovery_tmo, "
784                                         " error %d", pp->dev, errno);
785                         }
786                 }
787         }
788         udev_device_unref(session_dev);
789         return;
790 }
791
792 static void
793 sysfs_set_nexus_loss_tmo(struct multipath *mpp, struct path *pp)
794 {
795         struct udev_device *parent, *sas_dev = NULL;
796         const char *end_dev_id = NULL;
797         char value[11];
798         static const char ed_str[] = "end_device-";
799
800         if (!pp->udev || mpp->dev_loss == DEV_LOSS_TMO_UNSET)
801                 return;
802
803         for (parent = udev_device_get_parent(pp->udev);
804              parent;
805              parent = udev_device_get_parent(parent)) {
806                 const char *ed = udev_device_get_sysname(parent);
807
808                 if (ed && !strncmp(ed, ed_str, sizeof(ed_str) - 1)) {
809                         end_dev_id = ed;
810                         break;
811                 }
812         }
813         if (!end_dev_id) {
814                 condlog(1, "%s: No SAS end device", pp->dev);
815                 return;
816         }
817         sas_dev = udev_device_new_from_subsystem_sysname(udev,
818                                 "sas_end_device", end_dev_id);
819         if (!sas_dev) {
820                 condlog(1, "%s: No SAS end device for '%s'", pp->dev,
821                         end_dev_id);
822                 return;
823         }
824         condlog(4, "target%d:%d:%d -> %s", pp->sg_id.host_no,
825                 pp->sg_id.channel, pp->sg_id.scsi_id, end_dev_id);
826
827         if (mpp->dev_loss != DEV_LOSS_TMO_UNSET) {
828                 snprintf(value, 11, "%u", mpp->dev_loss);
829                 if (sysfs_attr_set_value(sas_dev, "I_T_nexus_loss_timeout",
830                                          value, strlen(value)) <= 0)
831                         condlog(3, "%s: failed to update "
832                                 "I_T Nexus loss timeout, error %d",
833                                 pp->dev, errno);
834         }
835         udev_device_unref(sas_dev);
836         return;
837 }
838
839 int
840 sysfs_set_scsi_tmo (struct multipath *mpp, unsigned int checkint)
841 {
842         struct path *pp;
843         int i;
844         unsigned int dev_loss_tmo = mpp->dev_loss;
845         struct path *err_path = NULL;
846         STATIC_BITFIELD(bf, LAST_BUS_PROTOCOL_ID + 1);
847
848         if (mpp->no_path_retry > 0) {
849                 uint64_t no_path_retry_tmo =
850                         (uint64_t)mpp->no_path_retry * checkint;
851
852                 if (no_path_retry_tmo > MAX_DEV_LOSS_TMO)
853                         no_path_retry_tmo = MAX_DEV_LOSS_TMO;
854                 if (no_path_retry_tmo > dev_loss_tmo)
855                         dev_loss_tmo = no_path_retry_tmo;
856         } else if (mpp->no_path_retry == NO_PATH_RETRY_QUEUE) {
857                 dev_loss_tmo = MAX_DEV_LOSS_TMO;
858         }
859         if (mpp->dev_loss != DEV_LOSS_TMO_UNSET &&
860             mpp->dev_loss != dev_loss_tmo) {
861                 condlog(2, "%s: Using dev_loss_tmo=%u instead of %u because of no_path_retry setting",
862                         mpp->alias, dev_loss_tmo, mpp->dev_loss);
863                 mpp->dev_loss = dev_loss_tmo;
864         }
865         if (mpp->dev_loss != DEV_LOSS_TMO_UNSET &&
866             mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET &&
867             (unsigned int)mpp->fast_io_fail >= mpp->dev_loss) {
868                 condlog(3, "%s: turning off fast_io_fail (%d is not smaller than dev_loss_tmo)",
869                         mpp->alias, mpp->fast_io_fail);
870                 mpp->fast_io_fail = MP_FAST_IO_FAIL_OFF;
871         }
872         if (mpp->dev_loss == DEV_LOSS_TMO_UNSET &&
873             mpp->fast_io_fail == MP_FAST_IO_FAIL_UNSET &&
874             mpp->eh_deadline == EH_DEADLINE_UNSET)
875                 return 0;
876
877         vector_foreach_slot(mpp->paths, pp, i) {
878                 if (pp->bus != SYSFS_BUS_SCSI) {
879                         if (!err_path)
880                                 err_path = pp;
881                         continue;
882                 }
883
884                 switch (pp->sg_id.proto_id) {
885                 case SCSI_PROTOCOL_FCP:
886                         sysfs_set_rport_tmo(mpp, pp);
887                         break;
888                 case SCSI_PROTOCOL_ISCSI:
889                         sysfs_set_session_tmo(mpp, pp);
890                         break;
891                 case SCSI_PROTOCOL_SAS:
892                         sysfs_set_nexus_loss_tmo(mpp, pp);
893                         break;
894                 default:
895                         if (!err_path)
896                                 err_path = pp;
897                 }
898                 sysfs_set_eh_deadline(mpp, pp);
899         }
900
901         if (err_path && !is_bit_set_in_bitfield(bus_protocol_id(pp), bf)) {
902                 STRBUF_ON_STACK(proto_buf);
903
904                 snprint_path_protocol(&proto_buf, err_path);
905                 condlog(2, "%s: setting dev_loss_tmo is unsupported for protocol %s",
906                         mpp->alias, get_strbuf_str(&proto_buf));
907                 set_bit_in_bitfield(bus_protocol_id(pp), bf);
908         }
909         return 0;
910 }
911
912 int
913 do_inq(int sg_fd, int cmddt, int evpd, unsigned int pg_op,
914        void *resp, int mx_resp_len)
915 {
916         unsigned char inqCmdBlk[INQUIRY_CMDLEN] =
917                 { INQUIRY_CMD, 0, 0, 0, 0, 0 };
918         unsigned char sense_b[SENSE_BUFF_LEN];
919         struct sg_io_hdr io_hdr;
920
921         if (cmddt)
922                 inqCmdBlk[1] |= 2;
923         if (evpd)
924                 inqCmdBlk[1] |= 1;
925         inqCmdBlk[2] = (unsigned char) pg_op;
926         inqCmdBlk[3] = (unsigned char)((mx_resp_len >> 8) & 0xff);
927         inqCmdBlk[4] = (unsigned char) (mx_resp_len & 0xff);
928         memset(&io_hdr, 0, sizeof (struct sg_io_hdr));
929         memset(sense_b, 0, SENSE_BUFF_LEN);
930         io_hdr.interface_id = 'S';
931         io_hdr.cmd_len = sizeof (inqCmdBlk);
932         io_hdr.mx_sb_len = sizeof (sense_b);
933         io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
934         io_hdr.dxfer_len = mx_resp_len;
935         io_hdr.dxferp = resp;
936         io_hdr.cmdp = inqCmdBlk;
937         io_hdr.sbp = sense_b;
938         io_hdr.timeout = DEF_TIMEOUT * 1000;
939
940         if (ioctl(sg_fd, SG_IO, &io_hdr) < 0)
941                 return -1;
942
943         /* treat SG_ERR here to get rid of sg_err.[ch] */
944         io_hdr.status &= 0x7e;
945         if ((0 == io_hdr.status) && (0 == io_hdr.host_status) &&
946             (0 == io_hdr.driver_status))
947                 return 0;
948         if ((SCSI_CHECK_CONDITION == io_hdr.status) ||
949             (SCSI_COMMAND_TERMINATED == io_hdr.status) ||
950             (SG_ERR_DRIVER_SENSE == (0xf & io_hdr.driver_status))) {
951                 if (io_hdr.sbp && (io_hdr.sb_len_wr > 2)) {
952                         int sense_key;
953                         unsigned char * sense_buffer = io_hdr.sbp;
954                         if (sense_buffer[0] & 0x2)
955                                 sense_key = sense_buffer[1] & 0xf;
956                         else
957                                 sense_key = sense_buffer[2] & 0xf;
958                         if(RECOVERED_ERROR == sense_key)
959                                 return 0;
960                 }
961         }
962         return -1;
963 }
964
965 static int
966 get_serial (char * str, int maxlen, int fd)
967 {
968         int len = 0;
969         char buff[MX_ALLOC_LEN + 1] = {0};
970
971         if (fd < 0)
972                 return 1;
973
974         if (0 == do_inq(fd, 0, 1, 0x80, buff, MX_ALLOC_LEN)) {
975                 len = buff[3];
976                 if (len >= maxlen)
977                         return 1;
978                 if (len > 0) {
979                         memcpy(str, buff + 4, len);
980                         str[len] = '\0';
981                 }
982                 return 0;
983         }
984         return 1;
985 }
986
987 /*
988  * Side effect: sets pp->tpgs if it could be determined.
989  * If ALUA calls fail because paths are unreachable, pp->tpgs remains unchanged.
990  */
991 static void
992 detect_alua(struct path * pp)
993 {
994         int ret;
995         int tpgs;
996         unsigned int timeout;
997
998
999         if (pp->bus != SYSFS_BUS_SCSI) {
1000                 pp->tpgs = TPGS_NONE;
1001                 return;
1002         }
1003
1004         if (sysfs_get_timeout(pp, &timeout) <= 0)
1005                 timeout = DEF_TIMEOUT;
1006
1007         tpgs = get_target_port_group_support(pp, timeout);
1008         if (tpgs == -RTPG_INQUIRY_FAILED)
1009                 return;
1010         else if (tpgs <= 0) {
1011                 pp->tpgs = TPGS_NONE;
1012                 return;
1013         }
1014
1015         if (pp->fd == -1 || pp->offline)
1016                 return;
1017
1018         ret = get_target_port_group(pp, timeout);
1019         if (ret < 0 || get_asymmetric_access_state(pp, ret, timeout) < 0) {
1020                 int state;
1021
1022                 if (ret == -RTPG_INQUIRY_FAILED)
1023                         return;
1024
1025                 state = path_offline(pp);
1026                 if (state == PATH_DOWN || state == PATH_PENDING)
1027                         return;
1028
1029                 pp->tpgs = TPGS_NONE;
1030                 return;
1031         }
1032         pp->tpgs = tpgs;
1033 }
1034
1035 int path_get_tpgs(struct path *pp)
1036 {
1037         if (pp->tpgs == TPGS_UNDEF)
1038                 detect_alua(pp);
1039         return pp->tpgs;
1040 }
1041
1042 #define DEFAULT_SGIO_LEN 254
1043
1044 /* Query VPD page @pg. Returns number of INQUIRY bytes
1045    upon success and -1 upon failure. */
1046 static int
1047 sgio_get_vpd (unsigned char * buff, int maxlen, int fd, int pg)
1048 {
1049         int len = DEFAULT_SGIO_LEN;
1050         int rlen;
1051
1052         if (fd < 0) {
1053                 errno = EBADF;
1054                 return -1;
1055         }
1056 retry:
1057         if (0 == do_inq(fd, 0, 1, pg, buff, len)) {
1058                 rlen = get_unaligned_be16(&buff[2]) + 4;
1059                 if (rlen <= len || len >= maxlen)
1060                         return rlen;
1061                 len = (rlen < maxlen)? rlen : maxlen;
1062                 goto retry;
1063         }
1064         return -1;
1065 }
1066
1067 static int
1068 get_geometry(struct path *pp)
1069 {
1070         if (pp->fd < 0)
1071                 return 1;
1072
1073         if (ioctl(pp->fd, HDIO_GETGEO, &pp->geom)) {
1074                 condlog(2, "%s: HDIO_GETGEO failed with %d", pp->dev, errno);
1075                 memset(&pp->geom, 0, sizeof(pp->geom));
1076                 return 1;
1077         }
1078         condlog(3, "%s: %u cyl, %u heads, %u sectors/track, start at %lu",
1079                 pp->dev, pp->geom.cylinders, pp->geom.heads,
1080                 pp->geom.sectors, pp->geom.start);
1081         return 0;
1082 }
1083
1084 static int
1085 parse_vpd_pg80(const unsigned char *in, char *out, size_t out_len)
1086 {
1087         size_t len = get_unaligned_be16(&in[2]);
1088
1089         if (out_len == 0)
1090                 return 0;
1091
1092         if (len > WWID_SIZE)
1093                 len = WWID_SIZE;
1094         /*
1095          * Strip leading and trailing whitespace
1096          */
1097         while (len > 0 && in[len + 3] == ' ')
1098                 --len;
1099         while (len > 0 && in[4] == ' ') {
1100                 ++in;
1101                 --len;
1102         }
1103
1104         if (len >= out_len) {
1105                 condlog(2, "vpd pg80 overflow, %zu/%zu bytes required",
1106                         len + 1, out_len);
1107                 len = out_len - 1;
1108         }
1109         if (len > 0) {
1110                 memcpy(out, in + 4, len);
1111                 out[len] = '\0';
1112         }
1113         return len;
1114 }
1115
1116 static int
1117 parse_vpd_pg83(const unsigned char *in, size_t in_len,
1118                char *out, size_t out_len)
1119 {
1120         const unsigned char *d;
1121         const unsigned char *vpd = NULL;
1122         size_t len, vpd_len, i;
1123         int vpd_type, prio = -1;
1124         int err = -ENODATA;
1125         STRBUF_ON_STACK(buf);
1126
1127         /* Need space at least for one digit */
1128         if (out_len <= 1)
1129                 return 0;
1130
1131         d = in + 4;
1132         while (d <= in + in_len - 4) {
1133                 bool invalid = false;
1134                 int new_prio = -1;
1135
1136                 /* Select 'association: LUN' */
1137                 if ((d[1] & 0x30) == 0x30) {
1138                         invalid = true;
1139                         goto next_designator;
1140                 } else if ((d[1] & 0x30) != 0x00)
1141                         goto next_designator;
1142
1143                 switch (d[1] & 0xf) {
1144                         unsigned char good_len;
1145                 case 0x3:
1146                         /* NAA: Prio 5 */
1147                         switch (d[4] >> 4) {
1148                         case 6:
1149                                 /* IEEE Registered Extended: Prio 8 */
1150                                 new_prio = 8;
1151                                 good_len = 16;
1152                                 break;
1153                         case 5:
1154                                 /* IEEE Registered: Prio 7 */
1155                                 new_prio = 7;
1156                                 good_len = 8;
1157                                 break;
1158                         case 2:
1159                                 /* IEEE Extended: Prio 6 */
1160                                 new_prio = 6;
1161                                 good_len = 8;
1162                                 break;
1163                         case 3:
1164                                 /* IEEE Locally assigned: Prio 1 */
1165                                 new_prio = 1;
1166                                 good_len = 8;
1167                                 break;
1168                         default:
1169                                 /* Default: no priority */
1170                                 good_len = 0xff;
1171                                 break;
1172                         }
1173
1174                         invalid = good_len == 0xff || good_len != d[3];
1175                         break;
1176                 case 0x2:
1177                         /* EUI-64: Prio 4 */
1178                         invalid = (d[3] != 8 && d[3] != 12 && d[3] != 16);
1179                         new_prio = 4;
1180                         break;
1181                 case 0x8:
1182                         /* SCSI Name: Prio 3 */
1183                         invalid = (d[3] < 4 || (memcmp(d + 4, "eui.", 4) &&
1184                                                 memcmp(d + 4, "naa.", 4) &&
1185                                                 memcmp(d + 4, "iqn.", 4)));
1186                         new_prio = 3;
1187                         break;
1188                 case 0x1:
1189                         /* T-10 Vendor ID: Prio 2 */
1190                         invalid = (d[3] < 8);
1191                         new_prio = 2;
1192                         break;
1193                 case 0xa:
1194                         condlog(2, "%s: UUID identifiers not yet supported",
1195                                 __func__);
1196                         break;
1197                 default:
1198                         invalid = true;
1199                         break;
1200                 }
1201
1202         next_designator:
1203                 if (d + d[3] + 4 - in > (ssize_t)in_len) {
1204                         condlog(2, "%s: device descriptor length overflow: %zd > %zu",
1205                                 __func__, d + d[3] + 4 - in, in_len);
1206                         err = -EOVERFLOW;
1207                         break;
1208                 } else if (invalid) {
1209                         condlog(2, "%s: invalid device designator at offset %zd: %02x%02x%02x%02x",
1210                                 __func__, d - in, d[0], d[1], d[2], d[3]);
1211                         /*
1212                          * We checked above that the next offset is within limits.
1213                          * Proceed, fingers crossed.
1214                          */
1215                         err = -EINVAL;
1216                 } else if (new_prio > prio) {
1217                         vpd = d;
1218                         prio = new_prio;
1219                 }
1220                 d += d[3] + 4;
1221         }
1222
1223         if (prio <= 0)
1224                 return err;
1225
1226         if (d != in + in_len)
1227                 /* Should this be fatal? (overflow covered above) */
1228                 condlog(2, "%s: warning: last descriptor end %zd != VPD length %zu",
1229                         __func__, d - in, in_len);
1230
1231         len = 0;
1232         vpd_type = vpd[1] & 0xf;
1233         vpd_len = vpd[3];
1234         vpd += 4;
1235         /* untaint vpd_len for coverity */
1236         if (vpd_len > WWID_SIZE) {
1237                 condlog(1, "%s: suspicious designator length %zu truncated to %u",
1238                         __func__, vpd_len, WWID_SIZE);
1239                 vpd_len = WWID_SIZE;
1240         }
1241         if (vpd_type == 0x2 || vpd_type == 0x3) {
1242                 size_t i;
1243
1244                 if ((err = print_strbuf(&buf, "%d", vpd_type)) < 0)
1245                         return err;
1246                 for (i = 0; i < vpd_len; i++)
1247                         if ((err = print_strbuf(&buf, "%02x", vpd[i])) < 0)
1248                                 return err;
1249         } else if (vpd_type == 0x8) {
1250                 char type;
1251
1252                 if (!memcmp("eui.", vpd, 4))
1253                         type =  '2';
1254                 else if (!memcmp("naa.", vpd, 4))
1255                         type = '3';
1256                 else
1257                         type = '8';
1258                 if ((err = fill_strbuf(&buf, type, 1)) < 0)
1259                         return err;
1260
1261                 vpd += 4;
1262                 len = vpd_len - 4;
1263                 if ((err = __append_strbuf_str(&buf, (const char *)vpd, len)) < 0)
1264                         return err;
1265
1266                 /* The input is 0-padded, make sure the length is correct */
1267                 truncate_strbuf(&buf, strlen(get_strbuf_str(&buf)));
1268                 len = get_strbuf_len(&buf);
1269                 if (type != '8') {
1270                         char *buffer = __get_strbuf_buf(&buf);
1271
1272                         for (i = 0; i < len; ++i)
1273                                 buffer[i] = tolower(buffer[i]);
1274                 }
1275
1276         } else if (vpd_type == 0x1) {
1277                 const unsigned char *p;
1278                 size_t p_len;
1279
1280                 if ((err = fill_strbuf(&buf, '1', 1)) < 0)
1281                         return err;
1282                 while (vpd && (p = memchr(vpd, ' ', vpd_len))) {
1283                         p_len = p - vpd;
1284                         if ((err = __append_strbuf_str(&buf, (const char *)vpd,
1285                                                        p_len)) < 0)
1286                                 return err;
1287                         vpd = p;
1288                         vpd_len -= p_len;
1289                         while (vpd && vpd_len > 0 && *vpd == ' ') {
1290                                 vpd++;
1291                                 vpd_len --;
1292                         }
1293                         if (vpd_len > 0 && (err = fill_strbuf(&buf, '_', 1)) < 0)
1294                                 return err;
1295                 }
1296                 if (vpd_len > 0) {
1297                         if ((err = __append_strbuf_str(&buf, (const char *)vpd,
1298                                                        vpd_len)) < 0)
1299                                 return err;
1300                 }
1301         }
1302
1303         len = get_strbuf_len(&buf);
1304         if (len >= out_len) {
1305                 condlog(1, "%s: WWID overflow, type %d, %zu/%zu bytes required",
1306                         __func__, vpd_type, len, out_len);
1307                 if (vpd_type == 2 || vpd_type == 3)
1308                         /* designator must have an even number of characters */
1309                         len = 2 * (out_len / 2) - 1;
1310                 else
1311                         len = out_len - 1;
1312         }
1313         strlcpy(out, get_strbuf_str(&buf), len + 1);
1314         return len;
1315 }
1316
1317 static int
1318 parse_vpd_c0_hp3par(const unsigned char *in, size_t in_len,
1319                     char *out, size_t out_len)
1320 {
1321         size_t len;
1322
1323         memset(out, 0x0, out_len);
1324         if (in_len <= 4 || (in[4] > 3 && in_len < 44)) {
1325                 condlog(3, "HP/3PAR vendor specific VPD page length too short: %zu", in_len);
1326                 return -EINVAL;
1327         }
1328         if (in[4] <= 3) /* revision must be > 3 to have Vomlume Name */
1329                 return -ENODATA;
1330         len = get_unaligned_be32(&in[40]);
1331         if (len > out_len || len + 44 > in_len) {
1332                 condlog(3, "HP/3PAR vendor specific Volume name too long: %zu",
1333                         len);
1334                 return -EINVAL;
1335         }
1336         memcpy(out, &in[44], len);
1337         out[out_len - 1] = '\0';
1338         return len;
1339 }
1340
1341 static int
1342 get_vpd_sysfs (struct udev_device *parent, int pg, char * str, int maxlen)
1343 {
1344         int len;
1345         size_t buff_len;
1346         unsigned char buff[VPD_BUFLEN];
1347
1348         memset(buff, 0x0, VPD_BUFLEN);
1349         if (!parent || sysfs_get_vpd(parent, pg, buff, VPD_BUFLEN) <= 0) {
1350                 condlog(3, "failed to read sysfs vpd pg%02x", pg);
1351                 return -EINVAL;
1352         }
1353
1354         if (buff[1] != pg) {
1355                 condlog(3, "vpd pg%02x error, invalid vpd page %02x",
1356                         pg, buff[1]);
1357                 return -ENODATA;
1358         }
1359         buff_len = get_unaligned_be16(&buff[2]) + 4;
1360         if (buff_len > VPD_BUFLEN) {
1361                 condlog(3, "vpd pg%02x page truncated", pg);
1362                 buff_len = VPD_BUFLEN;
1363         }
1364
1365         if (pg == 0x80)
1366                 len = parse_vpd_pg80(buff, str, maxlen);
1367         else if (pg == 0x83)
1368                 len = parse_vpd_pg83(buff, buff_len, str, maxlen);
1369         else
1370                 len = -ENOSYS;
1371
1372         return len;
1373 }
1374
1375 static int
1376 fetch_vpd_page(int fd, int pg, unsigned char *buff, int maxlen)
1377 {
1378         int buff_len;
1379
1380         memset(buff, 0x0, maxlen);
1381         if (sgio_get_vpd(buff, maxlen, fd, pg) < 0) {
1382                 int lvl = pg == 0x80 || pg == 0x83 ? 3 : 4;
1383
1384                 condlog(lvl, "failed to issue vpd inquiry for pg%02x",
1385                         pg);
1386                 return -errno;
1387         }
1388
1389         if (buff[1] != pg) {
1390                 condlog(3, "vpd pg%02x error, invalid vpd page %02x",
1391                         pg, buff[1]);
1392                 return -ENODATA;
1393         }
1394         buff_len = get_unaligned_be16(&buff[2]) + 4;
1395         if (buff_len > maxlen) {
1396                 condlog(3, "vpd pg%02x page truncated", pg);
1397                 buff_len = maxlen;
1398         }
1399         return buff_len;
1400 }
1401
1402 /* based on sg_inq.c from sg3_utils */
1403 bool
1404 is_vpd_page_supported(int fd, int pg)
1405 {
1406         int i, len;
1407         unsigned char buff[VPD_BUFLEN];
1408
1409         len = fetch_vpd_page(fd, 0x00, buff, sizeof(buff));
1410         if (len < 0)
1411                 return false;
1412
1413         for (i = 4; i < len; ++i)
1414                 if (buff[i] == pg)
1415                         return true;
1416         return false;
1417 }
1418
1419 int
1420 get_vpd_sgio (int fd, int pg, int vend_id, char * str, int maxlen)
1421 {
1422         int len, buff_len;
1423         unsigned char buff[VPD_BUFLEN];
1424
1425         buff_len = fetch_vpd_page(fd, pg, buff, sizeof(buff));
1426         if (buff_len < 0)
1427                 return buff_len;
1428         if (pg == 0x80)
1429                 len = parse_vpd_pg80(buff, str, maxlen);
1430         else if (pg == 0x83)
1431                 len = parse_vpd_pg83(buff, buff_len, str, maxlen);
1432         else if (pg == 0xc9 && maxlen >= 8) {
1433                 if (buff_len < 8)
1434                         len = -ENODATA;
1435                 else {
1436                         len = (buff_len <= maxlen)? buff_len : maxlen;
1437                         memcpy (str, buff, len);
1438                 }
1439         } else if (pg == 0xc0 && vend_id == VPD_VP_HP3PAR)
1440                 len = parse_vpd_c0_hp3par(buff, buff_len, str, maxlen);
1441         else
1442                 len = -ENOSYS;
1443
1444         return len;
1445 }
1446
1447 static int
1448 scsi_sysfs_pathinfo (struct path *pp, const struct _vector *hwtable)
1449 {
1450         struct udev_device *parent;
1451         const char *attr_path = NULL;
1452
1453         parent = pp->udev;
1454         while (parent) {
1455                 const char *subsys = udev_device_get_subsystem(parent);
1456                 if (subsys && !strncmp(subsys, "scsi", 4)) {
1457                         attr_path = udev_device_get_sysname(parent);
1458                         if (!attr_path)
1459                                 break;
1460                         if (sscanf(attr_path, "%i:%i:%i:%" SCNu64,
1461                                    &pp->sg_id.host_no,
1462                                    &pp->sg_id.channel,
1463                                    &pp->sg_id.scsi_id,
1464                                    &pp->sg_id.lun) == 4)
1465                                 break;
1466                 }
1467                 parent = udev_device_get_parent(parent);
1468         }
1469         if (!attr_path || pp->sg_id.host_no == -1)
1470                 return PATHINFO_FAILED;
1471
1472         if (sysfs_get_vendor(parent, pp->vendor_id, SCSI_VENDOR_SIZE) <= 0)
1473                 return PATHINFO_FAILED;;
1474
1475         condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
1476
1477         if (sysfs_get_model(parent, pp->product_id, PATH_PRODUCT_SIZE) <= 0)
1478                 return PATHINFO_FAILED;;
1479
1480         condlog(3, "%s: product = %s", pp->dev, pp->product_id);
1481
1482         if (sysfs_get_rev(parent, pp->rev, PATH_REV_SIZE) < 0)
1483                 return PATHINFO_FAILED;;
1484
1485         condlog(3, "%s: rev = %s", pp->dev, pp->rev);
1486
1487         /*
1488          * set the hwe configlet pointer
1489          */
1490         find_hwe(hwtable, pp->vendor_id, pp->product_id, pp->rev, pp->hwe);
1491
1492         /*
1493          * host / bus / target / lun
1494          */
1495         condlog(3, "%s: h:b:t:l = %i:%i:%i:%" PRIu64,
1496                         pp->dev,
1497                         pp->sg_id.host_no,
1498                         pp->sg_id.channel,
1499                         pp->sg_id.scsi_id,
1500                         pp->sg_id.lun);
1501
1502         /*
1503          * target node name
1504          */
1505         if(sysfs_get_tgt_nodename(pp, pp->tgt_node_name))
1506                 return PATHINFO_FAILED;
1507
1508         condlog(3, "%s: tgt_node_name = %s",
1509                 pp->dev, pp->tgt_node_name);
1510
1511         return PATHINFO_OK;
1512 }
1513
1514 static int
1515 nvme_sysfs_pathinfo (struct path *pp, const struct _vector *hwtable)
1516 {
1517         struct udev_device *parent;
1518         const char *attr_path = NULL;
1519         const char *attr;
1520
1521         if (pp->udev)
1522                 attr_path = udev_device_get_sysname(pp->udev);
1523         if (!attr_path)
1524                 return PATHINFO_FAILED;
1525
1526         if (sscanf(attr_path, "nvme%dn%d",
1527                    &pp->sg_id.host_no,
1528                    &pp->sg_id.scsi_id) != 2)
1529                 return PATHINFO_FAILED;
1530
1531         parent = udev_device_get_parent_with_subsystem_devtype(pp->udev,
1532                                                                "nvme", NULL);
1533         if (!parent)
1534                 return PATHINFO_SKIPPED;
1535
1536         attr = udev_device_get_sysattr_value(pp->udev, "nsid");
1537         pp->sg_id.lun = attr ? atoi(attr) : 0;
1538
1539         attr = udev_device_get_sysattr_value(parent, "cntlid");
1540         pp->sg_id.channel = attr ? atoi(attr) : 0;
1541
1542         snprintf(pp->vendor_id, SCSI_VENDOR_SIZE, "NVME");
1543         snprintf(pp->product_id, PATH_PRODUCT_SIZE, "%s",
1544                  udev_device_get_sysattr_value(parent, "model"));
1545         snprintf(pp->serial, SERIAL_SIZE, "%s",
1546                  udev_device_get_sysattr_value(parent, "serial"));
1547         snprintf(pp->rev, PATH_REV_SIZE, "%s",
1548                  udev_device_get_sysattr_value(parent, "firmware_rev"));
1549
1550         condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
1551         condlog(3, "%s: product = %s", pp->dev, pp->product_id);
1552         condlog(3, "%s: serial = %s", pp->dev, pp->serial);
1553         condlog(3, "%s: rev = %s", pp->dev, pp->rev);
1554
1555         find_hwe(hwtable, pp->vendor_id, pp->product_id, NULL, pp->hwe);
1556
1557         return PATHINFO_OK;
1558 }
1559
1560 static int
1561 ccw_sysfs_pathinfo (struct path *pp, const struct _vector *hwtable)
1562 {
1563         struct udev_device *parent;
1564         char attr_buff[NAME_SIZE];
1565         const char *attr_path;
1566
1567         parent = pp->udev;
1568         while (parent) {
1569                 const char *subsys = udev_device_get_subsystem(parent);
1570                 if (subsys && !strncmp(subsys, "ccw", 3))
1571                         break;
1572                 parent = udev_device_get_parent(parent);
1573         }
1574         if (!parent)
1575                 return PATHINFO_FAILED;
1576
1577         sprintf(pp->vendor_id, "IBM");
1578
1579         condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
1580
1581         if (sysfs_get_devtype(parent, attr_buff, FILE_NAME_SIZE) <= 0)
1582                 return PATHINFO_FAILED;
1583
1584         if (!strncmp(attr_buff, "3370", 4)) {
1585                 sprintf(pp->product_id,"S/390 DASD FBA");
1586         } else if (!strncmp(attr_buff, "9336", 4)) {
1587                 sprintf(pp->product_id,"S/390 DASD FBA");
1588         } else {
1589                 sprintf(pp->product_id,"S/390 DASD ECKD");
1590         }
1591
1592         condlog(3, "%s: product = %s", pp->dev, pp->product_id);
1593
1594         /*
1595          * set the hwe configlet pointer
1596          */
1597         find_hwe(hwtable, pp->vendor_id, pp->product_id, NULL, pp->hwe);
1598
1599         /*
1600          * host / bus / target / lun
1601          */
1602         attr_path = udev_device_get_sysname(parent);
1603         if (!attr_path)
1604                 return PATHINFO_FAILED;
1605         pp->sg_id.lun = 0;
1606         if (sscanf(attr_path, "%i.%i.%x",
1607                    &pp->sg_id.host_no,
1608                    &pp->sg_id.channel,
1609                    &pp->sg_id.scsi_id) == 3) {
1610                 condlog(3, "%s: h:b:t:l = %i:%i:%i:%" PRIu64,
1611                         pp->dev,
1612                         pp->sg_id.host_no,
1613                         pp->sg_id.channel,
1614                         pp->sg_id.scsi_id,
1615                         pp->sg_id.lun);
1616         }
1617
1618         return PATHINFO_OK;
1619 }
1620
1621 static int
1622 cciss_sysfs_pathinfo (struct path *pp, const struct _vector *hwtable)
1623 {
1624         const char * attr_path = NULL;
1625         struct udev_device *parent;
1626
1627         parent = pp->udev;
1628         while (parent) {
1629                 const char *subsys = udev_device_get_subsystem(parent);
1630                 if (subsys && !strncmp(subsys, "cciss", 5)) {
1631                         attr_path = udev_device_get_sysname(parent);
1632                         if (!attr_path)
1633                                 break;
1634                         if (sscanf(attr_path, "c%id%i",
1635                                    &pp->sg_id.host_no,
1636                                    &pp->sg_id.scsi_id) == 2)
1637                                 break;
1638                 }
1639                 parent = udev_device_get_parent(parent);
1640         }
1641         if (!attr_path || pp->sg_id.host_no == -1)
1642                 return PATHINFO_FAILED;
1643
1644         if (sysfs_get_vendor(parent, pp->vendor_id, SCSI_VENDOR_SIZE) <= 0)
1645                 return PATHINFO_FAILED;
1646
1647         condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
1648
1649         if (sysfs_get_model(parent, pp->product_id, PATH_PRODUCT_SIZE) <= 0)
1650                 return PATHINFO_FAILED;
1651
1652         condlog(3, "%s: product = %s", pp->dev, pp->product_id);
1653
1654         if (sysfs_get_rev(parent, pp->rev, PATH_REV_SIZE) <= 0)
1655                 return PATHINFO_FAILED;
1656
1657         condlog(3, "%s: rev = %s", pp->dev, pp->rev);
1658
1659         /*
1660          * set the hwe configlet pointer
1661          */
1662         find_hwe(hwtable, pp->vendor_id, pp->product_id, pp->rev, pp->hwe);
1663
1664         /*
1665          * host / bus / target / lun
1666          */
1667         pp->sg_id.lun = 0;
1668         pp->sg_id.channel = 0;
1669         condlog(3, "%s: h:b:t:l = %i:%i:%i:%" PRIu64,
1670                 pp->dev,
1671                 pp->sg_id.host_no,
1672                 pp->sg_id.channel,
1673                 pp->sg_id.scsi_id,
1674                 pp->sg_id.lun);
1675
1676         return PATHINFO_OK;
1677 }
1678
1679 static int
1680 common_sysfs_pathinfo (struct path * pp)
1681 {
1682         dev_t devt;
1683
1684         if (!pp)
1685                 return PATHINFO_FAILED;
1686
1687         if (!pp->udev) {
1688                 condlog(4, "%s: udev not initialised", pp->dev);
1689                 return PATHINFO_FAILED;
1690         }
1691         devt = udev_device_get_devnum(pp->udev);
1692         if (major(devt) == 0 && minor(devt) == 0)
1693                 return PATHINFO_FAILED;
1694
1695         snprintf(pp->dev_t, BLK_DEV_SIZE, "%d:%d", major(devt), minor(devt));
1696
1697         condlog(4, "%s: dev_t = %s", pp->dev, pp->dev_t);
1698
1699         if (sysfs_get_size(pp, &pp->size))
1700                 return PATHINFO_FAILED;
1701
1702         condlog(3, "%s: size = %llu", pp->dev, pp->size);
1703
1704         return PATHINFO_OK;
1705 }
1706
1707 int
1708 path_offline (struct path * pp)
1709 {
1710         struct udev_device * parent;
1711         char buff[SCSI_STATE_SIZE];
1712         int err;
1713         const char *subsys_type;
1714
1715         if (pp->bus == SYSFS_BUS_SCSI) {
1716                 subsys_type = "scsi";
1717         }
1718         else if (pp->bus == SYSFS_BUS_NVME) {
1719                 subsys_type = "nvme";
1720         }
1721         else {
1722                 return PATH_UP;
1723         }
1724
1725         parent = pp->udev;
1726         while (parent) {
1727                 const char *subsys = udev_device_get_subsystem(parent);
1728                 if (subsys && !strncmp(subsys, subsys_type, 4))
1729                         break;
1730                 parent = udev_device_get_parent(parent);
1731         }
1732
1733         if (!parent) {
1734                 condlog(1, "%s: failed to get sysfs information", pp->dev);
1735                 return PATH_REMOVED;
1736         }
1737
1738         memset(buff, 0x0, SCSI_STATE_SIZE);
1739         err = sysfs_attr_get_value(parent, "state", buff, SCSI_STATE_SIZE);
1740         if (err <= 0) {
1741                 if (err == -ENXIO)
1742                         return PATH_REMOVED;
1743                 else
1744                         return PATH_DOWN;
1745         }
1746
1747
1748         condlog(4, "%s: path state = %s", pp->dev, buff);
1749
1750         if (pp->bus == SYSFS_BUS_SCSI) {
1751                 if (!strncmp(buff, "offline", 7)) {
1752                         pp->offline = 1;
1753                         return PATH_DOWN;
1754                 }
1755                 pp->offline = 0;
1756                 if (!strncmp(buff, "blocked", 7) ||
1757                     !strncmp(buff, "quiesce", 7))
1758                         return PATH_PENDING;
1759                 else if (!strncmp(buff, "running", 7))
1760                         return PATH_UP;
1761
1762         }
1763         else if (pp->bus == SYSFS_BUS_NVME) {
1764                 if (!strncmp(buff, "dead", 4)) {
1765                         pp->offline = 1;
1766                         return PATH_DOWN;
1767                 }
1768                 pp->offline = 0;
1769                 if (!strncmp(buff, "new", 3) ||
1770                     !strncmp(buff, "deleting", 8))
1771                         return PATH_PENDING;
1772                 else if (!strncmp(buff, "live", 4))
1773                         return PATH_UP;
1774         }
1775
1776         return PATH_DOWN;
1777 }
1778
1779 static int
1780 sysfs_pathinfo(struct path *pp, const struct _vector *hwtable)
1781 {
1782         int r = common_sysfs_pathinfo(pp);
1783
1784         if (r != PATHINFO_OK)
1785                 return r;
1786
1787         pp->bus = SYSFS_BUS_UNDEF;
1788         if (!strncmp(pp->dev,"cciss",5))
1789                 pp->bus = SYSFS_BUS_CCISS;
1790         if (!strncmp(pp->dev,"dasd", 4))
1791                 pp->bus = SYSFS_BUS_CCW;
1792         if (!strncmp(pp->dev,"sd", 2))
1793                 pp->bus = SYSFS_BUS_SCSI;
1794         if (!strncmp(pp->dev,"nvme", 4))
1795                 pp->bus = SYSFS_BUS_NVME;
1796
1797         switch (pp->bus) {
1798         case SYSFS_BUS_SCSI:
1799                 return scsi_sysfs_pathinfo(pp, hwtable);
1800         case SYSFS_BUS_CCW:
1801                 return ccw_sysfs_pathinfo(pp, hwtable);
1802         case SYSFS_BUS_CCISS:
1803                 return cciss_sysfs_pathinfo(pp, hwtable);
1804         case SYSFS_BUS_NVME:
1805                 return nvme_sysfs_pathinfo(pp, hwtable);
1806         case SYSFS_BUS_UNDEF:
1807         default:
1808                 return PATHINFO_OK;
1809         }
1810 }
1811
1812 static void
1813 scsi_ioctl_pathinfo (struct path * pp, int mask)
1814 {
1815         struct udev_device *parent;
1816         const char *attr_path = NULL;
1817         int vpd_id;
1818
1819         if (!(mask & DI_SERIAL))
1820                 return;
1821
1822         select_vpd_vendor_id(pp);
1823         vpd_id = pp->vpd_vendor_id;
1824
1825         if (vpd_id != VPD_VP_UNDEF) {
1826                 char vpd_data[VPD_DATA_SIZE] = {0};
1827
1828                 if (get_vpd_sgio(pp->fd, vpd_vendor_pages[vpd_id].pg, vpd_id,
1829                     vpd_data, sizeof(vpd_data)) < 0)
1830                         condlog(3, "%s: failed to get extra vpd data", pp->dev);
1831                 else {
1832                         vpd_data[VPD_DATA_SIZE - 1] = '\0';
1833                         if (pp->vpd_data)
1834                                 free(pp->vpd_data);
1835                         pp->vpd_data = strdup(vpd_data);
1836                         if (!pp->vpd_data)
1837                                 condlog(0, "%s: failed to allocate space for vpd data", pp->dev);
1838                 }
1839         }
1840
1841         parent = pp->udev;
1842         while (parent) {
1843                 const char *subsys = udev_device_get_subsystem(parent);
1844                 if (subsys && !strncmp(subsys, "scsi", 4)) {
1845                         attr_path = udev_device_get_sysname(parent);
1846                         if (!attr_path)
1847                                 break;
1848                         if (sscanf(attr_path, "%i:%i:%i:%" SCNu64,
1849                                    &pp->sg_id.host_no,
1850                                    &pp->sg_id.channel,
1851                                    &pp->sg_id.scsi_id,
1852                                    &pp->sg_id.lun) == 4)
1853                                 break;
1854                 }
1855                 parent = udev_device_get_parent(parent);
1856         }
1857         if (!attr_path || pp->sg_id.host_no == -1)
1858                 return;
1859
1860         if (get_vpd_sysfs(parent, 0x80, pp->serial, SERIAL_SIZE) <= 0) {
1861                 if (get_serial(pp->serial, SERIAL_SIZE, pp->fd)) {
1862                         condlog(3, "%s: fail to get serial", pp->dev);
1863                         return;
1864                 }
1865         }
1866
1867         condlog(3, "%s: serial = %s", pp->dev, pp->serial);
1868         return;
1869 }
1870
1871 static void
1872 cciss_ioctl_pathinfo(struct path *pp)
1873 {
1874         get_serial(pp->serial, SERIAL_SIZE, pp->fd);
1875         condlog(3, "%s: serial = %s", pp->dev, pp->serial);
1876 }
1877
1878 int
1879 get_state (struct path * pp, struct config *conf, int daemon, int oldstate)
1880 {
1881         struct checker * c = &pp->checker;
1882         int state;
1883
1884         if (!checker_selected(c)) {
1885                 if (daemon) {
1886                         if (pathinfo(pp, conf, DI_SYSFS) != PATHINFO_OK) {
1887                                 condlog(3, "%s: couldn't get sysfs pathinfo",
1888                                         pp->dev);
1889                                 return PATH_UNCHECKED;
1890                         }
1891                 }
1892                 select_detect_checker(conf, pp);
1893                 select_checker(conf, pp);
1894                 if (!checker_selected(c)) {
1895                         condlog(3, "%s: No checker selected", pp->dev);
1896                         return PATH_UNCHECKED;
1897                 }
1898                 checker_set_fd(c, pp->fd);
1899                 if (checker_init(c, pp->mpp?&pp->mpp->mpcontext:NULL)) {
1900                         checker_clear(c);
1901                         condlog(3, "%s: checker init failed", pp->dev);
1902                         return PATH_UNCHECKED;
1903                 }
1904         }
1905         if (pp->mpp && !c->mpcontext)
1906                 checker_mp_init(c, &pp->mpp->mpcontext);
1907         checker_clear_message(c);
1908         if (conf->force_sync == 0)
1909                 checker_set_async(c);
1910         else
1911                 checker_set_sync(c);
1912         if (!conf->checker_timeout &&
1913             sysfs_get_timeout(pp, &(c->timeout)) <= 0)
1914                 c->timeout = DEF_TIMEOUT;
1915         state = checker_check(c, oldstate);
1916         condlog(3, "%s: %s state = %s", pp->dev,
1917                 checker_name(c), checker_state_name(state));
1918         if (state != PATH_UP && state != PATH_GHOST &&
1919             strlen(checker_message(c)))
1920                 condlog(3, "%s: %s checker%s",
1921                         pp->dev, checker_name(c), checker_message(c));
1922         return state;
1923 }
1924
1925 static int
1926 get_prio (struct path * pp, int timeout)
1927 {
1928         struct prio * p;
1929         struct config *conf;
1930         int old_prio;
1931
1932         if (!pp)
1933                 return 0;
1934
1935         p = &pp->prio;
1936         if (!prio_selected(p)) {
1937                 conf = get_multipath_config();
1938                 pthread_cleanup_push(put_multipath_config, conf);
1939                 select_detect_prio(conf, pp);
1940                 select_prio(conf, pp);
1941                 pthread_cleanup_pop(1);
1942                 if (!prio_selected(p)) {
1943                         condlog(3, "%s: no prio selected", pp->dev);
1944                         pp->priority = PRIO_UNDEF;
1945                         return 1;
1946                 }
1947         }
1948         old_prio = pp->priority;
1949         pp->priority = prio_getprio(p, pp, timeout);
1950         if (pp->priority < 0) {
1951                 /* this changes pp->offline, but why not */
1952                 int state = path_offline(pp);
1953
1954                 if (state == PATH_DOWN || state == PATH_PENDING) {
1955                         pp->priority = old_prio;
1956                         condlog(3, "%s: %s prio error in state %d, keeping prio = %d",
1957                                 pp->dev, prio_name(p), state, pp->priority);
1958                 } else {
1959                         condlog(3, "%s: %s prio error in state %d",
1960                                 pp->dev, prio_name(p), state);
1961                         pp->priority = PRIO_UNDEF;
1962                 }
1963                 return 1;
1964         }
1965         condlog((old_prio == pp->priority ? 4 : 3), "%s: %s prio = %u",
1966                 pp->dev, prio_name(p), pp->priority);
1967         return 0;
1968 }
1969
1970 /*
1971  * Mangle string of length *len starting at start
1972  * by removing character sequence "00" (hex for a 0 byte),
1973  * starting at end, backwards.
1974  * Changes the value of *len if characters were removed.
1975  * Returns a pointer to the position where "end" was moved to.
1976  */
1977 static char
1978 *skip_zeroes_backward(char* start, size_t *len, char *end)
1979 {
1980         char *p = end;
1981
1982         while (p >= start + 2 && *(p - 1) == '0' && *(p - 2) == '0')
1983                 p -= 2;
1984
1985         if (p == end)
1986                 return p;
1987
1988         memmove(p, end, start + *len + 1 - end);
1989         *len -= end - p;
1990
1991         return p;
1992 }
1993
1994 /*
1995  * Fix for NVME wwids looking like this:
1996  * nvme.0000-3163653363666438366239656630386200-4c696e75780000000000000000000000000000000000000000000000000000000000000000000000-00000002
1997  * which are encountered in some combinations of Linux NVME host and target.
1998  * The '00' are hex-encoded 0-bytes which are forbidden in the serial (SN)
1999  * and model (MN) fields. Discard them.
2000  * If a WWID of the above type is found, sets pp->wwid and returns a value > 0.
2001  * Otherwise, returns 0.
2002  */
2003 static int
2004 fix_broken_nvme_wwid(struct path *pp, const char *value, size_t size)
2005 {
2006         static const char _nvme[] = "nvme.";
2007         size_t len, i;
2008         char mangled[256];
2009         char *p;
2010
2011         len = strlen(value);
2012         if (len >= sizeof(mangled))
2013                 return 0;
2014
2015         /* Check that value starts with "nvme.%04x-" */
2016         if (memcmp(value, _nvme, sizeof(_nvme) - 1) || value[9] != '-')
2017                 return 0;
2018         for (i = 5; i < 9; i++)
2019                 if (!isxdigit(value[i]))
2020                         return 0;
2021
2022         memcpy(mangled, value, len + 1);
2023
2024         /* search end of "model" part and strip trailing '00' */
2025         p = memrchr(mangled, '-', len);
2026         if (p == NULL)
2027                 return 0;
2028
2029         p = skip_zeroes_backward(mangled, &len, p);
2030
2031         /* search end of "serial" part */
2032         p = memrchr(mangled, '-', p - mangled);
2033         if (p == NULL || memrchr(mangled, '-', p - mangled) != mangled + 9)
2034             /* We expect exactly 3 '-' in the value */
2035                 return 0;
2036
2037         p = skip_zeroes_backward(mangled, &len, p);
2038         if (len >= size)
2039                 return 0;
2040
2041         memcpy(pp->wwid, mangled, len + 1);
2042         condlog(2, "%s: over-long WWID shortened to %s", pp->dev, pp->wwid);
2043         return len;
2044 }
2045
2046 static int
2047 get_udev_uid(struct path * pp, char *uid_attribute, struct udev_device *udev)
2048 {
2049         ssize_t len;
2050         const char *value;
2051
2052         value = udev_device_get_property_value(udev, uid_attribute);
2053         if (!value || strlen(value) == 0)
2054                 value = getenv(uid_attribute);
2055         if (value && strlen(value)) {
2056                 len = strlcpy(pp->wwid, value, WWID_SIZE);
2057                 if (len >= WWID_SIZE) {
2058                         len = fix_broken_nvme_wwid(pp, value, WWID_SIZE);
2059                         if (len > 0)
2060                                 return len;
2061                         condlog(0, "%s: wwid overflow", pp->dev);
2062                         len = WWID_SIZE;
2063                 }
2064         } else {
2065                 condlog(3, "%s: no %s attribute", pp->dev,
2066                         uid_attribute);
2067                 len = -ENODATA;
2068         }
2069         return len;
2070 }
2071
2072 static int
2073 get_vpd_uid(struct path * pp)
2074 {
2075         struct udev_device *parent = pp->udev;
2076
2077         while (parent) {
2078                 const char *subsys = udev_device_get_subsystem(parent);
2079                 if (subsys && !strncmp(subsys, "scsi", 4))
2080                         break;
2081                 parent = udev_device_get_parent(parent);
2082         }
2083
2084         if (!parent)
2085                 return -EINVAL;
2086
2087         return get_vpd_sysfs(parent, 0x83, pp->wwid, WWID_SIZE);
2088 }
2089
2090 /* based on code from s390-tools/dasdinfo/dasdinfo.c */
2091 static ssize_t dasd_get_uid(struct path *pp)
2092 {
2093         struct udev_device *parent;
2094         char value[80];
2095         char *p;
2096         int i;
2097
2098         parent = udev_device_get_parent_with_subsystem_devtype(pp->udev, "ccw",
2099                                                                NULL);
2100         if (!parent)
2101                 return -1;
2102
2103         if (sysfs_attr_get_value(parent, "uid", value, 80) < 0)
2104                 return -1;
2105
2106         p = value - 1;
2107         /* look for the 4th '.' and cut there */
2108         for (i = 0; i < 4; i++) {
2109                 p = index(p + 1, '.');
2110                 if (!p)
2111                         break;
2112         }
2113         if (p)
2114                 *p = '\0';
2115
2116         return strlcpy(pp->wwid, value, WWID_SIZE);
2117 }
2118
2119 static ssize_t uid_fallback(struct path *pp, int path_state,
2120                             const char **origin)
2121 {
2122         ssize_t len = -1;
2123
2124         if (pp->bus == SYSFS_BUS_CCW) {
2125                 len = dasd_get_uid(pp);
2126                 *origin = "sysfs";
2127         } else if (pp->bus == SYSFS_BUS_SCSI) {
2128                 len = get_vpd_uid(pp);
2129                 *origin = "sysfs";
2130                 if (len < 0 && path_state == PATH_UP) {
2131                         condlog(1, "%s: failed to get sysfs uid: %s",
2132                                 pp->dev, strerror(-len));
2133                         len = get_vpd_sgio(pp->fd, 0x83, 0, pp->wwid,
2134                                            WWID_SIZE);
2135                         *origin = "sgio";
2136                 }
2137         } else if (pp->bus == SYSFS_BUS_NVME) {
2138                 char value[256];
2139
2140                 if (!pp->udev)
2141                         return -1;
2142                 len = sysfs_attr_get_value(pp->udev, "wwid", value,
2143                                            sizeof(value));
2144                 if (len <= 0)
2145                         return -1;
2146                 len = strlcpy(pp->wwid, value, WWID_SIZE);
2147                 if (len >= WWID_SIZE) {
2148                         len = fix_broken_nvme_wwid(pp, value,
2149                                                    WWID_SIZE);
2150                         if (len > 0)
2151                                 return len;
2152                         condlog(0, "%s: wwid overflow", pp->dev);
2153                         len = WWID_SIZE;
2154                 }
2155                 *origin = "sysfs";
2156         }
2157         return len;
2158 }
2159
2160 bool has_uid_fallback(struct path *pp)
2161 {
2162         /*
2163          * Falling back to direct WWID determination is dangerous
2164          * if uid_attribute is set to something non-standard.
2165          * Allow it only if it's either the default, or if udev
2166          * has been disabled by setting 'uid_attribute ""'.
2167          */
2168         if (!pp->uid_attribute)
2169                 return false;
2170         return ((pp->bus == SYSFS_BUS_SCSI &&
2171                  (!strcmp(pp->uid_attribute, DEFAULT_UID_ATTRIBUTE) ||
2172                   !strcmp(pp->uid_attribute, ""))) ||
2173                 (pp->bus == SYSFS_BUS_NVME &&
2174                  (!strcmp(pp->uid_attribute, DEFAULT_NVME_UID_ATTRIBUTE) ||
2175                   !strcmp(pp->uid_attribute, ""))) ||
2176                 (pp->bus == SYSFS_BUS_CCW &&
2177                  (!strcmp(pp->uid_attribute, DEFAULT_DASD_UID_ATTRIBUTE) ||
2178                   !strcmp(pp->uid_attribute, ""))));
2179 }
2180
2181 int
2182 get_uid (struct path * pp, int path_state, struct udev_device *udev,
2183          int allow_fallback)
2184 {
2185         const char *origin = "unknown";
2186         ssize_t len = 0;
2187         struct config *conf;
2188         int used_fallback = 0;
2189         size_t i;
2190
2191         if (!pp->uid_attribute && !pp->getuid) {
2192                 conf = get_multipath_config();
2193                 pthread_cleanup_push(put_multipath_config, conf);
2194                 select_getuid(conf, pp);
2195                 select_recheck_wwid(conf, pp);
2196                 pthread_cleanup_pop(1);
2197         }
2198
2199         memset(pp->wwid, 0, WWID_SIZE);
2200         if (pp->getuid) {
2201                 char buff[CALLOUT_MAX_SIZE];
2202
2203                 /* Use 'getuid' callout, deprecated */
2204                 condlog(1, "%s: using deprecated getuid callout", pp->dev);
2205                 if (path_state != PATH_UP) {
2206                         condlog(3, "%s: path inaccessible", pp->dev);
2207                         len = -EWOULDBLOCK;
2208                 } else if (apply_format(pp->getuid, &buff[0], pp)) {
2209                         condlog(0, "error formatting uid callout command");
2210                         len = -EINVAL;
2211                 } else if (execute_program(buff, pp->wwid, WWID_SIZE)) {
2212                         condlog(3, "error calling out %s", buff);
2213                         len = -EIO;
2214                 } else
2215                         len = strlen(pp->wwid);
2216                 origin = "callout";
2217         } else if (pp->uid_attribute) {
2218                 /* if the uid_attribute is an empty string skip udev checking */
2219                 bool check_uid_attr = udev && *pp->uid_attribute;
2220
2221                 if (check_uid_attr) {
2222                         len = get_udev_uid(pp, pp->uid_attribute, udev);
2223                         origin = "udev";
2224                         if (len == 0)
2225                                 condlog(1, "%s: empty udev uid", pp->dev);
2226                 }
2227                 if ((!check_uid_attr || (len <= 0 && allow_fallback))
2228                     && has_uid_fallback(pp)) {
2229                         /* if udev wasn't set or we failed in get_udev_uid()
2230                          * log at a higher priority */
2231                         if (!udev || check_uid_attr)
2232                                 used_fallback = 1;
2233                         len = uid_fallback(pp, path_state, &origin);
2234                 }
2235         }
2236         if ( len < 0 ) {
2237                 condlog(1, "%s: failed to get %s uid: %s",
2238                         pp->dev, origin, strerror(-len));
2239                 memset(pp->wwid, 0x0, WWID_SIZE);
2240                 return 1;
2241         } else {
2242                 /* Strip any trailing blanks */
2243                 for (i = strlen(pp->wwid); i > 0 && pp->wwid[i-1] == ' '; i--);
2244                         /* no-op */
2245                 pp->wwid[i] = '\0';
2246         }
2247         condlog((used_fallback)? 1 : 3, "%s: uid = %s (%s)", pp->dev,
2248                 *pp->wwid == '\0' ? "<empty>" : pp->wwid, origin);
2249         return 0;
2250 }
2251
2252 int pathinfo(struct path *pp, struct config *conf, int mask)
2253 {
2254         int path_state;
2255
2256         if (!pp || !conf)
2257                 return PATHINFO_FAILED;
2258
2259         /* Treat removed paths as if they didn't exist */
2260         if (pp->initialized == INIT_REMOVED)
2261                 return PATHINFO_FAILED;
2262
2263         /*
2264          * For behavior backward-compatibility with multipathd,
2265          * the blacklisting by filter_property|devnode() is not
2266          * limited by DI_BLACKLIST and occurs before this debug
2267          * message with the mask value.
2268          */
2269         if (pp->udev) {
2270                 const char *hidden =
2271                         udev_device_get_sysattr_value(pp->udev, "hidden");
2272
2273                 if (hidden && !strcmp(hidden, "1")) {
2274                         condlog(4, "%s: hidden", pp->dev);
2275                         return PATHINFO_SKIPPED;
2276                 }
2277
2278                 if (is_claimed_by_foreign(pp->udev))
2279                         return PATHINFO_SKIPPED;
2280
2281                 /*
2282                  * uid_attribute is required for filter_property below,
2283                  * and needs access to pp->hwe.
2284                  */
2285                 if (!(mask & DI_SYSFS) && (mask & DI_BLACKLIST) &&
2286                     !pp->uid_attribute && VECTOR_SIZE(pp->hwe) == 0)
2287                         mask |= DI_SYSFS;
2288         }
2289
2290         if (strlen(pp->dev) != 0 && filter_devnode(conf->blist_devnode,
2291                            conf->elist_devnode,
2292                            pp->dev) > 0)
2293                 return PATHINFO_SKIPPED;
2294
2295         condlog(4, "%s: mask = 0x%x", pp->dev, mask);
2296
2297         /*
2298          * Sanity check: we need the device number to
2299          * avoid inconsistent information in
2300          * find_path_by_dev()/find_path_by_devt()
2301          */
2302         if (!strlen(pp->dev_t) && !(mask & DI_SYSFS)) {
2303                 condlog(1, "%s: empty device number", pp->dev);
2304                 mask |= DI_SYSFS;
2305         }
2306
2307         /*
2308          * fetch info available in sysfs
2309          */
2310         if (mask & DI_SYSFS) {
2311                 int rc = sysfs_pathinfo(pp, conf->hwtable);
2312
2313                 if (rc != PATHINFO_OK)
2314                         return rc;
2315
2316                 if (pp->bus == SYSFS_BUS_SCSI &&
2317                     pp->sg_id.proto_id == SCSI_PROTOCOL_USB &&
2318                     !conf->allow_usb_devices) {
2319                         condlog(3, "%s: skip USB device %s", pp->dev,
2320                                 pp->tgt_node_name);
2321                         return PATHINFO_SKIPPED;
2322                 }
2323         }
2324
2325         if (mask & DI_BLACKLIST && mask & DI_SYSFS) {
2326                 /* uid_attribute is required for filter_property() */
2327                 if (pp->udev && !pp->uid_attribute) {
2328                         select_getuid(conf, pp);
2329                         select_recheck_wwid(conf, pp);
2330                 }
2331
2332                 if (filter_property(conf, pp->udev, 4, pp->uid_attribute) > 0 ||
2333                     filter_device(conf->blist_device, conf->elist_device,
2334                                   pp->vendor_id, pp->product_id, pp->dev) > 0 ||
2335                     filter_protocol(conf->blist_protocol, conf->elist_protocol,
2336                                     pp) > 0)
2337                         return PATHINFO_SKIPPED;
2338         }
2339
2340         path_state = path_offline(pp);
2341         if (path_state == PATH_REMOVED)
2342                 goto blank;
2343         else if (mask & DI_NOIO) {
2344                 if (mask & DI_CHECKER)
2345                         /*
2346                          * Avoid any IO on the device itself.
2347                          * simply use the path_offline() return as its state
2348                          */
2349                         if (path_state != PATH_PENDING ||
2350                             pp->state == PATH_UNCHECKED ||
2351                             pp->state == PATH_WILD)
2352                                 pp->chkrstate = pp->state = path_state;
2353                 return PATHINFO_OK;
2354         }
2355
2356         /*
2357          * fetch info not available through sysfs
2358          */
2359         if (pp->fd < 0)
2360                 pp->fd = open(udev_device_get_devnode(pp->udev), O_RDONLY);
2361
2362         if (pp->fd < 0) {
2363                 condlog(4, "Couldn't open device node for %s: %s",
2364                         pp->dev, strerror(errno));
2365                 goto blank;
2366         }
2367
2368         if (mask & DI_SERIAL)
2369                 get_geometry(pp);
2370
2371         if (path_state == PATH_UP && pp->bus == SYSFS_BUS_SCSI)
2372                 scsi_ioctl_pathinfo(pp, mask);
2373
2374         if (pp->bus == SYSFS_BUS_CCISS && mask & DI_SERIAL)
2375                 cciss_ioctl_pathinfo(pp);
2376
2377         if (mask & DI_CHECKER) {
2378                 if (path_state == PATH_UP) {
2379                         int newstate = get_state(pp, conf, 0, path_state);
2380                         if (newstate != PATH_PENDING ||
2381                             pp->state == PATH_UNCHECKED ||
2382                             pp->state == PATH_WILD)
2383                                 pp->chkrstate = pp->state = newstate;
2384                         if (pp->state == PATH_TIMEOUT)
2385                                 pp->state = PATH_DOWN;
2386                         if (pp->state == PATH_UP && !pp->size) {
2387                                 condlog(3, "%s: device size is 0, "
2388                                         "path unusable", pp->dev);
2389                                 pp->state = PATH_GHOST;
2390                         }
2391                 } else {
2392                         condlog(3, "%s: path inaccessible", pp->dev);
2393                         pp->chkrstate = pp->state = path_state;
2394                 }
2395         }
2396
2397         if ((mask & DI_WWID) && !strlen(pp->wwid)) {
2398                 int allow_fallback = ((mask & DI_NOFALLBACK) == 0 &&
2399                                       pp->retriggers >= conf->retrigger_tries);
2400                 get_uid(pp, path_state, pp->udev, allow_fallback);
2401                 if (!strlen(pp->wwid)) {
2402                         if (pp->bus == SYSFS_BUS_UNDEF)
2403                                 return PATHINFO_SKIPPED;
2404                         if (pp->initialized != INIT_FAILED) {
2405                                 pp->initialized = INIT_MISSING_UDEV;
2406                                 pp->tick = conf->retrigger_delay;
2407                         } else if (allow_fallback &&
2408                                    (pp->state == PATH_UP || pp->state == PATH_GHOST)) {
2409                                 /*
2410                                  * We have failed to read udev info for this path
2411                                  * repeatedly. We used the fallback in get_uid()
2412                                  * if there was any, and still got no WWID,
2413                                  * although the path is allegedly up.
2414                                  * It's likely that this path is not fit for
2415                                  * multipath use.
2416                                  */
2417                                 STRBUF_ON_STACK(buf);
2418
2419                                 snprint_path(&buf, "%T", pp, 0);
2420                                 condlog(1, "%s: no WWID in state \"%s\", giving up",
2421                                         pp->dev, get_strbuf_str(&buf));
2422                                 return PATHINFO_SKIPPED;
2423                         }
2424                         return PATHINFO_OK;
2425                 }
2426                 else
2427                         pp->tick = 1;
2428         }
2429
2430         if (mask & DI_BLACKLIST && mask & DI_WWID) {
2431                 if (filter_wwid(conf->blist_wwid, conf->elist_wwid,
2432                                 pp->wwid, pp->dev) > 0) {
2433                         return PATHINFO_SKIPPED;
2434                 }
2435         }
2436
2437          /*
2438           * Retrieve path priority, even for PATH_DOWN paths if it has never
2439           * been successfully obtained before. If path is down don't try
2440           * for too long.
2441           */
2442         if ((mask & DI_PRIO) && path_state == PATH_UP && strlen(pp->wwid)) {
2443                 if (pp->state != PATH_DOWN || pp->priority == PRIO_UNDEF) {
2444                         get_prio(pp, (pp->state != PATH_DOWN)?
2445                                      (conf->checker_timeout * 1000) : 10);
2446                 }
2447         }
2448
2449         if ((mask & DI_ALL) == DI_ALL)
2450                 pp->initialized = INIT_OK;
2451         return PATHINFO_OK;
2452
2453 blank:
2454         /*
2455          * Recoverable error, for example faulty or offline path
2456          */
2457         pp->chkrstate = pp->state = PATH_DOWN;
2458         if (pp->initialized == INIT_NEW || pp->initialized == INIT_FAILED)
2459                 memset(pp->wwid, 0, WWID_SIZE);
2460
2461         return PATHINFO_OK;
2462 }