]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - db/type.c
libxfs: sanitize agcount on load
[thirdparty/xfsprogs-dev.git] / db / type.c
CommitLineData
2bd0ea18 1/*
da23017d
NS
2 * Copyright (c) 2000-2001,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"
2bd0ea18
NS
20#include "block.h"
21#include "command.h"
2bd0ea18
NS
22#include "type.h"
23#include "faddr.h"
24#include "fprint.h"
25#include "field.h"
26#include "print.h"
27#include "sb.h"
28#include "inode.h"
49b31417 29#include "btblock.h"
2bd0ea18
NS
30#include "bmroot.h"
31#include "agf.h"
32#include "agfl.h"
33#include "agi.h"
2bd0ea18
NS
34#include "io.h"
35#include "output.h"
36#include "write.h"
37#include "attr.h"
38#include "dquot.h"
39#include "dir2.h"
c6b24b3b 40#include "text.h"
3cafd875 41#include "symlink.h"
2bd0ea18
NS
42
43static const typ_t *findtyp(char *name);
44static int type_f(int argc, char **argv);
45
46const typ_t *cur_typ;
47
48static const cmdinfo_t type_cmd =
9ee7055c
AM
49 { "type", NULL, type_f, 0, 1, 1, N_("[newtype]"),
50 N_("set/show current data type"), NULL };
2bd0ea18 51
c2907bd7 52static const typ_t __typtab[] = {
86769b32
DC
53 { TYP_AGF, "agf", handle_struct, agf_hfld, NULL, TYP_F_NO_CRC_OFF },
54 { TYP_AGFL, "agfl", handle_struct, agfl_hfld, NULL, TYP_F_NO_CRC_OFF },
55 { TYP_AGI, "agi", handle_struct, agi_hfld, NULL, TYP_F_NO_CRC_OFF },
56 { TYP_ATTR, "attr", handle_struct, attr_hfld, NULL, TYP_F_NO_CRC_OFF },
57 { TYP_BMAPBTA, "bmapbta", handle_struct, bmapbta_hfld, NULL,
58 TYP_F_NO_CRC_OFF },
59 { TYP_BMAPBTD, "bmapbtd", handle_struct, bmapbtd_hfld, NULL,
60 TYP_F_NO_CRC_OFF },
61 { TYP_BNOBT, "bnobt", handle_struct, bnobt_hfld, NULL, TYP_F_NO_CRC_OFF },
62 { TYP_CNTBT, "cntbt", handle_struct, cntbt_hfld, NULL, TYP_F_NO_CRC_OFF },
a9d7b986 63 { TYP_RMAPBT, NULL },
9fb2cb27 64 { TYP_REFCBT, NULL },
86769b32
DC
65 { TYP_DATA, "data", handle_block, NULL, NULL, TYP_F_NO_CRC_OFF },
66 { TYP_DIR2, "dir2", handle_struct, dir2_hfld, NULL, TYP_F_NO_CRC_OFF },
67 { TYP_DQBLK, "dqblk", handle_struct, dqblk_hfld, NULL, TYP_F_NO_CRC_OFF },
68 { TYP_INOBT, "inobt", handle_struct, inobt_hfld, NULL, TYP_F_NO_CRC_OFF },
69 { TYP_INODATA, "inodata", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
70 { TYP_INODE, "inode", handle_struct, inode_hfld, NULL, TYP_F_NO_CRC_OFF },
71 { TYP_LOG, "log", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
72 { TYP_RTBITMAP, "rtbitmap", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
73 { TYP_RTSUMMARY, "rtsummary", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
74 { TYP_SB, "sb", handle_struct, sb_hfld, NULL, TYP_F_NO_CRC_OFF },
75 { TYP_SYMLINK, "symlink", handle_string, NULL, NULL, TYP_F_NO_CRC_OFF },
76 { TYP_TEXT, "text", handle_text, NULL, NULL, TYP_F_NO_CRC_OFF },
77 { TYP_FINOBT, "finobt", handle_struct, inobt_hfld, NULL,
78 TYP_F_NO_CRC_OFF },
2bd0ea18
NS
79 { TYP_NONE, NULL }
80};
81
c2907bd7 82static const typ_t __typtab_crc[] = {
86769b32
DC
83 { TYP_AGF, "agf", handle_struct, agf_hfld, &xfs_agf_buf_ops,
84 XFS_AGF_CRC_OFF },
85 { TYP_AGFL, "agfl", handle_struct, agfl_crc_hfld, &xfs_agfl_buf_ops,
86 XFS_AGFL_CRC_OFF },
87 { TYP_AGI, "agi", handle_struct, agi_hfld, &xfs_agi_buf_ops,
88 XFS_AGI_CRC_OFF },
2847273f 89 { TYP_ATTR, "attr3", handle_struct, attr3_hfld,
86769b32 90 &xfs_attr3_db_buf_ops, TYP_F_NO_CRC_OFF },
6fea8f83 91 { TYP_BMAPBTA, "bmapbta", handle_struct, bmapbta_crc_hfld,
86769b32 92 &xfs_bmbt_buf_ops, XFS_BTREE_LBLOCK_CRC_OFF },
6fea8f83 93 { TYP_BMAPBTD, "bmapbtd", handle_struct, bmapbtd_crc_hfld,
86769b32 94 &xfs_bmbt_buf_ops, XFS_BTREE_LBLOCK_CRC_OFF },
6fea8f83 95 { TYP_BNOBT, "bnobt", handle_struct, bnobt_crc_hfld,
86769b32 96 &xfs_allocbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
6fea8f83 97 { TYP_CNTBT, "cntbt", handle_struct, cntbt_crc_hfld,
86769b32 98 &xfs_allocbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
a9d7b986
DW
99 { TYP_RMAPBT, "rmapbt", handle_struct, rmapbt_crc_hfld,
100 &xfs_rmapbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
9fb2cb27
DW
101 { TYP_REFCBT, "refcntbt", handle_struct, refcbt_crc_hfld,
102 &xfs_refcountbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
86769b32 103 { TYP_DATA, "data", handle_block, NULL, NULL, TYP_F_NO_CRC_OFF },
fc068a19 104 { TYP_DIR2, "dir3", handle_struct, dir3_hfld,
86769b32 105 &xfs_dir3_db_buf_ops, TYP_F_NO_CRC_OFF },
6fea8f83 106 { TYP_DQBLK, "dqblk", handle_struct, dqblk_hfld,
86769b32 107 &xfs_dquot_buf_ops, TYP_F_NO_CRC_OFF },
6fea8f83 108 { TYP_INOBT, "inobt", handle_struct, inobt_crc_hfld,
86769b32
DC
109 &xfs_inobt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
110 { TYP_INODATA, "inodata", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
6fea8f83 111 { TYP_INODE, "inode", handle_struct, inode_crc_hfld,
86769b32
DC
112 &xfs_inode_buf_ops, TYP_F_NO_CRC_OFF },
113 { TYP_LOG, "log", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
114 { TYP_RTBITMAP, "rtbitmap", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
115 { TYP_RTSUMMARY, "rtsummary", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
116 { TYP_SB, "sb", handle_struct, sb_hfld, &xfs_sb_buf_ops,
117 XFS_SB_CRC_OFF },
6fea8f83 118 { TYP_SYMLINK, "symlink", handle_struct, symlink_crc_hfld,
86769b32
DC
119 &xfs_symlink_buf_ops, XFS_SYMLINK_CRC_OFF },
120 { TYP_TEXT, "text", handle_text, NULL, NULL, TYP_F_NO_CRC_OFF },
20f35ef4 121 { TYP_FINOBT, "finobt", handle_struct, inobt_crc_hfld,
86769b32 122 &xfs_inobt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
c2907bd7
DC
123 { TYP_NONE, NULL }
124};
125
061e316e 126static const typ_t __typtab_spcrc[] = {
86769b32
DC
127 { TYP_AGF, "agf", handle_struct, agf_hfld, &xfs_agf_buf_ops,
128 XFS_AGF_CRC_OFF },
129 { TYP_AGFL, "agfl", handle_struct, agfl_crc_hfld, &xfs_agfl_buf_ops ,
130 XFS_AGFL_CRC_OFF },
131 { TYP_AGI, "agi", handle_struct, agi_hfld, &xfs_agi_buf_ops ,
132 XFS_AGI_CRC_OFF },
061e316e 133 { TYP_ATTR, "attr3", handle_struct, attr3_hfld,
86769b32 134 &xfs_attr3_db_buf_ops, TYP_F_NO_CRC_OFF },
061e316e 135 { TYP_BMAPBTA, "bmapbta", handle_struct, bmapbta_crc_hfld,
86769b32 136 &xfs_bmbt_buf_ops, XFS_BTREE_LBLOCK_CRC_OFF },
061e316e 137 { TYP_BMAPBTD, "bmapbtd", handle_struct, bmapbtd_crc_hfld,
86769b32 138 &xfs_bmbt_buf_ops, XFS_BTREE_LBLOCK_CRC_OFF },
061e316e 139 { TYP_BNOBT, "bnobt", handle_struct, bnobt_crc_hfld,
86769b32 140 &xfs_allocbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
061e316e 141 { TYP_CNTBT, "cntbt", handle_struct, cntbt_crc_hfld,
86769b32 142 &xfs_allocbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
a9d7b986
DW
143 { TYP_RMAPBT, "rmapbt", handle_struct, rmapbt_crc_hfld,
144 &xfs_rmapbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
9fb2cb27
DW
145 { TYP_REFCBT, "refcntbt", handle_struct, refcbt_crc_hfld,
146 &xfs_refcountbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
86769b32 147 { TYP_DATA, "data", handle_block, NULL, NULL, TYP_F_NO_CRC_OFF },
061e316e 148 { TYP_DIR2, "dir3", handle_struct, dir3_hfld,
86769b32 149 &xfs_dir3_db_buf_ops, TYP_F_NO_CRC_OFF },
061e316e 150 { TYP_DQBLK, "dqblk", handle_struct, dqblk_hfld,
86769b32 151 &xfs_dquot_buf_ops, TYP_F_NO_CRC_OFF },
061e316e 152 { TYP_INOBT, "inobt", handle_struct, inobt_spcrc_hfld,
86769b32
DC
153 &xfs_inobt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
154 { TYP_INODATA, "inodata", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
061e316e 155 { TYP_INODE, "inode", handle_struct, inode_crc_hfld,
86769b32
DC
156 &xfs_inode_buf_ops, TYP_F_NO_CRC_OFF },
157 { TYP_LOG, "log", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
158 { TYP_RTBITMAP, "rtbitmap", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
159 { TYP_RTSUMMARY, "rtsummary", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
160 { TYP_SB, "sb", handle_struct, sb_hfld, &xfs_sb_buf_ops,
161 XFS_SB_CRC_OFF },
061e316e 162 { TYP_SYMLINK, "symlink", handle_struct, symlink_crc_hfld,
86769b32
DC
163 &xfs_symlink_buf_ops, XFS_SYMLINK_CRC_OFF },
164 { TYP_TEXT, "text", handle_text, NULL, NULL, TYP_F_NO_CRC_OFF },
e96864ff 165 { TYP_FINOBT, "finobt", handle_struct, inobt_crc_hfld,
86769b32 166 &xfs_inobt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
061e316e
BF
167 { TYP_NONE, NULL }
168};
169
c2907bd7
DC
170const typ_t *typtab = __typtab;
171
172void
173type_set_tab_crc(void)
174{
175 typtab = __typtab_crc;
176}
177
061e316e
BF
178void
179type_set_tab_spcrc(void)
180{
181 typtab = __typtab_spcrc;
182}
183
2bd0ea18
NS
184static const typ_t *
185findtyp(
186 char *name)
187{
188 const typ_t *tt;
189
a364668b 190 for (tt = typtab; tt->typnm != TYP_NONE; tt++) {
2bd0ea18 191 ASSERT(tt->typnm == (typnm_t)(tt - typtab));
a364668b 192 if (tt->name && strcmp(tt->name, name) == 0)
2bd0ea18
NS
193 return tt;
194 }
195 return NULL;
196}
197
198static int
199type_f(
200 int argc,
201 char **argv)
202{
203 const typ_t *tt;
204 int count = 0;
205
206 if (argc == 1) {
207 if (cur_typ == NULL)
9ee7055c 208 dbprintf(_("no current type\n"));
2bd0ea18 209 else
9ee7055c 210 dbprintf(_("current type is \"%s\"\n"), cur_typ->name);
2bd0ea18 211
9ee7055c 212 dbprintf(_("\n supported types are:\n "));
a364668b
DC
213 for (tt = typtab, count = 0; tt->typnm != TYP_NONE; tt++) {
214 if (tt->name == NULL)
215 continue;
2bd0ea18
NS
216 if ((tt+1)->name != NULL) {
217 dbprintf("%s, ", tt->name);
218 if ((++count % 8) == 0)
219 dbprintf("\n ");
a364668b 220 } else if ((tt+1)->typnm == TYP_NONE) {
2bd0ea18
NS
221 dbprintf("%s\n", tt->name);
222 }
223 }
224
dfc130f3 225
2bd0ea18
NS
226 } else {
227 tt = findtyp(argv[1]);
228 if (tt == NULL) {
9ee7055c 229 dbprintf(_("no such type %s\n"), argv[1]);
dfc130f3 230 } else {
9ba69ce2
DC
231 if (iocur_top->typ == NULL)
232 dbprintf(_("no current object\n"));
233 else {
234 cur_typ = tt;
235 set_iocur_type(tt);
dfc130f3
RC
236 }
237 }
2bd0ea18
NS
238 }
239 return 0;
240}
241
242void
243type_init(void)
244{
245 add_command(&type_cmd);
246}
247
248/* read/write selectors for each major data type */
249
250void
251handle_struct(
252 int action,
253 const field_t *fields,
254 int argc,
255 char **argv)
256{
257 if (action == DB_WRITE)
258 write_struct(fields, argc, argv);
259 else
260 print_struct(fields, argc, argv);
261}
262
263void
264handle_string(
265 int action,
266 const field_t *fields,
267 int argc,
268 char **argv)
269{
270 if (action == DB_WRITE)
271 write_string(fields, argc, argv);
272 else
273 print_string(fields, argc, argv);
274}
275
276void
277handle_block(
278 int action,
279 const field_t *fields,
280 int argc,
281 char **argv)
282{
283 if (action == DB_WRITE)
284 write_block(fields, argc, argv);
285 else
286 print_block(fields, argc, argv);
287}
c6b24b3b
NS
288
289void
290handle_text(
291 int action,
292 const field_t *fields,
293 int argc,
294 char **argv)
295{
296 if (action != DB_WRITE)
297 print_text(fields, argc, argv);
298}