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