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