]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - repair/phase1.c
libxfs: refactor manage_zones()
[thirdparty/xfsprogs-dev.git] / repair / phase1.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6
7 #include "libxfs.h"
8 #include "globals.h"
9 #include "agheader.h"
10 #include "protos.h"
11 #include "err_protos.h"
12
13 static void
14 no_sb(void)
15 {
16 do_warn(_("Sorry, could not find valid secondary superblock\n"));
17 do_warn(_("Exiting now.\n"));
18 exit(1);
19 }
20
21 char *
22 alloc_ag_buf(int size)
23 {
24 char *bp;
25
26 bp = (char *)memalign(libxfs_device_alignment(), size);
27 if (!bp)
28 do_error(_("could not allocate ag header buffer (%d bytes)\n"),
29 size);
30 return(bp);
31 }
32
33 /*
34 * this has got to be big enough to hold 4 sectors
35 */
36 #define MAX_SECTSIZE (512 * 1024)
37
38 /* ARGSUSED */
39 void
40 phase1(xfs_mount_t *mp)
41 {
42 xfs_sb_t *sb;
43 char *ag_bp;
44 int rval;
45
46 do_log(_("Phase 1 - find and verify superblock...\n"));
47
48 primary_sb_modified = 0;
49 need_root_inode = 0;
50 need_root_dotdot = 0;
51 need_rbmino = 0;
52 need_rsumino = 0;
53 lost_quotas = 0;
54
55 /*
56 * get AG 0 into ag header buf
57 */
58 ag_bp = alloc_ag_buf(MAX_SECTSIZE);
59 sb = (xfs_sb_t *) ag_bp;
60
61 rval = get_sb(sb, 0LL, MAX_SECTSIZE, 0);
62 if (rval == XR_EOF)
63 do_error(_("error reading primary superblock\n"));
64
65 /*
66 * is this really an sb, verify internal consistency
67 */
68 if (rval != XR_OK) {
69 do_warn(_("bad primary superblock - %s !!!\n"),
70 err_string(rval));
71 if (!find_secondary_sb(sb))
72 no_sb();
73 primary_sb_modified = 1;
74 } else if ((rval = verify_set_primary_sb(sb, 0,
75 &primary_sb_modified)) != XR_OK) {
76 do_warn(_("couldn't verify primary superblock - %s !!!\n"),
77 err_string(rval));
78 if (!find_secondary_sb(sb))
79 no_sb();
80 primary_sb_modified = 1;
81 }
82
83 /*
84 * Check bad_features2 and make sure features2 the same as
85 * bad_features (ORing the two together). Leave bad_features2
86 * set so older kernels can still use it and not mount unsupported
87 * filesystems when it reads bad_features2.
88 */
89 if (sb->sb_bad_features2 != 0 &&
90 sb->sb_bad_features2 != sb->sb_features2) {
91 sb->sb_features2 |= sb->sb_bad_features2;
92 sb->sb_bad_features2 = sb->sb_features2;
93 primary_sb_modified = 1;
94 do_warn(_("superblock has a features2 mismatch, correcting\n"));
95 }
96
97 /*
98 * apply any version changes or conversions after the primary
99 * superblock has been verified or repaired
100 *
101 * Send output to stdout as do_log and everything else in repair
102 * is sent to stderr and there is no "quiet" option. xfs_admin
103 * will filter stderr but not stdout. This situation must be improved.
104 */
105 if (convert_lazy_count) {
106 if (lazy_count && !xfs_sb_version_haslazysbcount(sb)) {
107 sb->sb_versionnum |= XFS_SB_VERSION_MOREBITSBIT;
108 sb->sb_features2 |= XFS_SB_VERSION2_LAZYSBCOUNTBIT;
109 sb->sb_bad_features2 |= XFS_SB_VERSION2_LAZYSBCOUNTBIT;
110 primary_sb_modified = 1;
111 printf(_("Enabling lazy-counters\n"));
112 } else if (!lazy_count && xfs_sb_version_haslazysbcount(sb)) {
113 if (XFS_SB_VERSION_NUM(sb) == XFS_SB_VERSION_5) {
114 printf(
115 _("Cannot disable lazy-counters on V5 fs\n"));
116 exit(1);
117 }
118 sb->sb_features2 &= ~XFS_SB_VERSION2_LAZYSBCOUNTBIT;
119 sb->sb_bad_features2 &= ~XFS_SB_VERSION2_LAZYSBCOUNTBIT;
120 printf(_("Disabling lazy-counters\n"));
121 primary_sb_modified = 1;
122 } else {
123 printf(_("Lazy-counters are already %s\n"),
124 lazy_count ? _("enabled") : _("disabled"));
125 exit(0); /* no conversion required, exit */
126 }
127 }
128
129 /* shared_vn should be zero */
130 if (sb->sb_shared_vn) {
131 do_warn(_("resetting shared_vn to zero\n"));
132 sb->sb_shared_vn = 0;
133 primary_sb_modified = 1;
134 }
135
136 if (primary_sb_modified) {
137 if (!no_modify) {
138 do_warn(_("writing modified primary superblock\n"));
139 write_primary_sb(sb, sb->sb_sectsize);
140 } else {
141 do_warn(_("would write modified primary superblock\n"));
142 }
143 }
144
145 /*
146 * misc. global var initialization
147 */
148 sb_ifree = sb_icount = sb_fdblocks = sb_frextents = 0;
149
150 free(sb);
151 }