aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

ext2fs.h (27472B)


      1 /*	$NetBSD: ext2fs.h,v 1.53 2025/05/04 20:26:56 rillig Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)fs.h	8.10 (Berkeley) 10/27/94
     32  *  Modified for ext2fs by Manuel Bouyer.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1997 Manuel Bouyer.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  *
     47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57  *
     58  *	@(#)fs.h	8.10 (Berkeley) 10/27/94
     59  *  Modified for ext2fs by Manuel Bouyer.
     60  */
     61 
     62 #ifndef _UFS_EXT2FS_EXT2FS_H_
     63 #define _UFS_EXT2FS_EXT2FS_H_
     64 
     65 #include <stdint.h>
     66 
     67 /*
     68  * Each disk drive contains some number of file systems.
     69  * A file system consists of a number of cylinder groups.
     70  * Each cylinder group has inodes and data.
     71  *
     72  * A file system is described by its super-block, which in turn
     73  * describes the cylinder groups.  The super-block is critical
     74  * data and is replicated in each cylinder group to protect against
     75  * catastrophic loss.  This is done at `newfs' time and the critical
     76  * super-block data does not change, so the copies need not be
     77  * referenced further unless disaster strikes.
     78  *
     79  * The first boot and super blocks are given in absolute disk addresses.
     80  * The byte-offset forms are preferred, as they don't imply a sector size.
     81  */
     82 #define BBSIZE 1024
     83 #define SBSIZE 1024
     84 #define BBOFF  ((off_t)(0))
     85 #define SBOFF  ((off_t)(BBOFF + BBSIZE))
     86 #define BBLOCK ((daddr_t)(0))
     87 #define SBLOCK ((daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE))
     88 
     89 #define fsbtodb(fs, b) ((daddr_t)(b) << (fs)->e2fs_fsbtodb)
     90 /* calculates (loc / fs->fs_bsize) */
     91 #define lblkno(fs, loc)      ((loc) >> (fs->e2fs_bshift))
     92 #define blksize(fs, ip, lbn) ((fs)->e2fs_bsize)
     93 
     94 /*
     95  * Addresses stored in inodes are capable of addressing blocks
     96  * XXX
     97  */
     98 
     99 /*
    100  * MINBSIZE is the smallest allowable block size.
    101  * MINBSIZE must be big enough to hold a cylinder group block,
    102  * thus changes to (struct cg) must keep its size within MINBSIZE.
    103  * Note that super blocks are always of size SBSIZE,
    104  * and that both SBSIZE and MAXBSIZE must be >= MINBSIZE.
    105  */
    106 #define LOG_MINBSIZE 10
    107 #define MINBSIZE     (1 << LOG_MINBSIZE)
    108 
    109 /*
    110  * The path name on which the file system is mounted is maintained
    111  * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
    112  * the super block for this name.
    113  */
    114 #define MAXMNTLEN 512
    115 
    116 /*
    117  * MINFREE gives the minimum acceptable percentage of file system
    118  * blocks which may be free. If the freelist drops below this level
    119  * only the superuser may continue to allocate blocks. This may
    120  * be set to 0 if no reserve of free blocks is deemed necessary,
    121  * however throughput drops by fifty percent if the file system
    122  * is run at between 95% and 100% full; thus the minimum default
    123  * value of fs_minfree is 5%. However, to get good clustering
    124  * performance, 10% is a better choice. hence we use 10% as our
    125  * default value. With 10% free space, fragmentation is not a
    126  * problem, so we choose to optimize for time.
    127  */
    128 #define MINFREE 5
    129 
    130 /*
    131  * This is maximum amount of links allowed for files. For directories,
    132  * going over this means setting DIR_NLINK feature.
    133  */
    134 #define EXT2FS_LINK_MAX 65000
    135 #define EXT2FS_LINK_INF 1 /* link count unknown */
    136 
    137 /*
    138  * Super block for an ext2fs file system.
    139  */
    140 struct ext2fs {
    141   uint32_t e2fs_icount;        /* Inode count */
    142   uint32_t e2fs_bcount;        /* blocks count */
    143   uint32_t e2fs_rbcount;       /* reserved blocks count */
    144   uint32_t e2fs_fbcount;       /* free blocks count */
    145   uint32_t e2fs_ficount;       /* free inodes count */
    146   uint32_t e2fs_first_dblock;  /* first data block */
    147   uint32_t e2fs_log_bsize;     /* bsize = 1024*(2^e2fs_log_bsize) */
    148   uint32_t e2fs_fsize;         /* fragment size */
    149   uint32_t e2fs_bpg;           /* blocks per group */
    150   uint32_t e2fs_fpg;           /* frags per group */
    151   uint32_t e2fs_ipg;           /* inodes per group */
    152   uint32_t e2fs_mtime;         /* mount time */
    153   uint32_t e2fs_wtime;         /* write time */
    154   uint16_t e2fs_mnt_count;     /* mount count */
    155   uint16_t e2fs_max_mnt_count; /* max mount count */
    156   uint16_t e2fs_magic;         /* magic number */
    157   uint16_t e2fs_state;         /* file system state */
    158   uint16_t e2fs_beh;           /* behavior on errors */
    159   uint16_t e2fs_minrev;        /* minor revision level */
    160   uint32_t e2fs_lastfsck;      /* time of last fsck */
    161   uint32_t e2fs_fsckintv;      /* max time between fscks */
    162   uint32_t e2fs_creator;       /* creator OS */
    163   uint32_t e2fs_rev;           /* revision level */
    164   uint16_t e2fs_ruid;          /* default uid for reserved blocks */
    165   uint16_t e2fs_rgid;          /* default gid for reserved blocks */
    166   /* EXT2_DYNAMIC_REV superblocks */
    167   uint32_t e2fs_first_ino;         /* first non-reserved inode */
    168   uint16_t e2fs_inode_size;        /* size of inode structure */
    169   uint16_t e2fs_block_group_nr;    /* block grp number of this sblk*/
    170   uint32_t e2fs_features_compat;   /*  compatible feature set */
    171   uint32_t e2fs_features_incompat; /* incompatible feature set */
    172   uint32_t e2fs_features_rocompat; /* RO-compatible feature set */
    173   uint8_t  e2fs_uuid[16];          /* 128-bit uuid for volume */
    174   char     e2fs_vname[16];         /* volume name */
    175   char     e2fs_fsmnt[64];         /* name mounted on */
    176   uint32_t e2fs_algo;              /* For compression */
    177   uint8_t  e2fs_prealloc;          /* # of blocks to preallocate */
    178   uint8_t  e2fs_dir_prealloc;      /* # of blocks to preallocate for dir */
    179   uint16_t e2fs_reserved_ngdb;     /* # of reserved gd blocks for resize */
    180 
    181   /* Additional fields */
    182   char     e3fs_journal_uuid[16]; /* uuid of journal superblock */
    183   uint32_t e3fs_journal_inum;     /* inode number of journal file */
    184   uint32_t e3fs_journal_dev;      /* device number of journal file */
    185   uint32_t e3fs_last_orphan;      /* start of list of inodes to delete */
    186   uint32_t e3fs_hash_seed[4];     /* HTREE hash seed */
    187   char     e3fs_def_hash_version; /* Default hash version to use */
    188   char     e3fs_jnl_backup_type;
    189   uint16_t e3fs_desc_size; /* size of group descriptor */
    190   uint32_t e3fs_default_mount_opts;
    191   uint32_t e3fs_first_meta_bg;    /* First metablock block group */
    192   uint32_t e3fs_mkfs_time;        /* when the fs was created */
    193   uint32_t e3fs_jnl_blks[17];     /* backup of the journal inode */
    194   uint32_t e4fs_bcount_hi;        /* high bits of blocks count */
    195   uint32_t e4fs_rbcount_hi;       /* high bits of reserved blocks count */
    196   uint32_t e4fs_fbcount_hi;       /* high bits of free blocks count */
    197   uint16_t e4fs_min_extra_isize;  /* all inodes have some bytes */
    198   uint16_t e4fs_want_extra_isize; /* inodes must reserve some bytes */
    199   uint32_t e4fs_flags;            /* miscellaneous flags */
    200   uint16_t e4fs_raid_stride;      /* RAID stride */
    201   uint16_t e4fs_mmpintv;          /* seconds to wait in MMP checking */
    202   uint64_t e4fs_mmpblk;           /* block for multi-mount protection */
    203   uint32_t e4fs_raid_stripe_wid;  /* blocks on data disks (N * stride) */
    204   uint8_t  e4fs_log_gpf;          /* FLEX_BG group size */
    205   uint8_t  e4fs_chksum_type;      /* metadata checksum algorithm used */
    206   uint8_t  e4fs_encrypt;          /* versioning level for encryption */
    207   uint8_t  e4fs_reserved_pad;
    208   uint64_t e4fs_kbytes_written;    /* number of lifetime kilobytes */
    209   uint32_t e4fs_snapinum;          /* inode number of active snapshot */
    210   uint32_t e4fs_snapid;            /* sequential ID of active snapshot */
    211   uint64_t e4fs_snaprbcount;       /* rsvd blocks for active snapshot */
    212   uint32_t e4fs_snaplist;          /* inode number for on-disk snapshot */
    213   uint32_t e4fs_errcount;          /* number of file system errors */
    214   uint32_t e4fs_first_errtime;     /* first time an error happened */
    215   uint32_t e4fs_first_errino;      /* inode involved in first error */
    216   uint64_t e4fs_first_errblk;      /* block involved of first error */
    217   uint8_t  e4fs_first_errfunc[32]; /* function where error happened */
    218   uint32_t e4fs_first_errline;     /* line number where error happened */
    219   uint32_t e4fs_last_errtime;      /* most recent time of an error */
    220   uint32_t e4fs_last_errino;       /* inode involved in last error */
    221   uint32_t e4fs_last_errline;      /* line number where error happened */
    222   uint64_t e4fs_last_errblk;       /* block involved of last error */
    223   uint8_t  e4fs_last_errfunc[32];  /* function where error happened */
    224   uint8_t  e4fs_mount_opts[64];
    225   uint32_t e4fs_usrquota_inum;       /* inode for tracking user quota */
    226   uint32_t e4fs_grpquota_inum;       /* inode for tracking group quota */
    227   uint32_t e4fs_overhead_clusters;   /* overhead blocks/clusters */
    228   uint32_t e4fs_backup_bgs[2];       /* groups with sparse_super2 SBs */
    229   uint8_t  e4fs_encrypt_algos[4];    /* encryption algorithms in use */
    230   uint8_t  e4fs_encrypt_pw_salt[16]; /* salt used for string2key */
    231   uint32_t e4fs_lpf_ino;             /* location of the lost+found inode */
    232   uint32_t e4fs_proj_quota_inum;     /* inode for tracking project quota */
    233   uint32_t e4fs_chksum_seed;         /* checksum seed */
    234   uint32_t e4fs_reserved[98];        /* padding to the end of the block */
    235   uint32_t e4fs_sbchksum;            /* superblock checksum */
    236 };
    237 
    238 /* in-memory data for ext2fs */
    239 struct m_ext2fs {
    240   struct ext2fs   e2fs;
    241   u_char          e2fs_fsmnt[MAXMNTLEN]; /* name mounted on */
    242   int8_t          e2fs_ronly;            /* mounted read-only flag */
    243   int8_t          e2fs_fmod;             /* super block modified flag */
    244   int8_t          e2fs_uhash;            /* 3 if hash should be signed, 0 if not */
    245   int32_t         e2fs_bsize;            /* block size */
    246   int32_t         e2fs_bshift;           /* ``lblkno'' calc of logical blkno */
    247   int32_t         e2fs_bmask;            /* ``blkoff'' calc of blk offsets */
    248   int64_t         e2fs_qbmask;           /* ~fs_bmask - for use with quad size */
    249   int32_t         e2fs_fsbtodb;          /* fsbtodb and dbtofsb shift constant */
    250   int32_t         e2fs_ncg;              /* number of cylinder groups */
    251   int32_t         e2fs_ngdb;             /* number of group descriptor blocks */
    252   int32_t         e2fs_ipb;              /* number of inodes per block */
    253   int32_t         e2fs_itpg;             /* number of inode table blocks per group */
    254   uint8_t         e2fs_group_desc_shift; /* binary log group desc size */
    255   struct ext2_gd *e2fs_gd;               /* group descriptors (data not byteswapped) */
    256 };
    257 
    258 /*
    259  * Filesystem identification
    260  */
    261 #define E2FS_MAGIC 0xef53 /* the ext2fs magic number */
    262 #define E2FS_REV0  0      /* GOOD_OLD revision */
    263 #define E2FS_REV1  1      /* Support compat/incompat features */
    264 
    265 /* compatible/incompatible features */
    266 #define EXT2F_COMPAT_PREALLOC     0x0001
    267 #define EXT2F_COMPAT_AFS          0x0002
    268 #define EXT2F_COMPAT_HASJOURNAL   0x0004
    269 #define EXT2F_COMPAT_EXTATTR      0x0008
    270 #define EXT2F_COMPAT_RESIZE       0x0010
    271 #define EXT2F_COMPAT_DIRHASHINDEX 0x0020
    272 #define EXT2F_COMPAT_SPARSESUPER2 0x0200
    273 #define EXT2F_COMPAT_BITS                                                                          \
    274   "\20"                                                                                            \
    275   "\12COMPAT_SPARSESUPER2"                                                                         \
    276   "\11"                                                                                            \
    277   "\10"                                                                                            \
    278   "\07"                                                                                            \
    279   "\06COMPAT_DIRHASHINDEX"                                                                         \
    280   "\05COMPAT_RESIZE"                                                                               \
    281   "\04COMPAT_EXTATTR"                                                                              \
    282   "\03COMPAT_HASJOURNAL"                                                                           \
    283   "\02COMPAT_AFS"                                                                                  \
    284   "\01COMPAT_PREALLOC"
    285 
    286 #define EXT2F_ROCOMPAT_SPARSESUPER    0x0001
    287 #define EXT2F_ROCOMPAT_LARGEFILE      0x0002
    288 #define EXT2F_ROCOMPAT_BTREE_DIR      0x0004
    289 #define EXT2F_ROCOMPAT_HUGE_FILE      0x0008
    290 #define EXT2F_ROCOMPAT_GDT_CSUM       0x0010
    291 #define EXT2F_ROCOMPAT_DIR_NLINK      0x0020
    292 #define EXT2F_ROCOMPAT_EXTRA_ISIZE    0x0040
    293 #define EXT2F_ROCOMPAT_QUOTA          0x0100
    294 #define EXT2F_ROCOMPAT_BIGALLOC       0x0200
    295 #define EXT2F_ROCOMPAT_METADATA_CKSUM 0x0400
    296 #define EXT2F_ROCOMPAT_READONLY       0x1000
    297 #define EXT2F_ROCOMPAT_PROJECT        0x2000
    298 #define EXT2F_ROCOMPAT_BITS                                                                        \
    299   "\20"                                                                                            \
    300   "\16ROCOMPAT_PROJECT"                                                                            \
    301   "\15ROCOMPAT_READONLY"                                                                           \
    302   "\14?"                                                                                           \
    303   "\13ROCOMPAT_METADATA_CKSUM"                                                                     \
    304   "\12ROCOMPAT_BIGALLOC"                                                                           \
    305   "\11ROCOMPAT_QUOTA"                                                                              \
    306   "\10?"                                                                                           \
    307   "\07ROCOMPAT_EXTRA_ISIZE"                                                                        \
    308   "\06ROCOMPAT_DIR_NLINK"                                                                          \
    309   "\05ROCOMPAT_GDT_CSUM"                                                                           \
    310   "\04ROCOMPAT_HUGE_FILE"                                                                          \
    311   "\03ROCOMPAT_BTREE_DIR"                                                                          \
    312   "\02ROCOMPAT_LARGEFILE"                                                                          \
    313   "\01ROCOMPAT_SPARSESUPER"
    314 
    315 #define EXT2F_INCOMPAT_COMP           0x0001
    316 #define EXT2F_INCOMPAT_FTYPE          0x0002
    317 #define EXT2F_INCOMPAT_REPLAY_JOURNAL 0x0004
    318 #define EXT2F_INCOMPAT_USES_JOURNAL   0x0008
    319 #define EXT2F_INCOMPAT_META_BG        0x0010
    320 #define EXT2F_INCOMPAT_EXTENTS        0x0040
    321 #define EXT2F_INCOMPAT_64BIT          0x0080
    322 #define EXT2F_INCOMPAT_MMP            0x0100
    323 #define EXT2F_INCOMPAT_FLEX_BG        0x0200
    324 #define EXT2F_INCOMPAT_EA_INODE       0x0400
    325 #define EXT2F_INCOMPAT_DIRDATA        0x1000
    326 #define EXT2F_INCOMPAT_CSUM_SEED      0x2000
    327 #define EXT2F_INCOMPAT_LARGEDIR       0x4000
    328 #define EXT2F_INCOMPAT_INLINE_DATA    0x8000
    329 #define EXT2F_INCOMPAT_ENCRYPT        0x10000
    330 #define EXT2F_INCOMPAT_BITS                                                                        \
    331   "\20"                                                                                            \
    332   "\021INCOMPAT_ENCRYPT"                                                                           \
    333   "\020INCOMPAT_INLINE_DATA"                                                                       \
    334   "\017INCOMPAT_LARGEDIR"                                                                          \
    335   "\016INCOMPAT_CSUM_SEED"                                                                         \
    336   "\015INCOMPAT_DIRDATA"                                                                           \
    337   "\014?"                                                                                          \
    338   "\013INCOMPAT_EA_INODE"                                                                          \
    339   "\012INCOMPAT_FLEX_BG"                                                                           \
    340   "\011INCOMPAT_MMP"                                                                               \
    341   "\010INCOMPAT_64BIT"                                                                             \
    342   "\07INCOMPAT_EXTENTS"                                                                            \
    343   "\05INCOMPAT_META_BG"                                                                            \
    344   "\04INCOMPAT_USES_JOURNAL"                                                                       \
    345   "\03INCOMPAT_REPLAY_JOURNAL"                                                                     \
    346   "\02INCOMPAT_FTYPE"                                                                              \
    347   "\01INCOMPAT_COMP"
    348 
    349 /*
    350  * Features supported in this implementation
    351  *
    352  * We support the following REV1 features:
    353  * - EXT2F_ROCOMPAT_SPARSESUPER
    354  *    superblock backups stored only in cg_has_sb(bno) groups
    355  * - EXT2F_ROCOMPAT_LARGEFILE
    356  *    use e2di_size_high in struct ext2fs_dinode to store
    357  *    upper 32bit of size for >2GB files
    358  * - EXT2F_INCOMPAT_FTYPE
    359  *    store file type to e2d_type in struct ext2fs_direct
    360  *    (on REV0 e2d_namlen is uint16_t and no e2d_type, like ffs)
    361  */
    362 #define EXT2F_COMPAT_SUPP 0x0000
    363 #define EXT2F_ROCOMPAT_SUPP                                                                        \
    364   (EXT2F_ROCOMPAT_SPARSESUPER | EXT2F_ROCOMPAT_LARGEFILE | EXT2F_ROCOMPAT_HUGE_FILE                \
    365    | EXT2F_ROCOMPAT_EXTRA_ISIZE | EXT2F_ROCOMPAT_DIR_NLINK | EXT2F_ROCOMPAT_GDT_CSUM)
    366 #define EXT2F_INCOMPAT_SUPP                                                                        \
    367   (EXT2F_INCOMPAT_FTYPE | EXT2F_INCOMPAT_EXTENTS | EXT2F_INCOMPAT_FLEX_BG | EXT2F_INCOMPAT_64BIT)
    368 
    369 /*
    370  * Feature set definitions
    371  */
    372 #define EXT2F_HAS_COMPAT_FEATURE(fs, feature)                                                      \
    373   ((fs)->e2fs.e2fs_rev >= E2FS_REV1 && ((fs)->e2fs.e2fs_features_compat & (feature)) != 0)
    374 
    375 #define EXT2F_HAS_ROCOMPAT_FEATURE(fs, feature)                                                    \
    376   ((fs)->e2fs.e2fs_rev >= E2FS_REV1 && ((fs)->e2fs.e2fs_features_rocompat & (feature)) != 0)
    377 
    378 #define EXT2F_HAS_INCOMPAT_FEATURE(fs, feature)                                                    \
    379   ((fs)->e2fs.e2fs_rev >= E2FS_REV1 && ((fs)->e2fs.e2fs_features_incompat & (feature)) != 0)
    380 
    381 /*
    382  * Definitions of behavior on errors
    383  */
    384 #define E2FS_BEH_CONTINUE 1 /* continue operation */
    385 #define E2FS_BEH_READONLY 2 /* remount fs read only */
    386 #define E2FS_BEH_PANIC    3 /* cause panic */
    387 #define E2FS_BEH_DEFAULT  E2FS_BEH_CONTINUE
    388 
    389 /*
    390  * OS identification
    391  */
    392 #define E2FS_OS_LINUX   0
    393 #define E2FS_OS_HURD    1
    394 #define E2FS_OS_MASIX   2
    395 #define E2FS_OS_FREEBSD 3
    396 #define E2FS_OS_LITES   4
    397 
    398 /*
    399  * Filesystem clean flags
    400  */
    401 #define E2FS_ISCLEAN 0x01
    402 #define E2FS_ERRORS  0x02
    403 
    404 /* ext2 file system block group descriptor */
    405 
    406 struct ext2_gd {
    407   uint32_t ext2bgd_b_bitmap; /* blocks bitmap block */
    408   uint32_t ext2bgd_i_bitmap; /* inodes bitmap block */
    409   uint32_t ext2bgd_i_tables; /* first inodes table block */
    410   uint16_t ext2bgd_nbfree;   /* number of free blocks */
    411   uint16_t ext2bgd_nifree;   /* number of free inodes */
    412   uint16_t ext2bgd_ndirs;    /* number of directories */
    413 
    414   /*
    415    * Following only valid when either GDT_CSUM (AKA uninit_bg)
    416    * or METADATA_CKSUM feature is on
    417    */
    418   uint16_t ext2bgd_flags;                /* ext4 bg flags (INODE_UNINIT, ...)*/
    419   uint32_t ext2bgd_exclude_bitmap_lo;    /* snapshot exclude bitmap */
    420   uint16_t ext2bgd_block_bitmap_csum_lo; /* Low block bitmap checksum */
    421   uint16_t ext2bgd_inode_bitmap_csum_lo; /* Low inode bitmap checksum */
    422   uint16_t ext2bgd_itable_unused_lo;     /* Low unused inode offset */
    423   uint16_t ext2bgd_checksum;             /* Group desc checksum */
    424 
    425   uint32_t ext2bgd_b_bitmap_hi; /* blocks bitmap block (high bits) */
    426   uint32_t ext2bgd_i_bitmap_hi; /* inodes bitmap block (high bits) */
    427   uint32_t ext2bgd_i_tables_hi; /* inodes table block (high bits)  */
    428   uint16_t ext2bgd_nbfree_hi;   /* number of free blocks (high bits) */
    429   uint16_t ext2bgd_nifree_hi;   /* number of free inodes (high bits) */
    430   uint16_t ext2bgd_ndirs_hi;    /* number of directories (high bits) */
    431   uint16_t reserved_hi;
    432   uint32_t reserved2_hi[3];
    433 };
    434 
    435 #define E2FS_REV0_GD_SIZE    (sizeof(struct ext2_gd) / 2) /* 32 */
    436 #define E2FS_BG_INODE_UNINIT 0x0001                       /* Inode bitmap not used/initialized */
    437 #define E2FS_BG_BLOCK_UNINIT 0x0002                       /* Block bitmap not used/initialized */
    438 #define E2FS_BG_INODE_ZEROED 0x0004                       /* On-disk inode table initialized */
    439 
    440 #define E2FS_HAS_GD_CSUM(fs)                                                                       \
    441   EXT2F_HAS_ROCOMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM | EXT2F_ROCOMPAT_METADATA_CKSUM) != 0
    442 
    443 /*
    444  * If the EXT2F_ROCOMPAT_SPARSESUPER flag is set, the cylinder group has a
    445  * copy of the super and cylinder group descriptors blocks only if it's
    446  * 1, a power of 3, 5 or 7
    447  */
    448 
    449 static __inline int cg_has_sb(int) __unused;
    450 static __inline int
    451 cg_has_sb(int i)
    452 {
    453   int a3, a5, a7;
    454 
    455   if (i == 0 || i == 1)
    456     return 1;
    457   for (a3 = 3, a5 = 5, a7 = 7; a3 <= i || a5 <= i || a7 <= i; a3 *= 3, a5 *= 5, a7 *= 7)
    458     if (i == a3 || i == a5 || i == a7)
    459       return 1;
    460   return 0;
    461 }
    462 
    463 /* EXT2FS metadatas are stored in little-endian byte order. These macros
    464  * helps reading theses metadatas
    465  */
    466 
    467 #ifdef fsck_little_endian
    468 #define h2fs16(x)             (x)
    469 #define h2fs32(x)             (x)
    470 #define h2fs64(x)             (x)
    471 #define fs2h16(x)             (x)
    472 #define fs2h32(x)             (x)
    473 #define fs2h64(x)             (x)
    474 #define e2fs_sbload(old, new) memcpy((new), (old), SBSIZE)
    475 #define e2fs_sbsave(old, new) memcpy((new), (old), SBSIZE)
    476 #else
    477 void e2fs_sb_bswap(struct ext2fs *, struct ext2fs *);
    478 #define h2fs16(x)             bswap16(x)
    479 #define h2fs32(x)             bswap32(x)
    480 #define h2fs64(x)             bswap64(x)
    481 #define fs2h16(x)             bswap16(x)
    482 #define fs2h32(x)             bswap32(x)
    483 #define fs2h64(x)             bswap64(x)
    484 #define e2fs_sbload(old, new) e2fs_sb_bswap((old), (new))
    485 #define e2fs_sbsave(old, new) e2fs_sb_bswap((old), (new))
    486 #endif
    487 
    488 #ifndef _KERNEL /* XXX */
    489 /* Group descriptors are not byte swapped */
    490 #define e2fs_cgload(old, new, size) memcpy((new), (old), (size))
    491 #define e2fs_cgsave(old, new, size) memcpy((new), (old), (size))
    492 #endif
    493 
    494 /*
    495  * Turn file system block numbers into disk block addresses.
    496  * This maps file system blocks to device size blocks.
    497  */
    498 #define EXT2_FSBTODB(fs, b)         ((int64_t)(b) << (fs)->e2fs_fsbtodb)
    499 #define EXT2_FSBTODB64(fs, b, b_hi) (((((uint64_t)(b_hi)) << 32) | (b)) << (fs)->e2fs_fsbtodb)
    500 #define EXT2_FSBTODB64OFF(fs, b, b_hi, off)                                                        \
    501   ((((((uint64_t)(b_hi)) << 32) | (b)) + (off)) << (fs)->e2fs_fsbtodb)
    502 #define EXT2_DBTOFSB(fs, b) ((b) >> (fs)->e2fs_fsbtodb)
    503 
    504 /*
    505  * Macros for handling inode numbers:
    506  *	 inode number to file system block offset.
    507  *	 inode number to cylinder group number.
    508  *	 inode number to file system block address.
    509  */
    510 #define ino_to_cg(fs, x) (((x) - 1) / (fs)->e2fs.e2fs_ipg)
    511 #define _e2fs_gd(fs, x)  (fs)->e2fs_gd[ino_to_cg((fs), (x))]
    512 #define ino_to_fsba(fs, x)                                                                         \
    513   (fs2h32(_e2fs_gd(fs, x).ext2bgd_i_tables)                                                        \
    514    + (((uint64_t)fs2h32(_e2fs_gd(fs, x).ext2bgd_i_tables_hi)) << 32)                               \
    515    + (((x) - 1) % (fs)->e2fs.e2fs_ipg) / (fs)->e2fs_ipb)
    516 #define ino_to_fsbo(fs, x) (((x) - 1) % (fs)->e2fs_ipb)
    517 
    518 /*
    519  * Give cylinder group number for a file system block.
    520  * Give cylinder group block number for a file system block.
    521  */
    522 #define dtog(fs, d)  (((d) - (fs)->e2fs.e2fs_first_dblock) / (fs)->e2fs.e2fs_fpg)
    523 #define dtogd(fs, d) (((d) - (fs)->e2fs.e2fs_first_dblock) % (fs)->e2fs.e2fs_fpg)
    524 
    525 /*
    526  * The following macros optimize certain frequently calculated
    527  * quantities by using shifts and masks in place of divisions
    528  * modulos and multiplications.
    529  */
    530 #define ext2_blkoff(fs, loc)     /* calculates (loc % fs->e2fs_bsize) */ ((loc) & (fs)->e2fs_qbmask)
    531 #define ext2_lblktosize(fs, blk) /* calculates (blk * fs->e2fs_bsize) */                           \
    532   ((blk) << (fs)->e2fs_bshift)
    533 #define ext2_lblkno(fs, loc) /* calculates (loc / fs->e2fs_bsize) */ ((loc) >> (fs)->e2fs_bshift)
    534 #define ext2_blkroundup(fs, size) /* calculates roundup(size, fs->e2fs_bsize) */                   \
    535   (((size) + (fs)->e2fs_qbmask) & (fs)->e2fs_bmask)
    536 #define ext2_fragroundup(fs, size) /* calculates roundup(size, fs->e2fs_bsize) */                  \
    537   (((size) + (fs)->e2fs_qbmask) & (fs)->e2fs_bmask)
    538 /*
    539  * Determine the number of available frags given a
    540  * percentage to hold in reserve.
    541  */
    542 #define freespace(fs) ((fs)->e2fs.e2fs_fbcount - (fs)->e2fs.e2fs_rbcount)
    543 
    544 /*
    545  * Number of indirects in a file system block.
    546  */
    547 #define EXT2_NINDIR(fs) ((fs)->e2fs_bsize / sizeof(uint32_t))
    548 
    549 #endif /* !_UFS_EXT2FS_EXT2FS_H_ */