4 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
9 * Where the master copy of the superblock is located, and how big
10 * superblocks are supposed to be. We define SUPERBLOCK_SIZE because
11 * the size of the superblock structure is not necessarily trustworthy
12 * (some versions have the padding set up so that the superblock is
15 #define SUPERBLOCK_OFFSET 1024
16 #define SUPERBLOCK_SIZE 1024
19 * The last ext2fs revision level that this version of the library is
22 #define EXT2_LIB_CURRENT_REV 0
24 typedef unsigned long blk_t;
25 typedef unsigned int dgrp_t;
27 #include "et/com_err.h"
28 #include "ext2fs/io.h"
29 #include "ext2fs/ext2_err.h"
31 typedef struct struct_ext2_filsys *ext2_filsys;
33 struct ext2fs_struct_inode_bitmap {
43 typedef struct ext2fs_struct_inode_bitmap *ext2fs_inode_bitmap;
45 struct ext2fs_struct_block_bitmap {
55 typedef struct ext2fs_struct_block_bitmap *ext2fs_block_bitmap;
58 * Flags for the ext2_filsys structure
61 #define EXT2_FLAG_RW 0x01
62 #define EXT2_FLAG_CHANGED 0x02
63 #define EXT2_FLAG_DIRTY 0x04
64 #define EXT2_FLAG_VALID 0x08
65 #define EXT2_FLAG_IB_DIRTY 0x10
66 #define EXT2_FLAG_BB_DIRTY 0x20
68 struct struct_ext2_filsys {
73 struct ext2_super_block * super;
76 unsigned long group_desc_count;
77 unsigned long desc_blocks;
78 struct ext2_group_desc * group_desc;
79 int inode_blocks_per_group;
80 ext2fs_inode_bitmap inode_map;
81 ext2fs_block_bitmap block_map;
82 errcode_t (*get_blocks)(ext2_filsys fs, ino_t ino, blk_t *blocks);
83 errcode_t (*check_directory)(ext2_filsys fs, ino_t ino);
84 errcode_t (*write_bitmaps)(ext2_filsys fs);
88 * Not used by ext2fs library; reserved for the use of the
89 * calling application.
95 * badblocks list definitions
98 typedef struct struct_badblocks_list *badblocks_list;
100 struct struct_badblocks_list {
109 #define BADBLOCKS_FLAG_DIRTY 1
111 typedef struct struct_badblocks_iterate *badblocks_iterate;
113 struct struct_badblocks_iterate {
120 #include "ext2fs/bitops.h"
123 * Return flags for the block iterator functions
125 #define BLOCK_CHANGED 1
126 #define BLOCK_ABORT 2
127 #define BLOCK_ERROR 4
130 * Block interate flags
132 #define BLOCK_FLAG_APPEND 1
133 #define BLOCK_FLAG_DEPTH_TRAVERSE 2
136 * Return flags for the directory iterator functions
138 #define DIRENT_CHANGED 1
139 #define DIRENT_ABORT 2
140 #define DIRENT_ERROR 3
143 * Directory iterator flags
146 #define DIRENT_FLAG_INCLUDE_EMPTY 1
149 * Inode scan definitions
151 typedef struct ext2_struct_inode_scan *ext2_inode_scan;
153 struct ext2_struct_inode_scan {
158 dgrp_t current_group;
159 int inodes_left, blocks_left, groups_left;
160 int inode_buffer_blocks;
162 struct ext2_inode * inode_scan_ptr;
163 errcode_t (*done_group)(ext2_filsys fs,
164 ext2_inode_scan scan,
167 void * done_group_data;
172 * For checking structure magic numbers...
175 #define EXT2_CHECK_MAGIC(struct, code) \
176 if ((struct)->magic != (code)) return (code)
179 * function prototypes
183 extern errcode_t ext2fs_new_inode(ext2_filsys fs, ino_t dir, int mode,
184 ext2fs_inode_bitmap map, ino_t *ret);
185 extern errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
186 ext2fs_block_bitmap map, blk_t *ret);
187 extern errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start,
188 blk_t finish, int num,
189 ext2fs_block_bitmap map,
193 extern errcode_t badblocks_list_create(badblocks_list *ret, int size);
194 extern void badblocks_list_free(badblocks_list bb);
195 extern errcode_t badblocks_list_add(badblocks_list bb, blk_t blk);
196 extern int badblocks_list_test(badblocks_list bb, blk_t blk);
197 extern errcode_t badblocks_list_iterate_begin(badblocks_list bb,
198 badblocks_iterate *ret);
199 extern int badblocks_list_iterate(badblocks_iterate iter, blk_t *blk);
200 extern void badblocks_list_iterate_end(badblocks_iterate iter);
203 extern errcode_t ext2fs_update_bb_inode(ext2_filsys fs,
204 badblocks_list bb_list);
207 extern errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs);
208 extern errcode_t ext2fs_write_block_bitmap (ext2_filsys fs);
209 extern errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs);
210 extern errcode_t ext2fs_read_block_bitmap(ext2_filsys fs);
211 errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
213 ext2fs_block_bitmap *ret);
214 errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
216 ext2fs_inode_bitmap *ret);
217 errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
218 ino_t end, ino_t *oend);
219 errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
220 blk_t end, blk_t *oend);
221 void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap);
222 void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap);
223 void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap);
224 void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap);
225 extern errcode_t ext2fs_read_bitmaps(ext2_filsys fs);
226 extern errcode_t ext2fs_write_bitmaps(ext2_filsys fs);
229 extern errcode_t ext2fs_block_iterate(ext2_filsys fs,
233 int (*func)(ext2_filsys fs,
240 extern errcode_t ext2fs_check_desc(ext2_filsys fs);
243 extern errcode_t ext2fs_close(ext2_filsys fs);
244 extern errcode_t ext2fs_flush(ext2_filsys fs);
247 extern errcode_t ext2fs_expand_dir(ext2_filsys fs, ino_t dir);
250 extern void ext2fs_free(ext2_filsys fs);
253 extern errcode_t ext2fs_initialize(const char *name, int flags,
254 struct ext2_super_block *param,
255 io_manager manager, ext2_filsys *ret_fs);
258 extern errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
259 ext2_inode_scan *ret_scan);
260 extern void ext2fs_close_inode_scan(ext2_inode_scan scan);
261 extern errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ino_t *ino,
262 struct ext2_inode *inode);
263 void ext2fs_set_inode_callback(ext2_inode_scan scan,
264 errcode_t (*done_group)(ext2_filsys fs,
265 ext2_inode_scan scan,
268 void *done_group_data);
269 void ext2fs_set_inode_callback(ext2_inode_scan scan,
270 errcode_t (*done_group)(ext2_filsys fs,
271 ext2_inode_scan scan,
274 void *done_group_data);
275 extern errcode_t ext2fs_read_inode (ext2_filsys fs, unsigned long ino,
276 struct ext2_inode * inode);
277 extern errcode_t ext2fs_write_inode(ext2_filsys fs, unsigned long ino,
278 struct ext2_inode * inode);
279 extern errcode_t ext2fs_get_blocks(ext2_filsys fs, ino_t ino, blk_t *blocks);
280 extern errcode_t ext2fs_check_directory(ext2_filsys fs, ino_t ino);
283 extern errcode_t ext2fs_dir_iterate(ext2_filsys fs,
287 int (*func)(struct ext2_dir_entry *dirent,
293 extern errcode_t ext2fs_lookup(ext2_filsys fs, ino_t dir, const char *name,
294 int namelen, char *buf, ino_t *inode);
295 extern errcode_t ext2fs_namei(ext2_filsys fs, ino_t root, ino_t cwd,
296 const char *name, ino_t *inode);
299 extern errcode_t ext2fs_new_dir_block(ext2_filsys fs, ino_t dir_ino,
300 ino_t parent_ino, char **block);
303 extern errcode_t ext2fs_mkdir(ext2_filsys fs, ino_t parent, ino_t inum,
307 extern errcode_t ext2fs_open(const char *name, int flags, int superblock,
308 int block_size, io_manager manager,
309 ext2_filsys *ret_fs);
312 extern errcode_t ext2fs_get_pathname(ext2_filsys fs, ino_t dir, ino_t ino,
316 errcode_t ext2fs_link(ext2_filsys fs, ino_t dir, const char *name,
317 ino_t ino, int flags);
318 errcode_t ext2fs_unlink(ext2_filsys fs, ino_t dir, const char *name,
319 ino_t ino, int flags);
322 extern errcode_t ext2fs_read_bb_inode(ext2_filsys fs, badblocks_list *bb_list);
325 extern errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f,
326 badblocks_list *bb_list,
327 void (*invalid)(ext2_filsys fs,
330 /* inline functions */
331 extern void ext2fs_mark_super_dirty(ext2_filsys fs);
332 extern void ext2fs_mark_changed(ext2_filsys fs);
333 extern int ext2fs_test_changed(ext2_filsys fs);
334 extern void ext2fs_mark_valid(ext2_filsys fs);
335 extern void ext2fs_unmark_valid(ext2_filsys fs);
336 extern int ext2fs_test_valid(ext2_filsys fs);
337 extern void ext2fs_mark_ib_dirty(ext2_filsys fs);
338 extern void ext2fs_mark_bb_dirty(ext2_filsys fs);
339 extern int ext2fs_test_ib_dirty(ext2_filsys fs);
340 extern int ext2fs_test_bb_dirty(ext2_filsys fs);
341 extern int ext2fs_group_of_blk(ext2_filsys fs, blk_t blk);
342 extern int ext2fs_group_of_ino(ext2_filsys fs, ino_t ino);
345 * The actual inlined functions definitions themselves...
347 * If NO_INLINE_FUNCS is defined, then we won't try to do inline
350 #if (defined(INCLUDE_INLINE_FUNCS) || !defined(NO_INLINE_FUNCS))
351 #ifdef INCLUDE_INLINE_FUNCS
352 #define _INLINE_ extern
354 #define _INLINE_ extern __inline__
358 * Mark a filesystem superblock as dirty
360 _INLINE_ void ext2fs_mark_super_dirty(ext2_filsys fs)
362 fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_CHANGED;
366 * Mark a filesystem as changed
368 _INLINE_ void ext2fs_mark_changed(ext2_filsys fs)
370 fs->flags |= EXT2_FLAG_CHANGED;
374 * Check to see if a filesystem has changed
376 _INLINE_ int ext2fs_test_changed(ext2_filsys fs)
378 return (fs->flags & EXT2_FLAG_CHANGED);
382 * Mark a filesystem as valid
384 _INLINE_ void ext2fs_mark_valid(ext2_filsys fs)
386 fs->flags |= EXT2_FLAG_VALID;
390 * Mark a filesystem as NOT valid
392 _INLINE_ void ext2fs_unmark_valid(ext2_filsys fs)
394 fs->flags &= ~EXT2_FLAG_VALID;
398 * Check to see if a filesystem is valid
400 _INLINE_ int ext2fs_test_valid(ext2_filsys fs)
402 return (fs->flags & EXT2_FLAG_VALID);
406 * Mark the inode bitmap as dirty
408 _INLINE_ void ext2fs_mark_ib_dirty(ext2_filsys fs)
410 fs->flags |= EXT2_FLAG_IB_DIRTY | EXT2_FLAG_CHANGED;
414 * Mark the block bitmap as dirty
416 _INLINE_ void ext2fs_mark_bb_dirty(ext2_filsys fs)
418 fs->flags |= EXT2_FLAG_BB_DIRTY | EXT2_FLAG_CHANGED;
422 * Check to see if a filesystem's inode bitmap is dirty
424 _INLINE_ int ext2fs_test_ib_dirty(ext2_filsys fs)
426 return (fs->flags & EXT2_FLAG_IB_DIRTY);
430 * Check to see if a filesystem's block bitmap is dirty
432 _INLINE_ int ext2fs_test_bb_dirty(ext2_filsys fs)
434 return (fs->flags & EXT2_FLAG_BB_DIRTY);
438 * Return the group # of a block
440 _INLINE_ int ext2fs_group_of_blk(ext2_filsys fs, blk_t blk)
442 return (blk - fs->super->s_first_data_block) /
443 fs->super->s_blocks_per_group;
447 * Return the group # of an inode number
449 _INLINE_ int ext2fs_group_of_ino(ext2_filsys fs, ino_t ino)
451 return (ino - 1) / fs->super->s_inodes_per_group;