xref: /freebsd/sys/fs/ext2fs/ext2_inode_cnv.c (revision a9d1b2999545765bd24d17ee614eec4f739d19ba)
1e09c00caSUlf Lilleengen /*-
2e09c00caSUlf Lilleengen  * Copyright (c) 1995 The University of Utah and
3e09c00caSUlf Lilleengen  * the Computer Systems Laboratory at the University of Utah (CSL).
4e09c00caSUlf Lilleengen  * All rights reserved.
5e09c00caSUlf Lilleengen  *
6e09c00caSUlf Lilleengen  * Permission to use, copy, modify and distribute this software is hereby
7e09c00caSUlf Lilleengen  * granted provided that (1) source code retains these copyright, permission,
8e09c00caSUlf Lilleengen  * and disclaimer notices, and (2) redistributions including binaries
9e09c00caSUlf Lilleengen  * reproduce the notices in supporting documentation, and (3) all advertising
10e09c00caSUlf Lilleengen  * materials mentioning features or use of this software display the following
11e09c00caSUlf Lilleengen  * acknowledgement: ``This product includes software developed by the
12e09c00caSUlf Lilleengen  * Computer Systems Laboratory at the University of Utah.''
13e09c00caSUlf Lilleengen  *
14e09c00caSUlf Lilleengen  * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
15e09c00caSUlf Lilleengen  * IS" CONDITION.  THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
16e09c00caSUlf Lilleengen  * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17e09c00caSUlf Lilleengen  *
18e09c00caSUlf Lilleengen  * CSL requests users of this software to return to csl-dist@cs.utah.edu any
19e09c00caSUlf Lilleengen  * improvements that they make and grant CSL redistribution rights.
20e09c00caSUlf Lilleengen  *
21e09c00caSUlf Lilleengen  *      Utah $Hdr$
22e09c00caSUlf Lilleengen  * $FreeBSD$
23e09c00caSUlf Lilleengen  */
24e09c00caSUlf Lilleengen 
25e09c00caSUlf Lilleengen /*
26e09c00caSUlf Lilleengen  * routines to convert on disk ext2 inodes into inodes and back
27e09c00caSUlf Lilleengen  */
28e09c00caSUlf Lilleengen #include <sys/param.h>
29e09c00caSUlf Lilleengen #include <sys/systm.h>
30e28f5d52SPedro F. Giffuni #include <sys/endian.h>
31e09c00caSUlf Lilleengen #include <sys/lock.h>
32e09c00caSUlf Lilleengen #include <sys/stat.h>
33e09c00caSUlf Lilleengen #include <sys/vnode.h>
34e09c00caSUlf Lilleengen 
35e09c00caSUlf Lilleengen #include <fs/ext2fs/inode.h>
36e09c00caSUlf Lilleengen #include <fs/ext2fs/ext2fs.h>
37e09c00caSUlf Lilleengen #include <fs/ext2fs/ext2_dinode.h>
38e28f5d52SPedro F. Giffuni #include <fs/ext2fs/ext2_extern.h>
39e09c00caSUlf Lilleengen 
40035e4e04SPedro F. Giffuni #define XTIME_TO_NSEC(x)	((x & EXT3_NSEC_MASK) >> 2)
41e28f5d52SPedro F. Giffuni #define NSEC_TO_XTIME(t)	(le32toh(t << 2) & EXT3_NSEC_MASK)
42035e4e04SPedro F. Giffuni 
43e09c00caSUlf Lilleengen void
44*a9d1b299SPedro F. Giffuni ext2_print_inode(struct inode *in)
45e09c00caSUlf Lilleengen {
46e09c00caSUlf Lilleengen 	int i;
47e09c00caSUlf Lilleengen 
48fc8fdae0SMatthew D Fleming 	printf( "Inode: %5ju", (uintmax_t)in->i_number);
49e09c00caSUlf Lilleengen 	printf( /* "Inode: %5d" */
50e09c00caSUlf Lilleengen 		" Type: %10s Mode: 0x%o Flags: 0x%x  Version: %d\n",
51e09c00caSUlf Lilleengen 		"n/a", in->i_mode, in->i_flags, in->i_gen);
52e09c00caSUlf Lilleengen 	printf( "User: %5lu Group: %5lu  Size: %lu\n",
53e09c00caSUlf Lilleengen 		(unsigned long)in->i_uid, (unsigned long)in->i_gid,
54e09c00caSUlf Lilleengen 		(unsigned long)in->i_size);
55e09c00caSUlf Lilleengen 	printf( "Links: %3d Blockcount: %d\n",
56e09c00caSUlf Lilleengen 		in->i_nlink, in->i_blocks);
57e09c00caSUlf Lilleengen 	printf( "ctime: 0x%x", in->i_ctime);
58e09c00caSUlf Lilleengen 	printf( "atime: 0x%x", in->i_atime);
59e09c00caSUlf Lilleengen 	printf( "mtime: 0x%x", in->i_mtime);
60e09c00caSUlf Lilleengen 	printf( "BLOCKS: ");
61e09c00caSUlf Lilleengen 	for(i=0; i < (in->i_blocks <= 24 ? ((in->i_blocks+1)/2): 12); i++)
62e09c00caSUlf Lilleengen 		printf("%d ", in->i_db[i]);
63e09c00caSUlf Lilleengen 	printf("\n");
64e09c00caSUlf Lilleengen }
65e09c00caSUlf Lilleengen 
66e09c00caSUlf Lilleengen /*
67e09c00caSUlf Lilleengen  *	raw ext2 inode to inode
68e09c00caSUlf Lilleengen  */
69e09c00caSUlf Lilleengen void
70*a9d1b299SPedro F. Giffuni ext2_ei2i(struct ext2fs_dinode *ei, struct inode *ip)
71e09c00caSUlf Lilleengen {
72e09c00caSUlf Lilleengen         int i;
73e09c00caSUlf Lilleengen 
74e09c00caSUlf Lilleengen 	ip->i_nlink = ei->e2di_nlink;
75e09c00caSUlf Lilleengen 	/* Godmar thinks - if the link count is zero, then the inode is
76e09c00caSUlf Lilleengen 	   unused - according to ext2 standards. Ufs marks this fact
77e09c00caSUlf Lilleengen 	   by setting i_mode to zero - why ?
78e09c00caSUlf Lilleengen 	   I can see that this might lead to problems in an undelete.
79e09c00caSUlf Lilleengen 	*/
80e09c00caSUlf Lilleengen 	ip->i_mode = ei->e2di_nlink ? ei->e2di_mode : 0;
81e09c00caSUlf Lilleengen 	ip->i_size = ei->e2di_size;
82e09c00caSUlf Lilleengen 	if (S_ISREG(ip->i_mode))
83e09c00caSUlf Lilleengen 		ip->i_size |= ((u_int64_t)ei->e2di_size_high) << 32;
84e09c00caSUlf Lilleengen 	ip->i_atime = ei->e2di_atime;
85e09c00caSUlf Lilleengen 	ip->i_mtime = ei->e2di_mtime;
86e09c00caSUlf Lilleengen 	ip->i_ctime = ei->e2di_ctime;
87035e4e04SPedro F. Giffuni 	if (E2DI_HAS_XTIME(ip)) {
88035e4e04SPedro F. Giffuni 		ip->i_atimensec = XTIME_TO_NSEC(ei->e2di_atime_extra);
89035e4e04SPedro F. Giffuni 		ip->i_mtimensec = XTIME_TO_NSEC(ei->e2di_mtime_extra);
90035e4e04SPedro F. Giffuni 		ip->i_ctimensec = XTIME_TO_NSEC(ei->e2di_ctime_extra);
91035e4e04SPedro F. Giffuni 		ip->i_birthtime = ei->e2di_crtime;
92035e4e04SPedro F. Giffuni 		ip->i_birthnsec = XTIME_TO_NSEC(ei->e2di_crtime_extra);
93035e4e04SPedro F. Giffuni 	}
94e09c00caSUlf Lilleengen 	ip->i_flags = 0;
95e09c00caSUlf Lilleengen 	ip->i_flags |= (ei->e2di_flags & EXT2_APPEND) ? SF_APPEND : 0;
96e09c00caSUlf Lilleengen 	ip->i_flags |= (ei->e2di_flags & EXT2_IMMUTABLE) ? SF_IMMUTABLE : 0;
97e09c00caSUlf Lilleengen 	ip->i_flags |= (ei->e2di_flags & EXT2_NODUMP) ? UF_NODUMP : 0;
98e09c00caSUlf Lilleengen 	ip->i_blocks = ei->e2di_nblock;
99e09c00caSUlf Lilleengen 	ip->i_gen = ei->e2di_gen;
100e09c00caSUlf Lilleengen 	ip->i_uid = ei->e2di_uid;
101e09c00caSUlf Lilleengen 	ip->i_gid = ei->e2di_gid;
102e09c00caSUlf Lilleengen 	/* XXX use memcpy */
103e09c00caSUlf Lilleengen 	for(i = 0; i < NDADDR; i++)
104e09c00caSUlf Lilleengen 		ip->i_db[i] = ei->e2di_blocks[i];
105e09c00caSUlf Lilleengen 	for(i = 0; i < NIADDR; i++)
106e09c00caSUlf Lilleengen 		ip->i_ib[i] = ei->e2di_blocks[EXT2_NDIR_BLOCKS + i];
107e09c00caSUlf Lilleengen }
108e09c00caSUlf Lilleengen 
109e09c00caSUlf Lilleengen /*
110e09c00caSUlf Lilleengen  *	inode to raw ext2 inode
111e09c00caSUlf Lilleengen  */
112e09c00caSUlf Lilleengen void
113*a9d1b299SPedro F. Giffuni ext2_i2ei(struct inode *ip, struct ext2fs_dinode *ei)
114e09c00caSUlf Lilleengen {
115e09c00caSUlf Lilleengen 	int i;
116e09c00caSUlf Lilleengen 
117e09c00caSUlf Lilleengen 	ei->e2di_mode = ip->i_mode;
118e09c00caSUlf Lilleengen         ei->e2di_nlink = ip->i_nlink;
119e09c00caSUlf Lilleengen 	/*
120e09c00caSUlf Lilleengen 	   Godmar thinks: if dtime is nonzero, ext2 says this inode
121e09c00caSUlf Lilleengen 	   has been deleted, this would correspond to a zero link count
122e09c00caSUlf Lilleengen 	 */
123e09c00caSUlf Lilleengen 	ei->e2di_dtime = ei->e2di_nlink ? 0 : ip->i_mtime;
124e09c00caSUlf Lilleengen 	ei->e2di_size = ip->i_size;
125e09c00caSUlf Lilleengen 	if (S_ISREG(ip->i_mode))
126e09c00caSUlf Lilleengen 		ei->e2di_size_high = ip->i_size >> 32;
127e09c00caSUlf Lilleengen 	ei->e2di_atime = ip->i_atime;
128e09c00caSUlf Lilleengen 	ei->e2di_mtime = ip->i_mtime;
129e09c00caSUlf Lilleengen 	ei->e2di_ctime = ip->i_ctime;
130035e4e04SPedro F. Giffuni 	if (E2DI_HAS_XTIME(ip)) {
131035e4e04SPedro F. Giffuni 		ei->e2di_ctime_extra = NSEC_TO_XTIME(ip->i_ctimensec);
132035e4e04SPedro F. Giffuni 		ei->e2di_mtime_extra = NSEC_TO_XTIME(ip->i_mtimensec);
133035e4e04SPedro F. Giffuni 		ei->e2di_atime_extra = NSEC_TO_XTIME(ip->i_atimensec);
134035e4e04SPedro F. Giffuni 		ei->e2di_crtime = ip->i_birthtime;
135035e4e04SPedro F. Giffuni 		ei->e2di_crtime_extra = NSEC_TO_XTIME(ip->i_birthnsec);
136035e4e04SPedro F. Giffuni 	}
137e09c00caSUlf Lilleengen 	ei->e2di_flags = ip->i_flags;
138e09c00caSUlf Lilleengen 	ei->e2di_flags = 0;
139e09c00caSUlf Lilleengen 	ei->e2di_flags |= (ip->i_flags & SF_APPEND) ? EXT2_APPEND: 0;
140e09c00caSUlf Lilleengen 	ei->e2di_flags |= (ip->i_flags & SF_IMMUTABLE) ? EXT2_IMMUTABLE: 0;
141e09c00caSUlf Lilleengen 	ei->e2di_flags |= (ip->i_flags & UF_NODUMP) ? EXT2_NODUMP: 0;
142e09c00caSUlf Lilleengen 	ei->e2di_nblock = ip->i_blocks;
143e09c00caSUlf Lilleengen 	ei->e2di_gen = ip->i_gen;
144e09c00caSUlf Lilleengen 	ei->e2di_uid = ip->i_uid;
145e09c00caSUlf Lilleengen 	ei->e2di_gid = ip->i_gid;
146e09c00caSUlf Lilleengen 	/* XXX use memcpy */
147e09c00caSUlf Lilleengen 	for(i = 0; i < NDADDR; i++)
148e09c00caSUlf Lilleengen 		ei->e2di_blocks[i] = ip->i_db[i];
149e09c00caSUlf Lilleengen 	for(i = 0; i < NIADDR; i++)
150e09c00caSUlf Lilleengen 		ei->e2di_blocks[EXT2_NDIR_BLOCKS + i] = ip->i_ib[i];
151e09c00caSUlf Lilleengen }
152