ffs_subr.c revision 1.21 1 /* $NetBSD: ffs_subr.c,v 1.21 2002/01/31 19:19:22 tv 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.21 2002/01/31 19:19:22 tv 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 int inside[], around[];
51 extern u_char *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)))) == 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 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_frag) {
197 case 8:
198 return (cp[h] == 0xff);
199 case 4:
200 mask = 0x0f << ((h & 0x1) << 2);
201 return ((cp[h >> 1] & mask) == mask);
202 case 2:
203 mask = 0x03 << ((h & 0x3) << 1);
204 return ((cp[h >> 2] & mask) == mask);
205 case 1:
206 mask = 0x01 << (h & 0x7);
207 return ((cp[h >> 3] & mask) == mask);
208 default:
209 panic("ffs_isblock: unknown fs_frag %d", (int)fs->fs_frag);
210 }
211 }
212
213 /*
214 * check if a block is free
215 */
216 int
217 ffs_isfreeblock(fs, cp, h)
218 struct fs *fs;
219 u_char *cp;
220 ufs_daddr_t h;
221 {
222
223 switch ((int)fs->fs_frag) {
224 case 8:
225 return (cp[h] == 0);
226 case 4:
227 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
228 case 2:
229 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
230 case 1:
231 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
232 default:
233 panic("ffs_isfreeblock: unknown fs_frag %d", (int)fs->fs_frag);
234 }
235 }
236
237 /*
238 * take a block out of the map
239 */
240 void
241 ffs_clrblock(fs, cp, h)
242 struct fs *fs;
243 u_char *cp;
244 ufs_daddr_t h;
245 {
246
247 switch ((int)fs->fs_frag) {
248 case 8:
249 cp[h] = 0;
250 return;
251 case 4:
252 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
253 return;
254 case 2:
255 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
256 return;
257 case 1:
258 cp[h >> 3] &= ~(0x01 << (h & 0x7));
259 return;
260 default:
261 panic("ffs_clrblock: unknown fs_frag %d", (int)fs->fs_frag);
262 }
263 }
264
265 /*
266 * put a block into the map
267 */
268 void
269 ffs_setblock(fs, cp, h)
270 struct fs *fs;
271 u_char *cp;
272 ufs_daddr_t h;
273 {
274
275 switch ((int)fs->fs_frag) {
276
277 case 8:
278 cp[h] = 0xff;
279 return;
280 case 4:
281 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
282 return;
283 case 2:
284 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
285 return;
286 case 1:
287 cp[h >> 3] |= (0x01 << (h & 0x7));
288 return;
289 default:
290 panic("ffs_setblock: unknown fs_frag %d", (int)fs->fs_frag);
291 }
292 }
293