devproc: change user from char* to struct username
[akaros.git] / kern / include / ext2fs.h
1 /* Copyright (c) 2010 The Regents of the University of California
2  * Barret Rhoden <brho@cs.berkeley.edu>
3  * See LICENSE for details.
4  *
5  * The ROS port for Ext2 FS.  Thanks to Dave Poirier for maintaining this great
6  * documentation: http://www.nongnu.org/ext2-doc/ext2.html and the ext group
7  * that wrote the FS in the first place.
8  *
9  * Note all of ext2's disk structures are little-endian. */
10
11 #pragma once
12
13 #include <ros/common.h>
14 #include <vfs.h>
15 #include <endian.h>
16
17 #define EXT2_SUPER_MAGIC                0xef53
18
19 /* State settings */
20 #define EXT2_VALID_FS                   1               /* Cleanly unmounted */
21 #define EXT2_ERROR_FS                   2               /* Currently mounted / not unmounted */
22
23 /* s_error options */
24 #define EXT2_ERRORS_CONTINUE    1               /* continue on error */
25 #define EXT2_ERRORS_RO                  2               /* remount read-only */
26 #define EXT2_ERRORS_PANIC               3               /* panic on error */
27
28 /* Creator OS options */
29 #define EXT2_OS_LINUX                   0
30 #define EXT2_OS_HURD                    1
31 #define EXT2_OS_MASIX                   2
32 #define EXT2_OS_FREEBSD                 3
33 #define EXT2_OS_LITES                   4
34 #define EXT2_OS_ROS                             666             /* got dibs on the mark of the beast */
35
36 /* Revision Levels */
37 #define EXT2_GOOD_OLD_REV               0               /* Revision 0 */
38 #define EXT2_DYNAMIC_REV                1               /* Revision 1, extra crazies, etc */
39
40 /* FS Compatibile Features.  We can support them or now, without risk of
41  * damaging meta-data. */
42 #define EXT2_FEATURE_COMPAT_DIR_PREALLOC        0x0001  /* block prealloc */
43 #define EXT2_FEATURE_COMPAT_MAGIC_INODES        0x0002
44 #define EXT2_FEATURE_COMPAT_HAS_JOURNAL         0x0004  /* ext3/4 journal */
45 #define EXT2_FEATURE_COMPAT_EXT_ATTR            0x0008  /* extended inode attr */
46 #define EXT2_FEATURE_COMPAT_RESIZE_INO          0x0010  /* non-standard ino size */
47 #define EXT2_FEATURE_COMPAT_DIR_INDEX           0x0020  /* h-tree dir indexing */
48
49 /* FS Incompatibile Features.  We should refuse to mount if we don't support
50  * any of these. */
51 #define EXT2_FEATURE_INCOMPAT_COMPRESSION       0x0001  /* disk compression */
52 #define EXT2_FEATURE_INCOMPAT_FILETYPE          0x0002
53 #define EXT2_FEATURE_INCOMPAT_RECOVER           0x0004
54 #define EXT2_FEATURE_INCOMPAT_JOURNAL_DEV       0x0008
55 #define EXT2_FEATURE_INCOMPAT_META_BG           0x0010
56
57 /* FS read-only features: We should mount read-only if we don't support these */
58 #define EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER     0x0001  /* sparse superblock */
59 #define EXT2_FEATURE_RO_COMPAT_LARGE_FILE       0x0002  /* 64-bit filesize */
60 #define EXT2_FEATURE_RO_COMPAT_BTREE_DIR        0x0004  /* binary tree sorted dir */
61
62 /* Compression types (s_algo_bitmap) */
63 #define EXT2_LZV1_ALG                   0x0001
64 #define EXT2_LZRW3A_ALG                 0x0002
65 #define EXT2_GZIP_ALG                   0x0004
66 #define EXT2_BZIP2_ALG                  0x0008
67 #define EXT2_LZO_ALG                    0x0010
68
69 /* Defined Reserved inodes */
70 #define EXT2_BAD_INO                    1               /* bad blocks inode */
71 #define EXT2_ROOT_INO                   2               /* root directory inode */
72 #define EXT2_ACL_IDX_INO                3               /* ACL index (deprecated?) */
73 #define EXT2_ACL_DATA_INO               4               /* ACL data (deprecated?) */
74 #define EXT2_BOOT_LOADER_INO    5               /* boot loader inode */
75 #define EXT2_UNDEL_DIR_INO              6               /* undelete directory inode */
76
77 /* Inode/File access mode and type (i_mode).  Note how they use hex here, but
78 the crap we keep around for glibc/posix is (probably still) in octal. */
79 #define EXT2_S_IFSOCK                   0xC000  /* socket */
80 #define EXT2_S_IFLNK                    0xA000  /* symbolic link */
81 #define EXT2_S_IFREG                    0x8000  /* regular file */
82 #define EXT2_S_IFBLK                    0x6000  /* block device */
83 #define EXT2_S_IFDIR                    0x4000  /* directory */
84 #define EXT2_S_IFCHR                    0x2000  /* character device */
85 #define EXT2_S_IFIFO                    0x1000  /* fifo */
86 #define EXT2_S_ISUID                    0x0800  /* set process user id */
87 #define EXT2_S_ISGID                    0x0400  /* set process group id */
88 #define EXT2_S_ISVTX                    0x0200  /* sticky bit */
89 #define EXT2_S_IRUSR                    0x0100  /* user read */
90 #define EXT2_S_IWUSR                    0x0080  /* user write */
91 #define EXT2_S_IXUSR                    0x0040  /* user execute */
92 #define EXT2_S_IRGRP                    0x0020  /* group read */
93 #define EXT2_S_IWGRP                    0x0010  /* group write */
94 #define EXT2_S_IXGRP                    0x0008  /* group execute */
95 #define EXT2_S_IROTH                    0x0004  /* others read */
96 #define EXT2_S_IWOTH                    0x0002  /* others write */
97 #define EXT2_S_IXOTH                    0x0001  /* others execute */
98
99 /* Inode flags, for how to access data for an inode/file/object */
100 #define EXT2_SECRM_FL                   0x00000001      /* secure deletion */
101 #define EXT2_UNRM_FL                    0x00000002      /* record for undelete */
102 #define EXT2_COMPR_FL                   0x00000004      /* compressed file */
103 #define EXT2_SYNC_FL                    0x00000008      /* synchronous updates */
104 #define EXT2_IMMUTABLE_FL               0x00000010      /* immutable file */
105 #define EXT2_APPEND_FL                  0x00000020      /* append only */
106 #define EXT2_NODUMP_FL                  0x00000040      /* do not dump/delete file */
107 #define EXT2_NOATIME_FL                 0x00000080      /* do not update i_atime */
108 /* Compression Flags */
109 #define EXT2_DIRTY_FL                   0x00000100      /* dirty (modified) */
110 #define EXT2_COMPRBLK_FL                0x00000200      /* compressed blocks */
111 #define EXT2_NOCOMPR_FL                 0x00000400      /* access raw compressed data */
112 #define EXT2_ECOMPR_FL                  0x00000800      /* compression error */
113 /* End of compression flags */
114 #define EXT2_BTREE_FL                   0x00010000      /* b-tree format directory */
115 #define EXT2_INDEX_FL                   0x00010000      /* hash indexed directory */
116 #define EXT2_IMAGIC_FL                  0x00020000      /* AFS directory */
117 #define EXT3_JOURNAL_DATA_FL    0x00040000      /* journal file data */
118 #define EXT2_RESERVED_FL                0x80000000      /* reserved for ext2 library */
119
120 /* Directory entry file types */
121 #define EXT2_FT_UNKNOWN                 0       /* unknown file type */
122 #define EXT2_FT_REG_FILE                1       /* regular file */
123 #define EXT2_FT_DIR                             2       /* directory */
124 #define EXT2_FT_CHRDEV                  3       /* character device */
125 #define EXT2_FT_BLKDEV                  4       /* block device */
126 #define EXT2_FT_FIFO                    5       /* FIFO / buffer file */
127 #define EXT2_FT_SOCK                    6       /* socket */
128 #define EXT2_FT_SYMLINK                 7       /* symbolic link */
129
130 struct ext2_sb {
131         uint32_t                                        s_inodes_cnt;           /* total, both used/free */
132         uint32_t                                        s_blocks_cnt;           /* used/free/reserved */
133         uint32_t                                        s_rblocks_cnt;          /* reserved for su/brho */
134         uint32_t                                        s_free_blocks_cnt;      /* free, incl reserved */
135         uint32_t                                        s_free_inodes_cnt;
136         uint32_t                                        s_first_data_block;     /* id of block holding sb */
137         uint32_t                                        s_log_block_size;
138         uint32_t                                        s_log_frag_size;        /* no real frag support */
139         uint32_t                                        s_blocks_per_group;
140         uint32_t                                        s_frags_per_group;
141         uint32_t                                        s_inodes_per_group;
142         uint32_t                                        s_mtime;                        /* last mount time */
143         uint32_t                                        s_wtime;                        /* last write to the FS */
144         uint16_t                                        s_mnt_cnt;                      /* mounts since fsck */
145         uint16_t                                        s_max_mnt_cnt;          /* mounts between fscks */
146         uint16_t                                        s_magic;
147         uint16_t                                        s_state;                        /* mount state */
148         uint16_t                                        s_errors;                       /* what to do on error */
149         uint16_t                                        s_minor_rev_level;
150         uint32_t                                        s_lastcheck;            /* last fsck */
151         uint32_t                                        s_checkinterval;        /* max time btw fscks */
152         uint32_t                                        s_creator_os;
153         uint32_t                                        s_rev_level;
154         uint16_t                                        s_def_resuid;           /* uid for reserved blocks*/
155         uint16_t                                        s_def_resgid;           /* gid for reserved blocks*/
156 /* Next chunk, EXT2_DYNAMIC_REV specific */
157         uint32_t                                        s_first_ino;            /* first usable for F_REG */
158         uint16_t                                        s_inode_size;
159         uint16_t                                        s_block_group_nr;       /* BG holding *this* SB */
160         uint32_t                                        s_feature_compat;
161         uint32_t                                        s_feature_incompat;
162         uint32_t                                        s_feature_ro_compat;
163         uint64_t                                        s_uuid[2];                      /* volume id */
164         char                                            s_volume_name[16];      /* null terminated */
165         char                                            s_last_mounted[64];     /* dir path of mount */
166         uint32_t                                        s_algo_bitmap;          /* compression type */
167 /* Next chunk, Performance Hints */
168         uint8_t                                         s_prealloc_blocks;      /* when making F_REG */
169         uint8_t                                         s_prealloc_dir_blocks;  /* when making F_DIR */
170         uint16_t                                        s_padding1;
171 /* Next chunk, Journaling Support */
172         uint8_t                                         s_journal_uuid[16];
173         uint32_t                                        s_journal_inum;         /* ino of the journal file*/
174         uint32_t                                        s_journal_dev;          /* device num of journal */
175         uint32_t                                        s_last_orphan;          /* first in list to delete*/
176 /* Next chunk, Directory Indexing Support */
177         uint32_t                                        s_hash_seed[4];         /* for directory indexing */
178         uint8_t                                         s_def_hash_version;     /* default */
179         uint8_t                                         s_padding2;
180         uint16_t                                        s_padding3;
181 /* Next chunk, Other options */
182         uint32_t                                        s_default_mount_opts;
183         uint32_t                                        s_first_meta_bg;        /* BG id of first meta */
184         uint8_t                                         s_reserved[760];
185 };
186
187 /* All block ids are absolute (not relative to the BG). */
188 struct ext2_block_group {
189         uint32_t                                        bg_block_bitmap;        /* block id of bitmap */
190         uint32_t                                        bg_inode_bitmap;        /* block id of bitmap */
191         uint32_t                                        bg_inode_table;         /* id of first block */
192         uint16_t                                        bg_free_blocks_cnt;
193         uint16_t                                        bg_free_inodes_cnt;
194         uint16_t                                        bg_used_dirs_cnt;       /* inodes alloc to dirs */
195         uint16_t                                        bg_padding;
196         uint8_t                                         bg_reserved[12];
197 };
198
199 /* Random note: expect some number of the initial inodes to be reserved (11 in
200  * rev 0), and later versions just start from s_first_ino.
201  *
202  * Only regular files use the full 32bits of size.  Rev 0 uses a signed i_size.
203  *
204  * For the i_blocks, the first 12 are block numbers for direct blocks.  The
205  * 13th entry ([12]) is the block number for the first indirect block.  The
206  * 14th entry is the number for a doubly-indirect block, and the 15th is a
207  * triply indirect block. Having a value of 0 in the array terminates it, with
208  * no further blocks. (not clear how that works with holes)
209  *
210  * For the osd2[12], these should be unused for ext2, and they should be used
211  * differently for ext4.
212  *
213  * Giant warning: the inode numbers start at 1 in the system, not 0! */
214 struct ext2_inode {
215         uint16_t                                        i_mode;                         /* file type and mode */
216         uint16_t                                        i_uid;
217         uint32_t                                        i_size;                         /* lower 32bits of size */
218         uint32_t                                        i_atime;
219         uint32_t                                        i_ctime;
220         uint32_t                                        i_mtime;
221         uint32_t                                        i_dtime;                        /* delete time */
222         uint16_t                                        i_gid;
223         uint16_t                                        i_links_cnt;            /* fs_ino->i_nlink */
224         uint32_t                                        i_blocks;                       /* num blocks reserved */
225         uint32_t                                        i_flags;                        /* how to access data */
226         uint32_t                                        i_osd1;                         /* OS dependent */
227         uint32_t                                        i_block[15];            /* list of blocks reserved*/
228         uint32_t                                        i_generation;           /* used by NFS */
229         uint32_t                                        i_file_acl;                     /* block num hold ext attr*/
230         uint32_t                                        i_dir_acl;                      /* upper 32bits of size */
231         uint32_t                                        i_faddr;                        /* fragment, obsolete. */
232         uint8_t                                         i_osd2[12];                     /* OS dependent */
233 };
234
235 /* a dir_inode of 0 means an unused entry.  reclen will go to the end of the
236  * data block when it is the last entry.  These are 4-byte aligned on disk. */
237 struct ext2_dirent {
238         uint32_t                                        dir_inode;                      /* inode */
239         uint16_t                                        dir_reclen;                     /* len, including padding */
240         uint8_t                                         dir_namelen;            /* len of dir_name w/o \0 */
241         uint8_t                                         dir_filetype;
242         uint8_t                                         dir_name[256];          /* might be < 255 on disc */
243 };
244
245 /* Every FS must extern it's type, and be included in vfs_init() */
246 extern struct fs_type ext2_fs_type;
247
248 /* This hangs off the VFS's SB, and tracks in-memory copies of the disc SB and
249  * the block group descriptor table.  For now, s_dirty (VFS) will track the
250  * dirtiness of all things hanging off the sb.  Both of the objects contained
251  * are kmalloc()d, as is this struct. */
252 struct ext2_sb_info {
253         struct ext2_sb                          *e2sb;
254         struct ext2_block_group         *e2bg;
255         unsigned int                            nr_bgs;
256 };
257
258 /* Inode in-memory data.  This stuff is in cpu-native endianness.  If we start
259  * using the data in the actual inode and in the buffer cache, change
260  * ext2_my_bh() and its two callers.  Assume this data is dirty. */
261 struct ext2_i_info {
262         uint32_t                                        i_block[15];            /* list of blocks reserved*/
263 };