1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker * inode.c --- utility routines to read and write inodes
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 Library
8*6a54128fSAndroid Build Coastguard Worker * General Public License, version 2.
9*6a54128fSAndroid Build Coastguard Worker * %End-Header%
10*6a54128fSAndroid Build Coastguard Worker */
11*6a54128fSAndroid Build Coastguard Worker
12*6a54128fSAndroid Build Coastguard Worker #include "config.h"
13*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
14*6a54128fSAndroid Build Coastguard Worker #include <string.h>
15*6a54128fSAndroid Build Coastguard Worker #if HAVE_UNISTD_H
16*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
17*6a54128fSAndroid Build Coastguard Worker #endif
18*6a54128fSAndroid Build Coastguard Worker #if HAVE_ERRNO_H
19*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
20*6a54128fSAndroid Build Coastguard Worker #endif
21*6a54128fSAndroid Build Coastguard Worker #include <time.h>
22*6a54128fSAndroid Build Coastguard Worker #if HAVE_SYS_STAT_H
23*6a54128fSAndroid Build Coastguard Worker #include <sys/stat.h>
24*6a54128fSAndroid Build Coastguard Worker #endif
25*6a54128fSAndroid Build Coastguard Worker #if HAVE_SYS_TYPES_H
26*6a54128fSAndroid Build Coastguard Worker #include <sys/types.h>
27*6a54128fSAndroid Build Coastguard Worker #endif
28*6a54128fSAndroid Build Coastguard Worker
29*6a54128fSAndroid Build Coastguard Worker #include "ext2_fs.h"
30*6a54128fSAndroid Build Coastguard Worker #include "ext2fsP.h"
31*6a54128fSAndroid Build Coastguard Worker #include "e2image.h"
32*6a54128fSAndroid Build Coastguard Worker
33*6a54128fSAndroid Build Coastguard Worker #define IBLOCK_STATUS_CSUMS_OK 1
34*6a54128fSAndroid Build Coastguard Worker #define IBLOCK_STATUS_INSANE 2
35*6a54128fSAndroid Build Coastguard Worker #define SCAN_BLOCK_STATUS(scan) ((scan)->temp_buffer + (scan)->inode_size)
36*6a54128fSAndroid Build Coastguard Worker
37*6a54128fSAndroid Build Coastguard Worker struct ext2_struct_inode_scan {
38*6a54128fSAndroid Build Coastguard Worker errcode_t magic;
39*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs;
40*6a54128fSAndroid Build Coastguard Worker ext2_ino_t current_inode;
41*6a54128fSAndroid Build Coastguard Worker blk64_t current_block;
42*6a54128fSAndroid Build Coastguard Worker dgrp_t current_group;
43*6a54128fSAndroid Build Coastguard Worker ext2_ino_t inodes_left;
44*6a54128fSAndroid Build Coastguard Worker blk_t blocks_left;
45*6a54128fSAndroid Build Coastguard Worker dgrp_t groups_left;
46*6a54128fSAndroid Build Coastguard Worker blk_t inode_buffer_blocks;
47*6a54128fSAndroid Build Coastguard Worker char * inode_buffer;
48*6a54128fSAndroid Build Coastguard Worker int inode_size;
49*6a54128fSAndroid Build Coastguard Worker char * ptr;
50*6a54128fSAndroid Build Coastguard Worker int bytes_left;
51*6a54128fSAndroid Build Coastguard Worker char *temp_buffer;
52*6a54128fSAndroid Build Coastguard Worker errcode_t (*done_group)(ext2_filsys fs,
53*6a54128fSAndroid Build Coastguard Worker ext2_inode_scan scan,
54*6a54128fSAndroid Build Coastguard Worker dgrp_t group,
55*6a54128fSAndroid Build Coastguard Worker void * priv_data);
56*6a54128fSAndroid Build Coastguard Worker void * done_group_data;
57*6a54128fSAndroid Build Coastguard Worker int bad_block_ptr;
58*6a54128fSAndroid Build Coastguard Worker int scan_flags;
59*6a54128fSAndroid Build Coastguard Worker int reserved[6];
60*6a54128fSAndroid Build Coastguard Worker };
61*6a54128fSAndroid Build Coastguard Worker
62*6a54128fSAndroid Build Coastguard Worker /*
63*6a54128fSAndroid Build Coastguard Worker * This routine flushes the icache, if it exists.
64*6a54128fSAndroid Build Coastguard Worker */
ext2fs_flush_icache(ext2_filsys fs)65*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_flush_icache(ext2_filsys fs)
66*6a54128fSAndroid Build Coastguard Worker {
67*6a54128fSAndroid Build Coastguard Worker unsigned i;
68*6a54128fSAndroid Build Coastguard Worker
69*6a54128fSAndroid Build Coastguard Worker if (!fs->icache)
70*6a54128fSAndroid Build Coastguard Worker return 0;
71*6a54128fSAndroid Build Coastguard Worker
72*6a54128fSAndroid Build Coastguard Worker for (i=0; i < fs->icache->cache_size; i++)
73*6a54128fSAndroid Build Coastguard Worker fs->icache->cache[i].ino = 0;
74*6a54128fSAndroid Build Coastguard Worker
75*6a54128fSAndroid Build Coastguard Worker fs->icache->buffer_blk = 0;
76*6a54128fSAndroid Build Coastguard Worker return 0;
77*6a54128fSAndroid Build Coastguard Worker }
78*6a54128fSAndroid Build Coastguard Worker
79*6a54128fSAndroid Build Coastguard Worker /*
80*6a54128fSAndroid Build Coastguard Worker * Free the inode cache structure
81*6a54128fSAndroid Build Coastguard Worker */
ext2fs_free_inode_cache(struct ext2_inode_cache * icache)82*6a54128fSAndroid Build Coastguard Worker void ext2fs_free_inode_cache(struct ext2_inode_cache *icache)
83*6a54128fSAndroid Build Coastguard Worker {
84*6a54128fSAndroid Build Coastguard Worker unsigned i;
85*6a54128fSAndroid Build Coastguard Worker
86*6a54128fSAndroid Build Coastguard Worker if (--icache->refcount)
87*6a54128fSAndroid Build Coastguard Worker return;
88*6a54128fSAndroid Build Coastguard Worker if (icache->buffer)
89*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&icache->buffer);
90*6a54128fSAndroid Build Coastguard Worker for (i = 0; i < icache->cache_size; i++)
91*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&icache->cache[i].inode);
92*6a54128fSAndroid Build Coastguard Worker if (icache->cache)
93*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&icache->cache);
94*6a54128fSAndroid Build Coastguard Worker icache->buffer_blk = 0;
95*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&icache);
96*6a54128fSAndroid Build Coastguard Worker }
97*6a54128fSAndroid Build Coastguard Worker
ext2fs_create_inode_cache(ext2_filsys fs,unsigned int cache_size)98*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_create_inode_cache(ext2_filsys fs, unsigned int cache_size)
99*6a54128fSAndroid Build Coastguard Worker {
100*6a54128fSAndroid Build Coastguard Worker unsigned i;
101*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
102*6a54128fSAndroid Build Coastguard Worker
103*6a54128fSAndroid Build Coastguard Worker if (fs->icache)
104*6a54128fSAndroid Build Coastguard Worker return 0;
105*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache), &fs->icache);
106*6a54128fSAndroid Build Coastguard Worker if (retval)
107*6a54128fSAndroid Build Coastguard Worker return retval;
108*6a54128fSAndroid Build Coastguard Worker
109*6a54128fSAndroid Build Coastguard Worker memset(fs->icache, 0, sizeof(struct ext2_inode_cache));
110*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_mem(fs->blocksize, &fs->icache->buffer);
111*6a54128fSAndroid Build Coastguard Worker if (retval)
112*6a54128fSAndroid Build Coastguard Worker goto errout;
113*6a54128fSAndroid Build Coastguard Worker
114*6a54128fSAndroid Build Coastguard Worker fs->icache->buffer_blk = 0;
115*6a54128fSAndroid Build Coastguard Worker fs->icache->cache_last = -1;
116*6a54128fSAndroid Build Coastguard Worker fs->icache->cache_size = cache_size;
117*6a54128fSAndroid Build Coastguard Worker fs->icache->refcount = 1;
118*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_array(fs->icache->cache_size,
119*6a54128fSAndroid Build Coastguard Worker sizeof(struct ext2_inode_cache_ent),
120*6a54128fSAndroid Build Coastguard Worker &fs->icache->cache);
121*6a54128fSAndroid Build Coastguard Worker if (retval)
122*6a54128fSAndroid Build Coastguard Worker goto errout;
123*6a54128fSAndroid Build Coastguard Worker
124*6a54128fSAndroid Build Coastguard Worker for (i = 0; i < fs->icache->cache_size; i++) {
125*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_mem(EXT2_INODE_SIZE(fs->super),
126*6a54128fSAndroid Build Coastguard Worker &fs->icache->cache[i].inode);
127*6a54128fSAndroid Build Coastguard Worker if (retval)
128*6a54128fSAndroid Build Coastguard Worker goto errout;
129*6a54128fSAndroid Build Coastguard Worker }
130*6a54128fSAndroid Build Coastguard Worker
131*6a54128fSAndroid Build Coastguard Worker ext2fs_flush_icache(fs);
132*6a54128fSAndroid Build Coastguard Worker return 0;
133*6a54128fSAndroid Build Coastguard Worker errout:
134*6a54128fSAndroid Build Coastguard Worker ext2fs_free_inode_cache(fs->icache);
135*6a54128fSAndroid Build Coastguard Worker fs->icache = 0;
136*6a54128fSAndroid Build Coastguard Worker return retval;
137*6a54128fSAndroid Build Coastguard Worker }
138*6a54128fSAndroid Build Coastguard Worker
ext2fs_open_inode_scan(ext2_filsys fs,int buffer_blocks,ext2_inode_scan * ret_scan)139*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
140*6a54128fSAndroid Build Coastguard Worker ext2_inode_scan *ret_scan)
141*6a54128fSAndroid Build Coastguard Worker {
142*6a54128fSAndroid Build Coastguard Worker ext2_inode_scan scan;
143*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
144*6a54128fSAndroid Build Coastguard Worker errcode_t (*save_get_blocks)(ext2_filsys f, ext2_ino_t ino, blk_t *blocks);
145*6a54128fSAndroid Build Coastguard Worker
146*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
147*6a54128fSAndroid Build Coastguard Worker
148*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_feature_journal_dev(fs->super))
149*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTERNAL_JOURNAL_NOSUPP;
150*6a54128fSAndroid Build Coastguard Worker
151*6a54128fSAndroid Build Coastguard Worker if (fs->blocksize < 1024)
152*6a54128fSAndroid Build Coastguard Worker return EXT2_FILSYS_CORRUPTED; /* Should never happen */
153*6a54128fSAndroid Build Coastguard Worker
154*6a54128fSAndroid Build Coastguard Worker /*
155*6a54128fSAndroid Build Coastguard Worker * If fs->badblocks isn't set, then set it --- since the inode
156*6a54128fSAndroid Build Coastguard Worker * scanning functions require it.
157*6a54128fSAndroid Build Coastguard Worker */
158*6a54128fSAndroid Build Coastguard Worker if (fs->badblocks == 0) {
159*6a54128fSAndroid Build Coastguard Worker /*
160*6a54128fSAndroid Build Coastguard Worker * Temporarily save fs->get_blocks and set it to zero,
161*6a54128fSAndroid Build Coastguard Worker * for compatibility with old e2fsck's.
162*6a54128fSAndroid Build Coastguard Worker */
163*6a54128fSAndroid Build Coastguard Worker save_get_blocks = fs->get_blocks;
164*6a54128fSAndroid Build Coastguard Worker fs->get_blocks = 0;
165*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
166*6a54128fSAndroid Build Coastguard Worker if (retval && fs->badblocks) {
167*6a54128fSAndroid Build Coastguard Worker ext2fs_badblocks_list_free(fs->badblocks);
168*6a54128fSAndroid Build Coastguard Worker fs->badblocks = 0;
169*6a54128fSAndroid Build Coastguard Worker }
170*6a54128fSAndroid Build Coastguard Worker fs->get_blocks = save_get_blocks;
171*6a54128fSAndroid Build Coastguard Worker }
172*6a54128fSAndroid Build Coastguard Worker
173*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_mem(sizeof(struct ext2_struct_inode_scan), &scan);
174*6a54128fSAndroid Build Coastguard Worker if (retval)
175*6a54128fSAndroid Build Coastguard Worker return retval;
176*6a54128fSAndroid Build Coastguard Worker memset(scan, 0, sizeof(struct ext2_struct_inode_scan));
177*6a54128fSAndroid Build Coastguard Worker
178*6a54128fSAndroid Build Coastguard Worker scan->magic = EXT2_ET_MAGIC_INODE_SCAN;
179*6a54128fSAndroid Build Coastguard Worker scan->fs = fs;
180*6a54128fSAndroid Build Coastguard Worker scan->inode_size = EXT2_INODE_SIZE(fs->super);
181*6a54128fSAndroid Build Coastguard Worker scan->bytes_left = 0;
182*6a54128fSAndroid Build Coastguard Worker scan->current_group = 0;
183*6a54128fSAndroid Build Coastguard Worker scan->groups_left = fs->group_desc_count - 1;
184*6a54128fSAndroid Build Coastguard Worker scan->inode_buffer_blocks = buffer_blocks ? buffer_blocks :
185*6a54128fSAndroid Build Coastguard Worker EXT2_INODE_SCAN_DEFAULT_BUFFER_BLOCKS;
186*6a54128fSAndroid Build Coastguard Worker scan->current_block = ext2fs_inode_table_loc(scan->fs,
187*6a54128fSAndroid Build Coastguard Worker scan->current_group);
188*6a54128fSAndroid Build Coastguard Worker if (scan->current_block &&
189*6a54128fSAndroid Build Coastguard Worker ((scan->current_block < fs->super->s_first_data_block) ||
190*6a54128fSAndroid Build Coastguard Worker (scan->current_block + fs->inode_blocks_per_group - 1 >=
191*6a54128fSAndroid Build Coastguard Worker ext2fs_blocks_count(fs->super)))) {
192*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&scan);
193*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_GDESC_BAD_INODE_TABLE;
194*6a54128fSAndroid Build Coastguard Worker }
195*6a54128fSAndroid Build Coastguard Worker
196*6a54128fSAndroid Build Coastguard Worker scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
197*6a54128fSAndroid Build Coastguard Worker scan->blocks_left = scan->fs->inode_blocks_per_group;
198*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_group_desc_csum(fs)) {
199*6a54128fSAndroid Build Coastguard Worker __u32 unused = ext2fs_bg_itable_unused(fs, scan->current_group);
200*6a54128fSAndroid Build Coastguard Worker if (scan->inodes_left > unused)
201*6a54128fSAndroid Build Coastguard Worker scan->inodes_left -= unused;
202*6a54128fSAndroid Build Coastguard Worker else
203*6a54128fSAndroid Build Coastguard Worker scan->inodes_left = 0;
204*6a54128fSAndroid Build Coastguard Worker scan->blocks_left =
205*6a54128fSAndroid Build Coastguard Worker (scan->inodes_left +
206*6a54128fSAndroid Build Coastguard Worker (fs->blocksize / scan->inode_size - 1)) *
207*6a54128fSAndroid Build Coastguard Worker scan->inode_size / fs->blocksize;
208*6a54128fSAndroid Build Coastguard Worker }
209*6a54128fSAndroid Build Coastguard Worker retval = io_channel_alloc_buf(fs->io, scan->inode_buffer_blocks,
210*6a54128fSAndroid Build Coastguard Worker &scan->inode_buffer);
211*6a54128fSAndroid Build Coastguard Worker scan->done_group = 0;
212*6a54128fSAndroid Build Coastguard Worker scan->done_group_data = 0;
213*6a54128fSAndroid Build Coastguard Worker scan->bad_block_ptr = 0;
214*6a54128fSAndroid Build Coastguard Worker if (retval) {
215*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&scan);
216*6a54128fSAndroid Build Coastguard Worker return retval;
217*6a54128fSAndroid Build Coastguard Worker }
218*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_mem(scan->inode_size + scan->inode_buffer_blocks,
219*6a54128fSAndroid Build Coastguard Worker &scan->temp_buffer);
220*6a54128fSAndroid Build Coastguard Worker if (retval) {
221*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&scan->inode_buffer);
222*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&scan);
223*6a54128fSAndroid Build Coastguard Worker return retval;
224*6a54128fSAndroid Build Coastguard Worker }
225*6a54128fSAndroid Build Coastguard Worker memset(SCAN_BLOCK_STATUS(scan), 0, scan->inode_buffer_blocks);
226*6a54128fSAndroid Build Coastguard Worker if (scan->fs->badblocks && scan->fs->badblocks->num)
227*6a54128fSAndroid Build Coastguard Worker scan->scan_flags |= EXT2_SF_CHK_BADBLOCKS;
228*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_group_desc_csum(fs))
229*6a54128fSAndroid Build Coastguard Worker scan->scan_flags |= EXT2_SF_DO_LAZY;
230*6a54128fSAndroid Build Coastguard Worker *ret_scan = scan;
231*6a54128fSAndroid Build Coastguard Worker return 0;
232*6a54128fSAndroid Build Coastguard Worker }
233*6a54128fSAndroid Build Coastguard Worker
ext2fs_close_inode_scan(ext2_inode_scan scan)234*6a54128fSAndroid Build Coastguard Worker void ext2fs_close_inode_scan(ext2_inode_scan scan)
235*6a54128fSAndroid Build Coastguard Worker {
236*6a54128fSAndroid Build Coastguard Worker if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
237*6a54128fSAndroid Build Coastguard Worker return;
238*6a54128fSAndroid Build Coastguard Worker
239*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&scan->inode_buffer);
240*6a54128fSAndroid Build Coastguard Worker scan->inode_buffer = NULL;
241*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&scan->temp_buffer);
242*6a54128fSAndroid Build Coastguard Worker scan->temp_buffer = NULL;
243*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&scan);
244*6a54128fSAndroid Build Coastguard Worker return;
245*6a54128fSAndroid Build Coastguard Worker }
246*6a54128fSAndroid Build Coastguard Worker
ext2fs_set_inode_callback(ext2_inode_scan scan,errcode_t (* done_group)(ext2_filsys fs,ext2_inode_scan scan,dgrp_t group,void * priv_data),void * done_group_data)247*6a54128fSAndroid Build Coastguard Worker void ext2fs_set_inode_callback(ext2_inode_scan scan,
248*6a54128fSAndroid Build Coastguard Worker errcode_t (*done_group)(ext2_filsys fs,
249*6a54128fSAndroid Build Coastguard Worker ext2_inode_scan scan,
250*6a54128fSAndroid Build Coastguard Worker dgrp_t group,
251*6a54128fSAndroid Build Coastguard Worker void * priv_data),
252*6a54128fSAndroid Build Coastguard Worker void *done_group_data)
253*6a54128fSAndroid Build Coastguard Worker {
254*6a54128fSAndroid Build Coastguard Worker if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
255*6a54128fSAndroid Build Coastguard Worker return;
256*6a54128fSAndroid Build Coastguard Worker
257*6a54128fSAndroid Build Coastguard Worker scan->done_group = done_group;
258*6a54128fSAndroid Build Coastguard Worker scan->done_group_data = done_group_data;
259*6a54128fSAndroid Build Coastguard Worker }
260*6a54128fSAndroid Build Coastguard Worker
ext2fs_inode_scan_flags(ext2_inode_scan scan,int set_flags,int clear_flags)261*6a54128fSAndroid Build Coastguard Worker int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
262*6a54128fSAndroid Build Coastguard Worker int clear_flags)
263*6a54128fSAndroid Build Coastguard Worker {
264*6a54128fSAndroid Build Coastguard Worker int old_flags;
265*6a54128fSAndroid Build Coastguard Worker
266*6a54128fSAndroid Build Coastguard Worker if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
267*6a54128fSAndroid Build Coastguard Worker return 0;
268*6a54128fSAndroid Build Coastguard Worker
269*6a54128fSAndroid Build Coastguard Worker old_flags = scan->scan_flags;
270*6a54128fSAndroid Build Coastguard Worker scan->scan_flags &= ~clear_flags;
271*6a54128fSAndroid Build Coastguard Worker scan->scan_flags |= set_flags;
272*6a54128fSAndroid Build Coastguard Worker return old_flags;
273*6a54128fSAndroid Build Coastguard Worker }
274*6a54128fSAndroid Build Coastguard Worker
275*6a54128fSAndroid Build Coastguard Worker /*
276*6a54128fSAndroid Build Coastguard Worker * This function is called by ext2fs_get_next_inode when it needs to
277*6a54128fSAndroid Build Coastguard Worker * get ready to read in a new blockgroup.
278*6a54128fSAndroid Build Coastguard Worker */
get_next_blockgroup(ext2_inode_scan scan)279*6a54128fSAndroid Build Coastguard Worker static errcode_t get_next_blockgroup(ext2_inode_scan scan)
280*6a54128fSAndroid Build Coastguard Worker {
281*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs = scan->fs;
282*6a54128fSAndroid Build Coastguard Worker
283*6a54128fSAndroid Build Coastguard Worker scan->current_group++;
284*6a54128fSAndroid Build Coastguard Worker scan->groups_left--;
285*6a54128fSAndroid Build Coastguard Worker
286*6a54128fSAndroid Build Coastguard Worker scan->current_block = ext2fs_inode_table_loc(scan->fs,
287*6a54128fSAndroid Build Coastguard Worker scan->current_group);
288*6a54128fSAndroid Build Coastguard Worker scan->current_inode = scan->current_group *
289*6a54128fSAndroid Build Coastguard Worker EXT2_INODES_PER_GROUP(fs->super);
290*6a54128fSAndroid Build Coastguard Worker
291*6a54128fSAndroid Build Coastguard Worker scan->bytes_left = 0;
292*6a54128fSAndroid Build Coastguard Worker scan->inodes_left = EXT2_INODES_PER_GROUP(fs->super);
293*6a54128fSAndroid Build Coastguard Worker scan->blocks_left = fs->inode_blocks_per_group;
294*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_group_desc_csum(fs)) {
295*6a54128fSAndroid Build Coastguard Worker __u32 unused = ext2fs_bg_itable_unused(fs, scan->current_group);
296*6a54128fSAndroid Build Coastguard Worker if (scan->inodes_left > unused)
297*6a54128fSAndroid Build Coastguard Worker scan->inodes_left -= unused;
298*6a54128fSAndroid Build Coastguard Worker else
299*6a54128fSAndroid Build Coastguard Worker scan->inodes_left = 0;
300*6a54128fSAndroid Build Coastguard Worker scan->blocks_left =
301*6a54128fSAndroid Build Coastguard Worker (scan->inodes_left +
302*6a54128fSAndroid Build Coastguard Worker (fs->blocksize / scan->inode_size - 1)) *
303*6a54128fSAndroid Build Coastguard Worker scan->inode_size / fs->blocksize;
304*6a54128fSAndroid Build Coastguard Worker }
305*6a54128fSAndroid Build Coastguard Worker if (scan->current_block &&
306*6a54128fSAndroid Build Coastguard Worker ((scan->current_block < fs->super->s_first_data_block) ||
307*6a54128fSAndroid Build Coastguard Worker (scan->current_block + fs->inode_blocks_per_group - 1 >=
308*6a54128fSAndroid Build Coastguard Worker ext2fs_blocks_count(fs->super))))
309*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_GDESC_BAD_INODE_TABLE;
310*6a54128fSAndroid Build Coastguard Worker return 0;
311*6a54128fSAndroid Build Coastguard Worker }
312*6a54128fSAndroid Build Coastguard Worker
ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,int group)313*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
314*6a54128fSAndroid Build Coastguard Worker int group)
315*6a54128fSAndroid Build Coastguard Worker {
316*6a54128fSAndroid Build Coastguard Worker scan->current_group = group - 1;
317*6a54128fSAndroid Build Coastguard Worker scan->groups_left = scan->fs->group_desc_count - group;
318*6a54128fSAndroid Build Coastguard Worker scan->bad_block_ptr = 0;
319*6a54128fSAndroid Build Coastguard Worker return get_next_blockgroup(scan);
320*6a54128fSAndroid Build Coastguard Worker }
321*6a54128fSAndroid Build Coastguard Worker
322*6a54128fSAndroid Build Coastguard Worker /*
323*6a54128fSAndroid Build Coastguard Worker * This function is called by get_next_blocks() to check for bad
324*6a54128fSAndroid Build Coastguard Worker * blocks in the inode table.
325*6a54128fSAndroid Build Coastguard Worker *
326*6a54128fSAndroid Build Coastguard Worker * This function assumes that badblocks_list->list is sorted in
327*6a54128fSAndroid Build Coastguard Worker * increasing order.
328*6a54128fSAndroid Build Coastguard Worker */
check_for_inode_bad_blocks(ext2_inode_scan scan,blk64_t * num_blocks)329*6a54128fSAndroid Build Coastguard Worker static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan,
330*6a54128fSAndroid Build Coastguard Worker blk64_t *num_blocks)
331*6a54128fSAndroid Build Coastguard Worker {
332*6a54128fSAndroid Build Coastguard Worker blk64_t blk = scan->current_block;
333*6a54128fSAndroid Build Coastguard Worker badblocks_list bb = scan->fs->badblocks;
334*6a54128fSAndroid Build Coastguard Worker
335*6a54128fSAndroid Build Coastguard Worker /*
336*6a54128fSAndroid Build Coastguard Worker * If the inode table is missing, then obviously there are no
337*6a54128fSAndroid Build Coastguard Worker * bad blocks. :-)
338*6a54128fSAndroid Build Coastguard Worker */
339*6a54128fSAndroid Build Coastguard Worker if (blk == 0)
340*6a54128fSAndroid Build Coastguard Worker return 0;
341*6a54128fSAndroid Build Coastguard Worker
342*6a54128fSAndroid Build Coastguard Worker /* Make sure bad_block_ptr is still valid */
343*6a54128fSAndroid Build Coastguard Worker if (scan->bad_block_ptr >= bb->num) {
344*6a54128fSAndroid Build Coastguard Worker scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
345*6a54128fSAndroid Build Coastguard Worker return 0;
346*6a54128fSAndroid Build Coastguard Worker }
347*6a54128fSAndroid Build Coastguard Worker
348*6a54128fSAndroid Build Coastguard Worker /*
349*6a54128fSAndroid Build Coastguard Worker * If the current block is greater than the bad block listed
350*6a54128fSAndroid Build Coastguard Worker * in the bad block list, then advance the pointer until this
351*6a54128fSAndroid Build Coastguard Worker * is no longer the case. If we run out of bad blocks, then
352*6a54128fSAndroid Build Coastguard Worker * we don't need to do any more checking!
353*6a54128fSAndroid Build Coastguard Worker */
354*6a54128fSAndroid Build Coastguard Worker while (blk > bb->list[scan->bad_block_ptr]) {
355*6a54128fSAndroid Build Coastguard Worker if (++scan->bad_block_ptr >= bb->num) {
356*6a54128fSAndroid Build Coastguard Worker scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
357*6a54128fSAndroid Build Coastguard Worker return 0;
358*6a54128fSAndroid Build Coastguard Worker }
359*6a54128fSAndroid Build Coastguard Worker }
360*6a54128fSAndroid Build Coastguard Worker
361*6a54128fSAndroid Build Coastguard Worker /*
362*6a54128fSAndroid Build Coastguard Worker * If the current block is equal to the bad block listed in
363*6a54128fSAndroid Build Coastguard Worker * the bad block list, then handle that one block specially.
364*6a54128fSAndroid Build Coastguard Worker * (We could try to handle runs of bad blocks, but that
365*6a54128fSAndroid Build Coastguard Worker * only increases CPU efficiency by a small amount, at the
366*6a54128fSAndroid Build Coastguard Worker * expense of a huge expense of code complexity, and for an
367*6a54128fSAndroid Build Coastguard Worker * uncommon case at that.)
368*6a54128fSAndroid Build Coastguard Worker */
369*6a54128fSAndroid Build Coastguard Worker if (blk == bb->list[scan->bad_block_ptr]) {
370*6a54128fSAndroid Build Coastguard Worker scan->scan_flags |= EXT2_SF_BAD_INODE_BLK;
371*6a54128fSAndroid Build Coastguard Worker *num_blocks = 1;
372*6a54128fSAndroid Build Coastguard Worker if (++scan->bad_block_ptr >= bb->num)
373*6a54128fSAndroid Build Coastguard Worker scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
374*6a54128fSAndroid Build Coastguard Worker return 0;
375*6a54128fSAndroid Build Coastguard Worker }
376*6a54128fSAndroid Build Coastguard Worker
377*6a54128fSAndroid Build Coastguard Worker /*
378*6a54128fSAndroid Build Coastguard Worker * If there is a bad block in the range that we're about to
379*6a54128fSAndroid Build Coastguard Worker * read in, adjust the number of blocks to read so that we we
380*6a54128fSAndroid Build Coastguard Worker * don't read in the bad block. (Then the next block to read
381*6a54128fSAndroid Build Coastguard Worker * will be the bad block, which is handled in the above case.)
382*6a54128fSAndroid Build Coastguard Worker */
383*6a54128fSAndroid Build Coastguard Worker if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr])
384*6a54128fSAndroid Build Coastguard Worker *num_blocks = (int) (bb->list[scan->bad_block_ptr] - blk);
385*6a54128fSAndroid Build Coastguard Worker
386*6a54128fSAndroid Build Coastguard Worker return 0;
387*6a54128fSAndroid Build Coastguard Worker }
388*6a54128fSAndroid Build Coastguard Worker
block_map_looks_insane(ext2_filsys fs,struct ext2_inode_large * inode)389*6a54128fSAndroid Build Coastguard Worker static int block_map_looks_insane(ext2_filsys fs,
390*6a54128fSAndroid Build Coastguard Worker struct ext2_inode_large *inode)
391*6a54128fSAndroid Build Coastguard Worker {
392*6a54128fSAndroid Build Coastguard Worker unsigned int i, bad;
393*6a54128fSAndroid Build Coastguard Worker
394*6a54128fSAndroid Build Coastguard Worker /* We're only interested in block mapped files, dirs, and symlinks */
395*6a54128fSAndroid Build Coastguard Worker if ((inode->i_flags & EXT4_INLINE_DATA_FL) ||
396*6a54128fSAndroid Build Coastguard Worker (inode->i_flags & EXT4_EXTENTS_FL))
397*6a54128fSAndroid Build Coastguard Worker return 0;
398*6a54128fSAndroid Build Coastguard Worker if (!LINUX_S_ISREG(inode->i_mode) &&
399*6a54128fSAndroid Build Coastguard Worker !LINUX_S_ISLNK(inode->i_mode) &&
400*6a54128fSAndroid Build Coastguard Worker !LINUX_S_ISDIR(inode->i_mode))
401*6a54128fSAndroid Build Coastguard Worker return 0;
402*6a54128fSAndroid Build Coastguard Worker if (LINUX_S_ISLNK(inode->i_mode) &&
403*6a54128fSAndroid Build Coastguard Worker EXT2_I_SIZE(inode) <= sizeof(inode->i_block))
404*6a54128fSAndroid Build Coastguard Worker return 0;
405*6a54128fSAndroid Build Coastguard Worker
406*6a54128fSAndroid Build Coastguard Worker /* Unused inodes probably aren't insane */
407*6a54128fSAndroid Build Coastguard Worker if (inode->i_links_count == 0)
408*6a54128fSAndroid Build Coastguard Worker return 0;
409*6a54128fSAndroid Build Coastguard Worker
410*6a54128fSAndroid Build Coastguard Worker /* See if more than half the block maps are insane */
411*6a54128fSAndroid Build Coastguard Worker for (i = 0, bad = 0; i < EXT2_N_BLOCKS; i++)
412*6a54128fSAndroid Build Coastguard Worker if (inode->i_block[i] != 0 &&
413*6a54128fSAndroid Build Coastguard Worker (inode->i_block[i] < fs->super->s_first_data_block ||
414*6a54128fSAndroid Build Coastguard Worker inode->i_block[i] >= ext2fs_blocks_count(fs->super)))
415*6a54128fSAndroid Build Coastguard Worker bad++;
416*6a54128fSAndroid Build Coastguard Worker return bad > EXT2_N_BLOCKS / 2;
417*6a54128fSAndroid Build Coastguard Worker }
418*6a54128fSAndroid Build Coastguard Worker
extent_head_looks_insane(struct ext2_inode_large * inode)419*6a54128fSAndroid Build Coastguard Worker static int extent_head_looks_insane(struct ext2_inode_large *inode)
420*6a54128fSAndroid Build Coastguard Worker {
421*6a54128fSAndroid Build Coastguard Worker if (!(inode->i_flags & EXT4_EXTENTS_FL) ||
422*6a54128fSAndroid Build Coastguard Worker ext2fs_extent_header_verify(inode->i_block,
423*6a54128fSAndroid Build Coastguard Worker sizeof(inode->i_block)) == 0)
424*6a54128fSAndroid Build Coastguard Worker return 0;
425*6a54128fSAndroid Build Coastguard Worker return 1;
426*6a54128fSAndroid Build Coastguard Worker }
427*6a54128fSAndroid Build Coastguard Worker
428*6a54128fSAndroid Build Coastguard Worker /*
429*6a54128fSAndroid Build Coastguard Worker * Check all the inodes that we just read into the buffer. Record what we
430*6a54128fSAndroid Build Coastguard Worker * find here -- currently, we can observe that all checksums are ok; more
431*6a54128fSAndroid Build Coastguard Worker * than half the inodes are insane; or no conclusions at all.
432*6a54128fSAndroid Build Coastguard Worker */
check_inode_block_sanity(ext2_inode_scan scan,blk64_t num_blocks)433*6a54128fSAndroid Build Coastguard Worker static void check_inode_block_sanity(ext2_inode_scan scan, blk64_t num_blocks)
434*6a54128fSAndroid Build Coastguard Worker {
435*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino, inodes_to_scan;
436*6a54128fSAndroid Build Coastguard Worker unsigned int badness, checksum_failures;
437*6a54128fSAndroid Build Coastguard Worker unsigned int inodes_in_buf, inodes_per_block;
438*6a54128fSAndroid Build Coastguard Worker char *p;
439*6a54128fSAndroid Build Coastguard Worker struct ext2_inode_large *inode;
440*6a54128fSAndroid Build Coastguard Worker char *block_status;
441*6a54128fSAndroid Build Coastguard Worker unsigned int blk, bad_csum;
442*6a54128fSAndroid Build Coastguard Worker
443*6a54128fSAndroid Build Coastguard Worker if (!(scan->scan_flags & EXT2_SF_WARN_GARBAGE_INODES))
444*6a54128fSAndroid Build Coastguard Worker return;
445*6a54128fSAndroid Build Coastguard Worker
446*6a54128fSAndroid Build Coastguard Worker inodes_to_scan = scan->inodes_left;
447*6a54128fSAndroid Build Coastguard Worker inodes_in_buf = num_blocks * scan->fs->blocksize / scan->inode_size;
448*6a54128fSAndroid Build Coastguard Worker if (inodes_to_scan > inodes_in_buf)
449*6a54128fSAndroid Build Coastguard Worker inodes_to_scan = inodes_in_buf;
450*6a54128fSAndroid Build Coastguard Worker
451*6a54128fSAndroid Build Coastguard Worker p = (char *) scan->inode_buffer;
452*6a54128fSAndroid Build Coastguard Worker ino = scan->current_inode + 1;
453*6a54128fSAndroid Build Coastguard Worker checksum_failures = badness = 0;
454*6a54128fSAndroid Build Coastguard Worker block_status = SCAN_BLOCK_STATUS(scan);
455*6a54128fSAndroid Build Coastguard Worker memset(block_status, 0, scan->inode_buffer_blocks);
456*6a54128fSAndroid Build Coastguard Worker inodes_per_block = EXT2_INODES_PER_BLOCK(scan->fs->super);
457*6a54128fSAndroid Build Coastguard Worker
458*6a54128fSAndroid Build Coastguard Worker if (inodes_per_block < 2)
459*6a54128fSAndroid Build Coastguard Worker return;
460*6a54128fSAndroid Build Coastguard Worker
461*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
462*6a54128fSAndroid Build Coastguard Worker if (ext2fs_get_mem(EXT2_INODE_SIZE(scan->fs->super), &inode))
463*6a54128fSAndroid Build Coastguard Worker return;
464*6a54128fSAndroid Build Coastguard Worker #endif
465*6a54128fSAndroid Build Coastguard Worker
466*6a54128fSAndroid Build Coastguard Worker while (inodes_to_scan > 0) {
467*6a54128fSAndroid Build Coastguard Worker blk = (p - (char *)scan->inode_buffer) / scan->fs->blocksize;
468*6a54128fSAndroid Build Coastguard Worker bad_csum = ext2fs_inode_csum_verify(scan->fs, ino,
469*6a54128fSAndroid Build Coastguard Worker (struct ext2_inode_large *) p) == 0;
470*6a54128fSAndroid Build Coastguard Worker
471*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
472*6a54128fSAndroid Build Coastguard Worker ext2fs_swap_inode_full(scan->fs,
473*6a54128fSAndroid Build Coastguard Worker (struct ext2_inode_large *) inode,
474*6a54128fSAndroid Build Coastguard Worker (struct ext2_inode_large *) p,
475*6a54128fSAndroid Build Coastguard Worker 0, EXT2_INODE_SIZE(scan->fs->super));
476*6a54128fSAndroid Build Coastguard Worker #else
477*6a54128fSAndroid Build Coastguard Worker inode = (struct ext2_inode_large *) p;
478*6a54128fSAndroid Build Coastguard Worker #endif
479*6a54128fSAndroid Build Coastguard Worker
480*6a54128fSAndroid Build Coastguard Worker /* Is this inode insane? */
481*6a54128fSAndroid Build Coastguard Worker if (bad_csum) {
482*6a54128fSAndroid Build Coastguard Worker checksum_failures++;
483*6a54128fSAndroid Build Coastguard Worker badness++;
484*6a54128fSAndroid Build Coastguard Worker } else if (extent_head_looks_insane(inode) ||
485*6a54128fSAndroid Build Coastguard Worker block_map_looks_insane(scan->fs, inode))
486*6a54128fSAndroid Build Coastguard Worker badness++;
487*6a54128fSAndroid Build Coastguard Worker
488*6a54128fSAndroid Build Coastguard Worker /* If more than half are insane, declare the whole block bad */
489*6a54128fSAndroid Build Coastguard Worker if (badness > inodes_per_block / 2) {
490*6a54128fSAndroid Build Coastguard Worker unsigned int ino_adj;
491*6a54128fSAndroid Build Coastguard Worker
492*6a54128fSAndroid Build Coastguard Worker block_status[blk] |= IBLOCK_STATUS_INSANE;
493*6a54128fSAndroid Build Coastguard Worker ino_adj = inodes_per_block -
494*6a54128fSAndroid Build Coastguard Worker ((ino - 1) % inodes_per_block);
495*6a54128fSAndroid Build Coastguard Worker if (ino_adj > inodes_to_scan)
496*6a54128fSAndroid Build Coastguard Worker ino_adj = inodes_to_scan;
497*6a54128fSAndroid Build Coastguard Worker inodes_to_scan -= ino_adj;
498*6a54128fSAndroid Build Coastguard Worker p += scan->inode_size * ino_adj;
499*6a54128fSAndroid Build Coastguard Worker ino += ino_adj;
500*6a54128fSAndroid Build Coastguard Worker checksum_failures = badness = 0;
501*6a54128fSAndroid Build Coastguard Worker continue;
502*6a54128fSAndroid Build Coastguard Worker }
503*6a54128fSAndroid Build Coastguard Worker
504*6a54128fSAndroid Build Coastguard Worker if ((ino % inodes_per_block) == 0) {
505*6a54128fSAndroid Build Coastguard Worker if (checksum_failures == 0)
506*6a54128fSAndroid Build Coastguard Worker block_status[blk] |= IBLOCK_STATUS_CSUMS_OK;
507*6a54128fSAndroid Build Coastguard Worker checksum_failures = badness = 0;
508*6a54128fSAndroid Build Coastguard Worker }
509*6a54128fSAndroid Build Coastguard Worker inodes_to_scan--;
510*6a54128fSAndroid Build Coastguard Worker p += scan->inode_size;
511*6a54128fSAndroid Build Coastguard Worker ino++;
512*6a54128fSAndroid Build Coastguard Worker };
513*6a54128fSAndroid Build Coastguard Worker
514*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
515*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&inode);
516*6a54128fSAndroid Build Coastguard Worker #endif
517*6a54128fSAndroid Build Coastguard Worker }
518*6a54128fSAndroid Build Coastguard Worker
519*6a54128fSAndroid Build Coastguard Worker /*
520*6a54128fSAndroid Build Coastguard Worker * This function is called by ext2fs_get_next_inode when it needs to
521*6a54128fSAndroid Build Coastguard Worker * read in more blocks from the current blockgroup's inode table.
522*6a54128fSAndroid Build Coastguard Worker */
get_next_blocks(ext2_inode_scan scan)523*6a54128fSAndroid Build Coastguard Worker static errcode_t get_next_blocks(ext2_inode_scan scan)
524*6a54128fSAndroid Build Coastguard Worker {
525*6a54128fSAndroid Build Coastguard Worker blk64_t num_blocks;
526*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
527*6a54128fSAndroid Build Coastguard Worker
528*6a54128fSAndroid Build Coastguard Worker /*
529*6a54128fSAndroid Build Coastguard Worker * Figure out how many blocks to read; we read at most
530*6a54128fSAndroid Build Coastguard Worker * inode_buffer_blocks, and perhaps less if there aren't that
531*6a54128fSAndroid Build Coastguard Worker * many blocks left to read.
532*6a54128fSAndroid Build Coastguard Worker */
533*6a54128fSAndroid Build Coastguard Worker num_blocks = scan->inode_buffer_blocks;
534*6a54128fSAndroid Build Coastguard Worker if (num_blocks > scan->blocks_left)
535*6a54128fSAndroid Build Coastguard Worker num_blocks = scan->blocks_left;
536*6a54128fSAndroid Build Coastguard Worker
537*6a54128fSAndroid Build Coastguard Worker /*
538*6a54128fSAndroid Build Coastguard Worker * If the past block "read" was a bad block, then mark the
539*6a54128fSAndroid Build Coastguard Worker * left-over extra bytes as also being bad.
540*6a54128fSAndroid Build Coastguard Worker */
541*6a54128fSAndroid Build Coastguard Worker if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) {
542*6a54128fSAndroid Build Coastguard Worker if (scan->bytes_left)
543*6a54128fSAndroid Build Coastguard Worker scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES;
544*6a54128fSAndroid Build Coastguard Worker scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK;
545*6a54128fSAndroid Build Coastguard Worker }
546*6a54128fSAndroid Build Coastguard Worker
547*6a54128fSAndroid Build Coastguard Worker /*
548*6a54128fSAndroid Build Coastguard Worker * Do inode bad block processing, if necessary.
549*6a54128fSAndroid Build Coastguard Worker */
550*6a54128fSAndroid Build Coastguard Worker if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) {
551*6a54128fSAndroid Build Coastguard Worker retval = check_for_inode_bad_blocks(scan, &num_blocks);
552*6a54128fSAndroid Build Coastguard Worker if (retval)
553*6a54128fSAndroid Build Coastguard Worker return retval;
554*6a54128fSAndroid Build Coastguard Worker }
555*6a54128fSAndroid Build Coastguard Worker
556*6a54128fSAndroid Build Coastguard Worker if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) ||
557*6a54128fSAndroid Build Coastguard Worker (scan->current_block == 0)) {
558*6a54128fSAndroid Build Coastguard Worker memset(scan->inode_buffer, 0,
559*6a54128fSAndroid Build Coastguard Worker (size_t) num_blocks * scan->fs->blocksize);
560*6a54128fSAndroid Build Coastguard Worker } else {
561*6a54128fSAndroid Build Coastguard Worker retval = io_channel_read_blk64(scan->fs->io,
562*6a54128fSAndroid Build Coastguard Worker scan->current_block,
563*6a54128fSAndroid Build Coastguard Worker (int) num_blocks,
564*6a54128fSAndroid Build Coastguard Worker scan->inode_buffer);
565*6a54128fSAndroid Build Coastguard Worker if (retval)
566*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NEXT_INODE_READ;
567*6a54128fSAndroid Build Coastguard Worker }
568*6a54128fSAndroid Build Coastguard Worker check_inode_block_sanity(scan, num_blocks);
569*6a54128fSAndroid Build Coastguard Worker
570*6a54128fSAndroid Build Coastguard Worker scan->ptr = scan->inode_buffer;
571*6a54128fSAndroid Build Coastguard Worker scan->bytes_left = num_blocks * scan->fs->blocksize;
572*6a54128fSAndroid Build Coastguard Worker
573*6a54128fSAndroid Build Coastguard Worker scan->blocks_left -= num_blocks;
574*6a54128fSAndroid Build Coastguard Worker if (scan->current_block)
575*6a54128fSAndroid Build Coastguard Worker scan->current_block += num_blocks;
576*6a54128fSAndroid Build Coastguard Worker
577*6a54128fSAndroid Build Coastguard Worker return 0;
578*6a54128fSAndroid Build Coastguard Worker }
579*6a54128fSAndroid Build Coastguard Worker
580*6a54128fSAndroid Build Coastguard Worker #if 0
581*6a54128fSAndroid Build Coastguard Worker /*
582*6a54128fSAndroid Build Coastguard Worker * Returns 1 if the entire inode_buffer has a non-zero size and
583*6a54128fSAndroid Build Coastguard Worker * contains all zeros. (Not just deleted inodes, since that means
584*6a54128fSAndroid Build Coastguard Worker * that part of the inode table was used at one point; we want all
585*6a54128fSAndroid Build Coastguard Worker * zeros, which means that the inode table is pristine.)
586*6a54128fSAndroid Build Coastguard Worker */
587*6a54128fSAndroid Build Coastguard Worker static inline int is_empty_scan(ext2_inode_scan scan)
588*6a54128fSAndroid Build Coastguard Worker {
589*6a54128fSAndroid Build Coastguard Worker int i;
590*6a54128fSAndroid Build Coastguard Worker
591*6a54128fSAndroid Build Coastguard Worker if (scan->bytes_left == 0)
592*6a54128fSAndroid Build Coastguard Worker return 0;
593*6a54128fSAndroid Build Coastguard Worker
594*6a54128fSAndroid Build Coastguard Worker for (i=0; i < scan->bytes_left; i++)
595*6a54128fSAndroid Build Coastguard Worker if (scan->ptr[i])
596*6a54128fSAndroid Build Coastguard Worker return 0;
597*6a54128fSAndroid Build Coastguard Worker return 1;
598*6a54128fSAndroid Build Coastguard Worker }
599*6a54128fSAndroid Build Coastguard Worker #endif
600*6a54128fSAndroid Build Coastguard Worker
ext2fs_get_next_inode_full(ext2_inode_scan scan,ext2_ino_t * ino,struct ext2_inode * inode,int bufsize)601*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan, ext2_ino_t *ino,
602*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode, int bufsize)
603*6a54128fSAndroid Build Coastguard Worker {
604*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
605*6a54128fSAndroid Build Coastguard Worker int extra_bytes = 0;
606*6a54128fSAndroid Build Coastguard Worker int length;
607*6a54128fSAndroid Build Coastguard Worker struct ext2_inode_large *iptr = (struct ext2_inode_large *)inode;
608*6a54128fSAndroid Build Coastguard Worker char *iblock_status;
609*6a54128fSAndroid Build Coastguard Worker unsigned int iblk;
610*6a54128fSAndroid Build Coastguard Worker
611*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN);
612*6a54128fSAndroid Build Coastguard Worker length = EXT2_INODE_SIZE(scan->fs->super);
613*6a54128fSAndroid Build Coastguard Worker iblock_status = SCAN_BLOCK_STATUS(scan);
614*6a54128fSAndroid Build Coastguard Worker
615*6a54128fSAndroid Build Coastguard Worker /*
616*6a54128fSAndroid Build Coastguard Worker * Do we need to start reading a new block group?
617*6a54128fSAndroid Build Coastguard Worker */
618*6a54128fSAndroid Build Coastguard Worker if (scan->inodes_left <= 0) {
619*6a54128fSAndroid Build Coastguard Worker force_new_group:
620*6a54128fSAndroid Build Coastguard Worker if (scan->done_group) {
621*6a54128fSAndroid Build Coastguard Worker retval = (scan->done_group)
622*6a54128fSAndroid Build Coastguard Worker (scan->fs, scan, scan->current_group,
623*6a54128fSAndroid Build Coastguard Worker scan->done_group_data);
624*6a54128fSAndroid Build Coastguard Worker if (retval)
625*6a54128fSAndroid Build Coastguard Worker return retval;
626*6a54128fSAndroid Build Coastguard Worker }
627*6a54128fSAndroid Build Coastguard Worker if (scan->groups_left <= 0) {
628*6a54128fSAndroid Build Coastguard Worker *ino = 0;
629*6a54128fSAndroid Build Coastguard Worker return 0;
630*6a54128fSAndroid Build Coastguard Worker }
631*6a54128fSAndroid Build Coastguard Worker retval = get_next_blockgroup(scan);
632*6a54128fSAndroid Build Coastguard Worker if (retval)
633*6a54128fSAndroid Build Coastguard Worker return retval;
634*6a54128fSAndroid Build Coastguard Worker }
635*6a54128fSAndroid Build Coastguard Worker /*
636*6a54128fSAndroid Build Coastguard Worker * These checks are done outside the above if statement so
637*6a54128fSAndroid Build Coastguard Worker * they can be done for block group #0.
638*6a54128fSAndroid Build Coastguard Worker */
639*6a54128fSAndroid Build Coastguard Worker if ((scan->scan_flags & EXT2_SF_DO_LAZY) &&
640*6a54128fSAndroid Build Coastguard Worker (ext2fs_bg_flags_test(scan->fs, scan->current_group, EXT2_BG_INODE_UNINIT)
641*6a54128fSAndroid Build Coastguard Worker ))
642*6a54128fSAndroid Build Coastguard Worker goto force_new_group;
643*6a54128fSAndroid Build Coastguard Worker if (scan->inodes_left == 0)
644*6a54128fSAndroid Build Coastguard Worker goto force_new_group;
645*6a54128fSAndroid Build Coastguard Worker if (scan->current_block == 0) {
646*6a54128fSAndroid Build Coastguard Worker if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) {
647*6a54128fSAndroid Build Coastguard Worker goto force_new_group;
648*6a54128fSAndroid Build Coastguard Worker } else
649*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_MISSING_INODE_TABLE;
650*6a54128fSAndroid Build Coastguard Worker }
651*6a54128fSAndroid Build Coastguard Worker
652*6a54128fSAndroid Build Coastguard Worker
653*6a54128fSAndroid Build Coastguard Worker /*
654*6a54128fSAndroid Build Coastguard Worker * Have we run out of space in the inode buffer? If so, we
655*6a54128fSAndroid Build Coastguard Worker * need to read in more blocks.
656*6a54128fSAndroid Build Coastguard Worker */
657*6a54128fSAndroid Build Coastguard Worker if (scan->bytes_left < scan->inode_size) {
658*6a54128fSAndroid Build Coastguard Worker if (scan->bytes_left)
659*6a54128fSAndroid Build Coastguard Worker memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left);
660*6a54128fSAndroid Build Coastguard Worker extra_bytes = scan->bytes_left;
661*6a54128fSAndroid Build Coastguard Worker
662*6a54128fSAndroid Build Coastguard Worker retval = get_next_blocks(scan);
663*6a54128fSAndroid Build Coastguard Worker if (retval)
664*6a54128fSAndroid Build Coastguard Worker return retval;
665*6a54128fSAndroid Build Coastguard Worker #if 0
666*6a54128fSAndroid Build Coastguard Worker /*
667*6a54128fSAndroid Build Coastguard Worker * XXX test Need check for used inode somehow.
668*6a54128fSAndroid Build Coastguard Worker * (Note: this is hard.)
669*6a54128fSAndroid Build Coastguard Worker */
670*6a54128fSAndroid Build Coastguard Worker if (is_empty_scan(scan))
671*6a54128fSAndroid Build Coastguard Worker goto force_new_group;
672*6a54128fSAndroid Build Coastguard Worker #endif
673*6a54128fSAndroid Build Coastguard Worker }
674*6a54128fSAndroid Build Coastguard Worker
675*6a54128fSAndroid Build Coastguard Worker if (bufsize < length) {
676*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_mem(length, &iptr);
677*6a54128fSAndroid Build Coastguard Worker if (retval)
678*6a54128fSAndroid Build Coastguard Worker return retval;
679*6a54128fSAndroid Build Coastguard Worker }
680*6a54128fSAndroid Build Coastguard Worker
681*6a54128fSAndroid Build Coastguard Worker retval = 0;
682*6a54128fSAndroid Build Coastguard Worker iblk = scan->current_inode % EXT2_INODES_PER_GROUP(scan->fs->super) /
683*6a54128fSAndroid Build Coastguard Worker EXT2_INODES_PER_BLOCK(scan->fs->super) %
684*6a54128fSAndroid Build Coastguard Worker scan->inode_buffer_blocks;
685*6a54128fSAndroid Build Coastguard Worker if (extra_bytes) {
686*6a54128fSAndroid Build Coastguard Worker memcpy(scan->temp_buffer+extra_bytes, scan->ptr,
687*6a54128fSAndroid Build Coastguard Worker scan->inode_size - extra_bytes);
688*6a54128fSAndroid Build Coastguard Worker scan->ptr += scan->inode_size - extra_bytes;
689*6a54128fSAndroid Build Coastguard Worker scan->bytes_left -= scan->inode_size - extra_bytes;
690*6a54128fSAndroid Build Coastguard Worker
691*6a54128fSAndroid Build Coastguard Worker /* Verify the inode checksum. */
692*6a54128fSAndroid Build Coastguard Worker if (!(iblock_status[iblk] & IBLOCK_STATUS_CSUMS_OK) &&
693*6a54128fSAndroid Build Coastguard Worker !(scan->fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
694*6a54128fSAndroid Build Coastguard Worker !ext2fs_inode_csum_verify(scan->fs, scan->current_inode + 1,
695*6a54128fSAndroid Build Coastguard Worker (struct ext2_inode_large *)scan->temp_buffer))
696*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_INODE_CSUM_INVALID;
697*6a54128fSAndroid Build Coastguard Worker
698*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
699*6a54128fSAndroid Build Coastguard Worker memset(iptr, 0, length);
700*6a54128fSAndroid Build Coastguard Worker ext2fs_swap_inode_full(scan->fs,
701*6a54128fSAndroid Build Coastguard Worker (struct ext2_inode_large *) iptr,
702*6a54128fSAndroid Build Coastguard Worker (struct ext2_inode_large *) scan->temp_buffer,
703*6a54128fSAndroid Build Coastguard Worker 0, length);
704*6a54128fSAndroid Build Coastguard Worker #else
705*6a54128fSAndroid Build Coastguard Worker memcpy(iptr, scan->temp_buffer, length);
706*6a54128fSAndroid Build Coastguard Worker #endif
707*6a54128fSAndroid Build Coastguard Worker if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES)
708*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
709*6a54128fSAndroid Build Coastguard Worker scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES;
710*6a54128fSAndroid Build Coastguard Worker } else {
711*6a54128fSAndroid Build Coastguard Worker /* Verify the inode checksum. */
712*6a54128fSAndroid Build Coastguard Worker if (!(iblock_status[iblk] & IBLOCK_STATUS_CSUMS_OK) &&
713*6a54128fSAndroid Build Coastguard Worker !(scan->fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
714*6a54128fSAndroid Build Coastguard Worker !ext2fs_inode_csum_verify(scan->fs, scan->current_inode + 1,
715*6a54128fSAndroid Build Coastguard Worker (struct ext2_inode_large *)scan->ptr))
716*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_INODE_CSUM_INVALID;
717*6a54128fSAndroid Build Coastguard Worker
718*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
719*6a54128fSAndroid Build Coastguard Worker memset(iptr, 0, length);
720*6a54128fSAndroid Build Coastguard Worker ext2fs_swap_inode_full(scan->fs,
721*6a54128fSAndroid Build Coastguard Worker (struct ext2_inode_large *) iptr,
722*6a54128fSAndroid Build Coastguard Worker (struct ext2_inode_large *) scan->ptr,
723*6a54128fSAndroid Build Coastguard Worker 0, length);
724*6a54128fSAndroid Build Coastguard Worker #else
725*6a54128fSAndroid Build Coastguard Worker memcpy(iptr, scan->ptr, length);
726*6a54128fSAndroid Build Coastguard Worker #endif
727*6a54128fSAndroid Build Coastguard Worker scan->ptr += scan->inode_size;
728*6a54128fSAndroid Build Coastguard Worker scan->bytes_left -= scan->inode_size;
729*6a54128fSAndroid Build Coastguard Worker if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK)
730*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
731*6a54128fSAndroid Build Coastguard Worker }
732*6a54128fSAndroid Build Coastguard Worker if ((iblock_status[iblk] & IBLOCK_STATUS_INSANE) &&
733*6a54128fSAndroid Build Coastguard Worker (retval == 0 || retval == EXT2_ET_INODE_CSUM_INVALID))
734*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_INODE_IS_GARBAGE;
735*6a54128fSAndroid Build Coastguard Worker
736*6a54128fSAndroid Build Coastguard Worker scan->inodes_left--;
737*6a54128fSAndroid Build Coastguard Worker scan->current_inode++;
738*6a54128fSAndroid Build Coastguard Worker *ino = scan->current_inode;
739*6a54128fSAndroid Build Coastguard Worker if (iptr != (struct ext2_inode_large *)inode) {
740*6a54128fSAndroid Build Coastguard Worker memcpy(inode, iptr, bufsize);
741*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&iptr);
742*6a54128fSAndroid Build Coastguard Worker }
743*6a54128fSAndroid Build Coastguard Worker return retval;
744*6a54128fSAndroid Build Coastguard Worker }
745*6a54128fSAndroid Build Coastguard Worker
ext2fs_get_next_inode(ext2_inode_scan scan,ext2_ino_t * ino,struct ext2_inode * inode)746*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino,
747*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode)
748*6a54128fSAndroid Build Coastguard Worker {
749*6a54128fSAndroid Build Coastguard Worker return ext2fs_get_next_inode_full(scan, ino, inode,
750*6a54128fSAndroid Build Coastguard Worker sizeof(struct ext2_inode));
751*6a54128fSAndroid Build Coastguard Worker }
752*6a54128fSAndroid Build Coastguard Worker
753*6a54128fSAndroid Build Coastguard Worker /*
754*6a54128fSAndroid Build Coastguard Worker * Functions to read and write a single inode.
755*6a54128fSAndroid Build Coastguard Worker */
ext2fs_read_inode2(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,int bufsize,int flags)756*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_read_inode2(ext2_filsys fs, ext2_ino_t ino,
757*6a54128fSAndroid Build Coastguard Worker struct ext2_inode * inode, int bufsize,
758*6a54128fSAndroid Build Coastguard Worker int flags)
759*6a54128fSAndroid Build Coastguard Worker {
760*6a54128fSAndroid Build Coastguard Worker blk64_t block_nr;
761*6a54128fSAndroid Build Coastguard Worker dgrp_t group;
762*6a54128fSAndroid Build Coastguard Worker unsigned long block, offset;
763*6a54128fSAndroid Build Coastguard Worker char *ptr;
764*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
765*6a54128fSAndroid Build Coastguard Worker unsigned i;
766*6a54128fSAndroid Build Coastguard Worker int clen, inodes_per_block;
767*6a54128fSAndroid Build Coastguard Worker io_channel io;
768*6a54128fSAndroid Build Coastguard Worker int length = EXT2_INODE_SIZE(fs->super);
769*6a54128fSAndroid Build Coastguard Worker struct ext2_inode_large *iptr;
770*6a54128fSAndroid Build Coastguard Worker int cache_slot, fail_csum;
771*6a54128fSAndroid Build Coastguard Worker
772*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
773*6a54128fSAndroid Build Coastguard Worker
774*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_feature_journal_dev(fs->super))
775*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTERNAL_JOURNAL_NOSUPP;
776*6a54128fSAndroid Build Coastguard Worker
777*6a54128fSAndroid Build Coastguard Worker if (fs->blocksize < 1024)
778*6a54128fSAndroid Build Coastguard Worker return EXT2_FILSYS_CORRUPTED; /* Should never happen */
779*6a54128fSAndroid Build Coastguard Worker
780*6a54128fSAndroid Build Coastguard Worker /* Check to see if user has an override function */
781*6a54128fSAndroid Build Coastguard Worker if (fs->read_inode &&
782*6a54128fSAndroid Build Coastguard Worker ((bufsize == sizeof(struct ext2_inode)) ||
783*6a54128fSAndroid Build Coastguard Worker (EXT2_INODE_SIZE(fs->super) == sizeof(struct ext2_inode)))) {
784*6a54128fSAndroid Build Coastguard Worker retval = (fs->read_inode)(fs, ino, inode);
785*6a54128fSAndroid Build Coastguard Worker if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
786*6a54128fSAndroid Build Coastguard Worker return retval;
787*6a54128fSAndroid Build Coastguard Worker }
788*6a54128fSAndroid Build Coastguard Worker if ((ino == 0) || (ino > fs->super->s_inodes_count))
789*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_BAD_INODE_NUM;
790*6a54128fSAndroid Build Coastguard Worker /* Create inode cache if not present */
791*6a54128fSAndroid Build Coastguard Worker if (!fs->icache) {
792*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_create_inode_cache(fs, 4);
793*6a54128fSAndroid Build Coastguard Worker if (retval)
794*6a54128fSAndroid Build Coastguard Worker return retval;
795*6a54128fSAndroid Build Coastguard Worker }
796*6a54128fSAndroid Build Coastguard Worker /* Check to see if it's in the inode cache */
797*6a54128fSAndroid Build Coastguard Worker for (i = 0; i < fs->icache->cache_size; i++) {
798*6a54128fSAndroid Build Coastguard Worker if (fs->icache->cache[i].ino == ino) {
799*6a54128fSAndroid Build Coastguard Worker memcpy(inode, fs->icache->cache[i].inode,
800*6a54128fSAndroid Build Coastguard Worker (bufsize > length) ? length : bufsize);
801*6a54128fSAndroid Build Coastguard Worker return 0;
802*6a54128fSAndroid Build Coastguard Worker }
803*6a54128fSAndroid Build Coastguard Worker }
804*6a54128fSAndroid Build Coastguard Worker if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
805*6a54128fSAndroid Build Coastguard Worker inodes_per_block = fs->blocksize / EXT2_INODE_SIZE(fs->super);
806*6a54128fSAndroid Build Coastguard Worker block_nr = ext2fs_le32_to_cpu(fs->image_header->offset_inode) / fs->blocksize;
807*6a54128fSAndroid Build Coastguard Worker block_nr += (ino - 1) / inodes_per_block;
808*6a54128fSAndroid Build Coastguard Worker offset = ((ino - 1) % inodes_per_block) *
809*6a54128fSAndroid Build Coastguard Worker EXT2_INODE_SIZE(fs->super);
810*6a54128fSAndroid Build Coastguard Worker io = fs->image_io;
811*6a54128fSAndroid Build Coastguard Worker } else {
812*6a54128fSAndroid Build Coastguard Worker group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
813*6a54128fSAndroid Build Coastguard Worker if (group > fs->group_desc_count)
814*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_BAD_INODE_NUM;
815*6a54128fSAndroid Build Coastguard Worker offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
816*6a54128fSAndroid Build Coastguard Worker EXT2_INODE_SIZE(fs->super);
817*6a54128fSAndroid Build Coastguard Worker block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
818*6a54128fSAndroid Build Coastguard Worker block_nr = ext2fs_inode_table_loc(fs, group);
819*6a54128fSAndroid Build Coastguard Worker if (!block_nr)
820*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_MISSING_INODE_TABLE;
821*6a54128fSAndroid Build Coastguard Worker if ((block_nr < fs->super->s_first_data_block) ||
822*6a54128fSAndroid Build Coastguard Worker (block_nr + fs->inode_blocks_per_group - 1 >=
823*6a54128fSAndroid Build Coastguard Worker ext2fs_blocks_count(fs->super)))
824*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_GDESC_BAD_INODE_TABLE;
825*6a54128fSAndroid Build Coastguard Worker block_nr += block;
826*6a54128fSAndroid Build Coastguard Worker io = fs->io;
827*6a54128fSAndroid Build Coastguard Worker }
828*6a54128fSAndroid Build Coastguard Worker offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
829*6a54128fSAndroid Build Coastguard Worker
830*6a54128fSAndroid Build Coastguard Worker cache_slot = (fs->icache->cache_last + 1) % fs->icache->cache_size;
831*6a54128fSAndroid Build Coastguard Worker iptr = (struct ext2_inode_large *)fs->icache->cache[cache_slot].inode;
832*6a54128fSAndroid Build Coastguard Worker
833*6a54128fSAndroid Build Coastguard Worker ptr = (char *) iptr;
834*6a54128fSAndroid Build Coastguard Worker while (length) {
835*6a54128fSAndroid Build Coastguard Worker clen = length;
836*6a54128fSAndroid Build Coastguard Worker if ((offset + length) > fs->blocksize)
837*6a54128fSAndroid Build Coastguard Worker clen = fs->blocksize - offset;
838*6a54128fSAndroid Build Coastguard Worker
839*6a54128fSAndroid Build Coastguard Worker if (block_nr != fs->icache->buffer_blk) {
840*6a54128fSAndroid Build Coastguard Worker retval = io_channel_read_blk64(io, block_nr, 1,
841*6a54128fSAndroid Build Coastguard Worker fs->icache->buffer);
842*6a54128fSAndroid Build Coastguard Worker if (retval)
843*6a54128fSAndroid Build Coastguard Worker return retval;
844*6a54128fSAndroid Build Coastguard Worker fs->icache->buffer_blk = block_nr;
845*6a54128fSAndroid Build Coastguard Worker }
846*6a54128fSAndroid Build Coastguard Worker
847*6a54128fSAndroid Build Coastguard Worker memcpy(ptr, ((char *) fs->icache->buffer) + (unsigned) offset,
848*6a54128fSAndroid Build Coastguard Worker clen);
849*6a54128fSAndroid Build Coastguard Worker
850*6a54128fSAndroid Build Coastguard Worker offset = 0;
851*6a54128fSAndroid Build Coastguard Worker length -= clen;
852*6a54128fSAndroid Build Coastguard Worker ptr += clen;
853*6a54128fSAndroid Build Coastguard Worker block_nr++;
854*6a54128fSAndroid Build Coastguard Worker }
855*6a54128fSAndroid Build Coastguard Worker length = EXT2_INODE_SIZE(fs->super);
856*6a54128fSAndroid Build Coastguard Worker
857*6a54128fSAndroid Build Coastguard Worker /* Verify the inode checksum. */
858*6a54128fSAndroid Build Coastguard Worker fail_csum = !ext2fs_inode_csum_verify(fs, ino, iptr);
859*6a54128fSAndroid Build Coastguard Worker
860*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
861*6a54128fSAndroid Build Coastguard Worker ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) iptr,
862*6a54128fSAndroid Build Coastguard Worker (struct ext2_inode_large *) iptr,
863*6a54128fSAndroid Build Coastguard Worker 0, length);
864*6a54128fSAndroid Build Coastguard Worker #endif
865*6a54128fSAndroid Build Coastguard Worker
866*6a54128fSAndroid Build Coastguard Worker /* Update the inode cache bookkeeping */
867*6a54128fSAndroid Build Coastguard Worker if (!fail_csum) {
868*6a54128fSAndroid Build Coastguard Worker fs->icache->cache_last = cache_slot;
869*6a54128fSAndroid Build Coastguard Worker fs->icache->cache[cache_slot].ino = ino;
870*6a54128fSAndroid Build Coastguard Worker }
871*6a54128fSAndroid Build Coastguard Worker memcpy(inode, iptr, (bufsize > length) ? length : bufsize);
872*6a54128fSAndroid Build Coastguard Worker
873*6a54128fSAndroid Build Coastguard Worker if (!(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
874*6a54128fSAndroid Build Coastguard Worker !(flags & READ_INODE_NOCSUM) && fail_csum)
875*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_INODE_CSUM_INVALID;
876*6a54128fSAndroid Build Coastguard Worker
877*6a54128fSAndroid Build Coastguard Worker return 0;
878*6a54128fSAndroid Build Coastguard Worker }
879*6a54128fSAndroid Build Coastguard Worker
ext2fs_read_inode_full(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,int bufsize)880*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino,
881*6a54128fSAndroid Build Coastguard Worker struct ext2_inode * inode, int bufsize)
882*6a54128fSAndroid Build Coastguard Worker {
883*6a54128fSAndroid Build Coastguard Worker return ext2fs_read_inode2(fs, ino, inode, bufsize, 0);
884*6a54128fSAndroid Build Coastguard Worker }
885*6a54128fSAndroid Build Coastguard Worker
ext2fs_read_inode(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode)886*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_read_inode(ext2_filsys fs, ext2_ino_t ino,
887*6a54128fSAndroid Build Coastguard Worker struct ext2_inode * inode)
888*6a54128fSAndroid Build Coastguard Worker {
889*6a54128fSAndroid Build Coastguard Worker return ext2fs_read_inode2(fs, ino, inode,
890*6a54128fSAndroid Build Coastguard Worker sizeof(struct ext2_inode), 0);
891*6a54128fSAndroid Build Coastguard Worker }
892*6a54128fSAndroid Build Coastguard Worker
ext2fs_write_inode2(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,int bufsize,int flags)893*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_write_inode2(ext2_filsys fs, ext2_ino_t ino,
894*6a54128fSAndroid Build Coastguard Worker struct ext2_inode * inode, int bufsize,
895*6a54128fSAndroid Build Coastguard Worker int flags)
896*6a54128fSAndroid Build Coastguard Worker {
897*6a54128fSAndroid Build Coastguard Worker blk64_t block_nr;
898*6a54128fSAndroid Build Coastguard Worker dgrp_t group;
899*6a54128fSAndroid Build Coastguard Worker unsigned long block, offset;
900*6a54128fSAndroid Build Coastguard Worker errcode_t retval = 0;
901*6a54128fSAndroid Build Coastguard Worker struct ext2_inode_large *w_inode;
902*6a54128fSAndroid Build Coastguard Worker char *ptr;
903*6a54128fSAndroid Build Coastguard Worker unsigned i;
904*6a54128fSAndroid Build Coastguard Worker int clen;
905*6a54128fSAndroid Build Coastguard Worker int length = EXT2_INODE_SIZE(fs->super);
906*6a54128fSAndroid Build Coastguard Worker
907*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
908*6a54128fSAndroid Build Coastguard Worker
909*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_feature_journal_dev(fs->super))
910*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTERNAL_JOURNAL_NOSUPP;
911*6a54128fSAndroid Build Coastguard Worker
912*6a54128fSAndroid Build Coastguard Worker /* Check to see if user provided an override function */
913*6a54128fSAndroid Build Coastguard Worker if (fs->write_inode) {
914*6a54128fSAndroid Build Coastguard Worker retval = (fs->write_inode)(fs, ino, inode);
915*6a54128fSAndroid Build Coastguard Worker if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
916*6a54128fSAndroid Build Coastguard Worker return retval;
917*6a54128fSAndroid Build Coastguard Worker }
918*6a54128fSAndroid Build Coastguard Worker
919*6a54128fSAndroid Build Coastguard Worker if ((ino == 0) || (ino > fs->super->s_inodes_count))
920*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_BAD_INODE_NUM;
921*6a54128fSAndroid Build Coastguard Worker
922*6a54128fSAndroid Build Coastguard Worker /* Prepare our shadow buffer for read/modify/byteswap/write */
923*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_mem(length, &w_inode);
924*6a54128fSAndroid Build Coastguard Worker if (retval)
925*6a54128fSAndroid Build Coastguard Worker return retval;
926*6a54128fSAndroid Build Coastguard Worker
927*6a54128fSAndroid Build Coastguard Worker if (bufsize < length) {
928*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_read_inode2(fs, ino,
929*6a54128fSAndroid Build Coastguard Worker (struct ext2_inode *)w_inode,
930*6a54128fSAndroid Build Coastguard Worker length, READ_INODE_NOCSUM);
931*6a54128fSAndroid Build Coastguard Worker if (retval)
932*6a54128fSAndroid Build Coastguard Worker goto errout;
933*6a54128fSAndroid Build Coastguard Worker }
934*6a54128fSAndroid Build Coastguard Worker
935*6a54128fSAndroid Build Coastguard Worker /* Check to see if the inode cache needs to be updated */
936*6a54128fSAndroid Build Coastguard Worker if (fs->icache) {
937*6a54128fSAndroid Build Coastguard Worker for (i=0; i < fs->icache->cache_size; i++) {
938*6a54128fSAndroid Build Coastguard Worker if (fs->icache->cache[i].ino == ino) {
939*6a54128fSAndroid Build Coastguard Worker memcpy(fs->icache->cache[i].inode, inode,
940*6a54128fSAndroid Build Coastguard Worker (bufsize > length) ? length : bufsize);
941*6a54128fSAndroid Build Coastguard Worker break;
942*6a54128fSAndroid Build Coastguard Worker }
943*6a54128fSAndroid Build Coastguard Worker }
944*6a54128fSAndroid Build Coastguard Worker } else {
945*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_create_inode_cache(fs, 4);
946*6a54128fSAndroid Build Coastguard Worker if (retval)
947*6a54128fSAndroid Build Coastguard Worker goto errout;
948*6a54128fSAndroid Build Coastguard Worker }
949*6a54128fSAndroid Build Coastguard Worker memcpy(w_inode, inode, (bufsize > length) ? length : bufsize);
950*6a54128fSAndroid Build Coastguard Worker
951*6a54128fSAndroid Build Coastguard Worker if (!(fs->flags & EXT2_FLAG_RW)) {
952*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_RO_FILSYS;
953*6a54128fSAndroid Build Coastguard Worker goto errout;
954*6a54128fSAndroid Build Coastguard Worker }
955*6a54128fSAndroid Build Coastguard Worker
956*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
957*6a54128fSAndroid Build Coastguard Worker ext2fs_swap_inode_full(fs, w_inode, w_inode, 1, length);
958*6a54128fSAndroid Build Coastguard Worker #endif
959*6a54128fSAndroid Build Coastguard Worker
960*6a54128fSAndroid Build Coastguard Worker if ((flags & WRITE_INODE_NOCSUM) == 0) {
961*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_inode_csum_set(fs, ino, w_inode);
962*6a54128fSAndroid Build Coastguard Worker if (retval)
963*6a54128fSAndroid Build Coastguard Worker goto errout;
964*6a54128fSAndroid Build Coastguard Worker }
965*6a54128fSAndroid Build Coastguard Worker
966*6a54128fSAndroid Build Coastguard Worker group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
967*6a54128fSAndroid Build Coastguard Worker offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
968*6a54128fSAndroid Build Coastguard Worker EXT2_INODE_SIZE(fs->super);
969*6a54128fSAndroid Build Coastguard Worker block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
970*6a54128fSAndroid Build Coastguard Worker block_nr = ext2fs_inode_table_loc(fs, (unsigned) group);
971*6a54128fSAndroid Build Coastguard Worker if (!block_nr) {
972*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_MISSING_INODE_TABLE;
973*6a54128fSAndroid Build Coastguard Worker goto errout;
974*6a54128fSAndroid Build Coastguard Worker }
975*6a54128fSAndroid Build Coastguard Worker if ((block_nr < fs->super->s_first_data_block) ||
976*6a54128fSAndroid Build Coastguard Worker (block_nr + fs->inode_blocks_per_group - 1 >=
977*6a54128fSAndroid Build Coastguard Worker ext2fs_blocks_count(fs->super))) {
978*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_GDESC_BAD_INODE_TABLE;
979*6a54128fSAndroid Build Coastguard Worker goto errout;
980*6a54128fSAndroid Build Coastguard Worker }
981*6a54128fSAndroid Build Coastguard Worker block_nr += block;
982*6a54128fSAndroid Build Coastguard Worker
983*6a54128fSAndroid Build Coastguard Worker offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
984*6a54128fSAndroid Build Coastguard Worker
985*6a54128fSAndroid Build Coastguard Worker ptr = (char *) w_inode;
986*6a54128fSAndroid Build Coastguard Worker
987*6a54128fSAndroid Build Coastguard Worker while (length) {
988*6a54128fSAndroid Build Coastguard Worker clen = length;
989*6a54128fSAndroid Build Coastguard Worker if ((offset + length) > fs->blocksize)
990*6a54128fSAndroid Build Coastguard Worker clen = fs->blocksize - offset;
991*6a54128fSAndroid Build Coastguard Worker
992*6a54128fSAndroid Build Coastguard Worker if (fs->icache->buffer_blk != block_nr) {
993*6a54128fSAndroid Build Coastguard Worker retval = io_channel_read_blk64(fs->io, block_nr, 1,
994*6a54128fSAndroid Build Coastguard Worker fs->icache->buffer);
995*6a54128fSAndroid Build Coastguard Worker if (retval)
996*6a54128fSAndroid Build Coastguard Worker goto errout;
997*6a54128fSAndroid Build Coastguard Worker fs->icache->buffer_blk = block_nr;
998*6a54128fSAndroid Build Coastguard Worker }
999*6a54128fSAndroid Build Coastguard Worker
1000*6a54128fSAndroid Build Coastguard Worker
1001*6a54128fSAndroid Build Coastguard Worker memcpy((char *) fs->icache->buffer + (unsigned) offset,
1002*6a54128fSAndroid Build Coastguard Worker ptr, clen);
1003*6a54128fSAndroid Build Coastguard Worker
1004*6a54128fSAndroid Build Coastguard Worker retval = io_channel_write_blk64(fs->io, block_nr, 1,
1005*6a54128fSAndroid Build Coastguard Worker fs->icache->buffer);
1006*6a54128fSAndroid Build Coastguard Worker if (retval)
1007*6a54128fSAndroid Build Coastguard Worker goto errout;
1008*6a54128fSAndroid Build Coastguard Worker
1009*6a54128fSAndroid Build Coastguard Worker offset = 0;
1010*6a54128fSAndroid Build Coastguard Worker ptr += clen;
1011*6a54128fSAndroid Build Coastguard Worker length -= clen;
1012*6a54128fSAndroid Build Coastguard Worker block_nr++;
1013*6a54128fSAndroid Build Coastguard Worker }
1014*6a54128fSAndroid Build Coastguard Worker
1015*6a54128fSAndroid Build Coastguard Worker fs->flags |= EXT2_FLAG_CHANGED;
1016*6a54128fSAndroid Build Coastguard Worker errout:
1017*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&w_inode);
1018*6a54128fSAndroid Build Coastguard Worker return retval;
1019*6a54128fSAndroid Build Coastguard Worker }
1020*6a54128fSAndroid Build Coastguard Worker
ext2fs_write_inode_full(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,int bufsize)1021*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
1022*6a54128fSAndroid Build Coastguard Worker struct ext2_inode * inode, int bufsize)
1023*6a54128fSAndroid Build Coastguard Worker {
1024*6a54128fSAndroid Build Coastguard Worker return ext2fs_write_inode2(fs, ino, inode, bufsize, 0);
1025*6a54128fSAndroid Build Coastguard Worker }
1026*6a54128fSAndroid Build Coastguard Worker
ext2fs_write_inode(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode)1027*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
1028*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode)
1029*6a54128fSAndroid Build Coastguard Worker {
1030*6a54128fSAndroid Build Coastguard Worker return ext2fs_write_inode2(fs, ino, inode,
1031*6a54128fSAndroid Build Coastguard Worker sizeof(struct ext2_inode), 0);
1032*6a54128fSAndroid Build Coastguard Worker }
1033*6a54128fSAndroid Build Coastguard Worker
1034*6a54128fSAndroid Build Coastguard Worker /*
1035*6a54128fSAndroid Build Coastguard Worker * This function should be called when writing a new inode. It makes
1036*6a54128fSAndroid Build Coastguard Worker * sure that extra part of large inodes is initialized properly.
1037*6a54128fSAndroid Build Coastguard Worker */
ext2fs_write_new_inode(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode)1038*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino,
1039*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode)
1040*6a54128fSAndroid Build Coastguard Worker {
1041*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *buf;
1042*6a54128fSAndroid Build Coastguard Worker int size = EXT2_INODE_SIZE(fs->super);
1043*6a54128fSAndroid Build Coastguard Worker struct ext2_inode_large *large_inode;
1044*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
1045*6a54128fSAndroid Build Coastguard Worker __u32 t = fs->now ? fs->now : time(NULL);
1046*6a54128fSAndroid Build Coastguard Worker
1047*6a54128fSAndroid Build Coastguard Worker if (!inode->i_ctime)
1048*6a54128fSAndroid Build Coastguard Worker inode->i_ctime = t;
1049*6a54128fSAndroid Build Coastguard Worker if (!inode->i_mtime)
1050*6a54128fSAndroid Build Coastguard Worker inode->i_mtime = t;
1051*6a54128fSAndroid Build Coastguard Worker if (!inode->i_atime)
1052*6a54128fSAndroid Build Coastguard Worker inode->i_atime = t;
1053*6a54128fSAndroid Build Coastguard Worker
1054*6a54128fSAndroid Build Coastguard Worker if (size == sizeof(struct ext2_inode))
1055*6a54128fSAndroid Build Coastguard Worker return ext2fs_write_inode_full(fs, ino, inode,
1056*6a54128fSAndroid Build Coastguard Worker sizeof(struct ext2_inode));
1057*6a54128fSAndroid Build Coastguard Worker
1058*6a54128fSAndroid Build Coastguard Worker buf = malloc(size);
1059*6a54128fSAndroid Build Coastguard Worker if (!buf)
1060*6a54128fSAndroid Build Coastguard Worker return ENOMEM;
1061*6a54128fSAndroid Build Coastguard Worker
1062*6a54128fSAndroid Build Coastguard Worker memset(buf, 0, size);
1063*6a54128fSAndroid Build Coastguard Worker *buf = *inode;
1064*6a54128fSAndroid Build Coastguard Worker
1065*6a54128fSAndroid Build Coastguard Worker large_inode = (struct ext2_inode_large *) buf;
1066*6a54128fSAndroid Build Coastguard Worker large_inode->i_extra_isize = sizeof(struct ext2_inode_large) -
1067*6a54128fSAndroid Build Coastguard Worker EXT2_GOOD_OLD_INODE_SIZE;
1068*6a54128fSAndroid Build Coastguard Worker if (!large_inode->i_crtime)
1069*6a54128fSAndroid Build Coastguard Worker large_inode->i_crtime = t;
1070*6a54128fSAndroid Build Coastguard Worker
1071*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_write_inode_full(fs, ino, buf, size);
1072*6a54128fSAndroid Build Coastguard Worker free(buf);
1073*6a54128fSAndroid Build Coastguard Worker return retval;
1074*6a54128fSAndroid Build Coastguard Worker }
1075*6a54128fSAndroid Build Coastguard Worker
1076*6a54128fSAndroid Build Coastguard Worker
ext2fs_get_blocks(ext2_filsys fs,ext2_ino_t ino,blk_t * blocks)1077*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks)
1078*6a54128fSAndroid Build Coastguard Worker {
1079*6a54128fSAndroid Build Coastguard Worker struct ext2_inode inode;
1080*6a54128fSAndroid Build Coastguard Worker int i;
1081*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
1082*6a54128fSAndroid Build Coastguard Worker
1083*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
1084*6a54128fSAndroid Build Coastguard Worker
1085*6a54128fSAndroid Build Coastguard Worker if (ino > fs->super->s_inodes_count)
1086*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_BAD_INODE_NUM;
1087*6a54128fSAndroid Build Coastguard Worker
1088*6a54128fSAndroid Build Coastguard Worker if (fs->get_blocks) {
1089*6a54128fSAndroid Build Coastguard Worker if (!(*fs->get_blocks)(fs, ino, blocks))
1090*6a54128fSAndroid Build Coastguard Worker return 0;
1091*6a54128fSAndroid Build Coastguard Worker }
1092*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_read_inode(fs, ino, &inode);
1093*6a54128fSAndroid Build Coastguard Worker if (retval)
1094*6a54128fSAndroid Build Coastguard Worker return retval;
1095*6a54128fSAndroid Build Coastguard Worker for (i=0; i < EXT2_N_BLOCKS; i++)
1096*6a54128fSAndroid Build Coastguard Worker blocks[i] = inode.i_block[i];
1097*6a54128fSAndroid Build Coastguard Worker return 0;
1098*6a54128fSAndroid Build Coastguard Worker }
1099*6a54128fSAndroid Build Coastguard Worker
ext2fs_check_directory(ext2_filsys fs,ext2_ino_t ino)1100*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino)
1101*6a54128fSAndroid Build Coastguard Worker {
1102*6a54128fSAndroid Build Coastguard Worker struct ext2_inode inode;
1103*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
1104*6a54128fSAndroid Build Coastguard Worker
1105*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
1106*6a54128fSAndroid Build Coastguard Worker
1107*6a54128fSAndroid Build Coastguard Worker if (ino > fs->super->s_inodes_count)
1108*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_BAD_INODE_NUM;
1109*6a54128fSAndroid Build Coastguard Worker
1110*6a54128fSAndroid Build Coastguard Worker if (fs->check_directory) {
1111*6a54128fSAndroid Build Coastguard Worker retval = (fs->check_directory)(fs, ino);
1112*6a54128fSAndroid Build Coastguard Worker if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
1113*6a54128fSAndroid Build Coastguard Worker return retval;
1114*6a54128fSAndroid Build Coastguard Worker }
1115*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_read_inode(fs, ino, &inode);
1116*6a54128fSAndroid Build Coastguard Worker if (retval)
1117*6a54128fSAndroid Build Coastguard Worker return retval;
1118*6a54128fSAndroid Build Coastguard Worker if (!LINUX_S_ISDIR(inode.i_mode))
1119*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NO_DIRECTORY;
1120*6a54128fSAndroid Build Coastguard Worker return 0;
1121*6a54128fSAndroid Build Coastguard Worker }
1122*6a54128fSAndroid Build Coastguard Worker
1123