• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * ext2fs.h --- ext2fs
3  *
4  * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11 
12 #ifndef _EXT2FS_EXT2FS_H
13 #define _EXT2FS_EXT2FS_H
14 
15 #ifdef __GNUC__
16 #define EXT2FS_ATTR(x) __attribute__(x)
17 #else
18 #define EXT2FS_ATTR(x)
19 #endif
20 
21 #ifdef CONFIG_TDB
22 #define EXT2FS_NO_TDB_UNUSED
23 #else
24 #define EXT2FS_NO_TDB_UNUSED	EXT2FS_ATTR((unused))
25 #endif
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /*
32  * Non-GNU C compilers won't necessarily understand inline
33  */
34 #if (!defined(__GNUC__) && !defined(__WATCOMC__))
35 #define NO_INLINE_FUNCS
36 #endif
37 
38 /*
39  * Where the master copy of the superblock is located, and how big
40  * superblocks are supposed to be.  We define SUPERBLOCK_SIZE because
41  * the size of the superblock structure is not necessarily trustworthy
42  * (some versions have the padding set up so that the superblock is
43  * 1032 bytes long).
44  */
45 #define SUPERBLOCK_OFFSET	1024
46 #define SUPERBLOCK_SIZE		1024
47 
48 #define UUID_STR_SIZE 37
49 
50 /*
51  * The last ext2fs revision level that this version of the library is
52  * able to support.
53  */
54 #define EXT2_LIB_CURRENT_REV	EXT2_DYNAMIC_REV
55 
56 #ifdef HAVE_SYS_TYPES_H
57 #include <sys/types.h>
58 #endif
59 
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <time.h>
64 #include <errno.h>
65 
66 #if EXT2_FLAT_INCLUDES
67 #include "e2_types.h"
68 #include "ext2_fs.h"
69 #include "ext3_extents.h"
70 #else
71 #include <ext2fs/ext2_types.h>
72 #include <ext2fs/ext2_fs.h>
73 #include <ext2fs/ext3_extents.h>
74 #endif /* EXT2_FLAT_INCLUDES */
75 
76 typedef __u32 __bitwise		ext2_ino_t;
77 typedef __u32 __bitwise		blk_t;
78 typedef __u64 __bitwise		blk64_t;
79 typedef __u32 __bitwise		dgrp_t;
80 typedef __u32 __bitwise		ext2_off_t;
81 typedef __u64 __bitwise		ext2_off64_t;
82 typedef __s64 __bitwise		e2_blkcnt_t;
83 typedef __u32 __bitwise		ext2_dirhash_t;
84 
85 #if EXT2_FLAT_INCLUDES
86 #include "com_err.h"
87 #include "ext2_io.h"
88 #include "ext2_err.h"
89 #include "ext2_ext_attr.h"
90 #else
91 #include <et/com_err.h>
92 #include <ext2fs/ext2_io.h>
93 #include <ext2fs/ext2_err.h>
94 #include <ext2fs/ext2_ext_attr.h>
95 #endif
96 
97 #include "hashmap.h"
98 
99 /*
100  * Portability help for Microsoft Visual C++
101  */
102 #ifdef _MSC_VER
103 #define EXT2_QSORT_TYPE int __cdecl
104 #else
105 #define EXT2_QSORT_TYPE int
106 #endif
107 
108 typedef struct struct_ext2_filsys *ext2_filsys;
109 
110 #define EXT2FS_MARK_ERROR 	0
111 #define EXT2FS_UNMARK_ERROR 	1
112 #define EXT2FS_TEST_ERROR	2
113 
114 struct ext2fs_struct_generic_bitmap_base {
115 	errcode_t		magic;
116 	ext2_filsys 		fs;
117 };
118 
119 typedef struct ext2fs_struct_generic_bitmap_base *ext2fs_generic_bitmap;
120 typedef struct ext2fs_struct_generic_bitmap_base *ext2fs_inode_bitmap;
121 typedef struct ext2fs_struct_generic_bitmap_base *ext2fs_block_bitmap;
122 
123 #define EXT2_FIRST_INODE(s)	EXT2_FIRST_INO(s)
124 
125 
126 /*
127  * Badblocks list definitions
128  */
129 
130 typedef struct ext2_struct_u32_list *ext2_badblocks_list;
131 typedef struct ext2_struct_u32_iterate *ext2_badblocks_iterate;
132 
133 typedef struct ext2_struct_u32_list *ext2_u32_list;
134 typedef struct ext2_struct_u32_iterate *ext2_u32_iterate;
135 
136 /* old */
137 typedef struct ext2_struct_u32_list *badblocks_list;
138 typedef struct ext2_struct_u32_iterate *badblocks_iterate;
139 
140 #define BADBLOCKS_FLAG_DIRTY	1
141 
142 /*
143  * ext2_dblist structure and abstractions (see dblist.c)
144  */
145 struct ext2_db_entry2 {
146 	ext2_ino_t	ino;
147 	blk64_t	blk;
148 	e2_blkcnt_t	blockcnt;
149 };
150 
151 /* Ye Olde 32-bit version */
152 struct ext2_db_entry {
153 	ext2_ino_t	ino;
154 	blk_t	blk;
155 	int	blockcnt;
156 };
157 
158 typedef struct ext2_struct_dblist *ext2_dblist;
159 
160 #define DBLIST_ABORT	1
161 
162 /*
163  * ext2_fileio definitions
164  */
165 
166 #define EXT2_FILE_WRITE		0x0001
167 #define EXT2_FILE_CREATE	0x0002
168 
169 #define EXT2_FILE_MASK		0x00FF
170 
171 #define EXT2_FILE_BUF_DIRTY	0x4000
172 #define EXT2_FILE_BUF_VALID	0x2000
173 
174 typedef struct ext2_file *ext2_file_t;
175 
176 #define EXT2_SEEK_SET	0
177 #define EXT2_SEEK_CUR	1
178 #define EXT2_SEEK_END	2
179 
180 /*
181  * Flags for the ext2_filsys structure and for ext2fs_open()
182  */
183 #define EXT2_FLAG_RW			0x01
184 #define EXT2_FLAG_CHANGED		0x02
185 #define EXT2_FLAG_DIRTY			0x04
186 #define EXT2_FLAG_VALID			0x08
187 #define EXT2_FLAG_IB_DIRTY		0x10
188 #define EXT2_FLAG_BB_DIRTY		0x20
189 #define EXT2_FLAG_SWAP_BYTES		0x40
190 #define EXT2_FLAG_SWAP_BYTES_READ	0x80
191 #define EXT2_FLAG_SWAP_BYTES_WRITE	0x100
192 #define EXT2_FLAG_MASTER_SB_ONLY	0x200
193 #define EXT2_FLAG_FORCE			0x400
194 #define EXT2_FLAG_SUPER_ONLY		0x800
195 #define EXT2_FLAG_JOURNAL_DEV_OK	0x1000
196 #define EXT2_FLAG_IMAGE_FILE		0x2000
197 #define EXT2_FLAG_EXCLUSIVE		0x4000
198 #define EXT2_FLAG_SOFTSUPP_FEATURES	0x8000
199 #define EXT2_FLAG_NOFREE_ON_ERROR	0x10000
200 #define EXT2_FLAG_64BITS		0x20000
201 #define EXT2_FLAG_PRINT_PROGRESS	0x40000
202 #define EXT2_FLAG_DIRECT_IO		0x80000
203 #define EXT2_FLAG_SKIP_MMP		0x100000
204 #define EXT2_FLAG_IGNORE_CSUM_ERRORS	0x200000
205 #define EXT2_FLAG_SHARE_DUP		0x400000
206 #define EXT2_FLAG_IGNORE_SB_ERRORS	0x800000
207 #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 					 EXT4_FEATURE_COMPAT_STABLE_INODES)
616 
617 #ifdef CONFIG_MMP
618 #define EXT4_LIB_INCOMPAT_MMP		EXT4_FEATURE_INCOMPAT_MMP
619 #else
620 #define EXT4_LIB_INCOMPAT_MMP		(0)
621 #endif
622 
623 #define EXT2_LIB_FEATURE_INCOMPAT_SUPP	(EXT2_FEATURE_INCOMPAT_FILETYPE|\
624 					 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|\
625 					 EXT2_FEATURE_INCOMPAT_META_BG|\
626 					 EXT3_FEATURE_INCOMPAT_RECOVER|\
627 					 EXT3_FEATURE_INCOMPAT_EXTENTS|\
628 					 EXT4_FEATURE_INCOMPAT_FLEX_BG|\
629 					 EXT4_FEATURE_INCOMPAT_EA_INODE|\
630 					 EXT4_LIB_INCOMPAT_MMP|\
631 					 EXT4_FEATURE_INCOMPAT_64BIT|\
632 					 EXT4_FEATURE_INCOMPAT_INLINE_DATA|\
633 					 EXT4_FEATURE_INCOMPAT_ENCRYPT|\
634 					 EXT4_FEATURE_INCOMPAT_CASEFOLD|\
635 					 EXT4_FEATURE_INCOMPAT_CSUM_SEED|\
636 					 EXT4_FEATURE_INCOMPAT_LARGEDIR)
637 
638 #define EXT2_LIB_FEATURE_RO_COMPAT_SUPP	(EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|\
639 					 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|\
640 					 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|\
641 					 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|\
642 					 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|\
643 					 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|\
644 					 EXT4_FEATURE_RO_COMPAT_BIGALLOC|\
645 					 EXT4_FEATURE_RO_COMPAT_QUOTA|\
646 					 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM|\
647 					 EXT4_FEATURE_RO_COMPAT_READONLY |\
648 					 EXT4_FEATURE_RO_COMPAT_PROJECT |\
649 					 EXT4_FEATURE_RO_COMPAT_SHARED_BLOCKS |\
650 					 EXT4_FEATURE_RO_COMPAT_VERITY)
651 
652 /*
653  * These features are only allowed if EXT2_FLAG_SOFTSUPP_FEATURES is passed
654  * to ext2fs_openfs()
655  */
656 #define EXT2_LIB_SOFTSUPP_INCOMPAT	(0)
657 #define EXT2_LIB_SOFTSUPP_RO_COMPAT	(EXT4_FEATURE_RO_COMPAT_REPLICA)
658 
659 
660 /* Translate a block number to a cluster number */
661 #define EXT2FS_CLUSTER_RATIO(fs)	(1 << (fs)->cluster_ratio_bits)
662 #define EXT2FS_CLUSTER_MASK(fs)		(EXT2FS_CLUSTER_RATIO(fs) - 1)
663 #define EXT2FS_B2C(fs, blk)		((blk) >> (fs)->cluster_ratio_bits)
664 /* Translate a cluster number to a block number */
665 #define EXT2FS_C2B(fs, cluster)		((cluster) << (fs)->cluster_ratio_bits)
666 /* Translate # of blks to # of clusters */
667 #define EXT2FS_NUM_B2C(fs, blks)	(((blks) + EXT2FS_CLUSTER_MASK(fs)) >> \
668 					 (fs)->cluster_ratio_bits)
669 
670 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
671 typedef struct stat64 ext2fs_struct_stat;
672 #else
673 typedef struct stat ext2fs_struct_stat;
674 #endif
675 
676 /*
677  * For ext2fs_close2() and ext2fs_flush2(), this flag allows you to
678  * avoid the fsync call.
679  */
680 #define EXT2_FLAG_FLUSH_NO_SYNC          1
681 
682 /*
683  * Modify and iterate extended attributes
684  */
685 struct ext2_xattr_handle;
686 #define XATTR_ABORT	1
687 #define XATTR_CHANGED	2
688 
689 /*
690  * function prototypes
691  */
ext2fs_has_group_desc_csum(ext2_filsys fs)692 static inline int ext2fs_has_group_desc_csum(ext2_filsys fs)
693 {
694 	return ext2fs_has_feature_metadata_csum(fs->super) ||
695 	       ext2fs_has_feature_gdt_csum(fs->super);
696 }
697 
698 /* 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)699 static inline int ext2fs_needs_large_file_feature(unsigned long long file_size)
700 {
701 	return file_size >= 0x80000000ULL;
702 }
703 
704 /* alloc.c */
705 extern void ext2fs_clear_block_uninit(ext2_filsys fs, dgrp_t group);
706 extern errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir, int mode,
707 				  ext2fs_inode_bitmap map, ext2_ino_t *ret);
708 extern errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
709 				  ext2fs_block_bitmap map, blk_t *ret);
710 extern errcode_t ext2fs_new_block2(ext2_filsys fs, blk64_t goal,
711 				   ext2fs_block_bitmap map, blk64_t *ret);
712 extern errcode_t ext2fs_new_block3(ext2_filsys fs, blk64_t goal,
713 				   ext2fs_block_bitmap map, blk64_t *ret,
714 				   struct blk_alloc_ctx *ctx);
715 extern errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start,
716 					blk_t finish, int num,
717 					ext2fs_block_bitmap map,
718 					blk_t *ret);
719 extern errcode_t ext2fs_get_free_blocks2(ext2_filsys fs, blk64_t start,
720 					 blk64_t finish, int num,
721 					 ext2fs_block_bitmap map,
722 					 blk64_t *ret);
723 extern errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
724 				    char *block_buf, blk_t *ret);
725 extern errcode_t ext2fs_alloc_block2(ext2_filsys fs, blk64_t goal,
726 				     char *block_buf, blk64_t *ret);
727 extern errcode_t ext2fs_alloc_block3(ext2_filsys fs, blk64_t goal,
728 				     char *block_buf, blk64_t *ret,
729 				     struct blk_alloc_ctx *ctx);
730 
731 extern void ext2fs_set_alloc_block_callback(ext2_filsys fs,
732 					    errcode_t (*func)(ext2_filsys fs,
733 							      blk64_t goal,
734 							      blk64_t *ret),
735 					    errcode_t (**old)(ext2_filsys fs,
736 							      blk64_t goal,
737 							      blk64_t *ret));
738 blk64_t ext2fs_find_inode_goal(ext2_filsys fs, ext2_ino_t ino,
739 			       struct ext2_inode *inode, blk64_t lblk);
740 extern void ext2fs_set_new_range_callback(ext2_filsys fs,
741 	errcode_t (*func)(ext2_filsys fs, int flags, blk64_t goal,
742 			       blk64_t len, blk64_t *pblk, blk64_t *plen),
743 	errcode_t (**old)(ext2_filsys fs, int flags, blk64_t goal,
744 			       blk64_t len, blk64_t *pblk, blk64_t *plen));
745 extern void ext2fs_set_block_alloc_stats_range_callback(ext2_filsys fs,
746 	void (*func)(ext2_filsys fs, blk64_t blk,
747 				    blk_t num, int inuse),
748 	void (**old)(ext2_filsys fs, blk64_t blk,
749 				    blk_t num, int inuse));
750 #define EXT2_NEWRANGE_FIXED_GOAL	(0x1)
751 #define EXT2_NEWRANGE_MIN_LENGTH	(0x2)
752 #define EXT2_NEWRANGE_ALL_FLAGS		(0x3)
753 errcode_t ext2fs_new_range(ext2_filsys fs, int flags, blk64_t goal,
754 			   blk64_t len, ext2fs_block_bitmap map, blk64_t *pblk,
755 			   blk64_t *plen);
756 #define EXT2_ALLOCRANGE_FIXED_GOAL	(0x1)
757 #define EXT2_ALLOCRANGE_ZERO_BLOCKS	(0x2)
758 #define EXT2_ALLOCRANGE_ALL_FLAGS	(0x3)
759 errcode_t ext2fs_alloc_range(ext2_filsys fs, int flags, blk64_t goal,
760 			     blk_t len, blk64_t *ret);
761 
762 /* alloc_sb.c */
763 extern int ext2fs_reserve_super_and_bgd(ext2_filsys fs,
764 					dgrp_t group,
765 					ext2fs_block_bitmap bmap);
766 extern void ext2fs_set_block_alloc_stats_callback(ext2_filsys fs,
767 						  void (*func)(ext2_filsys fs,
768 							       blk64_t blk,
769 							       int inuse),
770 						  void (**old)(ext2_filsys fs,
771 							       blk64_t blk,
772 							       int inuse));
773 
774 /* alloc_stats.c */
775 void ext2fs_inode_alloc_stats(ext2_filsys fs, ext2_ino_t ino, int inuse);
776 void ext2fs_inode_alloc_stats2(ext2_filsys fs, ext2_ino_t ino,
777 			       int inuse, int isdir);
778 void ext2fs_block_alloc_stats(ext2_filsys fs, blk_t blk, int inuse);
779 void ext2fs_block_alloc_stats2(ext2_filsys fs, blk64_t blk, int inuse);
780 void ext2fs_block_alloc_stats_range(ext2_filsys fs, blk64_t blk,
781 				    blk_t num, int inuse);
782 
783 /* alloc_tables.c */
784 extern errcode_t ext2fs_allocate_tables(ext2_filsys fs);
785 extern errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
786 					     ext2fs_block_bitmap bmap);
787 
788 /* badblocks.c */
789 extern errcode_t ext2fs_u32_list_create(ext2_u32_list *ret, int size);
790 extern errcode_t ext2fs_u32_list_add(ext2_u32_list bb, __u32 blk);
791 extern int ext2fs_u32_list_find(ext2_u32_list bb, __u32 blk);
792 extern int ext2fs_u32_list_test(ext2_u32_list bb, blk_t blk);
793 extern errcode_t ext2fs_u32_list_iterate_begin(ext2_u32_list bb,
794 					       ext2_u32_iterate *ret);
795 extern int ext2fs_u32_list_iterate(ext2_u32_iterate iter, blk_t *blk);
796 extern void ext2fs_u32_list_iterate_end(ext2_u32_iterate iter);
797 extern errcode_t ext2fs_u32_copy(ext2_u32_list src, ext2_u32_list *dest);
798 extern int ext2fs_u32_list_equal(ext2_u32_list bb1, ext2_u32_list bb2);
799 
800 extern errcode_t ext2fs_badblocks_list_create(ext2_badblocks_list *ret,
801 					    int size);
802 extern errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb,
803 					   blk_t blk);
804 extern int ext2fs_badblocks_list_test(ext2_badblocks_list bb,
805 				    blk_t blk);
806 extern int ext2fs_u32_list_del(ext2_u32_list bb, __u32 blk);
807 extern void ext2fs_badblocks_list_del(ext2_u32_list bb, __u32 blk);
808 extern errcode_t
809 	ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb,
810 					    ext2_badblocks_iterate *ret);
811 extern int ext2fs_badblocks_list_iterate(ext2_badblocks_iterate iter,
812 					 blk_t *blk);
813 extern void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter);
814 extern errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src,
815 				       ext2_badblocks_list *dest);
816 extern int ext2fs_badblocks_equal(ext2_badblocks_list bb1,
817 				  ext2_badblocks_list bb2);
818 extern int ext2fs_u32_list_count(ext2_u32_list bb);
819 
820 /* bb_compat */
821 extern errcode_t badblocks_list_create(badblocks_list *ret, int size);
822 extern errcode_t badblocks_list_add(badblocks_list bb, blk_t blk);
823 extern int badblocks_list_test(badblocks_list bb, blk_t blk);
824 extern errcode_t badblocks_list_iterate_begin(badblocks_list bb,
825 					      badblocks_iterate *ret);
826 extern int badblocks_list_iterate(badblocks_iterate iter, blk_t *blk);
827 extern void badblocks_list_iterate_end(badblocks_iterate iter);
828 extern void badblocks_list_free(badblocks_list bb);
829 
830 /* bb_inode.c */
831 extern errcode_t ext2fs_update_bb_inode(ext2_filsys fs,
832 					ext2_badblocks_list bb_list);
833 
834 /* bitmaps.c */
835 extern void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap);
836 extern void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap);
837 extern errcode_t ext2fs_copy_bitmap(ext2fs_generic_bitmap src,
838 				    ext2fs_generic_bitmap *dest);
839 extern errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs);
840 extern errcode_t ext2fs_write_block_bitmap (ext2_filsys fs);
841 extern errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs);
842 extern errcode_t ext2fs_read_block_bitmap(ext2_filsys fs);
843 extern errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
844 					      const char *descr,
845 					      ext2fs_block_bitmap *ret);
846 extern errcode_t ext2fs_allocate_subcluster_bitmap(ext2_filsys fs,
847 						   const char *descr,
848 						   ext2fs_block_bitmap *ret);
849 extern int ext2fs_get_bitmap_granularity(ext2fs_block_bitmap bitmap);
850 extern errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
851 					      const char *descr,
852 					      ext2fs_inode_bitmap *ret);
853 extern errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
854 					       ext2_ino_t end, ext2_ino_t *oend);
855 extern errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
856 					       blk_t end, blk_t *oend);
857 extern errcode_t ext2fs_fudge_block_bitmap_end2(ext2fs_block_bitmap bitmap,
858 					 blk64_t end, blk64_t *oend);
859 extern void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap);
860 extern void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap);
861 extern errcode_t ext2fs_read_bitmaps(ext2_filsys fs);
862 extern errcode_t ext2fs_write_bitmaps(ext2_filsys fs);
863 extern errcode_t ext2fs_resize_inode_bitmap(__u32 new_end, __u32 new_real_end,
864 					    ext2fs_inode_bitmap bmap);
865 extern errcode_t ext2fs_resize_inode_bitmap2(__u64 new_end,
866 					     __u64 new_real_end,
867 					     ext2fs_inode_bitmap bmap);
868 extern errcode_t ext2fs_resize_block_bitmap(__u32 new_end, __u32 new_real_end,
869 					    ext2fs_block_bitmap bmap);
870 extern errcode_t ext2fs_resize_block_bitmap2(__u64 new_end,
871 					     __u64 new_real_end,
872 					     ext2fs_block_bitmap bmap);
873 extern errcode_t ext2fs_compare_block_bitmap(ext2fs_block_bitmap bm1,
874 					     ext2fs_block_bitmap bm2);
875 extern errcode_t ext2fs_compare_inode_bitmap(ext2fs_inode_bitmap bm1,
876 					     ext2fs_inode_bitmap bm2);
877 extern errcode_t ext2fs_set_inode_bitmap_range(ext2fs_inode_bitmap bmap,
878 					ext2_ino_t start, unsigned int num,
879 					void *in);
880 extern errcode_t ext2fs_set_inode_bitmap_range2(ext2fs_inode_bitmap bmap,
881 					 __u64 start, size_t num,
882 					 void *in);
883 extern errcode_t ext2fs_get_inode_bitmap_range(ext2fs_inode_bitmap bmap,
884 					ext2_ino_t start, unsigned int num,
885 					void *out);
886 extern errcode_t ext2fs_get_inode_bitmap_range2(ext2fs_inode_bitmap bmap,
887 					 __u64 start, size_t num,
888 					 void *out);
889 extern errcode_t ext2fs_set_block_bitmap_range(ext2fs_block_bitmap bmap,
890 					blk_t start, unsigned int num,
891 					void *in);
892 extern errcode_t ext2fs_set_block_bitmap_range2(ext2fs_block_bitmap bmap,
893 					 blk64_t start, size_t num,
894 					 void *in);
895 extern errcode_t ext2fs_get_block_bitmap_range(ext2fs_block_bitmap bmap,
896 					blk_t start, unsigned int num,
897 					void *out);
898 extern errcode_t ext2fs_get_block_bitmap_range2(ext2fs_block_bitmap bmap,
899 					 blk64_t start, size_t num,
900 					 void *out);
901 
902 /* blknum.c */
903 extern __u32 ext2fs_inode_bitmap_checksum(ext2_filsys fs, dgrp_t group);
904 extern __u32 ext2fs_block_bitmap_checksum(ext2_filsys fs, dgrp_t group);
905 extern dgrp_t ext2fs_group_of_blk2(ext2_filsys fs, blk64_t);
906 extern blk64_t ext2fs_group_first_block2(ext2_filsys fs, dgrp_t group);
907 extern blk64_t ext2fs_group_last_block2(ext2_filsys fs, dgrp_t group);
908 extern int ext2fs_group_blocks_count(ext2_filsys fs, dgrp_t group);
909 extern blk64_t ext2fs_inode_data_blocks2(ext2_filsys fs,
910 					 struct ext2_inode *inode);
911 extern blk64_t ext2fs_inode_i_blocks(ext2_filsys fs,
912 					 struct ext2_inode *inode);
913 extern blk64_t ext2fs_blocks_count(struct ext2_super_block *super);
914 extern void ext2fs_blocks_count_set(struct ext2_super_block *super,
915 				    blk64_t blk);
916 extern void ext2fs_blocks_count_add(struct ext2_super_block *super,
917 				    blk64_t blk);
918 extern blk64_t ext2fs_r_blocks_count(struct ext2_super_block *super);
919 extern void ext2fs_r_blocks_count_set(struct ext2_super_block *super,
920 				      blk64_t blk);
921 extern void ext2fs_r_blocks_count_add(struct ext2_super_block *super,
922 				      blk64_t blk);
923 extern blk64_t ext2fs_free_blocks_count(struct ext2_super_block *super);
924 extern void ext2fs_free_blocks_count_set(struct ext2_super_block *super,
925 					 blk64_t blk);
926 extern void ext2fs_free_blocks_count_add(struct ext2_super_block *super,
927 					 blk64_t blk);
928 /* Block group descriptor accessor functions */
929 extern struct ext2_group_desc *ext2fs_group_desc(ext2_filsys fs,
930 					  struct opaque_ext2_group_desc *gdp,
931 					  dgrp_t group);
932 extern blk64_t ext2fs_block_bitmap_csum(ext2_filsys fs, dgrp_t group);
933 extern blk64_t ext2fs_block_bitmap_loc(ext2_filsys fs, dgrp_t group);
934 extern void ext2fs_block_bitmap_loc_set(ext2_filsys fs, dgrp_t group,
935 					blk64_t blk);
936 extern __u32 ext2fs_inode_bitmap_csum(ext2_filsys fs, dgrp_t group);
937 extern blk64_t ext2fs_inode_bitmap_loc(ext2_filsys fs, dgrp_t group);
938 extern void ext2fs_inode_bitmap_loc_set(ext2_filsys fs, dgrp_t group,
939 					blk64_t blk);
940 extern blk64_t ext2fs_inode_table_loc(ext2_filsys fs, dgrp_t group);
941 extern void ext2fs_inode_table_loc_set(ext2_filsys fs, dgrp_t group,
942 				       blk64_t blk);
943 extern __u32 ext2fs_bg_free_blocks_count(ext2_filsys fs, dgrp_t group);
944 extern void ext2fs_bg_free_blocks_count_set(ext2_filsys fs, dgrp_t group,
945 					 __u32 n);
946 extern __u32 ext2fs_bg_free_inodes_count(ext2_filsys fs, dgrp_t group);
947 extern void ext2fs_bg_free_inodes_count_set(ext2_filsys fs, dgrp_t group,
948 					 __u32 n);
949 extern __u32 ext2fs_bg_used_dirs_count(ext2_filsys fs, dgrp_t group);
950 extern void ext2fs_bg_used_dirs_count_set(ext2_filsys fs, dgrp_t group,
951 				       __u32 n);
952 extern __u32 ext2fs_bg_itable_unused(ext2_filsys fs, dgrp_t group);
953 extern void ext2fs_bg_itable_unused_set(ext2_filsys fs, dgrp_t group,
954 				     __u32 n);
955 extern __u16 ext2fs_bg_flags(ext2_filsys fs, dgrp_t group);
956 extern void ext2fs_bg_flags_zap(ext2_filsys fs, dgrp_t group);
957 extern int ext2fs_bg_flags_test(ext2_filsys fs, dgrp_t group, __u16 bg_flag);
958 extern void ext2fs_bg_flags_set(ext2_filsys fs, dgrp_t group, __u16 bg_flags);
959 extern void ext2fs_bg_flags_clear(ext2_filsys fs, dgrp_t group, __u16 bg_flags);
960 extern __u16 ext2fs_bg_checksum(ext2_filsys fs, dgrp_t group);
961 extern void ext2fs_bg_checksum_set(ext2_filsys fs, dgrp_t group, __u16 checksum);
962 extern blk64_t ext2fs_file_acl_block(ext2_filsys fs,
963 				     const struct ext2_inode *inode);
964 extern void ext2fs_file_acl_block_set(ext2_filsys fs,
965 				      struct ext2_inode *inode, blk64_t blk);
966 extern errcode_t ext2fs_inode_size_set(ext2_filsys fs, struct ext2_inode *inode,
967 				       ext2_off64_t size);
968 
969 /* block.c */
970 extern errcode_t ext2fs_block_iterate(ext2_filsys fs,
971 				      ext2_ino_t	ino,
972 				      int	flags,
973 				      char *block_buf,
974 				      int (*func)(ext2_filsys fs,
975 						  blk_t	*blocknr,
976 						  int	blockcnt,
977 						  void	*priv_data),
978 				      void *priv_data);
979 errcode_t ext2fs_block_iterate2(ext2_filsys fs,
980 				ext2_ino_t	ino,
981 				int	flags,
982 				char *block_buf,
983 				int (*func)(ext2_filsys fs,
984 					    blk_t	*blocknr,
985 					    e2_blkcnt_t	blockcnt,
986 					    blk_t	ref_blk,
987 					    int		ref_offset,
988 					    void	*priv_data),
989 				void *priv_data);
990 errcode_t ext2fs_block_iterate3(ext2_filsys fs,
991 				ext2_ino_t ino,
992 				int	flags,
993 				char *block_buf,
994 				int (*func)(ext2_filsys fs,
995 					    blk64_t	*blocknr,
996 					    e2_blkcnt_t	blockcnt,
997 					    blk64_t	ref_blk,
998 					    int		ref_offset,
999 					    void	*priv_data),
1000 				void *priv_data);
1001 
1002 /* bmap.c */
1003 extern errcode_t ext2fs_bmap(ext2_filsys fs, ext2_ino_t ino,
1004 			     struct ext2_inode *inode,
1005 			     char *block_buf, int bmap_flags,
1006 			     blk_t block, blk_t *phys_blk);
1007 extern errcode_t ext2fs_bmap2(ext2_filsys fs, ext2_ino_t ino,
1008 			      struct ext2_inode *inode,
1009 			      char *block_buf, int bmap_flags, blk64_t block,
1010 			      int *ret_flags, blk64_t *phys_blk);
1011 errcode_t ext2fs_map_cluster_block(ext2_filsys fs, ext2_ino_t ino,
1012 				   struct ext2_inode *inode, blk64_t lblk,
1013 				   blk64_t *pblk);
1014 
1015 #if 0
1016 /* bmove.c */
1017 extern errcode_t ext2fs_move_blocks(ext2_filsys fs,
1018 				    ext2fs_block_bitmap reserve,
1019 				    ext2fs_block_bitmap alloc_map,
1020 				    int flags);
1021 #endif
1022 
1023 /* check_desc.c */
1024 extern errcode_t ext2fs_check_desc(ext2_filsys fs);
1025 
1026 /* closefs.c */
1027 extern errcode_t ext2fs_close(ext2_filsys fs);
1028 extern errcode_t ext2fs_close2(ext2_filsys fs, int flags);
1029 extern errcode_t ext2fs_close_free(ext2_filsys *fs);
1030 extern errcode_t ext2fs_flush(ext2_filsys fs);
1031 extern errcode_t ext2fs_flush2(ext2_filsys fs, int flags);
1032 extern int ext2fs_bg_has_super(ext2_filsys fs, dgrp_t group_block);
1033 extern errcode_t ext2fs_super_and_bgd_loc2(ext2_filsys fs,
1034 				    dgrp_t group,
1035 				    blk64_t *ret_super_blk,
1036 				    blk64_t *ret_old_desc_blk,
1037 				    blk64_t *ret_new_desc_blk,
1038 				    blk_t *ret_used_blks);
1039 extern int ext2fs_super_and_bgd_loc(ext2_filsys fs,
1040 				    dgrp_t group,
1041 				    blk_t *ret_super_blk,
1042 				    blk_t *ret_old_desc_blk,
1043 				    blk_t *ret_new_desc_blk,
1044 				    int *ret_meta_bg);
1045 extern void ext2fs_update_dynamic_rev(ext2_filsys fs);
1046 
1047 /* crc32c.c */
1048 extern __u32 ext2fs_crc32_be(__u32 crc, unsigned char const *p, size_t len);
1049 extern __u32 ext2fs_crc32c_le(__u32 crc, unsigned char const *p, size_t len);
1050 
1051 /* csum.c */
1052 extern void ext2fs_init_csum_seed(ext2_filsys fs);
1053 extern errcode_t ext2fs_mmp_csum_set(ext2_filsys fs, struct mmp_struct *mmp);
1054 extern int ext2fs_mmp_csum_verify(ext2_filsys, struct mmp_struct *mmp);
1055 extern int ext2fs_verify_csum_type(ext2_filsys fs, struct ext2_super_block *sb);
1056 extern errcode_t ext2fs_superblock_csum_set(ext2_filsys fs,
1057 					    struct ext2_super_block *sb);
1058 extern int ext2fs_superblock_csum_verify(ext2_filsys fs,
1059 					 struct ext2_super_block *sb);
1060 extern errcode_t ext2fs_ext_attr_block_csum_set(ext2_filsys fs,
1061 					ext2_ino_t inum, blk64_t block,
1062 					struct ext2_ext_attr_header *hdr);
1063 extern int ext2fs_ext_attr_block_csum_verify(ext2_filsys fs, ext2_ino_t inum,
1064 					     blk64_t block,
1065 					     struct ext2_ext_attr_header *hdr);
1066 #define EXT2_DIRENT_TAIL(block, blocksize) \
1067 	((struct ext2_dir_entry_tail *)(((char *)(block)) + \
1068 	(blocksize) - sizeof(struct ext2_dir_entry_tail)))
1069 
1070 extern void ext2fs_initialize_dirent_tail(ext2_filsys fs,
1071 					  struct ext2_dir_entry_tail *t);
1072 extern int ext2fs_dirent_has_tail(ext2_filsys fs,
1073 				  struct ext2_dir_entry *dirent);
1074 extern int ext2fs_dirent_csum_verify(ext2_filsys fs, ext2_ino_t inum,
1075 				     struct ext2_dir_entry *dirent);
1076 extern int ext2fs_dir_block_csum_verify(ext2_filsys fs, ext2_ino_t inum,
1077 					struct ext2_dir_entry *dirent);
1078 extern errcode_t ext2fs_dir_block_csum_set(ext2_filsys fs, ext2_ino_t inum,
1079 					   struct ext2_dir_entry *dirent);
1080 extern errcode_t ext2fs_get_dx_countlimit(ext2_filsys fs,
1081 					  struct ext2_dir_entry *dirent,
1082 					  struct ext2_dx_countlimit **cc,
1083 					  int *offset);
1084 extern errcode_t ext2fs_extent_block_csum_set(ext2_filsys fs,
1085 					      ext2_ino_t inum,
1086 					      struct ext3_extent_header *eh);
1087 extern int ext2fs_extent_block_csum_verify(ext2_filsys fs,
1088 					   ext2_ino_t inum,
1089 					   struct ext3_extent_header *eh);
1090 extern errcode_t ext2fs_block_bitmap_csum_set(ext2_filsys fs, dgrp_t group,
1091 					      char *bitmap, int size);
1092 extern int ext2fs_block_bitmap_csum_verify(ext2_filsys fs, dgrp_t group,
1093 					   char *bitmap, int size);
1094 extern errcode_t ext2fs_inode_bitmap_csum_set(ext2_filsys fs, dgrp_t group,
1095 					      char *bitmap, int size);
1096 extern int ext2fs_inode_bitmap_csum_verify(ext2_filsys fs, dgrp_t group,
1097 					   char *bitmap, int size);
1098 extern errcode_t ext2fs_inode_csum_set(ext2_filsys fs, ext2_ino_t inum,
1099 				       struct ext2_inode_large *inode);
1100 extern int ext2fs_inode_csum_verify(ext2_filsys fs, ext2_ino_t inum,
1101 				    struct ext2_inode_large *inode);
1102 extern void ext2fs_group_desc_csum_set(ext2_filsys fs, dgrp_t group);
1103 extern int ext2fs_group_desc_csum_verify(ext2_filsys fs, dgrp_t group);
1104 extern errcode_t ext2fs_set_gdt_csum(ext2_filsys fs);
1105 extern __u16 ext2fs_group_desc_csum(ext2_filsys fs, dgrp_t group);
1106 
1107 /* dblist.c */
1108 extern errcode_t ext2fs_init_dblist(ext2_filsys fs, ext2_dblist *ret_dblist);
1109 extern errcode_t ext2fs_add_dir_block(ext2_dblist dblist, ext2_ino_t ino,
1110 				      blk_t blk, int blockcnt);
1111 extern errcode_t ext2fs_add_dir_block2(ext2_dblist dblist, ext2_ino_t ino,
1112 				       blk64_t blk, e2_blkcnt_t blockcnt);
1113 extern void ext2fs_dblist_sort(ext2_dblist dblist,
1114 			       EXT2_QSORT_TYPE (*sortfunc)(const void *,
1115 							   const void *));
1116 extern void ext2fs_dblist_sort2(ext2_dblist dblist,
1117 				EXT2_QSORT_TYPE (*sortfunc)(const void *,
1118 							    const void *));
1119 extern errcode_t ext2fs_dblist_iterate(ext2_dblist dblist,
1120 	int (*func)(ext2_filsys fs, struct ext2_db_entry *db_info,
1121 		    void	*priv_data),
1122 	void *priv_data);
1123 extern errcode_t ext2fs_dblist_iterate2(ext2_dblist dblist,
1124 	int (*func)(ext2_filsys fs, struct ext2_db_entry2 *db_info,
1125 		    void	*priv_data),
1126 	void *priv_data);
1127 extern errcode_t ext2fs_dblist_iterate3(ext2_dblist dblist,
1128 	int (*func)(ext2_filsys fs, struct ext2_db_entry2 *db_info,
1129 		    void	*priv_data),
1130 	unsigned long long start,
1131 	unsigned long long count,
1132 	void *priv_data);
1133 extern errcode_t ext2fs_set_dir_block(ext2_dblist dblist, ext2_ino_t ino,
1134 				      blk_t blk, int blockcnt);
1135 extern errcode_t ext2fs_set_dir_block2(ext2_dblist dblist, ext2_ino_t ino,
1136 				       blk64_t blk, e2_blkcnt_t blockcnt);
1137 extern errcode_t ext2fs_copy_dblist(ext2_dblist src,
1138 				    ext2_dblist *dest);
1139 extern int ext2fs_dblist_count(ext2_dblist dblist);
1140 extern blk64_t ext2fs_dblist_count2(ext2_dblist dblist);
1141 extern errcode_t ext2fs_dblist_get_last(ext2_dblist dblist,
1142 					struct ext2_db_entry **entry);
1143 extern errcode_t ext2fs_dblist_get_last2(ext2_dblist dblist,
1144 					struct ext2_db_entry2 **entry);
1145 extern errcode_t ext2fs_dblist_drop_last(ext2_dblist dblist);
1146 
1147 /* dblist_dir.c */
1148 extern errcode_t
1149 	ext2fs_dblist_dir_iterate(ext2_dblist dblist,
1150 				  int	flags,
1151 				  char	*block_buf,
1152 				  int (*func)(ext2_ino_t	dir,
1153 					      int		entry,
1154 					      struct ext2_dir_entry *dirent,
1155 					      int	offset,
1156 					      int	blocksize,
1157 					      char	*buf,
1158 					      void	*priv_data),
1159 				  void *priv_data);
1160 
1161 #if 0
1162 /* digest_encode.c */
1163 #define EXT2FS_DIGEST_SIZE EXT2FS_SHA256_LENGTH
1164 extern int ext2fs_digest_encode(const char *src, int len, char *dst);
1165 extern int ext2fs_digest_decode(const char *src, int len, char *dst);
1166 #endif
1167 
1168 /* dirblock.c */
1169 extern errcode_t ext2fs_read_dir_block(ext2_filsys fs, blk_t block,
1170 				       void *buf);
1171 extern errcode_t ext2fs_read_dir_block2(ext2_filsys fs, blk_t block,
1172 					void *buf, int flags);
1173 extern errcode_t ext2fs_read_dir_block3(ext2_filsys fs, blk64_t block,
1174 					void *buf, int flags);
1175 extern errcode_t ext2fs_read_dir_block4(ext2_filsys fs, blk64_t block,
1176 					void *buf, int flags, ext2_ino_t ino);
1177 extern errcode_t ext2fs_write_dir_block(ext2_filsys fs, blk_t block,
1178 					void *buf);
1179 extern errcode_t ext2fs_write_dir_block2(ext2_filsys fs, blk_t block,
1180 					 void *buf, int flags);
1181 extern errcode_t ext2fs_write_dir_block3(ext2_filsys fs, blk64_t block,
1182 					 void *buf, int flags);
1183 extern errcode_t ext2fs_write_dir_block4(ext2_filsys fs, blk64_t block,
1184 					 void *buf, int flags, ext2_ino_t ino);
1185 
1186 /* dirhash.c */
1187 extern errcode_t ext2fs_dirhash(int version, const char *name, int len,
1188 				const __u32 *seed,
1189 				ext2_dirhash_t *ret_hash,
1190 				ext2_dirhash_t *ret_minor_hash);
1191 
1192 extern errcode_t ext2fs_dirhash2(int version, const char *name, int len,
1193 				 const struct ext2fs_nls_table *charset,
1194 				 int hash_flags,
1195 				 const __u32 *seed,
1196 				 ext2_dirhash_t *ret_hash,
1197 				 ext2_dirhash_t *ret_minor_hash);
1198 
1199 /* dir_iterate.c */
1200 extern errcode_t ext2fs_get_rec_len(ext2_filsys fs,
1201 				    struct ext2_dir_entry *dirent,
1202 				    unsigned int *rec_len);
1203 extern errcode_t ext2fs_set_rec_len(ext2_filsys fs,
1204 				    unsigned int len,
1205 				    struct ext2_dir_entry *dirent);
1206 extern errcode_t ext2fs_dir_iterate(ext2_filsys fs,
1207 			      ext2_ino_t dir,
1208 			      int flags,
1209 			      char *block_buf,
1210 			      int (*func)(struct ext2_dir_entry *dirent,
1211 					  int	offset,
1212 					  int	blocksize,
1213 					  char	*buf,
1214 					  void	*priv_data),
1215 			      void *priv_data);
1216 extern errcode_t ext2fs_dir_iterate2(ext2_filsys fs,
1217 			      ext2_ino_t dir,
1218 			      int flags,
1219 			      char *block_buf,
1220 			      int (*func)(ext2_ino_t	dir,
1221 					  int	entry,
1222 					  struct ext2_dir_entry *dirent,
1223 					  int	offset,
1224 					  int	blocksize,
1225 					  char	*buf,
1226 					  void	*priv_data),
1227 			      void *priv_data);
1228 
1229 /* dupfs.c */
1230 extern errcode_t ext2fs_dup_handle(ext2_filsys src, ext2_filsys *dest);
1231 
1232 /* expanddir.c */
1233 extern errcode_t ext2fs_expand_dir(ext2_filsys fs, ext2_ino_t dir);
1234 
1235 /* ext_attr.c */
1236 extern __u32 ext2fs_ext_attr_hash_entry(struct ext2_ext_attr_entry *entry,
1237 					void *data);
1238 extern errcode_t ext2fs_ext_attr_hash_entry2(ext2_filsys fs,
1239 					     struct ext2_ext_attr_entry *entry,
1240 					     void *data, __u32 *hash);
1241 extern errcode_t ext2fs_read_ext_attr(ext2_filsys fs, blk_t block, void *buf);
1242 extern errcode_t ext2fs_read_ext_attr2(ext2_filsys fs, blk64_t block,
1243 				       void *buf);
1244 extern errcode_t ext2fs_read_ext_attr3(ext2_filsys fs, blk64_t block,
1245 				       void *buf, ext2_ino_t inum);
1246 extern errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block,
1247 				       void *buf);
1248 extern errcode_t ext2fs_write_ext_attr2(ext2_filsys fs, blk64_t block,
1249 				       void *buf);
1250 extern errcode_t ext2fs_write_ext_attr3(ext2_filsys fs, blk64_t block,
1251 				       void *buf, ext2_ino_t inum);
1252 extern errcode_t ext2fs_adjust_ea_refcount(ext2_filsys fs, blk_t blk,
1253 					   char *block_buf,
1254 					   int adjust, __u32 *newcount);
1255 extern errcode_t ext2fs_adjust_ea_refcount2(ext2_filsys fs, blk64_t blk,
1256 					   char *block_buf,
1257 					   int adjust, __u32 *newcount);
1258 extern errcode_t ext2fs_adjust_ea_refcount3(ext2_filsys fs, blk64_t blk,
1259 					   char *block_buf,
1260 					   int adjust, __u32 *newcount,
1261 					   ext2_ino_t inum);
1262 errcode_t ext2fs_xattrs_write(struct ext2_xattr_handle *handle);
1263 errcode_t ext2fs_xattrs_read(struct ext2_xattr_handle *handle);
1264 errcode_t ext2fs_xattrs_iterate(struct ext2_xattr_handle *h,
1265 				int (*func)(char *name, char *value,
1266 					    size_t value_len, void *data),
1267 				void *data);
1268 errcode_t ext2fs_xattr_get(struct ext2_xattr_handle *h, const char *key,
1269 			   void **value, size_t *value_len);
1270 errcode_t ext2fs_xattr_set(struct ext2_xattr_handle *handle,
1271 			   const char *key,
1272 			   const void *value,
1273 			   size_t value_len);
1274 errcode_t ext2fs_xattr_remove(struct ext2_xattr_handle *handle,
1275 			      const char *key);
1276 errcode_t ext2fs_xattrs_open(ext2_filsys fs, ext2_ino_t ino,
1277 			     struct ext2_xattr_handle **handle);
1278 errcode_t ext2fs_xattrs_close(struct ext2_xattr_handle **handle);
1279 errcode_t ext2fs_free_ext_attr(ext2_filsys fs, ext2_ino_t ino,
1280 			       struct ext2_inode_large *inode);
1281 errcode_t ext2fs_xattrs_count(struct ext2_xattr_handle *handle, size_t *count);
1282 errcode_t ext2fs_xattr_inode_max_size(ext2_filsys fs, ext2_ino_t ino,
1283 				      size_t *size);
1284 #define XATTR_HANDLE_FLAG_RAW	0x0001
1285 errcode_t ext2fs_xattrs_flags(struct ext2_xattr_handle *handle,
1286 			      unsigned int *new_flags, unsigned int *old_flags);
1287 extern void ext2fs_ext_attr_block_rehash(struct ext2_ext_attr_header *header,
1288 					 struct ext2_ext_attr_entry *end);
1289 extern __u32 ext2fs_get_ea_inode_hash(struct ext2_inode *inode);
1290 extern void ext2fs_set_ea_inode_hash(struct ext2_inode *inode, __u32 hash);
1291 extern __u64 ext2fs_get_ea_inode_ref(struct ext2_inode *inode);
1292 extern void ext2fs_set_ea_inode_ref(struct ext2_inode *inode, __u64 ref_count);
1293 
1294 /* extent.c */
1295 extern errcode_t ext2fs_extent_header_verify(void *ptr, int size);
1296 extern errcode_t ext2fs_extent_open(ext2_filsys fs, ext2_ino_t ino,
1297 				    ext2_extent_handle_t *handle);
1298 extern errcode_t ext2fs_extent_open2(ext2_filsys fs, ext2_ino_t ino,
1299 					struct ext2_inode *inode,
1300 					ext2_extent_handle_t *ret_handle);
1301 extern void ext2fs_extent_free(ext2_extent_handle_t handle);
1302 extern errcode_t ext2fs_extent_get(ext2_extent_handle_t handle,
1303 				   int flags, struct ext2fs_extent *extent);
1304 extern errcode_t ext2fs_extent_node_split(ext2_extent_handle_t handle);
1305 extern errcode_t ext2fs_extent_replace(ext2_extent_handle_t handle, int flags,
1306 				       struct ext2fs_extent *extent);
1307 extern errcode_t ext2fs_extent_insert(ext2_extent_handle_t handle, int flags,
1308 				      struct ext2fs_extent *extent);
1309 extern errcode_t ext2fs_extent_set_bmap(ext2_extent_handle_t handle,
1310 					blk64_t logical, blk64_t physical,
1311 					int flags);
1312 extern errcode_t ext2fs_extent_delete(ext2_extent_handle_t handle, int flags);
1313 extern errcode_t ext2fs_extent_get_info(ext2_extent_handle_t handle,
1314 					struct ext2_extent_info *info);
1315 extern errcode_t ext2fs_extent_goto(ext2_extent_handle_t handle,
1316 				    blk64_t blk);
1317 extern errcode_t ext2fs_extent_goto2(ext2_extent_handle_t handle,
1318 				     int leaf_level, blk64_t blk);
1319 extern errcode_t ext2fs_extent_fix_parents(ext2_extent_handle_t handle);
1320 extern size_t ext2fs_max_extent_depth(ext2_extent_handle_t handle);
1321 extern errcode_t ext2fs_fix_extents_checksums(ext2_filsys fs, ext2_ino_t ino,
1322 					      struct ext2_inode *inode);
1323 
1324 /* fallocate.c */
1325 #define EXT2_FALLOCATE_ZERO_BLOCKS	(0x1)
1326 #define EXT2_FALLOCATE_FORCE_INIT	(0x2)
1327 #define EXT2_FALLOCATE_FORCE_UNINIT	(0x4)
1328 #define EXT2_FALLOCATE_INIT_BEYOND_EOF	(0x8)
1329 #define EXT2_FALLOCATE_ALL_FLAGS	(0xF)
1330 errcode_t ext2fs_fallocate(ext2_filsys fs, int flags, ext2_ino_t ino,
1331 			   struct ext2_inode *inode, blk64_t goal,
1332 			   blk64_t start, blk64_t len);
1333 
1334 /* fileio.c */
1335 extern errcode_t ext2fs_file_open2(ext2_filsys fs, ext2_ino_t ino,
1336 				   struct ext2_inode *inode,
1337 				   int flags, ext2_file_t *ret);
1338 extern errcode_t ext2fs_file_open(ext2_filsys fs, ext2_ino_t ino,
1339 				  int flags, ext2_file_t *ret);
1340 extern ext2_filsys ext2fs_file_get_fs(ext2_file_t file);
1341 struct ext2_inode *ext2fs_file_get_inode(ext2_file_t file);
1342 extern ext2_ino_t ext2fs_file_get_inode_num(ext2_file_t file);
1343 extern errcode_t ext2fs_file_close(ext2_file_t file);
1344 extern errcode_t ext2fs_file_flush(ext2_file_t file);
1345 extern errcode_t ext2fs_file_read(ext2_file_t file, void *buf,
1346 				  unsigned int wanted, unsigned int *got);
1347 extern errcode_t ext2fs_file_write(ext2_file_t file, const void *buf,
1348 				   unsigned int nbytes, unsigned int *written);
1349 extern errcode_t ext2fs_file_llseek(ext2_file_t file, __u64 offset,
1350 				   int whence, __u64 *ret_pos);
1351 extern errcode_t ext2fs_file_lseek(ext2_file_t file, ext2_off_t offset,
1352 				   int whence, ext2_off_t *ret_pos);
1353 errcode_t ext2fs_file_get_lsize(ext2_file_t file, __u64 *ret_size);
1354 extern ext2_off_t ext2fs_file_get_size(ext2_file_t file);
1355 extern errcode_t ext2fs_file_set_size(ext2_file_t file, ext2_off_t size);
1356 extern errcode_t ext2fs_file_set_size2(ext2_file_t file, ext2_off64_t size);
1357 
1358 /* finddev.c */
1359 extern char *ext2fs_find_block_device(dev_t device);
1360 
1361 /* flushb.c */
1362 extern errcode_t ext2fs_sync_device(int fd, int flushb);
1363 
1364 /* freefs.c */
1365 extern void ext2fs_free(ext2_filsys fs);
1366 extern void ext2fs_free_dblist(ext2_dblist dblist);
1367 extern void ext2fs_badblocks_list_free(ext2_badblocks_list bb);
1368 extern void ext2fs_u32_list_free(ext2_u32_list bb);
1369 
1370 /* gen_bitmap.c */
1371 extern void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap);
1372 extern errcode_t ext2fs_make_generic_bitmap(errcode_t magic, ext2_filsys fs,
1373 					    __u32 start, __u32 end,
1374 					    __u32 real_end,
1375 					    const char *descr, char *init_map,
1376 					    ext2fs_generic_bitmap *ret);
1377 extern errcode_t ext2fs_allocate_generic_bitmap(__u32 start,
1378 						__u32 end,
1379 						__u32 real_end,
1380 						const char *descr,
1381 						ext2fs_generic_bitmap *ret);
1382 extern errcode_t ext2fs_copy_generic_bitmap(ext2fs_generic_bitmap src,
1383 					    ext2fs_generic_bitmap *dest);
1384 extern void ext2fs_clear_generic_bitmap(ext2fs_generic_bitmap bitmap);
1385 extern errcode_t ext2fs_fudge_generic_bitmap_end(ext2fs_inode_bitmap bitmap,
1386 						 errcode_t magic,
1387 						 errcode_t neq,
1388 						 ext2_ino_t end,
1389 						 ext2_ino_t *oend);
1390 extern void ext2fs_set_generic_bitmap_padding(ext2fs_generic_bitmap map);
1391 extern errcode_t ext2fs_resize_generic_bitmap(errcode_t magic,
1392 					      __u32 new_end,
1393 					      __u32 new_real_end,
1394 					      ext2fs_generic_bitmap bmap);
1395 extern errcode_t ext2fs_compare_generic_bitmap(errcode_t magic, errcode_t neq,
1396 					       ext2fs_generic_bitmap bm1,
1397 					       ext2fs_generic_bitmap bm2);
1398 extern errcode_t ext2fs_get_generic_bitmap_range(ext2fs_generic_bitmap bmap,
1399 						 errcode_t magic,
1400 						 __u32 start, __u32 num,
1401 						 void *out);
1402 extern errcode_t ext2fs_set_generic_bitmap_range(ext2fs_generic_bitmap bmap,
1403 						 errcode_t magic,
1404 						 __u32 start, __u32 num,
1405 						 void *in);
1406 extern errcode_t ext2fs_find_first_zero_generic_bitmap(ext2fs_generic_bitmap bitmap,
1407 						       __u32 start, __u32 end,
1408 						       __u32 *out);
1409 extern errcode_t ext2fs_find_first_set_generic_bitmap(ext2fs_generic_bitmap bitmap,
1410 						       __u32 start, __u32 end,
1411 						       __u32 *out);
1412 
1413 /* gen_bitmap64.c */
1414 void ext2fs_free_generic_bmap(ext2fs_generic_bitmap bmap);
1415 errcode_t ext2fs_alloc_generic_bmap(ext2_filsys fs, errcode_t magic,
1416 				    int type, __u64 start, __u64 end,
1417 				    __u64 real_end,
1418 				    const char *descr,
1419 				    ext2fs_generic_bitmap *ret);
1420 errcode_t ext2fs_copy_generic_bmap(ext2fs_generic_bitmap src,
1421 				   ext2fs_generic_bitmap *dest);
1422 void ext2fs_clear_generic_bmap(ext2fs_generic_bitmap bitmap);
1423 errcode_t ext2fs_fudge_generic_bmap_end(ext2fs_generic_bitmap bitmap,
1424 					errcode_t neq,
1425 					__u64 end, __u64 *oend);
1426 void ext2fs_set_generic_bmap_padding(ext2fs_generic_bitmap bmap);
1427 errcode_t ext2fs_resize_generic_bmap(ext2fs_generic_bitmap bmap,
1428 				     __u64 new_end,
1429 				     __u64 new_real_end);
1430 errcode_t ext2fs_compare_generic_bmap(errcode_t neq,
1431 				      ext2fs_generic_bitmap bm1,
1432 				      ext2fs_generic_bitmap bm2);
1433 errcode_t ext2fs_get_generic_bmap_range(ext2fs_generic_bitmap bmap,
1434 					__u64 start, unsigned int num,
1435 					void *out);
1436 errcode_t ext2fs_set_generic_bmap_range(ext2fs_generic_bitmap bmap,
1437 					__u64 start, unsigned int num,
1438 					void *in);
1439 errcode_t ext2fs_convert_subcluster_bitmap(ext2_filsys fs,
1440 					   ext2fs_block_bitmap *bitmap);
1441 
1442 /* get_num_dirs.c */
1443 extern errcode_t ext2fs_get_num_dirs(ext2_filsys fs, ext2_ino_t *ret_num_dirs);
1444 
1445 /* getsize.c */
1446 extern errcode_t ext2fs_get_device_size(const char *file, int blocksize,
1447 					blk_t *retblocks);
1448 extern errcode_t ext2fs_get_device_size2(const char *file, int blocksize,
1449 					blk64_t *retblocks);
1450 
1451 /* getsectsize.c */
1452 extern int ext2fs_get_dio_alignment(int fd);
1453 errcode_t ext2fs_get_device_sectsize(const char *file, int *sectsize);
1454 errcode_t ext2fs_get_device_phys_sectsize(const char *file, int *sectsize);
1455 
1456 /* i_block.c */
1457 errcode_t ext2fs_iblk_add_blocks(ext2_filsys fs, struct ext2_inode *inode,
1458 				 blk64_t num_blocks);
1459 errcode_t ext2fs_iblk_sub_blocks(ext2_filsys fs, struct ext2_inode *inode,
1460 				 blk64_t num_blocks);
1461 errcode_t ext2fs_iblk_set(ext2_filsys fs, struct ext2_inode *inode, blk64_t b);
1462 
1463 /* imager.c */
1464 extern errcode_t ext2fs_image_inode_write(ext2_filsys fs, int fd, int flags);
1465 extern errcode_t ext2fs_image_inode_read(ext2_filsys fs, int fd, int flags);
1466 extern errcode_t ext2fs_image_super_write(ext2_filsys fs, int fd, int flags);
1467 extern errcode_t ext2fs_image_super_read(ext2_filsys fs, int fd, int flags);
1468 extern errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags);
1469 extern errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags);
1470 
1471 /* ind_block.c */
1472 errcode_t ext2fs_read_ind_block(ext2_filsys fs, blk_t blk, void *buf);
1473 errcode_t ext2fs_write_ind_block(ext2_filsys fs, blk_t blk, void *buf);
1474 
1475 /* initialize.c */
1476 extern errcode_t ext2fs_initialize(const char *name, int flags,
1477 				   struct ext2_super_block *param,
1478 				   io_manager manager, ext2_filsys *ret_fs);
1479 
1480 /* icount.c */
1481 extern void ext2fs_free_icount(ext2_icount_t icount);
1482 extern errcode_t ext2fs_create_icount_tdb(ext2_filsys fs, char *tdb_dir,
1483 					  int flags, ext2_icount_t *ret);
1484 extern errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags,
1485 				       unsigned int size,
1486 				       ext2_icount_t hint, ext2_icount_t *ret);
1487 extern errcode_t ext2fs_create_icount(ext2_filsys fs, int flags,
1488 				      unsigned int size,
1489 				      ext2_icount_t *ret);
1490 extern errcode_t ext2fs_icount_fetch(ext2_icount_t icount, ext2_ino_t ino,
1491 				     __u16 *ret);
1492 extern errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
1493 					 __u16 *ret);
1494 extern errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
1495 					 __u16 *ret);
1496 extern errcode_t ext2fs_icount_store(ext2_icount_t icount, ext2_ino_t ino,
1497 				     __u16 count);
1498 extern ext2_ino_t ext2fs_get_icount_size(ext2_icount_t icount);
1499 errcode_t ext2fs_icount_validate(ext2_icount_t icount, FILE *);
1500 
1501 /* inline.c */
1502 
1503 extern errcode_t ext2fs_get_memalign(unsigned long size,
1504 				     unsigned long align, void *ptr);
1505 
1506 /* inline_data.c */
1507 extern errcode_t ext2fs_inline_data_init(ext2_filsys fs, ext2_ino_t ino);
1508 extern errcode_t ext2fs_inline_data_size(ext2_filsys fs, ext2_ino_t ino,
1509 					 size_t *size);
1510 extern errcode_t ext2fs_inline_data_get(ext2_filsys fs, ext2_ino_t ino,
1511 					struct ext2_inode *inode,
1512 					void *buf, size_t *size);
1513 extern errcode_t ext2fs_inline_data_set(ext2_filsys fs, ext2_ino_t ino,
1514 					struct ext2_inode *inode,
1515 					void *buf, size_t size);
1516 
1517 /* inode.c */
1518 extern errcode_t ext2fs_create_inode_cache(ext2_filsys fs,
1519 					   unsigned int cache_size);
1520 extern void ext2fs_free_inode_cache(struct ext2_inode_cache *icache);
1521 extern errcode_t ext2fs_flush_icache(ext2_filsys fs);
1522 extern errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan,
1523 					    ext2_ino_t *ino,
1524 					    struct ext2_inode *inode,
1525 					    int bufsize);
1526 #define EXT2_INODE_SCAN_DEFAULT_BUFFER_BLOCKS	8
1527 extern errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
1528 				  ext2_inode_scan *ret_scan);
1529 extern void ext2fs_close_inode_scan(ext2_inode_scan scan);
1530 extern errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino,
1531 			       struct ext2_inode *inode);
1532 extern errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
1533 						   int	group);
1534 extern void ext2fs_set_inode_callback
1535 	(ext2_inode_scan scan,
1536 	 errcode_t (*done_group)(ext2_filsys fs,
1537 				 ext2_inode_scan scan,
1538 				 dgrp_t group,
1539 				 void * priv_data),
1540 	 void *done_group_data);
1541 extern int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
1542 				   int clear_flags);
1543 extern errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino,
1544 					struct ext2_inode * inode,
1545 					int bufsize);
1546 extern errcode_t ext2fs_read_inode(ext2_filsys fs, ext2_ino_t ino,
1547 			    struct ext2_inode * inode);
1548 extern errcode_t ext2fs_read_inode2(ext2_filsys fs, ext2_ino_t ino,
1549 				    struct ext2_inode * inode,
1550 				    int bufsize, int flags);
1551 extern errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
1552 					 struct ext2_inode * inode,
1553 					 int bufsize);
1554 extern errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
1555 			    struct ext2_inode * inode);
1556 extern errcode_t ext2fs_write_inode2(ext2_filsys fs, ext2_ino_t ino,
1557 				     struct ext2_inode * inode,
1558 				     int bufsize, int flags);
1559 extern errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino,
1560 			    struct ext2_inode * inode);
1561 extern errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks);
1562 extern errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino);
1563 
1564 /* inode_io.c */
1565 extern io_manager inode_io_manager;
1566 extern errcode_t ext2fs_inode_io_intern(ext2_filsys fs, ext2_ino_t ino,
1567 					char **name);
1568 extern errcode_t ext2fs_inode_io_intern2(ext2_filsys fs, ext2_ino_t ino,
1569 					 struct ext2_inode *inode,
1570 					 char **name);
1571 
1572 /* ismounted.c */
1573 extern errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags);
1574 extern errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
1575 					  char *mtpt, int mtlen);
1576 
1577 /* punch.c */
1578 /*
1579  * NOTE: This function removes from an inode the blocks "start", "end", and
1580  * every block in between.
1581  */
1582 extern errcode_t ext2fs_punch(ext2_filsys fs, ext2_ino_t ino,
1583 			      struct ext2_inode *inode,
1584 			      char *block_buf, blk64_t start,
1585 			      blk64_t end);
1586 
1587 /* namei.c */
1588 extern errcode_t ext2fs_lookup(ext2_filsys fs, ext2_ino_t dir, const char *name,
1589 			 int namelen, char *buf, ext2_ino_t *inode);
1590 extern errcode_t ext2fs_namei(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
1591 			const char *name, ext2_ino_t *inode);
1592 errcode_t ext2fs_namei_follow(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
1593 			      const char *name, ext2_ino_t *inode);
1594 extern errcode_t ext2fs_follow_link(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
1595 			ext2_ino_t inode, ext2_ino_t *res_inode);
1596 
1597 /* native.c */
1598 int ext2fs_native_flag(void);
1599 
1600 /* newdir.c */
1601 extern errcode_t ext2fs_new_dir_block(ext2_filsys fs, ext2_ino_t dir_ino,
1602 				ext2_ino_t parent_ino, char **block);
1603 extern errcode_t ext2fs_new_dir_inline_data(ext2_filsys fs, ext2_ino_t dir_ino,
1604 				ext2_ino_t parent_ino, __u32 *iblock);
1605 
1606 /* nls_utf8.c */
1607 extern const struct ext2fs_nls_table *ext2fs_load_nls_table(int encoding);
1608 
1609 /* mkdir.c */
1610 extern errcode_t ext2fs_mkdir(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t inum,
1611 			      const char *name);
1612 
1613 /* mkjournal.c */
1614 extern errcode_t ext2fs_zero_blocks(ext2_filsys fs, blk_t blk, int num,
1615 				    blk_t *ret_blk, int *ret_count);
1616 extern errcode_t ext2fs_zero_blocks2(ext2_filsys fs, blk64_t blk, int num,
1617 				     blk64_t *ret_blk, int *ret_count);
1618 extern errcode_t ext2fs_create_journal_superblock(ext2_filsys fs,
1619 						  __u32 num_blocks, int flags,
1620 						  char  **ret_jsb);
1621 extern errcode_t ext2fs_add_journal_device(ext2_filsys fs,
1622 					   ext2_filsys journal_dev);
1623 extern errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t num_blocks,
1624 					  int flags);
1625 extern errcode_t ext2fs_add_journal_inode2(ext2_filsys fs, blk_t num_blocks,
1626 					   blk64_t goal, int flags);
1627 extern int ext2fs_default_journal_size(__u64 num_blocks);
1628 extern int ext2fs_journal_sb_start(int blocksize);
1629 
1630 /* openfs.c */
1631 extern errcode_t ext2fs_open(const char *name, int flags, int superblock,
1632 			     unsigned int block_size, io_manager manager,
1633 			     ext2_filsys *ret_fs);
1634 extern errcode_t ext2fs_open2(const char *name, const char *io_options,
1635 			      int flags, int superblock,
1636 			      unsigned int block_size, io_manager manager,
1637 			      ext2_filsys *ret_fs);
1638 /*
1639  * The dgrp_t argument to these two functions is not actually a group number
1640  * but a block number offset within a group table!  Convert with the formula
1641  * (group_number / groups_per_block).
1642  */
1643 extern blk64_t ext2fs_descriptor_block_loc2(ext2_filsys fs,
1644 					blk64_t group_block, dgrp_t i);
1645 extern blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block,
1646 					 dgrp_t i);
1647 errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io);
1648 errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io);
1649 errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io);
1650 
1651 /* get_pathname.c */
1652 extern errcode_t ext2fs_get_pathname(ext2_filsys fs, ext2_ino_t dir, ext2_ino_t ino,
1653 			       char **name);
1654 
1655 /* link.c */
1656 errcode_t ext2fs_link(ext2_filsys fs, ext2_ino_t dir, const char *name,
1657 		      ext2_ino_t ino, int flags);
1658 errcode_t ext2fs_unlink(ext2_filsys fs, ext2_ino_t dir, const char *name,
1659 			ext2_ino_t ino, int flags);
1660 
1661 /* symlink.c */
1662 errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
1663 			 const char *name, const char *target);
1664 int ext2fs_is_fast_symlink(struct ext2_inode *inode);
1665 
1666 /* mmp.c */
1667 errcode_t ext2fs_mmp_read(ext2_filsys fs, blk64_t mmp_blk, void *buf);
1668 errcode_t ext2fs_mmp_write(ext2_filsys fs, blk64_t mmp_blk, void *buf);
1669 errcode_t ext2fs_mmp_clear(ext2_filsys fs);
1670 errcode_t ext2fs_mmp_init(ext2_filsys fs);
1671 errcode_t ext2fs_mmp_start(ext2_filsys fs);
1672 errcode_t ext2fs_mmp_update(ext2_filsys fs);
1673 errcode_t ext2fs_mmp_update2(ext2_filsys fs, int immediately);
1674 errcode_t ext2fs_mmp_stop(ext2_filsys fs);
1675 unsigned ext2fs_mmp_new_seq(void);
1676 
1677 /* read_bb.c */
1678 extern errcode_t ext2fs_read_bb_inode(ext2_filsys fs,
1679 				      ext2_badblocks_list *bb_list);
1680 
1681 /* read_bb_file.c */
1682 extern errcode_t ext2fs_read_bb_FILE2(ext2_filsys fs, FILE *f,
1683 				      ext2_badblocks_list *bb_list,
1684 				      void *priv_data,
1685 				      void (*invalid)(ext2_filsys fs,
1686 						      blk_t blk,
1687 						      char *badstr,
1688 						      void *priv_data));
1689 extern errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f,
1690 				     ext2_badblocks_list *bb_list,
1691 				     void (*invalid)(ext2_filsys fs,
1692 						     blk_t blk));
1693 
1694 /* res_gdt.c */
1695 extern errcode_t ext2fs_create_resize_inode(ext2_filsys fs);
1696 
1697 /*sha256.c */
1698 #define EXT2FS_SHA256_LENGTH 32
1699 #if 0
1700 extern void ext2fs_sha256(const unsigned char *in, unsigned long in_size,
1701 		   unsigned char out[EXT2FS_SHA256_LENGTH]);
1702 #endif
1703 
1704 /* sha512.c */
1705 #define EXT2FS_SHA512_LENGTH 64
1706 extern void ext2fs_sha512(const unsigned char *in, unsigned long in_size,
1707 			  unsigned char out[EXT2FS_SHA512_LENGTH]);
1708 
1709 /* swapfs.c */
1710 extern errcode_t ext2fs_dirent_swab_in2(ext2_filsys fs, char *buf, size_t size,
1711 					int flags);
1712 extern errcode_t ext2fs_dirent_swab_in(ext2_filsys fs, char *buf, int flags);
1713 extern errcode_t ext2fs_dirent_swab_out2(ext2_filsys fs, char *buf, size_t size,
1714 					 int flags);
1715 extern errcode_t ext2fs_dirent_swab_out(ext2_filsys fs, char *buf, int flags);
1716 extern void ext2fs_swap_ext_attr(char *to, char *from, int bufsize,
1717 				 int has_header);
1718 extern void ext2fs_swap_ext_attr_header(struct ext2_ext_attr_header *to_header,
1719 					struct ext2_ext_attr_header *from_hdr);
1720 extern void ext2fs_swap_ext_attr_entry(struct ext2_ext_attr_entry *to_entry,
1721 				       struct ext2_ext_attr_entry *from_entry);
1722 extern void ext2fs_swap_super(struct ext2_super_block * super);
1723 extern void ext2fs_swap_group_desc(struct ext2_group_desc *gdp);
1724 extern void ext2fs_swap_group_desc2(ext2_filsys, struct ext2_group_desc *gdp);
1725 extern void ext2fs_swap_inode_full(ext2_filsys fs, struct ext2_inode_large *t,
1726 				   struct ext2_inode_large *f, int hostorder,
1727 				   int bufsize);
1728 extern void ext2fs_swap_inode(ext2_filsys fs,struct ext2_inode *t,
1729 			      struct ext2_inode *f, int hostorder);
1730 extern void ext2fs_swap_mmp(struct mmp_struct *mmp);
1731 
1732 /* unix_io.c */
1733 extern int ext2fs_open_file(const char *pathname, int flags, mode_t mode);
1734 extern int ext2fs_stat(const char *path, ext2fs_struct_stat *buf);
1735 extern int ext2fs_fstat(int fd, ext2fs_struct_stat *buf);
1736 
1737 /* valid_blk.c */
1738 extern int ext2fs_inode_has_valid_blocks(struct ext2_inode *inode);
1739 extern int ext2fs_inode_has_valid_blocks2(ext2_filsys fs,
1740 					  struct ext2_inode *inode);
1741 
1742 /* version.c */
1743 extern int ext2fs_parse_version_string(const char *ver_string);
1744 extern int ext2fs_get_library_version(const char **ver_string,
1745 				      const char **date_string);
1746 
1747 /* write_bb_file.c */
1748 extern errcode_t ext2fs_write_bb_FILE(ext2_badblocks_list bb_list,
1749 				      unsigned int flags,
1750 				      FILE *f);
1751 
1752 
1753 /* inline functions */
1754 #ifdef NO_INLINE_FUNCS
1755 extern errcode_t ext2fs_get_mem(unsigned long size, void *ptr);
1756 extern errcode_t ext2fs_get_memzero(unsigned long size, void *ptr);
1757 extern errcode_t ext2fs_get_array(unsigned long count,
1758 				  unsigned long size, void *ptr);
1759 extern errcode_t ext2fs_get_arrayzero(unsigned long count,
1760 				      unsigned long size, void *ptr);
1761 extern errcode_t ext2fs_free_mem(void *ptr);
1762 extern errcode_t ext2fs_resize_mem(unsigned long old_size,
1763 				   unsigned long size, void *ptr);
1764 extern void ext2fs_mark_super_dirty(ext2_filsys fs);
1765 extern void ext2fs_mark_changed(ext2_filsys fs);
1766 extern int ext2fs_test_changed(ext2_filsys fs);
1767 extern void ext2fs_mark_valid(ext2_filsys fs);
1768 extern void ext2fs_unmark_valid(ext2_filsys fs);
1769 extern int ext2fs_test_valid(ext2_filsys fs);
1770 extern void ext2fs_mark_ib_dirty(ext2_filsys fs);
1771 extern void ext2fs_mark_bb_dirty(ext2_filsys fs);
1772 extern int ext2fs_test_ib_dirty(ext2_filsys fs);
1773 extern int ext2fs_test_bb_dirty(ext2_filsys fs);
1774 extern dgrp_t ext2fs_group_of_blk(ext2_filsys fs, blk_t blk);
1775 extern dgrp_t ext2fs_group_of_ino(ext2_filsys fs, ext2_ino_t ino);
1776 extern blk_t ext2fs_group_first_block(ext2_filsys fs, dgrp_t group);
1777 extern blk_t ext2fs_group_last_block(ext2_filsys fs, dgrp_t group);
1778 extern blk_t ext2fs_inode_data_blocks(ext2_filsys fs,
1779 				      struct ext2_inode *inode);
1780 extern int ext2fs_htree_intnode_maxrecs(ext2_filsys fs, int blocks);
1781 extern unsigned int ext2fs_div_ceil(unsigned int a, unsigned int b);
1782 extern __u64 ext2fs_div64_ceil(__u64 a, __u64 b);
1783 extern int ext2fs_dirent_name_len(const struct ext2_dir_entry *entry);
1784 extern void ext2fs_dirent_set_name_len(struct ext2_dir_entry *entry, int len);
1785 extern int ext2fs_dirent_file_type(const struct ext2_dir_entry *entry);
1786 extern void ext2fs_dirent_set_file_type(struct ext2_dir_entry *entry, int type);
1787 extern struct ext2_inode *ext2fs_inode(struct ext2_inode_large * large_inode);
1788 extern const struct ext2_inode *ext2fs_const_inode(const struct ext2_inode_large * large_inode);
1789 
1790 #endif
1791 
1792 /*
1793  * The actual inlined functions definitions themselves...
1794  *
1795  * If NO_INLINE_FUNCS is defined, then we won't try to do inline
1796  * functions at all!
1797  */
1798 #if (defined(INCLUDE_INLINE_FUNCS) || !defined(NO_INLINE_FUNCS))
1799 #ifdef INCLUDE_INLINE_FUNCS
1800 #define _INLINE_ extern
1801 #else
1802 #if (__STDC_VERSION__ >= 199901L)
1803 #define _INLINE_ inline
1804 #else
1805 #ifdef __GNUC__
1806 #define _INLINE_ extern __inline__
1807 #else				/* For Watcom C */
1808 #define _INLINE_ extern inline
1809 #endif /* __GNUC__ */
1810 #endif /* __STDC_VERSION__ >= 199901L */
1811 #endif
1812 
1813 #ifndef EXT2_CUSTOM_MEMORY_ROUTINES
1814 #include <string.h>
1815 /*
1816  *  Allocate memory.  The 'ptr' arg must point to a pointer.
1817  */
ext2fs_get_mem(unsigned long size,void * ptr)1818 _INLINE_ errcode_t ext2fs_get_mem(unsigned long size, void *ptr)
1819 {
1820 	void *pp;
1821 
1822 	pp = malloc(size);
1823 	if (!pp)
1824 		return EXT2_ET_NO_MEMORY;
1825 	memcpy(ptr, &pp, sizeof (pp));
1826 	return 0;
1827 }
1828 
ext2fs_get_memzero(unsigned long size,void * ptr)1829 _INLINE_ errcode_t ext2fs_get_memzero(unsigned long size, void *ptr)
1830 {
1831 	void *pp;
1832 
1833 	pp = malloc(size);
1834 	if (!pp)
1835 		return EXT2_ET_NO_MEMORY;
1836 	memset(pp, 0, size);
1837 	memcpy(ptr, &pp, sizeof(pp));
1838 	return 0;
1839 }
1840 
ext2fs_get_array(unsigned long count,unsigned long size,void * ptr)1841 _INLINE_ errcode_t ext2fs_get_array(unsigned long count, unsigned long size, void *ptr)
1842 {
1843 	if (count && (~0UL)/count < size)
1844 		return EXT2_ET_NO_MEMORY;
1845 	return ext2fs_get_mem(count*size, ptr);
1846 }
1847 
ext2fs_get_arrayzero(unsigned long count,unsigned long size,void * ptr)1848 _INLINE_ errcode_t ext2fs_get_arrayzero(unsigned long count,
1849 					unsigned long size, void *ptr)
1850 {
1851 	void *pp;
1852 
1853 	if (count && (~0UL)/count < size)
1854 		return EXT2_ET_NO_MEMORY;
1855 	pp = calloc(count, size);
1856 	if (!pp)
1857 		return EXT2_ET_NO_MEMORY;
1858 	memcpy(ptr, &pp, sizeof(pp));
1859 	return 0;
1860 }
1861 
1862 /*
1863  * Free memory.  The 'ptr' arg must point to a pointer.
1864  */
ext2fs_free_mem(void * ptr)1865 _INLINE_ errcode_t ext2fs_free_mem(void *ptr)
1866 {
1867 	void *p;
1868 
1869 	memcpy(&p, ptr, sizeof(p));
1870 	free(p);
1871 	p = 0;
1872 	memcpy(ptr, &p, sizeof(p));
1873 	return 0;
1874 }
1875 
1876 /*
1877  *  Resize memory.  The 'ptr' arg must point to a pointer.
1878  */
ext2fs_resize_mem(unsigned long EXT2FS_ATTR ((unused))old_size,unsigned long size,void * ptr)1879 _INLINE_ errcode_t ext2fs_resize_mem(unsigned long EXT2FS_ATTR((unused)) old_size,
1880 				     unsigned long size, void *ptr)
1881 {
1882 	void *p;
1883 
1884 	/* Use "memcpy" for pointer assignments here to avoid problems
1885 	 * with C99 strict type aliasing rules. */
1886 	memcpy(&p, ptr, sizeof(p));
1887 	p = realloc(p, size);
1888 	if (!p)
1889 		return EXT2_ET_NO_MEMORY;
1890 	memcpy(ptr, &p, sizeof(p));
1891 	return 0;
1892 }
1893 #endif	/* Custom memory routines */
1894 
1895 /*
1896  * Mark a filesystem superblock as dirty
1897  */
ext2fs_mark_super_dirty(ext2_filsys fs)1898 _INLINE_ void ext2fs_mark_super_dirty(ext2_filsys fs)
1899 {
1900 	fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_CHANGED;
1901 }
1902 
1903 /*
1904  * Mark a filesystem as changed
1905  */
ext2fs_mark_changed(ext2_filsys fs)1906 _INLINE_ void ext2fs_mark_changed(ext2_filsys fs)
1907 {
1908 	fs->flags |= EXT2_FLAG_CHANGED;
1909 }
1910 
1911 /*
1912  * Check to see if a filesystem has changed
1913  */
ext2fs_test_changed(ext2_filsys fs)1914 _INLINE_ int ext2fs_test_changed(ext2_filsys fs)
1915 {
1916 	return (fs->flags & EXT2_FLAG_CHANGED);
1917 }
1918 
1919 /*
1920  * Mark a filesystem as valid
1921  */
ext2fs_mark_valid(ext2_filsys fs)1922 _INLINE_ void ext2fs_mark_valid(ext2_filsys fs)
1923 {
1924 	fs->flags |= EXT2_FLAG_VALID;
1925 }
1926 
1927 /*
1928  * Mark a filesystem as NOT valid
1929  */
ext2fs_unmark_valid(ext2_filsys fs)1930 _INLINE_ void ext2fs_unmark_valid(ext2_filsys fs)
1931 {
1932 	fs->flags &= ~EXT2_FLAG_VALID;
1933 }
1934 
1935 /*
1936  * Check to see if a filesystem is valid
1937  */
ext2fs_test_valid(ext2_filsys fs)1938 _INLINE_ int ext2fs_test_valid(ext2_filsys fs)
1939 {
1940 	return (fs->flags & EXT2_FLAG_VALID);
1941 }
1942 
1943 /*
1944  * Mark the inode bitmap as dirty
1945  */
ext2fs_mark_ib_dirty(ext2_filsys fs)1946 _INLINE_ void ext2fs_mark_ib_dirty(ext2_filsys fs)
1947 {
1948 	fs->flags |= EXT2_FLAG_IB_DIRTY | EXT2_FLAG_CHANGED;
1949 }
1950 
1951 /*
1952  * Mark the block bitmap as dirty
1953  */
ext2fs_mark_bb_dirty(ext2_filsys fs)1954 _INLINE_ void ext2fs_mark_bb_dirty(ext2_filsys fs)
1955 {
1956 	fs->flags |= EXT2_FLAG_BB_DIRTY | EXT2_FLAG_CHANGED;
1957 }
1958 
1959 /*
1960  * Check to see if a filesystem's inode bitmap is dirty
1961  */
ext2fs_test_ib_dirty(ext2_filsys fs)1962 _INLINE_ int ext2fs_test_ib_dirty(ext2_filsys fs)
1963 {
1964 	return (fs->flags & EXT2_FLAG_IB_DIRTY);
1965 }
1966 
1967 /*
1968  * Check to see if a filesystem's block bitmap is dirty
1969  */
ext2fs_test_bb_dirty(ext2_filsys fs)1970 _INLINE_ int ext2fs_test_bb_dirty(ext2_filsys fs)
1971 {
1972 	return (fs->flags & EXT2_FLAG_BB_DIRTY);
1973 }
1974 
1975 /*
1976  * Return the group # of a block
1977  */
ext2fs_group_of_blk(ext2_filsys fs,blk_t blk)1978 _INLINE_ dgrp_t ext2fs_group_of_blk(ext2_filsys fs, blk_t blk)
1979 {
1980 	return ext2fs_group_of_blk2(fs, blk);
1981 }
1982 /*
1983  * Return the group # of an inode number
1984  */
ext2fs_group_of_ino(ext2_filsys fs,ext2_ino_t ino)1985 _INLINE_ dgrp_t ext2fs_group_of_ino(ext2_filsys fs, ext2_ino_t ino)
1986 {
1987 	return (ino - 1) / fs->super->s_inodes_per_group;
1988 }
1989 
1990 /*
1991  * Return the first block (inclusive) in a group
1992  */
ext2fs_group_first_block(ext2_filsys fs,dgrp_t group)1993 _INLINE_ blk_t ext2fs_group_first_block(ext2_filsys fs, dgrp_t group)
1994 {
1995 	return (blk_t) ext2fs_group_first_block2(fs, group);
1996 }
1997 
1998 /*
1999  * Return the last block (inclusive) in a group
2000  */
ext2fs_group_last_block(ext2_filsys fs,dgrp_t group)2001 _INLINE_ blk_t ext2fs_group_last_block(ext2_filsys fs, dgrp_t group)
2002 {
2003 	return (blk_t) ext2fs_group_last_block2(fs, group);
2004 }
2005 
ext2fs_inode_data_blocks(ext2_filsys fs,struct ext2_inode * inode)2006 _INLINE_ blk_t ext2fs_inode_data_blocks(ext2_filsys fs,
2007 					struct ext2_inode *inode)
2008 {
2009 	return (blk_t) ext2fs_inode_data_blocks2(fs, inode);
2010 }
2011 
ext2fs_htree_intnode_maxrecs(ext2_filsys fs,int blocks)2012 _INLINE_ int ext2fs_htree_intnode_maxrecs(ext2_filsys fs, int blocks)
2013 {
2014 	return blocks * ((fs->blocksize - 8) / sizeof(struct ext2_dx_entry));
2015 }
2016 
2017 /*
2018  * This is an efficient, overflow safe way of calculating ceil((1.0 * a) / b)
2019  */
ext2fs_div_ceil(unsigned int a,unsigned int b)2020 _INLINE_ unsigned int ext2fs_div_ceil(unsigned int a, unsigned int b)
2021 {
2022 	if (!a)
2023 		return 0;
2024 	return ((a - 1) / b) + 1;
2025 }
2026 
ext2fs_div64_ceil(__u64 a,__u64 b)2027 _INLINE_ __u64 ext2fs_div64_ceil(__u64 a, __u64 b)
2028 {
2029 	if (!a)
2030 		return 0;
2031 	return ((a - 1) / b) + 1;
2032 }
2033 
ext2fs_dirent_name_len(const struct ext2_dir_entry * entry)2034 _INLINE_ int ext2fs_dirent_name_len(const struct ext2_dir_entry *entry)
2035 {
2036 	return entry->name_len & 0xff;
2037 }
2038 
ext2fs_dirent_set_name_len(struct ext2_dir_entry * entry,int len)2039 _INLINE_ void ext2fs_dirent_set_name_len(struct ext2_dir_entry *entry, int len)
2040 {
2041 	entry->name_len = (entry->name_len & 0xff00) | (len & 0xff);
2042 }
2043 
ext2fs_dirent_file_type(const struct ext2_dir_entry * entry)2044 _INLINE_ int ext2fs_dirent_file_type(const struct ext2_dir_entry *entry)
2045 {
2046 	return entry->name_len >> 8;
2047 }
2048 
ext2fs_dirent_set_file_type(struct ext2_dir_entry * entry,int type)2049 _INLINE_ void ext2fs_dirent_set_file_type(struct ext2_dir_entry *entry, int type)
2050 {
2051 	entry->name_len = (entry->name_len & 0xff) | (type << 8);
2052 }
2053 
ext2fs_inode(struct ext2_inode_large * large_inode)2054 _INLINE_ struct ext2_inode *ext2fs_inode(struct ext2_inode_large * large_inode)
2055 {
2056 	/* It is always safe to convert large inode to a small inode */
2057 	return (struct ext2_inode *) large_inode;
2058 }
2059 
2060 _INLINE_ const struct ext2_inode *
ext2fs_const_inode(const struct ext2_inode_large * large_inode)2061 ext2fs_const_inode(const struct ext2_inode_large * large_inode)
2062 {
2063 	/* It is always safe to convert large inode to a small inode */
2064 	return (const struct ext2_inode *) large_inode;
2065 }
2066 
2067 #undef _INLINE_
2068 #endif
2069 
2070 /* htree levels for ext4 */
2071 #define EXT4_HTREE_LEVEL_COMPAT 2
2072 #define EXT4_HTREE_LEVEL	3
2073 
ext2_dir_htree_level(ext2_filsys fs)2074 static inline unsigned int ext2_dir_htree_level(ext2_filsys fs)
2075 {
2076 	if (ext2fs_has_feature_largedir(fs->super))
2077 		return EXT4_HTREE_LEVEL;
2078 
2079 	return EXT4_HTREE_LEVEL_COMPAT;
2080 }
2081 
2082 #ifdef __cplusplus
2083 }
2084 #endif
2085 
2086 #endif /* _EXT2FS_EXT2FS_H */
2087