home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / win95 / ext2tool.exe / EXT2FS / EXT2FS.H < prev    next >
C/C++ Source or Header  |  1995-05-10  |  12KB  |  439 lines

  1. /*
  2.  * ext2fs.h --- ext2fs
  3.  * 
  4.  * Copyright (C) 1993 Theodore Ts'o.  This file may be redistributed
  5.  * under the terms of the GNU Public License.
  6.  */
  7.  
  8. /*
  9.  * Where the master copy of the superblock is located, and how big
  10.  * superblocks are supposed to be.  We define SUPERBLOCK_SIZE because
  11.  * the size of the superblock structure is not necessarily trustworthy
  12.  * (some versions have the padding set up so that the superblock is
  13.  * 1032 bytes long).
  14.  */
  15. #define SUPERBLOCK_OFFSET    1024
  16. #define SUPERBLOCK_SIZE     1024
  17.  
  18. /*
  19.  * The last ext2fs revision level that this version of the library is
  20.  * able to support.
  21.  */
  22. #define EXT2_LIB_CURRENT_REV    0
  23.  
  24. typedef unsigned long    blk_t;
  25. typedef unsigned int    dgrp_t;
  26.  
  27. #include "com_err.h"
  28. #include "ext2fs/io.h"
  29. #include "ext2fs/ext2_err.h"
  30.  
  31. typedef struct struct_ext2_filsys *ext2_filsys;
  32.  
  33. struct ext2fs_struct_inode_bitmap {
  34.     int    magic;
  35.     ext2_filsys fs;
  36.     ino_t    start, end;
  37.     ino_t    real_end;
  38.     char    *description;
  39.     char    *bitmap;
  40.     int    reserved[8];
  41. };
  42.  
  43. typedef struct ext2fs_struct_inode_bitmap *ext2fs_inode_bitmap;
  44.  
  45. struct ext2fs_struct_block_bitmap {
  46.     int    magic;
  47.     ext2_filsys fs;
  48.     blk_t    start, end;
  49.     ino_t    real_end;
  50.     char    *description;
  51.     char    *bitmap;
  52.     int    reserved[8];
  53. };
  54.  
  55. typedef struct ext2fs_struct_block_bitmap *ext2fs_block_bitmap;
  56.  
  57. /*
  58.  * Flags for the ext2_filsys structure
  59.  */
  60.  
  61. #define EXT2_FLAG_RW        0x01
  62. #define EXT2_FLAG_CHANGED    0x02
  63. #define EXT2_FLAG_DIRTY        0x04
  64. #define EXT2_FLAG_VALID        0x08
  65. #define EXT2_FLAG_IB_DIRTY    0x10
  66. #define EXT2_FLAG_BB_DIRTY    0x20
  67.  
  68. struct struct_ext2_filsys {
  69.     int                magic;
  70.     io_channel            io;
  71.     int                flags;
  72.     char *                device_name;
  73.     struct ext2_super_block    *     super;
  74.     int                blocksize;
  75.     int                fragsize;
  76.     unsigned long            group_desc_count;
  77.     unsigned long            desc_blocks;
  78.     struct ext2_group_desc *    group_desc;
  79.     int                inode_blocks_per_group;
  80.     ext2fs_inode_bitmap        inode_map;
  81.     ext2fs_block_bitmap        block_map;
  82.     errcode_t (*get_blocks)(ext2_filsys fs, ino_t ino, blk_t *blocks);
  83.     errcode_t (*check_directory)(ext2_filsys fs, ino_t ino);
  84.     errcode_t (*write_bitmaps)(ext2_filsys fs);
  85.     int                reserved[16];
  86.  
  87.     /*
  88.      * Not used by ext2fs library; reserved for the use of the
  89.      * calling application.
  90.      */
  91.     void *                private; 
  92. };
  93.  
  94. /*
  95.  * badblocks list definitions
  96.  */
  97.  
  98. typedef struct struct_badblocks_list *badblocks_list;
  99.  
  100. struct struct_badblocks_list {
  101.     int    magic;
  102.     int    num;
  103.     int    size;
  104.     blk_t    *list;
  105.     int    badblocks_flags;
  106.     int    reserved[8];
  107. };
  108.  
  109. #define BADBLOCKS_FLAG_DIRTY    1
  110.  
  111. typedef struct struct_badblocks_iterate *badblocks_iterate;
  112.  
  113. struct struct_badblocks_iterate {
  114.     int        magic;
  115.     badblocks_list    bb;
  116.     int        ptr;
  117.     int    reserved[8];
  118. };
  119.  
  120. #include "ext2fs/bitops.h"
  121.     
  122. /*
  123.  * Return flags for the block iterator functions
  124.  */
  125. #define BLOCK_CHANGED    1
  126. #define BLOCK_ABORT    2
  127. #define BLOCK_ERROR    4
  128.  
  129. /*
  130.  * Block interate flags
  131.  */
  132. #define BLOCK_FLAG_APPEND    1
  133. #define BLOCK_FLAG_DEPTH_TRAVERSE    2
  134.  
  135. /*
  136.  * Return flags for the directory iterator functions
  137.  */
  138. #define DIRENT_CHANGED    1
  139. #define DIRENT_ABORT    2
  140. #define DIRENT_ERROR    3
  141.  
  142. /*
  143.  * Directory iterator flags
  144.  */
  145.  
  146. #define DIRENT_FLAG_INCLUDE_EMPTY    1
  147.  
  148. /*
  149.  * Inode scan definitions
  150.  */
  151. struct ext2_struct_inode_scan {
  152.     int            magic;
  153.     ext2_filsys        fs;
  154.     ino_t            current_inode;
  155.     blk_t            current_block;
  156.     dgrp_t            current_group;
  157.     int            inodes_left, blocks_left, groups_left;
  158.     int            inode_buffer_blocks;
  159.     char *            inode_buffer;
  160.     struct ext2_inode *    inode_scan_ptr;
  161.     int            reserved[8];
  162. };
  163.  
  164. typedef struct ext2_struct_inode_scan *ext2_inode_scan;
  165.  
  166. /*
  167.  * For checking structure magic numbers...
  168.  */
  169.  
  170. #define EXT2_CHECK_MAGIC(struct, code) \
  171.       if ((struct)->magic != (code)) return (code)
  172.   
  173. /*
  174.  * function prototypes
  175.  */
  176.  
  177. /* alloc.c */
  178. extern errcode_t ext2fs_new_inode(ext2_filsys fs, ino_t dir, int mode,
  179.                   ext2fs_inode_bitmap map, ino_t *ret);
  180. extern errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
  181.                   ext2fs_block_bitmap map, blk_t *ret);
  182. extern errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start,
  183.                     blk_t finish, int num,
  184.                     ext2fs_block_bitmap map,
  185.                     blk_t *ret);
  186.  
  187. /* badblocks.c */
  188. extern errcode_t badblocks_list_create(badblocks_list *ret, int size);
  189. extern void badblocks_list_free(badblocks_list bb);
  190. extern errcode_t badblocks_list_add(badblocks_list bb, blk_t blk);
  191. extern int badblocks_list_test(badblocks_list bb, blk_t blk);
  192. extern errcode_t badblocks_list_iterate_begin(badblocks_list bb,
  193.                           badblocks_iterate *ret);
  194. extern int badblocks_list_iterate(badblocks_iterate iter, blk_t *blk);
  195. extern void badblocks_list_iterate_end(badblocks_iterate iter);
  196.  
  197. /* bb_inode.c */
  198. extern errcode_t ext2fs_update_bb_inode(ext2_filsys fs,
  199.                     badblocks_list bb_list);
  200.  
  201. /* bitmaps.c */
  202. extern errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs);
  203. extern errcode_t ext2fs_write_block_bitmap (ext2_filsys fs);
  204. extern errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs);
  205. extern errcode_t ext2fs_read_block_bitmap(ext2_filsys fs);
  206. errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
  207.                        const char *descr,
  208.                        ext2fs_block_bitmap *ret);
  209. errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
  210.                        const char *descr,
  211.                        ext2fs_inode_bitmap *ret);
  212. errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
  213.                     ino_t end, ino_t *oend);
  214. errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
  215.                     blk_t end, blk_t *oend);
  216. void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap);
  217. void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap);
  218. void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap);
  219. void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap);
  220. extern errcode_t ext2fs_read_bitmaps(ext2_filsys fs);
  221. extern errcode_t ext2fs_write_bitmaps(ext2_filsys fs);
  222.  
  223. /* block.c */
  224. extern errcode_t ext2fs_block_iterate(ext2_filsys fs,
  225.                       ino_t    ino,
  226.                       int    flags,
  227.                       char *block_buf,
  228.                       int (*func)(ext2_filsys fs,
  229.                           blk_t    *blocknr,
  230.                           int    blockcnt,
  231.                           void    *private),
  232.                       void *private);
  233.  
  234. /* check_desc.c */
  235. extern errcode_t ext2fs_check_desc(ext2_filsys fs);
  236.  
  237. /* closefs.c */
  238. extern errcode_t ext2fs_close(ext2_filsys fs);
  239. extern errcode_t ext2fs_flush(ext2_filsys fs);
  240.  
  241. /* expanddir.c */
  242. extern errcode_t ext2fs_expand_dir(ext2_filsys fs, ino_t dir);
  243.  
  244. /* freefs.c */
  245. extern void ext2fs_free(ext2_filsys fs);
  246.  
  247. /* initialize.c */
  248. extern errcode_t ext2fs_initialize(const char *name, int flags,
  249.                    struct ext2_super_block *param,
  250.                    io_manager manager, ext2_filsys *ret_fs);
  251.  
  252. /* inode.c */
  253. extern errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
  254.                   ext2_inode_scan *ret_scan);
  255. extern void ext2fs_close_inode_scan(ext2_inode_scan scan);
  256. extern errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ino_t *ino,
  257.                    struct ext2_inode *inode);
  258. extern errcode_t ext2fs_read_inode (ext2_filsys fs, unsigned long ino,
  259.                 struct ext2_inode * inode);
  260. extern errcode_t ext2fs_write_inode(ext2_filsys fs, unsigned long ino,
  261.                 struct ext2_inode * inode);
  262. extern errcode_t ext2fs_get_blocks(ext2_filsys fs, ino_t ino, blk_t *blocks);
  263. extern errcode_t ext2fs_check_directory(ext2_filsys fs, ino_t ino);
  264.  
  265. /* namei.c */
  266. extern errcode_t ext2fs_dir_iterate(ext2_filsys fs, 
  267.                   ino_t dir,
  268.                   int flags,
  269.                   char *block_buf,
  270.                   int (*func)(struct ext2_dir_entry *dirent,
  271.                       int    offset,
  272.                       int    blocksize,
  273.                       char    *buf,
  274.                       void    *private),
  275.                   void *private);
  276. extern errcode_t ext2fs_lookup(ext2_filsys fs, ino_t dir, const char *name,
  277.              int namelen, char *buf, ino_t *inode);
  278. extern errcode_t ext2fs_namei(ext2_filsys fs, ino_t root, ino_t cwd,
  279.             const char *name, ino_t *inode);
  280.  
  281. /* newdir.c */
  282. extern errcode_t ext2fs_new_dir_block(ext2_filsys fs, ino_t dir_ino,
  283.                 ino_t parent_ino, char **block);
  284.  
  285. /* mkdir.c */
  286. extern errcode_t ext2fs_mkdir(ext2_filsys fs, ino_t parent, ino_t inum,
  287.                   const char *name);
  288.  
  289. /* openfs.c */
  290. extern errcode_t ext2fs_open(const char *name, int flags, int superblock,
  291.                  int block_size, io_manager manager,
  292.                  ext2_filsys *ret_fs);
  293.  
  294. /* get_pathname.c */
  295. extern errcode_t ext2fs_get_pathname(ext2_filsys fs, ino_t dir, ino_t ino,
  296.                    char **name);
  297.  
  298. /* link.c */
  299. errcode_t ext2fs_link(ext2_filsys fs, ino_t dir, const char *name,
  300.               ino_t ino, int flags);
  301. errcode_t ext2fs_unlink(ext2_filsys fs, ino_t dir, const char *name,
  302.             ino_t ino, int flags);
  303.  
  304. /* read_bb.c */
  305. extern errcode_t ext2fs_read_bb_inode(ext2_filsys fs, badblocks_list *bb_list);
  306.  
  307. /* read_bb_file.c */
  308. extern errcode_t ext2fs_read_bb_FILE(ext2_filsys fs, FILE *f, 
  309.                      badblocks_list *bb_list,
  310.                      void (*invalid)(ext2_filsys fs,
  311.                              blk_t blk));
  312.  
  313. /* inline functions */
  314. extern void ext2fs_mark_super_dirty(ext2_filsys fs);
  315. extern void ext2fs_mark_changed(ext2_filsys fs);
  316. extern int ext2fs_test_changed(ext2_filsys fs);
  317. extern void ext2fs_mark_valid(ext2_filsys fs);
  318. extern void ext2fs_unmark_valid(ext2_filsys fs);
  319. extern int ext2fs_test_valid(ext2_filsys fs);
  320. extern void ext2fs_mark_ib_dirty(ext2_filsys fs);
  321. extern void ext2fs_mark_bb_dirty(ext2_filsys fs);
  322. extern int ext2fs_test_ib_dirty(ext2_filsys fs);
  323. extern int ext2fs_test_bb_dirty(ext2_filsys fs);
  324. extern int ext2fs_group_of_blk(ext2_filsys fs, blk_t blk);
  325. extern int ext2fs_group_of_ino(ext2_filsys fs, ino_t ino);
  326.  
  327. /*
  328.  * The actual inlined functions definitions themselves...
  329.  *
  330.  * If NO_INLINE_FUNCS is defined, then we won't try to do inline
  331.  * functions at all!
  332.  */
  333. #if (defined(INCLUDE_INLINE_FUNCS) || !defined(NO_INLINE_FUNCS))
  334. #ifdef INCLUDE_INLINE_FUNCS
  335. #define _INLINE_ extern
  336. #else
  337. #define _INLINE_ extern __inline__
  338. #endif
  339.  
  340. /*
  341.  * Mark a filesystem superblock as dirty
  342.  */
  343. _INLINE_ void ext2fs_mark_super_dirty(ext2_filsys fs)
  344. {
  345.     fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_CHANGED;
  346. }
  347.  
  348. /*
  349.  * Mark a filesystem as changed
  350.  */
  351. _INLINE_ void ext2fs_mark_changed(ext2_filsys fs)
  352. {
  353.     fs->flags |= EXT2_FLAG_CHANGED;
  354. }
  355.  
  356. /*
  357.  * Check to see if a filesystem has changed
  358.  */
  359. _INLINE_ int ext2fs_test_changed(ext2_filsys fs)
  360. {
  361.     return (fs->flags & EXT2_FLAG_CHANGED);
  362. }
  363.  
  364. /*
  365.  * Mark a filesystem as valid
  366.  */
  367. _INLINE_ void ext2fs_mark_valid(ext2_filsys fs)
  368. {
  369.     fs->flags |= EXT2_FLAG_VALID;
  370. }
  371.  
  372. /*
  373.  * Mark a filesystem as NOT valid
  374.  */
  375. _INLINE_ void ext2fs_unmark_valid(ext2_filsys fs)
  376. {
  377.     fs->flags &= ~EXT2_FLAG_VALID;
  378. }
  379.  
  380. /*
  381.  * Check to see if a filesystem is valid
  382.  */
  383. _INLINE_ int ext2fs_test_valid(ext2_filsys fs)
  384. {
  385.     return (fs->flags & EXT2_FLAG_VALID);
  386. }
  387.  
  388. /*
  389.  * Mark the inode bitmap as dirty
  390.  */
  391. _INLINE_ void ext2fs_mark_ib_dirty(ext2_filsys fs)
  392. {
  393.     fs->flags |= EXT2_FLAG_IB_DIRTY | EXT2_FLAG_CHANGED;
  394. }
  395.  
  396. /*
  397.  * Mark the block bitmap as dirty
  398.  */
  399. _INLINE_ void ext2fs_mark_bb_dirty(ext2_filsys fs)
  400. {
  401.     fs->flags |= EXT2_FLAG_BB_DIRTY | EXT2_FLAG_CHANGED;
  402. }
  403.  
  404. /*
  405.  * Check to see if a filesystem's inode bitmap is dirty
  406.  */
  407. _INLINE_ int ext2fs_test_ib_dirty(ext2_filsys fs)
  408. {
  409.     return (fs->flags & EXT2_FLAG_IB_DIRTY);
  410. }
  411.  
  412. /*
  413.  * Check to see if a filesystem's block bitmap is dirty
  414.  */
  415. _INLINE_ int ext2fs_test_bb_dirty(ext2_filsys fs)
  416. {
  417.     return (fs->flags & EXT2_FLAG_BB_DIRTY);
  418. }
  419.  
  420. /*
  421.  * Return the group # of a block
  422.  */
  423. _INLINE_ int ext2fs_group_of_blk(ext2_filsys fs, blk_t blk)
  424. {
  425.     return (blk - fs->super->s_first_data_block) /
  426.         fs->super->s_blocks_per_group;
  427. }
  428.  
  429. /*
  430.  * Return the group # of an inode number
  431.  */
  432. _INLINE_ int ext2fs_group_of_ino(ext2_filsys fs, ino_t ino)
  433. {
  434.     return (ino - 1) / fs->super->s_inodes_per_group;
  435. }
  436. #undef _INLINE_
  437. #endif
  438.  
  439.