blob: 16a4da723210405bed99271a21887b59d842e96f [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * pass2.c --- check directory structure
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00004 * 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%
Theodore Ts'oefc6f622008-08-27 23:07:54 -040010 *
Theodore Ts'o3839e651997-04-26 13:21:57 +000011 * 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)
Theodore Ts'oefc6f622008-08-27 23:07:54 -040019 * should be less than (rec_len - 8).
Theodore Ts'o3839e651997-04-26 13:21:57 +000020 * - 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
Theodore Ts'o3839e651997-04-26 13:21:57 +000038 *
39 * Pass 2 frees the following data structures
40 * - The inode_bad_map bitmap
Theodore Ts'oaa4115a1999-10-21 19:33:18 +000041 * - The inode_reg_map bitmap
Theodore Ts'o3839e651997-04-26 13:21:57 +000042 */
43
Matthias Andreeb969b1b2003-12-28 13:04:35 +010044#define _GNU_SOURCE 1 /* get strnlen() */
Theodore Ts'o48e6e812003-07-06 00:36:48 -040045#include <string.h>
46
Theodore Ts'o3839e651997-04-26 13:21:57 +000047#include "e2fsck.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000048#include "problem.h"
Theodore Ts'o09266682003-03-14 22:19:10 -050049#include "dict.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000050
Theodore Ts'oaa4115a1999-10-21 19:33:18 +000051#ifdef NO_INLINE_FUNCS
52#define _INLINE_
53#else
54#define _INLINE_ inline
55#endif
56
Theodore Ts'oad4fa462002-09-30 11:19:19 -040057/* #define DX_DEBUG */
Theodore Ts'o8fdc9982002-06-25 23:26:34 -040058
Theodore Ts'o3839e651997-04-26 13:21:57 +000059/*
60 * Keeps track of how many times an inode is referenced.
61 */
Theodore Ts'o4035f402001-01-12 20:25:50 +000062static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +000063static int check_dir_block(ext2_filsys fs,
JP Abgralle0ed7402014-03-19 19:08:39 -070064 struct ext2_db_entry2 *dir_blocks_info,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +000065 void *priv_data);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000066static int allocate_dir_block(e2fsck_t ctx,
JP Abgralle0ed7402014-03-19 19:08:39 -070067 struct ext2_db_entry2 *dir_blocks_info,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000068 char *buf, struct problem_context *pctx);
Theodore Ts'o8fdc9982002-06-25 23:26:34 -040069static void clear_htree(e2fsck_t ctx, ext2_ino_t ino);
Theodore Ts'oad4fa462002-09-30 11:19:19 -040070static int htree_depth(struct dx_dir_info *dx_dir,
71 struct dx_dirblock_info *dx_db);
Theodore Ts'oea1959f2002-08-31 02:32:41 -040072static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b);
Theodore Ts'o3839e651997-04-26 13:21:57 +000073
Theodore Ts'o21c84b71997-04-29 16:15:03 +000074struct check_dir_struct {
75 char *buf;
76 struct problem_context pctx;
Theodore Ts'of8188ff1997-11-14 05:23:04 +000077 int count, max;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000078 e2fsck_t ctx;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040079};
Theodore Ts'o21c84b71997-04-29 16:15:03 +000080
Theodore Ts'o08b21301997-11-03 19:42:40 +000081void e2fsck_pass2(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +000082{
Theodore Ts'oa4742692001-08-09 04:14:04 -040083 struct ext2_super_block *sb = ctx->fs->super;
84 struct problem_context pctx;
85 ext2_filsys fs = ctx->fs;
86 char *buf;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000087#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000088 struct resource_track rtrack;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000089#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +000090 struct check_dir_struct cd;
Theodore Ts'o8fdc9982002-06-25 23:26:34 -040091 struct dx_dir_info *dx_dir;
92 struct dx_dirblock_info *dx_db, *dx_parent;
JP Abgralle0ed7402014-03-19 19:08:39 -070093 unsigned int save_type;
Theodore Ts'o54434922003-12-07 01:28:50 -050094 int b;
Theodore Ts'oad4fa462002-09-30 11:19:19 -040095 int i, depth;
Theodore Ts'o8fdc9982002-06-25 23:26:34 -040096 problem_t code;
97 int bad_dir;
98
Theodore Ts'o6d96b002007-08-03 20:07:09 -040099 init_resource_track(&rtrack, ctx->fs->io);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000100 clear_problem_context(&cd.pctx);
101
Theodore Ts'o3839e651997-04-26 13:21:57 +0000102#ifdef MTRACE
103 mtrace_print("Pass 2");
104#endif
105
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000106 if (!(ctx->options & E2F_OPT_PREEN))
107 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
108
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400109 e2fsck_setup_tdb_icount(ctx, EXT2_ICOUNT_OPT_INCREMENT,
Theodore Ts'o34b9f792007-04-06 18:43:11 -0400110 &ctx->inode_count);
111 if (ctx->inode_count)
112 cd.pctx.errcode = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -0700113 else {
114 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
115 "inode_count", &save_type);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400116 cd.pctx.errcode = ext2fs_create_icount2(fs,
Theodore Ts'o34b9f792007-04-06 18:43:11 -0400117 EXT2_ICOUNT_OPT_INCREMENT,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000118 0, ctx->inode_link_info,
119 &ctx->inode_count);
JP Abgralle0ed7402014-03-19 19:08:39 -0700120 fs->default_bitmap_type = save_type;
121 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000122 if (cd.pctx.errcode) {
123 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000124 ctx->flags |= E2F_FLAG_ABORT;
125 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000126 }
Theodore Ts'obcf9c5d2002-05-21 09:14:17 -0400127 buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000128 "directory scan buffer");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000129
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000130 /*
131 * Set up the parent pointer for the root directory, if
132 * present. (If the root directory is not present, we will
133 * create it in pass 3.)
134 */
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400135 (void) e2fsck_dir_info_set_parent(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000136
137 cd.buf = buf;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000138 cd.ctx = ctx;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000139 cd.count = 1;
JP Abgralle0ed7402014-03-19 19:08:39 -0700140 cd.max = ext2fs_dblist_count2(fs->dblist);
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000141
142 if (ctx->progress)
143 (void) (ctx->progress)(ctx, 2, 0, cd.max);
Theodore Ts'oea1959f2002-08-31 02:32:41 -0400144
145 if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX)
JP Abgralle0ed7402014-03-19 19:08:39 -0700146 ext2fs_dblist_sort2(fs->dblist, special_dir_block_cmp);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400147
JP Abgralle0ed7402014-03-19 19:08:39 -0700148 cd.pctx.errcode = ext2fs_dblist_iterate2(fs->dblist, check_dir_block,
149 &cd);
Jose R. Santos49a73602007-10-21 21:04:03 -0500150 if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000151 return;
Andreas Dilger6267ee42009-05-28 00:39:49 -0600152
153 if (ctx->flags & E2F_FLAG_RESTART_LATER) {
154 ctx->flags |= E2F_FLAG_RESTART;
155 return;
156 }
157
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000158 if (cd.pctx.errcode) {
159 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000160 ctx->flags |= E2F_FLAG_ABORT;
161 return;
Theodore Ts'o7ac02a51997-06-11 18:32:35 +0000162 }
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400163
164#ifdef ENABLE_HTREE
165 for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) {
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400166 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
167 return;
Theodore Ts'o62acaa12002-07-15 16:54:42 -0400168 if (dx_dir->numblocks == 0)
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400169 continue;
170 clear_problem_context(&pctx);
171 bad_dir = 0;
172 pctx.dir = dx_dir->ino;
173 dx_db = dx_dir->dx_block;
174 if (dx_db->flags & DX_FLAG_REFERENCED)
175 dx_db->flags |= DX_FLAG_DUP_REF;
176 else
177 dx_db->flags |= DX_FLAG_REFERENCED;
178 /*
179 * Find all of the first and last leaf blocks, and
180 * update their parent's min and max hash values
181 */
182 for (b=0, dx_db = dx_dir->dx_block;
183 b < dx_dir->numblocks;
184 b++, dx_db++) {
185 if ((dx_db->type != DX_DIRBLOCK_LEAF) ||
186 !(dx_db->flags & (DX_FLAG_FIRST | DX_FLAG_LAST)))
187 continue;
188 dx_parent = &dx_dir->dx_block[dx_db->parent];
189 /*
190 * XXX Make sure dx_parent->min_hash > dx_db->min_hash
191 */
192 if (dx_db->flags & DX_FLAG_FIRST)
193 dx_parent->min_hash = dx_db->min_hash;
194 /*
195 * XXX Make sure dx_parent->max_hash < dx_db->max_hash
196 */
197 if (dx_db->flags & DX_FLAG_LAST)
198 dx_parent->max_hash = dx_db->max_hash;
199 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400200
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400201 for (b=0, dx_db = dx_dir->dx_block;
202 b < dx_dir->numblocks;
203 b++, dx_db++) {
204 pctx.blkcount = b;
205 pctx.group = dx_db->parent;
206 code = 0;
207 if (!(dx_db->flags & DX_FLAG_FIRST) &&
208 (dx_db->min_hash < dx_db->node_min_hash)) {
209 pctx.blk = dx_db->min_hash;
210 pctx.blk2 = dx_db->node_min_hash;
211 code = PR_2_HTREE_MIN_HASH;
212 fix_problem(ctx, code, &pctx);
213 bad_dir++;
214 }
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400215 if (dx_db->type == DX_DIRBLOCK_LEAF) {
216 depth = htree_depth(dx_dir, dx_db);
217 if (depth != dx_dir->depth) {
Andreas Dilgere5e12db2008-08-23 21:45:11 -0600218 pctx.num = dx_dir->depth;
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400219 code = PR_2_HTREE_BAD_DEPTH;
220 fix_problem(ctx, code, &pctx);
221 bad_dir++;
222 }
223 }
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400224 /*
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400225 * This test doesn't apply for the root block
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400226 * at block #0
227 */
228 if (b &&
229 (dx_db->max_hash > dx_db->node_max_hash)) {
230 pctx.blk = dx_db->max_hash;
231 pctx.blk2 = dx_db->node_max_hash;
232 code = PR_2_HTREE_MAX_HASH;
233 fix_problem(ctx, code, &pctx);
Theodore Ts'o503f9e72002-06-26 16:52:10 -0400234 bad_dir++;
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400235 }
236 if (!(dx_db->flags & DX_FLAG_REFERENCED)) {
237 code = PR_2_HTREE_NOTREF;
238 fix_problem(ctx, code, &pctx);
239 bad_dir++;
240 } else if (dx_db->flags & DX_FLAG_DUP_REF) {
241 code = PR_2_HTREE_DUPREF;
242 fix_problem(ctx, code, &pctx);
243 bad_dir++;
244 }
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400245 }
246 if (bad_dir && fix_problem(ctx, PR_2_HTREE_CLEAR, &pctx)) {
247 clear_htree(ctx, dx_dir->ino);
Theodore Ts'o62acaa12002-07-15 16:54:42 -0400248 dx_dir->numblocks = 0;
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400249 }
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400250 }
Theodore Ts'o23f75f62009-06-15 03:50:07 -0400251 e2fsck_free_dx_dir_info(ctx);
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400252#endif
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400253 ext2fs_free_mem(&buf);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000254 ext2fs_free_dblist(fs->dblist);
255
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000256 if (ctx->inode_bad_map) {
257 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
258 ctx->inode_bad_map = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000259 }
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000260 if (ctx->inode_reg_map) {
261 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
262 ctx->inode_reg_map = 0;
263 }
Theodore Ts'o890a2f92015-07-14 22:50:51 -0400264 if (ctx->encrypted_dirs) {
265 ext2fs_u32_list_free(ctx->encrypted_dirs);
266 ctx->encrypted_dirs = 0;
267 }
Theodore Ts'oa4742692001-08-09 04:14:04 -0400268
269 clear_problem_context(&pctx);
270 if (ctx->large_files) {
271 if (!(sb->s_feature_ro_compat &
272 EXT2_FEATURE_RO_COMPAT_LARGE_FILE) &&
273 fix_problem(ctx, PR_2_FEATURE_LARGE_FILES, &pctx)) {
274 sb->s_feature_ro_compat |=
275 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
Theodore Ts'o0cfce7f2007-10-06 12:37:08 -0400276 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
Theodore Ts'oa4742692001-08-09 04:14:04 -0400277 ext2fs_mark_super_dirty(fs);
278 }
279 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
280 fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
281 ext2fs_update_dynamic_rev(fs);
282 ext2fs_mark_super_dirty(fs);
283 }
Theodore Ts'oa4742692001-08-09 04:14:04 -0400284 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400285
Ken Chen9facd072009-05-28 09:55:10 -0400286 print_resource_track(ctx, _("Pass 2"), &rtrack, fs->io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000287}
288
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400289#define MAX_DEPTH 32000
290static int htree_depth(struct dx_dir_info *dx_dir,
291 struct dx_dirblock_info *dx_db)
292{
293 int depth = 0;
294
295 while (dx_db->type != DX_DIRBLOCK_ROOT && depth < MAX_DEPTH) {
296 dx_db = &dx_dir->dx_block[dx_db->parent];
297 depth++;
298 }
299 return depth;
300}
301
Theodore Ts'o09266682003-03-14 22:19:10 -0500302static int dict_de_cmp(const void *a, const void *b)
303{
Theodore Ts'o520ead32003-04-19 13:48:27 -0400304 const struct ext2_dir_entry *de_a, *de_b;
Theodore Ts'o09266682003-03-14 22:19:10 -0500305 int a_len, b_len;
306
Theodore Ts'o520ead32003-04-19 13:48:27 -0400307 de_a = (const struct ext2_dir_entry *) a;
Theodore Ts'o09266682003-03-14 22:19:10 -0500308 a_len = de_a->name_len & 0xFF;
Theodore Ts'o520ead32003-04-19 13:48:27 -0400309 de_b = (const struct ext2_dir_entry *) b;
Theodore Ts'o09266682003-03-14 22:19:10 -0500310 b_len = de_b->name_len & 0xFF;
311
312 if (a_len != b_len)
313 return (a_len - b_len);
314
Theodore Ts'o890a2f92015-07-14 22:50:51 -0400315 return memcmp(de_a->name, de_b->name, a_len);
Theodore Ts'o09266682003-03-14 22:19:10 -0500316}
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400317
Theodore Ts'o3839e651997-04-26 13:21:57 +0000318/*
Theodore Ts'oea1959f2002-08-31 02:32:41 -0400319 * This is special sort function that makes sure that directory blocks
320 * with a dirblock of zero are sorted to the beginning of the list.
321 * This guarantees that the root node of the htree directories are
322 * processed first, so we know what hash version to use.
323 */
324static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b)
325{
JP Abgralle0ed7402014-03-19 19:08:39 -0700326 const struct ext2_db_entry2 *db_a =
327 (const struct ext2_db_entry2 *) a;
328 const struct ext2_db_entry2 *db_b =
329 (const struct ext2_db_entry2 *) b;
Theodore Ts'oea1959f2002-08-31 02:32:41 -0400330
331 if (db_a->blockcnt && !db_b->blockcnt)
332 return 1;
333
334 if (!db_a->blockcnt && db_b->blockcnt)
335 return -1;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400336
Theodore Ts'oea1959f2002-08-31 02:32:41 -0400337 if (db_a->blk != db_b->blk)
338 return (int) (db_a->blk - db_b->blk);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400339
Theodore Ts'oea1959f2002-08-31 02:32:41 -0400340 if (db_a->ino != db_b->ino)
341 return (int) (db_a->ino - db_b->ino);
342
343 return (int) (db_a->blockcnt - db_b->blockcnt);
344}
345
346
347/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000348 * Make sure the first entry in the directory is '.', and that the
349 * directory entry is sane.
350 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000351static int check_dot(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000352 struct ext2_dir_entry *dirent,
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000353 ext2_ino_t ino, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000354{
355 struct ext2_dir_entry *nextdir;
Theodore Ts'o8a480352009-06-21 21:07:38 -0400356 unsigned int rec_len, new_len;
JP Abgralle0ed7402014-03-19 19:08:39 -0700357 int status = 0;
358 int created = 0;
359 problem_t problem = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400360
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000361 if (!dirent->inode)
362 problem = PR_2_MISSING_DOT;
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000363 else if (((dirent->name_len & 0xFF) != 1) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000364 (dirent->name[0] != '.'))
365 problem = PR_2_1ST_NOT_DOT;
366 else if (dirent->name[1] != '\0')
367 problem = PR_2_DOT_NULL_TERM;
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400368
Theodore Ts'o8a480352009-06-21 21:07:38 -0400369 (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000370 if (problem) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000371 if (fix_problem(ctx, problem, pctx)) {
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400372 if (rec_len < 12)
373 rec_len = dirent->rec_len = 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000374 dirent->inode = ino;
375 dirent->name_len = 1;
376 dirent->name[0] = '.';
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000377 dirent->name[1] = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000378 status = 1;
379 created = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000380 }
381 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000382 if (dirent->inode != ino) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000383 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000384 dirent->inode = ino;
385 status = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000386 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000387 }
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400388 if (rec_len > 12) {
389 new_len = rec_len - 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000390 if (new_len > 12) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000391 if (created ||
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000392 fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000393 nextdir = (struct ext2_dir_entry *)
394 ((char *) dirent + 12);
395 dirent->rec_len = 12;
Theodore Ts'o8a480352009-06-21 21:07:38 -0400396 (void) ext2fs_set_rec_len(ctx->fs, new_len,
397 nextdir);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000398 nextdir->inode = 0;
399 nextdir->name_len = 0;
400 status = 1;
401 }
402 }
403 }
404 return status;
405}
406
407/*
408 * Make sure the second entry in the directory is '..', and that the
409 * directory entry is sane. We do not check the inode number of '..'
410 * here; this gets done in pass 3.
411 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000412static int check_dotdot(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000413 struct ext2_dir_entry *dirent,
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400414 ext2_ino_t ino, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000415{
JP Abgralle0ed7402014-03-19 19:08:39 -0700416 problem_t problem = 0;
417 unsigned int rec_len;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400418
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000419 if (!dirent->inode)
420 problem = PR_2_MISSING_DOT_DOT;
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000421 else if (((dirent->name_len & 0xFF) != 2) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000422 (dirent->name[0] != '.') ||
423 (dirent->name[1] != '.'))
424 problem = PR_2_2ND_NOT_DOT_DOT;
425 else if (dirent->name[2] != '\0')
426 problem = PR_2_DOT_DOT_NULL_TERM;
427
Theodore Ts'o8a480352009-06-21 21:07:38 -0400428 (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000429 if (problem) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000430 if (fix_problem(ctx, problem, pctx)) {
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400431 if (rec_len < 12)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000432 dirent->rec_len = 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000433 /*
434 * Note: we don't have the parent inode just
435 * yet, so we will fill it in with the root
436 * inode. This will get fixed in pass 3.
437 */
438 dirent->inode = EXT2_ROOT_INO;
439 dirent->name_len = 2;
440 dirent->name[0] = '.';
441 dirent->name[1] = '.';
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000442 dirent->name[2] = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000443 return 1;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400444 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000445 return 0;
446 }
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400447 if (e2fsck_dir_info_set_dotdot(ctx, ino, dirent->inode)) {
448 fix_problem(ctx, PR_2_NO_DIRINFO, pctx);
449 return -1;
450 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000451 return 0;
452}
453
454/*
455 * Check to make sure a directory entry doesn't contain any illegal
456 * characters.
457 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000458static int check_name(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000459 struct ext2_dir_entry *dirent,
Theodore Ts'o54434922003-12-07 01:28:50 -0500460 struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000461{
462 int i;
463 int fixup = -1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000464 int ret = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400465
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000466 for ( i = 0; i < (dirent->name_len & 0xFF); i++) {
Theodore Ts'o638c15d2015-10-25 22:19:04 -0400467 if (dirent->name[i] != '/' && dirent->name[i] != '\0')
468 continue;
469 if (fixup < 0)
470 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
471 if (fixup == 0)
472 return 0;
473 dirent->name[i] = '.';
474 ret = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000475 }
476 return ret;
477}
478
Theodore Ts'o638c15d2015-10-25 22:19:04 -0400479static int encrypted_check_name(e2fsck_t ctx,
480 struct ext2_dir_entry *dirent,
481 struct problem_context *pctx)
482{
483 if ((dirent->name_len & 0xff) < EXT4_CRYPTO_BLOCK_SIZE) {
484 if (fix_problem(ctx, PR_2_BAD_ENCRYPTED_NAME, pctx)) {
485 dirent->inode = 0;
486 return 1;
487 }
488 ext2fs_unmark_valid(ctx->fs);
489 }
490 return 0;
491}
492
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000493/*
494 * Check the directory filetype (if present)
495 */
496static _INLINE_ int check_filetype(e2fsck_t ctx,
Theodore Ts'o54434922003-12-07 01:28:50 -0500497 struct ext2_dir_entry *dirent,
498 ext2_ino_t dir_ino EXT2FS_ATTR((unused)),
499 struct problem_context *pctx)
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000500{
501 int filetype = dirent->name_len >> 8;
502 int should_be = EXT2_FT_UNKNOWN;
503 struct ext2_inode inode;
504
505 if (!(ctx->fs->super->s_feature_incompat &
Theodore Ts'o7847c1d1999-10-22 15:11:42 +0000506 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
507 if (filetype == 0 ||
508 !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx))
509 return 0;
510 dirent->name_len = dirent->name_len & 0xFF;
511 return 1;
512 }
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000513
JP Abgralle0ed7402014-03-19 19:08:39 -0700514 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dirent->inode)) {
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000515 should_be = EXT2_FT_DIR;
JP Abgralle0ed7402014-03-19 19:08:39 -0700516 } else if (ext2fs_test_inode_bitmap2(ctx->inode_reg_map,
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000517 dirent->inode)) {
518 should_be = EXT2_FT_REG_FILE;
519 } else if (ctx->inode_bad_map &&
JP Abgralle0ed7402014-03-19 19:08:39 -0700520 ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000521 dirent->inode))
522 should_be = 0;
523 else {
524 e2fsck_read_inode(ctx, dirent->inode, &inode,
525 "check_filetype");
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000526 should_be = ext2_file_type(inode.i_mode);
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000527 }
528 if (filetype == should_be)
529 return 0;
530 pctx->num = should_be;
531
532 if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE,
533 pctx) == 0)
534 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400535
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000536 dirent->name_len = (dirent->name_len & 0xFF) | should_be << 8;
537 return 1;
538}
539
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400540#ifdef ENABLE_HTREE
541static void parse_int_node(ext2_filsys fs,
JP Abgralle0ed7402014-03-19 19:08:39 -0700542 struct ext2_db_entry2 *db,
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400543 struct check_dir_struct *cd,
544 struct dx_dir_info *dx_dir,
545 char *block_buf)
546{
547 struct ext2_dx_root_info *root;
548 struct ext2_dx_entry *ent;
549 struct ext2_dx_countlimit *limit;
550 struct dx_dirblock_info *dx_db;
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400551 int i, expect_limit, count;
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400552 blk_t blk;
553 ext2_dirhash_t min_hash = 0xffffffff;
554 ext2_dirhash_t max_hash = 0;
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400555 ext2_dirhash_t hash = 0, prev_hash;
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400556
557 if (db->blockcnt == 0) {
558 root = (struct ext2_dx_root_info *) (block_buf + 24);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400559
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400560#ifdef DX_DEBUG
561 printf("Root node dump:\n");
Takashi Sato8deb80a2006-03-18 21:43:46 -0500562 printf("\t Reserved zero: %u\n", root->reserved_zero);
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400563 printf("\t Hash Version: %d\n", root->hash_version);
564 printf("\t Info length: %d\n", root->info_length);
565 printf("\t Indirect levels: %d\n", root->indirect_levels);
566 printf("\t Flags: %d\n", root->unused_flags);
567#endif
568
569 ent = (struct ext2_dx_entry *) (block_buf + 24 + root->info_length);
570 } else {
571 ent = (struct ext2_dx_entry *) (block_buf+8);
572 }
573 limit = (struct ext2_dx_countlimit *) ent;
574
575#ifdef DX_DEBUG
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400576 printf("Number of entries (count): %d\n",
Theodore Ts'o8132d842002-10-02 22:07:17 -0400577 ext2fs_le16_to_cpu(limit->count));
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400578 printf("Number of entries (limit): %d\n",
Theodore Ts'o8132d842002-10-02 22:07:17 -0400579 ext2fs_le16_to_cpu(limit->limit));
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400580#endif
581
Theodore Ts'o8132d842002-10-02 22:07:17 -0400582 count = ext2fs_le16_to_cpu(limit->count);
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400583 expect_limit = (fs->blocksize - ((char *) ent - block_buf)) /
584 sizeof(struct ext2_dx_entry);
Theodore Ts'o8132d842002-10-02 22:07:17 -0400585 if (ext2fs_le16_to_cpu(limit->limit) != expect_limit) {
586 cd->pctx.num = ext2fs_le16_to_cpu(limit->limit);
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400587 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_LIMIT, &cd->pctx))
588 goto clear_and_exit;
589 }
Theodore Ts'o8132d842002-10-02 22:07:17 -0400590 if (count > expect_limit) {
591 cd->pctx.num = count;
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400592 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_COUNT, &cd->pctx))
593 goto clear_and_exit;
594 count = expect_limit;
595 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400596
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400597 for (i=0; i < count; i++) {
598 prev_hash = hash;
Theodore Ts'o8132d842002-10-02 22:07:17 -0400599 hash = i ? (ext2fs_le32_to_cpu(ent[i].hash) & ~1) : 0;
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400600#ifdef DX_DEBUG
Takashi Sato8deb80a2006-03-18 21:43:46 -0500601 printf("Entry #%d: Hash 0x%08x, block %u\n", i,
Theodore Ts'o8132d842002-10-02 22:07:17 -0400602 hash, ext2fs_le32_to_cpu(ent[i].block));
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400603#endif
Theodore Ts'o8132d842002-10-02 22:07:17 -0400604 blk = ext2fs_le32_to_cpu(ent[i].block) & 0x0ffffff;
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400605 /* Check to make sure the block is valid */
Theodore Ts'o977ac872006-10-21 23:27:03 -0400606 if (blk >= (blk_t) dx_dir->numblocks) {
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400607 cd->pctx.blk = blk;
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400608 if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK,
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400609 &cd->pctx))
610 goto clear_and_exit;
Theodore Ts'o977ac872006-10-21 23:27:03 -0400611 continue;
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400612 }
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400613 if (hash < prev_hash &&
614 fix_problem(cd->ctx, PR_2_HTREE_HASH_ORDER, &cd->pctx))
615 goto clear_and_exit;
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400616 dx_db = &dx_dir->dx_block[blk];
617 if (dx_db->flags & DX_FLAG_REFERENCED) {
618 dx_db->flags |= DX_FLAG_DUP_REF;
619 } else {
620 dx_db->flags |= DX_FLAG_REFERENCED;
621 dx_db->parent = db->blockcnt;
622 }
623 if (hash < min_hash)
624 min_hash = hash;
625 if (hash > max_hash)
626 max_hash = hash;
627 dx_db->node_min_hash = hash;
Theodore Ts'o8132d842002-10-02 22:07:17 -0400628 if ((i+1) < count)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400629 dx_db->node_max_hash =
Theodore Ts'o8132d842002-10-02 22:07:17 -0400630 ext2fs_le32_to_cpu(ent[i+1].hash) & ~1;
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400631 else {
632 dx_db->node_max_hash = 0xfffffffe;
633 dx_db->flags |= DX_FLAG_LAST;
634 }
635 if (i == 0)
636 dx_db->flags |= DX_FLAG_FIRST;
637 }
638#ifdef DX_DEBUG
639 printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n",
640 db->blockcnt, min_hash, max_hash);
641#endif
642 dx_db = &dx_dir->dx_block[db->blockcnt];
643 dx_db->min_hash = min_hash;
644 dx_db->max_hash = max_hash;
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400645 return;
646
647clear_and_exit:
648 clear_htree(cd->ctx, cd->pctx.ino);
649 dx_dir->numblocks = 0;
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400650}
651#endif /* ENABLE_HTREE */
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000652
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400653/*
654 * Given a busted directory, try to salvage it somehow.
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400655 *
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400656 */
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400657static void salvage_directory(ext2_filsys fs,
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400658 struct ext2_dir_entry *dirent,
659 struct ext2_dir_entry *prev,
Theodore Ts'o54434922003-12-07 01:28:50 -0500660 unsigned int *offset)
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400661{
662 char *cp = (char *) dirent;
Theodore Ts'o8a480352009-06-21 21:07:38 -0400663 int left;
664 unsigned int rec_len, prev_rec_len;
Theodore Ts'o642935c2006-11-14 23:38:17 -0500665 unsigned int name_len = dirent->name_len & 0xFF;
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400666
Theodore Ts'o8a480352009-06-21 21:07:38 -0400667 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400668 left = fs->blocksize - *offset - rec_len;
669
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400670 /*
671 * Special case of directory entry of size 8: copy what's left
672 * of the directory block up to cover up the invalid hole.
673 */
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400674 if ((left >= 12) && (rec_len == 8)) {
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400675 memmove(cp, cp+8, left);
676 memset(cp + left, 0, 8);
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400677 return;
678 }
679 /*
680 * If the directory entry overruns the end of the directory
681 * block, and the name is small enough to fit, then adjust the
682 * record length.
683 */
684 if ((left < 0) &&
Theodore Ts'o8a480352009-06-21 21:07:38 -0400685 ((int) rec_len + left > 8) &&
JP Abgralle0ed7402014-03-19 19:08:39 -0700686 ((int) name_len + 8 <= (int) rec_len + left) &&
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400687 dirent->inode <= fs->super->s_inodes_count &&
688 strnlen(dirent->name, name_len) == name_len) {
Theodore Ts'o8a480352009-06-21 21:07:38 -0400689 (void) ext2fs_set_rec_len(fs, (int) rec_len + left, dirent);
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400690 return;
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400691 }
692 /*
Kalpak Shah575307c2007-07-09 13:05:45 -0400693 * If the record length of the directory entry is a multiple
694 * of four, and not too big, such that it is valid, let the
695 * previous directory entry absorb the invalid one.
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400696 */
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400697 if (prev && rec_len && (rec_len % 4) == 0 &&
698 (*offset + rec_len <= fs->blocksize)) {
Theodore Ts'o8a480352009-06-21 21:07:38 -0400699 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
700 prev_rec_len += rec_len;
701 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400702 *offset += rec_len;
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400703 return;
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400704 }
705 /*
706 * Default salvage method --- kill all of the directory
707 * entries for the rest of the block. We will either try to
708 * absorb it into the previous directory entry, or create a
709 * new empty directory entry the rest of the directory block.
710 */
711 if (prev) {
Theodore Ts'o8a480352009-06-21 21:07:38 -0400712 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
713 prev_rec_len += fs->blocksize - *offset;
714 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400715 *offset = fs->blocksize;
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400716 } else {
Theodore Ts'o8a480352009-06-21 21:07:38 -0400717 rec_len = fs->blocksize - *offset;
718 (void) ext2fs_set_rec_len(fs, rec_len, dirent);
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400719 dirent->name_len = 0;
720 dirent->inode = 0;
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400721 }
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400722}
723
Theodore Ts'o3839e651997-04-26 13:21:57 +0000724static int check_dir_block(ext2_filsys fs,
JP Abgralle0ed7402014-03-19 19:08:39 -0700725 struct ext2_db_entry2 *db,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000726 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000727{
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400728 struct dx_dir_info *dx_dir;
729#ifdef ENABLE_HTREE
730 struct dx_dirblock_info *dx_db = 0;
731#endif /* ENABLE_HTREE */
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400732 struct ext2_dir_entry *dirent, *prev;
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400733 ext2_dirhash_t hash;
Theodore Ts'o54434922003-12-07 01:28:50 -0500734 unsigned int offset = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000735 int dir_modified = 0;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000736 int dot_state;
Theodore Ts'o03fa6f82008-11-16 10:03:00 -0500737 unsigned int rec_len;
JP Abgralle0ed7402014-03-19 19:08:39 -0700738 blk64_t block_nr = db->blk;
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000739 ext2_ino_t ino = db->ino;
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400740 ext2_ino_t subdir_parent;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000741 __u16 links;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000742 struct check_dir_struct *cd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000743 char *buf;
744 e2fsck_t ctx;
JP Abgralle0ed7402014-03-19 19:08:39 -0700745 problem_t problem;
Theodore Ts'oea1959f2002-08-31 02:32:41 -0400746 struct ext2_dx_root_info *root;
Theodore Ts'oe8254bf2002-09-29 19:30:28 -0400747 struct ext2_dx_countlimit *limit;
Theodore Ts'o09266682003-03-14 22:19:10 -0500748 static dict_t de_dict;
749 struct problem_context pctx;
750 int dups_found = 0;
Theodore Ts'o890a2f92015-07-14 22:50:51 -0400751 int encrypted = 0;
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400752 int ret;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000753
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000754 cd = (struct check_dir_struct *) priv_data;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000755 buf = cd->buf;
756 ctx = cd->ctx;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000757
Jose R. Santos49a73602007-10-21 21:04:03 -0500758 if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400759 return DIRENT_ABORT;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400760
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400761 if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max))
762 return DIRENT_ABORT;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400763
Theodore Ts'o3839e651997-04-26 13:21:57 +0000764 /*
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400765 * Make sure the inode is still in use (could have been
Theodore Ts'o3839e651997-04-26 13:21:57 +0000766 * deleted in the duplicate/bad blocks pass.
767 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700768 if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000769 return 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000770
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000771 cd->pctx.ino = ino;
772 cd->pctx.blk = block_nr;
773 cd->pctx.blkcount = db->blockcnt;
774 cd->pctx.ino2 = 0;
775 cd->pctx.dirent = 0;
776 cd->pctx.num = 0;
777
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000778 if (db->blk == 0) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000779 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000780 return 0;
781 block_nr = db->blk;
782 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400783
Theodore Ts'o3839e651997-04-26 13:21:57 +0000784 if (db->blockcnt)
785 dot_state = 2;
786 else
787 dot_state = 0;
788
Theodore Ts'o09266682003-03-14 22:19:10 -0500789 if (ctx->dirs_to_hash &&
790 ext2fs_u32_list_test(ctx->dirs_to_hash, ino))
791 dups_found++;
792
Theodore Ts'o3839e651997-04-26 13:21:57 +0000793#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000794 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000795 db->blockcnt, ino);
796#endif
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400797
JP Abgralle0ed7402014-03-19 19:08:39 -0700798 ehandler_operation(_("reading directory block"));
799 cd->pctx.errcode = ext2fs_read_dir_block3(fs, block_nr, buf, 0);
Theodore Ts'oe94bc632007-04-14 09:29:02 -0400800 ehandler_operation(0);
Theodore Ts'ob9852cd2001-05-05 05:14:59 +0000801 if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED)
802 cd->pctx.errcode = 0; /* We'll handle this ourselves */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000803 if (cd->pctx.errcode) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000804 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
805 ctx->flags |= E2F_FLAG_ABORT;
806 return DIRENT_ABORT;
807 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000808 memset(buf, 0, fs->blocksize);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000809 }
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400810#ifdef ENABLE_HTREE
811 dx_dir = e2fsck_get_dx_dir_info(ctx, ino);
Theodore Ts'o62acaa12002-07-15 16:54:42 -0400812 if (dx_dir && dx_dir->numblocks) {
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400813 if (db->blockcnt >= dx_dir->numblocks) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400814 if (fix_problem(ctx, PR_2_UNEXPECTED_HTREE_BLOCK,
Theodore Ts'od45edec2008-03-12 16:10:48 -0400815 &pctx)) {
816 clear_htree(ctx, ino);
817 dx_dir->numblocks = 0;
818 dx_db = 0;
819 goto out_htree;
820 }
821 fatal_error(ctx, _("Can not continue."));
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400822 }
823 dx_db = &dx_dir->dx_block[db->blockcnt];
824 dx_db->type = DX_DIRBLOCK_LEAF;
825 dx_db->phys = block_nr;
826 dx_db->min_hash = ~0;
827 dx_db->max_hash = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400828
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400829 dirent = (struct ext2_dir_entry *) buf;
Theodore Ts'o8a480352009-06-21 21:07:38 -0400830 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
Theodore Ts'oe8254bf2002-09-29 19:30:28 -0400831 limit = (struct ext2_dx_countlimit *) (buf+8);
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400832 if (db->blockcnt == 0) {
Theodore Ts'oea1959f2002-08-31 02:32:41 -0400833 root = (struct ext2_dx_root_info *) (buf + 24);
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400834 dx_db->type = DX_DIRBLOCK_ROOT;
835 dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST;
Theodore Ts'oea1959f2002-08-31 02:32:41 -0400836 if ((root->reserved_zero ||
837 root->info_length < 8 ||
838 root->indirect_levels > 1) &&
839 fix_problem(ctx, PR_2_HTREE_BAD_ROOT, &cd->pctx)) {
840 clear_htree(ctx, ino);
841 dx_dir->numblocks = 0;
842 dx_db = 0;
Theodore Ts'of77704e2006-11-11 22:32:35 -0500843 }
Theodore Ts'oea1959f2002-08-31 02:32:41 -0400844 dx_dir->hashversion = root->hash_version;
Theodore Ts'of77704e2006-11-11 22:32:35 -0500845 if ((dx_dir->hashversion <= EXT2_HASH_TEA) &&
846 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
847 dx_dir->hashversion += 3;
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400848 dx_dir->depth = root->indirect_levels + 1;
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400849 } else if ((dirent->inode == 0) &&
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400850 (rec_len == fs->blocksize) &&
Theodore Ts'oe8254bf2002-09-29 19:30:28 -0400851 (dirent->name_len == 0) &&
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400852 (ext2fs_le16_to_cpu(limit->limit) ==
853 ((fs->blocksize-8) /
Theodore Ts'o8132d842002-10-02 22:07:17 -0400854 sizeof(struct ext2_dx_entry))))
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400855 dx_db->type = DX_DIRBLOCK_NODE;
856 }
Theodore Ts'od45edec2008-03-12 16:10:48 -0400857out_htree:
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400858#endif /* ENABLE_HTREE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000859
Theodore Ts'o890a2f92015-07-14 22:50:51 -0400860 if (ctx->encrypted_dirs)
861 encrypted = ext2fs_u32_list_test(ctx->encrypted_dirs, ino);
862
Theodore Ts'o09266682003-03-14 22:19:10 -0500863 dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cmp);
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400864 prev = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000865 do {
JP Abgralle0ed7402014-03-19 19:08:39 -0700866 dgrp_t group;
Jose R. Santos49a73602007-10-21 21:04:03 -0500867 ext2_ino_t first_unused_inode;
868
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000869 problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000870 dirent = (struct ext2_dir_entry *) (buf + offset);
Theodore Ts'o8a480352009-06-21 21:07:38 -0400871 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000872 cd->pctx.dirent = dirent;
873 cd->pctx.num = offset;
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400874 if (((offset + rec_len) > fs->blocksize) ||
875 (rec_len < 12) ||
876 ((rec_len % 4) != 0) ||
Theodore Ts'o03fa6f82008-11-16 10:03:00 -0500877 (((dirent->name_len & (unsigned) 0xFF)+8) > rec_len)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000878 if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
Theodore Ts'oad4fa462002-09-30 11:19:19 -0400879 salvage_directory(fs, dirent, prev, &offset);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000880 dir_modified++;
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400881 continue;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000882 } else
Theodore Ts'o09266682003-03-14 22:19:10 -0500883 goto abort_free_dict;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000884 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000885
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400886 if (dot_state == 0) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000887 if (check_dot(ctx, dirent, ino, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000888 dir_modified++;
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400889 } else if (dot_state == 1) {
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400890 ret = check_dotdot(ctx, dirent, ino, &cd->pctx);
891 if (ret < 0)
Theodore Ts'o09266682003-03-14 22:19:10 -0500892 goto abort_free_dict;
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400893 if (ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000894 dir_modified++;
895 } else if (dirent->inode == ino) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000896 problem = PR_2_LINK_DOT;
897 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000898 dirent->inode = 0;
899 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000900 goto next;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000901 }
902 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400903 if (!dirent->inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000904 goto next;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400905
Theodore Ts'o3839e651997-04-26 13:21:57 +0000906 /*
907 * Make sure the inode listed is a legal one.
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400908 */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000909 if (((dirent->inode != EXT2_ROOT_INO) &&
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000910 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000911 (dirent->inode > fs->super->s_inodes_count)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000912 problem = PR_2_BAD_INO;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000913 } else if (ctx->inode_bb_map &&
JP Abgralle0ed7402014-03-19 19:08:39 -0700914 (ext2fs_test_inode_bitmap2(ctx->inode_bb_map,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000915 dirent->inode))) {
916 /*
917 * If the inode is in a bad block, offer to
918 * clear it.
919 */
920 problem = PR_2_BB_INODE;
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400921 } else if ((dot_state > 1) &&
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000922 ((dirent->name_len & 0xFF) == 1) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000923 (dirent->name[0] == '.')) {
924 /*
925 * If there's a '.' entry in anything other
926 * than the first directory entry, it's a
927 * duplicate entry that should be removed.
928 */
929 problem = PR_2_DUP_DOT;
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400930 } else if ((dot_state > 1) &&
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000931 ((dirent->name_len & 0xFF) == 2) &&
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400932 (dirent->name[0] == '.') &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000933 (dirent->name[1] == '.')) {
934 /*
935 * If there's a '..' entry in anything other
936 * than the second directory entry, it's a
937 * duplicate entry that should be removed.
938 */
939 problem = PR_2_DUP_DOT_DOT;
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400940 } else if ((dot_state > 1) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000941 (dirent->inode == EXT2_ROOT_INO)) {
942 /*
943 * Don't allow links to the root directory.
944 * We check this specially to make sure we
945 * catch this error case even if the root
946 * directory hasn't been created yet.
947 */
948 problem = PR_2_LINK_ROOT;
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400949 } else if ((dot_state > 1) &&
Theodore Ts'oc40db6d1999-10-25 21:03:34 +0000950 (dirent->name_len & 0xFF) == 0) {
951 /*
952 * Don't allow zero-length directory names.
953 */
954 problem = PR_2_NULL_NAME;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000955 }
956
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000957 if (problem) {
958 if (fix_problem(ctx, problem, &cd->pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000959 dirent->inode = 0;
960 dir_modified++;
961 goto next;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000962 } else {
963 ext2fs_unmark_valid(fs);
964 if (problem == PR_2_BAD_INO)
965 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000966 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000967 }
968
969 /*
970 * If the inode was marked as having bad fields in
971 * pass1, process it and offer to fix/clear it.
972 * (We wait until now so that we can display the
973 * pathname to the user.)
974 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000975 if (ctx->inode_bad_map &&
JP Abgralle0ed7402014-03-19 19:08:39 -0700976 ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000977 dirent->inode)) {
Theodore Ts'oe72a9ba1999-06-25 15:40:18 +0000978 if (e2fsck_process_bad_inode(ctx, ino,
Theodore Ts'obcf9c5d2002-05-21 09:14:17 -0400979 dirent->inode,
980 buf + fs->blocksize)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000981 dirent->inode = 0;
982 dir_modified++;
983 goto next;
984 }
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000985 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000986 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000987 }
988
Jose R. Santos49a73602007-10-21 21:04:03 -0500989 group = ext2fs_group_of_ino(fs, dirent->inode);
990 first_unused_inode = group * fs->super->s_inodes_per_group +
991 1 + fs->super->s_inodes_per_group -
JP Abgralle0ed7402014-03-19 19:08:39 -0700992 ext2fs_bg_itable_unused(fs, group);
Jose R. Santos49a73602007-10-21 21:04:03 -0500993 cd->pctx.group = group;
994
995 /*
Theodore Ts'o42e89ce2008-11-26 20:41:26 -0500996 * Check if the inode was missed out because
997 * _INODE_UNINIT flag was set or bg_itable_unused was
998 * incorrect. If so, clear the _INODE_UNINIT flag and
999 * restart e2fsck. In the future it would be nice if
1000 * we could call a function in pass1.c that checks the
1001 * newly visible inodes.
Jose R. Santos49a73602007-10-21 21:04:03 -05001002 */
JP Abgralle0ed7402014-03-19 19:08:39 -07001003 if (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)) {
Andreas Dilger6267ee42009-05-28 00:39:49 -06001004 pctx.num = dirent->inode;
Jose R. Santos49a73602007-10-21 21:04:03 -05001005 if (fix_problem(ctx, PR_2_INOREF_BG_INO_UNINIT,
1006 &cd->pctx)){
JP Abgralle0ed7402014-03-19 19:08:39 -07001007 ext2fs_bg_flags_clear(fs, group,
1008 EXT2_BG_INODE_UNINIT);
Theodore Ts'o42e89ce2008-11-26 20:41:26 -05001009 ext2fs_mark_super_dirty(fs);
Andreas Dilger6267ee42009-05-28 00:39:49 -06001010 ctx->flags |= E2F_FLAG_RESTART_LATER;
Jose R. Santos49a73602007-10-21 21:04:03 -05001011 } else {
1012 ext2fs_unmark_valid(fs);
1013 if (problem == PR_2_BAD_INO)
1014 goto next;
1015 }
1016 } else if (dirent->inode >= first_unused_inode) {
Andreas Dilger6267ee42009-05-28 00:39:49 -06001017 pctx.num = dirent->inode;
Jose R. Santos49a73602007-10-21 21:04:03 -05001018 if (fix_problem(ctx, PR_2_INOREF_IN_UNUSED, &cd->pctx)){
JP Abgralle0ed7402014-03-19 19:08:39 -07001019 ext2fs_bg_itable_unused_set(fs, group, 0);
Jose R. Santos49a73602007-10-21 21:04:03 -05001020 ext2fs_mark_super_dirty(fs);
Andreas Dilger6267ee42009-05-28 00:39:49 -06001021 ctx->flags |= E2F_FLAG_RESTART_LATER;
Jose R. Santos49a73602007-10-21 21:04:03 -05001022 } else {
1023 ext2fs_unmark_valid(fs);
1024 if (problem == PR_2_BAD_INO)
1025 goto next;
1026 }
1027 }
1028
Theodore Ts'o0433c1f2010-05-11 15:12:52 -04001029 /*
1030 * Offer to clear unused inodes; if we are going to be
1031 * restarting the scan due to bg_itable_unused being
1032 * wrong, then don't clear any inodes to avoid zapping
1033 * inodes that were skipped during pass1 due to an
1034 * incorrect bg_itable_unused; we'll get any real
1035 * problems after we restart.
1036 */
1037 if (!(ctx->flags & E2F_FLAG_RESTART_LATER) &&
JP Abgralle0ed7402014-03-19 19:08:39 -07001038 !(ext2fs_test_inode_bitmap2(ctx->inode_used_map,
1039 dirent->inode)))
Jose R. Santos49a73602007-10-21 21:04:03 -05001040 problem = PR_2_UNUSED_INODE;
Jose R. Santos49a73602007-10-21 21:04:03 -05001041
1042 if (problem) {
1043 if (fix_problem(ctx, problem, &cd->pctx)) {
1044 dirent->inode = 0;
1045 dir_modified++;
1046 goto next;
1047 } else {
1048 ext2fs_unmark_valid(fs);
1049 if (problem == PR_2_BAD_INO)
1050 goto next;
1051 }
1052 }
1053
Theodore Ts'o890a2f92015-07-14 22:50:51 -04001054 if (!encrypted && check_name(ctx, dirent, &cd->pctx))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001055 dir_modified++;
1056
Theodore Ts'o638c15d2015-10-25 22:19:04 -04001057 if (encrypted && (dot_state) > 1 &&
1058 encrypted_check_name(ctx, dirent, &cd->pctx)) {
1059 dir_modified++;
1060 goto next;
1061 }
1062
Theodore Ts'oaa4115a1999-10-21 19:33:18 +00001063 if (check_filetype(ctx, dirent, ino, &cd->pctx))
1064 dir_modified++;
1065
Theodore Ts'o8fdc9982002-06-25 23:26:34 -04001066#ifdef ENABLE_HTREE
1067 if (dx_db) {
1068 ext2fs_dirhash(dx_dir->hashversion, dirent->name,
Theodore Ts'o503f9e72002-06-26 16:52:10 -04001069 (dirent->name_len & 0xFF),
1070 fs->super->s_hash_seed, &hash, 0);
Theodore Ts'o8fdc9982002-06-25 23:26:34 -04001071 if (hash < dx_db->min_hash)
1072 dx_db->min_hash = hash;
1073 if (hash > dx_db->max_hash)
1074 dx_db->max_hash = hash;
1075 }
1076#endif
1077
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001078 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +00001079 * If this is a directory, then mark its parent in its
1080 * dir_info structure. If the parent field is already
1081 * filled in, then this directory has more than one
1082 * hard link. We assume the first link is correct,
1083 * and ask the user if he/she wants to clear this one.
1084 */
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001085 if ((dot_state > 1) &&
JP Abgralle0ed7402014-03-19 19:08:39 -07001086 (ext2fs_test_inode_bitmap2(ctx->inode_dir_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +00001087 dirent->inode))) {
Theodore Ts'o28db82a2007-04-04 22:33:31 -04001088 if (e2fsck_dir_info_get_parent(ctx, dirent->inode,
1089 &subdir_parent)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001090 cd->pctx.ino = dirent->inode;
1091 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
Theodore Ts'o09266682003-03-14 22:19:10 -05001092 goto abort_free_dict;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001093 }
Theodore Ts'o28db82a2007-04-04 22:33:31 -04001094 if (subdir_parent) {
1095 cd->pctx.ino2 = subdir_parent;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001096 if (fix_problem(ctx, PR_2_LINK_DIR,
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001097 &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001098 dirent->inode = 0;
1099 dir_modified++;
1100 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001101 }
1102 cd->pctx.ino2 = 0;
Theodore Ts'o28db82a2007-04-04 22:33:31 -04001103 } else {
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001104 (void) e2fsck_dir_info_set_parent(ctx,
Theodore Ts'o28db82a2007-04-04 22:33:31 -04001105 dirent->inode, ino);
1106 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001107 }
Theodore Ts'o09266682003-03-14 22:19:10 -05001108
1109 if (dups_found) {
1110 ;
1111 } else if (dict_lookup(&de_dict, dirent)) {
1112 clear_problem_context(&pctx);
1113 pctx.ino = ino;
1114 pctx.dirent = dirent;
1115 fix_problem(ctx, PR_2_REPORT_DUP_DIRENT, &pctx);
1116 if (!ctx->dirs_to_hash)
1117 ext2fs_u32_list_create(&ctx->dirs_to_hash, 50);
1118 if (ctx->dirs_to_hash)
1119 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1120 dups_found++;
1121 } else
1122 dict_alloc_insert(&de_dict, dirent, dirent);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001123
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001124 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
1125 &links);
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001126 if (links > 1)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001127 ctx->fs_links_count++;
1128 ctx->fs_total_count++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001129 next:
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001130 prev = dirent;
Theodore Ts'o5dd77db2008-08-25 21:08:19 -04001131 if (dir_modified)
Theodore Ts'o8a480352009-06-21 21:07:38 -04001132 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
Theodore Ts'o5dd77db2008-08-25 21:08:19 -04001133 offset += rec_len;
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001134 dot_state++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001135 } while (offset < fs->blocksize);
1136#if 0
1137 printf("\n");
1138#endif
Theodore Ts'o8fdc9982002-06-25 23:26:34 -04001139#ifdef ENABLE_HTREE
1140 if (dx_db) {
1141#ifdef DX_DEBUG
1142 printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n",
1143 db->blockcnt, dx_db->type,
1144 dx_db->min_hash, dx_db->max_hash);
1145#endif
Theodore Ts'ob7a00562002-07-20 00:28:07 -04001146 cd->pctx.dir = cd->pctx.ino;
Theodore Ts'o8fdc9982002-06-25 23:26:34 -04001147 if ((dx_db->type == DX_DIRBLOCK_ROOT) ||
1148 (dx_db->type == DX_DIRBLOCK_NODE))
1149 parse_int_node(fs, db, cd, dx_dir, buf);
1150 }
1151#endif /* ENABLE_HTREE */
Theodore Ts'o3839e651997-04-26 13:21:57 +00001152 if (offset != fs->blocksize) {
Theodore Ts'o5dd77db2008-08-25 21:08:19 -04001153 cd->pctx.num = rec_len - fs->blocksize + offset;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001154 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
1155 dirent->rec_len = cd->pctx.num;
1156 dir_modified++;
1157 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001158 }
1159 if (dir_modified) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001160 cd->pctx.errcode = ext2fs_write_dir_block3(fs, block_nr, buf, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001161 if (cd->pctx.errcode) {
Theodore Ts'o08b21301997-11-03 19:42:40 +00001162 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
Theodore Ts'o09266682003-03-14 22:19:10 -05001163 &cd->pctx))
1164 goto abort_free_dict;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001165 }
1166 ext2fs_mark_changed(fs);
1167 }
Theodore Ts'o09266682003-03-14 22:19:10 -05001168 dict_free_nodes(&de_dict);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001169 return 0;
Theodore Ts'o09266682003-03-14 22:19:10 -05001170abort_free_dict:
Theodore Ts'o09266682003-03-14 22:19:10 -05001171 ctx->flags |= E2F_FLAG_ABORT;
Jose R. Santos49a73602007-10-21 21:04:03 -05001172 dict_free_nodes(&de_dict);
Theodore Ts'o09266682003-03-14 22:19:10 -05001173 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001174}
1175
JP Abgralle0ed7402014-03-19 19:08:39 -07001176struct del_block {
1177 e2fsck_t ctx;
1178 e2_blkcnt_t num;
1179};
1180
Theodore Ts'o3839e651997-04-26 13:21:57 +00001181/*
1182 * This function is called to deallocate a block, and is an interator
1183 * functioned called by deallocate inode via ext2fs_iterate_block().
1184 */
1185static int deallocate_inode_block(ext2_filsys fs,
JP Abgralle0ed7402014-03-19 19:08:39 -07001186 blk64_t *block_nr,
Theodore Ts'o54434922003-12-07 01:28:50 -05001187 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
JP Abgralle0ed7402014-03-19 19:08:39 -07001188 blk64_t ref_block EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -05001189 int ref_offset EXT2FS_ATTR((unused)),
Theodore Ts'o133a56d2000-11-17 05:40:49 +00001190 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001191{
JP Abgralle0ed7402014-03-19 19:08:39 -07001192 struct del_block *p = priv_data;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001193
Theodore Ts'o19178752000-02-11 15:55:07 +00001194 if (HOLE_BLKADDR(*block_nr))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001195 return 0;
Theodore Ts'o1ba7a2f2004-01-30 01:48:06 -05001196 if ((*block_nr < fs->super->s_first_data_block) ||
JP Abgralle0ed7402014-03-19 19:08:39 -07001197 (*block_nr >= ext2fs_blocks_count(fs->super)))
Theodore Ts'o1ba7a2f2004-01-30 01:48:06 -05001198 return 0;
JP Abgralle0ed7402014-03-19 19:08:39 -07001199 ext2fs_unmark_block_bitmap2(p->ctx->block_found_map, *block_nr);
1200 ext2fs_block_alloc_stats2(fs, *block_nr, -1);
1201 p->num++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001202 return 0;
1203}
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001204
Theodore Ts'o3839e651997-04-26 13:21:57 +00001205/*
1206 * This fuction deallocates an inode
1207 */
Theodore Ts'o4035f402001-01-12 20:25:50 +00001208static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001209{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001210 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001211 struct ext2_inode inode;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001212 struct problem_context pctx;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -04001213 __u32 count;
JP Abgralle0ed7402014-03-19 19:08:39 -07001214 struct del_block del_block;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001215
Theodore Ts'o08b21301997-11-03 19:42:40 +00001216 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001217 clear_problem_context(&pctx);
1218 pctx.ino = ino;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001219
Theodore Ts'o3839e651997-04-26 13:21:57 +00001220 /*
1221 * Fix up the bitmaps...
1222 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001223 e2fsck_read_bitmaps(ctx);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -04001224 ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
1225
JP Abgralle0ed7402014-03-19 19:08:39 -07001226 if (ext2fs_file_acl_block(fs, &inode) &&
Theodore Ts'o0684a4f2002-08-17 10:19:44 -04001227 (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001228 pctx.errcode = ext2fs_adjust_ea_refcount2(fs,
1229 ext2fs_file_acl_block(fs, &inode),
1230 block_buf, -1, &count);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -04001231 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
1232 pctx.errcode = 0;
1233 count = 1;
1234 }
1235 if (pctx.errcode) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001236 pctx.blk = ext2fs_file_acl_block(fs, &inode);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -04001237 fix_problem(ctx, PR_2_ADJ_EA_REFCOUNT, &pctx);
1238 ctx->flags |= E2F_FLAG_ABORT;
1239 return;
1240 }
1241 if (count == 0) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001242 ext2fs_unmark_block_bitmap2(ctx->block_found_map,
1243 ext2fs_file_acl_block(fs, &inode));
1244 ext2fs_block_alloc_stats2(fs,
1245 ext2fs_file_acl_block(fs, &inode), -1);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -04001246 }
JP Abgralle0ed7402014-03-19 19:08:39 -07001247 ext2fs_file_acl_block_set(fs, &inode, 0);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -04001248 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001249
JP Abgralle0ed7402014-03-19 19:08:39 -07001250 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
1251 goto clear_inode;
Theodore Ts'oa4742692001-08-09 04:14:04 -04001252
Darrick J. Wong3b6c0932013-12-10 17:18:27 -08001253 if (LINUX_S_ISREG(inode.i_mode) &&
JP Abgralle0ed7402014-03-19 19:08:39 -07001254 ext2fs_needs_large_file_feature(EXT2_I_SIZE(&inode)))
Theodore Ts'oa4742692001-08-09 04:14:04 -04001255 ctx->large_files--;
1256
JP Abgralle0ed7402014-03-19 19:08:39 -07001257 del_block.ctx = ctx;
1258 del_block.num = 0;
1259 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf,
1260 deallocate_inode_block,
1261 &del_block);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001262 if (pctx.errcode) {
1263 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +00001264 ctx->flags |= E2F_FLAG_ABORT;
1265 return;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001266 }
JP Abgralle0ed7402014-03-19 19:08:39 -07001267clear_inode:
1268 /* Inode may have changed by block_iterate, so reread it */
1269 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1270 e2fsck_clear_inode(ctx, ino, &inode, 0, "deallocate_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001271}
1272
Theodore Ts'o8fdc9982002-06-25 23:26:34 -04001273/*
1274 * This fuction clears the htree flag on an inode
1275 */
1276static void clear_htree(e2fsck_t ctx, ext2_ino_t ino)
1277{
1278 struct ext2_inode inode;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001279
Theodore Ts'o8fdc9982002-06-25 23:26:34 -04001280 e2fsck_read_inode(ctx, ino, &inode, "clear_htree");
1281 inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL;
1282 e2fsck_write_inode(ctx, ino, &inode, "clear_htree");
Theodore Ts'ob7a00562002-07-20 00:28:07 -04001283 if (ctx->dirs_to_hash)
1284 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
Theodore Ts'o8fdc9982002-06-25 23:26:34 -04001285}
1286
1287
JP Abgralle0ed7402014-03-19 19:08:39 -07001288int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
1289 ext2_ino_t ino, char *buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001290{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001291 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001292 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001293 int inode_modified = 0;
Theodore Ts'o6c313fd2005-01-27 14:28:41 -05001294 int not_fixed = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001295 unsigned char *frag, *fsize;
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001296 struct problem_context pctx;
JP Abgralle0ed7402014-03-19 19:08:39 -07001297 problem_t problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001298
Theodore Ts'o08b21301997-11-03 19:42:40 +00001299 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001300
1301 clear_problem_context(&pctx);
1302 pctx.ino = ino;
1303 pctx.dir = dir;
1304 pctx.inode = &inode;
1305
JP Abgralle0ed7402014-03-19 19:08:39 -07001306 if (ext2fs_file_acl_block(fs, &inode) &&
Theodore Ts'of76344f2005-07-04 12:53:36 -05001307 !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
1308 if (fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001309 ext2fs_file_acl_block_set(fs, &inode, 0);
Theodore Ts'of76344f2005-07-04 12:53:36 -05001310 inode_modified++;
1311 } else
1312 not_fixed++;
1313 }
Theodore Ts'o6c313fd2005-01-27 14:28:41 -05001314
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001315 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
1316 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
1317 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
Theodore Ts'o08b21301997-11-03 19:42:40 +00001318 !(LINUX_S_ISSOCK(inode.i_mode)))
1319 problem = PR_2_BAD_MODE;
Theodore Ts'ofdbdea02001-06-02 04:26:26 +00001320 else if (LINUX_S_ISCHR(inode.i_mode)
Theodore Ts'o0684a4f2002-08-17 10:19:44 -04001321 && !e2fsck_pass1_check_device_inode(fs, &inode))
Theodore Ts'o08b21301997-11-03 19:42:40 +00001322 problem = PR_2_BAD_CHAR_DEV;
Theodore Ts'ofdbdea02001-06-02 04:26:26 +00001323 else if (LINUX_S_ISBLK(inode.i_mode)
Theodore Ts'o0684a4f2002-08-17 10:19:44 -04001324 && !e2fsck_pass1_check_device_inode(fs, &inode))
Theodore Ts'o08b21301997-11-03 19:42:40 +00001325 problem = PR_2_BAD_BLOCK_DEV;
Theodore Ts'ofdbdea02001-06-02 04:26:26 +00001326 else if (LINUX_S_ISFIFO(inode.i_mode)
Theodore Ts'o0684a4f2002-08-17 10:19:44 -04001327 && !e2fsck_pass1_check_device_inode(fs, &inode))
Theodore Ts'o1dde43f1998-11-14 04:18:28 +00001328 problem = PR_2_BAD_FIFO;
Theodore Ts'ofdbdea02001-06-02 04:26:26 +00001329 else if (LINUX_S_ISSOCK(inode.i_mode)
Theodore Ts'o0684a4f2002-08-17 10:19:44 -04001330 && !e2fsck_pass1_check_device_inode(fs, &inode))
Theodore Ts'o1dde43f1998-11-14 04:18:28 +00001331 problem = PR_2_BAD_SOCKET;
Theodore Ts'ofdbdea02001-06-02 04:26:26 +00001332 else if (LINUX_S_ISLNK(inode.i_mode)
Theodore Ts'o7cadc572008-03-13 23:05:00 -04001333 && !e2fsck_pass1_check_symlink(fs, ino, &inode, buf)) {
Theodore Ts'obcf9c5d2002-05-21 09:14:17 -04001334 problem = PR_2_INVALID_SYMLINK;
Andreas Dilger67052a82001-08-04 00:51:18 -06001335 }
Theodore Ts'o1dde43f1998-11-14 04:18:28 +00001336
Theodore Ts'o08b21301997-11-03 19:42:40 +00001337 if (problem) {
1338 if (fix_problem(ctx, problem, &pctx)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001339 deallocate_inode(ctx, ino, 0);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001340 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o08b21301997-11-03 19:42:40 +00001341 return 0;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +00001342 return 1;
Theodore Ts'o6c313fd2005-01-27 14:28:41 -05001343 } else
1344 not_fixed++;
Theodore Ts'o08b21301997-11-03 19:42:40 +00001345 problem = 0;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +00001346 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001347
Theodore Ts'o6c313fd2005-01-27 14:28:41 -05001348 if (inode.i_faddr) {
1349 if (fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
1350 inode.i_faddr = 0;
1351 inode_modified++;
1352 } else
1353 not_fixed++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001354 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001355
1356 switch (fs->super->s_creator_os) {
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001357 case EXT2_OS_HURD:
1358 frag = &inode.osd2.hurd2.h_i_frag;
1359 fsize = &inode.osd2.hurd2.h_i_fsize;
1360 break;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001361 default:
1362 frag = fsize = 0;
1363 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001364 if (frag && *frag) {
1365 pctx.num = *frag;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001366 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001367 *frag = 0;
1368 inode_modified++;
Theodore Ts'o7e0282c2005-01-27 21:40:53 -05001369 } else
1370 not_fixed++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001371 pctx.num = 0;
1372 }
1373 if (fsize && *fsize) {
1374 pctx.num = *fsize;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001375 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001376 *fsize = 0;
1377 inode_modified++;
Theodore Ts'o6c313fd2005-01-27 14:28:41 -05001378 } else
1379 not_fixed++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001380 pctx.num = 0;
1381 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001382
Theodore Ts'o5d171192006-11-11 06:32:03 -05001383 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001384 !(fs->super->s_feature_ro_compat &
Theodore Ts'o5d171192006-11-11 06:32:03 -05001385 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1386 (inode.osd2.linux2.l_i_blocks_hi != 0)) {
1387 pctx.num = inode.osd2.linux2.l_i_blocks_hi;
1388 if (fix_problem(ctx, PR_2_BLOCKS_HI_ZERO, &pctx)) {
1389 inode.osd2.linux2.l_i_blocks_hi = 0;
1390 inode_modified++;
1391 }
1392 }
1393
Theodore Ts'o911ec622009-04-23 21:31:16 -04001394 if (!(fs->super->s_feature_incompat &
1395 EXT4_FEATURE_INCOMPAT_64BIT) &&
1396 inode.osd2.linux2.l_i_file_acl_high != 0) {
1397 pctx.num = inode.osd2.linux2.l_i_file_acl_high;
1398 if (fix_problem(ctx, PR_2_I_FILE_ACL_HI_ZERO, &pctx)) {
1399 inode.osd2.linux2.l_i_file_acl_high = 0;
1400 inode_modified++;
1401 } else
1402 not_fixed++;
1403 }
1404
JP Abgralle0ed7402014-03-19 19:08:39 -07001405 if (ext2fs_file_acl_block(fs, &inode) &&
1406 ((ext2fs_file_acl_block(fs, &inode) < fs->super->s_first_data_block) ||
1407 (ext2fs_file_acl_block(fs, &inode) >= ext2fs_blocks_count(fs->super)))) {
Theodore Ts'o6c313fd2005-01-27 14:28:41 -05001408 if (fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001409 ext2fs_file_acl_block_set(fs, &inode, 0);
Theodore Ts'o6c313fd2005-01-27 14:28:41 -05001410 inode_modified++;
1411 } else
1412 not_fixed++;
Theodore Ts'o342d8472001-07-02 11:54:09 -04001413 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001414 if (inode.i_dir_acl &&
Theodore Ts'o6c313fd2005-01-27 14:28:41 -05001415 LINUX_S_ISDIR(inode.i_mode)) {
1416 if (fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
1417 inode.i_dir_acl = 0;
1418 inode_modified++;
1419 } else
1420 not_fixed++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001421 }
Theodore Ts'o6c313fd2005-01-27 14:28:41 -05001422
Theodore Ts'of3db3561997-04-26 13:34:30 +00001423 if (inode_modified)
Theodore Ts'o08b21301997-11-03 19:42:40 +00001424 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
Theodore Ts'of76344f2005-07-04 12:53:36 -05001425 if (!not_fixed && ctx->inode_bad_map)
JP Abgralle0ed7402014-03-19 19:08:39 -07001426 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001427 return 0;
1428}
1429
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001430
1431/*
1432 * allocate_dir_block --- this function allocates a new directory
1433 * block for a particular inode; this is done if a directory has
1434 * a "hole" in it, or if a directory has a illegal block number
1435 * that was zeroed out and now needs to be replaced.
1436 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001437static int allocate_dir_block(e2fsck_t ctx,
JP Abgralle0ed7402014-03-19 19:08:39 -07001438 struct ext2_db_entry2 *db,
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001439 char *buf EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -05001440 struct problem_context *pctx)
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001441{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001442 ext2_filsys fs = ctx->fs;
JP Abgralle0ed7402014-03-19 19:08:39 -07001443 blk64_t blk;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001444 char *block;
1445 struct ext2_inode inode;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001446
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001447 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001448 return 1;
1449
1450 /*
1451 * Read the inode and block bitmaps in; we'll be messing with
1452 * them.
1453 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001454 e2fsck_read_bitmaps(ctx);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001455
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001456 /*
1457 * First, find a free block
1458 */
JP Abgralle0ed7402014-03-19 19:08:39 -07001459 pctx->errcode = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001460 if (pctx->errcode) {
1461 pctx->str = "ext2fs_new_block";
1462 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001463 return 1;
1464 }
JP Abgralle0ed7402014-03-19 19:08:39 -07001465 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1466 ext2fs_mark_block_bitmap2(fs->block_map, blk);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001467 ext2fs_mark_bb_dirty(fs);
1468
1469 /*
1470 * Now let's create the actual data block for the inode
1471 */
1472 if (db->blockcnt)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001473 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001474 else
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001475 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
1476 EXT2_ROOT_INO, &block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001477
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001478 if (pctx->errcode) {
1479 pctx->str = "ext2fs_new_dir_block";
1480 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001481 return 1;
1482 }
1483
JP Abgralle0ed7402014-03-19 19:08:39 -07001484 pctx->errcode = ext2fs_write_dir_block3(fs, blk, block, 0);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -04001485 ext2fs_free_mem(&block);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001486 if (pctx->errcode) {
1487 pctx->str = "ext2fs_write_dir_block";
1488 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001489 return 1;
1490 }
1491
1492 /*
1493 * Update the inode block count
1494 */
Theodore Ts'o08b21301997-11-03 19:42:40 +00001495 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
Theodore Ts'o1ca10592008-04-09 11:39:11 -04001496 ext2fs_iblk_add_blocks(fs, &inode, 1);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001497 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
1498 inode.i_size = (db->blockcnt+1) * fs->blocksize;
Theodore Ts'o08b21301997-11-03 19:42:40 +00001499 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001500
1501 /*
1502 * Finally, update the block pointers for the inode
1503 */
1504 db->blk = blk;
JP Abgralle0ed7402014-03-19 19:08:39 -07001505 pctx->errcode = ext2fs_bmap2(fs, db->ino, &inode, 0, BMAP_SET,
1506 db->blockcnt, 0, &blk);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001507 if (pctx->errcode) {
1508 pctx->str = "ext2fs_block_iterate";
1509 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001510 return 1;
1511 }
1512
1513 return 0;
1514}