Whamcloud - gitweb
dirblock.c (ext2fs_read_dir_block2, ext2fs_write_dir_block): New
[tools/e2fsprogs.git] / lib / ext2fs / ext2fs.h
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 Public
8  * License.
9  * %End-Header%
10  */
11
12 #ifndef _EXT2FS_EXT2FS_H
13 #define _EXT2FS_EXT2FS_H
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 /*
20  * Non-GNU C compilers won't necessarily understand inline
21  */
22 #if (!defined(__GNUC__) && !defined(__WATCOMC__))
23 #define NO_INLINE_FUNCS
24 #endif
25
26 /*
27  * Build in support for byte-swapping filesystems if we the feature
28  * has been configured or if we're being built on a CPU architecture
29  * with a non-native byte order.
30  */
31 #if defined(ENABLE_SWAPFS) || defined(WORDS_BIGENDIAN)
32 #define EXT2FS_ENABLE_SWAPFS
33 #endif
34
35 /*
36  * Where the master copy of the superblock is located, and how big
37  * superblocks are supposed to be.  We define SUPERBLOCK_SIZE because
38  * the size of the superblock structure is not necessarily trustworthy
39  * (some versions have the padding set up so that the superblock is
40  * 1032 bytes long).
41  */
42 #define SUPERBLOCK_OFFSET       1024
43 #define SUPERBLOCK_SIZE         1024
44
45 /*
46  * The last ext2fs revision level that this version of the library is
47  * able to support.
48  */
49 #define EXT2_LIB_CURRENT_REV    EXT2_DYNAMIC_REV
50
51 #ifdef HAVE_SYS_TYPES_H
52 #include <sys/types.h>
53 #endif
54
55 #include <stdlib.h>
56
57 #if EXT2_FLAT_INCLUDES
58 #include "e2_types.h"
59 #else
60 #include <ext2fs/ext2_types.h>
61 #endif /* EXT2_FLAT_INCLUDES */
62
63 typedef __u32           ext2_ino_t;
64 typedef __u32           blk_t;
65 typedef __u32           dgrp_t;
66 typedef __u32           ext2_off_t;
67 typedef __s64           e2_blkcnt_t;
68 typedef __u32           ext2_dirhash_t;
69
70 #if EXT2_FLAT_INCLUDES
71 #include "com_err.h"
72 #include "ext2_io.h"
73 #include "ext2_err.h"
74 #else
75 #include <et/com_err.h>
76 #include <ext2fs/ext2_io.h>
77 #include <ext2fs/ext2_err.h>
78 #endif
79
80 /*
81  * Portability help for Microsoft Visual C++
82  */
83 #ifdef _MSC_VER
84 #define EXT2_QSORT_TYPE int __cdecl
85 #else
86 #define EXT2_QSORT_TYPE int
87 #endif
88
89 typedef struct struct_ext2_filsys *ext2_filsys;
90
91 struct ext2fs_struct_generic_bitmap {
92         errcode_t       magic;
93         ext2_filsys     fs;
94         __u32           start, end;
95         __u32           real_end;
96         char    *       description;
97         char    *       bitmap;
98         errcode_t       base_error_code;
99         __u32           reserved[7];
100 };
101
102 #define EXT2FS_MARK_ERROR       0
103 #define EXT2FS_UNMARK_ERROR     1
104 #define EXT2FS_TEST_ERROR       2
105
106 typedef struct ext2fs_struct_generic_bitmap *ext2fs_generic_bitmap;
107 typedef struct ext2fs_struct_generic_bitmap *ext2fs_inode_bitmap;
108 typedef struct ext2fs_struct_generic_bitmap *ext2fs_block_bitmap;
109
110 #ifdef EXT2_DYNAMIC_REV
111 #define EXT2_FIRST_INODE(s)     EXT2_FIRST_INO(s)
112 #else
113 #define EXT2_FIRST_INODE(s)     EXT2_FIRST_INO
114 #define EXT2_INODE_SIZE(s)      sizeof(struct ext2_inode)
115 #endif
116
117 /*
118  * badblocks list definitions
119  */
120
121 typedef struct ext2_struct_badblocks_list *ext2_badblocks_list;
122 typedef struct ext2_struct_badblocks_iterate *ext2_badblocks_iterate;
123
124 /* old */
125 typedef struct ext2_struct_badblocks_list *badblocks_list;
126 typedef struct ext2_struct_badblocks_iterate *badblocks_iterate;
127
128 #define BADBLOCKS_FLAG_DIRTY    1
129
130 /*
131  * ext2_dblist structure and abstractions (see dblist.c)
132  */
133 struct ext2_db_entry {
134         ext2_ino_t      ino;
135         blk_t   blk;
136         int     blockcnt;
137 };
138
139 typedef struct ext2_struct_dblist *ext2_dblist;
140
141 #define DBLIST_ABORT    1
142
143 /*
144  * ext2_fileio definitions
145  */
146
147 #define EXT2_FILE_WRITE         0x0001
148 #define EXT2_FILE_CREATE        0x0002
149
150 #define EXT2_FILE_MASK          0x00FF
151
152 #define EXT2_FILE_BUF_DIRTY     0x4000
153 #define EXT2_FILE_BUF_VALID     0x2000
154
155 typedef struct ext2_file *ext2_file_t;
156
157 #define EXT2_SEEK_SET   0
158 #define EXT2_SEEK_CUR   1
159 #define EXT2_SEEK_END   2
160
161 /*
162  * Flags for the ext2_filsys structure and for ext2fs_open()
163  */
164 #define EXT2_FLAG_RW                    0x01
165 #define EXT2_FLAG_CHANGED               0x02
166 #define EXT2_FLAG_DIRTY                 0x04
167 #define EXT2_FLAG_VALID                 0x08
168 #define EXT2_FLAG_IB_DIRTY              0x10
169 #define EXT2_FLAG_BB_DIRTY              0x20
170 #define EXT2_FLAG_SWAP_BYTES            0x40
171 #define EXT2_FLAG_SWAP_BYTES_READ       0x80
172 #define EXT2_FLAG_SWAP_BYTES_WRITE      0x100
173 #define EXT2_FLAG_MASTER_SB_ONLY        0x200
174 #define EXT2_FLAG_FORCE                 0x400
175 #define EXT2_FLAG_SUPER_ONLY            0x800
176 #define EXT2_FLAG_JOURNAL_DEV_OK        0x1000
177 #define EXT2_FLAG_IMAGE_FILE            0x2000
178
179 /*
180  * Special flag in the ext2 inode i_flag field that means that this is
181  * a new inode.  (So that ext2_write_inode() can clear extra fields.)
182  */
183 #define EXT2_NEW_INODE_FL       0x80000000
184
185 /*
186  * Flags for mkjournal
187  *
188  * EXT2_MKJOURNAL_V1_SUPER      Make a (deprecated) V1 journal superblock
189  */
190 #define EXT2_MKJOURNAL_V1_SUPER 0x0000001
191
192 struct struct_ext2_filsys {
193         errcode_t                       magic;
194         io_channel                      io;
195         int                             flags;
196         char *                          device_name;
197         struct ext2_super_block *       super;
198         int                             blocksize;
199         int                             fragsize;
200         dgrp_t                          group_desc_count;
201         unsigned long                   desc_blocks;
202         struct ext2_group_desc *        group_desc;
203         int                             inode_blocks_per_group;
204         ext2fs_inode_bitmap             inode_map;
205         ext2fs_block_bitmap             block_map;
206         errcode_t (*get_blocks)(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks);
207         errcode_t (*check_directory)(ext2_filsys fs, ext2_ino_t ino);
208         errcode_t (*write_bitmaps)(ext2_filsys fs);
209         errcode_t (*read_inode)(ext2_filsys fs, ext2_ino_t ino,
210                                 struct ext2_inode *inode);
211         errcode_t (*write_inode)(ext2_filsys fs, ext2_ino_t ino,
212                                 struct ext2_inode *inode);
213         badblocks_list                  badblocks;
214         ext2_dblist                     dblist;
215         __u32                           stride; /* for mke2fs */
216         struct ext2_super_block *       orig_super;
217         struct ext2_image_hdr *         image_header;
218         __u32                           umask;
219         /*
220          * Reserved for future expansion
221          */
222         __u32                           reserved[8];
223
224         /*
225          * Reserved for the use of the calling application.
226          */
227         void *                          priv_data;
228
229         /*
230          * Inode cache
231          */
232         struct ext2_inode_cache         *icache;
233 };
234
235 #if EXT2_FLAT_INCLUDES
236 #include "e2_bitops.h"
237 #else
238 #include <ext2fs/bitops.h>
239 #endif
240
241 /*
242  * Return flags for the block iterator functions
243  */
244 #define BLOCK_CHANGED   1
245 #define BLOCK_ABORT     2
246 #define BLOCK_ERROR     4
247
248 /*
249  * Block interate flags
250  *
251  * BLOCK_FLAG_APPEND, or BLOCK_FLAG_HOLE, indicates that the interator
252  * function should be called on blocks where the block number is zero.
253  * This is used by ext2fs_expand_dir() to be able to add a new block
254  * to an inode.  It can also be used for programs that want to be able
255  * to deal with files that contain "holes".
256  * 
257  * BLOCK_FLAG_TRAVERSE indicates that the iterator function for the
258  * indirect, doubly indirect, etc. blocks should be called after all
259  * of the blocks containined in the indirect blocks are processed.
260  * This is useful if you are going to be deallocating blocks from an
261  * inode.
262  *
263  * BLOCK_FLAG_DATA_ONLY indicates that the iterator function should be
264  * called for data blocks only.
265  *
266  * BLOCK_FLAG_NO_LARGE is for internal use only.  It informs
267  * ext2fs_block_iterate2 that large files won't be accepted.
268  */
269 #define BLOCK_FLAG_APPEND       1
270 #define BLOCK_FLAG_HOLE         1
271 #define BLOCK_FLAG_DEPTH_TRAVERSE       2
272 #define BLOCK_FLAG_DATA_ONLY    4
273
274 #define BLOCK_FLAG_NO_LARGE     0x1000
275
276 /*
277  * Magic "block count" return values for the block iterator function.
278  */
279 #define BLOCK_COUNT_IND         (-1)
280 #define BLOCK_COUNT_DIND        (-2)
281 #define BLOCK_COUNT_TIND        (-3)
282 #define BLOCK_COUNT_TRANSLATOR  (-4)
283
284 #if 0
285 /*
286  * Flags for ext2fs_move_blocks
287  */
288 #define EXT2_BMOVE_GET_DBLIST   0x0001  
289 #define EXT2_BMOVE_DEBUG        0x0002
290 #endif
291
292 /*
293  * Flags for directory block reading and writing functions
294  */
295 #define EXT2_DIRBLOCK_V2_STRUCT 0x0001
296
297 /*
298  * Return flags for the directory iterator functions
299  */
300 #define DIRENT_CHANGED  1
301 #define DIRENT_ABORT    2
302 #define DIRENT_ERROR    3
303
304 /*
305  * Directory iterator flags
306  */
307
308 #define DIRENT_FLAG_INCLUDE_EMPTY       1
309 #define DIRENT_FLAG_INCLUDE_REMOVED     2
310
311 #define DIRENT_DOT_FILE         1
312 #define DIRENT_DOT_DOT_FILE     2
313 #define DIRENT_OTHER_FILE       3
314 #define DIRENT_DELETED_FILE     4
315
316 /*
317  * Inode scan definitions
318  */
319 typedef struct ext2_struct_inode_scan *ext2_inode_scan;
320
321 /*
322  * ext2fs_scan flags
323  */
324 #define EXT2_SF_CHK_BADBLOCKS   0x0001
325 #define EXT2_SF_BAD_INODE_BLK   0x0002
326 #define EXT2_SF_BAD_EXTRA_BYTES 0x0004
327 #define EXT2_SF_SKIP_MISSING_ITABLE     0x0008
328
329 /*
330  * ext2fs_check_if_mounted flags
331  */
332 #define EXT2_MF_MOUNTED         1
333 #define EXT2_MF_ISROOT          2
334 #define EXT2_MF_READONLY        4
335 #define EXT2_MF_SWAP            8
336
337 /*
338  * Ext2/linux mode flags.  We define them here so that we don't need
339  * to depend on the OS's sys/stat.h, since we may be compiling on a
340  * non-Linux system.
341  */
342 #define LINUX_S_IFMT  00170000
343 #define LINUX_S_IFSOCK 0140000
344 #define LINUX_S_IFLNK    0120000
345 #define LINUX_S_IFREG  0100000
346 #define LINUX_S_IFBLK  0060000
347 #define LINUX_S_IFDIR  0040000
348 #define LINUX_S_IFCHR  0020000
349 #define LINUX_S_IFIFO  0010000
350 #define LINUX_S_ISUID  0004000
351 #define LINUX_S_ISGID  0002000
352 #define LINUX_S_ISVTX  0001000
353
354 #define LINUX_S_IRWXU 00700
355 #define LINUX_S_IRUSR 00400
356 #define LINUX_S_IWUSR 00200
357 #define LINUX_S_IXUSR 00100
358
359 #define LINUX_S_IRWXG 00070
360 #define LINUX_S_IRGRP 00040
361 #define LINUX_S_IWGRP 00020
362 #define LINUX_S_IXGRP 00010
363
364 #define LINUX_S_IRWXO 00007
365 #define LINUX_S_IROTH 00004
366 #define LINUX_S_IWOTH 00002
367 #define LINUX_S_IXOTH 00001
368
369 #define LINUX_S_ISLNK(m)        (((m) & LINUX_S_IFMT) == LINUX_S_IFLNK)
370 #define LINUX_S_ISREG(m)        (((m) & LINUX_S_IFMT) == LINUX_S_IFREG)
371 #define LINUX_S_ISDIR(m)        (((m) & LINUX_S_IFMT) == LINUX_S_IFDIR)
372 #define LINUX_S_ISCHR(m)        (((m) & LINUX_S_IFMT) == LINUX_S_IFCHR)
373 #define LINUX_S_ISBLK(m)        (((m) & LINUX_S_IFMT) == LINUX_S_IFBLK)
374 #define LINUX_S_ISFIFO(m)       (((m) & LINUX_S_IFMT) == LINUX_S_IFIFO)
375 #define LINUX_S_ISSOCK(m)       (((m) & LINUX_S_IFMT) == LINUX_S_IFSOCK)
376
377 /*
378  * ext2_icount_t abstraction
379  */
380 #define EXT2_ICOUNT_OPT_INCREMENT       0x01
381
382 typedef struct ext2_icount *ext2_icount_t;
383
384 /*
385  * Flags for ext2fs_bmap
386  */
387 #define BMAP_ALLOC      1
388
389 /*
390  * Flags for imager.c functions
391  */
392 #define IMAGER_FLAG_INODEMAP    1
393 #define IMAGER_FLAG_SPARSEWRITE 2
394
395 /*
396  * For checking structure magic numbers...
397  */
398
399 #define EXT2_CHECK_MAGIC(struct, code) \
400           if ((struct)->magic != (code)) return (code)
401
402
403 /*
404  * For ext2 compression support
405  */
406 #define EXT2FS_COMPRESSED_BLKADDR ((blk_t) 0xffffffff)
407 #define HOLE_BLKADDR(_b) ((_b) == 0 || (_b) == EXT2FS_COMPRESSED_BLKADDR)
408
409 /*
410  * Features supported by this version of the library
411  */
412 #define EXT2_LIB_FEATURE_COMPAT_SUPP    (EXT2_FEATURE_COMPAT_DIR_PREALLOC|\
413                                          EXT2_FEATURE_COMPAT_IMAGIC_INODES|\
414                                          EXT3_FEATURE_COMPAT_HAS_JOURNAL|\
415                                          EXT2_FEATURE_COMPAT_DIR_INDEX|\
416                                          EXT2_FEATURE_COMPAT_EXT_ATTR)
417
418 /* This #ifdef is temporary until compression is fully supported */
419 #ifdef ENABLE_COMPRESSION
420 #ifndef I_KNOW_THAT_COMPRESSION_IS_EXPERIMENTAL
421 /* If the below warning bugs you, then have
422    `CPPFLAGS=-DI_KNOW_THAT_COMPRESSION_IS_EXPERIMENTAL' in your
423    environment at configure time. */
424  #warning "Compression support is experimental"
425 #endif
426 #define EXT2_LIB_FEATURE_INCOMPAT_SUPP  (EXT2_FEATURE_INCOMPAT_FILETYPE|\
427                                          EXT2_FEATURE_INCOMPAT_COMPRESSION|\
428                                          EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|\
429                                          EXT3_FEATURE_INCOMPAT_RECOVER)
430 #else
431 #define EXT2_LIB_FEATURE_INCOMPAT_SUPP  (EXT2_FEATURE_INCOMPAT_FILETYPE|\
432                                          EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|\
433                                          EXT3_FEATURE_INCOMPAT_RECOVER)
434 #endif
435 #define EXT2_LIB_FEATURE_RO_COMPAT_SUPP (EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|\
436                                          EXT2_FEATURE_RO_COMPAT_LARGE_FILE)
437 /*
438  * function prototypes
439  */
440
441 /* alloc.c */
442 extern errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir, int mode,
443                                   ext2fs_inode_bitmap map, ext2_ino_t *ret);
444 extern errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
445                                   ext2fs_block_bitmap map, blk_t *ret);
446 extern errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start,
447                                         blk_t finish, int num,
448                                         ext2fs_block_bitmap map,
449                                         blk_t *ret);
450 extern errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
451                                     char *block_buf, blk_t *ret);
452
453 /* alloc_stats.c */
454 void ext2fs_inode_alloc_stats(ext2_filsys fs, ext2_ino_t ino, int inuse);
455 void ext2fs_inode_alloc_stats2(ext2_filsys fs, ext2_ino_t ino,
456                                int inuse, int isdir);
457 void ext2fs_block_alloc_stats(ext2_filsys fs, blk_t blk, int inuse);
458
459 /* alloc_tables.c */
460 extern errcode_t ext2fs_allocate_tables(ext2_filsys fs);
461 extern errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
462                                              ext2fs_block_bitmap bmap);
463
464 /* badblocks.c */
465 extern errcode_t ext2fs_badblocks_list_create(ext2_badblocks_list *ret,
466                                             int size);
467 extern errcode_t ext2fs_badblocks_list_add(ext2_badblocks_list bb,
468                                            blk_t blk);
469 extern int ext2fs_badblocks_list_test(ext2_badblocks_list bb,
470                                     blk_t blk);
471 extern errcode_t
472         ext2fs_badblocks_list_iterate_begin(ext2_badblocks_list bb,
473                                             ext2_badblocks_iterate *ret);
474 extern int ext2fs_badblocks_list_iterate(ext2_badblocks_iterate iter,
475                                          blk_t *blk);
476 extern void ext2fs_badblocks_list_iterate_end(ext2_badblocks_iterate iter);
477 extern errcode_t ext2fs_badblocks_copy(ext2_badblocks_list src,
478                                        ext2_badblocks_list *dest);
479 extern int ext2fs_badblocks_equal(ext2_badblocks_list bb1,
480                                   ext2_badblocks_list bb2);
481
482 /* bb_compat */
483 extern errcode_t badblocks_list_create(badblocks_list *ret, int size);
484 extern errcode_t badblocks_list_add(badblocks_list bb, blk_t blk);
485 extern int badblocks_list_test(badblocks_list bb, blk_t blk);
486 extern errcode_t badblocks_list_iterate_begin(badblocks_list bb,
487                                               badblocks_iterate *ret);
488 extern int badblocks_list_iterate(badblocks_iterate iter, blk_t *blk);
489 extern void badblocks_list_iterate_end(badblocks_iterate iter);
490 extern void badblocks_list_free(badblocks_list bb);
491
492 /* bb_inode.c */
493 extern errcode_t ext2fs_update_bb_inode(ext2_filsys fs,
494                                         ext2_badblocks_list bb_list);
495
496 /* bitmaps.c */
497 extern errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs);
498 extern errcode_t ext2fs_write_block_bitmap (ext2_filsys fs);
499 extern errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs);
500 extern errcode_t ext2fs_read_block_bitmap(ext2_filsys fs);
501 extern errcode_t ext2fs_allocate_generic_bitmap(__u32 start,
502                                                 __u32 end,
503                                                 __u32 real_end,
504                                                 const char *descr,
505                                                 ext2fs_generic_bitmap *ret);
506 extern errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
507                                               const char *descr,
508                                               ext2fs_block_bitmap *ret);
509 extern errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
510                                               const char *descr,
511                                               ext2fs_inode_bitmap *ret);
512 extern errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
513                                                ext2_ino_t end, ext2_ino_t *oend);
514 extern errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
515                                                blk_t end, blk_t *oend);
516 extern void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap);
517 extern void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap);
518 extern errcode_t ext2fs_read_bitmaps(ext2_filsys fs);
519 extern errcode_t ext2fs_write_bitmaps(ext2_filsys fs);
520
521 /* block.c */
522 extern errcode_t ext2fs_block_iterate(ext2_filsys fs,
523                                       ext2_ino_t        ino,
524                                       int       flags,
525                                       char *block_buf,
526                                       int (*func)(ext2_filsys fs,
527                                                   blk_t *blocknr,
528                                                   int   blockcnt,
529                                                   void  *priv_data),
530                                       void *priv_data);
531 errcode_t ext2fs_block_iterate2(ext2_filsys fs,
532                                 ext2_ino_t      ino,
533                                 int     flags,
534                                 char *block_buf,
535                                 int (*func)(ext2_filsys fs,
536                                             blk_t       *blocknr,
537                                             e2_blkcnt_t blockcnt,
538                                             blk_t       ref_blk,
539                                             int         ref_offset,
540                                             void        *priv_data),
541                                 void *priv_data);
542
543 /* bmap.c */
544 extern errcode_t ext2fs_bmap(ext2_filsys fs, ext2_ino_t ino,
545                              struct ext2_inode *inode, 
546                              char *block_buf, int bmap_flags,
547                              blk_t block, blk_t *phys_blk);
548
549
550 #if 0
551 /* bmove.c */
552 extern errcode_t ext2fs_move_blocks(ext2_filsys fs,
553                                     ext2fs_block_bitmap reserve,
554                                     ext2fs_block_bitmap alloc_map,
555                                     int flags);
556 #endif
557
558 /* check_desc.c */
559 extern errcode_t ext2fs_check_desc(ext2_filsys fs);
560
561 /* closefs.c */
562 extern errcode_t ext2fs_close(ext2_filsys fs);
563 extern errcode_t ext2fs_flush(ext2_filsys fs);
564 extern int ext2fs_bg_has_super(ext2_filsys fs, int group_block);
565 extern void ext2fs_update_dynamic_rev(ext2_filsys fs);
566
567 /* cmp_bitmaps.c */
568 extern errcode_t ext2fs_compare_block_bitmap(ext2fs_block_bitmap bm1,
569                                              ext2fs_block_bitmap bm2);
570 extern errcode_t ext2fs_compare_inode_bitmap(ext2fs_inode_bitmap bm1,
571                                              ext2fs_inode_bitmap bm2);
572
573 /* dblist.c */
574
575 extern errcode_t ext2fs_get_num_dirs(ext2_filsys fs, ext2_ino_t *ret_num_dirs);
576 extern errcode_t ext2fs_init_dblist(ext2_filsys fs, ext2_dblist *ret_dblist);
577 extern errcode_t ext2fs_add_dir_block(ext2_dblist dblist, ext2_ino_t ino,
578                                       blk_t blk, int blockcnt);
579 extern errcode_t ext2fs_dblist_iterate(ext2_dblist dblist,
580         int (*func)(ext2_filsys fs, struct ext2_db_entry *db_info,
581                     void        *priv_data),
582        void *priv_data);
583 extern errcode_t ext2fs_set_dir_block(ext2_dblist dblist, ext2_ino_t ino,
584                                       blk_t blk, int blockcnt);
585 extern errcode_t ext2fs_copy_dblist(ext2_dblist src,
586                                     ext2_dblist *dest);
587 extern int ext2fs_dblist_count(ext2_dblist dblist);
588
589 /* dblist_dir.c */
590 extern errcode_t
591         ext2fs_dblist_dir_iterate(ext2_dblist dblist,
592                                   int   flags,
593                                   char  *block_buf,
594                                   int (*func)(ext2_ino_t        dir,
595                                               int               entry,
596                                               struct ext2_dir_entry *dirent,
597                                               int       offset,
598                                               int       blocksize,
599                                               char      *buf,
600                                               void      *priv_data),
601                                   void *priv_data);
602
603 /* dirblock.c */
604 extern errcode_t ext2fs_read_dir_block(ext2_filsys fs, blk_t block,
605                                        void *buf);
606 extern errcode_t ext2fs_read_dir_block2(ext2_filsys fs, blk_t block,
607                                         void *buf, int flags);
608 extern errcode_t ext2fs_write_dir_block(ext2_filsys fs, blk_t block,
609                                         void *buf);
610 extern errcode_t ext2fs_write_dir_block2(ext2_filsys fs, blk_t block,
611                                          void *buf, int flags);
612
613 /* dirhash.c */
614 extern errcode_t ext2fs_dirhash(int version, const char *name, int len,
615                                 ext2_dirhash_t *ret_hash);
616
617
618 /* dir_iterate.c */
619 extern errcode_t ext2fs_dir_iterate(ext2_filsys fs, 
620                               ext2_ino_t dir,
621                               int flags,
622                               char *block_buf,
623                               int (*func)(struct ext2_dir_entry *dirent,
624                                           int   offset,
625                                           int   blocksize,
626                                           char  *buf,
627                                           void  *priv_data),
628                               void *priv_data);
629 extern errcode_t ext2fs_dir_iterate2(ext2_filsys fs, 
630                               ext2_ino_t dir,
631                               int flags,
632                               char *block_buf,
633                               int (*func)(ext2_ino_t    dir,
634                                           int   entry,
635                                           struct ext2_dir_entry *dirent,
636                                           int   offset,
637                                           int   blocksize,
638                                           char  *buf,
639                                           void  *priv_data),
640                               void *priv_data);
641
642 /* dupfs.c */
643 extern errcode_t ext2fs_dup_handle(ext2_filsys src, ext2_filsys *dest);
644
645 /* expanddir.c */
646 extern errcode_t ext2fs_expand_dir(ext2_filsys fs, ext2_ino_t dir);
647
648 /* ext_attr.c */
649 void ext2fs_swap_ext_attr(ext2_filsys fs, char *to, char *from);
650 extern errcode_t ext2fs_read_ext_attr(ext2_filsys fs, blk_t block, void *buf);
651 extern errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block, void *buf); 
652 /* fileio.c */
653 extern errcode_t ext2fs_file_open(ext2_filsys fs, ext2_ino_t ino,
654                                   int flags, ext2_file_t *ret);
655 extern ext2_filsys ext2fs_file_get_fs(ext2_file_t file);
656 extern errcode_t ext2fs_file_close(ext2_file_t file);
657 extern errcode_t ext2fs_file_flush(ext2_file_t file);
658 extern errcode_t ext2fs_file_read(ext2_file_t file, void *buf,
659                                   unsigned int wanted, unsigned int *got);
660 extern errcode_t ext2fs_file_write(ext2_file_t file, const void *buf,
661                                    unsigned int nbytes, unsigned int *written);
662 extern errcode_t ext2fs_file_lseek(ext2_file_t file, ext2_off_t offset,
663                                    int whence, ext2_off_t *ret_pos);
664 extern ext2_off_t ext2fs_file_get_size(ext2_file_t file);
665 extern errcode_t ext2fs_file_set_size(ext2_file_t file, ext2_off_t size);
666
667 /* finddev.c */
668 extern char *ext2fs_find_block_device(dev_t device);
669
670 /* flushb.c */
671 extern errcode_t ext2fs_sync_device(int fd, int flushb);
672
673 /* freefs.c */
674 extern void ext2fs_free(ext2_filsys fs);
675 extern void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap);
676 extern void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap);
677 extern void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap);
678 extern void ext2fs_free_dblist(ext2_dblist dblist);
679 extern void ext2fs_badblocks_list_free(badblocks_list bb);
680
681 /* getsize.c */
682 extern errcode_t ext2fs_get_device_size(const char *file, int blocksize,
683                                         blk_t *retblocks);
684
685 /* imager.c */
686 extern errcode_t ext2fs_image_inode_write(ext2_filsys fs, int fd, int flags);
687 extern errcode_t ext2fs_image_inode_read(ext2_filsys fs, int fd, int flags);
688 extern errcode_t ext2fs_image_super_write(ext2_filsys fs, int fd, int flags);
689 extern errcode_t ext2fs_image_super_read(ext2_filsys fs, int fd, int flags);
690 extern errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags);
691 extern errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags);
692
693 /* initialize.c */
694 extern errcode_t ext2fs_initialize(const char *name, int flags,
695                                    struct ext2_super_block *param,
696                                    io_manager manager, ext2_filsys *ret_fs);
697
698 /* icount.c */
699 extern void ext2fs_free_icount(ext2_icount_t icount);
700 extern errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags, int size,
701                                        ext2_icount_t hint, ext2_icount_t *ret);
702 extern errcode_t ext2fs_create_icount(ext2_filsys fs, int flags, int size,
703                                       ext2_icount_t *ret);
704 extern errcode_t ext2fs_icount_fetch(ext2_icount_t icount, ext2_ino_t ino,
705                                      __u16 *ret);
706 extern errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
707                                          __u16 *ret);
708 extern errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
709                                          __u16 *ret);
710 extern errcode_t ext2fs_icount_store(ext2_icount_t icount, ext2_ino_t ino,
711                                      __u16 count);
712 extern ext2_ino_t ext2fs_get_icount_size(ext2_icount_t icount);
713 errcode_t ext2fs_icount_validate(ext2_icount_t icount, FILE *);
714
715 /* inode.c */
716 extern errcode_t ext2fs_flush_icache(ext2_filsys fs);
717 extern errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
718                                   ext2_inode_scan *ret_scan);
719 extern void ext2fs_close_inode_scan(ext2_inode_scan scan);
720 extern errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino,
721                                struct ext2_inode *inode);
722 extern errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
723                                                    int  group);
724 extern void ext2fs_set_inode_callback
725         (ext2_inode_scan scan,
726          errcode_t (*done_group)(ext2_filsys fs,
727                                  ext2_inode_scan scan,
728                                  dgrp_t group,
729                                  void * priv_data),
730          void *done_group_data);
731 extern int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
732                                    int clear_flags);
733 extern errcode_t ext2fs_read_inode (ext2_filsys fs, ext2_ino_t ino,
734                             struct ext2_inode * inode);
735 extern errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
736                             struct ext2_inode * inode);
737 extern errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks);
738 extern errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino);
739
740 /* inode_io.c */
741 extern io_manager inode_io_manager;
742 extern errcode_t ext2fs_inode_io_intern(ext2_filsys fs, ext2_ino_t ino, 
743                                         char **name);
744         
745 /* ismounted.c */
746 extern errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags);
747 extern errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
748                                           char *mtpt, int mtlen);
749
750 /* namei.c */
751 extern errcode_t ext2fs_lookup(ext2_filsys fs, ext2_ino_t dir, const char *name,
752                          int namelen, char *buf, ext2_ino_t *inode);
753 extern errcode_t ext2fs_namei(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
754                         const char *name, ext2_ino_t *inode);
755 errcode_t ext2fs_namei_follow(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
756                               const char *name, ext2_ino_t *inode);
757 extern errcode_t ext2fs_follow_link(ext2_filsys fs, ext2_ino_t root, ext2_ino_t cwd,
758                         ext2_ino_t inode, ext2_ino_t *res_inode);
759
760 /* native.c */
761 int ext2fs_native_flag(void);
762
763 /* newdir.c */
764 extern errcode_t ext2fs_new_dir_block(ext2_filsys fs, ext2_ino_t dir_ino,
765                                 ext2_ino_t parent_ino, char **block);
766
767 /* mkdir.c */
768 extern errcode_t ext2fs_mkdir(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t inum,
769                               const char *name);
770
771 /* mkjournal.c */
772 extern errcode_t ext2fs_create_journal_superblock(ext2_filsys fs,
773                                                   __u32 size, int flags,
774                                                   char  **ret_jsb);
775 extern errcode_t ext2fs_add_journal_device(ext2_filsys fs,
776                                            ext2_filsys journal_dev);
777 extern errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t size,
778                                           int flags);
779
780 /* openfs.c */
781 extern errcode_t ext2fs_open(const char *name, int flags, int superblock,
782                              int block_size, io_manager manager,
783                              ext2_filsys *ret_fs);
784
785 /* get_pathname.c */
786 extern errcode_t ext2fs_get_pathname(ext2_filsys fs, ext2_ino_t dir, ext2_ino_t ino,
787                                char **name);
788
789 /* link.c */
790 errcode_t ext2fs_link(ext2_filsys fs, ext2_ino_t dir, const char *name,
791                       ext2_ino_t ino, int flags);
792 errcode_t ext2fs_unlink(ext2_filsys fs, ext2_ino_t dir, const char *name,
793                         ext2_ino_t ino, int flags);
794
795 /* read_bb.c */
796 extern errcode_t ext2fs_read_bb_inode(ext2_filsys fs,
797                                       ext2_badblocks_list *bb_list);
798
799 /* read_bb_file.c */
800 extern errcode_t ext2fs_read_bb_FILE2(ext2_filsys fs, FILE *f, 
801                                       ext2_badblocks_list *bb_list,
802                                       void *private,
803                                       void (*invalid)(ext2_filsys fs,
804                                                       blk_t blk,
805                                                       char *badstr,
806                                                       void *private));
807 extern errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f, 
808                                      ext2_badblocks_list *bb_list,
809                                      void (*invalid)(ext2_filsys fs,
810                                                      blk_t blk));
811
812 /* rs_bitmap.c */
813 extern errcode_t ext2fs_resize_generic_bitmap(__u32 new_end,
814                                               __u32 new_real_end,
815                                               ext2fs_generic_bitmap bmap);
816 extern errcode_t ext2fs_resize_inode_bitmap(__u32 new_end, __u32 new_real_end,
817                                             ext2fs_inode_bitmap bmap);
818 extern errcode_t ext2fs_resize_block_bitmap(__u32 new_end, __u32 new_real_end,
819                                             ext2fs_block_bitmap bmap);
820 extern errcode_t ext2fs_copy_bitmap(ext2fs_generic_bitmap src,
821                                     ext2fs_generic_bitmap *dest);
822
823 /* swapfs.c */
824 extern void ext2fs_swap_super(struct ext2_super_block * super);
825 extern void ext2fs_swap_group_desc(struct ext2_group_desc *gdp);
826 extern void ext2fs_swap_inode(ext2_filsys fs,struct ext2_inode *t,
827                               struct ext2_inode *f, int hostorder);
828
829 /* valid_blk.c */
830 extern int ext2fs_inode_has_valid_blocks(struct ext2_inode *inode);
831
832 /* version.c */
833 extern int ext2fs_parse_version_string(const char *ver_string);
834 extern int ext2fs_get_library_version(const char **ver_string,
835                                       const char **date_string);
836
837 /* write_bb_file.c */
838 extern errcode_t ext2fs_write_bb_FILE(ext2_badblocks_list bb_list,
839                                       unsigned int flags,
840                                       FILE *f);
841
842
843 /* inline functions */
844 extern errcode_t ext2fs_get_mem(unsigned long size, void **ptr);
845 extern errcode_t ext2fs_free_mem(void **ptr);
846 extern errcode_t ext2fs_resize_mem(unsigned long old_size,
847                                    unsigned long size, void **ptr);
848 extern void ext2fs_mark_super_dirty(ext2_filsys fs);
849 extern void ext2fs_mark_changed(ext2_filsys fs);
850 extern int ext2fs_test_changed(ext2_filsys fs);
851 extern void ext2fs_mark_valid(ext2_filsys fs);
852 extern void ext2fs_unmark_valid(ext2_filsys fs);
853 extern int ext2fs_test_valid(ext2_filsys fs);
854 extern void ext2fs_mark_ib_dirty(ext2_filsys fs);
855 extern void ext2fs_mark_bb_dirty(ext2_filsys fs);
856 extern int ext2fs_test_ib_dirty(ext2_filsys fs);
857 extern int ext2fs_test_bb_dirty(ext2_filsys fs);
858 extern int ext2fs_group_of_blk(ext2_filsys fs, blk_t blk);
859 extern int ext2fs_group_of_ino(ext2_filsys fs, ext2_ino_t ino);
860
861 /*
862  * The actual inlined functions definitions themselves...
863  *
864  * If NO_INLINE_FUNCS is defined, then we won't try to do inline
865  * functions at all!
866  */
867 #if (defined(INCLUDE_INLINE_FUNCS) || !defined(NO_INLINE_FUNCS))
868 #ifdef INCLUDE_INLINE_FUNCS
869 #define _INLINE_ extern
870 #else
871 #ifdef __GNUC__
872 #define _INLINE_ extern __inline__
873 #else                           /* For Watcom C */
874 #define _INLINE_ extern inline
875 #endif
876 #endif
877
878 #ifndef EXT2_CUSTOM_MEMORY_ROUTINES
879 /*
880  *  Allocate memory
881  */
882 _INLINE_ errcode_t ext2fs_get_mem(unsigned long size, void **ptr)
883 {
884         *ptr = malloc(size);
885         if (!*ptr)
886                 return EXT2_ET_NO_MEMORY;
887         return 0;
888 }
889
890 /*
891  * Free memory
892  */
893 _INLINE_ errcode_t ext2fs_free_mem(void **ptr)
894 {
895         free(*ptr);
896         *ptr = 0;
897         return 0;
898 }
899         
900 /*
901  *  Resize memory
902  */
903 _INLINE_ errcode_t ext2fs_resize_mem(unsigned long old_size,
904                                      unsigned long size, void **ptr)
905 {
906         void *p;
907
908         p = realloc(*ptr, size);
909         if (!p)
910                 return EXT2_ET_NO_MEMORY;
911         *ptr = p;
912         return 0;
913 }
914 #endif  /* Custom memory routines */
915
916 /*
917  * Mark a filesystem superblock as dirty
918  */
919 _INLINE_ void ext2fs_mark_super_dirty(ext2_filsys fs)
920 {
921         fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_CHANGED;
922 }
923
924 /*
925  * Mark a filesystem as changed
926  */
927 _INLINE_ void ext2fs_mark_changed(ext2_filsys fs)
928 {
929         fs->flags |= EXT2_FLAG_CHANGED;
930 }
931
932 /*
933  * Check to see if a filesystem has changed
934  */
935 _INLINE_ int ext2fs_test_changed(ext2_filsys fs)
936 {
937         return (fs->flags & EXT2_FLAG_CHANGED);
938 }
939
940 /*
941  * Mark a filesystem as valid
942  */
943 _INLINE_ void ext2fs_mark_valid(ext2_filsys fs)
944 {
945         fs->flags |= EXT2_FLAG_VALID;
946 }
947
948 /*
949  * Mark a filesystem as NOT valid
950  */
951 _INLINE_ void ext2fs_unmark_valid(ext2_filsys fs)
952 {
953         fs->flags &= ~EXT2_FLAG_VALID;
954 }
955
956 /*
957  * Check to see if a filesystem is valid
958  */
959 _INLINE_ int ext2fs_test_valid(ext2_filsys fs)
960 {
961         return (fs->flags & EXT2_FLAG_VALID);
962 }
963
964 /*
965  * Mark the inode bitmap as dirty
966  */
967 _INLINE_ void ext2fs_mark_ib_dirty(ext2_filsys fs)
968 {
969         fs->flags |= EXT2_FLAG_IB_DIRTY | EXT2_FLAG_CHANGED;
970 }
971
972 /*
973  * Mark the block bitmap as dirty
974  */
975 _INLINE_ void ext2fs_mark_bb_dirty(ext2_filsys fs)
976 {
977         fs->flags |= EXT2_FLAG_BB_DIRTY | EXT2_FLAG_CHANGED;
978 }
979
980 /*
981  * Check to see if a filesystem's inode bitmap is dirty
982  */
983 _INLINE_ int ext2fs_test_ib_dirty(ext2_filsys fs)
984 {
985         return (fs->flags & EXT2_FLAG_IB_DIRTY);
986 }
987
988 /*
989  * Check to see if a filesystem's block bitmap is dirty
990  */
991 _INLINE_ int ext2fs_test_bb_dirty(ext2_filsys fs)
992 {
993         return (fs->flags & EXT2_FLAG_BB_DIRTY);
994 }
995
996 /*
997  * Return the group # of a block
998  */
999 _INLINE_ int ext2fs_group_of_blk(ext2_filsys fs, blk_t blk)
1000 {
1001         return (blk - fs->super->s_first_data_block) /
1002                 fs->super->s_blocks_per_group;
1003 }
1004
1005 /*
1006  * Return the group # of an inode number
1007  */
1008 _INLINE_ int ext2fs_group_of_ino(ext2_filsys fs, ext2_ino_t ino)
1009 {
1010         return (ino - 1) / fs->super->s_inodes_per_group;
1011 }
1012 #undef _INLINE_
1013 #endif
1014
1015 #ifdef __cplusplus
1016 }
1017 #endif
1018
1019 #endif /* _EXT2FS_EXT2FS_H */