blob: 37114e315f1bb8f282ce5b95dd37dcce1e01a331 [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'o3839e651997-04-26 13:21:57 +000051static int check_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000052 struct ext2_db_entry *dir_blocks_info,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +000053 void *priv_data);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000054static int allocate_dir_block(e2fsck_t ctx,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000055 struct ext2_db_entry *dir_blocks_info,
56 char *buf, struct problem_context *pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000057static int update_dir_block(ext2_filsys fs,
58 blk_t *block_nr,
59 int blockcnt,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +000060 void *priv_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +000061
Theodore Ts'o21c84b71997-04-29 16:15:03 +000062struct check_dir_struct {
63 char *buf;
64 struct problem_context pctx;
Theodore Ts'of8188ff1997-11-14 05:23:04 +000065 int count, max;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000066 e2fsck_t ctx;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000067};
68
Theodore Ts'o08b21301997-11-03 19:42:40 +000069void e2fsck_pass2(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +000070{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000071 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +000072 char *buf;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000073#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000074 struct resource_track rtrack;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000075#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +000076 struct dir_info *dir;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000077 struct check_dir_struct cd;
78
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000079#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000080 init_resource_track(&rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000081#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000082
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000083 clear_problem_context(&cd.pctx);
84
Theodore Ts'o3839e651997-04-26 13:21:57 +000085#ifdef MTRACE
86 mtrace_print("Pass 2");
87#endif
88
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000089 if (!(ctx->options & E2F_OPT_PREEN))
90 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
91
92 cd.pctx.errcode = ext2fs_create_icount2(fs, EXT2_ICOUNT_OPT_INCREMENT,
93 0, ctx->inode_link_info,
94 &ctx->inode_count);
95 if (cd.pctx.errcode) {
96 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +000097 ctx->flags |= E2F_FLAG_ABORT;
98 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000099 }
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000100 buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize,
101 "directory scan buffer");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000102
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000103 /*
104 * Set up the parent pointer for the root directory, if
105 * present. (If the root directory is not present, we will
106 * create it in pass 3.)
107 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000108 dir = e2fsck_get_dir_info(ctx, EXT2_ROOT_INO);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000109 if (dir)
110 dir->parent = EXT2_ROOT_INO;
111
112 cd.buf = buf;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000113 cd.ctx = ctx;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000114 cd.count = 1;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000115 cd.max = ext2fs_dblist_count(fs->dblist);
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000116
117 if (ctx->progress)
118 (void) (ctx->progress)(ctx, 2, 0, cd.max);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000119
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000120 cd.pctx.errcode = ext2fs_dblist_iterate(fs->dblist, check_dir_block,
121 &cd);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000122 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000123 return;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000124 if (cd.pctx.errcode) {
125 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000126 ctx->flags |= E2F_FLAG_ABORT;
127 return;
Theodore Ts'o7ac02a51997-06-11 18:32:35 +0000128 }
129
Theodore Ts'o08b21301997-11-03 19:42:40 +0000130 ext2fs_free_mem((void **) &buf);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000131 ext2fs_free_dblist(fs->dblist);
132
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000133 if (ctx->inode_bad_map) {
134 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
135 ctx->inode_bad_map = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000136 }
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000137#ifdef RESOURCE_TRACK
Theodore Ts'o5596def1999-07-19 15:27:37 +0000138 if (ctx->options & E2F_OPT_TIME2) {
139 e2fsck_clear_progbar(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000140 print_resource_track("Pass 2", &rtrack);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000141 }
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000142#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000143}
144
145/*
146 * Make sure the first entry in the directory is '.', and that the
147 * directory entry is sane.
148 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000149static int check_dot(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000150 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000151 ino_t ino, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000152{
153 struct ext2_dir_entry *nextdir;
154 int status = 0;
155 int created = 0;
156 int new_len;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000157 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000158
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000159 if (!dirent->inode)
160 problem = PR_2_MISSING_DOT;
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000161 else if (((dirent->name_len & 0xFF) != 1) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000162 (dirent->name[0] != '.'))
163 problem = PR_2_1ST_NOT_DOT;
164 else if (dirent->name[1] != '\0')
165 problem = PR_2_DOT_NULL_TERM;
166
167 if (problem) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000168 if (fix_problem(ctx, problem, pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000169 if (dirent->rec_len < 12)
170 dirent->rec_len = 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000171 dirent->inode = ino;
172 dirent->name_len = 1;
173 dirent->name[0] = '.';
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000174 dirent->name[1] = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000175 status = 1;
176 created = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000177 }
178 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000179 if (dirent->inode != ino) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000180 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000181 dirent->inode = ino;
182 status = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000183 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000184 }
185 if (dirent->rec_len > 12) {
186 new_len = dirent->rec_len - 12;
187 if (new_len > 12) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000188 if (created ||
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000189 fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000190 nextdir = (struct ext2_dir_entry *)
191 ((char *) dirent + 12);
192 dirent->rec_len = 12;
193 nextdir->rec_len = new_len;
194 nextdir->inode = 0;
195 nextdir->name_len = 0;
196 status = 1;
197 }
198 }
199 }
200 return status;
201}
202
203/*
204 * Make sure the second entry in the directory is '..', and that the
205 * directory entry is sane. We do not check the inode number of '..'
206 * here; this gets done in pass 3.
207 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000208static int check_dotdot(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000209 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000210 struct dir_info *dir, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000211{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000212 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000213
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000214 if (!dirent->inode)
215 problem = PR_2_MISSING_DOT_DOT;
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000216 else if (((dirent->name_len & 0xFF) != 2) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000217 (dirent->name[0] != '.') ||
218 (dirent->name[1] != '.'))
219 problem = PR_2_2ND_NOT_DOT_DOT;
220 else if (dirent->name[2] != '\0')
221 problem = PR_2_DOT_DOT_NULL_TERM;
222
223 if (problem) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000224 if (fix_problem(ctx, problem, pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000225 if (dirent->rec_len < 12)
226 dirent->rec_len = 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000227 /*
228 * Note: we don't have the parent inode just
229 * yet, so we will fill it in with the root
230 * inode. This will get fixed in pass 3.
231 */
232 dirent->inode = EXT2_ROOT_INO;
233 dirent->name_len = 2;
234 dirent->name[0] = '.';
235 dirent->name[1] = '.';
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000236 dirent->name[2] = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000237 return 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000238 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000239 return 0;
240 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000241 dir->dotdot = dirent->inode;
242 return 0;
243}
244
245/*
246 * Check to make sure a directory entry doesn't contain any illegal
247 * characters.
248 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000249static int check_name(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000250 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000251 ino_t dir_ino, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000252{
253 int i;
254 int fixup = -1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000255 int ret = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000256
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000257 for ( i = 0; i < (dirent->name_len & 0xFF); i++) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000258 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
259 if (fixup < 0) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000260 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000261 }
262 if (fixup) {
263 dirent->name[i] = '.';
264 ret = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000265 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000266 }
267 }
268 return ret;
269}
270
271static int check_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000272 struct ext2_db_entry *db,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000273 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000274{
275 struct dir_info *subdir, *dir;
276 struct ext2_dir_entry *dirent;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000277 int offset = 0;
278 int dir_modified = 0;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000279 int dot_state;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000280 blk_t block_nr = db->blk;
281 ino_t ino = db->ino;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000282 __u16 links;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000283 struct check_dir_struct *cd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000284 char *buf;
285 e2fsck_t ctx;
286 int problem;
287
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000288 cd = (struct check_dir_struct *) priv_data;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000289 buf = cd->buf;
290 ctx = cd->ctx;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000291
292 if (ctx->progress)
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000293 if ((ctx->progress)(ctx, 2, cd->count++, cd->max))
294 return DIRENT_ABORT;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000295
Theodore Ts'o3839e651997-04-26 13:21:57 +0000296 /*
297 * Make sure the inode is still in use (could have been
298 * deleted in the duplicate/bad blocks pass.
299 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000300 if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, ino)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000301 return 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000302
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000303 cd->pctx.ino = ino;
304 cd->pctx.blk = block_nr;
305 cd->pctx.blkcount = db->blockcnt;
306 cd->pctx.ino2 = 0;
307 cd->pctx.dirent = 0;
308 cd->pctx.num = 0;
309
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000310 if (db->blk == 0) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000311 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000312 return 0;
313 block_nr = db->blk;
314 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000315
316 if (db->blockcnt)
317 dot_state = 2;
318 else
319 dot_state = 0;
320
321#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000322 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000323 db->blockcnt, ino);
324#endif
325
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000326 cd->pctx.errcode = ext2fs_read_dir_block(fs, block_nr, buf);
327 if (cd->pctx.errcode) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000328 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
329 ctx->flags |= E2F_FLAG_ABORT;
330 return DIRENT_ABORT;
331 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000332 memset(buf, 0, fs->blocksize);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000333 }
334
335 do {
336 dot_state++;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000337 problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000338 dirent = (struct ext2_dir_entry *) (buf + offset);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000339 cd->pctx.dirent = dirent;
340 cd->pctx.num = offset;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000341 if (((offset + dirent->rec_len) > fs->blocksize) ||
342 (dirent->rec_len < 8) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000343 ((dirent->rec_len % 4) != 0) ||
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000344 (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000345 if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000346 dirent->rec_len = fs->blocksize - offset;
347 dirent->name_len = 0;
348 dirent->inode = 0;
349 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000350 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000351 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000352 }
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000353 if ((dirent->name_len & 0xFF) > EXT2_NAME_LEN) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000354 if (fix_problem(ctx, PR_2_FILENAME_LONG, &cd->pctx)) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000355 dirent->name_len = EXT2_NAME_LEN;
356 dir_modified++;
357 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000358 }
359
Theodore Ts'o3839e651997-04-26 13:21:57 +0000360 if (dot_state == 1) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000361 if (check_dot(ctx, dirent, ino, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000362 dir_modified++;
363 } else if (dot_state == 2) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000364 dir = e2fsck_get_dir_info(ctx, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000365 if (!dir) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000366 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000367 ctx->flags |= E2F_FLAG_ABORT;
368 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000369 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000370 if (check_dotdot(ctx, dirent, dir, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000371 dir_modified++;
372 } else if (dirent->inode == ino) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000373 problem = PR_2_LINK_DOT;
374 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000375 dirent->inode = 0;
376 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000377 goto next;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000378 }
379 }
380 if (!dirent->inode)
381 goto next;
382
Theodore Ts'o3839e651997-04-26 13:21:57 +0000383 /*
384 * Make sure the inode listed is a legal one.
385 */
386 if (((dirent->inode != EXT2_ROOT_INO) &&
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000387 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000388 (dirent->inode > fs->super->s_inodes_count)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000389 problem = PR_2_BAD_INO;
390 } else if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000391 dirent->inode))) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000392 /*
393 * If the inode is unused, offer to clear it.
394 */
395 problem = PR_2_UNUSED_INODE;
396 } else if (ctx->inode_bb_map &&
397 (ext2fs_test_inode_bitmap(ctx->inode_bb_map,
398 dirent->inode))) {
399 /*
400 * If the inode is in a bad block, offer to
401 * clear it.
402 */
403 problem = PR_2_BB_INODE;
404 } else if ((dot_state > 2) &&
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000405 ((dirent->name_len & 0xFF) == 1) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000406 (dirent->name[0] == '.')) {
407 /*
408 * If there's a '.' entry in anything other
409 * than the first directory entry, it's a
410 * duplicate entry that should be removed.
411 */
412 problem = PR_2_DUP_DOT;
413 } else if ((dot_state > 2) &&
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000414 ((dirent->name_len & 0xFF) == 2) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000415 (dirent->name[0] == '.') &&
416 (dirent->name[1] == '.')) {
417 /*
418 * If there's a '..' entry in anything other
419 * than the second directory entry, it's a
420 * duplicate entry that should be removed.
421 */
422 problem = PR_2_DUP_DOT_DOT;
423 } else if ((dot_state > 2) &&
424 (dirent->inode == EXT2_ROOT_INO)) {
425 /*
426 * Don't allow links to the root directory.
427 * We check this specially to make sure we
428 * catch this error case even if the root
429 * directory hasn't been created yet.
430 */
431 problem = PR_2_LINK_ROOT;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000432 }
433
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000434 if (problem) {
435 if (fix_problem(ctx, problem, &cd->pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000436 dirent->inode = 0;
437 dir_modified++;
438 goto next;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000439 } else {
440 ext2fs_unmark_valid(fs);
441 if (problem == PR_2_BAD_INO)
442 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000443 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000444 }
445
446 /*
447 * If the inode was marked as having bad fields in
448 * pass1, process it and offer to fix/clear it.
449 * (We wait until now so that we can display the
450 * pathname to the user.)
451 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000452 if (ctx->inode_bad_map &&
453 ext2fs_test_inode_bitmap(ctx->inode_bad_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000454 dirent->inode)) {
Theodore Ts'oe72a9ba1999-06-25 15:40:18 +0000455 if (e2fsck_process_bad_inode(ctx, ino,
456 dirent->inode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000457 dirent->inode = 0;
458 dir_modified++;
459 goto next;
460 }
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000461 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000462 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000463 }
464
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000465 if (check_name(ctx, dirent, ino, &cd->pctx))
466 dir_modified++;
467
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000468 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000469 * If this is a directory, then mark its parent in its
470 * dir_info structure. If the parent field is already
471 * filled in, then this directory has more than one
472 * hard link. We assume the first link is correct,
473 * and ask the user if he/she wants to clear this one.
474 */
475 if ((dot_state > 2) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000476 (ext2fs_test_inode_bitmap(ctx->inode_dir_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000477 dirent->inode))) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000478 subdir = e2fsck_get_dir_info(ctx, dirent->inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000479 if (!subdir) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000480 cd->pctx.ino = dirent->inode;
481 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000482 ctx->flags |= E2F_FLAG_ABORT;
483 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000484 }
485 if (subdir->parent) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000486 cd->pctx.ino2 = subdir->parent;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000487 if (fix_problem(ctx, PR_2_LINK_DIR,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000488 &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000489 dirent->inode = 0;
490 dir_modified++;
491 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000492 }
493 cd->pctx.ino2 = 0;
494 } else
495 subdir->parent = ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000496 }
497
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000498 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
499 &links);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000500 if (links > 1)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000501 ctx->fs_links_count++;
502 ctx->fs_total_count++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000503 next:
504 offset += dirent->rec_len;
505 } while (offset < fs->blocksize);
506#if 0
507 printf("\n");
508#endif
509 if (offset != fs->blocksize) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000510 cd->pctx.num = dirent->rec_len - fs->blocksize + offset;
511 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
512 dirent->rec_len = cd->pctx.num;
513 dir_modified++;
514 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000515 }
516 if (dir_modified) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000517 cd->pctx.errcode = ext2fs_write_dir_block(fs, block_nr, buf);
518 if (cd->pctx.errcode) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000519 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
520 &cd->pctx)) {
521 ctx->flags |= E2F_FLAG_ABORT;
522 return DIRENT_ABORT;
523 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000524 }
525 ext2fs_mark_changed(fs);
526 }
527 return 0;
528}
529
530/*
531 * This function is called to deallocate a block, and is an interator
532 * functioned called by deallocate inode via ext2fs_iterate_block().
533 */
534static int deallocate_inode_block(ext2_filsys fs,
535 blk_t *block_nr,
536 int blockcnt,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000537 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000538{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000539 e2fsck_t ctx = (e2fsck_t) priv_data;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000540
Theodore Ts'o3839e651997-04-26 13:21:57 +0000541 if (!*block_nr)
542 return 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000543 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000544 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000545 return 0;
546}
547
548/*
549 * This fuction deallocates an inode
550 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000551static void deallocate_inode(e2fsck_t ctx, ino_t ino,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000552 char* block_buf)
553{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000554 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000555 struct ext2_inode inode;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000556 struct problem_context pctx;
557
558 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000559 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000560 inode.i_links_count = 0;
561 inode.i_dtime = time(0);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000562 e2fsck_write_inode(ctx, ino, &inode, "deallocate_inode");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000563 clear_problem_context(&pctx);
564 pctx.ino = ino;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000565
Theodore Ts'o3839e651997-04-26 13:21:57 +0000566 /*
567 * Fix up the bitmaps...
568 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000569 e2fsck_read_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000570 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
571 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
572 if (ctx->inode_bad_map)
573 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000574 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000575 ext2fs_mark_ib_dirty(fs);
576
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000577 if (!ext2fs_inode_has_valid_blocks(&inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000578 return;
579
580 ext2fs_mark_bb_dirty(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000581 pctx.errcode = ext2fs_block_iterate(fs, ino, 0, block_buf,
582 deallocate_inode_block, ctx);
583 if (pctx.errcode) {
584 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000585 ctx->flags |= E2F_FLAG_ABORT;
586 return;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000587 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000588}
589
Theodore Ts'oe72a9ba1999-06-25 15:40:18 +0000590extern int e2fsck_process_bad_inode(e2fsck_t ctx, ino_t dir, ino_t ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000591{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000592 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000593 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000594 int inode_modified = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000595 unsigned char *frag, *fsize;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000596 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000597 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000598
Theodore Ts'o08b21301997-11-03 19:42:40 +0000599 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000600
601 clear_problem_context(&pctx);
602 pctx.ino = ino;
603 pctx.dir = dir;
604 pctx.inode = &inode;
605
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000606 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
607 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
608 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
Theodore Ts'o08b21301997-11-03 19:42:40 +0000609 !(LINUX_S_ISSOCK(inode.i_mode)))
610 problem = PR_2_BAD_MODE;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000611
612 if (LINUX_S_ISCHR(inode.i_mode)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000613 && !e2fsck_pass1_check_device_inode(&inode))
614 problem = PR_2_BAD_CHAR_DEV;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000615
616 if (LINUX_S_ISBLK(inode.i_mode)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000617 && !e2fsck_pass1_check_device_inode(&inode))
618 problem = PR_2_BAD_BLOCK_DEV;
619
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000620 if (LINUX_S_ISFIFO(inode.i_mode)
621 && !e2fsck_pass1_check_device_inode(&inode))
622 problem = PR_2_BAD_FIFO;
623
624 if (LINUX_S_ISSOCK(inode.i_mode)
625 && !e2fsck_pass1_check_device_inode(&inode))
626 problem = PR_2_BAD_SOCKET;
627
Theodore Ts'o08b21301997-11-03 19:42:40 +0000628 if (problem) {
629 if (fix_problem(ctx, problem, &pctx)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000630 deallocate_inode(ctx, ino, 0);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000631 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000632 return 0;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000633 return 1;
634 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000635 problem = 0;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000636 }
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000637
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000638 if (inode.i_faddr &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000639 fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000640 inode.i_faddr = 0;
641 inode_modified++;
642 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000643
644 switch (fs->super->s_creator_os) {
645 case EXT2_OS_LINUX:
646 frag = &inode.osd2.linux2.l_i_frag;
647 fsize = &inode.osd2.linux2.l_i_fsize;
648 break;
649 case EXT2_OS_HURD:
650 frag = &inode.osd2.hurd2.h_i_frag;
651 fsize = &inode.osd2.hurd2.h_i_fsize;
652 break;
653 case EXT2_OS_MASIX:
654 frag = &inode.osd2.masix2.m_i_frag;
655 fsize = &inode.osd2.masix2.m_i_fsize;
656 break;
657 default:
658 frag = fsize = 0;
659 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000660 if (frag && *frag) {
661 pctx.num = *frag;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000662 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000663 *frag = 0;
664 inode_modified++;
665 }
666 pctx.num = 0;
667 }
668 if (fsize && *fsize) {
669 pctx.num = *fsize;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000670 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000671 *fsize = 0;
672 inode_modified++;
673 }
674 pctx.num = 0;
675 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000676
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000677 if (inode.i_file_acl &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000678 fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000679 inode.i_file_acl = 0;
680 inode_modified++;
681 }
682 if (inode.i_dir_acl &&
Theodore Ts'o246501c1998-03-24 16:22:38 +0000683 LINUX_S_ISDIR(inode.i_mode) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000684 fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000685 inode.i_dir_acl = 0;
686 inode_modified++;
687 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000688 if (inode_modified)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000689 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000690 return 0;
691}
692
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000693
694/*
695 * allocate_dir_block --- this function allocates a new directory
696 * block for a particular inode; this is done if a directory has
697 * a "hole" in it, or if a directory has a illegal block number
698 * that was zeroed out and now needs to be replaced.
699 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000700static int allocate_dir_block(e2fsck_t ctx,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000701 struct ext2_db_entry *db,
702 char *buf, struct problem_context *pctx)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000703{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000704 ext2_filsys fs = ctx->fs;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000705 blk_t blk;
706 char *block;
707 struct ext2_inode inode;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000708
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000709 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000710 return 1;
711
712 /*
713 * Read the inode and block bitmaps in; we'll be messing with
714 * them.
715 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000716 e2fsck_read_bitmaps(ctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000717
718 /*
719 * First, find a free block
720 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000721 pctx->errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
722 if (pctx->errcode) {
723 pctx->str = "ext2fs_new_block";
724 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000725 return 1;
726 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000727 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000728 ext2fs_mark_block_bitmap(fs->block_map, blk);
729 ext2fs_mark_bb_dirty(fs);
730
731 /*
732 * Now let's create the actual data block for the inode
733 */
734 if (db->blockcnt)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000735 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000736 else
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000737 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
738 EXT2_ROOT_INO, &block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000739
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000740 if (pctx->errcode) {
741 pctx->str = "ext2fs_new_dir_block";
742 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000743 return 1;
744 }
745
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000746 pctx->errcode = ext2fs_write_dir_block(fs, blk, block);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000747 ext2fs_free_mem((void **) &block);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000748 if (pctx->errcode) {
749 pctx->str = "ext2fs_write_dir_block";
750 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000751 return 1;
752 }
753
754 /*
755 * Update the inode block count
756 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000757 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000758 inode.i_blocks += fs->blocksize / 512;
759 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
760 inode.i_size = (db->blockcnt+1) * fs->blocksize;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000761 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000762
763 /*
764 * Finally, update the block pointers for the inode
765 */
766 db->blk = blk;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000767 pctx->errcode = ext2fs_block_iterate(fs, db->ino, BLOCK_FLAG_HOLE,
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000768 0, update_dir_block, db);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000769 if (pctx->errcode) {
770 pctx->str = "ext2fs_block_iterate";
771 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000772 return 1;
773 }
774
775 return 0;
776}
777
778/*
779 * This is a helper function for allocate_dir_block().
780 */
781static int update_dir_block(ext2_filsys fs,
782 blk_t *block_nr,
783 int blockcnt,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000784 void *priv_data)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000785{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000786 struct ext2_db_entry *db;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000787
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000788 db = (struct ext2_db_entry *) priv_data;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000789 if (db->blockcnt == blockcnt) {
790 *block_nr = db->blk;
791 return BLOCK_CHANGED;
792 }
793 return 0;
794}