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