Home | History | Annotate | Line # | Download | only in ffs
ffs_subr.c revision 1.1.1.2
      1 /*
      2  * Copyright (c) 1982, 1986, 1989, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  *	@(#)ffs_subr.c	8.5 (Berkeley) 3/21/95
     34  */
     35 
     36 #include <sys/param.h>
     37 #ifndef KERNEL
     38 #include <ufs/ufs/dinode.h>
     39 #include <ufs/ffs/fs.h>
     40 #else
     41 
     42 #include <sys/systm.h>
     43 #include <sys/vnode.h>
     44 #include <sys/buf.h>
     45 #include <ufs/ufs/quota.h>
     46 #include <ufs/ufs/inode.h>
     47 #include <ufs/ffs/fs.h>
     48 #include <ufs/ffs/ffs_extern.h>
     49 
     50 /*
     51  * Return buffer with the contents of block "offset" from the beginning of
     52  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
     53  * remaining space in the directory.
     54  */
     55 int
     56 ffs_blkatoff(ap)
     57 	struct vop_blkatoff_args /* {
     58 		struct vnode *a_vp;
     59 		off_t a_offset;
     60 		char **a_res;
     61 		struct buf **a_bpp;
     62 	} */ *ap;
     63 {
     64 	struct inode *ip;
     65 	register struct fs *fs;
     66 	struct buf *bp;
     67 	ufs_daddr_t lbn;
     68 	int bsize, error;
     69 
     70 	ip = VTOI(ap->a_vp);
     71 	fs = ip->i_fs;
     72 	lbn = lblkno(fs, ap->a_offset);
     73 	bsize = blksize(fs, ip, lbn);
     74 
     75 	*ap->a_bpp = NULL;
     76 	if (error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) {
     77 		brelse(bp);
     78 		return (error);
     79 	}
     80 	if (ap->a_res)
     81 		*ap->a_res = (char *)bp->b_data + blkoff(fs, ap->a_offset);
     82 	*ap->a_bpp = bp;
     83 	return (0);
     84 }
     85 #endif
     86 
     87 /*
     88  * Update the frsum fields to reflect addition or deletion
     89  * of some frags.
     90  */
     91 void
     92 ffs_fragacct(fs, fragmap, fraglist, cnt)
     93 	struct fs *fs;
     94 	int fragmap;
     95 	int32_t fraglist[];
     96 	int cnt;
     97 {
     98 	int inblk;
     99 	register int field, subfield;
    100 	register int siz, pos;
    101 
    102 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
    103 	fragmap <<= 1;
    104 	for (siz = 1; siz < fs->fs_frag; siz++) {
    105 		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
    106 			continue;
    107 		field = around[siz];
    108 		subfield = inside[siz];
    109 		for (pos = siz; pos <= fs->fs_frag; pos++) {
    110 			if ((fragmap & field) == subfield) {
    111 				fraglist[siz] += cnt;
    112 				pos += siz;
    113 				field <<= siz;
    114 				subfield <<= siz;
    115 			}
    116 			field <<= 1;
    117 			subfield <<= 1;
    118 		}
    119 	}
    120 }
    121 
    122 #if defined(KERNEL) && defined(DIAGNOSTIC)
    123 void
    124 ffs_checkoverlap(bp, ip)
    125 	struct buf *bp;
    126 	struct inode *ip;
    127 {
    128 	register struct buf *ebp, *ep;
    129 	register ufs_daddr_t start, last;
    130 	struct vnode *vp;
    131 
    132 	ebp = &buf[nbuf];
    133 	start = bp->b_blkno;
    134 	last = start + btodb(bp->b_bcount) - 1;
    135 	for (ep = buf; ep < ebp; ep++) {
    136 		if (ep == bp || (ep->b_flags & B_INVAL) ||
    137 		    ep->b_vp == NULLVP)
    138 			continue;
    139 		if (VOP_BMAP(ep->b_vp, (ufs_daddr_t)0, &vp, (ufs_daddr_t)0,
    140 		    NULL))
    141 			continue;
    142 		if (vp != ip->i_devvp)
    143 			continue;
    144 		/* look for overlap */
    145 		if (ep->b_bcount == 0 || ep->b_blkno > last ||
    146 		    ep->b_blkno + btodb(ep->b_bcount) <= start)
    147 			continue;
    148 		vprint("Disk overlap", vp);
    149 		(void)printf("\tstart %d, end %d overlap start %d, end %d\n",
    150 			start, last, ep->b_blkno,
    151 			ep->b_blkno + btodb(ep->b_bcount) - 1);
    152 		panic("Disk buffer overlap");
    153 	}
    154 }
    155 #endif /* DIAGNOSTIC */
    156 
    157 /*
    158  * block operations
    159  *
    160  * check if a block is available
    161  */
    162 int
    163 ffs_isblock(fs, cp, h)
    164 	struct fs *fs;
    165 	unsigned char *cp;
    166 	ufs_daddr_t h;
    167 {
    168 	unsigned char mask;
    169 
    170 	switch ((int)fs->fs_frag) {
    171 	case 8:
    172 		return (cp[h] == 0xff);
    173 	case 4:
    174 		mask = 0x0f << ((h & 0x1) << 2);
    175 		return ((cp[h >> 1] & mask) == mask);
    176 	case 2:
    177 		mask = 0x03 << ((h & 0x3) << 1);
    178 		return ((cp[h >> 2] & mask) == mask);
    179 	case 1:
    180 		mask = 0x01 << (h & 0x7);
    181 		return ((cp[h >> 3] & mask) == mask);
    182 	default:
    183 		panic("ffs_isblock");
    184 	}
    185 }
    186 
    187 /*
    188  * take a block out of the map
    189  */
    190 void
    191 ffs_clrblock(fs, cp, h)
    192 	struct fs *fs;
    193 	u_char *cp;
    194 	ufs_daddr_t h;
    195 {
    196 
    197 	switch ((int)fs->fs_frag) {
    198 	case 8:
    199 		cp[h] = 0;
    200 		return;
    201 	case 4:
    202 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
    203 		return;
    204 	case 2:
    205 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
    206 		return;
    207 	case 1:
    208 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
    209 		return;
    210 	default:
    211 		panic("ffs_clrblock");
    212 	}
    213 }
    214 
    215 /*
    216  * put a block into the map
    217  */
    218 void
    219 ffs_setblock(fs, cp, h)
    220 	struct fs *fs;
    221 	unsigned char *cp;
    222 	ufs_daddr_t h;
    223 {
    224 
    225 	switch ((int)fs->fs_frag) {
    226 
    227 	case 8:
    228 		cp[h] = 0xff;
    229 		return;
    230 	case 4:
    231 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
    232 		return;
    233 	case 2:
    234 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
    235 		return;
    236 	case 1:
    237 		cp[h >> 3] |= (0x01 << (h & 0x7));
    238 		return;
    239 	default:
    240 		panic("ffs_setblock");
    241 	}
    242 }
    243