b1db00fe7d79b2a0ab784e78bbc5d663b77b7421
[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 int
211 sysfs_get_timeout(struct path *pp, unsigned int *timeout)
212 {
213         const char *attr = NULL;
214         const char *subsys;
215         struct udev_device *parent;
216         int r;
217         unsigned int t;
218
219         if (!pp->udev || pp->bus != SYSFS_BUS_SCSI)
220                 return -ENOSYS;
221
222         parent = pp->udev;
223         while (parent) {
224                 subsys = udev_device_get_subsystem(parent);
225                 attr = udev_device_get_sysattr_value(parent, "timeout");
226                 if (subsys && attr)
227                         break;
228                 parent = udev_device_get_parent(parent);
229         }
230         if (!attr) {
231                 condlog(3, "%s: No timeout value in sysfs", pp->dev);
232                 return -ENXIO;
233         }
234
235         r = sscanf(attr, "%u\n", &t);
236
237         if (r != 1) {
238                 condlog(3, "%s: Cannot parse timeout attribute '%s'",
239                         pp->dev, attr);
240                 return -EINVAL;
241         }
242
243         *timeout = t;
244
245         return 0;
246 }
247
248 int
249 sysfs_get_tgt_nodename (struct path *pp, char * node)
250 {
251         const char *tgtname, *value;
252         struct udev_device *parent, *tgtdev;
253         int host, channel, tgtid = -1;
254
255         parent = udev_device_get_parent_with_subsystem_devtype(pp->udev, "scsi", "scsi_device");
256         if (!parent)
257                 return 1;
258         /* Check for SAS */
259         value = udev_device_get_sysattr_value(parent, "sas_address");
260         if (value) {
261                 tgtdev = udev_device_get_parent(parent);
262                 while (tgtdev) {
263                         tgtname = udev_device_get_sysname(tgtdev);
264                         if (sscanf(tgtname, "end_device-%d:%d",
265                                    &host, &tgtid) == 2)
266                                 break;
267                         tgtdev = udev_device_get_parent(tgtdev);
268                         tgtid = -1;
269                 }
270                 if (tgtid >= 0) {
271                         pp->sg_id.proto_id = SCSI_PROTOCOL_SAS;
272                         pp->sg_id.transport_id = tgtid;
273                         strncpy(node, value, NODE_NAME_SIZE);
274                         return 0;
275                 }
276         }
277
278         parent = udev_device_get_parent_with_subsystem_devtype(pp->udev, "scsi", "scsi_target");
279         if (!parent)
280                 return 1;
281         /* Check for FibreChannel */
282         tgtdev = udev_device_get_parent(parent);
283         value = udev_device_get_sysname(tgtdev);
284         if (sscanf(value, "rport-%d:%d-%d",
285                    &host, &channel, &tgtid) == 3) {
286                 tgtdev = udev_device_new_from_subsystem_sysname(conf->udev,
287                                 "fc_remote_ports", value);
288                 if (tgtdev) {
289                         condlog(3, "SCSI target %d:%d:%d -> "
290                                 "FC rport %d:%d-%d",
291                                 pp->sg_id.host_no, pp->sg_id.channel,
292                                 pp->sg_id.scsi_id, host, channel,
293                                 tgtid);
294                         value = udev_device_get_sysattr_value(tgtdev,
295                                                               "node_name");
296                         if (value) {
297                                 pp->sg_id.proto_id = SCSI_PROTOCOL_FCP;
298                                 pp->sg_id.transport_id = tgtid;
299                                 strncpy(node, value, NODE_NAME_SIZE);
300                                 udev_device_unref(tgtdev);
301                                 return 0;
302                         } else
303                                 udev_device_unref(tgtdev);
304                 }
305         }
306
307         /* Check for iSCSI */
308         parent = pp->udev;
309         tgtname = NULL;
310         while (parent) {
311                 tgtname = udev_device_get_sysname(parent);
312                 if (tgtname && sscanf(tgtname , "session%d", &tgtid) == 1)
313                         break;
314                 parent = udev_device_get_parent(parent);
315                 tgtname = NULL;
316                 tgtid = -1;
317         }
318         if (parent && tgtname) {
319                 tgtdev = udev_device_new_from_subsystem_sysname(conf->udev,
320                                 "iscsi_session", tgtname);
321                 if (tgtdev) {
322                         const char *value;
323
324                         value = udev_device_get_sysattr_value(tgtdev, "targetname");
325                         if (value) {
326                                 pp->sg_id.proto_id = SCSI_PROTOCOL_ISCSI;
327                                 pp->sg_id.transport_id = tgtid;
328                                 strncpy(node, value, NODE_NAME_SIZE);
329                                 udev_device_unref(tgtdev);
330                                 return 0;
331                         }
332                         else
333                                 udev_device_unref(tgtdev);
334                 }
335         }
336         /* Check for libata */
337         parent = pp->udev;
338         tgtname = NULL;
339         while (parent) {
340                 tgtname = udev_device_get_sysname(parent);
341                 if (tgtname && sscanf(tgtname, "ata%d", &tgtid) == 1)
342                         break;
343                 parent = udev_device_get_parent(parent);
344                 tgtname = NULL;
345         }
346         if (tgtname) {
347                 pp->sg_id.proto_id = SCSI_PROTOCOL_ATA;
348                 pp->sg_id.transport_id = tgtid;
349                 snprintf(node, NODE_NAME_SIZE, "ata-%d.00", tgtid);
350                 return 0;
351         }
352         pp->sg_id.proto_id = SCSI_PROTOCOL_UNSPEC;
353         return 1;
354 }
355
356 int sysfs_get_host_adapter_name(struct path *pp, char *adapter_name)
357 {
358         int proto_id;
359
360         if (!pp || !adapter_name)
361                 return 1;
362
363         proto_id = pp->sg_id.proto_id;
364
365         if (proto_id != SCSI_PROTOCOL_FCP &&
366             proto_id != SCSI_PROTOCOL_SAS &&
367             proto_id != SCSI_PROTOCOL_ISCSI &&
368             proto_id != SCSI_PROTOCOL_SRP) {
369                 return 1;
370         }
371         /* iscsi doesn't have adapter info in sysfs
372          * get ip_address for grouping paths
373          */
374         if (pp->sg_id.proto_id == SCSI_PROTOCOL_ISCSI)
375                 return sysfs_get_iscsi_ip_address(pp, adapter_name);
376
377         /* fetch adapter pci name for other protocols
378          */
379         return sysfs_get_host_pci_name(pp, adapter_name);
380 }
381
382 int sysfs_get_host_pci_name(struct path *pp, char *pci_name)
383 {
384         struct udev_device *hostdev, *parent;
385         char host_name[HOST_NAME_LEN];
386         const char *driver_name, *value;
387
388         if (!pp || !pci_name)
389                 return 1;
390
391         sprintf(host_name, "host%d", pp->sg_id.host_no);
392         hostdev = udev_device_new_from_subsystem_sysname(conf->udev,
393                         "scsi_host", host_name);
394         if (!hostdev)
395                 return 1;
396
397         parent = udev_device_get_parent(hostdev);
398         while (parent) {
399                 driver_name = udev_device_get_driver(parent);
400                 if (!driver_name) {
401                         parent = udev_device_get_parent(parent);
402                         continue;
403                 }
404                 if (!strcmp(driver_name, "pcieport"))
405                         break;
406                 parent = udev_device_get_parent(parent);
407         }
408         if (parent) {
409                 /* pci_device found
410                  */
411                 value = udev_device_get_sysname(parent);
412
413                 strncpy(pci_name, value, SLOT_NAME_SIZE);
414                 udev_device_unref(hostdev);
415                 return 0;
416         }
417         udev_device_unref(hostdev);
418         return 1;
419 }
420
421 int sysfs_get_iscsi_ip_address(struct path *pp, char *ip_address)
422 {
423         struct udev_device *hostdev;
424         char host_name[HOST_NAME_LEN];
425         const char *value;
426
427         sprintf(host_name, "host%d", pp->sg_id.host_no);
428         hostdev = udev_device_new_from_subsystem_sysname(conf->udev,
429                         "iscsi_host", host_name);
430         if (hostdev) {
431                 value = udev_device_get_sysattr_value(hostdev,
432                                 "ipaddress");
433                 if (value) {
434                         strncpy(ip_address, value, SLOT_NAME_SIZE);
435                         udev_device_unref(hostdev);
436                         return 0;
437                 } else
438                         udev_device_unref(hostdev);
439         }
440         return 1;
441 }
442
443 static void
444 sysfs_set_rport_tmo(struct multipath *mpp, struct path *pp)
445 {
446         struct udev_device *rport_dev = NULL;
447         char value[16];
448         char rport_id[32];
449         unsigned long long tmo = 0;
450         int ret;
451
452         sprintf(rport_id, "rport-%d:%d-%d",
453                 pp->sg_id.host_no, pp->sg_id.channel, pp->sg_id.transport_id);
454         rport_dev = udev_device_new_from_subsystem_sysname(conf->udev,
455                                 "fc_remote_ports", rport_id);
456         if (!rport_dev) {
457                 condlog(1, "%s: No fc_remote_port device for '%s'", pp->dev,
458                         rport_id);
459                 return;
460         }
461         condlog(4, "target%d:%d:%d -> %s", pp->sg_id.host_no,
462                 pp->sg_id.channel, pp->sg_id.scsi_id, rport_id);
463
464         /*
465          * This is tricky.
466          * dev_loss_tmo will be limited to 600 if fast_io_fail
467          * is _not_ set.
468          * fast_io_fail will be limited by the current dev_loss_tmo
469          * setting.
470          * So to get everything right we first need to increase
471          * dev_loss_tmo to the fast_io_fail setting (if present),
472          * then set fast_io_fail, and _then_ set dev_loss_tmo
473          * to the correct value.
474          */
475         memset(value, 0, 16);
476         if (mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET &&
477             mpp->fast_io_fail != MP_FAST_IO_FAIL_ZERO &&
478             mpp->fast_io_fail != MP_FAST_IO_FAIL_OFF) {
479                 /* Check if we need to temporarily increase dev_loss_tmo */
480                 ret = sysfs_attr_get_value(rport_dev, "dev_loss_tmo",
481                                            value, 16);
482                 if (ret <= 0) {
483                         condlog(0, "%s: failed to read dev_loss_tmo value, "
484                                 "error %d", rport_id, -ret);
485                         goto out;
486                 }
487                 if (sscanf(value, "%llu\n", &tmo) != 1) {
488                         condlog(0, "%s: Cannot parse dev_loss_tmo "
489                                 "attribute '%s'", rport_id, value);
490                         goto out;
491                 }
492                 if (mpp->fast_io_fail >= tmo) {
493                         snprintf(value, 16, "%u", mpp->fast_io_fail + 1);
494                 }
495         } else if (mpp->dev_loss > 600) {
496                 condlog(3, "%s: limiting dev_loss_tmo to 600, since "
497                         "fast_io_fail is not set", rport_id);
498                 snprintf(value, 16, "%u", 600);
499         } else {
500                 snprintf(value, 16, "%u", mpp->dev_loss);
501         }
502         if (strlen(value)) {
503                 ret = sysfs_attr_set_value(rport_dev, "dev_loss_tmo",
504                                            value, strlen(value));
505                 if (ret <= 0) {
506                         if (ret == -EBUSY)
507                                 condlog(3, "%s: rport blocked", rport_id);
508                         else
509                                 condlog(0, "%s: failed to set dev_loss_tmo to %s, error %d",
510                                         rport_id, value, -ret);
511                         goto out;
512                 }
513         }
514         if (mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET) {
515                 if (mpp->fast_io_fail == MP_FAST_IO_FAIL_OFF)
516                         sprintf(value, "off");
517                 else if (mpp->fast_io_fail == MP_FAST_IO_FAIL_ZERO)
518                         sprintf(value, "0");
519                 else
520                         snprintf(value, 16, "%u", mpp->fast_io_fail);
521                 ret = sysfs_attr_set_value(rport_dev, "fast_io_fail_tmo",
522                                            value, strlen(value));
523                 if (ret <= 0) {
524                         if (ret == -EBUSY)
525                                 condlog(3, "%s: rport blocked", rport_id);
526                         else
527                                 condlog(0, "%s: failed to set fast_io_fail_tmo to %s, error %d",
528                                         rport_id, value, -ret);
529                 }
530         }
531         if (tmo > 0) {
532                 snprintf(value, 16, "%u", mpp->dev_loss);
533                 ret = sysfs_attr_set_value(rport_dev, "dev_loss_tmo",
534                                            value, strlen(value));
535                 if (ret <= 0) {
536                         if (ret == -EBUSY)
537                                 condlog(3, "%s: rport blocked", rport_id);
538                         else
539                                 condlog(0, "%s: failed to set dev_loss_tmo to %s, error %d",
540                                         rport_id, value, -ret);
541                 }
542         }
543 out:
544         udev_device_unref(rport_dev);
545 }
546
547 static void
548 sysfs_set_session_tmo(struct multipath *mpp, struct path *pp)
549 {
550         struct udev_device *session_dev = NULL;
551         char session_id[64];
552         char value[11];
553
554         sprintf(session_id, "session%d", pp->sg_id.transport_id);
555         session_dev = udev_device_new_from_subsystem_sysname(conf->udev,
556                                 "iscsi_session", session_id);
557         if (!session_dev) {
558                 condlog(1, "%s: No iscsi session for '%s'", pp->dev,
559                         session_id);
560                 return;
561         }
562         condlog(4, "target%d:%d:%d -> %s", pp->sg_id.host_no,
563                 pp->sg_id.channel, pp->sg_id.scsi_id, session_id);
564
565         if (mpp->dev_loss) {
566                 condlog(3, "%s: ignoring dev_loss_tmo on iSCSI", pp->dev);
567         }
568         if (mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET) {
569                 if (mpp->fast_io_fail == MP_FAST_IO_FAIL_OFF) {
570                         condlog(3, "%s: can't switch off fast_io_fail_tmo "
571                                 "on iSCSI", pp->dev);
572                 } else if (mpp->fast_io_fail == MP_FAST_IO_FAIL_ZERO) {
573                         condlog(3, "%s: can't set fast_io_fail_tmo to '0'"
574                                 "on iSCSI", pp->dev);
575                 } else {
576                         snprintf(value, 11, "%u", mpp->fast_io_fail);
577                         if (sysfs_attr_set_value(session_dev, "recovery_tmo",
578                                                  value, 11) <= 0) {
579                                 condlog(3, "%s: Failed to set recovery_tmo, "
580                                         " error %d", pp->dev, errno);
581                         }
582                 }
583         }
584         udev_device_unref(session_dev);
585         return;
586 }
587
588 static void
589 sysfs_set_nexus_loss_tmo(struct multipath *mpp, struct path *pp)
590 {
591         struct udev_device *sas_dev = NULL;
592         char end_dev_id[64];
593         char value[11];
594
595         sprintf(end_dev_id, "end_device-%d:%d",
596                 pp->sg_id.host_no, pp->sg_id.transport_id);
597         sas_dev = udev_device_new_from_subsystem_sysname(conf->udev,
598                                 "sas_end_device", end_dev_id);
599         if (!sas_dev) {
600                 condlog(1, "%s: No SAS end device for '%s'", pp->dev,
601                         end_dev_id);
602                 return;
603         }
604         condlog(4, "target%d:%d:%d -> %s", pp->sg_id.host_no,
605                 pp->sg_id.channel, pp->sg_id.scsi_id, end_dev_id);
606
607         if (mpp->dev_loss) {
608                 snprintf(value, 11, "%u", mpp->dev_loss);
609                 if (sysfs_attr_set_value(sas_dev, "I_T_nexus_loss_timeout",
610                                          value, 11) <= 0)
611                         condlog(3, "%s: failed to update "
612                                 "I_T Nexus loss timeout, error %d",
613                                 pp->dev, errno);
614         }
615         udev_device_unref(sas_dev);
616         return;
617 }
618
619 int
620 sysfs_set_scsi_tmo (struct multipath *mpp)
621 {
622         struct path *pp;
623         int i;
624         int dev_loss_tmo = mpp->dev_loss;
625
626         if (mpp->no_path_retry > 0) {
627                 int no_path_retry_tmo = mpp->no_path_retry * conf->checkint;
628
629                 if (no_path_retry_tmo > MAX_DEV_LOSS_TMO)
630                         no_path_retry_tmo = MAX_DEV_LOSS_TMO;
631                 if (no_path_retry_tmo > dev_loss_tmo)
632                         dev_loss_tmo = no_path_retry_tmo;
633                 condlog(3, "%s: update dev_loss_tmo to %d",
634                         mpp->alias, dev_loss_tmo);
635         } else if (mpp->no_path_retry == NO_PATH_RETRY_QUEUE) {
636                 dev_loss_tmo = MAX_DEV_LOSS_TMO;
637                 condlog(3, "%s: update dev_loss_tmo to %d",
638                         mpp->alias, dev_loss_tmo);
639         }
640         mpp->dev_loss = dev_loss_tmo;
641         if (mpp->dev_loss && mpp->fast_io_fail >= (int)mpp->dev_loss) {
642                 condlog(3, "%s: turning off fast_io_fail (%d is not smaller than dev_loss_tmo)",
643                         mpp->alias, mpp->fast_io_fail);
644                 mpp->fast_io_fail = MP_FAST_IO_FAIL_OFF;
645         }
646         if (!mpp->dev_loss && mpp->fast_io_fail == MP_FAST_IO_FAIL_UNSET)
647                 return 0;
648
649         vector_foreach_slot(mpp->paths, pp, i) {
650                 if (pp->sg_id.proto_id == SCSI_PROTOCOL_FCP)
651                         sysfs_set_rport_tmo(mpp, pp);
652                 if (pp->sg_id.proto_id == SCSI_PROTOCOL_ISCSI)
653                         sysfs_set_session_tmo(mpp, pp);
654                 if (pp->sg_id.proto_id == SCSI_PROTOCOL_SAS)
655                         sysfs_set_nexus_loss_tmo(mpp, pp);
656         }
657         return 0;
658 }
659
660 int
661 do_inq(int sg_fd, int cmddt, int evpd, unsigned int pg_op,
662        void *resp, int mx_resp_len)
663 {
664         unsigned char inqCmdBlk[INQUIRY_CMDLEN] =
665                 { INQUIRY_CMD, 0, 0, 0, 0, 0 };
666         unsigned char sense_b[SENSE_BUFF_LEN];
667         struct sg_io_hdr io_hdr;
668
669         if (cmddt)
670                 inqCmdBlk[1] |= 2;
671         if (evpd)
672                 inqCmdBlk[1] |= 1;
673         inqCmdBlk[2] = (unsigned char) pg_op;
674         inqCmdBlk[3] = (unsigned char)((mx_resp_len >> 8) & 0xff);
675         inqCmdBlk[4] = (unsigned char) (mx_resp_len & 0xff);
676         memset(&io_hdr, 0, sizeof (struct sg_io_hdr));
677         memset(sense_b, 0, SENSE_BUFF_LEN);
678         io_hdr.interface_id = 'S';
679         io_hdr.cmd_len = sizeof (inqCmdBlk);
680         io_hdr.mx_sb_len = sizeof (sense_b);
681         io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
682         io_hdr.dxfer_len = mx_resp_len;
683         io_hdr.dxferp = resp;
684         io_hdr.cmdp = inqCmdBlk;
685         io_hdr.sbp = sense_b;
686         io_hdr.timeout = DEF_TIMEOUT;
687
688         if (ioctl(sg_fd, SG_IO, &io_hdr) < 0)
689                 return -1;
690
691         /* treat SG_ERR here to get rid of sg_err.[ch] */
692         io_hdr.status &= 0x7e;
693         if ((0 == io_hdr.status) && (0 == io_hdr.host_status) &&
694             (0 == io_hdr.driver_status))
695                 return 0;
696         if ((SCSI_CHECK_CONDITION == io_hdr.status) ||
697             (SCSI_COMMAND_TERMINATED == io_hdr.status) ||
698             (SG_ERR_DRIVER_SENSE == (0xf & io_hdr.driver_status))) {
699                 if (io_hdr.sbp && (io_hdr.sb_len_wr > 2)) {
700                         int sense_key;
701                         unsigned char * sense_buffer = io_hdr.sbp;
702                         if (sense_buffer[0] & 0x2)
703                                 sense_key = sense_buffer[1] & 0xf;
704                         else
705                                 sense_key = sense_buffer[2] & 0xf;
706                         if(RECOVERED_ERROR == sense_key)
707                                 return 0;
708                 }
709         }
710         return -1;
711 }
712
713 static int
714 get_serial (char * str, int maxlen, int fd)
715 {
716         int len = 0;
717         char buff[MX_ALLOC_LEN + 1] = {0};
718
719         if (fd < 0)
720                 return 1;
721
722         if (0 == do_inq(fd, 0, 1, 0x80, buff, MX_ALLOC_LEN)) {
723                 len = buff[3];
724                 if (len >= maxlen)
725                         return 1;
726                 if (len > 0) {
727                         memcpy(str, buff + 4, len);
728                         str[len] = '\0';
729                 }
730                 return 0;
731         }
732         return 1;
733 }
734
735 static int
736 get_geometry(struct path *pp)
737 {
738         if (pp->fd < 0)
739                 return 1;
740
741         if (ioctl(pp->fd, HDIO_GETGEO, &pp->geom)) {
742                 condlog(2, "%s: HDIO_GETGEO failed with %d", pp->dev, errno);
743                 memset(&pp->geom, 0, sizeof(pp->geom));
744                 return 1;
745         }
746         condlog(3, "%s: %u cyl, %u heads, %u sectors/track, start at %lu",
747                 pp->dev, pp->geom.cylinders, pp->geom.heads,
748                 pp->geom.sectors, pp->geom.start);
749         return 0;
750 }
751
752 static int
753 scsi_sysfs_pathinfo (struct path * pp)
754 {
755         struct udev_device *parent;
756         const char *attr_path = NULL;
757
758         parent = pp->udev;
759         while (parent) {
760                 const char *subsys = udev_device_get_subsystem(parent);
761                 if (subsys && !strncmp(subsys, "scsi", 4)) {
762                         attr_path = udev_device_get_sysname(parent);
763                         if (!attr_path)
764                                 break;
765                         if (sscanf(attr_path, "%i:%i:%i:%i",
766                                    &pp->sg_id.host_no,
767                                    &pp->sg_id.channel,
768                                    &pp->sg_id.scsi_id,
769                                    &pp->sg_id.lun) == 4)
770                                 break;
771                 }
772                 parent = udev_device_get_parent(parent);
773         }
774         if (!attr_path || pp->sg_id.host_no == -1)
775                 return 1;
776
777         if (sysfs_get_vendor(parent, pp->vendor_id, SCSI_VENDOR_SIZE) <= 0)
778                 return 1;
779
780         condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
781
782         if (sysfs_get_model(parent, pp->product_id, SCSI_PRODUCT_SIZE) <= 0)
783                 return 1;
784
785         condlog(3, "%s: product = %s", pp->dev, pp->product_id);
786
787         if (sysfs_get_rev(parent, pp->rev, SCSI_REV_SIZE) <= 0)
788                 return 1;
789
790         condlog(3, "%s: rev = %s", pp->dev, pp->rev);
791
792         /*
793          * set the hwe configlet pointer
794          */
795         pp->hwe = find_hwe(conf->hwtable, pp->vendor_id, pp->product_id, pp->rev);
796
797         /*
798          * host / bus / target / lun
799          */
800         condlog(3, "%s: h:b:t:l = %i:%i:%i:%i",
801                         pp->dev,
802                         pp->sg_id.host_no,
803                         pp->sg_id.channel,
804                         pp->sg_id.scsi_id,
805                         pp->sg_id.lun);
806
807         /*
808          * target node name
809          */
810         if(!sysfs_get_tgt_nodename(pp, pp->tgt_node_name)) {
811                 condlog(3, "%s: tgt_node_name = %s",
812                         pp->dev, pp->tgt_node_name);
813         }
814
815         return 0;
816 }
817
818 static int
819 ccw_sysfs_pathinfo (struct path * pp)
820 {
821         struct udev_device *parent;
822         char attr_buff[NAME_SIZE];
823         const char *attr_path;
824
825         parent = pp->udev;
826         while (parent) {
827                 const char *subsys = udev_device_get_subsystem(parent);
828                 if (subsys && !strncmp(subsys, "ccw", 3))
829                         break;
830                 parent = udev_device_get_parent(parent);
831         }
832         if (!parent)
833                 return 1;
834
835         sprintf(pp->vendor_id, "IBM");
836
837         condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
838
839         if (sysfs_get_devtype(parent, attr_buff, FILE_NAME_SIZE) <= 0)
840                 return 1;
841
842         if (!strncmp(attr_buff, "3370", 4)) {
843                 sprintf(pp->product_id,"S/390 DASD FBA");
844         } else if (!strncmp(attr_buff, "9336", 4)) {
845                 sprintf(pp->product_id,"S/390 DASD FBA");
846         } else {
847                 sprintf(pp->product_id,"S/390 DASD ECKD");
848         }
849
850         condlog(3, "%s: product = %s", pp->dev, pp->product_id);
851
852         /*
853          * set the hwe configlet pointer
854          */
855         pp->hwe = find_hwe(conf->hwtable, pp->vendor_id, pp->product_id, NULL);
856
857         /*
858          * host / bus / target / lun
859          */
860         attr_path = udev_device_get_sysname(parent);
861         pp->sg_id.lun = 0;
862         sscanf(attr_path, "%i.%i.%x",
863                         &pp->sg_id.host_no,
864                         &pp->sg_id.channel,
865                         &pp->sg_id.scsi_id);
866         condlog(3, "%s: h:b:t:l = %i:%i:%i:%i",
867                         pp->dev,
868                         pp->sg_id.host_no,
869                         pp->sg_id.channel,
870                         pp->sg_id.scsi_id,
871                         pp->sg_id.lun);
872
873         return 0;
874 }
875
876 static int
877 cciss_sysfs_pathinfo (struct path * pp)
878 {
879         const char * attr_path = NULL;
880         struct udev_device *parent;
881
882         parent = pp->udev;
883         while (parent) {
884                 const char *subsys = udev_device_get_subsystem(parent);
885                 if (subsys && !strncmp(subsys, "cciss", 5)) {
886                         attr_path = udev_device_get_sysname(parent);
887                         if (!attr_path)
888                                 break;
889                         if (sscanf(attr_path, "c%id%i",
890                                    &pp->sg_id.host_no,
891                                    &pp->sg_id.scsi_id) == 2)
892                                 break;
893                 }
894                 parent = udev_device_get_parent(parent);
895         }
896         if (!attr_path || pp->sg_id.host_no == -1)
897                 return 1;
898
899         if (sysfs_get_vendor(parent, pp->vendor_id, SCSI_VENDOR_SIZE) <= 0)
900                 return 1;
901
902         condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
903
904         if (sysfs_get_model(parent, pp->product_id, SCSI_PRODUCT_SIZE) <= 0)
905                 return 1;
906
907         condlog(3, "%s: product = %s", pp->dev, pp->product_id);
908
909         if (sysfs_get_rev(parent, pp->rev, SCSI_REV_SIZE) <= 0)
910                 return 1;
911
912         condlog(3, "%s: rev = %s", pp->dev, pp->rev);
913
914         /*
915          * set the hwe configlet pointer
916          */
917         pp->hwe = find_hwe(conf->hwtable, pp->vendor_id, pp->product_id, pp->rev);
918
919         /*
920          * host / bus / target / lun
921          */
922         pp->sg_id.lun = 0;
923         pp->sg_id.channel = 0;
924         condlog(3, "%s: h:b:t:l = %i:%i:%i:%i",
925                 pp->dev,
926                 pp->sg_id.host_no,
927                 pp->sg_id.channel,
928                 pp->sg_id.scsi_id,
929                 pp->sg_id.lun);
930         return 0;
931 }
932
933 static int
934 common_sysfs_pathinfo (struct path * pp)
935 {
936         dev_t devt;
937
938         if (!pp)
939                 return 1;
940
941         if (!pp->udev) {
942                 condlog(4, "%s: udev not initialised", pp->dev);
943                 return 1;
944         }
945         devt = udev_device_get_devnum(pp->udev);
946         snprintf(pp->dev_t, BLK_DEV_SIZE, "%d:%d", major(devt), minor(devt));
947
948         condlog(3, "%s: dev_t = %s", pp->dev, pp->dev_t);
949
950         if (sysfs_get_size(pp, &pp->size))
951                 return 1;
952
953         condlog(3, "%s: size = %llu", pp->dev, pp->size);
954
955         return 0;
956 }
957
958 int
959 path_offline (struct path * pp)
960 {
961         struct udev_device * parent;
962         char buff[SCSI_STATE_SIZE];
963         int err;
964
965         if (pp->bus != SYSFS_BUS_SCSI)
966                 return PATH_UP;
967
968         parent = pp->udev;
969         while (parent) {
970                 const char *subsys = udev_device_get_subsystem(parent);
971                 if (subsys && !strncmp(subsys, "scsi", 4))
972                         break;
973                 parent = udev_device_get_parent(parent);
974         }
975
976         if (!parent) {
977                 condlog(1, "%s: failed to get sysfs information", pp->dev);
978                 return PATH_REMOVED;
979         }
980
981         memset(buff, 0x0, SCSI_STATE_SIZE);
982         err = sysfs_attr_get_value(parent, "state", buff, SCSI_STATE_SIZE);
983         if (err <= 0) {
984                 if (err == -ENXIO)
985                         return PATH_REMOVED;
986                 else
987                         return PATH_DOWN;
988         }
989
990
991         condlog(3, "%s: path state = %s", pp->dev, buff);
992
993         if (!strncmp(buff, "offline", 7)) {
994                 pp->offline = 1;
995                 return PATH_DOWN;
996         }
997         pp->offline = 0;
998         if (!strncmp(buff, "blocked", 7) || !strncmp(buff, "quiesce", 7))
999                 return PATH_PENDING;
1000         else if (!strncmp(buff, "running", 7))
1001                 return PATH_UP;
1002
1003         return PATH_DOWN;
1004 }
1005
1006 int
1007 sysfs_pathinfo(struct path * pp)
1008 {
1009         if (common_sysfs_pathinfo(pp))
1010                 return 1;
1011
1012         pp->bus = SYSFS_BUS_UNDEF;
1013         if (!strncmp(pp->dev,"cciss",5))
1014                 pp->bus = SYSFS_BUS_CCISS;
1015         if (!strncmp(pp->dev,"dasd", 4))
1016                 pp->bus = SYSFS_BUS_CCW;
1017         if (!strncmp(pp->dev,"sd", 2))
1018                 pp->bus = SYSFS_BUS_SCSI;
1019
1020         if (pp->bus == SYSFS_BUS_UNDEF)
1021                 return 0;
1022         else if (pp->bus == SYSFS_BUS_SCSI) {
1023                 if (scsi_sysfs_pathinfo(pp))
1024                         return 1;
1025         } else if (pp->bus == SYSFS_BUS_CCW) {
1026                 if (ccw_sysfs_pathinfo(pp))
1027                         return 1;
1028         } else if (pp->bus == SYSFS_BUS_CCISS) {
1029                 if (cciss_sysfs_pathinfo(pp))
1030                         return 1;
1031         }
1032         return 0;
1033 }
1034
1035 static int
1036 scsi_ioctl_pathinfo (struct path * pp, int mask)
1037 {
1038         if (mask & DI_SERIAL) {
1039                 get_serial(pp->serial, SERIAL_SIZE, pp->fd);
1040                 condlog(3, "%s: serial = %s", pp->dev, pp->serial);
1041         }
1042
1043         return 0;
1044 }
1045
1046 static int
1047 cciss_ioctl_pathinfo (struct path * pp, int mask)
1048 {
1049         if (mask & DI_SERIAL) {
1050                 get_serial(pp->serial, SERIAL_SIZE, pp->fd);
1051                 condlog(3, "%s: serial = %s", pp->dev, pp->serial);
1052         }
1053         return 0;
1054 }
1055
1056 int
1057 get_state (struct path * pp, int daemon)
1058 {
1059         struct checker * c = &pp->checker;
1060         int state;
1061
1062         condlog(3, "%s: get_state", pp->dev);
1063
1064         if (!checker_selected(c)) {
1065                 if (daemon) {
1066                         if (pathinfo(pp, conf->hwtable, DI_SYSFS) != PATHINFO_OK) {
1067                                 condlog(3, "%s: couldn't get sysfs pathinfo",
1068                                         pp->dev);
1069                                 return PATH_UNCHECKED;
1070                         }
1071                 }
1072                 select_checker(pp);
1073                 if (!checker_selected(c)) {
1074                         condlog(3, "%s: No checker selected", pp->dev);
1075                         return PATH_UNCHECKED;
1076                 }
1077                 checker_set_fd(c, pp->fd);
1078                 if (checker_init(c, pp->mpp?&pp->mpp->mpcontext:NULL)) {
1079                         memset(c, 0x0, sizeof(struct checker));
1080                         condlog(3, "%s: checker init failed", pp->dev);
1081                         return PATH_UNCHECKED;
1082                 }
1083         }
1084         checker_clear_message(c);
1085         if (daemon) {
1086                 if (conf->force_sync == 0)
1087                         checker_set_async(c);
1088                 else
1089                         checker_set_sync(c);
1090         }
1091         if (!conf->checker_timeout &&
1092             sysfs_get_timeout(pp, &(c->timeout)) <= 0)
1093                 c->timeout = DEF_TIMEOUT;
1094         state = checker_check(c);
1095         condlog(3, "%s: state = %s", pp->dev, checker_state_name(state));
1096         if (state != PATH_UP && state != PATH_GHOST &&
1097             strlen(checker_message(c)))
1098                 condlog(3, "%s: checker msg is \"%s\"",
1099                         pp->dev, checker_message(c));
1100         return state;
1101 }
1102
1103 static int
1104 get_prio (struct path * pp)
1105 {
1106         if (!pp)
1107                 return 0;
1108
1109         struct prio * p = &pp->prio;
1110
1111         if (!prio_selected(p)) {
1112                 select_detect_prio(pp);
1113                 select_prio(pp);
1114                 if (!prio_selected(p)) {
1115                         condlog(3, "%s: no prio selected", pp->dev);
1116                         pp->priority = PRIO_UNDEF;
1117                         return 1;
1118                 }
1119         }
1120         pp->priority = prio_getprio(p, pp);
1121         if (pp->priority < 0) {
1122                 condlog(3, "%s: %s prio error", pp->dev, prio_name(p));
1123                 pp->priority = PRIO_UNDEF;
1124                 return 1;
1125         }
1126         condlog(3, "%s: %s prio = %u",
1127                 pp->dev, prio_name(p), pp->priority);
1128         return 0;
1129 }
1130
1131 static int
1132 get_uid (struct path * pp)
1133 {
1134         char *c;
1135         const char *origin;
1136
1137         if (!pp->uid_attribute && !pp->getuid)
1138                 select_getuid(pp);
1139
1140         if (!pp->udev) {
1141                 condlog(1, "%s: no udev information", pp->dev);
1142                 return 1;
1143         }
1144
1145         memset(pp->wwid, 0, WWID_SIZE);
1146         if (pp->getuid) {
1147                 char buff[CALLOUT_MAX_SIZE];
1148
1149                 /* Use 'getuid' callout, deprecated */
1150                 condlog(1, "%s: using deprecated getuid callout", pp->dev);
1151                 if (apply_format(pp->getuid, &buff[0], pp)) {
1152                         condlog(0, "error formatting uid callout command");
1153                         memset(pp->wwid, 0, WWID_SIZE);
1154                 } else if (execute_program(buff, pp->wwid, WWID_SIZE)) {
1155                         condlog(3, "error calling out %s", buff);
1156                         memset(pp->wwid, 0, WWID_SIZE);
1157                 }
1158                 origin = "callout";
1159         } else {
1160                 const char *value;
1161
1162                 value = udev_device_get_property_value(pp->udev,
1163                                                        pp->uid_attribute);
1164                 if ((!value || strlen(value) == 0) &&
1165                     conf->cmd == CMD_VALID_PATH)
1166                         value = getenv(pp->uid_attribute);
1167                 if (value && strlen(value)) {
1168                         size_t len = WWID_SIZE;
1169
1170                         if (strlen(value) + 1 > WWID_SIZE) {
1171                                 condlog(0, "%s: wwid overflow", pp->dev);
1172                         } else {
1173                                 len = strlen(value);
1174                         }
1175                         strncpy(pp->wwid, value, len);
1176                 } else {
1177                         condlog(3, "%s: no %s attribute", pp->dev,
1178                                 pp->uid_attribute);
1179                 }
1180                 origin = "udev";
1181         }
1182         /* Strip any trailing blanks */
1183         c = strchr(pp->wwid, '\0');
1184         c--;
1185         while (c && c >= pp->wwid && *c == ' ') {
1186                 *c = '\0';
1187                 c--;
1188         }
1189         condlog(3, "%s: uid = %s (%s)", pp->dev,
1190                 *pp->wwid == '\0' ? "<empty>" : pp->wwid, origin);
1191         return 0;
1192 }
1193
1194 extern int
1195 pathinfo (struct path *pp, vector hwtable, int mask)
1196 {
1197         int path_state;
1198
1199         if (!pp)
1200                 return PATHINFO_FAILED;
1201
1202         condlog(3, "%s: mask = 0x%x", pp->dev, mask);
1203
1204         /*
1205          * fetch info available in sysfs
1206          */
1207         if (mask & DI_SYSFS && sysfs_pathinfo(pp))
1208                 return PATHINFO_FAILED;
1209
1210         if (mask & DI_BLACKLIST && mask & DI_SYSFS) {
1211                 if (filter_device(conf->blist_device, conf->elist_device,
1212                                   pp->vendor_id, pp->product_id) > 0) {
1213                         return PATHINFO_SKIPPED;
1214                 }
1215         }
1216
1217         path_state = path_offline(pp);
1218         if (path_state == PATH_REMOVED)
1219                 goto blank;
1220
1221         /*
1222          * fetch info not available through sysfs
1223          */
1224         if (pp->fd < 0)
1225                 pp->fd = open(udev_device_get_devnode(pp->udev), O_RDONLY);
1226
1227         if (pp->fd < 0) {
1228                 condlog(4, "Couldn't open node for %s: %s",
1229                         pp->dev, strerror(errno));
1230                 goto blank;
1231         }
1232
1233         if (mask & DI_SERIAL)
1234                 get_geometry(pp);
1235
1236         if (path_state == PATH_UP && pp->bus == SYSFS_BUS_SCSI &&
1237             scsi_ioctl_pathinfo(pp, mask))
1238                 goto blank;
1239
1240         if (pp->bus == SYSFS_BUS_CCISS &&
1241             cciss_ioctl_pathinfo(pp, mask))
1242                 goto blank;
1243
1244         if (mask & DI_CHECKER) {
1245                 if (path_state == PATH_UP) {
1246                         pp->chkrstate = pp->state = get_state(pp, 0);
1247                         if (pp->state == PATH_UNCHECKED ||
1248                             pp->state == PATH_WILD)
1249                                 goto blank;
1250                         if (pp->state == PATH_TIMEOUT)
1251                                 pp->state = PATH_DOWN;
1252                         if (pp->state == PATH_UP && !pp->size) {
1253                                 condlog(3, "%s: device size is 0, "
1254                                         "path unuseable", pp->dev);
1255                                 pp->state = PATH_GHOST;
1256                         }
1257                 } else {
1258                         condlog(3, "%s: path inaccessible", pp->dev);
1259                         pp->chkrstate = pp->state = path_state;
1260                         if (path_state == PATH_PENDING ||
1261                             path_state == PATH_DOWN)
1262                                 pp->priority = 0;
1263                 }
1264         }
1265
1266         if ((mask & DI_WWID) && !strlen(pp->wwid))
1267                 get_uid(pp);
1268         if (mask & DI_BLACKLIST && mask & DI_WWID) {
1269                 if (!strlen(pp->wwid) ||
1270                     filter_wwid(conf->blist_wwid, conf->elist_wwid,
1271                                 pp->wwid, pp->dev) > 0) {
1272                         return PATHINFO_SKIPPED;
1273                 }
1274         }
1275
1276          /*
1277           * Retrieve path priority, even for PATH_DOWN paths if it has never
1278           * been successfully obtained before.
1279           */
1280         if ((mask & DI_PRIO) && path_state == PATH_UP) {
1281                 if (pp->state != PATH_DOWN || pp->priority == PRIO_UNDEF) {
1282                         if (!strlen(pp->wwid))
1283                                 get_uid(pp);
1284                         if (!strlen(pp->wwid))
1285                                 return PATHINFO_SKIPPED;
1286                         get_prio(pp);
1287                 }
1288         }
1289
1290         pp->initialized = 1;
1291         return PATHINFO_OK;
1292
1293 blank:
1294         /*
1295          * Recoverable error, for example faulty or offline path
1296          */
1297         memset(pp->wwid, 0, WWID_SIZE);
1298         pp->chkrstate = pp->state = PATH_DOWN;
1299         pp->initialized = 0;
1300
1301         return 0;
1302 }