]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/agi.c
libxfs: sanitize agcount on load
[thirdparty/xfsprogs-dev.git] / db / agi.c
1 /*
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
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
7 * published by the Free Software Foundation.
8 *
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.
13 *
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
17 */
18
19 #include "libxfs.h"
20 #include "command.h"
21 #include "type.h"
22 #include "faddr.h"
23 #include "fprint.h"
24 #include "field.h"
25 #include "io.h"
26 #include "bit.h"
27 #include "output.h"
28 #include "init.h"
29 #include "agi.h"
30
31 static int agi_f(int argc, char **argv);
32 static void agi_help(void);
33
34 static const cmdinfo_t agi_cmd =
35 { "agi", NULL, agi_f, 0, 1, 1, N_("[agno]"),
36 N_("set address to agi header"), agi_help };
37
38 const field_t agi_hfld[] = {
39 { "", FLDT_AGI, OI(0), C1, 0, TYP_NONE },
40 { NULL }
41 };
42
43 #define OFF(f) bitize(offsetof(xfs_agi_t, agi_ ## f))
44 const field_t agi_flds[] = {
45 { "magicnum", FLDT_UINT32X, OI(OFF(magicnum)), C1, 0, TYP_NONE },
46 { "versionnum", FLDT_UINT32D, OI(OFF(versionnum)), C1, 0, TYP_NONE },
47 { "seqno", FLDT_AGNUMBER, OI(OFF(seqno)), C1, 0, TYP_NONE },
48 { "length", FLDT_AGBLOCK, OI(OFF(length)), C1, 0, TYP_NONE },
49 { "count", FLDT_AGINO, OI(OFF(count)), C1, 0, TYP_NONE },
50 { "root", FLDT_AGBLOCK, OI(OFF(root)), C1, 0, TYP_INOBT },
51 { "level", FLDT_UINT32D, OI(OFF(level)), C1, 0, TYP_NONE },
52 { "freecount", FLDT_AGINO, OI(OFF(freecount)), C1, 0, TYP_NONE },
53 { "newino", FLDT_AGINO, OI(OFF(newino)), C1, 0, TYP_INODE },
54 { "dirino", FLDT_AGINO, OI(OFF(dirino)), C1, 0, TYP_INODE },
55 { "unlinked", FLDT_AGINONN, OI(OFF(unlinked)),
56 CI(XFS_AGI_UNLINKED_BUCKETS), FLD_ARRAY, TYP_NONE },
57 { "uuid", FLDT_UUID, OI(OFF(uuid)), C1, 0, TYP_NONE },
58 { "lsn", FLDT_UINT64X, OI(OFF(lsn)), C1, 0, TYP_NONE },
59 { "crc", FLDT_CRC, OI(OFF(crc)), C1, 0, TYP_NONE },
60 { "free_root", FLDT_AGBLOCK, OI(OFF(free_root)), C1, 0, TYP_INOBT },
61 { "free_level", FLDT_UINT32D, OI(OFF(free_level)), C1, 0, TYP_NONE },
62 { NULL }
63 };
64
65 static void
66 agi_help(void)
67 {
68 dbprintf(_(
69 "\n"
70 " set allocation group inode btree\n"
71 "\n"
72 " Example:\n"
73 "\n"
74 " agi 3 (set location to 3rd allocation group inode btree and type to 'agi')\n"
75 "\n"
76 " Located in the 3rd 512 byte block of each allocation group,\n"
77 " the agi inode btree tracks all used/free inodes in the allocation group.\n"
78 " Inodes are allocated in 16k 'chunks', each btree entry tracks a 'chunk'.\n"
79 "\n"
80 ));
81 }
82
83 static int
84 agi_f(
85 int argc,
86 char **argv)
87 {
88 xfs_agnumber_t agno;
89 char *p;
90
91 if (argc > 1) {
92 agno = (xfs_agnumber_t)strtoul(argv[1], &p, 0);
93 if (*p != '\0' || agno >= mp->m_sb.sb_agcount) {
94 dbprintf(_("bad allocation group number %s\n"), argv[1]);
95 return 0;
96 }
97 cur_agno = agno;
98 } else if (cur_agno == NULLAGNUMBER)
99 cur_agno = 0;
100 ASSERT(typtab[TYP_AGI].typnm == TYP_AGI);
101 set_cur(&typtab[TYP_AGI],
102 XFS_AG_DADDR(mp, cur_agno, XFS_AGI_DADDR(mp)),
103 XFS_FSS_TO_BB(mp, 1), DB_RING_ADD, NULL);
104 return 0;
105 }
106
107 void
108 agi_init(void)
109 {
110 add_command(&agi_cmd);
111 }
112
113 /*ARGSUSED*/
114 int
115 agi_size(
116 void *obj,
117 int startoff,
118 int idx)
119 {
120 return bitize(mp->m_sb.sb_sectsize);
121 }