1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker * openfs.c --- open an ext2 filesystem
3*6a54128fSAndroid Build Coastguard Worker *
4*6a54128fSAndroid Build Coastguard Worker * Copyright (C) 1993, 1994, 1995, 1996 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 #include <fcntl.h>
19*6a54128fSAndroid Build Coastguard Worker #include <time.h>
20*6a54128fSAndroid Build Coastguard Worker #if HAVE_SYS_STAT_H
21*6a54128fSAndroid Build Coastguard Worker #include <sys/stat.h>
22*6a54128fSAndroid Build Coastguard Worker #endif
23*6a54128fSAndroid Build Coastguard Worker #if HAVE_SYS_TYPES_H
24*6a54128fSAndroid Build Coastguard Worker #include <sys/types.h>
25*6a54128fSAndroid Build Coastguard Worker #endif
26*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_ERRNO_H
27*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
28*6a54128fSAndroid Build Coastguard Worker #endif
29*6a54128fSAndroid Build Coastguard Worker
30*6a54128fSAndroid Build Coastguard Worker #include "ext2_fs.h"
31*6a54128fSAndroid Build Coastguard Worker
32*6a54128fSAndroid Build Coastguard Worker #include "ext2fs.h"
33*6a54128fSAndroid Build Coastguard Worker #include "e2image.h"
34*6a54128fSAndroid Build Coastguard Worker
ext2fs_descriptor_block_loc2(ext2_filsys fs,blk64_t group_block,dgrp_t i)35*6a54128fSAndroid Build Coastguard Worker blk64_t ext2fs_descriptor_block_loc2(ext2_filsys fs, blk64_t group_block,
36*6a54128fSAndroid Build Coastguard Worker dgrp_t i)
37*6a54128fSAndroid Build Coastguard Worker {
38*6a54128fSAndroid Build Coastguard Worker int bg;
39*6a54128fSAndroid Build Coastguard Worker int has_super = 0, group_zero_adjust = 0;
40*6a54128fSAndroid Build Coastguard Worker blk64_t ret_blk;
41*6a54128fSAndroid Build Coastguard Worker
42*6a54128fSAndroid Build Coastguard Worker /*
43*6a54128fSAndroid Build Coastguard Worker * On a bigalloc FS with 1K blocks, block 0 is reserved for non-ext4
44*6a54128fSAndroid Build Coastguard Worker * stuff, so adjust for that if we're being asked for group 0.
45*6a54128fSAndroid Build Coastguard Worker */
46*6a54128fSAndroid Build Coastguard Worker if (i == 0 && fs->blocksize == 1024 && EXT2FS_CLUSTER_RATIO(fs) > 1)
47*6a54128fSAndroid Build Coastguard Worker group_zero_adjust = 1;
48*6a54128fSAndroid Build Coastguard Worker
49*6a54128fSAndroid Build Coastguard Worker if (!ext2fs_has_feature_meta_bg(fs->super) ||
50*6a54128fSAndroid Build Coastguard Worker (i < fs->super->s_first_meta_bg))
51*6a54128fSAndroid Build Coastguard Worker return group_block + i + 1 + group_zero_adjust;
52*6a54128fSAndroid Build Coastguard Worker
53*6a54128fSAndroid Build Coastguard Worker bg = EXT2_DESC_PER_BLOCK(fs->super) * i;
54*6a54128fSAndroid Build Coastguard Worker if (ext2fs_bg_has_super(fs, bg))
55*6a54128fSAndroid Build Coastguard Worker has_super = 1;
56*6a54128fSAndroid Build Coastguard Worker ret_blk = ext2fs_group_first_block2(fs, bg);
57*6a54128fSAndroid Build Coastguard Worker /*
58*6a54128fSAndroid Build Coastguard Worker * If group_block is not the normal value, we're trying to use
59*6a54128fSAndroid Build Coastguard Worker * the backup group descriptors and superblock --- so use the
60*6a54128fSAndroid Build Coastguard Worker * alternate location of the second block group in the
61*6a54128fSAndroid Build Coastguard Worker * metablock group. Ideally we should be testing each bg
62*6a54128fSAndroid Build Coastguard Worker * descriptor block individually for correctness, but we don't
63*6a54128fSAndroid Build Coastguard Worker * have the infrastructure in place to do that.
64*6a54128fSAndroid Build Coastguard Worker */
65*6a54128fSAndroid Build Coastguard Worker if (group_block != fs->super->s_first_data_block &&
66*6a54128fSAndroid Build Coastguard Worker ((ret_blk + has_super + fs->super->s_blocks_per_group) <
67*6a54128fSAndroid Build Coastguard Worker ext2fs_blocks_count(fs->super))) {
68*6a54128fSAndroid Build Coastguard Worker ret_blk += fs->super->s_blocks_per_group;
69*6a54128fSAndroid Build Coastguard Worker
70*6a54128fSAndroid Build Coastguard Worker /*
71*6a54128fSAndroid Build Coastguard Worker * If we're going to jump forward a block group, make sure
72*6a54128fSAndroid Build Coastguard Worker * that we adjust has_super to account for the next group's
73*6a54128fSAndroid Build Coastguard Worker * backup superblock (or lack thereof).
74*6a54128fSAndroid Build Coastguard Worker */
75*6a54128fSAndroid Build Coastguard Worker if (ext2fs_bg_has_super(fs, bg + 1))
76*6a54128fSAndroid Build Coastguard Worker has_super = 1;
77*6a54128fSAndroid Build Coastguard Worker else
78*6a54128fSAndroid Build Coastguard Worker has_super = 0;
79*6a54128fSAndroid Build Coastguard Worker }
80*6a54128fSAndroid Build Coastguard Worker return ret_blk + has_super + group_zero_adjust;
81*6a54128fSAndroid Build Coastguard Worker }
82*6a54128fSAndroid Build Coastguard Worker
ext2fs_descriptor_block_loc(ext2_filsys fs,blk_t group_block,dgrp_t i)83*6a54128fSAndroid Build Coastguard Worker blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i)
84*6a54128fSAndroid Build Coastguard Worker {
85*6a54128fSAndroid Build Coastguard Worker return ext2fs_descriptor_block_loc2(fs, group_block, i);
86*6a54128fSAndroid Build Coastguard Worker }
87*6a54128fSAndroid Build Coastguard Worker
ext2fs_open(const char * name,int flags,int superblock,unsigned int block_size,io_manager manager,ext2_filsys * ret_fs)88*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_open(const char *name, int flags, int superblock,
89*6a54128fSAndroid Build Coastguard Worker unsigned int block_size, io_manager manager,
90*6a54128fSAndroid Build Coastguard Worker ext2_filsys *ret_fs)
91*6a54128fSAndroid Build Coastguard Worker {
92*6a54128fSAndroid Build Coastguard Worker return ext2fs_open2(name, 0, flags, superblock, block_size,
93*6a54128fSAndroid Build Coastguard Worker manager, ret_fs);
94*6a54128fSAndroid Build Coastguard Worker }
95*6a54128fSAndroid Build Coastguard Worker
block_sha_map_free_entry(void * data)96*6a54128fSAndroid Build Coastguard Worker static void block_sha_map_free_entry(void *data)
97*6a54128fSAndroid Build Coastguard Worker {
98*6a54128fSAndroid Build Coastguard Worker free(data);
99*6a54128fSAndroid Build Coastguard Worker return;
100*6a54128fSAndroid Build Coastguard Worker }
101*6a54128fSAndroid Build Coastguard Worker
102*6a54128fSAndroid Build Coastguard Worker /*
103*6a54128fSAndroid Build Coastguard Worker * Note: if superblock is non-zero, block-size must also be non-zero.
104*6a54128fSAndroid Build Coastguard Worker * Superblock and block_size can be zero to use the default size.
105*6a54128fSAndroid Build Coastguard Worker *
106*6a54128fSAndroid Build Coastguard Worker * Valid flags for ext2fs_open()
107*6a54128fSAndroid Build Coastguard Worker *
108*6a54128fSAndroid Build Coastguard Worker * EXT2_FLAG_RW - Open the filesystem for read/write.
109*6a54128fSAndroid Build Coastguard Worker * EXT2_FLAG_FORCE - Open the filesystem even if some of the
110*6a54128fSAndroid Build Coastguard Worker * features aren't supported.
111*6a54128fSAndroid Build Coastguard Worker * EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device
112*6a54128fSAndroid Build Coastguard Worker * EXT2_FLAG_SKIP_MMP - Open without multi-mount protection check.
113*6a54128fSAndroid Build Coastguard Worker * EXT2_FLAG_64BITS - Allow 64-bit bitfields (needed for large
114*6a54128fSAndroid Build Coastguard Worker * filesystems)
115*6a54128fSAndroid Build Coastguard Worker */
ext2fs_open2(const char * name,const char * io_options,int flags,int superblock,unsigned int block_size,io_manager manager,ext2_filsys * ret_fs)116*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_open2(const char *name, const char *io_options,
117*6a54128fSAndroid Build Coastguard Worker int flags, int superblock,
118*6a54128fSAndroid Build Coastguard Worker unsigned int block_size, io_manager manager,
119*6a54128fSAndroid Build Coastguard Worker ext2_filsys *ret_fs)
120*6a54128fSAndroid Build Coastguard Worker {
121*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs;
122*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
123*6a54128fSAndroid Build Coastguard Worker unsigned long i, first_meta_bg;
124*6a54128fSAndroid Build Coastguard Worker __u32 features;
125*6a54128fSAndroid Build Coastguard Worker unsigned int blocks_per_group, io_flags;
126*6a54128fSAndroid Build Coastguard Worker blk64_t group_block, blk;
127*6a54128fSAndroid Build Coastguard Worker char *dest, *cp;
128*6a54128fSAndroid Build Coastguard Worker int group_zero_adjust = 0;
129*6a54128fSAndroid Build Coastguard Worker unsigned int inode_size;
130*6a54128fSAndroid Build Coastguard Worker __u64 groups_cnt;
131*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
132*6a54128fSAndroid Build Coastguard Worker unsigned int groups_per_block;
133*6a54128fSAndroid Build Coastguard Worker struct ext2_group_desc *gdp;
134*6a54128fSAndroid Build Coastguard Worker int j;
135*6a54128fSAndroid Build Coastguard Worker #endif
136*6a54128fSAndroid Build Coastguard Worker char *time_env;
137*6a54128fSAndroid Build Coastguard Worker int csum_retries = 0;
138*6a54128fSAndroid Build Coastguard Worker
139*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
140*6a54128fSAndroid Build Coastguard Worker
141*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
142*6a54128fSAndroid Build Coastguard Worker if (retval)
143*6a54128fSAndroid Build Coastguard Worker return retval;
144*6a54128fSAndroid Build Coastguard Worker
145*6a54128fSAndroid Build Coastguard Worker memset(fs, 0, sizeof(struct struct_ext2_filsys));
146*6a54128fSAndroid Build Coastguard Worker fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
147*6a54128fSAndroid Build Coastguard Worker fs->flags = flags;
148*6a54128fSAndroid Build Coastguard Worker /* don't overwrite sb backups unless flag is explicitly cleared */
149*6a54128fSAndroid Build Coastguard Worker fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
150*6a54128fSAndroid Build Coastguard Worker fs->umask = 022;
151*6a54128fSAndroid Build Coastguard Worker
152*6a54128fSAndroid Build Coastguard Worker time_env = getenv("E2FSPROGS_FAKE_TIME");
153*6a54128fSAndroid Build Coastguard Worker if (time_env)
154*6a54128fSAndroid Build Coastguard Worker fs->now = strtoul(time_env, NULL, 0);
155*6a54128fSAndroid Build Coastguard Worker
156*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
157*6a54128fSAndroid Build Coastguard Worker if (retval)
158*6a54128fSAndroid Build Coastguard Worker goto cleanup;
159*6a54128fSAndroid Build Coastguard Worker strcpy(fs->device_name, name);
160*6a54128fSAndroid Build Coastguard Worker cp = strchr(fs->device_name, '?');
161*6a54128fSAndroid Build Coastguard Worker if (!io_options && cp) {
162*6a54128fSAndroid Build Coastguard Worker *cp++ = 0;
163*6a54128fSAndroid Build Coastguard Worker io_options = cp;
164*6a54128fSAndroid Build Coastguard Worker }
165*6a54128fSAndroid Build Coastguard Worker
166*6a54128fSAndroid Build Coastguard Worker io_flags = 0;
167*6a54128fSAndroid Build Coastguard Worker if (flags & EXT2_FLAG_RW)
168*6a54128fSAndroid Build Coastguard Worker io_flags |= IO_FLAG_RW;
169*6a54128fSAndroid Build Coastguard Worker if (flags & EXT2_FLAG_EXCLUSIVE)
170*6a54128fSAndroid Build Coastguard Worker io_flags |= IO_FLAG_EXCLUSIVE;
171*6a54128fSAndroid Build Coastguard Worker if (flags & EXT2_FLAG_DIRECT_IO)
172*6a54128fSAndroid Build Coastguard Worker io_flags |= IO_FLAG_DIRECT_IO;
173*6a54128fSAndroid Build Coastguard Worker if (flags & EXT2_FLAG_THREADS)
174*6a54128fSAndroid Build Coastguard Worker io_flags |= IO_FLAG_THREADS;
175*6a54128fSAndroid Build Coastguard Worker retval = manager->open(fs->device_name, io_flags, &fs->io);
176*6a54128fSAndroid Build Coastguard Worker if (retval)
177*6a54128fSAndroid Build Coastguard Worker goto cleanup;
178*6a54128fSAndroid Build Coastguard Worker if (io_options &&
179*6a54128fSAndroid Build Coastguard Worker (retval = io_channel_set_options(fs->io, io_options)))
180*6a54128fSAndroid Build Coastguard Worker goto cleanup;
181*6a54128fSAndroid Build Coastguard Worker fs->image_io = fs->io;
182*6a54128fSAndroid Build Coastguard Worker fs->io->app_data = fs;
183*6a54128fSAndroid Build Coastguard Worker retval = io_channel_alloc_buf(fs->io, -SUPERBLOCK_SIZE, &fs->super);
184*6a54128fSAndroid Build Coastguard Worker if (retval)
185*6a54128fSAndroid Build Coastguard Worker goto cleanup;
186*6a54128fSAndroid Build Coastguard Worker if (flags & EXT2_FLAG_IMAGE_FILE) {
187*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr),
188*6a54128fSAndroid Build Coastguard Worker &fs->image_header);
189*6a54128fSAndroid Build Coastguard Worker if (retval)
190*6a54128fSAndroid Build Coastguard Worker goto cleanup;
191*6a54128fSAndroid Build Coastguard Worker retval = io_channel_read_blk(fs->io, 0,
192*6a54128fSAndroid Build Coastguard Worker -(int)sizeof(struct ext2_image_hdr),
193*6a54128fSAndroid Build Coastguard Worker fs->image_header);
194*6a54128fSAndroid Build Coastguard Worker if (retval)
195*6a54128fSAndroid Build Coastguard Worker goto cleanup;
196*6a54128fSAndroid Build Coastguard Worker if (ext2fs_le32_to_cpu(fs->image_header->magic_number) != EXT2_ET_MAGIC_E2IMAGE)
197*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_MAGIC_E2IMAGE;
198*6a54128fSAndroid Build Coastguard Worker superblock = 1;
199*6a54128fSAndroid Build Coastguard Worker block_size = ext2fs_le32_to_cpu(fs->image_header->fs_blocksize);
200*6a54128fSAndroid Build Coastguard Worker }
201*6a54128fSAndroid Build Coastguard Worker
202*6a54128fSAndroid Build Coastguard Worker /*
203*6a54128fSAndroid Build Coastguard Worker * If the user specifies a specific block # for the
204*6a54128fSAndroid Build Coastguard Worker * superblock, then he/she must also specify the block size!
205*6a54128fSAndroid Build Coastguard Worker * Otherwise, read the master superblock located at offset
206*6a54128fSAndroid Build Coastguard Worker * SUPERBLOCK_OFFSET from the start of the partition.
207*6a54128fSAndroid Build Coastguard Worker *
208*6a54128fSAndroid Build Coastguard Worker * Note: we only save a backup copy of the superblock if we
209*6a54128fSAndroid Build Coastguard Worker * are reading the superblock from the primary superblock location.
210*6a54128fSAndroid Build Coastguard Worker */
211*6a54128fSAndroid Build Coastguard Worker if (superblock) {
212*6a54128fSAndroid Build Coastguard Worker if (!block_size) {
213*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_INVALID_ARGUMENT;
214*6a54128fSAndroid Build Coastguard Worker goto cleanup;
215*6a54128fSAndroid Build Coastguard Worker }
216*6a54128fSAndroid Build Coastguard Worker io_channel_set_blksize(fs->io, block_size);
217*6a54128fSAndroid Build Coastguard Worker group_block = superblock;
218*6a54128fSAndroid Build Coastguard Worker fs->orig_super = 0;
219*6a54128fSAndroid Build Coastguard Worker } else {
220*6a54128fSAndroid Build Coastguard Worker io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
221*6a54128fSAndroid Build Coastguard Worker superblock = 1;
222*6a54128fSAndroid Build Coastguard Worker group_block = 0;
223*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
224*6a54128fSAndroid Build Coastguard Worker if (retval)
225*6a54128fSAndroid Build Coastguard Worker goto cleanup;
226*6a54128fSAndroid Build Coastguard Worker }
227*6a54128fSAndroid Build Coastguard Worker retry:
228*6a54128fSAndroid Build Coastguard Worker retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
229*6a54128fSAndroid Build Coastguard Worker fs->super);
230*6a54128fSAndroid Build Coastguard Worker if (retval)
231*6a54128fSAndroid Build Coastguard Worker goto cleanup;
232*6a54128fSAndroid Build Coastguard Worker if (fs->orig_super)
233*6a54128fSAndroid Build Coastguard Worker memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);
234*6a54128fSAndroid Build Coastguard Worker
235*6a54128fSAndroid Build Coastguard Worker if (!(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS)) {
236*6a54128fSAndroid Build Coastguard Worker retval = 0;
237*6a54128fSAndroid Build Coastguard Worker if (!ext2fs_verify_csum_type(fs, fs->super))
238*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_UNKNOWN_CSUM;
239*6a54128fSAndroid Build Coastguard Worker if (!ext2fs_superblock_csum_verify(fs, fs->super)) {
240*6a54128fSAndroid Build Coastguard Worker if (csum_retries++ < 3)
241*6a54128fSAndroid Build Coastguard Worker goto retry;
242*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_SB_CSUM_INVALID;
243*6a54128fSAndroid Build Coastguard Worker }
244*6a54128fSAndroid Build Coastguard Worker }
245*6a54128fSAndroid Build Coastguard Worker
246*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
247*6a54128fSAndroid Build Coastguard Worker fs->flags |= EXT2_FLAG_SWAP_BYTES;
248*6a54128fSAndroid Build Coastguard Worker ext2fs_swap_super(fs->super);
249*6a54128fSAndroid Build Coastguard Worker #else
250*6a54128fSAndroid Build Coastguard Worker if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
251*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_UNIMPLEMENTED;
252*6a54128fSAndroid Build Coastguard Worker goto cleanup;
253*6a54128fSAndroid Build Coastguard Worker }
254*6a54128fSAndroid Build Coastguard Worker #endif
255*6a54128fSAndroid Build Coastguard Worker
256*6a54128fSAndroid Build Coastguard Worker if (fs->super->s_magic != EXT2_SUPER_MAGIC)
257*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_BAD_MAGIC;
258*6a54128fSAndroid Build Coastguard Worker if (retval)
259*6a54128fSAndroid Build Coastguard Worker goto cleanup;
260*6a54128fSAndroid Build Coastguard Worker
261*6a54128fSAndroid Build Coastguard Worker if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
262*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_REV_TOO_HIGH;
263*6a54128fSAndroid Build Coastguard Worker goto cleanup;
264*6a54128fSAndroid Build Coastguard Worker }
265*6a54128fSAndroid Build Coastguard Worker
266*6a54128fSAndroid Build Coastguard Worker /*
267*6a54128fSAndroid Build Coastguard Worker * Check for feature set incompatibility
268*6a54128fSAndroid Build Coastguard Worker */
269*6a54128fSAndroid Build Coastguard Worker if (!(flags & EXT2_FLAG_FORCE)) {
270*6a54128fSAndroid Build Coastguard Worker features = fs->super->s_feature_incompat;
271*6a54128fSAndroid Build Coastguard Worker #ifdef EXT2_LIB_SOFTSUPP_INCOMPAT
272*6a54128fSAndroid Build Coastguard Worker if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
273*6a54128fSAndroid Build Coastguard Worker features &= ~EXT2_LIB_SOFTSUPP_INCOMPAT;
274*6a54128fSAndroid Build Coastguard Worker #endif
275*6a54128fSAndroid Build Coastguard Worker if (features & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
276*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_UNSUPP_FEATURE;
277*6a54128fSAndroid Build Coastguard Worker goto cleanup;
278*6a54128fSAndroid Build Coastguard Worker }
279*6a54128fSAndroid Build Coastguard Worker
280*6a54128fSAndroid Build Coastguard Worker features = fs->super->s_feature_ro_compat;
281*6a54128fSAndroid Build Coastguard Worker #ifdef EXT2_LIB_SOFTSUPP_RO_COMPAT
282*6a54128fSAndroid Build Coastguard Worker if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
283*6a54128fSAndroid Build Coastguard Worker features &= ~EXT2_LIB_SOFTSUPP_RO_COMPAT;
284*6a54128fSAndroid Build Coastguard Worker #endif
285*6a54128fSAndroid Build Coastguard Worker if ((flags & EXT2_FLAG_RW) &&
286*6a54128fSAndroid Build Coastguard Worker (features & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
287*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_RO_UNSUPP_FEATURE;
288*6a54128fSAndroid Build Coastguard Worker goto cleanup;
289*6a54128fSAndroid Build Coastguard Worker }
290*6a54128fSAndroid Build Coastguard Worker
291*6a54128fSAndroid Build Coastguard Worker if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
292*6a54128fSAndroid Build Coastguard Worker ext2fs_has_feature_journal_dev(fs->super)) {
293*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_UNSUPP_FEATURE;
294*6a54128fSAndroid Build Coastguard Worker goto cleanup;
295*6a54128fSAndroid Build Coastguard Worker }
296*6a54128fSAndroid Build Coastguard Worker }
297*6a54128fSAndroid Build Coastguard Worker
298*6a54128fSAndroid Build Coastguard Worker if ((fs->super->s_log_block_size >
299*6a54128fSAndroid Build Coastguard Worker (unsigned) (EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE)) ||
300*6a54128fSAndroid Build Coastguard Worker (fs->super->s_log_cluster_size >
301*6a54128fSAndroid Build Coastguard Worker (unsigned) (EXT2_MAX_CLUSTER_LOG_SIZE - EXT2_MIN_CLUSTER_LOG_SIZE)) ||
302*6a54128fSAndroid Build Coastguard Worker (fs->super->s_log_block_size > fs->super->s_log_cluster_size) ||
303*6a54128fSAndroid Build Coastguard Worker (fs->super->s_log_groups_per_flex > 31)) {
304*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_CORRUPT_SUPERBLOCK;
305*6a54128fSAndroid Build Coastguard Worker goto cleanup;
306*6a54128fSAndroid Build Coastguard Worker }
307*6a54128fSAndroid Build Coastguard Worker
308*6a54128fSAndroid Build Coastguard Worker /*
309*6a54128fSAndroid Build Coastguard Worker * bigalloc requires cluster-aware bitfield operations, which at the
310*6a54128fSAndroid Build Coastguard Worker * moment means we need EXT2_FLAG_64BITS.
311*6a54128fSAndroid Build Coastguard Worker */
312*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_feature_bigalloc(fs->super) &&
313*6a54128fSAndroid Build Coastguard Worker !(flags & EXT2_FLAG_64BITS)) {
314*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_CANT_USE_LEGACY_BITMAPS;
315*6a54128fSAndroid Build Coastguard Worker goto cleanup;
316*6a54128fSAndroid Build Coastguard Worker }
317*6a54128fSAndroid Build Coastguard Worker
318*6a54128fSAndroid Build Coastguard Worker if (!ext2fs_has_feature_bigalloc(fs->super) &&
319*6a54128fSAndroid Build Coastguard Worker (fs->super->s_log_block_size != fs->super->s_log_cluster_size)) {
320*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_CORRUPT_SUPERBLOCK;
321*6a54128fSAndroid Build Coastguard Worker goto cleanup;
322*6a54128fSAndroid Build Coastguard Worker }
323*6a54128fSAndroid Build Coastguard Worker fs->fragsize = fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
324*6a54128fSAndroid Build Coastguard Worker inode_size = EXT2_INODE_SIZE(fs->super);
325*6a54128fSAndroid Build Coastguard Worker if ((inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
326*6a54128fSAndroid Build Coastguard Worker (inode_size > fs->blocksize) ||
327*6a54128fSAndroid Build Coastguard Worker (inode_size & (inode_size - 1))) {
328*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_CORRUPT_SUPERBLOCK;
329*6a54128fSAndroid Build Coastguard Worker goto cleanup;
330*6a54128fSAndroid Build Coastguard Worker }
331*6a54128fSAndroid Build Coastguard Worker
332*6a54128fSAndroid Build Coastguard Worker /* Enforce the block group descriptor size */
333*6a54128fSAndroid Build Coastguard Worker if (!(flags & EXT2_FLAG_IGNORE_SB_ERRORS) &&
334*6a54128fSAndroid Build Coastguard Worker ext2fs_has_feature_64bit(fs->super)) {
335*6a54128fSAndroid Build Coastguard Worker unsigned desc_size = fs->super->s_desc_size;
336*6a54128fSAndroid Build Coastguard Worker
337*6a54128fSAndroid Build Coastguard Worker if ((desc_size < EXT2_MIN_DESC_SIZE_64BIT) ||
338*6a54128fSAndroid Build Coastguard Worker (desc_size > EXT2_MAX_DESC_SIZE) ||
339*6a54128fSAndroid Build Coastguard Worker (desc_size & (desc_size - 1)) != 0) {
340*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_BAD_DESC_SIZE;
341*6a54128fSAndroid Build Coastguard Worker goto cleanup;
342*6a54128fSAndroid Build Coastguard Worker }
343*6a54128fSAndroid Build Coastguard Worker }
344*6a54128fSAndroid Build Coastguard Worker
345*6a54128fSAndroid Build Coastguard Worker fs->cluster_ratio_bits = fs->super->s_log_cluster_size -
346*6a54128fSAndroid Build Coastguard Worker fs->super->s_log_block_size;
347*6a54128fSAndroid Build Coastguard Worker if (EXT2_BLOCKS_PER_GROUP(fs->super) !=
348*6a54128fSAndroid Build Coastguard Worker EXT2_CLUSTERS_PER_GROUP(fs->super) << fs->cluster_ratio_bits) {
349*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_CORRUPT_SUPERBLOCK;
350*6a54128fSAndroid Build Coastguard Worker goto cleanup;
351*6a54128fSAndroid Build Coastguard Worker }
352*6a54128fSAndroid Build Coastguard Worker fs->inode_blocks_per_group = ((EXT2_INODES_PER_GROUP(fs->super) *
353*6a54128fSAndroid Build Coastguard Worker EXT2_INODE_SIZE(fs->super) +
354*6a54128fSAndroid Build Coastguard Worker EXT2_BLOCK_SIZE(fs->super) - 1) /
355*6a54128fSAndroid Build Coastguard Worker EXT2_BLOCK_SIZE(fs->super));
356*6a54128fSAndroid Build Coastguard Worker if (block_size) {
357*6a54128fSAndroid Build Coastguard Worker if (block_size != fs->blocksize) {
358*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
359*6a54128fSAndroid Build Coastguard Worker goto cleanup;
360*6a54128fSAndroid Build Coastguard Worker }
361*6a54128fSAndroid Build Coastguard Worker }
362*6a54128fSAndroid Build Coastguard Worker /*
363*6a54128fSAndroid Build Coastguard Worker * Set the blocksize to the filesystem's blocksize.
364*6a54128fSAndroid Build Coastguard Worker */
365*6a54128fSAndroid Build Coastguard Worker io_channel_set_blksize(fs->io, fs->blocksize);
366*6a54128fSAndroid Build Coastguard Worker
367*6a54128fSAndroid Build Coastguard Worker /*
368*6a54128fSAndroid Build Coastguard Worker * If this is an external journal device, don't try to read
369*6a54128fSAndroid Build Coastguard Worker * the group descriptors, because they're not there.
370*6a54128fSAndroid Build Coastguard Worker */
371*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_feature_journal_dev(fs->super)) {
372*6a54128fSAndroid Build Coastguard Worker fs->group_desc_count = 0;
373*6a54128fSAndroid Build Coastguard Worker *ret_fs = fs;
374*6a54128fSAndroid Build Coastguard Worker return 0;
375*6a54128fSAndroid Build Coastguard Worker }
376*6a54128fSAndroid Build Coastguard Worker
377*6a54128fSAndroid Build Coastguard Worker if (EXT2_INODES_PER_GROUP(fs->super) == 0) {
378*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_CORRUPT_SUPERBLOCK;
379*6a54128fSAndroid Build Coastguard Worker goto cleanup;
380*6a54128fSAndroid Build Coastguard Worker }
381*6a54128fSAndroid Build Coastguard Worker /* Precompute the FS UUID to seed other checksums */
382*6a54128fSAndroid Build Coastguard Worker ext2fs_init_csum_seed(fs);
383*6a54128fSAndroid Build Coastguard Worker
384*6a54128fSAndroid Build Coastguard Worker /*
385*6a54128fSAndroid Build Coastguard Worker * Read group descriptors
386*6a54128fSAndroid Build Coastguard Worker */
387*6a54128fSAndroid Build Coastguard Worker blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
388*6a54128fSAndroid Build Coastguard Worker if (blocks_per_group < 8 ||
389*6a54128fSAndroid Build Coastguard Worker blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
390*6a54128fSAndroid Build Coastguard Worker fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super) ||
391*6a54128fSAndroid Build Coastguard Worker EXT2_DESC_PER_BLOCK(fs->super) == 0 ||
392*6a54128fSAndroid Build Coastguard Worker fs->super->s_first_data_block >= ext2fs_blocks_count(fs->super)) {
393*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_CORRUPT_SUPERBLOCK;
394*6a54128fSAndroid Build Coastguard Worker goto cleanup;
395*6a54128fSAndroid Build Coastguard Worker }
396*6a54128fSAndroid Build Coastguard Worker groups_cnt = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
397*6a54128fSAndroid Build Coastguard Worker fs->super->s_first_data_block,
398*6a54128fSAndroid Build Coastguard Worker blocks_per_group);
399*6a54128fSAndroid Build Coastguard Worker if (groups_cnt >> 32) {
400*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_CORRUPT_SUPERBLOCK;
401*6a54128fSAndroid Build Coastguard Worker goto cleanup;
402*6a54128fSAndroid Build Coastguard Worker }
403*6a54128fSAndroid Build Coastguard Worker fs->group_desc_count = groups_cnt;
404*6a54128fSAndroid Build Coastguard Worker if (!(flags & EXT2_FLAG_IGNORE_SB_ERRORS) &&
405*6a54128fSAndroid Build Coastguard Worker (__u64)fs->group_desc_count * EXT2_INODES_PER_GROUP(fs->super) !=
406*6a54128fSAndroid Build Coastguard Worker fs->super->s_inodes_count) {
407*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_CORRUPT_SUPERBLOCK;
408*6a54128fSAndroid Build Coastguard Worker goto cleanup;
409*6a54128fSAndroid Build Coastguard Worker }
410*6a54128fSAndroid Build Coastguard Worker fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
411*6a54128fSAndroid Build Coastguard Worker EXT2_DESC_PER_BLOCK(fs->super));
412*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_feature_meta_bg(fs->super) &&
413*6a54128fSAndroid Build Coastguard Worker (fs->super->s_first_meta_bg > fs->desc_blocks) &&
414*6a54128fSAndroid Build Coastguard Worker !(flags & EXT2_FLAG_IGNORE_SB_ERRORS)) {
415*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_CORRUPT_SUPERBLOCK;
416*6a54128fSAndroid Build Coastguard Worker goto cleanup;
417*6a54128fSAndroid Build Coastguard Worker }
418*6a54128fSAndroid Build Coastguard Worker if (flags & EXT2_FLAG_SUPER_ONLY)
419*6a54128fSAndroid Build Coastguard Worker goto skip_read_bg;
420*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
421*6a54128fSAndroid Build Coastguard Worker &fs->group_desc);
422*6a54128fSAndroid Build Coastguard Worker if (retval)
423*6a54128fSAndroid Build Coastguard Worker goto cleanup;
424*6a54128fSAndroid Build Coastguard Worker if (!group_block)
425*6a54128fSAndroid Build Coastguard Worker group_block = fs->super->s_first_data_block;
426*6a54128fSAndroid Build Coastguard Worker /*
427*6a54128fSAndroid Build Coastguard Worker * On a FS with a 1K blocksize, block 0 is reserved for bootloaders
428*6a54128fSAndroid Build Coastguard Worker * so we must increment block numbers to any group 0 items.
429*6a54128fSAndroid Build Coastguard Worker *
430*6a54128fSAndroid Build Coastguard Worker * However, we cannot touch group_block directly because in the meta_bg
431*6a54128fSAndroid Build Coastguard Worker * case, the ext2fs_descriptor_block_loc2() function will interpret
432*6a54128fSAndroid Build Coastguard Worker * group_block != s_first_data_block to mean that we want to access the
433*6a54128fSAndroid Build Coastguard Worker * backup group descriptors. This is not what we want if the caller
434*6a54128fSAndroid Build Coastguard Worker * set superblock == 0 (i.e. auto-detect the superblock), which is
435*6a54128fSAndroid Build Coastguard Worker * what's going on here.
436*6a54128fSAndroid Build Coastguard Worker */
437*6a54128fSAndroid Build Coastguard Worker if (group_block == 0 && fs->blocksize == 1024)
438*6a54128fSAndroid Build Coastguard Worker group_zero_adjust = 1;
439*6a54128fSAndroid Build Coastguard Worker dest = (char *) fs->group_desc;
440*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
441*6a54128fSAndroid Build Coastguard Worker groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
442*6a54128fSAndroid Build Coastguard Worker #endif
443*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_feature_meta_bg(fs->super) &&
444*6a54128fSAndroid Build Coastguard Worker !(flags & EXT2_FLAG_IMAGE_FILE)) {
445*6a54128fSAndroid Build Coastguard Worker first_meta_bg = fs->super->s_first_meta_bg;
446*6a54128fSAndroid Build Coastguard Worker if (first_meta_bg > fs->desc_blocks)
447*6a54128fSAndroid Build Coastguard Worker first_meta_bg = fs->desc_blocks;
448*6a54128fSAndroid Build Coastguard Worker } else
449*6a54128fSAndroid Build Coastguard Worker first_meta_bg = fs->desc_blocks;
450*6a54128fSAndroid Build Coastguard Worker if (first_meta_bg) {
451*6a54128fSAndroid Build Coastguard Worker retval = io_channel_read_blk(fs->io, group_block +
452*6a54128fSAndroid Build Coastguard Worker group_zero_adjust + 1,
453*6a54128fSAndroid Build Coastguard Worker first_meta_bg, dest);
454*6a54128fSAndroid Build Coastguard Worker if (retval)
455*6a54128fSAndroid Build Coastguard Worker goto cleanup;
456*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
457*6a54128fSAndroid Build Coastguard Worker gdp = (struct ext2_group_desc *) dest;
458*6a54128fSAndroid Build Coastguard Worker for (j=0; j < groups_per_block*first_meta_bg; j++) {
459*6a54128fSAndroid Build Coastguard Worker gdp = ext2fs_group_desc(fs, fs->group_desc, j);
460*6a54128fSAndroid Build Coastguard Worker if (gdp)
461*6a54128fSAndroid Build Coastguard Worker ext2fs_swap_group_desc2(fs, gdp);
462*6a54128fSAndroid Build Coastguard Worker }
463*6a54128fSAndroid Build Coastguard Worker #endif
464*6a54128fSAndroid Build Coastguard Worker dest += fs->blocksize*first_meta_bg;
465*6a54128fSAndroid Build Coastguard Worker }
466*6a54128fSAndroid Build Coastguard Worker
467*6a54128fSAndroid Build Coastguard Worker for (i = first_meta_bg ; i < fs->desc_blocks; i++) {
468*6a54128fSAndroid Build Coastguard Worker blk = ext2fs_descriptor_block_loc2(fs, group_block, i);
469*6a54128fSAndroid Build Coastguard Worker io_channel_cache_readahead(fs->io, blk, 1);
470*6a54128fSAndroid Build Coastguard Worker }
471*6a54128fSAndroid Build Coastguard Worker
472*6a54128fSAndroid Build Coastguard Worker for (i=first_meta_bg ; i < fs->desc_blocks; i++) {
473*6a54128fSAndroid Build Coastguard Worker blk = ext2fs_descriptor_block_loc2(fs, group_block, i);
474*6a54128fSAndroid Build Coastguard Worker retval = io_channel_read_blk64(fs->io, blk, 1, dest);
475*6a54128fSAndroid Build Coastguard Worker if (retval)
476*6a54128fSAndroid Build Coastguard Worker goto cleanup;
477*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
478*6a54128fSAndroid Build Coastguard Worker for (j=0; j < groups_per_block; j++) {
479*6a54128fSAndroid Build Coastguard Worker gdp = ext2fs_group_desc(fs, fs->group_desc,
480*6a54128fSAndroid Build Coastguard Worker i * groups_per_block + j);
481*6a54128fSAndroid Build Coastguard Worker if (gdp)
482*6a54128fSAndroid Build Coastguard Worker ext2fs_swap_group_desc2(fs, gdp);
483*6a54128fSAndroid Build Coastguard Worker }
484*6a54128fSAndroid Build Coastguard Worker #endif
485*6a54128fSAndroid Build Coastguard Worker dest += fs->blocksize;
486*6a54128fSAndroid Build Coastguard Worker }
487*6a54128fSAndroid Build Coastguard Worker
488*6a54128fSAndroid Build Coastguard Worker fs->stride = fs->super->s_raid_stride;
489*6a54128fSAndroid Build Coastguard Worker
490*6a54128fSAndroid Build Coastguard Worker /*
491*6a54128fSAndroid Build Coastguard Worker * If recovery is from backup superblock, Clear _UNININT flags &
492*6a54128fSAndroid Build Coastguard Worker * reset bg_itable_unused to zero
493*6a54128fSAndroid Build Coastguard Worker */
494*6a54128fSAndroid Build Coastguard Worker if (superblock > 1 && ext2fs_has_group_desc_csum(fs)) {
495*6a54128fSAndroid Build Coastguard Worker dgrp_t group;
496*6a54128fSAndroid Build Coastguard Worker
497*6a54128fSAndroid Build Coastguard Worker for (group = 0; group < fs->group_desc_count; group++) {
498*6a54128fSAndroid Build Coastguard Worker ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
499*6a54128fSAndroid Build Coastguard Worker ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
500*6a54128fSAndroid Build Coastguard Worker ext2fs_bg_itable_unused_set(fs, group, 0);
501*6a54128fSAndroid Build Coastguard Worker /* The checksum will be reset later, but fix it here
502*6a54128fSAndroid Build Coastguard Worker * anyway to avoid printing a lot of spurious errors. */
503*6a54128fSAndroid Build Coastguard Worker ext2fs_group_desc_csum_set(fs, group);
504*6a54128fSAndroid Build Coastguard Worker }
505*6a54128fSAndroid Build Coastguard Worker if (fs->flags & EXT2_FLAG_RW)
506*6a54128fSAndroid Build Coastguard Worker ext2fs_mark_super_dirty(fs);
507*6a54128fSAndroid Build Coastguard Worker }
508*6a54128fSAndroid Build Coastguard Worker skip_read_bg:
509*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_feature_mmp(fs->super) &&
510*6a54128fSAndroid Build Coastguard Worker !(flags & EXT2_FLAG_SKIP_MMP) &&
511*6a54128fSAndroid Build Coastguard Worker (flags & (EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE))) {
512*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_mmp_start(fs);
513*6a54128fSAndroid Build Coastguard Worker if (retval) {
514*6a54128fSAndroid Build Coastguard Worker fs->flags |= EXT2_FLAG_SKIP_MMP; /* just do cleanup */
515*6a54128fSAndroid Build Coastguard Worker ext2fs_mmp_stop(fs);
516*6a54128fSAndroid Build Coastguard Worker goto cleanup;
517*6a54128fSAndroid Build Coastguard Worker }
518*6a54128fSAndroid Build Coastguard Worker }
519*6a54128fSAndroid Build Coastguard Worker
520*6a54128fSAndroid Build Coastguard Worker if (fs->flags & EXT2_FLAG_SHARE_DUP) {
521*6a54128fSAndroid Build Coastguard Worker fs->block_sha_map = ext2fs_hashmap_create(ext2fs_djb2_hash,
522*6a54128fSAndroid Build Coastguard Worker block_sha_map_free_entry, 4096);
523*6a54128fSAndroid Build Coastguard Worker if (!fs->block_sha_map) {
524*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_NO_MEMORY;
525*6a54128fSAndroid Build Coastguard Worker goto cleanup;
526*6a54128fSAndroid Build Coastguard Worker }
527*6a54128fSAndroid Build Coastguard Worker ext2fs_set_feature_shared_blocks(fs->super);
528*6a54128fSAndroid Build Coastguard Worker }
529*6a54128fSAndroid Build Coastguard Worker
530*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_feature_casefold(fs->super))
531*6a54128fSAndroid Build Coastguard Worker fs->encoding = ext2fs_load_nls_table(fs->super->s_encoding);
532*6a54128fSAndroid Build Coastguard Worker
533*6a54128fSAndroid Build Coastguard Worker fs->flags &= ~EXT2_FLAG_NOFREE_ON_ERROR;
534*6a54128fSAndroid Build Coastguard Worker *ret_fs = fs;
535*6a54128fSAndroid Build Coastguard Worker
536*6a54128fSAndroid Build Coastguard Worker return 0;
537*6a54128fSAndroid Build Coastguard Worker cleanup:
538*6a54128fSAndroid Build Coastguard Worker if (!(flags & EXT2_FLAG_NOFREE_ON_ERROR)) {
539*6a54128fSAndroid Build Coastguard Worker ext2fs_free(fs);
540*6a54128fSAndroid Build Coastguard Worker fs = NULL;
541*6a54128fSAndroid Build Coastguard Worker }
542*6a54128fSAndroid Build Coastguard Worker *ret_fs = fs;
543*6a54128fSAndroid Build Coastguard Worker return retval;
544*6a54128fSAndroid Build Coastguard Worker }
545*6a54128fSAndroid Build Coastguard Worker
546*6a54128fSAndroid Build Coastguard Worker /*
547*6a54128fSAndroid Build Coastguard Worker * Set/get the filesystem data I/O channel.
548*6a54128fSAndroid Build Coastguard Worker *
549*6a54128fSAndroid Build Coastguard Worker * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true.
550*6a54128fSAndroid Build Coastguard Worker */
ext2fs_get_data_io(ext2_filsys fs,io_channel * old_io)551*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io)
552*6a54128fSAndroid Build Coastguard Worker {
553*6a54128fSAndroid Build Coastguard Worker if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
554*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NOT_IMAGE_FILE;
555*6a54128fSAndroid Build Coastguard Worker if (old_io) {
556*6a54128fSAndroid Build Coastguard Worker *old_io = (fs->image_io == fs->io) ? 0 : fs->io;
557*6a54128fSAndroid Build Coastguard Worker }
558*6a54128fSAndroid Build Coastguard Worker return 0;
559*6a54128fSAndroid Build Coastguard Worker }
560*6a54128fSAndroid Build Coastguard Worker
ext2fs_set_data_io(ext2_filsys fs,io_channel new_io)561*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io)
562*6a54128fSAndroid Build Coastguard Worker {
563*6a54128fSAndroid Build Coastguard Worker if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
564*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NOT_IMAGE_FILE;
565*6a54128fSAndroid Build Coastguard Worker fs->io = new_io ? new_io : fs->image_io;
566*6a54128fSAndroid Build Coastguard Worker return 0;
567*6a54128fSAndroid Build Coastguard Worker }
568*6a54128fSAndroid Build Coastguard Worker
ext2fs_rewrite_to_io(ext2_filsys fs,io_channel new_io)569*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io)
570*6a54128fSAndroid Build Coastguard Worker {
571*6a54128fSAndroid Build Coastguard Worker errcode_t err;
572*6a54128fSAndroid Build Coastguard Worker
573*6a54128fSAndroid Build Coastguard Worker if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
574*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NOT_IMAGE_FILE;
575*6a54128fSAndroid Build Coastguard Worker err = io_channel_set_blksize(new_io, fs->blocksize);
576*6a54128fSAndroid Build Coastguard Worker if (err)
577*6a54128fSAndroid Build Coastguard Worker return err;
578*6a54128fSAndroid Build Coastguard Worker if ((new_io == fs->image_io) || (new_io == fs->io))
579*6a54128fSAndroid Build Coastguard Worker return 0;
580*6a54128fSAndroid Build Coastguard Worker if ((fs->image_io != fs->io) &&
581*6a54128fSAndroid Build Coastguard Worker fs->image_io)
582*6a54128fSAndroid Build Coastguard Worker io_channel_close(fs->image_io);
583*6a54128fSAndroid Build Coastguard Worker if (fs->io)
584*6a54128fSAndroid Build Coastguard Worker io_channel_close(fs->io);
585*6a54128fSAndroid Build Coastguard Worker fs->io = fs->image_io = new_io;
586*6a54128fSAndroid Build Coastguard Worker fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW |
587*6a54128fSAndroid Build Coastguard Worker EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY;
588*6a54128fSAndroid Build Coastguard Worker fs->flags &= ~EXT2_FLAG_IMAGE_FILE;
589*6a54128fSAndroid Build Coastguard Worker return 0;
590*6a54128fSAndroid Build Coastguard Worker }
591