blob: 1767d00ca1a0fec1cfacc13749879656cd5f98d4 [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
41 */
42
Theodore Ts'o3839e651997-04-26 13:21:57 +000043#include "e2fsck.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000044#include "problem.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000045
46/*
47 * Keeps track of how many times an inode is referenced.
48 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000049static void deallocate_inode(e2fsck_t ctx, ino_t ino,
Theodore Ts'o3839e651997-04-26 13:21:57 +000050 char* block_buf);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000051static int process_bad_inode(e2fsck_t ctx, ino_t dir, ino_t ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +000052static int check_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000053 struct ext2_db_entry *dir_blocks_info,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +000054 void *priv_data);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000055static int allocate_dir_block(e2fsck_t ctx,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000056 struct ext2_db_entry *dir_blocks_info,
57 char *buf, struct problem_context *pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000058static int update_dir_block(ext2_filsys fs,
59 blk_t *block_nr,
60 int blockcnt,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +000061 void *priv_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +000062
Theodore Ts'o21c84b71997-04-29 16:15:03 +000063struct check_dir_struct {
64 char *buf;
65 struct problem_context pctx;
Theodore Ts'of8188ff1997-11-14 05:23:04 +000066 int count, max;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000067 e2fsck_t ctx;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000068};
69
Theodore Ts'o08b21301997-11-03 19:42:40 +000070void e2fsck_pass2(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +000071{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000072 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +000073 char *buf;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000074#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000075 struct resource_track rtrack;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000076#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +000077 struct dir_info *dir;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000078 struct check_dir_struct cd;
79
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000080#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000081 init_resource_track(&rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000082#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000083
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000084 clear_problem_context(&cd.pctx);
85
Theodore Ts'o3839e651997-04-26 13:21:57 +000086#ifdef MTRACE
87 mtrace_print("Pass 2");
88#endif
89
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000090 if (!(ctx->options & E2F_OPT_PREEN))
91 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
92
93 cd.pctx.errcode = ext2fs_create_icount2(fs, EXT2_ICOUNT_OPT_INCREMENT,
94 0, ctx->inode_link_info,
95 &ctx->inode_count);
96 if (cd.pctx.errcode) {
97 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +000098 ctx->flags |= E2F_FLAG_ABORT;
99 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000100 }
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000101 buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize,
102 "directory scan buffer");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000104 /*
105 * Set up the parent pointer for the root directory, if
106 * present. (If the root directory is not present, we will
107 * create it in pass 3.)
108 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000109 dir = e2fsck_get_dir_info(ctx, EXT2_ROOT_INO);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000110 if (dir)
111 dir->parent = EXT2_ROOT_INO;
112
113 cd.buf = buf;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000114 cd.ctx = ctx;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000115 cd.count = 1;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000116 cd.max = ext2fs_dblist_count(fs->dblist);
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000117
118 if (ctx->progress)
119 (void) (ctx->progress)(ctx, 2, 0, cd.max);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000120
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000121 cd.pctx.errcode = ext2fs_dblist_iterate(fs->dblist, check_dir_block,
122 &cd);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000123 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000124 return;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000125 if (cd.pctx.errcode) {
126 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000127 ctx->flags |= E2F_FLAG_ABORT;
128 return;
Theodore Ts'o7ac02a51997-06-11 18:32:35 +0000129 }
130
Theodore Ts'o08b21301997-11-03 19:42:40 +0000131 ext2fs_free_mem((void **) &buf);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000132 ext2fs_free_dblist(fs->dblist);
133
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000134 if (ctx->inode_bad_map) {
135 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
136 ctx->inode_bad_map = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000137 }
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000138#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000139 if (ctx->options & E2F_OPT_TIME2)
140 print_resource_track("Pass 2", &rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000141#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000142}
143
144/*
145 * Make sure the first entry in the directory is '.', and that the
146 * directory entry is sane.
147 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000148static int check_dot(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000149 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000150 ino_t ino, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000151{
152 struct ext2_dir_entry *nextdir;
153 int status = 0;
154 int created = 0;
155 int new_len;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000156 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000158 if (!dirent->inode)
159 problem = PR_2_MISSING_DOT;
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000160 else if (((dirent->name_len & 0xFF) != 1) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000161 (dirent->name[0] != '.'))
162 problem = PR_2_1ST_NOT_DOT;
163 else if (dirent->name[1] != '\0')
164 problem = PR_2_DOT_NULL_TERM;
165
166 if (problem) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000167 if (fix_problem(ctx, problem, pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000168 if (dirent->rec_len < 12)
169 dirent->rec_len = 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000170 dirent->inode = ino;
171 dirent->name_len = 1;
172 dirent->name[0] = '.';
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000173 dirent->name[1] = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000174 status = 1;
175 created = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000176 }
177 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000178 if (dirent->inode != ino) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000179 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000180 dirent->inode = ino;
181 status = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000182 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000183 }
184 if (dirent->rec_len > 12) {
185 new_len = dirent->rec_len - 12;
186 if (new_len > 12) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000187 if (created ||
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000188 fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000189 nextdir = (struct ext2_dir_entry *)
190 ((char *) dirent + 12);
191 dirent->rec_len = 12;
192 nextdir->rec_len = new_len;
193 nextdir->inode = 0;
194 nextdir->name_len = 0;
195 status = 1;
196 }
197 }
198 }
199 return status;
200}
201
202/*
203 * Make sure the second entry in the directory is '..', and that the
204 * directory entry is sane. We do not check the inode number of '..'
205 * here; this gets done in pass 3.
206 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000207static int check_dotdot(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000208 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000209 struct dir_info *dir, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000210{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000211 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000212
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000213 if (!dirent->inode)
214 problem = PR_2_MISSING_DOT_DOT;
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000215 else if (((dirent->name_len & 0xFF) != 2) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000216 (dirent->name[0] != '.') ||
217 (dirent->name[1] != '.'))
218 problem = PR_2_2ND_NOT_DOT_DOT;
219 else if (dirent->name[2] != '\0')
220 problem = PR_2_DOT_DOT_NULL_TERM;
221
222 if (problem) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000223 if (fix_problem(ctx, problem, pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000224 if (dirent->rec_len < 12)
225 dirent->rec_len = 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000226 /*
227 * Note: we don't have the parent inode just
228 * yet, so we will fill it in with the root
229 * inode. This will get fixed in pass 3.
230 */
231 dirent->inode = EXT2_ROOT_INO;
232 dirent->name_len = 2;
233 dirent->name[0] = '.';
234 dirent->name[1] = '.';
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000235 dirent->name[2] = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000236 return 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000237 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000238 return 0;
239 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000240 dir->dotdot = dirent->inode;
241 return 0;
242}
243
244/*
245 * Check to make sure a directory entry doesn't contain any illegal
246 * characters.
247 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000248static int check_name(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000249 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000250 ino_t dir_ino, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000251{
252 int i;
253 int fixup = -1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000254 int ret = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000255
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000256 for ( i = 0; i < (dirent->name_len & 0xFF); i++) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000257 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
258 if (fixup < 0) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000259 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000260 }
261 if (fixup) {
262 dirent->name[i] = '.';
263 ret = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000264 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000265 }
266 }
267 return ret;
268}
269
270static int check_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000271 struct ext2_db_entry *db,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000272 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000273{
274 struct dir_info *subdir, *dir;
275 struct ext2_dir_entry *dirent;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000276 int offset = 0;
277 int dir_modified = 0;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000278 int dot_state;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000279 blk_t block_nr = db->blk;
280 ino_t ino = db->ino;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000281 __u16 links;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000282 struct check_dir_struct *cd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000283 char *buf;
284 e2fsck_t ctx;
285 int problem;
286
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000287 cd = (struct check_dir_struct *) priv_data;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000288 buf = cd->buf;
289 ctx = cd->ctx;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000290
291 if (ctx->progress)
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000292 if ((ctx->progress)(ctx, 2, cd->count++, cd->max))
293 return DIRENT_ABORT;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000294
Theodore Ts'o3839e651997-04-26 13:21:57 +0000295 /*
296 * Make sure the inode is still in use (could have been
297 * deleted in the duplicate/bad blocks pass.
298 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000299 if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, ino)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000300 return 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000301
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000302 cd->pctx.ino = ino;
303 cd->pctx.blk = block_nr;
304 cd->pctx.blkcount = db->blockcnt;
305 cd->pctx.ino2 = 0;
306 cd->pctx.dirent = 0;
307 cd->pctx.num = 0;
308
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000309 if (db->blk == 0) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000310 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000311 return 0;
312 block_nr = db->blk;
313 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000314
315 if (db->blockcnt)
316 dot_state = 2;
317 else
318 dot_state = 0;
319
320#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000321 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000322 db->blockcnt, ino);
323#endif
324
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000325 cd->pctx.errcode = ext2fs_read_dir_block(fs, block_nr, buf);
326 if (cd->pctx.errcode) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000327 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
328 ctx->flags |= E2F_FLAG_ABORT;
329 return DIRENT_ABORT;
330 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000331 memset(buf, 0, fs->blocksize);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000332 }
333
334 do {
335 dot_state++;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000336 problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000337 dirent = (struct ext2_dir_entry *) (buf + offset);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000338 cd->pctx.dirent = dirent;
339 cd->pctx.num = offset;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000340 if (((offset + dirent->rec_len) > fs->blocksize) ||
341 (dirent->rec_len < 8) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000342 ((dirent->rec_len % 4) != 0) ||
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000343 (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000344 if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000345 dirent->rec_len = fs->blocksize - offset;
346 dirent->name_len = 0;
347 dirent->inode = 0;
348 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000349 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000350 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000351 }
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000352 if ((dirent->name_len & 0xFF) > EXT2_NAME_LEN) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000353 if (fix_problem(ctx, PR_2_FILENAME_LONG, &cd->pctx)) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000354 dirent->name_len = EXT2_NAME_LEN;
355 dir_modified++;
356 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000357 }
358
Theodore Ts'o3839e651997-04-26 13:21:57 +0000359 if (dot_state == 1) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000360 if (check_dot(ctx, dirent, ino, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000361 dir_modified++;
362 } else if (dot_state == 2) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000363 dir = e2fsck_get_dir_info(ctx, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000364 if (!dir) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000365 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000366 ctx->flags |= E2F_FLAG_ABORT;
367 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000368 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000369 if (check_dotdot(ctx, dirent, dir, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000370 dir_modified++;
371 } else if (dirent->inode == ino) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000372 problem = PR_2_LINK_DOT;
373 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000374 dirent->inode = 0;
375 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000376 goto next;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000377 }
378 }
379 if (!dirent->inode)
380 goto next;
381
Theodore Ts'o3839e651997-04-26 13:21:57 +0000382 /*
383 * Make sure the inode listed is a legal one.
384 */
385 if (((dirent->inode != EXT2_ROOT_INO) &&
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000386 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000387 (dirent->inode > fs->super->s_inodes_count)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000388 problem = PR_2_BAD_INO;
389 } else if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000390 dirent->inode))) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000391 /*
392 * If the inode is unused, offer to clear it.
393 */
394 problem = PR_2_UNUSED_INODE;
395 } else if (ctx->inode_bb_map &&
396 (ext2fs_test_inode_bitmap(ctx->inode_bb_map,
397 dirent->inode))) {
398 /*
399 * If the inode is in a bad block, offer to
400 * clear it.
401 */
402 problem = PR_2_BB_INODE;
403 } else if ((dot_state > 2) &&
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000404 ((dirent->name_len & 0xFF) == 1) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000405 (dirent->name[0] == '.')) {
406 /*
407 * If there's a '.' entry in anything other
408 * than the first directory entry, it's a
409 * duplicate entry that should be removed.
410 */
411 problem = PR_2_DUP_DOT;
412 } else if ((dot_state > 2) &&
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000413 ((dirent->name_len & 0xFF) == 2) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000414 (dirent->name[0] == '.') &&
415 (dirent->name[1] == '.')) {
416 /*
417 * If there's a '..' entry in anything other
418 * than the second directory entry, it's a
419 * duplicate entry that should be removed.
420 */
421 problem = PR_2_DUP_DOT_DOT;
422 } else if ((dot_state > 2) &&
423 (dirent->inode == EXT2_ROOT_INO)) {
424 /*
425 * Don't allow links to the root directory.
426 * We check this specially to make sure we
427 * catch this error case even if the root
428 * directory hasn't been created yet.
429 */
430 problem = PR_2_LINK_ROOT;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000431 }
432
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000433 if (problem) {
434 if (fix_problem(ctx, problem, &cd->pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000435 dirent->inode = 0;
436 dir_modified++;
437 goto next;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000438 } else {
439 ext2fs_unmark_valid(fs);
440 if (problem == PR_2_BAD_INO)
441 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000442 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000443 }
444
445 /*
446 * If the inode was marked as having bad fields in
447 * pass1, process it and offer to fix/clear it.
448 * (We wait until now so that we can display the
449 * pathname to the user.)
450 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000451 if (ctx->inode_bad_map &&
452 ext2fs_test_inode_bitmap(ctx->inode_bad_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000453 dirent->inode)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000454 if (process_bad_inode(ctx, ino, dirent->inode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000455 dirent->inode = 0;
456 dir_modified++;
457 goto next;
458 }
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000459 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000460 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000461 }
462
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000463 if (check_name(ctx, dirent, ino, &cd->pctx))
464 dir_modified++;
465
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000466 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000467 * If this is a directory, then mark its parent in its
468 * dir_info structure. If the parent field is already
469 * filled in, then this directory has more than one
470 * hard link. We assume the first link is correct,
471 * and ask the user if he/she wants to clear this one.
472 */
473 if ((dot_state > 2) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000474 (ext2fs_test_inode_bitmap(ctx->inode_dir_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000475 dirent->inode))) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000476 subdir = e2fsck_get_dir_info(ctx, dirent->inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000477 if (!subdir) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000478 cd->pctx.ino = dirent->inode;
479 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000480 ctx->flags |= E2F_FLAG_ABORT;
481 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000482 }
483 if (subdir->parent) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000484 cd->pctx.ino2 = subdir->parent;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000485 if (fix_problem(ctx, PR_2_LINK_DIR,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000486 &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000487 dirent->inode = 0;
488 dir_modified++;
489 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000490 }
491 cd->pctx.ino2 = 0;
492 } else
493 subdir->parent = ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000494 }
495
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000496 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
497 &links);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000498 if (links > 1)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000499 ctx->fs_links_count++;
500 ctx->fs_total_count++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000501 next:
502 offset += dirent->rec_len;
503 } while (offset < fs->blocksize);
504#if 0
505 printf("\n");
506#endif
507 if (offset != fs->blocksize) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000508 cd->pctx.num = dirent->rec_len - fs->blocksize + offset;
509 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
510 dirent->rec_len = cd->pctx.num;
511 dir_modified++;
512 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000513 }
514 if (dir_modified) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000515 cd->pctx.errcode = ext2fs_write_dir_block(fs, block_nr, buf);
516 if (cd->pctx.errcode) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000517 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
518 &cd->pctx)) {
519 ctx->flags |= E2F_FLAG_ABORT;
520 return DIRENT_ABORT;
521 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000522 }
523 ext2fs_mark_changed(fs);
524 }
525 return 0;
526}
527
528/*
529 * This function is called to deallocate a block, and is an interator
530 * functioned called by deallocate inode via ext2fs_iterate_block().
531 */
532static int deallocate_inode_block(ext2_filsys fs,
533 blk_t *block_nr,
534 int blockcnt,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000535 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000536{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000537 e2fsck_t ctx = (e2fsck_t) priv_data;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000538
Theodore Ts'o3839e651997-04-26 13:21:57 +0000539 if (!*block_nr)
540 return 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000541 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000542 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000543 return 0;
544}
545
546/*
547 * This fuction deallocates an inode
548 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000549static void deallocate_inode(e2fsck_t ctx, ino_t ino,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000550 char* block_buf)
551{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000552 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000553 struct ext2_inode inode;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000554 struct problem_context pctx;
555
556 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000557 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000558 inode.i_links_count = 0;
559 inode.i_dtime = time(0);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000560 e2fsck_write_inode(ctx, ino, &inode, "deallocate_inode");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000561 clear_problem_context(&pctx);
562 pctx.ino = ino;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000563
Theodore Ts'o3839e651997-04-26 13:21:57 +0000564 /*
565 * Fix up the bitmaps...
566 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000567 e2fsck_read_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000568 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
569 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
570 if (ctx->inode_bad_map)
571 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000572 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000573 ext2fs_mark_ib_dirty(fs);
574
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000575 if (!ext2fs_inode_has_valid_blocks(&inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000576 return;
577
578 ext2fs_mark_bb_dirty(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000579 pctx.errcode = ext2fs_block_iterate(fs, ino, 0, block_buf,
580 deallocate_inode_block, ctx);
581 if (pctx.errcode) {
582 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000583 ctx->flags |= E2F_FLAG_ABORT;
584 return;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000585 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000586}
587
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000588static int process_bad_inode(e2fsck_t ctx, ino_t dir, ino_t ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000589{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000590 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000591 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000592 int inode_modified = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000593 unsigned char *frag, *fsize;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000594 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000595 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000596
Theodore Ts'o08b21301997-11-03 19:42:40 +0000597 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000598
599 clear_problem_context(&pctx);
600 pctx.ino = ino;
601 pctx.dir = dir;
602 pctx.inode = &inode;
603
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000604 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
605 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
606 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
Theodore Ts'o08b21301997-11-03 19:42:40 +0000607 !(LINUX_S_ISSOCK(inode.i_mode)))
608 problem = PR_2_BAD_MODE;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000609
610 if (LINUX_S_ISCHR(inode.i_mode)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000611 && !e2fsck_pass1_check_device_inode(&inode))
612 problem = PR_2_BAD_CHAR_DEV;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000613
614 if (LINUX_S_ISBLK(inode.i_mode)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000615 && !e2fsck_pass1_check_device_inode(&inode))
616 problem = PR_2_BAD_BLOCK_DEV;
617
618 if (problem) {
619 if (fix_problem(ctx, problem, &pctx)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000620 deallocate_inode(ctx, ino, 0);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000621 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000622 return 0;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000623 return 1;
624 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000625 problem = 0;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000626 }
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000627
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000628 if (inode.i_faddr &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000629 fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000630 inode.i_faddr = 0;
631 inode_modified++;
632 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000633
634 switch (fs->super->s_creator_os) {
635 case EXT2_OS_LINUX:
636 frag = &inode.osd2.linux2.l_i_frag;
637 fsize = &inode.osd2.linux2.l_i_fsize;
638 break;
639 case EXT2_OS_HURD:
640 frag = &inode.osd2.hurd2.h_i_frag;
641 fsize = &inode.osd2.hurd2.h_i_fsize;
642 break;
643 case EXT2_OS_MASIX:
644 frag = &inode.osd2.masix2.m_i_frag;
645 fsize = &inode.osd2.masix2.m_i_fsize;
646 break;
647 default:
648 frag = fsize = 0;
649 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000650 if (frag && *frag) {
651 pctx.num = *frag;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000652 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000653 *frag = 0;
654 inode_modified++;
655 }
656 pctx.num = 0;
657 }
658 if (fsize && *fsize) {
659 pctx.num = *fsize;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000660 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000661 *fsize = 0;
662 inode_modified++;
663 }
664 pctx.num = 0;
665 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000666
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000667 if (inode.i_file_acl &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000668 fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000669 inode.i_file_acl = 0;
670 inode_modified++;
671 }
672 if (inode.i_dir_acl &&
Theodore Ts'o246501c1998-03-24 16:22:38 +0000673 LINUX_S_ISDIR(inode.i_mode) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000674 fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000675 inode.i_dir_acl = 0;
676 inode_modified++;
677 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000678 if (inode_modified)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000679 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000680 return 0;
681}
682
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000683
684/*
685 * allocate_dir_block --- this function allocates a new directory
686 * block for a particular inode; this is done if a directory has
687 * a "hole" in it, or if a directory has a illegal block number
688 * that was zeroed out and now needs to be replaced.
689 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000690static int allocate_dir_block(e2fsck_t ctx,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000691 struct ext2_db_entry *db,
692 char *buf, struct problem_context *pctx)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000693{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000694 ext2_filsys fs = ctx->fs;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000695 blk_t blk;
696 char *block;
697 struct ext2_inode inode;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000698
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000699 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000700 return 1;
701
702 /*
703 * Read the inode and block bitmaps in; we'll be messing with
704 * them.
705 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000706 e2fsck_read_bitmaps(ctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000707
708 /*
709 * First, find a free block
710 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000711 pctx->errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
712 if (pctx->errcode) {
713 pctx->str = "ext2fs_new_block";
714 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000715 return 1;
716 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000717 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000718 ext2fs_mark_block_bitmap(fs->block_map, blk);
719 ext2fs_mark_bb_dirty(fs);
720
721 /*
722 * Now let's create the actual data block for the inode
723 */
724 if (db->blockcnt)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000725 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000726 else
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000727 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
728 EXT2_ROOT_INO, &block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000729
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000730 if (pctx->errcode) {
731 pctx->str = "ext2fs_new_dir_block";
732 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000733 return 1;
734 }
735
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000736 pctx->errcode = ext2fs_write_dir_block(fs, blk, block);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000737 ext2fs_free_mem((void **) &block);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000738 if (pctx->errcode) {
739 pctx->str = "ext2fs_write_dir_block";
740 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000741 return 1;
742 }
743
744 /*
745 * Update the inode block count
746 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000747 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000748 inode.i_blocks += fs->blocksize / 512;
749 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
750 inode.i_size = (db->blockcnt+1) * fs->blocksize;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000751 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000752
753 /*
754 * Finally, update the block pointers for the inode
755 */
756 db->blk = blk;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000757 pctx->errcode = ext2fs_block_iterate(fs, db->ino, BLOCK_FLAG_HOLE,
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000758 0, update_dir_block, db);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000759 if (pctx->errcode) {
760 pctx->str = "ext2fs_block_iterate";
761 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000762 return 1;
763 }
764
765 return 0;
766}
767
768/*
769 * This is a helper function for allocate_dir_block().
770 */
771static int update_dir_block(ext2_filsys fs,
772 blk_t *block_nr,
773 int blockcnt,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000774 void *priv_data)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000775{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000776 struct ext2_db_entry *db;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000777
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000778 db = (struct ext2_db_entry *) priv_data;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000779 if (db->blockcnt == blockcnt) {
780 *block_nr = db->blk;
781 return BLOCK_CHANGED;
782 }
783 return 0;
784}