Home | History | Annotate | Line # | Download | only in ffs
ffs_subr.c revision 1.44
      1  1.44   hubertf /*	$NetBSD: ffs_subr.c,v 1.44 2007/01/29 01:52:46 hubertf Exp $	*/
      2   1.2       cgd 
      3   1.1   mycroft /*
      4   1.1   mycroft  * Copyright (c) 1982, 1986, 1989, 1993
      5   1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1   mycroft  *
      7   1.1   mycroft  * Redistribution and use in source and binary forms, with or without
      8   1.1   mycroft  * modification, are permitted provided that the following conditions
      9   1.1   mycroft  * are met:
     10   1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     11   1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     12   1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     15  1.29       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1   mycroft  *    may be used to endorse or promote products derived from this software
     17   1.1   mycroft  *    without specific prior written permission.
     18   1.1   mycroft  *
     19   1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1   mycroft  * SUCH DAMAGE.
     30   1.1   mycroft  *
     31  1.10      fvdl  *	@(#)ffs_subr.c	8.5 (Berkeley) 3/21/95
     32   1.1   mycroft  */
     33  1.19     lukem 
     34  1.30     lukem #if HAVE_NBTOOL_CONFIG_H
     35  1.30     lukem #include "nbtool_config.h"
     36  1.21        tv #endif
     37  1.21        tv 
     38  1.30     lukem #include <sys/cdefs.h>
     39  1.44   hubertf __KERNEL_RCSID(0, "$NetBSD: ffs_subr.c,v 1.44 2007/01/29 01:52:46 hubertf Exp $");
     40   1.1   mycroft 
     41   1.1   mycroft #include <sys/param.h>
     42  1.20     lukem 
     43  1.20     lukem /* in ffs_tables.c */
     44  1.24      matt extern const int inside[], around[];
     45  1.24      matt extern const u_char * const fragtbl[];
     46  1.20     lukem 
     47   1.1   mycroft #include <ufs/ffs/fs.h>
     48   1.7  christos #include <ufs/ffs/ffs_extern.h>
     49  1.11    bouyer #include <ufs/ufs/ufs_bswap.h>
     50  1.44   hubertf 
     51  1.44   hubertf #ifndef _KERNEL
     52  1.44   hubertf #include <ufs/ufs/dinode.h>
     53  1.35   xtraeme void    panic(const char *, ...)
     54  1.20     lukem     __attribute__((__noreturn__,__format__(__printf__,1,2)));
     55  1.13  drochner 
     56  1.20     lukem #else	/* _KERNEL */
     57  1.20     lukem #include <sys/systm.h>
     58   1.1   mycroft #include <sys/vnode.h>
     59  1.11    bouyer #include <sys/mount.h>
     60   1.1   mycroft #include <sys/buf.h>
     61  1.27      tron #include <sys/inttypes.h>
     62  1.28      fvdl #include <sys/pool.h>
     63  1.18     lukem #include <ufs/ufs/inode.h>
     64  1.11    bouyer #include <ufs/ufs/ufsmount.h>
     65  1.11    bouyer #include <ufs/ufs/ufs_extern.h>
     66   1.1   mycroft 
     67   1.1   mycroft /*
     68  1.28      fvdl  * Load up the contents of an inode and copy the appropriate pieces
     69  1.28      fvdl  * to the incore copy.
     70  1.28      fvdl  */
     71  1.28      fvdl void
     72  1.34   thorpej ffs_load_inode(struct buf *bp, struct inode *ip, struct fs *fs, ino_t ino)
     73  1.28      fvdl {
     74  1.28      fvdl 	struct ufs1_dinode *dp1;
     75  1.28      fvdl 	struct ufs2_dinode *dp2;
     76  1.28      fvdl 
     77  1.28      fvdl 	if (ip->i_ump->um_fstype == UFS1) {
     78  1.28      fvdl 		dp1 = (struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino);
     79  1.28      fvdl #ifdef FFS_EI
     80  1.28      fvdl 		if (UFS_FSNEEDSWAP(fs))
     81  1.28      fvdl 			ffs_dinode1_swap(dp1, ip->i_din.ffs1_din);
     82  1.28      fvdl 		else
     83  1.28      fvdl #endif
     84  1.28      fvdl 		*ip->i_din.ffs1_din = *dp1;
     85  1.28      fvdl 
     86  1.28      fvdl 		ip->i_mode = ip->i_ffs1_mode;
     87  1.28      fvdl 		ip->i_nlink = ip->i_ffs1_nlink;
     88  1.28      fvdl 		ip->i_size = ip->i_ffs1_size;
     89  1.28      fvdl 		ip->i_flags = ip->i_ffs1_flags;
     90  1.28      fvdl 		ip->i_gen = ip->i_ffs1_gen;
     91  1.28      fvdl 		ip->i_uid = ip->i_ffs1_uid;
     92  1.28      fvdl 		ip->i_gid = ip->i_ffs1_gid;
     93  1.28      fvdl 	} else {
     94  1.28      fvdl 		dp2 = (struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino);
     95  1.28      fvdl #ifdef FFS_EI
     96  1.28      fvdl 		if (UFS_FSNEEDSWAP(fs))
     97  1.28      fvdl 			ffs_dinode2_swap(dp2, ip->i_din.ffs2_din);
     98  1.28      fvdl 		else
     99  1.28      fvdl #endif
    100  1.28      fvdl 		*ip->i_din.ffs2_din = *dp2;
    101  1.28      fvdl 
    102  1.28      fvdl 		ip->i_mode = ip->i_ffs2_mode;
    103  1.28      fvdl 		ip->i_nlink = ip->i_ffs2_nlink;
    104  1.28      fvdl 		ip->i_size = ip->i_ffs2_size;
    105  1.28      fvdl 		ip->i_flags = ip->i_ffs2_flags;
    106  1.28      fvdl 		ip->i_gen = ip->i_ffs2_gen;
    107  1.28      fvdl 		ip->i_uid = ip->i_ffs2_uid;
    108  1.28      fvdl 		ip->i_gid = ip->i_ffs2_gid;
    109  1.28      fvdl 	}
    110  1.28      fvdl }
    111  1.28      fvdl 
    112  1.20     lukem #endif	/* _KERNEL */
    113   1.1   mycroft 
    114   1.1   mycroft /*
    115  1.33     perry  * Update the frsum fields to reflect addition or deletion
    116   1.1   mycroft  * of some frags.
    117   1.1   mycroft  */
    118   1.1   mycroft void
    119  1.34   thorpej ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt,
    120  1.43  christos     int needswap)
    121   1.1   mycroft {
    122   1.1   mycroft 	int inblk;
    123  1.15  augustss 	int field, subfield;
    124  1.15  augustss 	int siz, pos;
    125   1.1   mycroft 
    126   1.1   mycroft 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
    127   1.1   mycroft 	fragmap <<= 1;
    128   1.1   mycroft 	for (siz = 1; siz < fs->fs_frag; siz++) {
    129  1.22   mycroft 		if ((inblk & (1 << (siz + (fs->fs_frag & (NBBY - 1))))) == 0)
    130   1.1   mycroft 			continue;
    131   1.1   mycroft 		field = around[siz];
    132   1.1   mycroft 		subfield = inside[siz];
    133   1.1   mycroft 		for (pos = siz; pos <= fs->fs_frag; pos++) {
    134   1.1   mycroft 			if ((fragmap & field) == subfield) {
    135  1.11    bouyer 				fraglist[siz] = ufs_rw32(
    136  1.12    kleink 				    ufs_rw32(fraglist[siz], needswap) + cnt,
    137  1.12    kleink 				    needswap);
    138   1.1   mycroft 				pos += siz;
    139   1.1   mycroft 				field <<= siz;
    140   1.1   mycroft 				subfield <<= siz;
    141   1.1   mycroft 			}
    142   1.1   mycroft 			field <<= 1;
    143   1.1   mycroft 			subfield <<= 1;
    144   1.1   mycroft 		}
    145   1.1   mycroft 	}
    146   1.1   mycroft }
    147   1.1   mycroft 
    148   1.1   mycroft /*
    149   1.1   mycroft  * block operations
    150   1.1   mycroft  *
    151   1.1   mycroft  * check if a block is available
    152  1.31       dbj  *  returns true if all the correponding bits in the free map are 1
    153  1.33     perry  *  returns false if any corresponding bit in the free map is 0
    154   1.1   mycroft  */
    155   1.1   mycroft int
    156  1.34   thorpej ffs_isblock(struct fs *fs, u_char *cp, int32_t h)
    157   1.1   mycroft {
    158  1.16     lukem 	u_char mask;
    159   1.1   mycroft 
    160  1.22   mycroft 	switch ((int)fs->fs_fragshift) {
    161  1.22   mycroft 	case 3:
    162   1.1   mycroft 		return (cp[h] == 0xff);
    163  1.22   mycroft 	case 2:
    164   1.1   mycroft 		mask = 0x0f << ((h & 0x1) << 2);
    165   1.1   mycroft 		return ((cp[h >> 1] & mask) == mask);
    166  1.22   mycroft 	case 1:
    167   1.1   mycroft 		mask = 0x03 << ((h & 0x3) << 1);
    168   1.1   mycroft 		return ((cp[h >> 2] & mask) == mask);
    169  1.22   mycroft 	case 0:
    170   1.1   mycroft 		mask = 0x01 << (h & 0x7);
    171   1.1   mycroft 		return ((cp[h >> 3] & mask) == mask);
    172   1.1   mycroft 	default:
    173  1.22   mycroft 		panic("ffs_isblock: unknown fs_fragshift %d",
    174  1.22   mycroft 		    (int)fs->fs_fragshift);
    175  1.14      fvdl 	}
    176  1.14      fvdl }
    177  1.14      fvdl 
    178  1.14      fvdl /*
    179  1.31       dbj  * check if a block is completely allocated
    180  1.31       dbj  *  returns true if all the corresponding bits in the free map are 0
    181  1.31       dbj  *  returns false if any corresponding bit in the free map is 1
    182  1.14      fvdl  */
    183  1.14      fvdl int
    184  1.34   thorpej ffs_isfreeblock(struct fs *fs, u_char *cp, int32_t h)
    185  1.14      fvdl {
    186  1.14      fvdl 
    187  1.22   mycroft 	switch ((int)fs->fs_fragshift) {
    188  1.22   mycroft 	case 3:
    189  1.14      fvdl 		return (cp[h] == 0);
    190  1.22   mycroft 	case 2:
    191  1.14      fvdl 		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
    192  1.22   mycroft 	case 1:
    193  1.14      fvdl 		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
    194  1.22   mycroft 	case 0:
    195  1.14      fvdl 		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
    196  1.14      fvdl 	default:
    197  1.22   mycroft 		panic("ffs_isfreeblock: unknown fs_fragshift %d",
    198  1.22   mycroft 		    (int)fs->fs_fragshift);
    199   1.1   mycroft 	}
    200   1.1   mycroft }
    201   1.1   mycroft 
    202   1.1   mycroft /*
    203   1.1   mycroft  * take a block out of the map
    204   1.1   mycroft  */
    205   1.1   mycroft void
    206  1.34   thorpej ffs_clrblock(struct fs *fs, u_char *cp, int32_t h)
    207   1.1   mycroft {
    208   1.1   mycroft 
    209  1.22   mycroft 	switch ((int)fs->fs_fragshift) {
    210  1.22   mycroft 	case 3:
    211   1.1   mycroft 		cp[h] = 0;
    212   1.1   mycroft 		return;
    213  1.22   mycroft 	case 2:
    214   1.1   mycroft 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
    215   1.1   mycroft 		return;
    216  1.22   mycroft 	case 1:
    217   1.1   mycroft 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
    218   1.1   mycroft 		return;
    219  1.22   mycroft 	case 0:
    220   1.1   mycroft 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
    221   1.1   mycroft 		return;
    222   1.1   mycroft 	default:
    223  1.22   mycroft 		panic("ffs_clrblock: unknown fs_fragshift %d",
    224  1.22   mycroft 		    (int)fs->fs_fragshift);
    225   1.1   mycroft 	}
    226   1.1   mycroft }
    227   1.1   mycroft 
    228   1.1   mycroft /*
    229   1.1   mycroft  * put a block into the map
    230   1.1   mycroft  */
    231   1.1   mycroft void
    232  1.34   thorpej ffs_setblock(struct fs *fs, u_char *cp, int32_t h)
    233   1.1   mycroft {
    234   1.1   mycroft 
    235  1.22   mycroft 	switch ((int)fs->fs_fragshift) {
    236  1.22   mycroft 	case 3:
    237   1.1   mycroft 		cp[h] = 0xff;
    238   1.1   mycroft 		return;
    239  1.22   mycroft 	case 2:
    240   1.1   mycroft 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
    241   1.1   mycroft 		return;
    242  1.22   mycroft 	case 1:
    243   1.1   mycroft 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
    244   1.1   mycroft 		return;
    245  1.22   mycroft 	case 0:
    246   1.1   mycroft 		cp[h >> 3] |= (0x01 << (h & 0x7));
    247   1.1   mycroft 		return;
    248   1.1   mycroft 	default:
    249  1.22   mycroft 		panic("ffs_setblock: unknown fs_fragshift %d",
    250  1.22   mycroft 		    (int)fs->fs_fragshift);
    251   1.1   mycroft 	}
    252   1.1   mycroft }
    253