1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
6 #include "libfrog/util.h"
13 #include "err_protos.h"
14 #include "xfs_multidisk.h"
16 #define BSIZE (1024 * 1024)
19 * copy the fields of a superblock that are present in primary and
20 * secondaries -- preserve fields that are different in the primary.
23 copy_sb(xfs_sb_t
*source
, xfs_sb_t
*dest
)
34 rootino
= dest
->sb_rootino
;
35 rbmino
= dest
->sb_rbmino
;
36 rsumino
= dest
->sb_rsumino
;
37 uquotino
= dest
->sb_uquotino
;
38 gquotino
= dest
->sb_gquotino
;
39 pquotino
= dest
->sb_pquotino
;
40 metadirino
= dest
->sb_metadirino
;
42 versionnum
= dest
->sb_versionnum
;
46 dest
->sb_rootino
= rootino
;
47 dest
->sb_rbmino
= rbmino
;
48 dest
->sb_rsumino
= rsumino
;
49 dest
->sb_uquotino
= uquotino
;
50 dest
->sb_gquotino
= gquotino
;
51 dest
->sb_pquotino
= pquotino
;
52 dest
->sb_metadirino
= metadirino
;
54 dest
->sb_versionnum
= versionnum
;
57 * copy over version bits that are stamped into all
58 * secondaries and cannot be changed at run time in
59 * the primary superblock
61 if (xfs_sb_version_hasdalign(source
))
62 dest
->sb_versionnum
|= XFS_SB_VERSION_DALIGNBIT
;
63 dest
->sb_versionnum
|= XFS_SB_VERSION_EXTFLGBIT
;
66 * these are all supposed to be zero or will get reset anyway
70 dest
->sb_fdblocks
= 0;
71 dest
->sb_frextents
= 0;
73 memset(source
->sb_fname
, 0, 12);
77 verify_sb_blocksize(xfs_sb_t
*sb
)
79 /* check to make sure blocksize is legal 2^N, 9 <= N <= 16 */
80 if (sb
->sb_blocksize
== 0)
81 return XR_BAD_BLOCKSIZE
;
82 if (sb
->sb_blocklog
< XFS_MIN_BLOCKSIZE_LOG
||
83 sb
->sb_blocklog
> XFS_MAX_BLOCKSIZE_LOG
)
84 return XR_BAD_BLOCKLOG
;
85 if (sb
->sb_blocksize
!= (1 << sb
->sb_blocklog
))
86 return XR_BAD_BLOCKLOG
;
92 * find a secondary superblock, copy it into the sb buffer.
93 * start is the point to begin reading BSIZE bytes.
94 * skip contains a byte-count of how far to advance for next read.
112 sb
= (xfs_sb_t
*)memalign(libxfs_device_alignment(), BSIZE
);
115 _("error finding secondary superblock -- failed to memalign buffer\n"));
119 memset(&bufsb
, 0, sizeof(xfs_sb_t
));
125 * skip first sector since we know that's bad
127 for (done
= 0, off
= start
; !done
; off
+= skip
) {
129 * read disk 1 MByte at a time.
131 if (lseek(x
.data
.fd
, off
, SEEK_SET
) != off
)
134 if (!done
&& (bsize
= read(x
.data
.fd
, sb
, BSIZE
)) <= 0)
140 * check the buffer 512 bytes at a time since
141 * we don't know how big the sectors really are.
143 for (i
= 0; !done
&& i
< bsize
; i
+= BBSIZE
) {
144 c_bufsb
= (char *)sb
+ i
;
145 libxfs_sb_from_disk(&bufsb
, (struct xfs_dsb
*)c_bufsb
);
147 if (verify_sb(c_bufsb
, &bufsb
, 0) != XR_OK
)
150 do_warn(_("found candidate secondary superblock...\n"));
153 * found one. now verify it by looking
154 * for other secondaries.
156 memmove(rsb
, &bufsb
, sizeof(xfs_sb_t
));
157 rsb
->sb_inprogress
= 0;
160 if (verify_set_primary_sb(rsb
, 0, &dirty
) == XR_OK
) {
162 _("verified secondary superblock...\n"));
167 _("unable to verify superblock, continuing...\n"));
177 guess_default_geometry(
180 struct libxfs_init
*x
)
182 struct fs_topology ft
;
187 memset(&ft
, 0, sizeof(ft
));
188 get_topology(x
, &ft
, 1);
191 * get geometry from get_topology result.
192 * Use default block size (2^12)
195 multidisk
= ft
.data
.swidth
| ft
.data
.sunit
;
196 dblocks
= x
->data
.size
>> (blocklog
- BBSHIFT
);
197 calc_default_ag_geometry(blocklog
, dblocks
, multidisk
,
204 find_secondary_sb(xfs_sb_t
*rsb
)
213 * Attempt to find secondary sb with a coarse approach,
214 * first trying agblocks and blocksize read from sb, providing
217 do_warn(_("\nattempting to find secondary superblock...\n"));
219 if (verify_sb_blocksize(rsb
) == 0) {
220 skip
= (uint64_t)rsb
->sb_agblocks
* rsb
->sb_blocksize
;
221 if (skip
>= XFS_AG_MIN_BYTES
&& skip
<= XFS_AG_MAX_BYTES
)
222 retval
= __find_secondary_sb(rsb
, skip
, skip
);
225 /* If that failed, retry coarse approach, using default geometry */
227 blocklog
= guess_default_geometry(&agsize
, &agcount
, &x
);
228 skip
= agsize
<< blocklog
;
229 retval
= __find_secondary_sb(rsb
, skip
, skip
);
232 /* If that failed, fall back to the brute force method */
234 retval
= __find_secondary_sb(rsb
, XFS_AG_MIN_BYTES
, BSIZE
);
236 if (retval
&& xfs_sb_version_hasmetadir(rsb
))
237 do_warn(_("quota accounting and enforcement flags lost\n"));
243 * Calculate what the inode alignment field ought to be based on internal
244 * superblock info and determine if it is valid.
246 * For standard v5 superblocks, the inode alignment must either match
247 * XFS_INODE_BIG_CLUSTER_SIZE or a multiple based on the inode size. For v5
248 * superblocks with sparse inode chunks enabled, inode alignment must match the
251 * Return true if the alignment is valid, false otherwise.
254 sb_validate_ino_align(struct xfs_sb
*sb
)
258 if (!xfs_sb_version_hasalign(sb
))
261 /* standard cluster size alignment is always valid */
262 align
= XFS_INODE_BIG_CLUSTER_SIZE
>> sb
->sb_blocklog
;
263 if (align
== sb
->sb_inoalignmt
)
266 /* alignment scaled by inode size is v5 only for now */
267 if (!xfs_sb_version_hascrc(sb
))
270 align
= (XFS_INODE_BIG_CLUSTER_SIZE
*
271 sb
->sb_inodesize
/ XFS_DINODE_MIN_SIZE
) >> sb
->sb_blocklog
;
272 if (align
== sb
->sb_inoalignmt
)
276 * Sparse inodes requires inoalignmt to match full inode chunk size and
277 * spino_align to match the scaled alignment (as calculated above).
279 if (xfs_sb_version_hassparseinodes(sb
)) {
280 if (align
!= sb
->sb_spino_align
)
283 align
= (sb
->sb_inodesize
* XFS_INODES_PER_CHUNK
)
285 if (align
== sb
->sb_inoalignmt
)
293 * Validate the given log space. Derived from xfs_log_mount, though we
294 * can't validate the minimum log size until later. We only do this
295 * validation on V5 filesystems because the kernel doesn't reject malformed
296 * log geometry on older revision filesystems.
298 * Returns false if the log is garbage.
304 if (xfs_sb_version_hascrc(sb
) &&
305 (sb
->sb_logblocks
== 0 ||
306 sb
->sb_logblocks
> XFS_MAX_LOG_BLOCKS
||
307 ((unsigned long long)sb
->sb_logblocks
<< sb
->sb_blocklog
) >
311 if (sb
->sb_logsunit
> 1 && sb
->sb_logsunit
% sb
->sb_blocksize
)
323 if (sbp
->sb_rextsize
== 0)
324 return XR_BAD_RT_GEO_DATA
;
326 if (sbp
->sb_rgextents
> XFS_MAX_RGBLOCKS
/ sbp
->sb_rextsize
)
327 return XR_BAD_RT_GEO_DATA
;
329 if (sbp
->sb_rgextents
< XFS_MIN_RGEXTENTS
)
330 return XR_BAD_RT_GEO_DATA
;
332 if (sbp
->sb_rgcount
> XFS_MAX_RGNUMBER
)
333 return XR_BAD_RT_GEO_DATA
;
335 groups
= howmany_64(sbp
->sb_rextents
, sbp
->sb_rgextents
);
336 if (groups
!= sbp
->sb_rgcount
)
337 return XR_BAD_RT_GEO_DATA
;
339 if (!(sbp
->sb_features_incompat
& XFS_SB_FEAT_INCOMPAT_EXCHRANGE
))
340 return XR_BAD_RT_GEO_DATA
;
342 if (sbp
->sb_rgblklog
!= libxfs_compute_rgblklog(sbp
->sb_rgextents
,
344 return XR_BAD_RT_GEO_DATA
;
350 * verify a superblock -- does not verify root inode #
351 * can only check that geometry info is internally
352 * consistent. because of growfs, that's no guarantee
353 * of correctness (e.g. geometry may have changed)
355 * fields verified or consistency checked:
363 * sb_blocksize (as a group)
366 * geometry info - sb_dblocks (as a group)
371 * inode info - sb_inodesize (x-checked with geo info)
387 * ALL real-time fields
388 * final 4 summary counters
392 verify_sb(char *sb_buf
, xfs_sb_t
*sb
, int is_primary_sb
)
398 /* check magic number and version number */
400 if (sb
->sb_magicnum
!= XFS_SB_MAGIC
)
401 return(XR_BAD_MAGIC
);
403 if (!xfs_sb_good_version(sb
))
404 return(XR_BAD_VERSION
);
406 /* does sb think mkfs really finished ? */
407 if (is_primary_sb
&& sb
->sb_inprogress
)
408 return(XR_BAD_INPROGRESS
);
411 * before going *any further*, validate the sector size and if the
412 * version says we should have CRCs enabled, validate that.
415 /* check to make sure sectorsize is legal 2^N, 9 <= N <= 15 */
416 if (sb
->sb_sectsize
== 0)
417 return(XR_BAD_SECT_SIZE_DATA
);
420 for (i
= 0; bsize
< sb
->sb_sectsize
&&
421 i
< sizeof(sb
->sb_sectsize
) * NBBY
; i
++) {
425 if (i
< XFS_MIN_SECTORSIZE_LOG
|| i
> XFS_MAX_SECTORSIZE_LOG
)
426 return(XR_BAD_SECT_SIZE_DATA
);
428 /* check sb sectorsize field against sb sectlog field */
429 if (i
!= sb
->sb_sectlog
)
430 return(XR_BAD_SECT_SIZE_DATA
);
432 /* sector size in range - CRC check time */
433 if (xfs_sb_version_hascrc(sb
) &&
434 !libxfs_verify_cksum(sb_buf
, sb
->sb_sectsize
, XFS_SB_CRC_OFF
))
437 /* check to ensure blocksize and blocklog are legal */
438 ret
= verify_sb_blocksize(sb
);
442 /* sanity check ag count, size fields against data size field */
444 if (sb
->sb_dblocks
== 0 ||
445 sb
->sb_dblocks
> XFS_MAX_DBLOCKS(sb
) ||
446 sb
->sb_dblocks
< XFS_MIN_DBLOCKS(sb
))
447 return(XR_BAD_FS_SIZE_DATA
);
449 if (sb
->sb_agblklog
!= (uint8_t)log2_roundup(sb
->sb_agblocks
))
450 return(XR_BAD_FS_SIZE_DATA
);
452 if (sb
->sb_inodesize
< XFS_DINODE_MIN_SIZE
||
453 sb
->sb_inodesize
> XFS_DINODE_MAX_SIZE
||
454 sb
->sb_inodelog
< XFS_DINODE_MIN_LOG
||
455 sb
->sb_inodelog
> XFS_DINODE_MAX_LOG
||
456 sb
->sb_inodesize
!= (1 << sb
->sb_inodelog
) ||
457 sb
->sb_logsunit
> XLOG_MAX_RECORD_BSIZE
||
458 sb
->sb_inopblock
!= howmany(sb
->sb_blocksize
, sb
->sb_inodesize
) ||
459 (sb
->sb_blocklog
- sb
->sb_inodelog
!= sb
->sb_inopblog
))
460 return XR_BAD_INO_SIZE_DATA
;
462 if (!verify_sb_loginfo(sb
))
463 return XR_BAD_LOG_GEOMETRY
;
465 if (xfs_sb_version_hassector(sb
)) {
467 /* check to make sure log sector is legal 2^N, 9 <= N <= 15 */
469 if (sb
->sb_logsectsize
== 0)
470 return(XR_BAD_SECT_SIZE_DATA
);
474 for (i
= 0; bsize
< sb
->sb_logsectsize
&&
475 i
< sizeof(sb
->sb_logsectsize
) * NBBY
; i
++) {
479 if (i
< XFS_MIN_SECTORSIZE_LOG
|| i
> XFS_MAX_SECTORSIZE_LOG
)
480 return(XR_BAD_SECT_SIZE_DATA
);
482 /* check sb log sectorsize field against sb log sectlog field */
484 if (i
!= sb
->sb_logsectlog
)
485 return(XR_BAD_SECT_SIZE_DATA
);
488 if (!libxfs_validate_rt_geometry(sb
))
489 return XR_BAD_RT_GEO_DATA
;
492 * verify correctness of inode alignment if it's there
494 if (!sb_validate_ino_align(sb
))
495 return(XR_BAD_INO_ALIGN
);
498 * verify max. % of inodes (sb_imax_pct)
500 if (sb
->sb_imax_pct
> 100)
501 return(XR_BAD_INO_MAX_PCT
);
504 * verify stripe alignment fields if present
506 if (xfs_sb_version_hasdalign(sb
)) {
507 if ((!sb
->sb_unit
&& sb
->sb_width
) ||
508 (sb
->sb_unit
&& sb
->sb_agblocks
% sb
->sb_unit
))
509 return(XR_BAD_SB_UNIT
);
510 if ((sb
->sb_unit
&& !sb
->sb_width
) ||
511 (sb
->sb_width
&& sb
->sb_unit
&& sb
->sb_width
% sb
->sb_unit
))
512 return(XR_BAD_SB_WIDTH
);
513 } else if (sb
->sb_unit
|| sb
->sb_width
)
514 return XR_BAD_SB_WIDTH
;
516 /* Directory block log */
517 if (sb
->sb_blocklog
+ sb
->sb_dirblklog
> XFS_MAX_BLOCKSIZE_LOG
)
518 return XR_BAD_DIR_SIZE_DATA
;
520 if (xfs_sb_version_hasmetadir(sb
)) {
521 if (memchr_inv(sb
->sb_pad
, 0, sizeof(sb
->sb_pad
)))
522 return XR_SB_GEO_MISMATCH
;
524 ret
= verify_sb_rtgroups(sb
);
533 write_primary_sb(xfs_sb_t
*sbp
, int size
)
540 buf
= memalign(libxfs_device_alignment(), size
);
542 do_error(_("failed to memalign superblock buffer\n"));
545 memset(buf
, 0, size
);
547 if (lseek(x
.data
.fd
, 0LL, SEEK_SET
) != 0LL) {
549 do_error(_("couldn't seek to offset 0 in filesystem\n"));
552 libxfs_sb_to_disk(buf
, sbp
);
554 if (xfs_sb_version_hascrc(sbp
))
555 xfs_update_cksum((char *)buf
, size
, XFS_SB_CRC_OFF
);
557 if (write(x
.data
.fd
, buf
, size
) != size
) {
559 do_error(_("primary superblock write failed!\n"));
566 * get a possible superblock -- checks for internal consistency
569 get_sb(xfs_sb_t
*sbp
, xfs_off_t off
, int size
, xfs_agnumber_t agno
)
574 buf
= memalign(libxfs_device_alignment(), size
);
577 _("error reading superblock %u -- failed to memalign buffer\n"),
581 memset(buf
, 0, size
);
582 memset(sbp
, 0, sizeof(*sbp
));
584 /* try and read it first */
586 if (lseek(x
.data
.fd
, off
, SEEK_SET
) != off
) {
588 _("error reading superblock %u -- seek to offset %" PRId64
" failed\n"),
594 if ((rval
= read(x
.data
.fd
, buf
, size
)) != size
) {
597 _("superblock read failed, offset %" PRId64
", size %d, ag %u, rval %d\n"),
598 off
, size
, agno
, rval
);
599 do_error("%s\n", strerror(error
));
601 libxfs_sb_from_disk(sbp
, buf
);
603 rval
= verify_sb((char *)buf
, sbp
, agno
== 0);
608 /* returns element on list with highest reference count */
609 static fs_geo_list_t
*
610 get_best_geo(fs_geo_list_t
*list
)
613 fs_geo_list_t
*current
, *rval
= NULL
;
617 while (current
!= NULL
) {
618 if (current
->refs
> cnt
) {
622 current
= current
->next
;
628 /* adds geometry info to linked list. returns (sometimes new) head of list */
629 static fs_geo_list_t
*
630 add_geo(fs_geo_list_t
*list
, fs_geometry_t
*geo_p
, int index
)
632 fs_geo_list_t
*current
= list
;
634 while (current
!= NULL
) {
635 if (memcmp(geo_p
, ¤t
->geo
, sizeof(fs_geometry_t
)) == 0) {
640 current
= current
->next
;
643 if ((current
= malloc(sizeof(fs_geo_list_t
))) == NULL
) {
644 do_error(_("couldn't malloc geometry structure\n"));
648 current
->geo
= *geo_p
;
650 current
->next
= list
;
651 current
->index
= index
;
657 free_geo(fs_geo_list_t
*list
)
660 fs_geo_list_t
*current
;
662 for (current
= list
; current
!= NULL
; current
= next
) {
663 next
= current
->next
;
669 get_sb_geometry(fs_geometry_t
*geo
, xfs_sb_t
*sbp
)
671 memset(geo
, 0, sizeof(fs_geometry_t
));
674 * blindly set fields that we know are always good
676 geo
->sb_blocksize
= sbp
->sb_blocksize
;
677 geo
->sb_dblocks
= sbp
->sb_dblocks
;
678 geo
->sb_rblocks
= sbp
->sb_rblocks
;
679 geo
->sb_rextents
= sbp
->sb_rextents
;
680 geo
->sb_logstart
= sbp
->sb_logstart
;
681 geo
->sb_rextsize
= sbp
->sb_rextsize
;
682 geo
->sb_agblocks
= sbp
->sb_agblocks
;
683 geo
->sb_agcount
= sbp
->sb_agcount
;
684 geo
->sb_rbmblocks
= sbp
->sb_rbmblocks
;
685 geo
->sb_logblocks
= sbp
->sb_logblocks
;
686 geo
->sb_sectsize
= sbp
->sb_sectsize
;
687 geo
->sb_inodesize
= sbp
->sb_inodesize
;
689 if (xfs_sb_version_hasalign(sbp
))
690 geo
->sb_ialignbit
= 1;
692 if (xfs_sb_version_hasdalign(sbp
))
693 geo
->sb_salignbit
= 1;
695 geo
->sb_extflgbit
= 1;
696 geo
->sb_fully_zeroed
= 1;
700 * the way to verify that a primary sb is consistent with the
701 * filesystem is find the secondaries given the info in the
702 * primary and compare the geometries in the secondaries against
703 * the geometry indicated by the primary.
705 * returns 0 if ok, else error code (XR_EOF, XR_INSUFF_SEC_SB, etc).
708 verify_set_primary_sb(xfs_sb_t
*rsb
,
716 fs_geo_list_t
*current
;
724 * We haven't been able to validate the sector size yet properly
725 * (e.g. in the case of repairing an image in a file), so we need to
726 * take into account sector mismatches and so use the maximum possible
727 * sector size rather than the sector size in @rsb.
729 size
= NUM_AGH_SECTS
* (1 << (XFS_MAX_SECTORSIZE_LOG
));
733 num_sbs
= rsb
->sb_agcount
;
735 sb
= (xfs_sb_t
*) alloc_ag_buf(size
);
738 * put the primary sb geometry info onto the geometry list
740 get_sb_geometry(&geo
, rsb
);
741 list
= add_geo(list
, &geo
, sb_index
);
744 * scan the secondaries and check them off as we get them so we only
745 * process each one once
747 for (agno
= 1; agno
< rsb
->sb_agcount
; agno
++) {
748 off
= (xfs_off_t
)agno
* rsb
->sb_agblocks
<< rsb
->sb_blocklog
;
750 retval
= get_sb(sb
, off
, size
, agno
);
751 if (retval
== XR_EOF
)
754 if (retval
== XR_OK
) {
756 * save away geometry info. don't bother checking the
757 * sb against the agi/agf as the odds of the sb being
758 * corrupted in a way that it is internally consistent
759 * but not consistent with the rest of the filesystem is
762 get_sb_geometry(&geo
, sb
);
763 list
= add_geo(list
, &geo
, agno
);
769 * see if we have enough superblocks to bother with
772 if (num_ok
< num_sbs
/ 2) {
773 retval
= XR_INSUFF_SEC_SB
;
777 current
= get_best_geo(list
);
780 * check that enough sbs agree that we're willing to
781 * go with this geometry. if not, print out the
782 * geometry and a message about the force option.
787 * If we only have two allocation groups, and the superblock
788 * in the second allocation group differs from the primary
789 * superblock we can't verify the geometry information.
790 * Warn the user about this situation and get out unless
791 * explicitly overridden.
793 if (current
->refs
!= 2) {
796 _("Only two AGs detected and they do not match - "
797 "cannot validate filesystem geometry.\n"
798 "Use the -o force_geometry option to proceed.\n"));
805 * If we only have a single allocation group there is no
806 * secondary superblock that we can use to verify the geometry
807 * information. Warn the user about this situation and get
808 * out unless explicitly overridden.
812 _("Only one AG detected - "
813 "cannot validate filesystem geometry.\n"
814 "Use the -o force_geometry option to proceed.\n"));
820 * at least half of the probed superblocks have
821 * to agree. if they don't, this fs is probably
822 * too far gone anyway considering the fact that
823 * XFS normally doesn't alter the secondary superblocks.
825 if (current
->refs
< num_sbs
/ 2) {
827 _("Not enough matching superblocks - cannot proceed.\n"));
833 * set the geometry into primary superblock if necessary.
836 if (current
->index
!= sb_index
) {
838 off
= (xfs_off_t
)current
->index
* current
->geo
.sb_agblocks
839 * current
->geo
.sb_blocksize
;
840 if (get_sb(sb
, off
, current
->geo
.sb_sectsize
,
841 current
->index
) != XR_OK
)
842 do_error(_("could not read superblock\n"));
847 * turn off inprogress bit since this is the primary.
848 * also save away values that we need to ensure are
849 * consistent in the other secondaries.
851 rsb
->sb_inprogress
= 0;
852 sb_inoalignmt
= sb
->sb_inoalignmt
;
853 sb_unit
= sb
->sb_unit
;
854 sb_width
= sb
->sb_width
;