xref: /netbsd/sys/ufs/ffs/ffs_subr.c (revision c4a72b64)
1 /*	$NetBSD: ffs_subr.c,v 1.24 2002/12/01 00:12:09 matt 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)ffs_subr.c	8.5 (Berkeley) 3/21/95
36  */
37 
38 #include <sys/cdefs.h>
39 #if defined(__KERNEL_RCSID)
40 __KERNEL_RCSID(0, "$NetBSD: ffs_subr.c,v 1.24 2002/12/01 00:12:09 matt Exp $");
41 #endif
42 
43 #if HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46 
47 #include <sys/param.h>
48 
49 /* in ffs_tables.c */
50 extern const int inside[], around[];
51 extern const u_char * const fragtbl[];
52 
53 #ifndef _KERNEL
54 #include <ufs/ufs/dinode.h>
55 #include <ufs/ffs/fs.h>
56 #include <ufs/ffs/ffs_extern.h>
57 #include <ufs/ufs/ufs_bswap.h>
58 void    panic __P((const char *, ...))
59     __attribute__((__noreturn__,__format__(__printf__,1,2)));
60 
61 #else	/* _KERNEL */
62 #include <sys/systm.h>
63 #include <sys/vnode.h>
64 #include <sys/mount.h>
65 #include <sys/buf.h>
66 #include <ufs/ufs/inode.h>
67 #include <ufs/ufs/ufsmount.h>
68 #include <ufs/ufs/ufs_extern.h>
69 #include <ufs/ffs/fs.h>
70 #include <ufs/ffs/ffs_extern.h>
71 #include <ufs/ufs/ufs_bswap.h>
72 
73 /*
74  * Return buffer with the contents of block "offset" from the beginning of
75  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
76  * remaining space in the directory.
77  */
78 int
79 ffs_blkatoff(v)
80 	void *v;
81 {
82 	struct vop_blkatoff_args /* {
83 		struct vnode *a_vp;
84 		off_t a_offset;
85 		char **a_res;
86 		struct buf **a_bpp;
87 	} */ *ap = v;
88 	struct inode *ip;
89 	struct fs *fs;
90 	struct buf *bp;
91 	ufs_daddr_t lbn;
92 	int bsize, error;
93 
94 	ip = VTOI(ap->a_vp);
95 	fs = ip->i_fs;
96 	lbn = lblkno(fs, ap->a_offset);
97 	bsize = blksize(fs, ip, lbn);
98 
99 	*ap->a_bpp = NULL;
100 	if ((error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) != 0) {
101 		brelse(bp);
102 		return (error);
103 	}
104 	if (ap->a_res)
105 		*ap->a_res = (char *)bp->b_data + blkoff(fs, ap->a_offset);
106 	*ap->a_bpp = bp;
107 	return (0);
108 }
109 #endif	/* _KERNEL */
110 
111 /*
112  * Update the frsum fields to reflect addition or deletion
113  * of some frags.
114  */
115 void
116 ffs_fragacct(fs, fragmap, fraglist, cnt, needswap)
117 	struct fs *fs;
118 	int fragmap;
119 	int32_t fraglist[];
120 	int cnt;
121 	int needswap;
122 {
123 	int inblk;
124 	int field, subfield;
125 	int siz, pos;
126 
127 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
128 	fragmap <<= 1;
129 	for (siz = 1; siz < fs->fs_frag; siz++) {
130 		if ((inblk & (1 << (siz + (fs->fs_frag & (NBBY - 1))))) == 0)
131 			continue;
132 		field = around[siz];
133 		subfield = inside[siz];
134 		for (pos = siz; pos <= fs->fs_frag; pos++) {
135 			if ((fragmap & field) == subfield) {
136 				fraglist[siz] = ufs_rw32(
137 				    ufs_rw32(fraglist[siz], needswap) + cnt,
138 				    needswap);
139 				pos += siz;
140 				field <<= siz;
141 				subfield <<= siz;
142 			}
143 			field <<= 1;
144 			subfield <<= 1;
145 		}
146 	}
147 }
148 
149 #if defined(_KERNEL) && defined(DIAGNOSTIC)
150 void
151 ffs_checkoverlap(bp, ip)
152 	struct buf *bp;
153 	struct inode *ip;
154 {
155 	struct buf *ebp, *ep;
156 	ufs_daddr_t start, last;
157 	struct vnode *vp;
158 
159 	ebp = &buf[nbuf];
160 	start = bp->b_blkno;
161 	last = start + btodb(bp->b_bcount) - 1;
162 	for (ep = buf; ep < ebp; ep++) {
163 		if (ep == bp || (ep->b_flags & B_INVAL) ||
164 		    ep->b_vp == NULLVP)
165 			continue;
166 		if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0, NULL))
167 			continue;
168 		if (vp != ip->i_devvp)
169 			continue;
170 		/* look for overlap */
171 		if (ep->b_bcount == 0 || ep->b_blkno > last ||
172 		    ep->b_blkno + btodb(ep->b_bcount) <= start)
173 			continue;
174 		vprint("Disk overlap", vp);
175 		printf("\tstart %d, end %d overlap start %d, end %ld\n",
176 		    start, last, ep->b_blkno,
177 		    (long) ep->b_blkno + btodb(ep->b_bcount) - 1);
178 		panic("Disk buffer overlap");
179 	}
180 }
181 #endif /* _KERNEL && DIAGNOSTIC */
182 
183 /*
184  * block operations
185  *
186  * check if a block is available
187  */
188 int
189 ffs_isblock(fs, cp, h)
190 	struct fs *fs;
191 	u_char *cp;
192 	ufs_daddr_t h;
193 {
194 	u_char mask;
195 
196 	switch ((int)fs->fs_fragshift) {
197 	case 3:
198 		return (cp[h] == 0xff);
199 	case 2:
200 		mask = 0x0f << ((h & 0x1) << 2);
201 		return ((cp[h >> 1] & mask) == mask);
202 	case 1:
203 		mask = 0x03 << ((h & 0x3) << 1);
204 		return ((cp[h >> 2] & mask) == mask);
205 	case 0:
206 		mask = 0x01 << (h & 0x7);
207 		return ((cp[h >> 3] & mask) == mask);
208 	default:
209 		panic("ffs_isblock: unknown fs_fragshift %d",
210 		    (int)fs->fs_fragshift);
211 	}
212 }
213 
214 /*
215  * check if a block is free
216  */
217 int
218 ffs_isfreeblock(fs, cp, h)
219 	struct fs *fs;
220 	u_char *cp;
221 	ufs_daddr_t h;
222 {
223 
224 	switch ((int)fs->fs_fragshift) {
225 	case 3:
226 		return (cp[h] == 0);
227 	case 2:
228 		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
229 	case 1:
230 		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
231 	case 0:
232 		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
233 	default:
234 		panic("ffs_isfreeblock: unknown fs_fragshift %d",
235 		    (int)fs->fs_fragshift);
236 	}
237 }
238 
239 /*
240  * take a block out of the map
241  */
242 void
243 ffs_clrblock(fs, cp, h)
244 	struct fs *fs;
245 	u_char *cp;
246 	ufs_daddr_t h;
247 {
248 
249 	switch ((int)fs->fs_fragshift) {
250 	case 3:
251 		cp[h] = 0;
252 		return;
253 	case 2:
254 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
255 		return;
256 	case 1:
257 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
258 		return;
259 	case 0:
260 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
261 		return;
262 	default:
263 		panic("ffs_clrblock: unknown fs_fragshift %d",
264 		    (int)fs->fs_fragshift);
265 	}
266 }
267 
268 /*
269  * put a block into the map
270  */
271 void
272 ffs_setblock(fs, cp, h)
273 	struct fs *fs;
274 	u_char *cp;
275 	ufs_daddr_t h;
276 {
277 
278 	switch ((int)fs->fs_fragshift) {
279 	case 3:
280 		cp[h] = 0xff;
281 		return;
282 	case 2:
283 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
284 		return;
285 	case 1:
286 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
287 		return;
288 	case 0:
289 		cp[h >> 3] |= (0x01 << (h & 0x7));
290 		return;
291 	default:
292 		panic("ffs_setblock: unknown fs_fragshift %d",
293 		    (int)fs->fs_fragshift);
294 	}
295 }
296