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>
36 #include <sys/vnode.h>
38 #include <fs/ext2fs/fs.h>
39 #include <fs/ext2fs/inode.h>
40 #include <fs/ext2fs/ext2fs.h>
41 #include <fs/ext2fs/ext2_dinode.h>
42 #include <fs/ext2fs/ext2_extern.h>
44 SDT_PROVIDER_DECLARE(ext2fs);
47 * arg0: verbosity. Higher numbers give more verbose messages
48 * arg1: Textual message
50 SDT_PROBE_DEFINE2(ext2fs, , trace, inode_cnv, "int", "char*");
52 #define XTIME_TO_NSEC(x) ((x & EXT3_NSEC_MASK) >> 2)
53 #define NSEC_TO_XTIME(t) (le32toh(t << 2) & EXT3_NSEC_MASK)
55 #ifdef EXT2FS_PRINT_EXTENTS
57 ext2_print_inode(struct inode *in)
60 struct ext4_extent_header *ehp;
61 struct ext4_extent *ep;
63 printf("Inode: %5ju", (uintmax_t)in->i_number);
64 printf( /* "Inode: %5d" */
65 " Type: %10s Mode: 0x%o Flags: 0x%x Version: %d acl: 0x%jx\n",
66 "n/a", in->i_mode, in->i_flags, in->i_gen, in->i_facl);
67 printf("User: %5u Group: %5u Size: %ju\n",
68 in->i_uid, in->i_gid, (uintmax_t)in->i_size);
69 printf("Links: %3d Blockcount: %ju\n",
70 in->i_nlink, (uintmax_t)in->i_blocks);
71 printf("ctime: 0x%x ", in->i_ctime);
72 printf("atime: 0x%x ", in->i_atime);
73 printf("mtime: 0x%x ", in->i_mtime);
74 if (E2DI_HAS_XTIME(in))
75 printf("crtime %#x\n", in->i_birthtime);
78 if (in->i_flag & IN_E4EXTENTS) {
80 ehp = (struct ext4_extent_header *)in->i_db;
81 printf("Header (magic 0x%x entries %d max %d depth %d gen %d)\n",
82 ehp->eh_magic, ehp->eh_ecount, ehp->eh_max, ehp->eh_depth,
84 ep = (struct ext4_extent *)(char *)(ehp + 1);
85 printf("Index (blk %d len %d start_lo %d start_hi %d)\n", ep->e_blk,
86 ep->e_len, ep->e_start_lo, ep->e_start_hi);
90 for (i = 0; i < (in->i_blocks <= 24 ? (in->i_blocks + 1) / 2 : 12); i++)
91 printf(" %d", in->i_db[i]);
95 #endif /* EXT2FS_PRINT_EXTENTS */
98 * raw ext2 inode to inode
101 ext2_ei2i(struct ext2fs_dinode *ei, struct inode *ip)
103 struct m_ext2fs *fs = ip->i_e2fs;
105 if ((ip->i_number < EXT2_FIRST_INO(fs) && ip->i_number != EXT2_ROOTINO) ||
106 (ip->i_number < EXT2_ROOTINO) ||
107 (ip->i_number > fs->e2fs->e2fs_icount)) {
108 SDT_PROBE2(ext2fs, , trace, inode_cnv, 1, "bad inode number");
112 if (ip->i_number == EXT2_ROOTINO && ei->e2di_nlink == 0) {
113 SDT_PROBE2(ext2fs, , trace, inode_cnv, 1, "root inode unallocated");
116 ip->i_nlink = ei->e2di_nlink;
118 /* Check extra inode size */
119 if (EXT2_INODE_SIZE(fs) > E2FS_REV0_INODE_SIZE) {
120 if (E2FS_REV0_INODE_SIZE + ei->e2di_extra_isize >
121 EXT2_INODE_SIZE(fs) || (ei->e2di_extra_isize & 3)) {
122 SDT_PROBE2(ext2fs, , trace, inode_cnv, 1,
123 "bad extra inode size");
129 * Godmar thinks - if the link count is zero, then the inode is
130 * unused - according to ext2 standards. Ufs marks this fact by
131 * setting i_mode to zero - why ? I can see that this might lead to
132 * problems in an undelete.
134 ip->i_mode = ei->e2di_nlink ? ei->e2di_mode : 0;
135 ip->i_size = ei->e2di_size;
136 if (S_ISREG(ip->i_mode))
137 ip->i_size |= ((u_int64_t)ei->e2di_size_high) << 32;
138 ip->i_atime = ei->e2di_atime;
139 ip->i_mtime = ei->e2di_mtime;
140 ip->i_ctime = ei->e2di_ctime;
141 if (E2DI_HAS_XTIME(ip)) {
142 ip->i_atimensec = XTIME_TO_NSEC(ei->e2di_atime_extra);
143 ip->i_mtimensec = XTIME_TO_NSEC(ei->e2di_mtime_extra);
144 ip->i_ctimensec = XTIME_TO_NSEC(ei->e2di_ctime_extra);
145 ip->i_birthtime = ei->e2di_crtime;
146 ip->i_birthnsec = XTIME_TO_NSEC(ei->e2di_crtime_extra);
149 ip->i_flags |= (ei->e2di_flags & EXT2_APPEND) ? SF_APPEND : 0;
150 ip->i_flags |= (ei->e2di_flags & EXT2_IMMUTABLE) ? SF_IMMUTABLE : 0;
151 ip->i_flags |= (ei->e2di_flags & EXT2_NODUMP) ? UF_NODUMP : 0;
152 ip->i_flag |= (ei->e2di_flags & EXT3_INDEX) ? IN_E3INDEX : 0;
153 ip->i_flag |= (ei->e2di_flags & EXT4_EXTENTS) ? IN_E4EXTENTS : 0;
154 ip->i_blocks = ei->e2di_nblock;
155 ip->i_facl = ei->e2di_facl;
156 if (E2DI_HAS_HUGE_FILE(ip)) {
157 ip->i_blocks |= (uint64_t)ei->e2di_nblock_high << 32;
158 ip->i_facl |= (uint64_t)ei->e2di_facl_high << 32;
159 if (ei->e2di_flags & EXT4_HUGE_FILE)
160 ip->i_blocks = fsbtodb(ip->i_e2fs, ip->i_blocks);
162 ip->i_gen = ei->e2di_gen;
163 ip->i_uid = ei->e2di_uid;
164 ip->i_gid = ei->e2di_gid;
165 ip->i_uid |= (uint32_t)ei->e2di_uid_high << 16;
166 ip->i_gid |= (uint32_t)ei->e2di_gid_high << 16;
168 memcpy(ip->i_data, ei->e2di_blocks, sizeof(ei->e2di_blocks));
170 /* Verify inode csum. */
171 return (ext2_ei_csum_verify(ip, ei));
175 * inode to raw ext2 inode
178 ext2_i2ei(struct inode *ip, struct ext2fs_dinode *ei)
183 ei->e2di_mode = ip->i_mode;
184 ei->e2di_nlink = ip->i_nlink;
186 * Godmar thinks: if dtime is nonzero, ext2 says this inode has been
187 * deleted, this would correspond to a zero link count
189 ei->e2di_dtime = ei->e2di_nlink ? 0 : ip->i_mtime;
190 ei->e2di_size = ip->i_size;
191 if (S_ISREG(ip->i_mode))
192 ei->e2di_size_high = ip->i_size >> 32;
193 ei->e2di_atime = ip->i_atime;
194 ei->e2di_mtime = ip->i_mtime;
195 ei->e2di_ctime = ip->i_ctime;
196 if (E2DI_HAS_XTIME(ip)) {
197 ei->e2di_ctime_extra = NSEC_TO_XTIME(ip->i_ctimensec);
198 ei->e2di_mtime_extra = NSEC_TO_XTIME(ip->i_mtimensec);
199 ei->e2di_atime_extra = NSEC_TO_XTIME(ip->i_atimensec);
200 ei->e2di_crtime = ip->i_birthtime;
201 ei->e2di_crtime_extra = NSEC_TO_XTIME(ip->i_birthnsec);
204 ei->e2di_flags |= (ip->i_flags & SF_APPEND) ? EXT2_APPEND : 0;
205 ei->e2di_flags |= (ip->i_flags & SF_IMMUTABLE) ? EXT2_IMMUTABLE : 0;
206 ei->e2di_flags |= (ip->i_flags & UF_NODUMP) ? EXT2_NODUMP : 0;
207 ei->e2di_flags |= (ip->i_flag & IN_E3INDEX) ? EXT3_INDEX : 0;
208 ei->e2di_flags |= (ip->i_flag & IN_E4EXTENTS) ? EXT4_EXTENTS : 0;
209 if (ip->i_blocks > ~0U &&
210 !EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_HUGE_FILE)) {
211 SDT_PROBE2(ext2fs, , trace, inode_cnv, 1, "i_blocks value is out of range");
214 if (ip->i_blocks <= 0xffffffffffffULL) {
215 ei->e2di_nblock = ip->i_blocks & 0xffffffff;
216 ei->e2di_nblock_high = ip->i_blocks >> 32 & 0xffff;
218 ei->e2di_flags |= EXT4_HUGE_FILE;
219 ei->e2di_nblock = dbtofsb(fs, ip->i_blocks);
220 ei->e2di_nblock_high = dbtofsb(fs, ip->i_blocks) >> 32 & 0xffff;
222 ei->e2di_facl = ip->i_facl & 0xffffffff;
223 ei->e2di_facl_high = ip->i_facl >> 32 & 0xffff;
224 ei->e2di_gen = ip->i_gen;
225 ei->e2di_uid = ip->i_uid & 0xffff;
226 ei->e2di_uid_high = ip->i_uid >> 16 & 0xffff;
227 ei->e2di_gid = ip->i_gid & 0xffff;
228 ei->e2di_gid_high = ip->i_gid >> 16 & 0xffff;
230 memcpy(ei->e2di_blocks, ip->i_data, sizeof(ei->e2di_blocks));
232 /* Set inode csum. */
233 ext2_ei_csum_set(ip, ei);