ext2fs.h 56.2 KB
Newer Older
Theodore Ts'o's avatar
Theodore Ts'o committed
1 2
/*
 * ext2fs.h --- ext2fs
3
 *
Theodore Ts'o's avatar
Theodore Ts'o committed
4 5 6
 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
 *
 * %Begin-Header%
7 8
 * This file may be redistributed under the terms of the GNU Library
 * General Public License, version 2.
Theodore Ts'o's avatar
Theodore Ts'o committed
9
 * %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
/*
 * 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
40
#define SUPERBLOCK_SIZE		1024
Theodore Ts'o's avatar
Theodore Ts'o committed
41

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
#include <errno.h>
56

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

67 68 69 70 71 72 73 74 75 76 77 78 79 80
#ifdef __CHECK_ENDIAN__
#define __bitwise __attribute__((bitwise))
#else
#define __bitwise
#endif

typedef __u32 __bitwise		ext2_ino_t;
typedef __u32 __bitwise		blk_t;
typedef __u64 __bitwise		blk64_t;
typedef __u32 __bitwise		dgrp_t;
typedef __u32 __bitwise		ext2_off_t;
typedef __u64 __bitwise		ext2_off64_t;
typedef __s64 __bitwise		e2_blkcnt_t;
typedef __u32 __bitwise		ext2_dirhash_t;
Theodore Ts'o's avatar
Theodore Ts'o committed
81

Theodore Ts'o's avatar
Theodore Ts'o committed
82 83 84 85
#if EXT2_FLAT_INCLUDES
#include "com_err.h"
#include "ext2_io.h"
#include "ext2_err.h"
86
#include "ext2_ext_attr.h"
Theodore Ts'o's avatar
Theodore Ts'o committed
87
#else
Theodore Ts'o's avatar
Theodore Ts'o committed
88 89 90
#include <et/com_err.h>
#include <ext2fs/ext2_io.h>
#include <ext2fs/ext2_err.h>
91
#include <ext2fs/ext2_ext_attr.h>
Theodore Ts'o's avatar
Theodore Ts'o committed
92
#endif
Theodore Ts'o's avatar
Theodore Ts'o committed
93

94 95 96 97 98 99 100 101 102
/*
 * 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
103 104
typedef struct struct_ext2_filsys *ext2_filsys;

Theodore Ts'o's avatar
Theodore Ts'o committed
105 106 107
#define EXT2FS_MARK_ERROR 	0
#define EXT2FS_UNMARK_ERROR 	1
#define EXT2FS_TEST_ERROR	2
Theodore Ts'o's avatar
Theodore Ts'o committed
108

Theodore Ts'o's avatar
Theodore Ts'o committed
109 110 111
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
112

Theodore Ts'o's avatar
Theodore Ts'o committed
113
#define EXT2_FIRST_INODE(s)	EXT2_FIRST_INO(s)
114

Theodore Ts'o's avatar
Theodore Ts'o committed
115

Theodore Ts'o's avatar
Theodore Ts'o committed
116
/*
117
 * Badblocks list definitions
Theodore Ts'o's avatar
Theodore Ts'o committed
118 119
 */

120 121 122 123 124
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
125 126

/* old */
127 128
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
129 130 131 132 133 134

#define BADBLOCKS_FLAG_DIRTY	1

/*
 * ext2_dblist structure and abstractions (see dblist.c)
 */
135 136 137 138 139 140 141
struct ext2_db_entry2 {
	ext2_ino_t	ino;
	blk64_t	blk;
	e2_blkcnt_t	blockcnt;
};

/* Ye Olde 32-bit version */
Theodore Ts'o's avatar
Theodore Ts'o committed
142
struct ext2_db_entry {
Theodore Ts'o's avatar
Theodore Ts'o committed
143
	ext2_ino_t	ino;
Theodore Ts'o's avatar
Theodore Ts'o committed
144 145 146 147 148 149 150 151
	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
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
/*
 * 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
170
/*
171
 * Flags for the ext2_filsys structure and for ext2fs_open()
Theodore Ts'o's avatar
Theodore Ts'o committed
172
 */
Theodore Ts'o's avatar
Theodore Ts'o committed
173 174 175 176 177 178
#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
179 180 181 182
#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
183
#define EXT2_FLAG_FORCE			0x400
184
#define EXT2_FLAG_SUPER_ONLY		0x800
185
#define EXT2_FLAG_JOURNAL_DEV_OK	0x1000
Theodore Ts'o's avatar
Theodore Ts'o committed
186
#define EXT2_FLAG_IMAGE_FILE		0x2000
187
#define EXT2_FLAG_EXCLUSIVE		0x4000
188
#define EXT2_FLAG_SOFTSUPP_FEATURES	0x8000
189
#define EXT2_FLAG_NOFREE_ON_ERROR	0x10000
190 191
#define EXT2_FLAG_64BITS		0x20000
#define EXT2_FLAG_PRINT_PROGRESS	0x40000
192
#define EXT2_FLAG_DIRECT_IO		0x80000
193
#define EXT2_FLAG_SKIP_MMP		0x100000
Theodore Ts'o's avatar
Theodore Ts'o committed
194

Theodore Ts'o's avatar
Theodore Ts'o committed
195 196 197 198 199 200
/*
 * 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

201 202 203
/*
 * Flags for mkjournal
 */
204 205 206 207 208
#define EXT2_MKJOURNAL_V1_SUPER	0x0000001 /* create V1 superblock (deprecated) */
#define EXT2_MKJOURNAL_LAZYINIT	0x0000002 /* don't zero journal inode before use*/
#define EXT2_MKJOURNAL_NO_MNT_CHECK 0x0000004 /* don't check mount status */

struct opaque_ext2_group_desc;
209

Theodore Ts'o's avatar
Theodore Ts'o committed
210
struct struct_ext2_filsys {
Theodore Ts'o's avatar
Theodore Ts'o committed
211
	errcode_t			magic;
Theodore Ts'o's avatar
Theodore Ts'o committed
212 213 214 215
	io_channel			io;
	int				flags;
	char *				device_name;
	struct ext2_super_block	* 	super;
Theodore Ts'o's avatar
Theodore Ts'o committed
216
	unsigned int			blocksize;
217
	int				fragsize;
Theodore Ts'o's avatar
Theodore Ts'o committed
218
	dgrp_t				group_desc_count;
Theodore Ts'o's avatar
Theodore Ts'o committed
219
	unsigned long			desc_blocks;
220 221
	struct opaque_ext2_group_desc *	group_desc;
	unsigned int			inode_blocks_per_group;
Theodore Ts'o's avatar
Theodore Ts'o committed
222 223
	ext2fs_inode_bitmap		inode_map;
	ext2fs_block_bitmap		block_map;
224
	/* XXX FIXME-64: not 64-bit safe, but not used? */
Theodore Ts'o's avatar
Theodore Ts'o committed
225 226
	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
227
	errcode_t (*write_bitmaps)(ext2_filsys fs);
Theodore Ts'o's avatar
Theodore Ts'o committed
228
	errcode_t (*read_inode)(ext2_filsys fs, ext2_ino_t ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
229
				struct ext2_inode *inode);
Theodore Ts'o's avatar
Theodore Ts'o committed
230
	errcode_t (*write_inode)(ext2_filsys fs, ext2_ino_t ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
231
				struct ext2_inode *inode);
232
	ext2_badblocks_list		badblocks;
Theodore Ts'o's avatar
Theodore Ts'o committed
233
	ext2_dblist			dblist;
Theodore Ts'o's avatar
Theodore Ts'o committed
234
	__u32				stride;	/* for mke2fs */
Theodore Ts'o's avatar
Theodore Ts'o committed
235
	struct ext2_super_block *	orig_super;
Theodore Ts'o's avatar
Theodore Ts'o committed
236
	struct ext2_image_hdr *		image_header;
237
	__u32				umask;
238
	time_t				now;
239 240 241
	int				cluster_ratio_bits;
	__u16				default_bitmap_type;
	__u16				pad;
Theodore Ts'o's avatar
Theodore Ts'o committed
242 243 244
	/*
	 * Reserved for future expansion
	 */
245
	__u32				reserved[5];
Theodore Ts'o's avatar
Theodore Ts'o committed
246 247

	/*
Theodore Ts'o's avatar
Theodore Ts'o committed
248
	 * Reserved for the use of the calling application.
Theodore Ts'o's avatar
Theodore Ts'o committed
249
	 */
Theodore Ts'o's avatar
Theodore Ts'o committed
250
	void *				priv_data;
Theodore Ts'o's avatar
Theodore Ts'o committed
251 252 253 254 255

	/*
	 * Inode cache
	 */
	struct ext2_inode_cache		*icache;
256
	io_channel			image_io;
257

258 259 260 261 262 263
	/*
	 * More callback functions
	 */
	errcode_t (*get_alloc_block)(ext2_filsys fs, blk64_t goal,
				     blk64_t *ret);
	void (*block_alloc_stats)(ext2_filsys fs, blk64_t blk, int inuse);
264 265 266 267 268 269 270 271 272 273 274 275

	/*
	 * Buffers for Multiple mount protection(MMP) block.
	 */
	void *mmp_buf;
	void *mmp_cmp;
	int mmp_fd;

	/*
	 * Time at which e2fsck last updated the MMP block.
	 */
	long mmp_last_written;
Theodore Ts'o's avatar
Theodore Ts'o committed
276 277
};

278 279 280
#if EXT2_FLAT_INCLUDES
#include "e2_bitops.h"
#else
Theodore Ts'o's avatar
Theodore Ts'o committed
281
#include <ext2fs/bitops.h>
282
#endif
Theodore Ts'o's avatar
Theodore Ts'o committed
283

284 285 286 287 288 289 290
/*
 * 64-bit bitmap backend types
 */
#define EXT2FS_BMAP64_BITARRAY	1
#define EXT2FS_BMAP64_RBTREE	2
#define EXT2FS_BMAP64_AUTODIR	3

Theodore Ts'o's avatar
Theodore Ts'o committed
291 292 293 294 295 296 297 298 299
/*
 * 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
300 301 302 303 304 305
 *
 * 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".
306
 *
307 308 309
 * 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
310 311
 * This is useful if you are going to be deallocating blocks from an
 * inode.
Theodore Ts'o's avatar
Theodore Ts'o committed
312 313 314
 *
 * 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
315
 *
316
 * BLOCK_FLAG_READ_ONLY is a promise by the caller that it will not
317 318
 * modify returned block number.
 *
Theodore Ts'o's avatar
Theodore Ts'o committed
319
 * BLOCK_FLAG_NO_LARGE is for internal use only.  It informs
Theodore Ts'o's avatar
Theodore Ts'o committed
320
 * ext2fs_block_iterate2 that large files won't be accepted.
Theodore Ts'o's avatar
Theodore Ts'o committed
321 322
 */
#define BLOCK_FLAG_APPEND	1
Theodore Ts'o's avatar
Theodore Ts'o committed
323
#define BLOCK_FLAG_HOLE		1
Theodore Ts'o's avatar
Theodore Ts'o committed
324
#define BLOCK_FLAG_DEPTH_TRAVERSE	2
Theodore Ts'o's avatar
Theodore Ts'o committed
325
#define BLOCK_FLAG_DATA_ONLY	4
326
#define BLOCK_FLAG_READ_ONLY	8
Theodore Ts'o's avatar
Theodore Ts'o committed
327

Theodore Ts'o's avatar
Theodore Ts'o committed
328 329
#define BLOCK_FLAG_NO_LARGE	0x1000

Theodore Ts'o's avatar
Theodore Ts'o committed
330 331 332 333 334 335 336
/*
 * 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
337

338
#if 0
Theodore Ts'o's avatar
Theodore Ts'o committed
339 340 341
/*
 * Flags for ext2fs_move_blocks
 */
342
#define EXT2_BMOVE_GET_DBLIST	0x0001
Theodore Ts'o's avatar
Theodore Ts'o committed
343
#define EXT2_BMOVE_DEBUG	0x0002
344
#endif
Theodore Ts'o's avatar
Theodore Ts'o committed
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
/*
 * 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()
 */
386 387
#define EXT2_EXTENT_INSERT_AFTER	0x0001 /* insert after handle loc'n */
#define EXT2_EXTENT_INSERT_NOSPLIT	0x0002 /* insert may not cause split */
388

389 390 391 392 393
/*
 * Flags used by ext2fs_extent_delete()
 */
#define EXT2_EXTENT_DELETE_KEEP_EMPTY	0x001 /* keep node if last extnt gone */

394 395 396 397 398
/*
 * Flags used by ext2fs_extent_set_bmap()
 */
#define EXT2_EXTENT_SET_BMAP_UNINIT	0x0001

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
/*
 * 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;
};

415 416 417 418 419
/*
 * Flags for directory block reading and writing functions
 */
#define EXT2_DIRBLOCK_V2_STRUCT	0x0001

Theodore Ts'o's avatar
Theodore Ts'o committed
420 421 422 423 424 425 426 427 428 429 430 431
/*
 * 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
432
#define DIRENT_FLAG_INCLUDE_REMOVED	2
Theodore Ts'o's avatar
Theodore Ts'o committed
433 434 435 436

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

Theodore Ts'o's avatar
Theodore Ts'o committed
439 440 441
/*
 * Inode scan definitions
 */
Theodore Ts'o's avatar
Theodore Ts'o committed
442 443
typedef struct ext2_struct_inode_scan *ext2_inode_scan;

Theodore Ts'o's avatar
Theodore Ts'o committed
444 445 446 447 448 449 450
/*
 * 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
451
#define EXT2_SF_DO_LAZY		0x0010
Theodore Ts'o's avatar
Theodore Ts'o committed
452

Theodore Ts'o's avatar
Theodore Ts'o committed
453 454 455 456 457
/*
 * ext2fs_check_if_mounted flags
 */
#define EXT2_MF_MOUNTED		1
#define EXT2_MF_ISROOT		2
Theodore Ts'o's avatar
Theodore Ts'o committed
458
#define EXT2_MF_READONLY	4
459
#define EXT2_MF_SWAP		8
460
#define EXT2_MF_BUSY		16
Theodore Ts'o's avatar
Theodore Ts'o committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

/*
 * 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
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
#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
494 495 496 497 498 499 500 501
#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)

502 503 504 505 506
/*
 * 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
507 508 509 510 511 512 513
/*
 * 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
514 515 516
/*
 * Flags for ext2fs_bmap
 */
517 518
#define BMAP_ALLOC	0x0001
#define BMAP_SET	0x0002
Theodore Ts'o's avatar
Theodore Ts'o committed
519

520 521 522 523 524
/*
 * Returned flags from ext2fs_bmap
 */
#define BMAP_RET_UNINIT	0x0001

525 526 527 528 529 530
/*
 * Flags for imager.c functions
 */
#define IMAGER_FLAG_INODEMAP	1
#define IMAGER_FLAG_SPARSEWRITE	2

Theodore Ts'o's avatar
Theodore Ts'o committed
531 532 533
/*
 * For checking structure magic numbers...
 */
Theodore Ts'o's avatar
Theodore Ts'o committed
534

Theodore Ts'o's avatar
Theodore Ts'o committed
535 536
#define EXT2_CHECK_MAGIC(struct, code) \
	  if ((struct)->magic != (code)) return (code)
Theodore Ts'o's avatar
Theodore Ts'o committed
537 538 539


/*
Theodore Ts'o's avatar
Theodore Ts'o committed
540
 * For ext2 compression support
Theodore Ts'o's avatar
Theodore Ts'o committed
541
 */
542
#define EXT2FS_COMPRESSED_BLKADDR ((blk_t) -1)
Theodore Ts'o's avatar
Theodore Ts'o committed
543 544
#define HOLE_BLKADDR(_b) ((_b) == 0 || (_b) == EXT2FS_COMPRESSED_BLKADDR)

Theodore Ts'o's avatar
Theodore Ts'o committed
545
/*
Theodore Ts'o's avatar
Theodore Ts'o committed
546
 * Features supported by this version of the library
Theodore Ts'o's avatar
Theodore Ts'o committed
547
 */
548 549
#define EXT2_LIB_FEATURE_COMPAT_SUPP	(EXT2_FEATURE_COMPAT_DIR_PREALLOC|\
					 EXT2_FEATURE_COMPAT_IMAGIC_INODES|\
550
					 EXT3_FEATURE_COMPAT_HAS_JOURNAL|\
551
					 EXT2_FEATURE_COMPAT_RESIZE_INODE|\
552
					 EXT2_FEATURE_COMPAT_DIR_INDEX|\
553
					 EXT2_FEATURE_COMPAT_EXT_ATTR)
Theodore Ts'o's avatar
Theodore Ts'o committed
554

Theodore Ts'o's avatar
Theodore Ts'o committed
555 556
/* This #ifdef is temporary until compression is fully supported */
#ifdef ENABLE_COMPRESSION
Theodore Ts'o's avatar
Theodore Ts'o committed
557 558 559 560
#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
561
 #warning "Compression support is experimental"
Theodore Ts'o's avatar
Theodore Ts'o committed
562
#endif
Theodore Ts'o's avatar
Theodore Ts'o committed
563
#define EXT2_LIB_FEATURE_INCOMPAT_SUPP	(EXT2_FEATURE_INCOMPAT_FILETYPE|\
Theodore Ts'o's avatar
Theodore Ts'o committed
564
					 EXT2_FEATURE_INCOMPAT_COMPRESSION|\
565
					 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|\
566
					 EXT2_FEATURE_INCOMPAT_META_BG|\
567
					 EXT3_FEATURE_INCOMPAT_RECOVER|\
568
					 EXT3_FEATURE_INCOMPAT_EXTENTS|\
569 570 571
					 EXT4_FEATURE_INCOMPAT_FLEX_BG|\
					 EXT4_FEATURE_INCOMPAT_MMP|\
					 EXT4_FEATURE_INCOMPAT_64BIT)
Theodore Ts'o's avatar
Theodore Ts'o committed
572
#else
Theodore Ts'o's avatar
Theodore Ts'o committed
573
#define EXT2_LIB_FEATURE_INCOMPAT_SUPP	(EXT2_FEATURE_INCOMPAT_FILETYPE|\
574
					 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|\
575
					 EXT2_FEATURE_INCOMPAT_META_BG|\
576
					 EXT3_FEATURE_INCOMPAT_RECOVER|\
577
					 EXT3_FEATURE_INCOMPAT_EXTENTS|\
578 579 580
					 EXT4_FEATURE_INCOMPAT_FLEX_BG|\
					 EXT4_FEATURE_INCOMPAT_MMP|\
					 EXT4_FEATURE_INCOMPAT_64BIT)
Theodore Ts'o's avatar
Theodore Ts'o committed
581
#endif
582
#ifdef CONFIG_QUOTA
Theodore Ts'o's avatar
Theodore Ts'o committed
583
#define EXT2_LIB_FEATURE_RO_COMPAT_SUPP	(EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|\
584
					 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|\
585
					 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|\
586
					 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|\
587
					 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|\
588 589 590 591 592 593 594 595 596 597 598 599
					 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|\
					 EXT4_FEATURE_RO_COMPAT_BIGALLOC|\
					 EXT4_FEATURE_RO_COMPAT_QUOTA)
#else
#define EXT2_LIB_FEATURE_RO_COMPAT_SUPP	(EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|\
					 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|\
					 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|\
					 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|\
					 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|\
					 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|\
					 EXT4_FEATURE_RO_COMPAT_BIGALLOC)
#endif
600 601 602 603 604

/*
 * These features are only allowed if EXT2_FLAG_SOFTSUPP_FEATURES is passed
 * to ext2fs_openfs()
 */
605
#define EXT2_LIB_SOFTSUPP_INCOMPAT	(0)
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
#define EXT2_LIB_SOFTSUPP_RO_COMPAT	(EXT4_FEATURE_RO_COMPAT_REPLICA)


/* Translate a block number to a cluster number */
#define EXT2FS_CLUSTER_RATIO(fs)	(1 << (fs)->cluster_ratio_bits)
#define EXT2FS_CLUSTER_MASK(fs)		(EXT2FS_CLUSTER_RATIO(fs) - 1)
#define EXT2FS_B2C(fs, blk)		((blk) >> (fs)->cluster_ratio_bits)
/* Translate a cluster number to a block number */
#define EXT2FS_C2B(fs, cluster)		((cluster) << (fs)->cluster_ratio_bits)
/* Translate # of blks to # of clusters */
#define EXT2FS_NUM_B2C(fs, blks)	(((blks) + EXT2FS_CLUSTER_MASK(fs)) >> \
					 (fs)->cluster_ratio_bits)

#if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
typedef struct stat64 ext2fs_struct_stat;
#else
typedef struct stat ext2fs_struct_stat;
#endif

/*
 * For ext2fs_close2() and ext2fs_flush2(), this flag allows you to
 * avoid the fsync call.
 */
#define EXT2_FLAG_FLUSH_NO_SYNC          1
630

Theodore Ts'o's avatar
Theodore Ts'o committed
631 632 633 634
/*
 * function prototypes
 */

635 636 637 638 639 640
/* The LARGE_FILE feature should be set if we have stored files 2GB+ in size */
static inline int ext2fs_needs_large_file_feature(unsigned long long file_size)
{
	return file_size >= 0x80000000ULL;
}

Theodore Ts'o's avatar
Theodore Ts'o committed
641
/* alloc.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
642 643
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
644
extern errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
Theodore Ts'o's avatar
Theodore Ts'o committed
645
				  ext2fs_block_bitmap map, blk_t *ret);
646 647
extern errcode_t ext2fs_new_block2(ext2_filsys fs, blk64_t goal,
				   ext2fs_block_bitmap map, blk64_t *ret);
Theodore Ts'o's avatar
Theodore Ts'o committed
648
extern errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start,
Theodore Ts'o's avatar
Theodore Ts'o committed
649 650
					blk_t finish, int num,
					ext2fs_block_bitmap map,
Theodore Ts'o's avatar
Theodore Ts'o committed
651
					blk_t *ret);
652 653 654 655
extern errcode_t ext2fs_get_free_blocks2(ext2_filsys fs, blk64_t start,
					 blk64_t finish, int num,
					 ext2fs_block_bitmap map,
					 blk64_t *ret);
Theodore Ts'o's avatar
Theodore Ts'o committed
656 657
extern errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
				    char *block_buf, blk_t *ret);
658 659
extern errcode_t ext2fs_alloc_block2(ext2_filsys fs, blk64_t goal,
				     char *block_buf, blk64_t *ret);
660
extern void ext2fs_set_alloc_block_callback(ext2_filsys fs,
661 662 663 664 665 666
					    errcode_t (*func)(ext2_filsys fs,
							      blk64_t goal,
							      blk64_t *ret),
					    errcode_t (**old)(ext2_filsys fs,
							      blk64_t goal,
							      blk64_t *ret));
Theodore Ts'o's avatar
Theodore Ts'o committed
667

668
/* alloc_sb.c */
669
extern int ext2fs_reserve_super_and_bgd(ext2_filsys fs,
670 671
					dgrp_t group,
					ext2fs_block_bitmap bmap);
672
extern void ext2fs_set_block_alloc_stats_callback(ext2_filsys fs,
673 674 675 676 677 678
						  void (*func)(ext2_filsys fs,
							       blk64_t blk,
							       int inuse),
						  void (**old)(ext2_filsys fs,
							       blk64_t blk,
							       int inuse));
679

680 681
/* alloc_stats.c */
void ext2fs_inode_alloc_stats(ext2_filsys fs, ext2_ino_t ino, int inuse);
682 683
void ext2fs_inode_alloc_stats2(ext2_filsys fs, ext2_ino_t ino,
			       int inuse, int isdir);
684
void ext2fs_block_alloc_stats(ext2_filsys fs, blk_t blk, int inuse);
685
void ext2fs_block_alloc_stats2(ext2_filsys fs, blk64_t blk, int inuse);
686

Theodore Ts'o's avatar
Theodore Ts'o committed
687 688
/* alloc_tables.c */
extern errcode_t ext2fs_allocate_tables(ext2_filsys fs);
Theodore Ts'o's avatar
Theodore Ts'o committed
689
extern errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
Theodore Ts'o's avatar
Theodore Ts'o committed
690
					     ext2fs_block_bitmap bmap);
Theodore Ts'o's avatar
Theodore Ts'o committed
691

Theodore Ts'o's avatar
Theodore Ts'o committed
692
/* badblocks.c */
693 694
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
695
extern int ext2fs_u32_list_find(ext2_u32_list bb, __u32 blk);
696 697 698 699 700 701 702 703
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
704 705 706 707 708 709
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);
710 711
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
712 713 714 715 716 717
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
718 719
extern errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src,
				       ext2_badblocks_list *dest);
Theodore Ts'o's avatar
Theodore Ts'o committed
720 721
extern int ext2fs_badblocks_equal(ext2_badblocks_list bb1,
				  ext2_badblocks_list bb2);
722
extern int ext2fs_u32_list_count(ext2_u32_list bb);
Theodore Ts'o's avatar
Theodore Ts'o committed
723 724

/* bb_compat */
Theodore Ts'o's avatar
Theodore Ts'o committed
725 726 727 728 729 730 731
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
732
extern void badblocks_list_free(badblocks_list bb);
Theodore Ts'o's avatar
Theodore Ts'o committed
733 734 735

/* bb_inode.c */
extern errcode_t ext2fs_update_bb_inode(ext2_filsys fs,
Theodore Ts'o's avatar
Theodore Ts'o committed
736
					ext2_badblocks_list bb_list);
Theodore Ts'o's avatar
Theodore Ts'o committed
737 738

/* bitmaps.c */
739 740
extern void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap);
extern void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap);
741 742
extern errcode_t ext2fs_copy_bitmap(ext2fs_generic_bitmap src,
				    ext2fs_generic_bitmap *dest);
Theodore Ts'o's avatar
Theodore Ts'o committed
743 744 745 746
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
747 748 749
extern errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
					      const char *descr,
					      ext2fs_block_bitmap *ret);
750 751 752 753
extern errcode_t ext2fs_allocate_subcluster_bitmap(ext2_filsys fs,
						   const char *descr,
						   ext2fs_block_bitmap *ret);
extern int ext2fs_get_bitmap_granularity(ext2fs_block_bitmap bitmap);
Theodore Ts'o's avatar
Theodore Ts'o committed
754 755 756 757
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
758
					       ext2_ino_t end, ext2_ino_t *oend);
Theodore Ts'o's avatar
Theodore Ts'o committed
759 760
extern errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
					       blk_t end, blk_t *oend);
761 762
extern errcode_t ext2fs_fudge_block_bitmap_end2(ext2fs_block_bitmap bitmap,
					 blk64_t end, blk64_t *oend);
Theodore Ts'o's avatar
Theodore Ts'o committed
763 764
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
765 766
extern errcode_t ext2fs_read_bitmaps(ext2_filsys fs);
extern errcode_t ext2fs_write_bitmaps(ext2_filsys fs);
767 768
extern errcode_t ext2fs_resize_inode_bitmap(__u32 new_end, __u32 new_real_end,
					    ext2fs_inode_bitmap bmap);
769 770 771
extern errcode_t ext2fs_resize_inode_bitmap2(__u64 new_end,
					     __u64 new_real_end,
					     ext2fs_inode_bitmap bmap);
772 773
extern errcode_t ext2fs_resize_block_bitmap(__u32 new_end, __u32 new_real_end,
					    ext2fs_block_bitmap bmap);
774 775 776
extern errcode_t ext2fs_resize_block_bitmap2(__u64 new_end,
					     __u64 new_real_end,
					     ext2fs_block_bitmap bmap);
777 778 779 780
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);
781 782 783
extern errcode_t ext2fs_set_inode_bitmap_range(ext2fs_inode_bitmap bmap,
					ext2_ino_t start, unsigned int num,
					void *in);
784 785 786
extern errcode_t ext2fs_set_inode_bitmap_range2(ext2fs_inode_bitmap bmap,
					 __u64 start, size_t num,
					 void *in);
787 788 789
extern errcode_t ext2fs_get_inode_bitmap_range(ext2fs_inode_bitmap bmap,
					ext2_ino_t start, unsigned int num,
					void *out);
790 791 792
extern errcode_t ext2fs_get_inode_bitmap_range2(ext2fs_inode_bitmap bmap,
					 __u64 start, size_t num,
					 void *out);
793 794 795
extern errcode_t ext2fs_set_block_bitmap_range(ext2fs_block_bitmap bmap,
					blk_t start, unsigned int num,
					void *in);
796 797 798
extern errcode_t ext2fs_set_block_bitmap_range2(ext2fs_block_bitmap bmap,
					 blk64_t start, size_t num,
					 void *in);
799 800 801
extern errcode_t ext2fs_get_block_bitmap_range(ext2fs_block_bitmap bmap,
					blk_t start, unsigned int num,
					void *out);
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
extern errcode_t ext2fs_get_block_bitmap_range2(ext2fs_block_bitmap bmap,
					 blk64_t start, size_t num,
					 void *out);

/* blknum.c */
extern dgrp_t ext2fs_group_of_blk2(ext2_filsys fs, blk64_t);
extern blk64_t ext2fs_group_first_block2(ext2_filsys fs, dgrp_t group);
extern blk64_t ext2fs_group_last_block2(ext2_filsys fs, dgrp_t group);
extern int ext2fs_group_blocks_count(ext2_filsys fs, dgrp_t group);
extern blk64_t ext2fs_inode_data_blocks2(ext2_filsys fs,
					 struct ext2_inode *inode);
extern blk64_t ext2fs_inode_i_blocks(ext2_filsys fs,
					 struct ext2_inode *inode);
extern blk64_t ext2fs_blocks_count(struct ext2_super_block *super);
extern void ext2fs_blocks_count_set(struct ext2_super_block *super,
				    blk64_t blk);
extern void ext2fs_blocks_count_add(struct ext2_super_block *super,
				    blk64_t blk);
extern blk64_t ext2fs_r_blocks_count(struct ext2_super_block *super);
extern void ext2fs_r_blocks_count_set(struct ext2_super_block *super,
				      blk64_t blk);
extern void ext2fs_r_blocks_count_add(struct ext2_super_block *super,
				      blk64_t blk);
extern blk64_t ext2fs_free_blocks_count(struct ext2_super_block *super);
extern void ext2fs_free_blocks_count_set(struct ext2_super_block *super,
					 blk64_t blk);
extern void ext2fs_free_blocks_count_add(struct ext2_super_block *super,
					 blk64_t blk);
/* Block group descriptor accessor functions */
extern struct ext2_group_desc *ext2fs_group_desc(ext2_filsys fs,
					  struct opaque_ext2_group_desc *gdp,
					  dgrp_t group);
extern blk64_t ext2fs_block_bitmap_loc(ext2_filsys fs, dgrp_t group);
extern void ext2fs_block_bitmap_loc_set(ext2_filsys fs, dgrp_t group,
					blk64_t blk);
extern blk64_t ext2fs_inode_bitmap_loc(ext2_filsys fs, dgrp_t group);
extern void ext2fs_inode_bitmap_loc_set(ext2_filsys fs, dgrp_t group,
					blk64_t blk);
extern blk64_t ext2fs_inode_table_loc(ext2_filsys fs, dgrp_t group);
extern void ext2fs_inode_table_loc_set(ext2_filsys fs, dgrp_t group,
				       blk64_t blk);
extern __u32 ext2fs_bg_free_blocks_count(ext2_filsys fs, dgrp_t group);
extern void ext2fs_bg_free_blocks_count_set(ext2_filsys fs, dgrp_t group,
					 __u32 n);
extern __u32 ext2fs_bg_free_inodes_count(ext2_filsys fs, dgrp_t group);
extern void ext2fs_bg_free_inodes_count_set(ext2_filsys fs, dgrp_t group,
					 __u32 n);
extern __u32 ext2fs_bg_used_dirs_count(ext2_filsys fs, dgrp_t group);
extern void ext2fs_bg_used_dirs_count_set(ext2_filsys fs, dgrp_t group,
				       __u32 n);
extern __u32 ext2fs_bg_itable_unused(ext2_filsys fs, dgrp_t group);
extern void ext2fs_bg_itable_unused_set(ext2_filsys fs, dgrp_t group,
				     __u32 n);
extern __u16 ext2fs_bg_flags(ext2_filsys fs, dgrp_t group);
extern void ext2fs_bg_flags_zap(ext2_filsys fs, dgrp_t group);
extern int ext2fs_bg_flags_test(ext2_filsys fs, dgrp_t group, __u16 bg_flag);
extern void ext2fs_bg_flags_set(ext2_filsys fs, dgrp_t group, __u16 bg_flags);
extern void ext2fs_bg_flags_clear(ext2_filsys fs, dgrp_t group, __u16 bg_flags);
extern __u16 ext2fs_bg_checksum(ext2_filsys fs, dgrp_t group);
extern void ext2fs_bg_checksum_set(ext2_filsys fs, dgrp_t group, __u16 checksum);
extern blk64_t ext2fs_file_acl_block(ext2_filsys fs,
				     const struct ext2_inode *inode);
extern void ext2fs_file_acl_block_set(ext2_filsys fs,
				      struct ext2_inode *inode, blk64_t blk);
Theodore Ts'o's avatar
Theodore Ts'o committed
866 867 868

/* block.c */
extern errcode_t ext2fs_block_iterate(ext2_filsys fs,
Theodore Ts'o's avatar
Theodore Ts'o committed
869
				      ext2_ino_t	ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
870 871 872 873 874
				      int	flags,
				      char *block_buf,
				      int (*func)(ext2_filsys fs,
						  blk_t	*blocknr,
						  int	blockcnt,
Theodore Ts'o's avatar
Theodore Ts'o committed
875 876
						  void	*priv_data),
				      void *priv_data);
Theodore Ts'o's avatar
Theodore Ts'o committed
877
errcode_t ext2fs_block_iterate2(ext2_filsys fs,
Theodore Ts'o's avatar
Theodore Ts'o committed
878
				ext2_ino_t	ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
879 880 881 882
				int	flags,
				char *block_buf,
				int (*func)(ext2_filsys fs,
					    blk_t	*blocknr,
Theodore Ts'o's avatar
Theodore Ts'o committed
883
					    e2_blkcnt_t	blockcnt,
Theodore Ts'o's avatar
Theodore Ts'o committed
884 885 886 887
					    blk_t	ref_blk,
					    int		ref_offset,
					    void	*priv_data),
				void *priv_data);
888 889 890 891 892 893 894 895 896 897 898
errcode_t ext2fs_block_iterate3(ext2_filsys fs,
				ext2_ino_t ino,
				int	flags,
				char *block_buf,
				int (*func)(ext2_filsys fs,
					    blk64_t	*blocknr,
					    e2_blkcnt_t	blockcnt,
					    blk64_t	ref_blk,
					    int		ref_offset,
					    void	*priv_data),
				void *priv_data);
Theodore Ts'o's avatar
Theodore Ts'o committed
899

Theodore Ts'o's avatar
Theodore Ts'o committed
900
/* bmap.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
901
extern errcode_t ext2fs_bmap(ext2_filsys fs, ext2_ino_t ino,
902
			     struct ext2_inode *inode,
Theodore Ts'o's avatar
Theodore Ts'o committed
903 904
			     char *block_buf, int bmap_flags,
			     blk_t block, blk_t *phys_blk);
905
extern errcode_t ext2fs_bmap2(ext2_filsys fs, ext2_ino_t ino,
906 907 908
			      struct ext2_inode *inode,
			      char *block_buf, int bmap_flags, blk64_t block,
			      int *ret_flags, blk64_t *phys_blk);
909 910 911
errcode_t ext2fs_map_cluster_block(ext2_filsys fs, ext2_ino_t ino,
				   struct ext2_inode *inode, blk64_t lblk,
				   blk64_t *pblk);
Theodore Ts'o's avatar
Theodore Ts'o committed
912

913
#if 0
Theodore Ts'o's avatar
Theodore Ts'o committed
914 915 916
/* bmove.c */
extern errcode_t ext2fs_move_blocks(ext2_filsys fs,
				    ext2fs_block_bitmap reserve,
Theodore Ts'o's avatar
Theodore Ts'o committed
917
				    ext2fs_block_bitmap alloc_map,
Theodore Ts'o's avatar
Theodore Ts'o committed
918
				    int flags);
919
#endif
Theodore Ts'o's avatar
Theodore Ts'o committed
920

Theodore Ts'o's avatar
Theodore Ts'o committed
921 922 923
/* check_desc.c */
extern errcode_t ext2fs_check_desc(ext2_filsys fs);

Theodore Ts'o's avatar
Theodore Ts'o committed
924 925
/* closefs.c */
extern errcode_t ext2fs_close(ext2_filsys fs);
926
extern errcode_t ext2fs_close2(ext2_filsys fs, int flags);
Theodore Ts'o's avatar
Theodore Ts'o committed
927
extern errcode_t ext2fs_flush(ext2_filsys fs);
928 929 930 931 932 933 934 935
extern errcode_t ext2fs_flush2(ext2_filsys fs, int flags);
extern int ext2fs_bg_has_super(ext2_filsys fs, dgrp_t group_block);
extern errcode_t ext2fs_super_and_bgd_loc2(ext2_filsys fs,
				    dgrp_t group,
				    blk64_t *ret_super_blk,
				    blk64_t *ret_old_desc_blk,
				    blk64_t *ret_new_desc_blk,
				    blk_t *ret_used_blks);
936
extern int ext2fs_super_and_bgd_loc(ext2_filsys fs,
937 938 939 940 941
				    dgrp_t group,
				    blk_t *ret_super_blk,
				    blk_t *ret_old_desc_blk,
				    blk_t *ret_new_desc_blk,
				    int *ret_meta_bg);
942
extern void ext2fs_update_dynamic_rev(ext2_filsys fs);
Theodore Ts'o's avatar
Theodore Ts'o committed
943

944 945 946 947
/* crc32c.c */
extern __u32 ext2fs_crc32c_be(__u32 crc, unsigned char const *p, size_t len);
extern __u32 ext2fs_crc32c_le(__u32 crc, unsigned char const *p, size_t len);

948 949 950
/* csum.c */
extern void ext2fs_group_desc_csum_set(ext2_filsys fs, dgrp_t group);
extern int ext2fs_group_desc_csum_verify(ext2_filsys fs, dgrp_t group);
951
extern errcode_t ext2fs_set_gdt_csum(ext2_filsys fs);
952
extern __u16 ext2fs_group_desc_csum(ext2_filsys fs, dgrp_t group);
953

Theodore Ts'o's avatar
Theodore Ts'o committed
954 955
/* dblist.c */

Theodore Ts'o's avatar
Theodore Ts'o committed
956
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
957
extern errcode_t ext2fs_init_dblist(ext2_filsys fs, ext2_dblist *ret_dblist);
Theodore Ts'o's avatar
Theodore Ts'o committed
958
extern errcode_t ext2fs_add_dir_block(ext2_dblist dblist, ext2_ino_t ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
959
				      blk_t blk, int blockcnt);
960 961
extern errcode_t ext2fs_add_dir_block2(ext2_dblist dblist, ext2_ino_t ino,
				       blk64_t blk, e2_blkcnt_t blockcnt);
962 963 964
extern void ext2fs_dblist_sort(ext2_dblist dblist,
			       EXT2_QSORT_TYPE (*sortfunc)(const void *,
							   const void *));
965 966 967
extern void ext2fs_dblist_sort2(ext2_dblist dblist,
				EXT2_QSORT_TYPE (*sortfunc)(const void *,
							    const void *));
Theodore Ts'o's avatar
Theodore Ts'o committed
968 969
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
970 971
		    void	*priv_data),
       void *priv_data);
972 973 974 975
extern errcode_t ext2fs_dblist_iterate2(ext2_dblist dblist,
	int (*func)(ext2_filsys fs, struct ext2_db_entry2 *db_info,
		    void	*priv_data),
       void *priv_data);
Theodore Ts'o's avatar
Theodore Ts'o committed
976
extern errcode_t ext2fs_set_dir_block(ext2_dblist dblist, ext2_ino_t ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
977
				      blk_t blk, int blockcnt);
978 979
extern errcode_t ext2fs_set_dir_block2(ext2_dblist dblist, ext2_ino_t ino,
				       blk64_t blk, e2_blkcnt_t blockcnt);
Theodore Ts'o's avatar
Theodore Ts'o committed
980 981
extern errcode_t ext2fs_copy_dblist(ext2_dblist src,
				    ext2_dblist *dest);
982
extern int ext2fs_dblist_count(ext2_dblist dblist);
983
extern blk64_t ext2fs_dblist_count2(ext2_dblist dblist);
984
extern errcode_t ext2fs_dblist_get_last(ext2_dblist dblist,
985
					struct ext2_db_entry **entry);
986 987
extern errcode_t ext2fs_dblist_get_last2(ext2_dblist dblist,
					struct ext2_db_entry2 **entry);
988
extern errcode_t ext2fs_dblist_drop_last(ext2_dblist dblist);
Theodore Ts'o's avatar
Theodore Ts'o committed
989 990 991 992 993 994

/* 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
995
				  int (*func)(ext2_ino_t	dir,
Theodore Ts'o's avatar
Theodore Ts'o committed
996 997 998 999 1000
					      int		entry,
					      struct ext2_dir_entry *dirent,
					      int	offset,
					      int	blocksize,
					      char	*buf,
Theodore Ts'o's avatar
Theodore Ts'o committed
1001 1002
					      void	*priv_data),
				  void *priv_data);
Theodore Ts'o's avatar
Theodore Ts'o committed
1003 1004 1005 1006

/* dirblock.c */
extern errcode_t ext2fs_read_dir_block(ext2_filsys fs, blk_t block,
				       void *buf);
1007 1008
extern errcode_t ext2fs_read_dir_block2(ext2_filsys fs, blk_t block,
					void *buf, int flags);
1009 1010
extern errcode_t ext2fs_read_dir_block3(ext2_filsys fs, blk64_t block,
					void *buf, int flags);
Theodore Ts'o's avatar
Theodore Ts'o committed
1011 1012
extern errcode_t ext2fs_write_dir_block(ext2_filsys fs, blk_t block,
					void *buf);
1013 1014
extern errcode_t ext2fs_write_dir_block2(ext2_filsys fs, blk_t block,
					 void *buf, int flags);
1015 1016
extern errcode_t ext2fs_write_dir_block3(ext2_filsys fs, blk64_t block,
					 void *buf, int flags);
Theodore Ts'o's avatar
Theodore Ts'o committed
1017

1018 1019
/* dirhash.c */
extern errcode_t ext2fs_dirhash(int version, const char *name, int len,
1020
				const __u32 *seed,
1021 1022
				ext2_dirhash_t *ret_hash,
				ext2_dirhash_t *ret_minor_hash);
1023 1024


Theodore Ts'o's avatar
Theodore Ts'o committed
1025
/* dir_iterate.c */
1026 1027 1028 1029 1030 1031
extern errcode_t ext2fs_get_rec_len(ext2_filsys fs,
				    struct ext2_dir_entry *dirent,
				    unsigned int *rec_len);
extern errcode_t ext2fs_set_rec_len(ext2_filsys fs,
				    unsigned int len,
				    struct ext2_dir_entry *dirent);
1032
extern errcode_t ext2fs_dir_iterate(ext2_filsys fs,
Theodore Ts'o's avatar
Theodore Ts'o committed
1033
			      ext2_ino_t dir,
Theodore Ts'o's avatar
Theodore Ts'o committed
1034 1035 1036 1037 1038 1039
			      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
1040 1041
					  void	*priv_data),
			      void *priv_data);
1042
extern errcode_t ext2fs_dir_iterate2(ext2_filsys fs,
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
			      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
1054

Theodore Ts'o's avatar
Theodore Ts'o committed
1055 1056
/* dupfs.c */
extern errcode_t ext2fs_dup_handle(ext2_filsys src, ext2_filsys *dest);
Theodore Ts'o's avatar
Theodore Ts'o committed
1057

Theodore Ts'o's avatar
Theodore Ts'o committed
1058
/* expanddir.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
1059
extern errcode_t ext2fs_expand_dir(ext2_filsys fs, ext2_ino_t dir);
Theodore Ts'o's avatar
Theodore Ts'o committed
1060

1061
/* ext_attr.c */
1062 1063
extern __u32 ext2fs_ext_attr_hash_entry(struct ext2_ext_attr_entry *entry,
					void *data);
1064
extern errcode_t ext2fs_read_ext_attr(ext2_filsys fs, blk_t block, void *buf);
1065 1066
extern errcode_t ext2fs_read_ext_attr2(ext2_filsys fs, blk64_t block,
				       void *buf);
1067 1068
extern errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block,
				       void *buf);
1069 1070
extern errcode_t ext2fs_write_ext_attr2(ext2_filsys fs, blk64_t block,
				       void *buf);
1071 1072 1073
extern errcode_t ext2fs_adjust_ea_refcount(ext2_filsys fs, blk_t blk,
					   char *block_buf,
					   int adjust, __u32 *newcount);
1074 1075 1076
extern errcode_t ext2fs_adjust_ea_refcount2(ext2_filsys fs, blk64_t blk,
					   char *block_buf,
					   int adjust, __u32 *newcount);
1077

1078 1079 1080 1081
/* 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);
1082 1083 1084
extern errcode_t ext2fs_extent_open2(ext2_filsys fs, ext2_ino_t ino,
					struct ext2_inode *inode,
					ext2_extent_handle_t *ret_handle);
1085
extern void ext2fs_extent_free(ext2_extent_handle_t handle);
1086 1087
extern errcode_t ext2fs_extent_get(ext2_extent_handle_t handle,
				   int flags, struct ext2fs_extent *extent);
1088
extern errcode_t ext2fs_extent_node_split(ext2_extent_handle_t handle);
1089 1090 1091 1092
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);
1093 1094 1095
extern errcode_t ext2fs_extent_set_bmap(ext2_extent_handle_t handle,
					blk64_t logical, blk64_t physical,
					int flags);
1096 1097 1098 1099 1100
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);
1101 1102 1103
extern errcode_t ext2fs_extent_goto2(ext2_extent_handle_t handle,
				     int leaf_level, blk64_t blk);
extern errcode_t ext2fs_extent_fix_parents(ext2_extent_handle_t handle);
1104

Theodore Ts'o's avatar
Theodore Ts'o committed
1105
/* fileio.c */
1106 1107 1108
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
1109
extern errcode_t ext2fs_file_open(ext2_filsys fs, ext2_ino_t ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
1110
				  int flags, ext2_file_t *ret);
1111
extern ext2_filsys ext2fs_file_get_fs(ext2_file_t file);
1112 1113
struct ext2_inode *ext2fs_file_get_inode(ext2_file_t file);
extern ext2_ino_t ext2fs_file_get_inode_num(ext2_file_t file);
Theodore Ts'o's avatar
Theodore Ts'o committed
1114
extern errcode_t ext2fs_file_close(ext2_file_t file);
1115
extern errcode_t ext2fs_file_flush(ext2_file_t file);
Theodore Ts'o's avatar
Theodore Ts'o committed
1116
extern errcode_t ext2fs_file_read(ext2_file_t file, void *buf,
1117
				  unsigned int wanted, unsigned int *got);
1118
extern errcode_t ext2fs_file_write(ext2_file_t file, const void *buf,
1119
				   unsigned int nbytes, unsigned int *written);
1120 1121
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
1122 1123
extern errcode_t ext2fs_file_lseek(ext2_file_t file, ext2_off_t offset,
				   int whence, ext2_off_t *ret_pos);
1124
errcode_t ext2fs_file_get_lsize(ext2_file_t file, __u64 *ret_size);
1125 1126
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);
1127
extern errcode_t ext2fs_file_set_size2(ext2_file_t file, ext2_off64_t size);
Theodore Ts'o's avatar
Theodore Ts'o committed
1128

1129 1130 1131
/* finddev.c */
extern char *ext2fs_find_block_device(dev_t device);

1132 1133 1134
/* flushb.c */
extern errcode_t ext2fs_sync_device(int fd, int flushb);

Theodore Ts'o's avatar
Theodore Ts'o committed
1135 1136
/* freefs.c */
extern void ext2fs_free(ext2_filsys fs);
Theodore Ts'o's avatar
Theodore Ts'o committed
1137
extern void ext2fs_free_dblist(ext2_dblist dblist);
1138 1139
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
1140

1141 1142
/* gen_bitmap.c */
extern void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap);
1143 1144
extern errcode_t ext2fs_make_generic_bitmap(errcode_t magic, ext2_filsys fs,
					    __u32 start, __u32 end,
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
					    __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,
1157
						 errcode_t magic,
1158
						 errcode_t neq,
1159
						 ext2_ino_t end,
1160 1161
						 ext2_ino_t *oend);
extern void ext2fs_set_generic_bitmap_padding(ext2fs_generic_bitmap map);
1162 1163 1164 1165 1166 1167 1168
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);
1169 1170 1171 1172 1173 1174 1175 1176
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);
1177 1178 1179 1180 1181 1182 1183 1184 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
extern errcode_t ext2fs_find_first_zero_generic_bitmap(ext2fs_generic_bitmap bitmap,
						       __u32 start, __u32 end,
						       __u32 *out);

/* gen_bitmap64.c */

/* Generate and print bitmap usage statistics */
#define BMAP_STATS

void ext2fs_free_generic_bmap(ext2fs_generic_bitmap bmap);
errcode_t ext2fs_alloc_generic_bmap(ext2_filsys fs, errcode_t magic,
				    int type, __u64 start, __u64 end,
				    __u64 real_end,
				    const char *descr,
				    ext2fs_generic_bitmap *ret);
errcode_t ext2fs_copy_generic_bmap(ext2fs_generic_bitmap src,
				   ext2fs_generic_bitmap *dest);
void ext2fs_clear_generic_bmap(ext2fs_generic_bitmap bitmap);
errcode_t ext2fs_fudge_generic_bmap_end(ext2fs_generic_bitmap bitmap,
					errcode_t neq,
					__u64 end, __u64 *oend);
void ext2fs_set_generic_bmap_padding(ext2fs_generic_bitmap bmap);
errcode_t ext2fs_resize_generic_bmap(ext2fs_generic_bitmap bmap,
				     __u64 new_end,
				     __u64 new_real_end);
errcode_t ext2fs_compare_generic_bmap(errcode_t neq,
				      ext2fs_generic_bitmap bm1,
				      ext2fs_generic_bitmap bm2);
errcode_t ext2fs_get_generic_bmap_range(ext2fs_generic_bitmap bmap,
					__u64 start, unsigned int num,
					void *out);
errcode_t ext2fs_set_generic_bmap_range(ext2fs_generic_bitmap bmap,
					__u64 start, unsigned int num,
					void *in);
errcode_t ext2fs_convert_subcluster_bitmap(ext2_filsys fs,
					   ext2fs_block_bitmap *bitmap);
1213

Theodore Ts'o's avatar
Theodore Ts'o committed
1214 1215 1216
/* getsize.c */
extern errcode_t ext2fs_get_device_size(const char *file, int blocksize,
					blk_t *retblocks);
Jose R. Santos's avatar
Jose R. Santos committed
1217 1218
extern errcode_t ext2fs_get_device_size2(const char *file, int blocksize,
					blk64_t *retblocks);
Theodore Ts'o's avatar
Theodore Ts'o committed
1219

1220
/* getsectsize.c */
1221
extern int ext2fs_get_dio_alignment(int fd);
1222
errcode_t ext2fs_get_device_sectsize(const char *file, int *sectsize);
1223
errcode_t ext2fs_get_device_phys_sectsize(const char *file, int *sectsize);
1224

1225 1226 1227 1228 1229 1230 1231
/* i_block.c */
errcode_t ext2fs_iblk_add_blocks(ext2_filsys fs, struct ext2_inode *inode,
				 blk64_t num_blocks);
errcode_t ext2fs_iblk_sub_blocks(ext2_filsys fs, struct ext2_inode *inode,
				 blk64_t num_blocks);
errcode_t ext2fs_iblk_set(ext2_filsys fs, struct ext2_inode *inode, blk64_t b);

1232 1233 1234 1235 1236 1237 1238 1239
/* 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);

1240 1241 1242 1243
/* 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
1244 1245 1246 1247 1248
/* initialize.c */
extern errcode_t ext2fs_initialize(const char *name, int flags,
				   struct ext2_super_block *param,
				   io_manager manager, ext2_filsys *ret_fs);

1249 1250
/* icount.c */
extern void ext2fs_free_icount(ext2_icount_t icount);
1251 1252
extern errcode_t ext2fs_create_icount_tdb(ext2_filsys fs, char *tdb_dir,
					  int flags, ext2_icount_t *ret);
1253
extern errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags,
Theodore Ts'o's avatar
Theodore Ts'o committed
1254
				       unsigned int size,
1255
				       ext2_icount_t hint, ext2_icount_t *ret);
1256
extern errcode_t ext2fs_create_icount(ext2_filsys fs, int flags,
Theodore Ts'o's avatar
Theodore Ts'o committed
1257
				      unsigned int size,
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
				      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 *);

1270 1271 1272 1273 1274
/* inline.c */

extern errcode_t ext2fs_get_memalign(unsigned long size,
				     unsigned long align, void *ptr);

Theodore Ts'o's avatar
Theodore Ts'o committed
1275
/* inode.c */
1276
extern errcode_t ext2fs_flush_icache(ext2_filsys fs);
1277
extern errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan,
1278
					    ext2_ino_t *ino,
1279
					    struct ext2_inode *inode,
1280
					    int bufsize);
Theodore Ts'o's avatar
Theodore Ts'o committed
1281 1282 1283
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
1284
extern errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
1285
			       struct ext2_inode *inode);
Theodore Ts'o's avatar
Theodore Ts'o committed
1286 1287 1288 1289 1290 1291 1292
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
1293
				 void * priv_data),
Theodore Ts'o's avatar
Theodore Ts'o committed
1294 1295 1296
	 void *done_group_data);
extern int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
				   int clear_flags);
1297
extern errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino,
1298
					struct ext2_inode * inode,
1299
					int bufsize);
Theodore Ts'o's avatar
Theodore Ts'o committed
1300
extern errcode_t ext2fs_read_inode (ext2_filsys fs, ext2_ino_t ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
1301
			    struct ext2_inode * inode);
1302
extern errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
1303
					 struct ext2_inode * inode,
1304
					 int bufsize);
Theodore Ts'o's avatar
Theodore Ts'o committed
1305
extern errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
Theodore Ts'o's avatar
Theodore Ts'o committed
1306
			    struct ext2_inode * inode);
1307 1308
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
1309 1310
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
1311

1312 1313
/* inode_io.c */
extern io_manager inode_io_manager;
1314
extern errcode_t ext2fs_inode_io_intern(ext2_filsys fs, ext2_ino_t ino,
1315
					char **name);
1316 1317 1318
extern errcode_t ext2fs_inode_io_intern2(ext2_filsys fs, ext2_ino_t ino,
					 struct ext2_inode *inode,
					 char **name);
1319

Theodore Ts'o's avatar
Theodore Ts'o committed
1320 1321
/* ismounted.c */
extern errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags);
1322 1323
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
1324

1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
/* punch.c */
/*
 * NOTE: This function removes from an inode the blocks "start", "end", and
 * every block in between.
 */
extern errcode_t ext2fs_punch(ext2_filsys fs, ext2_ino_t ino,
			      struct ext2_inode *inode,
			      char *block_buf, blk64_t start,
			      blk64_t end);

Theodore Ts'o's avatar
Theodore Ts'o committed
1335
/* namei.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
1336 1337 1338 1339 1340 1341 1342 1343
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
1344 1345 1346

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

/* newdir.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
1349 1350
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
1351 1352

/* mkdir.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
1353
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
1354 1355
			      const char *name);

1356
/* mkjournal.c */
1357 1358
extern errcode_t ext2fs_zero_blocks(ext2_filsys fs, blk_t blk, int num,
				    blk_t *ret_blk, int *ret_count);
1359 1360
extern errcode_t ext2fs_zero_blocks2(ext2_filsys fs, blk64_t blk, int num,
				     blk64_t *ret_blk, int *ret_count);
1361
extern errcode_t ext2fs_create_journal_superblock(ext2_filsys fs,
1362
						  __u32 num_blocks, int flags,
1363 1364 1365
						  char  **ret_jsb);
extern errcode_t ext2fs_add_journal_device(ext2_filsys fs,
					   ext2_filsys journal_dev);
1366
extern errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t num_blocks,
1367
					  int flags);
1368
extern int ext2fs_default_journal_size(__u64 num_blocks);
1369

Theodore Ts'o's avatar
Theodore Ts'o committed
1370 1371
/* openfs.c */
extern errcode_t ext2fs_open(const char *name, int flags, int superblock,
Theodore Ts'o's avatar
Theodore Ts'o committed
1372
			     unsigned int block_size, io_manager manager,
Theodore Ts'o's avatar
Theodore Ts'o committed
1373
			     ext2_filsys *ret_fs);
1374 1375
extern errcode_t ext2fs_open2(const char *name, const char *io_options,
			      int flags, int superblock,
1376 1377
			      unsigned int block_size, io_manager manager,
			      ext2_filsys *ret_fs);
1378 1379
extern blk64_t ext2fs_descriptor_block_loc2(ext2_filsys fs,
					blk64_t group_block, dgrp_t i);
1380
extern blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block,
1381
					 dgrp_t i);
1382 1383 1384
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
1385 1386

/* get_pathname.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
1387
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
1388 1389 1390
			       char **name);

/* link.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
1391 1392 1393 1394
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
1395

1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
/* symlink.c */
errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
			 const char *name, char *target);

/* mmp.c */
errcode_t ext2fs_mmp_read(ext2_filsys fs, blk64_t mmp_blk, void *buf);
errcode_t ext2fs_mmp_write(ext2_filsys fs, blk64_t mmp_blk, void *buf);
errcode_t ext2fs_mmp_clear(ext2_filsys fs);
errcode_t ext2fs_mmp_init(ext2_filsys fs);
errcode_t ext2fs_mmp_start(ext2_filsys fs);
errcode_t ext2fs_mmp_update(ext2_filsys fs);
errcode_t ext2fs_mmp_stop(ext2_filsys fs);
unsigned ext2fs_mmp_new_seq(void);

Theodore Ts'o's avatar
Theodore Ts'o committed
1410
/* read_bb.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
1411 1412
extern errcode_t ext2fs_read_bb_inode(ext2_filsys fs,
				      ext2_badblocks_list *bb_list);
Theodore Ts'o's avatar
Theodore Ts'o committed
1413 1414

/* read_bb_file.c */
1415
extern errcode_t ext2fs_read_bb_FILE2(ext2_filsys fs, FILE *f,
Theodore Ts'o's avatar
Theodore Ts'o committed
1416
				      ext2_badblocks_list *bb_list,
1417
				      void *priv_data,
Theodore Ts'o's avatar
Theodore Ts'o committed
1418 1419 1420
				      void (*invalid)(ext2_filsys fs,
						      blk_t blk,
						      char *badstr,
1421
						      void *priv_data));
1422
extern errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f,
Theodore Ts'o's avatar
Theodore Ts'o committed
1423
				     ext2_badblocks_list *bb_list,
Theodore Ts'o's avatar
Theodore Ts'o committed
1424 1425 1426
				     void (*invalid)(ext2_filsys fs,
						     blk_t blk));

1427 1428 1429
/* res_gdt.c */
extern errcode_t ext2fs_create_resize_inode(ext2_filsys fs);

Theodore Ts'o's avatar
Theodore Ts'o committed
1430
/* swapfs.c */
1431
extern void ext2fs_swap_ext_attr(char *to, char *from, int bufsize,
1432
				 int has_header);
1433 1434 1435 1436
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
1437 1438
extern void ext2fs_swap_super(struct ext2_super_block * super);
extern void ext2fs_swap_group_desc(struct ext2_group_desc *gdp);
1439
extern void ext2fs_swap_group_desc2(ext2_filsys, struct ext2_group_desc *gdp);
1440 1441 1442
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
1443 1444
extern void ext2fs_swap_inode(ext2_filsys fs,struct ext2_inode *t,
			      struct ext2_inode *f, int hostorder);
1445 1446 1447 1448 1449 1450
extern void ext2fs_swap_mmp(struct mmp_struct *mmp);

/* unix_io.c */
extern int ext2fs_open_file(const char *pathname, int flags, mode_t mode);
extern int ext2fs_stat(const char *path, ext2fs_struct_stat *buf);
extern int ext2fs_fstat(int fd, ext2fs_struct_stat *buf);
1451

Theodore Ts'o's avatar
Theodore Ts'o committed
1452
/* valid_blk.c */
Theodore Ts'o's avatar
Theodore Ts'o committed
1453
extern int ext2fs_inode_has_valid_blocks(struct ext2_inode *inode);
1454 1455
extern int ext2fs_inode_has_valid_blocks2(ext2_filsys fs,
					  struct ext2_inode *inode);
Theodore Ts'o's avatar
Theodore Ts'o committed
1456 1457 1458 1459 1460

/* 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
1461

Theodore Ts'o's avatar
Theodore Ts'o committed
1462 1463 1464 1465 1466 1467
/* 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
1468
/* inline functions */
1469
#ifdef NO_INLINE_FUNCS
1470
extern errcode_t ext2fs_get_mem(unsigned long size, void *ptr);
1471 1472 1473 1474 1475
extern errcode_t ext2fs_get_memzero(unsigned long size, void *ptr);
extern errcode_t ext2fs_get_array(unsigned long count,
				  unsigned long size, void *ptr);
extern errcode_t ext2fs_get_arrayzero(unsigned long count,
				      unsigned long size, void *ptr);
1476
extern errcode_t ext2fs_free_mem(void *ptr);
Theodore Ts'o's avatar
Theodore Ts'o committed
1477
extern errcode_t ext2fs_resize_mem(unsigned long old_size,
1478
				   unsigned long size, void *ptr);
Theodore Ts'o's avatar
Theodore Ts'o committed
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
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);
1489 1490
extern dgrp_t ext2fs_group_of_blk(ext2_filsys fs, blk_t blk);
extern dgrp_t ext2fs_group_of_ino(ext2_filsys fs, ext2_ino_t ino);
1491 1492
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);
1493 1494
extern blk_t ext2fs_inode_data_blocks(ext2_filsys fs,
				      struct ext2_inode *inode);
1495
extern unsigned int ext2fs_div_ceil(unsigned int a, unsigned int b);
1496 1497
extern __u64 ext2fs_div64_ceil(__u64 a, __u64 b);
#endif
Theodore Ts'o's avatar
Theodore Ts'o committed
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508

/*
 * 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
1509 1510 1511
#if (__STDC_VERSION__ >= 199901L)
#define _INLINE_ inline
#else
Theodore Ts'o's avatar
Theodore Ts'o committed
1512
#ifdef __GNUC__
Theodore Ts'o's avatar
Theodore Ts'o committed
1513
#define _INLINE_ extern __inline__
Theodore Ts'o's avatar
Theodore Ts'o committed
1514 1515
#else				/* For Watcom C */
#define _INLINE_ extern inline
1516 1517
#endif /* __GNUC__ */
#endif /* __STDC_VERSION__ >= 199901L */
Theodore Ts'o's avatar
Theodore Ts'o committed
1518 1519
#endif

Theodore Ts'o's avatar
Theodore Ts'o committed
1520
#ifndef EXT2_CUSTOM_MEMORY_ROUTINES
1521
#include <string.h>
Theodore Ts'o's avatar
Theodore Ts'o committed
1522
/*
1523
 *  Allocate memory.  The 'ptr' arg must point to a pointer.
Theodore Ts'o's avatar
Theodore Ts'o committed
1524
 */
1525
_INLINE_ errcode_t ext2fs_get_mem(unsigned long size, void *ptr)
Theodore Ts'o's avatar
Theodore Ts'o committed
1526
{
1527
	void *pp;
1528

1529 1530
	pp = malloc(size);
	if (!pp)
1531
		return EXT2_ET_NO_MEMORY;
1532
	memcpy(ptr, &pp, sizeof (pp));
Theodore Ts'o's avatar
Theodore Ts'o committed
1533 1534
	return 0;
}
1535

1536
_INLINE_ errcode_t ext2fs_get_memzero(unsigned long size, void *ptr)
1537
{
1538
	void *pp;
1539

1540 1541
	pp = malloc(size);
	if (!pp)
1542
		return EXT2_ET_NO_MEMORY;
1543 1544
	memset(pp, 0, size);
	memcpy(ptr, &pp, sizeof(pp));
1545 1546 1547
	return 0;
}

1548 1549 1550
_INLINE_ errcode_t ext2fs_get_array(unsigned long count, unsigned long size, void *ptr)
{
	if (count && (-1UL)/count<size)
1551
		return EXT2_ET_NO_MEMORY;
1552 1553
	return ext2fs_get_mem(count*size, ptr);
}
Theodore Ts'o's avatar
Theodore Ts'o committed
1554

1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
_INLINE_ errcode_t ext2fs_get_arrayzero(unsigned long count,
					unsigned long size, void *ptr)
{
	void *pp;

	if (count && (-1UL)/count<size)
		return EXT2_ET_NO_MEMORY;
	pp = calloc(count, size);
	if (!pp)
		return EXT2_ET_NO_MEMORY;
	memcpy(ptr, &pp, sizeof(pp));
	return 0;
}

Theodore Ts'o's avatar
Theodore Ts'o committed
1569
/*
1570
 * Free memory.  The 'ptr' arg must point to a pointer.
Theodore Ts'o's avatar
Theodore Ts'o committed
1571
 */
1572
_INLINE_ errcode_t ext2fs_free_mem(void *ptr)
Theodore Ts'o's avatar
Theodore Ts'o committed
1573
{
1574
	void *p;
1575

1576 1577 1578 1579
	memcpy(&p, ptr, sizeof(p));
	free(p);
	p = 0;
	memcpy(ptr, &p, sizeof(p));
Theodore Ts'o's avatar
Theodore Ts'o committed
1580 1581
	return 0;
}
1582

Theodore Ts'o's avatar
Theodore Ts'o committed
1583
/*
1584
 *  Resize memory.  The 'ptr' arg must point to a pointer.
Theodore Ts'o's avatar
Theodore Ts'o committed
1585
 */
1586
_INLINE_ errcode_t ext2fs_resize_mem(unsigned long EXT2FS_ATTR((unused)) old_size,
1587
				     unsigned long size, void *ptr)
Theodore Ts'o's avatar
Theodore Ts'o committed
1588 1589 1590
{
	void *p;

1591 1592
	/* Use "memcpy" for pointer assignments here to avoid problems
	 * with C99 strict type aliasing rules. */
1593
	memcpy(&p, ptr, sizeof(p));
1594
	p = realloc(p, size);
Theodore Ts'o's avatar
Theodore Ts'o committed
1595
	if (!p)
1596
		return EXT2_ET_NO_MEMORY;
1597
	memcpy(ptr, &p, sizeof(p));
Theodore Ts'o's avatar
Theodore Ts'o committed
1598 1599 1600 1601
	return 0;
}
#endif	/* Custom memory routines */

Theodore Ts'o's avatar
Theodore Ts'o committed
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
/*
 * 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
 */
1685
_INLINE_ dgrp_t ext2fs_group_of_blk(ext2_filsys fs, blk_t blk)
Theodore Ts'o's avatar
Theodore Ts'o committed
1686
{
1687
	return ext2fs_group_of_blk2(fs, blk);
Theodore Ts'o's avatar
Theodore Ts'o committed
1688 1689 1690 1691
}
/*
 * Return the group # of an inode number
 */
1692
_INLINE_ dgrp_t ext2fs_group_of_ino(ext2_filsys fs, ext2_ino_t ino)
Theodore Ts'o's avatar
Theodore Ts'o committed
1693 1694 1695
{
	return (ino - 1) / fs->super->s_inodes_per_group;
}
1696

1697 1698 1699 1700 1701
/*
 * Return the first block (inclusive) in a group
 */
_INLINE_ blk_t ext2fs_group_first_block(ext2_filsys fs, dgrp_t group)
{
1702
	return (blk_t) ext2fs_group_first_block2(fs, group);
1703 1704 1705 1706 1707 1708 1709
}

/*
 * Return the last block (inclusive) in a group
 */
_INLINE_ blk_t ext2fs_group_last_block(ext2_filsys fs, dgrp_t group)
{
1710
	return (blk_t) ext2fs_group_last_block2(fs, group);
1711 1712
}

1713 1714 1715
_INLINE_ blk_t ext2fs_inode_data_blocks(ext2_filsys fs,
					struct ext2_inode *inode)
{
1716
	return (blk_t) ext2fs_inode_data_blocks2(fs, inode);
1717
}
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727

/*
 * 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;
}
1728 1729 1730 1731 1732 1733 1734 1735

_INLINE_ __u64 ext2fs_div64_ceil(__u64 a, __u64 b)
{
	if (!a)
		return 0;
	return ((a - 1) / b) + 1;
}

Theodore Ts'o's avatar
Theodore Ts'o committed
1736 1737 1738
#undef _INLINE_
#endif

Theodore Ts'o's avatar
Theodore Ts'o committed
1739 1740 1741 1742
#ifdef __cplusplus
}
#endif

1743
#endif /* _EXT2FS_EXT2FS_H */