]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/block.c
Undoes mod: xfs-cmds:slinx:120772a
[thirdparty/xfsprogs-dev.git] / db / block.c
1 /*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33 #include <libxfs.h>
34 #include "block.h"
35 #include "bmap.h"
36 #include "command.h"
37 #include "data.h"
38 #include "type.h"
39 #include "faddr.h"
40 #include "fprint.h"
41 #include "field.h"
42 #include "inode.h"
43 #include "io.h"
44 #include "output.h"
45 #include "mount.h"
46
47 static int ablock_f(int argc, char **argv);
48 static void ablock_help(void);
49 static int daddr_f(int argc, char **argv);
50 static void daddr_help(void);
51 static int dblock_f(int argc, char **argv);
52 static void dblock_help(void);
53 static int fsblock_f(int argc, char **argv);
54 static void fsblock_help(void);
55 static void print_rawdata(void *data, int len);
56
57 static const cmdinfo_t ablock_cmd =
58 { "ablock", NULL, ablock_f, 1, 1, 1, "filoff",
59 "set address to file offset (attr fork)", ablock_help };
60 static const cmdinfo_t daddr_cmd =
61 { "daddr", NULL, daddr_f, 0, 1, 1, "[d]",
62 "set address to daddr value", daddr_help };
63 static const cmdinfo_t dblock_cmd =
64 { "dblock", NULL, dblock_f, 1, 1, 1, "filoff",
65 "set address to file offset (data fork)", dblock_help };
66 static const cmdinfo_t fsblock_cmd =
67 { "fsblock", "fsb", fsblock_f, 0, 1, 1, "[fsb]",
68 "set address to fsblock value", fsblock_help };
69
70 static void
71 ablock_help(void)
72 {
73 dbprintf(
74 "\n Example:\n"
75 "\n"
76 " 'ablock 23' - sets the file position to the 23rd filesystem block in\n"
77 " the inode's attribute fork. The filesystem block size is specified in\n"
78 " the superblock.\n\n"
79 );
80 }
81
82 /*ARGSUSED*/
83 static int
84 ablock_f(
85 int argc,
86 char **argv)
87 {
88 bmap_ext_t bm;
89 xfs_dfiloff_t bno;
90 xfs_dfsbno_t dfsbno;
91 int haveattr;
92 int nex;
93 char *p;
94
95 bno = (xfs_dfiloff_t)strtoull(argv[1], &p, 0);
96 if (*p != '\0') {
97 dbprintf("bad block number %s\n", argv[1]);
98 return 0;
99 }
100 push_cur();
101 set_cur_inode(iocur_top->ino);
102 haveattr = XFS_DFORK_Q((xfs_dinode_t *)iocur_top->data);
103 pop_cur();
104 if (!haveattr) {
105 dbprintf("no attribute data for file\n");
106 return 0;
107 }
108 nex = 1;
109 bmap(bno, 1, XFS_ATTR_FORK, &nex, &bm);
110 if (nex == 0) {
111 dbprintf("file attr block is unmapped\n");
112 return 0;
113 }
114 dfsbno = bm.startblock + (bno - bm.startoff);
115 ASSERT(typtab[TYP_ATTR].typnm == TYP_ATTR);
116 set_cur(&typtab[TYP_ATTR], (__int64_t)XFS_FSB_TO_DADDR(mp, dfsbno),
117 blkbb, DB_RING_ADD, NULL);
118 return 0;
119 }
120
121 void
122 block_init(void)
123 {
124 add_command(&ablock_cmd);
125 add_command(&daddr_cmd);
126 add_command(&dblock_cmd);
127 add_command(&fsblock_cmd);
128 }
129
130 static void
131 daddr_help(void)
132 {
133 dbprintf(
134 "\n Example:\n"
135 "\n"
136 " 'daddr 102' - sets position to the 102nd absolute disk block\n"
137 " (512 byte block).\n"
138 );
139 }
140
141 static int
142 daddr_f(
143 int argc,
144 char **argv)
145 {
146 __int64_t d;
147 char *p;
148
149 if (argc == 1) {
150 dbprintf("current daddr is %lld\n", iocur_top->off >> BBSHIFT);
151 return 0;
152 }
153 d = (__int64_t)strtoull(argv[1], &p, 0);
154 if (*p != '\0' ||
155 d >= mp->m_sb.sb_dblocks << (mp->m_sb.sb_blocklog - BBSHIFT)) {
156 dbprintf("bad daddr %s\n", argv[1]);
157 return 0;
158 }
159 ASSERT(typtab[TYP_DATA].typnm == TYP_DATA);
160 set_cur(&typtab[TYP_DATA], d, 1, DB_RING_ADD, NULL);
161 return 0;
162 }
163
164 static void
165 dblock_help(void)
166 {
167 dbprintf(
168 "\n Example:\n"
169 "\n"
170 " 'dblock 23' - sets the file position to the 23rd filesystem block in\n"
171 " the inode's data fork. The filesystem block size is specified in the\n"
172 " superblock.\n\n"
173 );
174 }
175
176 static int
177 dblock_f(
178 int argc,
179 char **argv)
180 {
181 bbmap_t bbmap;
182 bmap_ext_t *bmp;
183 xfs_dfiloff_t bno;
184 xfs_dfsbno_t dfsbno;
185 int nb;
186 int nex;
187 char *p;
188 typnm_t type;
189
190 bno = (xfs_dfiloff_t)strtoull(argv[1], &p, 0);
191 if (*p != '\0') {
192 dbprintf("bad block number %s\n", argv[1]);
193 return 0;
194 }
195 push_cur();
196 set_cur_inode(iocur_top->ino);
197 type = inode_next_type();
198 pop_cur();
199 if (type == TYP_NONE) {
200 dbprintf("no type for file data\n");
201 return 0;
202 }
203 nex = nb = type == TYP_DIR2 ? mp->m_dirblkfsbs : 1;
204 bmp = malloc(nb * sizeof(*bmp));
205 bmap(bno, nb, XFS_DATA_FORK, &nex, bmp);
206 if (nex == 0) {
207 dbprintf("file data block is unmapped\n");
208 free(bmp);
209 return 0;
210 }
211 dfsbno = bmp->startblock + (bno - bmp->startoff);
212 ASSERT(typtab[type].typnm == type);
213 if (nex > 1)
214 make_bbmap(&bbmap, nex, bmp);
215 set_cur(&typtab[type], (__int64_t)XFS_FSB_TO_DADDR(mp, dfsbno),
216 nb * blkbb, DB_RING_ADD, nex > 1 ? &bbmap : NULL);
217 free(bmp);
218 return 0;
219 }
220
221 static void
222 fsblock_help(void)
223 {
224 dbprintf(
225 "\n Example:\n"
226 "\n"
227 " 'fsblock 1023' - sets the file position to the 1023rd filesystem block.\n"
228 " The filesystem block size is specified in the superblock and set during\n"
229 " mkfs time. Offset is absolute (not AG relative).\n\n"
230 );
231 }
232
233 static int
234 fsblock_f(
235 int argc,
236 char **argv)
237 {
238 xfs_agblock_t agbno;
239 xfs_agnumber_t agno;
240 xfs_dfsbno_t d;
241 char *p;
242
243 if (argc == 1) {
244 dbprintf("current fsblock is %lld\n",
245 XFS_DADDR_TO_FSB(mp, iocur_top->off >> BBSHIFT));
246 return 0;
247 }
248 d = strtoull(argv[1], &p, 0);
249 if (*p != '\0') {
250 dbprintf("bad fsblock %s\n", argv[1]);
251 return 0;
252 }
253 agno = XFS_FSB_TO_AGNO(mp, d);
254 agbno = XFS_FSB_TO_AGBNO(mp, d);
255 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks) {
256 dbprintf("bad fsblock %s\n", argv[1]);
257 return 0;
258 }
259 ASSERT(typtab[TYP_DATA].typnm == TYP_DATA);
260 set_cur(&typtab[TYP_DATA], XFS_AGB_TO_DADDR(mp, agno, agbno),
261 blkbb, DB_RING_ADD, NULL);
262 return 0;
263 }
264
265 void
266 print_block(
267 const field_t *fields,
268 int argc,
269 char **argv)
270 {
271 print_rawdata(iocur_top->data, iocur_top->len);
272 }
273
274 static void
275 print_rawdata(
276 void *data,
277 int len)
278 {
279 int i;
280 int j;
281 int lastaddr;
282 int offchars;
283 unsigned char *p;
284
285 lastaddr = (len - 1) & ~(32 - 1);
286 if (lastaddr < 0x10)
287 offchars = 1;
288 else if (lastaddr < 0x100)
289 offchars = 2;
290 else if (lastaddr < 0x1000)
291 offchars = 3;
292 else
293 offchars = 4;
294 for (i = 0, p = data; i < len; i += 32) {
295 dbprintf("%-0*.*x:", offchars, offchars, i);
296 for (j = 0; j < 32 && i + j < len; j++, p++) {
297 if ((j & 3) == 0)
298 dbprintf(" ");
299 dbprintf("%02x", *p);
300 }
301 dbprintf("\n");
302 }
303 }