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