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