ffs_subr.c revision 1.28 1 /* $NetBSD: ffs_subr.c,v 1.28 2003/04/02 10:39:38 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/cdefs.h>
39 #if defined(__KERNEL_RCSID)
40 __KERNEL_RCSID(0, "$NetBSD: ffs_subr.c,v 1.28 2003/04/02 10:39:38 fvdl Exp $");
41 #endif
42
43 #if HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46
47 #include <sys/param.h>
48
49 /* in ffs_tables.c */
50 extern const int inside[], around[];
51 extern const u_char * const fragtbl[];
52
53 #ifndef _KERNEL
54 #include <ufs/ufs/dinode.h>
55 #include <ufs/ffs/fs.h>
56 #include <ufs/ffs/ffs_extern.h>
57 #include <ufs/ufs/ufs_bswap.h>
58 void panic __P((const char *, ...))
59 __attribute__((__noreturn__,__format__(__printf__,1,2)));
60
61 #else /* _KERNEL */
62 #include <sys/systm.h>
63 #include <sys/vnode.h>
64 #include <sys/mount.h>
65 #include <sys/buf.h>
66 #include <sys/inttypes.h>
67 #include <sys/pool.h>
68 #include <ufs/ufs/inode.h>
69 #include <ufs/ufs/ufsmount.h>
70 #include <ufs/ufs/ufs_extern.h>
71 #include <ufs/ffs/fs.h>
72 #include <ufs/ffs/ffs_extern.h>
73 #include <ufs/ufs/ufs_bswap.h>
74
75 /*
76 * Return buffer with the contents of block "offset" from the beginning of
77 * directory "ip". If "res" is non-zero, fill it in with a pointer to the
78 * remaining space in the directory.
79 */
80 int
81 ffs_blkatoff(v)
82 void *v;
83 {
84 struct vop_blkatoff_args /* {
85 struct vnode *a_vp;
86 off_t a_offset;
87 char **a_res;
88 struct buf **a_bpp;
89 } */ *ap = v;
90 struct inode *ip;
91 struct fs *fs;
92 struct buf *bp;
93 daddr_t lbn;
94 int bsize, error;
95
96 ip = VTOI(ap->a_vp);
97 fs = ip->i_fs;
98 lbn = lblkno(fs, ap->a_offset);
99 bsize = blksize(fs, ip, lbn);
100
101 *ap->a_bpp = NULL;
102 if ((error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) != 0) {
103 brelse(bp);
104 return (error);
105 }
106 if (ap->a_res)
107 *ap->a_res = (char *)bp->b_data + blkoff(fs, ap->a_offset);
108 *ap->a_bpp = bp;
109 return (0);
110 }
111
112
113 /*
114 * Load up the contents of an inode and copy the appropriate pieces
115 * to the incore copy.
116 */
117 void
118 ffs_load_inode(bp, ip, fs, ino)
119 struct buf *bp;
120 struct inode *ip;
121 struct fs *fs;
122 ino_t ino;
123 {
124 struct ufs1_dinode *dp1;
125 struct ufs2_dinode *dp2;
126
127 if (ip->i_ump->um_fstype == UFS1) {
128 dp1 = (struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino);
129 #ifdef FFS_EI
130 if (UFS_FSNEEDSWAP(fs))
131 ffs_dinode1_swap(dp1, ip->i_din.ffs1_din);
132 else
133 #endif
134 *ip->i_din.ffs1_din = *dp1;
135
136 ip->i_mode = ip->i_ffs1_mode;
137 ip->i_nlink = ip->i_ffs1_nlink;
138 ip->i_size = ip->i_ffs1_size;
139 ip->i_flags = ip->i_ffs1_flags;
140 ip->i_gen = ip->i_ffs1_gen;
141 ip->i_uid = ip->i_ffs1_uid;
142 ip->i_gid = ip->i_ffs1_gid;
143 } else {
144 dp2 = (struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino);
145 #ifdef FFS_EI
146 if (UFS_FSNEEDSWAP(fs))
147 ffs_dinode2_swap(dp2, ip->i_din.ffs2_din);
148 else
149 #endif
150 *ip->i_din.ffs2_din = *dp2;
151
152 ip->i_mode = ip->i_ffs2_mode;
153 ip->i_nlink = ip->i_ffs2_nlink;
154 ip->i_size = ip->i_ffs2_size;
155 ip->i_flags = ip->i_ffs2_flags;
156 ip->i_gen = ip->i_ffs2_gen;
157 ip->i_uid = ip->i_ffs2_uid;
158 ip->i_gid = ip->i_ffs2_gid;
159 }
160 }
161
162 #endif /* _KERNEL */
163
164 /*
165 * Update the frsum fields to reflect addition or deletion
166 * of some frags.
167 */
168 void
169 ffs_fragacct(fs, fragmap, fraglist, cnt, needswap)
170 struct fs *fs;
171 int fragmap;
172 int32_t fraglist[];
173 int cnt;
174 int needswap;
175 {
176 int inblk;
177 int field, subfield;
178 int siz, pos;
179
180 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
181 fragmap <<= 1;
182 for (siz = 1; siz < fs->fs_frag; siz++) {
183 if ((inblk & (1 << (siz + (fs->fs_frag & (NBBY - 1))))) == 0)
184 continue;
185 field = around[siz];
186 subfield = inside[siz];
187 for (pos = siz; pos <= fs->fs_frag; pos++) {
188 if ((fragmap & field) == subfield) {
189 fraglist[siz] = ufs_rw32(
190 ufs_rw32(fraglist[siz], needswap) + cnt,
191 needswap);
192 pos += siz;
193 field <<= siz;
194 subfield <<= siz;
195 }
196 field <<= 1;
197 subfield <<= 1;
198 }
199 }
200 }
201
202 #if defined(_KERNEL) && defined(DIAGNOSTIC)
203 void
204 ffs_checkoverlap(bp, ip)
205 struct buf *bp;
206 struct inode *ip;
207 {
208 struct buf *ebp, *ep;
209 daddr_t start, last;
210 struct vnode *vp;
211
212 ebp = &buf[nbuf];
213 start = bp->b_blkno;
214 last = start + btodb(bp->b_bcount) - 1;
215 for (ep = buf; ep < ebp; ep++) {
216 if (ep == bp || (ep->b_flags & B_INVAL) ||
217 ep->b_vp == NULLVP)
218 continue;
219 if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0, NULL))
220 continue;
221 if (vp != ip->i_devvp)
222 continue;
223 /* look for overlap */
224 if (ep->b_bcount == 0 || ep->b_blkno > last ||
225 ep->b_blkno + btodb(ep->b_bcount) <= start)
226 continue;
227 vprint("Disk overlap", vp);
228 printf("\tstart %" PRId64 ", end %" PRId64 " overlap start "
229 "%" PRId64 ", end %" PRId64 "\n",
230 start, last, ep->b_blkno,
231 ep->b_blkno + btodb(ep->b_bcount) - 1);
232 panic("Disk buffer overlap");
233 }
234 }
235 #endif /* _KERNEL && DIAGNOSTIC */
236
237 /*
238 * block operations
239 *
240 * check if a block is available
241 */
242 int
243 ffs_isblock(fs, cp, h)
244 struct fs *fs;
245 u_char *cp;
246 int32_t h;
247 {
248 u_char mask;
249
250 switch ((int)fs->fs_fragshift) {
251 case 3:
252 return (cp[h] == 0xff);
253 case 2:
254 mask = 0x0f << ((h & 0x1) << 2);
255 return ((cp[h >> 1] & mask) == mask);
256 case 1:
257 mask = 0x03 << ((h & 0x3) << 1);
258 return ((cp[h >> 2] & mask) == mask);
259 case 0:
260 mask = 0x01 << (h & 0x7);
261 return ((cp[h >> 3] & mask) == mask);
262 default:
263 panic("ffs_isblock: unknown fs_fragshift %d",
264 (int)fs->fs_fragshift);
265 }
266 }
267
268 /*
269 * check if a block is free
270 */
271 int
272 ffs_isfreeblock(fs, cp, h)
273 struct fs *fs;
274 u_char *cp;
275 int32_t h;
276 {
277
278 switch ((int)fs->fs_fragshift) {
279 case 3:
280 return (cp[h] == 0);
281 case 2:
282 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
283 case 1:
284 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
285 case 0:
286 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
287 default:
288 panic("ffs_isfreeblock: unknown fs_fragshift %d",
289 (int)fs->fs_fragshift);
290 }
291 }
292
293 /*
294 * take a block out of the map
295 */
296 void
297 ffs_clrblock(fs, cp, h)
298 struct fs *fs;
299 u_char *cp;
300 int32_t h;
301 {
302
303 switch ((int)fs->fs_fragshift) {
304 case 3:
305 cp[h] = 0;
306 return;
307 case 2:
308 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
309 return;
310 case 1:
311 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
312 return;
313 case 0:
314 cp[h >> 3] &= ~(0x01 << (h & 0x7));
315 return;
316 default:
317 panic("ffs_clrblock: unknown fs_fragshift %d",
318 (int)fs->fs_fragshift);
319 }
320 }
321
322 /*
323 * put a block into the map
324 */
325 void
326 ffs_setblock(fs, cp, h)
327 struct fs *fs;
328 u_char *cp;
329 int32_t h;
330 {
331
332 switch ((int)fs->fs_fragshift) {
333 case 3:
334 cp[h] = 0xff;
335 return;
336 case 2:
337 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
338 return;
339 case 1:
340 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
341 return;
342 case 0:
343 cp[h >> 3] |= (0x01 << (h & 0x7));
344 return;
345 default:
346 panic("ffs_setblock: unknown fs_fragshift %d",
347 (int)fs->fs_fragshift);
348 }
349 }
350