Read wwid from sysfs vpg_pg83 attribute
[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 <fcntl.h>
10 #include <sys/ioctl.h>
11 #include <sys/stat.h>
12 #include <dirent.h>
13 #include <errno.h>
14 #include <libgen.h>
15 #include <libudev.h>
16
17 #include "checkers.h"
18 #include "vector.h"
19 #include "memory.h"
20 #include "util.h"
21 #include "structs.h"
22 #include "config.h"
23 #include "blacklist.h"
24 #include "callout.h"
25 #include "debug.h"
26 #include "propsel.h"
27 #include "sg_include.h"
28 #include "sysfs.h"
29 #include "discovery.h"
30 #include "prio.h"
31 #include "defaults.h"
32
33 int
34 alloc_path_with_pathinfo (vector hwtable, struct udev_device *udevice,
35                           int flag, struct path **pp_ptr)
36 {
37         int err = PATHINFO_FAILED;
38         struct path * pp;
39         const char * devname;
40
41         if (pp_ptr)
42                 *pp_ptr = NULL;
43
44         devname = udev_device_get_sysname(udevice);
45         if (!devname)
46                 return PATHINFO_FAILED;
47
48         pp = alloc_path();
49
50         if (!pp)
51                 return PATHINFO_FAILED;
52
53         if (safe_sprintf(pp->dev, "%s", devname)) {
54                 condlog(0, "pp->dev too small");
55         } else {
56                 pp->udev = udev_device_ref(udevice);
57                 err = pathinfo(pp, hwtable, flag | DI_BLACKLIST);
58         }
59
60         if (err)
61                 free_path(pp);
62         else if (pp_ptr)
63                 *pp_ptr = pp;
64         return err;
65 }
66
67 int
68 store_pathinfo (vector pathvec, vector hwtable, struct udev_device *udevice,
69                 int flag, struct path **pp_ptr)
70 {
71         int err = PATHINFO_FAILED;
72         struct path * pp;
73         const char * devname;
74
75         if (pp_ptr)
76                 *pp_ptr = NULL;
77
78         devname = udev_device_get_sysname(udevice);
79         if (!devname)
80                 return PATHINFO_FAILED;
81
82         pp = alloc_path();
83
84         if (!pp)
85                 return PATHINFO_FAILED;
86
87         if(safe_sprintf(pp->dev, "%s", devname)) {
88                 condlog(0, "pp->dev too small");
89                 goto out;
90         }
91         pp->udev = udev_device_ref(udevice);
92         err = pathinfo(pp, hwtable,
93                        (conf->cmd == CMD_REMOVE_WWID)? flag :
94                                                        (flag | DI_BLACKLIST));
95         if (err)
96                 goto out;
97
98         err = store_path(pathvec, pp);
99         if (err)
100                 goto out;
101
102 out:
103         if (err)
104                 free_path(pp);
105         else if (pp_ptr)
106                 *pp_ptr = pp;
107         return err;
108 }
109
110 static int
111 path_discover (vector pathvec, struct config * conf,
112                struct udev_device *udevice, int flag)
113 {
114         struct path * pp;
115         const char * devname;
116
117         devname = udev_device_get_sysname(udevice);
118         if (!devname)
119                 return PATHINFO_FAILED;
120
121         if (filter_property(conf, udevice) > 0)
122                 return PATHINFO_SKIPPED;
123
124         if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
125                            (char *)devname) > 0)
126                 return PATHINFO_SKIPPED;
127
128         pp = find_path_by_dev(pathvec, (char *)devname);
129         if (!pp) {
130                 return store_pathinfo(pathvec, conf->hwtable,
131                                       udevice, flag, NULL);
132         }
133         return pathinfo(pp, conf->hwtable, flag);
134 }
135
136 int
137 path_discovery (vector pathvec, struct config * conf, int flag)
138 {
139         struct udev_enumerate *udev_iter;
140         struct udev_list_entry *entry;
141         struct udev_device *udevice;
142         const char *devpath;
143         int num_paths = 0, total_paths = 0;
144
145         udev_iter = udev_enumerate_new(conf->udev);
146         if (!udev_iter)
147                 return -ENOMEM;
148
149         udev_enumerate_add_match_subsystem(udev_iter, "block");
150         udev_enumerate_scan_devices(udev_iter);
151
152         udev_list_entry_foreach(entry,
153                                 udev_enumerate_get_list_entry(udev_iter)) {
154                 const char *devtype;
155                 devpath = udev_list_entry_get_name(entry);
156                 condlog(4, "Discover device %s", devpath);
157                 udevice = udev_device_new_from_syspath(conf->udev, devpath);
158                 if (!udevice) {
159                         condlog(4, "%s: no udev information", devpath);
160                         continue;
161                 }
162                 devtype = udev_device_get_devtype(udevice);
163                 if(devtype && !strncmp(devtype, "disk", 4)) {
164                         total_paths++;
165                         if (path_discover(pathvec, conf,
166                                           udevice, flag) == PATHINFO_OK)
167                                 num_paths++;
168                 }
169                 udev_device_unref(udevice);
170         }
171         udev_enumerate_unref(udev_iter);
172         condlog(4, "Discovered %d/%d paths", num_paths, total_paths);
173         return (total_paths - num_paths);
174 }
175
176 #define declare_sysfs_get_str(fname)                                    \
177 extern ssize_t                                                          \
178 sysfs_get_##fname (struct udev_device * udev, char * buff, size_t len)  \
179 {                                                                       \
180         int l;                                                  \
181         const char * attr;                                              \
182         const char * devname;                                           \
183                                                                         \
184         if (!udev)                                                      \
185                 return -ENOSYS;                                         \
186                                                                         \
187         devname = udev_device_get_sysname(udev);                        \
188                                                                         \
189         attr = udev_device_get_sysattr_value(udev, #fname);             \
190         if (!attr) {                                                    \
191                 condlog(3, "%s: attribute %s not found in sysfs",       \
192                         devname, #fname);                               \
193                 return -ENXIO;                                          \
194         }                                                               \
195         for (l = strlen(attr); l >= 1 && isspace(attr[l-1]); l--);      \
196         if (l > len) {                                                  \
197                 condlog(3, "%s: overflow in attribute %s",              \
198                         devname, #fname);                               \
199                 return -EINVAL;                                         \
200         }                                                               \
201         strlcpy(buff, attr, len);                                       \
202         return strchop(buff);                                           \
203 }
204
205 declare_sysfs_get_str(devtype);
206 declare_sysfs_get_str(vendor);
207 declare_sysfs_get_str(model);
208 declare_sysfs_get_str(rev);
209
210 ssize_t
211 sysfs_get_vpd (struct udev_device * udev, int pg,
212                unsigned char * buff, size_t len)
213 {
214         ssize_t attr_len;
215         char attrname[9];
216         const char * devname;
217
218         if (!udev) {
219                 condlog(3, "No udev device given\n");
220                 return -ENOSYS;
221         }
222
223         devname = udev_device_get_sysname(udev);
224         sprintf(attrname, "vpd_pg%02x", pg);
225         attr_len = sysfs_bin_attr_get_value(udev, attrname, buff, len);
226         if (attr_len < 0) {
227                 condlog(3, "%s: attribute %s not found in sysfs",
228                         devname, attrname);
229                 return attr_len;
230         }
231         return attr_len;
232 }
233
234 int
235 sysfs_get_timeout(struct path *pp, unsigned int *timeout)
236 {
237         const char *attr = NULL;
238         const char *subsys;
239         struct udev_device *parent;
240         int r;
241         unsigned int t;
242
243         if (!pp->udev || pp->bus != SYSFS_BUS_SCSI)
244                 return -ENOSYS;
245
246         parent = pp->udev;
247         while (parent) {
248                 subsys = udev_device_get_subsystem(parent);
249                 attr = udev_device_get_sysattr_value(parent, "timeout");
250                 if (subsys && attr)
251                         break;
252                 parent = udev_device_get_parent(parent);
253         }
254         if (!attr) {
255                 condlog(3, "%s: No timeout value in sysfs", pp->dev);
256                 return -ENXIO;
257         }
258
259         r = sscanf(attr, "%u\n", &t);
260
261         if (r != 1) {
262                 condlog(3, "%s: Cannot parse timeout attribute '%s'",
263                         pp->dev, attr);
264                 return -EINVAL;
265         }
266
267         *timeout = t;
268
269         return 0;
270 }
271
272 int
273 sysfs_get_tgt_nodename (struct path *pp, char * node)
274 {
275         const char *tgtname, *value;
276         struct udev_device *parent, *tgtdev;
277         int host, channel, tgtid = -1;
278
279         parent = udev_device_get_parent_with_subsystem_devtype(pp->udev, "scsi", "scsi_device");
280         if (!parent)
281                 return 1;
282         /* Check for SAS */
283         value = udev_device_get_sysattr_value(parent, "sas_address");
284         if (value) {
285                 tgtdev = udev_device_get_parent(parent);
286                 while (tgtdev) {
287                         tgtname = udev_device_get_sysname(tgtdev);
288                         if (sscanf(tgtname, "end_device-%d:%d",
289                                    &host, &tgtid) == 2)
290                                 break;
291                         tgtdev = udev_device_get_parent(tgtdev);
292                         tgtid = -1;
293                 }
294                 if (tgtid >= 0) {
295                         pp->sg_id.proto_id = SCSI_PROTOCOL_SAS;
296                         pp->sg_id.transport_id = tgtid;
297                         strncpy(node, value, NODE_NAME_SIZE);
298                         return 0;
299                 }
300         }
301
302         /* Check for USB */
303         tgtdev = udev_device_get_parent(parent);
304         while (tgtdev) {
305                 value = udev_device_get_subsystem(tgtdev);
306                 if (value && !strcmp(value, "usb")) {
307                         pp->sg_id.proto_id = SCSI_PROTOCOL_UNSPEC;
308                         tgtname = udev_device_get_sysname(tgtdev);
309                         strncpy(node, tgtname, strlen(tgtname));
310                         condlog(3, "%s: skip USB device %s", pp->dev, node);
311                         return 1;
312                 }
313                 tgtdev = udev_device_get_parent(tgtdev);
314         }
315         parent = udev_device_get_parent_with_subsystem_devtype(pp->udev, "scsi", "scsi_target");
316         if (!parent)
317                 return 1;
318         /* Check for FibreChannel */
319         tgtdev = udev_device_get_parent(parent);
320         value = udev_device_get_sysname(tgtdev);
321         if (sscanf(value, "rport-%d:%d-%d",
322                    &host, &channel, &tgtid) == 3) {
323                 tgtdev = udev_device_new_from_subsystem_sysname(conf->udev,
324                                 "fc_remote_ports", value);
325                 if (tgtdev) {
326                         condlog(3, "SCSI target %d:%d:%d -> "
327                                 "FC rport %d:%d-%d",
328                                 pp->sg_id.host_no, pp->sg_id.channel,
329                                 pp->sg_id.scsi_id, host, channel,
330                                 tgtid);
331                         value = udev_device_get_sysattr_value(tgtdev,
332                                                               "node_name");
333                         if (value) {
334                                 pp->sg_id.proto_id = SCSI_PROTOCOL_FCP;
335                                 pp->sg_id.transport_id = tgtid;
336                                 strncpy(node, value, NODE_NAME_SIZE);
337                                 udev_device_unref(tgtdev);
338                                 return 0;
339                         } else
340                                 udev_device_unref(tgtdev);
341                 }
342         }
343
344         /* Check for iSCSI */
345         parent = pp->udev;
346         tgtname = NULL;
347         while (parent) {
348                 tgtname = udev_device_get_sysname(parent);
349                 if (tgtname && sscanf(tgtname , "session%d", &tgtid) == 1)
350                         break;
351                 parent = udev_device_get_parent(parent);
352                 tgtname = NULL;
353                 tgtid = -1;
354         }
355         if (parent && tgtname) {
356                 tgtdev = udev_device_new_from_subsystem_sysname(conf->udev,
357                                 "iscsi_session", tgtname);
358                 if (tgtdev) {
359                         const char *value;
360
361                         value = udev_device_get_sysattr_value(tgtdev, "targetname");
362                         if (value) {
363                                 pp->sg_id.proto_id = SCSI_PROTOCOL_ISCSI;
364                                 pp->sg_id.transport_id = tgtid;
365                                 strncpy(node, value, NODE_NAME_SIZE);
366                                 udev_device_unref(tgtdev);
367                                 return 0;
368                         }
369                         else
370                                 udev_device_unref(tgtdev);
371                 }
372         }
373         /* Check for libata */
374         parent = pp->udev;
375         tgtname = NULL;
376         while (parent) {
377                 tgtname = udev_device_get_sysname(parent);
378                 if (tgtname && sscanf(tgtname, "ata%d", &tgtid) == 1)
379                         break;
380                 parent = udev_device_get_parent(parent);
381                 tgtname = NULL;
382         }
383         if (tgtname) {
384                 pp->sg_id.proto_id = SCSI_PROTOCOL_ATA;
385                 pp->sg_id.transport_id = tgtid;
386                 snprintf(node, NODE_NAME_SIZE, "ata-%d.00", tgtid);
387                 return 0;
388         }
389         /* Unknown SCSI transport. Keep fingers crossed */
390         pp->sg_id.proto_id = SCSI_PROTOCOL_UNSPEC;
391         return 0;
392 }
393
394 int sysfs_get_host_adapter_name(struct path *pp, char *adapter_name)
395 {
396         int proto_id;
397
398         if (!pp || !adapter_name)
399                 return 1;
400
401         proto_id = pp->sg_id.proto_id;
402
403         if (proto_id != SCSI_PROTOCOL_FCP &&
404             proto_id != SCSI_PROTOCOL_SAS &&
405             proto_id != SCSI_PROTOCOL_ISCSI &&
406             proto_id != SCSI_PROTOCOL_SRP) {
407                 return 1;
408         }
409         /* iscsi doesn't have adapter info in sysfs
410          * get ip_address for grouping paths
411          */
412         if (pp->sg_id.proto_id == SCSI_PROTOCOL_ISCSI)
413                 return sysfs_get_iscsi_ip_address(pp, adapter_name);
414
415         /* fetch adapter pci name for other protocols
416          */
417         return sysfs_get_host_pci_name(pp, adapter_name);
418 }
419
420 int sysfs_get_host_pci_name(struct path *pp, char *pci_name)
421 {
422         struct udev_device *hostdev, *parent;
423         char host_name[HOST_NAME_LEN];
424         const char *driver_name, *value;
425
426         if (!pp || !pci_name)
427                 return 1;
428
429         sprintf(host_name, "host%d", pp->sg_id.host_no);
430         hostdev = udev_device_new_from_subsystem_sysname(conf->udev,
431                         "scsi_host", host_name);
432         if (!hostdev)
433                 return 1;
434
435         parent = udev_device_get_parent(hostdev);
436         while (parent) {
437                 driver_name = udev_device_get_driver(parent);
438                 if (!driver_name) {
439                         parent = udev_device_get_parent(parent);
440                         continue;
441                 }
442                 if (!strcmp(driver_name, "pcieport"))
443                         break;
444                 parent = udev_device_get_parent(parent);
445         }
446         if (parent) {
447                 /* pci_device found
448                  */
449                 value = udev_device_get_sysname(parent);
450
451                 strncpy(pci_name, value, SLOT_NAME_SIZE);
452                 udev_device_unref(hostdev);
453                 return 0;
454         }
455         udev_device_unref(hostdev);
456         return 1;
457 }
458
459 int sysfs_get_iscsi_ip_address(struct path *pp, char *ip_address)
460 {
461         struct udev_device *hostdev;
462         char host_name[HOST_NAME_LEN];
463         const char *value;
464
465         sprintf(host_name, "host%d", pp->sg_id.host_no);
466         hostdev = udev_device_new_from_subsystem_sysname(conf->udev,
467                         "iscsi_host", host_name);
468         if (hostdev) {
469                 value = udev_device_get_sysattr_value(hostdev,
470                                 "ipaddress");
471                 if (value) {
472                         strncpy(ip_address, value, SLOT_NAME_SIZE);
473                         udev_device_unref(hostdev);
474                         return 0;
475                 } else
476                         udev_device_unref(hostdev);
477         }
478         return 1;
479 }
480
481 static void
482 sysfs_set_rport_tmo(struct multipath *mpp, struct path *pp)
483 {
484         struct udev_device *rport_dev = NULL;
485         char value[16];
486         char rport_id[32];
487         unsigned long long tmo = 0;
488         int ret;
489
490         sprintf(rport_id, "rport-%d:%d-%d",
491                 pp->sg_id.host_no, pp->sg_id.channel, pp->sg_id.transport_id);
492         rport_dev = udev_device_new_from_subsystem_sysname(conf->udev,
493                                 "fc_remote_ports", rport_id);
494         if (!rport_dev) {
495                 condlog(1, "%s: No fc_remote_port device for '%s'", pp->dev,
496                         rport_id);
497                 return;
498         }
499         condlog(4, "target%d:%d:%d -> %s", pp->sg_id.host_no,
500                 pp->sg_id.channel, pp->sg_id.scsi_id, rport_id);
501
502         /*
503          * This is tricky.
504          * dev_loss_tmo will be limited to 600 if fast_io_fail
505          * is _not_ set.
506          * fast_io_fail will be limited by the current dev_loss_tmo
507          * setting.
508          * So to get everything right we first need to increase
509          * dev_loss_tmo to the fast_io_fail setting (if present),
510          * then set fast_io_fail, and _then_ set dev_loss_tmo
511          * to the correct value.
512          */
513         memset(value, 0, 16);
514         if (mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET &&
515             mpp->fast_io_fail != MP_FAST_IO_FAIL_ZERO &&
516             mpp->fast_io_fail != MP_FAST_IO_FAIL_OFF) {
517                 /* Check if we need to temporarily increase dev_loss_tmo */
518                 ret = sysfs_attr_get_value(rport_dev, "dev_loss_tmo",
519                                            value, 16);
520                 if (ret <= 0) {
521                         condlog(0, "%s: failed to read dev_loss_tmo value, "
522                                 "error %d", rport_id, -ret);
523                         goto out;
524                 }
525                 if (sscanf(value, "%llu\n", &tmo) != 1) {
526                         condlog(0, "%s: Cannot parse dev_loss_tmo "
527                                 "attribute '%s'", rport_id, value);
528                         goto out;
529                 }
530                 if (mpp->fast_io_fail >= tmo) {
531                         snprintf(value, 16, "%u", mpp->fast_io_fail + 1);
532                 }
533         } else if (mpp->dev_loss > 600) {
534                 condlog(3, "%s: limiting dev_loss_tmo to 600, since "
535                         "fast_io_fail is not set", rport_id);
536                 snprintf(value, 16, "%u", 600);
537         } else {
538                 snprintf(value, 16, "%u", mpp->dev_loss);
539         }
540         if (strlen(value)) {
541                 ret = sysfs_attr_set_value(rport_dev, "dev_loss_tmo",
542                                            value, strlen(value));
543                 if (ret <= 0) {
544                         if (ret == -EBUSY)
545                                 condlog(3, "%s: rport blocked", rport_id);
546                         else
547                                 condlog(0, "%s: failed to set dev_loss_tmo to %s, error %d",
548                                         rport_id, value, -ret);
549                         goto out;
550                 }
551         }
552         if (mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET) {
553                 if (mpp->fast_io_fail == MP_FAST_IO_FAIL_OFF)
554                         sprintf(value, "off");
555                 else if (mpp->fast_io_fail == MP_FAST_IO_FAIL_ZERO)
556                         sprintf(value, "0");
557                 else
558                         snprintf(value, 16, "%u", mpp->fast_io_fail);
559                 ret = sysfs_attr_set_value(rport_dev, "fast_io_fail_tmo",
560                                            value, strlen(value));
561                 if (ret <= 0) {
562                         if (ret == -EBUSY)
563                                 condlog(3, "%s: rport blocked", rport_id);
564                         else
565                                 condlog(0, "%s: failed to set fast_io_fail_tmo to %s, error %d",
566                                         rport_id, value, -ret);
567                 }
568         }
569         if (tmo > 0) {
570                 snprintf(value, 16, "%u", mpp->dev_loss);
571                 ret = sysfs_attr_set_value(rport_dev, "dev_loss_tmo",
572                                            value, strlen(value));
573                 if (ret <= 0) {
574                         if (ret == -EBUSY)
575                                 condlog(3, "%s: rport blocked", rport_id);
576                         else
577                                 condlog(0, "%s: failed to set dev_loss_tmo to %s, error %d",
578                                         rport_id, value, -ret);
579                 }
580         }
581 out:
582         udev_device_unref(rport_dev);
583 }
584
585 static void
586 sysfs_set_session_tmo(struct multipath *mpp, struct path *pp)
587 {
588         struct udev_device *session_dev = NULL;
589         char session_id[64];
590         char value[11];
591
592         sprintf(session_id, "session%d", pp->sg_id.transport_id);
593         session_dev = udev_device_new_from_subsystem_sysname(conf->udev,
594                                 "iscsi_session", session_id);
595         if (!session_dev) {
596                 condlog(1, "%s: No iscsi session for '%s'", pp->dev,
597                         session_id);
598                 return;
599         }
600         condlog(4, "target%d:%d:%d -> %s", pp->sg_id.host_no,
601                 pp->sg_id.channel, pp->sg_id.scsi_id, session_id);
602
603         if (mpp->dev_loss) {
604                 condlog(3, "%s: ignoring dev_loss_tmo on iSCSI", pp->dev);
605         }
606         if (mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET) {
607                 if (mpp->fast_io_fail == MP_FAST_IO_FAIL_OFF) {
608                         condlog(3, "%s: can't switch off fast_io_fail_tmo "
609                                 "on iSCSI", pp->dev);
610                 } else if (mpp->fast_io_fail == MP_FAST_IO_FAIL_ZERO) {
611                         condlog(3, "%s: can't set fast_io_fail_tmo to '0'"
612                                 "on iSCSI", pp->dev);
613                 } else {
614                         snprintf(value, 11, "%u", mpp->fast_io_fail);
615                         if (sysfs_attr_set_value(session_dev, "recovery_tmo",
616                                                  value, 11) <= 0) {
617                                 condlog(3, "%s: Failed to set recovery_tmo, "
618                                         " error %d", pp->dev, errno);
619                         }
620                 }
621         }
622         udev_device_unref(session_dev);
623         return;
624 }
625
626 static void
627 sysfs_set_nexus_loss_tmo(struct multipath *mpp, struct path *pp)
628 {
629         struct udev_device *sas_dev = NULL;
630         char end_dev_id[64];
631         char value[11];
632
633         sprintf(end_dev_id, "end_device-%d:%d",
634                 pp->sg_id.host_no, pp->sg_id.transport_id);
635         sas_dev = udev_device_new_from_subsystem_sysname(conf->udev,
636                                 "sas_end_device", end_dev_id);
637         if (!sas_dev) {
638                 condlog(1, "%s: No SAS end device for '%s'", pp->dev,
639                         end_dev_id);
640                 return;
641         }
642         condlog(4, "target%d:%d:%d -> %s", pp->sg_id.host_no,
643                 pp->sg_id.channel, pp->sg_id.scsi_id, end_dev_id);
644
645         if (mpp->dev_loss) {
646                 snprintf(value, 11, "%u", mpp->dev_loss);
647                 if (sysfs_attr_set_value(sas_dev, "I_T_nexus_loss_timeout",
648                                          value, 11) <= 0)
649                         condlog(3, "%s: failed to update "
650                                 "I_T Nexus loss timeout, error %d",
651                                 pp->dev, errno);
652         }
653         udev_device_unref(sas_dev);
654         return;
655 }
656
657 int
658 sysfs_set_scsi_tmo (struct multipath *mpp)
659 {
660         struct path *pp;
661         int i;
662         int dev_loss_tmo = mpp->dev_loss;
663
664         if (mpp->no_path_retry > 0) {
665                 int no_path_retry_tmo = mpp->no_path_retry * conf->checkint;
666
667                 if (no_path_retry_tmo > MAX_DEV_LOSS_TMO)
668                         no_path_retry_tmo = MAX_DEV_LOSS_TMO;
669                 if (no_path_retry_tmo > dev_loss_tmo)
670                         dev_loss_tmo = no_path_retry_tmo;
671                 condlog(3, "%s: update dev_loss_tmo to %d",
672                         mpp->alias, dev_loss_tmo);
673         } else if (mpp->no_path_retry == NO_PATH_RETRY_QUEUE) {
674                 dev_loss_tmo = MAX_DEV_LOSS_TMO;
675                 condlog(3, "%s: update dev_loss_tmo to %d",
676                         mpp->alias, dev_loss_tmo);
677         }
678         mpp->dev_loss = dev_loss_tmo;
679         if (mpp->dev_loss && mpp->fast_io_fail >= (int)mpp->dev_loss) {
680                 condlog(3, "%s: turning off fast_io_fail (%d is not smaller than dev_loss_tmo)",
681                         mpp->alias, mpp->fast_io_fail);
682                 mpp->fast_io_fail = MP_FAST_IO_FAIL_OFF;
683         }
684         if (!mpp->dev_loss && mpp->fast_io_fail == MP_FAST_IO_FAIL_UNSET)
685                 return 0;
686
687         vector_foreach_slot(mpp->paths, pp, i) {
688                 if (pp->sg_id.proto_id == SCSI_PROTOCOL_FCP)
689                         sysfs_set_rport_tmo(mpp, pp);
690                 if (pp->sg_id.proto_id == SCSI_PROTOCOL_ISCSI)
691                         sysfs_set_session_tmo(mpp, pp);
692                 if (pp->sg_id.proto_id == SCSI_PROTOCOL_SAS)
693                         sysfs_set_nexus_loss_tmo(mpp, pp);
694         }
695         return 0;
696 }
697
698 int
699 do_inq(int sg_fd, int cmddt, int evpd, unsigned int pg_op,
700        void *resp, int mx_resp_len)
701 {
702         unsigned char inqCmdBlk[INQUIRY_CMDLEN] =
703                 { INQUIRY_CMD, 0, 0, 0, 0, 0 };
704         unsigned char sense_b[SENSE_BUFF_LEN];
705         struct sg_io_hdr io_hdr;
706
707         if (cmddt)
708                 inqCmdBlk[1] |= 2;
709         if (evpd)
710                 inqCmdBlk[1] |= 1;
711         inqCmdBlk[2] = (unsigned char) pg_op;
712         inqCmdBlk[3] = (unsigned char)((mx_resp_len >> 8) & 0xff);
713         inqCmdBlk[4] = (unsigned char) (mx_resp_len & 0xff);
714         memset(&io_hdr, 0, sizeof (struct sg_io_hdr));
715         memset(sense_b, 0, SENSE_BUFF_LEN);
716         io_hdr.interface_id = 'S';
717         io_hdr.cmd_len = sizeof (inqCmdBlk);
718         io_hdr.mx_sb_len = sizeof (sense_b);
719         io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
720         io_hdr.dxfer_len = mx_resp_len;
721         io_hdr.dxferp = resp;
722         io_hdr.cmdp = inqCmdBlk;
723         io_hdr.sbp = sense_b;
724         io_hdr.timeout = DEF_TIMEOUT;
725
726         if (ioctl(sg_fd, SG_IO, &io_hdr) < 0)
727                 return -1;
728
729         /* treat SG_ERR here to get rid of sg_err.[ch] */
730         io_hdr.status &= 0x7e;
731         if ((0 == io_hdr.status) && (0 == io_hdr.host_status) &&
732             (0 == io_hdr.driver_status))
733                 return 0;
734         if ((SCSI_CHECK_CONDITION == io_hdr.status) ||
735             (SCSI_COMMAND_TERMINATED == io_hdr.status) ||
736             (SG_ERR_DRIVER_SENSE == (0xf & io_hdr.driver_status))) {
737                 if (io_hdr.sbp && (io_hdr.sb_len_wr > 2)) {
738                         int sense_key;
739                         unsigned char * sense_buffer = io_hdr.sbp;
740                         if (sense_buffer[0] & 0x2)
741                                 sense_key = sense_buffer[1] & 0xf;
742                         else
743                                 sense_key = sense_buffer[2] & 0xf;
744                         if(RECOVERED_ERROR == sense_key)
745                                 return 0;
746                 }
747         }
748         return -1;
749 }
750
751 static int
752 get_serial (char * str, int maxlen, int fd)
753 {
754         int len = 0;
755         char buff[MX_ALLOC_LEN + 1] = {0};
756
757         if (fd < 0)
758                 return 1;
759
760         if (0 == do_inq(fd, 0, 1, 0x80, buff, MX_ALLOC_LEN)) {
761                 len = buff[3];
762                 if (len >= maxlen)
763                         return 1;
764                 if (len > 0) {
765                         memcpy(str, buff + 4, len);
766                         str[len] = '\0';
767                 }
768                 return 0;
769         }
770         return 1;
771 }
772
773 static int
774 get_geometry(struct path *pp)
775 {
776         if (pp->fd < 0)
777                 return 1;
778
779         if (ioctl(pp->fd, HDIO_GETGEO, &pp->geom)) {
780                 condlog(2, "%s: HDIO_GETGEO failed with %d", pp->dev, errno);
781                 memset(&pp->geom, 0, sizeof(pp->geom));
782                 return 1;
783         }
784         condlog(3, "%s: %u cyl, %u heads, %u sectors/track, start at %lu",
785                 pp->dev, pp->geom.cylinders, pp->geom.heads,
786                 pp->geom.sectors, pp->geom.start);
787         return 0;
788 }
789
790 static int
791 get_vpd (struct udev_device *parent, int pg, char * str, int maxlen)
792 {
793         int len = -ENODATA, buff_len;
794         unsigned char buff[4096];
795
796         memset(buff, 0x0, 4096);
797         if (sysfs_get_vpd(parent, pg, buff, 4096) <= 0) {
798                 condlog(3, "failed to get vpd pg%02x", pg);
799                 return -EIO;
800         }
801
802         if (buff[1] != pg) {
803                 condlog(3, "vpd pg%02x error, invalid vpd page %02x",
804                         pg, buff[1]);
805                 return -ENODATA;
806         }
807         buff_len = (buff[2] << 8) + buff[3] + 4;
808         if (buff_len > 4096)
809                 condlog(3, "vpd pg%02x page truncated", pg);
810
811         if (pg == 0x80) {
812                 char *p = NULL;
813                 len = buff[3] + (buff[2] << 8);
814                 if (len >= maxlen) {
815                         condlog(3, "vpd pg%02x overflow, %d/%d bytes required",
816                                 pg, len, maxlen);
817                         return -EINVAL;
818                 }
819                 if (len > 0) {
820                         memcpy(str, buff + 4, len);
821                         str[len] = '\0';
822                 }
823                 p = str + len - 1;
824                 while (p > str && *p == ' ') {
825                         *p = '\0';
826                         p--;
827                         len --;
828                 }
829         } else if (pg == 0x83) {
830                 unsigned char *d;
831                 unsigned char *vpd = NULL;
832                 int vpd_type, vpd_len, prio = -1, i;
833
834                 d = (unsigned char *)buff + 4;
835                 while (d < (unsigned char *)buff + buff_len) {
836                         /* Select 'association: LUN' */
837                         if ((d[1] & 0x30) != 0) {
838                                 d += d[3] + 4;
839                                 continue;
840                         }
841                         switch (d[1] & 0xf) {
842                         case 0x3:
843                                 /* NAA: Prio 5 */
844                                 if (prio < 5) {
845                                         prio = 5;
846                                         vpd = d;
847                                 }
848                                 break;
849                         case 0x8:
850                                 /* SCSI Name: Prio 4 */
851                                 if (memcmp(d + 4, "eui.", 4) &&
852                                     memcmp(d + 4, "naa.", 4) &&
853                                     memcmp(d + 4, "iqn.", 4))
854                                         continue;
855                                 if (prio < 4) {
856                                         prio = 4;
857                                         vpd = d;
858                                 }
859                                 break;
860                         case 0x2:
861                                 /* EUI-64: Prio 3 */
862                                 if (prio < 3) {
863                                         prio = 3;
864                                         vpd = d;
865                                 }
866                                 break;
867                         case 0x1:
868                                 /* T-10 Vendor ID: Prio 2 */
869                                 if (prio < 2) {
870                                         prio = 2;
871                                         vpd = d;
872                                 }
873                                 break;
874                         }
875                         d += d[3] + 4;
876                 }
877                 if (prio > 0) {
878                         vpd_type = vpd[1] & 0xf;
879                         vpd_len = vpd[3];
880                         vpd += 4;
881                         if (vpd_type == 0x2 || vpd_type == 0x3) {
882                                 int i;
883
884                                 len = sprintf(str, "%d", vpd_type);
885                                 for (i = 0; i < vpd_len; i++) {
886                                         len += sprintf(str + len,
887                                                        "%02x", vpd[i]);
888                                         if (len >= maxlen)
889                                                 break;
890                                 }
891                         } else if (vpd_type == 0x8) {
892                                 if (!memcmp("eui.", vpd, 4)) {
893                                         str[0] =  '2';
894                                         len = 1;
895                                         vpd += 4;
896                                         vpd_len -= 4;
897                                         for (i = 0; i < vpd_len; i++) {
898                                                 len += sprintf(str + len, "%c",
899                                                                tolower(vpd[i]));
900                                                 if (len >= maxlen)
901                                                         break;
902                                         }
903                                         len = vpd_len + 1;
904                                         str[len] = '\0';
905                                 } else if (!memcmp("naa.", vpd, 4)) {
906                                         str[0] = '3';
907                                         len = 1;
908                                         vpd += 4;
909                                         vpd_len -= 4;
910                                         for (i = 0; i < vpd_len; i++) {
911                                                 len += sprintf(str + len, "%c",
912                                                                tolower(vpd[i]));
913                                                 if (len >= maxlen)
914                                                         break;
915                                         }
916                                         len = vpd_len + 1;
917                                         str[len] = '\0';
918                                 } else {
919                                         str[0] = '8';
920                                         len = 1;
921                                         vpd += 4;
922                                         vpd_len -= 4;
923                                         if (vpd_len > maxlen + 2)
924                                                 vpd_len = maxlen - 2;
925                                         memcpy(str, vpd, vpd_len);
926                                         len = vpd_len + 1;
927                                         str[len] = '\0';
928                                 }
929                         } else if (vpd_type == 0x1) {
930                                 unsigned char *p;
931                                 int p_len;
932
933                                 str[0] = '1';
934                                 len = 1;
935                                 p = vpd;
936                                 while ((p = memchr(vpd, ' ', vpd_len))) {
937                                         p_len = p - vpd;
938                                         if (len + p_len > maxlen - 1)
939                                                 p_len = maxlen - len - 2;
940                                         memcpy(str + len, vpd, p_len);
941                                         len += p_len;
942                                         if (len >= maxlen - 1) {
943                                                 str[len] = '\0';
944                                                 break;
945                                         }
946                                         str[len] = '_';
947                                         len ++;
948                                         vpd = p;
949                                         vpd_len -= p_len;
950                                         while (vpd && *vpd == ' ') {
951                                                 vpd++;
952                                                 vpd_len --;
953                                         }
954                                 }
955                                 if (len > 1 && str[len - 1] == '_') {
956                                         str[len - 1] = '\0';
957                                         len--;
958                                 }
959                         }
960                 }
961         } else
962                 len = -ENOSYS;
963
964         return len;
965 }
966
967 static int
968 scsi_sysfs_pathinfo (struct path * pp)
969 {
970         struct udev_device *parent;
971         const char *attr_path = NULL;
972
973         parent = pp->udev;
974         while (parent) {
975                 const char *subsys = udev_device_get_subsystem(parent);
976                 if (subsys && !strncmp(subsys, "scsi", 4)) {
977                         attr_path = udev_device_get_sysname(parent);
978                         if (!attr_path)
979                                 break;
980                         if (sscanf(attr_path, "%i:%i:%i:%i",
981                                    &pp->sg_id.host_no,
982                                    &pp->sg_id.channel,
983                                    &pp->sg_id.scsi_id,
984                                    &pp->sg_id.lun) == 4)
985                                 break;
986                 }
987                 parent = udev_device_get_parent(parent);
988         }
989         if (!attr_path || pp->sg_id.host_no == -1)
990                 return 1;
991
992         if (sysfs_get_vendor(parent, pp->vendor_id, SCSI_VENDOR_SIZE) <= 0)
993                 return 1;
994
995         condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
996
997         if (sysfs_get_model(parent, pp->product_id, SCSI_PRODUCT_SIZE) <= 0)
998                 return 1;
999
1000         condlog(3, "%s: product = %s", pp->dev, pp->product_id);
1001
1002         if (sysfs_get_rev(parent, pp->rev, SCSI_REV_SIZE) <= 0)
1003                 return 1;
1004
1005         condlog(3, "%s: rev = %s", pp->dev, pp->rev);
1006
1007         if (get_vpd(parent, 0x80, pp->serial, SERIAL_SIZE) >= 0)
1008                 condlog(3, "%s: serial = %s", pp->dev, pp->serial);
1009
1010         /*
1011          * set the hwe configlet pointer
1012          */
1013         pp->hwe = find_hwe(conf->hwtable, pp->vendor_id, pp->product_id, pp->rev);
1014
1015         /*
1016          * host / bus / target / lun
1017          */
1018         condlog(3, "%s: h:b:t:l = %i:%i:%i:%i",
1019                         pp->dev,
1020                         pp->sg_id.host_no,
1021                         pp->sg_id.channel,
1022                         pp->sg_id.scsi_id,
1023                         pp->sg_id.lun);
1024
1025         /*
1026          * target node name
1027          */
1028         if(sysfs_get_tgt_nodename(pp, pp->tgt_node_name))
1029                 return 1;
1030
1031         condlog(3, "%s: tgt_node_name = %s",
1032                 pp->dev, pp->tgt_node_name);
1033
1034         return 0;
1035 }
1036
1037 static int
1038 ccw_sysfs_pathinfo (struct path * pp)
1039 {
1040         struct udev_device *parent;
1041         char attr_buff[NAME_SIZE];
1042         const char *attr_path;
1043
1044         parent = pp->udev;
1045         while (parent) {
1046                 const char *subsys = udev_device_get_subsystem(parent);
1047                 if (subsys && !strncmp(subsys, "ccw", 3))
1048                         break;
1049                 parent = udev_device_get_parent(parent);
1050         }
1051         if (!parent)
1052                 return 1;
1053
1054         sprintf(pp->vendor_id, "IBM");
1055
1056         condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
1057
1058         if (sysfs_get_devtype(parent, attr_buff, FILE_NAME_SIZE) <= 0)
1059                 return 1;
1060
1061         if (!strncmp(attr_buff, "3370", 4)) {
1062                 sprintf(pp->product_id,"S/390 DASD FBA");
1063         } else if (!strncmp(attr_buff, "9336", 4)) {
1064                 sprintf(pp->product_id,"S/390 DASD FBA");
1065         } else {
1066                 sprintf(pp->product_id,"S/390 DASD ECKD");
1067         }
1068
1069         condlog(3, "%s: product = %s", pp->dev, pp->product_id);
1070
1071         /*
1072          * set the hwe configlet pointer
1073          */
1074         pp->hwe = find_hwe(conf->hwtable, pp->vendor_id, pp->product_id, NULL);
1075
1076         /*
1077          * host / bus / target / lun
1078          */
1079         attr_path = udev_device_get_sysname(parent);
1080         pp->sg_id.lun = 0;
1081         sscanf(attr_path, "%i.%i.%x",
1082                         &pp->sg_id.host_no,
1083                         &pp->sg_id.channel,
1084                         &pp->sg_id.scsi_id);
1085         condlog(3, "%s: h:b:t:l = %i:%i:%i:%i",
1086                         pp->dev,
1087                         pp->sg_id.host_no,
1088                         pp->sg_id.channel,
1089                         pp->sg_id.scsi_id,
1090                         pp->sg_id.lun);
1091
1092         return 0;
1093 }
1094
1095 static int
1096 cciss_sysfs_pathinfo (struct path * pp)
1097 {
1098         const char * attr_path = NULL;
1099         struct udev_device *parent;
1100
1101         parent = pp->udev;
1102         while (parent) {
1103                 const char *subsys = udev_device_get_subsystem(parent);
1104                 if (subsys && !strncmp(subsys, "cciss", 5)) {
1105                         attr_path = udev_device_get_sysname(parent);
1106                         if (!attr_path)
1107                                 break;
1108                         if (sscanf(attr_path, "c%id%i",
1109                                    &pp->sg_id.host_no,
1110                                    &pp->sg_id.scsi_id) == 2)
1111                                 break;
1112                 }
1113                 parent = udev_device_get_parent(parent);
1114         }
1115         if (!attr_path || pp->sg_id.host_no == -1)
1116                 return 1;
1117
1118         if (sysfs_get_vendor(parent, pp->vendor_id, SCSI_VENDOR_SIZE) <= 0)
1119                 return 1;
1120
1121         condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
1122
1123         if (sysfs_get_model(parent, pp->product_id, SCSI_PRODUCT_SIZE) <= 0)
1124                 return 1;
1125
1126         condlog(3, "%s: product = %s", pp->dev, pp->product_id);
1127
1128         if (sysfs_get_rev(parent, pp->rev, SCSI_REV_SIZE) <= 0)
1129                 return 1;
1130
1131         condlog(3, "%s: rev = %s", pp->dev, pp->rev);
1132
1133         /*
1134          * set the hwe configlet pointer
1135          */
1136         pp->hwe = find_hwe(conf->hwtable, pp->vendor_id, pp->product_id, pp->rev);
1137
1138         /*
1139          * host / bus / target / lun
1140          */
1141         pp->sg_id.lun = 0;
1142         pp->sg_id.channel = 0;
1143         condlog(3, "%s: h:b:t:l = %i:%i:%i:%i",
1144                 pp->dev,
1145                 pp->sg_id.host_no,
1146                 pp->sg_id.channel,
1147                 pp->sg_id.scsi_id,
1148                 pp->sg_id.lun);
1149         return 0;
1150 }
1151
1152 static int
1153 common_sysfs_pathinfo (struct path * pp)
1154 {
1155         dev_t devt;
1156
1157         if (!pp)
1158                 return 1;
1159
1160         if (!pp->udev) {
1161                 condlog(4, "%s: udev not initialised", pp->dev);
1162                 return 1;
1163         }
1164         devt = udev_device_get_devnum(pp->udev);
1165         snprintf(pp->dev_t, BLK_DEV_SIZE, "%d:%d", major(devt), minor(devt));
1166
1167         condlog(3, "%s: dev_t = %s", pp->dev, pp->dev_t);
1168
1169         if (sysfs_get_size(pp, &pp->size))
1170                 return 1;
1171
1172         condlog(3, "%s: size = %llu", pp->dev, pp->size);
1173
1174         return 0;
1175 }
1176
1177 int
1178 path_offline (struct path * pp)
1179 {
1180         struct udev_device * parent;
1181         char buff[SCSI_STATE_SIZE];
1182         int err;
1183
1184         if (pp->bus != SYSFS_BUS_SCSI)
1185                 return PATH_UP;
1186
1187         parent = pp->udev;
1188         while (parent) {
1189                 const char *subsys = udev_device_get_subsystem(parent);
1190                 if (subsys && !strncmp(subsys, "scsi", 4))
1191                         break;
1192                 parent = udev_device_get_parent(parent);
1193         }
1194
1195         if (!parent) {
1196                 condlog(1, "%s: failed to get sysfs information", pp->dev);
1197                 return PATH_REMOVED;
1198         }
1199
1200         memset(buff, 0x0, SCSI_STATE_SIZE);
1201         err = sysfs_attr_get_value(parent, "state", buff, SCSI_STATE_SIZE);
1202         if (err <= 0) {
1203                 if (err == -ENXIO)
1204                         return PATH_REMOVED;
1205                 else
1206                         return PATH_DOWN;
1207         }
1208
1209
1210         condlog(3, "%s: path state = %s", pp->dev, buff);
1211
1212         if (!strncmp(buff, "offline", 7)) {
1213                 pp->offline = 1;
1214                 return PATH_DOWN;
1215         }
1216         pp->offline = 0;
1217         if (!strncmp(buff, "blocked", 7) || !strncmp(buff, "quiesce", 7))
1218                 return PATH_PENDING;
1219         else if (!strncmp(buff, "running", 7))
1220                 return PATH_UP;
1221
1222         return PATH_DOWN;
1223 }
1224
1225 int
1226 sysfs_pathinfo(struct path * pp)
1227 {
1228         if (common_sysfs_pathinfo(pp))
1229                 return 1;
1230
1231         pp->bus = SYSFS_BUS_UNDEF;
1232         if (!strncmp(pp->dev,"cciss",5))
1233                 pp->bus = SYSFS_BUS_CCISS;
1234         if (!strncmp(pp->dev,"dasd", 4))
1235                 pp->bus = SYSFS_BUS_CCW;
1236         if (!strncmp(pp->dev,"sd", 2))
1237                 pp->bus = SYSFS_BUS_SCSI;
1238
1239         if (pp->bus == SYSFS_BUS_UNDEF)
1240                 return 0;
1241         else if (pp->bus == SYSFS_BUS_SCSI) {
1242                 if (scsi_sysfs_pathinfo(pp))
1243                         return 1;
1244         } else if (pp->bus == SYSFS_BUS_CCW) {
1245                 if (ccw_sysfs_pathinfo(pp))
1246                         return 1;
1247         } else if (pp->bus == SYSFS_BUS_CCISS) {
1248                 if (cciss_sysfs_pathinfo(pp))
1249                         return 1;
1250         }
1251         return 0;
1252 }
1253
1254 static int
1255 scsi_ioctl_pathinfo (struct path * pp, int mask)
1256 {
1257         if (mask & DI_SERIAL) {
1258                 if (strlen(pp->serial) == 0)
1259                         get_serial(pp->serial, SERIAL_SIZE, pp->fd);
1260                 condlog(3, "%s: serial = %s", pp->dev, pp->serial);
1261         }
1262
1263         return 0;
1264 }
1265
1266 static int
1267 cciss_ioctl_pathinfo (struct path * pp, int mask)
1268 {
1269         if (mask & DI_SERIAL) {
1270                 get_serial(pp->serial, SERIAL_SIZE, pp->fd);
1271                 condlog(3, "%s: serial = %s", pp->dev, pp->serial);
1272         }
1273         return 0;
1274 }
1275
1276 int
1277 get_state (struct path * pp, int daemon)
1278 {
1279         struct checker * c = &pp->checker;
1280         int state;
1281
1282         condlog(3, "%s: get_state", pp->dev);
1283
1284         if (!checker_selected(c)) {
1285                 if (daemon) {
1286                         if (pathinfo(pp, conf->hwtable, DI_SYSFS) != PATHINFO_OK) {
1287                                 condlog(3, "%s: couldn't get sysfs pathinfo",
1288                                         pp->dev);
1289                                 return PATH_UNCHECKED;
1290                         }
1291                 }
1292                 select_checker(pp);
1293                 if (!checker_selected(c)) {
1294                         condlog(3, "%s: No checker selected", pp->dev);
1295                         return PATH_UNCHECKED;
1296                 }
1297                 checker_set_fd(c, pp->fd);
1298                 if (checker_init(c, pp->mpp?&pp->mpp->mpcontext:NULL)) {
1299                         memset(c, 0x0, sizeof(struct checker));
1300                         condlog(3, "%s: checker init failed", pp->dev);
1301                         return PATH_UNCHECKED;
1302                 }
1303         }
1304         checker_clear_message(c);
1305         if (daemon) {
1306                 if (conf->force_sync == 0)
1307                         checker_set_async(c);
1308                 else
1309                         checker_set_sync(c);
1310         }
1311         if (!conf->checker_timeout &&
1312             sysfs_get_timeout(pp, &(c->timeout)) <= 0)
1313                 c->timeout = DEF_TIMEOUT;
1314         state = checker_check(c);
1315         condlog(3, "%s: state = %s", pp->dev, checker_state_name(state));
1316         if (state != PATH_UP && state != PATH_GHOST &&
1317             strlen(checker_message(c)))
1318                 condlog(3, "%s: checker msg is \"%s\"",
1319                         pp->dev, checker_message(c));
1320         return state;
1321 }
1322
1323 static int
1324 get_prio (struct path * pp)
1325 {
1326         if (!pp)
1327                 return 0;
1328
1329         struct prio * p = &pp->prio;
1330
1331         if (!prio_selected(p)) {
1332                 select_detect_prio(pp);
1333                 select_prio(pp);
1334                 if (!prio_selected(p)) {
1335                         condlog(3, "%s: no prio selected", pp->dev);
1336                         pp->priority = PRIO_UNDEF;
1337                         return 1;
1338                 }
1339         }
1340         pp->priority = prio_getprio(p, pp);
1341         if (pp->priority < 0) {
1342                 condlog(3, "%s: %s prio error", pp->dev, prio_name(p));
1343                 pp->priority = PRIO_UNDEF;
1344                 return 1;
1345         }
1346         condlog(3, "%s: %s prio = %u",
1347                 pp->dev, prio_name(p), pp->priority);
1348         return 0;
1349 }
1350
1351 static int
1352 get_udev_uid(struct path * pp, char *uid_attribute)
1353 {
1354         ssize_t len;
1355         const char *value;
1356
1357         value = udev_device_get_property_value(pp->udev,
1358                                                uid_attribute);
1359         if ((!value || strlen(value) == 0) && conf->cmd == CMD_VALID_PATH)
1360                 value = getenv(uid_attribute);
1361         if (value && strlen(value)) {
1362                 if (strlen(value) + 1 > WWID_SIZE) {
1363                         condlog(0, "%s: wwid overflow", pp->dev);
1364                         len = WWID_SIZE;
1365                 } else {
1366                         len = strlen(value);
1367                 }
1368                 strncpy(pp->wwid, value, len);
1369         } else {
1370                 condlog(3, "%s: no %s attribute", pp->dev,
1371                         uid_attribute);
1372                 len = -EINVAL;
1373         }
1374         return len;
1375 }
1376
1377 static int
1378 get_vpd_uid(struct path * pp)
1379 {
1380         struct udev_device *parent = pp->udev;
1381
1382         while (parent) {
1383                 const char *subsys = udev_device_get_subsystem(parent);
1384                 if (subsys && !strncmp(subsys, "scsi", 4))
1385                         break;
1386                 parent = udev_device_get_parent(parent);
1387         }
1388
1389         if (!parent) {
1390                 condlog(3, "%s: no scsi device found in sysfs", pp->dev);
1391                 return -ENXIO;
1392         }
1393         return get_vpd(parent, 0x83, pp->wwid, WWID_SIZE);
1394 }
1395
1396 static int
1397 get_uid (struct path * pp)
1398 {
1399         char *c;
1400         const char *origin = "none";
1401         ssize_t len = 0;
1402
1403         if (!pp->uid_attribute && !pp->getuid)
1404                 select_getuid(pp);
1405
1406         if (!pp->udev) {
1407                 condlog(1, "%s: no udev information", pp->dev);
1408                 return 1;
1409         }
1410
1411         memset(pp->wwid, 0, WWID_SIZE);
1412         if (pp->getuid) {
1413                 char buff[CALLOUT_MAX_SIZE];
1414
1415                 /* Use 'getuid' callout, deprecated */
1416                 condlog(1, "%s: using deprecated getuid callout", pp->dev);
1417                 if (apply_format(pp->getuid, &buff[0], pp)) {
1418                         condlog(0, "error formatting uid callout command");
1419                         memset(pp->wwid, 0, WWID_SIZE);
1420                         len = -EINVAL;
1421                 } else if (execute_program(buff, pp->wwid, WWID_SIZE)) {
1422                         condlog(3, "error calling out %s", buff);
1423                         memset(pp->wwid, 0, WWID_SIZE);
1424                         len = -EIO;
1425                 } else
1426                         len = strlen(pp->wwid);
1427                 origin = "callout";
1428         } else {
1429                 if (pp->uid_attribute) {
1430                         len = get_udev_uid(pp, pp->uid_attribute);
1431                         origin = "udev";
1432                 } else {
1433                         len = get_vpd_uid(pp);
1434                         if (len > 0)
1435                                 origin = "sysfs";
1436                         else {
1437                                 len = get_udev_uid(pp, DEFAULT_UID_ATTRIBUTE);
1438                                 origin = "udev";
1439                         }
1440                 }
1441         }
1442         if ( len < 0 ) {
1443                 condlog(1, "%s: failed to get uid: %s",
1444                         pp->dev, strerror(-len));
1445                 memset(pp->wwid, 0x0, WWID_SIZE);
1446         } else {
1447                 /* Strip any trailing blanks */
1448                 c = strchr(pp->wwid, '\0');
1449                 c--;
1450                 while (c && c >= pp->wwid && *c == ' ') {
1451                         *c = '\0';
1452                         c--;
1453                 }
1454         }
1455         condlog(3, "%s: uid = %s (%s)", pp->dev,
1456                 *pp->wwid == '\0' ? "<empty>" : pp->wwid, origin);
1457         return 0;
1458 }
1459
1460 extern int
1461 pathinfo (struct path *pp, vector hwtable, int mask)
1462 {
1463         int path_state;
1464
1465         if (!pp)
1466                 return PATHINFO_FAILED;
1467
1468         condlog(3, "%s: mask = 0x%x", pp->dev, mask);
1469
1470         /*
1471          * fetch info available in sysfs
1472          */
1473         if (mask & DI_SYSFS && sysfs_pathinfo(pp))
1474                 return PATHINFO_FAILED;
1475
1476         if (mask & DI_BLACKLIST && mask & DI_SYSFS) {
1477                 if (filter_device(conf->blist_device, conf->elist_device,
1478                                   pp->vendor_id, pp->product_id) > 0) {
1479                         return PATHINFO_SKIPPED;
1480                 }
1481         }
1482
1483         path_state = path_offline(pp);
1484         if (path_state == PATH_REMOVED)
1485                 goto blank;
1486
1487         /*
1488          * fetch info not available through sysfs
1489          */
1490         if (pp->fd < 0)
1491                 pp->fd = open(udev_device_get_devnode(pp->udev), O_RDONLY);
1492
1493         if (pp->fd < 0) {
1494                 condlog(4, "Couldn't open node for %s: %s",
1495                         pp->dev, strerror(errno));
1496                 goto blank;
1497         }
1498
1499         if (mask & DI_SERIAL)
1500                 get_geometry(pp);
1501
1502         if (path_state == PATH_UP && pp->bus == SYSFS_BUS_SCSI &&
1503             scsi_ioctl_pathinfo(pp, mask))
1504                 goto blank;
1505
1506         if (pp->bus == SYSFS_BUS_CCISS &&
1507             cciss_ioctl_pathinfo(pp, mask))
1508                 goto blank;
1509
1510         if (mask & DI_CHECKER) {
1511                 if (path_state == PATH_UP) {
1512                         pp->chkrstate = pp->state = get_state(pp, 0);
1513                         if (pp->state == PATH_UNCHECKED ||
1514                             pp->state == PATH_WILD)
1515                                 goto blank;
1516                         if (pp->state == PATH_TIMEOUT)
1517                                 pp->state = PATH_DOWN;
1518                         if (pp->state == PATH_UP && !pp->size) {
1519                                 condlog(3, "%s: device size is 0, "
1520                                         "path unuseable", pp->dev);
1521                                 pp->state = PATH_GHOST;
1522                         }
1523                 } else {
1524                         condlog(3, "%s: path inaccessible", pp->dev);
1525                         pp->chkrstate = pp->state = path_state;
1526                         if (path_state == PATH_PENDING ||
1527                             path_state == PATH_DOWN)
1528                                 pp->priority = 0;
1529                 }
1530         }
1531
1532         if ((mask & DI_WWID) && !strlen(pp->wwid))
1533                 get_uid(pp);
1534         if (mask & DI_BLACKLIST && mask & DI_WWID) {
1535                 if (!strlen(pp->wwid) ||
1536                     filter_wwid(conf->blist_wwid, conf->elist_wwid,
1537                                 pp->wwid, pp->dev) > 0) {
1538                         return PATHINFO_SKIPPED;
1539                 }
1540         }
1541
1542          /*
1543           * Retrieve path priority, even for PATH_DOWN paths if it has never
1544           * been successfully obtained before.
1545           */
1546         if ((mask & DI_PRIO) && path_state == PATH_UP) {
1547                 if (pp->state != PATH_DOWN || pp->priority == PRIO_UNDEF) {
1548                         if (!strlen(pp->wwid))
1549                                 get_uid(pp);
1550                         if (!strlen(pp->wwid))
1551                                 return PATHINFO_SKIPPED;
1552                         get_prio(pp);
1553                 }
1554         }
1555
1556         pp->initialized = 1;
1557         return PATHINFO_OK;
1558
1559 blank:
1560         /*
1561          * Recoverable error, for example faulty or offline path
1562          */
1563         memset(pp->wwid, 0, WWID_SIZE);
1564         pp->chkrstate = pp->state = PATH_DOWN;
1565         pp->initialized = 0;
1566
1567         return 0;
1568 }