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