1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker * pass1.c -- pass #1 of e2fsck: sequential scan of the inode table
3*6a54128fSAndroid Build Coastguard Worker *
4*6a54128fSAndroid Build Coastguard Worker * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5*6a54128fSAndroid Build Coastguard Worker *
6*6a54128fSAndroid Build Coastguard Worker * %Begin-Header%
7*6a54128fSAndroid Build Coastguard Worker * This file may be redistributed under the terms of the GNU Public
8*6a54128fSAndroid Build Coastguard Worker * License.
9*6a54128fSAndroid Build Coastguard Worker * %End-Header%
10*6a54128fSAndroid Build Coastguard Worker *
11*6a54128fSAndroid Build Coastguard Worker * Pass 1 of e2fsck iterates over all the inodes in the filesystems,
12*6a54128fSAndroid Build Coastguard Worker * and applies the following tests to each inode:
13*6a54128fSAndroid Build Coastguard Worker *
14*6a54128fSAndroid Build Coastguard Worker * - The mode field of the inode must be legal.
15*6a54128fSAndroid Build Coastguard Worker * - The size and block count fields of the inode are correct.
16*6a54128fSAndroid Build Coastguard Worker * - A data block must not be used by another inode
17*6a54128fSAndroid Build Coastguard Worker *
18*6a54128fSAndroid Build Coastguard Worker * Pass 1 also gathers the collects the following information:
19*6a54128fSAndroid Build Coastguard Worker *
20*6a54128fSAndroid Build Coastguard Worker * - A bitmap of which inodes are in use. (inode_used_map)
21*6a54128fSAndroid Build Coastguard Worker * - A bitmap of which inodes are directories. (inode_dir_map)
22*6a54128fSAndroid Build Coastguard Worker * - A bitmap of which inodes are regular files. (inode_reg_map)
23*6a54128fSAndroid Build Coastguard Worker * - A bitmap of which inodes have bad fields. (inode_bad_map)
24*6a54128fSAndroid Build Coastguard Worker * - A bitmap of which inodes are in bad blocks. (inode_bb_map)
25*6a54128fSAndroid Build Coastguard Worker * - A bitmap of which inodes are imagic inodes. (inode_imagic_map)
26*6a54128fSAndroid Build Coastguard Worker * - A bitmap of which inodes are casefolded. (inode_casefold_map)
27*6a54128fSAndroid Build Coastguard Worker * - A bitmap of which blocks are in use. (block_found_map)
28*6a54128fSAndroid Build Coastguard Worker * - A bitmap of which blocks are in use by two inodes (block_dup_map)
29*6a54128fSAndroid Build Coastguard Worker * - The data blocks of the directory inodes. (dir_map)
30*6a54128fSAndroid Build Coastguard Worker * - Ref counts for ea_inodes. (ea_inode_refs)
31*6a54128fSAndroid Build Coastguard Worker * - The encryption policy ID of each encrypted inode. (encrypted_files)
32*6a54128fSAndroid Build Coastguard Worker *
33*6a54128fSAndroid Build Coastguard Worker * Pass 1 is designed to stash away enough information so that the
34*6a54128fSAndroid Build Coastguard Worker * other passes should not need to read in the inode information
35*6a54128fSAndroid Build Coastguard Worker * during the normal course of a filesystem check. (Although if an
36*6a54128fSAndroid Build Coastguard Worker * inconsistency is detected, other passes may need to read in an
37*6a54128fSAndroid Build Coastguard Worker * inode to fix it.)
38*6a54128fSAndroid Build Coastguard Worker *
39*6a54128fSAndroid Build Coastguard Worker * Note that pass 1B will be invoked if there are any duplicate blocks
40*6a54128fSAndroid Build Coastguard Worker * found.
41*6a54128fSAndroid Build Coastguard Worker */
42*6a54128fSAndroid Build Coastguard Worker
43*6a54128fSAndroid Build Coastguard Worker #define _GNU_SOURCE 1 /* get strnlen() */
44*6a54128fSAndroid Build Coastguard Worker #include "config.h"
45*6a54128fSAndroid Build Coastguard Worker #include <string.h>
46*6a54128fSAndroid Build Coastguard Worker #include <time.h>
47*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_ERRNO_H
48*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
49*6a54128fSAndroid Build Coastguard Worker #endif
50*6a54128fSAndroid Build Coastguard Worker
51*6a54128fSAndroid Build Coastguard Worker #include "e2fsck.h"
52*6a54128fSAndroid Build Coastguard Worker #include <ext2fs/ext2_ext_attr.h>
53*6a54128fSAndroid Build Coastguard Worker #include <e2p/e2p.h>
54*6a54128fSAndroid Build Coastguard Worker
55*6a54128fSAndroid Build Coastguard Worker #include "problem.h"
56*6a54128fSAndroid Build Coastguard Worker
57*6a54128fSAndroid Build Coastguard Worker #ifdef NO_INLINE_FUNCS
58*6a54128fSAndroid Build Coastguard Worker #define _INLINE_
59*6a54128fSAndroid Build Coastguard Worker #else
60*6a54128fSAndroid Build Coastguard Worker #define _INLINE_ inline
61*6a54128fSAndroid Build Coastguard Worker #endif
62*6a54128fSAndroid Build Coastguard Worker
63*6a54128fSAndroid Build Coastguard Worker #undef DEBUG
64*6a54128fSAndroid Build Coastguard Worker
65*6a54128fSAndroid Build Coastguard Worker struct ea_quota {
66*6a54128fSAndroid Build Coastguard Worker blk64_t blocks;
67*6a54128fSAndroid Build Coastguard Worker __u64 inodes;
68*6a54128fSAndroid Build Coastguard Worker };
69*6a54128fSAndroid Build Coastguard Worker
70*6a54128fSAndroid Build Coastguard Worker static int process_block(ext2_filsys fs, blk64_t *blocknr,
71*6a54128fSAndroid Build Coastguard Worker e2_blkcnt_t blockcnt, blk64_t ref_blk,
72*6a54128fSAndroid Build Coastguard Worker int ref_offset, void *priv_data);
73*6a54128fSAndroid Build Coastguard Worker static int process_bad_block(ext2_filsys fs, blk64_t *block_nr,
74*6a54128fSAndroid Build Coastguard Worker e2_blkcnt_t blockcnt, blk64_t ref_blk,
75*6a54128fSAndroid Build Coastguard Worker int ref_offset, void *priv_data);
76*6a54128fSAndroid Build Coastguard Worker static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
77*6a54128fSAndroid Build Coastguard Worker char *block_buf,
78*6a54128fSAndroid Build Coastguard Worker const struct ea_quota *ea_ibody_quota);
79*6a54128fSAndroid Build Coastguard Worker static void mark_table_blocks(e2fsck_t ctx);
80*6a54128fSAndroid Build Coastguard Worker static void alloc_bb_map(e2fsck_t ctx);
81*6a54128fSAndroid Build Coastguard Worker static void alloc_imagic_map(e2fsck_t ctx);
82*6a54128fSAndroid Build Coastguard Worker static void mark_inode_bad(e2fsck_t ctx, ext2_ino_t ino);
83*6a54128fSAndroid Build Coastguard Worker static void add_casefolded_dir(e2fsck_t ctx, ext2_ino_t ino);
84*6a54128fSAndroid Build Coastguard Worker static void handle_fs_bad_blocks(e2fsck_t ctx);
85*6a54128fSAndroid Build Coastguard Worker static void process_inodes(e2fsck_t ctx, char *block_buf);
86*6a54128fSAndroid Build Coastguard Worker static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
87*6a54128fSAndroid Build Coastguard Worker static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
88*6a54128fSAndroid Build Coastguard Worker dgrp_t group, void * priv_data);
89*6a54128fSAndroid Build Coastguard Worker static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
90*6a54128fSAndroid Build Coastguard Worker char *block_buf, int adjust_sign);
91*6a54128fSAndroid Build Coastguard Worker /* static char *describe_illegal_block(ext2_filsys fs, blk64_t block); */
92*6a54128fSAndroid Build Coastguard Worker
93*6a54128fSAndroid Build Coastguard Worker struct process_block_struct {
94*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino;
95*6a54128fSAndroid Build Coastguard Worker unsigned is_dir:1, is_reg:1, clear:1, suppress:1,
96*6a54128fSAndroid Build Coastguard Worker fragmented:1, compressed:1, bbcheck:1,
97*6a54128fSAndroid Build Coastguard Worker inode_modified:1;
98*6a54128fSAndroid Build Coastguard Worker blk64_t num_blocks;
99*6a54128fSAndroid Build Coastguard Worker blk64_t max_blocks;
100*6a54128fSAndroid Build Coastguard Worker blk64_t last_block;
101*6a54128fSAndroid Build Coastguard Worker e2_blkcnt_t last_init_lblock;
102*6a54128fSAndroid Build Coastguard Worker e2_blkcnt_t last_db_block;
103*6a54128fSAndroid Build Coastguard Worker int num_illegal_blocks;
104*6a54128fSAndroid Build Coastguard Worker blk64_t previous_block;
105*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode;
106*6a54128fSAndroid Build Coastguard Worker struct problem_context *pctx;
107*6a54128fSAndroid Build Coastguard Worker ext2fs_block_bitmap fs_meta_blocks;
108*6a54128fSAndroid Build Coastguard Worker e2fsck_t ctx;
109*6a54128fSAndroid Build Coastguard Worker blk64_t next_lblock;
110*6a54128fSAndroid Build Coastguard Worker struct extent_tree_info eti;
111*6a54128fSAndroid Build Coastguard Worker };
112*6a54128fSAndroid Build Coastguard Worker
113*6a54128fSAndroid Build Coastguard Worker struct process_inode_block {
114*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino;
115*6a54128fSAndroid Build Coastguard Worker struct ea_quota ea_ibody_quota;
116*6a54128fSAndroid Build Coastguard Worker struct ext2_inode_large inode;
117*6a54128fSAndroid Build Coastguard Worker };
118*6a54128fSAndroid Build Coastguard Worker
119*6a54128fSAndroid Build Coastguard Worker struct scan_callback_struct {
120*6a54128fSAndroid Build Coastguard Worker e2fsck_t ctx;
121*6a54128fSAndroid Build Coastguard Worker char *block_buf;
122*6a54128fSAndroid Build Coastguard Worker };
123*6a54128fSAndroid Build Coastguard Worker
124*6a54128fSAndroid Build Coastguard Worker /*
125*6a54128fSAndroid Build Coastguard Worker * For the inodes to process list.
126*6a54128fSAndroid Build Coastguard Worker */
127*6a54128fSAndroid Build Coastguard Worker static struct process_inode_block *inodes_to_process;
128*6a54128fSAndroid Build Coastguard Worker static int process_inode_count;
129*6a54128fSAndroid Build Coastguard Worker
130*6a54128fSAndroid Build Coastguard Worker static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
131*6a54128fSAndroid Build Coastguard Worker EXT2_MIN_BLOCK_LOG_SIZE + 1];
132*6a54128fSAndroid Build Coastguard Worker
133*6a54128fSAndroid Build Coastguard Worker /*
134*6a54128fSAndroid Build Coastguard Worker * Check to make sure a device inode is real. Returns 1 if the device
135*6a54128fSAndroid Build Coastguard Worker * checks out, 0 if not.
136*6a54128fSAndroid Build Coastguard Worker *
137*6a54128fSAndroid Build Coastguard Worker * Note: this routine is now also used to check FIFO's and Sockets,
138*6a54128fSAndroid Build Coastguard Worker * since they have the same requirement; the i_block fields should be
139*6a54128fSAndroid Build Coastguard Worker * zero.
140*6a54128fSAndroid Build Coastguard Worker */
e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR ((unused)),struct ext2_inode * inode)141*6a54128fSAndroid Build Coastguard Worker int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
142*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode)
143*6a54128fSAndroid Build Coastguard Worker {
144*6a54128fSAndroid Build Coastguard Worker int i;
145*6a54128fSAndroid Build Coastguard Worker
146*6a54128fSAndroid Build Coastguard Worker /*
147*6a54128fSAndroid Build Coastguard Worker * If the index or extents flag is set, then this is a bogus
148*6a54128fSAndroid Build Coastguard Worker * device/fifo/socket
149*6a54128fSAndroid Build Coastguard Worker */
150*6a54128fSAndroid Build Coastguard Worker if (inode->i_flags & (EXT2_INDEX_FL | EXT4_EXTENTS_FL))
151*6a54128fSAndroid Build Coastguard Worker return 0;
152*6a54128fSAndroid Build Coastguard Worker
153*6a54128fSAndroid Build Coastguard Worker /*
154*6a54128fSAndroid Build Coastguard Worker * We should be able to do the test below all the time, but
155*6a54128fSAndroid Build Coastguard Worker * because the kernel doesn't forcibly clear the device
156*6a54128fSAndroid Build Coastguard Worker * inode's additional i_block fields, there are some rare
157*6a54128fSAndroid Build Coastguard Worker * occasions when a legitimate device inode will have non-zero
158*6a54128fSAndroid Build Coastguard Worker * additional i_block fields. So for now, we only complain
159*6a54128fSAndroid Build Coastguard Worker * when the immutable flag is set, which should never happen
160*6a54128fSAndroid Build Coastguard Worker * for devices. (And that's when the problem is caused, since
161*6a54128fSAndroid Build Coastguard Worker * you can't set or clear immutable flags for devices.) Once
162*6a54128fSAndroid Build Coastguard Worker * the kernel has been fixed we can change this...
163*6a54128fSAndroid Build Coastguard Worker */
164*6a54128fSAndroid Build Coastguard Worker if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
165*6a54128fSAndroid Build Coastguard Worker for (i=4; i < EXT2_N_BLOCKS; i++)
166*6a54128fSAndroid Build Coastguard Worker if (inode->i_block[i])
167*6a54128fSAndroid Build Coastguard Worker return 0;
168*6a54128fSAndroid Build Coastguard Worker }
169*6a54128fSAndroid Build Coastguard Worker return 1;
170*6a54128fSAndroid Build Coastguard Worker }
171*6a54128fSAndroid Build Coastguard Worker
172*6a54128fSAndroid Build Coastguard Worker /*
173*6a54128fSAndroid Build Coastguard Worker * Check to make sure a symlink inode is real. Returns 1 if the symlink
174*6a54128fSAndroid Build Coastguard Worker * checks out, 0 if not.
175*6a54128fSAndroid Build Coastguard Worker */
e2fsck_pass1_check_symlink(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,char * buf)176*6a54128fSAndroid Build Coastguard Worker int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
177*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode, char *buf)
178*6a54128fSAndroid Build Coastguard Worker {
179*6a54128fSAndroid Build Coastguard Worker unsigned int buflen;
180*6a54128fSAndroid Build Coastguard Worker unsigned int len;
181*6a54128fSAndroid Build Coastguard Worker
182*6a54128fSAndroid Build Coastguard Worker if ((inode->i_size_high || inode->i_size == 0) ||
183*6a54128fSAndroid Build Coastguard Worker (inode->i_flags & EXT2_INDEX_FL))
184*6a54128fSAndroid Build Coastguard Worker return 0;
185*6a54128fSAndroid Build Coastguard Worker
186*6a54128fSAndroid Build Coastguard Worker if (inode->i_flags & EXT4_INLINE_DATA_FL) {
187*6a54128fSAndroid Build Coastguard Worker size_t inline_size;
188*6a54128fSAndroid Build Coastguard Worker
189*6a54128fSAndroid Build Coastguard Worker if (inode->i_flags & EXT4_EXTENTS_FL)
190*6a54128fSAndroid Build Coastguard Worker return 0;
191*6a54128fSAndroid Build Coastguard Worker if (ext2fs_inline_data_size(fs, ino, &inline_size))
192*6a54128fSAndroid Build Coastguard Worker return 0;
193*6a54128fSAndroid Build Coastguard Worker if (inode->i_size != inline_size)
194*6a54128fSAndroid Build Coastguard Worker return 0;
195*6a54128fSAndroid Build Coastguard Worker
196*6a54128fSAndroid Build Coastguard Worker return 1;
197*6a54128fSAndroid Build Coastguard Worker }
198*6a54128fSAndroid Build Coastguard Worker
199*6a54128fSAndroid Build Coastguard Worker if (ext2fs_is_fast_symlink(inode)) {
200*6a54128fSAndroid Build Coastguard Worker if (inode->i_flags & EXT4_EXTENTS_FL)
201*6a54128fSAndroid Build Coastguard Worker return 0;
202*6a54128fSAndroid Build Coastguard Worker buf = (char *)inode->i_block;
203*6a54128fSAndroid Build Coastguard Worker buflen = sizeof(inode->i_block);
204*6a54128fSAndroid Build Coastguard Worker } else {
205*6a54128fSAndroid Build Coastguard Worker ext2_extent_handle_t handle;
206*6a54128fSAndroid Build Coastguard Worker struct ext2_extent_info info;
207*6a54128fSAndroid Build Coastguard Worker struct ext2fs_extent extent;
208*6a54128fSAndroid Build Coastguard Worker blk64_t blk;
209*6a54128fSAndroid Build Coastguard Worker int i;
210*6a54128fSAndroid Build Coastguard Worker
211*6a54128fSAndroid Build Coastguard Worker if (inode->i_flags & EXT4_EXTENTS_FL) {
212*6a54128fSAndroid Build Coastguard Worker if (ext2fs_extent_open2(fs, ino, inode, &handle))
213*6a54128fSAndroid Build Coastguard Worker return 0;
214*6a54128fSAndroid Build Coastguard Worker if (ext2fs_extent_get_info(handle, &info) ||
215*6a54128fSAndroid Build Coastguard Worker (info.num_entries != 1) ||
216*6a54128fSAndroid Build Coastguard Worker (info.max_depth != 0)) {
217*6a54128fSAndroid Build Coastguard Worker ext2fs_extent_free(handle);
218*6a54128fSAndroid Build Coastguard Worker return 0;
219*6a54128fSAndroid Build Coastguard Worker }
220*6a54128fSAndroid Build Coastguard Worker if (ext2fs_extent_get(handle, EXT2_EXTENT_ROOT,
221*6a54128fSAndroid Build Coastguard Worker &extent) ||
222*6a54128fSAndroid Build Coastguard Worker (extent.e_lblk != 0) ||
223*6a54128fSAndroid Build Coastguard Worker (extent.e_len != 1)) {
224*6a54128fSAndroid Build Coastguard Worker ext2fs_extent_free(handle);
225*6a54128fSAndroid Build Coastguard Worker return 0;
226*6a54128fSAndroid Build Coastguard Worker }
227*6a54128fSAndroid Build Coastguard Worker blk = extent.e_pblk;
228*6a54128fSAndroid Build Coastguard Worker ext2fs_extent_free(handle);
229*6a54128fSAndroid Build Coastguard Worker } else {
230*6a54128fSAndroid Build Coastguard Worker blk = inode->i_block[0];
231*6a54128fSAndroid Build Coastguard Worker
232*6a54128fSAndroid Build Coastguard Worker for (i = 1; i < EXT2_N_BLOCKS; i++)
233*6a54128fSAndroid Build Coastguard Worker if (inode->i_block[i])
234*6a54128fSAndroid Build Coastguard Worker return 0;
235*6a54128fSAndroid Build Coastguard Worker }
236*6a54128fSAndroid Build Coastguard Worker
237*6a54128fSAndroid Build Coastguard Worker if (blk < fs->super->s_first_data_block ||
238*6a54128fSAndroid Build Coastguard Worker blk >= ext2fs_blocks_count(fs->super))
239*6a54128fSAndroid Build Coastguard Worker return 0;
240*6a54128fSAndroid Build Coastguard Worker
241*6a54128fSAndroid Build Coastguard Worker if (io_channel_read_blk64(fs->io, blk, 1, buf))
242*6a54128fSAndroid Build Coastguard Worker return 0;
243*6a54128fSAndroid Build Coastguard Worker
244*6a54128fSAndroid Build Coastguard Worker buflen = fs->blocksize;
245*6a54128fSAndroid Build Coastguard Worker }
246*6a54128fSAndroid Build Coastguard Worker
247*6a54128fSAndroid Build Coastguard Worker if (inode->i_flags & EXT4_ENCRYPT_FL)
248*6a54128fSAndroid Build Coastguard Worker len = ext2fs_le16_to_cpu(*(__u16 *)buf) + 2;
249*6a54128fSAndroid Build Coastguard Worker else
250*6a54128fSAndroid Build Coastguard Worker len = strnlen(buf, buflen);
251*6a54128fSAndroid Build Coastguard Worker
252*6a54128fSAndroid Build Coastguard Worker if (len >= buflen)
253*6a54128fSAndroid Build Coastguard Worker return 0;
254*6a54128fSAndroid Build Coastguard Worker
255*6a54128fSAndroid Build Coastguard Worker if (len != inode->i_size)
256*6a54128fSAndroid Build Coastguard Worker return 0;
257*6a54128fSAndroid Build Coastguard Worker return 1;
258*6a54128fSAndroid Build Coastguard Worker }
259*6a54128fSAndroid Build Coastguard Worker
260*6a54128fSAndroid Build Coastguard Worker /*
261*6a54128fSAndroid Build Coastguard Worker * If the extents or inlinedata flags are set on the inode, offer to clear 'em.
262*6a54128fSAndroid Build Coastguard Worker */
263*6a54128fSAndroid Build Coastguard Worker #define BAD_SPECIAL_FLAGS (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)
check_extents_inlinedata(e2fsck_t ctx,struct problem_context * pctx)264*6a54128fSAndroid Build Coastguard Worker static void check_extents_inlinedata(e2fsck_t ctx,
265*6a54128fSAndroid Build Coastguard Worker struct problem_context *pctx)
266*6a54128fSAndroid Build Coastguard Worker {
267*6a54128fSAndroid Build Coastguard Worker if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
268*6a54128fSAndroid Build Coastguard Worker return;
269*6a54128fSAndroid Build Coastguard Worker
270*6a54128fSAndroid Build Coastguard Worker if (!fix_problem(ctx, PR_1_SPECIAL_EXTENTS_IDATA, pctx))
271*6a54128fSAndroid Build Coastguard Worker return;
272*6a54128fSAndroid Build Coastguard Worker
273*6a54128fSAndroid Build Coastguard Worker pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
274*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
275*6a54128fSAndroid Build Coastguard Worker }
276*6a54128fSAndroid Build Coastguard Worker #undef BAD_SPECIAL_FLAGS
277*6a54128fSAndroid Build Coastguard Worker
278*6a54128fSAndroid Build Coastguard Worker /*
279*6a54128fSAndroid Build Coastguard Worker * If the immutable (or append-only) flag is set on the inode, offer
280*6a54128fSAndroid Build Coastguard Worker * to clear it.
281*6a54128fSAndroid Build Coastguard Worker */
282*6a54128fSAndroid Build Coastguard Worker #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
check_immutable(e2fsck_t ctx,struct problem_context * pctx)283*6a54128fSAndroid Build Coastguard Worker static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
284*6a54128fSAndroid Build Coastguard Worker {
285*6a54128fSAndroid Build Coastguard Worker if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
286*6a54128fSAndroid Build Coastguard Worker return;
287*6a54128fSAndroid Build Coastguard Worker
288*6a54128fSAndroid Build Coastguard Worker if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
289*6a54128fSAndroid Build Coastguard Worker return;
290*6a54128fSAndroid Build Coastguard Worker
291*6a54128fSAndroid Build Coastguard Worker pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
292*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
293*6a54128fSAndroid Build Coastguard Worker }
294*6a54128fSAndroid Build Coastguard Worker
295*6a54128fSAndroid Build Coastguard Worker /*
296*6a54128fSAndroid Build Coastguard Worker * If device, fifo or socket, check size is zero -- if not offer to
297*6a54128fSAndroid Build Coastguard Worker * clear it
298*6a54128fSAndroid Build Coastguard Worker */
check_size(e2fsck_t ctx,struct problem_context * pctx)299*6a54128fSAndroid Build Coastguard Worker static void check_size(e2fsck_t ctx, struct problem_context *pctx)
300*6a54128fSAndroid Build Coastguard Worker {
301*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode = pctx->inode;
302*6a54128fSAndroid Build Coastguard Worker
303*6a54128fSAndroid Build Coastguard Worker if (EXT2_I_SIZE(inode) == 0)
304*6a54128fSAndroid Build Coastguard Worker return;
305*6a54128fSAndroid Build Coastguard Worker
306*6a54128fSAndroid Build Coastguard Worker if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
307*6a54128fSAndroid Build Coastguard Worker return;
308*6a54128fSAndroid Build Coastguard Worker
309*6a54128fSAndroid Build Coastguard Worker ext2fs_inode_size_set(ctx->fs, inode, 0);
310*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
311*6a54128fSAndroid Build Coastguard Worker }
312*6a54128fSAndroid Build Coastguard Worker
313*6a54128fSAndroid Build Coastguard Worker /*
314*6a54128fSAndroid Build Coastguard Worker * For a given size, calculate how many blocks would be charged towards quota.
315*6a54128fSAndroid Build Coastguard Worker */
size_to_quota_blocks(ext2_filsys fs,size_t size)316*6a54128fSAndroid Build Coastguard Worker static blk64_t size_to_quota_blocks(ext2_filsys fs, size_t size)
317*6a54128fSAndroid Build Coastguard Worker {
318*6a54128fSAndroid Build Coastguard Worker blk64_t clusters;
319*6a54128fSAndroid Build Coastguard Worker
320*6a54128fSAndroid Build Coastguard Worker clusters = DIV_ROUND_UP(size, fs->blocksize << fs->cluster_ratio_bits);
321*6a54128fSAndroid Build Coastguard Worker return EXT2FS_C2B(fs, clusters);
322*6a54128fSAndroid Build Coastguard Worker }
323*6a54128fSAndroid Build Coastguard Worker
324*6a54128fSAndroid Build Coastguard Worker /*
325*6a54128fSAndroid Build Coastguard Worker * Check validity of EA inode. Return 0 if EA inode is valid, otherwise return
326*6a54128fSAndroid Build Coastguard Worker * the problem code.
327*6a54128fSAndroid Build Coastguard Worker */
check_large_ea_inode(e2fsck_t ctx,struct ext2_ext_attr_entry * entry,struct problem_context * pctx,blk64_t * quota_blocks)328*6a54128fSAndroid Build Coastguard Worker static problem_t check_large_ea_inode(e2fsck_t ctx,
329*6a54128fSAndroid Build Coastguard Worker struct ext2_ext_attr_entry *entry,
330*6a54128fSAndroid Build Coastguard Worker struct problem_context *pctx,
331*6a54128fSAndroid Build Coastguard Worker blk64_t *quota_blocks)
332*6a54128fSAndroid Build Coastguard Worker {
333*6a54128fSAndroid Build Coastguard Worker struct ext2_inode inode;
334*6a54128fSAndroid Build Coastguard Worker __u32 hash, signed_hash;
335*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
336*6a54128fSAndroid Build Coastguard Worker
337*6a54128fSAndroid Build Coastguard Worker /* Check if inode is within valid range */
338*6a54128fSAndroid Build Coastguard Worker if ((entry->e_value_inum < EXT2_FIRST_INODE(ctx->fs->super)) ||
339*6a54128fSAndroid Build Coastguard Worker (entry->e_value_inum > ctx->fs->super->s_inodes_count)) {
340*6a54128fSAndroid Build Coastguard Worker pctx->num = entry->e_value_inum;
341*6a54128fSAndroid Build Coastguard Worker return PR_1_ATTR_VALUE_EA_INODE;
342*6a54128fSAndroid Build Coastguard Worker }
343*6a54128fSAndroid Build Coastguard Worker
344*6a54128fSAndroid Build Coastguard Worker e2fsck_read_inode(ctx, entry->e_value_inum, &inode, "pass1");
345*6a54128fSAndroid Build Coastguard Worker
346*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_ext_attr_hash_entry3(ctx->fs, entry, NULL, &hash,
347*6a54128fSAndroid Build Coastguard Worker &signed_hash);
348*6a54128fSAndroid Build Coastguard Worker if (retval) {
349*6a54128fSAndroid Build Coastguard Worker com_err("check_large_ea_inode", retval,
350*6a54128fSAndroid Build Coastguard Worker _("while hashing entry with e_value_inum = %u"),
351*6a54128fSAndroid Build Coastguard Worker entry->e_value_inum);
352*6a54128fSAndroid Build Coastguard Worker fatal_error(ctx, 0);
353*6a54128fSAndroid Build Coastguard Worker }
354*6a54128fSAndroid Build Coastguard Worker
355*6a54128fSAndroid Build Coastguard Worker if ((hash == entry->e_hash) || (signed_hash == entry->e_hash)) {
356*6a54128fSAndroid Build Coastguard Worker *quota_blocks = size_to_quota_blocks(ctx->fs,
357*6a54128fSAndroid Build Coastguard Worker entry->e_value_size);
358*6a54128fSAndroid Build Coastguard Worker } else {
359*6a54128fSAndroid Build Coastguard Worker /* This might be an old Lustre-style ea_inode reference. */
360*6a54128fSAndroid Build Coastguard Worker if (inode.i_mtime == pctx->ino &&
361*6a54128fSAndroid Build Coastguard Worker inode.i_generation == pctx->inode->i_generation) {
362*6a54128fSAndroid Build Coastguard Worker *quota_blocks = 0;
363*6a54128fSAndroid Build Coastguard Worker } else {
364*6a54128fSAndroid Build Coastguard Worker /* If target inode is also missing EA_INODE flag,
365*6a54128fSAndroid Build Coastguard Worker * this is likely to be a bad reference.
366*6a54128fSAndroid Build Coastguard Worker */
367*6a54128fSAndroid Build Coastguard Worker if (!(inode.i_flags & EXT4_EA_INODE_FL)) {
368*6a54128fSAndroid Build Coastguard Worker pctx->num = entry->e_value_inum;
369*6a54128fSAndroid Build Coastguard Worker return PR_1_ATTR_VALUE_EA_INODE;
370*6a54128fSAndroid Build Coastguard Worker } else {
371*6a54128fSAndroid Build Coastguard Worker pctx->num = entry->e_hash;
372*6a54128fSAndroid Build Coastguard Worker return PR_1_ATTR_HASH;
373*6a54128fSAndroid Build Coastguard Worker }
374*6a54128fSAndroid Build Coastguard Worker }
375*6a54128fSAndroid Build Coastguard Worker }
376*6a54128fSAndroid Build Coastguard Worker
377*6a54128fSAndroid Build Coastguard Worker if (!(inode.i_flags & EXT4_EA_INODE_FL)) {
378*6a54128fSAndroid Build Coastguard Worker pctx->num = entry->e_value_inum;
379*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_ATTR_SET_EA_INODE_FL, pctx)) {
380*6a54128fSAndroid Build Coastguard Worker inode.i_flags |= EXT4_EA_INODE_FL;
381*6a54128fSAndroid Build Coastguard Worker ext2fs_write_inode(ctx->fs, entry->e_value_inum,
382*6a54128fSAndroid Build Coastguard Worker &inode);
383*6a54128fSAndroid Build Coastguard Worker } else {
384*6a54128fSAndroid Build Coastguard Worker return PR_1_ATTR_NO_EA_INODE_FL;
385*6a54128fSAndroid Build Coastguard Worker }
386*6a54128fSAndroid Build Coastguard Worker }
387*6a54128fSAndroid Build Coastguard Worker return 0;
388*6a54128fSAndroid Build Coastguard Worker }
389*6a54128fSAndroid Build Coastguard Worker
inc_ea_inode_refs(e2fsck_t ctx,struct problem_context * pctx,struct ext2_ext_attr_entry * first,void * end)390*6a54128fSAndroid Build Coastguard Worker static void inc_ea_inode_refs(e2fsck_t ctx, struct problem_context *pctx,
391*6a54128fSAndroid Build Coastguard Worker struct ext2_ext_attr_entry *first, void *end)
392*6a54128fSAndroid Build Coastguard Worker {
393*6a54128fSAndroid Build Coastguard Worker struct ext2_ext_attr_entry *entry = first;
394*6a54128fSAndroid Build Coastguard Worker struct ext2_ext_attr_entry *np = EXT2_EXT_ATTR_NEXT(entry);
395*6a54128fSAndroid Build Coastguard Worker
396*6a54128fSAndroid Build Coastguard Worker while ((void *) entry < end && (void *) np < end &&
397*6a54128fSAndroid Build Coastguard Worker !EXT2_EXT_IS_LAST_ENTRY(entry)) {
398*6a54128fSAndroid Build Coastguard Worker if (!entry->e_value_inum)
399*6a54128fSAndroid Build Coastguard Worker goto next;
400*6a54128fSAndroid Build Coastguard Worker if (!ctx->ea_inode_refs) {
401*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ea_refcount_create(0,
402*6a54128fSAndroid Build Coastguard Worker &ctx->ea_inode_refs);
403*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode) {
404*6a54128fSAndroid Build Coastguard Worker pctx->num = 4;
405*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
406*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
407*6a54128fSAndroid Build Coastguard Worker return;
408*6a54128fSAndroid Build Coastguard Worker }
409*6a54128fSAndroid Build Coastguard Worker }
410*6a54128fSAndroid Build Coastguard Worker ea_refcount_increment(ctx->ea_inode_refs, entry->e_value_inum,
411*6a54128fSAndroid Build Coastguard Worker 0);
412*6a54128fSAndroid Build Coastguard Worker next:
413*6a54128fSAndroid Build Coastguard Worker entry = np;
414*6a54128fSAndroid Build Coastguard Worker np = EXT2_EXT_ATTR_NEXT(entry);
415*6a54128fSAndroid Build Coastguard Worker }
416*6a54128fSAndroid Build Coastguard Worker }
417*6a54128fSAndroid Build Coastguard Worker
check_ea_in_inode(e2fsck_t ctx,struct problem_context * pctx,struct ea_quota * ea_ibody_quota)418*6a54128fSAndroid Build Coastguard Worker static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx,
419*6a54128fSAndroid Build Coastguard Worker struct ea_quota *ea_ibody_quota)
420*6a54128fSAndroid Build Coastguard Worker {
421*6a54128fSAndroid Build Coastguard Worker struct ext2_super_block *sb = ctx->fs->super;
422*6a54128fSAndroid Build Coastguard Worker struct ext2_inode_large *inode;
423*6a54128fSAndroid Build Coastguard Worker struct ext2_ext_attr_entry *entry;
424*6a54128fSAndroid Build Coastguard Worker char *start, *header, *end;
425*6a54128fSAndroid Build Coastguard Worker unsigned int storage_size, remain;
426*6a54128fSAndroid Build Coastguard Worker problem_t problem = 0;
427*6a54128fSAndroid Build Coastguard Worker region_t region = 0;
428*6a54128fSAndroid Build Coastguard Worker
429*6a54128fSAndroid Build Coastguard Worker ea_ibody_quota->blocks = 0;
430*6a54128fSAndroid Build Coastguard Worker ea_ibody_quota->inodes = 0;
431*6a54128fSAndroid Build Coastguard Worker
432*6a54128fSAndroid Build Coastguard Worker inode = (struct ext2_inode_large *) pctx->inode;
433*6a54128fSAndroid Build Coastguard Worker storage_size = EXT2_INODE_SIZE(ctx->fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
434*6a54128fSAndroid Build Coastguard Worker inode->i_extra_isize;
435*6a54128fSAndroid Build Coastguard Worker header = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
436*6a54128fSAndroid Build Coastguard Worker inode->i_extra_isize;
437*6a54128fSAndroid Build Coastguard Worker end = header + storage_size;
438*6a54128fSAndroid Build Coastguard Worker start = header + sizeof(__u32);
439*6a54128fSAndroid Build Coastguard Worker entry = (struct ext2_ext_attr_entry *) start;
440*6a54128fSAndroid Build Coastguard Worker
441*6a54128fSAndroid Build Coastguard Worker /* scan all entry's headers first */
442*6a54128fSAndroid Build Coastguard Worker
443*6a54128fSAndroid Build Coastguard Worker /* take finish entry 0UL into account */
444*6a54128fSAndroid Build Coastguard Worker remain = storage_size - sizeof(__u32);
445*6a54128fSAndroid Build Coastguard Worker
446*6a54128fSAndroid Build Coastguard Worker region = region_create(0, storage_size);
447*6a54128fSAndroid Build Coastguard Worker if (!region) {
448*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
449*6a54128fSAndroid Build Coastguard Worker problem = 0;
450*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
451*6a54128fSAndroid Build Coastguard Worker return;
452*6a54128fSAndroid Build Coastguard Worker }
453*6a54128fSAndroid Build Coastguard Worker if (region_allocate(region, 0, sizeof(__u32))) {
454*6a54128fSAndroid Build Coastguard Worker problem = PR_1_INODE_EA_ALLOC_COLLISION;
455*6a54128fSAndroid Build Coastguard Worker goto fix;
456*6a54128fSAndroid Build Coastguard Worker }
457*6a54128fSAndroid Build Coastguard Worker
458*6a54128fSAndroid Build Coastguard Worker while (remain >= sizeof(struct ext2_ext_attr_entry) &&
459*6a54128fSAndroid Build Coastguard Worker !EXT2_EXT_IS_LAST_ENTRY(entry)) {
460*6a54128fSAndroid Build Coastguard Worker __u32 hash;
461*6a54128fSAndroid Build Coastguard Worker
462*6a54128fSAndroid Build Coastguard Worker if (region_allocate(region, (char *)entry - (char *)header,
463*6a54128fSAndroid Build Coastguard Worker EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
464*6a54128fSAndroid Build Coastguard Worker problem = PR_1_INODE_EA_ALLOC_COLLISION;
465*6a54128fSAndroid Build Coastguard Worker goto fix;
466*6a54128fSAndroid Build Coastguard Worker }
467*6a54128fSAndroid Build Coastguard Worker
468*6a54128fSAndroid Build Coastguard Worker /* header eats this space */
469*6a54128fSAndroid Build Coastguard Worker remain -= sizeof(struct ext2_ext_attr_entry);
470*6a54128fSAndroid Build Coastguard Worker
471*6a54128fSAndroid Build Coastguard Worker /* is attribute name valid? */
472*6a54128fSAndroid Build Coastguard Worker if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
473*6a54128fSAndroid Build Coastguard Worker pctx->num = entry->e_name_len;
474*6a54128fSAndroid Build Coastguard Worker problem = PR_1_ATTR_NAME_LEN;
475*6a54128fSAndroid Build Coastguard Worker goto fix;
476*6a54128fSAndroid Build Coastguard Worker }
477*6a54128fSAndroid Build Coastguard Worker
478*6a54128fSAndroid Build Coastguard Worker /* attribute len eats this space */
479*6a54128fSAndroid Build Coastguard Worker remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
480*6a54128fSAndroid Build Coastguard Worker
481*6a54128fSAndroid Build Coastguard Worker if (entry->e_value_inum == 0) {
482*6a54128fSAndroid Build Coastguard Worker /* check value size */
483*6a54128fSAndroid Build Coastguard Worker if (entry->e_value_size > remain) {
484*6a54128fSAndroid Build Coastguard Worker pctx->num = entry->e_value_size;
485*6a54128fSAndroid Build Coastguard Worker problem = PR_1_ATTR_VALUE_SIZE;
486*6a54128fSAndroid Build Coastguard Worker goto fix;
487*6a54128fSAndroid Build Coastguard Worker }
488*6a54128fSAndroid Build Coastguard Worker
489*6a54128fSAndroid Build Coastguard Worker if (entry->e_value_size &&
490*6a54128fSAndroid Build Coastguard Worker region_allocate(region,
491*6a54128fSAndroid Build Coastguard Worker sizeof(__u32) + entry->e_value_offs,
492*6a54128fSAndroid Build Coastguard Worker EXT2_EXT_ATTR_SIZE(
493*6a54128fSAndroid Build Coastguard Worker entry->e_value_size))) {
494*6a54128fSAndroid Build Coastguard Worker problem = PR_1_INODE_EA_ALLOC_COLLISION;
495*6a54128fSAndroid Build Coastguard Worker goto fix;
496*6a54128fSAndroid Build Coastguard Worker }
497*6a54128fSAndroid Build Coastguard Worker
498*6a54128fSAndroid Build Coastguard Worker hash = ext2fs_ext_attr_hash_entry(entry,
499*6a54128fSAndroid Build Coastguard Worker start + entry->e_value_offs);
500*6a54128fSAndroid Build Coastguard Worker if (entry->e_hash != 0 && entry->e_hash != hash)
501*6a54128fSAndroid Build Coastguard Worker hash = ext2fs_ext_attr_hash_entry_signed(entry,
502*6a54128fSAndroid Build Coastguard Worker start + entry->e_value_offs);
503*6a54128fSAndroid Build Coastguard Worker
504*6a54128fSAndroid Build Coastguard Worker /* e_hash may be 0 in older inode's ea */
505*6a54128fSAndroid Build Coastguard Worker if (entry->e_hash != 0 && entry->e_hash != hash) {
506*6a54128fSAndroid Build Coastguard Worker pctx->num = entry->e_hash;
507*6a54128fSAndroid Build Coastguard Worker problem = PR_1_ATTR_HASH;
508*6a54128fSAndroid Build Coastguard Worker goto fix;
509*6a54128fSAndroid Build Coastguard Worker }
510*6a54128fSAndroid Build Coastguard Worker } else {
511*6a54128fSAndroid Build Coastguard Worker blk64_t quota_blocks;
512*6a54128fSAndroid Build Coastguard Worker
513*6a54128fSAndroid Build Coastguard Worker problem = check_large_ea_inode(ctx, entry, pctx,
514*6a54128fSAndroid Build Coastguard Worker "a_blocks);
515*6a54128fSAndroid Build Coastguard Worker if (problem != 0)
516*6a54128fSAndroid Build Coastguard Worker goto fix;
517*6a54128fSAndroid Build Coastguard Worker
518*6a54128fSAndroid Build Coastguard Worker ea_ibody_quota->blocks += quota_blocks;
519*6a54128fSAndroid Build Coastguard Worker ea_ibody_quota->inodes++;
520*6a54128fSAndroid Build Coastguard Worker }
521*6a54128fSAndroid Build Coastguard Worker
522*6a54128fSAndroid Build Coastguard Worker /* If EA value is stored in external inode then it does not
523*6a54128fSAndroid Build Coastguard Worker * consume space here */
524*6a54128fSAndroid Build Coastguard Worker if (entry->e_value_inum == 0)
525*6a54128fSAndroid Build Coastguard Worker remain -= entry->e_value_size;
526*6a54128fSAndroid Build Coastguard Worker
527*6a54128fSAndroid Build Coastguard Worker entry = EXT2_EXT_ATTR_NEXT(entry);
528*6a54128fSAndroid Build Coastguard Worker }
529*6a54128fSAndroid Build Coastguard Worker
530*6a54128fSAndroid Build Coastguard Worker if (region_allocate(region, (char *)entry - (char *)header,
531*6a54128fSAndroid Build Coastguard Worker sizeof(__u32))) {
532*6a54128fSAndroid Build Coastguard Worker problem = PR_1_INODE_EA_ALLOC_COLLISION;
533*6a54128fSAndroid Build Coastguard Worker goto fix;
534*6a54128fSAndroid Build Coastguard Worker }
535*6a54128fSAndroid Build Coastguard Worker fix:
536*6a54128fSAndroid Build Coastguard Worker if (region)
537*6a54128fSAndroid Build Coastguard Worker region_free(region);
538*6a54128fSAndroid Build Coastguard Worker /*
539*6a54128fSAndroid Build Coastguard Worker * it seems like a corruption. it's very unlikely we could repair
540*6a54128fSAndroid Build Coastguard Worker * EA(s) in automatic fashion -bzzz
541*6a54128fSAndroid Build Coastguard Worker */
542*6a54128fSAndroid Build Coastguard Worker if (problem == 0 || !fix_problem(ctx, problem, pctx)) {
543*6a54128fSAndroid Build Coastguard Worker inc_ea_inode_refs(ctx, pctx,
544*6a54128fSAndroid Build Coastguard Worker (struct ext2_ext_attr_entry *)start, end);
545*6a54128fSAndroid Build Coastguard Worker return;
546*6a54128fSAndroid Build Coastguard Worker }
547*6a54128fSAndroid Build Coastguard Worker
548*6a54128fSAndroid Build Coastguard Worker /* simply remove all possible EA(s) */
549*6a54128fSAndroid Build Coastguard Worker *((__u32 *)header) = 0UL;
550*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
551*6a54128fSAndroid Build Coastguard Worker EXT2_INODE_SIZE(sb), "pass1");
552*6a54128fSAndroid Build Coastguard Worker ea_ibody_quota->blocks = 0;
553*6a54128fSAndroid Build Coastguard Worker ea_ibody_quota->inodes = 0;
554*6a54128fSAndroid Build Coastguard Worker }
555*6a54128fSAndroid Build Coastguard Worker
check_inode_extra_negative_epoch(__u32 xtime,__u32 extra)556*6a54128fSAndroid Build Coastguard Worker static int check_inode_extra_negative_epoch(__u32 xtime, __u32 extra) {
557*6a54128fSAndroid Build Coastguard Worker return (xtime & (1U << 31)) != 0 &&
558*6a54128fSAndroid Build Coastguard Worker (extra & EXT4_EPOCH_MASK) == EXT4_EPOCH_MASK;
559*6a54128fSAndroid Build Coastguard Worker }
560*6a54128fSAndroid Build Coastguard Worker
561*6a54128fSAndroid Build Coastguard Worker #define CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, xtime) \
562*6a54128fSAndroid Build Coastguard Worker check_inode_extra_negative_epoch(inode->i_##xtime, \
563*6a54128fSAndroid Build Coastguard Worker inode->i_##xtime##_extra)
564*6a54128fSAndroid Build Coastguard Worker
565*6a54128fSAndroid Build Coastguard Worker /* When today's date is earlier than 2242, we assume that atimes,
566*6a54128fSAndroid Build Coastguard Worker * ctimes, crtimes, and mtimes with years in the range 2310..2378 are
567*6a54128fSAndroid Build Coastguard Worker * actually pre-1970 dates mis-encoded.
568*6a54128fSAndroid Build Coastguard Worker */
569*6a54128fSAndroid Build Coastguard Worker #define EXT4_EXTRA_NEGATIVE_DATE_CUTOFF 2 * (1LL << 32)
570*6a54128fSAndroid Build Coastguard Worker
check_inode_extra_space(e2fsck_t ctx,struct problem_context * pctx,struct ea_quota * ea_ibody_quota)571*6a54128fSAndroid Build Coastguard Worker static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx,
572*6a54128fSAndroid Build Coastguard Worker struct ea_quota *ea_ibody_quota)
573*6a54128fSAndroid Build Coastguard Worker {
574*6a54128fSAndroid Build Coastguard Worker struct ext2_super_block *sb = ctx->fs->super;
575*6a54128fSAndroid Build Coastguard Worker struct ext2_inode_large *inode;
576*6a54128fSAndroid Build Coastguard Worker __u32 *eamagic;
577*6a54128fSAndroid Build Coastguard Worker int min, max;
578*6a54128fSAndroid Build Coastguard Worker
579*6a54128fSAndroid Build Coastguard Worker ea_ibody_quota->blocks = 0;
580*6a54128fSAndroid Build Coastguard Worker ea_ibody_quota->inodes = 0;
581*6a54128fSAndroid Build Coastguard Worker
582*6a54128fSAndroid Build Coastguard Worker inode = (struct ext2_inode_large *) pctx->inode;
583*6a54128fSAndroid Build Coastguard Worker if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
584*6a54128fSAndroid Build Coastguard Worker /* this isn't large inode. so, nothing to check */
585*6a54128fSAndroid Build Coastguard Worker return;
586*6a54128fSAndroid Build Coastguard Worker }
587*6a54128fSAndroid Build Coastguard Worker
588*6a54128fSAndroid Build Coastguard Worker #if 0
589*6a54128fSAndroid Build Coastguard Worker printf("inode #%u, i_extra_size %d\n", pctx->ino,
590*6a54128fSAndroid Build Coastguard Worker inode->i_extra_isize);
591*6a54128fSAndroid Build Coastguard Worker #endif
592*6a54128fSAndroid Build Coastguard Worker /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
593*6a54128fSAndroid Build Coastguard Worker min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
594*6a54128fSAndroid Build Coastguard Worker max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
595*6a54128fSAndroid Build Coastguard Worker /*
596*6a54128fSAndroid Build Coastguard Worker * For now we will allow i_extra_isize to be 0, but really
597*6a54128fSAndroid Build Coastguard Worker * implementations should never allow i_extra_isize to be 0
598*6a54128fSAndroid Build Coastguard Worker */
599*6a54128fSAndroid Build Coastguard Worker if (inode->i_extra_isize &&
600*6a54128fSAndroid Build Coastguard Worker (inode->i_extra_isize < min || inode->i_extra_isize > max ||
601*6a54128fSAndroid Build Coastguard Worker inode->i_extra_isize & 3)) {
602*6a54128fSAndroid Build Coastguard Worker if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
603*6a54128fSAndroid Build Coastguard Worker return;
604*6a54128fSAndroid Build Coastguard Worker if (inode->i_extra_isize < min || inode->i_extra_isize > max)
605*6a54128fSAndroid Build Coastguard Worker inode->i_extra_isize = sb->s_want_extra_isize;
606*6a54128fSAndroid Build Coastguard Worker else
607*6a54128fSAndroid Build Coastguard Worker inode->i_extra_isize = (inode->i_extra_isize + 3) & ~3;
608*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
609*6a54128fSAndroid Build Coastguard Worker EXT2_INODE_SIZE(sb), "pass1");
610*6a54128fSAndroid Build Coastguard Worker }
611*6a54128fSAndroid Build Coastguard Worker
612*6a54128fSAndroid Build Coastguard Worker /* check if there is no place for an EA header */
613*6a54128fSAndroid Build Coastguard Worker if (inode->i_extra_isize >= max - sizeof(__u32))
614*6a54128fSAndroid Build Coastguard Worker return;
615*6a54128fSAndroid Build Coastguard Worker
616*6a54128fSAndroid Build Coastguard Worker eamagic = (__u32 *) (((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
617*6a54128fSAndroid Build Coastguard Worker inode->i_extra_isize);
618*6a54128fSAndroid Build Coastguard Worker if (*eamagic == EXT2_EXT_ATTR_MAGIC) {
619*6a54128fSAndroid Build Coastguard Worker /* it seems inode has an extended attribute(s) in body */
620*6a54128fSAndroid Build Coastguard Worker check_ea_in_inode(ctx, pctx, ea_ibody_quota);
621*6a54128fSAndroid Build Coastguard Worker }
622*6a54128fSAndroid Build Coastguard Worker
623*6a54128fSAndroid Build Coastguard Worker /*
624*6a54128fSAndroid Build Coastguard Worker * If the inode's extended atime (ctime, crtime, mtime) is stored in
625*6a54128fSAndroid Build Coastguard Worker * the old, invalid format, repair it.
626*6a54128fSAndroid Build Coastguard Worker */
627*6a54128fSAndroid Build Coastguard Worker if (((sizeof(time_t) <= 4) ||
628*6a54128fSAndroid Build Coastguard Worker (((sizeof(time_t) > 4) &&
629*6a54128fSAndroid Build Coastguard Worker ctx->now < EXT4_EXTRA_NEGATIVE_DATE_CUTOFF))) &&
630*6a54128fSAndroid Build Coastguard Worker (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime) ||
631*6a54128fSAndroid Build Coastguard Worker CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime) ||
632*6a54128fSAndroid Build Coastguard Worker CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime) ||
633*6a54128fSAndroid Build Coastguard Worker CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))) {
634*6a54128fSAndroid Build Coastguard Worker
635*6a54128fSAndroid Build Coastguard Worker if (!fix_problem(ctx, PR_1_EA_TIME_OUT_OF_RANGE, pctx))
636*6a54128fSAndroid Build Coastguard Worker return;
637*6a54128fSAndroid Build Coastguard Worker
638*6a54128fSAndroid Build Coastguard Worker if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime))
639*6a54128fSAndroid Build Coastguard Worker inode->i_atime_extra &= ~EXT4_EPOCH_MASK;
640*6a54128fSAndroid Build Coastguard Worker if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime))
641*6a54128fSAndroid Build Coastguard Worker inode->i_ctime_extra &= ~EXT4_EPOCH_MASK;
642*6a54128fSAndroid Build Coastguard Worker if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime))
643*6a54128fSAndroid Build Coastguard Worker inode->i_crtime_extra &= ~EXT4_EPOCH_MASK;
644*6a54128fSAndroid Build Coastguard Worker if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))
645*6a54128fSAndroid Build Coastguard Worker inode->i_mtime_extra &= ~EXT4_EPOCH_MASK;
646*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
647*6a54128fSAndroid Build Coastguard Worker EXT2_INODE_SIZE(sb), "pass1");
648*6a54128fSAndroid Build Coastguard Worker }
649*6a54128fSAndroid Build Coastguard Worker
650*6a54128fSAndroid Build Coastguard Worker }
651*6a54128fSAndroid Build Coastguard Worker
652*6a54128fSAndroid Build Coastguard Worker /*
653*6a54128fSAndroid Build Coastguard Worker * Check to see if the inode might really be a directory, despite i_mode
654*6a54128fSAndroid Build Coastguard Worker *
655*6a54128fSAndroid Build Coastguard Worker * This is a lot of complexity for something for which I'm not really
656*6a54128fSAndroid Build Coastguard Worker * convinced happens frequently in the wild. If for any reason this
657*6a54128fSAndroid Build Coastguard Worker * causes any problems, take this code out.
658*6a54128fSAndroid Build Coastguard Worker * [tytso:20070331.0827EDT]
659*6a54128fSAndroid Build Coastguard Worker */
check_is_really_dir(e2fsck_t ctx,struct problem_context * pctx,char * buf)660*6a54128fSAndroid Build Coastguard Worker static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
661*6a54128fSAndroid Build Coastguard Worker char *buf)
662*6a54128fSAndroid Build Coastguard Worker {
663*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode = pctx->inode;
664*6a54128fSAndroid Build Coastguard Worker struct ext2_dir_entry *dirent;
665*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
666*6a54128fSAndroid Build Coastguard Worker blk64_t blk;
667*6a54128fSAndroid Build Coastguard Worker unsigned int i, rec_len, not_device = 0;
668*6a54128fSAndroid Build Coastguard Worker int extent_fs;
669*6a54128fSAndroid Build Coastguard Worker int inlinedata_fs;
670*6a54128fSAndroid Build Coastguard Worker
671*6a54128fSAndroid Build Coastguard Worker /*
672*6a54128fSAndroid Build Coastguard Worker * If the mode looks OK, we believe it. If the first block in
673*6a54128fSAndroid Build Coastguard Worker * the i_block array is 0, this cannot be a directory. If the
674*6a54128fSAndroid Build Coastguard Worker * inode is extent-mapped, it is still the case that the latter
675*6a54128fSAndroid Build Coastguard Worker * cannot be 0 - the magic number in the extent header would make
676*6a54128fSAndroid Build Coastguard Worker * it nonzero.
677*6a54128fSAndroid Build Coastguard Worker */
678*6a54128fSAndroid Build Coastguard Worker if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
679*6a54128fSAndroid Build Coastguard Worker LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
680*6a54128fSAndroid Build Coastguard Worker return;
681*6a54128fSAndroid Build Coastguard Worker
682*6a54128fSAndroid Build Coastguard Worker /*
683*6a54128fSAndroid Build Coastguard Worker * Check the block numbers in the i_block array for validity:
684*6a54128fSAndroid Build Coastguard Worker * zero blocks are skipped (but the first one cannot be zero -
685*6a54128fSAndroid Build Coastguard Worker * see above), other blocks are checked against the first and
686*6a54128fSAndroid Build Coastguard Worker * max data blocks (from the the superblock) and against the
687*6a54128fSAndroid Build Coastguard Worker * block bitmap. Any invalid block found means this cannot be
688*6a54128fSAndroid Build Coastguard Worker * a directory.
689*6a54128fSAndroid Build Coastguard Worker *
690*6a54128fSAndroid Build Coastguard Worker * If there are non-zero blocks past the fourth entry, then
691*6a54128fSAndroid Build Coastguard Worker * this cannot be a device file: we remember that for the next
692*6a54128fSAndroid Build Coastguard Worker * check.
693*6a54128fSAndroid Build Coastguard Worker *
694*6a54128fSAndroid Build Coastguard Worker * For extent mapped files, we don't do any sanity checking:
695*6a54128fSAndroid Build Coastguard Worker * just try to get the phys block of logical block 0 and run
696*6a54128fSAndroid Build Coastguard Worker * with it.
697*6a54128fSAndroid Build Coastguard Worker *
698*6a54128fSAndroid Build Coastguard Worker * For inline data files, we just try to get the size of inline
699*6a54128fSAndroid Build Coastguard Worker * data. If it's true, we will treat it as a directory.
700*6a54128fSAndroid Build Coastguard Worker */
701*6a54128fSAndroid Build Coastguard Worker
702*6a54128fSAndroid Build Coastguard Worker extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
703*6a54128fSAndroid Build Coastguard Worker inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
704*6a54128fSAndroid Build Coastguard Worker if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL)) {
705*6a54128fSAndroid Build Coastguard Worker size_t size;
706*6a54128fSAndroid Build Coastguard Worker __u32 dotdot;
707*6a54128fSAndroid Build Coastguard Worker unsigned int rec_len2;
708*6a54128fSAndroid Build Coastguard Worker struct ext2_dir_entry de;
709*6a54128fSAndroid Build Coastguard Worker
710*6a54128fSAndroid Build Coastguard Worker if (ext2fs_inline_data_size(ctx->fs, pctx->ino, &size))
711*6a54128fSAndroid Build Coastguard Worker return;
712*6a54128fSAndroid Build Coastguard Worker /*
713*6a54128fSAndroid Build Coastguard Worker * If the size isn't a multiple of 4, it's probably not a
714*6a54128fSAndroid Build Coastguard Worker * directory??
715*6a54128fSAndroid Build Coastguard Worker */
716*6a54128fSAndroid Build Coastguard Worker if (size & 3)
717*6a54128fSAndroid Build Coastguard Worker return;
718*6a54128fSAndroid Build Coastguard Worker /*
719*6a54128fSAndroid Build Coastguard Worker * If the first 10 bytes don't look like a directory entry,
720*6a54128fSAndroid Build Coastguard Worker * it's probably not a directory.
721*6a54128fSAndroid Build Coastguard Worker */
722*6a54128fSAndroid Build Coastguard Worker memcpy(&dotdot, inode->i_block, sizeof(dotdot));
723*6a54128fSAndroid Build Coastguard Worker memcpy(&de, ((char *)inode->i_block) + EXT4_INLINE_DATA_DOTDOT_SIZE,
724*6a54128fSAndroid Build Coastguard Worker EXT2_DIR_REC_LEN(0));
725*6a54128fSAndroid Build Coastguard Worker dotdot = ext2fs_le32_to_cpu(dotdot);
726*6a54128fSAndroid Build Coastguard Worker de.inode = ext2fs_le32_to_cpu(de.inode);
727*6a54128fSAndroid Build Coastguard Worker de.rec_len = ext2fs_le16_to_cpu(de.rec_len);
728*6a54128fSAndroid Build Coastguard Worker ext2fs_get_rec_len(ctx->fs, &de, &rec_len2);
729*6a54128fSAndroid Build Coastguard Worker if (dotdot >= ctx->fs->super->s_inodes_count ||
730*6a54128fSAndroid Build Coastguard Worker (dotdot < EXT2_FIRST_INO(ctx->fs->super) &&
731*6a54128fSAndroid Build Coastguard Worker dotdot != EXT2_ROOT_INO) ||
732*6a54128fSAndroid Build Coastguard Worker de.inode >= ctx->fs->super->s_inodes_count ||
733*6a54128fSAndroid Build Coastguard Worker (de.inode < EXT2_FIRST_INO(ctx->fs->super) &&
734*6a54128fSAndroid Build Coastguard Worker de.inode != 0) ||
735*6a54128fSAndroid Build Coastguard Worker rec_len2 > EXT4_MIN_INLINE_DATA_SIZE -
736*6a54128fSAndroid Build Coastguard Worker EXT4_INLINE_DATA_DOTDOT_SIZE)
737*6a54128fSAndroid Build Coastguard Worker return;
738*6a54128fSAndroid Build Coastguard Worker /* device files never have a "system.data" entry */
739*6a54128fSAndroid Build Coastguard Worker goto isdir;
740*6a54128fSAndroid Build Coastguard Worker } else if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
741*6a54128fSAndroid Build Coastguard Worker /* extent mapped */
742*6a54128fSAndroid Build Coastguard Worker if (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
743*6a54128fSAndroid Build Coastguard Worker &blk))
744*6a54128fSAndroid Build Coastguard Worker return;
745*6a54128fSAndroid Build Coastguard Worker /* device files are never extent mapped */
746*6a54128fSAndroid Build Coastguard Worker not_device++;
747*6a54128fSAndroid Build Coastguard Worker } else {
748*6a54128fSAndroid Build Coastguard Worker for (i=0; i < EXT2_N_BLOCKS; i++) {
749*6a54128fSAndroid Build Coastguard Worker blk = inode->i_block[i];
750*6a54128fSAndroid Build Coastguard Worker if (!blk)
751*6a54128fSAndroid Build Coastguard Worker continue;
752*6a54128fSAndroid Build Coastguard Worker if (i >= 4)
753*6a54128fSAndroid Build Coastguard Worker not_device++;
754*6a54128fSAndroid Build Coastguard Worker
755*6a54128fSAndroid Build Coastguard Worker if (blk < ctx->fs->super->s_first_data_block ||
756*6a54128fSAndroid Build Coastguard Worker blk >= ext2fs_blocks_count(ctx->fs->super) ||
757*6a54128fSAndroid Build Coastguard Worker ext2fs_fast_test_block_bitmap2(ctx->block_found_map,
758*6a54128fSAndroid Build Coastguard Worker blk))
759*6a54128fSAndroid Build Coastguard Worker return; /* Invalid block, can't be dir */
760*6a54128fSAndroid Build Coastguard Worker }
761*6a54128fSAndroid Build Coastguard Worker blk = inode->i_block[0];
762*6a54128fSAndroid Build Coastguard Worker }
763*6a54128fSAndroid Build Coastguard Worker
764*6a54128fSAndroid Build Coastguard Worker /*
765*6a54128fSAndroid Build Coastguard Worker * If the mode says this is a device file and the i_links_count field
766*6a54128fSAndroid Build Coastguard Worker * is sane and we have not ruled it out as a device file previously,
767*6a54128fSAndroid Build Coastguard Worker * we declare it a device file, not a directory.
768*6a54128fSAndroid Build Coastguard Worker */
769*6a54128fSAndroid Build Coastguard Worker if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
770*6a54128fSAndroid Build Coastguard Worker (inode->i_links_count == 1) && !not_device)
771*6a54128fSAndroid Build Coastguard Worker return;
772*6a54128fSAndroid Build Coastguard Worker
773*6a54128fSAndroid Build Coastguard Worker /* read the first block */
774*6a54128fSAndroid Build Coastguard Worker ehandler_operation(_("reading directory block"));
775*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_read_dir_block4(ctx->fs, blk, buf, 0, pctx->ino);
776*6a54128fSAndroid Build Coastguard Worker ehandler_operation(0);
777*6a54128fSAndroid Build Coastguard Worker if (retval)
778*6a54128fSAndroid Build Coastguard Worker return;
779*6a54128fSAndroid Build Coastguard Worker
780*6a54128fSAndroid Build Coastguard Worker dirent = (struct ext2_dir_entry *) buf;
781*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
782*6a54128fSAndroid Build Coastguard Worker if (retval)
783*6a54128fSAndroid Build Coastguard Worker return;
784*6a54128fSAndroid Build Coastguard Worker if ((ext2fs_dirent_name_len(dirent) != 1) ||
785*6a54128fSAndroid Build Coastguard Worker (dirent->name[0] != '.') ||
786*6a54128fSAndroid Build Coastguard Worker (dirent->inode != pctx->ino) ||
787*6a54128fSAndroid Build Coastguard Worker (rec_len < 12) ||
788*6a54128fSAndroid Build Coastguard Worker (rec_len % 4) ||
789*6a54128fSAndroid Build Coastguard Worker (rec_len >= ctx->fs->blocksize - 12))
790*6a54128fSAndroid Build Coastguard Worker return;
791*6a54128fSAndroid Build Coastguard Worker
792*6a54128fSAndroid Build Coastguard Worker dirent = (struct ext2_dir_entry *) (buf + rec_len);
793*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
794*6a54128fSAndroid Build Coastguard Worker if (retval)
795*6a54128fSAndroid Build Coastguard Worker return;
796*6a54128fSAndroid Build Coastguard Worker if ((ext2fs_dirent_name_len(dirent) != 2) ||
797*6a54128fSAndroid Build Coastguard Worker (dirent->name[0] != '.') ||
798*6a54128fSAndroid Build Coastguard Worker (dirent->name[1] != '.') ||
799*6a54128fSAndroid Build Coastguard Worker (rec_len < 12) ||
800*6a54128fSAndroid Build Coastguard Worker (rec_len % 4))
801*6a54128fSAndroid Build Coastguard Worker return;
802*6a54128fSAndroid Build Coastguard Worker
803*6a54128fSAndroid Build Coastguard Worker isdir:
804*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
805*6a54128fSAndroid Build Coastguard Worker inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
806*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode_full(ctx, pctx->ino, inode,
807*6a54128fSAndroid Build Coastguard Worker EXT2_INODE_SIZE(ctx->fs->super),
808*6a54128fSAndroid Build Coastguard Worker "check_is_really_dir");
809*6a54128fSAndroid Build Coastguard Worker }
810*6a54128fSAndroid Build Coastguard Worker }
811*6a54128fSAndroid Build Coastguard Worker
e2fsck_setup_icount(e2fsck_t ctx,const char * icount_name,int flags,ext2_icount_t hint,ext2_icount_t * ret)812*6a54128fSAndroid Build Coastguard Worker extern errcode_t e2fsck_setup_icount(e2fsck_t ctx, const char *icount_name,
813*6a54128fSAndroid Build Coastguard Worker int flags, ext2_icount_t hint,
814*6a54128fSAndroid Build Coastguard Worker ext2_icount_t *ret)
815*6a54128fSAndroid Build Coastguard Worker {
816*6a54128fSAndroid Build Coastguard Worker unsigned int threshold;
817*6a54128fSAndroid Build Coastguard Worker unsigned int save_type;
818*6a54128fSAndroid Build Coastguard Worker ext2_ino_t num_dirs;
819*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
820*6a54128fSAndroid Build Coastguard Worker char *tdb_dir;
821*6a54128fSAndroid Build Coastguard Worker int enable;
822*6a54128fSAndroid Build Coastguard Worker
823*6a54128fSAndroid Build Coastguard Worker *ret = 0;
824*6a54128fSAndroid Build Coastguard Worker
825*6a54128fSAndroid Build Coastguard Worker profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
826*6a54128fSAndroid Build Coastguard Worker &tdb_dir);
827*6a54128fSAndroid Build Coastguard Worker profile_get_uint(ctx->profile, "scratch_files",
828*6a54128fSAndroid Build Coastguard Worker "numdirs_threshold", 0, 0, &threshold);
829*6a54128fSAndroid Build Coastguard Worker profile_get_boolean(ctx->profile, "scratch_files",
830*6a54128fSAndroid Build Coastguard Worker "icount", 0, 1, &enable);
831*6a54128fSAndroid Build Coastguard Worker
832*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
833*6a54128fSAndroid Build Coastguard Worker if (retval)
834*6a54128fSAndroid Build Coastguard Worker num_dirs = 1024; /* Guess */
835*6a54128fSAndroid Build Coastguard Worker
836*6a54128fSAndroid Build Coastguard Worker if (enable && tdb_dir && !access(tdb_dir, W_OK) &&
837*6a54128fSAndroid Build Coastguard Worker (!threshold || num_dirs > threshold)) {
838*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir,
839*6a54128fSAndroid Build Coastguard Worker flags, ret);
840*6a54128fSAndroid Build Coastguard Worker if (retval == 0)
841*6a54128fSAndroid Build Coastguard Worker return 0;
842*6a54128fSAndroid Build Coastguard Worker }
843*6a54128fSAndroid Build Coastguard Worker e2fsck_set_bitmap_type(ctx->fs, EXT2FS_BMAP64_RBTREE, icount_name,
844*6a54128fSAndroid Build Coastguard Worker &save_type);
845*6a54128fSAndroid Build Coastguard Worker if (ctx->options & E2F_OPT_ICOUNT_FULLMAP)
846*6a54128fSAndroid Build Coastguard Worker flags |= EXT2_ICOUNT_OPT_FULLMAP;
847*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_create_icount2(ctx->fs, flags, 0, hint, ret);
848*6a54128fSAndroid Build Coastguard Worker ctx->fs->default_bitmap_type = save_type;
849*6a54128fSAndroid Build Coastguard Worker return retval;
850*6a54128fSAndroid Build Coastguard Worker }
851*6a54128fSAndroid Build Coastguard Worker
recheck_bad_inode_checksum(ext2_filsys fs,ext2_ino_t ino,e2fsck_t ctx,struct problem_context * pctx)852*6a54128fSAndroid Build Coastguard Worker static errcode_t recheck_bad_inode_checksum(ext2_filsys fs, ext2_ino_t ino,
853*6a54128fSAndroid Build Coastguard Worker e2fsck_t ctx,
854*6a54128fSAndroid Build Coastguard Worker struct problem_context *pctx)
855*6a54128fSAndroid Build Coastguard Worker {
856*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
857*6a54128fSAndroid Build Coastguard Worker struct ext2_inode_large inode;
858*6a54128fSAndroid Build Coastguard Worker
859*6a54128fSAndroid Build Coastguard Worker /*
860*6a54128fSAndroid Build Coastguard Worker * Reread inode. If we don't see checksum error, then this inode
861*6a54128fSAndroid Build Coastguard Worker * has been fixed elsewhere.
862*6a54128fSAndroid Build Coastguard Worker */
863*6a54128fSAndroid Build Coastguard Worker ctx->stashed_ino = 0;
864*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
865*6a54128fSAndroid Build Coastguard Worker sizeof(inode));
866*6a54128fSAndroid Build Coastguard Worker if (retval && retval != EXT2_ET_INODE_CSUM_INVALID)
867*6a54128fSAndroid Build Coastguard Worker return retval;
868*6a54128fSAndroid Build Coastguard Worker if (!retval)
869*6a54128fSAndroid Build Coastguard Worker return 0;
870*6a54128fSAndroid Build Coastguard Worker
871*6a54128fSAndroid Build Coastguard Worker /*
872*6a54128fSAndroid Build Coastguard Worker * Checksum still doesn't match. That implies that the inode passes
873*6a54128fSAndroid Build Coastguard Worker * all the sanity checks, so maybe the checksum is simply corrupt.
874*6a54128fSAndroid Build Coastguard Worker * See if the user will go for fixing that.
875*6a54128fSAndroid Build Coastguard Worker */
876*6a54128fSAndroid Build Coastguard Worker if (!fix_problem(ctx, PR_1_INODE_ONLY_CSUM_INVALID, pctx))
877*6a54128fSAndroid Build Coastguard Worker return 0;
878*6a54128fSAndroid Build Coastguard Worker
879*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
880*6a54128fSAndroid Build Coastguard Worker sizeof(inode));
881*6a54128fSAndroid Build Coastguard Worker return retval;
882*6a54128fSAndroid Build Coastguard Worker }
883*6a54128fSAndroid Build Coastguard Worker
reserve_block_for_root_repair(e2fsck_t ctx)884*6a54128fSAndroid Build Coastguard Worker static void reserve_block_for_root_repair(e2fsck_t ctx)
885*6a54128fSAndroid Build Coastguard Worker {
886*6a54128fSAndroid Build Coastguard Worker blk64_t blk = 0;
887*6a54128fSAndroid Build Coastguard Worker errcode_t err;
888*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs = ctx->fs;
889*6a54128fSAndroid Build Coastguard Worker
890*6a54128fSAndroid Build Coastguard Worker ctx->root_repair_block = 0;
891*6a54128fSAndroid Build Coastguard Worker if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO))
892*6a54128fSAndroid Build Coastguard Worker return;
893*6a54128fSAndroid Build Coastguard Worker
894*6a54128fSAndroid Build Coastguard Worker err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
895*6a54128fSAndroid Build Coastguard Worker if (err)
896*6a54128fSAndroid Build Coastguard Worker return;
897*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
898*6a54128fSAndroid Build Coastguard Worker ctx->root_repair_block = blk;
899*6a54128fSAndroid Build Coastguard Worker }
900*6a54128fSAndroid Build Coastguard Worker
reserve_block_for_lnf_repair(e2fsck_t ctx)901*6a54128fSAndroid Build Coastguard Worker static void reserve_block_for_lnf_repair(e2fsck_t ctx)
902*6a54128fSAndroid Build Coastguard Worker {
903*6a54128fSAndroid Build Coastguard Worker blk64_t blk = 0;
904*6a54128fSAndroid Build Coastguard Worker errcode_t err;
905*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs = ctx->fs;
906*6a54128fSAndroid Build Coastguard Worker static const char name[] = "lost+found";
907*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino;
908*6a54128fSAndroid Build Coastguard Worker
909*6a54128fSAndroid Build Coastguard Worker ctx->lnf_repair_block = 0;
910*6a54128fSAndroid Build Coastguard Worker if (!ext2fs_lookup(fs, EXT2_ROOT_INO, name, sizeof(name)-1, 0, &ino))
911*6a54128fSAndroid Build Coastguard Worker return;
912*6a54128fSAndroid Build Coastguard Worker
913*6a54128fSAndroid Build Coastguard Worker err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
914*6a54128fSAndroid Build Coastguard Worker if (err)
915*6a54128fSAndroid Build Coastguard Worker return;
916*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
917*6a54128fSAndroid Build Coastguard Worker ctx->lnf_repair_block = blk;
918*6a54128fSAndroid Build Coastguard Worker }
919*6a54128fSAndroid Build Coastguard Worker
get_inline_data_ea_size(ext2_filsys fs,ext2_ino_t ino,size_t * sz)920*6a54128fSAndroid Build Coastguard Worker static errcode_t get_inline_data_ea_size(ext2_filsys fs, ext2_ino_t ino,
921*6a54128fSAndroid Build Coastguard Worker size_t *sz)
922*6a54128fSAndroid Build Coastguard Worker {
923*6a54128fSAndroid Build Coastguard Worker void *p;
924*6a54128fSAndroid Build Coastguard Worker struct ext2_xattr_handle *handle;
925*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
926*6a54128fSAndroid Build Coastguard Worker
927*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_xattrs_open(fs, ino, &handle);
928*6a54128fSAndroid Build Coastguard Worker if (retval)
929*6a54128fSAndroid Build Coastguard Worker return retval;
930*6a54128fSAndroid Build Coastguard Worker
931*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_xattrs_read(handle);
932*6a54128fSAndroid Build Coastguard Worker if (retval)
933*6a54128fSAndroid Build Coastguard Worker goto err;
934*6a54128fSAndroid Build Coastguard Worker
935*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_xattr_get(handle, "system.data", &p, sz);
936*6a54128fSAndroid Build Coastguard Worker if (retval)
937*6a54128fSAndroid Build Coastguard Worker goto err;
938*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&p);
939*6a54128fSAndroid Build Coastguard Worker err:
940*6a54128fSAndroid Build Coastguard Worker (void) ext2fs_xattrs_close(&handle);
941*6a54128fSAndroid Build Coastguard Worker return retval;
942*6a54128fSAndroid Build Coastguard Worker }
943*6a54128fSAndroid Build Coastguard Worker
finish_processing_inode(e2fsck_t ctx,ext2_ino_t ino,struct problem_context * pctx,int failed_csum)944*6a54128fSAndroid Build Coastguard Worker static void finish_processing_inode(e2fsck_t ctx, ext2_ino_t ino,
945*6a54128fSAndroid Build Coastguard Worker struct problem_context *pctx,
946*6a54128fSAndroid Build Coastguard Worker int failed_csum)
947*6a54128fSAndroid Build Coastguard Worker {
948*6a54128fSAndroid Build Coastguard Worker if (!failed_csum)
949*6a54128fSAndroid Build Coastguard Worker return;
950*6a54128fSAndroid Build Coastguard Worker
951*6a54128fSAndroid Build Coastguard Worker /*
952*6a54128fSAndroid Build Coastguard Worker * If the inode failed the checksum and the user didn't
953*6a54128fSAndroid Build Coastguard Worker * clear the inode, test the checksum again -- if it still
954*6a54128fSAndroid Build Coastguard Worker * fails, ask the user if the checksum should be corrected.
955*6a54128fSAndroid Build Coastguard Worker */
956*6a54128fSAndroid Build Coastguard Worker pctx->errcode = recheck_bad_inode_checksum(ctx->fs, ino, ctx, pctx);
957*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode)
958*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
959*6a54128fSAndroid Build Coastguard Worker }
960*6a54128fSAndroid Build Coastguard Worker #define FINISH_INODE_LOOP(ctx, ino, pctx, failed_csum) \
961*6a54128fSAndroid Build Coastguard Worker do { \
962*6a54128fSAndroid Build Coastguard Worker finish_processing_inode((ctx), (ino), (pctx), (failed_csum)); \
963*6a54128fSAndroid Build Coastguard Worker if ((ctx)->flags & E2F_FLAG_ABORT) \
964*6a54128fSAndroid Build Coastguard Worker return; \
965*6a54128fSAndroid Build Coastguard Worker } while (0)
966*6a54128fSAndroid Build Coastguard Worker
could_be_block_map(ext2_filsys fs,struct ext2_inode * inode)967*6a54128fSAndroid Build Coastguard Worker static int could_be_block_map(ext2_filsys fs, struct ext2_inode *inode)
968*6a54128fSAndroid Build Coastguard Worker {
969*6a54128fSAndroid Build Coastguard Worker __u32 x;
970*6a54128fSAndroid Build Coastguard Worker int i;
971*6a54128fSAndroid Build Coastguard Worker
972*6a54128fSAndroid Build Coastguard Worker for (i = 0; i < EXT2_N_BLOCKS; i++) {
973*6a54128fSAndroid Build Coastguard Worker x = inode->i_block[i];
974*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
975*6a54128fSAndroid Build Coastguard Worker x = ext2fs_swab32(x);
976*6a54128fSAndroid Build Coastguard Worker #endif
977*6a54128fSAndroid Build Coastguard Worker if (x >= ext2fs_blocks_count(fs->super))
978*6a54128fSAndroid Build Coastguard Worker return 0;
979*6a54128fSAndroid Build Coastguard Worker }
980*6a54128fSAndroid Build Coastguard Worker
981*6a54128fSAndroid Build Coastguard Worker return 1;
982*6a54128fSAndroid Build Coastguard Worker }
983*6a54128fSAndroid Build Coastguard Worker
984*6a54128fSAndroid Build Coastguard Worker /*
985*6a54128fSAndroid Build Coastguard Worker * Figure out what to do with an inode that has both extents and inline data
986*6a54128fSAndroid Build Coastguard Worker * inode flags set. Returns -1 if we decide to erase the inode, 0 otherwise.
987*6a54128fSAndroid Build Coastguard Worker */
fix_inline_data_extents_file(e2fsck_t ctx,ext2_ino_t ino,struct ext2_inode * inode,int inode_size,struct problem_context * pctx)988*6a54128fSAndroid Build Coastguard Worker static int fix_inline_data_extents_file(e2fsck_t ctx,
989*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino,
990*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode,
991*6a54128fSAndroid Build Coastguard Worker int inode_size,
992*6a54128fSAndroid Build Coastguard Worker struct problem_context *pctx)
993*6a54128fSAndroid Build Coastguard Worker {
994*6a54128fSAndroid Build Coastguard Worker size_t max_inline_ea_size;
995*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs = ctx->fs;
996*6a54128fSAndroid Build Coastguard Worker int dirty = 0;
997*6a54128fSAndroid Build Coastguard Worker
998*6a54128fSAndroid Build Coastguard Worker /* Both feature flags not set? Just run the regular checks */
999*6a54128fSAndroid Build Coastguard Worker if (!ext2fs_has_feature_extents(fs->super) &&
1000*6a54128fSAndroid Build Coastguard Worker !ext2fs_has_feature_inline_data(fs->super))
1001*6a54128fSAndroid Build Coastguard Worker return 0;
1002*6a54128fSAndroid Build Coastguard Worker
1003*6a54128fSAndroid Build Coastguard Worker /* Clear both flags if it's a special file */
1004*6a54128fSAndroid Build Coastguard Worker if (LINUX_S_ISCHR(inode->i_mode) ||
1005*6a54128fSAndroid Build Coastguard Worker LINUX_S_ISBLK(inode->i_mode) ||
1006*6a54128fSAndroid Build Coastguard Worker LINUX_S_ISFIFO(inode->i_mode) ||
1007*6a54128fSAndroid Build Coastguard Worker LINUX_S_ISSOCK(inode->i_mode)) {
1008*6a54128fSAndroid Build Coastguard Worker check_extents_inlinedata(ctx, pctx);
1009*6a54128fSAndroid Build Coastguard Worker return 0;
1010*6a54128fSAndroid Build Coastguard Worker }
1011*6a54128fSAndroid Build Coastguard Worker
1012*6a54128fSAndroid Build Coastguard Worker /* If it looks like an extent tree, try to clear inlinedata */
1013*6a54128fSAndroid Build Coastguard Worker if (ext2fs_extent_header_verify(inode->i_block,
1014*6a54128fSAndroid Build Coastguard Worker sizeof(inode->i_block)) == 0 &&
1015*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_CLEAR_INLINE_DATA_FOR_EXTENT, pctx)) {
1016*6a54128fSAndroid Build Coastguard Worker inode->i_flags &= ~EXT4_INLINE_DATA_FL;
1017*6a54128fSAndroid Build Coastguard Worker dirty = 1;
1018*6a54128fSAndroid Build Coastguard Worker goto out;
1019*6a54128fSAndroid Build Coastguard Worker }
1020*6a54128fSAndroid Build Coastguard Worker
1021*6a54128fSAndroid Build Coastguard Worker /* If it looks short enough to be inline data, try to clear extents */
1022*6a54128fSAndroid Build Coastguard Worker if (inode_size > EXT2_GOOD_OLD_INODE_SIZE)
1023*6a54128fSAndroid Build Coastguard Worker max_inline_ea_size = inode_size -
1024*6a54128fSAndroid Build Coastguard Worker (EXT2_GOOD_OLD_INODE_SIZE +
1025*6a54128fSAndroid Build Coastguard Worker ((struct ext2_inode_large *)inode)->i_extra_isize);
1026*6a54128fSAndroid Build Coastguard Worker else
1027*6a54128fSAndroid Build Coastguard Worker max_inline_ea_size = 0;
1028*6a54128fSAndroid Build Coastguard Worker if (EXT2_I_SIZE(inode) <
1029*6a54128fSAndroid Build Coastguard Worker EXT4_MIN_INLINE_DATA_SIZE + max_inline_ea_size &&
1030*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_CLEAR_EXTENT_FOR_INLINE_DATA, pctx)) {
1031*6a54128fSAndroid Build Coastguard Worker inode->i_flags &= ~EXT4_EXTENTS_FL;
1032*6a54128fSAndroid Build Coastguard Worker dirty = 1;
1033*6a54128fSAndroid Build Coastguard Worker goto out;
1034*6a54128fSAndroid Build Coastguard Worker }
1035*6a54128fSAndroid Build Coastguard Worker
1036*6a54128fSAndroid Build Coastguard Worker /*
1037*6a54128fSAndroid Build Coastguard Worker * Too big for inline data, but no evidence of extent tree -
1038*6a54128fSAndroid Build Coastguard Worker * maybe it's a block map file? If the mappings all look valid?
1039*6a54128fSAndroid Build Coastguard Worker */
1040*6a54128fSAndroid Build Coastguard Worker if (could_be_block_map(fs, inode) &&
1041*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_FLAGS, pctx)) {
1042*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
1043*6a54128fSAndroid Build Coastguard Worker int i;
1044*6a54128fSAndroid Build Coastguard Worker
1045*6a54128fSAndroid Build Coastguard Worker for (i = 0; i < EXT2_N_BLOCKS; i++)
1046*6a54128fSAndroid Build Coastguard Worker inode->i_block[i] = ext2fs_swab32(inode->i_block[i]);
1047*6a54128fSAndroid Build Coastguard Worker #endif
1048*6a54128fSAndroid Build Coastguard Worker
1049*6a54128fSAndroid Build Coastguard Worker inode->i_flags &= ~(EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL);
1050*6a54128fSAndroid Build Coastguard Worker dirty = 1;
1051*6a54128fSAndroid Build Coastguard Worker goto out;
1052*6a54128fSAndroid Build Coastguard Worker }
1053*6a54128fSAndroid Build Coastguard Worker
1054*6a54128fSAndroid Build Coastguard Worker /* Oh well, just clear the busted inode. */
1055*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_INODE, pctx)) {
1056*6a54128fSAndroid Build Coastguard Worker e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1057*6a54128fSAndroid Build Coastguard Worker return -1;
1058*6a54128fSAndroid Build Coastguard Worker }
1059*6a54128fSAndroid Build Coastguard Worker
1060*6a54128fSAndroid Build Coastguard Worker out:
1061*6a54128fSAndroid Build Coastguard Worker if (dirty)
1062*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, ino, inode, "pass1");
1063*6a54128fSAndroid Build Coastguard Worker
1064*6a54128fSAndroid Build Coastguard Worker return 0;
1065*6a54128fSAndroid Build Coastguard Worker }
1066*6a54128fSAndroid Build Coastguard Worker
pass1_readahead(e2fsck_t ctx,dgrp_t * group,ext2_ino_t * next_ino)1067*6a54128fSAndroid Build Coastguard Worker static void pass1_readahead(e2fsck_t ctx, dgrp_t *group, ext2_ino_t *next_ino)
1068*6a54128fSAndroid Build Coastguard Worker {
1069*6a54128fSAndroid Build Coastguard Worker ext2_ino_t inodes_in_group = 0, inodes_per_block, inodes_per_buffer;
1070*6a54128fSAndroid Build Coastguard Worker dgrp_t start = *group, grp;
1071*6a54128fSAndroid Build Coastguard Worker blk64_t blocks_to_read = 0;
1072*6a54128fSAndroid Build Coastguard Worker errcode_t err = EXT2_ET_INVALID_ARGUMENT;
1073*6a54128fSAndroid Build Coastguard Worker
1074*6a54128fSAndroid Build Coastguard Worker if (ctx->readahead_kb == 0)
1075*6a54128fSAndroid Build Coastguard Worker goto out;
1076*6a54128fSAndroid Build Coastguard Worker
1077*6a54128fSAndroid Build Coastguard Worker /* Keep iterating groups until we have enough to readahead */
1078*6a54128fSAndroid Build Coastguard Worker inodes_per_block = EXT2_INODES_PER_BLOCK(ctx->fs->super);
1079*6a54128fSAndroid Build Coastguard Worker for (grp = start; grp < ctx->fs->group_desc_count; grp++) {
1080*6a54128fSAndroid Build Coastguard Worker if (ext2fs_bg_flags_test(ctx->fs, grp, EXT2_BG_INODE_UNINIT))
1081*6a54128fSAndroid Build Coastguard Worker continue;
1082*6a54128fSAndroid Build Coastguard Worker inodes_in_group = ctx->fs->super->s_inodes_per_group -
1083*6a54128fSAndroid Build Coastguard Worker ext2fs_bg_itable_unused(ctx->fs, grp);
1084*6a54128fSAndroid Build Coastguard Worker blocks_to_read += (inodes_in_group + inodes_per_block - 1) /
1085*6a54128fSAndroid Build Coastguard Worker inodes_per_block;
1086*6a54128fSAndroid Build Coastguard Worker if (blocks_to_read * ctx->fs->blocksize >
1087*6a54128fSAndroid Build Coastguard Worker ctx->readahead_kb * 1024)
1088*6a54128fSAndroid Build Coastguard Worker break;
1089*6a54128fSAndroid Build Coastguard Worker }
1090*6a54128fSAndroid Build Coastguard Worker
1091*6a54128fSAndroid Build Coastguard Worker err = e2fsck_readahead(ctx->fs, E2FSCK_READA_ITABLE, start,
1092*6a54128fSAndroid Build Coastguard Worker grp - start + 1);
1093*6a54128fSAndroid Build Coastguard Worker if (err == EAGAIN) {
1094*6a54128fSAndroid Build Coastguard Worker ctx->readahead_kb /= 2;
1095*6a54128fSAndroid Build Coastguard Worker err = 0;
1096*6a54128fSAndroid Build Coastguard Worker }
1097*6a54128fSAndroid Build Coastguard Worker
1098*6a54128fSAndroid Build Coastguard Worker out:
1099*6a54128fSAndroid Build Coastguard Worker if (err) {
1100*6a54128fSAndroid Build Coastguard Worker /* Error; disable itable readahead */
1101*6a54128fSAndroid Build Coastguard Worker *group = ctx->fs->group_desc_count;
1102*6a54128fSAndroid Build Coastguard Worker *next_ino = ctx->fs->super->s_inodes_count;
1103*6a54128fSAndroid Build Coastguard Worker } else {
1104*6a54128fSAndroid Build Coastguard Worker /*
1105*6a54128fSAndroid Build Coastguard Worker * Don't do more readahead until we've reached the first inode
1106*6a54128fSAndroid Build Coastguard Worker * of the last inode scan buffer block for the last group.
1107*6a54128fSAndroid Build Coastguard Worker */
1108*6a54128fSAndroid Build Coastguard Worker *group = grp + 1;
1109*6a54128fSAndroid Build Coastguard Worker inodes_per_buffer = (ctx->inode_buffer_blocks ?
1110*6a54128fSAndroid Build Coastguard Worker ctx->inode_buffer_blocks :
1111*6a54128fSAndroid Build Coastguard Worker EXT2_INODE_SCAN_DEFAULT_BUFFER_BLOCKS) *
1112*6a54128fSAndroid Build Coastguard Worker ctx->fs->blocksize /
1113*6a54128fSAndroid Build Coastguard Worker EXT2_INODE_SIZE(ctx->fs->super);
1114*6a54128fSAndroid Build Coastguard Worker inodes_in_group--;
1115*6a54128fSAndroid Build Coastguard Worker *next_ino = inodes_in_group -
1116*6a54128fSAndroid Build Coastguard Worker (inodes_in_group % inodes_per_buffer) + 1 +
1117*6a54128fSAndroid Build Coastguard Worker (grp * ctx->fs->super->s_inodes_per_group);
1118*6a54128fSAndroid Build Coastguard Worker }
1119*6a54128fSAndroid Build Coastguard Worker }
1120*6a54128fSAndroid Build Coastguard Worker
1121*6a54128fSAndroid Build Coastguard Worker /*
1122*6a54128fSAndroid Build Coastguard Worker * Check if the passed ino is one of the used superblock quota inodes.
1123*6a54128fSAndroid Build Coastguard Worker *
1124*6a54128fSAndroid Build Coastguard Worker * Before the quota inodes were journaled, older superblock quota inodes
1125*6a54128fSAndroid Build Coastguard Worker * were just regular files in the filesystem and not reserved inodes. This
1126*6a54128fSAndroid Build Coastguard Worker * checks if the passed ino is one of the s_*_quota_inum superblock fields,
1127*6a54128fSAndroid Build Coastguard Worker * which may not always be the same as the EXT4_*_QUOTA_INO fields.
1128*6a54128fSAndroid Build Coastguard Worker */
quota_inum_is_super(struct ext2_super_block * sb,ext2_ino_t ino)1129*6a54128fSAndroid Build Coastguard Worker static int quota_inum_is_super(struct ext2_super_block *sb, ext2_ino_t ino)
1130*6a54128fSAndroid Build Coastguard Worker {
1131*6a54128fSAndroid Build Coastguard Worker enum quota_type qtype;
1132*6a54128fSAndroid Build Coastguard Worker
1133*6a54128fSAndroid Build Coastguard Worker for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1134*6a54128fSAndroid Build Coastguard Worker if (*quota_sb_inump(sb, qtype) == ino)
1135*6a54128fSAndroid Build Coastguard Worker return 1;
1136*6a54128fSAndroid Build Coastguard Worker
1137*6a54128fSAndroid Build Coastguard Worker return 0;
1138*6a54128fSAndroid Build Coastguard Worker }
1139*6a54128fSAndroid Build Coastguard Worker
1140*6a54128fSAndroid Build Coastguard Worker /*
1141*6a54128fSAndroid Build Coastguard Worker * Check if the passed ino is one of the reserved quota inodes.
1142*6a54128fSAndroid Build Coastguard Worker * This checks if the inode number is one of the reserved EXT4_*_QUOTA_INO
1143*6a54128fSAndroid Build Coastguard Worker * inodes. These inodes may or may not be in use by the quota feature.
1144*6a54128fSAndroid Build Coastguard Worker */
quota_inum_is_reserved(ext2_filsys fs,ext2_ino_t ino)1145*6a54128fSAndroid Build Coastguard Worker static int quota_inum_is_reserved(ext2_filsys fs, ext2_ino_t ino)
1146*6a54128fSAndroid Build Coastguard Worker {
1147*6a54128fSAndroid Build Coastguard Worker enum quota_type qtype;
1148*6a54128fSAndroid Build Coastguard Worker
1149*6a54128fSAndroid Build Coastguard Worker for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1150*6a54128fSAndroid Build Coastguard Worker if (quota_type2inum(qtype, fs->super) == ino)
1151*6a54128fSAndroid Build Coastguard Worker return 1;
1152*6a54128fSAndroid Build Coastguard Worker
1153*6a54128fSAndroid Build Coastguard Worker return 0;
1154*6a54128fSAndroid Build Coastguard Worker }
1155*6a54128fSAndroid Build Coastguard Worker
e2fsck_pass1(e2fsck_t ctx)1156*6a54128fSAndroid Build Coastguard Worker void e2fsck_pass1(e2fsck_t ctx)
1157*6a54128fSAndroid Build Coastguard Worker {
1158*6a54128fSAndroid Build Coastguard Worker int i;
1159*6a54128fSAndroid Build Coastguard Worker __u64 max_sizes;
1160*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs = ctx->fs;
1161*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino = 0;
1162*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode = NULL;
1163*6a54128fSAndroid Build Coastguard Worker ext2_inode_scan scan = NULL;
1164*6a54128fSAndroid Build Coastguard Worker char *block_buf = NULL;
1165*6a54128fSAndroid Build Coastguard Worker #ifdef RESOURCE_TRACK
1166*6a54128fSAndroid Build Coastguard Worker struct resource_track rtrack;
1167*6a54128fSAndroid Build Coastguard Worker #endif
1168*6a54128fSAndroid Build Coastguard Worker unsigned char frag, fsize;
1169*6a54128fSAndroid Build Coastguard Worker struct problem_context pctx;
1170*6a54128fSAndroid Build Coastguard Worker struct scan_callback_struct scan_struct;
1171*6a54128fSAndroid Build Coastguard Worker struct ext2_super_block *sb = ctx->fs->super;
1172*6a54128fSAndroid Build Coastguard Worker const char *old_op;
1173*6a54128fSAndroid Build Coastguard Worker const char *eop_next_inode = _("getting next inode from scan");
1174*6a54128fSAndroid Build Coastguard Worker int imagic_fs, extent_fs, inlinedata_fs, casefold_fs;
1175*6a54128fSAndroid Build Coastguard Worker int low_dtime_check = 1;
1176*6a54128fSAndroid Build Coastguard Worker unsigned int inode_size = EXT2_INODE_SIZE(fs->super);
1177*6a54128fSAndroid Build Coastguard Worker unsigned int bufsize;
1178*6a54128fSAndroid Build Coastguard Worker int failed_csum = 0;
1179*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino_threshold = 0;
1180*6a54128fSAndroid Build Coastguard Worker dgrp_t ra_group = 0;
1181*6a54128fSAndroid Build Coastguard Worker struct ea_quota ea_ibody_quota;
1182*6a54128fSAndroid Build Coastguard Worker
1183*6a54128fSAndroid Build Coastguard Worker init_resource_track(&rtrack, ctx->fs->io);
1184*6a54128fSAndroid Build Coastguard Worker clear_problem_context(&pctx);
1185*6a54128fSAndroid Build Coastguard Worker
1186*6a54128fSAndroid Build Coastguard Worker /* If we can do readahead, figure out how many groups to pull in. */
1187*6a54128fSAndroid Build Coastguard Worker if (!e2fsck_can_readahead(ctx->fs))
1188*6a54128fSAndroid Build Coastguard Worker ctx->readahead_kb = 0;
1189*6a54128fSAndroid Build Coastguard Worker else if (ctx->readahead_kb == ~0ULL)
1190*6a54128fSAndroid Build Coastguard Worker ctx->readahead_kb = e2fsck_guess_readahead(ctx->fs);
1191*6a54128fSAndroid Build Coastguard Worker pass1_readahead(ctx, &ra_group, &ino_threshold);
1192*6a54128fSAndroid Build Coastguard Worker
1193*6a54128fSAndroid Build Coastguard Worker if (!(ctx->options & E2F_OPT_PREEN))
1194*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
1195*6a54128fSAndroid Build Coastguard Worker
1196*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_feature_dir_index(fs->super) &&
1197*6a54128fSAndroid Build Coastguard Worker !(ctx->options & E2F_OPT_NO)) {
1198*6a54128fSAndroid Build Coastguard Worker if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
1199*6a54128fSAndroid Build Coastguard Worker ctx->dirs_to_hash = 0;
1200*6a54128fSAndroid Build Coastguard Worker }
1201*6a54128fSAndroid Build Coastguard Worker
1202*6a54128fSAndroid Build Coastguard Worker #ifdef MTRACE
1203*6a54128fSAndroid Build Coastguard Worker mtrace_print("Pass 1");
1204*6a54128fSAndroid Build Coastguard Worker #endif
1205*6a54128fSAndroid Build Coastguard Worker
1206*6a54128fSAndroid Build Coastguard Worker #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
1207*6a54128fSAndroid Build Coastguard Worker
1208*6a54128fSAndroid Build Coastguard Worker for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
1209*6a54128fSAndroid Build Coastguard Worker max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
1210*6a54128fSAndroid Build Coastguard Worker max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
1211*6a54128fSAndroid Build Coastguard Worker max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
1212*6a54128fSAndroid Build Coastguard Worker max_sizes = (max_sizes * (1UL << i));
1213*6a54128fSAndroid Build Coastguard Worker ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
1214*6a54128fSAndroid Build Coastguard Worker }
1215*6a54128fSAndroid Build Coastguard Worker #undef EXT2_BPP
1216*6a54128fSAndroid Build Coastguard Worker
1217*6a54128fSAndroid Build Coastguard Worker imagic_fs = ext2fs_has_feature_imagic_inodes(sb);
1218*6a54128fSAndroid Build Coastguard Worker extent_fs = ext2fs_has_feature_extents(sb);
1219*6a54128fSAndroid Build Coastguard Worker inlinedata_fs = ext2fs_has_feature_inline_data(sb);
1220*6a54128fSAndroid Build Coastguard Worker casefold_fs = ext2fs_has_feature_casefold(sb);
1221*6a54128fSAndroid Build Coastguard Worker
1222*6a54128fSAndroid Build Coastguard Worker /*
1223*6a54128fSAndroid Build Coastguard Worker * Allocate bitmaps structures
1224*6a54128fSAndroid Build Coastguard Worker */
1225*6a54128fSAndroid Build Coastguard Worker pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
1226*6a54128fSAndroid Build Coastguard Worker EXT2FS_BMAP64_RBTREE,
1227*6a54128fSAndroid Build Coastguard Worker "inode_used_map",
1228*6a54128fSAndroid Build Coastguard Worker &ctx->inode_used_map);
1229*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
1230*6a54128fSAndroid Build Coastguard Worker pctx.num = 1;
1231*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1232*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
1233*6a54128fSAndroid Build Coastguard Worker return;
1234*6a54128fSAndroid Build Coastguard Worker }
1235*6a54128fSAndroid Build Coastguard Worker pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1236*6a54128fSAndroid Build Coastguard Worker _("directory inode map"),
1237*6a54128fSAndroid Build Coastguard Worker EXT2FS_BMAP64_AUTODIR,
1238*6a54128fSAndroid Build Coastguard Worker "inode_dir_map", &ctx->inode_dir_map);
1239*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
1240*6a54128fSAndroid Build Coastguard Worker pctx.num = 2;
1241*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1242*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
1243*6a54128fSAndroid Build Coastguard Worker return;
1244*6a54128fSAndroid Build Coastguard Worker }
1245*6a54128fSAndroid Build Coastguard Worker pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1246*6a54128fSAndroid Build Coastguard Worker _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
1247*6a54128fSAndroid Build Coastguard Worker "inode_reg_map", &ctx->inode_reg_map);
1248*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
1249*6a54128fSAndroid Build Coastguard Worker pctx.num = 6;
1250*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1251*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
1252*6a54128fSAndroid Build Coastguard Worker return;
1253*6a54128fSAndroid Build Coastguard Worker }
1254*6a54128fSAndroid Build Coastguard Worker pctx.errcode = e2fsck_allocate_subcluster_bitmap(fs,
1255*6a54128fSAndroid Build Coastguard Worker _("in-use block map"), EXT2FS_BMAP64_RBTREE,
1256*6a54128fSAndroid Build Coastguard Worker "block_found_map", &ctx->block_found_map);
1257*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
1258*6a54128fSAndroid Build Coastguard Worker pctx.num = 1;
1259*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1260*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
1261*6a54128fSAndroid Build Coastguard Worker return;
1262*6a54128fSAndroid Build Coastguard Worker }
1263*6a54128fSAndroid Build Coastguard Worker pctx.errcode = e2fsck_allocate_block_bitmap(fs,
1264*6a54128fSAndroid Build Coastguard Worker _("metadata block map"), EXT2FS_BMAP64_RBTREE,
1265*6a54128fSAndroid Build Coastguard Worker "block_metadata_map", &ctx->block_metadata_map);
1266*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
1267*6a54128fSAndroid Build Coastguard Worker pctx.num = 1;
1268*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1269*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
1270*6a54128fSAndroid Build Coastguard Worker return;
1271*6a54128fSAndroid Build Coastguard Worker }
1272*6a54128fSAndroid Build Coastguard Worker if (casefold_fs) {
1273*6a54128fSAndroid Build Coastguard Worker pctx.errcode =
1274*6a54128fSAndroid Build Coastguard Worker e2fsck_allocate_inode_bitmap(fs,
1275*6a54128fSAndroid Build Coastguard Worker _("inode casefold map"),
1276*6a54128fSAndroid Build Coastguard Worker EXT2FS_BMAP64_RBTREE,
1277*6a54128fSAndroid Build Coastguard Worker "inode_casefold_map",
1278*6a54128fSAndroid Build Coastguard Worker &ctx->inode_casefold_map);
1279*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
1280*6a54128fSAndroid Build Coastguard Worker pctx.num = 1;
1281*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1282*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
1283*6a54128fSAndroid Build Coastguard Worker return;
1284*6a54128fSAndroid Build Coastguard Worker }
1285*6a54128fSAndroid Build Coastguard Worker }
1286*6a54128fSAndroid Build Coastguard Worker pctx.errcode = e2fsck_setup_icount(ctx, "inode_link_info", 0, NULL,
1287*6a54128fSAndroid Build Coastguard Worker &ctx->inode_link_info);
1288*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
1289*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1290*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
1291*6a54128fSAndroid Build Coastguard Worker return;
1292*6a54128fSAndroid Build Coastguard Worker }
1293*6a54128fSAndroid Build Coastguard Worker bufsize = inode_size;
1294*6a54128fSAndroid Build Coastguard Worker if (bufsize < sizeof(struct ext2_inode_large))
1295*6a54128fSAndroid Build Coastguard Worker bufsize = sizeof(struct ext2_inode_large);
1296*6a54128fSAndroid Build Coastguard Worker inode = (struct ext2_inode *)
1297*6a54128fSAndroid Build Coastguard Worker e2fsck_allocate_memory(ctx, bufsize, "scratch inode");
1298*6a54128fSAndroid Build Coastguard Worker
1299*6a54128fSAndroid Build Coastguard Worker inodes_to_process = (struct process_inode_block *)
1300*6a54128fSAndroid Build Coastguard Worker e2fsck_allocate_memory(ctx,
1301*6a54128fSAndroid Build Coastguard Worker (ctx->process_inode_size *
1302*6a54128fSAndroid Build Coastguard Worker sizeof(struct process_inode_block)),
1303*6a54128fSAndroid Build Coastguard Worker "array of inodes to process");
1304*6a54128fSAndroid Build Coastguard Worker process_inode_count = 0;
1305*6a54128fSAndroid Build Coastguard Worker
1306*6a54128fSAndroid Build Coastguard Worker pctx.errcode = ext2fs_init_dblist(fs, 0);
1307*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
1308*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
1309*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
1310*6a54128fSAndroid Build Coastguard Worker goto endit;
1311*6a54128fSAndroid Build Coastguard Worker }
1312*6a54128fSAndroid Build Coastguard Worker
1313*6a54128fSAndroid Build Coastguard Worker /*
1314*6a54128fSAndroid Build Coastguard Worker * If the last orphan field is set, clear it, since the pass1
1315*6a54128fSAndroid Build Coastguard Worker * processing will automatically find and clear the orphans.
1316*6a54128fSAndroid Build Coastguard Worker * In the future, we may want to try using the last_orphan
1317*6a54128fSAndroid Build Coastguard Worker * linked list ourselves, but for now, we clear it so that the
1318*6a54128fSAndroid Build Coastguard Worker * ext3 mount code won't get confused.
1319*6a54128fSAndroid Build Coastguard Worker */
1320*6a54128fSAndroid Build Coastguard Worker if (!(ctx->options & E2F_OPT_READONLY)) {
1321*6a54128fSAndroid Build Coastguard Worker if (fs->super->s_last_orphan) {
1322*6a54128fSAndroid Build Coastguard Worker fs->super->s_last_orphan = 0;
1323*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_super_dirty(fs);
1324*6a54128fSAndroid Build Coastguard Worker }
1325*6a54128fSAndroid Build Coastguard Worker }
1326*6a54128fSAndroid Build Coastguard Worker
1327*6a54128fSAndroid Build Coastguard Worker mark_table_blocks(ctx);
1328*6a54128fSAndroid Build Coastguard Worker pctx.errcode = ext2fs_convert_subcluster_bitmap(fs,
1329*6a54128fSAndroid Build Coastguard Worker &ctx->block_found_map);
1330*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
1331*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
1332*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
1333*6a54128fSAndroid Build Coastguard Worker goto endit;
1334*6a54128fSAndroid Build Coastguard Worker }
1335*6a54128fSAndroid Build Coastguard Worker block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
1336*6a54128fSAndroid Build Coastguard Worker "block iterate buffer");
1337*6a54128fSAndroid Build Coastguard Worker if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
1338*6a54128fSAndroid Build Coastguard Worker e2fsck_use_inode_shortcuts(ctx, 1);
1339*6a54128fSAndroid Build Coastguard Worker e2fsck_intercept_block_allocations(ctx);
1340*6a54128fSAndroid Build Coastguard Worker old_op = ehandler_operation(_("opening inode scan"));
1341*6a54128fSAndroid Build Coastguard Worker pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
1342*6a54128fSAndroid Build Coastguard Worker &scan);
1343*6a54128fSAndroid Build Coastguard Worker ehandler_operation(old_op);
1344*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
1345*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1346*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
1347*6a54128fSAndroid Build Coastguard Worker goto endit;
1348*6a54128fSAndroid Build Coastguard Worker }
1349*6a54128fSAndroid Build Coastguard Worker ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE |
1350*6a54128fSAndroid Build Coastguard Worker EXT2_SF_WARN_GARBAGE_INODES, 0);
1351*6a54128fSAndroid Build Coastguard Worker ctx->stashed_inode = inode;
1352*6a54128fSAndroid Build Coastguard Worker scan_struct.ctx = ctx;
1353*6a54128fSAndroid Build Coastguard Worker scan_struct.block_buf = block_buf;
1354*6a54128fSAndroid Build Coastguard Worker ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
1355*6a54128fSAndroid Build Coastguard Worker if (ctx->progress && ((ctx->progress)(ctx, 1, 0,
1356*6a54128fSAndroid Build Coastguard Worker ctx->fs->group_desc_count)))
1357*6a54128fSAndroid Build Coastguard Worker goto endit;
1358*6a54128fSAndroid Build Coastguard Worker if ((fs->super->s_wtime &&
1359*6a54128fSAndroid Build Coastguard Worker fs->super->s_wtime < fs->super->s_inodes_count) ||
1360*6a54128fSAndroid Build Coastguard Worker (fs->super->s_mtime &&
1361*6a54128fSAndroid Build Coastguard Worker fs->super->s_mtime < fs->super->s_inodes_count) ||
1362*6a54128fSAndroid Build Coastguard Worker (fs->super->s_mkfs_time &&
1363*6a54128fSAndroid Build Coastguard Worker fs->super->s_mkfs_time < fs->super->s_inodes_count))
1364*6a54128fSAndroid Build Coastguard Worker low_dtime_check = 0;
1365*6a54128fSAndroid Build Coastguard Worker
1366*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_feature_mmp(fs->super) &&
1367*6a54128fSAndroid Build Coastguard Worker fs->super->s_mmp_block > fs->super->s_first_data_block &&
1368*6a54128fSAndroid Build Coastguard Worker fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
1369*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_block_bitmap2(ctx->block_found_map,
1370*6a54128fSAndroid Build Coastguard Worker fs->super->s_mmp_block);
1371*6a54128fSAndroid Build Coastguard Worker
1372*6a54128fSAndroid Build Coastguard Worker /* Set up ctx->lost_and_found if possible */
1373*6a54128fSAndroid Build Coastguard Worker (void) e2fsck_get_lost_and_found(ctx, 0);
1374*6a54128fSAndroid Build Coastguard Worker
1375*6a54128fSAndroid Build Coastguard Worker while (1) {
1376*6a54128fSAndroid Build Coastguard Worker if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
1377*6a54128fSAndroid Build Coastguard Worker if (e2fsck_mmp_update(fs))
1378*6a54128fSAndroid Build Coastguard Worker fatal_error(ctx, 0);
1379*6a54128fSAndroid Build Coastguard Worker }
1380*6a54128fSAndroid Build Coastguard Worker old_op = ehandler_operation(eop_next_inode);
1381*6a54128fSAndroid Build Coastguard Worker pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
1382*6a54128fSAndroid Build Coastguard Worker inode, inode_size);
1383*6a54128fSAndroid Build Coastguard Worker if (ino > ino_threshold)
1384*6a54128fSAndroid Build Coastguard Worker pass1_readahead(ctx, &ra_group, &ino_threshold);
1385*6a54128fSAndroid Build Coastguard Worker ehandler_operation(old_op);
1386*6a54128fSAndroid Build Coastguard Worker if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1387*6a54128fSAndroid Build Coastguard Worker goto endit;
1388*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
1389*6a54128fSAndroid Build Coastguard Worker /*
1390*6a54128fSAndroid Build Coastguard Worker * If badblocks says badblocks is bad, offer to clear
1391*6a54128fSAndroid Build Coastguard Worker * the list, update the in-core bb list, and restart
1392*6a54128fSAndroid Build Coastguard Worker * the inode scan.
1393*6a54128fSAndroid Build Coastguard Worker */
1394*6a54128fSAndroid Build Coastguard Worker if (ino == EXT2_BAD_INO &&
1395*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_BADBLOCKS_IN_BADBLOCKS,
1396*6a54128fSAndroid Build Coastguard Worker &pctx)) {
1397*6a54128fSAndroid Build Coastguard Worker errcode_t err;
1398*6a54128fSAndroid Build Coastguard Worker
1399*6a54128fSAndroid Build Coastguard Worker e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1400*6a54128fSAndroid Build Coastguard Worker ext2fs_badblocks_list_free(ctx->fs->badblocks);
1401*6a54128fSAndroid Build Coastguard Worker ctx->fs->badblocks = NULL;
1402*6a54128fSAndroid Build Coastguard Worker err = ext2fs_read_bb_inode(ctx->fs,
1403*6a54128fSAndroid Build Coastguard Worker &ctx->fs->badblocks);
1404*6a54128fSAndroid Build Coastguard Worker if (err) {
1405*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ISCAN_ERROR,
1406*6a54128fSAndroid Build Coastguard Worker &pctx);
1407*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
1408*6a54128fSAndroid Build Coastguard Worker } else
1409*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_RESTART;
1410*6a54128fSAndroid Build Coastguard Worker goto endit;
1411*6a54128fSAndroid Build Coastguard Worker }
1412*6a54128fSAndroid Build Coastguard Worker if (!ctx->inode_bb_map)
1413*6a54128fSAndroid Build Coastguard Worker alloc_bb_map(ctx);
1414*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
1415*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1416*6a54128fSAndroid Build Coastguard Worker continue;
1417*6a54128fSAndroid Build Coastguard Worker }
1418*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode &&
1419*6a54128fSAndroid Build Coastguard Worker pctx.errcode != EXT2_ET_INODE_CSUM_INVALID &&
1420*6a54128fSAndroid Build Coastguard Worker pctx.errcode != EXT2_ET_INODE_IS_GARBAGE) {
1421*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1422*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
1423*6a54128fSAndroid Build Coastguard Worker goto endit;
1424*6a54128fSAndroid Build Coastguard Worker }
1425*6a54128fSAndroid Build Coastguard Worker if (!ino)
1426*6a54128fSAndroid Build Coastguard Worker break;
1427*6a54128fSAndroid Build Coastguard Worker pctx.ino = ino;
1428*6a54128fSAndroid Build Coastguard Worker pctx.inode = inode;
1429*6a54128fSAndroid Build Coastguard Worker ctx->stashed_ino = ino;
1430*6a54128fSAndroid Build Coastguard Worker
1431*6a54128fSAndroid Build Coastguard Worker /* Clear trashed inode? */
1432*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode == EXT2_ET_INODE_IS_GARBAGE &&
1433*6a54128fSAndroid Build Coastguard Worker inode->i_links_count > 0 &&
1434*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_INODE_IS_GARBAGE, &pctx)) {
1435*6a54128fSAndroid Build Coastguard Worker pctx.errcode = 0;
1436*6a54128fSAndroid Build Coastguard Worker e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1437*6a54128fSAndroid Build Coastguard Worker }
1438*6a54128fSAndroid Build Coastguard Worker failed_csum = pctx.errcode != 0;
1439*6a54128fSAndroid Build Coastguard Worker
1440*6a54128fSAndroid Build Coastguard Worker /*
1441*6a54128fSAndroid Build Coastguard Worker * Check for inodes who might have been part of the
1442*6a54128fSAndroid Build Coastguard Worker * orphaned list linked list. They should have gotten
1443*6a54128fSAndroid Build Coastguard Worker * dealt with by now, unless the list had somehow been
1444*6a54128fSAndroid Build Coastguard Worker * corrupted.
1445*6a54128fSAndroid Build Coastguard Worker *
1446*6a54128fSAndroid Build Coastguard Worker * FIXME: In the future, inodes which are still in use
1447*6a54128fSAndroid Build Coastguard Worker * (and which are therefore) pending truncation should
1448*6a54128fSAndroid Build Coastguard Worker * be handled specially. Right now we just clear the
1449*6a54128fSAndroid Build Coastguard Worker * dtime field, and the normal e2fsck handling of
1450*6a54128fSAndroid Build Coastguard Worker * inodes where i_size and the inode blocks are
1451*6a54128fSAndroid Build Coastguard Worker * inconsistent is to fix i_size, instead of releasing
1452*6a54128fSAndroid Build Coastguard Worker * the extra blocks. This won't catch the inodes that
1453*6a54128fSAndroid Build Coastguard Worker * was at the end of the orphan list, but it's better
1454*6a54128fSAndroid Build Coastguard Worker * than nothing. The right answer is that there
1455*6a54128fSAndroid Build Coastguard Worker * shouldn't be any bugs in the orphan list handling. :-)
1456*6a54128fSAndroid Build Coastguard Worker */
1457*6a54128fSAndroid Build Coastguard Worker if (inode->i_dtime && low_dtime_check &&
1458*6a54128fSAndroid Build Coastguard Worker inode->i_dtime < ctx->fs->super->s_inodes_count) {
1459*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
1460*6a54128fSAndroid Build Coastguard Worker inode->i_dtime = inode->i_links_count ?
1461*6a54128fSAndroid Build Coastguard Worker 0 : ctx->now;
1462*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, ino, inode,
1463*6a54128fSAndroid Build Coastguard Worker "pass1");
1464*6a54128fSAndroid Build Coastguard Worker failed_csum = 0;
1465*6a54128fSAndroid Build Coastguard Worker }
1466*6a54128fSAndroid Build Coastguard Worker }
1467*6a54128fSAndroid Build Coastguard Worker
1468*6a54128fSAndroid Build Coastguard Worker if (inode->i_links_count) {
1469*6a54128fSAndroid Build Coastguard Worker pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
1470*6a54128fSAndroid Build Coastguard Worker ino, inode->i_links_count);
1471*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
1472*6a54128fSAndroid Build Coastguard Worker pctx.num = inode->i_links_count;
1473*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
1474*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
1475*6a54128fSAndroid Build Coastguard Worker goto endit;
1476*6a54128fSAndroid Build Coastguard Worker }
1477*6a54128fSAndroid Build Coastguard Worker } else if ((ino >= EXT2_FIRST_INODE(fs->super)) &&
1478*6a54128fSAndroid Build Coastguard Worker !quota_inum_is_reserved(fs, ino)) {
1479*6a54128fSAndroid Build Coastguard Worker if (!inode->i_dtime && inode->i_mode) {
1480*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx,
1481*6a54128fSAndroid Build Coastguard Worker PR_1_ZERO_DTIME, &pctx)) {
1482*6a54128fSAndroid Build Coastguard Worker inode->i_dtime = ctx->now;
1483*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, ino, inode,
1484*6a54128fSAndroid Build Coastguard Worker "pass1");
1485*6a54128fSAndroid Build Coastguard Worker failed_csum = 0;
1486*6a54128fSAndroid Build Coastguard Worker }
1487*6a54128fSAndroid Build Coastguard Worker }
1488*6a54128fSAndroid Build Coastguard Worker FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1489*6a54128fSAndroid Build Coastguard Worker continue;
1490*6a54128fSAndroid Build Coastguard Worker }
1491*6a54128fSAndroid Build Coastguard Worker
1492*6a54128fSAndroid Build Coastguard Worker if ((inode->i_flags & EXT4_CASEFOLD_FL) &&
1493*6a54128fSAndroid Build Coastguard Worker ((!LINUX_S_ISDIR(inode->i_mode) &&
1494*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_CASEFOLD_NONDIR, &pctx)) ||
1495*6a54128fSAndroid Build Coastguard Worker (!casefold_fs &&
1496*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_CASEFOLD_FEATURE, &pctx)))) {
1497*6a54128fSAndroid Build Coastguard Worker inode->i_flags &= ~EXT4_CASEFOLD_FL;
1498*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, ino, inode, "pass1");
1499*6a54128fSAndroid Build Coastguard Worker }
1500*6a54128fSAndroid Build Coastguard Worker
1501*6a54128fSAndroid Build Coastguard Worker /* Conflicting inlinedata/extents inode flags? */
1502*6a54128fSAndroid Build Coastguard Worker if ((inode->i_flags & EXT4_INLINE_DATA_FL) &&
1503*6a54128fSAndroid Build Coastguard Worker (inode->i_flags & EXT4_EXTENTS_FL)) {
1504*6a54128fSAndroid Build Coastguard Worker int res = fix_inline_data_extents_file(ctx, ino, inode,
1505*6a54128fSAndroid Build Coastguard Worker inode_size,
1506*6a54128fSAndroid Build Coastguard Worker &pctx);
1507*6a54128fSAndroid Build Coastguard Worker if (res < 0) {
1508*6a54128fSAndroid Build Coastguard Worker /* skip FINISH_INODE_LOOP */
1509*6a54128fSAndroid Build Coastguard Worker continue;
1510*6a54128fSAndroid Build Coastguard Worker }
1511*6a54128fSAndroid Build Coastguard Worker }
1512*6a54128fSAndroid Build Coastguard Worker
1513*6a54128fSAndroid Build Coastguard Worker /* Test for incorrect inline_data flags settings. */
1514*6a54128fSAndroid Build Coastguard Worker if ((inode->i_flags & EXT4_INLINE_DATA_FL) && !inlinedata_fs &&
1515*6a54128fSAndroid Build Coastguard Worker (ino >= EXT2_FIRST_INODE(fs->super))) {
1516*6a54128fSAndroid Build Coastguard Worker size_t size = 0;
1517*6a54128fSAndroid Build Coastguard Worker
1518*6a54128fSAndroid Build Coastguard Worker pctx.errcode = get_inline_data_ea_size(fs, ino, &size);
1519*6a54128fSAndroid Build Coastguard Worker if (!pctx.errcode &&
1520*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_INLINE_DATA_FEATURE, &pctx)) {
1521*6a54128fSAndroid Build Coastguard Worker ext2fs_set_feature_inline_data(sb);
1522*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_super_dirty(fs);
1523*6a54128fSAndroid Build Coastguard Worker inlinedata_fs = 1;
1524*6a54128fSAndroid Build Coastguard Worker } else if (fix_problem(ctx, PR_1_INLINE_DATA_SET, &pctx)) {
1525*6a54128fSAndroid Build Coastguard Worker e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1526*6a54128fSAndroid Build Coastguard Worker /* skip FINISH_INODE_LOOP */
1527*6a54128fSAndroid Build Coastguard Worker continue;
1528*6a54128fSAndroid Build Coastguard Worker }
1529*6a54128fSAndroid Build Coastguard Worker }
1530*6a54128fSAndroid Build Coastguard Worker
1531*6a54128fSAndroid Build Coastguard Worker /* Test for inline data flag but no attr */
1532*6a54128fSAndroid Build Coastguard Worker if ((inode->i_flags & EXT4_INLINE_DATA_FL) && inlinedata_fs &&
1533*6a54128fSAndroid Build Coastguard Worker (ino >= EXT2_FIRST_INODE(fs->super))) {
1534*6a54128fSAndroid Build Coastguard Worker size_t size = 0;
1535*6a54128fSAndroid Build Coastguard Worker errcode_t err;
1536*6a54128fSAndroid Build Coastguard Worker int flags;
1537*6a54128fSAndroid Build Coastguard Worker
1538*6a54128fSAndroid Build Coastguard Worker flags = fs->flags;
1539*6a54128fSAndroid Build Coastguard Worker if (failed_csum)
1540*6a54128fSAndroid Build Coastguard Worker fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1541*6a54128fSAndroid Build Coastguard Worker err = get_inline_data_ea_size(fs, ino, &size);
1542*6a54128fSAndroid Build Coastguard Worker fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
1543*6a54128fSAndroid Build Coastguard Worker (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
1544*6a54128fSAndroid Build Coastguard Worker
1545*6a54128fSAndroid Build Coastguard Worker switch (err) {
1546*6a54128fSAndroid Build Coastguard Worker case 0:
1547*6a54128fSAndroid Build Coastguard Worker /* Everything is awesome... */
1548*6a54128fSAndroid Build Coastguard Worker break;
1549*6a54128fSAndroid Build Coastguard Worker case EXT2_ET_BAD_EA_BLOCK_NUM:
1550*6a54128fSAndroid Build Coastguard Worker case EXT2_ET_BAD_EA_HASH:
1551*6a54128fSAndroid Build Coastguard Worker case EXT2_ET_BAD_EA_HEADER:
1552*6a54128fSAndroid Build Coastguard Worker case EXT2_ET_EA_BAD_NAME_LEN:
1553*6a54128fSAndroid Build Coastguard Worker case EXT2_ET_EA_BAD_VALUE_SIZE:
1554*6a54128fSAndroid Build Coastguard Worker case EXT2_ET_EA_KEY_NOT_FOUND:
1555*6a54128fSAndroid Build Coastguard Worker case EXT2_ET_EA_NO_SPACE:
1556*6a54128fSAndroid Build Coastguard Worker case EXT2_ET_MISSING_EA_FEATURE:
1557*6a54128fSAndroid Build Coastguard Worker case EXT2_ET_INLINE_DATA_CANT_ITERATE:
1558*6a54128fSAndroid Build Coastguard Worker case EXT2_ET_INLINE_DATA_NO_BLOCK:
1559*6a54128fSAndroid Build Coastguard Worker case EXT2_ET_INLINE_DATA_NO_SPACE:
1560*6a54128fSAndroid Build Coastguard Worker case EXT2_ET_NO_INLINE_DATA:
1561*6a54128fSAndroid Build Coastguard Worker case EXT2_ET_EXT_ATTR_CSUM_INVALID:
1562*6a54128fSAndroid Build Coastguard Worker case EXT2_ET_EA_BAD_VALUE_OFFSET:
1563*6a54128fSAndroid Build Coastguard Worker case EXT2_ET_EA_INODE_CORRUPTED:
1564*6a54128fSAndroid Build Coastguard Worker /* broken EA or no system.data EA; truncate */
1565*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_INLINE_DATA_NO_ATTR,
1566*6a54128fSAndroid Build Coastguard Worker &pctx)) {
1567*6a54128fSAndroid Build Coastguard Worker err = ext2fs_inode_size_set(fs, inode, 0);
1568*6a54128fSAndroid Build Coastguard Worker if (err) {
1569*6a54128fSAndroid Build Coastguard Worker pctx.errcode = err;
1570*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
1571*6a54128fSAndroid Build Coastguard Worker goto endit;
1572*6a54128fSAndroid Build Coastguard Worker }
1573*6a54128fSAndroid Build Coastguard Worker inode->i_flags &= ~EXT4_INLINE_DATA_FL;
1574*6a54128fSAndroid Build Coastguard Worker memset(&inode->i_block, 0,
1575*6a54128fSAndroid Build Coastguard Worker sizeof(inode->i_block));
1576*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, ino, inode,
1577*6a54128fSAndroid Build Coastguard Worker "pass1");
1578*6a54128fSAndroid Build Coastguard Worker failed_csum = 0;
1579*6a54128fSAndroid Build Coastguard Worker }
1580*6a54128fSAndroid Build Coastguard Worker break;
1581*6a54128fSAndroid Build Coastguard Worker default:
1582*6a54128fSAndroid Build Coastguard Worker /* Some other kind of non-xattr error? */
1583*6a54128fSAndroid Build Coastguard Worker pctx.errcode = err;
1584*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
1585*6a54128fSAndroid Build Coastguard Worker goto endit;
1586*6a54128fSAndroid Build Coastguard Worker }
1587*6a54128fSAndroid Build Coastguard Worker }
1588*6a54128fSAndroid Build Coastguard Worker
1589*6a54128fSAndroid Build Coastguard Worker /*
1590*6a54128fSAndroid Build Coastguard Worker * Test for incorrect extent flag settings.
1591*6a54128fSAndroid Build Coastguard Worker *
1592*6a54128fSAndroid Build Coastguard Worker * On big-endian machines we must be careful:
1593*6a54128fSAndroid Build Coastguard Worker * When the inode is read, the i_block array is not swapped
1594*6a54128fSAndroid Build Coastguard Worker * if the extent flag is set. Therefore if we are testing
1595*6a54128fSAndroid Build Coastguard Worker * for or fixing a wrongly-set flag, we must potentially
1596*6a54128fSAndroid Build Coastguard Worker * (un)swap before testing, or after fixing.
1597*6a54128fSAndroid Build Coastguard Worker */
1598*6a54128fSAndroid Build Coastguard Worker
1599*6a54128fSAndroid Build Coastguard Worker /*
1600*6a54128fSAndroid Build Coastguard Worker * In this case the extents flag was set when read, so
1601*6a54128fSAndroid Build Coastguard Worker * extent_header_verify is ok. If the inode is cleared,
1602*6a54128fSAndroid Build Coastguard Worker * no need to swap... so no extra swapping here.
1603*6a54128fSAndroid Build Coastguard Worker */
1604*6a54128fSAndroid Build Coastguard Worker if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
1605*6a54128fSAndroid Build Coastguard Worker (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1606*6a54128fSAndroid Build Coastguard Worker (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
1607*6a54128fSAndroid Build Coastguard Worker if ((ext2fs_extent_header_verify(inode->i_block,
1608*6a54128fSAndroid Build Coastguard Worker sizeof(inode->i_block)) == 0) &&
1609*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
1610*6a54128fSAndroid Build Coastguard Worker ext2fs_set_feature_extents(sb);
1611*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_super_dirty(fs);
1612*6a54128fSAndroid Build Coastguard Worker extent_fs = 1;
1613*6a54128fSAndroid Build Coastguard Worker } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
1614*6a54128fSAndroid Build Coastguard Worker clear_inode:
1615*6a54128fSAndroid Build Coastguard Worker e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1616*6a54128fSAndroid Build Coastguard Worker if (ino == EXT2_BAD_INO)
1617*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
1618*6a54128fSAndroid Build Coastguard Worker ino);
1619*6a54128fSAndroid Build Coastguard Worker /* skip FINISH_INODE_LOOP */
1620*6a54128fSAndroid Build Coastguard Worker continue;
1621*6a54128fSAndroid Build Coastguard Worker }
1622*6a54128fSAndroid Build Coastguard Worker }
1623*6a54128fSAndroid Build Coastguard Worker
1624*6a54128fSAndroid Build Coastguard Worker /*
1625*6a54128fSAndroid Build Coastguard Worker * For big-endian machines:
1626*6a54128fSAndroid Build Coastguard Worker * If the inode didn't have the extents flag set when it
1627*6a54128fSAndroid Build Coastguard Worker * was read, then the i_blocks array was swapped. To test
1628*6a54128fSAndroid Build Coastguard Worker * as an extents header, we must swap it back first.
1629*6a54128fSAndroid Build Coastguard Worker * IF we then set the extents flag, the entire i_block
1630*6a54128fSAndroid Build Coastguard Worker * array must be un/re-swapped to make it proper extents data.
1631*6a54128fSAndroid Build Coastguard Worker */
1632*6a54128fSAndroid Build Coastguard Worker if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
1633*6a54128fSAndroid Build Coastguard Worker (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1634*6a54128fSAndroid Build Coastguard Worker (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
1635*6a54128fSAndroid Build Coastguard Worker (LINUX_S_ISREG(inode->i_mode) ||
1636*6a54128fSAndroid Build Coastguard Worker LINUX_S_ISDIR(inode->i_mode))) {
1637*6a54128fSAndroid Build Coastguard Worker void *ehp;
1638*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
1639*6a54128fSAndroid Build Coastguard Worker __u32 tmp_block[EXT2_N_BLOCKS];
1640*6a54128fSAndroid Build Coastguard Worker
1641*6a54128fSAndroid Build Coastguard Worker for (i = 0; i < EXT2_N_BLOCKS; i++)
1642*6a54128fSAndroid Build Coastguard Worker tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
1643*6a54128fSAndroid Build Coastguard Worker ehp = tmp_block;
1644*6a54128fSAndroid Build Coastguard Worker #else
1645*6a54128fSAndroid Build Coastguard Worker ehp = inode->i_block;
1646*6a54128fSAndroid Build Coastguard Worker #endif
1647*6a54128fSAndroid Build Coastguard Worker if ((ext2fs_extent_header_verify(ehp,
1648*6a54128fSAndroid Build Coastguard Worker sizeof(inode->i_block)) == 0) &&
1649*6a54128fSAndroid Build Coastguard Worker (fix_problem(ctx, PR_1_UNSET_EXTENT_FL, &pctx))) {
1650*6a54128fSAndroid Build Coastguard Worker inode->i_flags |= EXT4_EXTENTS_FL;
1651*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
1652*6a54128fSAndroid Build Coastguard Worker memcpy(inode->i_block, tmp_block,
1653*6a54128fSAndroid Build Coastguard Worker sizeof(inode->i_block));
1654*6a54128fSAndroid Build Coastguard Worker #endif
1655*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, ino, inode, "pass1");
1656*6a54128fSAndroid Build Coastguard Worker failed_csum = 0;
1657*6a54128fSAndroid Build Coastguard Worker }
1658*6a54128fSAndroid Build Coastguard Worker }
1659*6a54128fSAndroid Build Coastguard Worker
1660*6a54128fSAndroid Build Coastguard Worker if (ino == EXT2_BAD_INO) {
1661*6a54128fSAndroid Build Coastguard Worker struct process_block_struct pb;
1662*6a54128fSAndroid Build Coastguard Worker
1663*6a54128fSAndroid Build Coastguard Worker if ((failed_csum || inode->i_mode || inode->i_uid ||
1664*6a54128fSAndroid Build Coastguard Worker inode->i_gid || inode->i_links_count ||
1665*6a54128fSAndroid Build Coastguard Worker (inode->i_flags & EXT4_INLINE_DATA_FL) ||
1666*6a54128fSAndroid Build Coastguard Worker inode->i_file_acl) &&
1667*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
1668*6a54128fSAndroid Build Coastguard Worker memset(inode, 0, sizeof(struct ext2_inode));
1669*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, ino, inode,
1670*6a54128fSAndroid Build Coastguard Worker "clear bad inode");
1671*6a54128fSAndroid Build Coastguard Worker failed_csum = 0;
1672*6a54128fSAndroid Build Coastguard Worker }
1673*6a54128fSAndroid Build Coastguard Worker
1674*6a54128fSAndroid Build Coastguard Worker pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
1675*6a54128fSAndroid Build Coastguard Worker &pb.fs_meta_blocks);
1676*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
1677*6a54128fSAndroid Build Coastguard Worker pctx.num = 4;
1678*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1679*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
1680*6a54128fSAndroid Build Coastguard Worker goto endit;
1681*6a54128fSAndroid Build Coastguard Worker }
1682*6a54128fSAndroid Build Coastguard Worker pb.ino = EXT2_BAD_INO;
1683*6a54128fSAndroid Build Coastguard Worker pb.num_blocks = pb.last_block = 0;
1684*6a54128fSAndroid Build Coastguard Worker pb.last_db_block = -1;
1685*6a54128fSAndroid Build Coastguard Worker pb.num_illegal_blocks = 0;
1686*6a54128fSAndroid Build Coastguard Worker pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
1687*6a54128fSAndroid Build Coastguard Worker pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
1688*6a54128fSAndroid Build Coastguard Worker pb.inode = inode;
1689*6a54128fSAndroid Build Coastguard Worker pb.pctx = &pctx;
1690*6a54128fSAndroid Build Coastguard Worker pb.ctx = ctx;
1691*6a54128fSAndroid Build Coastguard Worker pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
1692*6a54128fSAndroid Build Coastguard Worker block_buf, process_bad_block, &pb);
1693*6a54128fSAndroid Build Coastguard Worker ext2fs_free_block_bitmap(pb.fs_meta_blocks);
1694*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
1695*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
1696*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
1697*6a54128fSAndroid Build Coastguard Worker goto endit;
1698*6a54128fSAndroid Build Coastguard Worker }
1699*6a54128fSAndroid Build Coastguard Worker if (pb.bbcheck)
1700*6a54128fSAndroid Build Coastguard Worker if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
1701*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
1702*6a54128fSAndroid Build Coastguard Worker goto endit;
1703*6a54128fSAndroid Build Coastguard Worker }
1704*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1705*6a54128fSAndroid Build Coastguard Worker clear_problem_context(&pctx);
1706*6a54128fSAndroid Build Coastguard Worker FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1707*6a54128fSAndroid Build Coastguard Worker continue;
1708*6a54128fSAndroid Build Coastguard Worker } else if (ino == EXT2_ROOT_INO) {
1709*6a54128fSAndroid Build Coastguard Worker /*
1710*6a54128fSAndroid Build Coastguard Worker * Make sure the root inode is a directory; if
1711*6a54128fSAndroid Build Coastguard Worker * not, offer to clear it. It will be
1712*6a54128fSAndroid Build Coastguard Worker * regenerated in pass #3.
1713*6a54128fSAndroid Build Coastguard Worker */
1714*6a54128fSAndroid Build Coastguard Worker if (!LINUX_S_ISDIR(inode->i_mode)) {
1715*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
1716*6a54128fSAndroid Build Coastguard Worker goto clear_inode;
1717*6a54128fSAndroid Build Coastguard Worker }
1718*6a54128fSAndroid Build Coastguard Worker /*
1719*6a54128fSAndroid Build Coastguard Worker * If dtime is set, offer to clear it. mke2fs
1720*6a54128fSAndroid Build Coastguard Worker * version 0.2b created filesystems with the
1721*6a54128fSAndroid Build Coastguard Worker * dtime field set for the root and lost+found
1722*6a54128fSAndroid Build Coastguard Worker * directories. We won't worry about
1723*6a54128fSAndroid Build Coastguard Worker * /lost+found, since that can be regenerated
1724*6a54128fSAndroid Build Coastguard Worker * easily. But we will fix the root directory
1725*6a54128fSAndroid Build Coastguard Worker * as a special case.
1726*6a54128fSAndroid Build Coastguard Worker */
1727*6a54128fSAndroid Build Coastguard Worker if (inode->i_dtime && inode->i_links_count) {
1728*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
1729*6a54128fSAndroid Build Coastguard Worker inode->i_dtime = 0;
1730*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, ino, inode,
1731*6a54128fSAndroid Build Coastguard Worker "pass1");
1732*6a54128fSAndroid Build Coastguard Worker failed_csum = 0;
1733*6a54128fSAndroid Build Coastguard Worker }
1734*6a54128fSAndroid Build Coastguard Worker }
1735*6a54128fSAndroid Build Coastguard Worker } else if (ino == EXT2_JOURNAL_INO) {
1736*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1737*6a54128fSAndroid Build Coastguard Worker if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
1738*6a54128fSAndroid Build Coastguard Worker if (!LINUX_S_ISREG(inode->i_mode) &&
1739*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
1740*6a54128fSAndroid Build Coastguard Worker &pctx)) {
1741*6a54128fSAndroid Build Coastguard Worker inode->i_mode = LINUX_S_IFREG;
1742*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, ino, inode,
1743*6a54128fSAndroid Build Coastguard Worker "pass1");
1744*6a54128fSAndroid Build Coastguard Worker failed_csum = 0;
1745*6a54128fSAndroid Build Coastguard Worker }
1746*6a54128fSAndroid Build Coastguard Worker check_blocks(ctx, &pctx, block_buf, NULL);
1747*6a54128fSAndroid Build Coastguard Worker FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1748*6a54128fSAndroid Build Coastguard Worker continue;
1749*6a54128fSAndroid Build Coastguard Worker }
1750*6a54128fSAndroid Build Coastguard Worker if ((inode->i_links_count ||
1751*6a54128fSAndroid Build Coastguard Worker inode->i_blocks || inode->i_block[0]) &&
1752*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
1753*6a54128fSAndroid Build Coastguard Worker &pctx)) {
1754*6a54128fSAndroid Build Coastguard Worker memset(inode, 0, inode_size);
1755*6a54128fSAndroid Build Coastguard Worker ext2fs_icount_store(ctx->inode_link_info,
1756*6a54128fSAndroid Build Coastguard Worker ino, 0);
1757*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode_full(ctx, ino, inode,
1758*6a54128fSAndroid Build Coastguard Worker inode_size, "pass1");
1759*6a54128fSAndroid Build Coastguard Worker failed_csum = 0;
1760*6a54128fSAndroid Build Coastguard Worker }
1761*6a54128fSAndroid Build Coastguard Worker } else if (quota_inum_is_reserved(fs, ino)) {
1762*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1763*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_feature_quota(fs->super) &&
1764*6a54128fSAndroid Build Coastguard Worker quota_inum_is_super(fs->super, ino)) {
1765*6a54128fSAndroid Build Coastguard Worker if (!LINUX_S_ISREG(inode->i_mode) &&
1766*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
1767*6a54128fSAndroid Build Coastguard Worker &pctx)) {
1768*6a54128fSAndroid Build Coastguard Worker inode->i_mode = LINUX_S_IFREG;
1769*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, ino, inode,
1770*6a54128fSAndroid Build Coastguard Worker "pass1");
1771*6a54128fSAndroid Build Coastguard Worker failed_csum = 0;
1772*6a54128fSAndroid Build Coastguard Worker }
1773*6a54128fSAndroid Build Coastguard Worker check_blocks(ctx, &pctx, block_buf, NULL);
1774*6a54128fSAndroid Build Coastguard Worker FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1775*6a54128fSAndroid Build Coastguard Worker continue;
1776*6a54128fSAndroid Build Coastguard Worker }
1777*6a54128fSAndroid Build Coastguard Worker if ((inode->i_links_count ||
1778*6a54128fSAndroid Build Coastguard Worker inode->i_blocks || inode->i_block[0]) &&
1779*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
1780*6a54128fSAndroid Build Coastguard Worker &pctx)) {
1781*6a54128fSAndroid Build Coastguard Worker memset(inode, 0, inode_size);
1782*6a54128fSAndroid Build Coastguard Worker ext2fs_icount_store(ctx->inode_link_info,
1783*6a54128fSAndroid Build Coastguard Worker ino, 0);
1784*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode_full(ctx, ino, inode,
1785*6a54128fSAndroid Build Coastguard Worker inode_size, "pass1");
1786*6a54128fSAndroid Build Coastguard Worker failed_csum = 0;
1787*6a54128fSAndroid Build Coastguard Worker }
1788*6a54128fSAndroid Build Coastguard Worker } else if (ino < EXT2_FIRST_INODE(fs->super)) {
1789*6a54128fSAndroid Build Coastguard Worker problem_t problem = 0;
1790*6a54128fSAndroid Build Coastguard Worker
1791*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1792*6a54128fSAndroid Build Coastguard Worker if (ino == EXT2_BOOT_LOADER_INO) {
1793*6a54128fSAndroid Build Coastguard Worker if (LINUX_S_ISDIR(inode->i_mode))
1794*6a54128fSAndroid Build Coastguard Worker problem = PR_1_RESERVED_BAD_MODE;
1795*6a54128fSAndroid Build Coastguard Worker } else if (ino == EXT2_RESIZE_INO) {
1796*6a54128fSAndroid Build Coastguard Worker if (inode->i_mode &&
1797*6a54128fSAndroid Build Coastguard Worker !LINUX_S_ISREG(inode->i_mode))
1798*6a54128fSAndroid Build Coastguard Worker problem = PR_1_RESERVED_BAD_MODE;
1799*6a54128fSAndroid Build Coastguard Worker } else {
1800*6a54128fSAndroid Build Coastguard Worker if (inode->i_mode != 0)
1801*6a54128fSAndroid Build Coastguard Worker problem = PR_1_RESERVED_BAD_MODE;
1802*6a54128fSAndroid Build Coastguard Worker }
1803*6a54128fSAndroid Build Coastguard Worker if (problem) {
1804*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, problem, &pctx)) {
1805*6a54128fSAndroid Build Coastguard Worker inode->i_mode = 0;
1806*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, ino, inode,
1807*6a54128fSAndroid Build Coastguard Worker "pass1");
1808*6a54128fSAndroid Build Coastguard Worker failed_csum = 0;
1809*6a54128fSAndroid Build Coastguard Worker }
1810*6a54128fSAndroid Build Coastguard Worker }
1811*6a54128fSAndroid Build Coastguard Worker check_blocks(ctx, &pctx, block_buf, NULL);
1812*6a54128fSAndroid Build Coastguard Worker FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1813*6a54128fSAndroid Build Coastguard Worker continue;
1814*6a54128fSAndroid Build Coastguard Worker }
1815*6a54128fSAndroid Build Coastguard Worker
1816*6a54128fSAndroid Build Coastguard Worker if (!inode->i_links_count) {
1817*6a54128fSAndroid Build Coastguard Worker FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1818*6a54128fSAndroid Build Coastguard Worker continue;
1819*6a54128fSAndroid Build Coastguard Worker }
1820*6a54128fSAndroid Build Coastguard Worker /*
1821*6a54128fSAndroid Build Coastguard Worker * n.b. 0.3c ext2fs code didn't clear i_links_count for
1822*6a54128fSAndroid Build Coastguard Worker * deleted files. Oops.
1823*6a54128fSAndroid Build Coastguard Worker *
1824*6a54128fSAndroid Build Coastguard Worker * Since all new ext2 implementations get this right,
1825*6a54128fSAndroid Build Coastguard Worker * we now assume that the case of non-zero
1826*6a54128fSAndroid Build Coastguard Worker * i_links_count and non-zero dtime means that we
1827*6a54128fSAndroid Build Coastguard Worker * should keep the file, not delete it.
1828*6a54128fSAndroid Build Coastguard Worker *
1829*6a54128fSAndroid Build Coastguard Worker */
1830*6a54128fSAndroid Build Coastguard Worker if (inode->i_dtime) {
1831*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
1832*6a54128fSAndroid Build Coastguard Worker inode->i_dtime = 0;
1833*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, ino, inode, "pass1");
1834*6a54128fSAndroid Build Coastguard Worker failed_csum = 0;
1835*6a54128fSAndroid Build Coastguard Worker }
1836*6a54128fSAndroid Build Coastguard Worker }
1837*6a54128fSAndroid Build Coastguard Worker
1838*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1839*6a54128fSAndroid Build Coastguard Worker switch (fs->super->s_creator_os) {
1840*6a54128fSAndroid Build Coastguard Worker case EXT2_OS_HURD:
1841*6a54128fSAndroid Build Coastguard Worker frag = inode->osd2.hurd2.h_i_frag;
1842*6a54128fSAndroid Build Coastguard Worker fsize = inode->osd2.hurd2.h_i_fsize;
1843*6a54128fSAndroid Build Coastguard Worker break;
1844*6a54128fSAndroid Build Coastguard Worker default:
1845*6a54128fSAndroid Build Coastguard Worker frag = fsize = 0;
1846*6a54128fSAndroid Build Coastguard Worker }
1847*6a54128fSAndroid Build Coastguard Worker
1848*6a54128fSAndroid Build Coastguard Worker if (inode->i_faddr || frag || fsize ||
1849*6a54128fSAndroid Build Coastguard Worker (!ext2fs_has_feature_largedir(fs->super) &&
1850*6a54128fSAndroid Build Coastguard Worker (LINUX_S_ISDIR(inode->i_mode) && inode->i_size_high)))
1851*6a54128fSAndroid Build Coastguard Worker mark_inode_bad(ctx, ino);
1852*6a54128fSAndroid Build Coastguard Worker if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
1853*6a54128fSAndroid Build Coastguard Worker !ext2fs_has_feature_64bit(fs->super) &&
1854*6a54128fSAndroid Build Coastguard Worker inode->osd2.linux2.l_i_file_acl_high != 0)
1855*6a54128fSAndroid Build Coastguard Worker mark_inode_bad(ctx, ino);
1856*6a54128fSAndroid Build Coastguard Worker if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
1857*6a54128fSAndroid Build Coastguard Worker !ext2fs_has_feature_huge_file(fs->super) &&
1858*6a54128fSAndroid Build Coastguard Worker (inode->osd2.linux2.l_i_blocks_hi != 0))
1859*6a54128fSAndroid Build Coastguard Worker mark_inode_bad(ctx, ino);
1860*6a54128fSAndroid Build Coastguard Worker if (inode->i_flags & EXT2_IMAGIC_FL) {
1861*6a54128fSAndroid Build Coastguard Worker if (imagic_fs) {
1862*6a54128fSAndroid Build Coastguard Worker if (!ctx->inode_imagic_map)
1863*6a54128fSAndroid Build Coastguard Worker alloc_imagic_map(ctx);
1864*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
1865*6a54128fSAndroid Build Coastguard Worker ino);
1866*6a54128fSAndroid Build Coastguard Worker } else {
1867*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
1868*6a54128fSAndroid Build Coastguard Worker inode->i_flags &= ~EXT2_IMAGIC_FL;
1869*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, ino,
1870*6a54128fSAndroid Build Coastguard Worker inode, "pass1");
1871*6a54128fSAndroid Build Coastguard Worker failed_csum = 0;
1872*6a54128fSAndroid Build Coastguard Worker }
1873*6a54128fSAndroid Build Coastguard Worker }
1874*6a54128fSAndroid Build Coastguard Worker }
1875*6a54128fSAndroid Build Coastguard Worker
1876*6a54128fSAndroid Build Coastguard Worker check_inode_extra_space(ctx, &pctx, &ea_ibody_quota);
1877*6a54128fSAndroid Build Coastguard Worker check_is_really_dir(ctx, &pctx, block_buf);
1878*6a54128fSAndroid Build Coastguard Worker
1879*6a54128fSAndroid Build Coastguard Worker /*
1880*6a54128fSAndroid Build Coastguard Worker * ext2fs_inode_has_valid_blocks2 does not actually look
1881*6a54128fSAndroid Build Coastguard Worker * at i_block[] values, so not endian-sensitive here.
1882*6a54128fSAndroid Build Coastguard Worker */
1883*6a54128fSAndroid Build Coastguard Worker if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
1884*6a54128fSAndroid Build Coastguard Worker LINUX_S_ISLNK(inode->i_mode) &&
1885*6a54128fSAndroid Build Coastguard Worker !ext2fs_inode_has_valid_blocks2(fs, inode) &&
1886*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
1887*6a54128fSAndroid Build Coastguard Worker inode->i_flags &= ~EXT4_EXTENTS_FL;
1888*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, ino, inode, "pass1");
1889*6a54128fSAndroid Build Coastguard Worker failed_csum = 0;
1890*6a54128fSAndroid Build Coastguard Worker }
1891*6a54128fSAndroid Build Coastguard Worker
1892*6a54128fSAndroid Build Coastguard Worker if ((inode->i_flags & EXT4_ENCRYPT_FL) &&
1893*6a54128fSAndroid Build Coastguard Worker add_encrypted_file(ctx, &pctx) < 0)
1894*6a54128fSAndroid Build Coastguard Worker goto clear_inode;
1895*6a54128fSAndroid Build Coastguard Worker
1896*6a54128fSAndroid Build Coastguard Worker if (casefold_fs && inode->i_flags & EXT4_CASEFOLD_FL)
1897*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_inode_bitmap2(ctx->inode_casefold_map, ino);
1898*6a54128fSAndroid Build Coastguard Worker
1899*6a54128fSAndroid Build Coastguard Worker if (LINUX_S_ISDIR(inode->i_mode)) {
1900*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
1901*6a54128fSAndroid Build Coastguard Worker e2fsck_add_dir_info(ctx, ino, 0);
1902*6a54128fSAndroid Build Coastguard Worker ctx->fs_directory_count++;
1903*6a54128fSAndroid Build Coastguard Worker if (inode->i_flags & EXT4_CASEFOLD_FL)
1904*6a54128fSAndroid Build Coastguard Worker add_casefolded_dir(ctx, ino);
1905*6a54128fSAndroid Build Coastguard Worker } else if (LINUX_S_ISREG (inode->i_mode)) {
1906*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
1907*6a54128fSAndroid Build Coastguard Worker ctx->fs_regular_count++;
1908*6a54128fSAndroid Build Coastguard Worker } else if (LINUX_S_ISCHR (inode->i_mode) &&
1909*6a54128fSAndroid Build Coastguard Worker e2fsck_pass1_check_device_inode(fs, inode)) {
1910*6a54128fSAndroid Build Coastguard Worker check_extents_inlinedata(ctx, &pctx);
1911*6a54128fSAndroid Build Coastguard Worker check_immutable(ctx, &pctx);
1912*6a54128fSAndroid Build Coastguard Worker check_size(ctx, &pctx);
1913*6a54128fSAndroid Build Coastguard Worker ctx->fs_chardev_count++;
1914*6a54128fSAndroid Build Coastguard Worker } else if (LINUX_S_ISBLK (inode->i_mode) &&
1915*6a54128fSAndroid Build Coastguard Worker e2fsck_pass1_check_device_inode(fs, inode)) {
1916*6a54128fSAndroid Build Coastguard Worker check_extents_inlinedata(ctx, &pctx);
1917*6a54128fSAndroid Build Coastguard Worker check_immutable(ctx, &pctx);
1918*6a54128fSAndroid Build Coastguard Worker check_size(ctx, &pctx);
1919*6a54128fSAndroid Build Coastguard Worker ctx->fs_blockdev_count++;
1920*6a54128fSAndroid Build Coastguard Worker } else if (LINUX_S_ISLNK (inode->i_mode) &&
1921*6a54128fSAndroid Build Coastguard Worker e2fsck_pass1_check_symlink(fs, ino, inode,
1922*6a54128fSAndroid Build Coastguard Worker block_buf)) {
1923*6a54128fSAndroid Build Coastguard Worker check_immutable(ctx, &pctx);
1924*6a54128fSAndroid Build Coastguard Worker ctx->fs_symlinks_count++;
1925*6a54128fSAndroid Build Coastguard Worker if (inode->i_flags & EXT4_INLINE_DATA_FL) {
1926*6a54128fSAndroid Build Coastguard Worker FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1927*6a54128fSAndroid Build Coastguard Worker continue;
1928*6a54128fSAndroid Build Coastguard Worker } else if (ext2fs_is_fast_symlink(inode)) {
1929*6a54128fSAndroid Build Coastguard Worker ctx->fs_fast_symlinks_count++;
1930*6a54128fSAndroid Build Coastguard Worker check_blocks(ctx, &pctx, block_buf,
1931*6a54128fSAndroid Build Coastguard Worker &ea_ibody_quota);
1932*6a54128fSAndroid Build Coastguard Worker FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1933*6a54128fSAndroid Build Coastguard Worker continue;
1934*6a54128fSAndroid Build Coastguard Worker }
1935*6a54128fSAndroid Build Coastguard Worker }
1936*6a54128fSAndroid Build Coastguard Worker else if (LINUX_S_ISFIFO (inode->i_mode) &&
1937*6a54128fSAndroid Build Coastguard Worker e2fsck_pass1_check_device_inode(fs, inode)) {
1938*6a54128fSAndroid Build Coastguard Worker check_extents_inlinedata(ctx, &pctx);
1939*6a54128fSAndroid Build Coastguard Worker check_immutable(ctx, &pctx);
1940*6a54128fSAndroid Build Coastguard Worker check_size(ctx, &pctx);
1941*6a54128fSAndroid Build Coastguard Worker ctx->fs_fifo_count++;
1942*6a54128fSAndroid Build Coastguard Worker } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
1943*6a54128fSAndroid Build Coastguard Worker e2fsck_pass1_check_device_inode(fs, inode)) {
1944*6a54128fSAndroid Build Coastguard Worker check_extents_inlinedata(ctx, &pctx);
1945*6a54128fSAndroid Build Coastguard Worker check_immutable(ctx, &pctx);
1946*6a54128fSAndroid Build Coastguard Worker check_size(ctx, &pctx);
1947*6a54128fSAndroid Build Coastguard Worker ctx->fs_sockets_count++;
1948*6a54128fSAndroid Build Coastguard Worker } else
1949*6a54128fSAndroid Build Coastguard Worker mark_inode_bad(ctx, ino);
1950*6a54128fSAndroid Build Coastguard Worker if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1951*6a54128fSAndroid Build Coastguard Worker !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
1952*6a54128fSAndroid Build Coastguard Worker if (inode->i_block[EXT2_IND_BLOCK])
1953*6a54128fSAndroid Build Coastguard Worker ctx->fs_ind_count++;
1954*6a54128fSAndroid Build Coastguard Worker if (inode->i_block[EXT2_DIND_BLOCK])
1955*6a54128fSAndroid Build Coastguard Worker ctx->fs_dind_count++;
1956*6a54128fSAndroid Build Coastguard Worker if (inode->i_block[EXT2_TIND_BLOCK])
1957*6a54128fSAndroid Build Coastguard Worker ctx->fs_tind_count++;
1958*6a54128fSAndroid Build Coastguard Worker }
1959*6a54128fSAndroid Build Coastguard Worker if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1960*6a54128fSAndroid Build Coastguard Worker !(inode->i_flags & EXT4_INLINE_DATA_FL) &&
1961*6a54128fSAndroid Build Coastguard Worker (inode->i_block[EXT2_IND_BLOCK] ||
1962*6a54128fSAndroid Build Coastguard Worker inode->i_block[EXT2_DIND_BLOCK] ||
1963*6a54128fSAndroid Build Coastguard Worker inode->i_block[EXT2_TIND_BLOCK] ||
1964*6a54128fSAndroid Build Coastguard Worker ext2fs_file_acl_block(fs, inode))) {
1965*6a54128fSAndroid Build Coastguard Worker struct process_inode_block *itp;
1966*6a54128fSAndroid Build Coastguard Worker
1967*6a54128fSAndroid Build Coastguard Worker itp = &inodes_to_process[process_inode_count];
1968*6a54128fSAndroid Build Coastguard Worker itp->ino = ino;
1969*6a54128fSAndroid Build Coastguard Worker itp->ea_ibody_quota = ea_ibody_quota;
1970*6a54128fSAndroid Build Coastguard Worker if (inode_size < sizeof(struct ext2_inode_large))
1971*6a54128fSAndroid Build Coastguard Worker memcpy(&itp->inode, inode, inode_size);
1972*6a54128fSAndroid Build Coastguard Worker else
1973*6a54128fSAndroid Build Coastguard Worker memcpy(&itp->inode, inode, sizeof(itp->inode));
1974*6a54128fSAndroid Build Coastguard Worker process_inode_count++;
1975*6a54128fSAndroid Build Coastguard Worker } else
1976*6a54128fSAndroid Build Coastguard Worker check_blocks(ctx, &pctx, block_buf, &ea_ibody_quota);
1977*6a54128fSAndroid Build Coastguard Worker
1978*6a54128fSAndroid Build Coastguard Worker FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1979*6a54128fSAndroid Build Coastguard Worker
1980*6a54128fSAndroid Build Coastguard Worker if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1981*6a54128fSAndroid Build Coastguard Worker goto endit;
1982*6a54128fSAndroid Build Coastguard Worker
1983*6a54128fSAndroid Build Coastguard Worker if (process_inode_count >= ctx->process_inode_size) {
1984*6a54128fSAndroid Build Coastguard Worker process_inodes(ctx, block_buf);
1985*6a54128fSAndroid Build Coastguard Worker
1986*6a54128fSAndroid Build Coastguard Worker if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1987*6a54128fSAndroid Build Coastguard Worker goto endit;
1988*6a54128fSAndroid Build Coastguard Worker }
1989*6a54128fSAndroid Build Coastguard Worker }
1990*6a54128fSAndroid Build Coastguard Worker process_inodes(ctx, block_buf);
1991*6a54128fSAndroid Build Coastguard Worker ext2fs_close_inode_scan(scan);
1992*6a54128fSAndroid Build Coastguard Worker scan = NULL;
1993*6a54128fSAndroid Build Coastguard Worker
1994*6a54128fSAndroid Build Coastguard Worker reserve_block_for_root_repair(ctx);
1995*6a54128fSAndroid Build Coastguard Worker reserve_block_for_lnf_repair(ctx);
1996*6a54128fSAndroid Build Coastguard Worker
1997*6a54128fSAndroid Build Coastguard Worker /*
1998*6a54128fSAndroid Build Coastguard Worker * If any extended attribute blocks' reference counts need to
1999*6a54128fSAndroid Build Coastguard Worker * be adjusted, either up (ctx->refcount_extra), or down
2000*6a54128fSAndroid Build Coastguard Worker * (ctx->refcount), then fix them.
2001*6a54128fSAndroid Build Coastguard Worker */
2002*6a54128fSAndroid Build Coastguard Worker if (ctx->refcount) {
2003*6a54128fSAndroid Build Coastguard Worker adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
2004*6a54128fSAndroid Build Coastguard Worker ea_refcount_free(ctx->refcount);
2005*6a54128fSAndroid Build Coastguard Worker ctx->refcount = 0;
2006*6a54128fSAndroid Build Coastguard Worker }
2007*6a54128fSAndroid Build Coastguard Worker if (ctx->refcount_extra) {
2008*6a54128fSAndroid Build Coastguard Worker adjust_extattr_refcount(ctx, ctx->refcount_extra,
2009*6a54128fSAndroid Build Coastguard Worker block_buf, +1);
2010*6a54128fSAndroid Build Coastguard Worker ea_refcount_free(ctx->refcount_extra);
2011*6a54128fSAndroid Build Coastguard Worker ctx->refcount_extra = 0;
2012*6a54128fSAndroid Build Coastguard Worker }
2013*6a54128fSAndroid Build Coastguard Worker
2014*6a54128fSAndroid Build Coastguard Worker if (ctx->ea_block_quota_blocks) {
2015*6a54128fSAndroid Build Coastguard Worker ea_refcount_free(ctx->ea_block_quota_blocks);
2016*6a54128fSAndroid Build Coastguard Worker ctx->ea_block_quota_blocks = 0;
2017*6a54128fSAndroid Build Coastguard Worker }
2018*6a54128fSAndroid Build Coastguard Worker
2019*6a54128fSAndroid Build Coastguard Worker if (ctx->ea_block_quota_inodes) {
2020*6a54128fSAndroid Build Coastguard Worker ea_refcount_free(ctx->ea_block_quota_inodes);
2021*6a54128fSAndroid Build Coastguard Worker ctx->ea_block_quota_inodes = 0;
2022*6a54128fSAndroid Build Coastguard Worker }
2023*6a54128fSAndroid Build Coastguard Worker
2024*6a54128fSAndroid Build Coastguard Worker if (ctx->invalid_bitmaps)
2025*6a54128fSAndroid Build Coastguard Worker handle_fs_bad_blocks(ctx);
2026*6a54128fSAndroid Build Coastguard Worker
2027*6a54128fSAndroid Build Coastguard Worker /* We don't need the block_ea_map any more */
2028*6a54128fSAndroid Build Coastguard Worker if (ctx->block_ea_map) {
2029*6a54128fSAndroid Build Coastguard Worker ext2fs_free_block_bitmap(ctx->block_ea_map);
2030*6a54128fSAndroid Build Coastguard Worker ctx->block_ea_map = 0;
2031*6a54128fSAndroid Build Coastguard Worker }
2032*6a54128fSAndroid Build Coastguard Worker
2033*6a54128fSAndroid Build Coastguard Worker /* We don't need the encryption policy => ID map any more */
2034*6a54128fSAndroid Build Coastguard Worker destroy_encryption_policy_map(ctx);
2035*6a54128fSAndroid Build Coastguard Worker
2036*6a54128fSAndroid Build Coastguard Worker if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
2037*6a54128fSAndroid Build Coastguard Worker clear_problem_context(&pctx);
2038*6a54128fSAndroid Build Coastguard Worker pctx.errcode = ext2fs_create_resize_inode(fs);
2039*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
2040*6a54128fSAndroid Build Coastguard Worker if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
2041*6a54128fSAndroid Build Coastguard Worker &pctx)) {
2042*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
2043*6a54128fSAndroid Build Coastguard Worker goto endit;
2044*6a54128fSAndroid Build Coastguard Worker }
2045*6a54128fSAndroid Build Coastguard Worker pctx.errcode = 0;
2046*6a54128fSAndroid Build Coastguard Worker }
2047*6a54128fSAndroid Build Coastguard Worker if (!pctx.errcode) {
2048*6a54128fSAndroid Build Coastguard Worker e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
2049*6a54128fSAndroid Build Coastguard Worker "recreate inode");
2050*6a54128fSAndroid Build Coastguard Worker inode->i_mtime = ctx->now;
2051*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
2052*6a54128fSAndroid Build Coastguard Worker "recreate inode");
2053*6a54128fSAndroid Build Coastguard Worker }
2054*6a54128fSAndroid Build Coastguard Worker ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
2055*6a54128fSAndroid Build Coastguard Worker }
2056*6a54128fSAndroid Build Coastguard Worker
2057*6a54128fSAndroid Build Coastguard Worker if (ctx->flags & E2F_FLAG_RESTART) {
2058*6a54128fSAndroid Build Coastguard Worker /*
2059*6a54128fSAndroid Build Coastguard Worker * Only the master copy of the superblock and block
2060*6a54128fSAndroid Build Coastguard Worker * group descriptors are going to be written during a
2061*6a54128fSAndroid Build Coastguard Worker * restart, so set the superblock to be used to be the
2062*6a54128fSAndroid Build Coastguard Worker * master superblock.
2063*6a54128fSAndroid Build Coastguard Worker */
2064*6a54128fSAndroid Build Coastguard Worker ctx->use_superblock = 0;
2065*6a54128fSAndroid Build Coastguard Worker goto endit;
2066*6a54128fSAndroid Build Coastguard Worker }
2067*6a54128fSAndroid Build Coastguard Worker
2068*6a54128fSAndroid Build Coastguard Worker if (ctx->large_dirs && !ext2fs_has_feature_largedir(fs->super)) {
2069*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_2_FEATURE_LARGE_DIRS, &pctx)) {
2070*6a54128fSAndroid Build Coastguard Worker ext2fs_set_feature_largedir(fs->super);
2071*6a54128fSAndroid Build Coastguard Worker fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2072*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_super_dirty(fs);
2073*6a54128fSAndroid Build Coastguard Worker }
2074*6a54128fSAndroid Build Coastguard Worker if (fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
2075*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
2076*6a54128fSAndroid Build Coastguard Worker ext2fs_update_dynamic_rev(fs);
2077*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_super_dirty(fs);
2078*6a54128fSAndroid Build Coastguard Worker }
2079*6a54128fSAndroid Build Coastguard Worker }
2080*6a54128fSAndroid Build Coastguard Worker
2081*6a54128fSAndroid Build Coastguard Worker if (ctx->block_dup_map) {
2082*6a54128fSAndroid Build Coastguard Worker if (ctx->options & E2F_OPT_PREEN) {
2083*6a54128fSAndroid Build Coastguard Worker clear_problem_context(&pctx);
2084*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
2085*6a54128fSAndroid Build Coastguard Worker }
2086*6a54128fSAndroid Build Coastguard Worker e2fsck_pass1_dupblocks(ctx, block_buf);
2087*6a54128fSAndroid Build Coastguard Worker }
2088*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ALLOC_OK;
2089*6a54128fSAndroid Build Coastguard Worker endit:
2090*6a54128fSAndroid Build Coastguard Worker e2fsck_use_inode_shortcuts(ctx, 0);
2091*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&inodes_to_process);
2092*6a54128fSAndroid Build Coastguard Worker inodes_to_process = 0;
2093*6a54128fSAndroid Build Coastguard Worker
2094*6a54128fSAndroid Build Coastguard Worker if (scan)
2095*6a54128fSAndroid Build Coastguard Worker ext2fs_close_inode_scan(scan);
2096*6a54128fSAndroid Build Coastguard Worker if (block_buf)
2097*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&block_buf);
2098*6a54128fSAndroid Build Coastguard Worker if (inode)
2099*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&inode);
2100*6a54128fSAndroid Build Coastguard Worker
2101*6a54128fSAndroid Build Coastguard Worker /*
2102*6a54128fSAndroid Build Coastguard Worker * The l+f inode may have been cleared, so zap it now and
2103*6a54128fSAndroid Build Coastguard Worker * later passes will recalculate it if necessary
2104*6a54128fSAndroid Build Coastguard Worker */
2105*6a54128fSAndroid Build Coastguard Worker ctx->lost_and_found = 0;
2106*6a54128fSAndroid Build Coastguard Worker
2107*6a54128fSAndroid Build Coastguard Worker if ((ctx->flags & E2F_FLAG_SIGNAL_MASK) == 0)
2108*6a54128fSAndroid Build Coastguard Worker print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
2109*6a54128fSAndroid Build Coastguard Worker else
2110*6a54128fSAndroid Build Coastguard Worker ctx->invalid_bitmaps++;
2111*6a54128fSAndroid Build Coastguard Worker }
2112*6a54128fSAndroid Build Coastguard Worker #undef FINISH_INODE_LOOP
2113*6a54128fSAndroid Build Coastguard Worker
2114*6a54128fSAndroid Build Coastguard Worker /*
2115*6a54128fSAndroid Build Coastguard Worker * When the inode_scan routines call this callback at the end of the
2116*6a54128fSAndroid Build Coastguard Worker * glock group, call process_inodes.
2117*6a54128fSAndroid Build Coastguard Worker */
scan_callback(ext2_filsys fs,ext2_inode_scan scan EXT2FS_ATTR ((unused)),dgrp_t group,void * priv_data)2118*6a54128fSAndroid Build Coastguard Worker static errcode_t scan_callback(ext2_filsys fs,
2119*6a54128fSAndroid Build Coastguard Worker ext2_inode_scan scan EXT2FS_ATTR((unused)),
2120*6a54128fSAndroid Build Coastguard Worker dgrp_t group, void * priv_data)
2121*6a54128fSAndroid Build Coastguard Worker {
2122*6a54128fSAndroid Build Coastguard Worker struct scan_callback_struct *scan_struct;
2123*6a54128fSAndroid Build Coastguard Worker e2fsck_t ctx;
2124*6a54128fSAndroid Build Coastguard Worker
2125*6a54128fSAndroid Build Coastguard Worker scan_struct = (struct scan_callback_struct *) priv_data;
2126*6a54128fSAndroid Build Coastguard Worker ctx = scan_struct->ctx;
2127*6a54128fSAndroid Build Coastguard Worker
2128*6a54128fSAndroid Build Coastguard Worker process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
2129*6a54128fSAndroid Build Coastguard Worker
2130*6a54128fSAndroid Build Coastguard Worker if (ctx->progress)
2131*6a54128fSAndroid Build Coastguard Worker if ((ctx->progress)(ctx, 1, group+1,
2132*6a54128fSAndroid Build Coastguard Worker ctx->fs->group_desc_count))
2133*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_CANCEL_REQUESTED;
2134*6a54128fSAndroid Build Coastguard Worker
2135*6a54128fSAndroid Build Coastguard Worker return 0;
2136*6a54128fSAndroid Build Coastguard Worker }
2137*6a54128fSAndroid Build Coastguard Worker
2138*6a54128fSAndroid Build Coastguard Worker /*
2139*6a54128fSAndroid Build Coastguard Worker * Process the inodes in the "inodes to process" list.
2140*6a54128fSAndroid Build Coastguard Worker */
process_inodes(e2fsck_t ctx,char * block_buf)2141*6a54128fSAndroid Build Coastguard Worker static void process_inodes(e2fsck_t ctx, char *block_buf)
2142*6a54128fSAndroid Build Coastguard Worker {
2143*6a54128fSAndroid Build Coastguard Worker int i;
2144*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *old_stashed_inode;
2145*6a54128fSAndroid Build Coastguard Worker ext2_ino_t old_stashed_ino;
2146*6a54128fSAndroid Build Coastguard Worker const char *old_operation;
2147*6a54128fSAndroid Build Coastguard Worker char buf[80];
2148*6a54128fSAndroid Build Coastguard Worker struct problem_context pctx;
2149*6a54128fSAndroid Build Coastguard Worker
2150*6a54128fSAndroid Build Coastguard Worker #if 0
2151*6a54128fSAndroid Build Coastguard Worker printf("begin process_inodes: ");
2152*6a54128fSAndroid Build Coastguard Worker #endif
2153*6a54128fSAndroid Build Coastguard Worker if (process_inode_count == 0)
2154*6a54128fSAndroid Build Coastguard Worker return;
2155*6a54128fSAndroid Build Coastguard Worker old_operation = ehandler_operation(0);
2156*6a54128fSAndroid Build Coastguard Worker old_stashed_inode = ctx->stashed_inode;
2157*6a54128fSAndroid Build Coastguard Worker old_stashed_ino = ctx->stashed_ino;
2158*6a54128fSAndroid Build Coastguard Worker qsort(inodes_to_process, process_inode_count,
2159*6a54128fSAndroid Build Coastguard Worker sizeof(struct process_inode_block), process_inode_cmp);
2160*6a54128fSAndroid Build Coastguard Worker clear_problem_context(&pctx);
2161*6a54128fSAndroid Build Coastguard Worker for (i=0; i < process_inode_count; i++) {
2162*6a54128fSAndroid Build Coastguard Worker pctx.inode = ctx->stashed_inode =
2163*6a54128fSAndroid Build Coastguard Worker (struct ext2_inode *) &inodes_to_process[i].inode;
2164*6a54128fSAndroid Build Coastguard Worker pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
2165*6a54128fSAndroid Build Coastguard Worker
2166*6a54128fSAndroid Build Coastguard Worker #if 0
2167*6a54128fSAndroid Build Coastguard Worker printf("%u ", pctx.ino);
2168*6a54128fSAndroid Build Coastguard Worker #endif
2169*6a54128fSAndroid Build Coastguard Worker sprintf(buf, _("reading indirect blocks of inode %u"),
2170*6a54128fSAndroid Build Coastguard Worker pctx.ino);
2171*6a54128fSAndroid Build Coastguard Worker ehandler_operation(buf);
2172*6a54128fSAndroid Build Coastguard Worker check_blocks(ctx, &pctx, block_buf,
2173*6a54128fSAndroid Build Coastguard Worker &inodes_to_process[i].ea_ibody_quota);
2174*6a54128fSAndroid Build Coastguard Worker if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2175*6a54128fSAndroid Build Coastguard Worker break;
2176*6a54128fSAndroid Build Coastguard Worker }
2177*6a54128fSAndroid Build Coastguard Worker ctx->stashed_inode = old_stashed_inode;
2178*6a54128fSAndroid Build Coastguard Worker ctx->stashed_ino = old_stashed_ino;
2179*6a54128fSAndroid Build Coastguard Worker process_inode_count = 0;
2180*6a54128fSAndroid Build Coastguard Worker #if 0
2181*6a54128fSAndroid Build Coastguard Worker printf("end process inodes\n");
2182*6a54128fSAndroid Build Coastguard Worker #endif
2183*6a54128fSAndroid Build Coastguard Worker ehandler_operation(old_operation);
2184*6a54128fSAndroid Build Coastguard Worker }
2185*6a54128fSAndroid Build Coastguard Worker
process_inode_cmp(const void * a,const void * b)2186*6a54128fSAndroid Build Coastguard Worker static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
2187*6a54128fSAndroid Build Coastguard Worker {
2188*6a54128fSAndroid Build Coastguard Worker const struct process_inode_block *ib_a =
2189*6a54128fSAndroid Build Coastguard Worker (const struct process_inode_block *) a;
2190*6a54128fSAndroid Build Coastguard Worker const struct process_inode_block *ib_b =
2191*6a54128fSAndroid Build Coastguard Worker (const struct process_inode_block *) b;
2192*6a54128fSAndroid Build Coastguard Worker int ret;
2193*6a54128fSAndroid Build Coastguard Worker
2194*6a54128fSAndroid Build Coastguard Worker ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
2195*6a54128fSAndroid Build Coastguard Worker ib_b->inode.i_block[EXT2_IND_BLOCK]);
2196*6a54128fSAndroid Build Coastguard Worker if (ret == 0)
2197*6a54128fSAndroid Build Coastguard Worker /*
2198*6a54128fSAndroid Build Coastguard Worker * We only call process_inodes() for non-extent
2199*6a54128fSAndroid Build Coastguard Worker * inodes, so it's OK to pass NULL to
2200*6a54128fSAndroid Build Coastguard Worker * ext2fs_file_acl_block() here.
2201*6a54128fSAndroid Build Coastguard Worker */
2202*6a54128fSAndroid Build Coastguard Worker ret = ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_a->inode)) -
2203*6a54128fSAndroid Build Coastguard Worker ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_b->inode));
2204*6a54128fSAndroid Build Coastguard Worker if (ret == 0)
2205*6a54128fSAndroid Build Coastguard Worker ret = ib_a->ino - ib_b->ino;
2206*6a54128fSAndroid Build Coastguard Worker return ret;
2207*6a54128fSAndroid Build Coastguard Worker }
2208*6a54128fSAndroid Build Coastguard Worker
2209*6a54128fSAndroid Build Coastguard Worker /*
2210*6a54128fSAndroid Build Coastguard Worker * Mark an inode as being bad in some what
2211*6a54128fSAndroid Build Coastguard Worker */
mark_inode_bad(e2fsck_t ctx,ext2_ino_t ino)2212*6a54128fSAndroid Build Coastguard Worker static void mark_inode_bad(e2fsck_t ctx, ext2_ino_t ino)
2213*6a54128fSAndroid Build Coastguard Worker {
2214*6a54128fSAndroid Build Coastguard Worker struct problem_context pctx;
2215*6a54128fSAndroid Build Coastguard Worker
2216*6a54128fSAndroid Build Coastguard Worker if (!ctx->inode_bad_map) {
2217*6a54128fSAndroid Build Coastguard Worker clear_problem_context(&pctx);
2218*6a54128fSAndroid Build Coastguard Worker
2219*6a54128fSAndroid Build Coastguard Worker pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
2220*6a54128fSAndroid Build Coastguard Worker _("bad inode map"), EXT2FS_BMAP64_RBTREE,
2221*6a54128fSAndroid Build Coastguard Worker "inode_bad_map", &ctx->inode_bad_map);
2222*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
2223*6a54128fSAndroid Build Coastguard Worker pctx.num = 3;
2224*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
2225*6a54128fSAndroid Build Coastguard Worker /* Should never get here */
2226*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
2227*6a54128fSAndroid Build Coastguard Worker return;
2228*6a54128fSAndroid Build Coastguard Worker }
2229*6a54128fSAndroid Build Coastguard Worker }
2230*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_inode_bitmap2(ctx->inode_bad_map, ino);
2231*6a54128fSAndroid Build Coastguard Worker }
2232*6a54128fSAndroid Build Coastguard Worker
add_casefolded_dir(e2fsck_t ctx,ext2_ino_t ino)2233*6a54128fSAndroid Build Coastguard Worker static void add_casefolded_dir(e2fsck_t ctx, ext2_ino_t ino)
2234*6a54128fSAndroid Build Coastguard Worker {
2235*6a54128fSAndroid Build Coastguard Worker struct problem_context pctx;
2236*6a54128fSAndroid Build Coastguard Worker
2237*6a54128fSAndroid Build Coastguard Worker if (!ctx->casefolded_dirs) {
2238*6a54128fSAndroid Build Coastguard Worker pctx.errcode = ext2fs_u32_list_create(&ctx->casefolded_dirs, 0);
2239*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode)
2240*6a54128fSAndroid Build Coastguard Worker goto error;
2241*6a54128fSAndroid Build Coastguard Worker }
2242*6a54128fSAndroid Build Coastguard Worker pctx.errcode = ext2fs_u32_list_add(ctx->casefolded_dirs, ino);
2243*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode == 0)
2244*6a54128fSAndroid Build Coastguard Worker return;
2245*6a54128fSAndroid Build Coastguard Worker error:
2246*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ALLOCATE_CASEFOLDED_DIRLIST, &pctx);
2247*6a54128fSAndroid Build Coastguard Worker /* Should never get here */
2248*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
2249*6a54128fSAndroid Build Coastguard Worker }
2250*6a54128fSAndroid Build Coastguard Worker
2251*6a54128fSAndroid Build Coastguard Worker /*
2252*6a54128fSAndroid Build Coastguard Worker * This procedure will allocate the inode "bb" (badblock) map table
2253*6a54128fSAndroid Build Coastguard Worker */
alloc_bb_map(e2fsck_t ctx)2254*6a54128fSAndroid Build Coastguard Worker static void alloc_bb_map(e2fsck_t ctx)
2255*6a54128fSAndroid Build Coastguard Worker {
2256*6a54128fSAndroid Build Coastguard Worker struct problem_context pctx;
2257*6a54128fSAndroid Build Coastguard Worker
2258*6a54128fSAndroid Build Coastguard Worker clear_problem_context(&pctx);
2259*6a54128fSAndroid Build Coastguard Worker pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
2260*6a54128fSAndroid Build Coastguard Worker _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
2261*6a54128fSAndroid Build Coastguard Worker "inode_bb_map", &ctx->inode_bb_map);
2262*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
2263*6a54128fSAndroid Build Coastguard Worker pctx.num = 4;
2264*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
2265*6a54128fSAndroid Build Coastguard Worker /* Should never get here */
2266*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
2267*6a54128fSAndroid Build Coastguard Worker return;
2268*6a54128fSAndroid Build Coastguard Worker }
2269*6a54128fSAndroid Build Coastguard Worker }
2270*6a54128fSAndroid Build Coastguard Worker
2271*6a54128fSAndroid Build Coastguard Worker /*
2272*6a54128fSAndroid Build Coastguard Worker * This procedure will allocate the inode imagic table
2273*6a54128fSAndroid Build Coastguard Worker */
alloc_imagic_map(e2fsck_t ctx)2274*6a54128fSAndroid Build Coastguard Worker static void alloc_imagic_map(e2fsck_t ctx)
2275*6a54128fSAndroid Build Coastguard Worker {
2276*6a54128fSAndroid Build Coastguard Worker struct problem_context pctx;
2277*6a54128fSAndroid Build Coastguard Worker
2278*6a54128fSAndroid Build Coastguard Worker clear_problem_context(&pctx);
2279*6a54128fSAndroid Build Coastguard Worker pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
2280*6a54128fSAndroid Build Coastguard Worker _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
2281*6a54128fSAndroid Build Coastguard Worker "inode_imagic_map", &ctx->inode_imagic_map);
2282*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
2283*6a54128fSAndroid Build Coastguard Worker pctx.num = 5;
2284*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
2285*6a54128fSAndroid Build Coastguard Worker /* Should never get here */
2286*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
2287*6a54128fSAndroid Build Coastguard Worker return;
2288*6a54128fSAndroid Build Coastguard Worker }
2289*6a54128fSAndroid Build Coastguard Worker }
2290*6a54128fSAndroid Build Coastguard Worker
2291*6a54128fSAndroid Build Coastguard Worker /*
2292*6a54128fSAndroid Build Coastguard Worker * Marks a block as in use, setting the dup_map if it's been set
2293*6a54128fSAndroid Build Coastguard Worker * already. Called by process_block and process_bad_block.
2294*6a54128fSAndroid Build Coastguard Worker *
2295*6a54128fSAndroid Build Coastguard Worker * WARNING: Assumes checks have already been done to make sure block
2296*6a54128fSAndroid Build Coastguard Worker * is valid. This is true in both process_block and process_bad_block.
2297*6a54128fSAndroid Build Coastguard Worker */
mark_block_used(e2fsck_t ctx,blk64_t block)2298*6a54128fSAndroid Build Coastguard Worker static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
2299*6a54128fSAndroid Build Coastguard Worker {
2300*6a54128fSAndroid Build Coastguard Worker struct problem_context pctx;
2301*6a54128fSAndroid Build Coastguard Worker
2302*6a54128fSAndroid Build Coastguard Worker clear_problem_context(&pctx);
2303*6a54128fSAndroid Build Coastguard Worker
2304*6a54128fSAndroid Build Coastguard Worker if (ext2fs_fast_test_block_bitmap2(ctx->block_found_map, block)) {
2305*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_feature_shared_blocks(ctx->fs->super) &&
2306*6a54128fSAndroid Build Coastguard Worker !(ctx->options & E2F_OPT_UNSHARE_BLOCKS)) {
2307*6a54128fSAndroid Build Coastguard Worker return;
2308*6a54128fSAndroid Build Coastguard Worker }
2309*6a54128fSAndroid Build Coastguard Worker if (!ctx->block_dup_map) {
2310*6a54128fSAndroid Build Coastguard Worker pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
2311*6a54128fSAndroid Build Coastguard Worker _("multiply claimed block map"),
2312*6a54128fSAndroid Build Coastguard Worker EXT2FS_BMAP64_RBTREE, "block_dup_map",
2313*6a54128fSAndroid Build Coastguard Worker &ctx->block_dup_map);
2314*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
2315*6a54128fSAndroid Build Coastguard Worker pctx.num = 3;
2316*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
2317*6a54128fSAndroid Build Coastguard Worker &pctx);
2318*6a54128fSAndroid Build Coastguard Worker /* Should never get here */
2319*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
2320*6a54128fSAndroid Build Coastguard Worker return;
2321*6a54128fSAndroid Build Coastguard Worker }
2322*6a54128fSAndroid Build Coastguard Worker }
2323*6a54128fSAndroid Build Coastguard Worker ext2fs_fast_mark_block_bitmap2(ctx->block_dup_map, block);
2324*6a54128fSAndroid Build Coastguard Worker } else {
2325*6a54128fSAndroid Build Coastguard Worker ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
2326*6a54128fSAndroid Build Coastguard Worker }
2327*6a54128fSAndroid Build Coastguard Worker }
2328*6a54128fSAndroid Build Coastguard Worker
2329*6a54128fSAndroid Build Coastguard Worker /*
2330*6a54128fSAndroid Build Coastguard Worker * When cluster size is greater than one block, it is caller's responsibility
2331*6a54128fSAndroid Build Coastguard Worker * to make sure block parameter starts at a cluster boundary.
2332*6a54128fSAndroid Build Coastguard Worker */
mark_blocks_used(e2fsck_t ctx,blk64_t block,unsigned int num)2333*6a54128fSAndroid Build Coastguard Worker static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
2334*6a54128fSAndroid Build Coastguard Worker unsigned int num)
2335*6a54128fSAndroid Build Coastguard Worker {
2336*6a54128fSAndroid Build Coastguard Worker if (ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num))
2337*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
2338*6a54128fSAndroid Build Coastguard Worker else {
2339*6a54128fSAndroid Build Coastguard Worker unsigned int i;
2340*6a54128fSAndroid Build Coastguard Worker
2341*6a54128fSAndroid Build Coastguard Worker for (i = 0; i < num; i += EXT2FS_CLUSTER_RATIO(ctx->fs))
2342*6a54128fSAndroid Build Coastguard Worker mark_block_used(ctx, block + i);
2343*6a54128fSAndroid Build Coastguard Worker }
2344*6a54128fSAndroid Build Coastguard Worker }
2345*6a54128fSAndroid Build Coastguard Worker
2346*6a54128fSAndroid Build Coastguard Worker /*
2347*6a54128fSAndroid Build Coastguard Worker * Adjust the extended attribute block's reference counts at the end
2348*6a54128fSAndroid Build Coastguard Worker * of pass 1, either by subtracting out references for EA blocks that
2349*6a54128fSAndroid Build Coastguard Worker * are still referenced in ctx->refcount, or by adding references for
2350*6a54128fSAndroid Build Coastguard Worker * EA blocks that had extra references as accounted for in
2351*6a54128fSAndroid Build Coastguard Worker * ctx->refcount_extra.
2352*6a54128fSAndroid Build Coastguard Worker */
adjust_extattr_refcount(e2fsck_t ctx,ext2_refcount_t refcount,char * block_buf,int adjust_sign)2353*6a54128fSAndroid Build Coastguard Worker static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
2354*6a54128fSAndroid Build Coastguard Worker char *block_buf, int adjust_sign)
2355*6a54128fSAndroid Build Coastguard Worker {
2356*6a54128fSAndroid Build Coastguard Worker struct ext2_ext_attr_header *header;
2357*6a54128fSAndroid Build Coastguard Worker struct problem_context pctx;
2358*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs = ctx->fs;
2359*6a54128fSAndroid Build Coastguard Worker blk64_t blk;
2360*6a54128fSAndroid Build Coastguard Worker __u32 should_be;
2361*6a54128fSAndroid Build Coastguard Worker ea_value_t count;
2362*6a54128fSAndroid Build Coastguard Worker
2363*6a54128fSAndroid Build Coastguard Worker clear_problem_context(&pctx);
2364*6a54128fSAndroid Build Coastguard Worker
2365*6a54128fSAndroid Build Coastguard Worker ea_refcount_intr_begin(refcount);
2366*6a54128fSAndroid Build Coastguard Worker while (1) {
2367*6a54128fSAndroid Build Coastguard Worker if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
2368*6a54128fSAndroid Build Coastguard Worker break;
2369*6a54128fSAndroid Build Coastguard Worker pctx.blk = blk;
2370*6a54128fSAndroid Build Coastguard Worker pctx.errcode = ext2fs_read_ext_attr3(fs, blk, block_buf,
2371*6a54128fSAndroid Build Coastguard Worker pctx.ino);
2372*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
2373*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
2374*6a54128fSAndroid Build Coastguard Worker return;
2375*6a54128fSAndroid Build Coastguard Worker }
2376*6a54128fSAndroid Build Coastguard Worker header = (struct ext2_ext_attr_header *) block_buf;
2377*6a54128fSAndroid Build Coastguard Worker pctx.blkcount = header->h_refcount;
2378*6a54128fSAndroid Build Coastguard Worker should_be = header->h_refcount + adjust_sign * (int)count;
2379*6a54128fSAndroid Build Coastguard Worker pctx.num = should_be;
2380*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
2381*6a54128fSAndroid Build Coastguard Worker header->h_refcount = should_be;
2382*6a54128fSAndroid Build Coastguard Worker pctx.errcode = ext2fs_write_ext_attr3(fs, blk,
2383*6a54128fSAndroid Build Coastguard Worker block_buf,
2384*6a54128fSAndroid Build Coastguard Worker pctx.ino);
2385*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
2386*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
2387*6a54128fSAndroid Build Coastguard Worker &pctx);
2388*6a54128fSAndroid Build Coastguard Worker continue;
2389*6a54128fSAndroid Build Coastguard Worker }
2390*6a54128fSAndroid Build Coastguard Worker }
2391*6a54128fSAndroid Build Coastguard Worker }
2392*6a54128fSAndroid Build Coastguard Worker }
2393*6a54128fSAndroid Build Coastguard Worker
2394*6a54128fSAndroid Build Coastguard Worker /*
2395*6a54128fSAndroid Build Coastguard Worker * Handle processing the extended attribute blocks
2396*6a54128fSAndroid Build Coastguard Worker */
check_ext_attr(e2fsck_t ctx,struct problem_context * pctx,char * block_buf,struct ea_quota * ea_block_quota)2397*6a54128fSAndroid Build Coastguard Worker static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
2398*6a54128fSAndroid Build Coastguard Worker char *block_buf, struct ea_quota *ea_block_quota)
2399*6a54128fSAndroid Build Coastguard Worker {
2400*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs = ctx->fs;
2401*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino = pctx->ino;
2402*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode = pctx->inode;
2403*6a54128fSAndroid Build Coastguard Worker blk64_t blk;
2404*6a54128fSAndroid Build Coastguard Worker char * end;
2405*6a54128fSAndroid Build Coastguard Worker struct ext2_ext_attr_header *header;
2406*6a54128fSAndroid Build Coastguard Worker struct ext2_ext_attr_entry *first, *entry;
2407*6a54128fSAndroid Build Coastguard Worker blk64_t quota_blocks = EXT2FS_C2B(fs, 1);
2408*6a54128fSAndroid Build Coastguard Worker __u64 quota_inodes = 0;
2409*6a54128fSAndroid Build Coastguard Worker region_t region = 0;
2410*6a54128fSAndroid Build Coastguard Worker int failed_csum = 0;
2411*6a54128fSAndroid Build Coastguard Worker
2412*6a54128fSAndroid Build Coastguard Worker ea_block_quota->blocks = 0;
2413*6a54128fSAndroid Build Coastguard Worker ea_block_quota->inodes = 0;
2414*6a54128fSAndroid Build Coastguard Worker
2415*6a54128fSAndroid Build Coastguard Worker blk = ext2fs_file_acl_block(fs, inode);
2416*6a54128fSAndroid Build Coastguard Worker if (blk == 0)
2417*6a54128fSAndroid Build Coastguard Worker return 0;
2418*6a54128fSAndroid Build Coastguard Worker
2419*6a54128fSAndroid Build Coastguard Worker /*
2420*6a54128fSAndroid Build Coastguard Worker * If the Extended attribute flag isn't set, then a non-zero
2421*6a54128fSAndroid Build Coastguard Worker * file acl means that the inode is corrupted.
2422*6a54128fSAndroid Build Coastguard Worker *
2423*6a54128fSAndroid Build Coastguard Worker * Or if the extended attribute block is an invalid block,
2424*6a54128fSAndroid Build Coastguard Worker * then the inode is also corrupted.
2425*6a54128fSAndroid Build Coastguard Worker */
2426*6a54128fSAndroid Build Coastguard Worker if (!ext2fs_has_feature_xattr(fs->super) ||
2427*6a54128fSAndroid Build Coastguard Worker (blk < fs->super->s_first_data_block) ||
2428*6a54128fSAndroid Build Coastguard Worker (blk >= ext2fs_blocks_count(fs->super))) {
2429*6a54128fSAndroid Build Coastguard Worker mark_inode_bad(ctx, ino);
2430*6a54128fSAndroid Build Coastguard Worker return 0;
2431*6a54128fSAndroid Build Coastguard Worker }
2432*6a54128fSAndroid Build Coastguard Worker
2433*6a54128fSAndroid Build Coastguard Worker /* If ea bitmap hasn't been allocated, create it */
2434*6a54128fSAndroid Build Coastguard Worker if (!ctx->block_ea_map) {
2435*6a54128fSAndroid Build Coastguard Worker pctx->errcode = e2fsck_allocate_block_bitmap(fs,
2436*6a54128fSAndroid Build Coastguard Worker _("ext attr block map"),
2437*6a54128fSAndroid Build Coastguard Worker EXT2FS_BMAP64_RBTREE, "block_ea_map",
2438*6a54128fSAndroid Build Coastguard Worker &ctx->block_ea_map);
2439*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode) {
2440*6a54128fSAndroid Build Coastguard Worker pctx->num = 2;
2441*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
2442*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
2443*6a54128fSAndroid Build Coastguard Worker return 0;
2444*6a54128fSAndroid Build Coastguard Worker }
2445*6a54128fSAndroid Build Coastguard Worker }
2446*6a54128fSAndroid Build Coastguard Worker
2447*6a54128fSAndroid Build Coastguard Worker /* Create the EA refcount structure if necessary */
2448*6a54128fSAndroid Build Coastguard Worker if (!ctx->refcount) {
2449*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ea_refcount_create(0, &ctx->refcount);
2450*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode) {
2451*6a54128fSAndroid Build Coastguard Worker pctx->num = 1;
2452*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
2453*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
2454*6a54128fSAndroid Build Coastguard Worker return 0;
2455*6a54128fSAndroid Build Coastguard Worker }
2456*6a54128fSAndroid Build Coastguard Worker }
2457*6a54128fSAndroid Build Coastguard Worker
2458*6a54128fSAndroid Build Coastguard Worker #if 0
2459*6a54128fSAndroid Build Coastguard Worker /* Debugging text */
2460*6a54128fSAndroid Build Coastguard Worker printf("Inode %u has EA block %u\n", ino, blk);
2461*6a54128fSAndroid Build Coastguard Worker #endif
2462*6a54128fSAndroid Build Coastguard Worker
2463*6a54128fSAndroid Build Coastguard Worker /* Have we seen this EA block before? */
2464*6a54128fSAndroid Build Coastguard Worker if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
2465*6a54128fSAndroid Build Coastguard Worker ea_block_quota->blocks = EXT2FS_C2B(fs, 1);
2466*6a54128fSAndroid Build Coastguard Worker ea_block_quota->inodes = 0;
2467*6a54128fSAndroid Build Coastguard Worker
2468*6a54128fSAndroid Build Coastguard Worker if (ctx->ea_block_quota_blocks) {
2469*6a54128fSAndroid Build Coastguard Worker ea_refcount_fetch(ctx->ea_block_quota_blocks, blk,
2470*6a54128fSAndroid Build Coastguard Worker "a_blocks);
2471*6a54128fSAndroid Build Coastguard Worker if (quota_blocks)
2472*6a54128fSAndroid Build Coastguard Worker ea_block_quota->blocks = quota_blocks;
2473*6a54128fSAndroid Build Coastguard Worker }
2474*6a54128fSAndroid Build Coastguard Worker
2475*6a54128fSAndroid Build Coastguard Worker if (ctx->ea_block_quota_inodes)
2476*6a54128fSAndroid Build Coastguard Worker ea_refcount_fetch(ctx->ea_block_quota_inodes, blk,
2477*6a54128fSAndroid Build Coastguard Worker &ea_block_quota->inodes);
2478*6a54128fSAndroid Build Coastguard Worker
2479*6a54128fSAndroid Build Coastguard Worker if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
2480*6a54128fSAndroid Build Coastguard Worker return 1;
2481*6a54128fSAndroid Build Coastguard Worker /* Ooops, this EA was referenced more than it stated */
2482*6a54128fSAndroid Build Coastguard Worker if (!ctx->refcount_extra) {
2483*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ea_refcount_create(0,
2484*6a54128fSAndroid Build Coastguard Worker &ctx->refcount_extra);
2485*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode) {
2486*6a54128fSAndroid Build Coastguard Worker pctx->num = 2;
2487*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
2488*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
2489*6a54128fSAndroid Build Coastguard Worker return 0;
2490*6a54128fSAndroid Build Coastguard Worker }
2491*6a54128fSAndroid Build Coastguard Worker }
2492*6a54128fSAndroid Build Coastguard Worker ea_refcount_increment(ctx->refcount_extra, blk, 0);
2493*6a54128fSAndroid Build Coastguard Worker return 1;
2494*6a54128fSAndroid Build Coastguard Worker }
2495*6a54128fSAndroid Build Coastguard Worker
2496*6a54128fSAndroid Build Coastguard Worker /*
2497*6a54128fSAndroid Build Coastguard Worker * OK, we haven't seen this EA block yet. So we need to
2498*6a54128fSAndroid Build Coastguard Worker * validate it
2499*6a54128fSAndroid Build Coastguard Worker */
2500*6a54128fSAndroid Build Coastguard Worker pctx->blk = blk;
2501*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_read_ext_attr3(fs, blk, block_buf, pctx->ino);
2502*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode == EXT2_ET_EXT_ATTR_CSUM_INVALID) {
2503*6a54128fSAndroid Build Coastguard Worker pctx->errcode = 0;
2504*6a54128fSAndroid Build Coastguard Worker failed_csum = 1;
2505*6a54128fSAndroid Build Coastguard Worker } else if (pctx->errcode == EXT2_ET_BAD_EA_HEADER)
2506*6a54128fSAndroid Build Coastguard Worker pctx->errcode = 0;
2507*6a54128fSAndroid Build Coastguard Worker
2508*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode &&
2509*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx)) {
2510*6a54128fSAndroid Build Coastguard Worker pctx->errcode = 0;
2511*6a54128fSAndroid Build Coastguard Worker goto clear_extattr;
2512*6a54128fSAndroid Build Coastguard Worker }
2513*6a54128fSAndroid Build Coastguard Worker header = (struct ext2_ext_attr_header *) block_buf;
2514*6a54128fSAndroid Build Coastguard Worker pctx->blk = ext2fs_file_acl_block(fs, inode);
2515*6a54128fSAndroid Build Coastguard Worker if (((ctx->ext_attr_ver == 1) &&
2516*6a54128fSAndroid Build Coastguard Worker (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
2517*6a54128fSAndroid Build Coastguard Worker ((ctx->ext_attr_ver == 2) &&
2518*6a54128fSAndroid Build Coastguard Worker (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
2519*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
2520*6a54128fSAndroid Build Coastguard Worker goto clear_extattr;
2521*6a54128fSAndroid Build Coastguard Worker }
2522*6a54128fSAndroid Build Coastguard Worker
2523*6a54128fSAndroid Build Coastguard Worker if (header->h_blocks != 1) {
2524*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
2525*6a54128fSAndroid Build Coastguard Worker goto clear_extattr;
2526*6a54128fSAndroid Build Coastguard Worker }
2527*6a54128fSAndroid Build Coastguard Worker
2528*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
2529*6a54128fSAndroid Build Coastguard Worker goto clear_extattr;
2530*6a54128fSAndroid Build Coastguard Worker
2531*6a54128fSAndroid Build Coastguard Worker region = region_create(0, fs->blocksize);
2532*6a54128fSAndroid Build Coastguard Worker if (!region) {
2533*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
2534*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
2535*6a54128fSAndroid Build Coastguard Worker return 0;
2536*6a54128fSAndroid Build Coastguard Worker }
2537*6a54128fSAndroid Build Coastguard Worker if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
2538*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2539*6a54128fSAndroid Build Coastguard Worker goto clear_extattr;
2540*6a54128fSAndroid Build Coastguard Worker }
2541*6a54128fSAndroid Build Coastguard Worker
2542*6a54128fSAndroid Build Coastguard Worker first = (struct ext2_ext_attr_entry *)(header+1);
2543*6a54128fSAndroid Build Coastguard Worker end = block_buf + fs->blocksize;
2544*6a54128fSAndroid Build Coastguard Worker entry = first;
2545*6a54128fSAndroid Build Coastguard Worker while ((char *)entry < end && *(__u32 *)entry) {
2546*6a54128fSAndroid Build Coastguard Worker __u32 hash;
2547*6a54128fSAndroid Build Coastguard Worker
2548*6a54128fSAndroid Build Coastguard Worker if (region_allocate(region, (char *)entry - (char *)header,
2549*6a54128fSAndroid Build Coastguard Worker EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
2550*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2551*6a54128fSAndroid Build Coastguard Worker goto clear_extattr;
2552*6a54128fSAndroid Build Coastguard Worker break;
2553*6a54128fSAndroid Build Coastguard Worker }
2554*6a54128fSAndroid Build Coastguard Worker if ((ctx->ext_attr_ver == 1 &&
2555*6a54128fSAndroid Build Coastguard Worker (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
2556*6a54128fSAndroid Build Coastguard Worker (ctx->ext_attr_ver == 2 &&
2557*6a54128fSAndroid Build Coastguard Worker entry->e_name_index == 0)) {
2558*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
2559*6a54128fSAndroid Build Coastguard Worker goto clear_extattr;
2560*6a54128fSAndroid Build Coastguard Worker break;
2561*6a54128fSAndroid Build Coastguard Worker }
2562*6a54128fSAndroid Build Coastguard Worker if (entry->e_value_inum == 0) {
2563*6a54128fSAndroid Build Coastguard Worker if (entry->e_value_size > EXT2_XATTR_SIZE_MAX ||
2564*6a54128fSAndroid Build Coastguard Worker (entry->e_value_offs + entry->e_value_size >
2565*6a54128fSAndroid Build Coastguard Worker fs->blocksize)) {
2566*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
2567*6a54128fSAndroid Build Coastguard Worker goto clear_extattr;
2568*6a54128fSAndroid Build Coastguard Worker break;
2569*6a54128fSAndroid Build Coastguard Worker }
2570*6a54128fSAndroid Build Coastguard Worker if (entry->e_value_size &&
2571*6a54128fSAndroid Build Coastguard Worker region_allocate(region, entry->e_value_offs,
2572*6a54128fSAndroid Build Coastguard Worker EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
2573*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION,
2574*6a54128fSAndroid Build Coastguard Worker pctx))
2575*6a54128fSAndroid Build Coastguard Worker goto clear_extattr;
2576*6a54128fSAndroid Build Coastguard Worker }
2577*6a54128fSAndroid Build Coastguard Worker
2578*6a54128fSAndroid Build Coastguard Worker hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
2579*6a54128fSAndroid Build Coastguard Worker entry->e_value_offs);
2580*6a54128fSAndroid Build Coastguard Worker if (entry->e_hash != hash)
2581*6a54128fSAndroid Build Coastguard Worker hash = ext2fs_ext_attr_hash_entry_signed(entry,
2582*6a54128fSAndroid Build Coastguard Worker block_buf + entry->e_value_offs);
2583*6a54128fSAndroid Build Coastguard Worker
2584*6a54128fSAndroid Build Coastguard Worker if (entry->e_hash != hash) {
2585*6a54128fSAndroid Build Coastguard Worker pctx->num = entry->e_hash;
2586*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
2587*6a54128fSAndroid Build Coastguard Worker goto clear_extattr;
2588*6a54128fSAndroid Build Coastguard Worker entry->e_hash = hash;
2589*6a54128fSAndroid Build Coastguard Worker }
2590*6a54128fSAndroid Build Coastguard Worker } else {
2591*6a54128fSAndroid Build Coastguard Worker problem_t problem;
2592*6a54128fSAndroid Build Coastguard Worker blk64_t entry_quota_blocks;
2593*6a54128fSAndroid Build Coastguard Worker
2594*6a54128fSAndroid Build Coastguard Worker problem = check_large_ea_inode(ctx, entry, pctx,
2595*6a54128fSAndroid Build Coastguard Worker &entry_quota_blocks);
2596*6a54128fSAndroid Build Coastguard Worker if (problem && fix_problem(ctx, problem, pctx))
2597*6a54128fSAndroid Build Coastguard Worker goto clear_extattr;
2598*6a54128fSAndroid Build Coastguard Worker
2599*6a54128fSAndroid Build Coastguard Worker quota_blocks += entry_quota_blocks;
2600*6a54128fSAndroid Build Coastguard Worker quota_inodes++;
2601*6a54128fSAndroid Build Coastguard Worker }
2602*6a54128fSAndroid Build Coastguard Worker
2603*6a54128fSAndroid Build Coastguard Worker entry = EXT2_EXT_ATTR_NEXT(entry);
2604*6a54128fSAndroid Build Coastguard Worker }
2605*6a54128fSAndroid Build Coastguard Worker if (region_allocate(region, (char *)entry - (char *)header, 4)) {
2606*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2607*6a54128fSAndroid Build Coastguard Worker goto clear_extattr;
2608*6a54128fSAndroid Build Coastguard Worker }
2609*6a54128fSAndroid Build Coastguard Worker region_free(region);
2610*6a54128fSAndroid Build Coastguard Worker
2611*6a54128fSAndroid Build Coastguard Worker /*
2612*6a54128fSAndroid Build Coastguard Worker * We only get here if there was no other errors that were fixed.
2613*6a54128fSAndroid Build Coastguard Worker * If there was a checksum fail, ask to correct it.
2614*6a54128fSAndroid Build Coastguard Worker */
2615*6a54128fSAndroid Build Coastguard Worker if (failed_csum &&
2616*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_EA_BLOCK_ONLY_CSUM_INVALID, pctx)) {
2617*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_write_ext_attr3(fs, blk, block_buf,
2618*6a54128fSAndroid Build Coastguard Worker pctx->ino);
2619*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode)
2620*6a54128fSAndroid Build Coastguard Worker return 0;
2621*6a54128fSAndroid Build Coastguard Worker }
2622*6a54128fSAndroid Build Coastguard Worker
2623*6a54128fSAndroid Build Coastguard Worker if (quota_blocks != EXT2FS_C2B(fs, 1U)) {
2624*6a54128fSAndroid Build Coastguard Worker if (!ctx->ea_block_quota_blocks) {
2625*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ea_refcount_create(0,
2626*6a54128fSAndroid Build Coastguard Worker &ctx->ea_block_quota_blocks);
2627*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode) {
2628*6a54128fSAndroid Build Coastguard Worker pctx->num = 3;
2629*6a54128fSAndroid Build Coastguard Worker goto refcount_fail;
2630*6a54128fSAndroid Build Coastguard Worker }
2631*6a54128fSAndroid Build Coastguard Worker }
2632*6a54128fSAndroid Build Coastguard Worker ea_refcount_store(ctx->ea_block_quota_blocks, blk,
2633*6a54128fSAndroid Build Coastguard Worker quota_blocks);
2634*6a54128fSAndroid Build Coastguard Worker }
2635*6a54128fSAndroid Build Coastguard Worker
2636*6a54128fSAndroid Build Coastguard Worker if (quota_inodes) {
2637*6a54128fSAndroid Build Coastguard Worker if (!ctx->ea_block_quota_inodes) {
2638*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ea_refcount_create(0,
2639*6a54128fSAndroid Build Coastguard Worker &ctx->ea_block_quota_inodes);
2640*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode) {
2641*6a54128fSAndroid Build Coastguard Worker pctx->num = 4;
2642*6a54128fSAndroid Build Coastguard Worker refcount_fail:
2643*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
2644*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
2645*6a54128fSAndroid Build Coastguard Worker return 0;
2646*6a54128fSAndroid Build Coastguard Worker }
2647*6a54128fSAndroid Build Coastguard Worker }
2648*6a54128fSAndroid Build Coastguard Worker
2649*6a54128fSAndroid Build Coastguard Worker ea_refcount_store(ctx->ea_block_quota_inodes, blk,
2650*6a54128fSAndroid Build Coastguard Worker quota_inodes);
2651*6a54128fSAndroid Build Coastguard Worker }
2652*6a54128fSAndroid Build Coastguard Worker ea_block_quota->blocks = quota_blocks;
2653*6a54128fSAndroid Build Coastguard Worker ea_block_quota->inodes = quota_inodes;
2654*6a54128fSAndroid Build Coastguard Worker
2655*6a54128fSAndroid Build Coastguard Worker inc_ea_inode_refs(ctx, pctx, first, end);
2656*6a54128fSAndroid Build Coastguard Worker ea_refcount_store(ctx->refcount, blk, header->h_refcount - 1);
2657*6a54128fSAndroid Build Coastguard Worker mark_block_used(ctx, blk);
2658*6a54128fSAndroid Build Coastguard Worker ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
2659*6a54128fSAndroid Build Coastguard Worker return 1;
2660*6a54128fSAndroid Build Coastguard Worker
2661*6a54128fSAndroid Build Coastguard Worker clear_extattr:
2662*6a54128fSAndroid Build Coastguard Worker if (region)
2663*6a54128fSAndroid Build Coastguard Worker region_free(region);
2664*6a54128fSAndroid Build Coastguard Worker ext2fs_file_acl_block_set(fs, inode, 0);
2665*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
2666*6a54128fSAndroid Build Coastguard Worker return 0;
2667*6a54128fSAndroid Build Coastguard Worker }
2668*6a54128fSAndroid Build Coastguard Worker
2669*6a54128fSAndroid Build Coastguard Worker /* Returns 1 if bad htree, 0 if OK */
handle_htree(e2fsck_t ctx,struct problem_context * pctx,ext2_ino_t ino,struct ext2_inode * inode,char * block_buf)2670*6a54128fSAndroid Build Coastguard Worker static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
2671*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino, struct ext2_inode *inode,
2672*6a54128fSAndroid Build Coastguard Worker char *block_buf)
2673*6a54128fSAndroid Build Coastguard Worker {
2674*6a54128fSAndroid Build Coastguard Worker struct ext2_dx_root_info *root;
2675*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs = ctx->fs;
2676*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
2677*6a54128fSAndroid Build Coastguard Worker blk64_t blk;
2678*6a54128fSAndroid Build Coastguard Worker
2679*6a54128fSAndroid Build Coastguard Worker if ((!LINUX_S_ISDIR(inode->i_mode) &&
2680*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
2681*6a54128fSAndroid Build Coastguard Worker (!ext2fs_has_feature_dir_index(fs->super) &&
2682*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_HTREE_SET, pctx)))
2683*6a54128fSAndroid Build Coastguard Worker return 1;
2684*6a54128fSAndroid Build Coastguard Worker
2685*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
2686*6a54128fSAndroid Build Coastguard Worker
2687*6a54128fSAndroid Build Coastguard Worker if ((pctx->errcode) ||
2688*6a54128fSAndroid Build Coastguard Worker (blk == 0) ||
2689*6a54128fSAndroid Build Coastguard Worker (blk < fs->super->s_first_data_block) ||
2690*6a54128fSAndroid Build Coastguard Worker (blk >= ext2fs_blocks_count(fs->super))) {
2691*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2692*6a54128fSAndroid Build Coastguard Worker return 1;
2693*6a54128fSAndroid Build Coastguard Worker else
2694*6a54128fSAndroid Build Coastguard Worker return 0;
2695*6a54128fSAndroid Build Coastguard Worker }
2696*6a54128fSAndroid Build Coastguard Worker
2697*6a54128fSAndroid Build Coastguard Worker retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
2698*6a54128fSAndroid Build Coastguard Worker if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2699*6a54128fSAndroid Build Coastguard Worker return 1;
2700*6a54128fSAndroid Build Coastguard Worker
2701*6a54128fSAndroid Build Coastguard Worker /* XXX should check that beginning matches a directory */
2702*6a54128fSAndroid Build Coastguard Worker root = (struct ext2_dx_root_info *) (block_buf + 24);
2703*6a54128fSAndroid Build Coastguard Worker
2704*6a54128fSAndroid Build Coastguard Worker if ((root->reserved_zero || root->info_length < 8) &&
2705*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2706*6a54128fSAndroid Build Coastguard Worker return 1;
2707*6a54128fSAndroid Build Coastguard Worker
2708*6a54128fSAndroid Build Coastguard Worker pctx->num = root->hash_version;
2709*6a54128fSAndroid Build Coastguard Worker if ((root->hash_version != EXT2_HASH_LEGACY) &&
2710*6a54128fSAndroid Build Coastguard Worker (root->hash_version != EXT2_HASH_HALF_MD4) &&
2711*6a54128fSAndroid Build Coastguard Worker (root->hash_version != EXT2_HASH_TEA) &&
2712*6a54128fSAndroid Build Coastguard Worker (root->hash_version != EXT2_HASH_SIPHASH) &&
2713*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
2714*6a54128fSAndroid Build Coastguard Worker return 1;
2715*6a54128fSAndroid Build Coastguard Worker
2716*6a54128fSAndroid Build Coastguard Worker if (ext4_hash_in_dirent(inode)) {
2717*6a54128fSAndroid Build Coastguard Worker if (root->hash_version != EXT2_HASH_SIPHASH &&
2718*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_HTREE_NEEDS_SIPHASH, pctx))
2719*6a54128fSAndroid Build Coastguard Worker return 1;
2720*6a54128fSAndroid Build Coastguard Worker } else {
2721*6a54128fSAndroid Build Coastguard Worker if (root->hash_version == EXT2_HASH_SIPHASH &&
2722*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_HTREE_CANNOT_SIPHASH, pctx))
2723*6a54128fSAndroid Build Coastguard Worker return 1;
2724*6a54128fSAndroid Build Coastguard Worker }
2725*6a54128fSAndroid Build Coastguard Worker
2726*6a54128fSAndroid Build Coastguard Worker if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
2727*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
2728*6a54128fSAndroid Build Coastguard Worker return 1;
2729*6a54128fSAndroid Build Coastguard Worker
2730*6a54128fSAndroid Build Coastguard Worker pctx->num = root->indirect_levels;
2731*6a54128fSAndroid Build Coastguard Worker /* if htree level is clearly too high, consider it to be broken */
2732*6a54128fSAndroid Build Coastguard Worker if (root->indirect_levels > EXT4_HTREE_LEVEL &&
2733*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
2734*6a54128fSAndroid Build Coastguard Worker return 1;
2735*6a54128fSAndroid Build Coastguard Worker
2736*6a54128fSAndroid Build Coastguard Worker /* if level is only maybe too high, LARGE_DIR feature could be unset */
2737*6a54128fSAndroid Build Coastguard Worker if (root->indirect_levels > ext2_dir_htree_level(fs) &&
2738*6a54128fSAndroid Build Coastguard Worker !ext2fs_has_feature_largedir(fs->super)) {
2739*6a54128fSAndroid Build Coastguard Worker int blockbits = EXT2_BLOCK_SIZE_BITS(fs->super) + 10;
2740*6a54128fSAndroid Build Coastguard Worker unsigned idx_pb = 1 << (blockbits - 3);
2741*6a54128fSAndroid Build Coastguard Worker
2742*6a54128fSAndroid Build Coastguard Worker /* compare inode size/blocks vs. max-sized 2-level htree */
2743*6a54128fSAndroid Build Coastguard Worker if (EXT2_I_SIZE(pctx->inode) <
2744*6a54128fSAndroid Build Coastguard Worker (idx_pb - 1) * (idx_pb - 2) << blockbits &&
2745*6a54128fSAndroid Build Coastguard Worker pctx->inode->i_blocks <
2746*6a54128fSAndroid Build Coastguard Worker (idx_pb - 1) * (idx_pb - 2) << (blockbits - 9) &&
2747*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
2748*6a54128fSAndroid Build Coastguard Worker return 1;
2749*6a54128fSAndroid Build Coastguard Worker }
2750*6a54128fSAndroid Build Coastguard Worker
2751*6a54128fSAndroid Build Coastguard Worker if (root->indirect_levels > EXT4_HTREE_LEVEL_COMPAT ||
2752*6a54128fSAndroid Build Coastguard Worker ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
2753*6a54128fSAndroid Build Coastguard Worker ctx->large_dirs++;
2754*6a54128fSAndroid Build Coastguard Worker
2755*6a54128fSAndroid Build Coastguard Worker return 0;
2756*6a54128fSAndroid Build Coastguard Worker }
2757*6a54128fSAndroid Build Coastguard Worker
e2fsck_clear_inode(e2fsck_t ctx,ext2_ino_t ino,struct ext2_inode * inode,int restart_flag,const char * source)2758*6a54128fSAndroid Build Coastguard Worker void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
2759*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode, int restart_flag,
2760*6a54128fSAndroid Build Coastguard Worker const char *source)
2761*6a54128fSAndroid Build Coastguard Worker {
2762*6a54128fSAndroid Build Coastguard Worker inode->i_flags = 0;
2763*6a54128fSAndroid Build Coastguard Worker inode->i_links_count = 0;
2764*6a54128fSAndroid Build Coastguard Worker ext2fs_icount_store(ctx->inode_link_info, ino, 0);
2765*6a54128fSAndroid Build Coastguard Worker inode->i_dtime = ctx->now;
2766*6a54128fSAndroid Build Coastguard Worker
2767*6a54128fSAndroid Build Coastguard Worker /*
2768*6a54128fSAndroid Build Coastguard Worker * If a special inode has such rotten block mappings that we
2769*6a54128fSAndroid Build Coastguard Worker * want to clear the whole inode, be sure to actually zap
2770*6a54128fSAndroid Build Coastguard Worker * the block maps because i_links_count isn't checked for
2771*6a54128fSAndroid Build Coastguard Worker * special inodes, and we'll end up right back here the next
2772*6a54128fSAndroid Build Coastguard Worker * time we run fsck.
2773*6a54128fSAndroid Build Coastguard Worker */
2774*6a54128fSAndroid Build Coastguard Worker if (ino < EXT2_FIRST_INODE(ctx->fs->super))
2775*6a54128fSAndroid Build Coastguard Worker memset(inode->i_block, 0, sizeof(inode->i_block));
2776*6a54128fSAndroid Build Coastguard Worker
2777*6a54128fSAndroid Build Coastguard Worker ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
2778*6a54128fSAndroid Build Coastguard Worker ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
2779*6a54128fSAndroid Build Coastguard Worker if (ctx->inode_reg_map)
2780*6a54128fSAndroid Build Coastguard Worker ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
2781*6a54128fSAndroid Build Coastguard Worker if (ctx->inode_bad_map)
2782*6a54128fSAndroid Build Coastguard Worker ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
2783*6a54128fSAndroid Build Coastguard Worker
2784*6a54128fSAndroid Build Coastguard Worker /*
2785*6a54128fSAndroid Build Coastguard Worker * If the inode was partially accounted for before processing
2786*6a54128fSAndroid Build Coastguard Worker * was aborted, we need to restart the pass 1 scan.
2787*6a54128fSAndroid Build Coastguard Worker */
2788*6a54128fSAndroid Build Coastguard Worker ctx->flags |= restart_flag;
2789*6a54128fSAndroid Build Coastguard Worker
2790*6a54128fSAndroid Build Coastguard Worker if (ino == EXT2_BAD_INO)
2791*6a54128fSAndroid Build Coastguard Worker memset(inode, 0, sizeof(struct ext2_inode));
2792*6a54128fSAndroid Build Coastguard Worker
2793*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, ino, inode, source);
2794*6a54128fSAndroid Build Coastguard Worker }
2795*6a54128fSAndroid Build Coastguard Worker
2796*6a54128fSAndroid Build Coastguard Worker /*
2797*6a54128fSAndroid Build Coastguard Worker * Use the multiple-blocks reclamation code to fix alignment problems in
2798*6a54128fSAndroid Build Coastguard Worker * a bigalloc filesystem. We want a logical cluster to map to *only* one
2799*6a54128fSAndroid Build Coastguard Worker * physical cluster, and we want the block offsets within that cluster to
2800*6a54128fSAndroid Build Coastguard Worker * line up.
2801*6a54128fSAndroid Build Coastguard Worker */
has_unaligned_cluster_map(e2fsck_t ctx,blk64_t last_pblk,blk64_t last_lblk,blk64_t pblk,blk64_t lblk)2802*6a54128fSAndroid Build Coastguard Worker static int has_unaligned_cluster_map(e2fsck_t ctx,
2803*6a54128fSAndroid Build Coastguard Worker blk64_t last_pblk, blk64_t last_lblk,
2804*6a54128fSAndroid Build Coastguard Worker blk64_t pblk, blk64_t lblk)
2805*6a54128fSAndroid Build Coastguard Worker {
2806*6a54128fSAndroid Build Coastguard Worker blk64_t cluster_mask;
2807*6a54128fSAndroid Build Coastguard Worker
2808*6a54128fSAndroid Build Coastguard Worker if (!ctx->fs->cluster_ratio_bits)
2809*6a54128fSAndroid Build Coastguard Worker return 0;
2810*6a54128fSAndroid Build Coastguard Worker cluster_mask = EXT2FS_CLUSTER_MASK(ctx->fs);
2811*6a54128fSAndroid Build Coastguard Worker
2812*6a54128fSAndroid Build Coastguard Worker /*
2813*6a54128fSAndroid Build Coastguard Worker * If the block in the logical cluster doesn't align with the block in
2814*6a54128fSAndroid Build Coastguard Worker * the physical cluster...
2815*6a54128fSAndroid Build Coastguard Worker */
2816*6a54128fSAndroid Build Coastguard Worker if ((lblk & cluster_mask) != (pblk & cluster_mask))
2817*6a54128fSAndroid Build Coastguard Worker return 1;
2818*6a54128fSAndroid Build Coastguard Worker
2819*6a54128fSAndroid Build Coastguard Worker /*
2820*6a54128fSAndroid Build Coastguard Worker * If we cross a physical cluster boundary within a logical cluster...
2821*6a54128fSAndroid Build Coastguard Worker */
2822*6a54128fSAndroid Build Coastguard Worker if (last_pblk && (lblk & cluster_mask) != 0 &&
2823*6a54128fSAndroid Build Coastguard Worker EXT2FS_B2C(ctx->fs, lblk) == EXT2FS_B2C(ctx->fs, last_lblk) &&
2824*6a54128fSAndroid Build Coastguard Worker EXT2FS_B2C(ctx->fs, pblk) != EXT2FS_B2C(ctx->fs, last_pblk))
2825*6a54128fSAndroid Build Coastguard Worker return 1;
2826*6a54128fSAndroid Build Coastguard Worker
2827*6a54128fSAndroid Build Coastguard Worker return 0;
2828*6a54128fSAndroid Build Coastguard Worker }
2829*6a54128fSAndroid Build Coastguard Worker
scan_extent_node(e2fsck_t ctx,struct problem_context * pctx,struct process_block_struct * pb,blk64_t start_block,blk64_t end_block,blk64_t eof_block,ext2_extent_handle_t ehandle,int try_repairs)2830*6a54128fSAndroid Build Coastguard Worker static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
2831*6a54128fSAndroid Build Coastguard Worker struct process_block_struct *pb,
2832*6a54128fSAndroid Build Coastguard Worker blk64_t start_block, blk64_t end_block,
2833*6a54128fSAndroid Build Coastguard Worker blk64_t eof_block,
2834*6a54128fSAndroid Build Coastguard Worker ext2_extent_handle_t ehandle,
2835*6a54128fSAndroid Build Coastguard Worker int try_repairs)
2836*6a54128fSAndroid Build Coastguard Worker {
2837*6a54128fSAndroid Build Coastguard Worker struct ext2fs_extent extent;
2838*6a54128fSAndroid Build Coastguard Worker blk64_t blk, last_lblk;
2839*6a54128fSAndroid Build Coastguard Worker unsigned int i, n;
2840*6a54128fSAndroid Build Coastguard Worker int is_dir, is_leaf;
2841*6a54128fSAndroid Build Coastguard Worker problem_t problem;
2842*6a54128fSAndroid Build Coastguard Worker struct ext2_extent_info info;
2843*6a54128fSAndroid Build Coastguard Worker int failed_csum = 0;
2844*6a54128fSAndroid Build Coastguard Worker
2845*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID)
2846*6a54128fSAndroid Build Coastguard Worker failed_csum = 1;
2847*6a54128fSAndroid Build Coastguard Worker
2848*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
2849*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode)
2850*6a54128fSAndroid Build Coastguard Worker return;
2851*6a54128fSAndroid Build Coastguard Worker if (!(ctx->options & E2F_OPT_FIXES_ONLY) &&
2852*6a54128fSAndroid Build Coastguard Worker !pb->eti.force_rebuild &&
2853*6a54128fSAndroid Build Coastguard Worker info.curr_level < MAX_EXTENT_DEPTH_COUNT) {
2854*6a54128fSAndroid Build Coastguard Worker struct extent_tree_level *etl;
2855*6a54128fSAndroid Build Coastguard Worker
2856*6a54128fSAndroid Build Coastguard Worker etl = pb->eti.ext_info + info.curr_level;
2857*6a54128fSAndroid Build Coastguard Worker etl->num_extents += info.num_entries;
2858*6a54128fSAndroid Build Coastguard Worker etl->max_extents += info.max_entries;
2859*6a54128fSAndroid Build Coastguard Worker /*
2860*6a54128fSAndroid Build Coastguard Worker * Implementation wart: Splitting extent blocks when appending
2861*6a54128fSAndroid Build Coastguard Worker * will leave the old block with one free entry. Therefore
2862*6a54128fSAndroid Build Coastguard Worker * unless the node is totally full, pretend that a non-root
2863*6a54128fSAndroid Build Coastguard Worker * extent block can hold one fewer entry than it actually does,
2864*6a54128fSAndroid Build Coastguard Worker * so that we don't repeatedly rebuild the extent tree.
2865*6a54128fSAndroid Build Coastguard Worker */
2866*6a54128fSAndroid Build Coastguard Worker if (info.curr_level && info.num_entries < info.max_entries)
2867*6a54128fSAndroid Build Coastguard Worker etl->max_extents--;
2868*6a54128fSAndroid Build Coastguard Worker }
2869*6a54128fSAndroid Build Coastguard Worker
2870*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
2871*6a54128fSAndroid Build Coastguard Worker &extent);
2872*6a54128fSAndroid Build Coastguard Worker while ((pctx->errcode == 0 ||
2873*6a54128fSAndroid Build Coastguard Worker pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID) &&
2874*6a54128fSAndroid Build Coastguard Worker info.num_entries-- > 0) {
2875*6a54128fSAndroid Build Coastguard Worker is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
2876*6a54128fSAndroid Build Coastguard Worker is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
2877*6a54128fSAndroid Build Coastguard Worker last_lblk = extent.e_lblk + extent.e_len - 1;
2878*6a54128fSAndroid Build Coastguard Worker
2879*6a54128fSAndroid Build Coastguard Worker problem = 0;
2880*6a54128fSAndroid Build Coastguard Worker pctx->blk = extent.e_pblk;
2881*6a54128fSAndroid Build Coastguard Worker pctx->blk2 = extent.e_lblk;
2882*6a54128fSAndroid Build Coastguard Worker pctx->num = extent.e_len;
2883*6a54128fSAndroid Build Coastguard Worker pctx->blkcount = extent.e_lblk + extent.e_len;
2884*6a54128fSAndroid Build Coastguard Worker
2885*6a54128fSAndroid Build Coastguard Worker if (extent.e_pblk == 0 ||
2886*6a54128fSAndroid Build Coastguard Worker extent.e_pblk < ctx->fs->super->s_first_data_block ||
2887*6a54128fSAndroid Build Coastguard Worker extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
2888*6a54128fSAndroid Build Coastguard Worker problem = PR_1_EXTENT_BAD_START_BLK;
2889*6a54128fSAndroid Build Coastguard Worker else if (extent.e_lblk < start_block)
2890*6a54128fSAndroid Build Coastguard Worker problem = PR_1_OUT_OF_ORDER_EXTENTS;
2891*6a54128fSAndroid Build Coastguard Worker else if ((end_block && last_lblk > end_block) &&
2892*6a54128fSAndroid Build Coastguard Worker !(last_lblk > eof_block &&
2893*6a54128fSAndroid Build Coastguard Worker ((extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) ||
2894*6a54128fSAndroid Build Coastguard Worker (pctx->inode->i_flags & EXT4_VERITY_FL))))
2895*6a54128fSAndroid Build Coastguard Worker problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
2896*6a54128fSAndroid Build Coastguard Worker else if (is_leaf && extent.e_len == 0)
2897*6a54128fSAndroid Build Coastguard Worker problem = PR_1_EXTENT_LENGTH_ZERO;
2898*6a54128fSAndroid Build Coastguard Worker else if (is_leaf &&
2899*6a54128fSAndroid Build Coastguard Worker (extent.e_pblk + extent.e_len) >
2900*6a54128fSAndroid Build Coastguard Worker ext2fs_blocks_count(ctx->fs->super))
2901*6a54128fSAndroid Build Coastguard Worker problem = PR_1_EXTENT_ENDS_BEYOND;
2902*6a54128fSAndroid Build Coastguard Worker else if (is_leaf && is_dir && !pctx->inode->i_size_high &&
2903*6a54128fSAndroid Build Coastguard Worker !ext2fs_has_feature_largedir(ctx->fs->super) &&
2904*6a54128fSAndroid Build Coastguard Worker ((extent.e_lblk + extent.e_len) >
2905*6a54128fSAndroid Build Coastguard Worker (1U << (21 - ctx->fs->super->s_log_block_size))))
2906*6a54128fSAndroid Build Coastguard Worker problem = PR_1_TOOBIG_DIR;
2907*6a54128fSAndroid Build Coastguard Worker
2908*6a54128fSAndroid Build Coastguard Worker if (is_leaf && problem == 0 && extent.e_len > 0) {
2909*6a54128fSAndroid Build Coastguard Worker #if 0
2910*6a54128fSAndroid Build Coastguard Worker printf("extent_region(ino=%u, expect=%llu, "
2911*6a54128fSAndroid Build Coastguard Worker "lblk=%llu, len=%u)\n", pb->ino,
2912*6a54128fSAndroid Build Coastguard Worker (unsigned long long) pb->next_lblock,
2913*6a54128fSAndroid Build Coastguard Worker (unsigned long long) extent.e_lblk,
2914*6a54128fSAndroid Build Coastguard Worker extent.e_len);
2915*6a54128fSAndroid Build Coastguard Worker #endif
2916*6a54128fSAndroid Build Coastguard Worker if (extent.e_lblk < pb->next_lblock)
2917*6a54128fSAndroid Build Coastguard Worker problem = PR_1_EXTENT_COLLISION;
2918*6a54128fSAndroid Build Coastguard Worker else if (extent.e_lblk + extent.e_len > pb->next_lblock)
2919*6a54128fSAndroid Build Coastguard Worker pb->next_lblock = extent.e_lblk + extent.e_len;
2920*6a54128fSAndroid Build Coastguard Worker }
2921*6a54128fSAndroid Build Coastguard Worker
2922*6a54128fSAndroid Build Coastguard Worker /*
2923*6a54128fSAndroid Build Coastguard Worker * Uninitialized blocks in a directory? Clear the flag and
2924*6a54128fSAndroid Build Coastguard Worker * we'll interpret the blocks later.
2925*6a54128fSAndroid Build Coastguard Worker */
2926*6a54128fSAndroid Build Coastguard Worker if (try_repairs && is_dir && problem == 0 &&
2927*6a54128fSAndroid Build Coastguard Worker (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
2928*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_UNINIT_DBLOCK, pctx)) {
2929*6a54128fSAndroid Build Coastguard Worker extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
2930*6a54128fSAndroid Build Coastguard Worker pb->inode_modified = 1;
2931*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_extent_replace(ehandle, 0,
2932*6a54128fSAndroid Build Coastguard Worker &extent);
2933*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode)
2934*6a54128fSAndroid Build Coastguard Worker return;
2935*6a54128fSAndroid Build Coastguard Worker failed_csum = 0;
2936*6a54128fSAndroid Build Coastguard Worker }
2937*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_DEVELOPER_FEATURES
2938*6a54128fSAndroid Build Coastguard Worker if (try_repairs && !is_dir && problem == 0 &&
2939*6a54128fSAndroid Build Coastguard Worker (ctx->options & E2F_OPT_CLEAR_UNINIT) &&
2940*6a54128fSAndroid Build Coastguard Worker (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
2941*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_CLEAR_UNINIT_EXTENT, pctx)) {
2942*6a54128fSAndroid Build Coastguard Worker extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
2943*6a54128fSAndroid Build Coastguard Worker pb->inode_modified = 1;
2944*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_extent_replace(ehandle, 0,
2945*6a54128fSAndroid Build Coastguard Worker &extent);
2946*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode)
2947*6a54128fSAndroid Build Coastguard Worker return;
2948*6a54128fSAndroid Build Coastguard Worker failed_csum = 0;
2949*6a54128fSAndroid Build Coastguard Worker }
2950*6a54128fSAndroid Build Coastguard Worker #endif
2951*6a54128fSAndroid Build Coastguard Worker if (try_repairs && problem) {
2952*6a54128fSAndroid Build Coastguard Worker report_problem:
2953*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, problem, pctx)) {
2954*6a54128fSAndroid Build Coastguard Worker if (ctx->invalid_bitmaps) {
2955*6a54128fSAndroid Build Coastguard Worker /*
2956*6a54128fSAndroid Build Coastguard Worker * If fsck knows the bitmaps are bad,
2957*6a54128fSAndroid Build Coastguard Worker * skip to the next extent and
2958*6a54128fSAndroid Build Coastguard Worker * try to clear this extent again
2959*6a54128fSAndroid Build Coastguard Worker * after fixing the bitmaps, by
2960*6a54128fSAndroid Build Coastguard Worker * restarting fsck.
2961*6a54128fSAndroid Build Coastguard Worker */
2962*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_extent_get(
2963*6a54128fSAndroid Build Coastguard Worker ehandle,
2964*6a54128fSAndroid Build Coastguard Worker EXT2_EXTENT_NEXT_SIB,
2965*6a54128fSAndroid Build Coastguard Worker &extent);
2966*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_RESTART_LATER;
2967*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode ==
2968*6a54128fSAndroid Build Coastguard Worker EXT2_ET_NO_CURRENT_NODE) {
2969*6a54128fSAndroid Build Coastguard Worker pctx->errcode = 0;
2970*6a54128fSAndroid Build Coastguard Worker break;
2971*6a54128fSAndroid Build Coastguard Worker }
2972*6a54128fSAndroid Build Coastguard Worker continue;
2973*6a54128fSAndroid Build Coastguard Worker }
2974*6a54128fSAndroid Build Coastguard Worker e2fsck_read_bitmaps(ctx);
2975*6a54128fSAndroid Build Coastguard Worker pb->inode_modified = 1;
2976*6a54128fSAndroid Build Coastguard Worker pctx->errcode =
2977*6a54128fSAndroid Build Coastguard Worker ext2fs_extent_delete(ehandle, 0);
2978*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode) {
2979*6a54128fSAndroid Build Coastguard Worker pctx->str = "ext2fs_extent_delete";
2980*6a54128fSAndroid Build Coastguard Worker return;
2981*6a54128fSAndroid Build Coastguard Worker }
2982*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2983*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode &&
2984*6a54128fSAndroid Build Coastguard Worker pctx->errcode != EXT2_ET_NO_CURRENT_NODE) {
2985*6a54128fSAndroid Build Coastguard Worker pctx->str = "ext2fs_extent_fix_parents";
2986*6a54128fSAndroid Build Coastguard Worker return;
2987*6a54128fSAndroid Build Coastguard Worker }
2988*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_extent_get(ehandle,
2989*6a54128fSAndroid Build Coastguard Worker EXT2_EXTENT_CURRENT,
2990*6a54128fSAndroid Build Coastguard Worker &extent);
2991*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
2992*6a54128fSAndroid Build Coastguard Worker pctx->errcode = 0;
2993*6a54128fSAndroid Build Coastguard Worker break;
2994*6a54128fSAndroid Build Coastguard Worker }
2995*6a54128fSAndroid Build Coastguard Worker failed_csum = 0;
2996*6a54128fSAndroid Build Coastguard Worker continue;
2997*6a54128fSAndroid Build Coastguard Worker }
2998*6a54128fSAndroid Build Coastguard Worker goto next;
2999*6a54128fSAndroid Build Coastguard Worker }
3000*6a54128fSAndroid Build Coastguard Worker
3001*6a54128fSAndroid Build Coastguard Worker if (!is_leaf) {
3002*6a54128fSAndroid Build Coastguard Worker blk64_t lblk = extent.e_lblk;
3003*6a54128fSAndroid Build Coastguard Worker int next_try_repairs = 1;
3004*6a54128fSAndroid Build Coastguard Worker
3005*6a54128fSAndroid Build Coastguard Worker blk = extent.e_pblk;
3006*6a54128fSAndroid Build Coastguard Worker
3007*6a54128fSAndroid Build Coastguard Worker /*
3008*6a54128fSAndroid Build Coastguard Worker * If this lower extent block collides with critical
3009*6a54128fSAndroid Build Coastguard Worker * metadata, don't try to repair the damage. Pass 1b
3010*6a54128fSAndroid Build Coastguard Worker * will reallocate the block; then we can try again.
3011*6a54128fSAndroid Build Coastguard Worker */
3012*6a54128fSAndroid Build Coastguard Worker if (pb->ino != EXT2_RESIZE_INO &&
3013*6a54128fSAndroid Build Coastguard Worker extent.e_pblk < ctx->fs->super->s_blocks_count &&
3014*6a54128fSAndroid Build Coastguard Worker ext2fs_test_block_bitmap2(ctx->block_metadata_map,
3015*6a54128fSAndroid Build Coastguard Worker extent.e_pblk)) {
3016*6a54128fSAndroid Build Coastguard Worker next_try_repairs = 0;
3017*6a54128fSAndroid Build Coastguard Worker pctx->blk = blk;
3018*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx,
3019*6a54128fSAndroid Build Coastguard Worker PR_1_CRITICAL_METADATA_COLLISION,
3020*6a54128fSAndroid Build Coastguard Worker pctx);
3021*6a54128fSAndroid Build Coastguard Worker if ((ctx->options & E2F_OPT_NO) == 0)
3022*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_RESTART_LATER;
3023*6a54128fSAndroid Build Coastguard Worker }
3024*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_extent_get(ehandle,
3025*6a54128fSAndroid Build Coastguard Worker EXT2_EXTENT_DOWN, &extent);
3026*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode &&
3027*6a54128fSAndroid Build Coastguard Worker pctx->errcode != EXT2_ET_EXTENT_CSUM_INVALID) {
3028*6a54128fSAndroid Build Coastguard Worker pctx->str = "EXT2_EXTENT_DOWN";
3029*6a54128fSAndroid Build Coastguard Worker problem = PR_1_EXTENT_HEADER_INVALID;
3030*6a54128fSAndroid Build Coastguard Worker if (!next_try_repairs)
3031*6a54128fSAndroid Build Coastguard Worker return;
3032*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
3033*6a54128fSAndroid Build Coastguard Worker goto report_problem;
3034*6a54128fSAndroid Build Coastguard Worker return;
3035*6a54128fSAndroid Build Coastguard Worker }
3036*6a54128fSAndroid Build Coastguard Worker /* The next extent should match this index's logical start */
3037*6a54128fSAndroid Build Coastguard Worker if (extent.e_lblk != lblk) {
3038*6a54128fSAndroid Build Coastguard Worker struct ext2_extent_info e_info;
3039*6a54128fSAndroid Build Coastguard Worker
3040*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_extent_get_info(ehandle,
3041*6a54128fSAndroid Build Coastguard Worker &e_info);
3042*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode) {
3043*6a54128fSAndroid Build Coastguard Worker pctx->str = "ext2fs_extent_get_info";
3044*6a54128fSAndroid Build Coastguard Worker return;
3045*6a54128fSAndroid Build Coastguard Worker }
3046*6a54128fSAndroid Build Coastguard Worker pctx->blk = lblk;
3047*6a54128fSAndroid Build Coastguard Worker pctx->blk2 = extent.e_lblk;
3048*6a54128fSAndroid Build Coastguard Worker pctx->num = e_info.curr_level - 1;
3049*6a54128fSAndroid Build Coastguard Worker problem = PR_1_EXTENT_INDEX_START_INVALID;
3050*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, problem, pctx)) {
3051*6a54128fSAndroid Build Coastguard Worker pb->inode_modified = 1;
3052*6a54128fSAndroid Build Coastguard Worker pctx->errcode =
3053*6a54128fSAndroid Build Coastguard Worker ext2fs_extent_fix_parents(ehandle);
3054*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode) {
3055*6a54128fSAndroid Build Coastguard Worker pctx->str = "ext2fs_extent_fix_parents";
3056*6a54128fSAndroid Build Coastguard Worker return;
3057*6a54128fSAndroid Build Coastguard Worker }
3058*6a54128fSAndroid Build Coastguard Worker }
3059*6a54128fSAndroid Build Coastguard Worker }
3060*6a54128fSAndroid Build Coastguard Worker scan_extent_node(ctx, pctx, pb, extent.e_lblk,
3061*6a54128fSAndroid Build Coastguard Worker last_lblk, eof_block, ehandle,
3062*6a54128fSAndroid Build Coastguard Worker next_try_repairs);
3063*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode)
3064*6a54128fSAndroid Build Coastguard Worker return;
3065*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_extent_get(ehandle,
3066*6a54128fSAndroid Build Coastguard Worker EXT2_EXTENT_UP, &extent);
3067*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode) {
3068*6a54128fSAndroid Build Coastguard Worker pctx->str = "EXT2_EXTENT_UP";
3069*6a54128fSAndroid Build Coastguard Worker return;
3070*6a54128fSAndroid Build Coastguard Worker }
3071*6a54128fSAndroid Build Coastguard Worker mark_block_used(ctx, blk);
3072*6a54128fSAndroid Build Coastguard Worker pb->num_blocks++;
3073*6a54128fSAndroid Build Coastguard Worker goto next;
3074*6a54128fSAndroid Build Coastguard Worker }
3075*6a54128fSAndroid Build Coastguard Worker
3076*6a54128fSAndroid Build Coastguard Worker if ((pb->previous_block != 0) &&
3077*6a54128fSAndroid Build Coastguard Worker (pb->previous_block+1 != extent.e_pblk)) {
3078*6a54128fSAndroid Build Coastguard Worker if (ctx->options & E2F_OPT_FRAGCHECK) {
3079*6a54128fSAndroid Build Coastguard Worker char type = '?';
3080*6a54128fSAndroid Build Coastguard Worker
3081*6a54128fSAndroid Build Coastguard Worker if (pb->is_dir)
3082*6a54128fSAndroid Build Coastguard Worker type = 'd';
3083*6a54128fSAndroid Build Coastguard Worker else if (pb->is_reg)
3084*6a54128fSAndroid Build Coastguard Worker type = 'f';
3085*6a54128fSAndroid Build Coastguard Worker
3086*6a54128fSAndroid Build Coastguard Worker printf(("%6lu(%c): expecting %6lu "
3087*6a54128fSAndroid Build Coastguard Worker "actual extent "
3088*6a54128fSAndroid Build Coastguard Worker "phys %6lu log %lu len %lu\n"),
3089*6a54128fSAndroid Build Coastguard Worker (unsigned long) pctx->ino, type,
3090*6a54128fSAndroid Build Coastguard Worker (unsigned long) pb->previous_block+1,
3091*6a54128fSAndroid Build Coastguard Worker (unsigned long) extent.e_pblk,
3092*6a54128fSAndroid Build Coastguard Worker (unsigned long) extent.e_lblk,
3093*6a54128fSAndroid Build Coastguard Worker (unsigned long) extent.e_len);
3094*6a54128fSAndroid Build Coastguard Worker }
3095*6a54128fSAndroid Build Coastguard Worker pb->fragmented = 1;
3096*6a54128fSAndroid Build Coastguard Worker }
3097*6a54128fSAndroid Build Coastguard Worker /*
3098*6a54128fSAndroid Build Coastguard Worker * If we notice a gap in the logical block mappings of an
3099*6a54128fSAndroid Build Coastguard Worker * extent-mapped directory, offer to close the hole by
3100*6a54128fSAndroid Build Coastguard Worker * moving the logical block down, otherwise we'll go mad in
3101*6a54128fSAndroid Build Coastguard Worker * pass 3 allocating empty directory blocks to fill the hole.
3102*6a54128fSAndroid Build Coastguard Worker */
3103*6a54128fSAndroid Build Coastguard Worker if (try_repairs && is_dir &&
3104*6a54128fSAndroid Build Coastguard Worker pb->last_block + 1 < extent.e_lblk) {
3105*6a54128fSAndroid Build Coastguard Worker blk64_t new_lblk;
3106*6a54128fSAndroid Build Coastguard Worker
3107*6a54128fSAndroid Build Coastguard Worker new_lblk = pb->last_block + 1;
3108*6a54128fSAndroid Build Coastguard Worker if (EXT2FS_CLUSTER_RATIO(ctx->fs) > 1)
3109*6a54128fSAndroid Build Coastguard Worker new_lblk = ((new_lblk +
3110*6a54128fSAndroid Build Coastguard Worker EXT2FS_CLUSTER_RATIO(ctx->fs) - 1) &
3111*6a54128fSAndroid Build Coastguard Worker ~EXT2FS_CLUSTER_MASK(ctx->fs)) |
3112*6a54128fSAndroid Build Coastguard Worker (extent.e_pblk &
3113*6a54128fSAndroid Build Coastguard Worker EXT2FS_CLUSTER_MASK(ctx->fs));
3114*6a54128fSAndroid Build Coastguard Worker pctx->blk = extent.e_lblk;
3115*6a54128fSAndroid Build Coastguard Worker pctx->blk2 = new_lblk;
3116*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_COLLAPSE_DBLOCK, pctx)) {
3117*6a54128fSAndroid Build Coastguard Worker extent.e_lblk = new_lblk;
3118*6a54128fSAndroid Build Coastguard Worker pb->inode_modified = 1;
3119*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_extent_replace(ehandle,
3120*6a54128fSAndroid Build Coastguard Worker 0, &extent);
3121*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode) {
3122*6a54128fSAndroid Build Coastguard Worker pctx->errcode = 0;
3123*6a54128fSAndroid Build Coastguard Worker goto alloc_later;
3124*6a54128fSAndroid Build Coastguard Worker }
3125*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_extent_fix_parents(ehandle);
3126*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode)
3127*6a54128fSAndroid Build Coastguard Worker goto failed_add_dir_block;
3128*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_extent_goto(ehandle,
3129*6a54128fSAndroid Build Coastguard Worker extent.e_lblk);
3130*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode)
3131*6a54128fSAndroid Build Coastguard Worker goto failed_add_dir_block;
3132*6a54128fSAndroid Build Coastguard Worker last_lblk = extent.e_lblk + extent.e_len - 1;
3133*6a54128fSAndroid Build Coastguard Worker failed_csum = 0;
3134*6a54128fSAndroid Build Coastguard Worker }
3135*6a54128fSAndroid Build Coastguard Worker }
3136*6a54128fSAndroid Build Coastguard Worker alloc_later:
3137*6a54128fSAndroid Build Coastguard Worker if (is_dir) {
3138*6a54128fSAndroid Build Coastguard Worker while (++pb->last_db_block <
3139*6a54128fSAndroid Build Coastguard Worker (e2_blkcnt_t) extent.e_lblk) {
3140*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_add_dir_block2(
3141*6a54128fSAndroid Build Coastguard Worker ctx->fs->dblist,
3142*6a54128fSAndroid Build Coastguard Worker pb->ino, 0,
3143*6a54128fSAndroid Build Coastguard Worker pb->last_db_block);
3144*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode) {
3145*6a54128fSAndroid Build Coastguard Worker pctx->blk = 0;
3146*6a54128fSAndroid Build Coastguard Worker pctx->num = pb->last_db_block;
3147*6a54128fSAndroid Build Coastguard Worker goto failed_add_dir_block;
3148*6a54128fSAndroid Build Coastguard Worker }
3149*6a54128fSAndroid Build Coastguard Worker }
3150*6a54128fSAndroid Build Coastguard Worker
3151*6a54128fSAndroid Build Coastguard Worker for (i = 0; i < extent.e_len; i++) {
3152*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_add_dir_block2(
3153*6a54128fSAndroid Build Coastguard Worker ctx->fs->dblist,
3154*6a54128fSAndroid Build Coastguard Worker pctx->ino,
3155*6a54128fSAndroid Build Coastguard Worker extent.e_pblk + i,
3156*6a54128fSAndroid Build Coastguard Worker extent.e_lblk + i);
3157*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode) {
3158*6a54128fSAndroid Build Coastguard Worker pctx->blk = extent.e_pblk + i;
3159*6a54128fSAndroid Build Coastguard Worker pctx->num = extent.e_lblk + i;
3160*6a54128fSAndroid Build Coastguard Worker failed_add_dir_block:
3161*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
3162*6a54128fSAndroid Build Coastguard Worker /* Should never get here */
3163*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
3164*6a54128fSAndroid Build Coastguard Worker return;
3165*6a54128fSAndroid Build Coastguard Worker }
3166*6a54128fSAndroid Build Coastguard Worker }
3167*6a54128fSAndroid Build Coastguard Worker if (extent.e_len > 0)
3168*6a54128fSAndroid Build Coastguard Worker pb->last_db_block = extent.e_lblk + extent.e_len - 1;
3169*6a54128fSAndroid Build Coastguard Worker }
3170*6a54128fSAndroid Build Coastguard Worker if (has_unaligned_cluster_map(ctx, pb->previous_block,
3171*6a54128fSAndroid Build Coastguard Worker pb->last_block,
3172*6a54128fSAndroid Build Coastguard Worker extent.e_pblk,
3173*6a54128fSAndroid Build Coastguard Worker extent.e_lblk)) {
3174*6a54128fSAndroid Build Coastguard Worker for (i = 0; i < extent.e_len; i++) {
3175*6a54128fSAndroid Build Coastguard Worker pctx->blk = extent.e_lblk + i;
3176*6a54128fSAndroid Build Coastguard Worker pctx->blk2 = extent.e_pblk + i;
3177*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
3178*6a54128fSAndroid Build Coastguard Worker mark_block_used(ctx, extent.e_pblk + i);
3179*6a54128fSAndroid Build Coastguard Worker mark_block_used(ctx, extent.e_pblk + i);
3180*6a54128fSAndroid Build Coastguard Worker }
3181*6a54128fSAndroid Build Coastguard Worker }
3182*6a54128fSAndroid Build Coastguard Worker
3183*6a54128fSAndroid Build Coastguard Worker /*
3184*6a54128fSAndroid Build Coastguard Worker * Check whether first cluster got marked in previous iteration.
3185*6a54128fSAndroid Build Coastguard Worker */
3186*6a54128fSAndroid Build Coastguard Worker if (ctx->fs->cluster_ratio_bits &&
3187*6a54128fSAndroid Build Coastguard Worker pb->previous_block &&
3188*6a54128fSAndroid Build Coastguard Worker (EXT2FS_B2C(ctx->fs, extent.e_pblk) ==
3189*6a54128fSAndroid Build Coastguard Worker EXT2FS_B2C(ctx->fs, pb->previous_block)))
3190*6a54128fSAndroid Build Coastguard Worker /* Set blk to the beginning of next cluster. */
3191*6a54128fSAndroid Build Coastguard Worker blk = EXT2FS_C2B(
3192*6a54128fSAndroid Build Coastguard Worker ctx->fs,
3193*6a54128fSAndroid Build Coastguard Worker EXT2FS_B2C(ctx->fs, extent.e_pblk) + 1);
3194*6a54128fSAndroid Build Coastguard Worker else
3195*6a54128fSAndroid Build Coastguard Worker /* Set blk to the beginning of current cluster. */
3196*6a54128fSAndroid Build Coastguard Worker blk = EXT2FS_C2B(ctx->fs,
3197*6a54128fSAndroid Build Coastguard Worker EXT2FS_B2C(ctx->fs, extent.e_pblk));
3198*6a54128fSAndroid Build Coastguard Worker
3199*6a54128fSAndroid Build Coastguard Worker if (blk < extent.e_pblk + extent.e_len) {
3200*6a54128fSAndroid Build Coastguard Worker mark_blocks_used(ctx, blk,
3201*6a54128fSAndroid Build Coastguard Worker extent.e_pblk + extent.e_len - blk);
3202*6a54128fSAndroid Build Coastguard Worker n = DIV_ROUND_UP(extent.e_pblk + extent.e_len - blk,
3203*6a54128fSAndroid Build Coastguard Worker EXT2FS_CLUSTER_RATIO(ctx->fs));
3204*6a54128fSAndroid Build Coastguard Worker pb->num_blocks += n;
3205*6a54128fSAndroid Build Coastguard Worker }
3206*6a54128fSAndroid Build Coastguard Worker pb->last_block = extent.e_lblk + extent.e_len - 1;
3207*6a54128fSAndroid Build Coastguard Worker pb->previous_block = extent.e_pblk + extent.e_len - 1;
3208*6a54128fSAndroid Build Coastguard Worker start_block = pb->last_block = last_lblk;
3209*6a54128fSAndroid Build Coastguard Worker if (is_leaf && !is_dir &&
3210*6a54128fSAndroid Build Coastguard Worker !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
3211*6a54128fSAndroid Build Coastguard Worker pb->last_init_lblock = last_lblk;
3212*6a54128fSAndroid Build Coastguard Worker next:
3213*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_extent_get(ehandle,
3214*6a54128fSAndroid Build Coastguard Worker EXT2_EXTENT_NEXT_SIB,
3215*6a54128fSAndroid Build Coastguard Worker &extent);
3216*6a54128fSAndroid Build Coastguard Worker }
3217*6a54128fSAndroid Build Coastguard Worker
3218*6a54128fSAndroid Build Coastguard Worker /* Failed csum but passes checks? Ask to fix checksum. */
3219*6a54128fSAndroid Build Coastguard Worker if (failed_csum &&
3220*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_EXTENT_ONLY_CSUM_INVALID, pctx)) {
3221*6a54128fSAndroid Build Coastguard Worker pb->inode_modified = 1;
3222*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_extent_replace(ehandle, 0, &extent);
3223*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode)
3224*6a54128fSAndroid Build Coastguard Worker return;
3225*6a54128fSAndroid Build Coastguard Worker }
3226*6a54128fSAndroid Build Coastguard Worker
3227*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
3228*6a54128fSAndroid Build Coastguard Worker pctx->errcode = 0;
3229*6a54128fSAndroid Build Coastguard Worker }
3230*6a54128fSAndroid Build Coastguard Worker
check_blocks_extents(e2fsck_t ctx,struct problem_context * pctx,struct process_block_struct * pb)3231*6a54128fSAndroid Build Coastguard Worker static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
3232*6a54128fSAndroid Build Coastguard Worker struct process_block_struct *pb)
3233*6a54128fSAndroid Build Coastguard Worker {
3234*6a54128fSAndroid Build Coastguard Worker struct ext2_extent_info info;
3235*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode = pctx->inode;
3236*6a54128fSAndroid Build Coastguard Worker ext2_extent_handle_t ehandle;
3237*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs = ctx->fs;
3238*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino = pctx->ino;
3239*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
3240*6a54128fSAndroid Build Coastguard Worker blk64_t eof_lblk;
3241*6a54128fSAndroid Build Coastguard Worker struct ext3_extent_header *eh;
3242*6a54128fSAndroid Build Coastguard Worker
3243*6a54128fSAndroid Build Coastguard Worker /* Check for a proper extent header... */
3244*6a54128fSAndroid Build Coastguard Worker eh = (struct ext3_extent_header *) &inode->i_block[0];
3245*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_header_verify(eh, sizeof(inode->i_block));
3246*6a54128fSAndroid Build Coastguard Worker if (retval) {
3247*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_MISSING_EXTENT_HEADER, pctx))
3248*6a54128fSAndroid Build Coastguard Worker e2fsck_clear_inode(ctx, ino, inode, 0,
3249*6a54128fSAndroid Build Coastguard Worker "check_blocks_extents");
3250*6a54128fSAndroid Build Coastguard Worker pctx->errcode = 0;
3251*6a54128fSAndroid Build Coastguard Worker return;
3252*6a54128fSAndroid Build Coastguard Worker }
3253*6a54128fSAndroid Build Coastguard Worker
3254*6a54128fSAndroid Build Coastguard Worker /* ...since this function doesn't fail if i_block is zeroed. */
3255*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
3256*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode) {
3257*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
3258*6a54128fSAndroid Build Coastguard Worker e2fsck_clear_inode(ctx, ino, inode, 0,
3259*6a54128fSAndroid Build Coastguard Worker "check_blocks_extents");
3260*6a54128fSAndroid Build Coastguard Worker pctx->errcode = 0;
3261*6a54128fSAndroid Build Coastguard Worker return;
3262*6a54128fSAndroid Build Coastguard Worker }
3263*6a54128fSAndroid Build Coastguard Worker
3264*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get_info(ehandle, &info);
3265*6a54128fSAndroid Build Coastguard Worker if (retval == 0) {
3266*6a54128fSAndroid Build Coastguard Worker int max_depth = info.max_depth;
3267*6a54128fSAndroid Build Coastguard Worker
3268*6a54128fSAndroid Build Coastguard Worker if (max_depth >= MAX_EXTENT_DEPTH_COUNT)
3269*6a54128fSAndroid Build Coastguard Worker max_depth = MAX_EXTENT_DEPTH_COUNT-1;
3270*6a54128fSAndroid Build Coastguard Worker ctx->extent_depth_count[max_depth]++;
3271*6a54128fSAndroid Build Coastguard Worker }
3272*6a54128fSAndroid Build Coastguard Worker
3273*6a54128fSAndroid Build Coastguard Worker /* Check maximum extent depth */
3274*6a54128fSAndroid Build Coastguard Worker pctx->blk = info.max_depth;
3275*6a54128fSAndroid Build Coastguard Worker pctx->blk2 = ext2fs_max_extent_depth(ehandle);
3276*6a54128fSAndroid Build Coastguard Worker if (pctx->blk2 < pctx->blk &&
3277*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_EXTENT_BAD_MAX_DEPTH, pctx))
3278*6a54128fSAndroid Build Coastguard Worker pb->eti.force_rebuild = 1;
3279*6a54128fSAndroid Build Coastguard Worker
3280*6a54128fSAndroid Build Coastguard Worker /* Can we collect extent tree level stats? */
3281*6a54128fSAndroid Build Coastguard Worker pctx->blk = MAX_EXTENT_DEPTH_COUNT;
3282*6a54128fSAndroid Build Coastguard Worker if (pctx->blk2 > pctx->blk)
3283*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1E_MAX_EXTENT_TREE_DEPTH, pctx);
3284*6a54128fSAndroid Build Coastguard Worker memset(pb->eti.ext_info, 0, sizeof(pb->eti.ext_info));
3285*6a54128fSAndroid Build Coastguard Worker pb->eti.ino = pb->ino;
3286*6a54128fSAndroid Build Coastguard Worker
3287*6a54128fSAndroid Build Coastguard Worker pb->next_lblock = 0;
3288*6a54128fSAndroid Build Coastguard Worker
3289*6a54128fSAndroid Build Coastguard Worker eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
3290*6a54128fSAndroid Build Coastguard Worker EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
3291*6a54128fSAndroid Build Coastguard Worker scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle, 1);
3292*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode &&
3293*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
3294*6a54128fSAndroid Build Coastguard Worker pb->num_blocks = 0;
3295*6a54128fSAndroid Build Coastguard Worker inode->i_blocks = 0;
3296*6a54128fSAndroid Build Coastguard Worker e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
3297*6a54128fSAndroid Build Coastguard Worker "check_blocks_extents");
3298*6a54128fSAndroid Build Coastguard Worker pctx->errcode = 0;
3299*6a54128fSAndroid Build Coastguard Worker }
3300*6a54128fSAndroid Build Coastguard Worker ext2fs_extent_free(ehandle);
3301*6a54128fSAndroid Build Coastguard Worker
3302*6a54128fSAndroid Build Coastguard Worker /* Rebuild unless it's a dir and we're rehashing it */
3303*6a54128fSAndroid Build Coastguard Worker if (LINUX_S_ISDIR(inode->i_mode) &&
3304*6a54128fSAndroid Build Coastguard Worker e2fsck_dir_will_be_rehashed(ctx, ino))
3305*6a54128fSAndroid Build Coastguard Worker return;
3306*6a54128fSAndroid Build Coastguard Worker
3307*6a54128fSAndroid Build Coastguard Worker if (ctx->options & E2F_OPT_CONVERT_BMAP)
3308*6a54128fSAndroid Build Coastguard Worker e2fsck_rebuild_extents_later(ctx, ino);
3309*6a54128fSAndroid Build Coastguard Worker else
3310*6a54128fSAndroid Build Coastguard Worker e2fsck_should_rebuild_extents(ctx, pctx, &pb->eti, &info);
3311*6a54128fSAndroid Build Coastguard Worker }
3312*6a54128fSAndroid Build Coastguard Worker
3313*6a54128fSAndroid Build Coastguard Worker /*
3314*6a54128fSAndroid Build Coastguard Worker * In fact we don't need to check blocks for an inode with inline data
3315*6a54128fSAndroid Build Coastguard Worker * because this inode doesn't have any blocks. In this function all
3316*6a54128fSAndroid Build Coastguard Worker * we need to do is add this inode into dblist when it is a directory.
3317*6a54128fSAndroid Build Coastguard Worker */
check_blocks_inline_data(e2fsck_t ctx,struct problem_context * pctx,struct process_block_struct * pb)3318*6a54128fSAndroid Build Coastguard Worker static void check_blocks_inline_data(e2fsck_t ctx, struct problem_context *pctx,
3319*6a54128fSAndroid Build Coastguard Worker struct process_block_struct *pb)
3320*6a54128fSAndroid Build Coastguard Worker {
3321*6a54128fSAndroid Build Coastguard Worker int flags;
3322*6a54128fSAndroid Build Coastguard Worker size_t inline_data_size = 0;
3323*6a54128fSAndroid Build Coastguard Worker
3324*6a54128fSAndroid Build Coastguard Worker if (!pb->is_dir) {
3325*6a54128fSAndroid Build Coastguard Worker pctx->errcode = 0;
3326*6a54128fSAndroid Build Coastguard Worker return;
3327*6a54128fSAndroid Build Coastguard Worker }
3328*6a54128fSAndroid Build Coastguard Worker
3329*6a54128fSAndroid Build Coastguard Worker /* Process the dirents in i_block[] as the "first" block. */
3330*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 0);
3331*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode)
3332*6a54128fSAndroid Build Coastguard Worker goto err;
3333*6a54128fSAndroid Build Coastguard Worker
3334*6a54128fSAndroid Build Coastguard Worker /* Process the dirents in the EA as a "second" block. */
3335*6a54128fSAndroid Build Coastguard Worker flags = ctx->fs->flags;
3336*6a54128fSAndroid Build Coastguard Worker ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
3337*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_inline_data_size(ctx->fs, pb->ino,
3338*6a54128fSAndroid Build Coastguard Worker &inline_data_size);
3339*6a54128fSAndroid Build Coastguard Worker ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
3340*6a54128fSAndroid Build Coastguard Worker (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
3341*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode) {
3342*6a54128fSAndroid Build Coastguard Worker pctx->errcode = 0;
3343*6a54128fSAndroid Build Coastguard Worker return;
3344*6a54128fSAndroid Build Coastguard Worker }
3345*6a54128fSAndroid Build Coastguard Worker
3346*6a54128fSAndroid Build Coastguard Worker if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE)
3347*6a54128fSAndroid Build Coastguard Worker return;
3348*6a54128fSAndroid Build Coastguard Worker
3349*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 1);
3350*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode)
3351*6a54128fSAndroid Build Coastguard Worker goto err;
3352*6a54128fSAndroid Build Coastguard Worker
3353*6a54128fSAndroid Build Coastguard Worker return;
3354*6a54128fSAndroid Build Coastguard Worker err:
3355*6a54128fSAndroid Build Coastguard Worker pctx->blk = 0;
3356*6a54128fSAndroid Build Coastguard Worker pctx->num = 0;
3357*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
3358*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
3359*6a54128fSAndroid Build Coastguard Worker }
3360*6a54128fSAndroid Build Coastguard Worker
3361*6a54128fSAndroid Build Coastguard Worker /*
3362*6a54128fSAndroid Build Coastguard Worker * This subroutine is called on each inode to account for all of the
3363*6a54128fSAndroid Build Coastguard Worker * blocks used by that inode.
3364*6a54128fSAndroid Build Coastguard Worker */
check_blocks(e2fsck_t ctx,struct problem_context * pctx,char * block_buf,const struct ea_quota * ea_ibody_quota)3365*6a54128fSAndroid Build Coastguard Worker static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
3366*6a54128fSAndroid Build Coastguard Worker char *block_buf, const struct ea_quota *ea_ibody_quota)
3367*6a54128fSAndroid Build Coastguard Worker {
3368*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs = ctx->fs;
3369*6a54128fSAndroid Build Coastguard Worker struct process_block_struct pb;
3370*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino = pctx->ino;
3371*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode = pctx->inode;
3372*6a54128fSAndroid Build Coastguard Worker unsigned bad_size = 0;
3373*6a54128fSAndroid Build Coastguard Worker int dirty_inode = 0;
3374*6a54128fSAndroid Build Coastguard Worker int extent_fs;
3375*6a54128fSAndroid Build Coastguard Worker int inlinedata_fs;
3376*6a54128fSAndroid Build Coastguard Worker __u64 size;
3377*6a54128fSAndroid Build Coastguard Worker struct ea_quota ea_block_quota;
3378*6a54128fSAndroid Build Coastguard Worker
3379*6a54128fSAndroid Build Coastguard Worker pb.ino = ino;
3380*6a54128fSAndroid Build Coastguard Worker pb.num_blocks = EXT2FS_B2C(ctx->fs,
3381*6a54128fSAndroid Build Coastguard Worker ea_ibody_quota ? ea_ibody_quota->blocks : 0);
3382*6a54128fSAndroid Build Coastguard Worker pb.last_block = ~0;
3383*6a54128fSAndroid Build Coastguard Worker pb.last_init_lblock = -1;
3384*6a54128fSAndroid Build Coastguard Worker pb.last_db_block = -1;
3385*6a54128fSAndroid Build Coastguard Worker pb.num_illegal_blocks = 0;
3386*6a54128fSAndroid Build Coastguard Worker pb.suppress = 0; pb.clear = 0;
3387*6a54128fSAndroid Build Coastguard Worker pb.fragmented = 0;
3388*6a54128fSAndroid Build Coastguard Worker pb.compressed = 0;
3389*6a54128fSAndroid Build Coastguard Worker pb.previous_block = 0;
3390*6a54128fSAndroid Build Coastguard Worker pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
3391*6a54128fSAndroid Build Coastguard Worker pb.is_reg = LINUX_S_ISREG(inode->i_mode);
3392*6a54128fSAndroid Build Coastguard Worker pb.max_blocks = 1U << (31 - fs->super->s_log_block_size);
3393*6a54128fSAndroid Build Coastguard Worker pb.inode = inode;
3394*6a54128fSAndroid Build Coastguard Worker pb.pctx = pctx;
3395*6a54128fSAndroid Build Coastguard Worker pb.ctx = ctx;
3396*6a54128fSAndroid Build Coastguard Worker pb.inode_modified = 0;
3397*6a54128fSAndroid Build Coastguard Worker pb.eti.force_rebuild = 0;
3398*6a54128fSAndroid Build Coastguard Worker pctx->ino = ino;
3399*6a54128fSAndroid Build Coastguard Worker pctx->errcode = 0;
3400*6a54128fSAndroid Build Coastguard Worker
3401*6a54128fSAndroid Build Coastguard Worker extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
3402*6a54128fSAndroid Build Coastguard Worker inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
3403*6a54128fSAndroid Build Coastguard Worker
3404*6a54128fSAndroid Build Coastguard Worker if (check_ext_attr(ctx, pctx, block_buf, &ea_block_quota)) {
3405*6a54128fSAndroid Build Coastguard Worker if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3406*6a54128fSAndroid Build Coastguard Worker goto out;
3407*6a54128fSAndroid Build Coastguard Worker pb.num_blocks += EXT2FS_B2C(ctx->fs, ea_block_quota.blocks);
3408*6a54128fSAndroid Build Coastguard Worker }
3409*6a54128fSAndroid Build Coastguard Worker
3410*6a54128fSAndroid Build Coastguard Worker if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL))
3411*6a54128fSAndroid Build Coastguard Worker check_blocks_inline_data(ctx, pctx, &pb);
3412*6a54128fSAndroid Build Coastguard Worker else if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
3413*6a54128fSAndroid Build Coastguard Worker if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
3414*6a54128fSAndroid Build Coastguard Worker check_blocks_extents(ctx, pctx, &pb);
3415*6a54128fSAndroid Build Coastguard Worker else {
3416*6a54128fSAndroid Build Coastguard Worker int flags;
3417*6a54128fSAndroid Build Coastguard Worker /*
3418*6a54128fSAndroid Build Coastguard Worker * If we've modified the inode, write it out before
3419*6a54128fSAndroid Build Coastguard Worker * iterate() tries to use it.
3420*6a54128fSAndroid Build Coastguard Worker */
3421*6a54128fSAndroid Build Coastguard Worker if (dirty_inode) {
3422*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, ino, inode,
3423*6a54128fSAndroid Build Coastguard Worker "check_blocks");
3424*6a54128fSAndroid Build Coastguard Worker dirty_inode = 0;
3425*6a54128fSAndroid Build Coastguard Worker }
3426*6a54128fSAndroid Build Coastguard Worker flags = fs->flags;
3427*6a54128fSAndroid Build Coastguard Worker fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
3428*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_block_iterate3(fs, ino,
3429*6a54128fSAndroid Build Coastguard Worker pb.is_dir ? BLOCK_FLAG_HOLE : 0,
3430*6a54128fSAndroid Build Coastguard Worker block_buf, process_block, &pb);
3431*6a54128fSAndroid Build Coastguard Worker /*
3432*6a54128fSAndroid Build Coastguard Worker * We do not have uninitialized extents in non extent
3433*6a54128fSAndroid Build Coastguard Worker * files.
3434*6a54128fSAndroid Build Coastguard Worker */
3435*6a54128fSAndroid Build Coastguard Worker pb.last_init_lblock = pb.last_block;
3436*6a54128fSAndroid Build Coastguard Worker /*
3437*6a54128fSAndroid Build Coastguard Worker * If iterate() changed a block mapping, we have to
3438*6a54128fSAndroid Build Coastguard Worker * re-read the inode. If we decide to clear the
3439*6a54128fSAndroid Build Coastguard Worker * inode after clearing some stuff, we'll re-write the
3440*6a54128fSAndroid Build Coastguard Worker * bad mappings into the inode!
3441*6a54128fSAndroid Build Coastguard Worker */
3442*6a54128fSAndroid Build Coastguard Worker if (pb.inode_modified)
3443*6a54128fSAndroid Build Coastguard Worker e2fsck_read_inode(ctx, ino, inode,
3444*6a54128fSAndroid Build Coastguard Worker "check_blocks");
3445*6a54128fSAndroid Build Coastguard Worker fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
3446*6a54128fSAndroid Build Coastguard Worker (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
3447*6a54128fSAndroid Build Coastguard Worker
3448*6a54128fSAndroid Build Coastguard Worker if (ctx->options & E2F_OPT_CONVERT_BMAP) {
3449*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
3450*6a54128fSAndroid Build Coastguard Worker printf("bmap rebuild ino=%d\n", ino);
3451*6a54128fSAndroid Build Coastguard Worker #endif
3452*6a54128fSAndroid Build Coastguard Worker if (!LINUX_S_ISDIR(inode->i_mode) ||
3453*6a54128fSAndroid Build Coastguard Worker !e2fsck_dir_will_be_rehashed(ctx, ino))
3454*6a54128fSAndroid Build Coastguard Worker e2fsck_rebuild_extents_later(ctx, ino);
3455*6a54128fSAndroid Build Coastguard Worker }
3456*6a54128fSAndroid Build Coastguard Worker }
3457*6a54128fSAndroid Build Coastguard Worker }
3458*6a54128fSAndroid Build Coastguard Worker end_problem_latch(ctx, PR_LATCH_BLOCK);
3459*6a54128fSAndroid Build Coastguard Worker end_problem_latch(ctx, PR_LATCH_TOOBIG);
3460*6a54128fSAndroid Build Coastguard Worker if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3461*6a54128fSAndroid Build Coastguard Worker goto out;
3462*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode)
3463*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
3464*6a54128fSAndroid Build Coastguard Worker
3465*6a54128fSAndroid Build Coastguard Worker if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
3466*6a54128fSAndroid Build Coastguard Worker if (LINUX_S_ISDIR(inode->i_mode))
3467*6a54128fSAndroid Build Coastguard Worker ctx->fs_fragmented_dir++;
3468*6a54128fSAndroid Build Coastguard Worker else
3469*6a54128fSAndroid Build Coastguard Worker ctx->fs_fragmented++;
3470*6a54128fSAndroid Build Coastguard Worker }
3471*6a54128fSAndroid Build Coastguard Worker
3472*6a54128fSAndroid Build Coastguard Worker if (pb.clear) {
3473*6a54128fSAndroid Build Coastguard Worker e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
3474*6a54128fSAndroid Build Coastguard Worker "check_blocks");
3475*6a54128fSAndroid Build Coastguard Worker return;
3476*6a54128fSAndroid Build Coastguard Worker }
3477*6a54128fSAndroid Build Coastguard Worker
3478*6a54128fSAndroid Build Coastguard Worker if (inode->i_flags & EXT2_INDEX_FL) {
3479*6a54128fSAndroid Build Coastguard Worker if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
3480*6a54128fSAndroid Build Coastguard Worker inode->i_flags &= ~EXT2_INDEX_FL;
3481*6a54128fSAndroid Build Coastguard Worker dirty_inode++;
3482*6a54128fSAndroid Build Coastguard Worker } else {
3483*6a54128fSAndroid Build Coastguard Worker e2fsck_add_dx_dir(ctx, ino, inode, pb.last_block+1);
3484*6a54128fSAndroid Build Coastguard Worker }
3485*6a54128fSAndroid Build Coastguard Worker }
3486*6a54128fSAndroid Build Coastguard Worker
3487*6a54128fSAndroid Build Coastguard Worker if (!pb.num_blocks && pb.is_dir &&
3488*6a54128fSAndroid Build Coastguard Worker !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
3489*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
3490*6a54128fSAndroid Build Coastguard Worker e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
3491*6a54128fSAndroid Build Coastguard Worker ctx->fs_directory_count--;
3492*6a54128fSAndroid Build Coastguard Worker return;
3493*6a54128fSAndroid Build Coastguard Worker }
3494*6a54128fSAndroid Build Coastguard Worker }
3495*6a54128fSAndroid Build Coastguard Worker
3496*6a54128fSAndroid Build Coastguard Worker if (ino != quota_type2inum(PRJQUOTA, fs->super) &&
3497*6a54128fSAndroid Build Coastguard Worker (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) &&
3498*6a54128fSAndroid Build Coastguard Worker !(inode->i_flags & EXT4_EA_INODE_FL)) {
3499*6a54128fSAndroid Build Coastguard Worker quota_data_add(ctx->qctx, (struct ext2_inode_large *) inode,
3500*6a54128fSAndroid Build Coastguard Worker ino,
3501*6a54128fSAndroid Build Coastguard Worker pb.num_blocks * EXT2_CLUSTER_SIZE(fs->super));
3502*6a54128fSAndroid Build Coastguard Worker quota_data_inodes(ctx->qctx, (struct ext2_inode_large *) inode,
3503*6a54128fSAndroid Build Coastguard Worker ino, (ea_ibody_quota ?
3504*6a54128fSAndroid Build Coastguard Worker ea_ibody_quota->inodes : 0) +
3505*6a54128fSAndroid Build Coastguard Worker ea_block_quota.inodes + 1);
3506*6a54128fSAndroid Build Coastguard Worker }
3507*6a54128fSAndroid Build Coastguard Worker
3508*6a54128fSAndroid Build Coastguard Worker if (!ext2fs_has_feature_huge_file(fs->super) ||
3509*6a54128fSAndroid Build Coastguard Worker !(inode->i_flags & EXT4_HUGE_FILE_FL))
3510*6a54128fSAndroid Build Coastguard Worker pb.num_blocks *= (fs->blocksize / 512);
3511*6a54128fSAndroid Build Coastguard Worker pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
3512*6a54128fSAndroid Build Coastguard Worker #if 0
3513*6a54128fSAndroid Build Coastguard Worker printf("inode %u, i_size = %u, last_block = %llu, i_blocks=%llu, num_blocks = %llu\n",
3514*6a54128fSAndroid Build Coastguard Worker ino, inode->i_size, (unsigned long long) pb.last_block,
3515*6a54128fSAndroid Build Coastguard Worker (unsigned long long) ext2fs_inode_i_blocks(fs, inode),
3516*6a54128fSAndroid Build Coastguard Worker (unsigned long long) pb.num_blocks);
3517*6a54128fSAndroid Build Coastguard Worker #endif
3518*6a54128fSAndroid Build Coastguard Worker size = EXT2_I_SIZE(inode);
3519*6a54128fSAndroid Build Coastguard Worker if (pb.is_dir) {
3520*6a54128fSAndroid Build Coastguard Worker unsigned nblock = size >> EXT2_BLOCK_SIZE_BITS(fs->super);
3521*6a54128fSAndroid Build Coastguard Worker if (inode->i_flags & EXT4_INLINE_DATA_FL) {
3522*6a54128fSAndroid Build Coastguard Worker int flags;
3523*6a54128fSAndroid Build Coastguard Worker size_t sz = 0;
3524*6a54128fSAndroid Build Coastguard Worker errcode_t err;
3525*6a54128fSAndroid Build Coastguard Worker
3526*6a54128fSAndroid Build Coastguard Worker flags = ctx->fs->flags;
3527*6a54128fSAndroid Build Coastguard Worker ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
3528*6a54128fSAndroid Build Coastguard Worker err = ext2fs_inline_data_size(ctx->fs, pctx->ino,
3529*6a54128fSAndroid Build Coastguard Worker &sz);
3530*6a54128fSAndroid Build Coastguard Worker ctx->fs->flags = (flags &
3531*6a54128fSAndroid Build Coastguard Worker EXT2_FLAG_IGNORE_CSUM_ERRORS) |
3532*6a54128fSAndroid Build Coastguard Worker (ctx->fs->flags &
3533*6a54128fSAndroid Build Coastguard Worker ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
3534*6a54128fSAndroid Build Coastguard Worker if (err || sz != size) {
3535*6a54128fSAndroid Build Coastguard Worker bad_size = 7;
3536*6a54128fSAndroid Build Coastguard Worker pctx->num = sz;
3537*6a54128fSAndroid Build Coastguard Worker }
3538*6a54128fSAndroid Build Coastguard Worker } else if (size & (fs->blocksize - 1))
3539*6a54128fSAndroid Build Coastguard Worker bad_size = 5;
3540*6a54128fSAndroid Build Coastguard Worker else if (nblock > (pb.last_block + 1))
3541*6a54128fSAndroid Build Coastguard Worker bad_size = 1;
3542*6a54128fSAndroid Build Coastguard Worker else if (nblock < (pb.last_block + 1)) {
3543*6a54128fSAndroid Build Coastguard Worker if (((pb.last_block + 1) - nblock) >
3544*6a54128fSAndroid Build Coastguard Worker fs->super->s_prealloc_dir_blocks)
3545*6a54128fSAndroid Build Coastguard Worker bad_size = 2;
3546*6a54128fSAndroid Build Coastguard Worker }
3547*6a54128fSAndroid Build Coastguard Worker } else {
3548*6a54128fSAndroid Build Coastguard Worker if ((pb.last_init_lblock >= 0) &&
3549*6a54128fSAndroid Build Coastguard Worker /* Do not allow initialized allocated blocks past i_size*/
3550*6a54128fSAndroid Build Coastguard Worker (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
3551*6a54128fSAndroid Build Coastguard Worker !(inode->i_flags & EXT4_VERITY_FL))
3552*6a54128fSAndroid Build Coastguard Worker bad_size = 3;
3553*6a54128fSAndroid Build Coastguard Worker else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
3554*6a54128fSAndroid Build Coastguard Worker size > ext2_max_sizes[fs->super->s_log_block_size])
3555*6a54128fSAndroid Build Coastguard Worker /* too big for a direct/indirect-mapped file */
3556*6a54128fSAndroid Build Coastguard Worker bad_size = 4;
3557*6a54128fSAndroid Build Coastguard Worker else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
3558*6a54128fSAndroid Build Coastguard Worker size >
3559*6a54128fSAndroid Build Coastguard Worker ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
3560*6a54128fSAndroid Build Coastguard Worker /* too big for an extent-based file - 32bit ee_block */
3561*6a54128fSAndroid Build Coastguard Worker bad_size = 6;
3562*6a54128fSAndroid Build Coastguard Worker }
3563*6a54128fSAndroid Build Coastguard Worker /* i_size for symlinks is checked elsewhere */
3564*6a54128fSAndroid Build Coastguard Worker if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
3565*6a54128fSAndroid Build Coastguard Worker /* Did inline_data set pctx->num earlier? */
3566*6a54128fSAndroid Build Coastguard Worker if (bad_size != 7)
3567*6a54128fSAndroid Build Coastguard Worker pctx->num = (pb.last_block + 1) * fs->blocksize;
3568*6a54128fSAndroid Build Coastguard Worker pctx->group = bad_size;
3569*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
3570*6a54128fSAndroid Build Coastguard Worker ext2fs_inode_size_set(fs, inode, pctx->num);
3571*6a54128fSAndroid Build Coastguard Worker if (EXT2_I_SIZE(inode) == 0 &&
3572*6a54128fSAndroid Build Coastguard Worker (inode->i_flags & EXT4_INLINE_DATA_FL)) {
3573*6a54128fSAndroid Build Coastguard Worker memset(inode->i_block, 0,
3574*6a54128fSAndroid Build Coastguard Worker sizeof(inode->i_block));
3575*6a54128fSAndroid Build Coastguard Worker inode->i_flags &= ~EXT4_INLINE_DATA_FL;
3576*6a54128fSAndroid Build Coastguard Worker }
3577*6a54128fSAndroid Build Coastguard Worker dirty_inode++;
3578*6a54128fSAndroid Build Coastguard Worker }
3579*6a54128fSAndroid Build Coastguard Worker pctx->num = 0;
3580*6a54128fSAndroid Build Coastguard Worker }
3581*6a54128fSAndroid Build Coastguard Worker if (LINUX_S_ISREG(inode->i_mode) &&
3582*6a54128fSAndroid Build Coastguard Worker ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
3583*6a54128fSAndroid Build Coastguard Worker ctx->large_files++;
3584*6a54128fSAndroid Build Coastguard Worker if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
3585*6a54128fSAndroid Build Coastguard Worker ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
3586*6a54128fSAndroid Build Coastguard Worker (ext2fs_has_feature_huge_file(fs->super) &&
3587*6a54128fSAndroid Build Coastguard Worker (inode->i_flags & EXT4_HUGE_FILE_FL) &&
3588*6a54128fSAndroid Build Coastguard Worker (inode->osd2.linux2.l_i_blocks_hi != 0)))) {
3589*6a54128fSAndroid Build Coastguard Worker pctx->num = pb.num_blocks;
3590*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
3591*6a54128fSAndroid Build Coastguard Worker inode->i_blocks = pb.num_blocks;
3592*6a54128fSAndroid Build Coastguard Worker inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
3593*6a54128fSAndroid Build Coastguard Worker dirty_inode++;
3594*6a54128fSAndroid Build Coastguard Worker }
3595*6a54128fSAndroid Build Coastguard Worker pctx->num = 0;
3596*6a54128fSAndroid Build Coastguard Worker }
3597*6a54128fSAndroid Build Coastguard Worker
3598*6a54128fSAndroid Build Coastguard Worker /*
3599*6a54128fSAndroid Build Coastguard Worker * The kernel gets mad if we ask it to allocate bigalloc clusters to
3600*6a54128fSAndroid Build Coastguard Worker * a block mapped file, so rebuild it as an extent file. We can skip
3601*6a54128fSAndroid Build Coastguard Worker * symlinks because they're never rewritten.
3602*6a54128fSAndroid Build Coastguard Worker */
3603*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_feature_bigalloc(fs->super) &&
3604*6a54128fSAndroid Build Coastguard Worker (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode)) &&
3605*6a54128fSAndroid Build Coastguard Worker ext2fs_inode_data_blocks2(fs, inode) > 0 &&
3606*6a54128fSAndroid Build Coastguard Worker (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INO(fs->super)) &&
3607*6a54128fSAndroid Build Coastguard Worker !(inode->i_flags & (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)) &&
3608*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_NO_BIGALLOC_BLOCKMAP_FILES, pctx)) {
3609*6a54128fSAndroid Build Coastguard Worker pctx->errcode = e2fsck_rebuild_extents_later(ctx, ino);
3610*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode)
3611*6a54128fSAndroid Build Coastguard Worker goto out;
3612*6a54128fSAndroid Build Coastguard Worker }
3613*6a54128fSAndroid Build Coastguard Worker
3614*6a54128fSAndroid Build Coastguard Worker if (ctx->dirs_to_hash && pb.is_dir &&
3615*6a54128fSAndroid Build Coastguard Worker !(ctx->lost_and_found && ctx->lost_and_found == ino) &&
3616*6a54128fSAndroid Build Coastguard Worker !(inode->i_flags & EXT2_INDEX_FL) &&
3617*6a54128fSAndroid Build Coastguard Worker ((inode->i_size / fs->blocksize) >= 3))
3618*6a54128fSAndroid Build Coastguard Worker e2fsck_rehash_dir_later(ctx, ino);
3619*6a54128fSAndroid Build Coastguard Worker
3620*6a54128fSAndroid Build Coastguard Worker out:
3621*6a54128fSAndroid Build Coastguard Worker if (dirty_inode)
3622*6a54128fSAndroid Build Coastguard Worker e2fsck_write_inode(ctx, ino, inode, "check_blocks");
3623*6a54128fSAndroid Build Coastguard Worker }
3624*6a54128fSAndroid Build Coastguard Worker
3625*6a54128fSAndroid Build Coastguard Worker #if 0
3626*6a54128fSAndroid Build Coastguard Worker /*
3627*6a54128fSAndroid Build Coastguard Worker * Helper function called by process block when an illegal block is
3628*6a54128fSAndroid Build Coastguard Worker * found. It returns a description about why the block is illegal
3629*6a54128fSAndroid Build Coastguard Worker */
3630*6a54128fSAndroid Build Coastguard Worker static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
3631*6a54128fSAndroid Build Coastguard Worker {
3632*6a54128fSAndroid Build Coastguard Worker blk64_t super;
3633*6a54128fSAndroid Build Coastguard Worker int i;
3634*6a54128fSAndroid Build Coastguard Worker static char problem[80];
3635*6a54128fSAndroid Build Coastguard Worker
3636*6a54128fSAndroid Build Coastguard Worker super = fs->super->s_first_data_block;
3637*6a54128fSAndroid Build Coastguard Worker strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
3638*6a54128fSAndroid Build Coastguard Worker if (block < super) {
3639*6a54128fSAndroid Build Coastguard Worker sprintf(problem, "< FIRSTBLOCK (%u)", super);
3640*6a54128fSAndroid Build Coastguard Worker return(problem);
3641*6a54128fSAndroid Build Coastguard Worker } else if (block >= ext2fs_blocks_count(fs->super)) {
3642*6a54128fSAndroid Build Coastguard Worker sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
3643*6a54128fSAndroid Build Coastguard Worker return(problem);
3644*6a54128fSAndroid Build Coastguard Worker }
3645*6a54128fSAndroid Build Coastguard Worker for (i = 0; i < fs->group_desc_count; i++) {
3646*6a54128fSAndroid Build Coastguard Worker if (block == super) {
3647*6a54128fSAndroid Build Coastguard Worker sprintf(problem, "is the superblock in group %d", i);
3648*6a54128fSAndroid Build Coastguard Worker break;
3649*6a54128fSAndroid Build Coastguard Worker }
3650*6a54128fSAndroid Build Coastguard Worker if (block > super &&
3651*6a54128fSAndroid Build Coastguard Worker block <= (super + fs->desc_blocks)) {
3652*6a54128fSAndroid Build Coastguard Worker sprintf(problem, "is in the group descriptors "
3653*6a54128fSAndroid Build Coastguard Worker "of group %d", i);
3654*6a54128fSAndroid Build Coastguard Worker break;
3655*6a54128fSAndroid Build Coastguard Worker }
3656*6a54128fSAndroid Build Coastguard Worker if (block == ext2fs_block_bitmap_loc(fs, i)) {
3657*6a54128fSAndroid Build Coastguard Worker sprintf(problem, "is the block bitmap of group %d", i);
3658*6a54128fSAndroid Build Coastguard Worker break;
3659*6a54128fSAndroid Build Coastguard Worker }
3660*6a54128fSAndroid Build Coastguard Worker if (block == ext2fs_inode_bitmap_loc(fs, i)) {
3661*6a54128fSAndroid Build Coastguard Worker sprintf(problem, "is the inode bitmap of group %d", i);
3662*6a54128fSAndroid Build Coastguard Worker break;
3663*6a54128fSAndroid Build Coastguard Worker }
3664*6a54128fSAndroid Build Coastguard Worker if (block >= ext2fs_inode_table_loc(fs, i) &&
3665*6a54128fSAndroid Build Coastguard Worker (block < ext2fs_inode_table_loc(fs, i)
3666*6a54128fSAndroid Build Coastguard Worker + fs->inode_blocks_per_group)) {
3667*6a54128fSAndroid Build Coastguard Worker sprintf(problem, "is in the inode table of group %d",
3668*6a54128fSAndroid Build Coastguard Worker i);
3669*6a54128fSAndroid Build Coastguard Worker break;
3670*6a54128fSAndroid Build Coastguard Worker }
3671*6a54128fSAndroid Build Coastguard Worker super += fs->super->s_blocks_per_group;
3672*6a54128fSAndroid Build Coastguard Worker }
3673*6a54128fSAndroid Build Coastguard Worker return(problem);
3674*6a54128fSAndroid Build Coastguard Worker }
3675*6a54128fSAndroid Build Coastguard Worker #endif
3676*6a54128fSAndroid Build Coastguard Worker
3677*6a54128fSAndroid Build Coastguard Worker /*
3678*6a54128fSAndroid Build Coastguard Worker * This is a helper function for check_blocks().
3679*6a54128fSAndroid Build Coastguard Worker */
process_block(ext2_filsys fs,blk64_t * block_nr,e2_blkcnt_t blockcnt,blk64_t ref_block EXT2FS_ATTR ((unused)),int ref_offset EXT2FS_ATTR ((unused)),void * priv_data)3680*6a54128fSAndroid Build Coastguard Worker static int process_block(ext2_filsys fs,
3681*6a54128fSAndroid Build Coastguard Worker blk64_t *block_nr,
3682*6a54128fSAndroid Build Coastguard Worker e2_blkcnt_t blockcnt,
3683*6a54128fSAndroid Build Coastguard Worker blk64_t ref_block EXT2FS_ATTR((unused)),
3684*6a54128fSAndroid Build Coastguard Worker int ref_offset EXT2FS_ATTR((unused)),
3685*6a54128fSAndroid Build Coastguard Worker void *priv_data)
3686*6a54128fSAndroid Build Coastguard Worker {
3687*6a54128fSAndroid Build Coastguard Worker struct process_block_struct *p;
3688*6a54128fSAndroid Build Coastguard Worker struct problem_context *pctx;
3689*6a54128fSAndroid Build Coastguard Worker blk64_t blk = *block_nr;
3690*6a54128fSAndroid Build Coastguard Worker int ret_code = 0;
3691*6a54128fSAndroid Build Coastguard Worker problem_t problem = 0;
3692*6a54128fSAndroid Build Coastguard Worker e2fsck_t ctx;
3693*6a54128fSAndroid Build Coastguard Worker
3694*6a54128fSAndroid Build Coastguard Worker p = (struct process_block_struct *) priv_data;
3695*6a54128fSAndroid Build Coastguard Worker pctx = p->pctx;
3696*6a54128fSAndroid Build Coastguard Worker ctx = p->ctx;
3697*6a54128fSAndroid Build Coastguard Worker
3698*6a54128fSAndroid Build Coastguard Worker /*
3699*6a54128fSAndroid Build Coastguard Worker * For a directory, add logical block zero for processing even if it's
3700*6a54128fSAndroid Build Coastguard Worker * not mapped or we'll be perennially stuck with broken "." and ".."
3701*6a54128fSAndroid Build Coastguard Worker * entries.
3702*6a54128fSAndroid Build Coastguard Worker */
3703*6a54128fSAndroid Build Coastguard Worker if (p->is_dir && blockcnt == 0 && blk == 0) {
3704*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino, 0, 0);
3705*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode) {
3706*6a54128fSAndroid Build Coastguard Worker pctx->blk = blk;
3707*6a54128fSAndroid Build Coastguard Worker pctx->num = blockcnt;
3708*6a54128fSAndroid Build Coastguard Worker goto failed_add_dir_block;
3709*6a54128fSAndroid Build Coastguard Worker }
3710*6a54128fSAndroid Build Coastguard Worker p->last_db_block++;
3711*6a54128fSAndroid Build Coastguard Worker }
3712*6a54128fSAndroid Build Coastguard Worker
3713*6a54128fSAndroid Build Coastguard Worker if (blk == 0)
3714*6a54128fSAndroid Build Coastguard Worker return 0;
3715*6a54128fSAndroid Build Coastguard Worker
3716*6a54128fSAndroid Build Coastguard Worker #if 0
3717*6a54128fSAndroid Build Coastguard Worker printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
3718*6a54128fSAndroid Build Coastguard Worker blockcnt);
3719*6a54128fSAndroid Build Coastguard Worker #endif
3720*6a54128fSAndroid Build Coastguard Worker
3721*6a54128fSAndroid Build Coastguard Worker /*
3722*6a54128fSAndroid Build Coastguard Worker * Simplistic fragmentation check. We merely require that the
3723*6a54128fSAndroid Build Coastguard Worker * file be contiguous. (Which can never be true for really
3724*6a54128fSAndroid Build Coastguard Worker * big files that are greater than a block group.)
3725*6a54128fSAndroid Build Coastguard Worker */
3726*6a54128fSAndroid Build Coastguard Worker if (p->previous_block && p->ino != EXT2_RESIZE_INO) {
3727*6a54128fSAndroid Build Coastguard Worker if (p->previous_block+1 != blk) {
3728*6a54128fSAndroid Build Coastguard Worker if (ctx->options & E2F_OPT_FRAGCHECK) {
3729*6a54128fSAndroid Build Coastguard Worker char type = '?';
3730*6a54128fSAndroid Build Coastguard Worker
3731*6a54128fSAndroid Build Coastguard Worker if (p->is_dir)
3732*6a54128fSAndroid Build Coastguard Worker type = 'd';
3733*6a54128fSAndroid Build Coastguard Worker else if (p->is_reg)
3734*6a54128fSAndroid Build Coastguard Worker type = 'f';
3735*6a54128fSAndroid Build Coastguard Worker
3736*6a54128fSAndroid Build Coastguard Worker printf(_("%6lu(%c): expecting %6lu "
3737*6a54128fSAndroid Build Coastguard Worker "got phys %6lu (blkcnt %lld)\n"),
3738*6a54128fSAndroid Build Coastguard Worker (unsigned long) pctx->ino, type,
3739*6a54128fSAndroid Build Coastguard Worker (unsigned long) p->previous_block+1,
3740*6a54128fSAndroid Build Coastguard Worker (unsigned long) blk,
3741*6a54128fSAndroid Build Coastguard Worker (long long) blockcnt);
3742*6a54128fSAndroid Build Coastguard Worker }
3743*6a54128fSAndroid Build Coastguard Worker p->fragmented = 1;
3744*6a54128fSAndroid Build Coastguard Worker }
3745*6a54128fSAndroid Build Coastguard Worker }
3746*6a54128fSAndroid Build Coastguard Worker
3747*6a54128fSAndroid Build Coastguard Worker if (p->is_dir && !ext2fs_has_feature_largedir(fs->super) &&
3748*6a54128fSAndroid Build Coastguard Worker !pctx->inode->i_size_high &&
3749*6a54128fSAndroid Build Coastguard Worker blockcnt > (1 << (21 - fs->super->s_log_block_size)))
3750*6a54128fSAndroid Build Coastguard Worker problem = PR_1_TOOBIG_DIR;
3751*6a54128fSAndroid Build Coastguard Worker if (p->is_dir && p->num_blocks + 1 >= p->max_blocks)
3752*6a54128fSAndroid Build Coastguard Worker problem = PR_1_TOOBIG_DIR;
3753*6a54128fSAndroid Build Coastguard Worker if (p->is_reg && p->num_blocks + 1 >= p->max_blocks)
3754*6a54128fSAndroid Build Coastguard Worker problem = PR_1_TOOBIG_REG;
3755*6a54128fSAndroid Build Coastguard Worker if (!p->is_dir && !p->is_reg && blockcnt > 0)
3756*6a54128fSAndroid Build Coastguard Worker problem = PR_1_TOOBIG_SYMLINK;
3757*6a54128fSAndroid Build Coastguard Worker
3758*6a54128fSAndroid Build Coastguard Worker if (blk < fs->super->s_first_data_block ||
3759*6a54128fSAndroid Build Coastguard Worker blk >= ext2fs_blocks_count(fs->super))
3760*6a54128fSAndroid Build Coastguard Worker problem = PR_1_ILLEGAL_BLOCK_NUM;
3761*6a54128fSAndroid Build Coastguard Worker
3762*6a54128fSAndroid Build Coastguard Worker /*
3763*6a54128fSAndroid Build Coastguard Worker * If this IND/DIND/TIND block is squatting atop some critical metadata
3764*6a54128fSAndroid Build Coastguard Worker * (group descriptors, superblock, bitmap, inode table), any write to
3765*6a54128fSAndroid Build Coastguard Worker * "fix" mapping problems will destroy the metadata. We'll let pass 1b
3766*6a54128fSAndroid Build Coastguard Worker * fix that and restart fsck.
3767*6a54128fSAndroid Build Coastguard Worker */
3768*6a54128fSAndroid Build Coastguard Worker if (blockcnt < 0 &&
3769*6a54128fSAndroid Build Coastguard Worker p->ino != EXT2_RESIZE_INO &&
3770*6a54128fSAndroid Build Coastguard Worker blk < ctx->fs->super->s_blocks_count &&
3771*6a54128fSAndroid Build Coastguard Worker ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk)) {
3772*6a54128fSAndroid Build Coastguard Worker pctx->blk = blk;
3773*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_CRITICAL_METADATA_COLLISION, pctx);
3774*6a54128fSAndroid Build Coastguard Worker if ((ctx->options & E2F_OPT_NO) == 0)
3775*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_RESTART_LATER;
3776*6a54128fSAndroid Build Coastguard Worker }
3777*6a54128fSAndroid Build Coastguard Worker
3778*6a54128fSAndroid Build Coastguard Worker if (problem) {
3779*6a54128fSAndroid Build Coastguard Worker p->num_illegal_blocks++;
3780*6a54128fSAndroid Build Coastguard Worker /*
3781*6a54128fSAndroid Build Coastguard Worker * A bit of subterfuge here -- we're trying to fix a block
3782*6a54128fSAndroid Build Coastguard Worker * mapping, but the IND/DIND/TIND block could have collided
3783*6a54128fSAndroid Build Coastguard Worker * with some critical metadata. So, fix the in-core mapping so
3784*6a54128fSAndroid Build Coastguard Worker * iterate won't go insane, but return 0 instead of
3785*6a54128fSAndroid Build Coastguard Worker * BLOCK_CHANGED so that it won't write the remapping out to
3786*6a54128fSAndroid Build Coastguard Worker * our multiply linked block.
3787*6a54128fSAndroid Build Coastguard Worker *
3788*6a54128fSAndroid Build Coastguard Worker * Even if we previously determined that an *IND block
3789*6a54128fSAndroid Build Coastguard Worker * conflicts with critical metadata, we must still try to
3790*6a54128fSAndroid Build Coastguard Worker * iterate the *IND block as if it is an *IND block to find and
3791*6a54128fSAndroid Build Coastguard Worker * mark the blocks it points to. Better to be overly cautious
3792*6a54128fSAndroid Build Coastguard Worker * with the used_blocks map so that we don't move the *IND
3793*6a54128fSAndroid Build Coastguard Worker * block to a block that's really in use!
3794*6a54128fSAndroid Build Coastguard Worker */
3795*6a54128fSAndroid Build Coastguard Worker if (p->ino != EXT2_RESIZE_INO &&
3796*6a54128fSAndroid Build Coastguard Worker ref_block != 0 &&
3797*6a54128fSAndroid Build Coastguard Worker ext2fs_test_block_bitmap2(ctx->block_metadata_map,
3798*6a54128fSAndroid Build Coastguard Worker ref_block)) {
3799*6a54128fSAndroid Build Coastguard Worker *block_nr = 0;
3800*6a54128fSAndroid Build Coastguard Worker return 0;
3801*6a54128fSAndroid Build Coastguard Worker }
3802*6a54128fSAndroid Build Coastguard Worker if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
3803*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
3804*6a54128fSAndroid Build Coastguard Worker p->clear = 1;
3805*6a54128fSAndroid Build Coastguard Worker return BLOCK_ABORT;
3806*6a54128fSAndroid Build Coastguard Worker }
3807*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
3808*6a54128fSAndroid Build Coastguard Worker p->suppress = 1;
3809*6a54128fSAndroid Build Coastguard Worker set_latch_flags(PR_LATCH_BLOCK,
3810*6a54128fSAndroid Build Coastguard Worker PRL_SUPPRESS, 0);
3811*6a54128fSAndroid Build Coastguard Worker }
3812*6a54128fSAndroid Build Coastguard Worker }
3813*6a54128fSAndroid Build Coastguard Worker pctx->blk = blk;
3814*6a54128fSAndroid Build Coastguard Worker pctx->blkcount = blockcnt;
3815*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, problem, pctx)) {
3816*6a54128fSAndroid Build Coastguard Worker blk = *block_nr = 0;
3817*6a54128fSAndroid Build Coastguard Worker ret_code = BLOCK_CHANGED;
3818*6a54128fSAndroid Build Coastguard Worker p->inode_modified = 1;
3819*6a54128fSAndroid Build Coastguard Worker /*
3820*6a54128fSAndroid Build Coastguard Worker * If the directory block is too big and is beyond the
3821*6a54128fSAndroid Build Coastguard Worker * end of the FS, don't bother trying to add it for
3822*6a54128fSAndroid Build Coastguard Worker * processing -- the kernel would never have created a
3823*6a54128fSAndroid Build Coastguard Worker * directory this large, and we risk an ENOMEM abort.
3824*6a54128fSAndroid Build Coastguard Worker * In any case, the toobig handler for extent-based
3825*6a54128fSAndroid Build Coastguard Worker * directories also doesn't feed toobig blocks to
3826*6a54128fSAndroid Build Coastguard Worker * pass 2.
3827*6a54128fSAndroid Build Coastguard Worker */
3828*6a54128fSAndroid Build Coastguard Worker if (problem == PR_1_TOOBIG_DIR)
3829*6a54128fSAndroid Build Coastguard Worker return ret_code;
3830*6a54128fSAndroid Build Coastguard Worker goto mark_dir;
3831*6a54128fSAndroid Build Coastguard Worker } else
3832*6a54128fSAndroid Build Coastguard Worker return 0;
3833*6a54128fSAndroid Build Coastguard Worker }
3834*6a54128fSAndroid Build Coastguard Worker
3835*6a54128fSAndroid Build Coastguard Worker if (p->ino == EXT2_RESIZE_INO) {
3836*6a54128fSAndroid Build Coastguard Worker /*
3837*6a54128fSAndroid Build Coastguard Worker * The resize inode has already be sanity checked
3838*6a54128fSAndroid Build Coastguard Worker * during pass #0 (the superblock checks). All we
3839*6a54128fSAndroid Build Coastguard Worker * have to do is mark the double indirect block as
3840*6a54128fSAndroid Build Coastguard Worker * being in use; all of the other blocks are handled
3841*6a54128fSAndroid Build Coastguard Worker * by mark_table_blocks()).
3842*6a54128fSAndroid Build Coastguard Worker */
3843*6a54128fSAndroid Build Coastguard Worker if (blockcnt == BLOCK_COUNT_DIND)
3844*6a54128fSAndroid Build Coastguard Worker mark_block_used(ctx, blk);
3845*6a54128fSAndroid Build Coastguard Worker p->num_blocks++;
3846*6a54128fSAndroid Build Coastguard Worker } else if (!(ctx->fs->cluster_ratio_bits &&
3847*6a54128fSAndroid Build Coastguard Worker p->previous_block &&
3848*6a54128fSAndroid Build Coastguard Worker (EXT2FS_B2C(ctx->fs, blk) ==
3849*6a54128fSAndroid Build Coastguard Worker EXT2FS_B2C(ctx->fs, p->previous_block)) &&
3850*6a54128fSAndroid Build Coastguard Worker (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
3851*6a54128fSAndroid Build Coastguard Worker ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
3852*6a54128fSAndroid Build Coastguard Worker mark_block_used(ctx, blk);
3853*6a54128fSAndroid Build Coastguard Worker p->num_blocks++;
3854*6a54128fSAndroid Build Coastguard Worker } else if (has_unaligned_cluster_map(ctx, p->previous_block,
3855*6a54128fSAndroid Build Coastguard Worker p->last_block, blk, blockcnt)) {
3856*6a54128fSAndroid Build Coastguard Worker pctx->blk = blockcnt;
3857*6a54128fSAndroid Build Coastguard Worker pctx->blk2 = blk;
3858*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
3859*6a54128fSAndroid Build Coastguard Worker mark_block_used(ctx, blk);
3860*6a54128fSAndroid Build Coastguard Worker mark_block_used(ctx, blk);
3861*6a54128fSAndroid Build Coastguard Worker }
3862*6a54128fSAndroid Build Coastguard Worker if (blockcnt >= 0)
3863*6a54128fSAndroid Build Coastguard Worker p->last_block = blockcnt;
3864*6a54128fSAndroid Build Coastguard Worker p->previous_block = blk;
3865*6a54128fSAndroid Build Coastguard Worker mark_dir:
3866*6a54128fSAndroid Build Coastguard Worker if (p->is_dir && (blockcnt >= 0)) {
3867*6a54128fSAndroid Build Coastguard Worker while (++p->last_db_block < blockcnt) {
3868*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
3869*6a54128fSAndroid Build Coastguard Worker p->ino, 0,
3870*6a54128fSAndroid Build Coastguard Worker p->last_db_block);
3871*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode) {
3872*6a54128fSAndroid Build Coastguard Worker pctx->blk = 0;
3873*6a54128fSAndroid Build Coastguard Worker pctx->num = p->last_db_block;
3874*6a54128fSAndroid Build Coastguard Worker goto failed_add_dir_block;
3875*6a54128fSAndroid Build Coastguard Worker }
3876*6a54128fSAndroid Build Coastguard Worker }
3877*6a54128fSAndroid Build Coastguard Worker pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
3878*6a54128fSAndroid Build Coastguard Worker blk, blockcnt);
3879*6a54128fSAndroid Build Coastguard Worker if (pctx->errcode) {
3880*6a54128fSAndroid Build Coastguard Worker pctx->blk = blk;
3881*6a54128fSAndroid Build Coastguard Worker pctx->num = blockcnt;
3882*6a54128fSAndroid Build Coastguard Worker failed_add_dir_block:
3883*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
3884*6a54128fSAndroid Build Coastguard Worker /* Should never get here */
3885*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
3886*6a54128fSAndroid Build Coastguard Worker return BLOCK_ABORT;
3887*6a54128fSAndroid Build Coastguard Worker }
3888*6a54128fSAndroid Build Coastguard Worker }
3889*6a54128fSAndroid Build Coastguard Worker return ret_code;
3890*6a54128fSAndroid Build Coastguard Worker }
3891*6a54128fSAndroid Build Coastguard Worker
process_bad_block(ext2_filsys fs,blk64_t * block_nr,e2_blkcnt_t blockcnt,blk64_t ref_block EXT2FS_ATTR ((unused)),int ref_offset EXT2FS_ATTR ((unused)),void * priv_data)3892*6a54128fSAndroid Build Coastguard Worker static int process_bad_block(ext2_filsys fs,
3893*6a54128fSAndroid Build Coastguard Worker blk64_t *block_nr,
3894*6a54128fSAndroid Build Coastguard Worker e2_blkcnt_t blockcnt,
3895*6a54128fSAndroid Build Coastguard Worker blk64_t ref_block EXT2FS_ATTR((unused)),
3896*6a54128fSAndroid Build Coastguard Worker int ref_offset EXT2FS_ATTR((unused)),
3897*6a54128fSAndroid Build Coastguard Worker void *priv_data)
3898*6a54128fSAndroid Build Coastguard Worker {
3899*6a54128fSAndroid Build Coastguard Worker struct process_block_struct *p;
3900*6a54128fSAndroid Build Coastguard Worker blk64_t blk = *block_nr;
3901*6a54128fSAndroid Build Coastguard Worker blk64_t first_block;
3902*6a54128fSAndroid Build Coastguard Worker dgrp_t i;
3903*6a54128fSAndroid Build Coastguard Worker struct problem_context *pctx;
3904*6a54128fSAndroid Build Coastguard Worker e2fsck_t ctx;
3905*6a54128fSAndroid Build Coastguard Worker
3906*6a54128fSAndroid Build Coastguard Worker if (!blk)
3907*6a54128fSAndroid Build Coastguard Worker return 0;
3908*6a54128fSAndroid Build Coastguard Worker
3909*6a54128fSAndroid Build Coastguard Worker p = (struct process_block_struct *) priv_data;
3910*6a54128fSAndroid Build Coastguard Worker ctx = p->ctx;
3911*6a54128fSAndroid Build Coastguard Worker pctx = p->pctx;
3912*6a54128fSAndroid Build Coastguard Worker
3913*6a54128fSAndroid Build Coastguard Worker pctx->ino = EXT2_BAD_INO;
3914*6a54128fSAndroid Build Coastguard Worker pctx->blk = blk;
3915*6a54128fSAndroid Build Coastguard Worker pctx->blkcount = blockcnt;
3916*6a54128fSAndroid Build Coastguard Worker
3917*6a54128fSAndroid Build Coastguard Worker if ((blk < fs->super->s_first_data_block) ||
3918*6a54128fSAndroid Build Coastguard Worker (blk >= ext2fs_blocks_count(fs->super))) {
3919*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
3920*6a54128fSAndroid Build Coastguard Worker *block_nr = 0;
3921*6a54128fSAndroid Build Coastguard Worker return BLOCK_CHANGED;
3922*6a54128fSAndroid Build Coastguard Worker } else
3923*6a54128fSAndroid Build Coastguard Worker return 0;
3924*6a54128fSAndroid Build Coastguard Worker }
3925*6a54128fSAndroid Build Coastguard Worker
3926*6a54128fSAndroid Build Coastguard Worker if (blockcnt < 0) {
3927*6a54128fSAndroid Build Coastguard Worker if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
3928*6a54128fSAndroid Build Coastguard Worker p->bbcheck = 1;
3929*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
3930*6a54128fSAndroid Build Coastguard Worker *block_nr = 0;
3931*6a54128fSAndroid Build Coastguard Worker return BLOCK_CHANGED;
3932*6a54128fSAndroid Build Coastguard Worker }
3933*6a54128fSAndroid Build Coastguard Worker } else if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3934*6a54128fSAndroid Build Coastguard Worker blk)) {
3935*6a54128fSAndroid Build Coastguard Worker p->bbcheck = 1;
3936*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
3937*6a54128fSAndroid Build Coastguard Worker pctx)) {
3938*6a54128fSAndroid Build Coastguard Worker *block_nr = 0;
3939*6a54128fSAndroid Build Coastguard Worker return BLOCK_CHANGED;
3940*6a54128fSAndroid Build Coastguard Worker }
3941*6a54128fSAndroid Build Coastguard Worker if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3942*6a54128fSAndroid Build Coastguard Worker return BLOCK_ABORT;
3943*6a54128fSAndroid Build Coastguard Worker } else
3944*6a54128fSAndroid Build Coastguard Worker mark_block_used(ctx, blk);
3945*6a54128fSAndroid Build Coastguard Worker return 0;
3946*6a54128fSAndroid Build Coastguard Worker }
3947*6a54128fSAndroid Build Coastguard Worker #if 0
3948*6a54128fSAndroid Build Coastguard Worker printf ("DEBUG: Marking %u as bad.\n", blk);
3949*6a54128fSAndroid Build Coastguard Worker #endif
3950*6a54128fSAndroid Build Coastguard Worker ctx->fs_badblocks_count++;
3951*6a54128fSAndroid Build Coastguard Worker /*
3952*6a54128fSAndroid Build Coastguard Worker * If the block is not used, then mark it as used and return.
3953*6a54128fSAndroid Build Coastguard Worker * If it is already marked as found, this must mean that
3954*6a54128fSAndroid Build Coastguard Worker * there's an overlap between the filesystem table blocks
3955*6a54128fSAndroid Build Coastguard Worker * (bitmaps and inode table) and the bad block list.
3956*6a54128fSAndroid Build Coastguard Worker */
3957*6a54128fSAndroid Build Coastguard Worker if (!ext2fs_test_block_bitmap2(ctx->block_found_map, blk)) {
3958*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
3959*6a54128fSAndroid Build Coastguard Worker return 0;
3960*6a54128fSAndroid Build Coastguard Worker }
3961*6a54128fSAndroid Build Coastguard Worker /*
3962*6a54128fSAndroid Build Coastguard Worker * Try to find the where the filesystem block was used...
3963*6a54128fSAndroid Build Coastguard Worker */
3964*6a54128fSAndroid Build Coastguard Worker first_block = fs->super->s_first_data_block;
3965*6a54128fSAndroid Build Coastguard Worker
3966*6a54128fSAndroid Build Coastguard Worker for (i = 0; i < fs->group_desc_count; i++ ) {
3967*6a54128fSAndroid Build Coastguard Worker pctx->group = i;
3968*6a54128fSAndroid Build Coastguard Worker pctx->blk = blk;
3969*6a54128fSAndroid Build Coastguard Worker if (!ext2fs_bg_has_super(fs, i))
3970*6a54128fSAndroid Build Coastguard Worker goto skip_super;
3971*6a54128fSAndroid Build Coastguard Worker if (blk == first_block) {
3972*6a54128fSAndroid Build Coastguard Worker if (i == 0) {
3973*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx,
3974*6a54128fSAndroid Build Coastguard Worker PR_1_BAD_PRIMARY_SUPERBLOCK,
3975*6a54128fSAndroid Build Coastguard Worker pctx)) {
3976*6a54128fSAndroid Build Coastguard Worker *block_nr = 0;
3977*6a54128fSAndroid Build Coastguard Worker return BLOCK_CHANGED;
3978*6a54128fSAndroid Build Coastguard Worker }
3979*6a54128fSAndroid Build Coastguard Worker return 0;
3980*6a54128fSAndroid Build Coastguard Worker }
3981*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
3982*6a54128fSAndroid Build Coastguard Worker return 0;
3983*6a54128fSAndroid Build Coastguard Worker }
3984*6a54128fSAndroid Build Coastguard Worker if ((blk > first_block) &&
3985*6a54128fSAndroid Build Coastguard Worker (blk <= first_block + fs->desc_blocks)) {
3986*6a54128fSAndroid Build Coastguard Worker if (i == 0) {
3987*6a54128fSAndroid Build Coastguard Worker pctx->blk = *block_nr;
3988*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx,
3989*6a54128fSAndroid Build Coastguard Worker PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
3990*6a54128fSAndroid Build Coastguard Worker *block_nr = 0;
3991*6a54128fSAndroid Build Coastguard Worker return BLOCK_CHANGED;
3992*6a54128fSAndroid Build Coastguard Worker }
3993*6a54128fSAndroid Build Coastguard Worker return 0;
3994*6a54128fSAndroid Build Coastguard Worker }
3995*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
3996*6a54128fSAndroid Build Coastguard Worker return 0;
3997*6a54128fSAndroid Build Coastguard Worker }
3998*6a54128fSAndroid Build Coastguard Worker skip_super:
3999*6a54128fSAndroid Build Coastguard Worker if (blk == ext2fs_block_bitmap_loc(fs, i)) {
4000*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
4001*6a54128fSAndroid Build Coastguard Worker ctx->invalid_block_bitmap_flag[i]++;
4002*6a54128fSAndroid Build Coastguard Worker ctx->invalid_bitmaps++;
4003*6a54128fSAndroid Build Coastguard Worker }
4004*6a54128fSAndroid Build Coastguard Worker return 0;
4005*6a54128fSAndroid Build Coastguard Worker }
4006*6a54128fSAndroid Build Coastguard Worker if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
4007*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
4008*6a54128fSAndroid Build Coastguard Worker ctx->invalid_inode_bitmap_flag[i]++;
4009*6a54128fSAndroid Build Coastguard Worker ctx->invalid_bitmaps++;
4010*6a54128fSAndroid Build Coastguard Worker }
4011*6a54128fSAndroid Build Coastguard Worker return 0;
4012*6a54128fSAndroid Build Coastguard Worker }
4013*6a54128fSAndroid Build Coastguard Worker if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
4014*6a54128fSAndroid Build Coastguard Worker (blk < (ext2fs_inode_table_loc(fs, i) +
4015*6a54128fSAndroid Build Coastguard Worker fs->inode_blocks_per_group))) {
4016*6a54128fSAndroid Build Coastguard Worker /*
4017*6a54128fSAndroid Build Coastguard Worker * If there are bad blocks in the inode table,
4018*6a54128fSAndroid Build Coastguard Worker * the inode scan code will try to do
4019*6a54128fSAndroid Build Coastguard Worker * something reasonable automatically.
4020*6a54128fSAndroid Build Coastguard Worker */
4021*6a54128fSAndroid Build Coastguard Worker return 0;
4022*6a54128fSAndroid Build Coastguard Worker }
4023*6a54128fSAndroid Build Coastguard Worker first_block += fs->super->s_blocks_per_group;
4024*6a54128fSAndroid Build Coastguard Worker }
4025*6a54128fSAndroid Build Coastguard Worker /*
4026*6a54128fSAndroid Build Coastguard Worker * If we've gotten to this point, then the only
4027*6a54128fSAndroid Build Coastguard Worker * possibility is that the bad block inode meta data
4028*6a54128fSAndroid Build Coastguard Worker * is using a bad block.
4029*6a54128fSAndroid Build Coastguard Worker */
4030*6a54128fSAndroid Build Coastguard Worker if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
4031*6a54128fSAndroid Build Coastguard Worker (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
4032*6a54128fSAndroid Build Coastguard Worker (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
4033*6a54128fSAndroid Build Coastguard Worker p->bbcheck = 1;
4034*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
4035*6a54128fSAndroid Build Coastguard Worker *block_nr = 0;
4036*6a54128fSAndroid Build Coastguard Worker return BLOCK_CHANGED;
4037*6a54128fSAndroid Build Coastguard Worker }
4038*6a54128fSAndroid Build Coastguard Worker if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
4039*6a54128fSAndroid Build Coastguard Worker return BLOCK_ABORT;
4040*6a54128fSAndroid Build Coastguard Worker return 0;
4041*6a54128fSAndroid Build Coastguard Worker }
4042*6a54128fSAndroid Build Coastguard Worker
4043*6a54128fSAndroid Build Coastguard Worker pctx->group = -1;
4044*6a54128fSAndroid Build Coastguard Worker
4045*6a54128fSAndroid Build Coastguard Worker /* Warn user that the block wasn't claimed */
4046*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
4047*6a54128fSAndroid Build Coastguard Worker
4048*6a54128fSAndroid Build Coastguard Worker return 0;
4049*6a54128fSAndroid Build Coastguard Worker }
4050*6a54128fSAndroid Build Coastguard Worker
new_table_block(e2fsck_t ctx,blk64_t first_block,dgrp_t group,const char * name,int num,blk64_t * new_block)4051*6a54128fSAndroid Build Coastguard Worker static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
4052*6a54128fSAndroid Build Coastguard Worker const char *name, int num, blk64_t *new_block)
4053*6a54128fSAndroid Build Coastguard Worker {
4054*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs = ctx->fs;
4055*6a54128fSAndroid Build Coastguard Worker dgrp_t last_grp;
4056*6a54128fSAndroid Build Coastguard Worker blk64_t old_block = *new_block;
4057*6a54128fSAndroid Build Coastguard Worker blk64_t last_block;
4058*6a54128fSAndroid Build Coastguard Worker dgrp_t flexbg;
4059*6a54128fSAndroid Build Coastguard Worker unsigned flexbg_size;
4060*6a54128fSAndroid Build Coastguard Worker int i, is_flexbg;
4061*6a54128fSAndroid Build Coastguard Worker char *buf;
4062*6a54128fSAndroid Build Coastguard Worker struct problem_context pctx;
4063*6a54128fSAndroid Build Coastguard Worker
4064*6a54128fSAndroid Build Coastguard Worker clear_problem_context(&pctx);
4065*6a54128fSAndroid Build Coastguard Worker
4066*6a54128fSAndroid Build Coastguard Worker pctx.group = group;
4067*6a54128fSAndroid Build Coastguard Worker pctx.blk = old_block;
4068*6a54128fSAndroid Build Coastguard Worker pctx.str = name;
4069*6a54128fSAndroid Build Coastguard Worker
4070*6a54128fSAndroid Build Coastguard Worker /*
4071*6a54128fSAndroid Build Coastguard Worker * For flex_bg filesystems, first try to allocate the metadata
4072*6a54128fSAndroid Build Coastguard Worker * within the flex_bg, and if that fails then try finding the
4073*6a54128fSAndroid Build Coastguard Worker * space anywhere in the filesystem.
4074*6a54128fSAndroid Build Coastguard Worker */
4075*6a54128fSAndroid Build Coastguard Worker is_flexbg = ext2fs_has_feature_flex_bg(fs->super);
4076*6a54128fSAndroid Build Coastguard Worker if (is_flexbg) {
4077*6a54128fSAndroid Build Coastguard Worker flexbg_size = 1U << fs->super->s_log_groups_per_flex;
4078*6a54128fSAndroid Build Coastguard Worker flexbg = group / flexbg_size;
4079*6a54128fSAndroid Build Coastguard Worker first_block = ext2fs_group_first_block2(fs,
4080*6a54128fSAndroid Build Coastguard Worker flexbg_size * flexbg);
4081*6a54128fSAndroid Build Coastguard Worker last_grp = group | (flexbg_size - 1);
4082*6a54128fSAndroid Build Coastguard Worker if (last_grp >= fs->group_desc_count)
4083*6a54128fSAndroid Build Coastguard Worker last_grp = fs->group_desc_count - 1;
4084*6a54128fSAndroid Build Coastguard Worker last_block = ext2fs_group_last_block2(fs, last_grp);
4085*6a54128fSAndroid Build Coastguard Worker } else
4086*6a54128fSAndroid Build Coastguard Worker last_block = ext2fs_group_last_block2(fs, group);
4087*6a54128fSAndroid Build Coastguard Worker pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
4088*6a54128fSAndroid Build Coastguard Worker num, ctx->block_found_map,
4089*6a54128fSAndroid Build Coastguard Worker new_block);
4090*6a54128fSAndroid Build Coastguard Worker if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
4091*6a54128fSAndroid Build Coastguard Worker pctx.errcode = ext2fs_get_free_blocks2(fs,
4092*6a54128fSAndroid Build Coastguard Worker fs->super->s_first_data_block,
4093*6a54128fSAndroid Build Coastguard Worker ext2fs_blocks_count(fs->super),
4094*6a54128fSAndroid Build Coastguard Worker num, ctx->block_found_map, new_block);
4095*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
4096*6a54128fSAndroid Build Coastguard Worker pctx.num = num;
4097*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
4098*6a54128fSAndroid Build Coastguard Worker ext2fs_unmark_valid(fs);
4099*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
4100*6a54128fSAndroid Build Coastguard Worker return;
4101*6a54128fSAndroid Build Coastguard Worker }
4102*6a54128fSAndroid Build Coastguard Worker pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
4103*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
4104*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
4105*6a54128fSAndroid Build Coastguard Worker ext2fs_unmark_valid(fs);
4106*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_ABORT;
4107*6a54128fSAndroid Build Coastguard Worker return;
4108*6a54128fSAndroid Build Coastguard Worker }
4109*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_super_dirty(fs);
4110*6a54128fSAndroid Build Coastguard Worker fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
4111*6a54128fSAndroid Build Coastguard Worker pctx.blk2 = *new_block;
4112*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
4113*6a54128fSAndroid Build Coastguard Worker PR_1_RELOC_TO), &pctx);
4114*6a54128fSAndroid Build Coastguard Worker pctx.blk2 = 0;
4115*6a54128fSAndroid Build Coastguard Worker for (i = 0; i < num; i++) {
4116*6a54128fSAndroid Build Coastguard Worker pctx.blk = i;
4117*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
4118*6a54128fSAndroid Build Coastguard Worker if (old_block) {
4119*6a54128fSAndroid Build Coastguard Worker pctx.errcode = io_channel_read_blk64(fs->io,
4120*6a54128fSAndroid Build Coastguard Worker old_block + i, 1, buf);
4121*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode)
4122*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
4123*6a54128fSAndroid Build Coastguard Worker pctx.blk = (*new_block) + i;
4124*6a54128fSAndroid Build Coastguard Worker pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
4125*6a54128fSAndroid Build Coastguard Worker 1, buf);
4126*6a54128fSAndroid Build Coastguard Worker } else {
4127*6a54128fSAndroid Build Coastguard Worker pctx.blk = (*new_block) + i;
4128*6a54128fSAndroid Build Coastguard Worker pctx.errcode = ext2fs_zero_blocks2(fs, pctx.blk, 1,
4129*6a54128fSAndroid Build Coastguard Worker NULL, NULL);
4130*6a54128fSAndroid Build Coastguard Worker }
4131*6a54128fSAndroid Build Coastguard Worker
4132*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode)
4133*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
4134*6a54128fSAndroid Build Coastguard Worker }
4135*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&buf);
4136*6a54128fSAndroid Build Coastguard Worker }
4137*6a54128fSAndroid Build Coastguard Worker
4138*6a54128fSAndroid Build Coastguard Worker /*
4139*6a54128fSAndroid Build Coastguard Worker * This routine gets called at the end of pass 1 if bad blocks are
4140*6a54128fSAndroid Build Coastguard Worker * detected in the superblock, group descriptors, inode_bitmaps, or
4141*6a54128fSAndroid Build Coastguard Worker * block bitmaps. At this point, all of the blocks have been mapped
4142*6a54128fSAndroid Build Coastguard Worker * out, so we can try to allocate new block(s) to replace the bad
4143*6a54128fSAndroid Build Coastguard Worker * blocks.
4144*6a54128fSAndroid Build Coastguard Worker */
handle_fs_bad_blocks(e2fsck_t ctx)4145*6a54128fSAndroid Build Coastguard Worker static void handle_fs_bad_blocks(e2fsck_t ctx)
4146*6a54128fSAndroid Build Coastguard Worker {
4147*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs = ctx->fs;
4148*6a54128fSAndroid Build Coastguard Worker dgrp_t i;
4149*6a54128fSAndroid Build Coastguard Worker blk64_t first_block;
4150*6a54128fSAndroid Build Coastguard Worker blk64_t new_blk;
4151*6a54128fSAndroid Build Coastguard Worker
4152*6a54128fSAndroid Build Coastguard Worker for (i = 0; i < fs->group_desc_count; i++) {
4153*6a54128fSAndroid Build Coastguard Worker first_block = ext2fs_group_first_block2(fs, i);
4154*6a54128fSAndroid Build Coastguard Worker
4155*6a54128fSAndroid Build Coastguard Worker if (ctx->invalid_block_bitmap_flag[i]) {
4156*6a54128fSAndroid Build Coastguard Worker new_blk = ext2fs_block_bitmap_loc(fs, i);
4157*6a54128fSAndroid Build Coastguard Worker new_table_block(ctx, first_block, i, _("block bitmap"),
4158*6a54128fSAndroid Build Coastguard Worker 1, &new_blk);
4159*6a54128fSAndroid Build Coastguard Worker ext2fs_block_bitmap_loc_set(fs, i, new_blk);
4160*6a54128fSAndroid Build Coastguard Worker }
4161*6a54128fSAndroid Build Coastguard Worker if (ctx->invalid_inode_bitmap_flag[i]) {
4162*6a54128fSAndroid Build Coastguard Worker new_blk = ext2fs_inode_bitmap_loc(fs, i);
4163*6a54128fSAndroid Build Coastguard Worker new_table_block(ctx, first_block, i, _("inode bitmap"),
4164*6a54128fSAndroid Build Coastguard Worker 1, &new_blk);
4165*6a54128fSAndroid Build Coastguard Worker ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
4166*6a54128fSAndroid Build Coastguard Worker }
4167*6a54128fSAndroid Build Coastguard Worker if (ctx->invalid_inode_table_flag[i]) {
4168*6a54128fSAndroid Build Coastguard Worker new_blk = ext2fs_inode_table_loc(fs, i);
4169*6a54128fSAndroid Build Coastguard Worker new_table_block(ctx, first_block, i, _("inode table"),
4170*6a54128fSAndroid Build Coastguard Worker fs->inode_blocks_per_group,
4171*6a54128fSAndroid Build Coastguard Worker &new_blk);
4172*6a54128fSAndroid Build Coastguard Worker ext2fs_inode_table_loc_set(fs, i, new_blk);
4173*6a54128fSAndroid Build Coastguard Worker ctx->flags |= E2F_FLAG_RESTART;
4174*6a54128fSAndroid Build Coastguard Worker }
4175*6a54128fSAndroid Build Coastguard Worker }
4176*6a54128fSAndroid Build Coastguard Worker ctx->invalid_bitmaps = 0;
4177*6a54128fSAndroid Build Coastguard Worker }
4178*6a54128fSAndroid Build Coastguard Worker
4179*6a54128fSAndroid Build Coastguard Worker /*
4180*6a54128fSAndroid Build Coastguard Worker * This routine marks all blocks which are used by the superblock,
4181*6a54128fSAndroid Build Coastguard Worker * group descriptors, inode bitmaps, and block bitmaps.
4182*6a54128fSAndroid Build Coastguard Worker */
mark_table_blocks(e2fsck_t ctx)4183*6a54128fSAndroid Build Coastguard Worker static void mark_table_blocks(e2fsck_t ctx)
4184*6a54128fSAndroid Build Coastguard Worker {
4185*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs = ctx->fs;
4186*6a54128fSAndroid Build Coastguard Worker blk64_t b;
4187*6a54128fSAndroid Build Coastguard Worker dgrp_t i;
4188*6a54128fSAndroid Build Coastguard Worker unsigned int j;
4189*6a54128fSAndroid Build Coastguard Worker struct problem_context pctx;
4190*6a54128fSAndroid Build Coastguard Worker
4191*6a54128fSAndroid Build Coastguard Worker clear_problem_context(&pctx);
4192*6a54128fSAndroid Build Coastguard Worker
4193*6a54128fSAndroid Build Coastguard Worker for (i = 0; i < fs->group_desc_count; i++) {
4194*6a54128fSAndroid Build Coastguard Worker pctx.group = i;
4195*6a54128fSAndroid Build Coastguard Worker
4196*6a54128fSAndroid Build Coastguard Worker ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
4197*6a54128fSAndroid Build Coastguard Worker ext2fs_reserve_super_and_bgd(fs, i, ctx->block_metadata_map);
4198*6a54128fSAndroid Build Coastguard Worker
4199*6a54128fSAndroid Build Coastguard Worker /*
4200*6a54128fSAndroid Build Coastguard Worker * Mark the blocks used for the inode table
4201*6a54128fSAndroid Build Coastguard Worker */
4202*6a54128fSAndroid Build Coastguard Worker if (ext2fs_inode_table_loc(fs, i)) {
4203*6a54128fSAndroid Build Coastguard Worker for (j = 0, b = ext2fs_inode_table_loc(fs, i);
4204*6a54128fSAndroid Build Coastguard Worker j < fs->inode_blocks_per_group;
4205*6a54128fSAndroid Build Coastguard Worker j++, b++) {
4206*6a54128fSAndroid Build Coastguard Worker if (ext2fs_test_block_bitmap2(ctx->block_found_map,
4207*6a54128fSAndroid Build Coastguard Worker b)) {
4208*6a54128fSAndroid Build Coastguard Worker pctx.blk = b;
4209*6a54128fSAndroid Build Coastguard Worker if (!ctx->invalid_inode_table_flag[i] &&
4210*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx,
4211*6a54128fSAndroid Build Coastguard Worker PR_1_ITABLE_CONFLICT, &pctx)) {
4212*6a54128fSAndroid Build Coastguard Worker ctx->invalid_inode_table_flag[i]++;
4213*6a54128fSAndroid Build Coastguard Worker ctx->invalid_bitmaps++;
4214*6a54128fSAndroid Build Coastguard Worker }
4215*6a54128fSAndroid Build Coastguard Worker } else {
4216*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_block_bitmap2(
4217*6a54128fSAndroid Build Coastguard Worker ctx->block_found_map, b);
4218*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_block_bitmap2(
4219*6a54128fSAndroid Build Coastguard Worker ctx->block_metadata_map, b);
4220*6a54128fSAndroid Build Coastguard Worker }
4221*6a54128fSAndroid Build Coastguard Worker }
4222*6a54128fSAndroid Build Coastguard Worker }
4223*6a54128fSAndroid Build Coastguard Worker
4224*6a54128fSAndroid Build Coastguard Worker /*
4225*6a54128fSAndroid Build Coastguard Worker * Mark block used for the block bitmap
4226*6a54128fSAndroid Build Coastguard Worker */
4227*6a54128fSAndroid Build Coastguard Worker if (ext2fs_block_bitmap_loc(fs, i)) {
4228*6a54128fSAndroid Build Coastguard Worker if (ext2fs_test_block_bitmap2(ctx->block_found_map,
4229*6a54128fSAndroid Build Coastguard Worker ext2fs_block_bitmap_loc(fs, i))) {
4230*6a54128fSAndroid Build Coastguard Worker pctx.blk = ext2fs_block_bitmap_loc(fs, i);
4231*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
4232*6a54128fSAndroid Build Coastguard Worker ctx->invalid_block_bitmap_flag[i]++;
4233*6a54128fSAndroid Build Coastguard Worker ctx->invalid_bitmaps++;
4234*6a54128fSAndroid Build Coastguard Worker }
4235*6a54128fSAndroid Build Coastguard Worker } else {
4236*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_block_bitmap2(ctx->block_found_map,
4237*6a54128fSAndroid Build Coastguard Worker ext2fs_block_bitmap_loc(fs, i));
4238*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
4239*6a54128fSAndroid Build Coastguard Worker ext2fs_block_bitmap_loc(fs, i));
4240*6a54128fSAndroid Build Coastguard Worker }
4241*6a54128fSAndroid Build Coastguard Worker }
4242*6a54128fSAndroid Build Coastguard Worker /*
4243*6a54128fSAndroid Build Coastguard Worker * Mark block used for the inode bitmap
4244*6a54128fSAndroid Build Coastguard Worker */
4245*6a54128fSAndroid Build Coastguard Worker if (ext2fs_inode_bitmap_loc(fs, i)) {
4246*6a54128fSAndroid Build Coastguard Worker if (ext2fs_test_block_bitmap2(ctx->block_found_map,
4247*6a54128fSAndroid Build Coastguard Worker ext2fs_inode_bitmap_loc(fs, i))) {
4248*6a54128fSAndroid Build Coastguard Worker pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
4249*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
4250*6a54128fSAndroid Build Coastguard Worker ctx->invalid_inode_bitmap_flag[i]++;
4251*6a54128fSAndroid Build Coastguard Worker ctx->invalid_bitmaps++;
4252*6a54128fSAndroid Build Coastguard Worker }
4253*6a54128fSAndroid Build Coastguard Worker } else {
4254*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
4255*6a54128fSAndroid Build Coastguard Worker ext2fs_inode_bitmap_loc(fs, i));
4256*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_block_bitmap2(ctx->block_found_map,
4257*6a54128fSAndroid Build Coastguard Worker ext2fs_inode_bitmap_loc(fs, i));
4258*6a54128fSAndroid Build Coastguard Worker }
4259*6a54128fSAndroid Build Coastguard Worker }
4260*6a54128fSAndroid Build Coastguard Worker }
4261*6a54128fSAndroid Build Coastguard Worker }
4262*6a54128fSAndroid Build Coastguard Worker
4263*6a54128fSAndroid Build Coastguard Worker /*
4264*6a54128fSAndroid Build Coastguard Worker * These subroutines short circuits ext2fs_get_blocks and
4265*6a54128fSAndroid Build Coastguard Worker * ext2fs_check_directory; we use them since we already have the inode
4266*6a54128fSAndroid Build Coastguard Worker * structure, so there's no point in letting the ext2fs library read
4267*6a54128fSAndroid Build Coastguard Worker * the inode again.
4268*6a54128fSAndroid Build Coastguard Worker */
pass1_get_blocks(ext2_filsys fs,ext2_ino_t ino,blk_t * blocks)4269*6a54128fSAndroid Build Coastguard Worker static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
4270*6a54128fSAndroid Build Coastguard Worker blk_t *blocks)
4271*6a54128fSAndroid Build Coastguard Worker {
4272*6a54128fSAndroid Build Coastguard Worker e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4273*6a54128fSAndroid Build Coastguard Worker int i;
4274*6a54128fSAndroid Build Coastguard Worker
4275*6a54128fSAndroid Build Coastguard Worker if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
4276*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_CALLBACK_NOTHANDLED;
4277*6a54128fSAndroid Build Coastguard Worker
4278*6a54128fSAndroid Build Coastguard Worker for (i=0; i < EXT2_N_BLOCKS; i++)
4279*6a54128fSAndroid Build Coastguard Worker blocks[i] = ctx->stashed_inode->i_block[i];
4280*6a54128fSAndroid Build Coastguard Worker return 0;
4281*6a54128fSAndroid Build Coastguard Worker }
4282*6a54128fSAndroid Build Coastguard Worker
pass1_read_inode(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode)4283*6a54128fSAndroid Build Coastguard Worker static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
4284*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode)
4285*6a54128fSAndroid Build Coastguard Worker {
4286*6a54128fSAndroid Build Coastguard Worker e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4287*6a54128fSAndroid Build Coastguard Worker
4288*6a54128fSAndroid Build Coastguard Worker if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
4289*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_CALLBACK_NOTHANDLED;
4290*6a54128fSAndroid Build Coastguard Worker *inode = *ctx->stashed_inode;
4291*6a54128fSAndroid Build Coastguard Worker return 0;
4292*6a54128fSAndroid Build Coastguard Worker }
4293*6a54128fSAndroid Build Coastguard Worker
pass1_write_inode(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode)4294*6a54128fSAndroid Build Coastguard Worker static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
4295*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode)
4296*6a54128fSAndroid Build Coastguard Worker {
4297*6a54128fSAndroid Build Coastguard Worker e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4298*6a54128fSAndroid Build Coastguard Worker
4299*6a54128fSAndroid Build Coastguard Worker if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
4300*6a54128fSAndroid Build Coastguard Worker (inode != ctx->stashed_inode))
4301*6a54128fSAndroid Build Coastguard Worker *ctx->stashed_inode = *inode;
4302*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_CALLBACK_NOTHANDLED;
4303*6a54128fSAndroid Build Coastguard Worker }
4304*6a54128fSAndroid Build Coastguard Worker
pass1_check_directory(ext2_filsys fs,ext2_ino_t ino)4305*6a54128fSAndroid Build Coastguard Worker static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
4306*6a54128fSAndroid Build Coastguard Worker {
4307*6a54128fSAndroid Build Coastguard Worker e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4308*6a54128fSAndroid Build Coastguard Worker
4309*6a54128fSAndroid Build Coastguard Worker if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
4310*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_CALLBACK_NOTHANDLED;
4311*6a54128fSAndroid Build Coastguard Worker
4312*6a54128fSAndroid Build Coastguard Worker if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
4313*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NO_DIRECTORY;
4314*6a54128fSAndroid Build Coastguard Worker return 0;
4315*6a54128fSAndroid Build Coastguard Worker }
4316*6a54128fSAndroid Build Coastguard Worker
e2fsck_get_alloc_block(ext2_filsys fs,blk64_t goal,blk64_t * ret)4317*6a54128fSAndroid Build Coastguard Worker static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
4318*6a54128fSAndroid Build Coastguard Worker blk64_t *ret)
4319*6a54128fSAndroid Build Coastguard Worker {
4320*6a54128fSAndroid Build Coastguard Worker e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4321*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
4322*6a54128fSAndroid Build Coastguard Worker blk64_t new_block;
4323*6a54128fSAndroid Build Coastguard Worker
4324*6a54128fSAndroid Build Coastguard Worker if (ctx->block_found_map) {
4325*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
4326*6a54128fSAndroid Build Coastguard Worker &new_block);
4327*6a54128fSAndroid Build Coastguard Worker if (retval)
4328*6a54128fSAndroid Build Coastguard Worker return retval;
4329*6a54128fSAndroid Build Coastguard Worker if (fs->block_map) {
4330*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_block_bitmap2(fs->block_map, new_block);
4331*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_bb_dirty(fs);
4332*6a54128fSAndroid Build Coastguard Worker }
4333*6a54128fSAndroid Build Coastguard Worker } else {
4334*6a54128fSAndroid Build Coastguard Worker if (!fs->block_map) {
4335*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_read_block_bitmap(fs);
4336*6a54128fSAndroid Build Coastguard Worker if (retval)
4337*6a54128fSAndroid Build Coastguard Worker return retval;
4338*6a54128fSAndroid Build Coastguard Worker }
4339*6a54128fSAndroid Build Coastguard Worker
4340*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_new_block2(fs, goal, fs->block_map, &new_block);
4341*6a54128fSAndroid Build Coastguard Worker if (retval)
4342*6a54128fSAndroid Build Coastguard Worker return retval;
4343*6a54128fSAndroid Build Coastguard Worker }
4344*6a54128fSAndroid Build Coastguard Worker
4345*6a54128fSAndroid Build Coastguard Worker *ret = new_block;
4346*6a54128fSAndroid Build Coastguard Worker return (0);
4347*6a54128fSAndroid Build Coastguard Worker }
4348*6a54128fSAndroid Build Coastguard Worker
e2fsck_new_range(ext2_filsys fs,int flags,blk64_t goal,blk64_t len,blk64_t * pblk,blk64_t * plen)4349*6a54128fSAndroid Build Coastguard Worker static errcode_t e2fsck_new_range(ext2_filsys fs, int flags, blk64_t goal,
4350*6a54128fSAndroid Build Coastguard Worker blk64_t len, blk64_t *pblk, blk64_t *plen)
4351*6a54128fSAndroid Build Coastguard Worker {
4352*6a54128fSAndroid Build Coastguard Worker e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4353*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
4354*6a54128fSAndroid Build Coastguard Worker
4355*6a54128fSAndroid Build Coastguard Worker if (ctx->block_found_map)
4356*6a54128fSAndroid Build Coastguard Worker return ext2fs_new_range(fs, flags, goal, len,
4357*6a54128fSAndroid Build Coastguard Worker ctx->block_found_map, pblk, plen);
4358*6a54128fSAndroid Build Coastguard Worker
4359*6a54128fSAndroid Build Coastguard Worker if (!fs->block_map) {
4360*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_read_block_bitmap(fs);
4361*6a54128fSAndroid Build Coastguard Worker if (retval)
4362*6a54128fSAndroid Build Coastguard Worker return retval;
4363*6a54128fSAndroid Build Coastguard Worker }
4364*6a54128fSAndroid Build Coastguard Worker
4365*6a54128fSAndroid Build Coastguard Worker return ext2fs_new_range(fs, flags, goal, len, fs->block_map,
4366*6a54128fSAndroid Build Coastguard Worker pblk, plen);
4367*6a54128fSAndroid Build Coastguard Worker }
4368*6a54128fSAndroid Build Coastguard Worker
e2fsck_block_alloc_stats(ext2_filsys fs,blk64_t blk,int inuse)4369*6a54128fSAndroid Build Coastguard Worker static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
4370*6a54128fSAndroid Build Coastguard Worker {
4371*6a54128fSAndroid Build Coastguard Worker e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4372*6a54128fSAndroid Build Coastguard Worker
4373*6a54128fSAndroid Build Coastguard Worker /* Never free a critical metadata block */
4374*6a54128fSAndroid Build Coastguard Worker if (ctx->block_found_map &&
4375*6a54128fSAndroid Build Coastguard Worker ctx->block_metadata_map &&
4376*6a54128fSAndroid Build Coastguard Worker inuse < 0 &&
4377*6a54128fSAndroid Build Coastguard Worker ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk))
4378*6a54128fSAndroid Build Coastguard Worker return;
4379*6a54128fSAndroid Build Coastguard Worker
4380*6a54128fSAndroid Build Coastguard Worker if (ctx->block_found_map) {
4381*6a54128fSAndroid Build Coastguard Worker if (inuse > 0)
4382*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
4383*6a54128fSAndroid Build Coastguard Worker else
4384*6a54128fSAndroid Build Coastguard Worker ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
4385*6a54128fSAndroid Build Coastguard Worker }
4386*6a54128fSAndroid Build Coastguard Worker }
4387*6a54128fSAndroid Build Coastguard Worker
e2fsck_block_alloc_stats_range(ext2_filsys fs,blk64_t blk,blk_t num,int inuse)4388*6a54128fSAndroid Build Coastguard Worker static void e2fsck_block_alloc_stats_range(ext2_filsys fs, blk64_t blk,
4389*6a54128fSAndroid Build Coastguard Worker blk_t num, int inuse)
4390*6a54128fSAndroid Build Coastguard Worker {
4391*6a54128fSAndroid Build Coastguard Worker e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4392*6a54128fSAndroid Build Coastguard Worker
4393*6a54128fSAndroid Build Coastguard Worker /* Never free a critical metadata block */
4394*6a54128fSAndroid Build Coastguard Worker if (ctx->block_found_map &&
4395*6a54128fSAndroid Build Coastguard Worker ctx->block_metadata_map &&
4396*6a54128fSAndroid Build Coastguard Worker inuse < 0 &&
4397*6a54128fSAndroid Build Coastguard Worker ext2fs_test_block_bitmap_range2(ctx->block_metadata_map, blk, num))
4398*6a54128fSAndroid Build Coastguard Worker return;
4399*6a54128fSAndroid Build Coastguard Worker
4400*6a54128fSAndroid Build Coastguard Worker if (ctx->block_found_map) {
4401*6a54128fSAndroid Build Coastguard Worker if (inuse > 0)
4402*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_block_bitmap_range2(ctx->block_found_map,
4403*6a54128fSAndroid Build Coastguard Worker blk, num);
4404*6a54128fSAndroid Build Coastguard Worker else
4405*6a54128fSAndroid Build Coastguard Worker ext2fs_unmark_block_bitmap_range2(ctx->block_found_map,
4406*6a54128fSAndroid Build Coastguard Worker blk, num);
4407*6a54128fSAndroid Build Coastguard Worker }
4408*6a54128fSAndroid Build Coastguard Worker }
4409*6a54128fSAndroid Build Coastguard Worker
e2fsck_use_inode_shortcuts(e2fsck_t ctx,int use_shortcuts)4410*6a54128fSAndroid Build Coastguard Worker void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
4411*6a54128fSAndroid Build Coastguard Worker {
4412*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs = ctx->fs;
4413*6a54128fSAndroid Build Coastguard Worker
4414*6a54128fSAndroid Build Coastguard Worker if (use_shortcuts) {
4415*6a54128fSAndroid Build Coastguard Worker fs->get_blocks = pass1_get_blocks;
4416*6a54128fSAndroid Build Coastguard Worker fs->check_directory = pass1_check_directory;
4417*6a54128fSAndroid Build Coastguard Worker fs->read_inode = pass1_read_inode;
4418*6a54128fSAndroid Build Coastguard Worker fs->write_inode = pass1_write_inode;
4419*6a54128fSAndroid Build Coastguard Worker ctx->stashed_ino = 0;
4420*6a54128fSAndroid Build Coastguard Worker } else {
4421*6a54128fSAndroid Build Coastguard Worker fs->get_blocks = 0;
4422*6a54128fSAndroid Build Coastguard Worker fs->check_directory = 0;
4423*6a54128fSAndroid Build Coastguard Worker fs->read_inode = 0;
4424*6a54128fSAndroid Build Coastguard Worker fs->write_inode = 0;
4425*6a54128fSAndroid Build Coastguard Worker }
4426*6a54128fSAndroid Build Coastguard Worker }
4427*6a54128fSAndroid Build Coastguard Worker
e2fsck_intercept_block_allocations(e2fsck_t ctx)4428*6a54128fSAndroid Build Coastguard Worker void e2fsck_intercept_block_allocations(e2fsck_t ctx)
4429*6a54128fSAndroid Build Coastguard Worker {
4430*6a54128fSAndroid Build Coastguard Worker ext2fs_set_alloc_block_callback(ctx->fs, e2fsck_get_alloc_block, 0);
4431*6a54128fSAndroid Build Coastguard Worker ext2fs_set_block_alloc_stats_callback(ctx->fs,
4432*6a54128fSAndroid Build Coastguard Worker e2fsck_block_alloc_stats, 0);
4433*6a54128fSAndroid Build Coastguard Worker ext2fs_set_new_range_callback(ctx->fs, e2fsck_new_range, NULL);
4434*6a54128fSAndroid Build Coastguard Worker ext2fs_set_block_alloc_stats_range_callback(ctx->fs,
4435*6a54128fSAndroid Build Coastguard Worker e2fsck_block_alloc_stats_range, NULL);
4436*6a54128fSAndroid Build Coastguard Worker }
4437