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