]>
Commit | Line | Data |
---|---|---|
959ef981 | 1 | // SPDX-License-Identifier: GPL-2.0 |
2bd0ea18 | 2 | /* |
da23017d NS |
3 | * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. |
4 | * All Rights Reserved. | |
2bd0ea18 | 5 | */ |
24f4f998 | 6 | #include "libfrog.h" |
6b803e5a | 7 | #include "libxfs.h" |
1b1b5359 | 8 | #include "libxcmd.h" |
6b803e5a | 9 | #include "libxlog.h" |
2bd0ea18 NS |
10 | #include "agheader.h" |
11 | #include "globals.h" | |
12 | #include "protos.h" | |
13 | #include "err_protos.h" | |
ef12e076 | 14 | #include "xfs_multidisk.h" |
2bd0ea18 | 15 | |
92217f0c NS |
16 | #define BSIZE (1024 * 1024) |
17 | ||
2bd0ea18 NS |
18 | /* |
19 | * copy the fields of a superblock that are present in primary and | |
20 | * secondaries -- preserve fields that are different in the primary. | |
21 | */ | |
8b8a6b02 | 22 | static void |
2bd0ea18 NS |
23 | copy_sb(xfs_sb_t *source, xfs_sb_t *dest) |
24 | { | |
25 | xfs_ino_t rootino; | |
26 | xfs_ino_t rbmino; | |
27 | xfs_ino_t rsumino; | |
28 | xfs_ino_t uquotino; | |
b36eef04 | 29 | xfs_ino_t gquotino; |
0340d706 | 30 | xfs_ino_t pquotino; |
14f8b681 | 31 | uint16_t versionnum; |
2bd0ea18 NS |
32 | |
33 | rootino = dest->sb_rootino; | |
34 | rbmino = dest->sb_rbmino; | |
35 | rsumino = dest->sb_rsumino; | |
36 | uquotino = dest->sb_uquotino; | |
b36eef04 | 37 | gquotino = dest->sb_gquotino; |
0340d706 | 38 | pquotino = dest->sb_pquotino; |
2bd0ea18 NS |
39 | |
40 | versionnum = dest->sb_versionnum; | |
41 | ||
42 | *dest = *source; | |
43 | ||
44 | dest->sb_rootino = rootino; | |
45 | dest->sb_rbmino = rbmino; | |
46 | dest->sb_rsumino = rsumino; | |
47 | dest->sb_uquotino = uquotino; | |
b36eef04 | 48 | dest->sb_gquotino = gquotino; |
0340d706 | 49 | dest->sb_pquotino = pquotino; |
2bd0ea18 NS |
50 | |
51 | dest->sb_versionnum = versionnum; | |
52 | ||
53 | /* | |
54 | * copy over version bits that are stamped into all | |
55 | * secondaries and cannot be changed at run time in | |
56 | * the primary superblock | |
57 | */ | |
5e656dbb BN |
58 | if (xfs_sb_version_hasdalign(source)) |
59 | dest->sb_versionnum |= XFS_SB_VERSION_DALIGNBIT; | |
b7fdeb4d | 60 | dest->sb_versionnum |= XFS_SB_VERSION_EXTFLGBIT; |
2bd0ea18 NS |
61 | |
62 | /* | |
63 | * these are all supposed to be zero or will get reset anyway | |
64 | */ | |
65 | dest->sb_icount = 0; | |
66 | dest->sb_ifree = 0; | |
67 | dest->sb_fdblocks = 0; | |
68 | dest->sb_frextents = 0; | |
69 | ||
dab9b8d6 | 70 | memset(source->sb_fname, 0, 12); |
2bd0ea18 NS |
71 | } |
72 | ||
00ff2b10 | 73 | static int |
ef12e076 BD |
74 | verify_sb_blocksize(xfs_sb_t *sb) |
75 | { | |
76 | /* check to make sure blocksize is legal 2^N, 9 <= N <= 16 */ | |
77 | if (sb->sb_blocksize == 0) | |
78 | return XR_BAD_BLOCKSIZE; | |
ef12e076 BD |
79 | if (sb->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG || |
80 | sb->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG) | |
81 | return XR_BAD_BLOCKLOG; | |
25f9772a DW |
82 | if (sb->sb_blocksize != (1 << sb->sb_blocklog)) |
83 | return XR_BAD_BLOCKLOG; | |
ef12e076 BD |
84 | |
85 | return 0; | |
86 | } | |
87 | ||
2bd0ea18 | 88 | /* |
1b1b5359 BD |
89 | * find a secondary superblock, copy it into the sb buffer. |
90 | * start is the point to begin reading BSIZE bytes. | |
91 | * skip contains a byte-count of how far to advance for next read. | |
2bd0ea18 | 92 | */ |
1b1b5359 BD |
93 | static int |
94 | __find_secondary_sb( | |
95 | xfs_sb_t *rsb, | |
14f8b681 DW |
96 | uint64_t start, |
97 | uint64_t skip) | |
2bd0ea18 NS |
98 | { |
99 | xfs_off_t off; | |
100 | xfs_sb_t *sb; | |
101 | xfs_sb_t bufsb; | |
102 | char *c_bufsb; | |
103 | int done; | |
104 | int i; | |
105 | int dirty; | |
106 | int retval; | |
107 | int bsize; | |
108 | ||
b74a1f6a | 109 | sb = (xfs_sb_t *)memalign(libxfs_device_alignment(), BSIZE); |
2bd0ea18 NS |
110 | if (!sb) { |
111 | do_error( | |
507f4e33 | 112 | _("error finding secondary superblock -- failed to memalign buffer\n")); |
2bd0ea18 NS |
113 | exit(1); |
114 | } | |
115 | ||
dab9b8d6 | 116 | memset(&bufsb, 0, sizeof(xfs_sb_t)); |
2bd0ea18 NS |
117 | retval = 0; |
118 | dirty = 0; | |
119 | bsize = 0; | |
120 | ||
121 | /* | |
122 | * skip first sector since we know that's bad | |
123 | */ | |
1b1b5359 | 124 | for (done = 0, off = start; !done ; off += skip) { |
2bd0ea18 NS |
125 | /* |
126 | * read disk 1 MByte at a time. | |
127 | */ | |
dc8878f4 | 128 | if (lseek(x.dfd, off, SEEK_SET) != off) { |
2bd0ea18 NS |
129 | done = 1; |
130 | } | |
131 | ||
2e4741ac | 132 | if (!done && (bsize = read(x.dfd, sb, BSIZE)) <= 0) { |
2bd0ea18 NS |
133 | done = 1; |
134 | } | |
135 | ||
136 | do_warn("."); | |
137 | ||
138 | /* | |
139 | * check the buffer 512 bytes at a time since | |
140 | * we don't know how big the sectors really are. | |
141 | */ | |
142 | for (i = 0; !done && i < bsize; i += BBSIZE) { | |
5e656dbb BN |
143 | c_bufsb = (char *)sb + i; |
144 | libxfs_sb_from_disk(&bufsb, (xfs_dsb_t *)c_bufsb); | |
2bd0ea18 | 145 | |
88f364a9 | 146 | if (verify_sb(c_bufsb, &bufsb, 0) != XR_OK) |
2bd0ea18 NS |
147 | continue; |
148 | ||
507f4e33 | 149 | do_warn(_("found candidate secondary superblock...\n")); |
2bd0ea18 NS |
150 | |
151 | /* | |
152 | * found one. now verify it by looking | |
153 | * for other secondaries. | |
154 | */ | |
dab9b8d6 | 155 | memmove(rsb, &bufsb, sizeof(xfs_sb_t)); |
2bd0ea18 | 156 | rsb->sb_inprogress = 0; |
6bf4721d | 157 | copied_sunit = 1; |
2bd0ea18 NS |
158 | |
159 | if (verify_set_primary_sb(rsb, 0, &dirty) == XR_OK) { | |
507f4e33 NS |
160 | do_warn( |
161 | _("verified secondary superblock...\n")); | |
2bd0ea18 NS |
162 | done = 1; |
163 | retval = 1; | |
164 | } else { | |
165 | do_warn( | |
507f4e33 | 166 | _("unable to verify superblock, continuing...\n")); |
2bd0ea18 NS |
167 | } |
168 | } | |
169 | } | |
170 | ||
171 | free(sb); | |
1b1b5359 BD |
172 | return retval; |
173 | } | |
174 | ||
175 | static int | |
176 | guess_default_geometry( | |
14f8b681 DW |
177 | uint64_t *agsize, |
178 | uint64_t *agcount, | |
1b1b5359 BD |
179 | libxfs_init_t *x) |
180 | { | |
181 | struct fs_topology ft; | |
182 | int blocklog; | |
14f8b681 | 183 | uint64_t dblocks; |
1b1b5359 BD |
184 | int multidisk; |
185 | ||
186 | memset(&ft, 0, sizeof(ft)); | |
187 | get_topology(x, &ft, 1); | |
188 | ||
189 | /* | |
190 | * get geometry from get_topology result. | |
191 | * Use default block size (2^12) | |
192 | */ | |
193 | blocklog = 12; | |
194 | multidisk = ft.dswidth | ft.dsunit; | |
195 | dblocks = x->dsize >> (blocklog - BBSHIFT); | |
196 | calc_default_ag_geometry(blocklog, dblocks, multidisk, | |
197 | agsize, agcount); | |
198 | ||
199 | return blocklog; | |
200 | } | |
201 | ||
202 | int | |
203 | find_secondary_sb(xfs_sb_t *rsb) | |
204 | { | |
ef12e076 | 205 | int retval = 0; |
14f8b681 DW |
206 | uint64_t agcount; |
207 | uint64_t agsize; | |
208 | uint64_t skip; | |
1b1b5359 BD |
209 | int blocklog; |
210 | ||
211 | /* | |
ef12e076 BD |
212 | * Attempt to find secondary sb with a coarse approach, |
213 | * first trying agblocks and blocksize read from sb, providing | |
214 | * they're sane. | |
1b1b5359 | 215 | */ |
ef12e076 | 216 | do_warn(_("\nattempting to find secondary superblock...\n")); |
1b1b5359 | 217 | |
ef12e076 | 218 | if (verify_sb_blocksize(rsb) == 0) { |
14f8b681 | 219 | skip = (uint64_t)rsb->sb_agblocks * rsb->sb_blocksize; |
ef12e076 BD |
220 | if (skip >= XFS_AG_MIN_BYTES && skip <= XFS_AG_MAX_BYTES) |
221 | retval = __find_secondary_sb(rsb, skip, skip); | |
1b1b5359 | 222 | } |
ef12e076 BD |
223 | |
224 | /* If that failed, retry coarse approach, using default geometry */ | |
225 | if (!retval) { | |
226 | blocklog = guess_default_geometry(&agsize, &agcount, &x); | |
227 | skip = agsize << blocklog; | |
228 | retval = __find_secondary_sb(rsb, skip, skip); | |
229 | } | |
230 | ||
231 | /* If that failed, fall back to the brute force method */ | |
232 | if (!retval) | |
233 | retval = __find_secondary_sb(rsb, XFS_AG_MIN_BYTES, BSIZE); | |
234 | ||
1b1b5359 | 235 | return retval; |
2bd0ea18 NS |
236 | } |
237 | ||
238 | /* | |
90c9ac9d BF |
239 | * Calculate what the inode alignment field ought to be based on internal |
240 | * superblock info and determine if it is valid. | |
7b5f9801 | 241 | * |
90c9ac9d BF |
242 | * For standard v5 superblocks, the inode alignment must either match |
243 | * XFS_INODE_BIG_CLUSTER_SIZE or a multiple based on the inode size. For v5 | |
244 | * superblocks with sparse inode chunks enabled, inode alignment must match the | |
245 | * inode chunk size. | |
7b5f9801 DC |
246 | * |
247 | * Return true if the alignment is valid, false otherwise. | |
2bd0ea18 | 248 | */ |
7b5f9801 DC |
249 | static bool |
250 | sb_validate_ino_align(struct xfs_sb *sb) | |
2bd0ea18 | 251 | { |
7b5f9801 | 252 | xfs_extlen_t align; |
2bd0ea18 | 253 | |
7b5f9801 DC |
254 | if (!xfs_sb_version_hasalign(sb)) |
255 | return true; | |
256 | ||
257 | /* standard cluster size alignment is always valid */ | |
2bd0ea18 | 258 | align = XFS_INODE_BIG_CLUSTER_SIZE >> sb->sb_blocklog; |
7b5f9801 DC |
259 | if (align == sb->sb_inoalignmt) |
260 | return true; | |
261 | ||
262 | /* alignment scaled by inode size is v5 only for now */ | |
263 | if (!xfs_sb_version_hascrc(sb)) | |
264 | return false; | |
2bd0ea18 | 265 | |
16e16276 BF |
266 | align = (XFS_INODE_BIG_CLUSTER_SIZE * |
267 | sb->sb_inodesize / XFS_DINODE_MIN_SIZE) >> sb->sb_blocklog; | |
7b5f9801 DC |
268 | if (align == sb->sb_inoalignmt) |
269 | return true; | |
270 | ||
90c9ac9d BF |
271 | /* |
272 | * Sparse inodes requires inoalignmt to match full inode chunk size and | |
273 | * spino_align to match the scaled alignment (as calculated above). | |
274 | */ | |
275 | if (xfs_sb_version_hassparseinodes(sb)) { | |
276 | if (align != sb->sb_spino_align) | |
277 | return false; | |
278 | ||
279 | align = (sb->sb_inodesize * XFS_INODES_PER_CHUNK) | |
280 | >> sb->sb_blocklog; | |
281 | if (align == sb->sb_inoalignmt) | |
282 | return true; | |
283 | } | |
284 | ||
7b5f9801 | 285 | return false; |
2bd0ea18 NS |
286 | } |
287 | ||
eb9cee60 DW |
288 | /* |
289 | * Validate the given log space. Derived from xfs_log_mount, though we | |
290 | * can't validate the minimum log size until later. We only do this | |
291 | * validation on V5 filesystems because the kernel doesn't reject malformed | |
292 | * log geometry on older revision filesystems. | |
293 | * | |
294 | * Returns false if the log is garbage. | |
295 | */ | |
296 | static bool | |
297 | verify_sb_loginfo( | |
298 | struct xfs_sb *sb) | |
299 | { | |
300 | if (xfs_sb_version_hascrc(sb) && | |
301 | (sb->sb_logblocks == 0 || | |
302 | sb->sb_logblocks > XFS_MAX_LOG_BLOCKS || | |
7931f861 DW |
303 | ((unsigned long long)sb->sb_logblocks << sb->sb_blocklog) > |
304 | XFS_MAX_LOG_BYTES)) | |
eb9cee60 DW |
305 | return false; |
306 | ||
307 | if (sb->sb_logsunit > 1 && sb->sb_logsunit % sb->sb_blocksize) | |
308 | return false; | |
309 | ||
310 | return true; | |
311 | } | |
312 | ||
2bd0ea18 NS |
313 | /* |
314 | * verify a superblock -- does not verify root inode # | |
315 | * can only check that geometry info is internally | |
316 | * consistent. because of growfs, that's no guarantee | |
317 | * of correctness (e.g. geometry may have changed) | |
318 | * | |
319 | * fields verified or consistency checked: | |
320 | * | |
321 | * sb_magicnum | |
322 | * | |
323 | * sb_versionnum | |
324 | * | |
325 | * sb_inprogress | |
326 | * | |
327 | * sb_blocksize (as a group) | |
328 | * sb_blocklog | |
329 | * | |
330 | * geometry info - sb_dblocks (as a group) | |
331 | * sb_agcount | |
332 | * sb_agblocks | |
333 | * sb_agblklog | |
334 | * | |
335 | * inode info - sb_inodesize (x-checked with geo info) | |
336 | * sb_inopblock | |
337 | * | |
338 | * sector size info - | |
339 | * sb_sectsize | |
340 | * sb_sectlog | |
3cc4d0db NS |
341 | * sb_logsectsize |
342 | * sb_logsectlog | |
2bd0ea18 NS |
343 | * |
344 | * not checked here - | |
345 | * sb_rootino | |
346 | * sb_fname | |
347 | * sb_fpack | |
348 | * sb_logstart | |
349 | * sb_uuid | |
350 | * | |
351 | * ALL real-time fields | |
352 | * final 4 summary counters | |
353 | */ | |
354 | ||
355 | int | |
88f364a9 | 356 | verify_sb(char *sb_buf, xfs_sb_t *sb, int is_primary_sb) |
2bd0ea18 | 357 | { |
14f8b681 | 358 | uint32_t bsize; |
2bd0ea18 | 359 | int i; |
ef12e076 | 360 | int ret; |
dfc130f3 | 361 | |
2bd0ea18 NS |
362 | /* check magic number and version number */ |
363 | ||
364 | if (sb->sb_magicnum != XFS_SB_MAGIC) | |
365 | return(XR_BAD_MAGIC); | |
366 | ||
5e656dbb | 367 | if (!xfs_sb_good_version(sb)) |
2bd0ea18 NS |
368 | return(XR_BAD_VERSION); |
369 | ||
370 | /* does sb think mkfs really finished ? */ | |
371 | ||
372 | if (is_primary_sb && sb->sb_inprogress == 1) | |
373 | return(XR_BAD_INPROGRESS); | |
374 | ||
88f364a9 DC |
375 | /* |
376 | * before going *any further*, validate the sector size and if the | |
377 | * version says we should have CRCs enabled, validate that. | |
378 | */ | |
379 | ||
380 | /* check to make sure sectorsize is legal 2^N, 9 <= N <= 15 */ | |
381 | if (sb->sb_sectsize == 0) | |
382 | return(XR_BAD_SECT_SIZE_DATA); | |
383 | ||
384 | bsize = 1; | |
385 | for (i = 0; bsize < sb->sb_sectsize && | |
386 | i < sizeof(sb->sb_sectsize) * NBBY; i++) { | |
387 | bsize <<= 1; | |
388 | } | |
389 | ||
390 | if (i < XFS_MIN_SECTORSIZE_LOG || i > XFS_MAX_SECTORSIZE_LOG) | |
391 | return(XR_BAD_SECT_SIZE_DATA); | |
392 | ||
393 | /* check sb sectorsize field against sb sectlog field */ | |
394 | if (i != sb->sb_sectlog) | |
395 | return(XR_BAD_SECT_SIZE_DATA); | |
396 | ||
397 | /* sector size in range - CRC check time */ | |
398 | if (xfs_sb_version_hascrc(sb) && | |
e2f60652 | 399 | !libxfs_verify_cksum(sb_buf, sb->sb_sectsize, XFS_SB_CRC_OFF)) |
88f364a9 | 400 | return XR_BAD_CRC; |
2bd0ea18 | 401 | |
ef12e076 BD |
402 | /* check to ensure blocksize and blocklog are legal */ |
403 | ret = verify_sb_blocksize(sb); | |
404 | if (ret != 0) | |
405 | return ret; | |
2bd0ea18 NS |
406 | |
407 | /* sanity check ag count, size fields against data size field */ | |
408 | ||
409 | if (sb->sb_dblocks == 0 || | |
02b56f87 DW |
410 | sb->sb_dblocks > XFS_MAX_DBLOCKS(sb) || |
411 | sb->sb_dblocks < XFS_MIN_DBLOCKS(sb)) | |
2bd0ea18 NS |
412 | return(XR_BAD_FS_SIZE_DATA); |
413 | ||
24f4f998 | 414 | if (sb->sb_agblklog != (uint8_t)log2_roundup(sb->sb_agblocks)) |
2bd0ea18 NS |
415 | return(XR_BAD_FS_SIZE_DATA); |
416 | ||
02b56f87 DW |
417 | if (sb->sb_inodesize < XFS_DINODE_MIN_SIZE || |
418 | sb->sb_inodesize > XFS_DINODE_MAX_SIZE || | |
419 | sb->sb_inodelog < XFS_DINODE_MIN_LOG || | |
420 | sb->sb_inodelog > XFS_DINODE_MAX_LOG || | |
421 | sb->sb_inodesize != (1 << sb->sb_inodelog) || | |
422 | sb->sb_logsunit > XLOG_MAX_RECORD_BSIZE || | |
423 | sb->sb_inopblock != howmany(sb->sb_blocksize, sb->sb_inodesize) || | |
424 | (sb->sb_blocklog - sb->sb_inodelog != sb->sb_inopblog)) | |
425 | return XR_BAD_INO_SIZE_DATA; | |
2bd0ea18 | 426 | |
eb9cee60 DW |
427 | if (!verify_sb_loginfo(sb)) |
428 | return XR_BAD_LOG_GEOMETRY; | |
429 | ||
5e656dbb | 430 | if (xfs_sb_version_hassector(sb)) { |
3cc4d0db NS |
431 | |
432 | /* check to make sure log sector is legal 2^N, 9 <= N <= 15 */ | |
433 | ||
434 | if (sb->sb_logsectsize == 0) | |
435 | return(XR_BAD_SECT_SIZE_DATA); | |
436 | ||
437 | bsize = 1; | |
438 | ||
439 | for (i = 0; bsize < sb->sb_logsectsize && | |
440 | i < sizeof(sb->sb_logsectsize) * NBBY; i++) { | |
441 | bsize <<= 1; | |
442 | } | |
443 | ||
444 | if (i < XFS_MIN_SECTORSIZE_LOG || i > XFS_MAX_SECTORSIZE_LOG) | |
445 | return(XR_BAD_SECT_SIZE_DATA); | |
446 | ||
447 | /* check sb log sectorsize field against sb log sectlog field */ | |
448 | ||
449 | if (i != sb->sb_logsectlog) | |
450 | return(XR_BAD_SECT_SIZE_DATA); | |
451 | } | |
452 | ||
2bd0ea18 NS |
453 | /* |
454 | * real-time extent size is always set | |
455 | */ | |
456 | if (sb->sb_rextsize * sb->sb_blocksize > XFS_MAX_RTEXTSIZE) | |
457 | return(XR_BAD_RT_GEO_DATA); | |
458 | ||
459 | if (sb->sb_rextsize * sb->sb_blocksize < XFS_MIN_RTEXTSIZE) | |
460 | return(XR_BAD_RT_GEO_DATA); | |
461 | ||
462 | if (sb->sb_rblocks == 0) { | |
463 | if (sb->sb_rextents != 0) | |
464 | return(XR_BAD_RT_GEO_DATA); | |
465 | ||
466 | if (sb->sb_rbmblocks != 0) | |
467 | return(XR_BAD_RT_GEO_DATA); | |
468 | ||
469 | if (sb->sb_rextslog != 0) | |
470 | return(XR_BAD_RT_GEO_DATA); | |
471 | ||
472 | if (sb->sb_frextents != 0) | |
473 | return(XR_BAD_RT_GEO_DATA); | |
474 | } else { | |
475 | /* | |
476 | * if we have a real-time partition, sanity-check geometry | |
477 | */ | |
478 | if (sb->sb_rblocks / sb->sb_rextsize != sb->sb_rextents) | |
479 | return(XR_BAD_RT_GEO_DATA); | |
480 | ||
481 | if (sb->sb_rextslog != | |
482 | libxfs_highbit32((unsigned int)sb->sb_rextents)) | |
483 | return(XR_BAD_RT_GEO_DATA); | |
484 | ||
485 | if (sb->sb_rbmblocks != (xfs_extlen_t) howmany(sb->sb_rextents, | |
486 | NBBY * sb->sb_blocksize)) | |
487 | return(XR_BAD_RT_GEO_DATA); | |
488 | } | |
489 | ||
490 | /* | |
491 | * verify correctness of inode alignment if it's there | |
492 | */ | |
7b5f9801 DC |
493 | if (!sb_validate_ino_align(sb)) |
494 | return(XR_BAD_INO_ALIGN); | |
2bd0ea18 NS |
495 | |
496 | /* | |
497 | * verify max. % of inodes (sb_imax_pct) | |
498 | */ | |
499 | if (sb->sb_imax_pct > 100) | |
500 | return(XR_BAD_INO_MAX_PCT); | |
501 | ||
502 | /* | |
503 | * verify stripe alignment fields if present | |
504 | */ | |
5e656dbb | 505 | if (xfs_sb_version_hasdalign(sb)) { |
dfc130f3 RC |
506 | if ((!sb->sb_unit && sb->sb_width) || |
507 | (sb->sb_unit && sb->sb_agblocks % sb->sb_unit)) | |
2bd0ea18 NS |
508 | return(XR_BAD_SB_UNIT); |
509 | if ((sb->sb_unit && !sb->sb_width) || | |
510 | (sb->sb_width && sb->sb_unit && sb->sb_width % sb->sb_unit)) | |
511 | return(XR_BAD_SB_WIDTH); | |
02b56f87 DW |
512 | } else if (sb->sb_unit || sb->sb_width) |
513 | return XR_BAD_SB_WIDTH; | |
514 | ||
515 | /* Directory block log */ | |
516 | if (sb->sb_blocklog + sb->sb_dirblklog > XFS_MAX_BLOCKSIZE_LOG) | |
517 | return XR_BAD_DIR_SIZE_DATA; | |
2bd0ea18 | 518 | |
2bd0ea18 NS |
519 | return(XR_OK); |
520 | } | |
521 | ||
522 | void | |
523 | write_primary_sb(xfs_sb_t *sbp, int size) | |
524 | { | |
5e656dbb | 525 | xfs_dsb_t *buf; |
dfc130f3 | 526 | |
2bd0ea18 NS |
527 | if (no_modify) |
528 | return; | |
dfc130f3 | 529 | |
5e656dbb BN |
530 | buf = memalign(libxfs_device_alignment(), size); |
531 | if (buf == NULL) { | |
b74a1f6a | 532 | do_error(_("failed to memalign superblock buffer\n")); |
dfc130f3 | 533 | return; |
2bd0ea18 | 534 | } |
b74a1f6a | 535 | memset(buf, 0, size); |
2bd0ea18 | 536 | |
dc8878f4 | 537 | if (lseek(x.dfd, 0LL, SEEK_SET) != 0LL) { |
dfc130f3 | 538 | free(buf); |
507f4e33 | 539 | do_error(_("couldn't seek to offset 0 in filesystem\n")); |
dfc130f3 RC |
540 | } |
541 | ||
19ebedcf | 542 | libxfs_sb_to_disk(buf, sbp); |
2bd0ea18 | 543 | |
88f364a9 DC |
544 | if (xfs_sb_version_hascrc(sbp)) |
545 | xfs_update_cksum((char *)buf, size, XFS_SB_CRC_OFF); | |
546 | ||
b74a1f6a | 547 | if (write(x.dfd, buf, size) != size) { |
dfc130f3 | 548 | free(buf); |
507f4e33 | 549 | do_error(_("primary superblock write failed!\n")); |
dfc130f3 | 550 | } |
2bd0ea18 | 551 | |
dfc130f3 | 552 | free(buf); |
2bd0ea18 NS |
553 | } |
554 | ||
555 | /* | |
88f364a9 | 556 | * get a possible superblock -- checks for internal consistency |
2bd0ea18 NS |
557 | */ |
558 | int | |
559 | get_sb(xfs_sb_t *sbp, xfs_off_t off, int size, xfs_agnumber_t agno) | |
560 | { | |
561 | int error, rval; | |
5e656dbb | 562 | xfs_dsb_t *buf; |
dfc130f3 | 563 | |
5e656dbb BN |
564 | buf = memalign(libxfs_device_alignment(), size); |
565 | if (buf == NULL) { | |
2bd0ea18 | 566 | do_error( |
b74a1f6a | 567 | _("error reading superblock %u -- failed to memalign buffer\n"), |
5d1b7f0f | 568 | agno); |
2bd0ea18 NS |
569 | exit(1); |
570 | } | |
b74a1f6a | 571 | memset(buf, 0, size); |
419d647d | 572 | memset(sbp, 0, sizeof(*sbp)); |
2bd0ea18 NS |
573 | |
574 | /* try and read it first */ | |
575 | ||
dc8878f4 | 576 | if (lseek(x.dfd, off, SEEK_SET) != off) { |
2bd0ea18 | 577 | do_warn( |
5d1b7f0f | 578 | _("error reading superblock %u -- seek to offset %" PRId64 " failed\n"), |
2bd0ea18 | 579 | agno, off); |
70ee4153 | 580 | free(buf); |
2bd0ea18 NS |
581 | return(XR_EOF); |
582 | } | |
583 | ||
b74a1f6a | 584 | if ((rval = read(x.dfd, buf, size)) != size) { |
2bd0ea18 NS |
585 | error = errno; |
586 | do_warn( | |
5d1b7f0f | 587 | _("superblock read failed, offset %" PRId64 ", size %d, ag %u, rval %d\n"), |
b74a1f6a | 588 | off, size, agno, rval); |
2bd0ea18 NS |
589 | do_error("%s\n", strerror(error)); |
590 | } | |
5e656dbb | 591 | libxfs_sb_from_disk(sbp, buf); |
2bd0ea18 | 592 | |
88f364a9 DC |
593 | rval = verify_sb((char *)buf, sbp, agno == 0); |
594 | free(buf); | |
595 | return rval; | |
2bd0ea18 NS |
596 | } |
597 | ||
2bd0ea18 | 598 | /* returns element on list with highest reference count */ |
8b8a6b02 | 599 | static fs_geo_list_t * |
2bd0ea18 NS |
600 | get_best_geo(fs_geo_list_t *list) |
601 | { | |
602 | int cnt = 0; | |
603 | fs_geo_list_t *current, *rval = NULL; | |
604 | ||
605 | current = list; | |
606 | ||
607 | while (current != NULL) { | |
608 | if (current->refs > cnt) { | |
609 | rval = current; | |
610 | cnt = current->refs; | |
611 | } | |
612 | current = current->next; | |
613 | } | |
614 | ||
615 | return(rval); | |
616 | } | |
617 | ||
618 | /* adds geometry info to linked list. returns (sometimes new) head of list */ | |
8b8a6b02 | 619 | static fs_geo_list_t * |
2bd0ea18 NS |
620 | add_geo(fs_geo_list_t *list, fs_geometry_t *geo_p, int index) |
621 | { | |
622 | fs_geo_list_t *current = list; | |
dfc130f3 | 623 | |
2bd0ea18 NS |
624 | while (current != NULL) { |
625 | if (memcmp(geo_p, ¤t->geo, sizeof(fs_geometry_t)) == 0) { | |
626 | current->refs++; | |
627 | return(list); | |
628 | } | |
629 | ||
630 | current = current->next; | |
631 | } | |
632 | ||
633 | if ((current = malloc(sizeof(fs_geo_list_t))) == NULL) { | |
507f4e33 | 634 | do_error(_("couldn't malloc geometry structure\n")); |
2bd0ea18 NS |
635 | exit(1); |
636 | } | |
637 | ||
638 | current->geo = *geo_p; | |
639 | current->refs = 1; | |
640 | current->next = list; | |
641 | current->index = index; | |
642 | ||
643 | return(current); | |
644 | } | |
645 | ||
8b8a6b02 | 646 | static void |
2bd0ea18 NS |
647 | free_geo(fs_geo_list_t *list) |
648 | { | |
649 | fs_geo_list_t *next; | |
650 | fs_geo_list_t *current; | |
651 | ||
2bd0ea18 NS |
652 | for (current = list; current != NULL; current = next) { |
653 | next = current->next; | |
654 | free(current); | |
655 | } | |
656 | } | |
657 | ||
658 | void | |
659 | get_sb_geometry(fs_geometry_t *geo, xfs_sb_t *sbp) | |
660 | { | |
dab9b8d6 | 661 | memset(geo, 0, sizeof(fs_geometry_t)); |
2bd0ea18 NS |
662 | |
663 | /* | |
664 | * blindly set fields that we know are always good | |
665 | */ | |
666 | geo->sb_blocksize = sbp->sb_blocksize; | |
667 | geo->sb_dblocks = sbp->sb_dblocks; | |
668 | geo->sb_rblocks = sbp->sb_rblocks; | |
669 | geo->sb_rextents = sbp->sb_rextents; | |
670 | geo->sb_logstart = sbp->sb_logstart; | |
671 | geo->sb_rextsize = sbp->sb_rextsize; | |
672 | geo->sb_agblocks = sbp->sb_agblocks; | |
673 | geo->sb_agcount = sbp->sb_agcount; | |
674 | geo->sb_rbmblocks = sbp->sb_rbmblocks; | |
675 | geo->sb_logblocks = sbp->sb_logblocks; | |
676 | geo->sb_sectsize = sbp->sb_sectsize; | |
677 | geo->sb_inodesize = sbp->sb_inodesize; | |
678 | ||
5e656dbb | 679 | if (xfs_sb_version_hasalign(sbp)) |
2bd0ea18 NS |
680 | geo->sb_ialignbit = 1; |
681 | ||
5e656dbb | 682 | if (xfs_sb_version_hasdalign(sbp)) |
2bd0ea18 NS |
683 | geo->sb_salignbit = 1; |
684 | ||
b7fdeb4d | 685 | geo->sb_extflgbit = 1; |
5f6f3660 | 686 | geo->sb_fully_zeroed = 1; |
2bd0ea18 NS |
687 | } |
688 | ||
689 | /* | |
690 | * the way to verify that a primary sb is consistent with the | |
691 | * filesystem is find the secondaries given the info in the | |
692 | * primary and compare the geometries in the secondaries against | |
693 | * the geometry indicated by the primary. | |
694 | * | |
ae181820 | 695 | * returns 0 if ok, else error code (XR_EOF, XR_INSUFF_SEC_SB, etc). |
2bd0ea18 NS |
696 | */ |
697 | int | |
698 | verify_set_primary_sb(xfs_sb_t *rsb, | |
699 | int sb_index, | |
700 | int *sb_modified) | |
701 | { | |
507f4e33 | 702 | xfs_off_t off; |
2bd0ea18 NS |
703 | fs_geometry_t geo; |
704 | xfs_sb_t *sb; | |
705 | fs_geo_list_t *list; | |
706 | fs_geo_list_t *current; | |
2bd0ea18 NS |
707 | xfs_agnumber_t agno; |
708 | int num_sbs; | |
2bd0ea18 NS |
709 | int size; |
710 | int num_ok; | |
711 | int retval; | |
f63fd268 DC |
712 | |
713 | /* | |
714 | * We haven't been able to validate the sector size yet properly | |
715 | * (e.g. in the case of repairing an image in a file), so we need to | |
716 | * take into account sector mismatches and so use the maximum possible | |
717 | * sector size rather than the sector size in @rsb. | |
718 | */ | |
719 | size = NUM_AGH_SECTS * (1 << (XFS_MAX_SECTORSIZE_LOG)); | |
2bd0ea18 NS |
720 | list = NULL; |
721 | num_ok = 0; | |
722 | *sb_modified = 0; | |
8216a58f | 723 | num_sbs = rsb->sb_agcount; |
2bd0ea18 NS |
724 | |
725 | sb = (xfs_sb_t *) alloc_ag_buf(size); | |
2bd0ea18 NS |
726 | |
727 | /* | |
728 | * put the primary sb geometry info onto the geometry list | |
729 | */ | |
2bd0ea18 NS |
730 | get_sb_geometry(&geo, rsb); |
731 | list = add_geo(list, &geo, sb_index); | |
732 | ||
733 | /* | |
8216a58f BF |
734 | * scan the secondaries and check them off as we get them so we only |
735 | * process each one once | |
2bd0ea18 | 736 | */ |
8216a58f BF |
737 | for (agno = 1; agno < rsb->sb_agcount; agno++) { |
738 | off = (xfs_off_t)agno * rsb->sb_agblocks << rsb->sb_blocklog; | |
2bd0ea18 | 739 | |
8216a58f BF |
740 | retval = get_sb(sb, off, size, agno); |
741 | if (retval == XR_EOF) | |
742 | goto out_free_list; | |
743 | ||
744 | if (retval == XR_OK) { | |
745 | /* | |
746 | * save away geometry info. don't bother checking the | |
747 | * sb against the agi/agf as the odds of the sb being | |
748 | * corrupted in a way that it is internally consistent | |
749 | * but not consistent with the rest of the filesystem is | |
750 | * really really low. | |
751 | */ | |
752 | get_sb_geometry(&geo, sb); | |
753 | list = add_geo(list, &geo, agno); | |
754 | num_ok++; | |
2bd0ea18 NS |
755 | } |
756 | } | |
757 | ||
758 | /* | |
759 | * see if we have enough superblocks to bother with | |
760 | */ | |
6d23d9a5 | 761 | retval = 0; |
548c2e3e LZ |
762 | if (num_ok < num_sbs / 2) { |
763 | retval = XR_INSUFF_SEC_SB; | |
764 | goto out_free_list; | |
765 | } | |
2bd0ea18 NS |
766 | |
767 | current = get_best_geo(list); | |
768 | ||
769 | /* | |
770 | * check that enough sbs agree that we're willing to | |
771 | * go with this geometry. if not, print out the | |
772 | * geometry and a message about the force option. | |
773 | */ | |
774 | switch (num_sbs) { | |
775 | case 2: | |
776 | /* | |
d4dd6ab5 CH |
777 | * If we only have two allocation groups, and the superblock |
778 | * in the second allocation group differs from the primary | |
779 | * superblock we can't verify the geometry information. | |
780 | * Warn the user about this situation and get out unless | |
781 | * explicitly overridden. | |
2bd0ea18 NS |
782 | */ |
783 | if (current->refs != 2) { | |
784 | if (!force_geo) { | |
507f4e33 | 785 | do_warn( |
d4dd6ab5 CH |
786 | _("Only two AGs detected and they do not match - " |
787 | "cannot validate filesystem geometry.\n" | |
788 | "Use the -o force_geometry option to proceed.\n")); | |
2bd0ea18 NS |
789 | exit(1); |
790 | } | |
791 | } | |
d4dd6ab5 | 792 | goto out_free_list; |
2bd0ea18 NS |
793 | case 1: |
794 | /* | |
d4dd6ab5 CH |
795 | * If we only have a single allocation group there is no |
796 | * secondary superblock that we can use to verify the geometry | |
797 | * information. Warn the user about this situation and get | |
798 | * out unless explicitly overridden. | |
2bd0ea18 NS |
799 | */ |
800 | if (!force_geo) { | |
d4dd6ab5 CH |
801 | do_warn( |
802 | _("Only one AG detected - " | |
803 | "cannot validate filesystem geometry.\n" | |
804 | "Use the -o force_geometry option to proceed.\n")); | |
2bd0ea18 NS |
805 | exit(1); |
806 | } | |
d4dd6ab5 | 807 | goto out_free_list; |
2bd0ea18 NS |
808 | default: |
809 | /* | |
810 | * at least half of the probed superblocks have | |
811 | * to agree. if they don't, this fs is probably | |
812 | * too far gone anyway considering the fact that | |
813 | * XFS normally doesn't alter the secondary superblocks. | |
814 | */ | |
815 | if (current->refs < num_sbs / 2) { | |
507f4e33 NS |
816 | do_warn( |
817 | _("Not enough matching superblocks - cannot proceed.\n")); | |
2bd0ea18 NS |
818 | exit(1); |
819 | } | |
820 | } | |
821 | ||
822 | /* | |
823 | * set the geometry into primary superblock if necessary. | |
824 | */ | |
825 | ||
826 | if (current->index != sb_index) { | |
827 | *sb_modified = 1; | |
41f361f8 | 828 | off = (xfs_off_t)current->index * current->geo.sb_agblocks |
2bd0ea18 NS |
829 | * current->geo.sb_blocksize; |
830 | if (get_sb(sb, off, current->geo.sb_sectsize, | |
831 | current->index) != XR_OK) | |
507f4e33 | 832 | do_error(_("could not read superblock\n")); |
2bd0ea18 NS |
833 | |
834 | copy_sb(sb, rsb); | |
835 | ||
836 | /* | |
837 | * turn off inprogress bit since this is the primary. | |
838 | * also save away values that we need to ensure are | |
839 | * consistent in the other secondaries. | |
840 | */ | |
841 | rsb->sb_inprogress = 0; | |
842 | sb_inoalignmt = sb->sb_inoalignmt; | |
843 | sb_unit = sb->sb_unit; | |
844 | sb_width = sb->sb_width; | |
845 | } | |
846 | ||
d4dd6ab5 | 847 | out_free_list: |
2bd0ea18 | 848 | free_geo(list); |
2bd0ea18 | 849 | free(sb); |
6d23d9a5 | 850 | return retval; |
2bd0ea18 | 851 | } |