Home | History | Annotate | Line # | Download | only in ffs
ffs_subr.c revision 1.13.20.2
      1 /*	$NetBSD: ffs_subr.c,v 1.13.20.2 1999/12/27 18:36:37 wrstuden 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/param.h>
     39 #include <sys/systm.h>
     40 #ifndef _KERNEL
     41 #include <ufs/ufs/dinode.h>
     42 #include <ufs/ffs/fs.h>
     43 #include <ufs/ffs/ffs_extern.h>
     44 #include <ufs/ufs/ufs_bswap.h>
     45 #endif
     46 
     47 /* in ffs_tables.c */
     48 extern int inside[], around[];
     49 extern u_char *fragtbl[];
     50 
     51 #ifdef _KERNEL
     52 #include <sys/vnode.h>
     53 #include <sys/mount.h>
     54 #include <sys/buf.h>
     55 #include <ufs/ufs/quota.h>
     56 #include <ufs/ufs/ufsmount.h>
     57 #include <ufs/ufs/inode.h>
     58 #include <ufs/ufs/ufs_extern.h>
     59 #include <ufs/ffs/fs.h>
     60 #include <ufs/ffs/ffs_extern.h>
     61 #include <ufs/ufs/ufs_bswap.h>
     62 
     63 /*
     64  * Return buffer with the contents of block "offset" from the beginning of
     65  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
     66  * remaining space in the directory.
     67  */
     68 int
     69 ffs_blkatoff(v)
     70 	void *v;
     71 {
     72 	struct vop_blkatoff_args /* {
     73 		struct vnode *a_vp;
     74 		off_t a_offset;
     75 		char **a_res;
     76 		struct buf **a_bpp;
     77 	} */ *ap = v;
     78 	struct inode *ip;
     79 	register struct fs *fs;
     80 	struct buf *bp;
     81 	ufs_daddr_t lbn;
     82 	int bsize, error;
     83 
     84 	ip = VTOI(ap->a_vp);
     85 	fs = ip->i_fs;
     86 	lbn = lblkno(fs, ap->a_offset);
     87 	bsize = blksize(fs, ip, lbn);
     88 
     89 	*ap->a_bpp = NULL;
     90 	if ((error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) != 0) {
     91 		brelse(bp);
     92 		return (error);
     93 	}
     94 	if (ap->a_res)
     95 		*ap->a_res = (char *)bp->b_data + blkoff(fs, ap->a_offset);
     96 	*ap->a_bpp = bp;
     97 	return (0);
     98 }
     99 #endif
    100 
    101 /*
    102  * Update the frsum fields to reflect addition or deletion
    103  * of some frags.
    104  */
    105 void
    106 ffs_fragacct(fs, fragmap, fraglist, cnt, needswap)
    107 	struct fs *fs;
    108 	int fragmap;
    109 	int32_t fraglist[];
    110 	int cnt;
    111 	int needswap;
    112 {
    113 	int inblk;
    114 	register int field, subfield;
    115 	register int siz, pos;
    116 
    117 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
    118 	fragmap <<= 1;
    119 	for (siz = 1; siz < fs->fs_frag; siz++) {
    120 		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
    121 			continue;
    122 		field = around[siz];
    123 		subfield = inside[siz];
    124 		for (pos = siz; pos <= fs->fs_frag; pos++) {
    125 			if ((fragmap & field) == subfield) {
    126 				fraglist[siz] = ufs_rw32(
    127 				    ufs_rw32(fraglist[siz], needswap) + cnt,
    128 				    needswap);
    129 				pos += siz;
    130 				field <<= siz;
    131 				subfield <<= siz;
    132 			}
    133 			field <<= 1;
    134 			subfield <<= 1;
    135 		}
    136 	}
    137 }
    138 
    139 #if defined(_KERNEL) && defined(DIAGNOSTIC)
    140 void
    141 ffs_checkoverlap(bp, ip)
    142 	struct buf *bp;
    143 	struct inode *ip;
    144 {
    145 	register struct buf *ebp, *ep;
    146 	register ufs_daddr_t start, last;
    147 	struct vnode *vp;
    148 
    149 	ebp = &buf[nbuf];
    150 	start = bp->b_blkno;
    151 	last = start + btodb(bp->b_bcount, bp->b_bshift) - 1;
    152 	for (ep = buf; ep < ebp; ep++) {
    153 		if (ep == bp || (ep->b_flags & B_INVAL) ||
    154 		    ep->b_vp == NULLVP)
    155 			continue;
    156 		if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0, NULL))
    157 			continue;
    158 		if (vp != ip->i_devvp)
    159 			continue;
    160 		/* look for overlap */
    161 		if (ep->b_bcount == 0 || ep->b_blkno > last ||
    162 		    start >= ep->b_blkno +
    163 				btodb(ep->b_bcount, bp->b_bshift))
    164 			continue;
    165 		vprint("Disk overlap", vp);
    166 		printf("\tstart %d, end %d overlap start %d, end %ld\n",
    167 		    start, last, ep->b_blkno,
    168 		    ep->b_blkno +
    169 			btodb(ep->b_bcount, bp->b_bshift) - 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 	unsigned char *cp;
    184 	ufs_daddr_t h;
    185 {
    186 	unsigned 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");
    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 	unsigned 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");
    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");
    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 	unsigned 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");
    283 	}
    284 }
    285