xref: /freebsd/sys/fs/ext2fs/ext2_inode_cnv.c (revision 2ff63af9b88c7413b7d71715b5532625752a248e)
1 /*-
2  * SPDX-License-Identifier: MIT-CMU
3  *
4  * Copyright (c) 1995 The University of Utah and
5  * the Computer Systems Laboratory at the University of Utah (CSL).
6  * All rights reserved.
7  *
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.''
15  *
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.
19  *
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.
22  *
23  *      Utah $Hdr$
24  */
25 
26 /*
27  * routines to convert on disk ext2 inodes into inodes and back
28  */
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/endian.h>
32 #include <sys/lock.h>
33 #include <sys/sdt.h>
34 #include <sys/stat.h>
35 #include <sys/vnode.h>
36 
37 #include <fs/ext2fs/fs.h>
38 #include <fs/ext2fs/inode.h>
39 #include <fs/ext2fs/ext2fs.h>
40 #include <fs/ext2fs/ext2_dinode.h>
41 #include <fs/ext2fs/ext2_extern.h>
42 
43 SDT_PROVIDER_DECLARE(ext2fs);
44 /*
45  * ext2fs trace probe:
46  * arg0: verbosity. Higher numbers give more verbose messages
47  * arg1: Textual message
48  */
49 SDT_PROBE_DEFINE2(ext2fs, , trace, inode_cnv, "int", "char*");
50 
51 #ifdef EXT2FS_PRINT_EXTENTS
52 void
ext2_print_inode(struct inode * in)53 ext2_print_inode(struct inode *in)
54 {
55 	int i;
56 	struct ext4_extent_header *ehp;
57 	struct ext4_extent *ep;
58 
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%llx ", (unsigned long long)in->i_ctime);
68 	printf("atime: 0x%llx ", (unsigned long long)in->i_atime);
69 	printf("mtime: 0x%llx ", (unsigned long long)in->i_mtime);
70 	if (E2DI_HAS_XTIME(in))
71 		printf("crtime %llx\n", (unsigned long long)in->i_birthtime);
72 	else
73 		printf("\n");
74 	if (in->i_flag & IN_E4EXTENTS) {
75 		printf("Extents:\n");
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",
83 		    le32toh(ep->e_blk),
84 		    le16toh(ep->e_len), le32toh(ep->e_start_lo),
85 		    le16toh(ep->e_start_hi));
86 		printf("\n");
87 	} else {
88 		printf("Blocks:");
89 		for (i = 0; i < (in->i_blocks <= 24 ? (in->i_blocks + 1) / 2 : 12); i++)
90 			printf("  %d", in->i_db[i]);
91 		printf("\n");
92 	}
93 }
94 #endif	/* EXT2FS_PRINT_EXTENTS */
95 
96 static inline bool
ext2_old_valid_dev(dev_t dev)97 ext2_old_valid_dev(dev_t dev)
98 {
99 	return (major(dev) < 256 && minor(dev) < 256);
100 }
101 
102 static inline uint16_t
ext2_old_encode_dev(dev_t dev)103 ext2_old_encode_dev(dev_t dev)
104 {
105 	return ((major(dev) << 8) | minor(dev));
106 }
107 
108 static inline dev_t
ext2_old_decode_dev(uint16_t val)109 ext2_old_decode_dev(uint16_t val)
110 {
111 	return (makedev((val >> 8) & 255, val & 255));
112 }
113 
114 static inline uint32_t
ext2_new_encode_dev(dev_t dev)115 ext2_new_encode_dev(dev_t dev)
116 {
117 	unsigned maj = major(dev);
118 	unsigned min = minor(dev);
119 
120 	return ((min & 0xff) | (maj << 8) | ((min & ~0xff) << 12));
121 }
122 
123 static inline dev_t
ext2_new_decode_dev(uint32_t dev)124 ext2_new_decode_dev(uint32_t dev)
125 {
126 	unsigned maj = (dev & 0xfff00) >> 8;
127 	unsigned min = (dev & 0xff) | ((dev >> 12) & 0xfff00);
128 
129 	return (makedev(maj, min));
130 }
131 
132 static inline void
ext2_decode_extra_time(ext_time_t * sec,int32_t * nsec,uint32_t extra)133 ext2_decode_extra_time(ext_time_t *sec, int32_t *nsec, uint32_t extra)
134 {
135 	if (extra & htole32(EXT3_EPOCH_MASK))
136 		*sec += (uint64_t)(le32toh(extra) & EXT3_EPOCH_MASK) << 32;
137 
138 	*nsec = (le32toh(extra) & EXT3_NSEC_MASK) >> EXT3_EPOCH_BITS;
139 }
140 
141 /*
142  *	raw ext2 inode LE to host inode conversion
143  */
144 int
ext2_ei2i(struct ext2fs_dinode * ei,struct inode * ip)145 ext2_ei2i(struct ext2fs_dinode *ei, struct inode *ip)
146 {
147 	struct m_ext2fs *fs = ip->i_e2fs;
148 	uint32_t ei_flags_host;
149 	uint16_t ei_extra_isize_le;
150 	int i;
151 
152 	if ((ip->i_number < EXT2_FIRST_INO(fs) && ip->i_number != EXT2_ROOTINO) ||
153 	    (ip->i_number < EXT2_ROOTINO) ||
154 	    (ip->i_number > le32toh(fs->e2fs->e2fs_icount))) {
155 		SDT_PROBE2(ext2fs, , trace, inode_cnv, 1, "bad inode number");
156 		return (EINVAL);
157 	}
158 
159 	/*
160 	 * Godmar thinks - if the link count is zero, then the inode is
161 	 * unused - according to ext2 standards. Ufs marks this fact by
162 	 * setting i_mode to zero - why ? I can see that this might lead to
163 	 * problems in an undelete.
164 	 */
165 	ip->i_nlink = le16toh(ei->e2di_nlink);
166 	ip->i_mode = ip->i_nlink ? le16toh(ei->e2di_mode) : 0;
167 	if (ip->i_number == EXT2_ROOTINO &&
168 	    (ip->i_nlink < 2 || !S_ISDIR(ip->i_mode))) {
169 		SDT_PROBE2(ext2fs, , trace, inode_cnv, 1, "root inode invalid");
170 		return (EINVAL);
171 	}
172 
173 	/* Check extra inode size */
174 	ei_extra_isize_le = le16toh(ei->e2di_extra_isize);
175 	if (EXT2_INODE_SIZE(fs) > E2FS_REV0_INODE_SIZE) {
176 		if (E2FS_REV0_INODE_SIZE + ei_extra_isize_le >
177 		    EXT2_INODE_SIZE(fs) || (ei_extra_isize_le & 3)) {
178 			SDT_PROBE2(ext2fs, , trace, inode_cnv, 1,
179 			    "bad extra inode size");
180 			return (EINVAL);
181 		}
182 	}
183 
184 	ip->i_size = le32toh(ei->e2di_size);
185 	if (S_ISREG(ip->i_mode))
186 		ip->i_size |= (uint64_t)le32toh(ei->e2di_size_high) << 32;
187 	ip->i_atime = (signed)le32toh(ei->e2di_atime);
188 	ip->i_mtime = (signed)le32toh(ei->e2di_mtime);
189 	ip->i_ctime = (signed)le32toh(ei->e2di_ctime);
190 	if (E2DI_HAS_XTIME(ip)) {
191 		ext2_decode_extra_time(&ip->i_atime, &ip->i_atimensec,
192 		    ei->e2di_atime_extra);
193 		ext2_decode_extra_time(&ip->i_mtime, &ip->i_mtimensec,
194 		    ei->e2di_mtime_extra);
195 		ext2_decode_extra_time(&ip->i_ctime, &ip->i_ctimensec,
196 		    ei->e2di_ctime_extra);
197 		ip->i_birthtime = (signed)le32toh(ei->e2di_crtime);
198 		ext2_decode_extra_time(&ip->i_birthtime, &ip->i_birthnsec,
199 		    ei->e2di_crtime_extra);
200 	}
201 	ip->i_flags = 0;
202 	ei_flags_host = le32toh(ei->e2di_flags);
203 	ip->i_flags |= (ei_flags_host & EXT2_APPEND) ? SF_APPEND : 0;
204 	ip->i_flags |= (ei_flags_host & EXT2_IMMUTABLE) ? SF_IMMUTABLE : 0;
205 	ip->i_flags |= (ei_flags_host & EXT2_NODUMP) ? UF_NODUMP : 0;
206 	ip->i_flag |= (ei_flags_host & EXT3_INDEX) ? IN_E3INDEX : 0;
207 	ip->i_flag |= (ei_flags_host & EXT4_EXTENTS) ? IN_E4EXTENTS : 0;
208 	ip->i_blocks = le32toh(ei->e2di_nblock);
209 	ip->i_facl = le32toh(ei->e2di_facl);
210 	if (E2DI_HAS_HUGE_FILE(ip)) {
211 		ip->i_blocks |= (uint64_t)le16toh(ei->e2di_nblock_high) << 32;
212 		ip->i_facl |= (uint64_t)le16toh(ei->e2di_facl_high) << 32;
213 		if (ei_flags_host & EXT4_HUGE_FILE)
214 			ip->i_blocks = fsbtodb(ip->i_e2fs, ip->i_blocks);
215 	}
216 	ip->i_gen = le32toh(ei->e2di_gen);
217 	ip->i_uid = le16toh(ei->e2di_uid);
218 	ip->i_gid = le16toh(ei->e2di_gid);
219 	ip->i_uid |= (uint32_t)le16toh(ei->e2di_uid_high) << 16;
220 	ip->i_gid |= (uint32_t)le16toh(ei->e2di_gid_high) << 16;
221 
222 	if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
223 		if (ei->e2di_blocks[0])
224 			ip->i_rdev = ext2_old_decode_dev(le32toh(ei->e2di_blocks[0]));
225 		else
226 			ip->i_rdev = ext2_new_decode_dev(le32toh(ei->e2di_blocks[1]));
227 	} else if ((ip->i_flag & IN_E4EXTENTS)) {
228 		memcpy(ip->i_data, ei->e2di_blocks, sizeof(ei->e2di_blocks));
229 	} else {
230 		for (i = 0; i < EXT2_NDADDR; i++)
231 			ip->i_db[i] = le32toh(ei->e2di_blocks[i]);
232 		for (i = 0; i < EXT2_NIADDR; i++)
233 			ip->i_ib[i] = le32toh(ei->e2di_blocks[EXT2_NDIR_BLOCKS + i]);
234 	}
235 
236 	/* Verify inode csum. */
237 	return (ext2_ei_csum_verify(ip, ei));
238 }
239 
240 static inline uint32_t
ext2_encode_extra_time(int64_t sec,int32_t nsec)241 ext2_encode_extra_time(int64_t sec, int32_t nsec)
242 {
243 	uint32_t extra;
244 
245 	extra = ((sec - (int32_t)sec) >> 32) & EXT3_EPOCH_MASK;
246 
247 	return (htole32(extra | (nsec << EXT3_EPOCH_BITS)));
248 }
249 
250 /*
251  *	inode to raw ext2 LE inode conversion
252  */
253 int
ext2_i2ei(struct inode * ip,struct ext2fs_dinode * ei)254 ext2_i2ei(struct inode *ip, struct ext2fs_dinode *ei)
255 {
256 	struct m_ext2fs *fs;
257 	int i;
258 
259 	fs = ip->i_e2fs;
260 	ei->e2di_mode = htole16(ip->i_mode);
261 	ei->e2di_nlink = htole16(ip->i_nlink);
262 	ei->e2di_size = htole32(ip->i_size);
263 	if (S_ISREG(ip->i_mode))
264 		ei->e2di_size_high = htole32(ip->i_size >> 32);
265 	ei->e2di_atime = htole32(ip->i_atime);
266 	ei->e2di_mtime = htole32(ip->i_mtime);
267 	ei->e2di_ctime = htole32(ip->i_ctime);
268 	/*
269 	 * Godmar thinks: if dtime is nonzero, ext2 says this inode has been
270 	 * deleted, this would correspond to a zero link count
271 	 */
272 	ei->e2di_dtime = htole32(le16toh(ei->e2di_nlink) ? 0 :
273 	    le32toh(ei->e2di_mtime));
274 	if (E2DI_HAS_XTIME(ip)) {
275 		ei->e2di_ctime_extra = ext2_encode_extra_time(ip->i_ctime,
276 		    ip->i_ctimensec);
277 		ei->e2di_mtime_extra = ext2_encode_extra_time(ip->i_mtime,
278 		    ip->i_mtimensec);
279 		ei->e2di_atime_extra = ext2_encode_extra_time(ip->i_atime,
280 		    ip->i_atimensec);
281 		ei->e2di_crtime = htole32(ip->i_birthtime);
282 		ei->e2di_crtime_extra = ext2_encode_extra_time(ip->i_birthtime,
283 		    ip->i_birthnsec);
284 	}
285 	/* Keep these in host endian for a while since they change a lot */
286 	ei->e2di_flags = 0;
287 	ei->e2di_flags |= htole32((ip->i_flags & SF_APPEND) ? EXT2_APPEND : 0);
288 	ei->e2di_flags |= htole32((ip->i_flags & SF_IMMUTABLE) ? EXT2_IMMUTABLE : 0);
289 	ei->e2di_flags |= htole32((ip->i_flags & UF_NODUMP) ? EXT2_NODUMP : 0);
290 	ei->e2di_flags |= htole32((ip->i_flag & IN_E3INDEX) ? EXT3_INDEX : 0);
291 	ei->e2di_flags |= htole32((ip->i_flag & IN_E4EXTENTS) ? EXT4_EXTENTS : 0);
292 	if (ip->i_blocks > ~0U &&
293 	    !EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_HUGE_FILE)) {
294 		SDT_PROBE2(ext2fs, , trace, inode_cnv, 1, "i_blocks value is out of range");
295 		return (EIO);
296 	}
297 	if (ip->i_blocks <= 0xffffffffffffULL) {
298 		ei->e2di_nblock = htole32(ip->i_blocks & 0xffffffff);
299 		ei->e2di_nblock_high = htole16(ip->i_blocks >> 32 & 0xffff);
300 	} else {
301 		ei->e2di_flags |= htole32(EXT4_HUGE_FILE);
302 		ei->e2di_nblock = htole32(dbtofsb(fs, ip->i_blocks));
303 		ei->e2di_nblock_high = htole16(dbtofsb(fs, ip->i_blocks) >> 32 & 0xffff);
304 	}
305 
306 	ei->e2di_facl = htole32(ip->i_facl & 0xffffffff);
307 	ei->e2di_facl_high = htole16(ip->i_facl >> 32 & 0xffff);
308 	ei->e2di_gen = htole32(ip->i_gen);
309 	ei->e2di_uid = htole16(ip->i_uid & 0xffff);
310 	ei->e2di_uid_high = htole16(ip->i_uid >> 16 & 0xffff);
311 	ei->e2di_gid = htole16(ip->i_gid & 0xffff);
312 	ei->e2di_gid_high = htole16(ip->i_gid >> 16 & 0xffff);
313 
314 	if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
315 		if (ext2_old_valid_dev(ip->i_rdev)) {
316 			ei->e2di_blocks[0] = htole32(ext2_old_encode_dev(ip->i_rdev));
317 			ei->e2di_blocks[1] = 0;
318 		} else {
319 			ei->e2di_blocks[0] = 0;
320 			ei->e2di_blocks[1] = htole32(ext2_new_encode_dev(ip->i_rdev));
321 			ei->e2di_blocks[2] = 0;
322 		}
323 	} else if ((ip->i_flag & IN_E4EXTENTS)) {
324 		memcpy(ei->e2di_blocks, ip->i_data, sizeof(ei->e2di_blocks));
325 	} else {
326 		for (i = 0; i < EXT2_NDADDR; i++)
327 			ei->e2di_blocks[i] = htole32(ip->i_db[i]);
328 		for (i = 0; i < EXT2_NIADDR; i++)
329 			ei->e2di_blocks[EXT2_NDIR_BLOCKS + i] = htole32(ip->i_ib[i]);
330 	}
331 
332 	/* Set inode csum. */
333 	ext2_ei_csum_set(ip, ei);
334 
335 	return (0);
336 }
337