blob: 35bd44bc0ca122110225f6d8848edb34d7ff7e8a [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * openfs.c --- open an ext2 filesystem
3 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
Theodore Ts'o50e1e101997-04-26 13:58:21 +00005 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00006 * %Begin-Header%
Theodore Ts'o50e1e101997-04-26 13:58:21 +00007 * This file may be redistributed under the terms of the GNU Public
8 * License.
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
Theodore Ts'o3839e651997-04-26 13:21:57 +000025
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000026#include "ext2_fs.h"
Theodore Ts'oc046ac72002-10-20 00:38:57 -040027
28
Theodore Ts'o3839e651997-04-26 13:21:57 +000029#include "ext2fs.h"
Theodore Ts'oa78926e2001-05-03 04:02:29 +000030#include "e2image.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000031
Theodore Ts'oc046ac72002-10-20 00:38:57 -040032blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i)
33{
34 int bg;
35 int has_super = 0;
Theodore Ts'o9ed06a12002-10-31 11:45:06 -050036 int ret_blk;
Theodore Ts'oc046ac72002-10-20 00:38:57 -040037
38 if (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) ||
39 (i < fs->super->s_first_meta_bg))
40 return (group_block + i + 1);
41
42 bg = (fs->blocksize / sizeof (struct ext2_group_desc)) * i;
43 if (ext2fs_bg_has_super(fs, bg))
44 has_super = 1;
Theodore Ts'o9ed06a12002-10-31 11:45:06 -050045 ret_blk = (fs->super->s_first_data_block + has_super +
46 (bg * fs->super->s_blocks_per_group));
47 /*
48 * If group_block is not the normal value, we're trying to use
49 * the backup group descriptors and superblock --- so use the
50 * alternate location of the second block group in the
51 * metablock group. Ideally we should be testing each bg
52 * descriptor block individually for correctness, but we don't
53 * have the infrastructure in place to do that.
54 */
55 if (group_block != fs->super->s_first_data_block &&
56 ((ret_blk + fs->super->s_blocks_per_group) <
57 fs->super->s_blocks_count))
58 ret_blk += fs->super->s_blocks_per_group;
59 return ret_blk;
Theodore Ts'oc046ac72002-10-20 00:38:57 -040060}
61
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050062errcode_t ext2fs_open(const char *name, int flags, int superblock,
63 unsigned int block_size, io_manager manager,
64 ext2_filsys *ret_fs)
65{
66 return ext2fs_open2(name, 0, flags, superblock, block_size,
67 manager, ret_fs);
68}
69
Theodore Ts'o3839e651997-04-26 13:21:57 +000070/*
71 * Note: if superblock is non-zero, block-size must also be non-zero.
72 * Superblock and block_size can be zero to use the default size.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000073 *
74 * Valid flags for ext2fs_open()
75 *
76 * EXT2_FLAG_RW - Open the filesystem for read/write.
77 * EXT2_FLAG_FORCE - Open the filesystem even if some of the
Theodore Ts'oa7773972001-05-13 23:12:10 +000078 * features aren't supported.
79 * EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device
Theodore Ts'o3839e651997-04-26 13:21:57 +000080 */
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050081errcode_t ext2fs_open2(const char *name, const char *io_options,
82 int flags, int superblock,
83 unsigned int block_size, io_manager manager,
84 ext2_filsys *ret_fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +000085{
86 ext2_filsys fs;
87 errcode_t retval;
Theodore Ts'o54434922003-12-07 01:28:50 -050088 unsigned long i;
Theodore Ts'ocf8272e2006-11-12 23:26:46 -050089 __u32 features;
Theodore Ts'o39c47ce2006-03-18 19:16:10 -050090 int j, groups_per_block, blocks_per_group, io_flags;
Theodore Ts'oc046ac72002-10-20 00:38:57 -040091 blk_t group_block, blk;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050092 char *dest, *cp;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000093 struct ext2_group_desc *gdp;
Theodore Ts'o3839e651997-04-26 13:21:57 +000094
Theodore Ts'of3db3561997-04-26 13:34:30 +000095 EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000096
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040097 retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000098 if (retval)
99 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000100
101 memset(fs, 0, sizeof(struct struct_ext2_filsys));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000102 fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103 fs->flags = flags;
Theodore Ts'o058ad1c2007-06-18 18:26:50 -0400104 /* don't overwrite sb backups unless flag is explicitly cleared */
105 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
Theodore Ts'o6a525062001-12-24 09:40:00 -0500106 fs->umask = 022;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400107 retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000108 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000109 goto cleanup;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000110 strcpy(fs->device_name, name);
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500111 cp = strchr(fs->device_name, '?');
112 if (!io_options && cp) {
113 *cp++ = 0;
114 io_options = cp;
115 }
116
Theodore Ts'o39c47ce2006-03-18 19:16:10 -0500117 io_flags = 0;
118 if (flags & EXT2_FLAG_RW)
119 io_flags |= IO_FLAG_RW;
120 if (flags & EXT2_FLAG_EXCLUSIVE)
121 io_flags |= IO_FLAG_EXCLUSIVE;
122 retval = manager->open(fs->device_name, io_flags, &fs->io);
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500123 if (retval)
124 goto cleanup;
125 if (io_options &&
126 (retval = io_channel_set_options(fs->io, io_options)))
127 goto cleanup;
128 fs->image_io = fs->io;
129 fs->io->app_data = fs;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400130 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->super);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000131 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000132 goto cleanup;
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000133 if (flags & EXT2_FLAG_IMAGE_FILE) {
134 retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr),
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400135 &fs->image_header);
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000136 if (retval)
137 goto cleanup;
138 retval = io_channel_read_blk(fs->io, 0,
Theodore Ts'o85f93ff2006-05-21 19:26:45 -0400139 -(int)sizeof(struct ext2_image_hdr),
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000140 fs->image_header);
141 if (retval)
142 goto cleanup;
143 if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE)
144 return EXT2_ET_MAGIC_E2IMAGE;
145 superblock = 1;
146 block_size = fs->image_header->fs_blocksize;
147 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000148
149 /*
150 * If the user specifies a specific block # for the
151 * superblock, then he/she must also specify the block size!
152 * Otherwise, read the master superblock located at offset
153 * SUPERBLOCK_OFFSET from the start of the partition.
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000154 *
155 * Note: we only save a backup copy of the superblock if we
156 * are reading the superblock from the primary superblock location.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157 */
158 if (superblock) {
159 if (!block_size) {
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +0000160 retval = EXT2_ET_INVALID_ARGUMENT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000161 goto cleanup;
162 }
163 io_channel_set_blksize(fs->io, block_size);
Theodore Ts'o9ed06a12002-10-31 11:45:06 -0500164 group_block = superblock;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000165 fs->orig_super = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000166 } else {
167 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
168 superblock = 1;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000169 group_block = 0;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400170 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000171 if (retval)
172 goto cleanup;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000173 }
174 retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
175 fs->super);
176 if (retval)
177 goto cleanup;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000178 if (fs->orig_super)
179 memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000180
Theodore Ts'o126a2912007-08-11 01:56:48 -0400181#ifdef WORDS_BIGENDIAN
182 fs->flags |= EXT2_FLAG_SWAP_BYTES;
183 ext2fs_swap_super(fs->super);
184#else
185 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
186 retval = EXT2_ET_UNIMPLEMENTED;
187 goto cleanup;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000188 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000189#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000190
191 if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
192 retval = EXT2_ET_BAD_MAGIC;
193 goto cleanup;
194 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000195 if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
196 retval = EXT2_ET_REV_TOO_HIGH;
197 goto cleanup;
198 }
Theodore Ts'oe5b38a52001-01-01 16:17:12 +0000199
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000200 /*
201 * Check for feature set incompatibility
202 */
203 if (!(flags & EXT2_FLAG_FORCE)) {
Theodore Ts'ocf8272e2006-11-12 23:26:46 -0500204 features = fs->super->s_feature_incompat;
205#ifdef EXT2_LIB_SOFTSUPP_INCOMPAT
206 if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
207 features &= !EXT2_LIB_SOFTSUPP_INCOMPAT;
208#endif
209 if (features & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000210 retval = EXT2_ET_UNSUPP_FEATURE;
211 goto cleanup;
212 }
Theodore Ts'ocf8272e2006-11-12 23:26:46 -0500213
214 features = fs->super->s_feature_ro_compat;
215#ifdef EXT2_LIB_SOFTSUPP_RO_COMPAT
216 if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
217 features &= !EXT2_LIB_SOFTSUPP_RO_COMPAT;
218#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000219 if ((flags & EXT2_FLAG_RW) &&
Theodore Ts'ocf8272e2006-11-12 23:26:46 -0500220 (features & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000221 retval = EXT2_ET_RO_UNSUPP_FEATURE;
222 goto cleanup;
223 }
Theodore Ts'ocf8272e2006-11-12 23:26:46 -0500224
Theodore Ts'oa1128472001-01-16 06:56:14 +0000225 if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
226 (fs->super->s_feature_incompat &
227 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
228 retval = EXT2_ET_UNSUPP_FEATURE;
229 goto cleanup;
230 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000231 }
232
Theodore Ts'o3839e651997-04-26 13:21:57 +0000233 fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000234 if (fs->blocksize == 0) {
235 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
236 goto cleanup;
237 }
Theodore Ts'oba9d9292007-09-07 16:40:25 -0400238 if (EXT2_INODE_SIZE(fs->super) < EXT2_GOOD_OLD_INODE_SIZE) {
239 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
240 goto cleanup;
241 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000242 fs->fragsize = EXT2_FRAG_SIZE(fs->super);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000243 fs->inode_blocks_per_group = ((fs->super->s_inodes_per_group *
244 EXT2_INODE_SIZE(fs->super) +
245 EXT2_BLOCK_SIZE(fs->super) - 1) /
246 EXT2_BLOCK_SIZE(fs->super));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000247 if (block_size) {
248 if (block_size != fs->blocksize) {
249 retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
250 goto cleanup;
251 }
252 }
253 /*
254 * Set the blocksize to the filesystem's blocksize.
255 */
256 io_channel_set_blksize(fs->io, fs->blocksize);
Theodore Ts'oa1128472001-01-16 06:56:14 +0000257
258 /*
259 * If this is an external journal device, don't try to read
260 * the group descriptors, because they're not there.
261 */
262 if (fs->super->s_feature_incompat &
263 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
264 fs->group_desc_count = 0;
265 *ret_fs = fs;
266 return 0;
267 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000268
269 /*
270 * Read group descriptors
271 */
Andreas Dilgerb21bf262002-06-10 11:05:56 -0600272 blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
273 if (blocks_per_group == 0 ||
274 blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
275 fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super)) {
Theodore Ts'of0687a51999-05-29 21:48:03 +0000276 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
277 goto cleanup;
278 }
Theodore Ts'o69022e02006-08-30 01:57:00 -0400279 fs->group_desc_count = ext2fs_div_ceil(fs->super->s_blocks_count -
280 fs->super->s_first_data_block,
281 blocks_per_group);
282 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
283 EXT2_DESC_PER_BLOCK(fs->super));
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000284 retval = ext2fs_get_mem(fs->desc_blocks * fs->blocksize,
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400285 &fs->group_desc);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000286 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000287 goto cleanup;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000288 if (!group_block)
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400289 group_block = fs->super->s_first_data_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000290 dest = (char *) fs->group_desc;
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400291 groups_per_block = fs->blocksize / sizeof(struct ext2_group_desc);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000292 for (i=0 ; i < fs->desc_blocks; i++) {
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400293 blk = ext2fs_descriptor_block_loc(fs, group_block, i);
294 retval = io_channel_read_blk(fs->io, blk, 1, dest);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000295 if (retval)
296 goto cleanup;
Theodore Ts'o126a2912007-08-11 01:56:48 -0400297#ifdef WORDS_BIGENDIAN
298 gdp = (struct ext2_group_desc *) dest;
299 for (j=0; j < groups_per_block; j++)
300 ext2fs_swap_group_desc(gdp++);
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000301#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000302 dest += fs->blocksize;
303 }
304
Theodore Ts'o96c6a3a2007-05-18 22:06:53 -0400305 fs->stride = fs->super->s_raid_stride;
306
Theodore Ts'o3839e651997-04-26 13:21:57 +0000307 *ret_fs = fs;
308 return 0;
309cleanup:
310 ext2fs_free(fs);
311 return retval;
312}
313
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400314/*
315 * Set/get the filesystem data I/O channel.
316 *
317 * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true.
318 */
319errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io)
320{
321 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
322 return EXT2_ET_NOT_IMAGE_FILE;
323 if (old_io) {
324 *old_io = (fs->image_io == fs->io) ? 0 : fs->io;
325 }
326 return 0;
327}
328
329errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io)
330{
331 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
332 return EXT2_ET_NOT_IMAGE_FILE;
333 fs->io = new_io ? new_io : fs->image_io;
334 return 0;
335}
336
337errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io)
338{
339 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
340 return EXT2_ET_NOT_IMAGE_FILE;
341 fs->io = fs->image_io = new_io;
342 fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW |
343 EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY;
344 fs->flags &= ~EXT2_FLAG_IMAGE_FILE;
345 return 0;
346}