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