2 * SPDX-License-Identifier: MIT-CMU
4 * Copyright (c) 1995 The University of Utah and
5 * the Computer Systems Laboratory at the University of Utah (CSL).
8 * Permission to use, copy, modify and distribute this software is hereby
9 * granted provided that (1) source code retains these copyright, permission,
10 * and disclaimer notices, and (2) redistributions including binaries
11 * reproduce the notices in supporting documentation, and (3) all advertising
12 * materials mentioning features or use of this software display the following
13 * acknowledgement: ``This product includes software developed by the
14 * Computer Systems Laboratory at the University of Utah.''
16 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
17 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
18 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
20 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
21 * improvements that they make and grant CSL redistribution rights.
28 * routines to convert on disk ext2 inodes into inodes and back
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/endian.h>
35 #include <sys/vnode.h>
37 #include <vfs/ext2fs/fs.h>
38 #include <vfs/ext2fs/inode.h>
39 #include <vfs/ext2fs/ext2fs.h>
40 #include <vfs/ext2fs/ext2_dinode.h>
41 #include <vfs/ext2fs/ext2_extern.h>
43 SDT_PROVIDER_DECLARE(ext2fs);
46 * arg0: verbosity. Higher numbers give more verbose messages
47 * arg1: Textual message
49 SDT_PROBE_DEFINE2(ext2fs, , trace, inode_cnv, "int", "char*");
51 #ifdef EXT2FS_PRINT_EXTENTS
53 ext2_print_inode(struct inode *in)
56 struct ext4_extent_header *ehp;
57 struct ext4_extent *ep;
59 printf("Inode: %5ju", (uintmax_t)in->i_number);
60 printf( /* "Inode: %5d" */
61 " Type: %10s Mode: 0x%o Flags: 0x%x Version: %d acl: 0x%jx\n",
62 "n/a", in->i_mode, in->i_flags, in->i_gen, in->i_facl);
63 printf("User: %5u Group: %5u Size: %ju\n",
64 in->i_uid, in->i_gid, (uintmax_t)in->i_size);
65 printf("Links: %3d Blockcount: %ju\n",
66 in->i_nlink, (uintmax_t)in->i_blocks);
67 printf("ctime: 0x%x ", in->i_ctime);
68 printf("atime: 0x%x ", in->i_atime);
69 printf("mtime: 0x%x ", in->i_mtime);
70 if (E2DI_HAS_XTIME(in))
71 printf("crtime %#x\n", in->i_birthtime);
74 if (in->i_flag & IN_E4EXTENTS) {
76 ehp = (struct ext4_extent_header *)in->i_db;
77 printf("Header (magic 0x%x entries %d max %d depth %d gen %d)\n",
78 le16toh(ehp->eh_magic), le16toh(ehp->eh_ecount),
79 le16toh(ehp->eh_max), le16toh(ehp->eh_depth),
80 le32toh(ehp->eh_gen));
81 ep = (struct ext4_extent *)(char *)(ehp + 1);
82 printf("Index (blk %d len %d start_lo %d start_hi %d)\n",
84 le16toh(ep->e_len), le32toh(ep->e_start_lo),
85 le16toh(ep->e_start_hi));
89 for (i = 0; i < (in->i_blocks <= 24 ? (in->i_blocks + 1) / 2 : 12); i++)
90 printf(" %d", in->i_db[i]);
94 #endif /* EXT2FS_PRINT_EXTENTS */
96 #define XTIME_TO_NSEC(x) ((le32toh(x) & EXT3_NSEC_MASK) >> 2)
99 * raw ext2 inode LE to host inode conversion
102 ext2_ei2i(struct ext2fs_dinode *ei, struct inode *ip)
104 struct m_ext2fs *fs = ip->i_e2fs;
105 uint32_t ei_flags_host;
106 uint16_t ei_extra_isize_le;
109 if ((ip->i_number < EXT2_FIRST_INO(fs) && ip->i_number != EXT2_ROOTINO) ||
110 (ip->i_number < EXT2_ROOTINO) ||
111 (ip->i_number > le32toh(fs->e2fs->e2fs_icount))) {
112 SDT_PROBE2(ext2fs, , trace, inode_cnv, 1, "bad inode number");
116 ip->i_nlink = le16toh(ei->e2di_nlink);
117 if (ip->i_number == EXT2_ROOTINO && ip->i_nlink == 0) {
118 SDT_PROBE2(ext2fs, , trace, inode_cnv, 1, "root inode unallocated");
122 /* Check extra inode size */
123 ei_extra_isize_le = le16toh(ei->e2di_extra_isize);
124 if (EXT2_INODE_SIZE(fs) > E2FS_REV0_INODE_SIZE) {
125 if (E2FS_REV0_INODE_SIZE + ei_extra_isize_le >
126 EXT2_INODE_SIZE(fs) || (ei_extra_isize_le & 3)) {
127 SDT_PROBE2(ext2fs, , trace, inode_cnv, 1,
128 "bad extra inode size");
134 * Godmar thinks - if the link count is zero, then the inode is
135 * unused - according to ext2 standards. Ufs marks this fact by
136 * setting i_mode to zero - why ? I can see that this might lead to
137 * problems in an undelete.
139 ip->i_mode = ip->i_nlink ? le16toh(ei->e2di_mode) : 0;
140 ip->i_size = le32toh(ei->e2di_size);
141 if (S_ISREG(ip->i_mode))
142 ip->i_size |= (uint64_t)le32toh(ei->e2di_size_high) << 32;
143 ip->i_atime = le32toh(ei->e2di_atime);
144 ip->i_mtime = le32toh(ei->e2di_mtime);
145 ip->i_ctime = le32toh(ei->e2di_ctime);
146 if (E2DI_HAS_XTIME(ip)) {
147 ip->i_atimensec = XTIME_TO_NSEC(ei->e2di_atime_extra);
148 ip->i_mtimensec = XTIME_TO_NSEC(ei->e2di_mtime_extra);
149 ip->i_ctimensec = XTIME_TO_NSEC(ei->e2di_ctime_extra);
150 ip->i_birthtime = le32toh(ei->e2di_crtime);
151 ip->i_birthnsec = XTIME_TO_NSEC(ei->e2di_crtime_extra);
154 ei_flags_host = le32toh(ei->e2di_flags);
155 ip->i_flags |= (ei_flags_host & EXT2_APPEND) ? SF_APPEND : 0;
156 ip->i_flags |= (ei_flags_host & EXT2_IMMUTABLE) ? SF_IMMUTABLE : 0;
157 ip->i_flags |= (ei_flags_host & EXT2_NODUMP) ? UF_NODUMP : 0;
158 ip->i_flag |= (ei_flags_host & EXT3_INDEX) ? IN_E3INDEX : 0;
159 ip->i_flag |= (ei_flags_host & EXT4_EXTENTS) ? IN_E4EXTENTS : 0;
160 ip->i_blocks = le32toh(ei->e2di_nblock);
161 ip->i_facl = le32toh(ei->e2di_facl);
162 if (E2DI_HAS_HUGE_FILE(ip)) {
163 ip->i_blocks |= (uint64_t)le16toh(ei->e2di_nblock_high) << 32;
164 ip->i_facl |= (uint64_t)le16toh(ei->e2di_facl_high) << 32;
165 if (ei_flags_host & EXT4_HUGE_FILE)
166 ip->i_blocks = fsbtodb(ip->i_e2fs, ip->i_blocks);
168 ip->i_gen = le32toh(ei->e2di_gen);
169 ip->i_uid = le16toh(ei->e2di_uid);
170 ip->i_gid = le16toh(ei->e2di_gid);
171 ip->i_uid |= (uint32_t)le16toh(ei->e2di_uid_high) << 16;
172 ip->i_gid |= (uint32_t)le16toh(ei->e2di_gid_high) << 16;
174 if ((ip->i_flag & IN_E4EXTENTS)) {
175 memcpy(ip->i_data, ei->e2di_blocks, sizeof(ei->e2di_blocks));
177 for (i = 0; i < EXT2_NDADDR; i++)
178 ip->i_db[i] = le32toh(ei->e2di_blocks[i]);
179 for (i = 0; i < EXT2_NIADDR; i++)
180 ip->i_ib[i] = le32toh(ei->e2di_blocks[EXT2_NDIR_BLOCKS + i]);
183 /* Verify inode csum. */
184 return (ext2_ei_csum_verify(ip, ei));
187 #define NSEC_TO_XTIME(t) (htole32((t << 2) & EXT3_NSEC_MASK))
190 * inode to raw ext2 LE inode conversion
193 ext2_i2ei(struct inode *ip, struct ext2fs_dinode *ei)
199 ei->e2di_mode = htole16(ip->i_mode);
200 ei->e2di_nlink = htole16(ip->i_nlink);
201 ei->e2di_size = htole32(ip->i_size);
202 if (S_ISREG(ip->i_mode))
203 ei->e2di_size_high = htole32(ip->i_size >> 32);
204 ei->e2di_atime = htole32(ip->i_atime);
205 ei->e2di_mtime = htole32(ip->i_mtime);
206 ei->e2di_ctime = htole32(ip->i_ctime);
208 * Godmar thinks: if dtime is nonzero, ext2 says this inode has been
209 * deleted, this would correspond to a zero link count
211 ei->e2di_dtime = htole32(le16toh(ei->e2di_nlink) ? 0 :
212 le32toh(ei->e2di_mtime));
213 if (E2DI_HAS_XTIME(ip)) {
214 ei->e2di_ctime_extra = NSEC_TO_XTIME(ip->i_ctimensec);
215 ei->e2di_mtime_extra = NSEC_TO_XTIME(ip->i_mtimensec);
216 ei->e2di_atime_extra = NSEC_TO_XTIME(ip->i_atimensec);
217 ei->e2di_crtime = htole32(ip->i_birthtime);
218 ei->e2di_crtime_extra = NSEC_TO_XTIME(ip->i_birthnsec);
220 /* Keep these in host endian for a while since they change a lot */
222 ei->e2di_flags |= htole32((ip->i_flags & SF_APPEND) ? EXT2_APPEND : 0);
223 ei->e2di_flags |= htole32((ip->i_flags & SF_IMMUTABLE) ? EXT2_IMMUTABLE : 0);
224 ei->e2di_flags |= htole32((ip->i_flags & UF_NODUMP) ? EXT2_NODUMP : 0);
225 ei->e2di_flags |= htole32((ip->i_flag & IN_E3INDEX) ? EXT3_INDEX : 0);
226 ei->e2di_flags |= htole32((ip->i_flag & IN_E4EXTENTS) ? EXT4_EXTENTS : 0);
227 if (ip->i_blocks > ~0U &&
228 !EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_HUGE_FILE)) {
229 SDT_PROBE2(ext2fs, , trace, inode_cnv, 1, "i_blocks value is out of range");
232 if (ip->i_blocks <= 0xffffffffffffULL) {
233 ei->e2di_nblock = htole32(ip->i_blocks & 0xffffffff);
234 ei->e2di_nblock_high = htole16(ip->i_blocks >> 32 & 0xffff);
236 ei->e2di_flags |= htole32(EXT4_HUGE_FILE);
237 ei->e2di_nblock = htole32(dbtofsb(fs, ip->i_blocks));
238 ei->e2di_nblock_high = htole16(dbtofsb(fs, ip->i_blocks) >> 32 & 0xffff);
241 ei->e2di_facl = htole32(ip->i_facl & 0xffffffff);
242 ei->e2di_facl_high = htole16(ip->i_facl >> 32 & 0xffff);
243 ei->e2di_gen = htole32(ip->i_gen);
244 ei->e2di_uid = htole16(ip->i_uid & 0xffff);
245 ei->e2di_uid_high = htole16(ip->i_uid >> 16 & 0xffff);
246 ei->e2di_gid = htole16(ip->i_gid & 0xffff);
247 ei->e2di_gid_high = htole16(ip->i_gid >> 16 & 0xffff);
249 if ((ip->i_flag & IN_E4EXTENTS)) {
250 memcpy(ei->e2di_blocks, ip->i_data, sizeof(ei->e2di_blocks));
252 for (i = 0; i < EXT2_NDADDR; i++)
253 ei->e2di_blocks[i] = htole32(ip->i_db[i]);
254 for (i = 0; i < EXT2_NIADDR; i++)
255 ei->e2di_blocks[EXT2_NDIR_BLOCKS + i] = htole32(ip->i_ib[i]);
258 /* Set inode csum. */
259 ext2_ei_csum_set(ip, ei);