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