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