1*6a20f05bSchristos /* $NetBSD: ext2fs_dinode.h,v 1.37 2017/01/13 18:04:36 christos Exp $ */
29e3c291dSbouyer
39e3c291dSbouyer /*
49e3c291dSbouyer * Copyright (c) 1982, 1989, 1993
59e3c291dSbouyer * The Regents of the University of California. All rights reserved.
69e3c291dSbouyer * (c) UNIX System Laboratories, Inc.
79e3c291dSbouyer * All or some portions of this file are derived from material licensed
89e3c291dSbouyer * to the University of California by American Telephone and Telegraph
99e3c291dSbouyer * Co. or Unix System Laboratories, Inc. and are reproduced herein with
109e3c291dSbouyer * the permission of UNIX System Laboratories, Inc.
119e3c291dSbouyer *
129e3c291dSbouyer * Redistribution and use in source and binary forms, with or without
139e3c291dSbouyer * modification, are permitted provided that the following conditions
149e3c291dSbouyer * are met:
159e3c291dSbouyer * 1. Redistributions of source code must retain the above copyright
169e3c291dSbouyer * notice, this list of conditions and the following disclaimer.
179e3c291dSbouyer * 2. Redistributions in binary form must reproduce the above copyright
189e3c291dSbouyer * notice, this list of conditions and the following disclaimer in the
199e3c291dSbouyer * documentation and/or other materials provided with the distribution.
20aad01611Sagc * 3. Neither the name of the University nor the names of its contributors
21aad01611Sagc * may be used to endorse or promote products derived from this software
22aad01611Sagc * without specific prior written permission.
23aad01611Sagc *
24aad01611Sagc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25aad01611Sagc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26aad01611Sagc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27aad01611Sagc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28aad01611Sagc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29aad01611Sagc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30aad01611Sagc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31aad01611Sagc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32aad01611Sagc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33aad01611Sagc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34aad01611Sagc * SUCH DAMAGE.
35aad01611Sagc *
36aad01611Sagc * @(#)dinode.h 8.6 (Berkeley) 9/13/94
37aad01611Sagc * Modified for ext2fs by Manuel Bouyer.
38aad01611Sagc */
39aad01611Sagc
40aad01611Sagc /*
41aad01611Sagc * Copyright (c) 1997 Manuel Bouyer.
42aad01611Sagc *
43aad01611Sagc * Redistribution and use in source and binary forms, with or without
44aad01611Sagc * modification, are permitted provided that the following conditions
45aad01611Sagc * are met:
46aad01611Sagc * 1. Redistributions of source code must retain the above copyright
47aad01611Sagc * notice, this list of conditions and the following disclaimer.
48aad01611Sagc * 2. Redistributions in binary form must reproduce the above copyright
49aad01611Sagc * notice, this list of conditions and the following disclaimer in the
50aad01611Sagc * documentation and/or other materials provided with the distribution.
519e3c291dSbouyer *
5235decc6eSbouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
5335decc6eSbouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
5435decc6eSbouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
5535decc6eSbouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
5635decc6eSbouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
5735decc6eSbouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
5835decc6eSbouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5935decc6eSbouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
6035decc6eSbouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
6135decc6eSbouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
629e3c291dSbouyer *
639e3c291dSbouyer * @(#)dinode.h 8.6 (Berkeley) 9/13/94
649e3c291dSbouyer * Modified for ext2fs by Manuel Bouyer.
659e3c291dSbouyer */
669e3c291dSbouyer
6760db16d1Smatt #ifndef _UFS_EXT2FS_EXT2FS_DINODE_H_
6860db16d1Smatt #define _UFS_EXT2FS_EXT2FS_DINODE_H_
6960db16d1Smatt
70c3d0e086Snonaka #if !defined(_KERNEL) && !defined(_STANDALONE)
71c3d0e086Snonaka #include <stddef.h> /* for offsetof */
72c3d0e086Snonaka #endif
73c3d0e086Snonaka
749e3c291dSbouyer #include <sys/stat.h>
759e3c291dSbouyer
769e3c291dSbouyer /*
779e3c291dSbouyer * The root inode is the root of the file system. Inode 0 can't be used for
789e3c291dSbouyer * normal purposes and bad blocks are normally linked to inode 1, thus
799e3c291dSbouyer * the root inode is 2.
809e3c291dSbouyer * Inode 3 to 10 are reserved in ext2fs.
819e3c291dSbouyer */
825517f8a4Stsutsui #define EXT2_BADBLKINO ((ino_t)1)
839e3c291dSbouyer #define EXT2_ROOTINO ((ino_t)2)
845517f8a4Stsutsui #define EXT2_ACLIDXINO ((ino_t)3)
855517f8a4Stsutsui #define EXT2_ACLDATAINO ((ino_t)4)
865517f8a4Stsutsui #define EXT2_BOOTLOADERINO ((ino_t)5)
875517f8a4Stsutsui #define EXT2_UNDELDIRINO ((ino_t)6)
880192f3ddStsutsui #define EXT2_RESIZEINO ((ino_t)7)
895517f8a4Stsutsui #define EXT2_JOURNALINO ((ino_t)8)
909e3c291dSbouyer #define EXT2_FIRSTINO ((ino_t)11)
919e3c291dSbouyer
929e3c291dSbouyer /*
939e3c291dSbouyer * A dinode contains all the meta-data associated with a UFS file.
949e3c291dSbouyer * This structure defines the on-disk format of a dinode. Since
959e3c291dSbouyer * this structure describes an on-disk structure, all its fields
969e3c291dSbouyer * are defined by types with precise widths.
979e3c291dSbouyer */
989e3c291dSbouyer
99dcd34a91Sdholland /*
100dcd34a91Sdholland * XXX these are the same values as UFS_NDADDR/UFS_NIADDR and it is
101dcd34a91Sdholland * far from clear that there isn't code that relies on them being the
102dcd34a91Sdholland * same.
103dcd34a91Sdholland */
104dcd34a91Sdholland #define EXT2FS_NDADDR 12 /* Direct addresses in inode. */
105dcd34a91Sdholland #define EXT2FS_NIADDR 3 /* Indirect addresses in inode. */
1069e3c291dSbouyer
107dcd34a91Sdholland #define EXT2_MAXSYMLINKLEN ((EXT2FS_NDADDR+EXT2FS_NIADDR) * sizeof (uint32_t))
108bddc3c12Sjdolecek #define E2MAXSYMLINKLEN EXT2_MAXSYMLINKLEN
1099e3c291dSbouyer
1109e3c291dSbouyer struct ext2fs_dinode {
111a811b3a6Stsutsui uint16_t e2di_mode; /* 0: IFMT, permissions; see below. */
112a811b3a6Stsutsui uint16_t e2di_uid; /* 2: Owner UID */
113a811b3a6Stsutsui uint32_t e2di_size; /* 4: Size (in bytes) */
1141703ea99Sjakllsch uint32_t e2di_atime; /* 8: Access time */
115a811b3a6Stsutsui uint32_t e2di_ctime; /* 12: Create time */
116a811b3a6Stsutsui uint32_t e2di_mtime; /* 16: Modification time */
117a811b3a6Stsutsui uint32_t e2di_dtime; /* 20: Deletion time */
118a811b3a6Stsutsui uint16_t e2di_gid; /* 24: Owner GID */
119a811b3a6Stsutsui uint16_t e2di_nlink; /* 26: File link count */
120a811b3a6Stsutsui uint32_t e2di_nblock; /* 28: Blocks count */
121a811b3a6Stsutsui uint32_t e2di_flags; /* 32: Status flags (chflags) */
12259134e0eSjakllsch uint32_t e2di_version; /* 36: was reserved1 */
123dcd34a91Sdholland uint32_t e2di_blocks[EXT2FS_NDADDR+EXT2FS_NIADDR];
124dcd34a91Sdholland /* 40: disk blocks */
125a811b3a6Stsutsui uint32_t e2di_gen; /* 100: generation number */
1262058250bSjdolecek uint32_t e2di_facl; /* 104: file ACL (ext3) */
127249e8f5fSjdolecek uint32_t e2di_size_high; /* 108: Size (in bytes) high */
1288601c2c3Sjdolecek uint32_t e2di_obso_faddr;/* 112: obsolete fragment address (ext2) */
1298601c2c3Sjdolecek uint16_t e2di_nblock_high; /* 116: Blocks count bits 47:32 (ext4) */
1302058250bSjdolecek uint16_t e2di_facl_high; /* 118: file ACL bits 47:32 (ext4/64bit) */
1318601c2c3Sjdolecek uint16_t e2di_uid_high; /* 120: Owner UID top 16 bits (ext4) */
1328601c2c3Sjdolecek uint16_t e2di_gid_high; /* 122: Owner GID top 16 bits (ext4) */
13396675501Sjdolecek uint16_t e2di_checksum_low; /* 124: crc LE (not implemented) (ext4) */
13496675501Sjdolecek uint16_t e2di_reserved; /* 126: reserved */
13596675501Sjdolecek uint16_t e2di_extra_isize; /* 128: inode extra size (over 128) actually used (ext4) */
13696675501Sjdolecek uint16_t e2di_checksum_high; /* 130: crc BE (not implemented) (ext4) */
13796675501Sjdolecek uint32_t e2di_ctime_extra; /* 132: ctime (nsec << 2 | high epoch) (ext4) */
13896675501Sjdolecek uint32_t e2di_mtime_extra; /* 136: mtime (nsec << 2 | high epoch) (ext4) */
13996675501Sjdolecek uint32_t e2di_atime_extra; /* 140: atime (nsec << 2 | high epoch) (ext4) */
14096675501Sjdolecek uint32_t e2di_crtime; /* 144: creation time (epoch) (ext4) */
14196675501Sjdolecek uint32_t e2di_crtime_extra; /* 148: creation time (nsec << 2 | high epoch) (ext4) */
14296675501Sjdolecek uint32_t e2di_version_high; /* 152: version high (ext4) */
14396675501Sjdolecek uint32_t e2di_projid; /* 156: project id (not implemented) (ext4) */
1449e3c291dSbouyer };
1459e3c291dSbouyer
1462f2ae027Sjdolecek #define i_e2fs_mode i_din.e2fs_din->e2di_mode
1472f2ae027Sjdolecek #define i_e2fs_uid i_din.e2fs_din->e2di_uid
1482f2ae027Sjdolecek #define i_e2fs_size i_din.e2fs_din->e2di_size
1492f2ae027Sjdolecek #define i_e2fs_atime i_din.e2fs_din->e2di_atime
1502f2ae027Sjdolecek #define i_e2fs_ctime i_din.e2fs_din->e2di_ctime
1512f2ae027Sjdolecek #define i_e2fs_mtime i_din.e2fs_din->e2di_mtime
1522f2ae027Sjdolecek #define i_e2fs_dtime i_din.e2fs_din->e2di_dtime
1532f2ae027Sjdolecek #define i_e2fs_gid i_din.e2fs_din->e2di_gid
1542f2ae027Sjdolecek #define i_e2fs_nlink i_din.e2fs_din->e2di_nlink
1552f2ae027Sjdolecek #define i_e2fs_nblock i_din.e2fs_din->e2di_nblock
1562f2ae027Sjdolecek #define i_e2fs_flags i_din.e2fs_din->e2di_flags
1572f2ae027Sjdolecek #define i_e2fs_version i_din.e2fs_din->e2di_version
1582f2ae027Sjdolecek #define i_e2fs_blocks i_din.e2fs_din->e2di_blocks
1592f2ae027Sjdolecek #define i_e2fs_rdev i_din.e2fs_din->e2di_rdev
1602f2ae027Sjdolecek #define i_e2fs_gen i_din.e2fs_din->e2di_gen
1612f2ae027Sjdolecek #define i_e2fs_facl i_din.e2fs_din->e2di_facl
1622f2ae027Sjdolecek #define i_e2fs_nblock_high i_din.e2fs_din->e2di_nblock_high
1632f2ae027Sjdolecek #define i_e2fs_facl_high i_din.e2fs_din->e2di_facl_high
1642f2ae027Sjdolecek #define i_e2fs_uid_high i_din.e2fs_din->e2di_uid_high
1652f2ae027Sjdolecek #define i_e2fs_gid_high i_din.e2fs_din->e2di_gid_high
1662f2ae027Sjdolecek
1679e3c291dSbouyer /* File permissions. */
1689e3c291dSbouyer #define EXT2_IEXEC 0000100 /* Executable. */
1691035faffSwiz #define EXT2_IWRITE 0000200 /* Writable. */
1709e3c291dSbouyer #define EXT2_IREAD 0000400 /* Readable. */
1719e3c291dSbouyer #define EXT2_ISVTX 0001000 /* Sticky bit. */
1729e3c291dSbouyer #define EXT2_ISGID 0002000 /* Set-gid. */
1739e3c291dSbouyer #define EXT2_ISUID 0004000 /* Set-uid. */
1749e3c291dSbouyer
1759e3c291dSbouyer /* File types. */
1769e3c291dSbouyer #define EXT2_IFMT 0170000 /* Mask of file type. */
1779e3c291dSbouyer #define EXT2_IFIFO 0010000 /* Named pipe (fifo). */
1789e3c291dSbouyer #define EXT2_IFCHR 0020000 /* Character device. */
1799e3c291dSbouyer #define EXT2_IFDIR 0040000 /* Directory file. */
1809e3c291dSbouyer #define EXT2_IFBLK 0060000 /* Block device. */
1819e3c291dSbouyer #define EXT2_IFREG 0100000 /* Regular file. */
1829e3c291dSbouyer #define EXT2_IFLNK 0120000 /* Symbolic link. */
1839e3c291dSbouyer #define EXT2_IFSOCK 0140000 /* UNIX domain socket. */
1849e3c291dSbouyer
1859e3c291dSbouyer /* file flags */
1869e3c291dSbouyer #define EXT2_SECRM 0x00000001 /* Secure deletion */
1879e3c291dSbouyer #define EXT2_UNRM 0x00000002 /* Undelete */
1889e3c291dSbouyer #define EXT2_COMPR 0x00000004 /* Compress file */
1899e3c291dSbouyer #define EXT2_SYNC 0x00000008 /* Synchronous updates */
1909e3c291dSbouyer #define EXT2_IMMUTABLE 0x00000010 /* Immutable file */
1919e3c291dSbouyer #define EXT2_APPEND 0x00000020 /* writes to file may only append */
1929e3c291dSbouyer #define EXT2_NODUMP 0x00000040 /* do not dump file */
19326f3fbc5Sjakllsch #define EXT2_NOATIME 0x00000080 /* do not update atime */
19426f3fbc5Sjakllsch #define EXT2_INDEX 0x00001000 /* hash-indexed directory */
19526f3fbc5Sjakllsch #define EXT2_IMAGIC 0x00002000 /* AFS directory */
19626f3fbc5Sjakllsch #define EXT2_JOURNAL_DATA 0x00004000 /* file data should be journaled */
19726f3fbc5Sjakllsch #define EXT2_NOTAIL 0x00008000 /* file tail should not be merged */
19826f3fbc5Sjakllsch #define EXT2_DIRSYNC 0x00010000 /* dirsync behaviour */
19926f3fbc5Sjakllsch #define EXT2_TOPDIR 0x00020000 /* Top of directory hierarchies*/
20026f3fbc5Sjakllsch #define EXT2_HUGE_FILE 0x00040000 /* Set to each huge file */
20126f3fbc5Sjakllsch #define EXT2_EXTENTS 0x00080000 /* Inode uses extents */
202118d3ec7Sjdolecek #define EXT2_EA_INODE 0x00200000 /* Inode used for large EA */
20326f3fbc5Sjakllsch #define EXT2_EOFBLOCKS 0x00400000 /* Blocks allocated beyond EOF */
204118d3ec7Sjdolecek #define EXT2_INLINE_DATA 0x10000000 /* Inode has inline data */
205118d3ec7Sjdolecek #define EXT2_PROJINHERIT 0x20000000 /* Children inherit project ID */
2069e3c291dSbouyer
207978c3d97Sthorpej /* Size of on-disk inode. */
208*6a20f05bSchristos #define EXT2_REV0_DINODE_SIZE 128U
209d0939ef4Stsutsui #define EXT2_DINODE_SIZE(fs) ((fs)->e2fs.e2fs_rev > E2FS_REV0 ? \
210d0939ef4Stsutsui (fs)->e2fs.e2fs_inode_size : \
211d0939ef4Stsutsui EXT2_REV0_DINODE_SIZE)
21296675501Sjdolecek #define EXT2_DINODE_FITS(dinode, field, isize) (\
21396675501Sjdolecek (isize > EXT2_REV0_DINODE_SIZE) \
21496675501Sjdolecek && ((EXT2_REV0_DINODE_SIZE + (dinode)->e2di_extra_isize) >= offsetof(struct ext2fs_dinode, field) + sizeof((dinode)->field)) \
21596675501Sjdolecek )
216978c3d97Sthorpej
2179e3c291dSbouyer /*
218fa408750Sjdolecek * Time encoding
219fa408750Sjdolecek * Lower two bits of extra field are extra high bits for epoch; unfortunately still, Linux kernels treat 11 there as 00 for compatibility
220fa408750Sjdolecek * Rest of extra fields are nanoseconds
221fa408750Sjdolecek */
222fa408750Sjdolecek static __inline void
ext2fs_dinode_time_get(struct timespec * ts,uint32_t epoch,uint32_t extra)223fa408750Sjdolecek ext2fs_dinode_time_get(struct timespec *ts, uint32_t epoch, uint32_t extra)
224fa408750Sjdolecek {
225fa408750Sjdolecek ts->tv_sec = (signed) epoch;
226fa408750Sjdolecek
227fa408750Sjdolecek if (extra) {
228fa408750Sjdolecek uint64_t epoch_bits = extra & 0x3;
229fa408750Sjdolecek /* XXX compatibility with linux kernel < 4.20 */
230fa408750Sjdolecek if (epoch_bits == 3 && ts->tv_sec < 0)
231fa408750Sjdolecek epoch_bits = 0;
232fa408750Sjdolecek
233fa408750Sjdolecek ts->tv_sec |= epoch_bits << 32;
234fa408750Sjdolecek
235fa408750Sjdolecek ts->tv_nsec = extra >> 2;
236fa408750Sjdolecek } else {
237fa408750Sjdolecek ts->tv_nsec = 0;
238fa408750Sjdolecek }
239fa408750Sjdolecek }
240fa408750Sjdolecek #define EXT2_DINODE_TIME_GET(ts, dinode, field, isize) \
241fa408750Sjdolecek ext2fs_dinode_time_get(ts, (dinode)->field, \
242fa408750Sjdolecek EXT2_DINODE_FITS(dinode, field ## _extra, isize) \
243fa408750Sjdolecek ? (dinode)->field ## _extra : 0 \
244fa408750Sjdolecek )
245fa408750Sjdolecek
246fa408750Sjdolecek static __inline void
ext2fs_dinode_time_set(const struct timespec * ts,uint32_t * epoch,uint32_t * extra)247fa408750Sjdolecek ext2fs_dinode_time_set(const struct timespec *ts, uint32_t *epoch, uint32_t *extra)
248fa408750Sjdolecek {
249fa408750Sjdolecek *epoch = (int32_t) ts->tv_sec;
250fa408750Sjdolecek
251fa408750Sjdolecek if (extra) {
252fa408750Sjdolecek uint32_t epoch_bits = (ts->tv_sec >> 32) & 0x3;
253fa408750Sjdolecek
254fa408750Sjdolecek *extra = (ts->tv_nsec << 2) | epoch_bits;
255fa408750Sjdolecek }
256fa408750Sjdolecek }
257fa408750Sjdolecek #define EXT2_DINODE_TIME_SET(ts, dinode, field, isize) \
258fa408750Sjdolecek ext2fs_dinode_time_set(ts, &(dinode)->field, \
259fa408750Sjdolecek EXT2_DINODE_FITS(dinode, field ## _extra, isize) \
260fa408750Sjdolecek ? &(dinode)->field ## _extra : NULL \
261fa408750Sjdolecek )
262fa408750Sjdolecek
263fa408750Sjdolecek /*
2649e3c291dSbouyer * The e2di_blocks fields may be overlaid with other information for
2659e3c291dSbouyer * file types that do not have associated disk storage. Block
2669e3c291dSbouyer * and character devices overlay the first data block with their
2679e3c291dSbouyer * dev_t value. Short symbolic links place their path in the
2689e3c291dSbouyer * di_db area.
2699e3c291dSbouyer */
2709e3c291dSbouyer
2719e3c291dSbouyer #define e2di_rdev e2di_blocks[0]
2729e3c291dSbouyer #define e2di_shortlink e2di_blocks
2736ab3092bSbouyer
2746ab3092bSbouyer /* e2fs needs byte swapping on big-endian systems */
2756ab3092bSbouyer #if BYTE_ORDER == LITTLE_ENDIAN
27696675501Sjdolecek # define e2fs_iload(old, new, isize) \
27796675501Sjdolecek memcpy((new),(old),(isize))
27896675501Sjdolecek # define e2fs_isave(old, new, isize) \
27996675501Sjdolecek memcpy((new),(old),(isize))
2806ab3092bSbouyer #else
28196675501Sjdolecek void e2fs_i_bswap(struct ext2fs_dinode *, struct ext2fs_dinode *, size_t);
28296da721fSnonaka # define e2fs_iload(old, new, isize) e2fs_i_bswap((old), (new), (isize))
28396da721fSnonaka # define e2fs_isave(old, new, isize) e2fs_i_bswap((old), (new), (isize))
2846ab3092bSbouyer #endif
28560db16d1Smatt
28660db16d1Smatt #endif /* !_UFS_EXT2FS_EXT2FS_DINODE_H_ */
287