2
* topology - gathers information about device topology
4
* Copyright 2009 Red Hat, Inc. All rights reserved.
6
* This file may be redistributed under the terms of the
7
* GNU Lesser General Public License.
19
* @title: Topology information
20
* @short_description: block device topology information.
22
* The topology chain provides details about Linux block devices, for more
25
* Linux kernel Documentation/ABI/testing/sysfs-block
27
* NAME=value (tags) interface is enabled by blkid_probe_enable_topology(),
30
* @LOGICAL_SECTOR_SIZE: this is the smallest unit the storage device can
31
* address. It is typically 512 bytes.
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.
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.
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.
43
* @ALIGNMENT_OFFSET: indicates how many bytes the beginning of the device is
44
* offset from the disk's natural alignment.
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.
52
* blkid_probe_get_topology()
54
* blkid_topology_get_'VALUENAME'()
56
static int topology_probe(blkid_probe pr, struct blkid_chain *chn);
57
static void topology_free(blkid_probe pr, void *data);
58
static int topology_is_complete(blkid_probe pr);
59
static int topology_set_logical_sector_size(blkid_probe pr);
64
struct 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;
73
* Topology chain probing functions
75
static const struct blkid_idinfo *idinfos[] =
91
const struct blkid_chaindrv topology_drv = {
92
.id = BLKID_CHAIN_TOPLGY,
94
.dflt_enabled = FALSE,
96
.nidinfos = ARRAY_SIZE(idinfos),
97
.probe = topology_probe,
98
.safeprobe = topology_probe,
99
.free_data = topology_free
103
* blkid_probe_enable_topology:
105
* @enable: TRUE/FALSE
107
* Enables/disables the topology probing for non-binary interface.
109
* Returns: 0 on success, or -1 in case of error.
111
int blkid_probe_enable_topology(blkid_probe pr, int enable)
115
pr->chains[BLKID_CHAIN_TOPLGY].enabled = enable;
120
* blkid_probe_get_topology:
123
* This is a binary interface for topology values. See also blkid_topology_*
126
* This function is independent on blkid_do_[safe,full]probe() and
127
* blkid_probe_enable_topology() calls.
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()).
134
* Returns: blkid_topopogy, or NULL in case of error.
136
blkid_topology blkid_probe_get_topology(blkid_probe pr)
138
return (blkid_topology) blkid_probe_get_binary_data(pr,
139
&pr->chains[BLKID_CHAIN_TOPLGY]);
143
* The blkid_do_probe() backend.
145
static int topology_probe(blkid_probe pr, struct blkid_chain *chn)
149
if (!pr || chn->idx < -1)
152
if (!S_ISBLK(pr->mode))
153
return -1; /* nothing, works with block devices only */
156
DBG(DEBUG_LOWPROBE, printf("initialize topology binary data\n"));
159
/* reset binary data */
161
sizeof(struct blkid_struct_topology));
163
chn->data = calloc(1,
164
sizeof(struct blkid_struct_topology));
170
blkid_probe_chain_reset_vals(pr, chn);
173
printf("--> starting probing loop [TOPOLOGY idx=%d]\n",
176
i = chn->idx < 0 ? 0 : chn->idx + 1U;
178
for ( ; i < ARRAY_SIZE(idinfos); i++) {
179
const struct blkid_idinfo *id = idinfos[i];
184
DBG(DEBUG_LOWPROBE, printf(
185
"%s: call probefunc()\n", id->name));
186
if (id->probefunc(pr, NULL) != 0)
190
if (!topology_is_complete(pr))
193
/* generic for all probing drivers */
194
topology_set_logical_sector_size(pr);
197
printf("<-- leaving probing loop (type=%s) [TOPOLOGY idx=%d]\n",
198
id->name, chn->idx));
203
printf("<-- leaving probing loop (failed) [TOPOLOGY idx=%d]\n",
208
static void topology_free(blkid_probe pr __attribute__((__unused__)),
214
static int topology_set_value(blkid_probe pr, const char *name,
215
size_t structoff, unsigned long data)
217
struct blkid_chain *chn = blkid_probe_get_chain(pr);
222
return 0; /* ignore zeros */
226
(unsigned long *) (chn->data + structoff);
230
return blkid_probe_sprintf_value(pr, name, "%llu", data);
233
/* the topology info is complete when we have at least "minimum_io_size" which
234
* is provided by all blkid topology drivers */
235
static int topology_is_complete(blkid_probe pr)
237
struct blkid_chain *chn = blkid_probe_get_chain(pr);
242
if (chn->binary && chn->data) {
243
blkid_topology tp = (blkid_topology) chn->data;
244
if (tp->minimum_io_size)
248
return __blkid_probe_lookup_value(pr, "MINIMUM_IO_SIZE") ? TRUE : FALSE;
251
int blkid_topology_set_alignment_offset(blkid_probe pr, int val)
255
/* Welcome to Hell. The kernel is able to return -1 as an
256
* alignment_offset if no compatible sizes and alignments
257
* exist for stacked devices.
259
* There is no way how libblkid caller can respond to the value -1, so
260
* we will hide this corner case...
262
* (TODO: maybe we can export an extra boolean value 'misaligned' rather
263
* then complete hide this problem.)
265
xval = val < 0 ? 0 : val;
267
return topology_set_value(pr,
269
offsetof(struct blkid_struct_topology, alignment_offset),
273
int blkid_topology_set_minimum_io_size(blkid_probe pr, unsigned long val)
275
return topology_set_value(pr,
277
offsetof(struct blkid_struct_topology, minimum_io_size),
281
int blkid_topology_set_optimal_io_size(blkid_probe pr, unsigned long val)
283
return topology_set_value(pr,
285
offsetof(struct blkid_struct_topology, optimal_io_size),
289
/* BLKSSZGET is provided on all systems since 2.3.3 -- so we don't have to
290
* waste time with sysfs.
292
static int topology_set_logical_sector_size(blkid_probe pr)
294
unsigned long val = blkid_probe_get_sectorsize(pr);
299
return topology_set_value(pr,
300
"LOGICAL_SECTOR_SIZE",
301
offsetof(struct blkid_struct_topology, logical_sector_size),
305
int blkid_topology_set_physical_sector_size(blkid_probe pr, unsigned long val)
307
return topology_set_value(pr,
308
"PHYSICAL_SECTOR_SIZE",
309
offsetof(struct blkid_struct_topology, physical_sector_size),
314
* blkid_topology_get_alignment_offset:
317
* Returns: alignment offset in bytes or 0.
319
unsigned long blkid_topology_get_alignment_offset(blkid_topology tp)
321
return tp ? tp->alignment_offset : 0;
325
* blkid_topology_get_minimum_io_size:
328
* Returns: minimum io size in bytes or 0.
330
unsigned long blkid_topology_get_minimum_io_size(blkid_topology tp)
332
return tp ? tp->minimum_io_size : 0;
336
* blkid_topology_get_optimal_io_size
339
* Returns: optimal io size in bytes or 0.
341
unsigned long blkid_topology_get_optimal_io_size(blkid_topology tp)
343
return tp ? tp->optimal_io_size : 0;
347
* blkid_topology_get_logical_sector_size
350
* Returns: logical sector size (BLKSSZGET ioctl) in bytes or 0.
352
unsigned long blkid_topology_get_logical_sector_size(blkid_topology tp)
354
return tp ? tp->logical_sector_size : 0;
358
* blkid_topology_get_physical_sector_size
361
* Returns: logical sector size (BLKSSZGET ioctl) in bytes or 0.
363
unsigned long blkid_topology_get_physical_sector_size(blkid_topology tp)
365
return tp ? tp->physical_sector_size : 0;