pass2.c revision 7cf73dcd3d173d88ceab26d381f4abac362d8518
1/*
2 * pass2.c --- check directory structure
3 *
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 *
11 * Pass 2 of e2fsck iterates through all active directory inodes, and
12 * applies to following tests to each directory entry in the directory
13 * blocks in the inodes:
14 *
15 *	- The length of the directory entry (rec_len) should be at
16 * 		least 8 bytes, and no more than the remaining space
17 * 		left in the directory block.
18 * 	- The length of the name in the directory entry (name_len)
19 * 		should be less than (rec_len - 8).
20 *	- The inode number in the directory entry should be within
21 * 		legal bounds.
22 * 	- The inode number should refer to a in-use inode.
23 *	- The first entry should be '.', and its inode should be
24 * 		the inode of the directory.
25 * 	- The second entry should be '..'.
26 *
27 * To minimize disk seek time, the directory blocks are processed in
28 * sorted order of block numbers.
29 *
30 * Pass 2 also collects the following information:
31 * 	- The inode numbers of the subdirectories for each directory.
32 *
33 * Pass 2 relies on the following information from previous passes:
34 * 	- The directory information collected in pass 1.
35 * 	- The inode_used_map bitmap
36 * 	- The inode_bad_map bitmap
37 * 	- The inode_dir_map bitmap
38 *
39 * Pass 2 frees the following data structures
40 * 	- The inode_bad_map bitmap
41 */
42
43#include "et/com_err.h"
44
45#include "e2fsck.h"
46#include "problem.h"
47
48/*
49 * Keeps track of how many times an inode is referenced.
50 */
51ext2_icount_t inode_count = 0;
52
53static void deallocate_inode(ext2_filsys fs, ino_t ino,
54			     char* block_buf);
55static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino);
56static int check_dir_block(ext2_filsys fs,
57			   struct ext2_db_entry *dir_blocks_info,
58			   void *private);
59static int allocate_dir_block(ext2_filsys fs,
60			      struct ext2_db_entry *dir_blocks_info,
61			      char *buf, struct problem_context *pctx);
62static int update_dir_block(ext2_filsys fs,
63			    blk_t	*block_nr,
64			    int blockcnt,
65			    void *private);
66
67struct check_dir_struct {
68	char *buf;
69	struct problem_context	pctx;
70};
71
72void pass2(ext2_filsys fs)
73{
74	char	*buf;
75	struct resource_track	rtrack;
76	struct dir_info *dir;
77	errcode_t	retval;
78	ino_t		size;
79	struct check_dir_struct cd;
80
81	init_resource_track(&rtrack);
82
83#ifdef MTRACE
84	mtrace_print("Pass 2");
85#endif
86
87	if (!preen)
88		printf("Pass 2: Checking directory structure\n");
89	retval = ext2fs_create_icount2(fs, EXT2_ICOUNT_OPT_INCREMENT,
90				       0, inode_link_info, &inode_count);
91	if (retval) {
92		com_err("ext2fs_create_icount", retval,
93			"while creating inode_count");
94		fatal_error(0);
95	}
96	buf = allocate_memory(fs->blocksize, "directory scan buffer");
97
98	/*
99	 * Set up the parent pointer for the root directory, if
100	 * present.  (If the root directory is not present, we will
101	 * create it in pass 3.)
102	 */
103	dir = get_dir_info(EXT2_ROOT_INO);
104	if (dir)
105		dir->parent = EXT2_ROOT_INO;
106
107	cd.buf = buf;
108	clear_problem_context(&cd.pctx);
109
110	retval = ext2fs_dblist_iterate(fs->dblist, check_dir_block, &cd);
111	if (retval) {
112		com_err("ext2fs_dblist_iterate", retval,
113			"while iterating through dblist");
114		fatal_error(0);
115	}
116
117	free(buf);
118	ext2fs_free_dblist(fs->dblist);
119
120	if (inode_bad_map) {
121		ext2fs_free_inode_bitmap(inode_bad_map);
122		inode_bad_map = 0;
123	}
124	if (tflag > 1) {
125		printf("Pass 2: ");
126		print_resource_track(&rtrack);
127	}
128}
129
130/*
131 * Make sure the first entry in the directory is '.', and that the
132 * directory entry is sane.
133 */
134static int check_dot(ext2_filsys fs,
135		     struct ext2_dir_entry *dirent,
136		     ino_t ino, struct problem_context *pctx)
137{
138	struct ext2_dir_entry *nextdir;
139	int	status = 0;
140	int	created = 0;
141	int	new_len;
142	int	problem = 0;
143
144	if (!dirent->inode)
145		problem = PR_2_MISSING_DOT;
146	else if ((dirent->name_len != 1) ||
147		 (dirent->name[0] != '.'))
148		problem = PR_2_1ST_NOT_DOT;
149	else if (dirent->name[1] != '\0')
150		problem = PR_2_DOT_NULL_TERM;
151
152	if (problem) {
153		if (fix_problem(fs, problem, pctx)) {
154			if (dirent->rec_len < 12)
155				dirent->rec_len = 12;
156			dirent->inode = ino;
157			dirent->name_len = 1;
158			dirent->name[0] = '.';
159			dirent->name[1] = '\0';
160			status = 1;
161			created = 1;
162		}
163	}
164	if (dirent->inode != ino) {
165		if (fix_problem(fs, PR_2_BAD_INODE_DOT, pctx)) {
166			dirent->inode = ino;
167			status = 1;
168		}
169	}
170	if (dirent->rec_len > 12) {
171		new_len = dirent->rec_len - 12;
172		if (new_len > 12) {
173			preenhalt(fs);
174			if (created ||
175			    ask("Directory entry for '.' is big.  Split", 1)) {
176				nextdir = (struct ext2_dir_entry *)
177					((char *) dirent + 12);
178				dirent->rec_len = 12;
179				nextdir->rec_len = new_len;
180				nextdir->inode = 0;
181				nextdir->name_len = 0;
182				status = 1;
183			}
184		}
185	}
186	return status;
187}
188
189/*
190 * Make sure the second entry in the directory is '..', and that the
191 * directory entry is sane.  We do not check the inode number of '..'
192 * here; this gets done in pass 3.
193 */
194static int check_dotdot(ext2_filsys fs,
195			struct ext2_dir_entry *dirent,
196			struct dir_info *dir, struct problem_context *pctx)
197{
198	int		problem = 0;
199
200	if (!dirent->inode)
201		problem = PR_2_MISSING_DOT_DOT;
202	else if ((dirent->name_len != 2) ||
203		 (dirent->name[0] != '.') ||
204		 (dirent->name[1] != '.'))
205		problem = PR_2_2ND_NOT_DOT_DOT;
206	else if (dirent->name[2] != '\0')
207		problem = PR_2_DOT_DOT_NULL_TERM;
208
209	if (problem) {
210		if (fix_problem(fs, problem, pctx)) {
211			if (dirent->rec_len < 12)
212				dirent->rec_len = 12;
213			/*
214			 * Note: we don't have the parent inode just
215			 * yet, so we will fill it in with the root
216			 * inode.  This will get fixed in pass 3.
217			 */
218			dirent->inode = EXT2_ROOT_INO;
219			dirent->name_len = 2;
220			dirent->name[0] = '.';
221			dirent->name[1] = '.';
222			dirent->name[2] = '\0';
223			return 1;
224		}
225		return 0;
226	}
227	dir->dotdot = dirent->inode;
228	return 0;
229}
230
231/*
232 * Check to make sure a directory entry doesn't contain any illegal
233 * characters.
234 */
235static int check_name(ext2_filsys fs,
236		      struct ext2_dir_entry *dirent,
237		      ino_t dir_ino, struct problem_context *pctx)
238{
239	int	i;
240	int	fixup = -1;
241	int	ret = 0;
242
243	for ( i = 0; i < dirent->name_len; i++) {
244		if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
245			if (fixup < 0) {
246				fixup = fix_problem(fs, PR_2_BAD_NAME, pctx);
247			}
248			if (fixup) {
249				dirent->name[i] = '.';
250				ret = 1;
251			}
252		}
253	}
254	return ret;
255}
256
257static int check_dir_block(ext2_filsys fs,
258			   struct ext2_db_entry *db,
259			   void *private)
260{
261	struct dir_info		*subdir, *dir;
262	struct ext2_dir_entry 	*dirent;
263	int			offset = 0;
264	int			dir_modified = 0;
265	errcode_t		retval;
266	int			dot_state;
267	blk_t			block_nr = db->blk;
268	ino_t 			ino = db->ino;
269	__u16			links;
270	struct check_dir_struct	*cd = private;
271	char 			*buf = cd->buf;
272
273	/*
274	 * Make sure the inode is still in use (could have been
275	 * deleted in the duplicate/bad blocks pass.
276	 */
277	if (!(ext2fs_test_inode_bitmap(inode_used_map, ino)))
278		return 0;
279
280	cd->pctx.ino = ino;
281	cd->pctx.blk = block_nr;
282	cd->pctx.blkcount = db->blockcnt;
283	cd->pctx.ino2 = 0;
284	cd->pctx.dirent = 0;
285	cd->pctx.num = 0;
286
287	if (db->blk == 0) {
288		if (allocate_dir_block(fs, db, buf, &cd->pctx))
289			return 0;
290		block_nr = db->blk;
291	}
292
293	if (db->blockcnt)
294		dot_state = 2;
295	else
296		dot_state = 0;
297
298#if 0
299	printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
300	       db->blockcnt, ino);
301#endif
302
303	retval = ext2fs_read_dir_block(fs, block_nr, buf);
304	if (retval) {
305		com_err(program_name, retval,
306			"while reading directory block %d", block_nr);
307	}
308
309	do {
310		dot_state++;
311		dirent = (struct ext2_dir_entry *) (buf + offset);
312		cd->pctx.dirent = dirent;
313		cd->pctx.num = offset;
314		if (((offset + dirent->rec_len) > fs->blocksize) ||
315		    (dirent->rec_len < 8) ||
316		    ((dirent->rec_len % 4) != 0) ||
317		    ((dirent->name_len+8) > dirent->rec_len)) {
318			if (fix_problem(fs, PR_2_DIR_CORRUPTED, &cd->pctx)) {
319				dirent->rec_len = fs->blocksize - offset;
320				dirent->name_len = 0;
321				dirent->inode = 0;
322				dir_modified++;
323			} else
324				return DIRENT_ABORT;
325		}
326
327		if (dirent->name_len > EXT2_NAME_LEN) {
328			if (fix_problem(fs, PR_2_FILENAME_LONG, &cd->pctx)) {
329				dirent->name_len = EXT2_NAME_LEN;
330				dir_modified++;
331			}
332		}
333
334		if (dot_state == 1) {
335			if (check_dot(fs, dirent, ino, &cd->pctx))
336				dir_modified++;
337		} else if (dot_state == 2) {
338			dir = get_dir_info(ino);
339			if (!dir) {
340				printf("Internal error: couldn't find dir_info for %lu\n",
341				       ino);
342				fatal_error(0);
343			}
344			if (check_dotdot(fs, dirent, dir, &cd->pctx))
345				dir_modified++;
346		} else if (dirent->inode == ino) {
347			if (fix_problem(fs, PR_2_LINK_DOT, &cd->pctx)) {
348				dirent->inode = 0;
349				dir_modified++;
350				goto next;
351			}
352		}
353		if (!dirent->inode)
354			goto next;
355
356		if (check_name(fs, dirent, ino, &cd->pctx))
357			dir_modified++;
358
359		/*
360		 * Make sure the inode listed is a legal one.
361		 */
362		if (((dirent->inode != EXT2_ROOT_INO) &&
363		     (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
364		    (dirent->inode > fs->super->s_inodes_count)) {
365			if (fix_problem(fs, PR_2_BAD_INO, &cd->pctx)) {
366				dirent->inode = 0;
367				dir_modified++;
368				goto next;
369			}
370		}
371
372		/*
373		 * If the inode is unused, offer to clear it.
374		 */
375		if (!(ext2fs_test_inode_bitmap(inode_used_map,
376					       dirent->inode))) {
377			if (fix_problem(fs, PR_2_UNUSED_INODE, &cd->pctx)) {
378				dirent->inode = 0;
379				dir_modified++;
380				goto next;
381			}
382		}
383
384		/*
385		 * If the inode is in a bad block, offer to clear it.
386		 */
387		if (inode_bb_map &&
388		    (ext2fs_test_inode_bitmap(inode_bb_map,
389					      dirent->inode))) {
390			if (fix_problem(fs, PR_2_BB_INODE, &cd->pctx)) {
391				dirent->inode = 0;
392				dir_modified++;
393				goto next;
394			}
395		}
396
397		/*
398		 * If the inode was marked as having bad fields in
399		 * pass1, process it and offer to fix/clear it.
400		 * (We wait until now so that we can display the
401		 * pathname to the user.)
402		 */
403		if (inode_bad_map &&
404		    ext2fs_test_inode_bitmap(inode_bad_map,
405					     dirent->inode)) {
406			if (process_bad_inode(fs, ino, dirent->inode)) {
407				dirent->inode = 0;
408				dir_modified++;
409				goto next;
410			}
411		}
412
413		/*
414		 * Don't allow links to the root directory.  We check
415		 * this specially to make sure we catch this error
416		 * case even if the root directory hasn't been created
417		 * yet.
418		 */
419		if ((dot_state > 2) && (dirent->inode == EXT2_ROOT_INO)) {
420			if (fix_problem(fs, PR_2_LINK_ROOT, &cd->pctx)) {
421				dirent->inode = 0;
422				dir_modified++;
423				goto next;
424			}
425		}
426
427		/*
428		 * If this is a directory, then mark its parent in its
429		 * dir_info structure.  If the parent field is already
430		 * filled in, then this directory has more than one
431		 * hard link.  We assume the first link is correct,
432		 * and ask the user if he/she wants to clear this one.
433		 */
434		if ((dot_state > 2) &&
435		    (ext2fs_test_inode_bitmap(inode_dir_map,
436					      dirent->inode))) {
437			subdir = get_dir_info(dirent->inode);
438			if (!subdir) {
439				printf("INTERNAL ERROR: missing dir %u\n",
440				       dirent->inode);
441				fatal_error(0);
442			}
443			if (subdir->parent) {
444				cd->pctx.ino2 = subdir->parent;
445				if (fix_problem(fs, PR_2_LINK_DIR,
446						&cd->pctx)) {
447					dirent->inode = 0;
448					dir_modified++;
449					goto next;
450				}
451				cd->pctx.ino2 = 0;
452			} else
453				subdir->parent = ino;
454		}
455
456		ext2fs_icount_increment(inode_count, dirent->inode, &links);
457		if (links > 1)
458			fs_links_count++;
459		fs_total_count++;
460	next:
461		offset += dirent->rec_len;
462	} while (offset < fs->blocksize);
463#if 0
464	printf("\n");
465#endif
466	if (offset != fs->blocksize) {
467		printf("Final rec_len is %d, should be %d\n",
468		       dirent->rec_len,
469		       dirent->rec_len - fs->blocksize + offset);
470	}
471	if (dir_modified) {
472		retval = ext2fs_write_dir_block(fs, block_nr, buf);
473		if (retval) {
474			com_err(program_name, retval,
475				"while writing directory block %d", block_nr);
476		}
477		ext2fs_mark_changed(fs);
478	}
479	return 0;
480}
481
482/*
483 * This function is called to deallocate a block, and is an interator
484 * functioned called by deallocate inode via ext2fs_iterate_block().
485 */
486static int deallocate_inode_block(ext2_filsys fs,
487			     blk_t	*block_nr,
488			     int blockcnt,
489			     void *private)
490{
491	if (!*block_nr)
492		return 0;
493	ext2fs_unmark_block_bitmap(block_found_map, *block_nr);
494	ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
495	return 0;
496}
497
498/*
499 * This fuction deallocates an inode
500 */
501static void deallocate_inode(ext2_filsys fs, ino_t ino,
502			     char* block_buf)
503{
504	errcode_t		retval;
505	struct ext2_inode	inode;
506
507	ext2fs_icount_store(inode_link_info, ino, 0);
508	e2fsck_read_inode(fs, ino, &inode, "deallocate_inode");
509	inode.i_links_count = 0;
510	inode.i_dtime = time(0);
511	e2fsck_write_inode(fs, ino, &inode, "deallocate_inode");
512
513	/*
514	 * Fix up the bitmaps...
515	 */
516	read_bitmaps(fs);
517	ext2fs_unmark_inode_bitmap(inode_used_map, ino);
518	ext2fs_unmark_inode_bitmap(inode_dir_map, ino);
519	if (inode_bad_map)
520		ext2fs_unmark_inode_bitmap(inode_bad_map, ino);
521	ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
522	ext2fs_mark_ib_dirty(fs);
523
524	if (!ext2fs_inode_has_valid_blocks(&inode))
525		return;
526
527	ext2fs_mark_bb_dirty(fs);
528	retval = ext2fs_block_iterate(fs, ino, 0, block_buf,
529				      deallocate_inode_block, 0);
530	if (retval)
531		com_err("deallocate_inode", retval,
532			"while calling ext2fs_block_iterate for inode %d",
533			ino);
534}
535
536static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino)
537{
538	struct ext2_inode	inode;
539	int			inode_modified = 0;
540	unsigned char		*frag, *fsize;
541	struct problem_context	pctx;
542
543	e2fsck_read_inode(fs, ino, &inode, "process_bad_inode");
544
545	clear_problem_context(&pctx);
546	pctx.ino = ino;
547	pctx.dir = dir;
548	pctx.inode = &inode;
549
550	if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
551	    !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
552	    !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
553	    !(LINUX_S_ISSOCK(inode.i_mode))) {
554		if (fix_problem(fs, PR_2_BAD_MODE, &pctx)) {
555			deallocate_inode(fs, ino, 0);
556			return 1;
557		}
558	}
559
560	if (LINUX_S_ISCHR(inode.i_mode)
561	    && !e2fsck_pass1_check_device_inode(&inode)) {
562		if (fix_problem(fs, PR_2_BAD_CHAR_DEV, &pctx)) {
563			deallocate_inode(fs, ino, 0);
564			return 1;
565		}
566	}
567
568	if (LINUX_S_ISBLK(inode.i_mode)
569	    && !e2fsck_pass1_check_device_inode(&inode)) {
570		if (fix_problem(fs, PR_2_BAD_BLOCK_DEV, &pctx)) {
571			deallocate_inode(fs, ino, 0);
572			return 1;
573		}
574	}
575
576
577	if (inode.i_faddr &&
578	    fix_problem(fs, PR_2_FADDR_ZERO, &pctx)) {
579		inode.i_faddr = 0;
580		inode_modified++;
581	}
582
583	switch (fs->super->s_creator_os) {
584	    case EXT2_OS_LINUX:
585		frag = &inode.osd2.linux2.l_i_frag;
586		fsize = &inode.osd2.linux2.l_i_fsize;
587		break;
588	    case EXT2_OS_HURD:
589		frag = &inode.osd2.hurd2.h_i_frag;
590		fsize = &inode.osd2.hurd2.h_i_fsize;
591		break;
592	    case EXT2_OS_MASIX:
593		frag = &inode.osd2.masix2.m_i_frag;
594		fsize = &inode.osd2.masix2.m_i_fsize;
595		break;
596	    default:
597		frag = fsize = 0;
598	}
599	if (frag && *frag) {
600		pctx.num = *frag;
601		if (fix_problem(fs, PR_2_FRAG_ZERO, &pctx)) {
602			*frag = 0;
603			inode_modified++;
604		}
605		pctx.num = 0;
606	}
607	if (fsize && *fsize) {
608		pctx.num = *fsize;
609		if (fix_problem(fs, PR_2_FSIZE_ZERO, &pctx)) {
610			*fsize = 0;
611			inode_modified++;
612		}
613		pctx.num = 0;
614	}
615
616	if (inode.i_file_acl &&
617	    fix_problem(fs, PR_2_FILE_ACL_ZERO, &pctx)) {
618		inode.i_file_acl = 0;
619		inode_modified++;
620	}
621	if (inode.i_dir_acl &&
622	    fix_problem(fs, PR_2_DIR_ACL_ZERO, &pctx)) {
623		inode.i_dir_acl = 0;
624		inode_modified++;
625	}
626	if (inode_modified)
627		e2fsck_write_inode(fs, ino, &inode, "process_bad_inode");
628	return 0;
629}
630
631
632/*
633 * allocate_dir_block --- this function allocates a new directory
634 * 	block for a particular inode; this is done if a directory has
635 * 	a "hole" in it, or if a directory has a illegal block number
636 * 	that was zeroed out and now needs to be replaced.
637 */
638static int allocate_dir_block(ext2_filsys fs,
639			      struct ext2_db_entry *db,
640			      char *buf, struct problem_context *pctx)
641{
642	blk_t			blk;
643	char			*block;
644	struct ext2_inode	inode;
645	errcode_t		retval;
646
647	if (fix_problem(fs, PR_2_DIRECTORY_HOLE, pctx) == 0)
648		return 1;
649
650	/*
651	 * Read the inode and block bitmaps in; we'll be messing with
652	 * them.
653	 */
654	read_bitmaps(fs);
655
656	/*
657	 * First, find a free block
658	 */
659	retval = ext2fs_new_block(fs, 0, block_found_map, &blk);
660	if (retval) {
661		com_err("allocate_dir_block", retval,
662			"while trying to fill a hole in a directory inode");
663		return 1;
664	}
665	ext2fs_mark_block_bitmap(block_found_map, blk);
666	ext2fs_mark_block_bitmap(fs->block_map, blk);
667	ext2fs_mark_bb_dirty(fs);
668
669	/*
670	 * Now let's create the actual data block for the inode
671	 */
672	if (db->blockcnt)
673		retval = ext2fs_new_dir_block(fs, 0, 0, &block);
674	else
675		retval = ext2fs_new_dir_block(fs, db->ino, EXT2_ROOT_INO,
676					      &block);
677
678	if (retval) {
679		com_err("allocate_dir_block", retval,
680			"while creating new directory block");
681		return 1;
682	}
683
684	retval = ext2fs_write_dir_block(fs, blk, block);
685	free(block);
686	if (retval) {
687		com_err("allocate_dir_block", retval,
688			"while writing an empty directory block");
689		return 1;
690	}
691
692	/*
693	 * Update the inode block count
694	 */
695	e2fsck_read_inode(fs, db->ino, &inode, "allocate_dir_block");
696	inode.i_blocks += fs->blocksize / 512;
697	if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
698		inode.i_size = (db->blockcnt+1) * fs->blocksize;
699	e2fsck_write_inode(fs, db->ino, &inode, "allocate_dir_block");
700
701	/*
702	 * Finally, update the block pointers for the inode
703	 */
704	db->blk = blk;
705	retval = ext2fs_block_iterate(fs, db->ino, BLOCK_FLAG_HOLE,
706				      0, update_dir_block, db);
707	if (retval) {
708		com_err("allocate_dir_block", retval,
709			"while calling ext2fs_block_iterate");
710		return 1;
711	}
712
713	return 0;
714}
715
716/*
717 * This is a helper function for allocate_dir_block().
718 */
719static int update_dir_block(ext2_filsys fs,
720			    blk_t	*block_nr,
721			    int blockcnt,
722			    void *private)
723{
724	struct ext2_db_entry *db = private;
725
726	if (db->blockcnt == blockcnt) {
727		*block_nr = db->blk;
728		return BLOCK_CHANGED;
729	}
730	return 0;
731}
732
733