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