xref: /netbsd-src/sys/lib/libsa/ffs_bswap.c (revision 13c625776cdaa3a0372c89d80872435957682950)
1 /*	$NetBSD: ffs_bswap.c,v 1.1 2021/05/27 06:54:44 mrg Exp $	*/
2 
3 /*
4  * Copyright (c) 1998 Manuel Bouyer.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 /*
29  * This copy is a minimal version for libsa and it's ufs.c.  The unused
30  * functions are removed, and additional swapping is performed on the
31  * di_extb, di_db, and di_ib arrays.
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ffs_bswap.c,v 1.1 2021/05/27 06:54:44 mrg Exp $");
36 
37 #include <sys/param.h>
38 #include <lib/libkern/libkern.h>
39 
40 #include <ufs/ufs/dinode.h>
41 #include <ufs/ufs/quota.h>
42 #include <ufs/ufs/ufs_bswap.h>
43 #include <ufs/ffs/fs.h>
44 #include <ufs/ffs/ffs_extern.h>
45 
46 static void
libsa_ffs_csumtotal_swap(const struct csum_total * o,struct csum_total * n)47 libsa_ffs_csumtotal_swap(const struct csum_total *o, struct csum_total *n)
48 {
49 	n->cs_ndir = bswap64(o->cs_ndir);
50 	n->cs_nbfree = bswap64(o->cs_nbfree);
51 	n->cs_nifree = bswap64(o->cs_nifree);
52 	n->cs_nffree = bswap64(o->cs_nffree);
53 }
54 
55 void
ffs_sb_swap(const struct fs * o,struct fs * n)56 ffs_sb_swap(const struct fs *o, struct fs *n)
57 {
58 	size_t i;
59 	const u_int32_t *o32;
60 	u_int32_t *n32;
61 
62 	/*
63 	 * In order to avoid a lot of lines, as the first N fields (52)
64 	 * of the superblock up to fs_fmod are u_int32_t, we just loop
65 	 * here to convert them.
66 	 */
67 	o32 = (const u_int32_t *)o;
68 	n32 = (u_int32_t *)n;
69 	for (i = 0; i < offsetof(struct fs, fs_fmod) / sizeof(u_int32_t); i++)
70 		n32[i] = bswap32(o32[i]);
71 
72 	n->fs_swuid = bswap64(o->fs_swuid);
73 	n->fs_cgrotor = bswap32(o->fs_cgrotor); /* Unused */
74 	n->fs_old_cpc = bswap32(o->fs_old_cpc);
75 
76 	/* These fields overlap with a possible location for the
77 	 * historic FS_DYNAMICPOSTBLFMT postbl table, and with the
78 	 * first half of the historic FS_42POSTBLFMT postbl table.
79 	 */
80 	n->fs_maxbsize = bswap32(o->fs_maxbsize);
81 	/* XXX journal */
82 	n->fs_quota_magic = bswap32(o->fs_quota_magic);
83 	for (i = 0; i < MAXQUOTAS; i++)
84 		n->fs_quotafile[i] = bswap64(o->fs_quotafile[i]);
85 	n->fs_sblockloc = bswap64(o->fs_sblockloc);
86 	libsa_ffs_csumtotal_swap(&o->fs_cstotal, &n->fs_cstotal);
87 	n->fs_time = bswap64(o->fs_time);
88 	n->fs_size = bswap64(o->fs_size);
89 	n->fs_dsize = bswap64(o->fs_dsize);
90 	n->fs_csaddr = bswap64(o->fs_csaddr);
91 	n->fs_pendingblocks = bswap64(o->fs_pendingblocks);
92 	n->fs_pendinginodes = bswap32(o->fs_pendinginodes);
93 
94 	/* These fields overlap with the second half of the
95 	 * historic FS_42POSTBLFMT postbl table
96 	 */
97 	for (i = 0; i < FSMAXSNAP; i++)
98 		n->fs_snapinum[i] = bswap32(o->fs_snapinum[i]);
99 	n->fs_avgfilesize = bswap32(o->fs_avgfilesize);
100 	n->fs_avgfpdir = bswap32(o->fs_avgfpdir);
101 	/* fs_sparecon[28] - ignore for now */
102 	n->fs_flags = bswap32(o->fs_flags);
103 	n->fs_contigsumsize = bswap32(o->fs_contigsumsize);
104 	n->fs_maxsymlinklen = bswap32(o->fs_maxsymlinklen);
105 	n->fs_old_inodefmt = bswap32(o->fs_old_inodefmt);
106 	n->fs_maxfilesize = bswap64(o->fs_maxfilesize);
107 	n->fs_qbmask = bswap64(o->fs_qbmask);
108 	n->fs_qfmask = bswap64(o->fs_qfmask);
109 	n->fs_state = bswap32(o->fs_state);
110 	n->fs_old_postblformat = bswap32(o->fs_old_postblformat);
111 	n->fs_old_nrpos = bswap32(o->fs_old_nrpos);
112 	n->fs_old_postbloff = bswap32(o->fs_old_postbloff);
113 	n->fs_old_rotbloff = bswap32(o->fs_old_rotbloff);
114 
115 	n->fs_magic = bswap32(o->fs_magic);
116 }
117 
118 void
ffs_dinode1_swap(struct ufs1_dinode * o,struct ufs1_dinode * n)119 ffs_dinode1_swap(struct ufs1_dinode *o, struct ufs1_dinode *n)
120 {
121 	size_t i;
122 
123 	n->di_mode = bswap16(o->di_mode);
124 	n->di_nlink = bswap16(o->di_nlink);
125 	n->di_oldids[0] = bswap16(o->di_oldids[0]);
126 	n->di_oldids[1] = bswap16(o->di_oldids[1]);
127 	n->di_size = bswap64(o->di_size);
128 	n->di_atime = bswap32(o->di_atime);
129 	n->di_atimensec = bswap32(o->di_atimensec);
130 	n->di_mtime = bswap32(o->di_mtime);
131 	n->di_mtimensec = bswap32(o->di_mtimensec);
132 	n->di_ctime = bswap32(o->di_ctime);
133 	n->di_ctimensec = bswap32(o->di_ctimensec);
134 	/* Swap these here, unlike kernel version .*/
135 	for (i = 0; i < UFS_NDADDR; i++)
136 		n->di_db[i] = bswap32(o->di_db[i]);
137 	for (i = 0; i < UFS_NIADDR; i++)
138 		n->di_ib[i] = bswap32(o->di_ib[i]);
139 	n->di_flags = bswap32(o->di_flags);
140 	n->di_blocks = bswap32(o->di_blocks);
141 	n->di_gen = bswap32(o->di_gen);
142 	n->di_uid = bswap32(o->di_uid);
143 	n->di_gid = bswap32(o->di_gid);
144 }
145 
146 void
ffs_dinode2_swap(struct ufs2_dinode * o,struct ufs2_dinode * n)147 ffs_dinode2_swap(struct ufs2_dinode *o, struct ufs2_dinode *n)
148 {
149 	size_t i;
150 
151 	n->di_mode = bswap16(o->di_mode);
152 	n->di_nlink = bswap16(o->di_nlink);
153 	n->di_uid = bswap32(o->di_uid);
154 	n->di_gid = bswap32(o->di_gid);
155 	n->di_blksize = bswap32(o->di_blksize);
156 	n->di_size = bswap64(o->di_size);
157 	n->di_blocks = bswap64(o->di_blocks);
158 	n->di_atime = bswap64(o->di_atime);
159 	n->di_atimensec = bswap32(o->di_atimensec);
160 	n->di_mtime = bswap64(o->di_mtime);
161 	n->di_mtimensec = bswap32(o->di_mtimensec);
162 	n->di_ctime = bswap64(o->di_ctime);
163 	n->di_ctimensec = bswap32(o->di_ctimensec);
164 	n->di_birthtime = bswap64(o->di_birthtime);
165 	n->di_birthnsec = bswap32(o->di_birthnsec);
166 	n->di_gen = bswap32(o->di_gen);
167 	n->di_kernflags = bswap32(o->di_kernflags);
168 	n->di_flags = bswap32(o->di_flags);
169 	n->di_extsize = bswap32(o->di_extsize);
170 	/* Swap these here, unlike kernel version .*/
171 	for (i = 0; i < UFS_NXADDR; i++)
172 		n->di_extb[i] = bswap64(o->di_extb[i]);
173 	for (i = 0; i < UFS_NDADDR; i++)
174 		n->di_db[i] = bswap64(o->di_db[i]);
175 	for (i = 0; i < UFS_NIADDR; i++)
176 		n->di_ib[i] = bswap64(o->di_ib[i]);
177 }
178