1185222Ssam/*	$NetBSD: ffs_subr.c,v 1.32 2003/12/30 12:33:24 pk Exp $	*/
2185222Ssam
3185222Ssam/*
4185222Ssam * Copyright (c) 1982, 1986, 1989, 1993
5185222Ssam *	The Regents of the University of California.  All rights reserved.
6185222Ssam *
7185222Ssam * Redistribution and use in source and binary forms, with or without
8185222Ssam * modification, are permitted provided that the following conditions
9185222Ssam * are met:
10185222Ssam * 1. Redistributions of source code must retain the above copyright
11185222Ssam *    notice, this list of conditions and the following disclaimer.
12185222Ssam * 2. Redistributions in binary form must reproduce the above copyright
13185222Ssam *    notice, this list of conditions and the following disclaimer in the
14185222Ssam *    documentation and/or other materials provided with the distribution.
15185222Ssam * 3. Neither the name of the University nor the names of its contributors
16185222Ssam *    may be used to endorse or promote products derived from this software
17185222Ssam *    without specific prior written permission.
18185222Ssam *
19185222Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20185222Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21185222Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22185222Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23185222Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24185222Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25185222Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26185222Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27185222Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28185222Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29185222Ssam * SUCH DAMAGE.
30185222Ssam *
31185222Ssam *	@(#)ffs_subr.c	8.5 (Berkeley) 3/21/95
32185222Ssam */
33185222Ssam
34185222Ssam#include <sys/cdefs.h>
35186261Ssam__FBSDID("$FreeBSD$");
36185222Ssam
37185222Ssam#include <sys/param.h>
38185222Ssam
39185222Ssam#include <ufs/ufs/dinode.h>
40185222Ssam#include <ufs/ffs/fs.h>
41248293Sbrooks#include "ffs/ffs_extern.h"
42186261Ssam#include "ffs/ufs_bswap.h"
43223169Smckusick
44185222Ssam/*
45185222Ssam * Update the frsum fields to reflect addition or deletion
46185222Ssam * of some frags.
47185222Ssam */
48185222Ssamvoid
49186261Ssamffs_fragacct_swap(struct fs *fs, int fragmap, int32_t fraglist[], int cnt, int needswap)
50185222Ssam{
51185222Ssam	int inblk;
52185222Ssam	int field, subfield;
53185222Ssam	int siz, pos;
54185222Ssam
55185222Ssam	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
56185222Ssam	fragmap <<= 1;
57185222Ssam	for (siz = 1; siz < fs->fs_frag; siz++) {
58185222Ssam		if ((inblk & (1 << (siz + (fs->fs_frag & (NBBY - 1))))) == 0)
59185222Ssam			continue;
60185222Ssam		field = around[siz];
61185222Ssam		subfield = inside[siz];
62185222Ssam		for (pos = siz; pos <= fs->fs_frag; pos++) {
63185222Ssam			if ((fragmap & field) == subfield) {
64185222Ssam				fraglist[siz] = ufs_rw32(
65185222Ssam				    ufs_rw32(fraglist[siz], needswap) + cnt,
66185222Ssam				    needswap);
67185222Ssam				pos += siz;
68185222Ssam				field <<= siz;
69185222Ssam				subfield <<= siz;
70185222Ssam			}
71185222Ssam			field <<= 1;
72185222Ssam			subfield <<= 1;
73185222Ssam		}
74185222Ssam	}
75185222Ssam}
76185222Ssam
77185222Ssam/*
78185222Ssam * block operations
79185222Ssam *
80185222Ssam * check if a block is available
81248293Sbrooks *  returns true if all the corresponding bits in the free map are 1
82248293Sbrooks *  returns false if any corresponding bit in the free map is 0
83185222Ssam */
84185222Ssamint
85185222Ssamffs_isblock(fs, cp, h)
86185222Ssam	struct fs *fs;
87185222Ssam	u_char *cp;
88185222Ssam	int32_t h;
89185222Ssam{
90185222Ssam	u_char mask;
91185222Ssam
92185222Ssam	switch ((int)fs->fs_fragshift) {
93185222Ssam	case 3:
94185222Ssam		return (cp[h] == 0xff);
95185222Ssam	case 2:
96185222Ssam		mask = 0x0f << ((h & 0x1) << 2);
97185222Ssam		return ((cp[h >> 1] & mask) == mask);
98185222Ssam	case 1:
99185222Ssam		mask = 0x03 << ((h & 0x3) << 1);
100185222Ssam		return ((cp[h >> 2] & mask) == mask);
101185222Ssam	case 0:
102185222Ssam		mask = 0x01 << (h & 0x7);
103185222Ssam		return ((cp[h >> 3] & mask) == mask);
104185222Ssam	default:
105185222Ssam		panic("ffs_isblock: unknown fs_fragshift %d",
106185222Ssam		    (int)fs->fs_fragshift);
107185222Ssam	}
108185222Ssam}
109185222Ssam
110185222Ssam/*
111185222Ssam * check if a block is completely allocated
112185222Ssam *  returns true if all the corresponding bits in the free map are 0
113185222Ssam *  returns false if any corresponding bit in the free map is 1
114185222Ssam */
115185222Ssamint
116185222Ssamffs_isfreeblock(fs, cp, h)
117185222Ssam	struct fs *fs;
118185222Ssam	u_char *cp;
119185222Ssam	int32_t h;
120185222Ssam{
121185222Ssam
122185222Ssam	switch ((int)fs->fs_fragshift) {
123185222Ssam	case 3:
124185222Ssam		return (cp[h] == 0);
125185222Ssam	case 2:
126185222Ssam		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
127185222Ssam	case 1:
128185222Ssam		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
129185222Ssam	case 0:
130185222Ssam		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
131185222Ssam	default:
132185222Ssam		panic("ffs_isfreeblock: unknown fs_fragshift %d",
133185222Ssam		    (int)fs->fs_fragshift);
134185222Ssam	}
135185222Ssam}
136185222Ssam
137185222Ssam/*
138185222Ssam * take a block out of the map
139185222Ssam */
140185222Ssamvoid
141185222Ssamffs_clrblock(fs, cp, h)
142185222Ssam	struct fs *fs;
143185222Ssam	u_char *cp;
144185222Ssam	int32_t h;
145185222Ssam{
146185222Ssam
147185222Ssam	switch ((int)fs->fs_fragshift) {
148185222Ssam	case 3:
149185222Ssam		cp[h] = 0;
150185222Ssam		return;
151185222Ssam	case 2:
152185222Ssam		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
153185222Ssam		return;
154185222Ssam	case 1:
155185222Ssam		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
156185222Ssam		return;
157185222Ssam	case 0:
158185222Ssam		cp[h >> 3] &= ~(0x01 << (h & 0x7));
159185222Ssam		return;
160185222Ssam	default:
161185222Ssam		panic("ffs_clrblock: unknown fs_fragshift %d",
162185222Ssam		    (int)fs->fs_fragshift);
163185222Ssam	}
164185222Ssam}
165185222Ssam
166185222Ssam/*
167185222Ssam * put a block into the map
168185222Ssam */
169185222Ssamvoid
170185222Ssamffs_setblock(fs, cp, h)
171185222Ssam	struct fs *fs;
172185222Ssam	u_char *cp;
173185222Ssam	int32_t h;
174185222Ssam{
175185222Ssam
176185222Ssam	switch ((int)fs->fs_fragshift) {
177185222Ssam	case 3:
178185222Ssam		cp[h] = 0xff;
179185222Ssam		return;
180185222Ssam	case 2:
181185222Ssam		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
182185222Ssam		return;
183185222Ssam	case 1:
184185222Ssam		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
185185222Ssam		return;
186185222Ssam	case 0:
187185222Ssam		cp[h >> 3] |= (0x01 << (h & 0x7));
188185222Ssam		return;
189185222Ssam	default:
190185222Ssam		panic("ffs_setblock: unknown fs_fragshift %d",
191185222Ssam		    (int)fs->fs_fragshift);
192185222Ssam	}
193185222Ssam}
194