xref: /freebsd/sys/fs/ext2fs/ext2_inode_cnv.c (revision 2ff63af9b88c7413b7d71715b5532625752a248e)
1e09c00caSUlf Lilleengen /*-
27abc09cdSPedro F. Giffuni  * SPDX-License-Identifier: MIT-CMU
37abc09cdSPedro F. Giffuni  *
4e09c00caSUlf Lilleengen  * Copyright (c) 1995 The University of Utah and
5e09c00caSUlf Lilleengen  * the Computer Systems Laboratory at the University of Utah (CSL).
6e09c00caSUlf Lilleengen  * All rights reserved.
7e09c00caSUlf Lilleengen  *
8e09c00caSUlf Lilleengen  * Permission to use, copy, modify and distribute this software is hereby
9e09c00caSUlf Lilleengen  * granted provided that (1) source code retains these copyright, permission,
10e09c00caSUlf Lilleengen  * and disclaimer notices, and (2) redistributions including binaries
11e09c00caSUlf Lilleengen  * reproduce the notices in supporting documentation, and (3) all advertising
12e09c00caSUlf Lilleengen  * materials mentioning features or use of this software display the following
13e09c00caSUlf Lilleengen  * acknowledgement: ``This product includes software developed by the
14e09c00caSUlf Lilleengen  * Computer Systems Laboratory at the University of Utah.''
15e09c00caSUlf Lilleengen  *
16e09c00caSUlf Lilleengen  * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
17e09c00caSUlf Lilleengen  * IS" CONDITION.  THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
18e09c00caSUlf Lilleengen  * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19e09c00caSUlf Lilleengen  *
20e09c00caSUlf Lilleengen  * CSL requests users of this software to return to csl-dist@cs.utah.edu any
21e09c00caSUlf Lilleengen  * improvements that they make and grant CSL redistribution rights.
22e09c00caSUlf Lilleengen  *
23e09c00caSUlf Lilleengen  *      Utah $Hdr$
24e09c00caSUlf Lilleengen  */
25e09c00caSUlf Lilleengen 
26e09c00caSUlf Lilleengen /*
27e09c00caSUlf Lilleengen  * routines to convert on disk ext2 inodes into inodes and back
28e09c00caSUlf Lilleengen  */
29e09c00caSUlf Lilleengen #include <sys/param.h>
30e09c00caSUlf Lilleengen #include <sys/systm.h>
31e28f5d52SPedro F. Giffuni #include <sys/endian.h>
32e09c00caSUlf Lilleengen #include <sys/lock.h>
33ebc94b66SFedor Uporov #include <sys/sdt.h>
34e09c00caSUlf Lilleengen #include <sys/stat.h>
35e09c00caSUlf Lilleengen #include <sys/vnode.h>
36e09c00caSUlf Lilleengen 
37d7511a40SPedro F. Giffuni #include <fs/ext2fs/fs.h>
38e09c00caSUlf Lilleengen #include <fs/ext2fs/inode.h>
39e09c00caSUlf Lilleengen #include <fs/ext2fs/ext2fs.h>
40e09c00caSUlf Lilleengen #include <fs/ext2fs/ext2_dinode.h>
41e28f5d52SPedro F. Giffuni #include <fs/ext2fs/ext2_extern.h>
42e09c00caSUlf Lilleengen 
43ebc94b66SFedor Uporov SDT_PROVIDER_DECLARE(ext2fs);
44ebc94b66SFedor Uporov /*
45ebc94b66SFedor Uporov  * ext2fs trace probe:
46ebc94b66SFedor Uporov  * arg0: verbosity. Higher numbers give more verbose messages
47ebc94b66SFedor Uporov  * arg1: Textual message
48ebc94b66SFedor Uporov  */
49ebc94b66SFedor Uporov SDT_PROBE_DEFINE2(ext2fs, , trace, inode_cnv, "int", "char*");
50ebc94b66SFedor Uporov 
51ebc94b66SFedor Uporov #ifdef EXT2FS_PRINT_EXTENTS
52e09c00caSUlf Lilleengen void
ext2_print_inode(struct inode * in)53a9d1b299SPedro F. Giffuni ext2_print_inode(struct inode *in)
54e09c00caSUlf Lilleengen {
55e09c00caSUlf Lilleengen 	int i;
56d7511a40SPedro F. Giffuni 	struct ext4_extent_header *ehp;
57d7511a40SPedro F. Giffuni 	struct ext4_extent *ep;
58e09c00caSUlf Lilleengen 
59fc8fdae0SMatthew D Fleming 	printf("Inode: %5ju", (uintmax_t)in->i_number);
60e09c00caSUlf Lilleengen 	printf(	/* "Inode: %5d" */
61e06e5241SFedor Uporov 	    " Type: %10s Mode: 0x%o Flags: 0x%x  Version: %d acl: 0x%jx\n",
6234f43888SPedro F. Giffuni 	    "n/a", in->i_mode, in->i_flags, in->i_gen, in->i_facl);
63d7511a40SPedro F. Giffuni 	printf("User: %5u Group: %5u  Size: %ju\n",
64d7511a40SPedro F. Giffuni 	    in->i_uid, in->i_gid, (uintmax_t)in->i_size);
65d7511a40SPedro F. Giffuni 	printf("Links: %3d Blockcount: %ju\n",
66d7511a40SPedro F. Giffuni 	    in->i_nlink, (uintmax_t)in->i_blocks);
67*f1d5e2c8SFedor Uporov 	printf("ctime: 0x%llx ", (unsigned long long)in->i_ctime);
68*f1d5e2c8SFedor Uporov 	printf("atime: 0x%llx ", (unsigned long long)in->i_atime);
69*f1d5e2c8SFedor Uporov 	printf("mtime: 0x%llx ", (unsigned long long)in->i_mtime);
70d7511a40SPedro F. Giffuni 	if (E2DI_HAS_XTIME(in))
71*f1d5e2c8SFedor Uporov 		printf("crtime %llx\n", (unsigned long long)in->i_birthtime);
72e06e5241SFedor Uporov 	else
73e06e5241SFedor Uporov 		printf("\n");
74b394cd1eSFedor Uporov 	if (in->i_flag & IN_E4EXTENTS) {
75d7511a40SPedro F. Giffuni 		printf("Extents:\n");
76d7511a40SPedro F. Giffuni 		ehp = (struct ext4_extent_header *)in->i_db;
77d7511a40SPedro F. Giffuni 		printf("Header (magic 0x%x entries %d max %d depth %d gen %d)\n",
78cd3acfe7SFedor Uporov 		    le16toh(ehp->eh_magic), le16toh(ehp->eh_ecount),
79cd3acfe7SFedor Uporov 		    le16toh(ehp->eh_max), le16toh(ehp->eh_depth),
80cd3acfe7SFedor Uporov 		    le32toh(ehp->eh_gen));
81d7511a40SPedro F. Giffuni 		ep = (struct ext4_extent *)(char *)(ehp + 1);
82cd3acfe7SFedor Uporov 		printf("Index (blk %d len %d start_lo %d start_hi %d)\n",
83cd3acfe7SFedor Uporov 		    le32toh(ep->e_blk),
84cd3acfe7SFedor Uporov 		    le16toh(ep->e_len), le32toh(ep->e_start_lo),
85cd3acfe7SFedor Uporov 		    le16toh(ep->e_start_hi));
86d7511a40SPedro F. Giffuni 		printf("\n");
87b394cd1eSFedor Uporov 	} else {
885679656eSFedor Uporov 		printf("Blocks:");
89b394cd1eSFedor Uporov 		for (i = 0; i < (in->i_blocks <= 24 ? (in->i_blocks + 1) / 2 : 12); i++)
90b394cd1eSFedor Uporov 			printf("  %d", in->i_db[i]);
91b394cd1eSFedor Uporov 		printf("\n");
92b394cd1eSFedor Uporov 	}
93e09c00caSUlf Lilleengen }
94ebc94b66SFedor Uporov #endif	/* EXT2FS_PRINT_EXTENTS */
95e09c00caSUlf Lilleengen 
961ed5f62dSFedor Uporov static inline bool
ext2_old_valid_dev(dev_t dev)971ed5f62dSFedor Uporov ext2_old_valid_dev(dev_t dev)
981ed5f62dSFedor Uporov {
991ed5f62dSFedor Uporov 	return (major(dev) < 256 && minor(dev) < 256);
1001ed5f62dSFedor Uporov }
1011ed5f62dSFedor Uporov 
1021ed5f62dSFedor Uporov static inline uint16_t
ext2_old_encode_dev(dev_t dev)1031ed5f62dSFedor Uporov ext2_old_encode_dev(dev_t dev)
1041ed5f62dSFedor Uporov {
1051ed5f62dSFedor Uporov 	return ((major(dev) << 8) | minor(dev));
1061ed5f62dSFedor Uporov }
1071ed5f62dSFedor Uporov 
1081ed5f62dSFedor Uporov static inline dev_t
ext2_old_decode_dev(uint16_t val)1091ed5f62dSFedor Uporov ext2_old_decode_dev(uint16_t val)
1101ed5f62dSFedor Uporov {
1111ed5f62dSFedor Uporov 	return (makedev((val >> 8) & 255, val & 255));
1121ed5f62dSFedor Uporov }
1131ed5f62dSFedor Uporov 
1141ed5f62dSFedor Uporov static inline uint32_t
ext2_new_encode_dev(dev_t dev)1151ed5f62dSFedor Uporov ext2_new_encode_dev(dev_t dev)
1161ed5f62dSFedor Uporov {
1171ed5f62dSFedor Uporov 	unsigned maj = major(dev);
1181ed5f62dSFedor Uporov 	unsigned min = minor(dev);
1191ed5f62dSFedor Uporov 
1201ed5f62dSFedor Uporov 	return ((min & 0xff) | (maj << 8) | ((min & ~0xff) << 12));
1211ed5f62dSFedor Uporov }
1221ed5f62dSFedor Uporov 
1231ed5f62dSFedor Uporov static inline dev_t
ext2_new_decode_dev(uint32_t dev)1241ed5f62dSFedor Uporov ext2_new_decode_dev(uint32_t dev)
1251ed5f62dSFedor Uporov {
1261ed5f62dSFedor Uporov 	unsigned maj = (dev & 0xfff00) >> 8;
1271ed5f62dSFedor Uporov 	unsigned min = (dev & 0xff) | ((dev >> 12) & 0xfff00);
1281ed5f62dSFedor Uporov 
1291ed5f62dSFedor Uporov 	return (makedev(maj, min));
1301ed5f62dSFedor Uporov }
1311ed5f62dSFedor Uporov 
1322a984c2bSFedor Uporov static inline void
ext2_decode_extra_time(ext_time_t * sec,int32_t * nsec,uint32_t extra)133c40a160fSFedor Uporov ext2_decode_extra_time(ext_time_t *sec, int32_t *nsec, uint32_t extra)
134c40a160fSFedor Uporov {
135c40a160fSFedor Uporov 	if (extra & htole32(EXT3_EPOCH_MASK))
136c40a160fSFedor Uporov 		*sec += (uint64_t)(le32toh(extra) & EXT3_EPOCH_MASK) << 32;
137c40a160fSFedor Uporov 
138c40a160fSFedor Uporov 	*nsec = (le32toh(extra) & EXT3_NSEC_MASK) >> EXT3_EPOCH_BITS;
139c40a160fSFedor Uporov }
140c40a160fSFedor Uporov 
141e09c00caSUlf Lilleengen /*
142cd3acfe7SFedor Uporov  *	raw ext2 inode LE to host inode conversion
143e09c00caSUlf Lilleengen  */
144512f29d1SFedor Uporov int
ext2_ei2i(struct ext2fs_dinode * ei,struct inode * ip)145a9d1b299SPedro F. Giffuni ext2_ei2i(struct ext2fs_dinode *ei, struct inode *ip)
146e09c00caSUlf Lilleengen {
147daa2d62dSFedor Uporov 	struct m_ext2fs *fs = ip->i_e2fs;
148cd3acfe7SFedor Uporov 	uint32_t ei_flags_host;
149cd3acfe7SFedor Uporov 	uint16_t ei_extra_isize_le;
150cd3acfe7SFedor Uporov 	int i;
151512f29d1SFedor Uporov 
152daa2d62dSFedor Uporov 	if ((ip->i_number < EXT2_FIRST_INO(fs) && ip->i_number != EXT2_ROOTINO) ||
153daa2d62dSFedor Uporov 	    (ip->i_number < EXT2_ROOTINO) ||
154cd3acfe7SFedor Uporov 	    (ip->i_number > le32toh(fs->e2fs->e2fs_icount))) {
155ebc94b66SFedor Uporov 		SDT_PROBE2(ext2fs, , trace, inode_cnv, 1, "bad inode number");
156daa2d62dSFedor Uporov 		return (EINVAL);
157daa2d62dSFedor Uporov 	}
158daa2d62dSFedor Uporov 
159ced21728SFedor Uporov 	/*
160ced21728SFedor Uporov 	 * Godmar thinks - if the link count is zero, then the inode is
161ced21728SFedor Uporov 	 * unused - according to ext2 standards. Ufs marks this fact by
162ced21728SFedor Uporov 	 * setting i_mode to zero - why ? I can see that this might lead to
163ced21728SFedor Uporov 	 * problems in an undelete.
164ced21728SFedor Uporov 	 */
165cd3acfe7SFedor Uporov 	ip->i_nlink = le16toh(ei->e2di_nlink);
166ced21728SFedor Uporov 	ip->i_mode = ip->i_nlink ? le16toh(ei->e2di_mode) : 0;
167ced21728SFedor Uporov 	if (ip->i_number == EXT2_ROOTINO &&
168ced21728SFedor Uporov 	    (ip->i_nlink < 2 || !S_ISDIR(ip->i_mode))) {
169ced21728SFedor Uporov 		SDT_PROBE2(ext2fs, , trace, inode_cnv, 1, "root inode invalid");
170daa2d62dSFedor Uporov 		return (EINVAL);
171daa2d62dSFedor Uporov 	}
172daa2d62dSFedor Uporov 
173daa2d62dSFedor Uporov 	/* Check extra inode size */
174cd3acfe7SFedor Uporov 	ei_extra_isize_le = le16toh(ei->e2di_extra_isize);
175daa2d62dSFedor Uporov 	if (EXT2_INODE_SIZE(fs) > E2FS_REV0_INODE_SIZE) {
176cd3acfe7SFedor Uporov 		if (E2FS_REV0_INODE_SIZE + ei_extra_isize_le >
177cd3acfe7SFedor Uporov 		    EXT2_INODE_SIZE(fs) || (ei_extra_isize_le & 3)) {
178ebc94b66SFedor Uporov 			SDT_PROBE2(ext2fs, , trace, inode_cnv, 1,
179ebc94b66SFedor Uporov 			    "bad extra inode size");
180daa2d62dSFedor Uporov 			return (EINVAL);
181daa2d62dSFedor Uporov 		}
182daa2d62dSFedor Uporov 	}
183daa2d62dSFedor Uporov 
184cd3acfe7SFedor Uporov 	ip->i_size = le32toh(ei->e2di_size);
185e09c00caSUlf Lilleengen 	if (S_ISREG(ip->i_mode))
186cd3acfe7SFedor Uporov 		ip->i_size |= (uint64_t)le32toh(ei->e2di_size_high) << 32;
187c40a160fSFedor Uporov 	ip->i_atime = (signed)le32toh(ei->e2di_atime);
188c40a160fSFedor Uporov 	ip->i_mtime = (signed)le32toh(ei->e2di_mtime);
189c40a160fSFedor Uporov 	ip->i_ctime = (signed)le32toh(ei->e2di_ctime);
190035e4e04SPedro F. Giffuni 	if (E2DI_HAS_XTIME(ip)) {
191c40a160fSFedor Uporov 		ext2_decode_extra_time(&ip->i_atime, &ip->i_atimensec,
192c40a160fSFedor Uporov 		    ei->e2di_atime_extra);
193c40a160fSFedor Uporov 		ext2_decode_extra_time(&ip->i_mtime, &ip->i_mtimensec,
194c40a160fSFedor Uporov 		    ei->e2di_mtime_extra);
195c40a160fSFedor Uporov 		ext2_decode_extra_time(&ip->i_ctime, &ip->i_ctimensec,
196c40a160fSFedor Uporov 		    ei->e2di_ctime_extra);
197c40a160fSFedor Uporov 		ip->i_birthtime = (signed)le32toh(ei->e2di_crtime);
198c40a160fSFedor Uporov 		ext2_decode_extra_time(&ip->i_birthtime, &ip->i_birthnsec,
199c40a160fSFedor Uporov 		    ei->e2di_crtime_extra);
200035e4e04SPedro F. Giffuni 	}
201c2e2b77bSPedro F. Giffuni 	ip->i_flags = 0;
202cd3acfe7SFedor Uporov 	ei_flags_host = le32toh(ei->e2di_flags);
203cd3acfe7SFedor Uporov 	ip->i_flags |= (ei_flags_host & EXT2_APPEND) ? SF_APPEND : 0;
204cd3acfe7SFedor Uporov 	ip->i_flags |= (ei_flags_host & EXT2_IMMUTABLE) ? SF_IMMUTABLE : 0;
205cd3acfe7SFedor Uporov 	ip->i_flags |= (ei_flags_host & EXT2_NODUMP) ? UF_NODUMP : 0;
206cd3acfe7SFedor Uporov 	ip->i_flag |= (ei_flags_host & EXT3_INDEX) ? IN_E3INDEX : 0;
207cd3acfe7SFedor Uporov 	ip->i_flag |= (ei_flags_host & EXT4_EXTENTS) ? IN_E4EXTENTS : 0;
208cd3acfe7SFedor Uporov 	ip->i_blocks = le32toh(ei->e2di_nblock);
209cd3acfe7SFedor Uporov 	ip->i_facl = le32toh(ei->e2di_facl);
210d7511a40SPedro F. Giffuni 	if (E2DI_HAS_HUGE_FILE(ip)) {
211cd3acfe7SFedor Uporov 		ip->i_blocks |= (uint64_t)le16toh(ei->e2di_nblock_high) << 32;
212cd3acfe7SFedor Uporov 		ip->i_facl |= (uint64_t)le16toh(ei->e2di_facl_high) << 32;
213cd3acfe7SFedor Uporov 		if (ei_flags_host & EXT4_HUGE_FILE)
214d7511a40SPedro F. Giffuni 			ip->i_blocks = fsbtodb(ip->i_e2fs, ip->i_blocks);
215d7511a40SPedro F. Giffuni 	}
216cd3acfe7SFedor Uporov 	ip->i_gen = le32toh(ei->e2di_gen);
217cd3acfe7SFedor Uporov 	ip->i_uid = le16toh(ei->e2di_uid);
218cd3acfe7SFedor Uporov 	ip->i_gid = le16toh(ei->e2di_gid);
219cd3acfe7SFedor Uporov 	ip->i_uid |= (uint32_t)le16toh(ei->e2di_uid_high) << 16;
220cd3acfe7SFedor Uporov 	ip->i_gid |= (uint32_t)le16toh(ei->e2di_gid_high) << 16;
22112b4678bSPedro F. Giffuni 
2221ed5f62dSFedor Uporov 	if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
2231ed5f62dSFedor Uporov 		if (ei->e2di_blocks[0])
2241ed5f62dSFedor Uporov 			ip->i_rdev = ext2_old_decode_dev(le32toh(ei->e2di_blocks[0]));
2251ed5f62dSFedor Uporov 		else
2261ed5f62dSFedor Uporov 			ip->i_rdev = ext2_new_decode_dev(le32toh(ei->e2di_blocks[1]));
2271ed5f62dSFedor Uporov 	} else if ((ip->i_flag & IN_E4EXTENTS)) {
228b394cd1eSFedor Uporov 		memcpy(ip->i_data, ei->e2di_blocks, sizeof(ei->e2di_blocks));
229cd3acfe7SFedor Uporov 	} else {
230cd3acfe7SFedor Uporov 		for (i = 0; i < EXT2_NDADDR; i++)
231cd3acfe7SFedor Uporov 			ip->i_db[i] = le32toh(ei->e2di_blocks[i]);
232cd3acfe7SFedor Uporov 		for (i = 0; i < EXT2_NIADDR; i++)
233cd3acfe7SFedor Uporov 			ip->i_ib[i] = le32toh(ei->e2di_blocks[EXT2_NDIR_BLOCKS + i]);
234cd3acfe7SFedor Uporov 	}
235512f29d1SFedor Uporov 
2366d4a4ed7SFedor Uporov 	/* Verify inode csum. */
237512f29d1SFedor Uporov 	return (ext2_ei_csum_verify(ip, ei));
238e09c00caSUlf Lilleengen }
239e09c00caSUlf Lilleengen 
2402a984c2bSFedor Uporov static inline uint32_t
ext2_encode_extra_time(int64_t sec,int32_t nsec)241c40a160fSFedor Uporov ext2_encode_extra_time(int64_t sec, int32_t nsec)
242c40a160fSFedor Uporov {
243c40a160fSFedor Uporov 	uint32_t extra;
244c40a160fSFedor Uporov 
245c40a160fSFedor Uporov 	extra = ((sec - (int32_t)sec) >> 32) & EXT3_EPOCH_MASK;
246c40a160fSFedor Uporov 
247c40a160fSFedor Uporov 	return (htole32(extra | (nsec << EXT3_EPOCH_BITS)));
248c40a160fSFedor Uporov }
249cd3acfe7SFedor Uporov 
250e09c00caSUlf Lilleengen /*
251cd3acfe7SFedor Uporov  *	inode to raw ext2 LE inode conversion
252e09c00caSUlf Lilleengen  */
25372530f91SFedor Uporov int
ext2_i2ei(struct inode * ip,struct ext2fs_dinode * ei)254a9d1b299SPedro F. Giffuni ext2_i2ei(struct inode *ip, struct ext2fs_dinode *ei)
255e09c00caSUlf Lilleengen {
25672530f91SFedor Uporov 	struct m_ext2fs *fs;
257cd3acfe7SFedor Uporov 	int i;
258e09c00caSUlf Lilleengen 
25972530f91SFedor Uporov 	fs = ip->i_e2fs;
260cd3acfe7SFedor Uporov 	ei->e2di_mode = htole16(ip->i_mode);
261cd3acfe7SFedor Uporov 	ei->e2di_nlink = htole16(ip->i_nlink);
262cd3acfe7SFedor Uporov 	ei->e2di_size = htole32(ip->i_size);
263cd3acfe7SFedor Uporov 	if (S_ISREG(ip->i_mode))
264cd3acfe7SFedor Uporov 		ei->e2di_size_high = htole32(ip->i_size >> 32);
265cd3acfe7SFedor Uporov 	ei->e2di_atime = htole32(ip->i_atime);
266cd3acfe7SFedor Uporov 	ei->e2di_mtime = htole32(ip->i_mtime);
267cd3acfe7SFedor Uporov 	ei->e2di_ctime = htole32(ip->i_ctime);
268e09c00caSUlf Lilleengen 	/*
269bf9a211dSPedro F. Giffuni 	 * Godmar thinks: if dtime is nonzero, ext2 says this inode has been
270bf9a211dSPedro F. Giffuni 	 * deleted, this would correspond to a zero link count
271e09c00caSUlf Lilleengen 	 */
272cd3acfe7SFedor Uporov 	ei->e2di_dtime = htole32(le16toh(ei->e2di_nlink) ? 0 :
273cd3acfe7SFedor Uporov 	    le32toh(ei->e2di_mtime));
274817dd257SPedro F. Giffuni 	if (E2DI_HAS_XTIME(ip)) {
275c40a160fSFedor Uporov 		ei->e2di_ctime_extra = ext2_encode_extra_time(ip->i_ctime,
276c40a160fSFedor Uporov 		    ip->i_ctimensec);
277c40a160fSFedor Uporov 		ei->e2di_mtime_extra = ext2_encode_extra_time(ip->i_mtime,
278c40a160fSFedor Uporov 		    ip->i_mtimensec);
279c40a160fSFedor Uporov 		ei->e2di_atime_extra = ext2_encode_extra_time(ip->i_atime,
280c40a160fSFedor Uporov 		    ip->i_atimensec);
281cd3acfe7SFedor Uporov 		ei->e2di_crtime = htole32(ip->i_birthtime);
282c40a160fSFedor Uporov 		ei->e2di_crtime_extra = ext2_encode_extra_time(ip->i_birthtime,
283c40a160fSFedor Uporov 		    ip->i_birthnsec);
284817dd257SPedro F. Giffuni 	}
285cd3acfe7SFedor Uporov 	/* Keep these in host endian for a while since they change a lot */
286c2e2b77bSPedro F. Giffuni 	ei->e2di_flags = 0;
287cd3acfe7SFedor Uporov 	ei->e2di_flags |= htole32((ip->i_flags & SF_APPEND) ? EXT2_APPEND : 0);
288cd3acfe7SFedor Uporov 	ei->e2di_flags |= htole32((ip->i_flags & SF_IMMUTABLE) ? EXT2_IMMUTABLE : 0);
289cd3acfe7SFedor Uporov 	ei->e2di_flags |= htole32((ip->i_flags & UF_NODUMP) ? EXT2_NODUMP : 0);
290cd3acfe7SFedor Uporov 	ei->e2di_flags |= htole32((ip->i_flag & IN_E3INDEX) ? EXT3_INDEX : 0);
291cd3acfe7SFedor Uporov 	ei->e2di_flags |= htole32((ip->i_flag & IN_E4EXTENTS) ? EXT4_EXTENTS : 0);
29272530f91SFedor Uporov 	if (ip->i_blocks > ~0U &&
29372530f91SFedor Uporov 	    !EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_HUGE_FILE)) {
294ebc94b66SFedor Uporov 		SDT_PROBE2(ext2fs, , trace, inode_cnv, 1, "i_blocks value is out of range");
29572530f91SFedor Uporov 		return (EIO);
29672530f91SFedor Uporov 	}
29772530f91SFedor Uporov 	if (ip->i_blocks <= 0xffffffffffffULL) {
298cd3acfe7SFedor Uporov 		ei->e2di_nblock = htole32(ip->i_blocks & 0xffffffff);
299cd3acfe7SFedor Uporov 		ei->e2di_nblock_high = htole16(ip->i_blocks >> 32 & 0xffff);
30072530f91SFedor Uporov 	} else {
301cd3acfe7SFedor Uporov 		ei->e2di_flags |= htole32(EXT4_HUGE_FILE);
302cd3acfe7SFedor Uporov 		ei->e2di_nblock = htole32(dbtofsb(fs, ip->i_blocks));
303cd3acfe7SFedor Uporov 		ei->e2di_nblock_high = htole16(dbtofsb(fs, ip->i_blocks) >> 32 & 0xffff);
30472530f91SFedor Uporov 	}
30512b4678bSPedro F. Giffuni 
306cd3acfe7SFedor Uporov 	ei->e2di_facl = htole32(ip->i_facl & 0xffffffff);
307cd3acfe7SFedor Uporov 	ei->e2di_facl_high = htole16(ip->i_facl >> 32 & 0xffff);
308cd3acfe7SFedor Uporov 	ei->e2di_gen = htole32(ip->i_gen);
309cd3acfe7SFedor Uporov 	ei->e2di_uid = htole16(ip->i_uid & 0xffff);
310cd3acfe7SFedor Uporov 	ei->e2di_uid_high = htole16(ip->i_uid >> 16 & 0xffff);
311cd3acfe7SFedor Uporov 	ei->e2di_gid = htole16(ip->i_gid & 0xffff);
312cd3acfe7SFedor Uporov 	ei->e2di_gid_high = htole16(ip->i_gid >> 16 & 0xffff);
313cd3acfe7SFedor Uporov 
3141ed5f62dSFedor Uporov 	if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
3151ed5f62dSFedor Uporov 		if (ext2_old_valid_dev(ip->i_rdev)) {
3161ed5f62dSFedor Uporov 			ei->e2di_blocks[0] = htole32(ext2_old_encode_dev(ip->i_rdev));
3171ed5f62dSFedor Uporov 			ei->e2di_blocks[1] = 0;
3181ed5f62dSFedor Uporov 		} else {
3191ed5f62dSFedor Uporov 			ei->e2di_blocks[0] = 0;
3201ed5f62dSFedor Uporov 			ei->e2di_blocks[1] = htole32(ext2_new_encode_dev(ip->i_rdev));
3211ed5f62dSFedor Uporov 			ei->e2di_blocks[2] = 0;
3221ed5f62dSFedor Uporov 		}
3231ed5f62dSFedor Uporov 	} else if ((ip->i_flag & IN_E4EXTENTS)) {
324b394cd1eSFedor Uporov 		memcpy(ei->e2di_blocks, ip->i_data, sizeof(ei->e2di_blocks));
325cd3acfe7SFedor Uporov 	} else {
326cd3acfe7SFedor Uporov 		for (i = 0; i < EXT2_NDADDR; i++)
327cd3acfe7SFedor Uporov 			ei->e2di_blocks[i] = htole32(ip->i_db[i]);
328cd3acfe7SFedor Uporov 		for (i = 0; i < EXT2_NIADDR; i++)
329cd3acfe7SFedor Uporov 			ei->e2di_blocks[EXT2_NDIR_BLOCKS + i] = htole32(ip->i_ib[i]);
330cd3acfe7SFedor Uporov 	}
33172530f91SFedor Uporov 
3326d4a4ed7SFedor Uporov 	/* Set inode csum. */
333512f29d1SFedor Uporov 	ext2_ei_csum_set(ip, ei);
334512f29d1SFedor Uporov 
33572530f91SFedor Uporov 	return (0);
336e09c00caSUlf Lilleengen }
337