]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - repair/agheader.c
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
11 #include "err_protos.h"
14 * XXX (dgc): What is the point of all the check and repair here when phase 5
15 * recreates the AGF/AGI/AGFL completely from scratch?
19 verify_set_agf(xfs_mount_t
*mp
, xfs_agf_t
*agf
, xfs_agnumber_t i
)
21 xfs_rfsblock_t agblocks
;
24 /* check common fields */
26 if (be32_to_cpu(agf
->agf_magicnum
) != XFS_AGF_MAGIC
) {
28 do_warn(_("bad magic # 0x%x for agf %d\n"),
29 be32_to_cpu(agf
->agf_magicnum
), i
);
32 agf
->agf_magicnum
= cpu_to_be32(XFS_AGF_MAGIC
);
35 if (!XFS_AGF_GOOD_VERSION(be32_to_cpu(agf
->agf_versionnum
))) {
37 do_warn(_("bad version # %d for agf %d\n"),
38 be32_to_cpu(agf
->agf_versionnum
), i
);
41 agf
->agf_versionnum
= cpu_to_be32(XFS_AGF_VERSION
);
44 if (be32_to_cpu(agf
->agf_seqno
) != i
) {
46 do_warn(_("bad sequence # %d for agf %d\n"),
47 be32_to_cpu(agf
->agf_seqno
), i
);
50 agf
->agf_seqno
= cpu_to_be32(i
);
53 if (be32_to_cpu(agf
->agf_length
) != mp
->m_sb
.sb_agblocks
) {
54 if (i
!= mp
->m_sb
.sb_agcount
- 1) {
56 do_warn(_("bad length %d for agf %d, should be %d\n"),
57 be32_to_cpu(agf
->agf_length
), i
,
58 mp
->m_sb
.sb_agblocks
);
61 cpu_to_be32(mp
->m_sb
.sb_agblocks
);
63 agblocks
= mp
->m_sb
.sb_dblocks
-
64 (xfs_rfsblock_t
) mp
->m_sb
.sb_agblocks
* i
;
66 if (be32_to_cpu(agf
->agf_length
) != agblocks
) {
69 _("bad length %d for agf %d, should be %" PRIu64
"\n"),
70 be32_to_cpu(agf
->agf_length
),
73 agf
->agf_length
= cpu_to_be32(agblocks
);
79 * check first/last AGF fields. if need be, lose the free
80 * space in the AGFL, we'll reclaim it later.
82 if (be32_to_cpu(agf
->agf_flfirst
) >= libxfs_agfl_size(mp
)) {
83 do_warn(_("flfirst %d in agf %d too large (max = %u)\n"),
84 be32_to_cpu(agf
->agf_flfirst
),
85 i
, libxfs_agfl_size(mp
) - 1);
87 agf
->agf_flfirst
= cpu_to_be32(0);
90 if (be32_to_cpu(agf
->agf_fllast
) >= libxfs_agfl_size(mp
)) {
91 do_warn(_("fllast %d in agf %d too large (max = %u)\n"),
92 be32_to_cpu(agf
->agf_fllast
),
93 i
, libxfs_agfl_size(mp
) - 1);
95 agf
->agf_fllast
= cpu_to_be32(0);
98 /* don't check freespace btrees -- will be checked by caller */
100 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
103 if (platform_uuid_compare(&agf
->agf_uuid
, &mp
->m_sb
.sb_meta_uuid
)) {
107 platform_uuid_unparse(&agf
->agf_uuid
, uu
);
108 do_warn(_("bad uuid %s for agf %d\n"), uu
, i
);
111 platform_uuid_copy(&agf
->agf_uuid
,
112 &mp
->m_sb
.sb_meta_uuid
);
118 verify_set_agi(xfs_mount_t
*mp
, xfs_agi_t
*agi
, xfs_agnumber_t agno
)
120 xfs_rfsblock_t agblocks
;
123 /* check common fields */
125 if (be32_to_cpu(agi
->agi_magicnum
) != XFS_AGI_MAGIC
) {
127 do_warn(_("bad magic # 0x%x for agi %d\n"),
128 be32_to_cpu(agi
->agi_magicnum
), agno
);
131 agi
->agi_magicnum
= cpu_to_be32(XFS_AGI_MAGIC
);
134 if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi
->agi_versionnum
))) {
136 do_warn(_("bad version # %d for agi %d\n"),
137 be32_to_cpu(agi
->agi_versionnum
), agno
);
140 agi
->agi_versionnum
= cpu_to_be32(XFS_AGI_VERSION
);
143 if (be32_to_cpu(agi
->agi_seqno
) != agno
) {
145 do_warn(_("bad sequence # %d for agi %d\n"),
146 be32_to_cpu(agi
->agi_seqno
), agno
);
149 agi
->agi_seqno
= cpu_to_be32(agno
);
152 if (be32_to_cpu(agi
->agi_length
) != mp
->m_sb
.sb_agblocks
) {
153 if (agno
!= mp
->m_sb
.sb_agcount
- 1) {
155 do_warn(_("bad length # %d for agi %d, should be %d\n"),
156 be32_to_cpu(agi
->agi_length
), agno
,
157 mp
->m_sb
.sb_agblocks
);
160 cpu_to_be32(mp
->m_sb
.sb_agblocks
);
162 agblocks
= mp
->m_sb
.sb_dblocks
-
163 (xfs_rfsblock_t
) mp
->m_sb
.sb_agblocks
* agno
;
165 if (be32_to_cpu(agi
->agi_length
) != agblocks
) {
168 _("bad length # %d for agi %d, should be %" PRIu64
"\n"),
169 be32_to_cpu(agi
->agi_length
),
172 agi
->agi_length
= cpu_to_be32(agblocks
);
177 /* don't check inode btree -- will be checked by caller */
179 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
182 if (platform_uuid_compare(&agi
->agi_uuid
, &mp
->m_sb
.sb_meta_uuid
)) {
186 platform_uuid_unparse(&agi
->agi_uuid
, uu
);
187 do_warn(_("bad uuid %s for agi %d\n"), uu
, agno
);
190 platform_uuid_copy(&agi
->agi_uuid
,
191 &mp
->m_sb
.sb_meta_uuid
);
198 * superblock comparison - compare arbitrary superblock with
199 * filesystem mount-point superblock
201 * the verified fields include id and geometry.
203 * the inprogress fields, version numbers, and counters
204 * are allowed to differ as well as all fields after the
205 * counters to cope with the pre-6.5 mkfs non-zeroed
206 * secondary superblock sectors.
209 compare_sb(xfs_mount_t
*mp
, xfs_sb_t
*sb
)
211 fs_geometry_t fs_geo
, sb_geo
;
213 get_sb_geometry(&fs_geo
, &mp
->m_sb
);
214 get_sb_geometry(&sb_geo
, sb
);
216 if (memcmp(&fs_geo
, &sb_geo
,
217 (char *) &fs_geo
.sb_shared_vn
- (char *) &fs_geo
))
218 return(XR_SB_GEO_MISMATCH
);
224 * Possible fields that may have been set at mkfs time,
225 * sb_inoalignmt, sb_unit, sb_width and sb_dirblklog.
226 * The quota inode fields in the secondaries should be zero.
227 * Likewise, the sb_flags and sb_shared_vn should also be
228 * zero and the shared version bit should be cleared for
231 * And everything else in the buffer beyond either sb_width,
232 * sb_dirblklog (v2 dirs), or sb_logsectsize can be zeroed.
234 * Note: contrary to the name, this routine is called for all
235 * superblocks, not just the secondary superblocks.
239 struct xfs_mount
*mp
,
240 struct xfs_buf
*sbuf
,
244 struct xfs_dsb
*dsb
= XFS_BUF_TO_SBP(sbuf
);
254 * Check for garbage beyond the last valid field.
255 * Use field addresses instead so this code will still
256 * work against older filesystems when the superblock
257 * gets rev'ed again with new fields appended.
259 * size is the size of data which is valid for this sb.
261 if (xfs_sb_version_hasmetauuid(sb
))
262 size
= offsetof(xfs_sb_t
, sb_meta_uuid
)
263 + sizeof(sb
->sb_meta_uuid
);
264 else if (xfs_sb_version_hascrc(sb
))
265 size
= offsetof(xfs_sb_t
, sb_lsn
)
266 + sizeof(sb
->sb_lsn
);
267 else if (xfs_sb_version_hasmorebits(sb
))
268 size
= offsetof(xfs_sb_t
, sb_bad_features2
)
269 + sizeof(sb
->sb_bad_features2
);
270 else if (xfs_sb_version_haslogv2(sb
))
271 size
= offsetof(xfs_sb_t
, sb_logsunit
)
272 + sizeof(sb
->sb_logsunit
);
273 else if (xfs_sb_version_hassector(sb
))
274 size
= offsetof(xfs_sb_t
, sb_logsectsize
)
275 + sizeof(sb
->sb_logsectsize
);
276 else /* only support dirv2 or more recent */
277 size
= offsetof(xfs_sb_t
, sb_dirblklog
)
278 + sizeof(sb
->sb_dirblklog
);
280 /* Check the buffer we read from disk for garbage outside size */
281 for (ip
= (char *)sbuf
->b_addr
+ size
;
282 ip
< (char *)sbuf
->b_addr
+ mp
->m_sb
.sb_sectsize
;
290 rval
|= XR_AG_SB_SEC
;
293 _("zeroing unused portion of %s superblock (AG #%u)\n"),
294 !i
? _("primary") : _("secondary"), i
);
296 * zero both the in-memory sb and the disk buffer,
297 * because the former was read from disk and
298 * may contain newer version fields that shouldn't
299 * be set, and the latter is never updated past
300 * the last field - just zap them both.
302 memcpy(&tmpuuid
, &sb
->sb_meta_uuid
, sizeof(uuid_t
));
303 memset((void *)((intptr_t)sb
+ size
), 0,
304 mp
->m_sb
.sb_sectsize
- size
);
305 memset((char *)sbuf
->b_addr
+ size
, 0,
306 mp
->m_sb
.sb_sectsize
- size
);
307 /* Preserve meta_uuid so we don't fail uuid checks */
308 memcpy(&sb
->sb_meta_uuid
, &tmpuuid
, sizeof(uuid_t
));
311 _("would zero unused portion of %s superblock (AG #%u)\n"),
312 !i
? _("primary") : _("secondary"), i
);
316 * now look for the fields we can manipulate directly.
317 * if we did a zero and that zero could have included
318 * the field in question, just silently reset it. otherwise,
321 * for now, just zero the flags field since only
322 * the readonly flag is used
329 do_warn(_("bad flags field in superblock %d\n"), i
);
331 rval
|= XR_AG_SB_SEC
;
335 * quota inodes and flags in secondary superblocks are never set by
336 * mkfs. However, they could be set in a secondary if a fs with quotas
337 * was growfs'ed since growfs copies the new primary into the
340 * Also, the in-core inode flags now have different meaning to the
341 * on-disk flags, and so libxfs_sb_to_disk cannot directly write the
342 * sb_gquotino/sb_pquotino fields without specific sb_qflags being set.
343 * Hence we need to zero those fields directly in the sb buffer here.
346 if (sb
->sb_inprogress
== 1 && sb
->sb_uquotino
!= NULLFSINO
) {
352 _("non-null user quota inode field in superblock %d\n"),
356 rval
|= XR_AG_SB_SEC
;
359 if (sb
->sb_inprogress
== 1 && sb
->sb_gquotino
!= NULLFSINO
) {
362 dsb
->sb_gquotino
= 0;
367 _("non-null group quota inode field in superblock %d\n"),
371 rval
|= XR_AG_SB_SEC
;
375 * Note that sb_pquotino is not considered a valid sb field for pre-v5
376 * superblocks. If it is anything other than 0 it is considered garbage
377 * data beyond the valid sb and explicitly zeroed above.
379 if (xfs_sb_version_has_pquotino(&mp
->m_sb
) &&
380 sb
->sb_inprogress
== 1 && sb
->sb_pquotino
!= NULLFSINO
) {
383 dsb
->sb_pquotino
= 0;
388 _("non-null project quota inode field in superblock %d\n"),
392 rval
|= XR_AG_SB_SEC
;
395 if (sb
->sb_inprogress
== 1 && sb
->sb_qflags
) {
400 do_warn(_("non-null quota flags in superblock %d\n"),
403 rval
|= XR_AG_SB_SEC
;
407 * if the secondaries agree on a stripe unit/width or inode
408 * alignment, those fields ought to be valid since they are
409 * written at mkfs time (and the corresponding sb version bits
412 if (!xfs_sb_version_hasalign(sb
) && sb
->sb_inoalignmt
!= 0) {
414 sb
->sb_inoalignmt
= 0;
418 _("bad inode alignment field in superblock %d\n"),
421 rval
|= XR_AG_SB_SEC
;
424 if (!xfs_sb_version_hasdalign(sb
) &&
425 (sb
->sb_unit
!= 0 || sb
->sb_width
!= 0)) {
427 sb
->sb_unit
= sb
->sb_width
= 0;
431 _("bad stripe unit/width fields in superblock %d\n"),
434 rval
|= XR_AG_SB_SEC
;
437 if (!xfs_sb_version_hassector(sb
) &&
438 (sb
->sb_sectsize
!= BBSIZE
|| sb
->sb_sectlog
!= BBSHIFT
||
439 sb
->sb_logsectsize
!= 0 || sb
->sb_logsectlog
!= 0)) {
441 sb
->sb_sectsize
= BBSIZE
;
442 sb
->sb_sectlog
= BBSHIFT
;
443 sb
->sb_logsectsize
= 0;
444 sb
->sb_logsectlog
= 0;
449 _("bad log/data device sector size fields in superblock %d\n"),
452 rval
|= XR_AG_SB_SEC
;
459 * verify and reset the ag header if required.
461 * lower 4 bits of rval are set depending on what got modified.
462 * (see agheader.h for more details)
464 * NOTE -- this routine does not tell the user that it has
465 * altered things. Rather, it is up to the caller to do so
466 * using the bits encoded into the return value.
470 verify_set_agheader(xfs_mount_t
*mp
, xfs_buf_t
*sbuf
, xfs_sb_t
*sb
,
471 xfs_agf_t
*agf
, xfs_agi_t
*agi
, xfs_agnumber_t i
)
475 int status_sb
= XR_OK
;
477 status
= verify_sb(sbuf
->b_addr
, sb
, (i
== 0));
479 if (status
!= XR_OK
) {
480 do_warn(_("bad on-disk superblock %d - %s\n"),
481 i
, err_string(status
));
484 status_sb
= compare_sb(mp
, sb
);
486 if (status_sb
!= XR_OK
) {
487 do_warn(_("primary/secondary superblock %d conflict - %s\n"),
488 i
, err_string(status_sb
));
491 if (status
!= XR_OK
|| status_sb
!= XR_OK
) {
496 * clear the more transient fields
498 sb
->sb_inprogress
= 1;
503 sb
->sb_frextents
= 0;
511 rval
|= secondary_sb_whack(mp
, sbuf
, sb
, i
);
513 rval
|= verify_set_agf(mp
, agf
, i
);
514 rval
|= verify_set_agi(mp
, agi
, i
);