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