blob: 45b3627ec55ad2266270f967b06bbfa797bbed9b [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * openfs.c --- open an ext2 filesystem
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04005 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00006 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00009 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
12#include <stdio.h>
13#include <string.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000014#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000015#include <unistd.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000016#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000017#include <fcntl.h>
18#include <time.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000019#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000020#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000021#endif
22#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000023#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000024#endif
JP Abgralle0ed7402014-03-19 19:08:39 -070025#ifdef HAVE_ERRNO_H
26#include <errno.h>
27#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000028
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000029#include "ext2_fs.h"
Theodore Ts'oc046ac72002-10-20 00:38:57 -040030
31
Theodore Ts'o3839e651997-04-26 13:21:57 +000032#include "ext2fs.h"
Theodore Ts'oa78926e2001-05-03 04:02:29 +000033#include "e2image.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000034
JP Abgralle0ed7402014-03-19 19:08:39 -070035blk64_t ext2fs_descriptor_block_loc2(ext2_filsys fs, blk64_t group_block,
36 dgrp_t i)
Theodore Ts'oc046ac72002-10-20 00:38:57 -040037{
38 int bg;
39 int has_super = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -070040 blk64_t ret_blk;
Theodore Ts'oc046ac72002-10-20 00:38:57 -040041
42 if (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) ||
43 (i < fs->super->s_first_meta_bg))
44 return (group_block + i + 1);
45
Valerie Clementf2de1d32007-08-30 17:38:13 +020046 bg = EXT2_DESC_PER_BLOCK(fs->super) * i;
Theodore Ts'oc046ac72002-10-20 00:38:57 -040047 if (ext2fs_bg_has_super(fs, bg))
48 has_super = 1;
JP Abgralle0ed7402014-03-19 19:08:39 -070049 ret_blk = ext2fs_group_first_block2(fs, bg) + has_super;
Theodore Ts'o9ed06a12002-10-31 11:45:06 -050050 /*
51 * If group_block is not the normal value, we're trying to use
52 * the backup group descriptors and superblock --- so use the
53 * alternate location of the second block group in the
54 * metablock group. Ideally we should be testing each bg
55 * descriptor block individually for correctness, but we don't
56 * have the infrastructure in place to do that.
57 */
58 if (group_block != fs->super->s_first_data_block &&
59 ((ret_blk + fs->super->s_blocks_per_group) <
JP Abgralle0ed7402014-03-19 19:08:39 -070060 ext2fs_blocks_count(fs->super)))
Theodore Ts'o9ed06a12002-10-31 11:45:06 -050061 ret_blk += fs->super->s_blocks_per_group;
62 return ret_blk;
Theodore Ts'oc046ac72002-10-20 00:38:57 -040063}
64
JP Abgralle0ed7402014-03-19 19:08:39 -070065blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i)
66{
67 return ext2fs_descriptor_block_loc2(fs, group_block, i);
68}
69
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050070errcode_t ext2fs_open(const char *name, int flags, int superblock,
Theodore Ts'oefc6f622008-08-27 23:07:54 -040071 unsigned int block_size, io_manager manager,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050072 ext2_filsys *ret_fs)
73{
Theodore Ts'oefc6f622008-08-27 23:07:54 -040074 return ext2fs_open2(name, 0, flags, superblock, block_size,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050075 manager, ret_fs);
76}
77
Theodore Ts'o3839e651997-04-26 13:21:57 +000078/*
79 * Note: if superblock is non-zero, block-size must also be non-zero.
80 * Superblock and block_size can be zero to use the default size.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000081 *
82 * Valid flags for ext2fs_open()
Theodore Ts'oefc6f622008-08-27 23:07:54 -040083 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000084 * EXT2_FLAG_RW - Open the filesystem for read/write.
85 * EXT2_FLAG_FORCE - Open the filesystem even if some of the
Theodore Ts'oa7773972001-05-13 23:12:10 +000086 * features aren't supported.
87 * EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device
JP Abgralle0ed7402014-03-19 19:08:39 -070088 * EXT2_FLAG_SKIP_MMP - Open without multi-mount protection check.
89 * EXT2_FLAG_64BITS - Allow 64-bit bitfields (needed for large
90 * filesystems)
Theodore Ts'o3839e651997-04-26 13:21:57 +000091 */
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050092errcode_t ext2fs_open2(const char *name, const char *io_options,
93 int flags, int superblock,
Theodore Ts'oefc6f622008-08-27 23:07:54 -040094 unsigned int block_size, io_manager manager,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050095 ext2_filsys *ret_fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +000096{
97 ext2_filsys fs;
98 errcode_t retval;
Theodore Ts'o8203fe52009-04-23 01:30:42 -040099 unsigned long i, first_meta_bg;
Theodore Ts'ocf8272e2006-11-12 23:26:46 -0500100 __u32 features;
JP Abgralle0ed7402014-03-19 19:08:39 -0700101 unsigned int blocks_per_group, io_flags;
102 blk64_t group_block, blk;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500103 char *dest, *cp;
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400104#ifdef WORDS_BIGENDIAN
JP Abgralle0ed7402014-03-19 19:08:39 -0700105 unsigned int groups_per_block;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000106 struct ext2_group_desc *gdp;
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400107 int j;
108#endif
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400109
Theodore Ts'of3db3561997-04-26 13:34:30 +0000110 EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000111
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400112 retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000113 if (retval)
114 return retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400115
Theodore Ts'o3839e651997-04-26 13:21:57 +0000116 memset(fs, 0, sizeof(struct struct_ext2_filsys));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000117 fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000118 fs->flags = flags;
Theodore Ts'o058ad1c2007-06-18 18:26:50 -0400119 /* don't overwrite sb backups unless flag is explicitly cleared */
120 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
Theodore Ts'o6a525062001-12-24 09:40:00 -0500121 fs->umask = 022;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400122 retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000123 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000124 goto cleanup;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000125 strcpy(fs->device_name, name);
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500126 cp = strchr(fs->device_name, '?');
127 if (!io_options && cp) {
128 *cp++ = 0;
129 io_options = cp;
130 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400131
Theodore Ts'o39c47ce2006-03-18 19:16:10 -0500132 io_flags = 0;
133 if (flags & EXT2_FLAG_RW)
134 io_flags |= IO_FLAG_RW;
135 if (flags & EXT2_FLAG_EXCLUSIVE)
136 io_flags |= IO_FLAG_EXCLUSIVE;
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400137 if (flags & EXT2_FLAG_DIRECT_IO)
138 io_flags |= IO_FLAG_DIRECT_IO;
Theodore Ts'o39c47ce2006-03-18 19:16:10 -0500139 retval = manager->open(fs->device_name, io_flags, &fs->io);
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500140 if (retval)
141 goto cleanup;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400142 if (io_options &&
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500143 (retval = io_channel_set_options(fs->io, io_options)))
144 goto cleanup;
145 fs->image_io = fs->io;
146 fs->io->app_data = fs;
JP Abgralle0ed7402014-03-19 19:08:39 -0700147 retval = io_channel_alloc_buf(fs->io, -SUPERBLOCK_SIZE, &fs->super);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000148 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000149 goto cleanup;
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000150 if (flags & EXT2_FLAG_IMAGE_FILE) {
151 retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr),
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400152 &fs->image_header);
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000153 if (retval)
154 goto cleanup;
155 retval = io_channel_read_blk(fs->io, 0,
Theodore Ts'o85f93ff2006-05-21 19:26:45 -0400156 -(int)sizeof(struct ext2_image_hdr),
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000157 fs->image_header);
158 if (retval)
159 goto cleanup;
160 if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE)
161 return EXT2_ET_MAGIC_E2IMAGE;
162 superblock = 1;
163 block_size = fs->image_header->fs_blocksize;
164 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000165
166 /*
167 * If the user specifies a specific block # for the
168 * superblock, then he/she must also specify the block size!
169 * Otherwise, read the master superblock located at offset
170 * SUPERBLOCK_OFFSET from the start of the partition.
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000171 *
172 * Note: we only save a backup copy of the superblock if we
173 * are reading the superblock from the primary superblock location.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000174 */
175 if (superblock) {
176 if (!block_size) {
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000177 retval = EXT2_ET_INVALID_ARGUMENT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000178 goto cleanup;
179 }
180 io_channel_set_blksize(fs->io, block_size);
Theodore Ts'o9ed06a12002-10-31 11:45:06 -0500181 group_block = superblock;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000182 fs->orig_super = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000183 } else {
184 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
185 superblock = 1;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000186 group_block = 0;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400187 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000188 if (retval)
189 goto cleanup;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000190 }
191 retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
192 fs->super);
193 if (retval)
194 goto cleanup;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000195 if (fs->orig_super)
196 memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000197
Theodore Ts'o126a2912007-08-11 01:56:48 -0400198#ifdef WORDS_BIGENDIAN
199 fs->flags |= EXT2_FLAG_SWAP_BYTES;
200 ext2fs_swap_super(fs->super);
201#else
202 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
203 retval = EXT2_ET_UNIMPLEMENTED;
204 goto cleanup;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000205 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000206#endif
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400207
Theodore Ts'o3839e651997-04-26 13:21:57 +0000208 if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
209 retval = EXT2_ET_BAD_MAGIC;
210 goto cleanup;
211 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000212 if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
213 retval = EXT2_ET_REV_TOO_HIGH;
214 goto cleanup;
215 }
Theodore Ts'oe5b38a52001-01-01 16:17:12 +0000216
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000217 /*
218 * Check for feature set incompatibility
219 */
220 if (!(flags & EXT2_FLAG_FORCE)) {
Theodore Ts'ocf8272e2006-11-12 23:26:46 -0500221 features = fs->super->s_feature_incompat;
222#ifdef EXT2_LIB_SOFTSUPP_INCOMPAT
223 if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
JP Abgralle0ed7402014-03-19 19:08:39 -0700224 features &= ~EXT2_LIB_SOFTSUPP_INCOMPAT;
Theodore Ts'ocf8272e2006-11-12 23:26:46 -0500225#endif
226 if (features & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000227 retval = EXT2_ET_UNSUPP_FEATURE;
228 goto cleanup;
229 }
Theodore Ts'ocf8272e2006-11-12 23:26:46 -0500230
231 features = fs->super->s_feature_ro_compat;
232#ifdef EXT2_LIB_SOFTSUPP_RO_COMPAT
233 if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
JP Abgralle0ed7402014-03-19 19:08:39 -0700234 features &= ~EXT2_LIB_SOFTSUPP_RO_COMPAT;
Theodore Ts'ocf8272e2006-11-12 23:26:46 -0500235#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000236 if ((flags & EXT2_FLAG_RW) &&
Theodore Ts'ocf8272e2006-11-12 23:26:46 -0500237 (features & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000238 retval = EXT2_ET_RO_UNSUPP_FEATURE;
239 goto cleanup;
240 }
Theodore Ts'ocf8272e2006-11-12 23:26:46 -0500241
Theodore Ts'oa1128472001-01-16 06:56:14 +0000242 if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
243 (fs->super->s_feature_incompat &
244 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
245 retval = EXT2_ET_UNSUPP_FEATURE;
246 goto cleanup;
247 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000248 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400249
Manish Katiyar49b46702008-07-11 17:45:07 -0400250 if ((fs->super->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) >
251 EXT2_MAX_BLOCK_LOG_SIZE) {
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000252 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
253 goto cleanup;
254 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700255
256 /*
257 * bigalloc requires cluster-aware bitfield operations, which at the
258 * moment means we need EXT2_FLAG_64BITS.
259 */
260 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
261 EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
262 !(flags & EXT2_FLAG_64BITS)) {
263 retval = EXT2_ET_CANT_USE_LEGACY_BITMAPS;
264 goto cleanup;
265 }
266
267 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
268 EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
269 (fs->super->s_log_block_size != fs->super->s_log_cluster_size)) {
270 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
271 goto cleanup;
272 }
273 fs->fragsize = fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
Theodore Ts'oba9d9292007-09-07 16:40:25 -0400274 if (EXT2_INODE_SIZE(fs->super) < EXT2_GOOD_OLD_INODE_SIZE) {
275 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
276 goto cleanup;
277 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700278 fs->cluster_ratio_bits = fs->super->s_log_cluster_size -
279 fs->super->s_log_block_size;
280 if (EXT2_BLOCKS_PER_GROUP(fs->super) !=
281 EXT2_CLUSTERS_PER_GROUP(fs->super) << fs->cluster_ratio_bits) {
282 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
283 goto cleanup;
284 }
Thiemo Nagel79a9ab12009-01-19 23:16:10 -0500285 fs->inode_blocks_per_group = ((EXT2_INODES_PER_GROUP(fs->super) *
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000286 EXT2_INODE_SIZE(fs->super) +
287 EXT2_BLOCK_SIZE(fs->super) - 1) /
288 EXT2_BLOCK_SIZE(fs->super));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000289 if (block_size) {
290 if (block_size != fs->blocksize) {
291 retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
292 goto cleanup;
293 }
294 }
295 /*
296 * Set the blocksize to the filesystem's blocksize.
297 */
298 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'oa1128472001-01-16 06:56:14 +0000299
300 /*
301 * If this is an external journal device, don't try to read
302 * the group descriptors, because they're not there.
303 */
304 if (fs->super->s_feature_incompat &
305 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
306 fs->group_desc_count = 0;
307 *ret_fs = fs;
308 return 0;
309 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400310
Theodore Ts'o341b52d2009-03-16 22:16:44 -0400311 if (EXT2_INODES_PER_GROUP(fs->super) == 0) {
312 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
313 goto cleanup;
314 }
315
Theodore Ts'o3839e651997-04-26 13:21:57 +0000316 /*
317 * Read group descriptors
318 */
Andreas Dilgerb21bf262002-06-10 11:05:56 -0600319 blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
320 if (blocks_per_group == 0 ||
321 blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
Thiemo Nagel79a9ab12009-01-19 23:16:10 -0500322 fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super) ||
323 EXT2_DESC_PER_BLOCK(fs->super) == 0 ||
JP Abgralle0ed7402014-03-19 19:08:39 -0700324 fs->super->s_first_data_block >= ext2fs_blocks_count(fs->super)) {
Theodore Ts'of0687a51999-05-29 21:48:03 +0000325 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
326 goto cleanup;
327 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700328 fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
329 fs->super->s_first_data_block,
330 blocks_per_group);
331 if (fs->group_desc_count * EXT2_INODES_PER_GROUP(fs->super) !=
332 fs->super->s_inodes_count) {
333 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
Thiemo Nagel79a9ab12009-01-19 23:16:10 -0500334 goto cleanup;
JP Abgralle0ed7402014-03-19 19:08:39 -0700335 }
Theodore Ts'o69022e02006-08-30 01:57:00 -0400336 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
337 EXT2_DESC_PER_BLOCK(fs->super));
Theodore Ts'oee010792007-11-09 19:01:06 -0500338 retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400339 &fs->group_desc);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000340 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000341 goto cleanup;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000342 if (!group_block)
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400343 group_block = fs->super->s_first_data_block;
JP Abgralle0ed7402014-03-19 19:08:39 -0700344 if (group_block == 0 && fs->blocksize == 1024)
345 group_block = 1; /* Deal with 1024 blocksize && bigalloc */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000346 dest = (char *) fs->group_desc;
JP Abgralle0ed7402014-03-19 19:08:39 -0700347#ifdef WORDS_BIGENDIAN
Valerie Clementf2de1d32007-08-30 17:38:13 +0200348 groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
JP Abgralle0ed7402014-03-19 19:08:39 -0700349#endif
Theodore Ts'o8203fe52009-04-23 01:30:42 -0400350 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
351 first_meta_bg = fs->super->s_first_meta_bg;
352 else
353 first_meta_bg = fs->desc_blocks;
354 if (first_meta_bg) {
355 retval = io_channel_read_blk(fs->io, group_block+1,
356 first_meta_bg, dest);
357 if (retval)
358 goto cleanup;
359#ifdef WORDS_BIGENDIAN
360 gdp = (struct ext2_group_desc *) dest;
JP Abgralle0ed7402014-03-19 19:08:39 -0700361 for (j=0; j < groups_per_block*first_meta_bg; j++) {
362 gdp = ext2fs_group_desc(fs, fs->group_desc, j);
363 ext2fs_swap_group_desc2(fs, gdp);
364 }
Theodore Ts'o8203fe52009-04-23 01:30:42 -0400365#endif
366 dest += fs->blocksize*first_meta_bg;
367 }
368 for (i=first_meta_bg ; i < fs->desc_blocks; i++) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700369 blk = ext2fs_descriptor_block_loc2(fs, group_block, i);
370 retval = io_channel_read_blk64(fs->io, blk, 1, dest);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000371 if (retval)
372 goto cleanup;
Theodore Ts'o126a2912007-08-11 01:56:48 -0400373#ifdef WORDS_BIGENDIAN
JP Abgralle0ed7402014-03-19 19:08:39 -0700374 for (j=0; j < groups_per_block; j++) {
375 gdp = ext2fs_group_desc(fs, fs->group_desc,
376 i * groups_per_block + j);
377 ext2fs_swap_group_desc2(fs, gdp);
378 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000379#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000380 dest += fs->blocksize;
381 }
382
Theodore Ts'o96c6a3a2007-05-18 22:06:53 -0400383 fs->stride = fs->super->s_raid_stride;
384
Jose R. Santosd4f34d42007-10-21 21:03:25 -0500385 /*
386 * If recovery is from backup superblock, Clear _UNININT flags &
387 * reset bg_itable_unused to zero
388 */
389 if (superblock > 1 && EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
390 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700391 dgrp_t group;
392
393 for (group = 0; group < fs->group_desc_count; group++) {
394 ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
395 ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
396 ext2fs_bg_itable_unused_set(fs, group, 0);
397 /* The checksum will be reset later, but fix it here
398 * anyway to avoid printing a lot of spurious errors. */
399 ext2fs_group_desc_csum_set(fs, group);
Jose R. Santosd4f34d42007-10-21 21:03:25 -0500400 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700401 if (fs->flags & EXT2_FLAG_RW)
402 ext2fs_mark_super_dirty(fs);
403 }
404
405 if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
406 !(flags & EXT2_FLAG_SKIP_MMP) &&
407 (flags & (EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE))) {
408 retval = ext2fs_mmp_start(fs);
409 if (retval) {
410 fs->flags |= EXT2_FLAG_SKIP_MMP; /* just do cleanup */
411 ext2fs_mmp_stop(fs);
412 goto cleanup;
413 }
Andreas Dilger0f5eba72011-09-24 13:48:55 -0400414 }
415
Theodore Ts'of0257d82013-01-08 20:47:11 -0500416 fs->flags &= ~EXT2_FLAG_NOFREE_ON_ERROR;
417 *ret_fs = fs;
JP Abgralle0ed7402014-03-19 19:08:39 -0700418
Theodore Ts'o3839e651997-04-26 13:21:57 +0000419 return 0;
420cleanup:
Theodore Ts'oab52e122008-02-26 20:45:36 -0500421 if (flags & EXT2_FLAG_NOFREE_ON_ERROR)
422 *ret_fs = fs;
423 else
424 ext2fs_free(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000425 return retval;
426}
427
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400428/*
429 * Set/get the filesystem data I/O channel.
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400430 *
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400431 * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true.
432 */
433errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io)
434{
435 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
436 return EXT2_ET_NOT_IMAGE_FILE;
437 if (old_io) {
438 *old_io = (fs->image_io == fs->io) ? 0 : fs->io;
439 }
440 return 0;
441}
442
443errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io)
444{
445 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
446 return EXT2_ET_NOT_IMAGE_FILE;
447 fs->io = new_io ? new_io : fs->image_io;
448 return 0;
449}
450
451errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io)
452{
JP Abgralle0ed7402014-03-19 19:08:39 -0700453 errcode_t err;
454
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400455 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
456 return EXT2_ET_NOT_IMAGE_FILE;
JP Abgralle0ed7402014-03-19 19:08:39 -0700457 err = io_channel_set_blksize(new_io, fs->blocksize);
458 if (err)
459 return err;
460 if ((new_io == fs->image_io) || (new_io == fs->io))
461 return 0;
462 if ((fs->image_io != fs->io) &&
463 fs->image_io)
464 io_channel_close(fs->image_io);
465 if (fs->io)
466 io_channel_close(fs->io);
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400467 fs->io = fs->image_io = new_io;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400468 fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW |
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400469 EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY;
470 fs->flags &= ~EXT2_FLAG_IMAGE_FILE;
471 return 0;
472}