blob: 54e55e20a73a195acc1d0e5c5d6299113865a4a1 [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
Darrick J. Wong4ee26692015-10-24 01:26:35 -040049 if (ext2fs_has_feature_metadata_csum(fs->super))
Darrick J. Wong81683c62012-08-02 17:27:43 -040050 csum_size = sizeof(struct ext2_dir_entry_tail);
Theodore Ts'oefc6f622008-08-27 23:07:54 -040051
Valerie Aurora Henson048786d2009-09-07 22:46:17 -040052 errcode = ext2fs_bmap2(fs, ino, inode, buf, 0, blk, 0, &pblk);
Theodore Ts'odf614db2002-02-25 04:28:45 -050053 if (errcode) {
54 com_err("htree_dump_leaf_node", errcode,
Valerie Aurora Henson048786d2009-09-07 22:46:17 -040055 "while mapping logical block %llu\n", blk);
Theodore Ts'odf614db2002-02-25 04:28:45 -050056 return;
57 }
58
Theodore Ts'oe6575ce2012-10-14 04:54:20 -040059 fprintf(pager, "Reading directory block %llu, phys %llu\n", blk, pblk);
Darrick J. Wong81683c62012-08-02 17:27:43 -040060 errcode = ext2fs_read_dir_block4(current_fs, pblk, buf, 0, ino);
Theodore Ts'odf614db2002-02-25 04:28:45 -050061 if (errcode) {
62 com_err("htree_dump_leaf_node", errcode,
Valerie Aurora Henson048786d2009-09-07 22:46:17 -040063 "while reading block %llu (%llu)\n",
64 blk, pblk);
Theodore Ts'odf614db2002-02-25 04:28:45 -050065 return;
66 }
Theodore Ts'of77704e2006-11-11 22:32:35 -050067 hash_alg = rootnode->hash_version;
68 if ((hash_alg <= EXT2_HASH_TEA) &&
69 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
70 hash_alg += 3;
Theodore Ts'odf614db2002-02-25 04:28:45 -050071
Theodore Ts'o41bf5992012-10-14 06:34:09 -040072 while (offset < fs->blocksize) {
Theodore Ts'odf614db2002-02-25 04:28:45 -050073 dirent = (struct ext2_dir_entry *) (buf + offset);
Theodore Ts'o8a480352009-06-21 21:07:38 -040074 errcode = ext2fs_get_rec_len(fs, dirent, &rec_len);
75 if (errcode) {
76 com_err("htree_dump_leaf_inode", errcode,
77 "while getting rec_len for block %lu",
78 (unsigned long) blk);
79 return;
80 }
Jan Kara70f46322013-06-08 11:26:14 -040081 thislen = ext2fs_dirent_name_len(dirent);
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'odf0b9072015-07-13 15:36:19 -040085 ((unsigned) thislen + 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 }
Theodore Ts'odf614db2002-02-25 04:28:45 -050090 strncpy(name, dirent->name, thislen);
91 name[thislen] = '\0';
Theodore Ts'of77704e2006-11-11 22:32:35 -050092 errcode = ext2fs_dirhash(hash_alg, name,
Theodore Ts'o3e699062002-10-13 23:56:28 -040093 thislen, fs->super->s_hash_seed,
Theodore Ts'o226515d2008-08-08 11:56:07 -040094 &hash, &minor_hash);
Theodore Ts'o52783e02002-03-11 15:04:45 -050095 if (errcode)
96 com_err("htree_dump_leaf_node", errcode,
97 "while calculating hash");
Theodore Ts'o41bf5992012-10-14 06:34:09 -040098 if ((offset == fs->blocksize - csum_size) &&
99 (dirent->inode == 0) &&
100 (dirent->rec_len == csum_size) &&
101 (dirent->name_len == EXT2_DIR_NAME_LEN_CSUM)) {
102 struct ext2_dir_entry_tail *t;
103
104 t = (struct ext2_dir_entry_tail *) dirent;
105
106 snprintf(tmp, EXT2_NAME_LEN + 64,
107 "leaf block checksum: 0x%08x ",
108 t->det_checksum);
109 } else {
110 snprintf(tmp, EXT2_NAME_LEN + 64,
111 "%u 0x%08x-%08x (%d) %s ",
112 dirent->inode, hash, minor_hash,
113 rec_len, name);
114 }
Theodore Ts'odf614db2002-02-25 04:28:45 -0500115 thislen = strlen(tmp);
116 if (col + thislen > 80) {
117 fprintf(pager, "\n");
118 col = 0;
119 }
120 fprintf(pager, "%s", tmp);
121 col += thislen;
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400122 offset += rec_len;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500123 }
124 fprintf(pager, "\n");
125}
126
127
128static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
129 struct ext2_inode *inode,
Theodore Ts'o3e699062002-10-13 23:56:28 -0400130 struct ext2_dx_root_info * rootnode,
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400131 blk64_t blk, char *buf, int level);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500132
133
134static void htree_dump_int_node(ext2_filsys fs, ext2_ino_t ino,
135 struct ext2_inode *inode,
Theodore Ts'o3e699062002-10-13 23:56:28 -0400136 struct ext2_dx_root_info * rootnode,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400137 struct ext2_dx_entry *ent,
Theodore Ts'odf614db2002-02-25 04:28:45 -0500138 char *buf, int level)
139{
Eric Sandeen77b493e2014-11-04 11:26:21 -0500140 struct ext2_dx_countlimit dx_countlimit;
Darrick J. Wonga9964662012-07-30 19:20:04 -0400141 struct ext2_dx_tail *tail;
Theodore Ts'o42e5b5f2002-09-22 15:27:28 -0400142 int hash, i;
Eric Sandeen77b493e2014-11-04 11:26:21 -0500143 int limit, count;
Darrick J. Wonga9964662012-07-30 19:20:04 -0400144 int remainder;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500145
Eric Sandeen77b493e2014-11-04 11:26:21 -0500146 dx_countlimit = *((struct ext2_dx_countlimit *) ent);
147 count = ext2fs_le16_to_cpu(dx_countlimit.count);
148 limit = ext2fs_le16_to_cpu(dx_countlimit.limit);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500149
Eric Sandeen77b493e2014-11-04 11:26:21 -0500150 fprintf(pager, "Number of entries (count): %d\n", count);
151 fprintf(pager, "Number of entries (limit): %d\n", limit);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500152
Eric Sandeen77b493e2014-11-04 11:26:21 -0500153 remainder = fs->blocksize - (limit * sizeof(struct ext2_dx_entry));
Darrick J. Wonga9964662012-07-30 19:20:04 -0400154 if (ent == (struct ext2_dx_entry *)(rootnode + 1))
155 remainder -= sizeof(struct ext2_dx_root_info) + 24;
156 else
157 remainder -= 8;
Darrick J. Wong4ee26692015-10-24 01:26:35 -0400158 if (ext2fs_has_feature_metadata_csum(fs->super) &&
Darrick J. Wonga9964662012-07-30 19:20:04 -0400159 remainder == sizeof(struct ext2_dx_tail)) {
Eric Sandeen77b493e2014-11-04 11:26:21 -0500160 tail = (struct ext2_dx_tail *)(ent + limit);
Darrick J. Wonga9964662012-07-30 19:20:04 -0400161 fprintf(pager, "Checksum: 0x%08x\n",
162 ext2fs_le32_to_cpu(tail->dt_checksum));
163 }
164
Eric Sandeen77b493e2014-11-04 11:26:21 -0500165 for (i=0; i < count; i++) {
Theodore Ts'o42e5b5f2002-09-22 15:27:28 -0400166 hash = i ? ext2fs_le32_to_cpu(ent[i].hash) : 0;
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400167 fprintf(pager, "Entry #%d: Hash 0x%08x%s, block %u\n", i,
Theodore Ts'o42e5b5f2002-09-22 15:27:28 -0400168 hash, (hash & 1) ? " (**)" : "",
Theodore Ts'o621732c2002-07-18 22:19:51 -0400169 ext2fs_le32_to_cpu(ent[i].block));
Theodore Ts'o42e5b5f2002-09-22 15:27:28 -0400170 }
Theodore Ts'odf614db2002-02-25 04:28:45 -0500171
172 fprintf(pager, "\n");
173
Eric Sandeen77b493e2014-11-04 11:26:21 -0500174 for (i=0; i < count; i++) {
Theodore Ts'odf0b9072015-07-13 15:36:19 -0400175 unsigned int hashval, block;
Eric Sandeen77b493e2014-11-04 11:26:21 -0500176
Theodore Ts'odf0b9072015-07-13 15:36:19 -0400177 hashval = ext2fs_le32_to_cpu(ent[i].hash);
Eric Sandeen77b493e2014-11-04 11:26:21 -0500178 block = ext2fs_le32_to_cpu(ent[i].block);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500179 fprintf(pager, "Entry #%d: Hash 0x%08x, block %u\n", i,
Theodore Ts'odf0b9072015-07-13 15:36:19 -0400180 i ? hashval : 0, block);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500181 if (level)
Theodore Ts'o3e699062002-10-13 23:56:28 -0400182 htree_dump_int_block(fs, ino, inode, rootnode,
Eric Sandeen77b493e2014-11-04 11:26:21 -0500183 block, buf, level-1);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500184 else
Theodore Ts'o3e699062002-10-13 23:56:28 -0400185 htree_dump_leaf_node(fs, ino, inode, rootnode,
Eric Sandeen77b493e2014-11-04 11:26:21 -0500186 block, buf);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500187 }
188
189 fprintf(pager, "---------------------\n");
190}
191
192static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
193 struct ext2_inode *inode,
Theodore Ts'o3e699062002-10-13 23:56:28 -0400194 struct ext2_dx_root_info * rootnode,
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400195 blk64_t blk, char *buf, int level)
Theodore Ts'odf614db2002-02-25 04:28:45 -0500196{
197 char *cbuf;
198 errcode_t errcode;
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400199 blk64_t pblk;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500200
201 cbuf = malloc(fs->blocksize);
202 if (!cbuf) {
203 fprintf(pager, "Couldn't allocate child block.\n");
204 return;
205 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400206
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400207 errcode = ext2fs_bmap2(fs, ino, inode, buf, 0, blk, 0, &pblk);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500208 if (errcode) {
209 com_err("htree_dump_int_block", errcode,
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400210 "while mapping logical block %llu\n", blk);
Brian Behlendorf89456552007-03-21 17:53:33 -0400211 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500212 }
213
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400214 errcode = io_channel_read_blk64(current_fs->io, pblk, 1, buf);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500215 if (errcode) {
216 com_err("htree_dump_int_block", errcode,
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400217 "while reading block %llu\n", blk);
Brian Behlendorf89456552007-03-21 17:53:33 -0400218 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500219 }
220
Theodore Ts'o3e699062002-10-13 23:56:28 -0400221 htree_dump_int_node(fs, ino, inode, rootnode,
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400222 (struct ext2_dx_entry *) (buf+8),
Theodore Ts'odf614db2002-02-25 04:28:45 -0500223 cbuf, level);
Brian Behlendorf89456552007-03-21 17:53:33 -0400224errout:
Theodore Ts'odf614db2002-02-25 04:28:45 -0500225 free(cbuf);
226}
227
228
229
230void do_htree_dump(int argc, char *argv[])
231{
232 ext2_ino_t ino;
233 struct ext2_inode inode;
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400234 blk64_t blk;
Theodore Ts'o3e699062002-10-13 23:56:28 -0400235 char *buf = NULL;
236 struct ext2_dx_root_info *rootnode;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500237 struct ext2_dx_entry *ent;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500238 errcode_t errcode;
239
240 if (check_fs_open(argv[0]))
241 return;
242
243 pager = open_pager();
244
Eric Sandeen1ec5d102011-05-11 11:46:19 -0500245 if (common_inode_args_process(argc, argv, &ino, 0))
Theodore Ts'o155f5772002-07-21 14:17:45 -0400246 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500247
248 if (debugfs_read_inode(ino, &inode, argv[1]))
Theodore Ts'o155f5772002-07-21 14:17:45 -0400249 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500250
251 if (!LINUX_S_ISDIR(inode.i_mode)) {
252 com_err(argv[0], 0, "Not a directory");
Theodore Ts'o155f5772002-07-21 14:17:45 -0400253 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500254 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400255
Theodore Ts'odf614db2002-02-25 04:28:45 -0500256 if ((inode.i_flags & EXT2_BTREE_FL) == 0) {
257 com_err(argv[0], 0, "Not a hash-indexed directory");
Theodore Ts'o155f5772002-07-21 14:17:45 -0400258 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500259 }
260
261 buf = malloc(2*current_fs->blocksize);
262 if (!buf) {
263 com_err(argv[0], 0, "Couldn't allocate htree buffer");
Theodore Ts'o155f5772002-07-21 14:17:45 -0400264 goto errout;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500265 }
266
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400267 errcode = ext2fs_bmap2(current_fs, ino, &inode, buf, 0, 0, 0, &blk);
Theodore Ts'o226515d2008-08-08 11:56:07 -0400268 if (errcode) {
269 com_err("do_htree_block", errcode,
270 "while mapping logical block 0\n");
271 goto errout;
272 }
273
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400274 errcode = io_channel_read_blk64(current_fs->io, blk,
275 1, buf);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500276 if (errcode) {
277 com_err(argv[0], errcode, "Error reading root node");
278 goto errout;
279 }
280
Theodore Ts'o3e699062002-10-13 23:56:28 -0400281 rootnode = (struct ext2_dx_root_info *) (buf + 24);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500282
283 fprintf(pager, "Root node dump:\n");
Takashi Sato8deb80a2006-03-18 21:43:46 -0500284 fprintf(pager, "\t Reserved zero: %u\n", rootnode->reserved_zero);
Theodore Ts'o3e699062002-10-13 23:56:28 -0400285 fprintf(pager, "\t Hash Version: %d\n", rootnode->hash_version);
286 fprintf(pager, "\t Info length: %d\n", rootnode->info_length);
287 fprintf(pager, "\t Indirect levels: %d\n", rootnode->indirect_levels);
288 fprintf(pager, "\t Flags: %d\n", rootnode->unused_flags);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500289
Theodore Ts'o3e699062002-10-13 23:56:28 -0400290 ent = (struct ext2_dx_entry *) (buf + 24 + rootnode->info_length);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500291
Theodore Ts'o3e699062002-10-13 23:56:28 -0400292 htree_dump_int_node(current_fs, ino, &inode, rootnode, ent,
Theodore Ts'odf614db2002-02-25 04:28:45 -0500293 buf + current_fs->blocksize,
Theodore Ts'o3e699062002-10-13 23:56:28 -0400294 rootnode->indirect_levels);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500295
296errout:
Jim Meyering45e338f2009-02-23 18:07:50 +0100297 free(buf);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500298 close_pager(pager);
299}
300
301/*
302 * This function prints the hash of a given file.
303 */
304void do_dx_hash(int argc, char *argv[])
305{
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400306 ext2_dirhash_t hash, minor_hash;
Theodore Ts'o52783e02002-03-11 15:04:45 -0500307 errcode_t err;
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400308 int c;
309 int hash_version = 0;
310 __u32 hash_seed[4];
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400311
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400312 hash_seed[0] = hash_seed[1] = hash_seed[2] = hash_seed[3] = 0;
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400313
314 reset_getopt();
Theodore Ts'of8bd5512008-09-10 11:30:46 -0400315 while ((c = getopt (argc, argv, "h:s:")) != EOF) {
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400316 switch (c) {
317 case 'h':
Theodore Ts'of8bd5512008-09-10 11:30:46 -0400318 hash_version = e2p_string2hash(optarg);
319 if (hash_version < 0)
320 hash_version = atoi(optarg);
321 break;
322 case 's':
Theodore Ts'o42080a82009-07-11 23:23:16 -0400323 if (uuid_parse(optarg, (unsigned char *) hash_seed)) {
Theodore Ts'of8bd5512008-09-10 11:30:46 -0400324 fprintf(stderr, "Invalid UUID format: %s\n",
325 optarg);
326 return;
327 }
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400328 break;
Theodore Ts'o49c6b4e2006-04-27 20:59:42 -0400329 default:
330 goto print_usage;
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400331 }
332 }
333 if (optind != argc-1) {
Theodore Ts'o49c6b4e2006-04-27 20:59:42 -0400334 print_usage:
Theodore Ts'of8bd5512008-09-10 11:30:46 -0400335 com_err(argv[0], 0, "usage: dx_hash [-h hash_alg] "
336 "[-s hash_seed] filename");
Theodore Ts'odf614db2002-02-25 04:28:45 -0500337 return;
338 }
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400339 err = ext2fs_dirhash(hash_version, argv[optind], strlen(argv[optind]),
340 hash_seed, &hash, &minor_hash);
Theodore Ts'o52783e02002-03-11 15:04:45 -0500341 if (err) {
342 com_err(argv[0], err, "while caclulating hash");
343 return;
344 }
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400345 printf("Hash of %s is 0x%0x (minor 0x%0x)\n", argv[optind],
346 hash, minor_hash);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500347}
348
349/*
350 * Search for particular directory entry (useful for debugging very
351 * large hash tree directories that have lost some blocks from the
352 * btree index).
353 */
354struct process_block_struct {
355 char *search_name;
356 char *buf;
357 int len;
358};
359
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400360static int search_dir_block(ext2_filsys fs, blk64_t *blocknr,
361 e2_blkcnt_t blockcnt, blk64_t ref_blk,
Theodore Ts'odf614db2002-02-25 04:28:45 -0500362 int ref_offset, void *priv_data);
363
364void do_dirsearch(int argc, char *argv[])
365{
366 ext2_ino_t inode;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500367 struct process_block_struct pb;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400368
Theodore Ts'odf614db2002-02-25 04:28:45 -0500369 if (check_fs_open(argv[0]))
370 return;
371
372 if (argc != 3) {
373 com_err(0, 0, "Usage: dirsearch dir filename");
374 return;
375 }
376
377 inode = string_to_inode(argv[1]);
378 if (!inode)
379 return;
380
381 pb.buf = malloc(current_fs->blocksize);
382 if (!pb.buf) {
383 com_err("dirsearch", 0, "Couldn't allocate buffer");
384 return;
385 }
386 pb.search_name = argv[2];
387 pb.len = strlen(pb.search_name);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400388
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400389 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, 0,
Theodore Ts'o43323be2008-02-07 14:37:17 -0500390 search_dir_block, &pb);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500391
392 free(pb.buf);
393}
394
395
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400396static int search_dir_block(ext2_filsys fs, blk64_t *blocknr,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400397 e2_blkcnt_t blockcnt,
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400398 blk64_t ref_blk EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -0500399 int ref_offset EXT2FS_ATTR((unused)),
400 void *priv_data)
Theodore Ts'odf614db2002-02-25 04:28:45 -0500401{
402 struct process_block_struct *p;
403 struct ext2_dir_entry *dirent;
404 errcode_t errcode;
Theodore Ts'o54434922003-12-07 01:28:50 -0500405 unsigned int offset = 0;
Theodore Ts'o8a480352009-06-21 21:07:38 -0400406 unsigned int rec_len;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500407
408 if (blockcnt < 0)
409 return 0;
410
411 p = (struct process_block_struct *) priv_data;
412
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400413 errcode = io_channel_read_blk64(current_fs->io, *blocknr, 1, p->buf);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500414 if (errcode) {
415 com_err("search_dir_block", errcode,
Theodore Ts'o54434922003-12-07 01:28:50 -0500416 "while reading block %lu", (unsigned long) *blocknr);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500417 return BLOCK_ABORT;
418 }
419
420 while (offset < fs->blocksize) {
421 dirent = (struct ext2_dir_entry *) (p->buf + offset);
Theodore Ts'o8a480352009-06-21 21:07:38 -0400422 errcode = ext2fs_get_rec_len(fs, dirent, &rec_len);
423 if (errcode) {
424 com_err("htree_dump_leaf_inode", errcode,
425 "while getting rec_len for block %lu",
426 (unsigned long) *blocknr);
Theodore Ts'o42080a82009-07-11 23:23:16 -0400427 return BLOCK_ABORT;
Theodore Ts'o8a480352009-06-21 21:07:38 -0400428 }
Theodore Ts'odf614db2002-02-25 04:28:45 -0500429 if (dirent->inode &&
Jan Kara70f46322013-06-08 11:26:14 -0400430 p->len == ext2fs_dirent_name_len(dirent) &&
Theodore Ts'odf614db2002-02-25 04:28:45 -0500431 strncmp(p->search_name, dirent->name,
432 p->len) == 0) {
433 printf("Entry found at logical block %lld, "
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400434 "phys %llu, offset %u\n", (long long)blockcnt,
Theodore Ts'odf614db2002-02-25 04:28:45 -0500435 *blocknr, offset);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500436 printf("offset %u\n", offset);
Theodore Ts'odf614db2002-02-25 04:28:45 -0500437 return BLOCK_ABORT;
438 }
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400439 offset += rec_len;
Theodore Ts'odf614db2002-02-25 04:28:45 -0500440 }
441 return 0;
442}
443