1 /*
2 * ioctl based topology -- gathers topology information
3 *
4 * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
5 *
6 * This file may be redistributed under the terms of the
7 * GNU Lesser General Public License.
8 *
9 */
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <stdint.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #include <errno.h>
18
19 #include "topology.h"
20
21 /*
22 * ioctl topology values
23 */
24 static const struct topology_val {
25
26 long ioc;
27
28 /* functions to set probing result */
29 int (*set_ulong)(blkid_probe, unsigned long);
30 int (*set_int)(blkid_probe, int);
31 int (*set_u64)(blkid_probe, uint64_t);
32
33 } topology_vals[] = {
34 { BLKALIGNOFF, NULL, blkid_topology_set_alignment_offset },
35 { BLKIOMIN, blkid_topology_set_minimum_io_size },
36 { BLKIOOPT, blkid_topology_set_optimal_io_size },
37 { BLKPBSZGET, blkid_topology_set_physical_sector_size },
38 { BLKGETDISKSEQ, .set_u64 = blkid_topology_set_diskseq },
39 /* we read BLKSSZGET in topology.c */
40 };
41
42 static int probe_ioctl_tp(blkid_probe pr,
43 const struct blkid_idmag *mag __attribute__((__unused__)))
44 {
45 size_t i;
46
47 for (i = 0; i < ARRAY_SIZE(topology_vals); i++) {
48 const struct topology_val *val = &topology_vals[i];
49 int rc = 1;
50 union {
51 unsigned long ul;
52 int i;
53 uint64_t u64;
54 } data;
55
56 if (ioctl(pr->fd, val->ioc, &data) == -1)
57 goto nothing;
58
59 if (val->set_int)
60 rc = val->set_int(pr, data.i);
61 else if (val->set_ulong)
62 rc = val->set_ulong(pr, data.ul);
63 else
64 rc = val->set_u64(pr, data.u64);
65
66 if (rc)
67 goto err;
68 }
69
70 return 0;
71 nothing:
72 return 1;
73 err:
74 return -1;
75 }
76
77 const struct blkid_idinfo ioctl_tp_idinfo =
78 {
79 .name = "ioctl",
80 .probefunc = probe_ioctl_tp,
81 .magics = BLKID_NONE_MAGIC
82 };
83