blob: 1932962d4fb3230271d2529886ed935f5b0a78b4 [file] [log] [blame]
Theodore Ts'odf614db2002-02-25 04:28:45 -05001/*
2 * htree.c --- hash tree routines
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'odf614db2002-02-25 04:28:45 -05004 * Copyright (C) 2002 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
6 */
7
Theodore Ts'od1154eb2011-09-18 17:34:37 -04008#include "config.h"
Theodore Ts'odf614db2002-02-25 04:28:45 -05009#include <stdio.h>
10#include <unistd.h>
11#include <stdlib.h>
12#include <ctype.h>
13#include <string.h>
14#include <time.h>
15#ifdef HAVE_ERRNO_H
16#include <errno.h>
17#endif
18#include <sys/types.h>
19#ifdef HAVE_GETOPT_H
20#include <getopt.h>
Theodore Ts'oefc6f622008-08-27 23:07:54 -040021#else
Theodore Ts'odf614db2002-02-25 04:28:45 -050022extern int optind;
23extern char *optarg;
24#endif
Theodore Ts'odf614db2002-02-25 04:28:45 -050025
26#include "debugfs.h"
Theodore Ts'o42080a82009-07-11 23:23:16 -040027#include "uuid/uuid.h"
28#include "e2p/e2p.h"
Theodore Ts'odf614db2002-02-25 04:28:45 -050029
30static FILE *pager;
31
Theodore Ts'odf614db2002-02-25 04:28:45 -050032static void htree_dump_leaf_node(ext2_filsys fs, ext2_ino_t ino,
33 struct ext2_inode *inode,
Theodore Ts'o3e699062002-10-13 23:56:28 -040034 struct ext2_dx_root_info * rootnode,
Valerie Aurora Henson048786d2009-09-07 22:46:17 -040035 blk64_t blk, char *buf)
Theodore Ts'odf614db2002-02-25 04:28:45 -050036{
37 errcode_t errcode;
38 struct ext2_dir_entry *dirent;
Theodore Ts'o54434922003-12-07 01:28:50 -050039 int thislen, col = 0;
40 unsigned int offset = 0;
Brian Behlendorfb7729002007-03-21 15:09:15 -040041 char name[EXT2_NAME_LEN + 1];
Darrick J. Wong9c23b892011-11-20 15:47:02 -050042 char tmp[EXT2_NAME_LEN + 64];
Valerie Aurora Henson048786d2009-09-07 22:46:17 -040043 blk64_t pblk;
Theodore Ts'o226515d2008-08-08 11:56:07 -040044 ext2_dirhash_t hash, minor_hash;
Theodore Ts'o8a480352009-06-21 21:07:38 -040045 unsigned int rec_len;
46 int hash_alg;
Darrick J. Wong81683c62012-08-02 17:27:43 -040047 int csum_size = 0;
48
49 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
50 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
51 csum_size = sizeof(struct ext2_dir_entry_tail);
Theodore Ts'oefc6f622008-08-27 23:07:54 -040052
Valerie Aurora Henson048786d2009-09-07 22:46:17 -040053 errcode = ext2fs_bmap2(fs, ino, inode, buf, 0, blk, 0, &pblk);
Theodore Ts'odf614db2002-02-25 04:28:45 -050054 if (errcode) {
55 com_err("htree_dump_leaf_node", errcode,
Valerie Aurora Henson048786d2009-09-07 22:46:17 -040056 "while mapping logical block %llu\n", blk);
Theodore Ts'odf614db2002-02-25 04:28:45 -050057 return;
58 }
59
Valerie Aurora Henson048786d2009-09-07 22:46:17 -040060 printf("Reading directory block %llu, phys %llu\n", blk, pblk);
Darrick J. Wong81683c62012-08-02 17:27:43 -040061 errcode = ext2fs_read_dir_block4(current_fs, pblk, buf, 0, ino);
Theodore Ts'odf614db2002-02-25 04:28:45 -050062 if (errcode) {
63 com_err("htree_dump_leaf_node", errcode,
Valerie Aurora Henson048786d2009-09-07 22:46:17 -040064 "while reading block %llu (%llu)\n",
65 blk, pblk);
Theodore Ts'odf614db2002-02-25 04:28:45 -050066 return;
67 }
Theodore Ts'of77704e2006-11-11 22:32:35 -050068 hash_alg = rootnode->hash_version;
69 if ((hash_alg <= EXT2_HASH_TEA) &&
70 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
71 hash_alg += 3;
Theodore Ts'odf614db2002-02-25 04:28:45 -050072
Darrick J. Wong81683c62012-08-02 17:27:43 -040073 while (offset < (fs->blocksize - csum_size)) {
Theodore Ts'odf614db2002-02-25 04:28:45 -050074 dirent = (struct ext2_dir_entry *) (buf + offset);
Theodore Ts'o8a480352009-06-21 21:07:38 -040075 errcode = ext2fs_get_rec_len(fs, dirent, &rec_len);
76 if (errcode) {
77 com_err("htree_dump_leaf_inode", errcode,
78 "while getting rec_len for block %lu",
79 (unsigned long) blk);
80 return;
81 }
Theodore Ts'o5dd77db2008-08-25 21:08:19 -040082 if (((offset + rec_len) > fs->blocksize) ||
83 (rec_len < 8) ||
84 ((rec_len % 4) != 0) ||
Theodore Ts'o42080a82009-07-11 23:23:16 -040085 ((((unsigned) dirent->name_len & 0xFF)+8) > rec_len)) {
Valerie Aurora Henson048786d2009-09-07 22:46:17 -040086 fprintf(pager, "Corrupted directory block (%llu)!\n",
87 blk);
Theodore Ts'odf614db2002-02-25 04:28:45 -050088 break;
89 }
Eric Sandeen11ba79b2011-09-16 15:49:17 -050090 thislen = dirent->name_len & 0xFF;
Theodore Ts'odf614db2002-02-25 04:28:45 -050091 strncpy(name, dirent->name, thislen);
92 name[thislen] = '\0';
Theodore Ts'of77704e2006-11-11 22:32:35 -050093 errcode = ext2fs_dirhash(hash_alg, name,
Theodore Ts'o3e699062002-10-13 23:56:28 -040094 thislen, fs->super->s_hash_seed,
Theodore Ts'o226515d2008-08-08 11:56:07 -040095 &hash, &minor_hash);
Theodore Ts'o52783e02002-03-11 15:04:45 -050096 if (errcode)
97 com_err("htree_dump_leaf_node", errcode,
98 "while calculating hash");
Darrick J. Wong9c23b892011-11-20 15:47:02 -050099 snprintf(tmp, EXT2_NAME_LEN + 64, "%u 0x%08x-%08x (%d) %s ",
100 dirent->inode, hash, minor_hash, rec_len, name);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500101 thislen = strlen(tmp);
102 if (col + thislen > 80) {
103 fprintf(pager, "\n");
104 col = 0;
105 }
106 fprintf(pager, "%s", tmp);
107 col += thislen;
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400108 offset += rec_len;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500109 }
110 fprintf(pager, "\n");
111}
112
113
114static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
115 struct ext2_inode *inode,
Theodore Ts'o3e699062002-10-13 23:56:28 -0400116 struct ext2_dx_root_info * rootnode,
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400117 blk64_t blk, char *buf, int level);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500118
119
120static void htree_dump_int_node(ext2_filsys fs, ext2_ino_t ino,
121 struct ext2_inode *inode,
Theodore Ts'o3e699062002-10-13 23:56:28 -0400122 struct ext2_dx_root_info * rootnode,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400123 struct ext2_dx_entry *ent,
Theodore Ts'odf614db2002-02-25 04:28:45 -0500124 char *buf, int level)
125{
Theodore Ts'o621732c2002-07-18 22:19:51 -0400126 struct ext2_dx_countlimit limit;
127 struct ext2_dx_entry e;
Darrick J. Wonga9964662012-07-30 19:20:04 -0400128 struct ext2_dx_tail *tail;
Theodore Ts'o42e5b5f2002-09-22 15:27:28 -0400129 int hash, i;
Darrick J. Wonga9964662012-07-30 19:20:04 -0400130 int remainder;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500131
Theodore Ts'o621732c2002-07-18 22:19:51 -0400132 limit = *((struct ext2_dx_countlimit *) ent);
133 limit.count = ext2fs_le16_to_cpu(limit.count);
134 limit.limit = ext2fs_le16_to_cpu(limit.limit);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500135
Theodore Ts'o621732c2002-07-18 22:19:51 -0400136 fprintf(pager, "Number of entries (count): %d\n", limit.count);
137 fprintf(pager, "Number of entries (limit): %d\n", limit.limit);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500138
Darrick J. Wonga9964662012-07-30 19:20:04 -0400139 remainder = fs->blocksize - (limit.limit *
140 sizeof(struct ext2_dx_entry));
141 if (ent == (struct ext2_dx_entry *)(rootnode + 1))
142 remainder -= sizeof(struct ext2_dx_root_info) + 24;
143 else
144 remainder -= 8;
145 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
146 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
147 remainder == sizeof(struct ext2_dx_tail)) {
148 tail = (struct ext2_dx_tail *)(ent + limit.limit);
149 fprintf(pager, "Checksum: 0x%08x\n",
150 ext2fs_le32_to_cpu(tail->dt_checksum));
151 }
152
Theodore Ts'o42e5b5f2002-09-22 15:27:28 -0400153 for (i=0; i < limit.count; i++) {
154 hash = i ? ext2fs_le32_to_cpu(ent[i].hash) : 0;
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400155 fprintf(pager, "Entry #%d: Hash 0x%08x%s, block %u\n", i,
Theodore Ts'o42e5b5f2002-09-22 15:27:28 -0400156 hash, (hash & 1) ? " (**)" : "",
Theodore Ts'o621732c2002-07-18 22:19:51 -0400157 ext2fs_le32_to_cpu(ent[i].block));
Theodore Ts'o42e5b5f2002-09-22 15:27:28 -0400158 }
Theodore Ts'odf614db2002-02-25 04:28:45 -0500159
160 fprintf(pager, "\n");
161
Theodore Ts'o621732c2002-07-18 22:19:51 -0400162 for (i=0; i < limit.count; i++) {
163 e.hash = ext2fs_le32_to_cpu(ent[i].hash);
164 e.block = ext2fs_le32_to_cpu(ent[i].block);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500165 fprintf(pager, "Entry #%d: Hash 0x%08x, block %u\n", i,
Theodore Ts'o621732c2002-07-18 22:19:51 -0400166 i ? e.hash : 0, e.block);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500167 if (level)
Theodore Ts'o3e699062002-10-13 23:56:28 -0400168 htree_dump_int_block(fs, ino, inode, rootnode,
Theodore Ts'o621732c2002-07-18 22:19:51 -0400169 e.block, buf, level-1);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500170 else
Theodore Ts'o3e699062002-10-13 23:56:28 -0400171 htree_dump_leaf_node(fs, ino, inode, rootnode,
Theodore Ts'o621732c2002-07-18 22:19:51 -0400172 e.block, buf);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500173 }
174
175 fprintf(pager, "---------------------\n");
176}
177
178static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
179 struct ext2_inode *inode,
Theodore Ts'o3e699062002-10-13 23:56:28 -0400180 struct ext2_dx_root_info * rootnode,
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400181 blk64_t blk, char *buf, int level)
Theodore Ts'odf614db2002-02-25 04:28:45 -0500182{
183 char *cbuf;
184 errcode_t errcode;
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400185 blk64_t pblk;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500186
187 cbuf = malloc(fs->blocksize);
188 if (!cbuf) {
189 fprintf(pager, "Couldn't allocate child block.\n");
190 return;
191 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400192
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400193 errcode = ext2fs_bmap2(fs, ino, inode, buf, 0, blk, 0, &pblk);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500194 if (errcode) {
195 com_err("htree_dump_int_block", errcode,
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400196 "while mapping logical block %llu\n", blk);
Brian Behlendorf89456552007-03-21 17:53:33 -0400197 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500198 }
199
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400200 errcode = io_channel_read_blk64(current_fs->io, pblk, 1, buf);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500201 if (errcode) {
202 com_err("htree_dump_int_block", errcode,
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400203 "while reading block %llu\n", blk);
Brian Behlendorf89456552007-03-21 17:53:33 -0400204 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500205 }
206
Theodore Ts'o3e699062002-10-13 23:56:28 -0400207 htree_dump_int_node(fs, ino, inode, rootnode,
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400208 (struct ext2_dx_entry *) (buf+8),
Theodore Ts'odf614db2002-02-25 04:28:45 -0500209 cbuf, level);
Brian Behlendorf89456552007-03-21 17:53:33 -0400210errout:
Theodore Ts'odf614db2002-02-25 04:28:45 -0500211 free(cbuf);
212}
213
214
215
216void do_htree_dump(int argc, char *argv[])
217{
218 ext2_ino_t ino;
219 struct ext2_inode inode;
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400220 blk64_t blk;
Theodore Ts'o3e699062002-10-13 23:56:28 -0400221 char *buf = NULL;
222 struct ext2_dx_root_info *rootnode;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500223 struct ext2_dx_entry *ent;
224 struct ext2_dx_countlimit *limit;
225 errcode_t errcode;
226
227 if (check_fs_open(argv[0]))
228 return;
229
230 pager = open_pager();
231
Eric Sandeen1ec5d102011-05-11 11:46:19 -0500232 if (common_inode_args_process(argc, argv, &ino, 0))
Theodore Ts'o155f5772002-07-21 14:17:45 -0400233 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500234
235 if (debugfs_read_inode(ino, &inode, argv[1]))
Theodore Ts'o155f5772002-07-21 14:17:45 -0400236 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500237
238 if (!LINUX_S_ISDIR(inode.i_mode)) {
239 com_err(argv[0], 0, "Not a directory");
Theodore Ts'o155f5772002-07-21 14:17:45 -0400240 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500241 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400242
Theodore Ts'odf614db2002-02-25 04:28:45 -0500243 if ((inode.i_flags & EXT2_BTREE_FL) == 0) {
244 com_err(argv[0], 0, "Not a hash-indexed directory");
Theodore Ts'o155f5772002-07-21 14:17:45 -0400245 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500246 }
247
248 buf = malloc(2*current_fs->blocksize);
249 if (!buf) {
250 com_err(argv[0], 0, "Couldn't allocate htree buffer");
Theodore Ts'o155f5772002-07-21 14:17:45 -0400251 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500252 }
253
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400254 errcode = ext2fs_bmap2(current_fs, ino, &inode, buf, 0, 0, 0, &blk);
Theodore Ts'o226515d2008-08-08 11:56:07 -0400255 if (errcode) {
256 com_err("do_htree_block", errcode,
257 "while mapping logical block 0\n");
258 goto errout;
259 }
260
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400261 errcode = io_channel_read_blk64(current_fs->io, blk,
262 1, buf);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500263 if (errcode) {
264 com_err(argv[0], errcode, "Error reading root node");
265 goto errout;
266 }
267
Theodore Ts'o3e699062002-10-13 23:56:28 -0400268 rootnode = (struct ext2_dx_root_info *) (buf + 24);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500269
270 fprintf(pager, "Root node dump:\n");
Takashi Sato8deb80a2006-03-18 21:43:46 -0500271 fprintf(pager, "\t Reserved zero: %u\n", rootnode->reserved_zero);
Theodore Ts'o3e699062002-10-13 23:56:28 -0400272 fprintf(pager, "\t Hash Version: %d\n", rootnode->hash_version);
273 fprintf(pager, "\t Info length: %d\n", rootnode->info_length);
274 fprintf(pager, "\t Indirect levels: %d\n", rootnode->indirect_levels);
275 fprintf(pager, "\t Flags: %d\n", rootnode->unused_flags);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500276
Theodore Ts'o3e699062002-10-13 23:56:28 -0400277 ent = (struct ext2_dx_entry *) (buf + 24 + rootnode->info_length);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500278 limit = (struct ext2_dx_countlimit *) ent;
279
Theodore Ts'o3e699062002-10-13 23:56:28 -0400280 htree_dump_int_node(current_fs, ino, &inode, rootnode, ent,
Theodore Ts'odf614db2002-02-25 04:28:45 -0500281 buf + current_fs->blocksize,
Theodore Ts'o3e699062002-10-13 23:56:28 -0400282 rootnode->indirect_levels);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500283
284errout:
Jim Meyering45e338f2009-02-23 18:07:50 +0100285 free(buf);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500286 close_pager(pager);
287}
288
289/*
290 * This function prints the hash of a given file.
291 */
292void do_dx_hash(int argc, char *argv[])
293{
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400294 ext2_dirhash_t hash, minor_hash;
Theodore Ts'o52783e02002-03-11 15:04:45 -0500295 errcode_t err;
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400296 int c;
297 int hash_version = 0;
298 __u32 hash_seed[4];
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400299
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400300 hash_seed[0] = hash_seed[1] = hash_seed[2] = hash_seed[3] = 0;
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400301
302 reset_getopt();
Theodore Ts'of8bd5512008-09-10 11:30:46 -0400303 while ((c = getopt (argc, argv, "h:s:")) != EOF) {
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400304 switch (c) {
305 case 'h':
Theodore Ts'of8bd5512008-09-10 11:30:46 -0400306 hash_version = e2p_string2hash(optarg);
307 if (hash_version < 0)
308 hash_version = atoi(optarg);
309 break;
310 case 's':
Theodore Ts'o42080a82009-07-11 23:23:16 -0400311 if (uuid_parse(optarg, (unsigned char *) hash_seed)) {
Theodore Ts'of8bd5512008-09-10 11:30:46 -0400312 fprintf(stderr, "Invalid UUID format: %s\n",
313 optarg);
314 return;
315 }
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400316 break;
Theodore Ts'o49c6b4e2006-04-27 20:59:42 -0400317 default:
318 goto print_usage;
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400319 }
320 }
321 if (optind != argc-1) {
Theodore Ts'o49c6b4e2006-04-27 20:59:42 -0400322 print_usage:
Theodore Ts'of8bd5512008-09-10 11:30:46 -0400323 com_err(argv[0], 0, "usage: dx_hash [-h hash_alg] "
324 "[-s hash_seed] filename");
Theodore Ts'odf614db2002-02-25 04:28:45 -0500325 return;
326 }
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400327 err = ext2fs_dirhash(hash_version, argv[optind], strlen(argv[optind]),
328 hash_seed, &hash, &minor_hash);
Theodore Ts'o52783e02002-03-11 15:04:45 -0500329 if (err) {
330 com_err(argv[0], err, "while caclulating hash");
331 return;
332 }
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400333 printf("Hash of %s is 0x%0x (minor 0x%0x)\n", argv[optind],
334 hash, minor_hash);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500335}
336
337/*
338 * Search for particular directory entry (useful for debugging very
339 * large hash tree directories that have lost some blocks from the
340 * btree index).
341 */
342struct process_block_struct {
343 char *search_name;
344 char *buf;
345 int len;
346};
347
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400348static int search_dir_block(ext2_filsys fs, blk64_t *blocknr,
349 e2_blkcnt_t blockcnt, blk64_t ref_blk,
Theodore Ts'odf614db2002-02-25 04:28:45 -0500350 int ref_offset, void *priv_data);
351
352void do_dirsearch(int argc, char *argv[])
353{
354 ext2_ino_t inode;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500355 struct process_block_struct pb;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400356
Theodore Ts'odf614db2002-02-25 04:28:45 -0500357 if (check_fs_open(argv[0]))
358 return;
359
360 if (argc != 3) {
361 com_err(0, 0, "Usage: dirsearch dir filename");
362 return;
363 }
364
365 inode = string_to_inode(argv[1]);
366 if (!inode)
367 return;
368
369 pb.buf = malloc(current_fs->blocksize);
370 if (!pb.buf) {
371 com_err("dirsearch", 0, "Couldn't allocate buffer");
372 return;
373 }
374 pb.search_name = argv[2];
375 pb.len = strlen(pb.search_name);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400376
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400377 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, 0,
Theodore Ts'o43323be2008-02-07 14:37:17 -0500378 search_dir_block, &pb);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500379
380 free(pb.buf);
381}
382
383
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400384static int search_dir_block(ext2_filsys fs, blk64_t *blocknr,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400385 e2_blkcnt_t blockcnt,
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400386 blk64_t ref_blk EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -0500387 int ref_offset EXT2FS_ATTR((unused)),
388 void *priv_data)
Theodore Ts'odf614db2002-02-25 04:28:45 -0500389{
390 struct process_block_struct *p;
391 struct ext2_dir_entry *dirent;
392 errcode_t errcode;
Theodore Ts'o54434922003-12-07 01:28:50 -0500393 unsigned int offset = 0;
Theodore Ts'o8a480352009-06-21 21:07:38 -0400394 unsigned int rec_len;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500395
396 if (blockcnt < 0)
397 return 0;
398
399 p = (struct process_block_struct *) priv_data;
400
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400401 errcode = io_channel_read_blk64(current_fs->io, *blocknr, 1, p->buf);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500402 if (errcode) {
403 com_err("search_dir_block", errcode,
Theodore Ts'o54434922003-12-07 01:28:50 -0500404 "while reading block %lu", (unsigned long) *blocknr);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500405 return BLOCK_ABORT;
406 }
407
408 while (offset < fs->blocksize) {
409 dirent = (struct ext2_dir_entry *) (p->buf + offset);
Theodore Ts'o8a480352009-06-21 21:07:38 -0400410 errcode = ext2fs_get_rec_len(fs, dirent, &rec_len);
411 if (errcode) {
412 com_err("htree_dump_leaf_inode", errcode,
413 "while getting rec_len for block %lu",
414 (unsigned long) *blocknr);
Theodore Ts'o42080a82009-07-11 23:23:16 -0400415 return BLOCK_ABORT;
Theodore Ts'o8a480352009-06-21 21:07:38 -0400416 }
Theodore Ts'odf614db2002-02-25 04:28:45 -0500417 if (dirent->inode &&
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400418 p->len == (dirent->name_len & 0xFF) &&
Theodore Ts'odf614db2002-02-25 04:28:45 -0500419 strncmp(p->search_name, dirent->name,
420 p->len) == 0) {
421 printf("Entry found at logical block %lld, "
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400422 "phys %llu, offset %u\n", (long long)blockcnt,
Theodore Ts'odf614db2002-02-25 04:28:45 -0500423 *blocknr, offset);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500424 printf("offset %u\n", offset);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500425 return BLOCK_ABORT;
426 }
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400427 offset += rec_len;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500428 }
429 return 0;
430}
431