ext2fs.h 40.2 KB
Newer Older
Theodore Ts'o's avatar
Theodore Ts'o committed
1 2 3
/*
 * ext2fs.h --- ext2fs
 * 
Theodore Ts'o's avatar
Theodore Ts'o committed
4 5 6 7 8 9
 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
 *
 * %Begin-Header%
 * This file may be redistributed under the terms of the GNU Public
 * License.
 * %End-Header%
Theodore Ts'o's avatar
Theodore Ts'o committed
10 11
 */

12 13 14
#ifndef _EXT2FS_EXT2FS_H
#define _EXT2FS_EXT2FS_H

Theodore Ts'o's avatar
Theodore Ts'o committed
15 16 17 18 19 20
#ifdef __GNUC__
#define EXT2FS_ATTR(x) __attribute__(x)
#else
#define EXT2FS_ATTR(x)
#endif

Theodore Ts'o's avatar
Theodore Ts'o committed
21 22 23 24
#ifdef __cplusplus
extern "C" {
#endif

Theodore Ts'o's avatar
Theodore Ts'o committed
25 26 27
/*
 * Non-GNU C compilers won't necessarily understand inline
 */
Theodore Ts'o's avatar
Theodore Ts'o committed
28
#if (!defined(__GNUC__) && !defined(__WATCOMC__))
Theodore Ts'o's avatar
Theodore Ts'o committed
29 30 31
#define NO_INLINE_FUNCS
#endif

Theodore Ts'o's avatar
Theodore Ts'o committed
32 33 34 35 36 37 38 39 40 41
/*
 * 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
42 43 44 45
/*
 * The last ext2fs revision level that this version of the library is
 * able to support.
 */
Theodore Ts'o's avatar
Theodore Ts'o committed
46
#define EXT2_LIB_CURRENT_REV	EXT2_DYNAMIC_REV
Theodore Ts'o's avatar
Theodore Ts'o committed
47

Theodore Ts'o's avatar
Theodore Ts'o committed
48
#ifdef HAVE_SYS_TYPES_H
Theodore Ts'o's avatar
Theodore Ts'o committed
49
#include <sys/types.h>
Theodore Ts'o's avatar
Theodore Ts'o committed
50 51
#endif

Theodore Ts'o's avatar
Theodore Ts'o committed
52
#include <stdio.h>
Theodore Ts'o's avatar
Theodore Ts'o committed
53
#include <stdlib.h>
54
#include <string.h>
55 56 57

#if EXT2_FLAT_INCLUDES
#include "e2_types.h"
Theodore Ts'o's avatar
Theodore Ts'o committed
58
#include "ext2_fs.h"
59
#include "ext3_extents.h"
60
#else
Theodore Ts'o's avatar
Theodore Ts'o committed
61
#include <ext2fs/ext2_types.h>
Theodore Ts'o's avatar
Theodore Ts'o committed
62
#include <ext2fs/ext2_fs.h>
63
#include <ext2fs/ext3_extents.h>
Theodore Ts'o's avatar
Theodore Ts'o committed
64
#endif /* EXT2_FLAT_INCLUDES */
Theodore Ts'o's avatar
Theodore Ts'o committed
65

Theodore Ts'o's avatar
Theodore Ts'o committed
66
typedef __u32		ext2_ino_t;
Theodore Ts'o's avatar
Theodore Ts'o committed
67
typedef __u32		blk_t;
Theodore Ts'o's avatar
Theodore Ts'o committed
68
typedef __u64		blk64_t;
Theodore Ts'o's avatar
Theodore Ts'o committed
69
typedef __u32		dgrp_t;
Theodore Ts'o's avatar
Theodore Ts'o committed
70
typedef __u32		ext2_off_t;
Theodore Ts'o's avatar
Theodore Ts'o committed
71
typedef __s64		e2_blkcnt_t;
72
typedef __u32		ext2_dirhash_t;
Theodore Ts'o's avatar
Theodore Ts'o committed
73

Theodore Ts'o's avatar
Theodore Ts'o committed
74 75 76 77
#if EXT2_FLAT_INCLUDES
#include "com_err.h"
#include "ext2_io.h"
#include "ext2_err.h"
78
#include "ext2_ext_attr.h"
Theodore Ts'o's avatar
Theodore Ts'o committed
79
#else
Theodore Ts'o's avatar
Theodore Ts'o committed
80 81 82
#include <et/com_err.h>
#include <ext2fs/ext2_io.h>
#include <ext2fs/ext2_err.h>
83
#include <ext2fs/ext2_ext_attr.h>
Theodore Ts'o's avatar
Theodore Ts'o committed
84
#endif
Theodore Ts'o's avatar
Theodore Ts'o committed
85

86 87 88 89 90 91 92 93 94
/*
 * Portability help for Microsoft Visual C++
 */
#ifdef _MSC_VER
#define EXT2_QSORT_TYPE int __cdecl
#else
#define EXT2_QSORT_TYPE int
#endif

Theodore Ts'o's avatar
Theodore Ts'o committed
95 96
typedef struct struct_ext2_filsys *ext2_filsys;

Theodore Ts'o's avatar
Theodore Ts'o committed
97 98 99
#define EXT2FS_MARK_ERROR 	0
#define EXT2FS_UNMARK_ERROR 	1
#define EXT2FS_TEST_ERROR	2
Theodore Ts'o's avatar
Theodore Ts'o committed
100

Theodore Ts'o's avatar
Theodore Ts'o committed
101 102 103
typedef struct ext2fs_struct_generic_bitmap *ext2fs_generic_bitmap;
typedef struct ext2fs_struct_generic_bitmap *ext2fs_inode_bitmap;
typedef struct ext2fs_struct_generic_bitmap *ext2fs_block_bitmap;
Theodore Ts'o's avatar
Theodore Ts'o committed
104

Theodore Ts'o's avatar
Theodore Ts'o committed
105
#define EXT2_FIRST_INODE(s)	EXT2_FIRST_INO(s)
106

Theodore Ts'o's avatar
Theodore Ts'o committed
107

Theodore Ts'o's avatar
Theodore Ts'o committed
108
/*
109
 * Badblocks list definitions
Theodore Ts'o's avatar
Theodore Ts'o committed
110 111
 */

112 113 114 115 116
typedef struct ext2_struct_u32_list *ext2_badblocks_list;
typedef struct ext2_struct_u32_iterate *ext2_badblocks_iterate;

typedef struct ext2_struct_u32_list *ext2_u32_list;
typedef struct ext2_struct_u32_iterate *ext2_u32_iterate;
Theodore Ts'o's avatar
Theodore Ts'o committed
117 118

/* old */
119 120
typedef struct ext2_struct_u32_list *badblocks_list;
typedef struct ext2_struct_u32_iterate *badblocks_iterate;
Theodore Ts'o's avatar
Theodore Ts'o committed
121 122 123 124 125 126 127

#define BADBLOCKS_FLAG_DIRTY	1

/*
 * ext2_dblist structure and abstractions (see dblist.c)
 */
struct ext2_db_entry {
Theodore Ts'o's avatar
Theodore Ts'o committed
128
	ext2_ino_t	ino;
Theodore Ts'o's avatar
Theodore Ts'o committed
129 130 131 132 133 134 135 136
	blk_t	blk;
	int	blockcnt;
};

typedef struct ext2_struct_dblist *ext2_dblist;

#define DBLIST_ABORT	1

Theodore Ts'o's avatar
Theodore Ts'o committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
/*
 * ext2_fileio definitions
 */

#define EXT2_FILE_WRITE		0x0001
#define EXT2_FILE_CREATE	0x0002

#define EXT2_FILE_MASK		0x00FF

#define EXT2_FILE_BUF_DIRTY	0x4000
#define EXT2_FILE_BUF_VALID	0x2000

typedef struct ext2_file *ext2_file_t;

#define EXT2_SEEK_SET	0
#define EXT2_SEEK_CUR	1
#define EXT2_SEEK_END	2

Theodore Ts'o's avatar
Theodore Ts'o committed
155
/*
156
 * Flags for the ext2_filsys structure and for ext2fs_open()
Theodore Ts'o's avatar
Theodore Ts'o committed
157
 */
Theodore Ts'o's avatar
Theodore Ts'o committed
158 159 160 161 162 163
#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
Theodore Ts'o's avatar
Theodore Ts'o committed
164 165 166 167
#define EXT2_FLAG_SWAP_BYTES		0x40
#define EXT2_FLAG_SWAP_BYTES_READ	0x80
#define EXT2_FLAG_SWAP_BYTES_WRITE	0x100
#define EXT2_FLAG_MASTER_SB_ONLY	0x200
Theodore Ts'o's avatar
Theodore Ts'o committed
168
#define EXT2_FLAG_FORCE			0x400
169
#define EXT2_FLAG_SUPER_ONLY		0x800
170
#define EXT2_FLAG_JOURNAL_DEV_OK	0x1000
Theodore Ts'o's avatar
Theodore Ts'o committed
171
#define EXT2_FLAG_IMAGE_FILE		0x2000
172
#define EXT2_FLAG_EXCLUSIVE		0x4000
173
#define EXT2_FLAG_SOFTSUPP_FEATURES	0x8000
174
#define EXT2_FLAG_NOFREE_ON_ERROR	0x10000
Theodore Ts'o's avatar
Theodore Ts'o committed
175

Theodore Ts'o's avatar
Theodore Ts'o committed
176 177 178 179 180 181
/*
 * Special flag in the ext2 inode i_flag field that means that this is
 * a new inode.  (So that ext2_write_inode() can clear extra fields.)
 */
#define EXT2_NEW_INODE_FL	0x80000000

182 183 184 185 186 187 188
/*
 * Flags for mkjournal
 *
 * EXT2_MKJOURNAL_V1_SUPER	Make a (deprecated) V1 journal superblock
 */
#define EXT2_MKJOURNAL_V1_SUPER	0x0000001

Theodore Ts'o's avatar
Theodore Ts'o committed
189
struct struct_ext2_filsys {
Theodore Ts'o's avatar
Theodore Ts'o committed
190
	errcode_t			magic;
Theodore Ts'o's avatar
Theodore Ts'o committed
191 192 193 194
	io_channel			io;
	int				flags;
	char *				device_name;
	struct ext2_super_block	* 	super;
Theodore Ts'o's avatar
Theodore Ts'o committed
195
	unsigned int			blocksize;
Theodore Ts'o's avatar
Theodore Ts'o committed
196
	int				fragsize;
Theodore Ts'o's avatar
Theodore Ts'o committed
197
	dgrp_t				group_desc_count;
Theodore Ts'o's avatar
Theodore Ts'o committed
198 199 200
	unsigned long			desc_blocks;
	struct ext2_group_desc *	group_desc;
	int				inode_blocks_per_group;
Theodore Ts'o's avatar
Theodore Ts'o committed
201 202
	ext2fs_inode_bitmap		inode_map;
	ext2fs_block_bitmap		block_map;
Theodore Ts'o's avatar
Theodore Ts'o committed
203 204
	errcode_t (*get_blocks)(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks);
	errcode_t (*check_directory)(ext2_filsys fs, ext2_ino_t ino);
Theodore Ts'o's avatar
Theodore Ts'o committed
205
	errcode_t (*write_bitmaps)(ext2_filsys fs);
Theodore Ts'o's avatar
Theodore Ts'o committed
206
	errcode_t (*read_inode)(ext2_filsys fs, ext2_ino_t ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
207
				struct ext2_inode *inode);
Theodore Ts'o's avatar
Theodore Ts'o committed
208
	errcode_t (*write_inode)(ext2_filsys fs, ext2_ino_t ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
209
				struct ext2_inode *inode);
210
	ext2_badblocks_list		badblocks;
Theodore Ts'o's avatar
Theodore Ts'o committed
211
	ext2_dblist			dblist;
Theodore Ts'o's avatar
Theodore Ts'o committed
212
	__u32				stride;	/* for mke2fs */
Theodore Ts'o's avatar
Theodore Ts'o committed
213
	struct ext2_super_block *	orig_super;
Theodore Ts'o's avatar
Theodore Ts'o committed
214
	struct ext2_image_hdr *		image_header;
215
	__u32				umask;
216
	time_t				now;
Theodore Ts'o's avatar
Theodore Ts'o committed
217 218 219
	/*
	 * Reserved for future expansion
	 */
220
	__u32				reserved[7];
Theodore Ts'o's avatar
Theodore Ts'o committed
221 222

	/*
Theodore Ts'o's avatar
Theodore Ts'o committed
223
	 * Reserved for the use of the calling application.
Theodore Ts'o's avatar
Theodore Ts'o committed
224
	 */
Theodore Ts'o's avatar
Theodore Ts'o committed
225
	void *				priv_data;
Theodore Ts'o's avatar
Theodore Ts'o committed
226 227 228 229 230

	/*
	 * Inode cache
	 */
	struct ext2_inode_cache		*icache;
231
	io_channel			image_io;
Theodore Ts'o's avatar
Theodore Ts'o committed
232 233
};

234 235 236
#if EXT2_FLAT_INCLUDES
#include "e2_bitops.h"
#else
Theodore Ts'o's avatar
Theodore Ts'o committed
237
#include <ext2fs/bitops.h>
238
#endif
Theodore Ts'o's avatar
Theodore Ts'o committed
239

Theodore Ts'o's avatar
Theodore Ts'o committed
240 241 242 243 244 245 246 247 248
/*
 * Return flags for the block iterator functions
 */
#define BLOCK_CHANGED	1
#define BLOCK_ABORT	2
#define BLOCK_ERROR	4

/*
 * Block interate flags
Theodore Ts'o's avatar
Theodore Ts'o committed
249 250 251 252 253 254 255
 *
 * BLOCK_FLAG_APPEND, or BLOCK_FLAG_HOLE, indicates that the interator
 * function should be called on blocks where the block number is zero.
 * This is used by ext2fs_expand_dir() to be able to add a new block
 * to an inode.  It can also be used for programs that want to be able
 * to deal with files that contain "holes".
 * 
256 257 258
 * BLOCK_FLAG_DEPTH_TRAVERSE indicates that the iterator function for
 * the indirect, doubly indirect, etc. blocks should be called after
 * all of the blocks containined in the indirect blocks are processed.
Theodore Ts'o's avatar
Theodore Ts'o committed
259 260
 * This is useful if you are going to be deallocating blocks from an
 * inode.
Theodore Ts'o's avatar
Theodore Ts'o committed
261 262 263
 *
 * BLOCK_FLAG_DATA_ONLY indicates that the iterator function should be
 * called for data blocks only.
Theodore Ts'o's avatar
Theodore Ts'o committed
264
 *
265 266 267
 * BLOCK_FLAG_READ_ONLY is a promise by the caller that it will not 
 * modify returned block number.
 *
Theodore Ts'o's avatar
Theodore Ts'o committed
268
 * BLOCK_FLAG_NO_LARGE is for internal use only.  It informs
Theodore Ts'o's avatar
Theodore Ts'o committed
269
 * ext2fs_block_iterate2 that large files won't be accepted.
Theodore Ts'o's avatar
Theodore Ts'o committed
270 271
 */
#define BLOCK_FLAG_APPEND	1
Theodore Ts'o's avatar
Theodore Ts'o committed
272
#define BLOCK_FLAG_HOLE		1
Theodore Ts'o's avatar
Theodore Ts'o committed
273
#define BLOCK_FLAG_DEPTH_TRAVERSE	2
Theodore Ts'o's avatar
Theodore Ts'o committed
274
#define BLOCK_FLAG_DATA_ONLY	4
275
#define BLOCK_FLAG_READ_ONLY	8
Theodore Ts'o's avatar
Theodore Ts'o committed
276

Theodore Ts'o's avatar
Theodore Ts'o committed
277 278
#define BLOCK_FLAG_NO_LARGE	0x1000

Theodore Ts'o's avatar
Theodore Ts'o committed
279 280 281 282 283 284 285
/*
 * Magic "block count" return values for the block iterator function.
 */
#define BLOCK_COUNT_IND		(-1)
#define BLOCK_COUNT_DIND	(-2)
#define BLOCK_COUNT_TIND	(-3)
#define BLOCK_COUNT_TRANSLATOR	(-4)
Theodore Ts'o's avatar
Theodore Ts'o committed
286

287
#if 0
Theodore Ts'o's avatar
Theodore Ts'o committed
288 289 290 291
/*
 * Flags for ext2fs_move_blocks
 */
#define EXT2_BMOVE_GET_DBLIST	0x0001	
Theodore Ts'o's avatar
Theodore Ts'o committed
292
#define EXT2_BMOVE_DEBUG	0x0002
293
#endif
Theodore Ts'o's avatar
Theodore Ts'o committed
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
/*
 * Generic (non-filesystem layout specific) extents structure
 */

#define EXT2_EXTENT_FLAGS_LEAF		0x0001
#define EXT2_EXTENT_FLAGS_UNINIT	0x0002
#define EXT2_EXTENT_FLAGS_SECOND_VISIT	0x0004

struct ext2fs_extent {
	blk64_t	e_pblk;		/* first physical block */
	blk64_t	e_lblk;		/* first logical block extent covers */
	__u32	e_len;		/* number of blocks covered by extent */
	__u32	e_flags;	/* extent flags */
};

typedef struct ext2_extent_handle *ext2_extent_handle_t;
typedef struct ext2_extent_path *ext2_extent_path_t;

/*
 * Flags used by ext2fs_extent_get()
 */
#define EXT2_EXTENT_CURRENT	0x0000
#define EXT2_EXTENT_MOVE_MASK	0x000F
#define EXT2_EXTENT_ROOT	0x0001
#define EXT2_EXTENT_LAST_LEAF	0x0002
#define EXT2_EXTENT_FIRST_SIB	0x0003
#define EXT2_EXTENT_LAST_SIB	0x0004
#define EXT2_EXTENT_NEXT_SIB	0x0005
#define EXT2_EXTENT_PREV_SIB	0x0006
#define EXT2_EXTENT_NEXT_LEAF	0x0007
#define EXT2_EXTENT_PREV_LEAF	0x0008
#define EXT2_EXTENT_NEXT	0x0009
#define EXT2_EXTENT_PREV	0x000A
#define EXT2_EXTENT_UP		0x000B
#define EXT2_EXTENT_DOWN	0x000C
#define EXT2_EXTENT_DOWN_AND_LAST 0x000D

/*
 * Flags used by ext2fs_extent_insert()
 */

#define EXT2_EXTENT_INSERT_AFTER  0x0001

/*
 * Data structure returned by ext2fs_extent_get_info()
 */
struct ext2_extent_info {
	int		curr_entry;
	int		curr_level;
	int		num_entries;
	int		max_entries;
	int		max_depth;
	int		bytes_avail;
	blk64_t		max_lblk;
	blk64_t		max_pblk;
	__u32		max_len;
	__u32		max_uninit_len;
};

354 355 356 357 358
/*
 * Flags for directory block reading and writing functions
 */
#define EXT2_DIRBLOCK_V2_STRUCT	0x0001

Theodore Ts'o's avatar
Theodore Ts'o committed
359 360 361 362 363 364 365 366 367 368 369 370
/*
 * 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
371
#define DIRENT_FLAG_INCLUDE_REMOVED	2
Theodore Ts'o's avatar
Theodore Ts'o committed
372 373 374 375

#define DIRENT_DOT_FILE		1
#define DIRENT_DOT_DOT_FILE	2
#define DIRENT_OTHER_FILE	3
376
#define DIRENT_DELETED_FILE	4
Theodore Ts'o's avatar
Theodore Ts'o committed
377

Theodore Ts'o's avatar
Theodore Ts'o committed
378 379 380
/*
 * Inode scan definitions
 */
Theodore Ts'o's avatar
Theodore Ts'o committed
381 382
typedef struct ext2_struct_inode_scan *ext2_inode_scan;

Theodore Ts'o's avatar
Theodore Ts'o committed
383 384 385 386 387 388 389
/*
 * ext2fs_scan flags
 */
#define EXT2_SF_CHK_BADBLOCKS	0x0001
#define EXT2_SF_BAD_INODE_BLK	0x0002
#define EXT2_SF_BAD_EXTRA_BYTES	0x0004
#define EXT2_SF_SKIP_MISSING_ITABLE	0x0008
390
#define EXT2_SF_DO_LAZY		0x0010
Theodore Ts'o's avatar
Theodore Ts'o committed
391

Theodore Ts'o's avatar
Theodore Ts'o committed
392 393 394 395 396
/*
 * ext2fs_check_if_mounted flags
 */
#define EXT2_MF_MOUNTED		1
#define EXT2_MF_ISROOT		2
Theodore Ts'o's avatar
Theodore Ts'o committed
397
#define EXT2_MF_READONLY	4
398
#define EXT2_MF_SWAP		8
399
#define EXT2_MF_BUSY		16
Theodore Ts'o's avatar
Theodore Ts'o committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

/*
 * Ext2/linux mode flags.  We define them here so that we don't need
 * to depend on the OS's sys/stat.h, since we may be compiling on a
 * non-Linux system.
 */
#define LINUX_S_IFMT  00170000
#define LINUX_S_IFSOCK 0140000
#define LINUX_S_IFLNK	 0120000
#define LINUX_S_IFREG  0100000
#define LINUX_S_IFBLK  0060000
#define LINUX_S_IFDIR  0040000
#define LINUX_S_IFCHR  0020000
#define LINUX_S_IFIFO  0010000
#define LINUX_S_ISUID  0004000
#define LINUX_S_ISGID  0002000
#define LINUX_S_ISVTX  0001000

Theodore Ts'o's avatar
Theodore Ts'o committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
#define LINUX_S_IRWXU 00700
#define LINUX_S_IRUSR 00400
#define LINUX_S_IWUSR 00200
#define LINUX_S_IXUSR 00100

#define LINUX_S_IRWXG 00070
#define LINUX_S_IRGRP 00040
#define LINUX_S_IWGRP 00020
#define LINUX_S_IXGRP 00010

#define LINUX_S_IRWXO 00007
#define LINUX_S_IROTH 00004
#define LINUX_S_IWOTH 00002
#define LINUX_S_IXOTH 00001

Theodore Ts'o's avatar
Theodore Ts'o committed
433 434 435 436 437 438 439 440
#define LINUX_S_ISLNK(m)	(((m) & LINUX_S_IFMT) == LINUX_S_IFLNK)
#define LINUX_S_ISREG(m)	(((m) & LINUX_S_IFMT) == LINUX_S_IFREG)
#define LINUX_S_ISDIR(m)	(((m) & LINUX_S_IFMT) == LINUX_S_IFDIR)
#define LINUX_S_ISCHR(m)	(((m) & LINUX_S_IFMT) == LINUX_S_IFCHR)
#define LINUX_S_ISBLK(m)	(((m) & LINUX_S_IFMT) == LINUX_S_IFBLK)
#define LINUX_S_ISFIFO(m)	(((m) & LINUX_S_IFMT) == LINUX_S_IFIFO)
#define LINUX_S_ISSOCK(m)	(((m) & LINUX_S_IFMT) == LINUX_S_IFSOCK)

441 442 443 444 445
/*
 * ext2 size of an inode
 */
#define EXT2_I_SIZE(i)	((i)->i_size | ((__u64) (i)->i_size_high << 32))

Theodore Ts'o's avatar
Theodore Ts'o committed
446 447 448 449 450 451 452
/*
 * ext2_icount_t abstraction
 */
#define EXT2_ICOUNT_OPT_INCREMENT	0x01

typedef struct ext2_icount *ext2_icount_t;

Theodore Ts'o's avatar
Theodore Ts'o committed
453 454 455
/*
 * Flags for ext2fs_bmap
 */
456 457
#define BMAP_ALLOC	0x0001
#define BMAP_SET	0x0002
Theodore Ts'o's avatar
Theodore Ts'o committed
458

459 460 461 462 463
/*
 * Returned flags from ext2fs_bmap
 */
#define BMAP_RET_UNINIT	0x0001

464 465 466 467 468 469
/*
 * Flags for imager.c functions
 */
#define IMAGER_FLAG_INODEMAP	1
#define IMAGER_FLAG_SPARSEWRITE	2

Theodore Ts'o's avatar
Theodore Ts'o committed
470 471 472
/*
 * For checking structure magic numbers...
 */
Theodore Ts'o's avatar
Theodore Ts'o committed
473

Theodore Ts'o's avatar
Theodore Ts'o committed
474 475
#define EXT2_CHECK_MAGIC(struct, code) \
	  if ((struct)->magic != (code)) return (code)
Theodore Ts'o's avatar
Theodore Ts'o committed
476 477 478


/*
Theodore Ts'o's avatar
Theodore Ts'o committed
479
 * For ext2 compression support
Theodore Ts'o's avatar
Theodore Ts'o committed
480
 */
481
#define EXT2FS_COMPRESSED_BLKADDR ((blk_t) -1)
Theodore Ts'o's avatar
Theodore Ts'o committed
482 483
#define HOLE_BLKADDR(_b) ((_b) == 0 || (_b) == EXT2FS_COMPRESSED_BLKADDR)

Theodore Ts'o's avatar
Theodore Ts'o committed
484
/*
Theodore Ts'o's avatar
Theodore Ts'o committed
485
 * Features supported by this version of the library
Theodore Ts'o's avatar
Theodore Ts'o committed
486
 */
487 488
#define EXT2_LIB_FEATURE_COMPAT_SUPP	(EXT2_FEATURE_COMPAT_DIR_PREALLOC|\
					 EXT2_FEATURE_COMPAT_IMAGIC_INODES|\
489
					 EXT3_FEATURE_COMPAT_HAS_JOURNAL|\
490
					 EXT2_FEATURE_COMPAT_RESIZE_INODE|\
491
					 EXT2_FEATURE_COMPAT_DIR_INDEX|\
492
					 EXT2_FEATURE_COMPAT_LAZY_BG|\
493
					 EXT2_FEATURE_COMPAT_EXT_ATTR)
Theodore Ts'o's avatar
Theodore Ts'o committed
494

Theodore Ts'o's avatar
Theodore Ts'o committed
495 496
/* This #ifdef is temporary until compression is fully supported */
#ifdef ENABLE_COMPRESSION
Theodore Ts'o's avatar
Theodore Ts'o committed
497 498 499 500
#ifndef I_KNOW_THAT_COMPRESSION_IS_EXPERIMENTAL
/* If the below warning bugs you, then have
   `CPPFLAGS=-DI_KNOW_THAT_COMPRESSION_IS_EXPERIMENTAL' in your
   environment at configure time. */
Theodore Ts'o's avatar
Theodore Ts'o committed
501
 #warning "Compression support is experimental"
Theodore Ts'o's avatar
Theodore Ts'o committed
502
#endif
Theodore Ts'o's avatar
Theodore Ts'o committed
503
#define EXT2_LIB_FEATURE_INCOMPAT_SUPP	(EXT2_FEATURE_INCOMPAT_FILETYPE|\
Theodore Ts'o's avatar
Theodore Ts'o committed
504
					 EXT2_FEATURE_INCOMPAT_COMPRESSION|\
505
					 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|\
506
					 EXT2_FEATURE_INCOMPAT_META_BG|\
507
					 EXT3_FEATURE_INCOMPAT_RECOVER|\
508
					 EXT3_FEATURE_INCOMPAT_EXTENTS|\
509
					 EXT4_FEATURE_INCOMPAT_FLEX_BG)
Theodore Ts'o's avatar
Theodore Ts'o committed
510
#else
Theodore Ts'o's avatar
Theodore Ts'o committed
511
#define EXT2_LIB_FEATURE_INCOMPAT_SUPP	(EXT2_FEATURE_INCOMPAT_FILETYPE|\
512
					 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|\
513
					 EXT2_FEATURE_INCOMPAT_META_BG|\
514
					 EXT3_FEATURE_INCOMPAT_RECOVER|\
515
					 EXT3_FEATURE_INCOMPAT_EXTENTS|\
516
					 EXT4_FEATURE_INCOMPAT_FLEX_BG)
Theodore Ts'o's avatar
Theodore Ts'o committed
517
#endif
Theodore Ts'o's avatar
Theodore Ts'o committed
518
#define EXT2_LIB_FEATURE_RO_COMPAT_SUPP	(EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|\
519 520
					 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|\
					 EXT4_FEATURE_RO_COMPAT_DIR_NLINK)
521 522 523 524 525 526 527 528 529 530

/*
 * These features are only allowed if EXT2_FLAG_SOFTSUPP_FEATURES is passed
 * to ext2fs_openfs()
 */
#define EXT2_LIB_SOFTSUPP_INCOMPAT	(EXT3_FEATURE_INCOMPAT_EXTENTS)
#define EXT2_LIB_SOFTSUPP_RO_COMPAT	(EXT4_FEATURE_RO_COMPAT_HUGE_FILE|\
					 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|\
					 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)

Theodore Ts'o's avatar
Theodore Ts'o committed
531 532 533 534 535
/*
 * function prototypes
 */

/* alloc.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
536 537
extern errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir, int mode,
				  ext2fs_inode_bitmap map, ext2_ino_t *ret);
Theodore Ts'o's avatar
Theodore Ts'o committed
538
extern errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
Theodore Ts'o's avatar
Theodore Ts'o committed
539
				  ext2fs_block_bitmap map, blk_t *ret);
Theodore Ts'o's avatar
Theodore Ts'o committed
540
extern errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start,
Theodore Ts'o's avatar
Theodore Ts'o committed
541 542
					blk_t finish, int num,
					ext2fs_block_bitmap map,
Theodore Ts'o's avatar
Theodore Ts'o committed
543
					blk_t *ret);
Theodore Ts'o's avatar
Theodore Ts'o committed
544 545
extern errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
				    char *block_buf, blk_t *ret);
Theodore Ts'o's avatar
Theodore Ts'o committed
546

547 548 549 550 551
/* alloc_sb.c */
extern int ext2fs_reserve_super_and_bgd(ext2_filsys fs, 
					dgrp_t group,
					ext2fs_block_bitmap bmap);

552 553
/* alloc_stats.c */
void ext2fs_inode_alloc_stats(ext2_filsys fs, ext2_ino_t ino, int inuse);
554 555
void ext2fs_inode_alloc_stats2(ext2_filsys fs, ext2_ino_t ino,
			       int inuse, int isdir);
556 557
void ext2fs_block_alloc_stats(ext2_filsys fs, blk_t blk, int inuse);

Theodore Ts'o's avatar
Theodore Ts'o committed
558 559
/* alloc_tables.c */
extern errcode_t ext2fs_allocate_tables(ext2_filsys fs);
Theodore Ts'o's avatar
Theodore Ts'o committed
560
extern errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
Theodore Ts'o's avatar
Theodore Ts'o committed
561
					     ext2fs_block_bitmap bmap);
Theodore Ts'o's avatar
Theodore Ts'o committed
562

Theodore Ts'o's avatar
Theodore Ts'o committed
563
/* badblocks.c */
564 565
extern errcode_t ext2fs_u32_list_create(ext2_u32_list *ret, int size);
extern errcode_t ext2fs_u32_list_add(ext2_u32_list bb, __u32 blk);
Theodore Ts'o's avatar
Theodore Ts'o committed
566
extern int ext2fs_u32_list_find(ext2_u32_list bb, __u32 blk);
567 568 569 570 571 572 573 574
extern int ext2fs_u32_list_test(ext2_u32_list bb, blk_t blk);
extern errcode_t ext2fs_u32_list_iterate_begin(ext2_u32_list bb,
					       ext2_u32_iterate *ret);
extern int ext2fs_u32_list_iterate(ext2_u32_iterate iter, blk_t *blk);
extern void ext2fs_u32_list_iterate_end(ext2_u32_iterate iter);
extern errcode_t ext2fs_u32_copy(ext2_u32_list src, ext2_u32_list *dest);
extern int ext2fs_u32_list_equal(ext2_u32_list bb1, ext2_u32_list bb2);

Theodore Ts'o's avatar
Theodore Ts'o committed
575 576 577 578 579 580
extern errcode_t ext2fs_badblocks_list_create(ext2_badblocks_list *ret,
					    int size);
extern errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb,
					   blk_t blk);
extern int ext2fs_badblocks_list_test(ext2_badblocks_list bb,
				    blk_t blk);
581 582
extern int ext2fs_u32_list_del(ext2_u32_list bb, __u32 blk);
extern void ext2fs_badblocks_list_del(ext2_u32_list bb, __u32 blk);
Theodore Ts'o's avatar
Theodore Ts'o committed
583 584 585 586 587 588
extern errcode_t
	ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb,
					    ext2_badblocks_iterate *ret);
extern int ext2fs_badblocks_list_iterate(ext2_badblocks_iterate iter,
					 blk_t *blk);
extern void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter);
Theodore Ts'o's avatar
Theodore Ts'o committed
589 590
extern errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src,
				       ext2_badblocks_list *dest);
Theodore Ts'o's avatar
Theodore Ts'o committed
591 592
extern int ext2fs_badblocks_equal(ext2_badblocks_list bb1,
				  ext2_badblocks_list bb2);
593
extern int ext2fs_u32_list_count(ext2_u32_list bb);
Theodore Ts'o's avatar
Theodore Ts'o committed
594 595

/* bb_compat */
Theodore Ts'o's avatar
Theodore Ts'o committed
596 597 598 599 600 601 602
extern errcode_t badblocks_list_create(badblocks_list *ret, int size);
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);
Theodore Ts'o's avatar
Theodore Ts'o committed
603
extern void badblocks_list_free(badblocks_list bb);
Theodore Ts'o's avatar
Theodore Ts'o committed
604 605 606

/* bb_inode.c */
extern errcode_t ext2fs_update_bb_inode(ext2_filsys fs,
Theodore Ts'o's avatar
Theodore Ts'o committed
607
					ext2_badblocks_list bb_list);
Theodore Ts'o's avatar
Theodore Ts'o committed
608 609

/* bitmaps.c */
610 611
extern void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap);
extern void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap);
612 613
extern errcode_t ext2fs_copy_bitmap(ext2fs_generic_bitmap src,
				    ext2fs_generic_bitmap *dest);
Theodore Ts'o's avatar
Theodore Ts'o committed
614 615 616 617
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
618 619 620 621 622 623 624
extern errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
					      const char *descr,
					      ext2fs_block_bitmap *ret);
extern errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
					      const char *descr,
					      ext2fs_inode_bitmap *ret);
extern errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
Theodore Ts'o's avatar
Theodore Ts'o committed
625
					       ext2_ino_t end, ext2_ino_t *oend);
Theodore Ts'o's avatar
Theodore Ts'o committed
626 627 628 629
extern errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
					       blk_t end, blk_t *oend);
extern void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap);
extern void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap);
Theodore Ts'o's avatar
Theodore Ts'o committed
630 631
extern errcode_t ext2fs_read_bitmaps(ext2_filsys fs);
extern errcode_t ext2fs_write_bitmaps(ext2_filsys fs);
632 633 634 635 636 637 638 639
extern errcode_t ext2fs_resize_inode_bitmap(__u32 new_end, __u32 new_real_end,
					    ext2fs_inode_bitmap bmap);
extern errcode_t ext2fs_resize_block_bitmap(__u32 new_end, __u32 new_real_end,
					    ext2fs_block_bitmap bmap);
extern errcode_t ext2fs_compare_block_bitmap(ext2fs_block_bitmap bm1,
					     ext2fs_block_bitmap bm2);
extern errcode_t ext2fs_compare_inode_bitmap(ext2fs_inode_bitmap bm1,
					     ext2fs_inode_bitmap bm2);
640 641 642 643 644 645 646 647 648 649 650 651
extern errcode_t ext2fs_set_inode_bitmap_range(ext2fs_inode_bitmap bmap,
					ext2_ino_t start, unsigned int num,
					void *in);
extern errcode_t ext2fs_get_inode_bitmap_range(ext2fs_inode_bitmap bmap,
					ext2_ino_t start, unsigned int num,
					void *out);
extern errcode_t ext2fs_set_block_bitmap_range(ext2fs_block_bitmap bmap,
					blk_t start, unsigned int num,
					void *in);
extern errcode_t ext2fs_get_block_bitmap_range(ext2fs_block_bitmap bmap,
					blk_t start, unsigned int num,
					void *out);
652

Theodore Ts'o's avatar
Theodore Ts'o committed
653 654 655

/* block.c */
extern errcode_t ext2fs_block_iterate(ext2_filsys fs,
Theodore Ts'o's avatar
Theodore Ts'o committed
656
				      ext2_ino_t	ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
657 658 659 660 661
				      int	flags,
				      char *block_buf,
				      int (*func)(ext2_filsys fs,
						  blk_t	*blocknr,
						  int	blockcnt,
Theodore Ts'o's avatar
Theodore Ts'o committed
662 663
						  void	*priv_data),
				      void *priv_data);
Theodore Ts'o's avatar
Theodore Ts'o committed
664
errcode_t ext2fs_block_iterate2(ext2_filsys fs,
Theodore Ts'o's avatar
Theodore Ts'o committed
665
				ext2_ino_t	ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
666 667 668 669
				int	flags,
				char *block_buf,
				int (*func)(ext2_filsys fs,
					    blk_t	*blocknr,
Theodore Ts'o's avatar
Theodore Ts'o committed
670
					    e2_blkcnt_t	blockcnt,
Theodore Ts'o's avatar
Theodore Ts'o committed
671 672 673 674
					    blk_t	ref_blk,
					    int		ref_offset,
					    void	*priv_data),
				void *priv_data);
Theodore Ts'o's avatar
Theodore Ts'o committed
675

Theodore Ts'o's avatar
Theodore Ts'o committed
676
/* bmap.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
677
extern errcode_t ext2fs_bmap(ext2_filsys fs, ext2_ino_t ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
678 679 680
			     struct ext2_inode *inode, 
			     char *block_buf, int bmap_flags,
			     blk_t block, blk_t *phys_blk);
681 682 683 684
extern errcode_t ext2fs_bmap2(ext2_filsys fs, ext2_ino_t ino, 
			      struct ext2_inode *inode,
			      char *block_buf, int bmap_flags, blk64_t block,
			      int *ret_flags, blk64_t *phys_blk);
Theodore Ts'o's avatar
Theodore Ts'o committed
685

686
#if 0
Theodore Ts'o's avatar
Theodore Ts'o committed
687 688 689
/* bmove.c */
extern errcode_t ext2fs_move_blocks(ext2_filsys fs,
				    ext2fs_block_bitmap reserve,
Theodore Ts'o's avatar
Theodore Ts'o committed
690
				    ext2fs_block_bitmap alloc_map,
Theodore Ts'o's avatar
Theodore Ts'o committed
691
				    int flags);
692
#endif
Theodore Ts'o's avatar
Theodore Ts'o committed
693

Theodore Ts'o's avatar
Theodore Ts'o committed
694 695 696
/* check_desc.c */
extern errcode_t ext2fs_check_desc(ext2_filsys fs);

Theodore Ts'o's avatar
Theodore Ts'o committed
697 698 699
/* closefs.c */
extern errcode_t ext2fs_close(ext2_filsys fs);
extern errcode_t ext2fs_flush(ext2_filsys fs);
700
extern int ext2fs_bg_has_super(ext2_filsys fs, int group_block);
701 702 703 704 705 706
extern int ext2fs_super_and_bgd_loc(ext2_filsys fs, 
				    dgrp_t group,
				    blk_t *ret_super_blk,
				    blk_t *ret_old_desc_blk,
				    blk_t *ret_new_desc_blk,
				    int *ret_meta_bg);
707
extern void ext2fs_update_dynamic_rev(ext2_filsys fs);
Theodore Ts'o's avatar
Theodore Ts'o committed
708

Theodore Ts'o's avatar
Theodore Ts'o committed
709 710
/* dblist.c */

Theodore Ts'o's avatar
Theodore Ts'o committed
711
extern errcode_t ext2fs_get_num_dirs(ext2_filsys fs, ext2_ino_t *ret_num_dirs);
Theodore Ts'o's avatar
Theodore Ts'o committed
712
extern errcode_t ext2fs_init_dblist(ext2_filsys fs, ext2_dblist *ret_dblist);
Theodore Ts'o's avatar
Theodore Ts'o committed
713
extern errcode_t ext2fs_add_dir_block(ext2_dblist dblist, ext2_ino_t ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
714
				      blk_t blk, int blockcnt);
715 716 717
extern void ext2fs_dblist_sort(ext2_dblist dblist,
			       EXT2_QSORT_TYPE (*sortfunc)(const void *,
							   const void *));
Theodore Ts'o's avatar
Theodore Ts'o committed
718 719
extern errcode_t ext2fs_dblist_iterate(ext2_dblist dblist,
	int (*func)(ext2_filsys fs, struct ext2_db_entry *db_info,
Theodore Ts'o's avatar
Theodore Ts'o committed
720 721
		    void	*priv_data),
       void *priv_data);
Theodore Ts'o's avatar
Theodore Ts'o committed
722
extern errcode_t ext2fs_set_dir_block(ext2_dblist dblist, ext2_ino_t ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
723 724 725
				      blk_t blk, int blockcnt);
extern errcode_t ext2fs_copy_dblist(ext2_dblist src,
				    ext2_dblist *dest);
726
extern int ext2fs_dblist_count(ext2_dblist dblist);
727 728 729
extern errcode_t ext2fs_dblist_get_last(ext2_dblist dblist, 
					struct ext2_db_entry **entry);
extern errcode_t ext2fs_dblist_drop_last(ext2_dblist dblist);
Theodore Ts'o's avatar
Theodore Ts'o committed
730 731 732 733 734 735

/* dblist_dir.c */
extern errcode_t
	ext2fs_dblist_dir_iterate(ext2_dblist dblist,
				  int	flags,
				  char	*block_buf,
Theodore Ts'o's avatar
Theodore Ts'o committed
736
				  int (*func)(ext2_ino_t	dir,
Theodore Ts'o's avatar
Theodore Ts'o committed
737 738 739 740 741
					      int		entry,
					      struct ext2_dir_entry *dirent,
					      int	offset,
					      int	blocksize,
					      char	*buf,
Theodore Ts'o's avatar
Theodore Ts'o committed
742 743
					      void	*priv_data),
				  void *priv_data);
Theodore Ts'o's avatar
Theodore Ts'o committed
744 745 746 747

/* dirblock.c */
extern errcode_t ext2fs_read_dir_block(ext2_filsys fs, blk_t block,
				       void *buf);
748 749
extern errcode_t ext2fs_read_dir_block2(ext2_filsys fs, blk_t block,
					void *buf, int flags);
Theodore Ts'o's avatar
Theodore Ts'o committed
750 751
extern errcode_t ext2fs_write_dir_block(ext2_filsys fs, blk_t block,
					void *buf);
752 753
extern errcode_t ext2fs_write_dir_block2(ext2_filsys fs, blk_t block,
					 void *buf, int flags);
Theodore Ts'o's avatar
Theodore Ts'o committed
754

755 756
/* dirhash.c */
extern errcode_t ext2fs_dirhash(int version, const char *name, int len,
757
				const __u32 *seed,
758 759
				ext2_dirhash_t *ret_hash,
				ext2_dirhash_t *ret_minor_hash);
760 761


Theodore Ts'o's avatar
Theodore Ts'o committed
762 763
/* dir_iterate.c */
extern errcode_t ext2fs_dir_iterate(ext2_filsys fs, 
Theodore Ts'o's avatar
Theodore Ts'o committed
764
			      ext2_ino_t dir,
Theodore Ts'o's avatar
Theodore Ts'o committed
765 766 767 768 769 770
			      int flags,
			      char *block_buf,
			      int (*func)(struct ext2_dir_entry *dirent,
					  int	offset,
					  int	blocksize,
					  char	*buf,
Theodore Ts'o's avatar
Theodore Ts'o committed
771 772
					  void	*priv_data),
			      void *priv_data);
773 774 775 776 777 778 779 780 781 782 783 784
extern errcode_t ext2fs_dir_iterate2(ext2_filsys fs, 
			      ext2_ino_t dir,
			      int flags,
			      char *block_buf,
			      int (*func)(ext2_ino_t	dir,
					  int	entry,
					  struct ext2_dir_entry *dirent,
					  int	offset,
					  int	blocksize,
					  char	*buf,
					  void	*priv_data),
			      void *priv_data);
Theodore Ts'o's avatar
Theodore Ts'o committed
785

Theodore Ts'o's avatar
Theodore Ts'o committed
786 787
/* dupfs.c */
extern errcode_t ext2fs_dup_handle(ext2_filsys src, ext2_filsys *dest);
Theodore Ts'o's avatar
Theodore Ts'o committed
788

Theodore Ts'o's avatar
Theodore Ts'o committed
789
/* expanddir.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
790
extern errcode_t ext2fs_expand_dir(ext2_filsys fs, ext2_ino_t dir);
Theodore Ts'o's avatar
Theodore Ts'o committed
791

792
/* ext_attr.c */
793 794
extern __u32 ext2fs_ext_attr_hash_entry(struct ext2_ext_attr_entry *entry,
					void *data);
795
extern errcode_t ext2fs_read_ext_attr(ext2_filsys fs, blk_t block, void *buf);
796 797 798 799 800 801
extern errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block,
				       void *buf);
extern errcode_t ext2fs_adjust_ea_refcount(ext2_filsys fs, blk_t blk,
					   char *block_buf,
					   int adjust, __u32 *newcount);

802 803 804 805
/* extent.c */
extern errcode_t ext2fs_extent_header_verify(void *ptr, int size);
extern errcode_t ext2fs_extent_open(ext2_filsys fs, ext2_ino_t ino,
				    ext2_extent_handle_t *handle);
806
extern void ext2fs_extent_free(ext2_extent_handle_t handle);
807 808 809 810 811 812 813 814 815 816 817 818
extern errcode_t ext2fs_extent_get(ext2_extent_handle_t handle,
				   int flags, struct ext2fs_extent *extent);
extern errcode_t ext2fs_extent_replace(ext2_extent_handle_t handle, int flags,
				       struct ext2fs_extent *extent);
extern errcode_t ext2fs_extent_insert(ext2_extent_handle_t handle, int flags,
				      struct ext2fs_extent *extent);
extern errcode_t ext2fs_extent_delete(ext2_extent_handle_t handle, int flags);
extern errcode_t ext2fs_extent_get_info(ext2_extent_handle_t handle,
					struct ext2_extent_info *info);
extern errcode_t ext2fs_extent_goto(ext2_extent_handle_t handle,
				    blk64_t blk);

Theodore Ts'o's avatar
Theodore Ts'o committed
819
/* fileio.c */
820 821 822
extern errcode_t ext2fs_file_open2(ext2_filsys fs, ext2_ino_t ino,
				   struct ext2_inode *inode,
				   int flags, ext2_file_t *ret);
Theodore Ts'o's avatar
Theodore Ts'o committed
823
extern errcode_t ext2fs_file_open(ext2_filsys fs, ext2_ino_t ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
824
				  int flags, ext2_file_t *ret);
825
extern ext2_filsys ext2fs_file_get_fs(ext2_file_t file);
Theodore Ts'o's avatar
Theodore Ts'o committed
826
extern errcode_t ext2fs_file_close(ext2_file_t file);
827
extern errcode_t ext2fs_file_flush(ext2_file_t file);
Theodore Ts'o's avatar
Theodore Ts'o committed
828
extern errcode_t ext2fs_file_read(ext2_file_t file, void *buf,
829
				  unsigned int wanted, unsigned int *got);
830
extern errcode_t ext2fs_file_write(ext2_file_t file, const void *buf,
831
				   unsigned int nbytes, unsigned int *written);
832 833
extern errcode_t ext2fs_file_llseek(ext2_file_t file, __u64 offset,
				   int whence, __u64 *ret_pos);
Theodore Ts'o's avatar
Theodore Ts'o committed
834 835
extern errcode_t ext2fs_file_lseek(ext2_file_t file, ext2_off_t offset,
				   int whence, ext2_off_t *ret_pos);
836
errcode_t ext2fs_file_get_lsize(ext2_file_t file, __u64 *ret_size);
837 838
extern ext2_off_t ext2fs_file_get_size(ext2_file_t file);
extern errcode_t ext2fs_file_set_size(ext2_file_t file, ext2_off_t size);
Theodore Ts'o's avatar
Theodore Ts'o committed
839

840 841 842
/* finddev.c */
extern char *ext2fs_find_block_device(dev_t device);

843 844 845
/* flushb.c */
extern errcode_t ext2fs_sync_device(int fd, int flushb);

Theodore Ts'o's avatar
Theodore Ts'o committed
846 847
/* freefs.c */
extern void ext2fs_free(ext2_filsys fs);
Theodore Ts'o's avatar
Theodore Ts'o committed
848
extern void ext2fs_free_dblist(ext2_dblist dblist);
849 850
extern void ext2fs_badblocks_list_free(ext2_badblocks_list bb);
extern void ext2fs_u32_list_free(ext2_u32_list bb);
Theodore Ts'o's avatar
Theodore Ts'o committed
851

852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
/* gen_bitmap.c */
extern void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap);
extern errcode_t ext2fs_make_generic_bitmap(errcode_t magic, ext2_filsys fs, 
					    __u32 start, __u32 end, 
					    __u32 real_end,
					    const char *descr, char *init_map,
					    ext2fs_generic_bitmap *ret);
extern errcode_t ext2fs_allocate_generic_bitmap(__u32 start,
						__u32 end,
						__u32 real_end,
						const char *descr,
						ext2fs_generic_bitmap *ret);
extern errcode_t ext2fs_copy_generic_bitmap(ext2fs_generic_bitmap src,
					    ext2fs_generic_bitmap *dest);
extern void ext2fs_clear_generic_bitmap(ext2fs_generic_bitmap bitmap);
extern errcode_t ext2fs_fudge_generic_bitmap_end(ext2fs_inode_bitmap bitmap,
						 errcode_t magic, 
						 errcode_t neq,
						 ext2_ino_t end, 
						 ext2_ino_t *oend);
extern void ext2fs_set_generic_bitmap_padding(ext2fs_generic_bitmap map);
873 874 875 876 877 878 879
extern errcode_t ext2fs_resize_generic_bitmap(errcode_t magic,
					      __u32 new_end,
					      __u32 new_real_end,
					      ext2fs_generic_bitmap bmap);
extern errcode_t ext2fs_compare_generic_bitmap(errcode_t magic, errcode_t neq,
					       ext2fs_generic_bitmap bm1,
					       ext2fs_generic_bitmap bm2);
880 881 882 883 884 885 886 887
extern errcode_t ext2fs_get_generic_bitmap_range(ext2fs_generic_bitmap bmap,
						 errcode_t magic,
						 __u32 start, __u32 num,
						 void *out);
extern errcode_t ext2fs_set_generic_bitmap_range(ext2fs_generic_bitmap bmap,
						 errcode_t magic,
						 __u32 start, __u32 num,
						 void *in);
888

Theodore Ts'o's avatar
Theodore Ts'o committed
889 890 891
/* getsize.c */
extern errcode_t ext2fs_get_device_size(const char *file, int blocksize,
					blk_t *retblocks);
Theodore Ts'o's avatar
Theodore Ts'o committed
892

893 894 895
/* getsectsize.c */
errcode_t ext2fs_get_device_sectsize(const char *file, int *sectsize);

896 897 898 899 900 901 902 903
/* imager.c */
extern errcode_t ext2fs_image_inode_write(ext2_filsys fs, int fd, int flags);
extern errcode_t ext2fs_image_inode_read(ext2_filsys fs, int fd, int flags);
extern errcode_t ext2fs_image_super_write(ext2_filsys fs, int fd, int flags);
extern errcode_t ext2fs_image_super_read(ext2_filsys fs, int fd, int flags);
extern errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags);
extern errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags);

904 905 906 907
/* ind_block.c */
errcode_t ext2fs_read_ind_block(ext2_filsys fs, blk_t blk, void *buf);
errcode_t ext2fs_write_ind_block(ext2_filsys fs, blk_t blk, void *buf);

Theodore Ts'o's avatar
Theodore Ts'o committed
908 909 910 911 912
/* initialize.c */
extern errcode_t ext2fs_initialize(const char *name, int flags,
				   struct ext2_super_block *param,
				   io_manager manager, ext2_filsys *ret_fs);

913 914
/* icount.c */
extern void ext2fs_free_icount(ext2_icount_t icount);
915 916
extern errcode_t ext2fs_create_icount_tdb(ext2_filsys fs, char *tdb_dir,
					  int flags, ext2_icount_t *ret);
Theodore Ts'o's avatar
Theodore Ts'o committed
917 918
extern errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags, 
				       unsigned int size,
919
				       ext2_icount_t hint, ext2_icount_t *ret);
Theodore Ts'o's avatar
Theodore Ts'o committed
920 921
extern errcode_t ext2fs_create_icount(ext2_filsys fs, int flags, 
				      unsigned int size,
922 923 924 925 926 927 928 929 930 931 932 933
				      ext2_icount_t *ret);
extern errcode_t ext2fs_icount_fetch(ext2_icount_t icount, ext2_ino_t ino,
				     __u16 *ret);
extern errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
					 __u16 *ret);
extern errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
					 __u16 *ret);
extern errcode_t ext2fs_icount_store(ext2_icount_t icount, ext2_ino_t ino,
				     __u16 count);
extern ext2_ino_t ext2fs_get_icount_size(ext2_icount_t icount);
errcode_t ext2fs_icount_validate(ext2_icount_t icount, FILE *);

Theodore Ts'o's avatar
Theodore Ts'o committed
934
/* inode.c */
935
extern errcode_t ext2fs_flush_icache(ext2_filsys fs);
936 937 938 939
extern errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan, 
					    ext2_ino_t *ino,
					    struct ext2_inode *inode, 
					    int bufsize);
Theodore Ts'o's avatar
Theodore Ts'o committed
940 941 942
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);
Theodore Ts'o's avatar
Theodore Ts'o committed
943
extern errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
944
			       struct ext2_inode *inode);
Theodore Ts'o's avatar
Theodore Ts'o committed
945 946 947 948 949 950 951
extern errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
						   int	group);
extern void ext2fs_set_inode_callback
	(ext2_inode_scan scan,
	 errcode_t (*done_group)(ext2_filsys fs,
				 ext2_inode_scan scan,
				 dgrp_t group,
Theodore Ts'o's avatar
Theodore Ts'o committed
952
				 void * priv_data),
Theodore Ts'o's avatar
Theodore Ts'o committed
953 954 955
	 void *done_group_data);
extern int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
				   int clear_flags);
956 957 958
extern errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino,
					struct ext2_inode * inode, 
					int bufsize);
Theodore Ts'o's avatar
Theodore Ts'o committed
959
extern errcode_t ext2fs_read_inode (ext2_filsys fs, ext2_ino_t ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
960
			    struct ext2_inode * inode);
961 962 963
extern errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
					 struct ext2_inode * inode, 
					 int bufsize);
Theodore Ts'o's avatar
Theodore Ts'o committed
964
extern errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
965
			    struct ext2_inode * inode);
966 967
extern errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino,
			    struct ext2_inode * inode);
Theodore Ts'o's avatar
Theodore Ts'o committed
968 969
extern errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks);
extern errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino);
Theodore Ts'o's avatar
Theodore Ts'o committed
970

971 972
/* inode_io.c */
extern io_manager inode_io_manager;
973 974
extern errcode_t ext2fs_inode_io_intern(ext2_filsys fs, ext2_ino_t ino, 
					char **name);
975 976 977
extern errcode_t ext2fs_inode_io_intern2(ext2_filsys fs, ext2_ino_t ino,
					 struct ext2_inode *inode,
					 char **name);
978
	
Theodore Ts'o's avatar
Theodore Ts'o committed
979 980
/* ismounted.c */
extern errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags);
981 982
extern errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
					  char *mtpt, int mtlen);
Theodore Ts'o's avatar
Theodore Ts'o committed
983

Theodore Ts'o's avatar
Theodore Ts'o committed
984
/* namei.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
985 986 987 988 989 990 991 992
extern errcode_t ext2fs_lookup(ext2_filsys fs, ext2_ino_t dir, const char *name,
			 int namelen, char *buf, ext2_ino_t *inode);
extern errcode_t ext2fs_namei(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
			const char *name, ext2_ino_t *inode);
errcode_t ext2fs_namei_follow(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
			      const char *name, ext2_ino_t *inode);
extern errcode_t ext2fs_follow_link(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
			ext2_ino_t inode, ext2_ino_t *res_inode);
Theodore Ts'o's avatar
Theodore Ts'o committed
993 994 995

/* native.c */
int ext2fs_native_flag(void);
Theodore Ts'o's avatar
Theodore Ts'o committed
996 997

/* newdir.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
998 999
extern errcode_t ext2fs_new_dir_block(ext2_filsys fs, ext2_ino_t dir_ino,
				ext2_ino_t parent_ino, char **block);
Theodore Ts'o's avatar
Theodore Ts'o committed
1000 1001

/* mkdir.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
1002
extern errcode_t ext2fs_mkdir(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t inum,
Theodore Ts'o's avatar
Theodore Ts'o committed
1003 1004
			      const char *name);

1005
/* mkjournal.c */
1006 1007 1008 1009 1010
extern errcode_t ext2fs_create_journal_superblock(ext2_filsys fs,
						  __u32 size, int flags,
						  char  **ret_jsb);
extern errcode_t ext2fs_add_journal_device(ext2_filsys fs,
					   ext2_filsys journal_dev);
1011 1012
extern errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t size,
					  int flags);
1013
extern int ext2fs_default_journal_size(__u64 blocks);
1014

Theodore Ts'o's avatar
Theodore Ts'o committed
1015 1016
/* openfs.c */
extern errcode_t ext2fs_open(const char *name, int flags, int superblock,
Theodore Ts'o's avatar
Theodore Ts'o committed
1017
			     unsigned int block_size, io_manager manager,
Theodore Ts'o's avatar
Theodore Ts'o committed
1018
			     ext2_filsys *ret_fs);
1019 1020 1021 1022
extern errcode_t ext2fs_open2(const char *name, const char *io_options, 
			      int flags, int superblock, 
			      unsigned int block_size, io_manager manager,
			      ext2_filsys *ret_fs);
1023 1024
extern blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, 
					 dgrp_t i);
1025 1026 1027
errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io);
errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io);
errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io);
Theodore Ts'o's avatar
Theodore Ts'o committed
1028 1029

/* get_pathname.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
1030
extern errcode_t ext2fs_get_pathname(ext2_filsys fs, ext2_ino_t dir, ext2_ino_t ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
1031 1032 1033
			       char **name);

/* link.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
1034 1035 1036 1037
errcode_t ext2fs_link(ext2_filsys fs, ext2_ino_t dir, const char *name,
		      ext2_ino_t ino, int flags);
errcode_t ext2fs_unlink(ext2_filsys fs, ext2_ino_t dir, const char *name,
			ext2_ino_t ino, int flags);
Theodore Ts'o's avatar
Theodore Ts'o committed
1038 1039

/* read_bb.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
1040 1041
extern errcode_t ext2fs_read_bb_inode(ext2_filsys fs,
				      ext2_badblocks_list *bb_list);
Theodore Ts'o's avatar
Theodore Ts'o committed
1042 1043

/* read_bb_file.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
1044 1045
extern errcode_t ext2fs_read_bb_FILE2(ext2_filsys fs, FILE *f, 
				      ext2_badblocks_list *bb_list,
1046
				      void *priv_data,
Theodore Ts'o's avatar
Theodore Ts'o committed
1047 1048 1049
				      void (*invalid)(ext2_filsys fs,
						      blk_t blk,
						      char *badstr,
1050
						      void *priv_data));
Theodore Ts'o's avatar
Theodore Ts'o committed
1051
extern errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f, 
Theodore Ts'o's avatar
Theodore Ts'o committed
1052
				     ext2_badblocks_list *bb_list,
Theodore Ts'o's avatar
Theodore Ts'o committed
1053 1054 1055
				     void (*invalid)(ext2_filsys fs,
						     blk_t blk));

1056 1057 1058
/* res_gdt.c */
extern errcode_t ext2fs_create_resize_inode(ext2_filsys fs);

Theodore Ts'o's avatar
Theodore Ts'o committed
1059
/* swapfs.c */
1060 1061
extern void ext2fs_swap_ext_attr(char *to, char *from, int bufsize, 
				 int has_header);
1062 1063 1064 1065
extern void ext2fs_swap_ext_attr_header(struct ext2_ext_attr_header *to_header,
					struct ext2_ext_attr_header *from_hdr);
extern void ext2fs_swap_ext_attr_entry(struct ext2_ext_attr_entry *to_entry,
				       struct ext2_ext_attr_entry *from_entry);
Theodore Ts'o's avatar
Theodore Ts'o committed
1066 1067
extern void ext2fs_swap_super(struct ext2_super_block * super);
extern void ext2fs_swap_group_desc(struct ext2_group_desc *gdp);
1068 1069 1070
extern void ext2fs_swap_inode_full(ext2_filsys fs, struct ext2_inode_large *t,
				   struct ext2_inode_large *f, int hostorder,
				   int bufsize);
Theodore Ts'o's avatar
Theodore Ts'o committed
1071 1072 1073
extern void ext2fs_swap_inode(ext2_filsys fs,struct ext2_inode *t,
			      struct ext2_inode *f, int hostorder);

Theodore Ts'o's avatar
Theodore Ts'o committed
1074
/* valid_blk.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
1075 1076 1077 1078 1079 1080
extern int ext2fs_inode_has_valid_blocks(struct ext2_inode *inode);

/* version.c */
extern int ext2fs_parse_version_string(const char *ver_string);
extern int ext2fs_get_library_version(const char **ver_string,
				      const char **date_string);
Theodore Ts'o's avatar
Theodore Ts'o committed
1081

Theodore Ts'o's avatar
Theodore Ts'o committed
1082 1083 1084 1085 1086 1087
/* write_bb_file.c */
extern errcode_t ext2fs_write_bb_FILE(ext2_badblocks_list bb_list,
				      unsigned int flags,
				      FILE *f);


Theodore Ts'o's avatar
Theodore Ts'o committed
1088
/* inline functions */
1089
extern errcode_t ext2fs_get_mem(unsigned long size, void *ptr);
1090
extern errcode_t ext2fs_get_array(unsigned long count, unsigned long size, void *ptr);
1091
extern errcode_t ext2fs_free_mem(void *ptr);
Theodore Ts'o's avatar
Theodore Ts'o committed
1092
extern errcode_t ext2fs_resize_mem(unsigned long old_size,
1093
				   unsigned long size, void *ptr);
Theodore Ts'o's avatar
Theodore Ts'o committed
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
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);
Theodore Ts'o's avatar
Theodore Ts'o committed
1105
extern int ext2fs_group_of_ino(ext2_filsys fs, ext2_ino_t ino);
1106 1107
extern blk_t ext2fs_group_first_block(ext2_filsys fs, dgrp_t group);
extern blk_t ext2fs_group_last_block(ext2_filsys fs, dgrp_t group);
1108 1109
extern blk_t ext2fs_inode_data_blocks(ext2_filsys fs,
				      struct ext2_inode *inode);
1110
extern unsigned int ext2fs_div_ceil(unsigned int a, unsigned int b);
Theodore Ts'o's avatar
Theodore Ts'o committed
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121

/*
 * 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
Theodore Ts'o's avatar
Theodore Ts'o committed
1122
#ifdef __GNUC__
Theodore Ts'o's avatar
Theodore Ts'o committed
1123
#define _INLINE_ extern __inline__
Theodore Ts'o's avatar
Theodore Ts'o committed
1124 1125 1126
#else				/* For Watcom C */
#define _INLINE_ extern inline
#endif
Theodore Ts'o's avatar
Theodore Ts'o committed
1127 1128
#endif

Theodore Ts'o's avatar
Theodore Ts'o committed
1129
#ifndef EXT2_CUSTOM_MEMORY_ROUTINES
1130
#include <string.h>
Theodore Ts'o's avatar
Theodore Ts'o committed
1131 1132 1133
/*
 *  Allocate memory
 */
1134
_INLINE_ errcode_t ext2fs_get_mem(unsigned long size, void *ptr)
Theodore Ts'o's avatar
Theodore Ts'o committed
1135
{
1136
	void *pp;
1137

1138 1139
	pp = malloc(size);
	if (!pp)
1140
		return EXT2_ET_NO_MEMORY;
1141
	memcpy(ptr, &pp, sizeof (pp));
Theodore Ts'o's avatar
Theodore Ts'o committed
1142 1143
	return 0;
}
1144

1145 1146 1147 1148 1149 1150
_INLINE_ errcode_t ext2fs_get_array(unsigned long count, unsigned long size, void *ptr)
{
	if (count && (-1UL)/count<size)
		return EXT2_ET_NO_MEMORY; //maybe define EXT2_ET_OVERFLOW ?
	return ext2fs_get_mem(count*size, ptr);
}
Theodore Ts'o's avatar
Theodore Ts'o committed
1151 1152 1153 1154

/*
 * Free memory
 */
1155
_INLINE_ errcode_t ext2fs_free_mem(void *ptr)
Theodore Ts'o's avatar
Theodore Ts'o committed
1156
{
1157
	void *p;
1158

1159 1160 1161 1162
	memcpy(&p, ptr, sizeof(p));
	free(p);
	p = 0;
	memcpy(ptr, &p, sizeof(p));
Theodore Ts'o's avatar
Theodore Ts'o committed
1163 1164 1165 1166 1167 1168
	return 0;
}
	
/*
 *  Resize memory
 */
1169
_INLINE_ errcode_t ext2fs_resize_mem(unsigned long EXT2FS_ATTR((unused)) old_size,
1170
				     unsigned long size, void *ptr)
Theodore Ts'o's avatar
Theodore Ts'o committed
1171 1172 1173
{
	void *p;

1174 1175
	/* Use "memcpy" for pointer assignments here to avoid problems
	 * with C99 strict type aliasing rules. */
1176
	memcpy(&p, ptr, sizeof(p));
1177
	p = realloc(p, size);
Theodore Ts'o's avatar
Theodore Ts'o committed
1178
	if (!p)
1179
		return EXT2_ET_NO_MEMORY;
1180
	memcpy(ptr, &p, sizeof(p));
Theodore Ts'o's avatar
Theodore Ts'o committed
1181 1182 1183 1184
	return 0;
}
#endif	/* Custom memory routines */

Theodore Ts'o's avatar
Theodore Ts'o committed
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
/*
 * 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
 */
Theodore Ts'o's avatar
Theodore Ts'o committed
1277
_INLINE_ int ext2fs_group_of_ino(ext2_filsys fs, ext2_ino_t ino)
Theodore Ts'o's avatar
Theodore Ts'o committed
1278 1279 1280
{
	return (ino - 1) / fs->super->s_inodes_per_group;
}
1281

1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
/*
 * Return the first block (inclusive) in a group
 */
_INLINE_ blk_t ext2fs_group_first_block(ext2_filsys fs, dgrp_t group)
{
	return fs->super->s_first_data_block +
		(group * fs->super->s_blocks_per_group);
}

/*
 * Return the last block (inclusive) in a group
 */
_INLINE_ blk_t ext2fs_group_last_block(ext2_filsys fs, dgrp_t group)
{
	return (group == fs->group_desc_count - 1 ?
		fs->super->s_blocks_count - 1 :
		ext2fs_group_first_block(fs, group) +
			(fs->super->s_blocks_per_group - 1));
}

1302 1303 1304 1305 1306 1307
_INLINE_ blk_t ext2fs_inode_data_blocks(ext2_filsys fs,
					struct ext2_inode *inode)
{
       return inode->i_blocks -
              (inode->i_file_acl ? fs->blocksize >> 9 : 0);
}
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317

/*
 * This is an efficient, overflow safe way of calculating ceil((1.0 * a) / b)
 */
_INLINE_ unsigned int ext2fs_div_ceil(unsigned int a, unsigned int b)
{
	if (!a)
		return 0;
	return ((a - 1) / b) + 1;
}
Theodore Ts'o's avatar
Theodore Ts'o committed
1318 1319 1320
#undef _INLINE_
#endif

Theodore Ts'o's avatar
Theodore Ts'o committed
1321 1322 1323 1324
#ifdef __cplusplus
}
#endif

1325
#endif /* _EXT2FS_EXT2FS_H */