ffs_subr.c revision 1.27 1 /* $NetBSD: ffs_subr.c,v 1.27 2003/01/25 18:12:32 tron 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.27 2003/01/25 18:12:32 tron 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 <sys/inttypes.h>
67 #include <ufs/ufs/inode.h>
68 #include <ufs/ufs/ufsmount.h>
69 #include <ufs/ufs/ufs_extern.h>
70 #include <ufs/ffs/fs.h>
71 #include <ufs/ffs/ffs_extern.h>
72 #include <ufs/ufs/ufs_bswap.h>
73
74 /*
75 * Return buffer with the contents of block "offset" from the beginning of
76 * directory "ip". If "res" is non-zero, fill it in with a pointer to the
77 * remaining space in the directory.
78 */
79 int
80 ffs_blkatoff(v)
81 void *v;
82 {
83 struct vop_blkatoff_args /* {
84 struct vnode *a_vp;
85 off_t a_offset;
86 char **a_res;
87 struct buf **a_bpp;
88 } */ *ap = v;
89 struct inode *ip;
90 struct fs *fs;
91 struct buf *bp;
92 daddr_t lbn;
93 int bsize, error;
94
95 ip = VTOI(ap->a_vp);
96 fs = ip->i_fs;
97 lbn = lblkno(fs, ap->a_offset);
98 bsize = blksize(fs, ip, lbn);
99
100 *ap->a_bpp = NULL;
101 if ((error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) != 0) {
102 brelse(bp);
103 return (error);
104 }
105 if (ap->a_res)
106 *ap->a_res = (char *)bp->b_data + blkoff(fs, ap->a_offset);
107 *ap->a_bpp = bp;
108 return (0);
109 }
110 #endif /* _KERNEL */
111
112 /*
113 * Update the frsum fields to reflect addition or deletion
114 * of some frags.
115 */
116 void
117 ffs_fragacct(fs, fragmap, fraglist, cnt, needswap)
118 struct fs *fs;
119 int fragmap;
120 int32_t fraglist[];
121 int cnt;
122 int needswap;
123 {
124 int inblk;
125 int field, subfield;
126 int siz, pos;
127
128 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
129 fragmap <<= 1;
130 for (siz = 1; siz < fs->fs_frag; siz++) {
131 if ((inblk & (1 << (siz + (fs->fs_frag & (NBBY - 1))))) == 0)
132 continue;
133 field = around[siz];
134 subfield = inside[siz];
135 for (pos = siz; pos <= fs->fs_frag; pos++) {
136 if ((fragmap & field) == subfield) {
137 fraglist[siz] = ufs_rw32(
138 ufs_rw32(fraglist[siz], needswap) + cnt,
139 needswap);
140 pos += siz;
141 field <<= siz;
142 subfield <<= siz;
143 }
144 field <<= 1;
145 subfield <<= 1;
146 }
147 }
148 }
149
150 #if defined(_KERNEL) && defined(DIAGNOSTIC)
151 void
152 ffs_checkoverlap(bp, ip)
153 struct buf *bp;
154 struct inode *ip;
155 {
156 struct buf *ebp, *ep;
157 daddr_t start, last;
158 struct vnode *vp;
159
160 ebp = &buf[nbuf];
161 start = bp->b_blkno;
162 last = start + btodb(bp->b_bcount) - 1;
163 for (ep = buf; ep < ebp; ep++) {
164 if (ep == bp || (ep->b_flags & B_INVAL) ||
165 ep->b_vp == NULLVP)
166 continue;
167 if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0, NULL))
168 continue;
169 if (vp != ip->i_devvp)
170 continue;
171 /* look for overlap */
172 if (ep->b_bcount == 0 || ep->b_blkno > last ||
173 ep->b_blkno + btodb(ep->b_bcount) <= start)
174 continue;
175 vprint("Disk overlap", vp);
176 printf("\tstart %" PRId64 ", end %" PRId64 " overlap start "
177 "%" PRId64 ", end %" PRId64 "\n",
178 start, last, ep->b_blkno,
179 ep->b_blkno + btodb(ep->b_bcount) - 1);
180 panic("Disk buffer overlap");
181 }
182 }
183 #endif /* _KERNEL && DIAGNOSTIC */
184
185 /*
186 * block operations
187 *
188 * check if a block is available
189 */
190 int
191 ffs_isblock(fs, cp, h)
192 struct fs *fs;
193 u_char *cp;
194 daddr_t h;
195 {
196 u_char mask;
197
198 switch ((int)fs->fs_fragshift) {
199 case 3:
200 return (cp[h] == 0xff);
201 case 2:
202 mask = 0x0f << ((h & 0x1) << 2);
203 return ((cp[h >> 1] & mask) == mask);
204 case 1:
205 mask = 0x03 << ((h & 0x3) << 1);
206 return ((cp[h >> 2] & mask) == mask);
207 case 0:
208 mask = 0x01 << (h & 0x7);
209 return ((cp[h >> 3] & mask) == mask);
210 default:
211 panic("ffs_isblock: unknown fs_fragshift %d",
212 (int)fs->fs_fragshift);
213 }
214 }
215
216 /*
217 * check if a block is free
218 */
219 int
220 ffs_isfreeblock(fs, cp, h)
221 struct fs *fs;
222 u_char *cp;
223 daddr_t h;
224 {
225
226 switch ((int)fs->fs_fragshift) {
227 case 3:
228 return (cp[h] == 0);
229 case 2:
230 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
231 case 1:
232 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
233 case 0:
234 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
235 default:
236 panic("ffs_isfreeblock: unknown fs_fragshift %d",
237 (int)fs->fs_fragshift);
238 }
239 }
240
241 /*
242 * take a block out of the map
243 */
244 void
245 ffs_clrblock(fs, cp, h)
246 struct fs *fs;
247 u_char *cp;
248 daddr_t h;
249 {
250
251 switch ((int)fs->fs_fragshift) {
252 case 3:
253 cp[h] = 0;
254 return;
255 case 2:
256 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
257 return;
258 case 1:
259 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
260 return;
261 case 0:
262 cp[h >> 3] &= ~(0x01 << (h & 0x7));
263 return;
264 default:
265 panic("ffs_clrblock: unknown fs_fragshift %d",
266 (int)fs->fs_fragshift);
267 }
268 }
269
270 /*
271 * put a block into the map
272 */
273 void
274 ffs_setblock(fs, cp, h)
275 struct fs *fs;
276 u_char *cp;
277 daddr_t h;
278 {
279
280 switch ((int)fs->fs_fragshift) {
281 case 3:
282 cp[h] = 0xff;
283 return;
284 case 2:
285 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
286 return;
287 case 1:
288 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
289 return;
290 case 0:
291 cp[h >> 3] |= (0x01 << (h & 0x7));
292 return;
293 default:
294 panic("ffs_setblock: unknown fs_fragshift %d",
295 (int)fs->fs_fragshift);
296 }
297 }
298