]>
Commit | Line | Data |
---|---|---|
959ef981 | 1 | // SPDX-License-Identifier: GPL-2.0 |
82c3a179 BD |
2 | /* |
3 | * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc. | |
4 | * All Rights Reserved. | |
82c3a179 BD |
5 | */ |
6 | ||
6bc3531c | 7 | #include "libxfs_priv.h" |
82c3a179 | 8 | #include "libxcmd.h" |
bd07eede | 9 | #include <blkid/blkid.h> |
82c3a179 | 10 | #include "xfs_multidisk.h" |
6bc3531c | 11 | #include "libfrog/platform.h" |
82c3a179 | 12 | |
14f8b681 DW |
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))) | |
82c3a179 BD |
16 | |
17 | void | |
18 | calc_default_ag_geometry( | |
19 | int blocklog, | |
14f8b681 | 20 | uint64_t dblocks, |
82c3a179 | 21 | int multidisk, |
14f8b681 DW |
22 | uint64_t *agsize, |
23 | uint64_t *agcount) | |
82c3a179 | 24 | { |
14f8b681 | 25 | uint64_t blocks = 0; |
82c3a179 BD |
26 | int shift = 0; |
27 | ||
28 | /* | |
29 | * First handle the high extreme - the point at which we will | |
30 | * always use the maximum AG size. | |
31 | * | |
32 | * This applies regardless of storage configuration. | |
33 | */ | |
34 | if (dblocks >= TERABYTES(32, blocklog)) { | |
35 | blocks = XFS_AG_MAX_BLOCKS(blocklog); | |
36 | goto done; | |
37 | } | |
38 | ||
39 | /* | |
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. | |
43 | */ | |
44 | if (!multidisk) { | |
45 | if (dblocks >= TERABYTES(4, blocklog)) { | |
46 | blocks = XFS_AG_MAX_BLOCKS(blocklog); | |
47 | goto done; | |
48 | } else if (dblocks >= MEGABYTES(128, blocklog)) { | |
49 | shift = XFS_NOMULTIDISK_AGLOG; | |
50 | goto calc_blocks; | |
51 | } | |
52 | } | |
53 | ||
54 | /* | |
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. | |
59 | */ | |
60 | shift = XFS_MULTIDISK_AGLOG; | |
61 | if (dblocks <= GIGABYTES(512, blocklog)) | |
62 | shift--; | |
63 | if (dblocks <= GIGABYTES(8, blocklog)) | |
64 | shift--; | |
65 | if (dblocks < MEGABYTES(128, blocklog)) | |
66 | shift--; | |
67 | if (dblocks < MEGABYTES(64, blocklog)) | |
68 | shift--; | |
69 | if (dblocks < MEGABYTES(32, blocklog)) | |
70 | shift--; | |
71 | ||
72 | /* | |
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! | |
77 | */ | |
78 | calc_blocks: | |
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)) | |
83 | blocks++; | |
84 | } | |
85 | done: | |
86 | *agsize = blocks; | |
87 | *agcount = dblocks / blocks + (dblocks % blocks != 0); | |
88 | } | |
89 | ||
90 | /* | |
91 | * Check for existing filesystem or partition table on device. | |
92 | * Returns: | |
93 | * 1 for existing fs or partition | |
94 | * 0 for nothing found | |
95 | * -1 for internal error | |
96 | */ | |
82c3a179 BD |
97 | int |
98 | check_overwrite( | |
99 | const char *device) | |
100 | { | |
101 | const char *type; | |
102 | blkid_probe pr = NULL; | |
103 | int ret; | |
104 | int fd; | |
105 | long long size; | |
106 | int bsz; | |
107 | ||
108 | if (!device || !*device) | |
109 | return 0; | |
110 | ||
111 | ret = -1; /* will reset on success of all setup calls */ | |
112 | ||
113 | fd = open(device, O_RDONLY); | |
114 | if (fd < 0) | |
115 | goto out; | |
116 | platform_findsizes((char *)device, fd, &size, &bsz); | |
117 | close(fd); | |
118 | ||
119 | /* nothing to overwrite on a 0-length device */ | |
120 | if (size == 0) { | |
121 | ret = 0; | |
122 | goto out; | |
123 | } | |
124 | ||
125 | pr = blkid_new_probe_from_filename(device); | |
126 | if (!pr) | |
127 | goto out; | |
128 | ||
129 | ret = blkid_probe_enable_partitions(pr, 1); | |
130 | if (ret < 0) | |
131 | goto out; | |
132 | ||
133 | ret = blkid_do_fullprobe(pr); | |
134 | if (ret < 0) | |
135 | goto out; | |
136 | ||
137 | /* | |
138 | * Blkid returns 1 for nothing found and 0 when it finds a signature, | |
139 | * but we want the exact opposite, so reverse the return value here. | |
140 | * | |
141 | * In addition print some useful diagnostics about what actually is | |
142 | * on the device. | |
143 | */ | |
144 | if (ret) { | |
145 | ret = 0; | |
146 | goto out; | |
147 | } | |
148 | ||
149 | if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL)) { | |
150 | fprintf(stderr, | |
151 | _("%s: %s appears to contain an existing " | |
152 | "filesystem (%s).\n"), progname, device, type); | |
153 | } else if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL)) { | |
154 | fprintf(stderr, | |
155 | _("%s: %s appears to contain a partition " | |
156 | "table (%s).\n"), progname, device, type); | |
157 | } else { | |
158 | fprintf(stderr, | |
159 | _("%s: %s appears to contain something weird " | |
160 | "according to blkid\n"), progname, device); | |
161 | } | |
162 | ret = 1; | |
163 | out: | |
164 | if (pr) | |
165 | blkid_free_probe(pr); | |
166 | if (ret == -1) | |
167 | fprintf(stderr, | |
168 | _("%s: probe of %s failed, cannot detect " | |
169 | "existing filesystem.\n"), progname, device); | |
170 | return ret; | |
171 | } | |
172 | ||
f35e15ee CH |
173 | static void |
174 | blkid_get_topology( | |
175 | const char *device, | |
176 | struct device_topology *dt, | |
177 | int force_overwrite) | |
82c3a179 | 178 | { |
82c3a179 BD |
179 | blkid_topology tp; |
180 | blkid_probe pr; | |
82c3a179 BD |
181 | |
182 | pr = blkid_new_probe_from_filename(device); | |
183 | if (!pr) | |
184 | return; | |
185 | ||
186 | tp = blkid_probe_get_topology(pr); | |
187 | if (!tp) | |
188 | goto out_free_probe; | |
189 | ||
f35e15ee CH |
190 | dt->logical_sector_size = blkid_topology_get_logical_sector_size(tp); |
191 | dt->physical_sector_size = blkid_topology_get_physical_sector_size(tp); | |
192 | dt->sunit = blkid_topology_get_minimum_io_size(tp); | |
193 | dt->swidth = blkid_topology_get_optimal_io_size(tp); | |
82c3a179 BD |
194 | |
195 | /* | |
196 | * If the reported values are the same as the physical sector size | |
197 | * do not bother to report anything. It will only cause warnings | |
198 | * if people specify larger stripe units or widths manually. | |
199 | */ | |
f35e15ee CH |
200 | if (dt->sunit == dt->physical_sector_size || |
201 | dt->swidth == dt->physical_sector_size) { | |
202 | dt->sunit = 0; | |
203 | dt->swidth = 0; | |
82c3a179 BD |
204 | } |
205 | ||
206 | /* | |
207 | * Blkid reports the information in terms of bytes, but we want it in | |
208 | * terms of 512 bytes blocks (only to convert it to bytes later..) | |
209 | */ | |
f35e15ee CH |
210 | dt->sunit >>= 9; |
211 | dt->swidth >>= 9; | |
82c3a179 BD |
212 | |
213 | if (blkid_topology_get_alignment_offset(tp) != 0) { | |
214 | fprintf(stderr, | |
215 | _("warning: device is not properly aligned %s\n"), | |
216 | device); | |
217 | ||
218 | if (!force_overwrite) { | |
219 | fprintf(stderr, | |
220 | _("Use -f to force usage of a misaligned device\n")); | |
221 | ||
222 | exit(EXIT_FAILURE); | |
223 | } | |
224 | /* Do not use physical sector size if the device is misaligned */ | |
f35e15ee | 225 | dt->physical_sector_size = dt->logical_sector_size; |
82c3a179 BD |
226 | } |
227 | ||
228 | blkid_free_probe(pr); | |
229 | return; | |
230 | ||
231 | out_free_probe: | |
232 | blkid_free_probe(pr); | |
233 | fprintf(stderr, | |
234 | _("warning: unable to probe device topology for device %s\n"), | |
235 | device); | |
236 | } | |
82c3a179 | 237 | |
f35e15ee CH |
238 | static void |
239 | get_device_topology( | |
240 | struct libxfs_dev *dev, | |
241 | struct device_topology *dt, | |
82c3a179 BD |
242 | int force_overwrite) |
243 | { | |
f35e15ee CH |
244 | struct stat st; |
245 | ||
246 | /* | |
247 | * Nothing to do if this particular subvolume doesn't exist. | |
248 | */ | |
249 | if (!dev->name) | |
250 | return; | |
82c3a179 BD |
251 | |
252 | /* | |
253 | * If our target is a regular file, use platform_findsizes | |
254 | * to try to obtain the underlying filesystem's requirements | |
255 | * for direct IO; we'll set our sector size to that if possible. | |
256 | */ | |
f35e15ee | 257 | if (dev->isfile || (!stat(dev->name, &st) && S_ISREG(st.st_mode))) { |
82c3a179 BD |
258 | int flags = O_RDONLY; |
259 | long long dummy; | |
f35e15ee | 260 | int fd; |
82c3a179 BD |
261 | |
262 | /* with xi->disfile we may not have the file yet! */ | |
f35e15ee | 263 | if (dev->isfile) |
82c3a179 BD |
264 | flags |= O_CREAT; |
265 | ||
f35e15ee | 266 | fd = open(dev->name, flags, 0666); |
82c3a179 | 267 | if (fd >= 0) { |
f35e15ee CH |
268 | platform_findsizes(dev->name, fd, &dummy, |
269 | &dt->logical_sector_size); | |
82c3a179 | 270 | close(fd); |
f35e15ee CH |
271 | } else { |
272 | dt->logical_sector_size = BBSIZE; | |
273 | } | |
82c3a179 | 274 | } else { |
f35e15ee | 275 | blkid_get_topology(dev->name, dt, force_overwrite); |
82c3a179 BD |
276 | } |
277 | ||
f35e15ee | 278 | ASSERT(dt->logical_sector_size); |
82c3a179 | 279 | |
f35e15ee CH |
280 | /* |
281 | * Older kernels may not have physical/logical distinction. | |
282 | */ | |
283 | if (!dt->physical_sector_size) | |
284 | dt->physical_sector_size = dt->logical_sector_size; | |
285 | } | |
286 | ||
287 | void | |
288 | get_topology( | |
289 | struct libxfs_init *xi, | |
290 | struct fs_topology *ft, | |
291 | int force_overwrite) | |
292 | { | |
293 | get_device_topology(&xi->data, &ft->data, force_overwrite); | |
294 | get_device_topology(&xi->rt, &ft->rt, force_overwrite); | |
258dd18d | 295 | get_device_topology(&xi->log, &ft->log, force_overwrite); |
82c3a179 | 296 | } |