xref: /openbsd/sys/ufs/ext2fs/ext2fs_bswap.c (revision d31d4a6e)
1 /*	$OpenBSD: ext2fs_bswap.c,v 1.8 2014/07/31 17:37:52 pelikan Exp $	*/
2 /*	$NetBSD: ext2fs_bswap.c,v 1.6 2000/07/24 00:23:10 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Manuel Bouyer.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 
36 #include <sys/types.h>
37 #if defined(_KERNEL)
38 #include <sys/systm.h>
39 #endif
40 
41 #include <ufs/ext2fs/ext2fs.h>
42 #include <ufs/ext2fs/ext2fs_dinode.h>
43 
44 #if !defined(_KERNEL)
45 #include <string.h>
46 #endif
47 
48 /* These functions are only needed if native byte order is not big endian */
49 #if BYTE_ORDER == BIG_ENDIAN
50 void
e2fs_sb_bswap(struct ext2fs * old,struct ext2fs * new)51 e2fs_sb_bswap(struct ext2fs *old, struct ext2fs *new)
52 {
53 	/* preserve unused fields */
54 	memcpy(new, old, sizeof(struct ext2fs));
55 	new->e2fs_icount	=	swap32(old->e2fs_icount);
56 	new->e2fs_bcount	=	swap32(old->e2fs_bcount);
57 	new->e2fs_rbcount	=	swap32(old->e2fs_rbcount);
58 	new->e2fs_fbcount	=	swap32(old->e2fs_fbcount);
59 	new->e2fs_ficount	=	swap32(old->e2fs_ficount);
60 	new->e2fs_first_dblock	=	swap32(old->e2fs_first_dblock);
61 	new->e2fs_log_bsize	=	swap32(old->e2fs_log_bsize);
62 	new->e2fs_log_fsize	=	swap32(old->e2fs_log_fsize);
63 	new->e2fs_bpg		=	swap32(old->e2fs_bpg);
64 	new->e2fs_fpg		=	swap32(old->e2fs_fpg);
65 	new->e2fs_ipg		=	swap32(old->e2fs_ipg);
66 	new->e2fs_mtime		=	swap32(old->e2fs_mtime);
67 	new->e2fs_wtime		=	swap32(old->e2fs_wtime);
68 	new->e2fs_mnt_count	=	swap16(old->e2fs_mnt_count);
69 	new->e2fs_max_mnt_count	=	swap16(old->e2fs_max_mnt_count);
70 	new->e2fs_magic		=	swap16(old->e2fs_magic);
71 	new->e2fs_state		=	swap16(old->e2fs_state);
72 	new->e2fs_beh		=	swap16(old->e2fs_beh);
73 	new->e2fs_minrev	=	swap16(old->e2fs_minrev);
74 	new->e2fs_lastfsck	=	swap32(old->e2fs_lastfsck);
75 	new->e2fs_fsckintv	=	swap32(old->e2fs_fsckintv);
76 	new->e2fs_creator	=	swap32(old->e2fs_creator);
77 	new->e2fs_rev		=	swap32(old->e2fs_rev);
78 	new->e2fs_ruid		=	swap16(old->e2fs_ruid);
79 	new->e2fs_rgid		=	swap16(old->e2fs_rgid);
80 	new->e2fs_first_ino	=	swap32(old->e2fs_first_ino);
81 	new->e2fs_inode_size	=	swap16(old->e2fs_inode_size);
82 	new->e2fs_block_group_nr =	swap16(old->e2fs_block_group_nr);
83 	new->e2fs_features_compat =	swap32(old->e2fs_features_compat);
84 	new->e2fs_features_incompat =	swap32(old->e2fs_features_incompat);
85 	new->e2fs_features_rocompat =	swap32(old->e2fs_features_rocompat);
86 	new->e2fs_algo		=	swap32(old->e2fs_algo);
87 
88 	/* SOME journaling-related fields. */
89 	new->e2fs_journal_ino	=	swap32(old->e2fs_journal_ino);
90 	new->e2fs_journal_dev	=	swap32(old->e2fs_journal_dev);
91 	new->e2fs_last_orphan	=	swap32(old->e2fs_last_orphan);
92 	new->e2fs_gdesc_size	=	swap16(old->e2fs_gdesc_size);
93 	new->e2fs_default_mount_opts	=	swap32(old->e2fs_default_mount_opts);
94 	new->e2fs_first_meta_bg	=	swap32(old->e2fs_first_meta_bg);
95 	new->e2fs_mkfs_time	=	swap32(old->e2fs_mkfs_time);
96 }
97 
98 void
e2fs_cg_bswap(struct ext2_gd * old,struct ext2_gd * new,int size)99 e2fs_cg_bswap(struct ext2_gd *old, struct ext2_gd *new, int size)
100 {
101 	int i;
102 	for (i=0; i < (size / sizeof(struct  ext2_gd)); i++) {
103 		new[i].ext2bgd_b_bitmap	= swap32(old[i].ext2bgd_b_bitmap);
104 		new[i].ext2bgd_i_bitmap	= swap32(old[i].ext2bgd_i_bitmap);
105 		new[i].ext2bgd_i_tables	= swap32(old[i].ext2bgd_i_tables);
106 		new[i].ext2bgd_nbfree	= swap16(old[i].ext2bgd_nbfree);
107 		new[i].ext2bgd_nifree	= swap16(old[i].ext2bgd_nifree);
108 		new[i].ext2bgd_ndirs	= swap16(old[i].ext2bgd_ndirs);
109 	}
110 }
111 
112 void
e2fs_i_bswap(struct m_ext2fs * fs,struct ext2fs_dinode * old,struct ext2fs_dinode * new)113 e2fs_i_bswap(struct m_ext2fs *fs, struct ext2fs_dinode *old,
114     struct ext2fs_dinode *new)
115 {
116 	new->e2di_mode		=	swap16(old->e2di_mode);
117 	new->e2di_uid_low	=	swap16(old->e2di_uid_low);
118 	new->e2di_gid_low	=	swap16(old->e2di_gid_low);
119 	new->e2di_uid_high	=	swap16(old->e2di_uid_high);
120 	new->e2di_gid_high	=	swap16(old->e2di_gid_high);
121 	new->e2di_nlink		=	swap16(old->e2di_nlink);
122 	new->e2di_size		=	swap32(old->e2di_size);
123 	new->e2di_atime		=	swap32(old->e2di_atime);
124 	new->e2di_ctime		=	swap32(old->e2di_ctime);
125 	new->e2di_mtime		=	swap32(old->e2di_mtime);
126 	new->e2di_dtime		=	swap32(old->e2di_dtime);
127 	new->e2di_nblock	=	swap32(old->e2di_nblock);
128 	new->e2di_flags		=	swap32(old->e2di_flags);
129 	new->e2di_gen		=	swap32(old->e2di_gen);
130 	new->e2di_facl		=	swap32(old->e2di_facl);
131 	new->e2di_size_hi	=	swap32(old->e2di_size_hi);
132 	new->e2di_faddr		=	swap32(old->e2di_faddr);
133 	new->e2di_nblock_hi	=	swap16(old->e2di_nblock_hi);
134 	new->e2di_facl_hi	=	swap16(old->e2di_facl_hi);
135 	memcpy(&new->e2di_blocks[0], &old->e2di_blocks[0],
136 		(NDADDR+NIADDR) * sizeof(int));
137 
138 	if (EXT2_DINODE_SIZE(fs) <= EXT2_REV0_DINODE_SIZE)
139 		return;
140 	new->e2di_isize		=	swap16(old->e2di_isize);
141 }
142 #endif
143