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