99c1a0f0a8229e75f8d12e5c576b8668a5cef8ca
[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 static void
880 detect_alua(struct path * pp)
881 {
882         int ret;
883         int tpgs;
884         unsigned int timeout;
885
886         if (sysfs_get_timeout(pp, &timeout) <= 0)
887                 timeout = DEF_TIMEOUT;
888
889         if ((tpgs = get_target_port_group_support(pp, timeout)) <= 0) {
890                 pp->tpgs = TPGS_NONE;
891                 return;
892         }
893         ret = get_target_port_group(pp, timeout);
894         if (ret < 0 || get_asymmetric_access_state(pp, ret, timeout) < 0) {
895                 pp->tpgs = TPGS_NONE;
896                 return;
897         }
898         pp->tpgs = tpgs;
899 }
900
901 int path_get_tpgs(struct path *pp)
902 {
903         if (pp->tpgs == TPGS_UNDEF)
904                 detect_alua(pp);
905         return pp->tpgs;
906 }
907
908 #define DEFAULT_SGIO_LEN 254
909
910 /* Query VPD page @pg. Returns number of INQUIRY bytes
911    upon success and -1 upon failure. */
912 static int
913 sgio_get_vpd (unsigned char * buff, int maxlen, int fd, int pg)
914 {
915         int len = DEFAULT_SGIO_LEN;
916         int rlen;
917
918         if (fd < 0) {
919                 errno = EBADF;
920                 return -1;
921         }
922 retry:
923         if (0 == do_inq(fd, 0, 1, pg, buff, len)) {
924                 rlen = get_unaligned_be16(&buff[2]) + 4;
925                 if (rlen <= len || len >= maxlen)
926                         return rlen;
927                 len = (rlen < maxlen)? rlen : maxlen;
928                 goto retry;
929         }
930         return -1;
931 }
932
933 static int
934 get_geometry(struct path *pp)
935 {
936         if (pp->fd < 0)
937                 return 1;
938
939         if (ioctl(pp->fd, HDIO_GETGEO, &pp->geom)) {
940                 condlog(2, "%s: HDIO_GETGEO failed with %d", pp->dev, errno);
941                 memset(&pp->geom, 0, sizeof(pp->geom));
942                 return 1;
943         }
944         condlog(3, "%s: %u cyl, %u heads, %u sectors/track, start at %lu",
945                 pp->dev, pp->geom.cylinders, pp->geom.heads,
946                 pp->geom.sectors, pp->geom.start);
947         return 0;
948 }
949
950 static int
951 parse_vpd_pg80(const unsigned char *in, char *out, size_t out_len)
952 {
953         size_t len = get_unaligned_be16(&in[2]);
954
955         if (out_len == 0)
956                 return 0;
957
958         /*
959          * Strip leading and trailing whitespace
960          */
961         while (len > 0 && in[len + 3] == ' ')
962                 --len;
963         while (len > 0 && in[4] == ' ') {
964                 ++in;
965                 --len;
966         }
967
968         if (len >= out_len) {
969                 condlog(2, "vpd pg80 overflow, %lu/%lu bytes required",
970                         len + 1, out_len);
971                 len = out_len - 1;
972         }
973         if (len > 0) {
974                 memcpy(out, in + 4, len);
975                 out[len] = '\0';
976         }
977         return len;
978 }
979
980 static int
981 parse_vpd_pg83(const unsigned char *in, size_t in_len,
982                char *out, size_t out_len)
983 {
984         const unsigned char *d;
985         const unsigned char *vpd = NULL;
986         size_t len, vpd_len, i;
987         int vpd_type, prio = -1, naa_prio;
988
989         d = in + 4;
990         while (d < in + in_len) {
991                 /* Select 'association: LUN' */
992                 if ((d[1] & 0x30) != 0) {
993                         d += d[3] + 4;
994                         continue;
995                 }
996                 switch (d[1] & 0xf) {
997                 case 0x3:
998                         /* NAA: Prio 5 */
999                         switch (d[4] >> 4) {
1000                         case 6:
1001                                 /* IEEE Registered Extended: Prio 8 */
1002                                 naa_prio = 8;
1003                                 break;
1004                         case 5:
1005                                 /* IEEE Registered: Prio 7 */
1006                                 naa_prio = 7;
1007                                 break;
1008                         case 2:
1009                                 /* IEEE Extended: Prio 6 */
1010                                 naa_prio = 6;
1011                                 break;
1012                         case 3:
1013                                 /* IEEE Locally assigned: Prio 1 */
1014                                 naa_prio = 1;
1015                                 break;
1016                         default:
1017                                 /* Default: no priority */
1018                                 naa_prio = -1;
1019                                 break;
1020                         }
1021                         if (prio < naa_prio) {
1022                                 prio = naa_prio;
1023                                 vpd = d;
1024                         }
1025                         break;
1026                 case 0x8:
1027                         /* SCSI Name: Prio 4 */
1028                         if (memcmp(d + 4, "eui.", 4) &&
1029                             memcmp(d + 4, "naa.", 4) &&
1030                             memcmp(d + 4, "iqn.", 4))
1031                                 continue;
1032                         if (prio < 4) {
1033                                 prio = 4;
1034                                 vpd = d;
1035                         }
1036                         break;
1037                 case 0x2:
1038                         /* EUI-64: Prio 3 */
1039                         if (prio < 3) {
1040                                 prio = 3;
1041                                 vpd = d;
1042                         }
1043                         break;
1044                 case 0x1:
1045                         /* T-10 Vendor ID: Prio 2 */
1046                         if (prio < 2) {
1047                                 prio = 2;
1048                                 vpd = d;
1049                         }
1050                         break;
1051                 }
1052                 d += d[3] + 4;
1053         }
1054
1055         if (prio <= 0)
1056                 return -ENODATA;
1057         /* Need space at least for one digit */
1058         else if (out_len <= 1)
1059                 return 0;
1060
1061         len = 0;
1062         vpd_type = vpd[1] & 0xf;
1063         vpd_len = vpd[3];
1064         vpd += 4;
1065         if (vpd_type == 0x2 || vpd_type == 0x3) {
1066                 size_t i;
1067
1068                 len = sprintf(out, "%d", vpd_type);
1069                 if (2 * vpd_len >= out_len - len) {
1070                         condlog(1, "%s: WWID overflow, type %d, %lu/%lu bytes required",
1071                                 __func__, vpd_type,
1072                                 2 * vpd_len + len + 1, out_len);
1073                         vpd_len = (out_len - len - 1) / 2;
1074                 }
1075                 for (i = 0; i < vpd_len; i++)
1076                         len += sprintf(out + len,
1077                                        "%02x", vpd[i]);
1078         } else if (vpd_type == 0x8 && vpd_len < 4) {
1079                 condlog(1, "%s: VPD length %lu too small for designator type 8",
1080                         __func__, vpd_len);
1081                 return -EINVAL;
1082         } else if (vpd_type == 0x8) {
1083                 if (!memcmp("eui.", vpd, 4))
1084                         out[0] =  '2';
1085                 else if (!memcmp("naa.", vpd, 4))
1086                         out[0] = '3';
1087                 else
1088                         out[0] = '8';
1089
1090                 vpd += 4;
1091                 len = vpd_len - 4;
1092                 while (len > 2 && vpd[len - 2] == '\0')
1093                         --len;
1094                 if (len > out_len - 1) {
1095                         condlog(1, "%s: WWID overflow, type 8/%c, %lu/%lu bytes required",
1096                                 __func__, out[0], len + 1, out_len);
1097                         len = out_len - 1;
1098                 }
1099
1100                 if (out[0] == '8')
1101                         for (i = 0; i < len; ++i)
1102                                 out[1 + i] = vpd[i];
1103                 else
1104                         for (i = 0; i < len; ++i)
1105                                 out[1 + i] = tolower(vpd[i]);
1106
1107                 /* designator should be 0-terminated, but let's make sure */
1108                 out[len] = '\0';
1109
1110         } else if (vpd_type == 0x1) {
1111                 const unsigned char *p;
1112                 size_t p_len;
1113
1114                 out[0] = '1';
1115                 len = 1;
1116                 while ((p = memchr(vpd, ' ', vpd_len))) {
1117                         p_len = p - vpd;
1118                         if (len + p_len > out_len - 1) {
1119                                 condlog(1, "%s: WWID overflow, type 1, %lu/%lu bytes required",
1120                                         __func__, len + p_len, out_len);
1121                                 p_len = out_len - len - 1;
1122                         }
1123                         memcpy(out + len, vpd, p_len);
1124                         len += p_len;
1125                         if (len >= out_len - 1) {
1126                                 out[len] = '\0';
1127                                 break;
1128                         }
1129                         out[len] = '_';
1130                         len ++;
1131                         if (len >= out_len - 1) {
1132                                 out[len] = '\0';
1133                                 break;
1134                         }
1135                         vpd = p;
1136                         vpd_len -= p_len;
1137                         while (vpd && *vpd == ' ') {
1138                                 vpd++;
1139                                 vpd_len --;
1140                         }
1141                 }
1142                 p_len = vpd_len;
1143                 if (p_len > 0 && len < out_len - 1) {
1144                         if (len + p_len > out_len - 1) {
1145                                 condlog(1, "%s: WWID overflow, type 1, %lu/%lu bytes required",
1146                                         __func__, len + p_len + 1, out_len);
1147                                 p_len = out_len - len - 1;
1148                         }
1149                         memcpy(out + len, vpd, p_len);
1150                         len += p_len;
1151                         out[len] = '\0';
1152                 }
1153                 if (len > 1 && out[len - 1] == '_') {
1154                         out[len - 1] = '\0';
1155                         len--;
1156                 }
1157         }
1158         return len;
1159 }
1160
1161 static int
1162 parse_vpd_c0_hp3par(const unsigned char *in, size_t in_len,
1163                     char *out, size_t out_len)
1164 {
1165         size_t len;
1166
1167         memset(out, 0x0, out_len);
1168         if (in_len <= 4 || (in[4] > 3 && in_len < 44)) {
1169                 condlog(3, "HP/3PAR vendor specific VPD page length too short: %lu", in_len);
1170                 return -EINVAL;
1171         }
1172         if (in[4] <= 3) /* revision must be > 3 to have Vomlume Name */
1173                 return -ENODATA;
1174         len = get_unaligned_be32(&in[40]);
1175         if (len > out_len || len + 44 > in_len) {
1176                 condlog(3, "HP/3PAR vendor specific Volume name too long: %lu",
1177                         len);
1178                 return -EINVAL;
1179         }
1180         memcpy(out, &in[44], len);
1181         out[out_len - 1] = '\0';
1182         return len;
1183 }
1184
1185 static int
1186 get_vpd_sysfs (struct udev_device *parent, int pg, char * str, int maxlen)
1187 {
1188         int len, buff_len;
1189         unsigned char buff[4096];
1190
1191         memset(buff, 0x0, 4096);
1192         if (!parent || sysfs_get_vpd(parent, pg, buff, 4096) <= 0) {
1193                 condlog(3, "failed to read sysfs vpd pg%02x", pg);
1194                 return -EINVAL;
1195         }
1196
1197         if (buff[1] != pg) {
1198                 condlog(3, "vpd pg%02x error, invalid vpd page %02x",
1199                         pg, buff[1]);
1200                 return -ENODATA;
1201         }
1202         buff_len = get_unaligned_be16(&buff[2]) + 4;
1203         if (buff_len > 4096)
1204                 condlog(3, "vpd pg%02x page truncated", pg);
1205
1206         if (pg == 0x80)
1207                 len = parse_vpd_pg80(buff, str, maxlen);
1208         else if (pg == 0x83)
1209                 len = parse_vpd_pg83(buff, buff_len, str, maxlen);
1210         else
1211                 len = -ENOSYS;
1212
1213         return len;
1214 }
1215
1216 int
1217 get_vpd_sgio (int fd, int pg, int vend_id, char * str, int maxlen)
1218 {
1219         int len, buff_len;
1220         unsigned char buff[4096];
1221
1222         memset(buff, 0x0, 4096);
1223         if (sgio_get_vpd(buff, 4096, fd, pg) < 0) {
1224                 int lvl = pg == 0x80 || pg == 0x83 ? 3 : 4;
1225
1226                 condlog(lvl, "failed to issue vpd inquiry for pg%02x",
1227                         pg);
1228                 return -errno;
1229         }
1230
1231         if (buff[1] != pg) {
1232                 condlog(3, "vpd pg%02x error, invalid vpd page %02x",
1233                         pg, buff[1]);
1234                 return -ENODATA;
1235         }
1236         buff_len = get_unaligned_be16(&buff[2]) + 4;
1237         if (buff_len > 4096) {
1238                 condlog(3, "vpd pg%02x page truncated", pg);
1239                 buff_len = 4096;
1240         }
1241         if (pg == 0x80)
1242                 len = parse_vpd_pg80(buff, str, maxlen);
1243         else if (pg == 0x83)
1244                 len = parse_vpd_pg83(buff, buff_len, str, maxlen);
1245         else if (pg == 0xc9 && maxlen >= 8) {
1246                 if (buff_len < 8)
1247                         len = -ENODATA;
1248                 else {
1249                         len = (buff_len <= maxlen)? buff_len : maxlen;
1250                         memcpy (str, buff, len);
1251                 }
1252         } else if (pg == 0xc0 && vend_id == VPD_VP_HP3PAR)
1253                 len = parse_vpd_c0_hp3par(buff, buff_len, str, maxlen);
1254         else
1255                 len = -ENOSYS;
1256
1257         return len;
1258 }
1259
1260 static int
1261 scsi_sysfs_pathinfo (struct path * pp, vector hwtable)
1262 {
1263         struct udev_device *parent;
1264         const char *attr_path = NULL;
1265
1266         parent = pp->udev;
1267         while (parent) {
1268                 const char *subsys = udev_device_get_subsystem(parent);
1269                 if (subsys && !strncmp(subsys, "scsi", 4)) {
1270                         attr_path = udev_device_get_sysname(parent);
1271                         if (!attr_path)
1272                                 break;
1273                         if (sscanf(attr_path, "%i:%i:%i:%i",
1274                                    &pp->sg_id.host_no,
1275                                    &pp->sg_id.channel,
1276                                    &pp->sg_id.scsi_id,
1277                                    &pp->sg_id.lun) == 4)
1278                                 break;
1279                 }
1280                 parent = udev_device_get_parent(parent);
1281         }
1282         if (!attr_path || pp->sg_id.host_no == -1)
1283                 return PATHINFO_FAILED;
1284
1285         if (sysfs_get_vendor(parent, pp->vendor_id, SCSI_VENDOR_SIZE) <= 0)
1286                 return PATHINFO_FAILED;;
1287
1288         condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
1289
1290         if (sysfs_get_model(parent, pp->product_id, PATH_PRODUCT_SIZE) <= 0)
1291                 return PATHINFO_FAILED;;
1292
1293         condlog(3, "%s: product = %s", pp->dev, pp->product_id);
1294
1295         if (sysfs_get_rev(parent, pp->rev, PATH_REV_SIZE) < 0)
1296                 return PATHINFO_FAILED;;
1297
1298         condlog(3, "%s: rev = %s", pp->dev, pp->rev);
1299
1300         /*
1301          * set the hwe configlet pointer
1302          */
1303         find_hwe(hwtable, pp->vendor_id, pp->product_id, pp->rev, pp->hwe);
1304
1305         /*
1306          * host / bus / target / lun
1307          */
1308         condlog(3, "%s: h:b:t:l = %i:%i:%i:%i",
1309                         pp->dev,
1310                         pp->sg_id.host_no,
1311                         pp->sg_id.channel,
1312                         pp->sg_id.scsi_id,
1313                         pp->sg_id.lun);
1314
1315         /*
1316          * target node name
1317          */
1318         if(sysfs_get_tgt_nodename(pp, pp->tgt_node_name))
1319                 return PATHINFO_FAILED;
1320
1321         condlog(3, "%s: tgt_node_name = %s",
1322                 pp->dev, pp->tgt_node_name);
1323
1324         return PATHINFO_OK;
1325 }
1326
1327 static int
1328 nvme_sysfs_pathinfo (struct path * pp, vector hwtable)
1329 {
1330         struct udev_device *parent;
1331         const char *attr_path = NULL;
1332         const char *attr;
1333
1334         attr_path = udev_device_get_sysname(pp->udev);
1335         if (!attr_path)
1336                 return PATHINFO_FAILED;
1337
1338         if (sscanf(attr_path, "nvme%dn%d",
1339                    &pp->sg_id.host_no,
1340                    &pp->sg_id.scsi_id) != 2)
1341                 return PATHINFO_FAILED;
1342
1343         parent = udev_device_get_parent_with_subsystem_devtype(pp->udev,
1344                                                                "nvme", NULL);
1345         if (!parent)
1346                 return PATHINFO_SKIPPED;
1347
1348         attr = udev_device_get_sysattr_value(pp->udev, "nsid");
1349         pp->sg_id.lun = attr ? atoi(attr) : 0;
1350
1351         attr = udev_device_get_sysattr_value(parent, "cntlid");
1352         pp->sg_id.channel = attr ? atoi(attr) : 0;
1353
1354         snprintf(pp->vendor_id, SCSI_VENDOR_SIZE, "NVME");
1355         snprintf(pp->product_id, PATH_PRODUCT_SIZE, "%s",
1356                  udev_device_get_sysattr_value(parent, "model"));
1357         snprintf(pp->serial, SERIAL_SIZE, "%s",
1358                  udev_device_get_sysattr_value(parent, "serial"));
1359         snprintf(pp->rev, PATH_REV_SIZE, "%s",
1360                  udev_device_get_sysattr_value(parent, "firmware_rev"));
1361
1362         condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
1363         condlog(3, "%s: product = %s", pp->dev, pp->product_id);
1364         condlog(3, "%s: serial = %s", pp->dev, pp->serial);
1365         condlog(3, "%s: rev = %s", pp->dev, pp->rev);
1366
1367         find_hwe(hwtable, pp->vendor_id, pp->product_id, NULL, pp->hwe);
1368
1369         return PATHINFO_OK;
1370 }
1371
1372 static int
1373 ccw_sysfs_pathinfo (struct path * pp, vector hwtable)
1374 {
1375         struct udev_device *parent;
1376         char attr_buff[NAME_SIZE];
1377         const char *attr_path;
1378
1379         parent = pp->udev;
1380         while (parent) {
1381                 const char *subsys = udev_device_get_subsystem(parent);
1382                 if (subsys && !strncmp(subsys, "ccw", 3))
1383                         break;
1384                 parent = udev_device_get_parent(parent);
1385         }
1386         if (!parent)
1387                 return PATHINFO_FAILED;
1388
1389         sprintf(pp->vendor_id, "IBM");
1390
1391         condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
1392
1393         if (sysfs_get_devtype(parent, attr_buff, FILE_NAME_SIZE) <= 0)
1394                 return PATHINFO_FAILED;
1395
1396         if (!strncmp(attr_buff, "3370", 4)) {
1397                 sprintf(pp->product_id,"S/390 DASD FBA");
1398         } else if (!strncmp(attr_buff, "9336", 4)) {
1399                 sprintf(pp->product_id,"S/390 DASD FBA");
1400         } else {
1401                 sprintf(pp->product_id,"S/390 DASD ECKD");
1402         }
1403
1404         condlog(3, "%s: product = %s", pp->dev, pp->product_id);
1405
1406         /*
1407          * set the hwe configlet pointer
1408          */
1409         find_hwe(hwtable, pp->vendor_id, pp->product_id, NULL, pp->hwe);
1410
1411         /*
1412          * host / bus / target / lun
1413          */
1414         attr_path = udev_device_get_sysname(parent);
1415         pp->sg_id.lun = 0;
1416         if (sscanf(attr_path, "%i.%i.%x",
1417                    &pp->sg_id.host_no,
1418                    &pp->sg_id.channel,
1419                    &pp->sg_id.scsi_id) == 3) {
1420                 condlog(3, "%s: h:b:t:l = %i:%i:%i:%i",
1421                         pp->dev,
1422                         pp->sg_id.host_no,
1423                         pp->sg_id.channel,
1424                         pp->sg_id.scsi_id,
1425                         pp->sg_id.lun);
1426         }
1427
1428         return PATHINFO_OK;
1429 }
1430
1431 static int
1432 cciss_sysfs_pathinfo (struct path * pp, vector hwtable)
1433 {
1434         const char * attr_path = NULL;
1435         struct udev_device *parent;
1436
1437         parent = pp->udev;
1438         while (parent) {
1439                 const char *subsys = udev_device_get_subsystem(parent);
1440                 if (subsys && !strncmp(subsys, "cciss", 5)) {
1441                         attr_path = udev_device_get_sysname(parent);
1442                         if (!attr_path)
1443                                 break;
1444                         if (sscanf(attr_path, "c%id%i",
1445                                    &pp->sg_id.host_no,
1446                                    &pp->sg_id.scsi_id) == 2)
1447                                 break;
1448                 }
1449                 parent = udev_device_get_parent(parent);
1450         }
1451         if (!attr_path || pp->sg_id.host_no == -1)
1452                 return PATHINFO_FAILED;
1453
1454         if (sysfs_get_vendor(parent, pp->vendor_id, SCSI_VENDOR_SIZE) <= 0)
1455                 return PATHINFO_FAILED;
1456
1457         condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
1458
1459         if (sysfs_get_model(parent, pp->product_id, PATH_PRODUCT_SIZE) <= 0)
1460                 return PATHINFO_FAILED;
1461
1462         condlog(3, "%s: product = %s", pp->dev, pp->product_id);
1463
1464         if (sysfs_get_rev(parent, pp->rev, PATH_REV_SIZE) <= 0)
1465                 return PATHINFO_FAILED;
1466
1467         condlog(3, "%s: rev = %s", pp->dev, pp->rev);
1468
1469         /*
1470          * set the hwe configlet pointer
1471          */
1472         find_hwe(hwtable, pp->vendor_id, pp->product_id, pp->rev, pp->hwe);
1473
1474         /*
1475          * host / bus / target / lun
1476          */
1477         pp->sg_id.lun = 0;
1478         pp->sg_id.channel = 0;
1479         condlog(3, "%s: h:b:t:l = %i:%i:%i:%i",
1480                 pp->dev,
1481                 pp->sg_id.host_no,
1482                 pp->sg_id.channel,
1483                 pp->sg_id.scsi_id,
1484                 pp->sg_id.lun);
1485
1486         return PATHINFO_OK;
1487 }
1488
1489 static int
1490 common_sysfs_pathinfo (struct path * pp)
1491 {
1492         dev_t devt;
1493
1494         if (!pp)
1495                 return PATHINFO_FAILED;
1496
1497         if (!pp->udev) {
1498                 condlog(4, "%s: udev not initialised", pp->dev);
1499                 return PATHINFO_FAILED;
1500         }
1501         devt = udev_device_get_devnum(pp->udev);
1502         snprintf(pp->dev_t, BLK_DEV_SIZE, "%d:%d", major(devt), minor(devt));
1503
1504         condlog(4, "%s: dev_t = %s", pp->dev, pp->dev_t);
1505
1506         if (sysfs_get_size(pp, &pp->size))
1507                 return PATHINFO_FAILED;
1508
1509         condlog(3, "%s: size = %llu", pp->dev, pp->size);
1510
1511         return PATHINFO_OK;
1512 }
1513
1514 int
1515 path_offline (struct path * pp)
1516 {
1517         struct udev_device * parent;
1518         char buff[SCSI_STATE_SIZE];
1519         int err;
1520         const char *subsys_type;
1521
1522         if (pp->bus == SYSFS_BUS_SCSI) {
1523                 subsys_type = "scsi";
1524         }
1525         else if (pp->bus == SYSFS_BUS_NVME) {
1526                 subsys_type = "nvme";
1527         }
1528         else {
1529                 return PATH_UP;
1530         }
1531
1532         parent = pp->udev;
1533         while (parent) {
1534                 const char *subsys = udev_device_get_subsystem(parent);
1535                 if (subsys && !strncmp(subsys, subsys_type, 4))
1536                         break;
1537                 parent = udev_device_get_parent(parent);
1538         }
1539
1540         if (!parent) {
1541                 condlog(1, "%s: failed to get sysfs information", pp->dev);
1542                 return PATH_REMOVED;
1543         }
1544
1545         memset(buff, 0x0, SCSI_STATE_SIZE);
1546         err = sysfs_attr_get_value(parent, "state", buff, SCSI_STATE_SIZE);
1547         if (err <= 0) {
1548                 if (err == -ENXIO)
1549                         return PATH_REMOVED;
1550                 else
1551                         return PATH_DOWN;
1552         }
1553
1554
1555         condlog(4, "%s: path state = %s", pp->dev, buff);
1556
1557         if (pp->bus == SYSFS_BUS_SCSI) {
1558                 if (!strncmp(buff, "offline", 7)) {
1559                         pp->offline = 1;
1560                         return PATH_DOWN;
1561                 }
1562                 pp->offline = 0;
1563                 if (!strncmp(buff, "blocked", 7) ||
1564                     !strncmp(buff, "quiesce", 7))
1565                         return PATH_PENDING;
1566                 else if (!strncmp(buff, "running", 7))
1567                         return PATH_UP;
1568
1569         }
1570         else if (pp->bus == SYSFS_BUS_NVME) {
1571                 if (!strncmp(buff, "dead", 4)) {
1572                         pp->offline = 1;
1573                         return PATH_DOWN;
1574                 }
1575                 pp->offline = 0;
1576                 if (!strncmp(buff, "new", 3) ||
1577                     !strncmp(buff, "deleting", 8))
1578                         return PATH_PENDING;
1579                 else if (!strncmp(buff, "live", 4))
1580                         return PATH_UP;
1581         }
1582
1583         return PATH_DOWN;
1584 }
1585
1586 int
1587 sysfs_pathinfo(struct path * pp, vector hwtable)
1588 {
1589         int r = common_sysfs_pathinfo(pp);
1590
1591         if (r != PATHINFO_OK)
1592                 return r;
1593
1594         pp->bus = SYSFS_BUS_UNDEF;
1595         if (!strncmp(pp->dev,"cciss",5))
1596                 pp->bus = SYSFS_BUS_CCISS;
1597         if (!strncmp(pp->dev,"dasd", 4))
1598                 pp->bus = SYSFS_BUS_CCW;
1599         if (!strncmp(pp->dev,"sd", 2))
1600                 pp->bus = SYSFS_BUS_SCSI;
1601         if (!strncmp(pp->dev,"nvme", 4))
1602                 pp->bus = SYSFS_BUS_NVME;
1603
1604         switch (pp->bus) {
1605         case SYSFS_BUS_SCSI:
1606                 return scsi_sysfs_pathinfo(pp, hwtable);
1607         case SYSFS_BUS_CCW:
1608                 return ccw_sysfs_pathinfo(pp, hwtable);
1609         case SYSFS_BUS_CCISS:
1610                 return cciss_sysfs_pathinfo(pp, hwtable);
1611         case SYSFS_BUS_NVME:
1612                 return nvme_sysfs_pathinfo(pp, hwtable);
1613         case SYSFS_BUS_UNDEF:
1614         default:
1615                 return PATHINFO_OK;
1616         }
1617 }
1618
1619 static void
1620 scsi_ioctl_pathinfo (struct path * pp, int mask)
1621 {
1622         struct udev_device *parent;
1623         const char *attr_path = NULL;
1624         int vpd_id;
1625
1626         if (!(mask & DI_SERIAL))
1627                 return;
1628
1629         select_vpd_vendor_id(pp);
1630         vpd_id = pp->vpd_vendor_id;
1631
1632         if (vpd_id != VPD_VP_UNDEF) {
1633                 char vpd_data[VPD_DATA_SIZE] = {0};
1634
1635                 if (get_vpd_sgio(pp->fd, vpd_vendor_pages[vpd_id].pg, vpd_id,
1636                     vpd_data, sizeof(vpd_data)) < 0)
1637                         condlog(3, "%s: failed to get extra vpd data", pp->dev);
1638                 else {
1639                         vpd_data[VPD_DATA_SIZE - 1] = '\0';
1640                         if (pp->vpd_data)
1641                                 free(pp->vpd_data);
1642                         pp->vpd_data = strdup(vpd_data);
1643                         if (!pp->vpd_data)
1644                                 condlog(0, "%s: failed to allocate space for vpd data", pp->dev);
1645                 }
1646         }
1647
1648         parent = pp->udev;
1649         while (parent) {
1650                 const char *subsys = udev_device_get_subsystem(parent);
1651                 if (subsys && !strncmp(subsys, "scsi", 4)) {
1652                         attr_path = udev_device_get_sysname(parent);
1653                         if (!attr_path)
1654                                 break;
1655                         if (sscanf(attr_path, "%i:%i:%i:%i",
1656                                    &pp->sg_id.host_no,
1657                                    &pp->sg_id.channel,
1658                                    &pp->sg_id.scsi_id,
1659                                    &pp->sg_id.lun) == 4)
1660                                 break;
1661                 }
1662                 parent = udev_device_get_parent(parent);
1663         }
1664         if (!attr_path || pp->sg_id.host_no == -1)
1665                 return;
1666
1667         if (get_vpd_sysfs(parent, 0x80, pp->serial, SERIAL_SIZE) <= 0) {
1668                 if (get_serial(pp->serial, SERIAL_SIZE, pp->fd)) {
1669                         condlog(3, "%s: fail to get serial", pp->dev);
1670                         return;
1671                 }
1672         }
1673
1674         condlog(3, "%s: serial = %s", pp->dev, pp->serial);
1675         return;
1676 }
1677
1678 static void
1679 cciss_ioctl_pathinfo(struct path *pp)
1680 {
1681         get_serial(pp->serial, SERIAL_SIZE, pp->fd);
1682         condlog(3, "%s: serial = %s", pp->dev, pp->serial);
1683 }
1684
1685 int
1686 get_state (struct path * pp, struct config *conf, int daemon, int oldstate)
1687 {
1688         struct checker * c = &pp->checker;
1689         int state;
1690
1691         if (!checker_selected(c)) {
1692                 if (daemon) {
1693                         if (pathinfo(pp, conf, DI_SYSFS) != PATHINFO_OK) {
1694                                 condlog(3, "%s: couldn't get sysfs pathinfo",
1695                                         pp->dev);
1696                                 return PATH_UNCHECKED;
1697                         }
1698                 }
1699                 select_detect_checker(conf, pp);
1700                 select_checker(conf, pp);
1701                 if (!checker_selected(c)) {
1702                         condlog(3, "%s: No checker selected", pp->dev);
1703                         return PATH_UNCHECKED;
1704                 }
1705                 checker_set_fd(c, pp->fd);
1706                 if (checker_init(c, pp->mpp?&pp->mpp->mpcontext:NULL)) {
1707                         checker_clear(c);
1708                         condlog(3, "%s: checker init failed", pp->dev);
1709                         return PATH_UNCHECKED;
1710                 }
1711         }
1712         if (pp->mpp && !c->mpcontext)
1713                 checker_mp_init(c, &pp->mpp->mpcontext);
1714         checker_clear_message(c);
1715         if (conf->force_sync == 0)
1716                 checker_set_async(c);
1717         else
1718                 checker_set_sync(c);
1719         if (!conf->checker_timeout &&
1720             sysfs_get_timeout(pp, &(c->timeout)) <= 0)
1721                 c->timeout = DEF_TIMEOUT;
1722         state = checker_check(c, oldstate);
1723         condlog(3, "%s: %s state = %s", pp->dev,
1724                 checker_name(c), checker_state_name(state));
1725         if (state != PATH_UP && state != PATH_GHOST &&
1726             strlen(checker_message(c)))
1727                 condlog(3, "%s: %s checker%s",
1728                         pp->dev, checker_name(c), checker_message(c));
1729         return state;
1730 }
1731
1732 static int
1733 get_prio (struct path * pp, int timeout)
1734 {
1735         struct prio * p;
1736         struct config *conf;
1737         int old_prio;
1738
1739         if (!pp)
1740                 return 0;
1741
1742         p = &pp->prio;
1743         if (!prio_selected(p)) {
1744                 conf = get_multipath_config();
1745                 pthread_cleanup_push(put_multipath_config, conf);
1746                 select_detect_prio(conf, pp);
1747                 select_prio(conf, pp);
1748                 pthread_cleanup_pop(1);
1749                 if (!prio_selected(p)) {
1750                         condlog(3, "%s: no prio selected", pp->dev);
1751                         pp->priority = PRIO_UNDEF;
1752                         return 1;
1753                 }
1754         }
1755         old_prio = pp->priority;
1756         pp->priority = prio_getprio(p, pp, timeout);
1757         if (pp->priority < 0) {
1758                 /* this changes pp->offline, but why not */
1759                 int state = path_offline(pp);
1760
1761                 if (state == PATH_DOWN || state == PATH_PENDING) {
1762                         pp->priority = old_prio;
1763                         condlog(3, "%s: %s prio error in state %d, keeping prio = %d",
1764                                 pp->dev, prio_name(p), state, pp->priority);
1765                 } else {
1766                         condlog(3, "%s: %s prio error in state %d",
1767                                 pp->dev, prio_name(p), state);
1768                         pp->priority = PRIO_UNDEF;
1769                 }
1770                 return 1;
1771         }
1772         condlog((old_prio == pp->priority ? 4 : 3), "%s: %s prio = %u",
1773                 pp->dev, prio_name(p), pp->priority);
1774         return 0;
1775 }
1776
1777 /*
1778  * Mangle string of length *len starting at start
1779  * by removing character sequence "00" (hex for a 0 byte),
1780  * starting at end, backwards.
1781  * Changes the value of *len if characters were removed.
1782  * Returns a pointer to the position where "end" was moved to.
1783  */
1784 static char
1785 *skip_zeroes_backward(char* start, size_t *len, char *end)
1786 {
1787         char *p = end;
1788
1789         while (p >= start + 2 && *(p - 1) == '0' && *(p - 2) == '0')
1790                 p -= 2;
1791
1792         if (p == end)
1793                 return p;
1794
1795         memmove(p, end, start + *len + 1 - end);
1796         *len -= end - p;
1797
1798         return p;
1799 }
1800
1801 /*
1802  * Fix for NVME wwids looking like this:
1803  * nvme.0000-3163653363666438366239656630386200-4c696e75780000000000000000000000000000000000000000000000000000000000000000000000-00000002
1804  * which are encountered in some combinations of Linux NVME host and target.
1805  * The '00' are hex-encoded 0-bytes which are forbidden in the serial (SN)
1806  * and model (MN) fields. Discard them.
1807  * If a WWID of the above type is found, sets pp->wwid and returns a value > 0.
1808  * Otherwise, returns 0.
1809  */
1810 static int
1811 fix_broken_nvme_wwid(struct path *pp, const char *value, size_t size)
1812 {
1813         static const char _nvme[] = "nvme.";
1814         size_t len, i;
1815         char mangled[256];
1816         char *p;
1817
1818         len = strlen(value);
1819         if (len >= sizeof(mangled))
1820                 return 0;
1821
1822         /* Check that value starts with "nvme.%04x-" */
1823         if (memcmp(value, _nvme, sizeof(_nvme) - 1) || value[9] != '-')
1824                 return 0;
1825         for (i = 5; i < 9; i++)
1826                 if (!isxdigit(value[i]))
1827                         return 0;
1828
1829         memcpy(mangled, value, len + 1);
1830
1831         /* search end of "model" part and strip trailing '00' */
1832         p = memrchr(mangled, '-', len);
1833         if (p == NULL)
1834                 return 0;
1835
1836         p = skip_zeroes_backward(mangled, &len, p);
1837
1838         /* search end of "serial" part */
1839         p = memrchr(mangled, '-', p - mangled);
1840         if (p == NULL || memrchr(mangled, '-', p - mangled) != mangled + 9)
1841             /* We expect exactly 3 '-' in the value */
1842                 return 0;
1843
1844         p = skip_zeroes_backward(mangled, &len, p);
1845         if (len >= size)
1846                 return 0;
1847
1848         memcpy(pp->wwid, mangled, len + 1);
1849         condlog(2, "%s: over-long WWID shortened to %s", pp->dev, pp->wwid);
1850         return len;
1851 }
1852
1853 static int
1854 get_udev_uid(struct path * pp, char *uid_attribute, struct udev_device *udev)
1855 {
1856         ssize_t len;
1857         const char *value;
1858
1859         value = udev_device_get_property_value(udev, uid_attribute);
1860         if (!value || strlen(value) == 0)
1861                 value = getenv(uid_attribute);
1862         if (value && strlen(value)) {
1863                 len = strlcpy(pp->wwid, value, WWID_SIZE);
1864                 if (len >= WWID_SIZE) {
1865                         len = fix_broken_nvme_wwid(pp, value, WWID_SIZE);
1866                         if (len > 0)
1867                                 return len;
1868                         condlog(0, "%s: wwid overflow", pp->dev);
1869                         len = WWID_SIZE;
1870                 }
1871         } else {
1872                 condlog(3, "%s: no %s attribute", pp->dev,
1873                         uid_attribute);
1874                 len = -EINVAL;
1875         }
1876         return len;
1877 }
1878
1879 static int
1880 get_vpd_uid(struct path * pp)
1881 {
1882         struct udev_device *parent = pp->udev;
1883
1884         while (parent) {
1885                 const char *subsys = udev_device_get_subsystem(parent);
1886                 if (subsys && !strncmp(subsys, "scsi", 4))
1887                         break;
1888                 parent = udev_device_get_parent(parent);
1889         }
1890
1891         if (!parent)
1892                 return -EINVAL;
1893
1894         return get_vpd_sysfs(parent, 0x83, pp->wwid, WWID_SIZE);
1895 }
1896
1897 static ssize_t uid_fallback(struct path *pp, int path_state,
1898                             const char **origin)
1899 {
1900         ssize_t len = -1;
1901
1902         if (pp->bus == SYSFS_BUS_SCSI) {
1903                 len = get_vpd_uid(pp);
1904                 *origin = "sysfs";
1905                 if (len < 0 && path_state == PATH_UP) {
1906                         condlog(1, "%s: failed to get sysfs uid: %s",
1907                                 pp->dev, strerror(-len));
1908                         len = get_vpd_sgio(pp->fd, 0x83, 0, pp->wwid,
1909                                            WWID_SIZE);
1910                         *origin = "sgio";
1911                 }
1912         } else if (pp->bus == SYSFS_BUS_NVME) {
1913                 char value[256];
1914                 len = sysfs_attr_get_value(pp->udev, "wwid", value,
1915                                            sizeof(value));
1916                 if (len <= 0)
1917                         return -1;
1918                 len = strlcpy(pp->wwid, value, WWID_SIZE);
1919                 if (len >= WWID_SIZE) {
1920                         len = fix_broken_nvme_wwid(pp, value,
1921                                                    WWID_SIZE);
1922                         if (len > 0)
1923                                 return len;
1924                         condlog(0, "%s: wwid overflow", pp->dev);
1925                         len = WWID_SIZE;
1926                 }
1927                 *origin = "sysfs";
1928         }
1929         return len;
1930 }
1931
1932 static bool has_uid_fallback(struct path *pp)
1933 {
1934         /*
1935          * Falling back to direct WWID determination is dangerous
1936          * if uid_attribute is set to something non-standard.
1937          * Allow it only if it's either the default, or if udev
1938          * has been disabled by setting 'uid_attribute ""'.
1939          */
1940         if (!pp->uid_attribute)
1941                 return false;
1942         return ((pp->bus == SYSFS_BUS_SCSI &&
1943                  (!strcmp(pp->uid_attribute, DEFAULT_UID_ATTRIBUTE) ||
1944                   !strcmp(pp->uid_attribute, ""))) ||
1945                 (pp->bus == SYSFS_BUS_NVME &&
1946                  (!strcmp(pp->uid_attribute, DEFAULT_NVME_UID_ATTRIBUTE) ||
1947                   !strcmp(pp->uid_attribute, ""))));
1948 }
1949
1950 int
1951 get_uid (struct path * pp, int path_state, struct udev_device *udev,
1952          int allow_fallback)
1953 {
1954         char *c;
1955         const char *origin = "unknown";
1956         ssize_t len = 0;
1957         struct config *conf;
1958         int used_fallback = 0;
1959
1960         if (!pp->uid_attribute && !pp->getuid) {
1961                 conf = get_multipath_config();
1962                 pthread_cleanup_push(put_multipath_config, conf);
1963                 select_getuid(conf, pp);
1964                 pthread_cleanup_pop(1);
1965         }
1966
1967         memset(pp->wwid, 0, WWID_SIZE);
1968         if (pp->getuid) {
1969                 char buff[CALLOUT_MAX_SIZE];
1970
1971                 /* Use 'getuid' callout, deprecated */
1972                 condlog(1, "%s: using deprecated getuid callout", pp->dev);
1973                 if (path_state != PATH_UP) {
1974                         condlog(3, "%s: path inaccessible", pp->dev);
1975                         len = -EWOULDBLOCK;
1976                 } else if (apply_format(pp->getuid, &buff[0], pp)) {
1977                         condlog(0, "error formatting uid callout command");
1978                         len = -EINVAL;
1979                 } else if (execute_program(buff, pp->wwid, WWID_SIZE)) {
1980                         condlog(3, "error calling out %s", buff);
1981                         len = -EIO;
1982                 } else
1983                         len = strlen(pp->wwid);
1984                 origin = "callout";
1985         } else {
1986                 bool udev_available = udev && pp->uid_attribute
1987                         && *pp->uid_attribute;
1988
1989                 if (udev_available) {
1990                         len = get_udev_uid(pp, pp->uid_attribute, udev);
1991                         if (len <= 0)
1992                                 condlog(1,
1993                                         "%s: failed to get udev uid: %s",
1994                                         pp->dev, strerror(-len));
1995                         else
1996                                 origin = "udev";
1997                 }
1998                 if ((!udev_available || (len <= 0 && allow_fallback))
1999                     && has_uid_fallback(pp)) {
2000                         used_fallback = 1;
2001                         len = uid_fallback(pp, path_state, &origin);
2002                 }
2003         }
2004         if ( len < 0 ) {
2005                 condlog(1, "%s: failed to get %s uid: %s",
2006                         pp->dev, origin, strerror(-len));
2007                 memset(pp->wwid, 0x0, WWID_SIZE);
2008                 return 1;
2009         } else {
2010                 /* Strip any trailing blanks */
2011                 c = strchr(pp->wwid, '\0');
2012                 c--;
2013                 while (c && c >= pp->wwid && *c == ' ') {
2014                         *c = '\0';
2015                         c--;
2016                 }
2017         }
2018         condlog((used_fallback)? 1 : 3, "%s: uid = %s (%s)", pp->dev,
2019                 *pp->wwid == '\0' ? "<empty>" : pp->wwid, origin);
2020         return 0;
2021 }
2022
2023 int pathinfo(struct path *pp, struct config *conf, int mask)
2024 {
2025         int path_state;
2026
2027         if (!pp || !conf)
2028                 return PATHINFO_FAILED;
2029
2030         /*
2031          * For behavior backward-compatibility with multipathd,
2032          * the blacklisting by filter_property|devnode() is not
2033          * limited by DI_BLACKLIST and occurs before this debug
2034          * message with the mask value.
2035          */
2036         if (pp->udev) {
2037                 const char *hidden =
2038                         udev_device_get_sysattr_value(pp->udev, "hidden");
2039
2040                 if (hidden && !strcmp(hidden, "1")) {
2041                         condlog(4, "%s: hidden", pp->dev);
2042                         return PATHINFO_SKIPPED;
2043                 }
2044                 if (is_claimed_by_foreign(pp->udev) ||
2045                     filter_property(conf, pp->udev, 4, pp->uid_attribute) > 0)
2046                         return PATHINFO_SKIPPED;
2047         }
2048
2049         if (filter_devnode(conf->blist_devnode,
2050                            conf->elist_devnode,
2051                            pp->dev) > 0)
2052                 return PATHINFO_SKIPPED;
2053
2054         condlog(4, "%s: mask = 0x%x", pp->dev, mask);
2055
2056         /*
2057          * Sanity check: we need the device number to
2058          * avoid inconsistent information in
2059          * find_path_by_dev()/find_path_by_devt()
2060          */
2061         if (!strlen(pp->dev_t) && !(mask & DI_SYSFS)) {
2062                 condlog(1, "%s: empty device number", pp->dev);
2063                 mask |= DI_SYSFS;
2064         }
2065
2066         /*
2067          * fetch info available in sysfs
2068          */
2069         if (mask & DI_SYSFS) {
2070                 int rc = sysfs_pathinfo(pp, conf->hwtable);
2071
2072                 if (rc != PATHINFO_OK)
2073                         return rc;
2074         }
2075
2076         if (mask & DI_BLACKLIST && mask & DI_SYSFS) {
2077                 if (filter_device(conf->blist_device, conf->elist_device,
2078                                   pp->vendor_id, pp->product_id, pp->dev) > 0 ||
2079                     filter_protocol(conf->blist_protocol, conf->elist_protocol,
2080                                     pp) > 0)
2081                         return PATHINFO_SKIPPED;
2082         }
2083
2084         path_state = path_offline(pp);
2085         if (path_state == PATH_REMOVED)
2086                 goto blank;
2087         else if (mask & DI_NOIO) {
2088                 if (mask & DI_CHECKER)
2089                         /*
2090                          * Avoid any IO on the device itself.
2091                          * simply use the path_offline() return as its state
2092                          */
2093                         pp->chkrstate = pp->state = path_state;
2094                 return PATHINFO_OK;
2095         }
2096
2097         /*
2098          * fetch info not available through sysfs
2099          */
2100         if (pp->fd < 0)
2101                 pp->fd = open(udev_device_get_devnode(pp->udev), O_RDONLY);
2102
2103         if (pp->fd < 0) {
2104                 condlog(4, "Couldn't open node for %s: %s",
2105                         pp->dev, strerror(errno));
2106                 goto blank;
2107         }
2108
2109         if (mask & DI_SERIAL)
2110                 get_geometry(pp);
2111
2112         if (path_state == PATH_UP && pp->bus == SYSFS_BUS_SCSI)
2113                 scsi_ioctl_pathinfo(pp, mask);
2114
2115         if (pp->bus == SYSFS_BUS_CCISS && mask & DI_SERIAL)
2116                 cciss_ioctl_pathinfo(pp);
2117
2118         if (mask & DI_CHECKER) {
2119                 if (path_state == PATH_UP) {
2120                         int newstate = get_state(pp, conf, 0, path_state);
2121                         if (newstate != PATH_PENDING ||
2122                             pp->state == PATH_UNCHECKED ||
2123                             pp->state == PATH_WILD)
2124                                 pp->chkrstate = pp->state = newstate;
2125                         if (pp->state == PATH_TIMEOUT)
2126                                 pp->state = PATH_DOWN;
2127                         if (pp->state == PATH_UP && !pp->size) {
2128                                 condlog(3, "%s: device size is 0, "
2129                                         "path unusable", pp->dev);
2130                                 pp->state = PATH_GHOST;
2131                         }
2132                 } else {
2133                         condlog(3, "%s: path inaccessible", pp->dev);
2134                         pp->chkrstate = pp->state = path_state;
2135                 }
2136         }
2137
2138         if ((mask & DI_WWID) && !strlen(pp->wwid)) {
2139                 get_uid(pp, path_state, pp->udev,
2140                         (pp->retriggers >= conf->retrigger_tries));
2141                 if (!strlen(pp->wwid)) {
2142                         if (pp->bus == SYSFS_BUS_UNDEF)
2143                                 return PATHINFO_SKIPPED;
2144                         if (pp->initialized != INIT_FAILED) {
2145                                 pp->initialized = INIT_MISSING_UDEV;
2146                                 pp->tick = conf->retrigger_delay;
2147                         }
2148                         return PATHINFO_OK;
2149                 }
2150                 else
2151                         pp->tick = 1;
2152         }
2153
2154         if (mask & DI_BLACKLIST && mask & DI_WWID) {
2155                 if (filter_wwid(conf->blist_wwid, conf->elist_wwid,
2156                                 pp->wwid, pp->dev) > 0) {
2157                         return PATHINFO_SKIPPED;
2158                 }
2159         }
2160
2161          /*
2162           * Retrieve path priority, even for PATH_DOWN paths if it has never
2163           * been successfully obtained before. If path is down don't try
2164           * for too long.
2165           */
2166         if ((mask & DI_PRIO) && path_state == PATH_UP && strlen(pp->wwid)) {
2167                 if (pp->state != PATH_DOWN || pp->priority == PRIO_UNDEF) {
2168                         get_prio(pp, (pp->state != PATH_DOWN)?
2169                                      (conf->checker_timeout * 1000) : 10);
2170                 }
2171         }
2172
2173         if ((mask & DI_ALL) == DI_ALL)
2174                 pp->initialized = INIT_OK;
2175         return PATHINFO_OK;
2176
2177 blank:
2178         /*
2179          * Recoverable error, for example faulty or offline path
2180          */
2181         pp->chkrstate = pp->state = PATH_DOWN;
2182         if (pp->initialized == INIT_NEW || pp->initialized == INIT_FAILED)
2183                 memset(pp->wwid, 0, WWID_SIZE);
2184
2185         return PATHINFO_OK;
2186 }