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