ext2_inode_cnv.c revision 311232
1/*-
2 * Copyright (c) 1995 The University of Utah and
3 * the Computer Systems Laboratory at the University of Utah (CSL).
4 * All rights reserved.
5 *
6 * Permission to use, copy, modify and distribute this software is hereby
7 * granted provided that (1) source code retains these copyright, permission,
8 * and disclaimer notices, and (2) redistributions including binaries
9 * reproduce the notices in supporting documentation, and (3) all advertising
10 * materials mentioning features or use of this software display the following
11 * acknowledgement: ``This product includes software developed by the
12 * Computer Systems Laboratory at the University of Utah.''
13 *
14 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
15 * IS" CONDITION.  THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
16 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 *
18 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
19 * improvements that they make and grant CSL redistribution rights.
20 *
21 *      Utah $Hdr$
22 * $FreeBSD: stable/10/sys/fs/ext2fs/ext2_inode_cnv.c 311232 2017-01-04 02:43:33Z pfg $
23 */
24
25/*
26 * routines to convert on disk ext2 inodes into inodes and back
27 */
28#include <sys/param.h>
29#include <sys/systm.h>
30#include <sys/endian.h>
31#include <sys/lock.h>
32#include <sys/stat.h>
33#include <sys/vnode.h>
34
35#include <fs/ext2fs/fs.h>
36#include <fs/ext2fs/inode.h>
37#include <fs/ext2fs/ext2fs.h>
38#include <fs/ext2fs/ext2_dinode.h>
39#include <fs/ext2fs/ext2_extern.h>
40
41#define XTIME_TO_NSEC(x)	((x & EXT3_NSEC_MASK) >> 2)
42#define NSEC_TO_XTIME(t)	(le32toh(t << 2) & EXT3_NSEC_MASK)
43
44#ifdef EXT2FS_DEBUG
45void
46ext2_print_inode(struct inode *in)
47{
48	int i;
49	struct ext4_extent_header *ehp;
50	struct ext4_extent *ep;
51
52	printf("Inode: %5ju", (uintmax_t)in->i_number);
53	printf(	/* "Inode: %5d" */
54	    " Type: %10s Mode: 0x%o Flags: 0x%x  Version: %d\n",
55	    "n/a", in->i_mode, in->i_flags, in->i_gen);
56	printf("User: %5u Group: %5u  Size: %ju\n",
57	    in->i_uid, in->i_gid, (uintmax_t)in->i_size);
58	printf("Links: %3d Blockcount: %ju\n",
59	    in->i_nlink, (uintmax_t)in->i_blocks);
60	printf("ctime: 0x%x", in->i_ctime);
61	printf("atime: 0x%x", in->i_atime);
62	printf("mtime: 0x%x", in->i_mtime);
63	if (E2DI_HAS_XTIME(in))
64		printf("crtime %#x ", in->i_birthtime);
65	printf("BLOCKS:");
66	for (i = 0; i < (in->i_blocks <= 24 ? (in->i_blocks + 1) / 2 : 12); i++)
67		printf("  %d", in->i_db[i]);
68	printf("\n");
69	printf("Extents:\n");
70	ehp = (struct ext4_extent_header *)in->i_db;
71	printf("Header (magic 0x%x entries %d max %d depth %d gen %d)\n",
72	    ehp->eh_magic, ehp->eh_ecount, ehp->eh_max, ehp->eh_depth,
73	    ehp->eh_gen);
74	ep = (struct ext4_extent *)(char *)(ehp + 1);
75	printf("Index (blk %d len %d start_lo %d start_hi %d)\n", ep->e_blk,
76	    ep->e_len, ep->e_start_lo, ep->e_start_hi);
77	printf("\n");
78}
79#endif	/* EXT2FS_DEBUG */
80
81/*
82 *	raw ext2 inode to inode
83 */
84void
85ext2_ei2i(struct ext2fs_dinode *ei, struct inode *ip)
86{
87	int i;
88
89	ip->i_nlink = ei->e2di_nlink;
90	/*
91	 * Godmar thinks - if the link count is zero, then the inode is
92	 * unused - according to ext2 standards. Ufs marks this fact by
93	 * setting i_mode to zero - why ? I can see that this might lead to
94	 * problems in an undelete.
95	 */
96	ip->i_mode = ei->e2di_nlink ? ei->e2di_mode : 0;
97	ip->i_size = ei->e2di_size;
98	if (S_ISREG(ip->i_mode))
99		ip->i_size |= ((u_int64_t)ei->e2di_size_high) << 32;
100	ip->i_atime = ei->e2di_atime;
101	ip->i_mtime = ei->e2di_mtime;
102	ip->i_ctime = ei->e2di_ctime;
103	if (E2DI_HAS_XTIME(ip)) {
104		ip->i_atimensec = XTIME_TO_NSEC(ei->e2di_atime_extra);
105		ip->i_mtimensec = XTIME_TO_NSEC(ei->e2di_mtime_extra);
106		ip->i_ctimensec = XTIME_TO_NSEC(ei->e2di_ctime_extra);
107		ip->i_birthtime = ei->e2di_crtime;
108		ip->i_birthnsec = XTIME_TO_NSEC(ei->e2di_crtime_extra);
109	}
110	ip->i_flags = 0;
111	ip->i_flags |= (ei->e2di_flags & EXT2_APPEND) ? SF_APPEND : 0;
112	ip->i_flags |= (ei->e2di_flags & EXT2_IMMUTABLE) ? SF_IMMUTABLE : 0;
113	ip->i_flags |= (ei->e2di_flags & EXT2_NODUMP) ? UF_NODUMP : 0;
114	ip->i_flag |= (ei->e2di_flags & EXT3_INDEX) ? IN_E3INDEX : 0;
115	ip->i_flag |= (ei->e2di_flags & EXT4_EXTENTS) ? IN_E4EXTENTS : 0;
116	ip->i_blocks = ei->e2di_nblock;
117	if (E2DI_HAS_HUGE_FILE(ip)) {
118		ip->i_blocks |= (uint64_t)ei->e2di_nblock_high << 32;
119		if (ei->e2di_flags & EXT4_HUGE_FILE)
120			ip->i_blocks = fsbtodb(ip->i_e2fs, ip->i_blocks);
121	}
122	ip->i_gen = ei->e2di_gen;
123	ip->i_uid = ei->e2di_uid;
124	ip->i_gid = ei->e2di_gid;
125	/* XXX use memcpy */
126	for (i = 0; i < NDADDR; i++)
127		ip->i_db[i] = ei->e2di_blocks[i];
128	for (i = 0; i < NIADDR; i++)
129		ip->i_ib[i] = ei->e2di_blocks[EXT2_NDIR_BLOCKS + i];
130}
131
132/*
133 *	inode to raw ext2 inode
134 */
135void
136ext2_i2ei(struct inode *ip, struct ext2fs_dinode *ei)
137{
138	int i;
139
140	ei->e2di_mode = ip->i_mode;
141	ei->e2di_nlink = ip->i_nlink;
142	/*
143	 * Godmar thinks: if dtime is nonzero, ext2 says this inode has been
144	 * deleted, this would correspond to a zero link count
145	 */
146	ei->e2di_dtime = ei->e2di_nlink ? 0 : ip->i_mtime;
147	ei->e2di_size = ip->i_size;
148	if (S_ISREG(ip->i_mode))
149		ei->e2di_size_high = ip->i_size >> 32;
150	ei->e2di_atime = ip->i_atime;
151	ei->e2di_mtime = ip->i_mtime;
152	ei->e2di_ctime = ip->i_ctime;
153	if (E2DI_HAS_XTIME(ip)) {
154		ei->e2di_ctime_extra = NSEC_TO_XTIME(ip->i_ctimensec);
155		ei->e2di_mtime_extra = NSEC_TO_XTIME(ip->i_mtimensec);
156		ei->e2di_atime_extra = NSEC_TO_XTIME(ip->i_atimensec);
157		ei->e2di_crtime = ip->i_birthtime;
158		ei->e2di_crtime_extra = NSEC_TO_XTIME(ip->i_birthnsec);
159	}
160	ei->e2di_flags = 0;
161	ei->e2di_flags |= (ip->i_flags & SF_APPEND) ? EXT2_APPEND : 0;
162	ei->e2di_flags |= (ip->i_flags & SF_IMMUTABLE) ? EXT2_IMMUTABLE : 0;
163	ei->e2di_flags |= (ip->i_flags & UF_NODUMP) ? EXT2_NODUMP : 0;
164	ei->e2di_flags |= (ip->i_flag & IN_E3INDEX) ? EXT3_INDEX : 0;
165	ei->e2di_flags |= (ip->i_flag & IN_E4EXTENTS) ? EXT4_EXTENTS : 0;
166	ei->e2di_nblock = ip->i_blocks & 0xffffffff;
167	ei->e2di_nblock_high = ip->i_blocks >> 32 & 0xffff;
168	ei->e2di_gen = ip->i_gen;
169	ei->e2di_uid = ip->i_uid;
170	ei->e2di_gid = ip->i_gid;
171	/* XXX use memcpy */
172	for (i = 0; i < NDADDR; i++)
173		ei->e2di_blocks[i] = ip->i_db[i];
174	for (i = 0; i < NIADDR; i++)
175		ei->e2di_blocks[EXT2_NDIR_BLOCKS + i] = ip->i_ib[i];
176}
177