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