• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * ext2fs.h --- ext2fs
3  *
4  * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11 
12 #ifndef _EXT2FS_EXT2FS_H
13 #define _EXT2FS_EXT2FS_H
14 
15 #ifdef __GNUC__
16 #define EXT2FS_ATTR(x) __attribute__(x)
17 #else
18 #define EXT2FS_ATTR(x)
19 #endif
20 
21 #ifdef CONFIG_TDB
22 #define EXT2FS_NO_TDB_UNUSED
23 #else
24 #define EXT2FS_NO_TDB_UNUSED	EXT2FS_ATTR((unused))
25 #endif
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /*
32  * Non-GNU C compilers won't necessarily understand inline
33  */
34 #if (!defined(__GNUC__) && !defined(__WATCOMC__))
35 #define NO_INLINE_FUNCS
36 #endif
37 
38 /*
39  * Where the master copy of the superblock is located, and how big
40  * superblocks are supposed to be.  We define SUPERBLOCK_SIZE because
41  * the size of the superblock structure is not necessarily trustworthy
42  * (some versions have the padding set up so that the superblock is
43  * 1032 bytes long).
44  */
45 #define SUPERBLOCK_OFFSET	1024
46 #define SUPERBLOCK_SIZE		1024
47 
48 #define UUID_STR_SIZE 37
49 
50 /*
51  * The last ext2fs revision level that this version of the library is
52  * able to support.
53  */
54 #define EXT2_LIB_CURRENT_REV	EXT2_DYNAMIC_REV
55 
56 #ifdef HAVE_SYS_TYPES_H
57 #include <sys/types.h>
58 #endif
59 
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <time.h>
64 #include <errno.h>
65 
66 #if EXT2_FLAT_INCLUDES
67 #include "e2_types.h"
68 #include "ext2_fs.h"
69 #include "ext3_extents.h"
70 #else
71 #include <ext2fs/ext2_types.h>
72 #include <ext2fs/ext2_fs.h>
73 #include <ext2fs/ext3_extents.h>
74 #endif /* EXT2_FLAT_INCLUDES */
75 
76 typedef __u32 __bitwise		ext2_ino_t;
77 typedef __u32 __bitwise		blk_t;
78 typedef __u64 __bitwise		blk64_t;
79 typedef __u32 __bitwise		dgrp_t;
80 typedef __u32 __bitwise		ext2_off_t;
81 typedef __u64 __bitwise		ext2_off64_t;
82 typedef __s64 __bitwise		e2_blkcnt_t;
83 typedef __u32 __bitwise		ext2_dirhash_t;
84 
85 #if EXT2_FLAT_INCLUDES
86 #include "com_err.h"
87 #include "ext2_io.h"
88 #include "ext2_err.h"
89 #include "ext2_ext_attr.h"
90 #else
91 #include <et/com_err.h>
92 #include <ext2fs/ext2_io.h>
93 #include <ext2fs/ext2_err.h>
94 #include <ext2fs/ext2_ext_attr.h>
95 #endif
96 
97 #include "hashmap.h"
98 
99 /*
100  * Portability help for Microsoft Visual C++
101  */
102 #ifdef _MSC_VER
103 #define EXT2_QSORT_TYPE int __cdecl
104 #else
105 #define EXT2_QSORT_TYPE int
106 #endif
107 
108 typedef struct struct_ext2_filsys *ext2_filsys;
109 
110 #define EXT2FS_MARK_ERROR 	0
111 #define EXT2FS_UNMARK_ERROR 	1
112 #define EXT2FS_TEST_ERROR	2
113 
114 struct ext2fs_struct_generic_bitmap_base {
115 	errcode_t		magic;
116 	ext2_filsys 		fs;
117 };
118 
119 typedef struct ext2fs_struct_generic_bitmap_base *ext2fs_generic_bitmap;
120 typedef struct ext2fs_struct_generic_bitmap_base *ext2fs_inode_bitmap;
121 typedef struct ext2fs_struct_generic_bitmap_base *ext2fs_block_bitmap;
122 
123 #define EXT2_FIRST_INODE(s)	EXT2_FIRST_INO(s)
124 
125 
126 /*
127  * Badblocks list definitions
128  */
129 
130 typedef struct ext2_struct_u32_list *ext2_badblocks_list;
131 typedef struct ext2_struct_u32_iterate *ext2_badblocks_iterate;
132 
133 typedef struct ext2_struct_u32_list *ext2_u32_list;
134 typedef struct ext2_struct_u32_iterate *ext2_u32_iterate;
135 
136 /* old */
137 typedef struct ext2_struct_u32_list *badblocks_list;
138 typedef struct ext2_struct_u32_iterate *badblocks_iterate;
139 
140 #define BADBLOCKS_FLAG_DIRTY	1
141 
142 /*
143  * ext2_dblist structure and abstractions (see dblist.c)
144  */
145 struct ext2_db_entry2 {
146 	ext2_ino_t	ino;
147 	blk64_t	blk;
148 	e2_blkcnt_t	blockcnt;
149 };
150 
151 /* Ye Olde 32-bit version */
152 struct ext2_db_entry {
153 	ext2_ino_t	ino;
154 	blk_t	blk;
155 	int	blockcnt;
156 };
157 
158 typedef struct ext2_struct_dblist *ext2_dblist;
159 
160 #define DBLIST_ABORT	1
161 
162 /*
163  * ext2_fileio definitions
164  */
165 
166 #define EXT2_FILE_WRITE		0x0001
167 #define EXT2_FILE_CREATE	0x0002
168 
169 #define EXT2_FILE_MASK		0x00FF
170 
171 #define EXT2_FILE_BUF_DIRTY	0x4000
172 #define EXT2_FILE_BUF_VALID	0x2000
173 
174 typedef struct ext2_file *ext2_file_t;
175 
176 #define EXT2_SEEK_SET	0
177 #define EXT2_SEEK_CUR	1
178 #define EXT2_SEEK_END	2
179 
180 /*
181  * Flags for the ext2_filsys structure and for ext2fs_open()
182  */
183 #define EXT2_FLAG_RW			0x01
184 #define EXT2_FLAG_CHANGED		0x02
185 #define EXT2_FLAG_DIRTY			0x04
186 #define EXT2_FLAG_VALID			0x08
187 #define EXT2_FLAG_IB_DIRTY		0x10
188 #define EXT2_FLAG_BB_DIRTY		0x20
189 #define EXT2_FLAG_SWAP_BYTES		0x40
190 #define EXT2_FLAG_SWAP_BYTES_READ	0x80
191 #define EXT2_FLAG_SWAP_BYTES_WRITE	0x100
192 #define EXT2_FLAG_MASTER_SB_ONLY	0x200
193 #define EXT2_FLAG_FORCE			0x400
194 #define EXT2_FLAG_SUPER_ONLY		0x800
195 #define EXT2_FLAG_JOURNAL_DEV_OK	0x1000
196 #define EXT2_FLAG_IMAGE_FILE		0x2000
197 #define EXT2_FLAG_EXCLUSIVE		0x4000
198 #define EXT2_FLAG_SOFTSUPP_FEATURES	0x8000
199 #define EXT2_FLAG_NOFREE_ON_ERROR	0x10000
200 #define EXT2_FLAG_64BITS		0x20000
201 #define EXT2_FLAG_PRINT_PROGRESS	0x40000
202 #define EXT2_FLAG_DIRECT_IO		0x80000
203 #define EXT2_FLAG_SKIP_MMP		0x100000
204 #define EXT2_FLAG_IGNORE_CSUM_ERRORS	0x200000
205 #define EXT2_FLAG_SHARE_DUP		0x400000
206 #define EXT2_FLAG_IGNORE_SB_ERRORS	0x800000
207 
208 /*
209  * Special flag in the ext2 inode i_flag field that means that this is
210  * a new inode.  (So that ext2_write_inode() can clear extra fields.)
211  */
212 #define EXT2_NEW_INODE_FL	0x80000000
213 
214 /*
215  * Flags for mkjournal
216  */
217 #define EXT2_MKJOURNAL_V1_SUPER	0x0000001 /* create V1 superblock (deprecated) */
218 #define EXT2_MKJOURNAL_LAZYINIT	0x0000002 /* don't zero journal inode before use*/
219 #define EXT2_MKJOURNAL_NO_MNT_CHECK 0x0000004 /* don't check mount status */
220 
221 struct blk_alloc_ctx;
222 struct opaque_ext2_group_desc;
223 
224 struct struct_ext2_filsys {
225 	errcode_t			magic;
226 	io_channel			io;
227 	int				flags;
228 	char *				device_name;
229 	struct ext2_super_block	* 	super;
230 	unsigned int			blocksize;
231 	int				fragsize;
232 	dgrp_t				group_desc_count;
233 	unsigned long			desc_blocks;
234 	struct opaque_ext2_group_desc *	group_desc;
235 	unsigned int			inode_blocks_per_group;
236 	ext2fs_inode_bitmap		inode_map;
237 	ext2fs_block_bitmap		block_map;
238 	/* XXX FIXME-64: not 64-bit safe, but not used? */
239 	errcode_t (*get_blocks)(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks);
240 	errcode_t (*check_directory)(ext2_filsys fs, ext2_ino_t ino);
241 	errcode_t (*write_bitmaps)(ext2_filsys fs);
242 	errcode_t (*read_inode)(ext2_filsys fs, ext2_ino_t ino,
243 				struct ext2_inode *inode);
244 	errcode_t (*write_inode)(ext2_filsys fs, ext2_ino_t ino,
245 				struct ext2_inode *inode);
246 	ext2_badblocks_list		badblocks;
247 	ext2_dblist			dblist;
248 	__u32				stride;	/* for mke2fs */
249 	struct ext2_super_block *	orig_super;
250 	struct ext2_image_hdr *		image_header;
251 	__u32				umask;
252 	time_t				now;
253 	int				cluster_ratio_bits;
254 	__u16				default_bitmap_type;
255 	__u16				pad;
256 	/*
257 	 * Reserved for future expansion
258 	 */
259 	__u32				reserved[5];
260 
261 	/*
262 	 * Reserved for the use of the calling application.
263 	 */
264 	void *				priv_data;
265 
266 	/*
267 	 * Inode cache
268 	 */
269 	struct ext2_inode_cache		*icache;
270 	io_channel			image_io;
271 
272 	/*
273 	 * More callback functions
274 	 */
275 	errcode_t (*get_alloc_block)(ext2_filsys fs, blk64_t goal,
276 				     blk64_t *ret);
277 	errcode_t (*get_alloc_block2)(ext2_filsys fs, blk64_t goal,
278 				      blk64_t *ret, struct blk_alloc_ctx *ctx);
279 	void (*block_alloc_stats)(ext2_filsys fs, blk64_t blk, int inuse);
280 
281 	/*
282 	 * Buffers for Multiple mount protection(MMP) block.
283 	 */
284 	void *mmp_buf;
285 	void *mmp_cmp;
286 	int mmp_fd;
287 
288 	/*
289 	 * Time at which e2fsck last updated the MMP block.
290 	 */
291 	long mmp_last_written;
292 
293 	/* progress operation functions */
294 	struct ext2fs_progress_ops *progress_ops;
295 
296 	/* Precomputed FS UUID checksum for seeding other checksums */
297 	__u32 csum_seed;
298 
299 	io_channel			journal_io;
300 	char				*journal_name;
301 
302 	/* New block range allocation hooks */
303 	errcode_t (*new_range)(ext2_filsys fs, int flags, blk64_t goal,
304 			       blk64_t len, blk64_t *pblk, blk64_t *plen);
305 	void (*block_alloc_stats_range)(ext2_filsys fs, blk64_t blk, blk_t num,
306 					int inuse);
307 
308 	/* hashmap for SHA of data blocks */
309 	struct ext2fs_hashmap* block_sha_map;
310 };
311 
312 #if EXT2_FLAT_INCLUDES
313 #include "e2_bitops.h"
314 #else
315 #include <ext2fs/bitops.h>
316 #endif
317 
318 /*
319  * 64-bit bitmap backend types
320  */
321 #define EXT2FS_BMAP64_BITARRAY	1
322 #define EXT2FS_BMAP64_RBTREE	2
323 #define EXT2FS_BMAP64_AUTODIR	3
324 
325 /*
326  * Return flags for the block iterator functions
327  */
328 #define BLOCK_CHANGED			1
329 #define BLOCK_ABORT			2
330 #define BLOCK_ERROR			4
331 #define BLOCK_INLINE_DATA_CHANGED	8
332 
333 /*
334  * Block interate flags
335  *
336  * BLOCK_FLAG_APPEND, or BLOCK_FLAG_HOLE, indicates that the interator
337  * function should be called on blocks where the block number is zero.
338  * This is used by ext2fs_expand_dir() to be able to add a new block
339  * to an inode.  It can also be used for programs that want to be able
340  * to deal with files that contain "holes".
341  *
342  * BLOCK_FLAG_DEPTH_TRAVERSE indicates that the iterator function for
343  * the indirect, doubly indirect, etc. blocks should be called after
344  * all of the blocks contained in the indirect blocks are processed.
345  * This is useful if you are going to be deallocating blocks from an
346  * inode.
347  *
348  * BLOCK_FLAG_DATA_ONLY indicates that the iterator function should be
349  * called for data blocks only.
350  *
351  * BLOCK_FLAG_READ_ONLY is a promise by the caller that it will not
352  * modify returned block number.
353  *
354  * BLOCK_FLAG_NO_LARGE is for internal use only.  It informs
355  * ext2fs_block_iterate2 that large files won't be accepted.
356  */
357 #define BLOCK_FLAG_APPEND	1
358 #define BLOCK_FLAG_HOLE		1
359 #define BLOCK_FLAG_DEPTH_TRAVERSE	2
360 #define BLOCK_FLAG_DATA_ONLY	4
361 #define BLOCK_FLAG_READ_ONLY	8
362 
363 #define BLOCK_FLAG_NO_LARGE	0x1000
364 
365 /*
366  * Magic "block count" return values for the block iterator function.
367  */
368 #define BLOCK_COUNT_IND		(-1)
369 #define BLOCK_COUNT_DIND	(-2)
370 #define BLOCK_COUNT_TIND	(-3)
371 #define BLOCK_COUNT_TRANSLATOR	(-4)
372 
373 #define BLOCK_ALLOC_UNKNOWN	0
374 #define BLOCK_ALLOC_DATA	1
375 #define BLOCK_ALLOC_METADATA	2
376 
377 struct blk_alloc_ctx {
378 	ext2_ino_t		ino;
379 	struct ext2_inode	*inode;
380 	blk64_t			lblk;
381 	int			flags;
382 };
383 
384 #if 0
385 /*
386  * Flags for ext2fs_move_blocks
387  */
388 #define EXT2_BMOVE_GET_DBLIST	0x0001
389 #define EXT2_BMOVE_DEBUG	0x0002
390 #endif
391 
392 /*
393  * Generic (non-filesystem layout specific) extents structure
394  */
395 
396 #define EXT2_EXTENT_FLAGS_LEAF		0x0001
397 #define EXT2_EXTENT_FLAGS_UNINIT	0x0002
398 #define EXT2_EXTENT_FLAGS_SECOND_VISIT	0x0004
399 
400 struct ext2fs_extent {
401 	blk64_t	e_pblk;		/* first physical block */
402 	blk64_t	e_lblk;		/* first logical block extent covers */
403 	__u32	e_len;		/* number of blocks covered by extent */
404 	__u32	e_flags;	/* extent flags */
405 };
406 
407 typedef struct ext2_extent_handle *ext2_extent_handle_t;
408 typedef struct ext2_extent_path *ext2_extent_path_t;
409 
410 /*
411  * Flags used by ext2fs_extent_get()
412  */
413 #define EXT2_EXTENT_CURRENT	0x0000
414 #define EXT2_EXTENT_MOVE_MASK	0x000F
415 #define EXT2_EXTENT_ROOT	0x0001
416 #define EXT2_EXTENT_LAST_LEAF	0x0002
417 #define EXT2_EXTENT_FIRST_SIB	0x0003
418 #define EXT2_EXTENT_LAST_SIB	0x0004
419 #define EXT2_EXTENT_NEXT_SIB	0x0005
420 #define EXT2_EXTENT_PREV_SIB	0x0006
421 #define EXT2_EXTENT_NEXT_LEAF	0x0007
422 #define EXT2_EXTENT_PREV_LEAF	0x0008
423 #define EXT2_EXTENT_NEXT	0x0009
424 #define EXT2_EXTENT_PREV	0x000A
425 #define EXT2_EXTENT_UP		0x000B
426 #define EXT2_EXTENT_DOWN	0x000C
427 #define EXT2_EXTENT_DOWN_AND_LAST 0x000D
428 
429 /*
430  * Flags used by ext2fs_extent_insert()
431  */
432 #define EXT2_EXTENT_INSERT_AFTER	0x0001 /* insert after handle loc'n */
433 #define EXT2_EXTENT_INSERT_NOSPLIT	0x0002 /* insert may not cause split */
434 
435 /*
436  * Flags used by ext2fs_extent_delete()
437  */
438 #define EXT2_EXTENT_DELETE_KEEP_EMPTY	0x001 /* keep node if last extent gone */
439 
440 /*
441  * Flags used by ext2fs_extent_set_bmap()
442  */
443 #define EXT2_EXTENT_SET_BMAP_UNINIT	0x0001
444 
445 /*
446  * Data structure returned by ext2fs_extent_get_info()
447  */
448 struct ext2_extent_info {
449 	int		curr_entry;
450 	int		curr_level;
451 	int		num_entries;
452 	int		max_entries;
453 	int		max_depth;
454 	int		bytes_avail;
455 	blk64_t		max_lblk;
456 	blk64_t		max_pblk;
457 	__u32		max_len;
458 	__u32		max_uninit_len;
459 };
460 
461 /*
462  * Flags for directory block reading and writing functions
463  */
464 #define EXT2_DIRBLOCK_V2_STRUCT	0x0001
465 
466 /*
467  * Return flags for the directory iterator functions
468  */
469 #define DIRENT_CHANGED	1
470 #define DIRENT_ABORT	2
471 #define DIRENT_ERROR	3
472 
473 /*
474  * Directory iterator flags
475  */
476 
477 #define DIRENT_FLAG_INCLUDE_EMPTY	1
478 #define DIRENT_FLAG_INCLUDE_REMOVED	2
479 #define DIRENT_FLAG_INCLUDE_CSUM	4
480 #define DIRENT_FLAG_INCLUDE_INLINE_DATA 8
481 
482 #define DIRENT_DOT_FILE		1
483 #define DIRENT_DOT_DOT_FILE	2
484 #define DIRENT_OTHER_FILE	3
485 #define DIRENT_DELETED_FILE	4
486 #define DIRENT_CHECKSUM		5
487 
488 /*
489  * Inode scan definitions
490  */
491 typedef struct ext2_struct_inode_scan *ext2_inode_scan;
492 
493 /*
494  * ext2fs_scan flags
495  */
496 #define EXT2_SF_CHK_BADBLOCKS	0x0001
497 #define EXT2_SF_BAD_INODE_BLK	0x0002
498 #define EXT2_SF_BAD_EXTRA_BYTES	0x0004
499 #define EXT2_SF_SKIP_MISSING_ITABLE	0x0008
500 #define EXT2_SF_DO_LAZY		0x0010
501 #define EXT2_SF_WARN_GARBAGE_INODES	0x0020
502 
503 /*
504  * ext2fs_check_if_mounted flags
505  */
506 #define EXT2_MF_MOUNTED		1
507 #define EXT2_MF_ISROOT		2
508 #define EXT2_MF_READONLY	4
509 #define EXT2_MF_SWAP		8
510 #define EXT2_MF_BUSY		16
511 
512 /*
513  * Ext2/linux mode flags.  We define them here so that we don't need
514  * to depend on the OS's sys/stat.h, since we may be compiling on a
515  * non-Linux system.
516  */
517 #define LINUX_S_IFMT  00170000
518 #define LINUX_S_IFSOCK 0140000
519 #define LINUX_S_IFLNK	 0120000
520 #define LINUX_S_IFREG  0100000
521 #define LINUX_S_IFBLK  0060000
522 #define LINUX_S_IFDIR  0040000
523 #define LINUX_S_IFCHR  0020000
524 #define LINUX_S_IFIFO  0010000
525 #define LINUX_S_ISUID  0004000
526 #define LINUX_S_ISGID  0002000
527 #define LINUX_S_ISVTX  0001000
528 
529 #define LINUX_S_IRWXU 00700
530 #define LINUX_S_IRUSR 00400
531 #define LINUX_S_IWUSR 00200
532 #define LINUX_S_IXUSR 00100
533 
534 #define LINUX_S_IRWXG 00070
535 #define LINUX_S_IRGRP 00040
536 #define LINUX_S_IWGRP 00020
537 #define LINUX_S_IXGRP 00010
538 
539 #define LINUX_S_IRWXO 00007
540 #define LINUX_S_IROTH 00004
541 #define LINUX_S_IWOTH 00002
542 #define LINUX_S_IXOTH 00001
543 
544 #define LINUX_S_ISLNK(m)	(((m) & LINUX_S_IFMT) == LINUX_S_IFLNK)
545 #define LINUX_S_ISREG(m)	(((m) & LINUX_S_IFMT) == LINUX_S_IFREG)
546 #define LINUX_S_ISDIR(m)	(((m) & LINUX_S_IFMT) == LINUX_S_IFDIR)
547 #define LINUX_S_ISCHR(m)	(((m) & LINUX_S_IFMT) == LINUX_S_IFCHR)
548 #define LINUX_S_ISBLK(m)	(((m) & LINUX_S_IFMT) == LINUX_S_IFBLK)
549 #define LINUX_S_ISFIFO(m)	(((m) & LINUX_S_IFMT) == LINUX_S_IFIFO)
550 #define LINUX_S_ISSOCK(m)	(((m) & LINUX_S_IFMT) == LINUX_S_IFSOCK)
551 
552 /*
553  * ext2 size of an inode
554  */
555 #define EXT2_I_SIZE(i)	((i)->i_size | ((__u64) (i)->i_size_high << 32))
556 
557 /*
558  * ext2_icount_t abstraction
559  */
560 #define EXT2_ICOUNT_OPT_INCREMENT	0x01
561 #define EXT2_ICOUNT_OPT_FULLMAP		0x02
562 
563 typedef struct ext2_icount *ext2_icount_t;
564 
565 /*
566  * Flags for ext2fs_bmap
567  */
568 #define BMAP_ALLOC	0x0001
569 #define BMAP_SET	0x0002
570 #define BMAP_UNINIT	0x0004
571 #define BMAP_ZERO	0x0008
572 
573 /*
574  * Returned flags from ext2fs_bmap
575  */
576 #define BMAP_RET_UNINIT	0x0001
577 
578 /*
579  * Flags for imager.c functions
580  */
581 #define IMAGER_FLAG_INODEMAP	1
582 #define IMAGER_FLAG_SPARSEWRITE	2
583 
584 /*
585  * For checking structure magic numbers...
586  */
587 
588 #define EXT2_CHECK_MAGIC(struct, code) \
589 	  if ((struct)->magic != (code)) return (code)
590 
591 /*
592  * Features supported by this version of the library
593  */
594 #define EXT2_LIB_FEATURE_COMPAT_SUPP	(EXT2_FEATURE_COMPAT_DIR_PREALLOC|\
595 					 EXT2_FEATURE_COMPAT_IMAGIC_INODES|\
596 					 EXT3_FEATURE_COMPAT_HAS_JOURNAL|\
597 					 EXT2_FEATURE_COMPAT_RESIZE_INODE|\
598 					 EXT2_FEATURE_COMPAT_DIR_INDEX|\
599 					 EXT2_FEATURE_COMPAT_EXT_ATTR|\
600 					 EXT4_FEATURE_COMPAT_SPARSE_SUPER2)
601 
602 #ifdef CONFIG_MMP
603 #define EXT4_LIB_INCOMPAT_MMP		EXT4_FEATURE_INCOMPAT_MMP
604 #else
605 #define EXT4_LIB_INCOMPAT_MMP		(0)
606 #endif
607 
608 #define EXT2_LIB_FEATURE_INCOMPAT_SUPP	(EXT2_FEATURE_INCOMPAT_FILETYPE|\
609 					 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|\
610 					 EXT2_FEATURE_INCOMPAT_META_BG|\
611 					 EXT3_FEATURE_INCOMPAT_RECOVER|\
612 					 EXT3_FEATURE_INCOMPAT_EXTENTS|\
613 					 EXT4_FEATURE_INCOMPAT_FLEX_BG|\
614 					 EXT4_FEATURE_INCOMPAT_EA_INODE|\
615 					 EXT4_LIB_INCOMPAT_MMP|\
616 					 EXT4_FEATURE_INCOMPAT_64BIT|\
617 					 EXT4_FEATURE_INCOMPAT_INLINE_DATA|\
618 					 EXT4_FEATURE_INCOMPAT_ENCRYPT|\
619 					 EXT4_FEATURE_INCOMPAT_CSUM_SEED|\
620 					 EXT4_FEATURE_INCOMPAT_LARGEDIR)
621 
622 #define EXT2_LIB_FEATURE_RO_COMPAT_SUPP	(EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|\
623 					 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|\
624 					 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|\
625 					 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|\
626 					 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|\
627 					 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|\
628 					 EXT4_FEATURE_RO_COMPAT_BIGALLOC|\
629 					 EXT4_FEATURE_RO_COMPAT_QUOTA|\
630 					 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM|\
631 					 EXT4_FEATURE_RO_COMPAT_READONLY |\
632 					 EXT4_FEATURE_RO_COMPAT_PROJECT |\
633 					 EXT4_FEATURE_RO_COMPAT_SHARED_BLOCKS |\
634 					 EXT4_FEATURE_RO_COMPAT_VERITY)
635 
636 /*
637  * These features are only allowed if EXT2_FLAG_SOFTSUPP_FEATURES is passed
638  * to ext2fs_openfs()
639  */
640 #define EXT2_LIB_SOFTSUPP_INCOMPAT	(0)
641 #define EXT2_LIB_SOFTSUPP_RO_COMPAT	(EXT4_FEATURE_RO_COMPAT_REPLICA)
642 
643 
644 /* Translate a block number to a cluster number */
645 #define EXT2FS_CLUSTER_RATIO(fs)	(1 << (fs)->cluster_ratio_bits)
646 #define EXT2FS_CLUSTER_MASK(fs)		(EXT2FS_CLUSTER_RATIO(fs) - 1)
647 #define EXT2FS_B2C(fs, blk)		((blk) >> (fs)->cluster_ratio_bits)
648 /* Translate a cluster number to a block number */
649 #define EXT2FS_C2B(fs, cluster)		((cluster) << (fs)->cluster_ratio_bits)
650 /* Translate # of blks to # of clusters */
651 #define EXT2FS_NUM_B2C(fs, blks)	(((blks) + EXT2FS_CLUSTER_MASK(fs)) >> \
652 					 (fs)->cluster_ratio_bits)
653 
654 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
655 typedef struct stat64 ext2fs_struct_stat;
656 #else
657 typedef struct stat ext2fs_struct_stat;
658 #endif
659 
660 /*
661  * For ext2fs_close2() and ext2fs_flush2(), this flag allows you to
662  * avoid the fsync call.
663  */
664 #define EXT2_FLAG_FLUSH_NO_SYNC          1
665 
666 /*
667  * Modify and iterate extended attributes
668  */
669 struct ext2_xattr_handle;
670 #define XATTR_ABORT	1
671 #define XATTR_CHANGED	2
672 
673 /*
674  * function prototypes
675  */
ext2fs_has_group_desc_csum(ext2_filsys fs)676 static inline int ext2fs_has_group_desc_csum(ext2_filsys fs)
677 {
678 	return ext2fs_has_feature_metadata_csum(fs->super) ||
679 	       ext2fs_has_feature_gdt_csum(fs->super);
680 }
681 
682 /* The LARGE_FILE feature should be set if we have stored files 2GB+ in size */
ext2fs_needs_large_file_feature(unsigned long long file_size)683 static inline int ext2fs_needs_large_file_feature(unsigned long long file_size)
684 {
685 	return file_size >= 0x80000000ULL;
686 }
687 
688 /* alloc.c */
689 extern void ext2fs_clear_block_uninit(ext2_filsys fs, dgrp_t group);
690 extern errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir, int mode,
691 				  ext2fs_inode_bitmap map, ext2_ino_t *ret);
692 extern errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
693 				  ext2fs_block_bitmap map, blk_t *ret);
694 extern errcode_t ext2fs_new_block2(ext2_filsys fs, blk64_t goal,
695 				   ext2fs_block_bitmap map, blk64_t *ret);
696 extern errcode_t ext2fs_new_block3(ext2_filsys fs, blk64_t goal,
697 				   ext2fs_block_bitmap map, blk64_t *ret,
698 				   struct blk_alloc_ctx *ctx);
699 extern errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start,
700 					blk_t finish, int num,
701 					ext2fs_block_bitmap map,
702 					blk_t *ret);
703 extern errcode_t ext2fs_get_free_blocks2(ext2_filsys fs, blk64_t start,
704 					 blk64_t finish, int num,
705 					 ext2fs_block_bitmap map,
706 					 blk64_t *ret);
707 extern errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
708 				    char *block_buf, blk_t *ret);
709 extern errcode_t ext2fs_alloc_block2(ext2_filsys fs, blk64_t goal,
710 				     char *block_buf, blk64_t *ret);
711 extern errcode_t ext2fs_alloc_block3(ext2_filsys fs, blk64_t goal,
712 				     char *block_buf, blk64_t *ret,
713 				     struct blk_alloc_ctx *ctx);
714 
715 extern void ext2fs_set_alloc_block_callback(ext2_filsys fs,
716 					    errcode_t (*func)(ext2_filsys fs,
717 							      blk64_t goal,
718 							      blk64_t *ret),
719 					    errcode_t (**old)(ext2_filsys fs,
720 							      blk64_t goal,
721 							      blk64_t *ret));
722 blk64_t ext2fs_find_inode_goal(ext2_filsys fs, ext2_ino_t ino,
723 			       struct ext2_inode *inode, blk64_t lblk);
724 extern void ext2fs_set_new_range_callback(ext2_filsys fs,
725 	errcode_t (*func)(ext2_filsys fs, int flags, blk64_t goal,
726 			       blk64_t len, blk64_t *pblk, blk64_t *plen),
727 	errcode_t (**old)(ext2_filsys fs, int flags, blk64_t goal,
728 			       blk64_t len, blk64_t *pblk, blk64_t *plen));
729 extern void ext2fs_set_block_alloc_stats_range_callback(ext2_filsys fs,
730 	void (*func)(ext2_filsys fs, blk64_t blk,
731 				    blk_t num, int inuse),
732 	void (**old)(ext2_filsys fs, blk64_t blk,
733 				    blk_t num, int inuse));
734 #define EXT2_NEWRANGE_FIXED_GOAL	(0x1)
735 #define EXT2_NEWRANGE_MIN_LENGTH	(0x2)
736 #define EXT2_NEWRANGE_ALL_FLAGS		(0x3)
737 errcode_t ext2fs_new_range(ext2_filsys fs, int flags, blk64_t goal,
738 			   blk64_t len, ext2fs_block_bitmap map, blk64_t *pblk,
739 			   blk64_t *plen);
740 #define EXT2_ALLOCRANGE_FIXED_GOAL	(0x1)
741 #define EXT2_ALLOCRANGE_ZERO_BLOCKS	(0x2)
742 #define EXT2_ALLOCRANGE_ALL_FLAGS	(0x3)
743 errcode_t ext2fs_alloc_range(ext2_filsys fs, int flags, blk64_t goal,
744 			     blk_t len, blk64_t *ret);
745 
746 /* alloc_sb.c */
747 extern int ext2fs_reserve_super_and_bgd(ext2_filsys fs,
748 					dgrp_t group,
749 					ext2fs_block_bitmap bmap);
750 extern void ext2fs_set_block_alloc_stats_callback(ext2_filsys fs,
751 						  void (*func)(ext2_filsys fs,
752 							       blk64_t blk,
753 							       int inuse),
754 						  void (**old)(ext2_filsys fs,
755 							       blk64_t blk,
756 							       int inuse));
757 
758 /* alloc_stats.c */
759 void ext2fs_inode_alloc_stats(ext2_filsys fs, ext2_ino_t ino, int inuse);
760 void ext2fs_inode_alloc_stats2(ext2_filsys fs, ext2_ino_t ino,
761 			       int inuse, int isdir);
762 void ext2fs_block_alloc_stats(ext2_filsys fs, blk_t blk, int inuse);
763 void ext2fs_block_alloc_stats2(ext2_filsys fs, blk64_t blk, int inuse);
764 void ext2fs_block_alloc_stats_range(ext2_filsys fs, blk64_t blk,
765 				    blk_t num, int inuse);
766 
767 /* alloc_tables.c */
768 extern errcode_t ext2fs_allocate_tables(ext2_filsys fs);
769 extern errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
770 					     ext2fs_block_bitmap bmap);
771 
772 /* badblocks.c */
773 extern errcode_t ext2fs_u32_list_create(ext2_u32_list *ret, int size);
774 extern errcode_t ext2fs_u32_list_add(ext2_u32_list bb, __u32 blk);
775 extern int ext2fs_u32_list_find(ext2_u32_list bb, __u32 blk);
776 extern int ext2fs_u32_list_test(ext2_u32_list bb, blk_t blk);
777 extern errcode_t ext2fs_u32_list_iterate_begin(ext2_u32_list bb,
778 					       ext2_u32_iterate *ret);
779 extern int ext2fs_u32_list_iterate(ext2_u32_iterate iter, blk_t *blk);
780 extern void ext2fs_u32_list_iterate_end(ext2_u32_iterate iter);
781 extern errcode_t ext2fs_u32_copy(ext2_u32_list src, ext2_u32_list *dest);
782 extern int ext2fs_u32_list_equal(ext2_u32_list bb1, ext2_u32_list bb2);
783 
784 extern errcode_t ext2fs_badblocks_list_create(ext2_badblocks_list *ret,
785 					    int size);
786 extern errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb,
787 					   blk_t blk);
788 extern int ext2fs_badblocks_list_test(ext2_badblocks_list bb,
789 				    blk_t blk);
790 extern int ext2fs_u32_list_del(ext2_u32_list bb, __u32 blk);
791 extern void ext2fs_badblocks_list_del(ext2_u32_list bb, __u32 blk);
792 extern errcode_t
793 	ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb,
794 					    ext2_badblocks_iterate *ret);
795 extern int ext2fs_badblocks_list_iterate(ext2_badblocks_iterate iter,
796 					 blk_t *blk);
797 extern void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter);
798 extern errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src,
799 				       ext2_badblocks_list *dest);
800 extern int ext2fs_badblocks_equal(ext2_badblocks_list bb1,
801 				  ext2_badblocks_list bb2);
802 extern int ext2fs_u32_list_count(ext2_u32_list bb);
803 
804 /* bb_compat */
805 extern errcode_t badblocks_list_create(badblocks_list *ret, int size);
806 extern errcode_t badblocks_list_add(badblocks_list bb, blk_t blk);
807 extern int badblocks_list_test(badblocks_list bb, blk_t blk);
808 extern errcode_t badblocks_list_iterate_begin(badblocks_list bb,
809 					      badblocks_iterate *ret);
810 extern int badblocks_list_iterate(badblocks_iterate iter, blk_t *blk);
811 extern void badblocks_list_iterate_end(badblocks_iterate iter);
812 extern void badblocks_list_free(badblocks_list bb);
813 
814 /* bb_inode.c */
815 extern errcode_t ext2fs_update_bb_inode(ext2_filsys fs,
816 					ext2_badblocks_list bb_list);
817 
818 /* bitmaps.c */
819 extern void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap);
820 extern void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap);
821 extern errcode_t ext2fs_copy_bitmap(ext2fs_generic_bitmap src,
822 				    ext2fs_generic_bitmap *dest);
823 extern errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs);
824 extern errcode_t ext2fs_write_block_bitmap (ext2_filsys fs);
825 extern errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs);
826 extern errcode_t ext2fs_read_block_bitmap(ext2_filsys fs);
827 extern errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
828 					      const char *descr,
829 					      ext2fs_block_bitmap *ret);
830 extern errcode_t ext2fs_allocate_subcluster_bitmap(ext2_filsys fs,
831 						   const char *descr,
832 						   ext2fs_block_bitmap *ret);
833 extern int ext2fs_get_bitmap_granularity(ext2fs_block_bitmap bitmap);
834 extern errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
835 					      const char *descr,
836 					      ext2fs_inode_bitmap *ret);
837 extern errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
838 					       ext2_ino_t end, ext2_ino_t *oend);
839 extern errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
840 					       blk_t end, blk_t *oend);
841 extern errcode_t ext2fs_fudge_block_bitmap_end2(ext2fs_block_bitmap bitmap,
842 					 blk64_t end, blk64_t *oend);
843 extern void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap);
844 extern void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap);
845 extern errcode_t ext2fs_read_bitmaps(ext2_filsys fs);
846 extern errcode_t ext2fs_write_bitmaps(ext2_filsys fs);
847 extern errcode_t ext2fs_resize_inode_bitmap(__u32 new_end, __u32 new_real_end,
848 					    ext2fs_inode_bitmap bmap);
849 extern errcode_t ext2fs_resize_inode_bitmap2(__u64 new_end,
850 					     __u64 new_real_end,
851 					     ext2fs_inode_bitmap bmap);
852 extern errcode_t ext2fs_resize_block_bitmap(__u32 new_end, __u32 new_real_end,
853 					    ext2fs_block_bitmap bmap);
854 extern errcode_t ext2fs_resize_block_bitmap2(__u64 new_end,
855 					     __u64 new_real_end,
856 					     ext2fs_block_bitmap bmap);
857 extern errcode_t ext2fs_compare_block_bitmap(ext2fs_block_bitmap bm1,
858 					     ext2fs_block_bitmap bm2);
859 extern errcode_t ext2fs_compare_inode_bitmap(ext2fs_inode_bitmap bm1,
860 					     ext2fs_inode_bitmap bm2);
861 extern errcode_t ext2fs_set_inode_bitmap_range(ext2fs_inode_bitmap bmap,
862 					ext2_ino_t start, unsigned int num,
863 					void *in);
864 extern errcode_t ext2fs_set_inode_bitmap_range2(ext2fs_inode_bitmap bmap,
865 					 __u64 start, size_t num,
866 					 void *in);
867 extern errcode_t ext2fs_get_inode_bitmap_range(ext2fs_inode_bitmap bmap,
868 					ext2_ino_t start, unsigned int num,
869 					void *out);
870 extern errcode_t ext2fs_get_inode_bitmap_range2(ext2fs_inode_bitmap bmap,
871 					 __u64 start, size_t num,
872 					 void *out);
873 extern errcode_t ext2fs_set_block_bitmap_range(ext2fs_block_bitmap bmap,
874 					blk_t start, unsigned int num,
875 					void *in);
876 extern errcode_t ext2fs_set_block_bitmap_range2(ext2fs_block_bitmap bmap,
877 					 blk64_t start, size_t num,
878 					 void *in);
879 extern errcode_t ext2fs_get_block_bitmap_range(ext2fs_block_bitmap bmap,
880 					blk_t start, unsigned int num,
881 					void *out);
882 extern errcode_t ext2fs_get_block_bitmap_range2(ext2fs_block_bitmap bmap,
883 					 blk64_t start, size_t num,
884 					 void *out);
885 
886 /* blknum.c */
887 extern __u32 ext2fs_inode_bitmap_checksum(ext2_filsys fs, dgrp_t group);
888 extern __u32 ext2fs_block_bitmap_checksum(ext2_filsys fs, dgrp_t group);
889 extern dgrp_t ext2fs_group_of_blk2(ext2_filsys fs, blk64_t);
890 extern blk64_t ext2fs_group_first_block2(ext2_filsys fs, dgrp_t group);
891 extern blk64_t ext2fs_group_last_block2(ext2_filsys fs, dgrp_t group);
892 extern int ext2fs_group_blocks_count(ext2_filsys fs, dgrp_t group);
893 extern blk64_t ext2fs_inode_data_blocks2(ext2_filsys fs,
894 					 struct ext2_inode *inode);
895 extern blk64_t ext2fs_inode_i_blocks(ext2_filsys fs,
896 					 struct ext2_inode *inode);
897 extern blk64_t ext2fs_blocks_count(struct ext2_super_block *super);
898 extern void ext2fs_blocks_count_set(struct ext2_super_block *super,
899 				    blk64_t blk);
900 extern void ext2fs_blocks_count_add(struct ext2_super_block *super,
901 				    blk64_t blk);
902 extern blk64_t ext2fs_r_blocks_count(struct ext2_super_block *super);
903 extern void ext2fs_r_blocks_count_set(struct ext2_super_block *super,
904 				      blk64_t blk);
905 extern void ext2fs_r_blocks_count_add(struct ext2_super_block *super,
906 				      blk64_t blk);
907 extern blk64_t ext2fs_free_blocks_count(struct ext2_super_block *super);
908 extern void ext2fs_free_blocks_count_set(struct ext2_super_block *super,
909 					 blk64_t blk);
910 extern void ext2fs_free_blocks_count_add(struct ext2_super_block *super,
911 					 blk64_t blk);
912 /* Block group descriptor accessor functions */
913 extern struct ext2_group_desc *ext2fs_group_desc(ext2_filsys fs,
914 					  struct opaque_ext2_group_desc *gdp,
915 					  dgrp_t group);
916 extern blk64_t ext2fs_block_bitmap_csum(ext2_filsys fs, dgrp_t group);
917 extern blk64_t ext2fs_block_bitmap_loc(ext2_filsys fs, dgrp_t group);
918 extern void ext2fs_block_bitmap_loc_set(ext2_filsys fs, dgrp_t group,
919 					blk64_t blk);
920 extern __u32 ext2fs_inode_bitmap_csum(ext2_filsys fs, dgrp_t group);
921 extern blk64_t ext2fs_inode_bitmap_loc(ext2_filsys fs, dgrp_t group);
922 extern void ext2fs_inode_bitmap_loc_set(ext2_filsys fs, dgrp_t group,
923 					blk64_t blk);
924 extern blk64_t ext2fs_inode_table_loc(ext2_filsys fs, dgrp_t group);
925 extern void ext2fs_inode_table_loc_set(ext2_filsys fs, dgrp_t group,
926 				       blk64_t blk);
927 extern __u32 ext2fs_bg_free_blocks_count(ext2_filsys fs, dgrp_t group);
928 extern void ext2fs_bg_free_blocks_count_set(ext2_filsys fs, dgrp_t group,
929 					 __u32 n);
930 extern __u32 ext2fs_bg_free_inodes_count(ext2_filsys fs, dgrp_t group);
931 extern void ext2fs_bg_free_inodes_count_set(ext2_filsys fs, dgrp_t group,
932 					 __u32 n);
933 extern __u32 ext2fs_bg_used_dirs_count(ext2_filsys fs, dgrp_t group);
934 extern void ext2fs_bg_used_dirs_count_set(ext2_filsys fs, dgrp_t group,
935 				       __u32 n);
936 extern __u32 ext2fs_bg_itable_unused(ext2_filsys fs, dgrp_t group);
937 extern void ext2fs_bg_itable_unused_set(ext2_filsys fs, dgrp_t group,
938 				     __u32 n);
939 extern __u16 ext2fs_bg_flags(ext2_filsys fs, dgrp_t group);
940 extern void ext2fs_bg_flags_zap(ext2_filsys fs, dgrp_t group);
941 extern int ext2fs_bg_flags_test(ext2_filsys fs, dgrp_t group, __u16 bg_flag);
942 extern void ext2fs_bg_flags_set(ext2_filsys fs, dgrp_t group, __u16 bg_flags);
943 extern void ext2fs_bg_flags_clear(ext2_filsys fs, dgrp_t group, __u16 bg_flags);
944 extern __u16 ext2fs_bg_checksum(ext2_filsys fs, dgrp_t group);
945 extern void ext2fs_bg_checksum_set(ext2_filsys fs, dgrp_t group, __u16 checksum);
946 extern blk64_t ext2fs_file_acl_block(ext2_filsys fs,
947 				     const struct ext2_inode *inode);
948 extern void ext2fs_file_acl_block_set(ext2_filsys fs,
949 				      struct ext2_inode *inode, blk64_t blk);
950 extern errcode_t ext2fs_inode_size_set(ext2_filsys fs, struct ext2_inode *inode,
951 				       ext2_off64_t size);
952 
953 /* block.c */
954 extern errcode_t ext2fs_block_iterate(ext2_filsys fs,
955 				      ext2_ino_t	ino,
956 				      int	flags,
957 				      char *block_buf,
958 				      int (*func)(ext2_filsys fs,
959 						  blk_t	*blocknr,
960 						  int	blockcnt,
961 						  void	*priv_data),
962 				      void *priv_data);
963 errcode_t ext2fs_block_iterate2(ext2_filsys fs,
964 				ext2_ino_t	ino,
965 				int	flags,
966 				char *block_buf,
967 				int (*func)(ext2_filsys fs,
968 					    blk_t	*blocknr,
969 					    e2_blkcnt_t	blockcnt,
970 					    blk_t	ref_blk,
971 					    int		ref_offset,
972 					    void	*priv_data),
973 				void *priv_data);
974 errcode_t ext2fs_block_iterate3(ext2_filsys fs,
975 				ext2_ino_t ino,
976 				int	flags,
977 				char *block_buf,
978 				int (*func)(ext2_filsys fs,
979 					    blk64_t	*blocknr,
980 					    e2_blkcnt_t	blockcnt,
981 					    blk64_t	ref_blk,
982 					    int		ref_offset,
983 					    void	*priv_data),
984 				void *priv_data);
985 
986 /* bmap.c */
987 extern errcode_t ext2fs_bmap(ext2_filsys fs, ext2_ino_t ino,
988 			     struct ext2_inode *inode,
989 			     char *block_buf, int bmap_flags,
990 			     blk_t block, blk_t *phys_blk);
991 extern errcode_t ext2fs_bmap2(ext2_filsys fs, ext2_ino_t ino,
992 			      struct ext2_inode *inode,
993 			      char *block_buf, int bmap_flags, blk64_t block,
994 			      int *ret_flags, blk64_t *phys_blk);
995 errcode_t ext2fs_map_cluster_block(ext2_filsys fs, ext2_ino_t ino,
996 				   struct ext2_inode *inode, blk64_t lblk,
997 				   blk64_t *pblk);
998 
999 #if 0
1000 /* bmove.c */
1001 extern errcode_t ext2fs_move_blocks(ext2_filsys fs,
1002 				    ext2fs_block_bitmap reserve,
1003 				    ext2fs_block_bitmap alloc_map,
1004 				    int flags);
1005 #endif
1006 
1007 /* check_desc.c */
1008 extern errcode_t ext2fs_check_desc(ext2_filsys fs);
1009 
1010 /* closefs.c */
1011 extern errcode_t ext2fs_close(ext2_filsys fs);
1012 extern errcode_t ext2fs_close2(ext2_filsys fs, int flags);
1013 extern errcode_t ext2fs_close_free(ext2_filsys *fs);
1014 extern errcode_t ext2fs_flush(ext2_filsys fs);
1015 extern errcode_t ext2fs_flush2(ext2_filsys fs, int flags);
1016 extern int ext2fs_bg_has_super(ext2_filsys fs, dgrp_t group_block);
1017 extern errcode_t ext2fs_super_and_bgd_loc2(ext2_filsys fs,
1018 				    dgrp_t group,
1019 				    blk64_t *ret_super_blk,
1020 				    blk64_t *ret_old_desc_blk,
1021 				    blk64_t *ret_new_desc_blk,
1022 				    blk_t *ret_used_blks);
1023 extern int ext2fs_super_and_bgd_loc(ext2_filsys fs,
1024 				    dgrp_t group,
1025 				    blk_t *ret_super_blk,
1026 				    blk_t *ret_old_desc_blk,
1027 				    blk_t *ret_new_desc_blk,
1028 				    int *ret_meta_bg);
1029 extern void ext2fs_update_dynamic_rev(ext2_filsys fs);
1030 
1031 /* crc32c.c */
1032 extern __u32 ext2fs_crc32_be(__u32 crc, unsigned char const *p, size_t len);
1033 extern __u32 ext2fs_crc32c_le(__u32 crc, unsigned char const *p, size_t len);
1034 
1035 /* csum.c */
1036 extern void ext2fs_init_csum_seed(ext2_filsys fs);
1037 extern errcode_t ext2fs_mmp_csum_set(ext2_filsys fs, struct mmp_struct *mmp);
1038 extern int ext2fs_mmp_csum_verify(ext2_filsys, struct mmp_struct *mmp);
1039 extern int ext2fs_verify_csum_type(ext2_filsys fs, struct ext2_super_block *sb);
1040 extern errcode_t ext2fs_superblock_csum_set(ext2_filsys fs,
1041 					    struct ext2_super_block *sb);
1042 extern int ext2fs_superblock_csum_verify(ext2_filsys fs,
1043 					 struct ext2_super_block *sb);
1044 extern errcode_t ext2fs_ext_attr_block_csum_set(ext2_filsys fs,
1045 					ext2_ino_t inum, blk64_t block,
1046 					struct ext2_ext_attr_header *hdr);
1047 extern int ext2fs_ext_attr_block_csum_verify(ext2_filsys fs, ext2_ino_t inum,
1048 					     blk64_t block,
1049 					     struct ext2_ext_attr_header *hdr);
1050 #define EXT2_DIRENT_TAIL(block, blocksize) \
1051 	((struct ext2_dir_entry_tail *)(((char *)(block)) + \
1052 	(blocksize) - sizeof(struct ext2_dir_entry_tail)))
1053 
1054 extern void ext2fs_initialize_dirent_tail(ext2_filsys fs,
1055 					  struct ext2_dir_entry_tail *t);
1056 extern int ext2fs_dirent_has_tail(ext2_filsys fs,
1057 				  struct ext2_dir_entry *dirent);
1058 extern int ext2fs_dirent_csum_verify(ext2_filsys fs, ext2_ino_t inum,
1059 				     struct ext2_dir_entry *dirent);
1060 extern int ext2fs_dir_block_csum_verify(ext2_filsys fs, ext2_ino_t inum,
1061 					struct ext2_dir_entry *dirent);
1062 extern errcode_t ext2fs_dir_block_csum_set(ext2_filsys fs, ext2_ino_t inum,
1063 					   struct ext2_dir_entry *dirent);
1064 extern errcode_t ext2fs_get_dx_countlimit(ext2_filsys fs,
1065 					  struct ext2_dir_entry *dirent,
1066 					  struct ext2_dx_countlimit **cc,
1067 					  int *offset);
1068 extern errcode_t ext2fs_extent_block_csum_set(ext2_filsys fs,
1069 					      ext2_ino_t inum,
1070 					      struct ext3_extent_header *eh);
1071 extern int ext2fs_extent_block_csum_verify(ext2_filsys fs,
1072 					   ext2_ino_t inum,
1073 					   struct ext3_extent_header *eh);
1074 extern errcode_t ext2fs_block_bitmap_csum_set(ext2_filsys fs, dgrp_t group,
1075 					      char *bitmap, int size);
1076 extern int ext2fs_block_bitmap_csum_verify(ext2_filsys fs, dgrp_t group,
1077 					   char *bitmap, int size);
1078 extern errcode_t ext2fs_inode_bitmap_csum_set(ext2_filsys fs, dgrp_t group,
1079 					      char *bitmap, int size);
1080 extern int ext2fs_inode_bitmap_csum_verify(ext2_filsys fs, dgrp_t group,
1081 					   char *bitmap, int size);
1082 extern errcode_t ext2fs_inode_csum_set(ext2_filsys fs, ext2_ino_t inum,
1083 				       struct ext2_inode_large *inode);
1084 extern int ext2fs_inode_csum_verify(ext2_filsys fs, ext2_ino_t inum,
1085 				    struct ext2_inode_large *inode);
1086 extern void ext2fs_group_desc_csum_set(ext2_filsys fs, dgrp_t group);
1087 extern int ext2fs_group_desc_csum_verify(ext2_filsys fs, dgrp_t group);
1088 extern errcode_t ext2fs_set_gdt_csum(ext2_filsys fs);
1089 extern __u16 ext2fs_group_desc_csum(ext2_filsys fs, dgrp_t group);
1090 
1091 /* dblist.c */
1092 extern errcode_t ext2fs_init_dblist(ext2_filsys fs, ext2_dblist *ret_dblist);
1093 extern errcode_t ext2fs_add_dir_block(ext2_dblist dblist, ext2_ino_t ino,
1094 				      blk_t blk, int blockcnt);
1095 extern errcode_t ext2fs_add_dir_block2(ext2_dblist dblist, ext2_ino_t ino,
1096 				       blk64_t blk, e2_blkcnt_t blockcnt);
1097 extern void ext2fs_dblist_sort(ext2_dblist dblist,
1098 			       EXT2_QSORT_TYPE (*sortfunc)(const void *,
1099 							   const void *));
1100 extern void ext2fs_dblist_sort2(ext2_dblist dblist,
1101 				EXT2_QSORT_TYPE (*sortfunc)(const void *,
1102 							    const void *));
1103 extern errcode_t ext2fs_dblist_iterate(ext2_dblist dblist,
1104 	int (*func)(ext2_filsys fs, struct ext2_db_entry *db_info,
1105 		    void	*priv_data),
1106 	void *priv_data);
1107 extern errcode_t ext2fs_dblist_iterate2(ext2_dblist dblist,
1108 	int (*func)(ext2_filsys fs, struct ext2_db_entry2 *db_info,
1109 		    void	*priv_data),
1110 	void *priv_data);
1111 extern errcode_t ext2fs_dblist_iterate3(ext2_dblist dblist,
1112 	int (*func)(ext2_filsys fs, struct ext2_db_entry2 *db_info,
1113 		    void	*priv_data),
1114 	unsigned long long start,
1115 	unsigned long long count,
1116 	void *priv_data);
1117 extern errcode_t ext2fs_set_dir_block(ext2_dblist dblist, ext2_ino_t ino,
1118 				      blk_t blk, int blockcnt);
1119 extern errcode_t ext2fs_set_dir_block2(ext2_dblist dblist, ext2_ino_t ino,
1120 				       blk64_t blk, e2_blkcnt_t blockcnt);
1121 extern errcode_t ext2fs_copy_dblist(ext2_dblist src,
1122 				    ext2_dblist *dest);
1123 extern int ext2fs_dblist_count(ext2_dblist dblist);
1124 extern blk64_t ext2fs_dblist_count2(ext2_dblist dblist);
1125 extern errcode_t ext2fs_dblist_get_last(ext2_dblist dblist,
1126 					struct ext2_db_entry **entry);
1127 extern errcode_t ext2fs_dblist_get_last2(ext2_dblist dblist,
1128 					struct ext2_db_entry2 **entry);
1129 extern errcode_t ext2fs_dblist_drop_last(ext2_dblist dblist);
1130 
1131 /* dblist_dir.c */
1132 extern errcode_t
1133 	ext2fs_dblist_dir_iterate(ext2_dblist dblist,
1134 				  int	flags,
1135 				  char	*block_buf,
1136 				  int (*func)(ext2_ino_t	dir,
1137 					      int		entry,
1138 					      struct ext2_dir_entry *dirent,
1139 					      int	offset,
1140 					      int	blocksize,
1141 					      char	*buf,
1142 					      void	*priv_data),
1143 				  void *priv_data);
1144 
1145 #if 0
1146 /* digest_encode.c */
1147 #define EXT2FS_DIGEST_SIZE EXT2FS_SHA256_LENGTH
1148 extern int ext2fs_digest_encode(const char *src, int len, char *dst);
1149 extern int ext2fs_digest_decode(const char *src, int len, char *dst);
1150 #endif
1151 
1152 /* dirblock.c */
1153 extern errcode_t ext2fs_read_dir_block(ext2_filsys fs, blk_t block,
1154 				       void *buf);
1155 extern errcode_t ext2fs_read_dir_block2(ext2_filsys fs, blk_t block,
1156 					void *buf, int flags);
1157 extern errcode_t ext2fs_read_dir_block3(ext2_filsys fs, blk64_t block,
1158 					void *buf, int flags);
1159 extern errcode_t ext2fs_read_dir_block4(ext2_filsys fs, blk64_t block,
1160 					void *buf, int flags, ext2_ino_t ino);
1161 extern errcode_t ext2fs_write_dir_block(ext2_filsys fs, blk_t block,
1162 					void *buf);
1163 extern errcode_t ext2fs_write_dir_block2(ext2_filsys fs, blk_t block,
1164 					 void *buf, int flags);
1165 extern errcode_t ext2fs_write_dir_block3(ext2_filsys fs, blk64_t block,
1166 					 void *buf, int flags);
1167 extern errcode_t ext2fs_write_dir_block4(ext2_filsys fs, blk64_t block,
1168 					 void *buf, int flags, ext2_ino_t ino);
1169 
1170 /* dirhash.c */
1171 extern errcode_t ext2fs_dirhash(int version, const char *name, int len,
1172 				const __u32 *seed,
1173 				ext2_dirhash_t *ret_hash,
1174 				ext2_dirhash_t *ret_minor_hash);
1175 
1176 
1177 /* dir_iterate.c */
1178 extern errcode_t ext2fs_get_rec_len(ext2_filsys fs,
1179 				    struct ext2_dir_entry *dirent,
1180 				    unsigned int *rec_len);
1181 extern errcode_t ext2fs_set_rec_len(ext2_filsys fs,
1182 				    unsigned int len,
1183 				    struct ext2_dir_entry *dirent);
1184 extern errcode_t ext2fs_dir_iterate(ext2_filsys fs,
1185 			      ext2_ino_t dir,
1186 			      int flags,
1187 			      char *block_buf,
1188 			      int (*func)(struct ext2_dir_entry *dirent,
1189 					  int	offset,
1190 					  int	blocksize,
1191 					  char	*buf,
1192 					  void	*priv_data),
1193 			      void *priv_data);
1194 extern errcode_t ext2fs_dir_iterate2(ext2_filsys fs,
1195 			      ext2_ino_t dir,
1196 			      int flags,
1197 			      char *block_buf,
1198 			      int (*func)(ext2_ino_t	dir,
1199 					  int	entry,
1200 					  struct ext2_dir_entry *dirent,
1201 					  int	offset,
1202 					  int	blocksize,
1203 					  char	*buf,
1204 					  void	*priv_data),
1205 			      void *priv_data);
1206 
1207 /* dupfs.c */
1208 extern errcode_t ext2fs_dup_handle(ext2_filsys src, ext2_filsys *dest);
1209 
1210 /* expanddir.c */
1211 extern errcode_t ext2fs_expand_dir(ext2_filsys fs, ext2_ino_t dir);
1212 
1213 /* ext_attr.c */
1214 extern __u32 ext2fs_ext_attr_hash_entry(struct ext2_ext_attr_entry *entry,
1215 					void *data);
1216 extern errcode_t ext2fs_ext_attr_hash_entry2(ext2_filsys fs,
1217 					     struct ext2_ext_attr_entry *entry,
1218 					     void *data, __u32 *hash);
1219 extern errcode_t ext2fs_read_ext_attr(ext2_filsys fs, blk_t block, void *buf);
1220 extern errcode_t ext2fs_read_ext_attr2(ext2_filsys fs, blk64_t block,
1221 				       void *buf);
1222 extern errcode_t ext2fs_read_ext_attr3(ext2_filsys fs, blk64_t block,
1223 				       void *buf, ext2_ino_t inum);
1224 extern errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block,
1225 				       void *buf);
1226 extern errcode_t ext2fs_write_ext_attr2(ext2_filsys fs, blk64_t block,
1227 				       void *buf);
1228 extern errcode_t ext2fs_write_ext_attr3(ext2_filsys fs, blk64_t block,
1229 				       void *buf, ext2_ino_t inum);
1230 extern errcode_t ext2fs_adjust_ea_refcount(ext2_filsys fs, blk_t blk,
1231 					   char *block_buf,
1232 					   int adjust, __u32 *newcount);
1233 extern errcode_t ext2fs_adjust_ea_refcount2(ext2_filsys fs, blk64_t blk,
1234 					   char *block_buf,
1235 					   int adjust, __u32 *newcount);
1236 extern errcode_t ext2fs_adjust_ea_refcount3(ext2_filsys fs, blk64_t blk,
1237 					   char *block_buf,
1238 					   int adjust, __u32 *newcount,
1239 					   ext2_ino_t inum);
1240 errcode_t ext2fs_xattrs_write(struct ext2_xattr_handle *handle);
1241 errcode_t ext2fs_xattrs_read(struct ext2_xattr_handle *handle);
1242 errcode_t ext2fs_xattrs_iterate(struct ext2_xattr_handle *h,
1243 				int (*func)(char *name, char *value,
1244 					    size_t value_len, void *data),
1245 				void *data);
1246 errcode_t ext2fs_xattr_get(struct ext2_xattr_handle *h, const char *key,
1247 			   void **value, size_t *value_len);
1248 errcode_t ext2fs_xattr_set(struct ext2_xattr_handle *handle,
1249 			   const char *key,
1250 			   const void *value,
1251 			   size_t value_len);
1252 errcode_t ext2fs_xattr_remove(struct ext2_xattr_handle *handle,
1253 			      const char *key);
1254 errcode_t ext2fs_xattrs_open(ext2_filsys fs, ext2_ino_t ino,
1255 			     struct ext2_xattr_handle **handle);
1256 errcode_t ext2fs_xattrs_close(struct ext2_xattr_handle **handle);
1257 errcode_t ext2fs_free_ext_attr(ext2_filsys fs, ext2_ino_t ino,
1258 			       struct ext2_inode_large *inode);
1259 errcode_t ext2fs_xattrs_count(struct ext2_xattr_handle *handle, size_t *count);
1260 errcode_t ext2fs_xattr_inode_max_size(ext2_filsys fs, ext2_ino_t ino,
1261 				      size_t *size);
1262 #define XATTR_HANDLE_FLAG_RAW	0x0001
1263 errcode_t ext2fs_xattrs_flags(struct ext2_xattr_handle *handle,
1264 			      unsigned int *new_flags, unsigned int *old_flags);
1265 extern void ext2fs_ext_attr_block_rehash(struct ext2_ext_attr_header *header,
1266 					 struct ext2_ext_attr_entry *end);
1267 extern __u32 ext2fs_get_ea_inode_hash(struct ext2_inode *inode);
1268 extern void ext2fs_set_ea_inode_hash(struct ext2_inode *inode, __u32 hash);
1269 extern __u64 ext2fs_get_ea_inode_ref(struct ext2_inode *inode);
1270 extern void ext2fs_set_ea_inode_ref(struct ext2_inode *inode, __u64 ref_count);
1271 
1272 /* extent.c */
1273 extern errcode_t ext2fs_extent_header_verify(void *ptr, int size);
1274 extern errcode_t ext2fs_extent_open(ext2_filsys fs, ext2_ino_t ino,
1275 				    ext2_extent_handle_t *handle);
1276 extern errcode_t ext2fs_extent_open2(ext2_filsys fs, ext2_ino_t ino,
1277 					struct ext2_inode *inode,
1278 					ext2_extent_handle_t *ret_handle);
1279 extern void ext2fs_extent_free(ext2_extent_handle_t handle);
1280 extern errcode_t ext2fs_extent_get(ext2_extent_handle_t handle,
1281 				   int flags, struct ext2fs_extent *extent);
1282 extern errcode_t ext2fs_extent_node_split(ext2_extent_handle_t handle);
1283 extern errcode_t ext2fs_extent_replace(ext2_extent_handle_t handle, int flags,
1284 				       struct ext2fs_extent *extent);
1285 extern errcode_t ext2fs_extent_insert(ext2_extent_handle_t handle, int flags,
1286 				      struct ext2fs_extent *extent);
1287 extern errcode_t ext2fs_extent_set_bmap(ext2_extent_handle_t handle,
1288 					blk64_t logical, blk64_t physical,
1289 					int flags);
1290 extern errcode_t ext2fs_extent_delete(ext2_extent_handle_t handle, int flags);
1291 extern errcode_t ext2fs_extent_get_info(ext2_extent_handle_t handle,
1292 					struct ext2_extent_info *info);
1293 extern errcode_t ext2fs_extent_goto(ext2_extent_handle_t handle,
1294 				    blk64_t blk);
1295 extern errcode_t ext2fs_extent_goto2(ext2_extent_handle_t handle,
1296 				     int leaf_level, blk64_t blk);
1297 extern errcode_t ext2fs_extent_fix_parents(ext2_extent_handle_t handle);
1298 size_t ext2fs_max_extent_depth(ext2_extent_handle_t handle);
1299 
1300 /* fallocate.c */
1301 #define EXT2_FALLOCATE_ZERO_BLOCKS	(0x1)
1302 #define EXT2_FALLOCATE_FORCE_INIT	(0x2)
1303 #define EXT2_FALLOCATE_FORCE_UNINIT	(0x4)
1304 #define EXT2_FALLOCATE_INIT_BEYOND_EOF	(0x8)
1305 #define EXT2_FALLOCATE_ALL_FLAGS	(0xF)
1306 errcode_t ext2fs_fallocate(ext2_filsys fs, int flags, ext2_ino_t ino,
1307 			   struct ext2_inode *inode, blk64_t goal,
1308 			   blk64_t start, blk64_t len);
1309 
1310 /* fileio.c */
1311 extern errcode_t ext2fs_file_open2(ext2_filsys fs, ext2_ino_t ino,
1312 				   struct ext2_inode *inode,
1313 				   int flags, ext2_file_t *ret);
1314 extern errcode_t ext2fs_file_open(ext2_filsys fs, ext2_ino_t ino,
1315 				  int flags, ext2_file_t *ret);
1316 extern ext2_filsys ext2fs_file_get_fs(ext2_file_t file);
1317 struct ext2_inode *ext2fs_file_get_inode(ext2_file_t file);
1318 extern ext2_ino_t ext2fs_file_get_inode_num(ext2_file_t file);
1319 extern errcode_t ext2fs_file_close(ext2_file_t file);
1320 extern errcode_t ext2fs_file_flush(ext2_file_t file);
1321 extern errcode_t ext2fs_file_read(ext2_file_t file, void *buf,
1322 				  unsigned int wanted, unsigned int *got);
1323 extern errcode_t ext2fs_file_write(ext2_file_t file, const void *buf,
1324 				   unsigned int nbytes, unsigned int *written);
1325 extern errcode_t ext2fs_file_llseek(ext2_file_t file, __u64 offset,
1326 				   int whence, __u64 *ret_pos);
1327 extern errcode_t ext2fs_file_lseek(ext2_file_t file, ext2_off_t offset,
1328 				   int whence, ext2_off_t *ret_pos);
1329 errcode_t ext2fs_file_get_lsize(ext2_file_t file, __u64 *ret_size);
1330 extern ext2_off_t ext2fs_file_get_size(ext2_file_t file);
1331 extern errcode_t ext2fs_file_set_size(ext2_file_t file, ext2_off_t size);
1332 extern errcode_t ext2fs_file_set_size2(ext2_file_t file, ext2_off64_t size);
1333 
1334 /* finddev.c */
1335 extern char *ext2fs_find_block_device(dev_t device);
1336 
1337 /* flushb.c */
1338 extern errcode_t ext2fs_sync_device(int fd, int flushb);
1339 
1340 /* freefs.c */
1341 extern void ext2fs_free(ext2_filsys fs);
1342 extern void ext2fs_free_dblist(ext2_dblist dblist);
1343 extern void ext2fs_badblocks_list_free(ext2_badblocks_list bb);
1344 extern void ext2fs_u32_list_free(ext2_u32_list bb);
1345 
1346 /* gen_bitmap.c */
1347 extern void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap);
1348 extern errcode_t ext2fs_make_generic_bitmap(errcode_t magic, ext2_filsys fs,
1349 					    __u32 start, __u32 end,
1350 					    __u32 real_end,
1351 					    const char *descr, char *init_map,
1352 					    ext2fs_generic_bitmap *ret);
1353 extern errcode_t ext2fs_allocate_generic_bitmap(__u32 start,
1354 						__u32 end,
1355 						__u32 real_end,
1356 						const char *descr,
1357 						ext2fs_generic_bitmap *ret);
1358 extern errcode_t ext2fs_copy_generic_bitmap(ext2fs_generic_bitmap src,
1359 					    ext2fs_generic_bitmap *dest);
1360 extern void ext2fs_clear_generic_bitmap(ext2fs_generic_bitmap bitmap);
1361 extern errcode_t ext2fs_fudge_generic_bitmap_end(ext2fs_inode_bitmap bitmap,
1362 						 errcode_t magic,
1363 						 errcode_t neq,
1364 						 ext2_ino_t end,
1365 						 ext2_ino_t *oend);
1366 extern void ext2fs_set_generic_bitmap_padding(ext2fs_generic_bitmap map);
1367 extern errcode_t ext2fs_resize_generic_bitmap(errcode_t magic,
1368 					      __u32 new_end,
1369 					      __u32 new_real_end,
1370 					      ext2fs_generic_bitmap bmap);
1371 extern errcode_t ext2fs_compare_generic_bitmap(errcode_t magic, errcode_t neq,
1372 					       ext2fs_generic_bitmap bm1,
1373 					       ext2fs_generic_bitmap bm2);
1374 extern errcode_t ext2fs_get_generic_bitmap_range(ext2fs_generic_bitmap bmap,
1375 						 errcode_t magic,
1376 						 __u32 start, __u32 num,
1377 						 void *out);
1378 extern errcode_t ext2fs_set_generic_bitmap_range(ext2fs_generic_bitmap bmap,
1379 						 errcode_t magic,
1380 						 __u32 start, __u32 num,
1381 						 void *in);
1382 extern errcode_t ext2fs_find_first_zero_generic_bitmap(ext2fs_generic_bitmap bitmap,
1383 						       __u32 start, __u32 end,
1384 						       __u32 *out);
1385 extern errcode_t ext2fs_find_first_set_generic_bitmap(ext2fs_generic_bitmap bitmap,
1386 						       __u32 start, __u32 end,
1387 						       __u32 *out);
1388 
1389 /* gen_bitmap64.c */
1390 void ext2fs_free_generic_bmap(ext2fs_generic_bitmap bmap);
1391 errcode_t ext2fs_alloc_generic_bmap(ext2_filsys fs, errcode_t magic,
1392 				    int type, __u64 start, __u64 end,
1393 				    __u64 real_end,
1394 				    const char *descr,
1395 				    ext2fs_generic_bitmap *ret);
1396 errcode_t ext2fs_copy_generic_bmap(ext2fs_generic_bitmap src,
1397 				   ext2fs_generic_bitmap *dest);
1398 void ext2fs_clear_generic_bmap(ext2fs_generic_bitmap bitmap);
1399 errcode_t ext2fs_fudge_generic_bmap_end(ext2fs_generic_bitmap bitmap,
1400 					errcode_t neq,
1401 					__u64 end, __u64 *oend);
1402 void ext2fs_set_generic_bmap_padding(ext2fs_generic_bitmap bmap);
1403 errcode_t ext2fs_resize_generic_bmap(ext2fs_generic_bitmap bmap,
1404 				     __u64 new_end,
1405 				     __u64 new_real_end);
1406 errcode_t ext2fs_compare_generic_bmap(errcode_t neq,
1407 				      ext2fs_generic_bitmap bm1,
1408 				      ext2fs_generic_bitmap bm2);
1409 errcode_t ext2fs_get_generic_bmap_range(ext2fs_generic_bitmap bmap,
1410 					__u64 start, unsigned int num,
1411 					void *out);
1412 errcode_t ext2fs_set_generic_bmap_range(ext2fs_generic_bitmap bmap,
1413 					__u64 start, unsigned int num,
1414 					void *in);
1415 errcode_t ext2fs_convert_subcluster_bitmap(ext2_filsys fs,
1416 					   ext2fs_block_bitmap *bitmap);
1417 
1418 /* get_num_dirs.c */
1419 extern errcode_t ext2fs_get_num_dirs(ext2_filsys fs, ext2_ino_t *ret_num_dirs);
1420 
1421 /* getsize.c */
1422 extern errcode_t ext2fs_get_device_size(const char *file, int blocksize,
1423 					blk_t *retblocks);
1424 extern errcode_t ext2fs_get_device_size2(const char *file, int blocksize,
1425 					blk64_t *retblocks);
1426 
1427 /* getsectsize.c */
1428 extern int ext2fs_get_dio_alignment(int fd);
1429 errcode_t ext2fs_get_device_sectsize(const char *file, int *sectsize);
1430 errcode_t ext2fs_get_device_phys_sectsize(const char *file, int *sectsize);
1431 
1432 /* i_block.c */
1433 errcode_t ext2fs_iblk_add_blocks(ext2_filsys fs, struct ext2_inode *inode,
1434 				 blk64_t num_blocks);
1435 errcode_t ext2fs_iblk_sub_blocks(ext2_filsys fs, struct ext2_inode *inode,
1436 				 blk64_t num_blocks);
1437 errcode_t ext2fs_iblk_set(ext2_filsys fs, struct ext2_inode *inode, blk64_t b);
1438 
1439 /* imager.c */
1440 extern errcode_t ext2fs_image_inode_write(ext2_filsys fs, int fd, int flags);
1441 extern errcode_t ext2fs_image_inode_read(ext2_filsys fs, int fd, int flags);
1442 extern errcode_t ext2fs_image_super_write(ext2_filsys fs, int fd, int flags);
1443 extern errcode_t ext2fs_image_super_read(ext2_filsys fs, int fd, int flags);
1444 extern errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags);
1445 extern errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags);
1446 
1447 /* ind_block.c */
1448 errcode_t ext2fs_read_ind_block(ext2_filsys fs, blk_t blk, void *buf);
1449 errcode_t ext2fs_write_ind_block(ext2_filsys fs, blk_t blk, void *buf);
1450 
1451 /* initialize.c */
1452 extern errcode_t ext2fs_initialize(const char *name, int flags,
1453 				   struct ext2_super_block *param,
1454 				   io_manager manager, ext2_filsys *ret_fs);
1455 
1456 /* icount.c */
1457 extern void ext2fs_free_icount(ext2_icount_t icount);
1458 extern errcode_t ext2fs_create_icount_tdb(ext2_filsys fs, char *tdb_dir,
1459 					  int flags, ext2_icount_t *ret);
1460 extern errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags,
1461 				       unsigned int size,
1462 				       ext2_icount_t hint, ext2_icount_t *ret);
1463 extern errcode_t ext2fs_create_icount(ext2_filsys fs, int flags,
1464 				      unsigned int size,
1465 				      ext2_icount_t *ret);
1466 extern errcode_t ext2fs_icount_fetch(ext2_icount_t icount, ext2_ino_t ino,
1467 				     __u16 *ret);
1468 extern errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
1469 					 __u16 *ret);
1470 extern errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
1471 					 __u16 *ret);
1472 extern errcode_t ext2fs_icount_store(ext2_icount_t icount, ext2_ino_t ino,
1473 				     __u16 count);
1474 extern ext2_ino_t ext2fs_get_icount_size(ext2_icount_t icount);
1475 errcode_t ext2fs_icount_validate(ext2_icount_t icount, FILE *);
1476 
1477 /* inline.c */
1478 
1479 extern errcode_t ext2fs_get_memalign(unsigned long size,
1480 				     unsigned long align, void *ptr);
1481 
1482 /* inline_data.c */
1483 extern errcode_t ext2fs_inline_data_init(ext2_filsys fs, ext2_ino_t ino);
1484 extern errcode_t ext2fs_inline_data_size(ext2_filsys fs, ext2_ino_t ino,
1485 					 size_t *size);
1486 extern errcode_t ext2fs_inline_data_get(ext2_filsys fs, ext2_ino_t ino,
1487 					struct ext2_inode *inode,
1488 					void *buf, size_t *size);
1489 extern errcode_t ext2fs_inline_data_set(ext2_filsys fs, ext2_ino_t ino,
1490 					struct ext2_inode *inode,
1491 					void *buf, size_t size);
1492 
1493 /* inode.c */
1494 extern errcode_t ext2fs_create_inode_cache(ext2_filsys fs,
1495 					   unsigned int cache_size);
1496 extern void ext2fs_free_inode_cache(struct ext2_inode_cache *icache);
1497 extern errcode_t ext2fs_flush_icache(ext2_filsys fs);
1498 extern errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan,
1499 					    ext2_ino_t *ino,
1500 					    struct ext2_inode *inode,
1501 					    int bufsize);
1502 #define EXT2_INODE_SCAN_DEFAULT_BUFFER_BLOCKS	8
1503 extern errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
1504 				  ext2_inode_scan *ret_scan);
1505 extern void ext2fs_close_inode_scan(ext2_inode_scan scan);
1506 extern errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino,
1507 			       struct ext2_inode *inode);
1508 extern errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
1509 						   int	group);
1510 extern void ext2fs_set_inode_callback
1511 	(ext2_inode_scan scan,
1512 	 errcode_t (*done_group)(ext2_filsys fs,
1513 				 ext2_inode_scan scan,
1514 				 dgrp_t group,
1515 				 void * priv_data),
1516 	 void *done_group_data);
1517 extern int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
1518 				   int clear_flags);
1519 extern errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino,
1520 					struct ext2_inode * inode,
1521 					int bufsize);
1522 extern errcode_t ext2fs_read_inode (ext2_filsys fs, ext2_ino_t ino,
1523 			    struct ext2_inode * inode);
1524 extern errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
1525 					 struct ext2_inode * inode,
1526 					 int bufsize);
1527 extern errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
1528 			    struct ext2_inode * inode);
1529 extern errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino,
1530 			    struct ext2_inode * inode);
1531 extern errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks);
1532 extern errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino);
1533 
1534 /* inode_io.c */
1535 extern io_manager inode_io_manager;
1536 extern errcode_t ext2fs_inode_io_intern(ext2_filsys fs, ext2_ino_t ino,
1537 					char **name);
1538 extern errcode_t ext2fs_inode_io_intern2(ext2_filsys fs, ext2_ino_t ino,
1539 					 struct ext2_inode *inode,
1540 					 char **name);
1541 
1542 /* ismounted.c */
1543 extern errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags);
1544 extern errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
1545 					  char *mtpt, int mtlen);
1546 
1547 /* punch.c */
1548 /*
1549  * NOTE: This function removes from an inode the blocks "start", "end", and
1550  * every block in between.
1551  */
1552 extern errcode_t ext2fs_punch(ext2_filsys fs, ext2_ino_t ino,
1553 			      struct ext2_inode *inode,
1554 			      char *block_buf, blk64_t start,
1555 			      blk64_t end);
1556 
1557 /* namei.c */
1558 extern errcode_t ext2fs_lookup(ext2_filsys fs, ext2_ino_t dir, const char *name,
1559 			 int namelen, char *buf, ext2_ino_t *inode);
1560 extern errcode_t ext2fs_namei(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
1561 			const char *name, ext2_ino_t *inode);
1562 errcode_t ext2fs_namei_follow(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
1563 			      const char *name, ext2_ino_t *inode);
1564 extern errcode_t ext2fs_follow_link(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
1565 			ext2_ino_t inode, ext2_ino_t *res_inode);
1566 
1567 /* native.c */
1568 int ext2fs_native_flag(void);
1569 
1570 /* newdir.c */
1571 extern errcode_t ext2fs_new_dir_block(ext2_filsys fs, ext2_ino_t dir_ino,
1572 				ext2_ino_t parent_ino, char **block);
1573 extern errcode_t ext2fs_new_dir_inline_data(ext2_filsys fs, ext2_ino_t dir_ino,
1574 				ext2_ino_t parent_ino, __u32 *iblock);
1575 
1576 /* mkdir.c */
1577 extern errcode_t ext2fs_mkdir(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t inum,
1578 			      const char *name);
1579 
1580 /* mkjournal.c */
1581 extern errcode_t ext2fs_zero_blocks(ext2_filsys fs, blk_t blk, int num,
1582 				    blk_t *ret_blk, int *ret_count);
1583 extern errcode_t ext2fs_zero_blocks2(ext2_filsys fs, blk64_t blk, int num,
1584 				     blk64_t *ret_blk, int *ret_count);
1585 extern errcode_t ext2fs_create_journal_superblock(ext2_filsys fs,
1586 						  __u32 num_blocks, int flags,
1587 						  char  **ret_jsb);
1588 extern errcode_t ext2fs_add_journal_device(ext2_filsys fs,
1589 					   ext2_filsys journal_dev);
1590 extern errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t num_blocks,
1591 					  int flags);
1592 extern errcode_t ext2fs_add_journal_inode2(ext2_filsys fs, blk_t num_blocks,
1593 					   blk64_t goal, int flags);
1594 extern int ext2fs_default_journal_size(__u64 num_blocks);
1595 extern int ext2fs_journal_sb_start(int blocksize);
1596 
1597 /* openfs.c */
1598 extern errcode_t ext2fs_open(const char *name, int flags, int superblock,
1599 			     unsigned int block_size, io_manager manager,
1600 			     ext2_filsys *ret_fs);
1601 extern errcode_t ext2fs_open2(const char *name, const char *io_options,
1602 			      int flags, int superblock,
1603 			      unsigned int block_size, io_manager manager,
1604 			      ext2_filsys *ret_fs);
1605 /*
1606  * The dgrp_t argument to these two functions is not actually a group number
1607  * but a block number offset within a group table!  Convert with the formula
1608  * (group_number / groups_per_block).
1609  */
1610 extern blk64_t ext2fs_descriptor_block_loc2(ext2_filsys fs,
1611 					blk64_t group_block, dgrp_t i);
1612 extern blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block,
1613 					 dgrp_t i);
1614 errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io);
1615 errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io);
1616 errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io);
1617 
1618 /* get_pathname.c */
1619 extern errcode_t ext2fs_get_pathname(ext2_filsys fs, ext2_ino_t dir, ext2_ino_t ino,
1620 			       char **name);
1621 
1622 /* link.c */
1623 errcode_t ext2fs_link(ext2_filsys fs, ext2_ino_t dir, const char *name,
1624 		      ext2_ino_t ino, int flags);
1625 errcode_t ext2fs_unlink(ext2_filsys fs, ext2_ino_t dir, const char *name,
1626 			ext2_ino_t ino, int flags);
1627 
1628 /* symlink.c */
1629 errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
1630 			 const char *name, const char *target);
1631 int ext2fs_is_fast_symlink(struct ext2_inode *inode);
1632 
1633 /* mmp.c */
1634 errcode_t ext2fs_mmp_read(ext2_filsys fs, blk64_t mmp_blk, void *buf);
1635 errcode_t ext2fs_mmp_write(ext2_filsys fs, blk64_t mmp_blk, void *buf);
1636 errcode_t ext2fs_mmp_clear(ext2_filsys fs);
1637 errcode_t ext2fs_mmp_init(ext2_filsys fs);
1638 errcode_t ext2fs_mmp_start(ext2_filsys fs);
1639 errcode_t ext2fs_mmp_update(ext2_filsys fs);
1640 errcode_t ext2fs_mmp_update2(ext2_filsys fs, int immediately);
1641 errcode_t ext2fs_mmp_stop(ext2_filsys fs);
1642 unsigned ext2fs_mmp_new_seq(void);
1643 
1644 /* read_bb.c */
1645 extern errcode_t ext2fs_read_bb_inode(ext2_filsys fs,
1646 				      ext2_badblocks_list *bb_list);
1647 
1648 /* read_bb_file.c */
1649 extern errcode_t ext2fs_read_bb_FILE2(ext2_filsys fs, FILE *f,
1650 				      ext2_badblocks_list *bb_list,
1651 				      void *priv_data,
1652 				      void (*invalid)(ext2_filsys fs,
1653 						      blk_t blk,
1654 						      char *badstr,
1655 						      void *priv_data));
1656 extern errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f,
1657 				     ext2_badblocks_list *bb_list,
1658 				     void (*invalid)(ext2_filsys fs,
1659 						     blk_t blk));
1660 
1661 /* res_gdt.c */
1662 extern errcode_t ext2fs_create_resize_inode(ext2_filsys fs);
1663 
1664 /*sha256.c */
1665 #define EXT2FS_SHA256_LENGTH 32
1666 #if 0
1667 extern void ext2fs_sha256(const unsigned char *in, unsigned long in_size,
1668 		   unsigned char out[EXT2FS_SHA256_LENGTH]);
1669 #endif
1670 
1671 /* sha512.c */
1672 #define EXT2FS_SHA512_LENGTH 64
1673 extern void ext2fs_sha512(const unsigned char *in, unsigned long in_size,
1674 			  unsigned char out[EXT2FS_SHA512_LENGTH]);
1675 
1676 /* swapfs.c */
1677 extern errcode_t ext2fs_dirent_swab_in2(ext2_filsys fs, char *buf, size_t size,
1678 					int flags);
1679 extern errcode_t ext2fs_dirent_swab_in(ext2_filsys fs, char *buf, int flags);
1680 extern errcode_t ext2fs_dirent_swab_out2(ext2_filsys fs, char *buf, size_t size,
1681 					 int flags);
1682 extern errcode_t ext2fs_dirent_swab_out(ext2_filsys fs, char *buf, int flags);
1683 extern void ext2fs_swap_ext_attr(char *to, char *from, int bufsize,
1684 				 int has_header);
1685 extern void ext2fs_swap_ext_attr_header(struct ext2_ext_attr_header *to_header,
1686 					struct ext2_ext_attr_header *from_hdr);
1687 extern void ext2fs_swap_ext_attr_entry(struct ext2_ext_attr_entry *to_entry,
1688 				       struct ext2_ext_attr_entry *from_entry);
1689 extern void ext2fs_swap_super(struct ext2_super_block * super);
1690 extern void ext2fs_swap_group_desc(struct ext2_group_desc *gdp);
1691 extern void ext2fs_swap_group_desc2(ext2_filsys, struct ext2_group_desc *gdp);
1692 extern void ext2fs_swap_inode_full(ext2_filsys fs, struct ext2_inode_large *t,
1693 				   struct ext2_inode_large *f, int hostorder,
1694 				   int bufsize);
1695 extern void ext2fs_swap_inode(ext2_filsys fs,struct ext2_inode *t,
1696 			      struct ext2_inode *f, int hostorder);
1697 extern void ext2fs_swap_mmp(struct mmp_struct *mmp);
1698 
1699 /* unix_io.c */
1700 extern int ext2fs_open_file(const char *pathname, int flags, mode_t mode);
1701 extern int ext2fs_stat(const char *path, ext2fs_struct_stat *buf);
1702 extern int ext2fs_fstat(int fd, ext2fs_struct_stat *buf);
1703 
1704 /* valid_blk.c */
1705 extern int ext2fs_inode_has_valid_blocks(struct ext2_inode *inode);
1706 extern int ext2fs_inode_has_valid_blocks2(ext2_filsys fs,
1707 					  struct ext2_inode *inode);
1708 
1709 /* version.c */
1710 extern int ext2fs_parse_version_string(const char *ver_string);
1711 extern int ext2fs_get_library_version(const char **ver_string,
1712 				      const char **date_string);
1713 
1714 /* write_bb_file.c */
1715 extern errcode_t ext2fs_write_bb_FILE(ext2_badblocks_list bb_list,
1716 				      unsigned int flags,
1717 				      FILE *f);
1718 
1719 
1720 /* inline functions */
1721 #ifdef NO_INLINE_FUNCS
1722 extern errcode_t ext2fs_get_mem(unsigned long size, void *ptr);
1723 extern errcode_t ext2fs_get_memzero(unsigned long size, void *ptr);
1724 extern errcode_t ext2fs_get_array(unsigned long count,
1725 				  unsigned long size, void *ptr);
1726 extern errcode_t ext2fs_get_arrayzero(unsigned long count,
1727 				      unsigned long size, void *ptr);
1728 extern errcode_t ext2fs_free_mem(void *ptr);
1729 extern errcode_t ext2fs_resize_mem(unsigned long old_size,
1730 				   unsigned long size, void *ptr);
1731 extern void ext2fs_mark_super_dirty(ext2_filsys fs);
1732 extern void ext2fs_mark_changed(ext2_filsys fs);
1733 extern int ext2fs_test_changed(ext2_filsys fs);
1734 extern void ext2fs_mark_valid(ext2_filsys fs);
1735 extern void ext2fs_unmark_valid(ext2_filsys fs);
1736 extern int ext2fs_test_valid(ext2_filsys fs);
1737 extern void ext2fs_mark_ib_dirty(ext2_filsys fs);
1738 extern void ext2fs_mark_bb_dirty(ext2_filsys fs);
1739 extern int ext2fs_test_ib_dirty(ext2_filsys fs);
1740 extern int ext2fs_test_bb_dirty(ext2_filsys fs);
1741 extern dgrp_t ext2fs_group_of_blk(ext2_filsys fs, blk_t blk);
1742 extern dgrp_t ext2fs_group_of_ino(ext2_filsys fs, ext2_ino_t ino);
1743 extern blk_t ext2fs_group_first_block(ext2_filsys fs, dgrp_t group);
1744 extern blk_t ext2fs_group_last_block(ext2_filsys fs, dgrp_t group);
1745 extern blk_t ext2fs_inode_data_blocks(ext2_filsys fs,
1746 				      struct ext2_inode *inode);
1747 extern int ext2fs_htree_intnode_maxrecs(ext2_filsys fs, int blocks);
1748 extern unsigned int ext2fs_div_ceil(unsigned int a, unsigned int b);
1749 extern __u64 ext2fs_div64_ceil(__u64 a, __u64 b);
1750 extern int ext2fs_dirent_name_len(const struct ext2_dir_entry *entry);
1751 extern void ext2fs_dirent_set_name_len(struct ext2_dir_entry *entry, int len);
1752 extern int ext2fs_dirent_file_type(const struct ext2_dir_entry *entry);
1753 extern void ext2fs_dirent_set_file_type(struct ext2_dir_entry *entry, int type);
1754 extern struct ext2_inode *ext2fs_inode(struct ext2_inode_large * large_inode);
1755 extern const struct ext2_inode *ext2fs_const_inode(const struct ext2_inode_large * large_inode);
1756 
1757 #endif
1758 
1759 /*
1760  * The actual inlined functions definitions themselves...
1761  *
1762  * If NO_INLINE_FUNCS is defined, then we won't try to do inline
1763  * functions at all!
1764  */
1765 #if (defined(INCLUDE_INLINE_FUNCS) || !defined(NO_INLINE_FUNCS))
1766 #ifdef INCLUDE_INLINE_FUNCS
1767 #define _INLINE_ extern
1768 #else
1769 #if (__STDC_VERSION__ >= 199901L)
1770 #define _INLINE_ inline
1771 #else
1772 #ifdef __GNUC__
1773 #define _INLINE_ extern __inline__
1774 #else				/* For Watcom C */
1775 #define _INLINE_ extern inline
1776 #endif /* __GNUC__ */
1777 #endif /* __STDC_VERSION__ >= 199901L */
1778 #endif
1779 
1780 #ifndef EXT2_CUSTOM_MEMORY_ROUTINES
1781 #include <string.h>
1782 /*
1783  *  Allocate memory.  The 'ptr' arg must point to a pointer.
1784  */
ext2fs_get_mem(unsigned long size,void * ptr)1785 _INLINE_ errcode_t ext2fs_get_mem(unsigned long size, void *ptr)
1786 {
1787 	void *pp;
1788 
1789 	pp = malloc(size);
1790 	if (!pp)
1791 		return EXT2_ET_NO_MEMORY;
1792 	memcpy(ptr, &pp, sizeof (pp));
1793 	return 0;
1794 }
1795 
ext2fs_get_memzero(unsigned long size,void * ptr)1796 _INLINE_ errcode_t ext2fs_get_memzero(unsigned long size, void *ptr)
1797 {
1798 	void *pp;
1799 
1800 	pp = malloc(size);
1801 	if (!pp)
1802 		return EXT2_ET_NO_MEMORY;
1803 	memset(pp, 0, size);
1804 	memcpy(ptr, &pp, sizeof(pp));
1805 	return 0;
1806 }
1807 
ext2fs_get_array(unsigned long count,unsigned long size,void * ptr)1808 _INLINE_ errcode_t ext2fs_get_array(unsigned long count, unsigned long size, void *ptr)
1809 {
1810 	if (count && (~0UL)/count < size)
1811 		return EXT2_ET_NO_MEMORY;
1812 	return ext2fs_get_mem(count*size, ptr);
1813 }
1814 
ext2fs_get_arrayzero(unsigned long count,unsigned long size,void * ptr)1815 _INLINE_ errcode_t ext2fs_get_arrayzero(unsigned long count,
1816 					unsigned long size, void *ptr)
1817 {
1818 	void *pp;
1819 
1820 	if (count && (~0UL)/count < size)
1821 		return EXT2_ET_NO_MEMORY;
1822 	pp = calloc(count, size);
1823 	if (!pp)
1824 		return EXT2_ET_NO_MEMORY;
1825 	memcpy(ptr, &pp, sizeof(pp));
1826 	return 0;
1827 }
1828 
1829 /*
1830  * Free memory.  The 'ptr' arg must point to a pointer.
1831  */
ext2fs_free_mem(void * ptr)1832 _INLINE_ errcode_t ext2fs_free_mem(void *ptr)
1833 {
1834 	void *p;
1835 
1836 	memcpy(&p, ptr, sizeof(p));
1837 	free(p);
1838 	p = 0;
1839 	memcpy(ptr, &p, sizeof(p));
1840 	return 0;
1841 }
1842 
1843 /*
1844  *  Resize memory.  The 'ptr' arg must point to a pointer.
1845  */
ext2fs_resize_mem(unsigned long EXT2FS_ATTR ((unused))old_size,unsigned long size,void * ptr)1846 _INLINE_ errcode_t ext2fs_resize_mem(unsigned long EXT2FS_ATTR((unused)) old_size,
1847 				     unsigned long size, void *ptr)
1848 {
1849 	void *p;
1850 
1851 	/* Use "memcpy" for pointer assignments here to avoid problems
1852 	 * with C99 strict type aliasing rules. */
1853 	memcpy(&p, ptr, sizeof(p));
1854 	p = realloc(p, size);
1855 	if (!p)
1856 		return EXT2_ET_NO_MEMORY;
1857 	memcpy(ptr, &p, sizeof(p));
1858 	return 0;
1859 }
1860 #endif	/* Custom memory routines */
1861 
1862 /*
1863  * Mark a filesystem superblock as dirty
1864  */
ext2fs_mark_super_dirty(ext2_filsys fs)1865 _INLINE_ void ext2fs_mark_super_dirty(ext2_filsys fs)
1866 {
1867 	fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_CHANGED;
1868 }
1869 
1870 /*
1871  * Mark a filesystem as changed
1872  */
ext2fs_mark_changed(ext2_filsys fs)1873 _INLINE_ void ext2fs_mark_changed(ext2_filsys fs)
1874 {
1875 	fs->flags |= EXT2_FLAG_CHANGED;
1876 }
1877 
1878 /*
1879  * Check to see if a filesystem has changed
1880  */
ext2fs_test_changed(ext2_filsys fs)1881 _INLINE_ int ext2fs_test_changed(ext2_filsys fs)
1882 {
1883 	return (fs->flags & EXT2_FLAG_CHANGED);
1884 }
1885 
1886 /*
1887  * Mark a filesystem as valid
1888  */
ext2fs_mark_valid(ext2_filsys fs)1889 _INLINE_ void ext2fs_mark_valid(ext2_filsys fs)
1890 {
1891 	fs->flags |= EXT2_FLAG_VALID;
1892 }
1893 
1894 /*
1895  * Mark a filesystem as NOT valid
1896  */
ext2fs_unmark_valid(ext2_filsys fs)1897 _INLINE_ void ext2fs_unmark_valid(ext2_filsys fs)
1898 {
1899 	fs->flags &= ~EXT2_FLAG_VALID;
1900 }
1901 
1902 /*
1903  * Check to see if a filesystem is valid
1904  */
ext2fs_test_valid(ext2_filsys fs)1905 _INLINE_ int ext2fs_test_valid(ext2_filsys fs)
1906 {
1907 	return (fs->flags & EXT2_FLAG_VALID);
1908 }
1909 
1910 /*
1911  * Mark the inode bitmap as dirty
1912  */
ext2fs_mark_ib_dirty(ext2_filsys fs)1913 _INLINE_ void ext2fs_mark_ib_dirty(ext2_filsys fs)
1914 {
1915 	fs->flags |= EXT2_FLAG_IB_DIRTY | EXT2_FLAG_CHANGED;
1916 }
1917 
1918 /*
1919  * Mark the block bitmap as dirty
1920  */
ext2fs_mark_bb_dirty(ext2_filsys fs)1921 _INLINE_ void ext2fs_mark_bb_dirty(ext2_filsys fs)
1922 {
1923 	fs->flags |= EXT2_FLAG_BB_DIRTY | EXT2_FLAG_CHANGED;
1924 }
1925 
1926 /*
1927  * Check to see if a filesystem's inode bitmap is dirty
1928  */
ext2fs_test_ib_dirty(ext2_filsys fs)1929 _INLINE_ int ext2fs_test_ib_dirty(ext2_filsys fs)
1930 {
1931 	return (fs->flags & EXT2_FLAG_IB_DIRTY);
1932 }
1933 
1934 /*
1935  * Check to see if a filesystem's block bitmap is dirty
1936  */
ext2fs_test_bb_dirty(ext2_filsys fs)1937 _INLINE_ int ext2fs_test_bb_dirty(ext2_filsys fs)
1938 {
1939 	return (fs->flags & EXT2_FLAG_BB_DIRTY);
1940 }
1941 
1942 /*
1943  * Return the group # of a block
1944  */
ext2fs_group_of_blk(ext2_filsys fs,blk_t blk)1945 _INLINE_ dgrp_t ext2fs_group_of_blk(ext2_filsys fs, blk_t blk)
1946 {
1947 	return ext2fs_group_of_blk2(fs, blk);
1948 }
1949 /*
1950  * Return the group # of an inode number
1951  */
ext2fs_group_of_ino(ext2_filsys fs,ext2_ino_t ino)1952 _INLINE_ dgrp_t ext2fs_group_of_ino(ext2_filsys fs, ext2_ino_t ino)
1953 {
1954 	return (ino - 1) / fs->super->s_inodes_per_group;
1955 }
1956 
1957 /*
1958  * Return the first block (inclusive) in a group
1959  */
ext2fs_group_first_block(ext2_filsys fs,dgrp_t group)1960 _INLINE_ blk_t ext2fs_group_first_block(ext2_filsys fs, dgrp_t group)
1961 {
1962 	return (blk_t) ext2fs_group_first_block2(fs, group);
1963 }
1964 
1965 /*
1966  * Return the last block (inclusive) in a group
1967  */
ext2fs_group_last_block(ext2_filsys fs,dgrp_t group)1968 _INLINE_ blk_t ext2fs_group_last_block(ext2_filsys fs, dgrp_t group)
1969 {
1970 	return (blk_t) ext2fs_group_last_block2(fs, group);
1971 }
1972 
ext2fs_inode_data_blocks(ext2_filsys fs,struct ext2_inode * inode)1973 _INLINE_ blk_t ext2fs_inode_data_blocks(ext2_filsys fs,
1974 					struct ext2_inode *inode)
1975 {
1976 	return (blk_t) ext2fs_inode_data_blocks2(fs, inode);
1977 }
1978 
ext2fs_htree_intnode_maxrecs(ext2_filsys fs,int blocks)1979 _INLINE_ int ext2fs_htree_intnode_maxrecs(ext2_filsys fs, int blocks)
1980 {
1981 	return blocks * ((fs->blocksize - 8) / sizeof(struct ext2_dx_entry));
1982 }
1983 
1984 /*
1985  * This is an efficient, overflow safe way of calculating ceil((1.0 * a) / b)
1986  */
ext2fs_div_ceil(unsigned int a,unsigned int b)1987 _INLINE_ unsigned int ext2fs_div_ceil(unsigned int a, unsigned int b)
1988 {
1989 	if (!a)
1990 		return 0;
1991 	return ((a - 1) / b) + 1;
1992 }
1993 
ext2fs_div64_ceil(__u64 a,__u64 b)1994 _INLINE_ __u64 ext2fs_div64_ceil(__u64 a, __u64 b)
1995 {
1996 	if (!a)
1997 		return 0;
1998 	return ((a - 1) / b) + 1;
1999 }
2000 
ext2fs_dirent_name_len(const struct ext2_dir_entry * entry)2001 _INLINE_ int ext2fs_dirent_name_len(const struct ext2_dir_entry *entry)
2002 {
2003 	return entry->name_len & 0xff;
2004 }
2005 
ext2fs_dirent_set_name_len(struct ext2_dir_entry * entry,int len)2006 _INLINE_ void ext2fs_dirent_set_name_len(struct ext2_dir_entry *entry, int len)
2007 {
2008 	entry->name_len = (entry->name_len & 0xff00) | (len & 0xff);
2009 }
2010 
ext2fs_dirent_file_type(const struct ext2_dir_entry * entry)2011 _INLINE_ int ext2fs_dirent_file_type(const struct ext2_dir_entry *entry)
2012 {
2013 	return entry->name_len >> 8;
2014 }
2015 
ext2fs_dirent_set_file_type(struct ext2_dir_entry * entry,int type)2016 _INLINE_ void ext2fs_dirent_set_file_type(struct ext2_dir_entry *entry, int type)
2017 {
2018 	entry->name_len = (entry->name_len & 0xff) | (type << 8);
2019 }
2020 
ext2fs_inode(struct ext2_inode_large * large_inode)2021 _INLINE_ struct ext2_inode *ext2fs_inode(struct ext2_inode_large * large_inode)
2022 {
2023 	/* It is always safe to convert large inode to a small inode */
2024 	return (struct ext2_inode *) large_inode;
2025 }
2026 
2027 _INLINE_ const struct ext2_inode *
ext2fs_const_inode(const struct ext2_inode_large * large_inode)2028 ext2fs_const_inode(const struct ext2_inode_large * large_inode)
2029 {
2030 	/* It is always safe to convert large inode to a small inode */
2031 	return (const struct ext2_inode *) large_inode;
2032 }
2033 
2034 #undef _INLINE_
2035 #endif
2036 
2037 /* htree levels for ext4 */
2038 #define EXT4_HTREE_LEVEL_COMPAT 2
2039 #define EXT4_HTREE_LEVEL	3
2040 
ext2_dir_htree_level(ext2_filsys fs)2041 static inline unsigned int ext2_dir_htree_level(ext2_filsys fs)
2042 {
2043 	if (ext2fs_has_feature_largedir(fs->super))
2044 		return EXT4_HTREE_LEVEL;
2045 
2046 	return EXT4_HTREE_LEVEL_COMPAT;
2047 }
2048 
2049 #ifdef __cplusplus
2050 }
2051 #endif
2052 
2053 #endif /* _EXT2FS_EXT2FS_H */
2054