Home | History | Annotate | Line # | Download | only in ffs
ffs_subr.c revision 1.28.2.5
      1 /*	$NetBSD: ffs_subr.c,v 1.28.2.5 2005/11/10 14:12:32 skrll 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 #if HAVE_NBTOOL_CONFIG_H
     35 #include "nbtool_config.h"
     36 #endif
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: ffs_subr.c,v 1.28.2.5 2005/11/10 14:12:32 skrll Exp $");
     40 
     41 #include <sys/param.h>
     42 
     43 /* in ffs_tables.c */
     44 extern const int inside[], around[];
     45 extern const u_char * const fragtbl[];
     46 
     47 #ifndef _KERNEL
     48 #include <ufs/ufs/dinode.h>
     49 #include <ufs/ffs/fs.h>
     50 #include <ufs/ffs/ffs_extern.h>
     51 #include <ufs/ufs/ufs_bswap.h>
     52 void    panic(const char *, ...)
     53     __attribute__((__noreturn__,__format__(__printf__,1,2)));
     54 
     55 #else	/* _KERNEL */
     56 #include <sys/systm.h>
     57 #include <sys/vnode.h>
     58 #include <sys/mount.h>
     59 #include <sys/buf.h>
     60 #include <sys/inttypes.h>
     61 #include <sys/pool.h>
     62 #include <ufs/ufs/inode.h>
     63 #include <ufs/ufs/ufsmount.h>
     64 #include <ufs/ufs/ufs_extern.h>
     65 #include <ufs/ffs/fs.h>
     66 #include <ufs/ffs/ffs_extern.h>
     67 #include <ufs/ufs/ufs_bswap.h>
     68 
     69 /*
     70  * Return buffer with the contents of block "offset" from the beginning of
     71  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
     72  * remaining space in the directory.
     73  */
     74 int
     75 ffs_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp)
     76 {
     77 	struct inode *ip;
     78 	struct fs *fs;
     79 	struct buf *bp;
     80 	daddr_t lbn;
     81 	int bsize, error;
     82 
     83 	ip = VTOI(vp);
     84 	fs = ip->i_fs;
     85 	lbn = lblkno(fs, offset);
     86 	bsize = blksize(fs, ip, lbn);
     87 
     88 	*bpp = NULL;
     89 	if ((error = bread(vp, lbn, bsize, NOCRED, &bp)) != 0) {
     90 		brelse(bp);
     91 		return (error);
     92 	}
     93 	if (res)
     94 		*res = (char *)bp->b_data + blkoff(fs, offset);
     95 	*bpp = bp;
     96 	return (0);
     97 }
     98 
     99 
    100 /*
    101  * Load up the contents of an inode and copy the appropriate pieces
    102  * to the incore copy.
    103  */
    104 void
    105 ffs_load_inode(struct buf *bp, struct inode *ip, struct fs *fs, ino_t ino)
    106 {
    107 	struct ufs1_dinode *dp1;
    108 	struct ufs2_dinode *dp2;
    109 
    110 	if (ip->i_ump->um_fstype == UFS1) {
    111 		dp1 = (struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino);
    112 #ifdef FFS_EI
    113 		if (UFS_FSNEEDSWAP(fs))
    114 			ffs_dinode1_swap(dp1, ip->i_din.ffs1_din);
    115 		else
    116 #endif
    117 		*ip->i_din.ffs1_din = *dp1;
    118 
    119 		ip->i_mode = ip->i_ffs1_mode;
    120 		ip->i_nlink = ip->i_ffs1_nlink;
    121 		ip->i_size = ip->i_ffs1_size;
    122 		ip->i_flags = ip->i_ffs1_flags;
    123 		ip->i_gen = ip->i_ffs1_gen;
    124 		ip->i_uid = ip->i_ffs1_uid;
    125 		ip->i_gid = ip->i_ffs1_gid;
    126 	} else {
    127 		dp2 = (struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino);
    128 #ifdef FFS_EI
    129 		if (UFS_FSNEEDSWAP(fs))
    130 			ffs_dinode2_swap(dp2, ip->i_din.ffs2_din);
    131 		else
    132 #endif
    133 		*ip->i_din.ffs2_din = *dp2;
    134 
    135 		ip->i_mode = ip->i_ffs2_mode;
    136 		ip->i_nlink = ip->i_ffs2_nlink;
    137 		ip->i_size = ip->i_ffs2_size;
    138 		ip->i_flags = ip->i_ffs2_flags;
    139 		ip->i_gen = ip->i_ffs2_gen;
    140 		ip->i_uid = ip->i_ffs2_uid;
    141 		ip->i_gid = ip->i_ffs2_gid;
    142 	}
    143 }
    144 
    145 #endif	/* _KERNEL */
    146 
    147 /*
    148  * Update the frsum fields to reflect addition or deletion
    149  * of some frags.
    150  */
    151 void
    152 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt,
    153     int needswap)
    154 {
    155 	int inblk;
    156 	int field, subfield;
    157 	int siz, pos;
    158 
    159 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
    160 	fragmap <<= 1;
    161 	for (siz = 1; siz < fs->fs_frag; siz++) {
    162 		if ((inblk & (1 << (siz + (fs->fs_frag & (NBBY - 1))))) == 0)
    163 			continue;
    164 		field = around[siz];
    165 		subfield = inside[siz];
    166 		for (pos = siz; pos <= fs->fs_frag; pos++) {
    167 			if ((fragmap & field) == subfield) {
    168 				fraglist[siz] = ufs_rw32(
    169 				    ufs_rw32(fraglist[siz], needswap) + cnt,
    170 				    needswap);
    171 				pos += siz;
    172 				field <<= siz;
    173 				subfield <<= siz;
    174 			}
    175 			field <<= 1;
    176 			subfield <<= 1;
    177 		}
    178 	}
    179 }
    180 
    181 #if defined(_KERNEL) && defined(DIAGNOSTIC)
    182 void
    183 ffs_checkoverlap(struct buf *bp, struct inode *ip)
    184 {
    185 #if 0
    186 	struct buf *ebp, *ep;
    187 	daddr_t start, last;
    188 	struct vnode *vp;
    189 
    190 	ebp = &buf[nbuf];
    191 	start = bp->b_blkno;
    192 	last = start + btodb(bp->b_bcount) - 1;
    193 	for (ep = buf; ep < ebp; ep++) {
    194 		if (ep == bp || (ep->b_flags & B_INVAL) ||
    195 		    ep->b_vp == NULLVP)
    196 			continue;
    197 		if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0, NULL))
    198 			continue;
    199 		if (vp != ip->i_devvp)
    200 			continue;
    201 		/* look for overlap */
    202 		if (ep->b_bcount == 0 || ep->b_blkno > last ||
    203 		    ep->b_blkno + btodb(ep->b_bcount) <= start)
    204 			continue;
    205 		vprint("Disk overlap", vp);
    206 		printf("\tstart %" PRId64 ", end %" PRId64 " overlap start "
    207 		    "%" PRId64 ", end %" PRId64 "\n",
    208 		    start, last, ep->b_blkno,
    209 		    ep->b_blkno + btodb(ep->b_bcount) - 1);
    210 		panic("Disk buffer overlap");
    211 	}
    212 #else
    213 	printf("ffs_checkoverlap disabled due to buffer cache implementation changes\n");
    214 #endif
    215 }
    216 #endif /* _KERNEL && DIAGNOSTIC */
    217 
    218 /*
    219  * block operations
    220  *
    221  * check if a block is available
    222  *  returns true if all the correponding bits in the free map are 1
    223  *  returns false if any corresponding bit in the free map is 0
    224  */
    225 int
    226 ffs_isblock(struct fs *fs, u_char *cp, int32_t h)
    227 {
    228 	u_char mask;
    229 
    230 	switch ((int)fs->fs_fragshift) {
    231 	case 3:
    232 		return (cp[h] == 0xff);
    233 	case 2:
    234 		mask = 0x0f << ((h & 0x1) << 2);
    235 		return ((cp[h >> 1] & mask) == mask);
    236 	case 1:
    237 		mask = 0x03 << ((h & 0x3) << 1);
    238 		return ((cp[h >> 2] & mask) == mask);
    239 	case 0:
    240 		mask = 0x01 << (h & 0x7);
    241 		return ((cp[h >> 3] & mask) == mask);
    242 	default:
    243 		panic("ffs_isblock: unknown fs_fragshift %d",
    244 		    (int)fs->fs_fragshift);
    245 	}
    246 }
    247 
    248 /*
    249  * check if a block is completely allocated
    250  *  returns true if all the corresponding bits in the free map are 0
    251  *  returns false if any corresponding bit in the free map is 1
    252  */
    253 int
    254 ffs_isfreeblock(struct fs *fs, u_char *cp, int32_t h)
    255 {
    256 
    257 	switch ((int)fs->fs_fragshift) {
    258 	case 3:
    259 		return (cp[h] == 0);
    260 	case 2:
    261 		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
    262 	case 1:
    263 		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
    264 	case 0:
    265 		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
    266 	default:
    267 		panic("ffs_isfreeblock: unknown fs_fragshift %d",
    268 		    (int)fs->fs_fragshift);
    269 	}
    270 }
    271 
    272 /*
    273  * take a block out of the map
    274  */
    275 void
    276 ffs_clrblock(struct fs *fs, u_char *cp, int32_t h)
    277 {
    278 
    279 	switch ((int)fs->fs_fragshift) {
    280 	case 3:
    281 		cp[h] = 0;
    282 		return;
    283 	case 2:
    284 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
    285 		return;
    286 	case 1:
    287 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
    288 		return;
    289 	case 0:
    290 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
    291 		return;
    292 	default:
    293 		panic("ffs_clrblock: unknown fs_fragshift %d",
    294 		    (int)fs->fs_fragshift);
    295 	}
    296 }
    297 
    298 /*
    299  * put a block into the map
    300  */
    301 void
    302 ffs_setblock(struct fs *fs, u_char *cp, int32_t h)
    303 {
    304 
    305 	switch ((int)fs->fs_fragshift) {
    306 	case 3:
    307 		cp[h] = 0xff;
    308 		return;
    309 	case 2:
    310 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
    311 		return;
    312 	case 1:
    313 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
    314 		return;
    315 	case 0:
    316 		cp[h >> 3] |= (0x01 << (h & 0x7));
    317 		return;
    318 	default:
    319 		panic("ffs_setblock: unknown fs_fragshift %d",
    320 		    (int)fs->fs_fragshift);
    321 	}
    322 }
    323