]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/type.c
324f416a49cc4dbcd7fe6536aa35c91fd7ac73ce
[thirdparty/xfsprogs-dev.git] / db / type.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 "block.h"
9 #include "command.h"
10 #include "type.h"
11 #include "faddr.h"
12 #include "fprint.h"
13 #include "field.h"
14 #include "print.h"
15 #include "sb.h"
16 #include "inode.h"
17 #include "btblock.h"
18 #include "bmroot.h"
19 #include "agf.h"
20 #include "agfl.h"
21 #include "agi.h"
22 #include "io.h"
23 #include "output.h"
24 #include "write.h"
25 #include "attr.h"
26 #include "dquot.h"
27 #include "dir2.h"
28 #include "text.h"
29 #include "symlink.h"
30 #include "fuzz.h"
31 #include "rtgroup.h"
32
33 static const typ_t *findtyp(char *name);
34 static int type_f(int argc, char **argv);
35
36 const typ_t *cur_typ;
37
38 static const cmdinfo_t type_cmd =
39 { "type", NULL, type_f, 0, 1, 1, N_("[newtype]"),
40 N_("set/show current data type"), NULL };
41
42 static const typ_t __typtab[] = {
43 { TYP_AGF, "agf", handle_struct, agf_hfld, NULL, TYP_F_NO_CRC_OFF },
44 { TYP_AGFL, "agfl", handle_struct, agfl_hfld, NULL, TYP_F_NO_CRC_OFF },
45 { TYP_AGI, "agi", handle_struct, agi_hfld, NULL, TYP_F_NO_CRC_OFF },
46 { TYP_ATTR, "attr", handle_struct, attr_hfld, NULL, TYP_F_NO_CRC_OFF },
47 { TYP_BMAPBTA, "bmapbta", handle_struct, bmapbta_hfld, NULL,
48 TYP_F_NO_CRC_OFF },
49 { TYP_BMAPBTD, "bmapbtd", handle_struct, bmapbtd_hfld, NULL,
50 TYP_F_NO_CRC_OFF },
51 { TYP_BNOBT, "bnobt", handle_struct, bnobt_hfld, NULL, TYP_F_NO_CRC_OFF },
52 { TYP_CNTBT, "cntbt", handle_struct, cntbt_hfld, NULL, TYP_F_NO_CRC_OFF },
53 { TYP_RMAPBT, NULL },
54 { TYP_RTRMAPBT, NULL },
55 { TYP_REFCBT, NULL },
56 { TYP_RTREFCBT, NULL },
57 { TYP_DATA, "data", handle_block, NULL, NULL, TYP_F_NO_CRC_OFF },
58 { TYP_DIR2, "dir2", handle_struct, dir2_hfld, NULL, TYP_F_NO_CRC_OFF },
59 { TYP_DQBLK, "dqblk", handle_struct, dqblk_hfld, NULL, TYP_F_NO_CRC_OFF },
60 { TYP_INOBT, "inobt", handle_struct, inobt_hfld, NULL, TYP_F_NO_CRC_OFF },
61 { TYP_INODATA, "inodata", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
62 { TYP_INODE, "inode", handle_struct, inode_hfld, NULL, TYP_F_NO_CRC_OFF },
63 { TYP_LOG, "log", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
64 { TYP_RTBITMAP, "rtbitmap", handle_text, NULL, NULL, TYP_F_NO_CRC_OFF },
65 { TYP_RTSUMMARY, "rtsummary", handle_text, NULL, NULL, TYP_F_NO_CRC_OFF },
66 { TYP_RTSB, "rtsb", handle_struct, rtsb_hfld, NULL, TYP_F_NO_CRC_OFF },
67 { TYP_SB, "sb", handle_struct, sb_hfld, NULL, TYP_F_NO_CRC_OFF },
68 { TYP_SYMLINK, "symlink", handle_string, NULL, NULL, TYP_F_NO_CRC_OFF },
69 { TYP_TEXT, "text", handle_text, NULL, NULL, TYP_F_NO_CRC_OFF },
70 { TYP_FINOBT, "finobt", handle_struct, finobt_hfld, NULL,
71 TYP_F_NO_CRC_OFF },
72 { TYP_RGBITMAP, NULL },
73 { TYP_RGSUMMARY, NULL },
74 { TYP_NONE, NULL }
75 };
76
77 static const typ_t __typtab_crc[] = {
78 { TYP_AGF, "agf", handle_struct, agf_hfld, &xfs_agf_buf_ops,
79 XFS_AGF_CRC_OFF },
80 { TYP_AGFL, "agfl", handle_struct, agfl_crc_hfld, &xfs_agfl_buf_ops,
81 XFS_AGFL_CRC_OFF },
82 { TYP_AGI, "agi", handle_struct, agi_hfld, &xfs_agi_buf_ops,
83 XFS_AGI_CRC_OFF },
84 { TYP_ATTR, "attr3", handle_struct, attr3_hfld,
85 &xfs_attr3_db_buf_ops, TYP_F_CRC_FUNC, xfs_attr3_set_crc },
86 { TYP_BMAPBTA, "bmapbta", handle_struct, bmapbta_crc_hfld,
87 &xfs_bmbt_buf_ops, XFS_BTREE_LBLOCK_CRC_OFF },
88 { TYP_BMAPBTD, "bmapbtd", handle_struct, bmapbtd_crc_hfld,
89 &xfs_bmbt_buf_ops, XFS_BTREE_LBLOCK_CRC_OFF },
90 { TYP_BNOBT, "bnobt", handle_struct, bnobt_crc_hfld,
91 &xfs_bnobt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
92 { TYP_CNTBT, "cntbt", handle_struct, cntbt_crc_hfld,
93 &xfs_cntbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
94 { TYP_RMAPBT, "rmapbt", handle_struct, rmapbt_crc_hfld,
95 &xfs_rmapbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
96 { TYP_RTRMAPBT, "rtrmapbt", handle_struct, rtrmapbt_crc_hfld,
97 &xfs_rtrmapbt_buf_ops, XFS_BTREE_LBLOCK_CRC_OFF },
98 { TYP_REFCBT, "refcntbt", handle_struct, refcbt_crc_hfld,
99 &xfs_refcountbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
100 { TYP_RTREFCBT, "rtrefcntbt", handle_struct, rtrefcbt_crc_hfld,
101 &xfs_rtrefcountbt_buf_ops, XFS_BTREE_LBLOCK_CRC_OFF },
102 { TYP_DATA, "data", handle_block, NULL, NULL, TYP_F_NO_CRC_OFF },
103 { TYP_DIR2, "dir3", handle_struct, dir3_hfld,
104 &xfs_dir3_db_buf_ops, TYP_F_CRC_FUNC, xfs_dir3_set_crc },
105 { TYP_DQBLK, "dqblk", handle_struct, dqblk_hfld,
106 &xfs_dquot_buf_ops, TYP_F_CRC_FUNC, xfs_dquot_set_crc },
107 { TYP_INOBT, "inobt", handle_struct, inobt_crc_hfld,
108 &xfs_inobt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
109 { TYP_INODATA, "inodata", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
110 { TYP_INODE, "inode", handle_struct, inode_crc_hfld,
111 &xfs_inode_buf_ops, TYP_F_CRC_FUNC, xfs_inode_set_crc },
112 { TYP_LOG, "log", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
113 { TYP_RTBITMAP, "rtbitmap", handle_text, NULL, NULL, TYP_F_NO_CRC_OFF },
114 { TYP_RTSUMMARY, "rtsummary", handle_text, NULL, NULL, TYP_F_NO_CRC_OFF },
115 { TYP_RTSB, "rtsb", handle_struct, rtsb_hfld, &xfs_rtsb_buf_ops,
116 XFS_SB_CRC_OFF },
117 { TYP_SB, "sb", handle_struct, sb_hfld, &xfs_sb_buf_ops,
118 XFS_SB_CRC_OFF },
119 { TYP_SYMLINK, "symlink", handle_struct, symlink_crc_hfld,
120 &xfs_symlink_buf_ops, XFS_SYMLINK_CRC_OFF },
121 { TYP_TEXT, "text", handle_text, NULL, NULL, TYP_F_NO_CRC_OFF },
122 { TYP_FINOBT, "finobt", handle_struct, finobt_crc_hfld,
123 &xfs_finobt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
124 { TYP_RGBITMAP, "rgbitmap", handle_struct, rgbitmap_hfld,
125 &xfs_rtbitmap_buf_ops, XFS_RTBUF_CRC_OFF },
126 { TYP_RGSUMMARY, "rgsummary", handle_struct, rgsummary_hfld,
127 &xfs_rtsummary_buf_ops, XFS_RTBUF_CRC_OFF },
128 { TYP_NONE, NULL }
129 };
130
131 static const typ_t __typtab_spcrc[] = {
132 { TYP_AGF, "agf", handle_struct, agf_hfld, &xfs_agf_buf_ops,
133 XFS_AGF_CRC_OFF },
134 { TYP_AGFL, "agfl", handle_struct, agfl_crc_hfld, &xfs_agfl_buf_ops ,
135 XFS_AGFL_CRC_OFF },
136 { TYP_AGI, "agi", handle_struct, agi_hfld, &xfs_agi_buf_ops ,
137 XFS_AGI_CRC_OFF },
138 { TYP_ATTR, "attr3", handle_struct, attr3_hfld,
139 &xfs_attr3_db_buf_ops, TYP_F_CRC_FUNC, xfs_attr3_set_crc },
140 { TYP_BMAPBTA, "bmapbta", handle_struct, bmapbta_crc_hfld,
141 &xfs_bmbt_buf_ops, XFS_BTREE_LBLOCK_CRC_OFF },
142 { TYP_BMAPBTD, "bmapbtd", handle_struct, bmapbtd_crc_hfld,
143 &xfs_bmbt_buf_ops, XFS_BTREE_LBLOCK_CRC_OFF },
144 { TYP_BNOBT, "bnobt", handle_struct, bnobt_crc_hfld,
145 &xfs_bnobt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
146 { TYP_CNTBT, "cntbt", handle_struct, cntbt_crc_hfld,
147 &xfs_cntbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
148 { TYP_RMAPBT, "rmapbt", handle_struct, rmapbt_crc_hfld,
149 &xfs_rmapbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
150 { TYP_RTRMAPBT, "rtrmapbt", handle_struct, rtrmapbt_crc_hfld,
151 &xfs_rtrmapbt_buf_ops, XFS_BTREE_LBLOCK_CRC_OFF },
152 { TYP_REFCBT, "refcntbt", handle_struct, refcbt_crc_hfld,
153 &xfs_refcountbt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
154 { TYP_RTREFCBT, "rtrefcntbt", handle_struct, rtrefcbt_crc_hfld,
155 &xfs_rtrefcountbt_buf_ops, XFS_BTREE_LBLOCK_CRC_OFF },
156 { TYP_DATA, "data", handle_block, NULL, NULL, TYP_F_NO_CRC_OFF },
157 { TYP_DIR2, "dir3", handle_struct, dir3_hfld,
158 &xfs_dir3_db_buf_ops, TYP_F_CRC_FUNC, xfs_dir3_set_crc },
159 { TYP_DQBLK, "dqblk", handle_struct, dqblk_hfld,
160 &xfs_dquot_buf_ops, TYP_F_CRC_FUNC, xfs_dquot_set_crc },
161 { TYP_INOBT, "inobt", handle_struct, inobt_spcrc_hfld,
162 &xfs_inobt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
163 { TYP_INODATA, "inodata", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
164 { TYP_INODE, "inode", handle_struct, inode_crc_hfld,
165 &xfs_inode_buf_ops, TYP_F_CRC_FUNC, xfs_inode_set_crc },
166 { TYP_LOG, "log", NULL, NULL, NULL, TYP_F_NO_CRC_OFF },
167 { TYP_RTBITMAP, "rtbitmap", handle_text, NULL, NULL, TYP_F_NO_CRC_OFF },
168 { TYP_RTSUMMARY, "rtsummary", handle_text, NULL, NULL, TYP_F_NO_CRC_OFF },
169 { TYP_RTSB, "rtsb", handle_struct, rtsb_hfld, &xfs_rtsb_buf_ops,
170 XFS_SB_CRC_OFF },
171 { TYP_SB, "sb", handle_struct, sb_hfld, &xfs_sb_buf_ops,
172 XFS_SB_CRC_OFF },
173 { TYP_SYMLINK, "symlink", handle_struct, symlink_crc_hfld,
174 &xfs_symlink_buf_ops, XFS_SYMLINK_CRC_OFF },
175 { TYP_TEXT, "text", handle_text, NULL, NULL, TYP_F_NO_CRC_OFF },
176 { TYP_FINOBT, "finobt", handle_struct, finobt_spcrc_hfld,
177 &xfs_finobt_buf_ops, XFS_BTREE_SBLOCK_CRC_OFF },
178 { TYP_RGBITMAP, "rgbitmap", handle_struct, rgbitmap_hfld,
179 &xfs_rtbitmap_buf_ops, XFS_RTBUF_CRC_OFF },
180 { TYP_RGSUMMARY, "rgsummary", handle_struct, rgsummary_hfld,
181 &xfs_rtsummary_buf_ops, XFS_RTBUF_CRC_OFF },
182 { TYP_NONE, NULL }
183 };
184
185 const typ_t *typtab = __typtab;
186
187 void
188 type_set_tab_crc(void)
189 {
190 typtab = __typtab_crc;
191 }
192
193 void
194 type_set_tab_spcrc(void)
195 {
196 typtab = __typtab_spcrc;
197 }
198
199 static const typ_t *
200 findtyp(
201 char *name)
202 {
203 const typ_t *tt;
204
205 for (tt = typtab; tt->typnm != TYP_NONE; tt++) {
206 ASSERT(tt->typnm == (typnm_t)(tt - typtab));
207 if (tt->name && strcmp(tt->name, name) == 0)
208 return tt;
209 }
210 return NULL;
211 }
212
213 static int
214 type_f(
215 int argc,
216 char **argv)
217 {
218 const typ_t *tt;
219 int count = 0;
220
221 if (argc == 1) {
222 if (cur_typ == NULL)
223 dbprintf(_("no current type\n"));
224 else
225 dbprintf(_("current type is \"%s\"\n"), cur_typ->name);
226
227 dbprintf(_("\n supported types are:\n "));
228 for (tt = typtab, count = 0; tt->typnm != TYP_NONE; tt++) {
229 if (tt->name == NULL)
230 continue;
231 if ((tt+1)->name != NULL) {
232 dbprintf("%s, ", tt->name);
233 if ((++count % 8) == 0)
234 dbprintf("\n ");
235 } else if ((tt+1)->typnm == TYP_NONE) {
236 dbprintf("%s\n", tt->name);
237 }
238 }
239
240
241 } else {
242 tt = findtyp(argv[1]);
243 if (tt == NULL) {
244 dbprintf(_("no such type %s\n"), argv[1]);
245 } else if (iocur_top->typ == tt) {
246 return 0;
247 } else {
248 if (iocur_top->typ == NULL)
249 dbprintf(_("no current object\n"));
250 else {
251 cur_typ = tt;
252 set_iocur_type(tt);
253 }
254 }
255 }
256 return 0;
257 }
258
259 void
260 type_init(void)
261 {
262 add_command(&type_cmd);
263 }
264
265 /* read/write selectors for each major data type */
266
267 void
268 handle_struct(
269 int action,
270 const field_t *fields,
271 int argc,
272 char **argv)
273 {
274 switch (action) {
275 case DB_FUZZ:
276 fuzz_struct(fields, argc, argv);
277 break;
278 case DB_WRITE:
279 write_struct(fields, argc, argv);
280 break;
281 case DB_READ:
282 print_struct(fields, argc, argv);
283 break;
284 }
285 }
286
287 void
288 handle_string(
289 int action,
290 const field_t *fields,
291 int argc,
292 char **argv)
293 {
294 switch (action) {
295 case DB_WRITE:
296 write_string(fields, argc, argv);
297 break;
298 case DB_READ:
299 print_string(fields, argc, argv);
300 break;
301 case DB_FUZZ:
302 dbprintf(_("string fuzzing not supported.\n"));
303 break;
304 }
305 }
306
307 void
308 handle_block(
309 int action,
310 const field_t *fields,
311 int argc,
312 char **argv)
313 {
314 switch (action) {
315 case DB_WRITE:
316 write_block(fields, argc, argv);
317 break;
318 case DB_READ:
319 print_block(fields, argc, argv);
320 break;
321 case DB_FUZZ:
322 dbprintf(_("use 'blocktrash' or 'write' to fuzz a block.\n"));
323 break;
324 }
325 }
326
327 void
328 handle_text(
329 int action,
330 const field_t *fields,
331 int argc,
332 char **argv)
333 {
334 switch (action) {
335 case DB_FUZZ:
336 fallthrough;
337 case DB_WRITE:
338 dbprintf(_("text writing/fuzzing not supported.\n"));
339 break;
340 case DB_READ:
341 print_text(fields, argc, argv);
342 break;
343 }
344 }