]>
Commit | Line | Data |
---|---|---|
959ef981 | 1 | // SPDX-License-Identifier: GPL-2.0 |
2bd0ea18 | 2 | /* |
da23017d NS |
3 | * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. |
4 | * All Rights Reserved. | |
2bd0ea18 NS |
5 | */ |
6 | ||
6b803e5a | 7 | #include "libxfs.h" |
7f725646 | 8 | #include "libxlog.h" |
2bd0ea18 NS |
9 | #include <signal.h> |
10 | #include "command.h" | |
2bd0ea18 NS |
11 | #include "init.h" |
12 | #include "input.h" | |
13 | #include "io.h" | |
4ca431fc | 14 | #include "init.h" |
2bd0ea18 NS |
15 | #include "sig.h" |
16 | #include "output.h" | |
c6b24b3b | 17 | #include "malloc.h" |
c2907bd7 | 18 | #include "type.h" |
2bd0ea18 | 19 | |
7f725646 BF |
20 | static char **cmdline; |
21 | static int ncmdline; | |
22 | char *fsdevice; | |
23 | int blkbb; | |
24 | int exitcode; | |
25 | int expert_mode; | |
00ff2b10 ES |
26 | static int force; |
27 | static struct xfs_mount xmount; | |
7f725646 | 28 | struct xfs_mount *mp; |
00ff2b10 | 29 | static struct xlog xlog; |
7f725646 BF |
30 | libxfs_init_t x; |
31 | xfs_agnumber_t cur_agno = NULLAGNUMBER; | |
2bd0ea18 NS |
32 | |
33 | static void | |
34 | usage(void) | |
35 | { | |
4ca431fc | 36 | fprintf(stderr, _( |
30626ef6 | 37 | "Usage: %s [-ifFrxV] [-p prog] [-l logdev] [-c cmd]... device\n" |
7f98455a | 38 | ), progname); |
2bd0ea18 NS |
39 | exit(1); |
40 | } | |
41 | ||
00ff2b10 | 42 | static void |
2bd0ea18 | 43 | init( |
4ca431fc NS |
44 | int argc, |
45 | char **argv) | |
2bd0ea18 | 46 | { |
72298d16 DC |
47 | struct xfs_sb *sbp; |
48 | struct xfs_buf *bp; | |
a547152d | 49 | unsigned int agcount; |
4ca431fc | 50 | int c; |
2bd0ea18 | 51 | |
9ee7055c AM |
52 | setlocale(LC_ALL, ""); |
53 | bindtextdomain(PACKAGE, LOCALEDIR); | |
54 | textdomain(PACKAGE); | |
55 | ||
2bd0ea18 | 56 | progname = basename(argv[0]); |
7f98455a | 57 | while ((c = getopt(argc, argv, "c:fFip:rxVl:")) != EOF) { |
2bd0ea18 NS |
58 | switch (c) { |
59 | case 'c': | |
c6b24b3b NS |
60 | cmdline = xrealloc(cmdline, (ncmdline+1)*sizeof(char*)); |
61 | cmdline[ncmdline++] = optarg; | |
2bd0ea18 NS |
62 | break; |
63 | case 'f': | |
4ca431fc | 64 | x.disfile = 1; |
2bd0ea18 | 65 | break; |
7f98455a CH |
66 | case 'F': |
67 | force = 1; | |
68 | break; | |
2bd0ea18 | 69 | case 'i': |
4ca431fc | 70 | x.isreadonly = (LIBXFS_ISREADONLY|LIBXFS_ISINACTIVE); |
2bd0ea18 NS |
71 | break; |
72 | case 'p': | |
73 | progname = optarg; | |
74 | break; | |
75 | case 'r': | |
4ca431fc | 76 | x.isreadonly = LIBXFS_ISREADONLY; |
2bd0ea18 NS |
77 | break; |
78 | case 'l': | |
4ca431fc | 79 | x.logname = optarg; |
2bd0ea18 NS |
80 | break; |
81 | case 'x': | |
4ca431fc | 82 | expert_mode = 1; |
2bd0ea18 NS |
83 | break; |
84 | case 'V': | |
9ee7055c | 85 | printf(_("%s version %s\n"), progname, VERSION); |
3d98fe63 | 86 | exit(0); |
2bd0ea18 NS |
87 | case '?': |
88 | usage(); | |
89 | /*NOTREACHED*/ | |
90 | } | |
91 | } | |
92 | if (optind + 1 != argc) { | |
93 | usage(); | |
94 | /*NOTREACHED*/ | |
95 | } | |
4ca431fc | 96 | |
2bd0ea18 | 97 | fsdevice = argv[optind]; |
4ca431fc NS |
98 | if (!x.disfile) |
99 | x.volname = fsdevice; | |
2bd0ea18 | 100 | else |
4ca431fc | 101 | x.dname = fsdevice; |
4ca431fc | 102 | |
ba9ecd40 | 103 | x.bcache_flags = CACHE_MISCOMPARE_PURGE; |
4ca431fc NS |
104 | if (!libxfs_init(&x)) { |
105 | fputs(_("\nfatal error -- couldn't initialize XFS library\n"), | |
2bd0ea18 NS |
106 | stderr); |
107 | exit(1); | |
108 | } | |
4ca431fc | 109 | |
72298d16 DC |
110 | /* |
111 | * Read the superblock, but don't validate it - we are a diagnostic | |
112 | * tool and so need to be able to mount busted filesystems. | |
113 | */ | |
114 | memset(&xmount, 0, sizeof(struct xfs_mount)); | |
115 | libxfs_buftarg_init(&xmount, x.ddev, x.logdev, x.rtdev); | |
116 | bp = libxfs_readbuf(xmount.m_ddev_targp, XFS_SB_DADDR, | |
117 | 1 << (XFS_MAX_SECTORSIZE_LOG - BBSHIFT), 0, NULL); | |
118 | ||
119 | if (!bp || bp->b_error) { | |
61983f67 BN |
120 | fprintf(stderr, _("%s: %s is invalid (cannot read first 512 " |
121 | "bytes)\n"), progname, fsdevice); | |
2bd0ea18 | 122 | exit(1); |
2bd0ea18 | 123 | } |
4ca431fc NS |
124 | |
125 | /* copy SB from buffer to in-core, converting architecture as we go */ | |
72298d16 DC |
126 | libxfs_sb_from_disk(&xmount.m_sb, XFS_BUF_TO_SBP(bp)); |
127 | libxfs_putbuf(bp); | |
128 | libxfs_purgebuf(bp); | |
4ca431fc NS |
129 | |
130 | sbp = &xmount.m_sb; | |
131 | if (sbp->sb_magicnum != XFS_SB_MAGIC) { | |
7f98455a CH |
132 | fprintf(stderr, _("%s: %s is not a valid XFS filesystem (unexpected SB magic number 0x%08x)\n"), |
133 | progname, fsdevice, sbp->sb_magicnum); | |
4e83ac7b ES |
134 | if (!force) { |
135 | fprintf(stderr, _("Use -F to force a read attempt.\n")); | |
7f98455a | 136 | exit(EXIT_FAILURE); |
4e83ac7b | 137 | } |
4ca431fc NS |
138 | } |
139 | ||
a547152d | 140 | agcount = sbp->sb_agcount; |
4ca431fc | 141 | mp = libxfs_mount(&xmount, sbp, x.ddev, x.logdev, x.rtdev, |
9aa57116 | 142 | LIBXFS_MOUNT_DEBUGGER); |
d1ee9d75 | 143 | if (!mp) { |
9aa57116 DC |
144 | fprintf(stderr, |
145 | _("%s: device %s unusable (not an XFS filesystem?)\n"), | |
146 | progname, fsdevice); | |
147 | exit(1); | |
d1ee9d75 | 148 | } |
7f725646 | 149 | mp->m_log = &xlog; |
2bd0ea18 | 150 | blkbb = 1 << mp->m_blkbb_log; |
4ca431fc | 151 | |
a547152d ES |
152 | /* Did we limit a broken agcount in libxfs_mount? */ |
153 | if (sbp->sb_agcount != agcount) | |
154 | exitcode = 1; | |
155 | ||
9aa57116 DC |
156 | /* |
157 | * xfs_check needs corrected incore superblock values | |
158 | */ | |
159 | if (sbp->sb_rootino != NULLFSINO && | |
160 | xfs_sb_version_haslazysbcount(&mp->m_sb)) { | |
e2f60652 | 161 | int error = -libxfs_initialize_perag_data(mp, sbp->sb_agcount); |
9aa57116 DC |
162 | if (error) { |
163 | fprintf(stderr, | |
164 | _("%s: cannot init perag data (%d). Continuing anyway.\n"), | |
165 | progname, error); | |
166 | } | |
167 | } | |
168 | ||
061e316e BF |
169 | if (xfs_sb_version_hassparseinodes(&mp->m_sb)) |
170 | type_set_tab_spcrc(); | |
171 | else if (xfs_sb_version_hascrc(&mp->m_sb)) | |
c2907bd7 DC |
172 | type_set_tab_crc(); |
173 | ||
2bd0ea18 NS |
174 | push_cur(); |
175 | init_commands(); | |
176 | init_sig(); | |
2bd0ea18 | 177 | } |
4ca431fc NS |
178 | |
179 | int | |
180 | main( | |
181 | int argc, | |
182 | char **argv) | |
183 | { | |
184 | int c, i, done = 0; | |
185 | char *input; | |
186 | char **v; | |
72298d16 | 187 | int start_iocur_sp; |
4ca431fc | 188 | |
4ca431fc | 189 | init(argc, argv); |
72298d16 | 190 | start_iocur_sp = iocur_sp; |
4ca431fc NS |
191 | |
192 | for (i = 0; !done && i < ncmdline; i++) { | |
193 | v = breakline(cmdline[i], &c); | |
194 | if (c) | |
195 | done = command(c, v); | |
196 | xfree(v); | |
197 | } | |
198 | if (cmdline) { | |
199 | xfree(cmdline); | |
1adfff6f | 200 | goto close_devices; |
4ca431fc NS |
201 | } |
202 | ||
c8dc4235 | 203 | pushfile(stdin); |
4ca431fc NS |
204 | while (!done) { |
205 | if ((input = fetchline()) == NULL) | |
206 | break; | |
207 | v = breakline(input, &c); | |
208 | if (c) | |
209 | done = command(c, v); | |
210 | doneline(input, v); | |
211 | } | |
1adfff6f DC |
212 | |
213 | close_devices: | |
72298d16 DC |
214 | /* |
215 | * Make sure that we pop the all the buffer contexts we hold so that | |
216 | * they are released before we purge the caches during unmount. | |
217 | */ | |
218 | while (iocur_sp > start_iocur_sp) | |
219 | pop_cur(); | |
220 | libxfs_umount(mp); | |
1adfff6f DC |
221 | if (x.ddev) |
222 | libxfs_device_close(x.ddev); | |
223 | if (x.logdev && x.logdev != x.ddev) | |
224 | libxfs_device_close(x.logdev); | |
225 | if (x.rtdev) | |
226 | libxfs_device_close(x.rtdev); | |
2ce8bff5 ES |
227 | libxfs_destroy(); |
228 | ||
4ca431fc NS |
229 | return exitcode; |
230 | } |