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