• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * htree.c --- hash tree routines
3  *
4  * Copyright (C) 2002 Theodore Ts'o.  This file may be redistributed
5  * under the terms of the GNU Public License.
6  */
7 
8 #include "config.h"
9 #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>
21 #else
22 extern int optind;
23 extern char *optarg;
24 #endif
25 
26 #include "debugfs.h"
27 #include "uuid/uuid.h"
28 #include "e2p/e2p.h"
29 
30 static FILE *pager;
31 
htree_dump_leaf_node(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,struct ext2_dx_root_info * rootnode,blk64_t blk,char * buf)32 static void htree_dump_leaf_node(ext2_filsys fs, ext2_ino_t ino,
33 				 struct ext2_inode *inode,
34 				 struct ext2_dx_root_info * rootnode,
35 				 blk64_t blk, char *buf)
36 {
37 	errcode_t	errcode;
38 	struct ext2_dir_entry *dirent;
39 	int		thislen, col = 0;
40 	unsigned int	offset = 0;
41 	char		name[EXT2_NAME_LEN + 1];
42 	char		tmp[EXT2_NAME_LEN + 64];
43 	blk64_t		pblk;
44 	ext2_dirhash_t 	hash, minor_hash;
45 	unsigned int	rec_len;
46 	int		hash_alg;
47 	int		csum_size = 0;
48 
49 	if (ext2fs_has_feature_metadata_csum(fs->super))
50 		csum_size = sizeof(struct ext2_dir_entry_tail);
51 
52 	errcode = ext2fs_bmap2(fs, ino, inode, buf, 0, blk, 0, &pblk);
53 	if (errcode) {
54 		com_err("htree_dump_leaf_node", errcode,
55 			"while mapping logical block %llu\n", blk);
56 		return;
57 	}
58 
59 	fprintf(pager, "Reading directory block %llu, phys %llu\n", blk, pblk);
60 	errcode = ext2fs_read_dir_block4(current_fs, pblk, buf, 0, ino);
61 	if (errcode) {
62 		com_err("htree_dump_leaf_node", errcode,
63 			"while reading block %llu (%llu)\n",
64 			blk, pblk);
65 		return;
66 	}
67 	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;
71 
72 	while (offset < fs->blocksize) {
73 		dirent = (struct ext2_dir_entry *) (buf + offset);
74 		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 		}
81 		thislen = ext2fs_dirent_name_len(dirent);
82 		if (((offset + rec_len) > fs->blocksize) ||
83 		    (rec_len < 8) ||
84 		    ((rec_len % 4) != 0) ||
85 		    ((unsigned) thislen + 8 > rec_len)) {
86 			fprintf(pager, "Corrupted directory block (%llu)!\n",
87 				blk);
88 			break;
89 		}
90 		strncpy(name, dirent->name, thislen);
91 		name[thislen] = '\0';
92 		errcode = ext2fs_dirhash(hash_alg, name,
93 					 thislen, fs->super->s_hash_seed,
94 					 &hash, &minor_hash);
95 		if (errcode)
96 			com_err("htree_dump_leaf_node", errcode,
97 				"while calculating hash");
98 		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 		}
115 		thislen = strlen(tmp);
116 		if (col + thislen > 80) {
117 			fprintf(pager, "\n");
118 			col = 0;
119 		}
120 		fprintf(pager, "%s", tmp);
121 		col += thislen;
122 		offset += rec_len;
123 	}
124 	fprintf(pager, "\n");
125 }
126 
127 
128 static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
129 				 struct ext2_inode *inode,
130 				 struct ext2_dx_root_info * rootnode,
131 				 blk64_t blk, char *buf, int level);
132 
133 
htree_dump_int_node(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,struct ext2_dx_root_info * rootnode,struct ext2_dx_entry * ent,char * buf,int level)134 static void htree_dump_int_node(ext2_filsys fs, ext2_ino_t ino,
135 				struct ext2_inode *inode,
136 				struct ext2_dx_root_info * rootnode,
137 				struct ext2_dx_entry *ent,
138 				char *buf, int level)
139 {
140 	struct ext2_dx_countlimit	dx_countlimit;
141 	struct ext2_dx_tail		*tail;
142 	int				hash, i;
143 	int				limit, count;
144 	int				remainder;
145 
146 	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);
149 
150 	fprintf(pager, "Number of entries (count): %d\n", count);
151 	fprintf(pager, "Number of entries (limit): %d\n", limit);
152 
153 	remainder = fs->blocksize - (limit * sizeof(struct ext2_dx_entry));
154 	if (ent == (struct ext2_dx_entry *)(rootnode + 1))
155 		remainder -= sizeof(struct ext2_dx_root_info) + 24;
156 	else
157 		remainder -= 8;
158 	if (ext2fs_has_feature_metadata_csum(fs->super) &&
159 	    remainder == sizeof(struct ext2_dx_tail)) {
160 		tail = (struct ext2_dx_tail *)(ent + limit);
161 		fprintf(pager, "Checksum: 0x%08x\n",
162 			ext2fs_le32_to_cpu(tail->dt_checksum));
163 	}
164 
165 	for (i=0; i < count; i++) {
166 		hash = i ? ext2fs_le32_to_cpu(ent[i].hash) : 0;
167 		fprintf(pager, "Entry #%d: Hash 0x%08x%s, block %u\n", i,
168 			hash, (hash & 1) ? " (**)" : "",
169 			ext2fs_le32_to_cpu(ent[i].block));
170 		}
171 
172 	fprintf(pager, "\n");
173 
174 	for (i=0; i < count; i++) {
175 		unsigned int hashval, block;
176 
177 		hashval = ext2fs_le32_to_cpu(ent[i].hash);
178 		block = ext2fs_le32_to_cpu(ent[i].block);
179 		fprintf(pager, "Entry #%d: Hash 0x%08x, block %u\n", i,
180 		       i ? hashval : 0, block);
181 		if (level)
182 			htree_dump_int_block(fs, ino, inode, rootnode,
183 					     block, buf, level-1);
184 		else
185 			htree_dump_leaf_node(fs, ino, inode, rootnode,
186 					     block, buf);
187 	}
188 
189 	fprintf(pager, "---------------------\n");
190 }
191 
htree_dump_int_block(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,struct ext2_dx_root_info * rootnode,blk64_t blk,char * buf,int level)192 static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
193 				 struct ext2_inode *inode,
194 				 struct ext2_dx_root_info * rootnode,
195 				 blk64_t blk, char *buf, int level)
196 {
197 	char		*cbuf;
198 	errcode_t	errcode;
199 	blk64_t		pblk;
200 
201 	cbuf = malloc(fs->blocksize);
202 	if (!cbuf) {
203 		fprintf(pager, "Couldn't allocate child block.\n");
204 		return;
205 	}
206 
207 	errcode = ext2fs_bmap2(fs, ino, inode, buf, 0, blk, 0, &pblk);
208 	if (errcode) {
209 		com_err("htree_dump_int_block", errcode,
210 			"while mapping logical block %llu\n", blk);
211 		goto errout;
212 	}
213 
214 	errcode = io_channel_read_blk64(current_fs->io, pblk, 1, buf);
215 	if (errcode) {
216 		com_err("htree_dump_int_block", errcode,
217 			"while 	reading block %llu\n", blk);
218 		goto errout;
219 	}
220 
221 	htree_dump_int_node(fs, ino, inode, rootnode,
222 			    (struct ext2_dx_entry *) (buf+8),
223 			    cbuf, level);
224 errout:
225 	free(cbuf);
226 }
227 
228 
229 
do_htree_dump(int argc,char * argv[],int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))230 void do_htree_dump(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
231 		   void *infop EXT2FS_ATTR((unused)))
232 {
233 	ext2_ino_t	ino;
234 	struct ext2_inode inode;
235 	blk64_t		blk;
236 	char		*buf = NULL;
237 	struct 		ext2_dx_root_info  *rootnode;
238 	struct 		ext2_dx_entry *ent;
239 	errcode_t	errcode;
240 
241 	if (check_fs_open(argv[0]))
242 		return;
243 
244 	pager = open_pager();
245 
246 	if (common_inode_args_process(argc, argv, &ino, 0))
247 		goto errout;
248 
249 	if (debugfs_read_inode(ino, &inode, argv[1]))
250 		goto errout;
251 
252 	if (!LINUX_S_ISDIR(inode.i_mode)) {
253 		com_err(argv[0], 0, "Not a directory");
254 		goto errout;
255 	}
256 
257 	if ((inode.i_flags & EXT2_BTREE_FL) == 0) {
258 		com_err(argv[0], 0, "Not a hash-indexed directory");
259 		goto errout;
260 	}
261 
262 	buf = malloc(2*current_fs->blocksize);
263 	if (!buf) {
264 		com_err(argv[0], 0, "Couldn't allocate htree buffer");
265 		goto errout;
266 	}
267 
268 	errcode = ext2fs_bmap2(current_fs, ino, &inode, buf, 0, 0, 0, &blk);
269 	if (errcode) {
270 		com_err("do_htree_block", errcode,
271 			"while mapping logical block 0\n");
272 		goto errout;
273 	}
274 
275 	errcode = io_channel_read_blk64(current_fs->io, blk,
276 					1, buf);
277 	if (errcode) {
278 		com_err(argv[0], errcode, "Error reading root node");
279 		goto errout;
280 	}
281 
282 	rootnode = (struct ext2_dx_root_info *) (buf + 24);
283 
284 	fprintf(pager, "Root node dump:\n");
285 	fprintf(pager, "\t Reserved zero: %u\n", rootnode->reserved_zero);
286 	fprintf(pager, "\t Hash Version: %d\n", rootnode->hash_version);
287 	fprintf(pager, "\t Info length: %d\n", rootnode->info_length);
288 	fprintf(pager, "\t Indirect levels: %d\n", rootnode->indirect_levels);
289 	fprintf(pager, "\t Flags: %d\n", rootnode->unused_flags);
290 
291 	ent = (struct ext2_dx_entry *)
292 		((char *)rootnode + rootnode->info_length);
293 
294 	htree_dump_int_node(current_fs, ino, &inode, rootnode, ent,
295 			    buf + current_fs->blocksize,
296 			    rootnode->indirect_levels);
297 
298 errout:
299 	free(buf);
300 	close_pager(pager);
301 }
302 
303 /*
304  * This function prints the hash of a given file.
305  */
do_dx_hash(int argc,char * argv[],int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))306 void do_dx_hash(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
307 		void *infop EXT2FS_ATTR((unused)))
308 {
309 	ext2_dirhash_t hash, minor_hash;
310 	errcode_t	err;
311 	int		c;
312 	int		hash_version = 0;
313 	__u32		hash_seed[4];
314 
315 	hash_seed[0] = hash_seed[1] = hash_seed[2] = hash_seed[3] = 0;
316 
317 	reset_getopt();
318 	while ((c = getopt (argc, argv, "h:s:")) != EOF) {
319 		switch (c) {
320 		case 'h':
321 			hash_version = e2p_string2hash(optarg);
322 			if (hash_version < 0)
323 				hash_version = atoi(optarg);
324 			break;
325 		case 's':
326 			if (uuid_parse(optarg, (unsigned char *) hash_seed)) {
327 				fprintf(stderr, "Invalid UUID format: %s\n",
328 					optarg);
329 				return;
330 			}
331 			break;
332 		default:
333 			goto print_usage;
334 		}
335 	}
336 	if (optind != argc-1) {
337 	print_usage:
338 		com_err(argv[0], 0, "usage: dx_hash [-h hash_alg] "
339 			"[-s hash_seed] filename");
340 		return;
341 	}
342 	err = ext2fs_dirhash(hash_version, argv[optind], strlen(argv[optind]),
343 			     hash_seed, &hash, &minor_hash);
344 	if (err) {
345 		com_err(argv[0], err, "while calculating hash");
346 		return;
347 	}
348 	printf("Hash of %s is 0x%0x (minor 0x%0x)\n", argv[optind],
349 	       hash, minor_hash);
350 }
351 
352 /*
353  * Search for particular directory entry (useful for debugging very
354  * large hash tree directories that have lost some blocks from the
355  * btree index).
356  */
357 struct process_block_struct {
358 	char	*search_name;
359 	char	*buf;
360 	int	len;
361 };
362 
363 static int search_dir_block(ext2_filsys fs, blk64_t *blocknr,
364 			    e2_blkcnt_t blockcnt, blk64_t ref_blk,
365 			    int ref_offset, void *priv_data);
366 
do_dirsearch(int argc,char * argv[],int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))367 void do_dirsearch(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
368 		  void *infop EXT2FS_ATTR((unused)))
369 {
370 	ext2_ino_t	inode;
371 	struct process_block_struct pb;
372 
373 	if (check_fs_open(argv[0]))
374 		return;
375 
376 	if (argc != 3) {
377 		com_err(0, 0, "Usage: dirsearch dir filename");
378 		return;
379 	}
380 
381 	inode = string_to_inode(argv[1]);
382 	if (!inode)
383 		return;
384 
385 	pb.buf = malloc(current_fs->blocksize);
386 	if (!pb.buf) {
387 		com_err("dirsearch", 0, "Couldn't allocate buffer");
388 		return;
389 	}
390 	pb.search_name = argv[2];
391 	pb.len = strlen(pb.search_name);
392 
393 	ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, 0,
394 			      search_dir_block, &pb);
395 
396 	free(pb.buf);
397 }
398 
399 
search_dir_block(ext2_filsys fs,blk64_t * blocknr,e2_blkcnt_t blockcnt,blk64_t ref_blk EXT2FS_ATTR ((unused)),int ref_offset EXT2FS_ATTR ((unused)),void * priv_data)400 static int search_dir_block(ext2_filsys fs, blk64_t *blocknr,
401 			    e2_blkcnt_t blockcnt,
402 			    blk64_t ref_blk EXT2FS_ATTR((unused)),
403 			    int ref_offset EXT2FS_ATTR((unused)),
404 			    void *priv_data)
405 {
406 	struct process_block_struct *p;
407 	struct ext2_dir_entry *dirent;
408 	errcode_t	       	errcode;
409 	unsigned int		offset = 0;
410 	unsigned int		rec_len;
411 
412 	if (blockcnt < 0)
413 		return 0;
414 
415 	p = (struct process_block_struct *) priv_data;
416 
417 	errcode = io_channel_read_blk64(current_fs->io, *blocknr, 1, p->buf);
418 	if (errcode) {
419 		com_err("search_dir_block", errcode,
420 			"while reading block %lu", (unsigned long) *blocknr);
421 		return BLOCK_ABORT;
422 	}
423 
424 	while (offset < fs->blocksize) {
425 		dirent = (struct ext2_dir_entry *) (p->buf + offset);
426 		errcode = ext2fs_get_rec_len(fs, dirent, &rec_len);
427 		if (errcode) {
428 			com_err("htree_dump_leaf_inode", errcode,
429 				"while getting rec_len for block %lu",
430 				(unsigned long) *blocknr);
431 			return BLOCK_ABORT;
432 		}
433 		if (dirent->inode &&
434 		    p->len == ext2fs_dirent_name_len(dirent) &&
435 		    strncmp(p->search_name, dirent->name,
436 			    p->len) == 0) {
437 			printf("Entry found at logical block %lld, "
438 			       "phys %llu, offset %u\n", (long long)blockcnt,
439 			       *blocknr, offset);
440 			printf("offset %u\n", offset);
441 			return BLOCK_ABORT;
442 		}
443 		offset += rec_len;
444 	}
445 	return 0;
446 }
447 
448