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