2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "err_protos.h"
26 #include "xfs_multidisk.h"
28 #define BSIZE (1024 * 1024)
31 * copy the fields of a superblock that are present in primary and
32 * secondaries -- preserve fields that are different in the primary.
35 copy_sb(xfs_sb_t
*source
, xfs_sb_t
*dest
)
45 rootino
= dest
->sb_rootino
;
46 rbmino
= dest
->sb_rbmino
;
47 rsumino
= dest
->sb_rsumino
;
48 uquotino
= dest
->sb_uquotino
;
49 gquotino
= dest
->sb_gquotino
;
50 pquotino
= dest
->sb_pquotino
;
52 versionnum
= dest
->sb_versionnum
;
56 dest
->sb_rootino
= rootino
;
57 dest
->sb_rbmino
= rbmino
;
58 dest
->sb_rsumino
= rsumino
;
59 dest
->sb_uquotino
= uquotino
;
60 dest
->sb_gquotino
= gquotino
;
61 dest
->sb_pquotino
= pquotino
;
63 dest
->sb_versionnum
= versionnum
;
66 * copy over version bits that are stamped into all
67 * secondaries and cannot be changed at run time in
68 * the primary superblock
70 if (xfs_sb_version_hasdalign(source
))
71 dest
->sb_versionnum
|= XFS_SB_VERSION_DALIGNBIT
;
72 if (xfs_sb_version_hasextflgbit(source
))
73 dest
->sb_versionnum
|= XFS_SB_VERSION_EXTFLGBIT
;
76 * these are all supposed to be zero or will get reset anyway
80 dest
->sb_fdblocks
= 0;
81 dest
->sb_frextents
= 0;
83 memset(source
->sb_fname
, 0, 12);
87 verify_sb_blocksize(xfs_sb_t
*sb
)
89 /* check to make sure blocksize is legal 2^N, 9 <= N <= 16 */
90 if (sb
->sb_blocksize
== 0)
91 return XR_BAD_BLOCKSIZE
;
92 if (sb
->sb_blocklog
< XFS_MIN_BLOCKSIZE_LOG
||
93 sb
->sb_blocklog
> XFS_MAX_BLOCKSIZE_LOG
)
94 return XR_BAD_BLOCKLOG
;
95 if (sb
->sb_blocksize
!= (1 << sb
->sb_blocklog
))
96 return XR_BAD_BLOCKLOG
;
102 * find a secondary superblock, copy it into the sb buffer.
103 * start is the point to begin reading BSIZE bytes.
104 * skip contains a byte-count of how far to advance for next read.
122 sb
= (xfs_sb_t
*)memalign(libxfs_device_alignment(), BSIZE
);
125 _("error finding secondary superblock -- failed to memalign buffer\n"));
129 memset(&bufsb
, 0, sizeof(xfs_sb_t
));
135 * skip first sector since we know that's bad
137 for (done
= 0, off
= start
; !done
; off
+= skip
) {
139 * read disk 1 MByte at a time.
141 if (lseek(x
.dfd
, off
, SEEK_SET
) != off
) {
145 if (!done
&& (bsize
= read(x
.dfd
, sb
, BSIZE
)) <= 0) {
152 * check the buffer 512 bytes at a time since
153 * we don't know how big the sectors really are.
155 for (i
= 0; !done
&& i
< bsize
; i
+= BBSIZE
) {
156 c_bufsb
= (char *)sb
+ i
;
157 libxfs_sb_from_disk(&bufsb
, (xfs_dsb_t
*)c_bufsb
);
159 if (verify_sb(c_bufsb
, &bufsb
, 0) != XR_OK
)
162 do_warn(_("found candidate secondary superblock...\n"));
165 * found one. now verify it by looking
166 * for other secondaries.
168 memmove(rsb
, &bufsb
, sizeof(xfs_sb_t
));
169 rsb
->sb_inprogress
= 0;
172 if (verify_set_primary_sb(rsb
, 0, &dirty
) == XR_OK
) {
174 _("verified secondary superblock...\n"));
179 _("unable to verify superblock, continuing...\n"));
189 guess_default_geometry(
194 struct fs_topology ft
;
199 memset(&ft
, 0, sizeof(ft
));
200 get_topology(x
, &ft
, 1);
203 * get geometry from get_topology result.
204 * Use default block size (2^12)
207 multidisk
= ft
.dswidth
| ft
.dsunit
;
208 dblocks
= x
->dsize
>> (blocklog
- BBSHIFT
);
209 calc_default_ag_geometry(blocklog
, dblocks
, multidisk
,
216 find_secondary_sb(xfs_sb_t
*rsb
)
225 * Attempt to find secondary sb with a coarse approach,
226 * first trying agblocks and blocksize read from sb, providing
229 do_warn(_("\nattempting to find secondary superblock...\n"));
231 if (verify_sb_blocksize(rsb
) == 0) {
232 skip
= (uint64_t)rsb
->sb_agblocks
* rsb
->sb_blocksize
;
233 if (skip
>= XFS_AG_MIN_BYTES
&& skip
<= XFS_AG_MAX_BYTES
)
234 retval
= __find_secondary_sb(rsb
, skip
, skip
);
237 /* If that failed, retry coarse approach, using default geometry */
239 blocklog
= guess_default_geometry(&agsize
, &agcount
, &x
);
240 skip
= agsize
<< blocklog
;
241 retval
= __find_secondary_sb(rsb
, skip
, skip
);
244 /* If that failed, fall back to the brute force method */
246 retval
= __find_secondary_sb(rsb
, XFS_AG_MIN_BYTES
, BSIZE
);
252 * Calculate what the inode alignment field ought to be based on internal
253 * superblock info and determine if it is valid.
255 * For standard v5 superblocks, the inode alignment must either match
256 * XFS_INODE_BIG_CLUSTER_SIZE or a multiple based on the inode size. For v5
257 * superblocks with sparse inode chunks enabled, inode alignment must match the
260 * Return true if the alignment is valid, false otherwise.
263 sb_validate_ino_align(struct xfs_sb
*sb
)
267 if (!xfs_sb_version_hasalign(sb
))
270 /* standard cluster size alignment is always valid */
271 align
= XFS_INODE_BIG_CLUSTER_SIZE
>> sb
->sb_blocklog
;
272 if (align
== sb
->sb_inoalignmt
)
275 /* alignment scaled by inode size is v5 only for now */
276 if (!xfs_sb_version_hascrc(sb
))
279 align
= (XFS_INODE_BIG_CLUSTER_SIZE
*
280 sb
->sb_inodesize
/ XFS_DINODE_MIN_SIZE
) >> sb
->sb_blocklog
;
281 if (align
== sb
->sb_inoalignmt
)
285 * Sparse inodes requires inoalignmt to match full inode chunk size and
286 * spino_align to match the scaled alignment (as calculated above).
288 if (xfs_sb_version_hassparseinodes(sb
)) {
289 if (align
!= sb
->sb_spino_align
)
292 align
= (sb
->sb_inodesize
* XFS_INODES_PER_CHUNK
)
294 if (align
== sb
->sb_inoalignmt
)
302 * Validate the given log space. Derived from xfs_log_mount, though we
303 * can't validate the minimum log size until later. We only do this
304 * validation on V5 filesystems because the kernel doesn't reject malformed
305 * log geometry on older revision filesystems.
307 * Returns false if the log is garbage.
313 if (xfs_sb_version_hascrc(sb
) &&
314 (sb
->sb_logblocks
== 0 ||
315 sb
->sb_logblocks
> XFS_MAX_LOG_BLOCKS
||
316 (sb
->sb_logblocks
<< sb
->sb_blocklog
) > XFS_MAX_LOG_BYTES
))
319 if (sb
->sb_logsunit
> 1 && sb
->sb_logsunit
% sb
->sb_blocksize
)
326 * verify a superblock -- does not verify root inode #
327 * can only check that geometry info is internally
328 * consistent. because of growfs, that's no guarantee
329 * of correctness (e.g. geometry may have changed)
331 * fields verified or consistency checked:
339 * sb_blocksize (as a group)
342 * geometry info - sb_dblocks (as a group)
347 * inode info - sb_inodesize (x-checked with geo info)
363 * ALL real-time fields
364 * final 4 summary counters
368 verify_sb(char *sb_buf
, xfs_sb_t
*sb
, int is_primary_sb
)
374 /* check magic number and version number */
376 if (sb
->sb_magicnum
!= XFS_SB_MAGIC
)
377 return(XR_BAD_MAGIC
);
379 if (!xfs_sb_good_version(sb
))
380 return(XR_BAD_VERSION
);
382 /* does sb think mkfs really finished ? */
384 if (is_primary_sb
&& sb
->sb_inprogress
== 1)
385 return(XR_BAD_INPROGRESS
);
388 * before going *any further*, validate the sector size and if the
389 * version says we should have CRCs enabled, validate that.
392 /* check to make sure sectorsize is legal 2^N, 9 <= N <= 15 */
393 if (sb
->sb_sectsize
== 0)
394 return(XR_BAD_SECT_SIZE_DATA
);
397 for (i
= 0; bsize
< sb
->sb_sectsize
&&
398 i
< sizeof(sb
->sb_sectsize
) * NBBY
; i
++) {
402 if (i
< XFS_MIN_SECTORSIZE_LOG
|| i
> XFS_MAX_SECTORSIZE_LOG
)
403 return(XR_BAD_SECT_SIZE_DATA
);
405 /* check sb sectorsize field against sb sectlog field */
406 if (i
!= sb
->sb_sectlog
)
407 return(XR_BAD_SECT_SIZE_DATA
);
409 /* sector size in range - CRC check time */
410 if (xfs_sb_version_hascrc(sb
) &&
411 !libxfs_verify_cksum(sb_buf
, sb
->sb_sectsize
, XFS_SB_CRC_OFF
))
414 /* check to ensure blocksize and blocklog are legal */
415 ret
= verify_sb_blocksize(sb
);
419 /* sanity check ag count, size fields against data size field */
421 if (sb
->sb_dblocks
== 0 ||
422 sb
->sb_dblocks
> XFS_MAX_DBLOCKS(sb
) ||
423 sb
->sb_dblocks
< XFS_MIN_DBLOCKS(sb
))
424 return(XR_BAD_FS_SIZE_DATA
);
426 if (sb
->sb_agblklog
!= (uint8_t)log2_roundup(sb
->sb_agblocks
))
427 return(XR_BAD_FS_SIZE_DATA
);
429 if (sb
->sb_inodesize
< XFS_DINODE_MIN_SIZE
||
430 sb
->sb_inodesize
> XFS_DINODE_MAX_SIZE
||
431 sb
->sb_inodelog
< XFS_DINODE_MIN_LOG
||
432 sb
->sb_inodelog
> XFS_DINODE_MAX_LOG
||
433 sb
->sb_inodesize
!= (1 << sb
->sb_inodelog
) ||
434 sb
->sb_logsunit
> XLOG_MAX_RECORD_BSIZE
||
435 sb
->sb_inopblock
!= howmany(sb
->sb_blocksize
, sb
->sb_inodesize
) ||
436 (sb
->sb_blocklog
- sb
->sb_inodelog
!= sb
->sb_inopblog
))
437 return XR_BAD_INO_SIZE_DATA
;
439 if (!verify_sb_loginfo(sb
))
440 return XR_BAD_LOG_GEOMETRY
;
442 if (xfs_sb_version_hassector(sb
)) {
444 /* check to make sure log sector is legal 2^N, 9 <= N <= 15 */
446 if (sb
->sb_logsectsize
== 0)
447 return(XR_BAD_SECT_SIZE_DATA
);
451 for (i
= 0; bsize
< sb
->sb_logsectsize
&&
452 i
< sizeof(sb
->sb_logsectsize
) * NBBY
; i
++) {
456 if (i
< XFS_MIN_SECTORSIZE_LOG
|| i
> XFS_MAX_SECTORSIZE_LOG
)
457 return(XR_BAD_SECT_SIZE_DATA
);
459 /* check sb log sectorsize field against sb log sectlog field */
461 if (i
!= sb
->sb_logsectlog
)
462 return(XR_BAD_SECT_SIZE_DATA
);
466 * real-time extent size is always set
468 if (sb
->sb_rextsize
* sb
->sb_blocksize
> XFS_MAX_RTEXTSIZE
)
469 return(XR_BAD_RT_GEO_DATA
);
471 if (sb
->sb_rextsize
* sb
->sb_blocksize
< XFS_MIN_RTEXTSIZE
)
472 return(XR_BAD_RT_GEO_DATA
);
474 if (sb
->sb_rblocks
== 0) {
475 if (sb
->sb_rextents
!= 0)
476 return(XR_BAD_RT_GEO_DATA
);
478 if (sb
->sb_rbmblocks
!= 0)
479 return(XR_BAD_RT_GEO_DATA
);
481 if (sb
->sb_rextslog
!= 0)
482 return(XR_BAD_RT_GEO_DATA
);
484 if (sb
->sb_frextents
!= 0)
485 return(XR_BAD_RT_GEO_DATA
);
488 * if we have a real-time partition, sanity-check geometry
490 if (sb
->sb_rblocks
/ sb
->sb_rextsize
!= sb
->sb_rextents
)
491 return(XR_BAD_RT_GEO_DATA
);
493 if (sb
->sb_rextslog
!=
494 libxfs_highbit32((unsigned int)sb
->sb_rextents
))
495 return(XR_BAD_RT_GEO_DATA
);
497 if (sb
->sb_rbmblocks
!= (xfs_extlen_t
) howmany(sb
->sb_rextents
,
498 NBBY
* sb
->sb_blocksize
))
499 return(XR_BAD_RT_GEO_DATA
);
503 * verify correctness of inode alignment if it's there
505 if (!sb_validate_ino_align(sb
))
506 return(XR_BAD_INO_ALIGN
);
509 * verify max. % of inodes (sb_imax_pct)
511 if (sb
->sb_imax_pct
> 100)
512 return(XR_BAD_INO_MAX_PCT
);
515 * verify stripe alignment fields if present
517 if (xfs_sb_version_hasdalign(sb
)) {
518 if ((!sb
->sb_unit
&& sb
->sb_width
) ||
519 (sb
->sb_unit
&& sb
->sb_agblocks
% sb
->sb_unit
))
520 return(XR_BAD_SB_UNIT
);
521 if ((sb
->sb_unit
&& !sb
->sb_width
) ||
522 (sb
->sb_width
&& sb
->sb_unit
&& sb
->sb_width
% sb
->sb_unit
))
523 return(XR_BAD_SB_WIDTH
);
524 } else if (sb
->sb_unit
|| sb
->sb_width
)
525 return XR_BAD_SB_WIDTH
;
527 /* Directory block log */
528 if (sb
->sb_blocklog
+ sb
->sb_dirblklog
> XFS_MAX_BLOCKSIZE_LOG
)
529 return XR_BAD_DIR_SIZE_DATA
;
535 write_primary_sb(xfs_sb_t
*sbp
, int size
)
542 buf
= memalign(libxfs_device_alignment(), size
);
544 do_error(_("failed to memalign superblock buffer\n"));
547 memset(buf
, 0, size
);
549 if (lseek(x
.dfd
, 0LL, SEEK_SET
) != 0LL) {
551 do_error(_("couldn't seek to offset 0 in filesystem\n"));
554 libxfs_sb_to_disk(buf
, sbp
);
556 if (xfs_sb_version_hascrc(sbp
))
557 xfs_update_cksum((char *)buf
, size
, XFS_SB_CRC_OFF
);
559 if (write(x
.dfd
, buf
, size
) != size
) {
561 do_error(_("primary superblock write failed!\n"));
568 * get a possible superblock -- checks for internal consistency
571 get_sb(xfs_sb_t
*sbp
, xfs_off_t off
, int size
, xfs_agnumber_t agno
)
576 buf
= memalign(libxfs_device_alignment(), size
);
579 _("error reading superblock %u -- failed to memalign buffer\n"),
583 memset(buf
, 0, size
);
584 memset(sbp
, 0, sizeof(*sbp
));
586 /* try and read it first */
588 if (lseek(x
.dfd
, off
, SEEK_SET
) != off
) {
590 _("error reading superblock %u -- seek to offset %" PRId64
" failed\n"),
596 if ((rval
= read(x
.dfd
, buf
, size
)) != size
) {
599 _("superblock read failed, offset %" PRId64
", size %d, ag %u, rval %d\n"),
600 off
, size
, agno
, rval
);
601 do_error("%s\n", strerror(error
));
603 libxfs_sb_from_disk(sbp
, buf
);
605 rval
= verify_sb((char *)buf
, sbp
, agno
== 0);
610 /* returns element on list with highest reference count */
611 static fs_geo_list_t
*
612 get_best_geo(fs_geo_list_t
*list
)
615 fs_geo_list_t
*current
, *rval
= NULL
;
619 while (current
!= NULL
) {
620 if (current
->refs
> cnt
) {
624 current
= current
->next
;
630 /* adds geometry info to linked list. returns (sometimes new) head of list */
631 static fs_geo_list_t
*
632 add_geo(fs_geo_list_t
*list
, fs_geometry_t
*geo_p
, int index
)
634 fs_geo_list_t
*current
= list
;
636 while (current
!= NULL
) {
637 if (memcmp(geo_p
, ¤t
->geo
, sizeof(fs_geometry_t
)) == 0) {
642 current
= current
->next
;
645 if ((current
= malloc(sizeof(fs_geo_list_t
))) == NULL
) {
646 do_error(_("couldn't malloc geometry structure\n"));
650 current
->geo
= *geo_p
;
652 current
->next
= list
;
653 current
->index
= index
;
659 free_geo(fs_geo_list_t
*list
)
662 fs_geo_list_t
*current
;
664 for (current
= list
; current
!= NULL
; current
= next
) {
665 next
= current
->next
;
671 get_sb_geometry(fs_geometry_t
*geo
, xfs_sb_t
*sbp
)
673 memset(geo
, 0, sizeof(fs_geometry_t
));
676 * blindly set fields that we know are always good
678 geo
->sb_blocksize
= sbp
->sb_blocksize
;
679 geo
->sb_dblocks
= sbp
->sb_dblocks
;
680 geo
->sb_rblocks
= sbp
->sb_rblocks
;
681 geo
->sb_rextents
= sbp
->sb_rextents
;
682 geo
->sb_logstart
= sbp
->sb_logstart
;
683 geo
->sb_rextsize
= sbp
->sb_rextsize
;
684 geo
->sb_agblocks
= sbp
->sb_agblocks
;
685 geo
->sb_agcount
= sbp
->sb_agcount
;
686 geo
->sb_rbmblocks
= sbp
->sb_rbmblocks
;
687 geo
->sb_logblocks
= sbp
->sb_logblocks
;
688 geo
->sb_sectsize
= sbp
->sb_sectsize
;
689 geo
->sb_inodesize
= sbp
->sb_inodesize
;
691 if (xfs_sb_version_hasalign(sbp
))
692 geo
->sb_ialignbit
= 1;
694 if (xfs_sb_version_hasdalign(sbp
))
695 geo
->sb_salignbit
= 1;
697 if (xfs_sb_version_hasextflgbit(sbp
))
698 geo
->sb_extflgbit
= 1;
700 geo
->sb_fully_zeroed
= 1;
704 * the way to verify that a primary sb is consistent with the
705 * filesystem is find the secondaries given the info in the
706 * primary and compare the geometries in the secondaries against
707 * the geometry indicated by the primary.
709 * returns 0 if ok, else error code (XR_EOF, XR_INSUFF_SEC_SB, etc).
712 verify_set_primary_sb(xfs_sb_t
*rsb
,
720 fs_geo_list_t
*current
;
728 * We haven't been able to validate the sector size yet properly
729 * (e.g. in the case of repairing an image in a file), so we need to
730 * take into account sector mismatches and so use the maximum possible
731 * sector size rather than the sector size in @rsb.
733 size
= NUM_AGH_SECTS
* (1 << (XFS_MAX_SECTORSIZE_LOG
));
737 num_sbs
= rsb
->sb_agcount
;
739 sb
= (xfs_sb_t
*) alloc_ag_buf(size
);
742 * put the primary sb geometry info onto the geometry list
744 get_sb_geometry(&geo
, rsb
);
745 list
= add_geo(list
, &geo
, sb_index
);
748 * scan the secondaries and check them off as we get them so we only
749 * process each one once
751 for (agno
= 1; agno
< rsb
->sb_agcount
; agno
++) {
752 off
= (xfs_off_t
)agno
* rsb
->sb_agblocks
<< rsb
->sb_blocklog
;
754 retval
= get_sb(sb
, off
, size
, agno
);
755 if (retval
== XR_EOF
)
758 if (retval
== XR_OK
) {
760 * save away geometry info. don't bother checking the
761 * sb against the agi/agf as the odds of the sb being
762 * corrupted in a way that it is internally consistent
763 * but not consistent with the rest of the filesystem is
766 get_sb_geometry(&geo
, sb
);
767 list
= add_geo(list
, &geo
, agno
);
773 * see if we have enough superblocks to bother with
776 if (num_ok
< num_sbs
/ 2) {
777 retval
= XR_INSUFF_SEC_SB
;
781 current
= get_best_geo(list
);
784 * check that enough sbs agree that we're willing to
785 * go with this geometry. if not, print out the
786 * geometry and a message about the force option.
791 * If we only have two allocation groups, and the superblock
792 * in the second allocation group differs from the primary
793 * superblock we can't verify the geometry information.
794 * Warn the user about this situation and get out unless
795 * explicitly overridden.
797 if (current
->refs
!= 2) {
800 _("Only two AGs detected and they do not match - "
801 "cannot validate filesystem geometry.\n"
802 "Use the -o force_geometry option to proceed.\n"));
809 * If we only have a single allocation group there is no
810 * secondary superblock that we can use to verify the geometry
811 * information. Warn the user about this situation and get
812 * out unless explicitly overridden.
816 _("Only one AG detected - "
817 "cannot validate filesystem geometry.\n"
818 "Use the -o force_geometry option to proceed.\n"));
824 * at least half of the probed superblocks have
825 * to agree. if they don't, this fs is probably
826 * too far gone anyway considering the fact that
827 * XFS normally doesn't alter the secondary superblocks.
829 if (current
->refs
< num_sbs
/ 2) {
831 _("Not enough matching superblocks - cannot proceed.\n"));
837 * set the geometry into primary superblock if necessary.
840 if (current
->index
!= sb_index
) {
842 off
= (xfs_off_t
)current
->index
* current
->geo
.sb_agblocks
843 * current
->geo
.sb_blocksize
;
844 if (get_sb(sb
, off
, current
->geo
.sb_sectsize
,
845 current
->index
) != XR_OK
)
846 do_error(_("could not read superblock\n"));
851 * turn off inprogress bit since this is the primary.
852 * also save away values that we need to ensure are
853 * consistent in the other secondaries.
855 rsb
->sb_inprogress
= 0;
856 sb_inoalignmt
= sb
->sb_inoalignmt
;
857 sb_unit
= sb
->sb_unit
;
858 sb_width
= sb
->sb_width
;