]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/topology.c
96ee74b61b30f5fc2ef4a9841cd338a1e95630d1
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
7 #include "libxfs_priv.h"
9 #include <blkid/blkid.h>
10 #include "xfs_multidisk.h"
11 #include "libfrog/platform.h"
13 #define TERABYTES(count, blog) ((uint64_t)(count) << (40 - (blog)))
14 #define GIGABYTES(count, blog) ((uint64_t)(count) << (30 - (blog)))
15 #define MEGABYTES(count, blog) ((uint64_t)(count) << (20 - (blog)))
18 calc_default_ag_geometry(
29 * First handle the high extreme - the point at which we will
30 * always use the maximum AG size.
32 * This applies regardless of storage configuration.
34 if (dblocks
>= TERABYTES(32, blocklog
)) {
35 blocks
= XFS_AG_MAX_BLOCKS(blocklog
);
40 * For a single underlying storage device over 4TB in size
41 * use the maximum AG size. Between 128MB and 4TB, just use
42 * 4 AGs and scale up smoothly between min/max AG sizes.
45 if (dblocks
>= TERABYTES(4, blocklog
)) {
46 blocks
= XFS_AG_MAX_BLOCKS(blocklog
);
48 } else if (dblocks
>= MEGABYTES(128, blocklog
)) {
49 shift
= XFS_NOMULTIDISK_AGLOG
;
55 * For the multidisk configs we choose an AG count based on the number
56 * of data blocks available, trying to keep the number of AGs higher
57 * than the single disk configurations. This makes the assumption that
58 * larger filesystems have more parallelism available to them.
60 shift
= XFS_MULTIDISK_AGLOG
;
61 if (dblocks
<= GIGABYTES(512, blocklog
))
63 if (dblocks
<= GIGABYTES(8, blocklog
))
65 if (dblocks
< MEGABYTES(128, blocklog
))
67 if (dblocks
< MEGABYTES(64, blocklog
))
69 if (dblocks
< MEGABYTES(32, blocklog
))
73 * If dblocks is not evenly divisible by the number of
74 * desired AGs, round "blocks" up so we don't lose the
75 * last bit of the filesystem. The same principle applies
76 * to the AG count, so we don't lose the last AG!
79 ASSERT(shift
>= 0 && shift
<= XFS_MULTIDISK_AGLOG
);
80 blocks
= dblocks
>> shift
;
81 if (dblocks
& xfs_mask32lo(shift
)) {
82 if (blocks
< XFS_AG_MAX_BLOCKS(blocklog
))
87 *agcount
= dblocks
/ blocks
+ (dblocks
% blocks
!= 0);
91 calc_default_rtgroup_geometry(
101 * For a single underlying storage device over 4TB in size use the
102 * maximum rtgroup size. Between 128MB and 4TB, just use 4 rtgroups
103 * and scale up smoothly between min/max rtgroup sizes.
105 if (rblocks
>= TERABYTES(4, blocklog
)) {
106 blocks
= XFS_MAX_RGBLOCKS
;
109 if (rblocks
>= MEGABYTES(128, blocklog
)) {
110 shift
= XFS_NOMULTIDISK_AGLOG
;
115 * If rblocks is not evenly divisible by the number of desired rt
116 * groups, round "blocks" up so we don't lose the last bit of the
117 * filesystem. The same principle applies to the rt group count, so we
118 * don't lose the last rt group!
121 ASSERT(shift
>= 0 && shift
<= XFS_MULTIDISK_AGLOG
);
122 blocks
= rblocks
>> shift
;
123 if (rblocks
& xfs_mask32lo(shift
)) {
124 if (blocks
< XFS_MAX_RGBLOCKS
)
129 *rgcount
= rblocks
/ blocks
+ (rblocks
% blocks
!= 0);
133 * Check for existing filesystem or partition table on device.
135 * 1 for existing fs or partition
136 * 0 for nothing found
137 * -1 for internal error
144 blkid_probe pr
= NULL
;
150 if (!device
|| !*device
)
153 ret
= -1; /* will reset on success of all setup calls */
155 fd
= open(device
, O_RDONLY
);
158 platform_findsizes((char *)device
, fd
, &size
, &bsz
);
161 /* nothing to overwrite on a 0-length device */
167 pr
= blkid_new_probe_from_filename(device
);
171 ret
= blkid_probe_enable_partitions(pr
, 1);
175 ret
= blkid_do_fullprobe(pr
);
180 * Blkid returns 1 for nothing found and 0 when it finds a signature,
181 * but we want the exact opposite, so reverse the return value here.
183 * In addition print some useful diagnostics about what actually is
191 if (!blkid_probe_lookup_value(pr
, "TYPE", &type
, NULL
)) {
193 _("%s: %s appears to contain an existing "
194 "filesystem (%s).\n"), progname
, device
, type
);
195 } else if (!blkid_probe_lookup_value(pr
, "PTTYPE", &type
, NULL
)) {
197 _("%s: %s appears to contain a partition "
198 "table (%s).\n"), progname
, device
, type
);
201 _("%s: %s appears to contain something weird "
202 "according to blkid\n"), progname
, device
);
207 blkid_free_probe(pr
);
208 /* libblkid 2.38.1 lies and can return -EIO */
211 _("%s: probe of %s failed, cannot detect "
212 "existing filesystem.\n"), progname
, device
);
219 struct device_topology
*dt
,
225 pr
= blkid_new_probe_from_filename(device
);
229 tp
= blkid_probe_get_topology(pr
);
233 dt
->logical_sector_size
= blkid_topology_get_logical_sector_size(tp
);
234 dt
->physical_sector_size
= blkid_topology_get_physical_sector_size(tp
);
235 dt
->sunit
= blkid_topology_get_minimum_io_size(tp
);
236 dt
->swidth
= blkid_topology_get_optimal_io_size(tp
);
239 * If the reported values are the same as the physical sector size
240 * do not bother to report anything. It will only cause warnings
241 * if people specify larger stripe units or widths manually.
243 if (dt
->sunit
== dt
->physical_sector_size
||
244 dt
->swidth
== dt
->physical_sector_size
) {
250 * Blkid reports the information in terms of bytes, but we want it in
251 * terms of 512 bytes blocks (only to convert it to bytes later..)
256 if (blkid_topology_get_alignment_offset(tp
) != 0) {
258 _("warning: device is not properly aligned %s\n"),
261 if (!force_overwrite
) {
263 _("Use -f to force usage of a misaligned device\n"));
267 /* Do not use physical sector size if the device is misaligned */
268 dt
->physical_sector_size
= dt
->logical_sector_size
;
271 blkid_free_probe(pr
);
275 blkid_free_probe(pr
);
277 _("warning: unable to probe device topology for device %s\n"),
283 struct libxfs_dev
*dev
,
284 struct device_topology
*dt
,
290 * Nothing to do if this particular subvolume doesn't exist.
296 * If our target is a regular file, use platform_findsizes
297 * to try to obtain the underlying filesystem's requirements
298 * for direct IO; we'll set our sector size to that if possible.
300 if (dev
->isfile
|| (!stat(dev
->name
, &st
) && S_ISREG(st
.st_mode
))) {
301 int flags
= O_RDONLY
;
305 /* with xi->disfile we may not have the file yet! */
309 fd
= open(dev
->name
, flags
, 0666);
311 platform_findsizes(dev
->name
, fd
, &dummy
,
312 &dt
->logical_sector_size
);
315 dt
->logical_sector_size
= BBSIZE
;
318 blkid_get_topology(dev
->name
, dt
, force_overwrite
);
321 ASSERT(dt
->logical_sector_size
);
324 * Older kernels may not have physical/logical distinction.
326 if (!dt
->physical_sector_size
)
327 dt
->physical_sector_size
= dt
->logical_sector_size
;
332 struct libxfs_init
*xi
,
333 struct fs_topology
*ft
,
336 get_device_topology(&xi
->data
, &ft
->data
, force_overwrite
);
337 get_device_topology(&xi
->rt
, &ft
->rt
, force_overwrite
);
338 get_device_topology(&xi
->log
, &ft
->log
, force_overwrite
);