Discover target ids for ATA
[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 <unistd.h>
8 #include <fcntl.h>
9 #include <sys/ioctl.h>
10 #include <sys/stat.h>
11 #include <dirent.h>
12 #include <errno.h>
13 #include <libgen.h>
14 #include <libudev.h>
15
16 #include "checkers.h"
17 #include "vector.h"
18 #include "memory.h"
19 #include "util.h"
20 #include "structs.h"
21 #include "config.h"
22 #include "blacklist.h"
23 #include "debug.h"
24 #include "propsel.h"
25 #include "sg_include.h"
26 #include "sysfs.h"
27 #include "discovery.h"
28 #include "prio.h"
29 #include "defaults.h"
30
31 int
32 store_pathinfo (vector pathvec, vector hwtable, struct udev_device *udevice,
33                 int flag, struct path **pp_ptr)
34 {
35         int err = 1;
36         struct path * pp;
37         const char * devname;
38
39         if (pp_ptr)
40                 *pp_ptr = NULL;
41
42         devname = udev_device_get_sysname(udevice);
43         if (!devname)
44                 return 1;
45
46         pp = alloc_path();
47
48         if (!pp)
49                 return 1;
50
51         if(safe_sprintf(pp->dev, "%s", devname)) {
52                 condlog(0, "pp->dev too small");
53                 goto out;
54         }
55         pp->udev = udev_device_ref(udevice);
56         err = pathinfo(pp, hwtable, flag | DI_BLACKLIST);
57         if (err)
58                 goto out;
59
60         err = store_path(pathvec, pp);
61         if (err)
62                 goto out;
63
64 out:
65         if (err)
66                 free_path(pp);
67         else if (pp_ptr)
68                 *pp_ptr = pp;
69         return err;
70 }
71
72 static int
73 path_discover (vector pathvec, struct config * conf,
74                struct udev_device *udevice, int flag)
75 {
76         struct path * pp;
77         const char * devname;
78
79         devname = udev_device_get_sysname(udevice);
80         if (!devname)
81                 return 0;
82
83         if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
84                            (char *)devname) > 0)
85                 return 0;
86
87         pp = find_path_by_dev(pathvec, (char *)devname);
88         if (!pp) {
89                 if (store_pathinfo(pathvec, conf->hwtable,
90                                    udevice, flag, NULL) != 1)
91                         return 0;
92                 else
93                         return 1;
94         }
95         return pathinfo(pp, conf->hwtable, flag);
96 }
97
98 int
99 path_discovery (vector pathvec, struct config * conf, int flag)
100 {
101         struct udev_enumerate *udev_iter;
102         struct udev_list_entry *entry;
103         struct udev_device *udevice;
104         const char *devpath;
105         int r = 0;
106
107         udev_iter = udev_enumerate_new(conf->udev);
108         if (!udev_iter)
109                 return 1;
110
111         udev_enumerate_add_match_subsystem(udev_iter, "block");
112         udev_enumerate_scan_devices(udev_iter);
113
114         udev_list_entry_foreach(entry,
115                                 udev_enumerate_get_list_entry(udev_iter)) {
116                 const char *devtype;
117                 devpath = udev_list_entry_get_name(entry);
118                 condlog(4, "Discover device %s", devpath);
119                 udevice = udev_device_new_from_syspath(conf->udev, devpath);
120                 if (!udevice) {
121                         condlog(4, "%s: no udev information", devpath);
122                         r++;
123                         continue;
124                 }
125                 devtype = udev_device_get_devtype(udevice);
126                 if(devtype && !strncmp(devtype, "disk", 4))
127                         r += path_discover(pathvec, conf, udevice, flag);
128                 udev_device_unref(udevice);
129         }
130         udev_enumerate_unref(udev_iter);
131         condlog(4, "Discovery status %d", r);
132         return r;
133 }
134
135 #define declare_sysfs_get_str(fname)                                    \
136 extern int                                                              \
137 sysfs_get_##fname (struct udev_device * udev, char * buff, size_t len)  \
138 {                                                                       \
139         const char * attr;                                              \
140         const char * devname;                                           \
141                                                                         \
142         devname = udev_device_get_sysname(udev);                        \
143                                                                         \
144         attr = udev_device_get_sysattr_value(udev, #fname);             \
145         if (!attr) {                                                    \
146                 condlog(3, "%s: attribute %s not found in sysfs",       \
147                         devname, #fname);                               \
148                 return 1;                                               \
149         }                                                               \
150         if (strlen(attr) > len) {                                       \
151                 condlog(3, "%s: overflow in attribute %s",              \
152                         devname, #fname);                               \
153                 return 2;                                               \
154         }                                                               \
155         strlcpy(buff, attr, len);                                       \
156         return 0;                                                       \
157 }
158
159 declare_sysfs_get_str(devtype);
160 declare_sysfs_get_str(cutype);
161 declare_sysfs_get_str(vendor);
162 declare_sysfs_get_str(model);
163 declare_sysfs_get_str(rev);
164 declare_sysfs_get_str(state);
165 declare_sysfs_get_str(dev);
166
167 int
168 sysfs_get_timeout(struct path *pp, unsigned int *timeout)
169 {
170         const char *attr = NULL;
171         const char *subsys;
172         struct udev_device *parent;
173         int r;
174         unsigned int t;
175
176         if (!pp->udev || pp->bus != SYSFS_BUS_SCSI)
177                 return 1;
178
179         parent = pp->udev;
180         while (parent) {
181                 subsys = udev_device_get_subsystem(parent);
182                 attr = udev_device_get_sysattr_value(parent, "timeout");
183                 if (subsys && attr)
184                         break;
185                 parent = udev_device_get_parent(parent);
186         }
187         if (!attr) {
188                 condlog(3, "%s: No timeout value in sysfs", pp->dev);
189                 return 1;
190         }
191
192         r = sscanf(attr, "%u\n", &t);
193
194         if (r != 1) {
195                 condlog(3, "%s: Cannot parse timeout attribute '%s'",
196                         pp->dev, attr);
197                 return 1;
198         }
199
200         *timeout = t * 1000;
201
202         return 0;
203 }
204
205 int
206 sysfs_get_tgt_nodename (struct path *pp, char * node)
207 {
208         const char *targetid, *value;
209         struct udev_device *parent, *tgtdev;
210         int host, channel, rport_id = -1, ata_id = -1;
211
212         parent = udev_device_get_parent_with_subsystem_devtype(pp->udev, "scsi", "scsi_device");
213         if (!parent)
214                 return 1;
215         /* Check for SAS */
216         value = udev_device_get_sysattr_value(parent, "sas_address");
217         if (value) {
218                 pp->sg_id.proto_id = SCSI_PROTOCOL_SAS;
219                 strncpy(node, value, NODE_NAME_SIZE);
220                 return 0;
221         }
222
223         parent = udev_device_get_parent_with_subsystem_devtype(pp->udev, "scsi", "scsi_target");
224         if (!parent)
225                 return 1;
226         /* Check for FibreChannel */
227         tgtdev = udev_device_get_parent(parent);
228         value = udev_device_get_sysname(tgtdev);
229         if (sscanf(value, "rport-%d:%d-%d",
230                    &host, &channel, &rport_id) == 3) {
231                 tgtdev = udev_device_new_from_subsystem_sysname(conf->udev,
232                                 "fc_remote_ports", value);
233                 if (tgtdev) {
234                         condlog(3, "SCSI target %d:%d:%d -> "
235                                 "FC rport %d:%d-%d",
236                                 pp->sg_id.host_no, pp->sg_id.channel,
237                                 pp->sg_id.scsi_id, host, channel,
238                                 rport_id);
239                         value = udev_device_get_sysattr_value(tgtdev,
240                                                               "node_name");
241                         if (value) {
242                                 pp->sg_id.proto_id = SCSI_PROTOCOL_FCP;
243                                 pp->sg_id.transport_id = rport_id;
244                                 strncpy(node, value, NODE_NAME_SIZE);
245                                 udev_device_unref(tgtdev);
246                                 return 0;
247                         } else
248                                 udev_device_unref(tgtdev);
249                 }
250         }
251
252         /* Check for iSCSI */
253         parent = pp->udev;
254         targetid = NULL;
255         while (parent) {
256                 targetid = udev_device_get_sysname(parent);
257                 if (!strncmp(targetid , "session", 6))
258                         break;
259                 parent = udev_device_get_parent(parent);
260                 targetid = NULL;
261         }
262         if (parent) {
263                 tgtdev = udev_device_new_from_subsystem_sysname(conf->udev, "iscsi_session", targetid);
264                 if (tgtdev) {
265                         const char *value;
266
267                         value = udev_device_get_sysattr_value(tgtdev, "targetname");
268                         if (value) {
269                                 pp->sg_id.proto_id = SCSI_PROTOCOL_ISCSI;
270                                 strncpy(node, value, NODE_NAME_SIZE);
271                                 udev_device_unref(tgtdev);
272                                 return 0;
273                         }
274                         else
275                                 udev_device_unref(tgtdev);
276                 }
277         }
278         /* Check for libata */
279         parent = pp->udev;
280         targetid = NULL;
281         while (parent) {
282                 targetid = udev_device_get_sysname(parent);
283                 if (targetid && sscanf(targetid, "ata%d", &ata_id) == 1)
284                         break;
285                 parent = udev_device_get_parent(parent);
286                 targetid = NULL;
287         }
288         if (targetid) {
289                 pp->sg_id.proto_id = SCSI_PROTOCOL_ATA;
290                 pp->sg_id.transport_id = ata_id;
291                 snprintf(node, NODE_NAME_SIZE, "ata-%d.00", ata_id);
292                 return 0;
293         }
294         pp->sg_id.proto_id = SCSI_PROTOCOL_UNSPEC;
295         return 1;
296 }
297
298 static void
299 sysfs_set_rport_tmo(struct multipath *mpp, struct path *pp)
300 {
301         struct udev_device *rport_dev = NULL;
302         char value[11];
303         char rport_id[32];
304
305         if (pp->sg_id.proto_id != SCSI_PROTOCOL_FCP) {
306                 condlog(3, "%s: Not a FCP device", pp->dev);
307                 return;
308         }
309         sprintf(rport_id, "rport-%d:%d-%d",
310                 pp->sg_id.host_no, pp->sg_id.channel, pp->sg_id.transport_id);
311         rport_dev = udev_device_new_from_subsystem_sysname(conf->udev,
312                                 "fc_remote_ports", rport_id);
313         if (!rport_dev) {
314                 condlog(1, "%s: No fc_remote_port device for '%s'", pp->dev,
315                         rport_id);
316                 return;
317         }
318         condlog(4, "target%d:%d:%d -> %s", pp->sg_id.host_no,
319                 pp->sg_id.channel, pp->sg_id.scsi_id, rport_id);
320
321         snprintf(value, 11, "%u", mpp->dev_loss);
322         if (mpp->dev_loss &&
323             sysfs_attr_set_value(rport_dev, "dev_loss_tmo", value, 11) <= 0) {
324                 if ((mpp->fast_io_fail == MP_FAST_IO_FAIL_UNSET ||
325                      mpp->fast_io_fail == MP_FAST_IO_FAIL_OFF)
326                     && mpp->dev_loss > 600) {
327                         condlog(3, "%s: limiting dev_loss_tmo to 600, since "
328                                 "fast_io_fail is not set", mpp->alias);
329                         snprintf(value, 11, "%u", 600);
330                         if (sysfs_attr_set_value(rport_dev, "dev_loss_tmo",
331                                                  value, 11) <= 0)
332                                 condlog(0, "%s failed to set dev_loss_tmo",
333                                         mpp->alias);
334                         goto out;
335                 }
336         }
337         if (mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET){
338                 if (mpp->fast_io_fail == MP_FAST_IO_FAIL_OFF)
339                         sprintf(value, "off");
340                 else if (mpp->fast_io_fail == MP_FAST_IO_FAIL_ZERO)
341                         sprintf(value, "0");
342                 else
343                         snprintf(value, 11, "%u", mpp->fast_io_fail);
344                 if (sysfs_attr_set_value(rport_dev, "fast_io_fail_tmo",
345                                          value, 11) <= 0) {
346                         condlog(0, "%s failed to set fast_io_fail_tmo",
347                                 mpp->alias);
348                 }
349         }
350 out:
351         udev_device_unref(rport_dev);
352 }
353
354 int
355 sysfs_set_scsi_tmo (struct multipath *mpp)
356 {
357         struct path *pp;
358         int i;
359         int dev_loss_tmo = mpp->dev_loss;
360
361         if (mpp->no_path_retry > 0) {
362                 int no_path_retry_tmo = mpp->no_path_retry * conf->checkint;
363
364                 if (no_path_retry_tmo > MAX_DEV_LOSS_TMO)
365                         no_path_retry_tmo = MAX_DEV_LOSS_TMO;
366                 if (no_path_retry_tmo > dev_loss_tmo)
367                         dev_loss_tmo = no_path_retry_tmo;
368                 condlog(3, "%s: update dev_loss_tmo to %d",
369                         mpp->alias, dev_loss_tmo);
370         } else if (mpp->no_path_retry == NO_PATH_RETRY_QUEUE) {
371                 dev_loss_tmo = MAX_DEV_LOSS_TMO;
372                 condlog(3, "%s: update dev_loss_tmo to %d",
373                         mpp->alias, dev_loss_tmo);
374         }
375         mpp->dev_loss = dev_loss_tmo;
376         if (mpp->dev_loss && mpp->fast_io_fail >= (int)mpp->dev_loss) {
377                 condlog(3, "%s: turning off fast_io_fail (%d is not smaller than dev_loss_tmo)",
378                         mpp->alias, mpp->fast_io_fail);
379                 mpp->fast_io_fail = MP_FAST_IO_FAIL_OFF;
380         }
381         if (!mpp->dev_loss && mpp->fast_io_fail == MP_FAST_IO_FAIL_UNSET)
382                 return 0;
383
384         vector_foreach_slot(mpp->paths, pp, i) {
385                 sysfs_set_rport_tmo(mpp, pp);
386         }
387         return 0;
388 }
389
390 int
391 do_inq(int sg_fd, int cmddt, int evpd, unsigned int pg_op,
392        void *resp, int mx_resp_len)
393 {
394         unsigned char inqCmdBlk[INQUIRY_CMDLEN] =
395                 { INQUIRY_CMD, 0, 0, 0, 0, 0 };
396         unsigned char sense_b[SENSE_BUFF_LEN];
397         struct sg_io_hdr io_hdr;
398
399         if (cmddt)
400                 inqCmdBlk[1] |= 2;
401         if (evpd)
402                 inqCmdBlk[1] |= 1;
403         inqCmdBlk[2] = (unsigned char) pg_op;
404         inqCmdBlk[3] = (unsigned char)((mx_resp_len >> 8) & 0xff);
405         inqCmdBlk[4] = (unsigned char) (mx_resp_len & 0xff);
406         memset(&io_hdr, 0, sizeof (struct sg_io_hdr));
407         memset(sense_b, 0, SENSE_BUFF_LEN);
408         io_hdr.interface_id = 'S';
409         io_hdr.cmd_len = sizeof (inqCmdBlk);
410         io_hdr.mx_sb_len = sizeof (sense_b);
411         io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
412         io_hdr.dxfer_len = mx_resp_len;
413         io_hdr.dxferp = resp;
414         io_hdr.cmdp = inqCmdBlk;
415         io_hdr.sbp = sense_b;
416         io_hdr.timeout = DEF_TIMEOUT;
417
418         if (ioctl(sg_fd, SG_IO, &io_hdr) < 0)
419                 return -1;
420
421         /* treat SG_ERR here to get rid of sg_err.[ch] */
422         io_hdr.status &= 0x7e;
423         if ((0 == io_hdr.status) && (0 == io_hdr.host_status) &&
424             (0 == io_hdr.driver_status))
425                 return 0;
426         if ((SCSI_CHECK_CONDITION == io_hdr.status) ||
427             (SCSI_COMMAND_TERMINATED == io_hdr.status) ||
428             (SG_ERR_DRIVER_SENSE == (0xf & io_hdr.driver_status))) {
429                 if (io_hdr.sbp && (io_hdr.sb_len_wr > 2)) {
430                         int sense_key;
431                         unsigned char * sense_buffer = io_hdr.sbp;
432                         if (sense_buffer[0] & 0x2)
433                                 sense_key = sense_buffer[1] & 0xf;
434                         else
435                                 sense_key = sense_buffer[2] & 0xf;
436                         if(RECOVERED_ERROR == sense_key)
437                                 return 0;
438                 }
439         }
440         return -1;
441 }
442
443 static int
444 get_serial (char * str, int maxlen, int fd)
445 {
446         int len = 0;
447         char buff[MX_ALLOC_LEN + 1] = {0};
448
449         if (fd < 0)
450                 return 1;
451
452         if (0 == do_inq(fd, 0, 1, 0x80, buff, MX_ALLOC_LEN)) {
453                 len = buff[3];
454                 if (len >= maxlen)
455                         return 1;
456                 if (len > 0) {
457                         memcpy(str, buff + 4, len);
458                         str[len] = '\0';
459                 }
460                 return 0;
461         }
462         return 1;
463 }
464
465 static int
466 get_geometry(struct path *pp)
467 {
468         if (pp->fd < 0)
469                 return 1;
470
471         if (ioctl(pp->fd, HDIO_GETGEO, &pp->geom)) {
472                 condlog(2, "%s: HDIO_GETGEO failed with %d", pp->dev, errno);
473                 memset(&pp->geom, 0, sizeof(pp->geom));
474                 return 1;
475         }
476         condlog(3, "%s: %u cyl, %u heads, %u sectors/track, start at %lu",
477                 pp->dev, pp->geom.cylinders, pp->geom.heads,
478                 pp->geom.sectors, pp->geom.start);
479         return 0;
480 }
481
482 static int
483 scsi_sysfs_pathinfo (struct path * pp)
484 {
485         struct udev_device *parent;
486         const char *attr_path = NULL;
487
488         parent = pp->udev;
489         while (parent) {
490                 const char *subsys = udev_device_get_subsystem(parent);
491                 if (subsys && !strncmp(subsys, "scsi", 4)) {
492                         attr_path = udev_device_get_sysname(parent);
493                         if (!attr_path)
494                                 break;
495                         if (sscanf(attr_path, "%i:%i:%i:%i",
496                                    &pp->sg_id.host_no,
497                                    &pp->sg_id.channel,
498                                    &pp->sg_id.scsi_id,
499                                    &pp->sg_id.lun) == 4)
500                                 break;
501                 }
502                 parent = udev_device_get_parent(parent);
503         }
504         if (!attr_path || pp->sg_id.host_no == -1)
505                 return 1;
506
507         if (sysfs_get_vendor(parent, pp->vendor_id, SCSI_VENDOR_SIZE))
508                 return 1;
509
510         condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
511
512         if (sysfs_get_model(parent, pp->product_id, SCSI_PRODUCT_SIZE))
513                 return 1;
514
515         condlog(3, "%s: product = %s", pp->dev, pp->product_id);
516
517         if (sysfs_get_rev(parent, pp->rev, SCSI_REV_SIZE))
518                 return 1;
519
520         condlog(3, "%s: rev = %s", pp->dev, pp->rev);
521
522         /*
523          * set the hwe configlet pointer
524          */
525         pp->hwe = find_hwe(conf->hwtable, pp->vendor_id, pp->product_id, pp->rev);
526
527         /*
528          * host / bus / target / lun
529          */
530         condlog(3, "%s: h:b:t:l = %i:%i:%i:%i",
531                         pp->dev,
532                         pp->sg_id.host_no,
533                         pp->sg_id.channel,
534                         pp->sg_id.scsi_id,
535                         pp->sg_id.lun);
536
537         /*
538          * target node name
539          */
540         if(!sysfs_get_tgt_nodename(pp, pp->tgt_node_name)) {
541                 condlog(3, "%s: tgt_node_name = %s",
542                         pp->dev, pp->tgt_node_name);
543         }
544
545         return 0;
546 }
547
548 static int
549 ccw_sysfs_pathinfo (struct path * pp)
550 {
551         struct udev_device *parent;
552         char attr_buff[NAME_SIZE];
553         const char *attr_path;
554
555         parent = pp->udev;
556         while (parent) {
557                 const char *subsys = udev_device_get_subsystem(parent);
558                 if (subsys && !strncmp(subsys, "ccw", 3))
559                         break;
560                 parent = udev_device_get_parent(parent);
561         }
562         if (!parent)
563                 return 1;
564
565         sprintf(pp->vendor_id, "IBM");
566
567         condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
568
569         if (sysfs_get_devtype(parent, attr_buff, FILE_NAME_SIZE))
570                 return 1;
571
572         if (!strncmp(attr_buff, "3370", 4)) {
573                 sprintf(pp->product_id,"S/390 DASD FBA");
574         } else if (!strncmp(attr_buff, "9336", 4)) {
575                 sprintf(pp->product_id,"S/390 DASD FBA");
576         } else {
577                 sprintf(pp->product_id,"S/390 DASD ECKD");
578         }
579
580         condlog(3, "%s: product = %s", pp->dev, pp->product_id);
581
582         /*
583          * set the hwe configlet pointer
584          */
585         pp->hwe = find_hwe(conf->hwtable, pp->vendor_id, pp->product_id, NULL);
586
587         /*
588          * host / bus / target / lun
589          */
590         attr_path = udev_device_get_sysname(parent);
591         pp->sg_id.lun = 0;
592         sscanf(attr_path, "%i.%i.%x",
593                         &pp->sg_id.host_no,
594                         &pp->sg_id.channel,
595                         &pp->sg_id.scsi_id);
596         condlog(3, "%s: h:b:t:l = %i:%i:%i:%i",
597                         pp->dev,
598                         pp->sg_id.host_no,
599                         pp->sg_id.channel,
600                         pp->sg_id.scsi_id,
601                         pp->sg_id.lun);
602
603         return 0;
604 }
605
606 static int
607 cciss_sysfs_pathinfo (struct path * pp)
608 {
609         const char * attr_path = NULL;
610         struct udev_device *parent;
611
612         parent = pp->udev;
613         while (parent) {
614                 const char *subsys = udev_device_get_subsystem(parent);
615                 if (subsys && !strncmp(subsys, "cciss", 5)) {
616                         attr_path = udev_device_get_sysname(parent);
617                         if (!attr_path)
618                                 break;
619                         if (sscanf(attr_path, "c%id%i",
620                                    &pp->sg_id.host_no,
621                                    &pp->sg_id.scsi_id) == 2)
622                                 break;
623                 }
624                 parent = udev_device_get_parent(parent);
625         }
626         if (!attr_path || pp->sg_id.host_no == -1)
627                 return 1;
628
629         if (sysfs_get_vendor(parent, pp->vendor_id, SCSI_VENDOR_SIZE))
630                 return 1;
631
632         condlog(3, "%s: vendor = %s", pp->dev, pp->vendor_id);
633
634         if (sysfs_get_model(parent, pp->product_id, SCSI_PRODUCT_SIZE))
635                 return 1;
636
637         condlog(3, "%s: product = %s", pp->dev, pp->product_id);
638
639         if (sysfs_get_rev(parent, pp->rev, SCSI_REV_SIZE))
640                 return 1;
641
642         condlog(3, "%s: rev = %s", pp->dev, pp->rev);
643
644         /*
645          * set the hwe configlet pointer
646          */
647         pp->hwe = find_hwe(conf->hwtable, pp->vendor_id, pp->product_id, pp->rev);
648
649         /*
650          * host / bus / target / lun
651          */
652         pp->sg_id.lun = 0;
653         pp->sg_id.channel = 0;
654         condlog(3, "%s: h:b:t:l = %i:%i:%i:%i",
655                 pp->dev,
656                 pp->sg_id.host_no,
657                 pp->sg_id.channel,
658                 pp->sg_id.scsi_id,
659                 pp->sg_id.lun);
660         return 0;
661 }
662
663 static int
664 common_sysfs_pathinfo (struct path * pp)
665 {
666         if (!pp->udev) {
667                 condlog(4, "%s: udev not initialised", pp->dev);
668                 return 1;
669         }
670         if (sysfs_get_dev(pp->udev, pp->dev_t, BLK_DEV_SIZE)) {
671                 condlog(3, "%s: no 'dev' attribute in sysfs", pp->dev);
672                 return 1;
673         }
674
675         condlog(3, "%s: dev_t = %s", pp->dev, pp->dev_t);
676
677         if (sysfs_get_size(pp, &pp->size))
678                 return 1;
679
680         condlog(3, "%s: size = %llu", pp->dev, pp->size);
681
682         return 0;
683 }
684
685 int
686 path_offline (struct path * pp)
687 {
688         struct udev_device * parent;
689         char buff[SCSI_STATE_SIZE];
690
691         if (pp->bus != SYSFS_BUS_SCSI)
692                 return PATH_UP;
693
694         parent = pp->udev;
695         while (parent) {
696                 const char *subsys = udev_device_get_subsystem(parent);
697                 if (subsys && !strncmp(subsys, "scsi", 4))
698                         break;
699                 parent = udev_device_get_parent(parent);
700         }
701
702         if (!parent) {
703                 condlog(1, "%s: failed to get sysfs information", pp->dev);
704                 return PATH_DOWN;
705         }
706
707         if (sysfs_get_state(parent, buff, SCSI_STATE_SIZE))
708                 return PATH_DOWN;
709
710         condlog(3, "%s: path state = %s", pp->dev, buff);
711
712         if (!strncmp(buff, "offline", 7)) {
713                 pp->offline = 1;
714                 return PATH_DOWN;
715         }
716         pp->offline = 0;
717         if (!strncmp(buff, "blocked", 7))
718                 return PATH_PENDING;
719         else if (!strncmp(buff, "running", 7))
720                 return PATH_UP;
721
722         return PATH_DOWN;
723 }
724
725 int
726 sysfs_pathinfo(struct path * pp)
727 {
728         if (common_sysfs_pathinfo(pp))
729                 return 1;
730
731         pp->bus = SYSFS_BUS_UNDEF;
732         if (!strncmp(pp->dev,"cciss",5))
733                 pp->bus = SYSFS_BUS_CCISS;
734         if (!strncmp(pp->dev,"dasd", 4))
735                 pp->bus = SYSFS_BUS_CCW;
736         if (!strncmp(pp->dev,"sd", 2))
737                 pp->bus = SYSFS_BUS_SCSI;
738
739         if (pp->bus == SYSFS_BUS_UNDEF)
740                 return 0;
741         else if (pp->bus == SYSFS_BUS_SCSI) {
742                 if (scsi_sysfs_pathinfo(pp))
743                         return 1;
744         } else if (pp->bus == SYSFS_BUS_CCW) {
745                 if (ccw_sysfs_pathinfo(pp))
746                         return 1;
747         } else if (pp->bus == SYSFS_BUS_CCISS) {
748                 if (cciss_sysfs_pathinfo(pp))
749                         return 1;
750         }
751         return 0;
752 }
753
754 static int
755 scsi_ioctl_pathinfo (struct path * pp, int mask)
756 {
757         if (mask & DI_SERIAL) {
758                 get_serial(pp->serial, SERIAL_SIZE, pp->fd);
759                 condlog(3, "%s: serial = %s", pp->dev, pp->serial);
760         }
761
762         return 0;
763 }
764
765 static int
766 cciss_ioctl_pathinfo (struct path * pp, int mask)
767 {
768         if (mask & DI_SERIAL) {
769                 get_serial(pp->serial, SERIAL_SIZE, pp->fd);
770                 condlog(3, "%s: serial = %s", pp->dev, pp->serial);
771         }
772         return 0;
773 }
774
775 int
776 get_state (struct path * pp, int daemon)
777 {
778         struct checker * c = &pp->checker;
779         int state;
780
781         condlog(3, "%s: get_state", pp->dev);
782
783         if (!checker_selected(c)) {
784                 if (daemon) {
785                         if (pathinfo(pp, conf->hwtable, DI_SYSFS) != 0) {
786                                 condlog(3, "%s: couldn't get sysfs pathinfo",
787                                         pp->dev);
788                                 return PATH_UNCHECKED;
789                         }
790                 }
791                 select_checker(pp);
792                 if (!checker_selected(c)) {
793                         condlog(3, "%s: No checker selected", pp->dev);
794                         return PATH_UNCHECKED;
795                 }
796                 checker_set_fd(c, pp->fd);
797                 if (checker_init(c, pp->mpp?&pp->mpp->mpcontext:NULL)) {
798                         memset(c, 0x0, sizeof(struct checker));
799                         condlog(3, "%s: checker init failed", pp->dev);
800                         return PATH_UNCHECKED;
801                 }
802         }
803         checker_clear_message(c);
804         if (daemon)
805                 checker_set_async(c);
806         if (!conf->checker_timeout &&
807             (pp->bus != SYSFS_BUS_SCSI ||
808              sysfs_get_timeout(pp, &(c->timeout))))
809                 c->timeout = DEF_TIMEOUT;
810         state = checker_check(c);
811         condlog(3, "%s: state = %s", pp->dev, checker_state_name(state));
812         if (state != PATH_UP && strlen(checker_message(c)))
813                 condlog(3, "%s: checker msg is \"%s\"",
814                         pp->dev, checker_message(c));
815         return state;
816 }
817
818 static int
819 get_prio (struct path * pp)
820 {
821         if (!pp)
822                 return 0;
823
824         struct prio * p = &pp->prio;
825
826         if (!prio_selected(p)) {
827                 select_detect_prio(pp);
828                 select_prio(pp);
829                 if (!prio_selected(p)) {
830                         condlog(3, "%s: no prio selected", pp->dev);
831                         return 1;
832                 }
833         }
834         pp->priority = prio_getprio(p, pp);
835         if (pp->priority < 0) {
836                 condlog(3, "%s: %s prio error", pp->dev, prio_name(p));
837                 pp->priority = PRIO_UNDEF;
838                 return 1;
839         }
840         condlog(3, "%s: %s prio = %u",
841                 pp->dev, prio_name(p), pp->priority);
842         return 0;
843 }
844
845 static int
846 get_uid (struct path * pp)
847 {
848         char *c;
849         const char *value;
850
851         if (!pp->uid_attribute)
852                 select_getuid(pp);
853
854         if (!pp->udev) {
855                 condlog(1, "%s: no udev information", pp->dev);
856                 return 1;
857         }
858
859         memset(pp->wwid, 0, WWID_SIZE);
860         value = udev_device_get_property_value(pp->udev, pp->uid_attribute);
861         if ((!value || strlen(value) == 0) && conf->dry_run == 2)
862                 value = getenv(pp->uid_attribute);
863         if (value && strlen(value)) {
864                 size_t len = WWID_SIZE;
865
866                 if (strlen(value) + 1 > WWID_SIZE) {
867                         condlog(0, "%s: wwid overflow", pp->dev);
868                 } else {
869                         len = strlen(value);
870                 }
871                 strncpy(pp->wwid, value, len);
872         } else {
873                 condlog(3, "%s: no %s attribute", pp->dev,
874                         pp->uid_attribute);
875         }
876
877         /* Strip any trailing blanks */
878         c = strchr(pp->wwid, '\0');
879         c--;
880         while (c && c >= pp->wwid && *c == ' ') {
881                 *c = '\0';
882                 c--;
883         }
884         condlog(3, "%s: uid = %s (udev)", pp->dev,
885                 *pp->wwid == '\0' ? "<empty>" : pp->wwid);
886         return 0;
887 }
888
889 extern int
890 pathinfo (struct path *pp, vector hwtable, int mask)
891 {
892         int path_state;
893
894         condlog(3, "%s: mask = 0x%x", pp->dev, mask);
895
896         /*
897          * fetch info available in sysfs
898          */
899         if (mask & DI_SYSFS && sysfs_pathinfo(pp))
900                 return 1;
901
902         if (mask & DI_BLACKLIST && mask & DI_SYSFS) {
903                 if (filter_device(conf->blist_device, conf->elist_device,
904                                   pp->vendor_id, pp->product_id) > 0) {
905                         return 2;
906                 }
907         }
908
909         path_state = path_offline(pp);
910
911         /*
912          * fetch info not available through sysfs
913          */
914         if (pp->fd < 0)
915                 pp->fd = open(udev_device_get_devnode(pp->udev), O_RDONLY);
916
917         if (pp->fd < 0) {
918                 condlog(4, "Couldn't open node for %s: %s",
919                         pp->dev, strerror(errno));
920                 goto blank;
921         }
922
923         if (mask & DI_SERIAL)
924                 get_geometry(pp);
925
926         if (path_state == PATH_UP && pp->bus == SYSFS_BUS_SCSI &&
927             scsi_ioctl_pathinfo(pp, mask))
928                 goto blank;
929
930         if (pp->bus == SYSFS_BUS_CCISS &&
931             cciss_ioctl_pathinfo(pp, mask))
932                 goto blank;
933
934         if (mask & DI_CHECKER) {
935                 if (path_state == PATH_UP) {
936                         pp->chkrstate = pp->state = get_state(pp, 0);
937                         if (pp->state == PATH_UNCHECKED ||
938                             pp->state == PATH_WILD)
939                                 goto blank;
940                 } else {
941                         condlog(3, "%s: path inaccessible", pp->dev);
942                         pp->chkrstate = pp->state = path_state;
943                 }
944         }
945
946         if (path_state == PATH_UP && (mask & DI_WWID) && !strlen(pp->wwid))
947                 get_uid(pp);
948         if (mask & DI_BLACKLIST && mask & DI_WWID) {
949                 if (filter_wwid(conf->blist_wwid, conf->elist_wwid,
950                                 pp->wwid) > 0) {
951                         return 2;
952                 }
953         }
954
955          /*
956           * Retrieve path priority, even for PATH_DOWN paths if it has never
957           * been successfully obtained before.
958           */
959         if ((mask & DI_PRIO) && path_state == PATH_UP) {
960                 if (pp->state != PATH_DOWN || pp->priority == PRIO_UNDEF) {
961                         if (!strlen(pp->wwid))
962                                 get_uid(pp);
963                         get_prio(pp);
964                 } else {
965                         pp->priority = PRIO_UNDEF;
966                 }
967         }
968
969         return 0;
970
971 blank:
972         /*
973          * Recoverable error, for example faulty or offline path
974          */
975         memset(pp->wwid, 0, WWID_SIZE);
976         pp->chkrstate = pp->state = PATH_DOWN;
977
978         return 0;
979 }