xref: /freebsd/usr.sbin/makefs/ffs/ffs_subr.c (revision e0c4386e)
1 /*	$NetBSD: ffs_subr.c,v 1.32 2003/12/30 12:33:24 pk Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 1982, 1986, 1989, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/types.h>
36 
37 #include <ufs/ufs/dinode.h>
38 #include <ufs/ffs/fs.h>
39 #include "ffs/ffs_extern.h"
40 #include "ffs/ufs_bswap.h"
41 
42 /*
43  * Update the frsum fields to reflect addition or deletion
44  * of some frags.
45  */
46 void
47 ffs_fragacct_swap(struct fs *fs, int fragmap, uint32_t fraglist[], int cnt, int needswap)
48 {
49 	int inblk;
50 	int field, subfield;
51 	int siz, pos;
52 
53 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
54 	fragmap <<= 1;
55 	for (siz = 1; siz < fs->fs_frag; siz++) {
56 		if ((inblk & (1 << (siz + (fs->fs_frag & (NBBY - 1))))) == 0)
57 			continue;
58 		field = around[siz];
59 		subfield = inside[siz];
60 		for (pos = siz; pos <= fs->fs_frag; pos++) {
61 			if ((fragmap & field) == subfield) {
62 				fraglist[siz] = ufs_rw32(
63 				    ufs_rw32(fraglist[siz], needswap) + cnt,
64 				    needswap);
65 				pos += siz;
66 				field <<= siz;
67 				subfield <<= siz;
68 			}
69 			field <<= 1;
70 			subfield <<= 1;
71 		}
72 	}
73 }
74 
75 /*
76  * block operations
77  *
78  * check if a block is available
79  *  returns true if all the corresponding bits in the free map are 1
80  *  returns false if any corresponding bit in the free map is 0
81  */
82 int
83 ffs_isblock(struct fs *fs, u_char *cp, int32_t h)
84 {
85 	u_char mask;
86 
87 	switch ((int)fs->fs_fragshift) {
88 	case 3:
89 		return (cp[h] == 0xff);
90 	case 2:
91 		mask = 0x0f << ((h & 0x1) << 2);
92 		return ((cp[h >> 1] & mask) == mask);
93 	case 1:
94 		mask = 0x03 << ((h & 0x3) << 1);
95 		return ((cp[h >> 2] & mask) == mask);
96 	case 0:
97 		mask = 0x01 << (h & 0x7);
98 		return ((cp[h >> 3] & mask) == mask);
99 	default:
100 		panic("ffs_isblock: unknown fs_fragshift %d",
101 		    (int)fs->fs_fragshift);
102 	}
103 }
104 
105 /*
106  * check if a block is completely allocated
107  *  returns true if all the corresponding bits in the free map are 0
108  *  returns false if any corresponding bit in the free map is 1
109  */
110 int
111 ffs_isfreeblock(struct fs *fs, u_char *cp, int32_t h)
112 {
113 
114 	switch ((int)fs->fs_fragshift) {
115 	case 3:
116 		return (cp[h] == 0);
117 	case 2:
118 		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
119 	case 1:
120 		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
121 	case 0:
122 		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
123 	default:
124 		panic("ffs_isfreeblock: unknown fs_fragshift %d",
125 		    (int)fs->fs_fragshift);
126 	}
127 }
128 
129 /*
130  * take a block out of the map
131  */
132 void
133 ffs_clrblock(struct fs *fs, u_char *cp, int32_t h)
134 {
135 
136 	switch ((int)fs->fs_fragshift) {
137 	case 3:
138 		cp[h] = 0;
139 		return;
140 	case 2:
141 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
142 		return;
143 	case 1:
144 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
145 		return;
146 	case 0:
147 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
148 		return;
149 	default:
150 		panic("ffs_clrblock: unknown fs_fragshift %d",
151 		    (int)fs->fs_fragshift);
152 	}
153 }
154 
155 /*
156  * put a block into the map
157  */
158 void
159 ffs_setblock(struct fs *fs, u_char *cp, int32_t h)
160 {
161 
162 	switch ((int)fs->fs_fragshift) {
163 	case 3:
164 		cp[h] = 0xff;
165 		return;
166 	case 2:
167 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
168 		return;
169 	case 1:
170 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
171 		return;
172 	case 0:
173 		cp[h >> 3] |= (0x01 << (h & 0x7));
174 		return;
175 	default:
176 		panic("ffs_setblock: unknown fs_fragshift %d",
177 		    (int)fs->fs_fragshift);
178 	}
179 }
180