xref: /freebsd/usr.sbin/makefs/ffs/ffs_subr.c (revision 852ba100814e41898dc6e673fe0af017084e411d)
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 #include <sys/types.h>
39 
40 #include <ufs/ufs/dinode.h>
41 #include <ufs/ffs/fs.h>
42 #include "ffs/ffs_extern.h"
43 #include "ffs/ufs_bswap.h"
44 
45 /*
46  * Update the frsum fields to reflect addition or deletion
47  * of some frags.
48  */
49 void
50 ffs_fragacct_swap(struct fs *fs, int fragmap, int32_t fraglist[], int cnt, int needswap)
51 {
52 	int inblk;
53 	int field, subfield;
54 	int siz, pos;
55 
56 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
57 	fragmap <<= 1;
58 	for (siz = 1; siz < fs->fs_frag; siz++) {
59 		if ((inblk & (1 << (siz + (fs->fs_frag & (NBBY - 1))))) == 0)
60 			continue;
61 		field = around[siz];
62 		subfield = inside[siz];
63 		for (pos = siz; pos <= fs->fs_frag; pos++) {
64 			if ((fragmap & field) == subfield) {
65 				fraglist[siz] = ufs_rw32(
66 				    ufs_rw32(fraglist[siz], needswap) + cnt,
67 				    needswap);
68 				pos += siz;
69 				field <<= siz;
70 				subfield <<= siz;
71 			}
72 			field <<= 1;
73 			subfield <<= 1;
74 		}
75 	}
76 }
77 
78 /*
79  * block operations
80  *
81  * check if a block is available
82  *  returns true if all the corresponding bits in the free map are 1
83  *  returns false if any corresponding bit in the free map is 0
84  */
85 int
86 ffs_isblock(fs, cp, h)
87 	struct fs *fs;
88 	u_char *cp;
89 	int32_t h;
90 {
91 	u_char mask;
92 
93 	switch ((int)fs->fs_fragshift) {
94 	case 3:
95 		return (cp[h] == 0xff);
96 	case 2:
97 		mask = 0x0f << ((h & 0x1) << 2);
98 		return ((cp[h >> 1] & mask) == mask);
99 	case 1:
100 		mask = 0x03 << ((h & 0x3) << 1);
101 		return ((cp[h >> 2] & mask) == mask);
102 	case 0:
103 		mask = 0x01 << (h & 0x7);
104 		return ((cp[h >> 3] & mask) == mask);
105 	default:
106 		panic("ffs_isblock: unknown fs_fragshift %d",
107 		    (int)fs->fs_fragshift);
108 	}
109 }
110 
111 /*
112  * check if a block is completely allocated
113  *  returns true if all the corresponding bits in the free map are 0
114  *  returns false if any corresponding bit in the free map is 1
115  */
116 int
117 ffs_isfreeblock(fs, cp, h)
118 	struct fs *fs;
119 	u_char *cp;
120 	int32_t h;
121 {
122 
123 	switch ((int)fs->fs_fragshift) {
124 	case 3:
125 		return (cp[h] == 0);
126 	case 2:
127 		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
128 	case 1:
129 		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
130 	case 0:
131 		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
132 	default:
133 		panic("ffs_isfreeblock: unknown fs_fragshift %d",
134 		    (int)fs->fs_fragshift);
135 	}
136 }
137 
138 /*
139  * take a block out of the map
140  */
141 void
142 ffs_clrblock(fs, cp, h)
143 	struct fs *fs;
144 	u_char *cp;
145 	int32_t h;
146 {
147 
148 	switch ((int)fs->fs_fragshift) {
149 	case 3:
150 		cp[h] = 0;
151 		return;
152 	case 2:
153 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
154 		return;
155 	case 1:
156 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
157 		return;
158 	case 0:
159 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
160 		return;
161 	default:
162 		panic("ffs_clrblock: unknown fs_fragshift %d",
163 		    (int)fs->fs_fragshift);
164 	}
165 }
166 
167 /*
168  * put a block into the map
169  */
170 void
171 ffs_setblock(fs, cp, h)
172 	struct fs *fs;
173 	u_char *cp;
174 	int32_t h;
175 {
176 
177 	switch ((int)fs->fs_fragshift) {
178 	case 3:
179 		cp[h] = 0xff;
180 		return;
181 	case 2:
182 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
183 		return;
184 	case 1:
185 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
186 		return;
187 	case 0:
188 		cp[h >> 3] |= (0x01 << (h & 0x7));
189 		return;
190 	default:
191 		panic("ffs_setblock: unknown fs_fragshift %d",
192 		    (int)fs->fs_fragshift);
193 	}
194 }
195