blob: 93fa3802e5efc37b717f428c27a8b69aa14218da [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
2 * topology - gathers information about device topology
3 *
4 * Copyright 2009 Red Hat, Inc. All rights reserved.
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 <stddef.h>
14
15#include "topology.h"
16
17/**
18 * SECTION:topology
19 * @title: Topology information
20 * @short_description: block device topology information.
21 *
22 * The topology chain provides details about Linux block devices, for more
23 * information see:
24 *
25 * Linux kernel Documentation/ABI/testing/sysfs-block
26 *
27 * NAME=value (tags) interface is enabled by blkid_probe_enable_topology(),
28 * and provides:
29 *
30 * @LOGICAL_SECTOR_SIZE: this is the smallest unit the storage device can
31 * address. It is typically 512 bytes.
32 *
33 * @PHYSICAL_SECTOR_SIZE: this is the smallest unit a physical storage device
34 * can write atomically. It is usually the same as the
35 * logical sector size but may be bigger.
36 *
37 * @MINIMUM_IO_SIZE: minimum size which is the device's preferred unit of I/O.
38 * For RAID arrays it is often the stripe chunk size.
39 *
40 * @OPTIMAL_IO_SIZE: usually the stripe width for RAID or zero. For RAID arrays
41 * it is usually the stripe width or the internal track size.
42 *
43 * @ALIGNMENT_OFFSET: indicates how many bytes the beginning of the device is
44 * offset from the disk's natural alignment.
45 *
46 * The NAME=value tags are not defined when the corresponding topology value
47 * is zero. The MINIMUM_IO_SIZE should be always defined if kernel provides
48 * topology information.
49 *
50 * Binary interface:
51 *
52 * blkid_probe_get_topology()
53 *
54 * blkid_topology_get_'VALUENAME'()
55 */
56static int topology_probe(blkid_probe pr, struct blkid_chain *chn);
57static void topology_free(blkid_probe pr, void *data);
58static int topology_is_complete(blkid_probe pr);
59static int topology_set_logical_sector_size(blkid_probe pr);
60
61/*
62 * Binary interface
63 */
64struct blkid_struct_topology {
65 unsigned long alignment_offset;
66 unsigned long minimum_io_size;
67 unsigned long optimal_io_size;
68 unsigned long logical_sector_size;
69 unsigned long physical_sector_size;
70};
71
72/*
73 * Topology chain probing functions
74 */
75static const struct blkid_idinfo *idinfos[] =
76{
77#ifdef __linux__
78 &ioctl_tp_idinfo,
79 &sysfs_tp_idinfo,
80 &md_tp_idinfo,
81 &dm_tp_idinfo,
82 &lvm_tp_idinfo,
83 &evms_tp_idinfo
84#endif
85};
86
87
88/*
89 * Driver definition
90 */
91const struct blkid_chaindrv topology_drv = {
92 .id = BLKID_CHAIN_TOPLGY,
93 .name = "topology",
94 .dflt_enabled = FALSE,
95 .idinfos = idinfos,
96 .nidinfos = ARRAY_SIZE(idinfos),
97 .probe = topology_probe,
98 .safeprobe = topology_probe,
99 .free_data = topology_free
100};
101
102/**
103 * blkid_probe_enable_topology:
104 * @pr: probe
105 * @enable: TRUE/FALSE
106 *
107 * Enables/disables the topology probing for non-binary interface.
108 *
109 * Returns: 0 on success, or -1 in case of error.
110 */
111int blkid_probe_enable_topology(blkid_probe pr, int enable)
112{
113 if (!pr)
114 return -1;
115 pr->chains[BLKID_CHAIN_TOPLGY].enabled = enable;
116 return 0;
117}
118
119/**
120 * blkid_probe_get_topology:
121 * @pr: probe
122 *
123 * This is a binary interface for topology values. See also blkid_topology_*
124 * functions.
125 *
126 * This function is independent on blkid_do_[safe,full]probe() and
127 * blkid_probe_enable_topology() calls.
128 *
129 * WARNING: the returned object will be overwritten by the next
130 * blkid_probe_get_topology() call for the same @pr. If you want to
131 * use more blkid_topopogy objects in the same time you have to create
132 * more blkid_probe handlers (see blkid_new_probe()).
133 *
134 * Returns: blkid_topopogy, or NULL in case of error.
135 */
136blkid_topology blkid_probe_get_topology(blkid_probe pr)
137{
138 return (blkid_topology) blkid_probe_get_binary_data(pr,
139 &pr->chains[BLKID_CHAIN_TOPLGY]);
140}
141
142/*
143 * The blkid_do_probe() backend.
144 */
145static int topology_probe(blkid_probe pr, struct blkid_chain *chn)
146{
147 size_t i;
148
149 if (!pr || chn->idx < -1)
150 return -1;
151
152 if (!S_ISBLK(pr->mode))
bigbiff7b4c7a62015-01-01 19:44:14 -0500153 return -EINVAL; /* nothing, works with block devices only */
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500154
155 if (chn->binary) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500156 DBG(LOWPROBE, ul_debug("initialize topology binary data"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500157
158 if (chn->data)
159 /* reset binary data */
160 memset(chn->data, 0,
161 sizeof(struct blkid_struct_topology));
162 else {
163 chn->data = calloc(1,
164 sizeof(struct blkid_struct_topology));
165 if (!chn->data)
bigbiff7b4c7a62015-01-01 19:44:14 -0500166 return -ENOMEM;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500167 }
168 }
169
170 blkid_probe_chain_reset_vals(pr, chn);
171
bigbiff7b4c7a62015-01-01 19:44:14 -0500172 DBG(LOWPROBE, ul_debug("--> starting probing loop [TOPOLOGY idx=%d]",
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500173 chn->idx));
174
175 i = chn->idx < 0 ? 0 : chn->idx + 1U;
176
177 for ( ; i < ARRAY_SIZE(idinfos); i++) {
178 const struct blkid_idinfo *id = idinfos[i];
179
180 chn->idx = i;
181
182 if (id->probefunc) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500183 DBG(LOWPROBE, ul_debug("%s: call probefunc()", id->name));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500184 if (id->probefunc(pr, NULL) != 0)
185 continue;
186 }
187
188 if (!topology_is_complete(pr))
189 continue;
190
191 /* generic for all probing drivers */
192 topology_set_logical_sector_size(pr);
193
bigbiff7b4c7a62015-01-01 19:44:14 -0500194 DBG(LOWPROBE, ul_debug("<-- leaving probing loop (type=%s) [TOPOLOGY idx=%d]",
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500195 id->name, chn->idx));
bigbiff7b4c7a62015-01-01 19:44:14 -0500196 return BLKID_PROBE_OK;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500197 }
198
bigbiff7b4c7a62015-01-01 19:44:14 -0500199 DBG(LOWPROBE, ul_debug("<-- leaving probing loop (failed) [TOPOLOGY idx=%d]",
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500200 chn->idx));
bigbiff7b4c7a62015-01-01 19:44:14 -0500201 return BLKID_PROBE_NONE;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500202}
203
204static void topology_free(blkid_probe pr __attribute__((__unused__)),
205 void *data)
206{
207 free(data);
208}
209
210static int topology_set_value(blkid_probe pr, const char *name,
211 size_t structoff, unsigned long data)
212{
213 struct blkid_chain *chn = blkid_probe_get_chain(pr);
214
215 if (!chn)
216 return -1;
217 if (!data)
218 return 0; /* ignore zeros */
219
220 if (chn->binary) {
221 memcpy(chn->data + structoff, &data, sizeof(data));
222 return 0;
223 }
224 return blkid_probe_sprintf_value(pr, name, "%lu", data);
225}
226
227
228/* the topology info is complete when we have at least "minimum_io_size" which
229 * is provided by all blkid topology drivers */
230static int topology_is_complete(blkid_probe pr)
231{
232 struct blkid_chain *chn = blkid_probe_get_chain(pr);
233
234 if (!chn)
235 return FALSE;
236
237 if (chn->binary && chn->data) {
238 blkid_topology tp = (blkid_topology) chn->data;
239 if (tp->minimum_io_size)
240 return TRUE;
241 }
242
243 return __blkid_probe_lookup_value(pr, "MINIMUM_IO_SIZE") ? TRUE : FALSE;
244}
245
246int blkid_topology_set_alignment_offset(blkid_probe pr, int val)
247{
248 unsigned long xval;
249
250 /* Welcome to Hell. The kernel is able to return -1 as an
251 * alignment_offset if no compatible sizes and alignments
252 * exist for stacked devices.
253 *
254 * There is no way how libblkid caller can respond to the value -1, so
255 * we will hide this corner case...
256 *
257 * (TODO: maybe we can export an extra boolean value 'misaligned' rather
258 * then complete hide this problem.)
259 */
260 xval = val < 0 ? 0 : val;
261
262 return topology_set_value(pr,
263 "ALIGNMENT_OFFSET",
264 offsetof(struct blkid_struct_topology, alignment_offset),
265 xval);
266}
267
268int blkid_topology_set_minimum_io_size(blkid_probe pr, unsigned long val)
269{
270 return topology_set_value(pr,
271 "MINIMUM_IO_SIZE",
272 offsetof(struct blkid_struct_topology, minimum_io_size),
273 val);
274}
275
276int blkid_topology_set_optimal_io_size(blkid_probe pr, unsigned long val)
277{
278 return topology_set_value(pr,
279 "OPTIMAL_IO_SIZE",
280 offsetof(struct blkid_struct_topology, optimal_io_size),
281 val);
282}
283
284/* BLKSSZGET is provided on all systems since 2.3.3 -- so we don't have to
285 * waste time with sysfs.
286 */
287static int topology_set_logical_sector_size(blkid_probe pr)
288{
289 unsigned long val = blkid_probe_get_sectorsize(pr);
290
291 if (!val)
292 return -1;
293
294 return topology_set_value(pr,
295 "LOGICAL_SECTOR_SIZE",
296 offsetof(struct blkid_struct_topology, logical_sector_size),
297 val);
298}
299
300int blkid_topology_set_physical_sector_size(blkid_probe pr, unsigned long val)
301{
302 return topology_set_value(pr,
303 "PHYSICAL_SECTOR_SIZE",
304 offsetof(struct blkid_struct_topology, physical_sector_size),
305 val);
306}
307
308/**
309 * blkid_topology_get_alignment_offset:
310 * @tp: topology
311 *
312 * Returns: alignment offset in bytes or 0.
313 */
314unsigned long blkid_topology_get_alignment_offset(blkid_topology tp)
315{
316 return tp->alignment_offset;
317}
318
319/**
320 * blkid_topology_get_minimum_io_size:
321 * @tp: topology
322 *
323 * Returns: minimum io size in bytes or 0.
324 */
325unsigned long blkid_topology_get_minimum_io_size(blkid_topology tp)
326{
327 return tp->minimum_io_size;
328}
329
330/**
331 * blkid_topology_get_optimal_io_size
332 * @tp: topology
333 *
334 * Returns: optimal io size in bytes or 0.
335 */
336unsigned long blkid_topology_get_optimal_io_size(blkid_topology tp)
337{
338 return tp->optimal_io_size;
339}
340
341/**
342 * blkid_topology_get_logical_sector_size
343 * @tp: topology
344 *
345 * Returns: logical sector size (BLKSSZGET ioctl) in bytes or 0.
346 */
347unsigned long blkid_topology_get_logical_sector_size(blkid_topology tp)
348{
349 return tp->logical_sector_size;
350}
351
352/**
353 * blkid_topology_get_physical_sector_size
354 * @tp: topology
355 *
356 * Returns: logical sector size (BLKSSZGET ioctl) in bytes or 0.
357 */
358unsigned long blkid_topology_get_physical_sector_size(blkid_topology tp)
359{
360 return tp->physical_sector_size;
361}
362