Home | History | Annotate | Line # | Download | only in ffs
ffs_subr.c revision 1.43
      1 /*	$NetBSD: ffs_subr.c,v 1.43 2006/11/16 01:33:53 christos 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.43 2006/11/16 01:33:53 christos 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  * Load up the contents of an inode and copy the appropriate pieces
     71  * to the incore copy.
     72  */
     73 void
     74 ffs_load_inode(struct buf *bp, struct inode *ip, struct fs *fs, ino_t ino)
     75 {
     76 	struct ufs1_dinode *dp1;
     77 	struct ufs2_dinode *dp2;
     78 
     79 	if (ip->i_ump->um_fstype == UFS1) {
     80 		dp1 = (struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino);
     81 #ifdef FFS_EI
     82 		if (UFS_FSNEEDSWAP(fs))
     83 			ffs_dinode1_swap(dp1, ip->i_din.ffs1_din);
     84 		else
     85 #endif
     86 		*ip->i_din.ffs1_din = *dp1;
     87 
     88 		ip->i_mode = ip->i_ffs1_mode;
     89 		ip->i_nlink = ip->i_ffs1_nlink;
     90 		ip->i_size = ip->i_ffs1_size;
     91 		ip->i_flags = ip->i_ffs1_flags;
     92 		ip->i_gen = ip->i_ffs1_gen;
     93 		ip->i_uid = ip->i_ffs1_uid;
     94 		ip->i_gid = ip->i_ffs1_gid;
     95 	} else {
     96 		dp2 = (struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino);
     97 #ifdef FFS_EI
     98 		if (UFS_FSNEEDSWAP(fs))
     99 			ffs_dinode2_swap(dp2, ip->i_din.ffs2_din);
    100 		else
    101 #endif
    102 		*ip->i_din.ffs2_din = *dp2;
    103 
    104 		ip->i_mode = ip->i_ffs2_mode;
    105 		ip->i_nlink = ip->i_ffs2_nlink;
    106 		ip->i_size = ip->i_ffs2_size;
    107 		ip->i_flags = ip->i_ffs2_flags;
    108 		ip->i_gen = ip->i_ffs2_gen;
    109 		ip->i_uid = ip->i_ffs2_uid;
    110 		ip->i_gid = ip->i_ffs2_gid;
    111 	}
    112 }
    113 
    114 #endif	/* _KERNEL */
    115 
    116 /*
    117  * Update the frsum fields to reflect addition or deletion
    118  * of some frags.
    119  */
    120 void
    121 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt,
    122     int needswap)
    123 {
    124 	int inblk;
    125 	int field, subfield;
    126 	int siz, pos;
    127 
    128 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
    129 	fragmap <<= 1;
    130 	for (siz = 1; siz < fs->fs_frag; siz++) {
    131 		if ((inblk & (1 << (siz + (fs->fs_frag & (NBBY - 1))))) == 0)
    132 			continue;
    133 		field = around[siz];
    134 		subfield = inside[siz];
    135 		for (pos = siz; pos <= fs->fs_frag; pos++) {
    136 			if ((fragmap & field) == subfield) {
    137 				fraglist[siz] = ufs_rw32(
    138 				    ufs_rw32(fraglist[siz], needswap) + cnt,
    139 				    needswap);
    140 				pos += siz;
    141 				field <<= siz;
    142 				subfield <<= siz;
    143 			}
    144 			field <<= 1;
    145 			subfield <<= 1;
    146 		}
    147 	}
    148 }
    149 
    150 /*
    151  * block operations
    152  *
    153  * check if a block is available
    154  *  returns true if all the correponding bits in the free map are 1
    155  *  returns false if any corresponding bit in the free map is 0
    156  */
    157 int
    158 ffs_isblock(struct fs *fs, u_char *cp, int32_t h)
    159 {
    160 	u_char mask;
    161 
    162 	switch ((int)fs->fs_fragshift) {
    163 	case 3:
    164 		return (cp[h] == 0xff);
    165 	case 2:
    166 		mask = 0x0f << ((h & 0x1) << 2);
    167 		return ((cp[h >> 1] & mask) == mask);
    168 	case 1:
    169 		mask = 0x03 << ((h & 0x3) << 1);
    170 		return ((cp[h >> 2] & mask) == mask);
    171 	case 0:
    172 		mask = 0x01 << (h & 0x7);
    173 		return ((cp[h >> 3] & mask) == mask);
    174 	default:
    175 		panic("ffs_isblock: unknown fs_fragshift %d",
    176 		    (int)fs->fs_fragshift);
    177 	}
    178 }
    179 
    180 /*
    181  * check if a block is completely allocated
    182  *  returns true if all the corresponding bits in the free map are 0
    183  *  returns false if any corresponding bit in the free map is 1
    184  */
    185 int
    186 ffs_isfreeblock(struct fs *fs, u_char *cp, int32_t h)
    187 {
    188 
    189 	switch ((int)fs->fs_fragshift) {
    190 	case 3:
    191 		return (cp[h] == 0);
    192 	case 2:
    193 		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
    194 	case 1:
    195 		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
    196 	case 0:
    197 		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
    198 	default:
    199 		panic("ffs_isfreeblock: unknown fs_fragshift %d",
    200 		    (int)fs->fs_fragshift);
    201 	}
    202 }
    203 
    204 /*
    205  * take a block out of the map
    206  */
    207 void
    208 ffs_clrblock(struct fs *fs, u_char *cp, int32_t h)
    209 {
    210 
    211 	switch ((int)fs->fs_fragshift) {
    212 	case 3:
    213 		cp[h] = 0;
    214 		return;
    215 	case 2:
    216 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
    217 		return;
    218 	case 1:
    219 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
    220 		return;
    221 	case 0:
    222 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
    223 		return;
    224 	default:
    225 		panic("ffs_clrblock: unknown fs_fragshift %d",
    226 		    (int)fs->fs_fragshift);
    227 	}
    228 }
    229 
    230 /*
    231  * put a block into the map
    232  */
    233 void
    234 ffs_setblock(struct fs *fs, u_char *cp, int32_t h)
    235 {
    236 
    237 	switch ((int)fs->fs_fragshift) {
    238 	case 3:
    239 		cp[h] = 0xff;
    240 		return;
    241 	case 2:
    242 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
    243 		return;
    244 	case 1:
    245 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
    246 		return;
    247 	case 0:
    248 		cp[h >> 3] |= (0x01 << (h & 0x7));
    249 		return;
    250 	default:
    251 		panic("ffs_setblock: unknown fs_fragshift %d",
    252 		    (int)fs->fs_fragshift);
    253 	}
    254 }
    255