blob: e6a434cb246bff04ca7fd01671b773bceb683061 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * pass2.c --- check directory structure
3 *
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'o3839e651997-04-26 13:21:57 +000010 *
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
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
Theodore Ts'o3839e651997-04-26 13:21:57 +000044#include "e2fsck.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000045#include "problem.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000046
Theodore Ts'oaa4115a1999-10-21 19:33:18 +000047#ifdef NO_INLINE_FUNCS
48#define _INLINE_
49#else
50#define _INLINE_ inline
51#endif
52
Theodore Ts'o3839e651997-04-26 13:21:57 +000053/*
54 * Keeps track of how many times an inode is referenced.
55 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000056static void deallocate_inode(e2fsck_t ctx, ino_t ino,
Theodore Ts'o3839e651997-04-26 13:21:57 +000057 char* block_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +000058static int check_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000059 struct ext2_db_entry *dir_blocks_info,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +000060 void *priv_data);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000061static int allocate_dir_block(e2fsck_t ctx,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000062 struct ext2_db_entry *dir_blocks_info,
63 char *buf, struct problem_context *pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000064static int update_dir_block(ext2_filsys fs,
65 blk_t *block_nr,
66 int blockcnt,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +000067 void *priv_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +000068
Theodore Ts'o21c84b71997-04-29 16:15:03 +000069struct check_dir_struct {
70 char *buf;
71 struct problem_context pctx;
Theodore Ts'of8188ff1997-11-14 05:23:04 +000072 int count, max;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000073 e2fsck_t ctx;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000074};
75
Theodore Ts'o08b21301997-11-03 19:42:40 +000076void e2fsck_pass2(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +000077{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000078 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +000079 char *buf;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000080#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000081 struct resource_track rtrack;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000082#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +000083 struct dir_info *dir;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000084 struct check_dir_struct cd;
85
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000086#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000087 init_resource_track(&rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000088#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000089
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000090 clear_problem_context(&cd.pctx);
91
Theodore Ts'o3839e651997-04-26 13:21:57 +000092#ifdef MTRACE
93 mtrace_print("Pass 2");
94#endif
95
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000096 if (!(ctx->options & E2F_OPT_PREEN))
97 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
98
99 cd.pctx.errcode = ext2fs_create_icount2(fs, EXT2_ICOUNT_OPT_INCREMENT,
100 0, ctx->inode_link_info,
101 &ctx->inode_count);
102 if (cd.pctx.errcode) {
103 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000104 ctx->flags |= E2F_FLAG_ABORT;
105 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000106 }
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000107 buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize,
108 "directory scan buffer");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000109
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000110 /*
111 * Set up the parent pointer for the root directory, if
112 * present. (If the root directory is not present, we will
113 * create it in pass 3.)
114 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000115 dir = e2fsck_get_dir_info(ctx, EXT2_ROOT_INO);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000116 if (dir)
117 dir->parent = EXT2_ROOT_INO;
118
119 cd.buf = buf;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000120 cd.ctx = ctx;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000121 cd.count = 1;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000122 cd.max = ext2fs_dblist_count(fs->dblist);
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000123
124 if (ctx->progress)
125 (void) (ctx->progress)(ctx, 2, 0, cd.max);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000126
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000127 cd.pctx.errcode = ext2fs_dblist_iterate(fs->dblist, check_dir_block,
128 &cd);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000129 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000130 return;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000131 if (cd.pctx.errcode) {
132 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000133 ctx->flags |= E2F_FLAG_ABORT;
134 return;
Theodore Ts'o7ac02a51997-06-11 18:32:35 +0000135 }
136
Theodore Ts'o08b21301997-11-03 19:42:40 +0000137 ext2fs_free_mem((void **) &buf);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000138 ext2fs_free_dblist(fs->dblist);
139
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000140 if (ctx->inode_bad_map) {
141 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
142 ctx->inode_bad_map = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000143 }
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000144 if (ctx->inode_reg_map) {
145 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
146 ctx->inode_reg_map = 0;
147 }
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000148#ifdef RESOURCE_TRACK
Theodore Ts'o5596def1999-07-19 15:27:37 +0000149 if (ctx->options & E2F_OPT_TIME2) {
150 e2fsck_clear_progbar(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000151 print_resource_track("Pass 2", &rtrack);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000152 }
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000153#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000154}
155
156/*
157 * Make sure the first entry in the directory is '.', and that the
158 * directory entry is sane.
159 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000160static int check_dot(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000161 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000162 ino_t ino, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000163{
164 struct ext2_dir_entry *nextdir;
165 int status = 0;
166 int created = 0;
167 int new_len;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000168 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000169
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000170 if (!dirent->inode)
171 problem = PR_2_MISSING_DOT;
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000172 else if (((dirent->name_len & 0xFF) != 1) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000173 (dirent->name[0] != '.'))
174 problem = PR_2_1ST_NOT_DOT;
175 else if (dirent->name[1] != '\0')
176 problem = PR_2_DOT_NULL_TERM;
177
178 if (problem) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000179 if (fix_problem(ctx, problem, pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000180 if (dirent->rec_len < 12)
181 dirent->rec_len = 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000182 dirent->inode = ino;
183 dirent->name_len = 1;
184 dirent->name[0] = '.';
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000185 dirent->name[1] = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000186 status = 1;
187 created = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000188 }
189 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000190 if (dirent->inode != ino) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000191 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000192 dirent->inode = ino;
193 status = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000194 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000195 }
196 if (dirent->rec_len > 12) {
197 new_len = dirent->rec_len - 12;
198 if (new_len > 12) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000199 if (created ||
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000200 fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000201 nextdir = (struct ext2_dir_entry *)
202 ((char *) dirent + 12);
203 dirent->rec_len = 12;
204 nextdir->rec_len = new_len;
205 nextdir->inode = 0;
206 nextdir->name_len = 0;
207 status = 1;
208 }
209 }
210 }
211 return status;
212}
213
214/*
215 * Make sure the second entry in the directory is '..', and that the
216 * directory entry is sane. We do not check the inode number of '..'
217 * here; this gets done in pass 3.
218 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000219static int check_dotdot(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000220 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000221 struct dir_info *dir, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000222{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000223 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000224
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000225 if (!dirent->inode)
226 problem = PR_2_MISSING_DOT_DOT;
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000227 else if (((dirent->name_len & 0xFF) != 2) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000228 (dirent->name[0] != '.') ||
229 (dirent->name[1] != '.'))
230 problem = PR_2_2ND_NOT_DOT_DOT;
231 else if (dirent->name[2] != '\0')
232 problem = PR_2_DOT_DOT_NULL_TERM;
233
234 if (problem) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000235 if (fix_problem(ctx, problem, pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000236 if (dirent->rec_len < 12)
237 dirent->rec_len = 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000238 /*
239 * Note: we don't have the parent inode just
240 * yet, so we will fill it in with the root
241 * inode. This will get fixed in pass 3.
242 */
243 dirent->inode = EXT2_ROOT_INO;
244 dirent->name_len = 2;
245 dirent->name[0] = '.';
246 dirent->name[1] = '.';
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000247 dirent->name[2] = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000248 return 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000249 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000250 return 0;
251 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000252 dir->dotdot = dirent->inode;
253 return 0;
254}
255
256/*
257 * Check to make sure a directory entry doesn't contain any illegal
258 * characters.
259 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000260static int check_name(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000261 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000262 ino_t dir_ino, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000263{
264 int i;
265 int fixup = -1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000266 int ret = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000267
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000268 for ( i = 0; i < (dirent->name_len & 0xFF); i++) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000269 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
270 if (fixup < 0) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000271 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000272 }
273 if (fixup) {
274 dirent->name[i] = '.';
275 ret = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000276 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000277 }
278 }
279 return ret;
280}
281
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000282/*
283 * Check the directory filetype (if present)
284 */
285static _INLINE_ int check_filetype(e2fsck_t ctx,
286 struct ext2_dir_entry *dirent,
287 ino_t dir_ino, struct problem_context *pctx)
288{
289 int filetype = dirent->name_len >> 8;
290 int should_be = EXT2_FT_UNKNOWN;
291 struct ext2_inode inode;
292
293 if (!(ctx->fs->super->s_feature_incompat &
Theodore Ts'o7847c1d1999-10-22 15:11:42 +0000294 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
295 if (filetype == 0 ||
296 !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx))
297 return 0;
298 dirent->name_len = dirent->name_len & 0xFF;
299 return 1;
300 }
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000301
302 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dirent->inode)) {
303 should_be = EXT2_FT_DIR;
304 } else if (ext2fs_test_inode_bitmap(ctx->inode_reg_map,
305 dirent->inode)) {
306 should_be = EXT2_FT_REG_FILE;
307 } else if (ctx->inode_bad_map &&
308 ext2fs_test_inode_bitmap(ctx->inode_bad_map,
309 dirent->inode))
310 should_be = 0;
311 else {
312 e2fsck_read_inode(ctx, dirent->inode, &inode,
313 "check_filetype");
314 if (LINUX_S_ISCHR (inode.i_mode))
315 should_be = EXT2_FT_CHRDEV;
316 else if (LINUX_S_ISBLK (inode.i_mode))
317 should_be = EXT2_FT_BLKDEV;
318 else if (LINUX_S_ISLNK (inode.i_mode))
319 should_be = EXT2_FT_SYMLINK;
320 else if (LINUX_S_ISFIFO (inode.i_mode))
321 should_be = EXT2_FT_FIFO;
322 else if (LINUX_S_ISSOCK (inode.i_mode))
323 should_be = EXT2_FT_SOCK;
324 }
325 if (filetype == should_be)
326 return 0;
327 pctx->num = should_be;
328
329 if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE,
330 pctx) == 0)
331 return 0;
332
333 dirent->name_len = (dirent->name_len & 0xFF) | should_be << 8;
334 return 1;
335}
336
337
Theodore Ts'o3839e651997-04-26 13:21:57 +0000338static int check_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000339 struct ext2_db_entry *db,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000340 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000341{
342 struct dir_info *subdir, *dir;
343 struct ext2_dir_entry *dirent;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000344 int offset = 0;
345 int dir_modified = 0;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000346 int dot_state;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000347 blk_t block_nr = db->blk;
348 ino_t ino = db->ino;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000349 __u16 links;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000350 struct check_dir_struct *cd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000351 char *buf;
352 e2fsck_t ctx;
353 int problem;
354
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000355 cd = (struct check_dir_struct *) priv_data;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000356 buf = cd->buf;
357 ctx = cd->ctx;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000358
359 if (ctx->progress)
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000360 if ((ctx->progress)(ctx, 2, cd->count++, cd->max))
361 return DIRENT_ABORT;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000362
Theodore Ts'o3839e651997-04-26 13:21:57 +0000363 /*
364 * Make sure the inode is still in use (could have been
365 * deleted in the duplicate/bad blocks pass.
366 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000367 if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, ino)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000368 return 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000369
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000370 cd->pctx.ino = ino;
371 cd->pctx.blk = block_nr;
372 cd->pctx.blkcount = db->blockcnt;
373 cd->pctx.ino2 = 0;
374 cd->pctx.dirent = 0;
375 cd->pctx.num = 0;
376
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000377 if (db->blk == 0) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000378 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000379 return 0;
380 block_nr = db->blk;
381 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000382
383 if (db->blockcnt)
384 dot_state = 2;
385 else
386 dot_state = 0;
387
388#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000389 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000390 db->blockcnt, ino);
391#endif
392
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000393 cd->pctx.errcode = ext2fs_read_dir_block(fs, block_nr, buf);
394 if (cd->pctx.errcode) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000395 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
396 ctx->flags |= E2F_FLAG_ABORT;
397 return DIRENT_ABORT;
398 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000399 memset(buf, 0, fs->blocksize);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000400 }
401
402 do {
403 dot_state++;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000404 problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000405 dirent = (struct ext2_dir_entry *) (buf + offset);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000406 cd->pctx.dirent = dirent;
407 cd->pctx.num = offset;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000408 if (((offset + dirent->rec_len) > fs->blocksize) ||
Theodore Ts'oc40db6d1999-10-25 21:03:34 +0000409 (dirent->rec_len < 12) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000410 ((dirent->rec_len % 4) != 0) ||
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000411 (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000412 if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000413 dirent->rec_len = fs->blocksize - offset;
414 dirent->name_len = 0;
415 dirent->inode = 0;
416 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000417 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000418 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000419 }
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000420 if ((dirent->name_len & 0xFF) > EXT2_NAME_LEN) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000421 if (fix_problem(ctx, PR_2_FILENAME_LONG, &cd->pctx)) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000422 dirent->name_len = EXT2_NAME_LEN;
423 dir_modified++;
424 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000425 }
426
Theodore Ts'o3839e651997-04-26 13:21:57 +0000427 if (dot_state == 1) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000428 if (check_dot(ctx, dirent, ino, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000429 dir_modified++;
430 } else if (dot_state == 2) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000431 dir = e2fsck_get_dir_info(ctx, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000432 if (!dir) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000433 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000434 ctx->flags |= E2F_FLAG_ABORT;
435 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000436 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000437 if (check_dotdot(ctx, dirent, dir, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000438 dir_modified++;
439 } else if (dirent->inode == ino) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000440 problem = PR_2_LINK_DOT;
441 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000442 dirent->inode = 0;
443 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000444 goto next;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000445 }
446 }
447 if (!dirent->inode)
448 goto next;
449
Theodore Ts'o3839e651997-04-26 13:21:57 +0000450 /*
451 * Make sure the inode listed is a legal one.
452 */
453 if (((dirent->inode != EXT2_ROOT_INO) &&
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000454 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000455 (dirent->inode > fs->super->s_inodes_count)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000456 problem = PR_2_BAD_INO;
457 } else if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000458 dirent->inode))) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000459 /*
460 * If the inode is unused, offer to clear it.
461 */
462 problem = PR_2_UNUSED_INODE;
463 } else if (ctx->inode_bb_map &&
464 (ext2fs_test_inode_bitmap(ctx->inode_bb_map,
465 dirent->inode))) {
466 /*
467 * If the inode is in a bad block, offer to
468 * clear it.
469 */
470 problem = PR_2_BB_INODE;
471 } else if ((dot_state > 2) &&
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000472 ((dirent->name_len & 0xFF) == 1) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000473 (dirent->name[0] == '.')) {
474 /*
475 * If there's a '.' entry in anything other
476 * than the first directory entry, it's a
477 * duplicate entry that should be removed.
478 */
479 problem = PR_2_DUP_DOT;
480 } else if ((dot_state > 2) &&
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000481 ((dirent->name_len & 0xFF) == 2) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000482 (dirent->name[0] == '.') &&
483 (dirent->name[1] == '.')) {
484 /*
485 * If there's a '..' entry in anything other
486 * than the second directory entry, it's a
487 * duplicate entry that should be removed.
488 */
489 problem = PR_2_DUP_DOT_DOT;
490 } else if ((dot_state > 2) &&
491 (dirent->inode == EXT2_ROOT_INO)) {
492 /*
493 * Don't allow links to the root directory.
494 * We check this specially to make sure we
495 * catch this error case even if the root
496 * directory hasn't been created yet.
497 */
498 problem = PR_2_LINK_ROOT;
Theodore Ts'oc40db6d1999-10-25 21:03:34 +0000499 } else if ((dot_state > 2) &&
500 (dirent->name_len & 0xFF) == 0) {
501 /*
502 * Don't allow zero-length directory names.
503 */
504 problem = PR_2_NULL_NAME;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000505 }
506
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000507 if (problem) {
508 if (fix_problem(ctx, problem, &cd->pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000509 dirent->inode = 0;
510 dir_modified++;
511 goto next;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000512 } else {
513 ext2fs_unmark_valid(fs);
514 if (problem == PR_2_BAD_INO)
515 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000516 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000517 }
518
519 /*
520 * If the inode was marked as having bad fields in
521 * pass1, process it and offer to fix/clear it.
522 * (We wait until now so that we can display the
523 * pathname to the user.)
524 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000525 if (ctx->inode_bad_map &&
526 ext2fs_test_inode_bitmap(ctx->inode_bad_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000527 dirent->inode)) {
Theodore Ts'oe72a9ba1999-06-25 15:40:18 +0000528 if (e2fsck_process_bad_inode(ctx, ino,
529 dirent->inode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000530 dirent->inode = 0;
531 dir_modified++;
532 goto next;
533 }
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000534 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000535 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000536 }
537
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000538 if (check_name(ctx, dirent, ino, &cd->pctx))
539 dir_modified++;
540
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000541 if (check_filetype(ctx, dirent, ino, &cd->pctx))
542 dir_modified++;
543
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000544 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000545 * If this is a directory, then mark its parent in its
546 * dir_info structure. If the parent field is already
547 * filled in, then this directory has more than one
548 * hard link. We assume the first link is correct,
549 * and ask the user if he/she wants to clear this one.
550 */
551 if ((dot_state > 2) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000552 (ext2fs_test_inode_bitmap(ctx->inode_dir_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000553 dirent->inode))) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000554 subdir = e2fsck_get_dir_info(ctx, dirent->inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000555 if (!subdir) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000556 cd->pctx.ino = dirent->inode;
557 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000558 ctx->flags |= E2F_FLAG_ABORT;
559 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000560 }
561 if (subdir->parent) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000562 cd->pctx.ino2 = subdir->parent;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000563 if (fix_problem(ctx, PR_2_LINK_DIR,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000564 &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000565 dirent->inode = 0;
566 dir_modified++;
567 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000568 }
569 cd->pctx.ino2 = 0;
570 } else
571 subdir->parent = ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000572 }
573
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000574 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
575 &links);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000576 if (links > 1)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000577 ctx->fs_links_count++;
578 ctx->fs_total_count++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000579 next:
580 offset += dirent->rec_len;
581 } while (offset < fs->blocksize);
582#if 0
583 printf("\n");
584#endif
585 if (offset != fs->blocksize) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000586 cd->pctx.num = dirent->rec_len - fs->blocksize + offset;
587 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
588 dirent->rec_len = cd->pctx.num;
589 dir_modified++;
590 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000591 }
592 if (dir_modified) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000593 cd->pctx.errcode = ext2fs_write_dir_block(fs, block_nr, buf);
594 if (cd->pctx.errcode) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000595 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
596 &cd->pctx)) {
597 ctx->flags |= E2F_FLAG_ABORT;
598 return DIRENT_ABORT;
599 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000600 }
601 ext2fs_mark_changed(fs);
602 }
603 return 0;
604}
605
606/*
607 * This function is called to deallocate a block, and is an interator
608 * functioned called by deallocate inode via ext2fs_iterate_block().
609 */
610static int deallocate_inode_block(ext2_filsys fs,
611 blk_t *block_nr,
612 int blockcnt,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000613 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000614{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000615 e2fsck_t ctx = (e2fsck_t) priv_data;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000616
Theodore Ts'o3839e651997-04-26 13:21:57 +0000617 if (!*block_nr)
618 return 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000619 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000620 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000621 return 0;
622}
623
624/*
625 * This fuction deallocates an inode
626 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000627static void deallocate_inode(e2fsck_t ctx, ino_t ino,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628 char* block_buf)
629{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000630 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000631 struct ext2_inode inode;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000632 struct problem_context pctx;
633
634 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000635 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000636 inode.i_links_count = 0;
637 inode.i_dtime = time(0);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000638 e2fsck_write_inode(ctx, ino, &inode, "deallocate_inode");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000639 clear_problem_context(&pctx);
640 pctx.ino = ino;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000641
Theodore Ts'o3839e651997-04-26 13:21:57 +0000642 /*
643 * Fix up the bitmaps...
644 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000645 e2fsck_read_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000646 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
647 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
648 if (ctx->inode_bad_map)
649 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000650 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000651 ext2fs_mark_ib_dirty(fs);
652
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000653 if (!ext2fs_inode_has_valid_blocks(&inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000654 return;
655
656 ext2fs_mark_bb_dirty(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000657 pctx.errcode = ext2fs_block_iterate(fs, ino, 0, block_buf,
658 deallocate_inode_block, ctx);
659 if (pctx.errcode) {
660 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000661 ctx->flags |= E2F_FLAG_ABORT;
662 return;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000663 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000664}
665
Theodore Ts'oe72a9ba1999-06-25 15:40:18 +0000666extern int e2fsck_process_bad_inode(e2fsck_t ctx, ino_t dir, ino_t ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000667{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000668 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000669 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000670 int inode_modified = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000671 unsigned char *frag, *fsize;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000672 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000673 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000674
Theodore Ts'o08b21301997-11-03 19:42:40 +0000675 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000676
677 clear_problem_context(&pctx);
678 pctx.ino = ino;
679 pctx.dir = dir;
680 pctx.inode = &inode;
681
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000682 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
683 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
684 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
Theodore Ts'o08b21301997-11-03 19:42:40 +0000685 !(LINUX_S_ISSOCK(inode.i_mode)))
686 problem = PR_2_BAD_MODE;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000687
688 if (LINUX_S_ISCHR(inode.i_mode)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000689 && !e2fsck_pass1_check_device_inode(&inode))
690 problem = PR_2_BAD_CHAR_DEV;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000691
692 if (LINUX_S_ISBLK(inode.i_mode)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000693 && !e2fsck_pass1_check_device_inode(&inode))
694 problem = PR_2_BAD_BLOCK_DEV;
695
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000696 if (LINUX_S_ISFIFO(inode.i_mode)
697 && !e2fsck_pass1_check_device_inode(&inode))
698 problem = PR_2_BAD_FIFO;
699
700 if (LINUX_S_ISSOCK(inode.i_mode)
701 && !e2fsck_pass1_check_device_inode(&inode))
702 problem = PR_2_BAD_SOCKET;
703
Theodore Ts'o08b21301997-11-03 19:42:40 +0000704 if (problem) {
705 if (fix_problem(ctx, problem, &pctx)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000706 deallocate_inode(ctx, ino, 0);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000707 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000708 return 0;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000709 return 1;
710 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000711 problem = 0;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000712 }
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000713
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000714 if (inode.i_faddr &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000715 fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000716 inode.i_faddr = 0;
717 inode_modified++;
718 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000719
720 switch (fs->super->s_creator_os) {
721 case EXT2_OS_LINUX:
722 frag = &inode.osd2.linux2.l_i_frag;
723 fsize = &inode.osd2.linux2.l_i_fsize;
724 break;
725 case EXT2_OS_HURD:
726 frag = &inode.osd2.hurd2.h_i_frag;
727 fsize = &inode.osd2.hurd2.h_i_fsize;
728 break;
729 case EXT2_OS_MASIX:
730 frag = &inode.osd2.masix2.m_i_frag;
731 fsize = &inode.osd2.masix2.m_i_fsize;
732 break;
733 default:
734 frag = fsize = 0;
735 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000736 if (frag && *frag) {
737 pctx.num = *frag;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000738 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000739 *frag = 0;
740 inode_modified++;
741 }
742 pctx.num = 0;
743 }
744 if (fsize && *fsize) {
745 pctx.num = *fsize;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000746 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000747 *fsize = 0;
748 inode_modified++;
749 }
750 pctx.num = 0;
751 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000752
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000753 if (inode.i_file_acl &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000754 fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000755 inode.i_file_acl = 0;
756 inode_modified++;
757 }
758 if (inode.i_dir_acl &&
Theodore Ts'o246501c1998-03-24 16:22:38 +0000759 LINUX_S_ISDIR(inode.i_mode) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000760 fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000761 inode.i_dir_acl = 0;
762 inode_modified++;
763 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000764 if (inode_modified)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000765 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000766 return 0;
767}
768
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000769
770/*
771 * allocate_dir_block --- this function allocates a new directory
772 * block for a particular inode; this is done if a directory has
773 * a "hole" in it, or if a directory has a illegal block number
774 * that was zeroed out and now needs to be replaced.
775 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000776static int allocate_dir_block(e2fsck_t ctx,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000777 struct ext2_db_entry *db,
778 char *buf, struct problem_context *pctx)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000779{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000780 ext2_filsys fs = ctx->fs;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000781 blk_t blk;
782 char *block;
783 struct ext2_inode inode;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000784
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000785 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000786 return 1;
787
788 /*
789 * Read the inode and block bitmaps in; we'll be messing with
790 * them.
791 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000792 e2fsck_read_bitmaps(ctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000793
794 /*
795 * First, find a free block
796 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000797 pctx->errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
798 if (pctx->errcode) {
799 pctx->str = "ext2fs_new_block";
800 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000801 return 1;
802 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000803 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000804 ext2fs_mark_block_bitmap(fs->block_map, blk);
805 ext2fs_mark_bb_dirty(fs);
806
807 /*
808 * Now let's create the actual data block for the inode
809 */
810 if (db->blockcnt)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000811 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000812 else
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000813 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
814 EXT2_ROOT_INO, &block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000815
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000816 if (pctx->errcode) {
817 pctx->str = "ext2fs_new_dir_block";
818 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000819 return 1;
820 }
821
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000822 pctx->errcode = ext2fs_write_dir_block(fs, blk, block);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000823 ext2fs_free_mem((void **) &block);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000824 if (pctx->errcode) {
825 pctx->str = "ext2fs_write_dir_block";
826 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000827 return 1;
828 }
829
830 /*
831 * Update the inode block count
832 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000833 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000834 inode.i_blocks += fs->blocksize / 512;
835 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
836 inode.i_size = (db->blockcnt+1) * fs->blocksize;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000837 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000838
839 /*
840 * Finally, update the block pointers for the inode
841 */
842 db->blk = blk;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000843 pctx->errcode = ext2fs_block_iterate(fs, db->ino, BLOCK_FLAG_HOLE,
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000844 0, update_dir_block, db);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000845 if (pctx->errcode) {
846 pctx->str = "ext2fs_block_iterate";
847 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000848 return 1;
849 }
850
851 return 0;
852}
853
854/*
855 * This is a helper function for allocate_dir_block().
856 */
857static int update_dir_block(ext2_filsys fs,
858 blk_t *block_nr,
859 int blockcnt,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000860 void *priv_data)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000861{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000862 struct ext2_db_entry *db;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000863
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000864 db = (struct ext2_db_entry *) priv_data;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000865 if (db->blockcnt == blockcnt) {
866 *block_nr = db->blk;
867 return BLOCK_CHANGED;
868 }
869 return 0;
870}