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