1 /*        $NetBSD: minixfs3.h,v 1.5 2013/06/23 07:28:36 dholland Exp $ */
2 
3 /*-
4  * Copyright (c) 2012
5  *        Vrije Universiteit, Amsterdam, The Netherlands. All rights reserved.
6  *
7  * Author: Evgeniy Ivanov
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
19  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef MINIX_FS_3_H
32 #define MINIX_FS_3_H
33 
34 FS_DEF(minixfs3);
35 
36 typedef uint32_t zone_t;
37 typedef uint16_t zone1_t;
38 typedef uint32_t block_t;
39 
40 #define NR_DZONES   7         /* # direct zone numbers in an inode */
41 #define NR_TZONES   10        /* total # zone numbers in an inode */
42 #define NIADDR                2       /* Indirect addresses in inode */
43 
44 struct mfs_dinode {
45           uint16_t  mdi_mode; /* file type, protection, etc. */
46           uint16_t  mdi_nlinks;         /* how many links to this file */
47           int16_t   mdi_uid;  /* user id of the file's owner */
48           uint16_t  mdi_gid;  /* group number */
49           uint32_t  mdi_size; /* current file size in bytes */
50           uint32_t  mdi_atime;          /* time of last access */
51           uint32_t  mdi_mtime;          /* when was file data last changed */
52           uint32_t  mdi_ctime;          /* when was inode itself changed */
53           zone_t    mdi_zone[NR_TZONES]; /* zone numbers for direct, ind, and
54                                                             dbl ind */
55 };
56 
57 /* Maximum Minix MFS on-disk directory filename.
58  * MFS uses 'struct direct' to write and parse
59  * directory entries, so this can't be changed
60  * without breaking filesystems.
61  */
62 #define MFS_DIRSIZ  60
63 
64 struct mfs_direct {
65           uint32_t  mfsd_ino;
66           char      mfsd_name[MFS_DIRSIZ];
67 } __packed;
68 
69 struct mfs_sblock {
70           uint32_t  mfs_ninodes;                  /* # usable inodes on the minor device */
71           zone1_t   mfs_nzones;                   /* total device size, including bit maps etc */
72           int16_t   mfs_imap_blocks;    /* # of blocks used by inode bit map */
73           int16_t   mfs_zmap_blocks;    /* # of blocks used by zone bit map */
74           zone1_t   mfs_firstdatazone_old;/* number of first data zone (small) */
75           int16_t   mfs_log_zone_size;  /* log2 of blocks/zone */
76           int16_t   mfs_pad;            /* try to avoid compiler-dependent padding */
77           int32_t   mfs_max_size;                 /* maximum file size on this device */
78           zone_t    mfs_zones;                    /* number of zones (replaces s_nzones in V2) */
79           int16_t   mfs_magic;                    /* magic number to recognize super-blocks */
80           int16_t   mfs_pad2;           /* try to avoid compiler-dependent padding */
81           uint16_t  mfs_block_size;     /* block size in bytes. */
82           char      mfs_disk_version;   /* filesystem format sub-version */
83 
84   /* The following items are only used when the super_block is in memory,
85    * mfs_inodes_per_block must be the firs one (see SBSIZE)
86    */
87           unsigned mfs_inodes_per_block;          /* precalculated from magic number */
88           zone_t   mfs_firstdatazone;   /* number of first data zone (big) */
89           int32_t  mfs_bshift;                    /* ``lblkno'' calc of logical blkno */
90           int32_t  mfs_bmask;           /* ``blkoff'' calc of blk offsets */
91           int64_t  mfs_qbmask;                    /* ~fs_bmask - for use with quad size */
92           int32_t  mfs_fsbtodb;                   /* fsbtodb and dbtofsb shift constant */
93 };
94 
95 #define LOG_MINBSIZE          10
96 #define MINBSIZE    (1 << LOG_MINBSIZE)
97 
98 #define SUPER_MAGIC 0x4d5a    /* magic # for MFSv3 file systems */
99 
100 #define ROOT_INODE  ((uint32_t) 1)      /* inode number for root directory */
101 #define SUPER_BLOCK_OFF (1024)                    /* bytes offset */
102 #define START_BLOCK ((block_t) 2)       /* first fs block (not counting SB) */
103 
104 /* # bytes/dir entry */
105 #define DIR_ENTRY_SIZE                  sizeof(struct mfs_direct)
106 /* # dir entries/blk */
107 #define NR_DIR_ENTRIES(fs)    ((fs)->mfs_block_size/DIR_ENTRY_SIZE)
108 /* mfs_sblock on-disk part size */
109 #define SBSIZE                          offsetof(struct mfs_sblock, mfs_inodes_per_block)
110 
111 #define ZONE_NUM_SIZE                   sizeof(zone_t) /* # bytes in zone  */
112 #define INODE_SIZE            sizeof(struct mfs_dinode) /* bytes in dsk ino */
113 /* # zones/indir block */
114 #define MFS_NINDIR(fs)                  ((fs)->mfs_block_size/ZONE_NUM_SIZE)
115 
116 #define NO_ZONE                         ((zone_t) 0)        /* absence of a zone number */
117 #define NO_BLOCK              ((block_t) 0)       /* absence of a block number */
118 
119 /* Turn file system block numbers into disk block addresses */
120 #define MFS_FSBTODB(fs, b)    ((b) << (fs)->mfs_fsbtodb)
121 
122 #define   ino_to_fsba(fs, x)                                                    \
123           (((x) - 1) / (fs)->mfs_inodes_per_block +                             \
124           START_BLOCK + (fs)->mfs_imap_blocks + (fs)->mfs_zmap_blocks)
125 #define   ino_to_fsbo(fs, x)  (((x) - 1) % (fs)->mfs_inodes_per_block)
126 
127 /*
128  * MFS metadatas are stored in little-endian byte order. These macros
129  * helps reading theses metadatas.
130  */
131 #if BYTE_ORDER == LITTLE_ENDIAN
132 #         define fs2h16(x) (x)
133 #         define fs2h32(x) (x)
134 #         define mfs_sbload(old, new)   \
135                     memcpy((new), (old), SBSIZE);
136 #         define mfs_iload(old, new)    \
137                     memcpy((new),(old),sizeof(struct mfs_dinode))
138 #else
139 void minixfs3_sb_bswap(struct mfs_sblock *, struct mfs_sblock *);
140 void minixfs3_i_bswap(struct mfs_dinode *, struct mfs_dinode *);
141 #         define fs2h16(x) bswap16(x)
142 #         define fs2h32(x) bswap32(x)
143 #         define mfs_sbload(old, new) minixfs3_sb_bswap((old), (new))
144 #         define mfs_iload(old, new) minixfs3_i_bswap((old), (new))
145 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
146 
147 /*
148  * The following macros optimize certain frequently calculated
149  * quantities by using shifts and masks in place of divisions
150  * modulos and multiplications.
151  */
152 #define mfs_blkoff(fs, loc)   /* calculates (loc % fs->mfs_bsize) */ \
153           ((loc) & (fs)->mfs_qbmask)
154 #define mfs_lblkno(fs, loc)   /* calculates (loc / fs->mfs_bsize) */ \
155           ((loc) >> (fs)->mfs_bshift)
156 
157 /* Flag bits for i_mode in the inode. */
158 #define I_TYPE          0170000 /* this field gives inode type */
159 #define I_UNIX_SOCKET   0140000 /* unix domain socket */
160 #define I_SYMBOLIC_LINK 0120000 /* file is a symbolic link */
161 #define I_REGULAR       0100000 /* regular file, not dir or special */
162 #define I_BLOCK_SPECIAL 0060000 /* block special file */
163 #define I_DIRECTORY     0040000 /* file is a directory */
164 #define I_CHAR_SPECIAL  0020000 /* character special file */
165 #define I_NAMED_PIPE    0010000 /* named pipe (FIFO) */
166 #define I_SET_UID_BIT   0004000 /* set effective uid_t on exec */
167 #define I_SET_GID_BIT   0002000 /* set effective gid_t on exec */
168 #define I_SET_STCKY_BIT 0001000 /* sticky bit */
169 #define ALL_MODES       0007777 /* all bits for user, group and others */
170 #define RWX_MODES       0000777 /* mode bits for RWX only */
171 #define R_BIT           0000004 /* Rwx protection bit */
172 #define W_BIT           0000002 /* rWx protection bit */
173 #define X_BIT           0000001 /* rwX protection bit */
174 #define I_NOT_ALLOC     0000000 /* this inode is free */
175 
176 #endif /* MINIX_FS_3_H */
177