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