Whamcloud - gitweb
Change fast ext2fs bitmap functions to use the generic bitmap functions
[tools/e2fsprogs.git] / lib / ext2fs / openfs.c
1 /*
2  * openfs.c --- open an ext2 filesystem
3  * 
4  * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
5  * 
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <fcntl.h>
18 #include <time.h>
19 #if HAVE_SYS_STAT_H
20 #include <sys/stat.h>
21 #endif
22 #if HAVE_SYS_TYPES_H
23 #include <sys/types.h>
24 #endif
25
26 #include "ext2_fs.h"
27
28
29 #include "ext2fs.h"
30 #include "e2image.h"
31
32 blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i)
33 {
34         int     bg;
35         int     has_super = 0;
36         int     ret_blk;
37
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;
45         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;
60 }
61
62 errcode_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
70 /*
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.
73  *
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
78  *                              features aren't supported.
79  *      EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device
80  */
81 errcode_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)
85 {
86         ext2_filsys     fs;
87         errcode_t       retval;
88         unsigned long   i;
89         __u32           features;
90         int             j, groups_per_block, blocks_per_group, io_flags;
91         blk_t           group_block, blk;
92         char            *dest, *cp;
93         struct ext2_group_desc *gdp;
94         
95         EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
96
97         retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
98         if (retval)
99                 return retval;
100         
101         memset(fs, 0, sizeof(struct struct_ext2_filsys));
102         fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
103         fs->flags = flags;
104         /* don't overwrite sb backups unless flag is explicitly cleared */
105         fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
106         fs->umask = 022;
107         retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
108         if (retval)
109                 goto cleanup;
110         strcpy(fs->device_name, name);
111         cp = strchr(fs->device_name, '?');
112         if (!io_options && cp) {
113                 *cp++ = 0;
114                 io_options = cp;
115         }
116                 
117         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);
123         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;
130         retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->super);
131         if (retval)
132                 goto cleanup;
133         if (flags & EXT2_FLAG_IMAGE_FILE) {
134                 retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr),
135                                         &fs->image_header);
136                 if (retval)
137                         goto cleanup;
138                 retval = io_channel_read_blk(fs->io, 0,
139                                              -(int)sizeof(struct ext2_image_hdr),
140                                              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         }
148
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.
154          *
155          * Note: we only save a backup copy of the superblock if we
156          * are reading the superblock from the primary superblock location.
157          */
158         if (superblock) {
159                 if (!block_size) {
160                         retval = EXT2_ET_INVALID_ARGUMENT;
161                         goto cleanup;
162                 }
163                 io_channel_set_blksize(fs->io, block_size);
164                 group_block = superblock;
165                 fs->orig_super = 0;
166         } else {
167                 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
168                 superblock = 1;
169                 group_block = 0;
170                 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
171                 if (retval)
172                         goto cleanup;
173         }
174         retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
175                                      fs->super);
176         if (retval)
177                 goto cleanup;
178         if (fs->orig_super)
179                 memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);
180
181 #ifdef EXT2FS_ENABLE_SWAPFS
182         if ((fs->super->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) ||
183             (fs->flags & EXT2_FLAG_SWAP_BYTES)) {
184                 fs->flags |= EXT2_FLAG_SWAP_BYTES;
185
186                 ext2fs_swap_super(fs->super);
187         }
188 #endif
189         
190         if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
191                 retval = EXT2_ET_BAD_MAGIC;
192                 goto cleanup;
193         }
194         if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
195                 retval = EXT2_ET_REV_TOO_HIGH;
196                 goto cleanup;
197         }
198
199         /*
200          * Check for feature set incompatibility
201          */
202         if (!(flags & EXT2_FLAG_FORCE)) {
203                 features = fs->super->s_feature_incompat;
204 #ifdef EXT2_LIB_SOFTSUPP_INCOMPAT
205                 if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
206                         features &= !EXT2_LIB_SOFTSUPP_INCOMPAT;
207 #endif
208                 if (features & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
209                         retval = EXT2_ET_UNSUPP_FEATURE;
210                         goto cleanup;
211                 }
212
213                 features = fs->super->s_feature_ro_compat;
214 #ifdef EXT2_LIB_SOFTSUPP_RO_COMPAT
215                 if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
216                         features &= !EXT2_LIB_SOFTSUPP_RO_COMPAT;
217 #endif
218                 if ((flags & EXT2_FLAG_RW) &&
219                     (features & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
220                         retval = EXT2_ET_RO_UNSUPP_FEATURE;
221                         goto cleanup;
222                 }
223
224                 if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
225                     (fs->super->s_feature_incompat &
226                      EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
227                         retval = EXT2_ET_UNSUPP_FEATURE;
228                         goto cleanup;
229                 }
230         }
231         
232         fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
233         if (fs->blocksize == 0) {
234                 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
235                 goto cleanup;
236         }
237         fs->fragsize = EXT2_FRAG_SIZE(fs->super);
238         fs->inode_blocks_per_group = ((fs->super->s_inodes_per_group *
239                                        EXT2_INODE_SIZE(fs->super) +
240                                        EXT2_BLOCK_SIZE(fs->super) - 1) /
241                                       EXT2_BLOCK_SIZE(fs->super));
242         if (block_size) {
243                 if (block_size != fs->blocksize) {
244                         retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
245                         goto cleanup;
246                 }
247         }
248         /*
249          * Set the blocksize to the filesystem's blocksize.
250          */
251         io_channel_set_blksize(fs->io, fs->blocksize);
252
253         /*
254          * If this is an external journal device, don't try to read
255          * the group descriptors, because they're not there.
256          */
257         if (fs->super->s_feature_incompat &
258             EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
259                 fs->group_desc_count = 0;
260                 *ret_fs = fs;
261                 return 0;
262         }
263         
264         /*
265          * Read group descriptors
266          */
267         blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
268         if (blocks_per_group == 0 ||
269             blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
270             fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super)) {
271                 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
272                 goto cleanup;
273         }
274         fs->group_desc_count = ext2fs_div_ceil(fs->super->s_blocks_count -
275                                                fs->super->s_first_data_block,
276                                                blocks_per_group);
277         fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
278                                           EXT2_DESC_PER_BLOCK(fs->super));
279         retval = ext2fs_get_mem(fs->desc_blocks * fs->blocksize,
280                                 &fs->group_desc);
281         if (retval)
282                 goto cleanup;
283         if (!group_block)
284                 group_block = fs->super->s_first_data_block;
285         dest = (char *) fs->group_desc;
286         groups_per_block = fs->blocksize / sizeof(struct ext2_group_desc);
287         for (i=0 ; i < fs->desc_blocks; i++) {
288                 blk = ext2fs_descriptor_block_loc(fs, group_block, i);
289                 retval = io_channel_read_blk(fs->io, blk, 1, dest);
290                 if (retval)
291                         goto cleanup;
292 #ifdef EXT2FS_ENABLE_SWAPFS
293                 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
294                         gdp = (struct ext2_group_desc *) dest;
295                         for (j=0; j < groups_per_block; j++)
296                                 ext2fs_swap_group_desc(gdp++);
297                 }
298 #endif
299                 dest += fs->blocksize;
300         }
301
302         fs->stride = fs->super->s_raid_stride;
303
304         *ret_fs = fs;
305         return 0;
306 cleanup:
307         ext2fs_free(fs);
308         return retval;
309 }
310
311 /*
312  * Set/get the filesystem data I/O channel.
313  * 
314  * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true.
315  */
316 errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io)
317 {
318         if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
319                 return EXT2_ET_NOT_IMAGE_FILE;
320         if (old_io) {
321                 *old_io = (fs->image_io == fs->io) ? 0 : fs->io;
322         }
323         return 0;
324 }
325
326 errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io)
327 {
328         if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
329                 return EXT2_ET_NOT_IMAGE_FILE;
330         fs->io = new_io ? new_io : fs->image_io;
331         return 0;
332 }
333
334 errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io)
335 {
336         if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
337                 return EXT2_ET_NOT_IMAGE_FILE;
338         fs->io = fs->image_io = new_io;
339         fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW | 
340                 EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY;
341         fs->flags &= ~EXT2_FLAG_IMAGE_FILE;
342         return 0;
343 }