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