]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/convert.c
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
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.
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.
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
25 #define M(A) (1 << CT_ ## A)
26 #define agblock_to_bytes(x) \
27 ((__uint64_t)(x) << mp->m_sb.sb_blocklog)
28 #define agino_to_bytes(x) \
29 ((__uint64_t)(x) << mp->m_sb.sb_inodelog)
30 #define agnumber_to_bytes(x) \
31 agblock_to_bytes((__uint64_t)(x) * mp->m_sb.sb_agblocks)
32 #define daddr_to_bytes(x) \
33 ((__uint64_t)(x) << BBSHIFT)
34 #define fsblock_to_bytes(x) \
35 (agnumber_to_bytes(XFS_FSB_TO_AGNO(mp, (x))) + \
36 agblock_to_bytes(XFS_FSB_TO_AGBNO(mp, (x))))
37 #define ino_to_bytes(x) \
38 (agnumber_to_bytes(XFS_INO_TO_AGNO(mp, (x))) + \
39 agino_to_bytes(XFS_INO_TO_AGINO(mp, (x))))
40 #define inoidx_to_bytes(x) \
41 ((__uint64_t)(x) << mp->m_sb.sb_inodelog)
45 CT_AGBLOCK
, /* xfs_agblock_t */
46 CT_AGINO
, /* xfs_agino_t */
47 CT_AGNUMBER
, /* xfs_agno_t */
48 CT_BBOFF
, /* byte offset in daddr */
49 CT_BLKOFF
, /* byte offset in fsb/agb */
50 CT_BYTE
, /* byte in filesystem */
51 CT_DADDR
, /* daddr_t */
52 CT_FSBLOCK
, /* xfs_fsblock_t */
53 CT_INO
, /* xfs_ino_t */
54 CT_INOIDX
, /* index of inode in fsblock */
55 CT_INOOFF
, /* byte offset in inode */
59 typedef struct ctydesc
{
66 xfs_agblock_t agblock
;
68 xfs_agnumber_t agnumber
;
73 xfs_fsblock_t fsblock
;
79 static __uint64_t
bytevalue(ctype_t ctype
, cval_t
*val
);
80 static int convert_f(int argc
, char **argv
);
81 static int getvalue(char *s
, ctype_t ctype
, cval_t
*val
);
82 static ctype_t
lookupcty(char *ctyname
);
84 static const char *agblock_names
[] = { "agblock", "agbno", NULL
};
85 static const char *agino_names
[] = { "agino", "aginode", NULL
};
86 static const char *agnumber_names
[] = { "agnumber", "agno", NULL
};
87 static const char *bboff_names
[] = { "bboff", "daddroff", NULL
};
88 static const char *blkoff_names
[] = { "blkoff", "fsboff", "agboff",
90 static const char *byte_names
[] = { "byte", "fsbyte", NULL
};
91 static const char *daddr_names
[] = { "daddr", "bb", NULL
};
92 static const char *fsblock_names
[] = { "fsblock", "fsb", "fsbno", NULL
};
93 static const char *ino_names
[] = { "ino", "inode", NULL
};
94 static const char *inoidx_names
[] = { "inoidx", "offset", NULL
};
95 static const char *inooff_names
[] = { "inooff", "inodeoff", NULL
};
97 static const ctydesc_t ctydescs
[NCTS
] = {
98 { CT_AGBLOCK
, M(AGNUMBER
)|M(BBOFF
)|M(BLKOFF
)|M(INOIDX
)|M(INOOFF
),
100 { CT_AGINO
, M(AGNUMBER
)|M(INOOFF
), agino_names
},
102 M(AGBLOCK
)|M(AGINO
)|M(BBOFF
)|M(BLKOFF
)|M(INOIDX
)|M(INOOFF
),
104 { CT_BBOFF
, M(AGBLOCK
)|M(AGNUMBER
)|M(DADDR
)|M(FSBLOCK
), bboff_names
},
105 { CT_BLKOFF
, M(AGBLOCK
)|M(AGNUMBER
)|M(FSBLOCK
), blkoff_names
},
106 { CT_BYTE
, 0, byte_names
},
107 { CT_DADDR
, M(BBOFF
), daddr_names
},
108 { CT_FSBLOCK
, M(BBOFF
)|M(BLKOFF
)|M(INOIDX
), fsblock_names
},
109 { CT_INO
, M(INOOFF
), ino_names
},
110 { CT_INOIDX
, M(AGBLOCK
)|M(AGNUMBER
)|M(FSBLOCK
)|M(INOOFF
),
113 M(AGBLOCK
)|M(AGINO
)|M(AGNUMBER
)|M(FSBLOCK
)|M(INO
)|M(INOIDX
),
117 static const cmdinfo_t convert_cmd
=
118 { "convert", NULL
, convert_f
, 3, 9, 0, "type num [type num]... type",
119 "convert from one address form to another", NULL
};
122 bytevalue(ctype_t ctype
, cval_t
*val
)
126 return agblock_to_bytes(val
->agblock
);
128 return agino_to_bytes(val
->agino
);
130 return agnumber_to_bytes(val
->agnumber
);
132 return (__uint64_t
)val
->bboff
;
134 return (__uint64_t
)val
->blkoff
;
138 return daddr_to_bytes(val
->daddr
);
140 return fsblock_to_bytes(val
->fsblock
);
142 return ino_to_bytes(val
->ino
);
144 return inoidx_to_bytes(val
->inoidx
);
146 return (__uint64_t
)val
->inooff
;
156 convert_f(int argc
, char **argv
)
160 cval_t cvals
[NCTS
] = {};
166 /* move past the "convert" command */
170 if ((argc
% 2) != 1) {
171 dbprintf(_("bad argument count %d to convert, expected 3,5,7,9 "
172 "arguments\n"), argc
);
175 if ((wtype
= lookupcty(argv
[argc
- 1])) == CT_NONE
) {
176 dbprintf(_("unknown conversion type %s\n"), argv
[argc
- 1]);
180 for (i
= mask
= conmask
= 0; i
< (argc
- 1) / 2; i
++) {
181 c
= lookupcty(argv
[i
* 2]);
183 dbprintf(_("unknown conversion type %s\n"), argv
[i
* 2]);
187 dbprintf(_("result type same as argument\n"));
190 if (conmask
& (1 << c
)) {
191 dbprintf(_("conflicting conversion type %s\n"),
195 if (!getvalue(argv
[i
* 2 + 1], c
, &cvals
[c
]))
198 conmask
|= ~ctydescs
[c
].allowed
;
200 if (cur_agno
!= NULLAGNUMBER
&& (conmask
& M(AGNUMBER
)) == 0) {
201 cvals
[CT_AGNUMBER
].agnumber
= cur_agno
;
205 for (c
= (ctype_t
)0; c
< NCTS
; c
++) {
206 if (!(mask
& (1 << c
)))
208 v
+= bytevalue(c
, &cvals
[c
]);
212 v
= xfs_daddr_to_agbno(mp
, v
>> BBSHIFT
);
215 v
= (v
>> mp
->m_sb
.sb_inodelog
) %
216 (mp
->m_sb
.sb_agblocks
<< mp
->m_sb
.sb_inopblog
);
219 v
= xfs_daddr_to_agno(mp
, v
>> BBSHIFT
);
225 v
&= mp
->m_blockmask
;
233 v
= XFS_DADDR_TO_FSB(mp
, v
>> BBSHIFT
);
236 v
= XFS_AGINO_TO_INO(mp
, xfs_daddr_to_agno(mp
, v
>> BBSHIFT
),
237 (v
>> mp
->m_sb
.sb_inodelog
) %
238 (mp
->m_sb
.sb_agblocks
<< mp
->m_sb
.sb_inopblog
));
241 v
= (v
>> mp
->m_sb
.sb_inodelog
) & (mp
->m_sb
.sb_inopblock
- 1);
244 v
&= mp
->m_sb
.sb_inodesize
- 1;
251 dbprintf("0x%llx (%llu)\n", v
, v
);
258 add_command(&convert_cmd
);
262 getvalue(char *s
, ctype_t ctype
, cval_t
*val
)
267 v
= strtoull(s
, &p
, 0);
269 dbprintf(_("%s is not a number\n"), s
);
274 val
->agblock
= (xfs_agblock_t
)v
;
277 val
->agino
= (xfs_agino_t
)v
;
280 val
->agnumber
= (xfs_agnumber_t
)v
;
286 val
->blkoff
= (int)v
;
289 val
->byte
= (__uint64_t
)v
;
292 val
->daddr
= (xfs_daddr_t
)v
;
295 val
->fsblock
= (xfs_fsblock_t
)v
;
298 val
->ino
= (xfs_ino_t
)v
;
301 val
->inoidx
= (int)v
;
304 val
->inooff
= (int)v
;
315 lookupcty(char *ctyname
)
320 for (cty
= (ctype_t
)0; cty
< NCTS
; cty
++) {
321 for (name
= ctydescs
[cty
].names
; *name
; name
++) {
322 if (strcmp(ctyname
, *name
) == 0)