ext2fs.h 11.7 KB
Newer Older
Theodore Ts'o's avatar
Theodore Ts'o committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * ext2fs.h --- ext2fs
 * 
 * Copyright (C) 1993 Theodore Ts'o.  This file may be redistributed
 * under the terms of the GNU Public License.
 */

/*
 * Where the master copy of the superblock is located, and how big
 * superblocks are supposed to be.  We define SUPERBLOCK_SIZE because
 * the size of the superblock structure is not necessarily trustworthy
 * (some versions have the padding set up so that the superblock is
 * 1032 bytes long).
 */
#define SUPERBLOCK_OFFSET	1024
#define SUPERBLOCK_SIZE 	1024

Theodore Ts'o's avatar
Theodore Ts'o committed
18 19 20 21 22 23
/*
 * The last ext2fs revision level that this version of the library is
 * able to support.
 */
#define EXT2_LIB_CURRENT_REV	0

Theodore Ts'o's avatar
Theodore Ts'o committed
24 25 26 27 28 29 30
typedef unsigned long	blk_t;
typedef unsigned int	dgrp_t;

#include "et/com_err.h"
#include "ext2fs/io.h"
#include "ext2fs/ext2_err.h"

Theodore Ts'o's avatar
Theodore Ts'o committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
typedef struct struct_ext2_filsys *ext2_filsys;

struct ext2fs_struct_inode_bitmap {
	int	magic;
	ext2_filsys fs;
	ino_t	start, end;
	ino_t	real_end;
	char	*description;
	char	*bitmap;
	int	reserved[8];
};

typedef struct ext2fs_struct_inode_bitmap *ext2fs_inode_bitmap;

struct ext2fs_struct_block_bitmap {
	int	magic;
	ext2_filsys fs;
	blk_t	start, end;
	ino_t	real_end;
	char	*description;
	char	*bitmap;
	int	reserved[8];
};

typedef struct ext2fs_struct_block_bitmap *ext2fs_block_bitmap;

Theodore Ts'o's avatar
Theodore Ts'o committed
57 58 59 60 61 62 63 64 65 66 67 68
/*
 * Flags for the ext2_filsys structure
 */

#define EXT2_FLAG_RW		0x01
#define EXT2_FLAG_CHANGED	0x02
#define EXT2_FLAG_DIRTY		0x04
#define EXT2_FLAG_VALID		0x08
#define EXT2_FLAG_IB_DIRTY	0x10
#define EXT2_FLAG_BB_DIRTY	0x20

struct struct_ext2_filsys {
Theodore Ts'o's avatar
Theodore Ts'o committed
69
	int				magic;
Theodore Ts'o's avatar
Theodore Ts'o committed
70 71 72 73 74 75 76 77 78 79
	io_channel			io;
	int				flags;
	char *				device_name;
	struct ext2_super_block	* 	super;
	int				blocksize;
	int				fragsize;
	unsigned long			group_desc_count;
	unsigned long			desc_blocks;
	struct ext2_group_desc *	group_desc;
	int				inode_blocks_per_group;
Theodore Ts'o's avatar
Theodore Ts'o committed
80 81
	ext2fs_inode_bitmap		inode_map;
	ext2fs_block_bitmap		block_map;
Theodore Ts'o's avatar
Theodore Ts'o committed
82 83 84
	errcode_t (*get_blocks)(ext2_filsys fs, ino_t ino, blk_t *blocks);
	errcode_t (*check_directory)(ext2_filsys fs, ino_t ino);
	errcode_t (*write_bitmaps)(ext2_filsys fs);
Theodore Ts'o's avatar
Theodore Ts'o committed
85
	int				reserved[16];
Theodore Ts'o's avatar
Theodore Ts'o committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

	/*
	 * Not used by ext2fs library; reserved for the use of the
	 * calling application.
	 */
	void *				private; 
};

/*
 * badblocks list definitions
 */

typedef struct struct_badblocks_list *badblocks_list;

struct struct_badblocks_list {
Theodore Ts'o's avatar
Theodore Ts'o committed
101
	int	magic;
Theodore Ts'o's avatar
Theodore Ts'o committed
102 103 104 105
	int	num;
	int	size;
	blk_t	*list;
	int	badblocks_flags;
Theodore Ts'o's avatar
Theodore Ts'o committed
106
	int	reserved[8];
Theodore Ts'o's avatar
Theodore Ts'o committed
107 108 109 110 111 112 113
};

#define BADBLOCKS_FLAG_DIRTY	1

typedef struct struct_badblocks_iterate *badblocks_iterate;

struct struct_badblocks_iterate {
Theodore Ts'o's avatar
Theodore Ts'o committed
114
	int		magic;
Theodore Ts'o's avatar
Theodore Ts'o committed
115 116
	badblocks_list	bb;
	int		ptr;
Theodore Ts'o's avatar
Theodore Ts'o committed
117
	int	reserved[8];
Theodore Ts'o's avatar
Theodore Ts'o committed
118 119
};

Theodore Ts'o's avatar
Theodore Ts'o committed
120 121
#include "ext2fs/bitops.h"
	
Theodore Ts'o's avatar
Theodore Ts'o committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
/*
 * Return flags for the block iterator functions
 */
#define BLOCK_CHANGED	1
#define BLOCK_ABORT	2
#define BLOCK_ERROR	4

/*
 * Block interate flags
 */
#define BLOCK_FLAG_APPEND	1
#define BLOCK_FLAG_DEPTH_TRAVERSE	2

/*
 * Return flags for the directory iterator functions
 */
#define DIRENT_CHANGED	1
#define DIRENT_ABORT	2
#define DIRENT_ERROR	3

/*
 * Directory iterator flags
 */

#define DIRENT_FLAG_INCLUDE_EMPTY	1

/*
 * Inode scan definitions
 */
Theodore Ts'o's avatar
Theodore Ts'o committed
151 152
typedef struct ext2_struct_inode_scan *ext2_inode_scan;

Theodore Ts'o's avatar
Theodore Ts'o committed
153
struct ext2_struct_inode_scan {
Theodore Ts'o's avatar
Theodore Ts'o committed
154
	int			magic;
Theodore Ts'o's avatar
Theodore Ts'o committed
155 156 157 158 159 160 161 162
	ext2_filsys		fs;
	ino_t			current_inode;
	blk_t			current_block;
	dgrp_t			current_group;
	int			inodes_left, blocks_left, groups_left;
	int			inode_buffer_blocks;
	char *			inode_buffer;
	struct ext2_inode *	inode_scan_ptr;
Theodore Ts'o's avatar
Theodore Ts'o committed
163 164 165 166 167 168
	errcode_t		(*done_group)(ext2_filsys fs,
					      ext2_inode_scan scan,
					      dgrp_t group,
					      void * private);
	void *			done_group_data;
	int			reserved[8];
Theodore Ts'o's avatar
Theodore Ts'o committed
169 170
};

Theodore Ts'o's avatar
Theodore Ts'o committed
171 172 173
/*
 * For checking structure magic numbers...
 */
Theodore Ts'o's avatar
Theodore Ts'o committed
174

Theodore Ts'o's avatar
Theodore Ts'o committed
175 176 177
#define EXT2_CHECK_MAGIC(struct, code) \
	  if ((struct)->magic != (code)) return (code)
  
Theodore Ts'o's avatar
Theodore Ts'o committed
178 179 180 181 182 183
/*
 * function prototypes
 */

/* alloc.c */
extern errcode_t ext2fs_new_inode(ext2_filsys fs, ino_t dir, int mode,
Theodore Ts'o's avatar
Theodore Ts'o committed
184
				  ext2fs_inode_bitmap map, ino_t *ret);
Theodore Ts'o's avatar
Theodore Ts'o committed
185
extern errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
Theodore Ts'o's avatar
Theodore Ts'o committed
186
				  ext2fs_block_bitmap map, blk_t *ret);
Theodore Ts'o's avatar
Theodore Ts'o committed
187
extern errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start,
Theodore Ts'o's avatar
Theodore Ts'o committed
188 189
					blk_t finish, int num,
					ext2fs_block_bitmap map,
Theodore Ts'o's avatar
Theodore Ts'o committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
					blk_t *ret);

/* badblocks.c */
extern errcode_t badblocks_list_create(badblocks_list *ret, int size);
extern void badblocks_list_free(badblocks_list bb);
extern errcode_t badblocks_list_add(badblocks_list bb, blk_t blk);
extern int badblocks_list_test(badblocks_list bb, blk_t blk);
extern errcode_t badblocks_list_iterate_begin(badblocks_list bb,
					      badblocks_iterate *ret);
extern int badblocks_list_iterate(badblocks_iterate iter, blk_t *blk);
extern void badblocks_list_iterate_end(badblocks_iterate iter);

/* bb_inode.c */
extern errcode_t ext2fs_update_bb_inode(ext2_filsys fs,
					badblocks_list bb_list);

/* bitmaps.c */
extern errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs);
extern errcode_t ext2fs_write_block_bitmap (ext2_filsys fs);
extern errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs);
extern errcode_t ext2fs_read_block_bitmap(ext2_filsys fs);
Theodore Ts'o's avatar
Theodore Ts'o committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224
errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
				       const char *descr,
				       ext2fs_block_bitmap *ret);
errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
				       const char *descr,
				       ext2fs_inode_bitmap *ret);
errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
					ino_t end, ino_t *oend);
errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
					blk_t end, blk_t *oend);
void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap);
void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap);
void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap);
void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap);
Theodore Ts'o's avatar
Theodore Ts'o committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238
extern errcode_t ext2fs_read_bitmaps(ext2_filsys fs);
extern errcode_t ext2fs_write_bitmaps(ext2_filsys fs);

/* block.c */
extern errcode_t ext2fs_block_iterate(ext2_filsys fs,
				      ino_t	ino,
				      int	flags,
				      char *block_buf,
				      int (*func)(ext2_filsys fs,
						  blk_t	*blocknr,
						  int	blockcnt,
						  void	*private),
				      void *private);

Theodore Ts'o's avatar
Theodore Ts'o committed
239 240 241
/* check_desc.c */
extern errcode_t ext2fs_check_desc(ext2_filsys fs);

Theodore Ts'o's avatar
Theodore Ts'o committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
/* closefs.c */
extern errcode_t ext2fs_close(ext2_filsys fs);
extern errcode_t ext2fs_flush(ext2_filsys fs);

/* expanddir.c */
extern errcode_t ext2fs_expand_dir(ext2_filsys fs, ino_t dir);

/* freefs.c */
extern void ext2fs_free(ext2_filsys fs);

/* initialize.c */
extern errcode_t ext2fs_initialize(const char *name, int flags,
				   struct ext2_super_block *param,
				   io_manager manager, ext2_filsys *ret_fs);

/* inode.c */
extern errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
				  ext2_inode_scan *ret_scan);
extern void ext2fs_close_inode_scan(ext2_inode_scan scan);
extern errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ino_t *ino,
			       struct ext2_inode *inode);
Theodore Ts'o's avatar
Theodore Ts'o committed
263 264 265 266 267 268 269 270 271 272 273 274
void ext2fs_set_inode_callback(ext2_inode_scan scan,
			       errcode_t (*done_group)(ext2_filsys fs,
						       ext2_inode_scan scan,
						       dgrp_t group,
						       void * private),
			       void *done_group_data);
void ext2fs_set_inode_callback(ext2_inode_scan scan,
			       errcode_t (*done_group)(ext2_filsys fs,
						       ext2_inode_scan scan,
						       dgrp_t group,
						       void * private),
			       void *done_group_data);
Theodore Ts'o's avatar
Theodore Ts'o committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
extern errcode_t ext2fs_read_inode (ext2_filsys fs, unsigned long ino,
			    struct ext2_inode * inode);
extern errcode_t ext2fs_write_inode(ext2_filsys fs, unsigned long ino,
			    struct ext2_inode * inode);
extern errcode_t ext2fs_get_blocks(ext2_filsys fs, ino_t ino, blk_t *blocks);
extern errcode_t ext2fs_check_directory(ext2_filsys fs, ino_t ino);

/* namei.c */
extern errcode_t ext2fs_dir_iterate(ext2_filsys fs, 
			      ino_t dir,
			      int flags,
			      char *block_buf,
			      int (*func)(struct ext2_dir_entry *dirent,
					  int	offset,
					  int	blocksize,
					  char	*buf,
					  void	*private),
			      void *private);
extern errcode_t ext2fs_lookup(ext2_filsys fs, ino_t dir, const char *name,
			 int namelen, char *buf, ino_t *inode);
extern errcode_t ext2fs_namei(ext2_filsys fs, ino_t root, ino_t cwd,
			const char *name, ino_t *inode);

/* newdir.c */
extern errcode_t ext2fs_new_dir_block(ext2_filsys fs, ino_t dir_ino,
				ino_t parent_ino, char **block);

/* mkdir.c */
extern errcode_t ext2fs_mkdir(ext2_filsys fs, ino_t parent, ino_t inum,
			      const char *name);

/* openfs.c */
extern errcode_t ext2fs_open(const char *name, int flags, int superblock,
			     int block_size, io_manager manager,
			     ext2_filsys *ret_fs);

/* get_pathname.c */
extern errcode_t ext2fs_get_pathname(ext2_filsys fs, ino_t dir, ino_t ino,
			       char **name);

/* link.c */
errcode_t ext2fs_link(ext2_filsys fs, ino_t dir, const char *name,
		      ino_t ino, int flags);
errcode_t ext2fs_unlink(ext2_filsys fs, ino_t dir, const char *name,
			ino_t ino, int flags);

/* read_bb.c */
extern errcode_t ext2fs_read_bb_inode(ext2_filsys fs, badblocks_list *bb_list);

/* read_bb_file.c */
extern errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f, 
				     badblocks_list *bb_list,
				     void (*invalid)(ext2_filsys fs,
						     blk_t blk));

/* inline functions */
extern void ext2fs_mark_super_dirty(ext2_filsys fs);
extern void ext2fs_mark_changed(ext2_filsys fs);
extern int ext2fs_test_changed(ext2_filsys fs);
extern void ext2fs_mark_valid(ext2_filsys fs);
extern void ext2fs_unmark_valid(ext2_filsys fs);
extern int ext2fs_test_valid(ext2_filsys fs);
extern void ext2fs_mark_ib_dirty(ext2_filsys fs);
extern void ext2fs_mark_bb_dirty(ext2_filsys fs);
extern int ext2fs_test_ib_dirty(ext2_filsys fs);
extern int ext2fs_test_bb_dirty(ext2_filsys fs);
extern int ext2fs_group_of_blk(ext2_filsys fs, blk_t blk);
extern int ext2fs_group_of_ino(ext2_filsys fs, ino_t ino);

/*
 * The actual inlined functions definitions themselves...
 *
 * If NO_INLINE_FUNCS is defined, then we won't try to do inline
 * functions at all!
 */
#if (defined(INCLUDE_INLINE_FUNCS) || !defined(NO_INLINE_FUNCS))
#ifdef INCLUDE_INLINE_FUNCS
#define _INLINE_ extern
#else
#define _INLINE_ extern __inline__
#endif

/*
 * Mark a filesystem superblock as dirty
 */
_INLINE_ void ext2fs_mark_super_dirty(ext2_filsys fs)
{
	fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_CHANGED;
}

/*
 * Mark a filesystem as changed
 */
_INLINE_ void ext2fs_mark_changed(ext2_filsys fs)
{
	fs->flags |= EXT2_FLAG_CHANGED;
}

/*
 * Check to see if a filesystem has changed
 */
_INLINE_ int ext2fs_test_changed(ext2_filsys fs)
{
	return (fs->flags & EXT2_FLAG_CHANGED);
}

/*
 * Mark a filesystem as valid
 */
_INLINE_ void ext2fs_mark_valid(ext2_filsys fs)
{
	fs->flags |= EXT2_FLAG_VALID;
}

/*
 * Mark a filesystem as NOT valid
 */
_INLINE_ void ext2fs_unmark_valid(ext2_filsys fs)
{
	fs->flags &= ~EXT2_FLAG_VALID;
}

/*
 * Check to see if a filesystem is valid
 */
_INLINE_ int ext2fs_test_valid(ext2_filsys fs)
{
	return (fs->flags & EXT2_FLAG_VALID);
}

/*
 * Mark the inode bitmap as dirty
 */
_INLINE_ void ext2fs_mark_ib_dirty(ext2_filsys fs)
{
	fs->flags |= EXT2_FLAG_IB_DIRTY | EXT2_FLAG_CHANGED;
}

/*
 * Mark the block bitmap as dirty
 */
_INLINE_ void ext2fs_mark_bb_dirty(ext2_filsys fs)
{
	fs->flags |= EXT2_FLAG_BB_DIRTY | EXT2_FLAG_CHANGED;
}

/*
 * Check to see if a filesystem's inode bitmap is dirty
 */
_INLINE_ int ext2fs_test_ib_dirty(ext2_filsys fs)
{
	return (fs->flags & EXT2_FLAG_IB_DIRTY);
}

/*
 * Check to see if a filesystem's block bitmap is dirty
 */
_INLINE_ int ext2fs_test_bb_dirty(ext2_filsys fs)
{
	return (fs->flags & EXT2_FLAG_BB_DIRTY);
}

/*
 * Return the group # of a block
 */
_INLINE_ int ext2fs_group_of_blk(ext2_filsys fs, blk_t blk)
{
	return (blk - fs->super->s_first_data_block) /
		fs->super->s_blocks_per_group;
}

/*
 * Return the group # of an inode number
 */
_INLINE_ int ext2fs_group_of_ino(ext2_filsys fs, ino_t ino)
{
	return (ino - 1) / fs->super->s_inodes_per_group;
}
#undef _INLINE_
#endif