Home | History | Annotate | Line # | Download | only in ffs
ffs_subr.c revision 1.29
      1 /*	$NetBSD: ffs_subr.c,v 1.29 2003/08/07 16:34:30 agc 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)ffs_subr.c	8.5 (Berkeley) 3/21/95
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #if defined(__KERNEL_RCSID)
     36 __KERNEL_RCSID(0, "$NetBSD: ffs_subr.c,v 1.29 2003/08/07 16:34:30 agc Exp $");
     37 #endif
     38 
     39 #if HAVE_CONFIG_H
     40 #include "config.h"
     41 #endif
     42 
     43 #include <sys/param.h>
     44 
     45 /* in ffs_tables.c */
     46 extern const int inside[], around[];
     47 extern const u_char * const fragtbl[];
     48 
     49 #ifndef _KERNEL
     50 #include <ufs/ufs/dinode.h>
     51 #include <ufs/ffs/fs.h>
     52 #include <ufs/ffs/ffs_extern.h>
     53 #include <ufs/ufs/ufs_bswap.h>
     54 void    panic __P((const char *, ...))
     55     __attribute__((__noreturn__,__format__(__printf__,1,2)));
     56 
     57 #else	/* _KERNEL */
     58 #include <sys/systm.h>
     59 #include <sys/vnode.h>
     60 #include <sys/mount.h>
     61 #include <sys/buf.h>
     62 #include <sys/inttypes.h>
     63 #include <sys/pool.h>
     64 #include <ufs/ufs/inode.h>
     65 #include <ufs/ufs/ufsmount.h>
     66 #include <ufs/ufs/ufs_extern.h>
     67 #include <ufs/ffs/fs.h>
     68 #include <ufs/ffs/ffs_extern.h>
     69 #include <ufs/ufs/ufs_bswap.h>
     70 
     71 /*
     72  * Return buffer with the contents of block "offset" from the beginning of
     73  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
     74  * remaining space in the directory.
     75  */
     76 int
     77 ffs_blkatoff(v)
     78 	void *v;
     79 {
     80 	struct vop_blkatoff_args /* {
     81 		struct vnode *a_vp;
     82 		off_t a_offset;
     83 		char **a_res;
     84 		struct buf **a_bpp;
     85 	} */ *ap = v;
     86 	struct inode *ip;
     87 	struct fs *fs;
     88 	struct buf *bp;
     89 	daddr_t lbn;
     90 	int bsize, error;
     91 
     92 	ip = VTOI(ap->a_vp);
     93 	fs = ip->i_fs;
     94 	lbn = lblkno(fs, ap->a_offset);
     95 	bsize = blksize(fs, ip, lbn);
     96 
     97 	*ap->a_bpp = NULL;
     98 	if ((error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) != 0) {
     99 		brelse(bp);
    100 		return (error);
    101 	}
    102 	if (ap->a_res)
    103 		*ap->a_res = (char *)bp->b_data + blkoff(fs, ap->a_offset);
    104 	*ap->a_bpp = bp;
    105 	return (0);
    106 }
    107 
    108 
    109 /*
    110  * Load up the contents of an inode and copy the appropriate pieces
    111  * to the incore copy.
    112  */
    113 void
    114 ffs_load_inode(bp, ip, fs, ino)
    115 	struct buf *bp;
    116 	struct inode *ip;
    117 	struct fs *fs;
    118 	ino_t ino;
    119 {
    120 	struct ufs1_dinode *dp1;
    121 	struct ufs2_dinode *dp2;
    122 
    123 	if (ip->i_ump->um_fstype == UFS1) {
    124 		dp1 = (struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino);
    125 #ifdef FFS_EI
    126 		if (UFS_FSNEEDSWAP(fs))
    127 			ffs_dinode1_swap(dp1, ip->i_din.ffs1_din);
    128 		else
    129 #endif
    130 		*ip->i_din.ffs1_din = *dp1;
    131 
    132 		ip->i_mode = ip->i_ffs1_mode;
    133 		ip->i_nlink = ip->i_ffs1_nlink;
    134 		ip->i_size = ip->i_ffs1_size;
    135 		ip->i_flags = ip->i_ffs1_flags;
    136 		ip->i_gen = ip->i_ffs1_gen;
    137 		ip->i_uid = ip->i_ffs1_uid;
    138 		ip->i_gid = ip->i_ffs1_gid;
    139 	} else {
    140 		dp2 = (struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino);
    141 #ifdef FFS_EI
    142 		if (UFS_FSNEEDSWAP(fs))
    143 			ffs_dinode2_swap(dp2, ip->i_din.ffs2_din);
    144 		else
    145 #endif
    146 		*ip->i_din.ffs2_din = *dp2;
    147 
    148 		ip->i_mode = ip->i_ffs2_mode;
    149 		ip->i_nlink = ip->i_ffs2_nlink;
    150 		ip->i_size = ip->i_ffs2_size;
    151 		ip->i_flags = ip->i_ffs2_flags;
    152 		ip->i_gen = ip->i_ffs2_gen;
    153 		ip->i_uid = ip->i_ffs2_uid;
    154 		ip->i_gid = ip->i_ffs2_gid;
    155 	}
    156 }
    157 
    158 #endif	/* _KERNEL */
    159 
    160 /*
    161  * Update the frsum fields to reflect addition or deletion
    162  * of some frags.
    163  */
    164 void
    165 ffs_fragacct(fs, fragmap, fraglist, cnt, needswap)
    166 	struct fs *fs;
    167 	int fragmap;
    168 	int32_t fraglist[];
    169 	int cnt;
    170 	int needswap;
    171 {
    172 	int inblk;
    173 	int field, subfield;
    174 	int siz, pos;
    175 
    176 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
    177 	fragmap <<= 1;
    178 	for (siz = 1; siz < fs->fs_frag; siz++) {
    179 		if ((inblk & (1 << (siz + (fs->fs_frag & (NBBY - 1))))) == 0)
    180 			continue;
    181 		field = around[siz];
    182 		subfield = inside[siz];
    183 		for (pos = siz; pos <= fs->fs_frag; pos++) {
    184 			if ((fragmap & field) == subfield) {
    185 				fraglist[siz] = ufs_rw32(
    186 				    ufs_rw32(fraglist[siz], needswap) + cnt,
    187 				    needswap);
    188 				pos += siz;
    189 				field <<= siz;
    190 				subfield <<= siz;
    191 			}
    192 			field <<= 1;
    193 			subfield <<= 1;
    194 		}
    195 	}
    196 }
    197 
    198 #if defined(_KERNEL) && defined(DIAGNOSTIC)
    199 void
    200 ffs_checkoverlap(bp, ip)
    201 	struct buf *bp;
    202 	struct inode *ip;
    203 {
    204 	struct buf *ebp, *ep;
    205 	daddr_t start, last;
    206 	struct vnode *vp;
    207 
    208 	ebp = &buf[nbuf];
    209 	start = bp->b_blkno;
    210 	last = start + btodb(bp->b_bcount) - 1;
    211 	for (ep = buf; ep < ebp; ep++) {
    212 		if (ep == bp || (ep->b_flags & B_INVAL) ||
    213 		    ep->b_vp == NULLVP)
    214 			continue;
    215 		if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0, NULL))
    216 			continue;
    217 		if (vp != ip->i_devvp)
    218 			continue;
    219 		/* look for overlap */
    220 		if (ep->b_bcount == 0 || ep->b_blkno > last ||
    221 		    ep->b_blkno + btodb(ep->b_bcount) <= start)
    222 			continue;
    223 		vprint("Disk overlap", vp);
    224 		printf("\tstart %" PRId64 ", end %" PRId64 " overlap start "
    225 		    "%" PRId64 ", end %" PRId64 "\n",
    226 		    start, last, ep->b_blkno,
    227 		    ep->b_blkno + btodb(ep->b_bcount) - 1);
    228 		panic("Disk buffer overlap");
    229 	}
    230 }
    231 #endif /* _KERNEL && DIAGNOSTIC */
    232 
    233 /*
    234  * block operations
    235  *
    236  * check if a block is available
    237  */
    238 int
    239 ffs_isblock(fs, cp, h)
    240 	struct fs *fs;
    241 	u_char *cp;
    242 	int32_t h;
    243 {
    244 	u_char mask;
    245 
    246 	switch ((int)fs->fs_fragshift) {
    247 	case 3:
    248 		return (cp[h] == 0xff);
    249 	case 2:
    250 		mask = 0x0f << ((h & 0x1) << 2);
    251 		return ((cp[h >> 1] & mask) == mask);
    252 	case 1:
    253 		mask = 0x03 << ((h & 0x3) << 1);
    254 		return ((cp[h >> 2] & mask) == mask);
    255 	case 0:
    256 		mask = 0x01 << (h & 0x7);
    257 		return ((cp[h >> 3] & mask) == mask);
    258 	default:
    259 		panic("ffs_isblock: unknown fs_fragshift %d",
    260 		    (int)fs->fs_fragshift);
    261 	}
    262 }
    263 
    264 /*
    265  * check if a block is free
    266  */
    267 int
    268 ffs_isfreeblock(fs, cp, h)
    269 	struct fs *fs;
    270 	u_char *cp;
    271 	int32_t h;
    272 {
    273 
    274 	switch ((int)fs->fs_fragshift) {
    275 	case 3:
    276 		return (cp[h] == 0);
    277 	case 2:
    278 		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
    279 	case 1:
    280 		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
    281 	case 0:
    282 		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
    283 	default:
    284 		panic("ffs_isfreeblock: unknown fs_fragshift %d",
    285 		    (int)fs->fs_fragshift);
    286 	}
    287 }
    288 
    289 /*
    290  * take a block out of the map
    291  */
    292 void
    293 ffs_clrblock(fs, cp, h)
    294 	struct fs *fs;
    295 	u_char *cp;
    296 	int32_t h;
    297 {
    298 
    299 	switch ((int)fs->fs_fragshift) {
    300 	case 3:
    301 		cp[h] = 0;
    302 		return;
    303 	case 2:
    304 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
    305 		return;
    306 	case 1:
    307 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
    308 		return;
    309 	case 0:
    310 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
    311 		return;
    312 	default:
    313 		panic("ffs_clrblock: unknown fs_fragshift %d",
    314 		    (int)fs->fs_fragshift);
    315 	}
    316 }
    317 
    318 /*
    319  * put a block into the map
    320  */
    321 void
    322 ffs_setblock(fs, cp, h)
    323 	struct fs *fs;
    324 	u_char *cp;
    325 	int32_t h;
    326 {
    327 
    328 	switch ((int)fs->fs_fragshift) {
    329 	case 3:
    330 		cp[h] = 0xff;
    331 		return;
    332 	case 2:
    333 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
    334 		return;
    335 	case 1:
    336 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
    337 		return;
    338 	case 0:
    339 		cp[h >> 3] |= (0x01 << (h & 0x7));
    340 		return;
    341 	default:
    342 		panic("ffs_setblock: unknown fs_fragshift %d",
    343 		    (int)fs->fs_fragshift);
    344 	}
    345 }
    346