Home | History | Annotate | Line # | Download | only in ffs
ffs_subr.c revision 1.45.20.1
      1  1.45.20.1     rmind /*	$NetBSD: ffs_subr.c,v 1.45.20.1 2011/04/21 01:42:20 rmind 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.45.20.1     rmind __KERNEL_RCSID(0, "$NetBSD: ffs_subr.c,v 1.45.20.1 2011/04/21 01:42:20 rmind 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.45.20.1     rmind #ifndef _KERNEL
     48  1.45.20.1     rmind #define FFS_EI /* always include byteswapped filesystems support */
     49  1.45.20.1     rmind #endif
     50        1.1   mycroft #include <ufs/ffs/fs.h>
     51        1.7  christos #include <ufs/ffs/ffs_extern.h>
     52       1.11    bouyer #include <ufs/ufs/ufs_bswap.h>
     53       1.44   hubertf 
     54       1.44   hubertf #ifndef _KERNEL
     55       1.44   hubertf #include <ufs/ufs/dinode.h>
     56       1.35   xtraeme void    panic(const char *, ...)
     57       1.20     lukem     __attribute__((__noreturn__,__format__(__printf__,1,2)));
     58       1.13  drochner 
     59       1.20     lukem #else	/* _KERNEL */
     60       1.20     lukem #include <sys/systm.h>
     61        1.1   mycroft #include <sys/vnode.h>
     62       1.11    bouyer #include <sys/mount.h>
     63        1.1   mycroft #include <sys/buf.h>
     64       1.27      tron #include <sys/inttypes.h>
     65       1.28      fvdl #include <sys/pool.h>
     66       1.45   hannken #include <sys/fstrans.h>
     67       1.18     lukem #include <ufs/ufs/inode.h>
     68       1.11    bouyer #include <ufs/ufs/ufsmount.h>
     69       1.11    bouyer #include <ufs/ufs/ufs_extern.h>
     70        1.1   mycroft 
     71        1.1   mycroft /*
     72       1.28      fvdl  * Load up the contents of an inode and copy the appropriate pieces
     73       1.28      fvdl  * to the incore copy.
     74       1.28      fvdl  */
     75       1.28      fvdl void
     76       1.34   thorpej ffs_load_inode(struct buf *bp, struct inode *ip, struct fs *fs, ino_t ino)
     77       1.28      fvdl {
     78       1.28      fvdl 	struct ufs1_dinode *dp1;
     79       1.28      fvdl 	struct ufs2_dinode *dp2;
     80       1.28      fvdl 
     81       1.28      fvdl 	if (ip->i_ump->um_fstype == UFS1) {
     82       1.28      fvdl 		dp1 = (struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino);
     83       1.28      fvdl #ifdef FFS_EI
     84       1.28      fvdl 		if (UFS_FSNEEDSWAP(fs))
     85       1.28      fvdl 			ffs_dinode1_swap(dp1, ip->i_din.ffs1_din);
     86       1.28      fvdl 		else
     87       1.28      fvdl #endif
     88       1.28      fvdl 		*ip->i_din.ffs1_din = *dp1;
     89       1.28      fvdl 
     90       1.28      fvdl 		ip->i_mode = ip->i_ffs1_mode;
     91       1.28      fvdl 		ip->i_nlink = ip->i_ffs1_nlink;
     92       1.28      fvdl 		ip->i_size = ip->i_ffs1_size;
     93       1.28      fvdl 		ip->i_flags = ip->i_ffs1_flags;
     94       1.28      fvdl 		ip->i_gen = ip->i_ffs1_gen;
     95       1.28      fvdl 		ip->i_uid = ip->i_ffs1_uid;
     96       1.28      fvdl 		ip->i_gid = ip->i_ffs1_gid;
     97       1.28      fvdl 	} else {
     98       1.28      fvdl 		dp2 = (struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino);
     99       1.28      fvdl #ifdef FFS_EI
    100       1.28      fvdl 		if (UFS_FSNEEDSWAP(fs))
    101       1.28      fvdl 			ffs_dinode2_swap(dp2, ip->i_din.ffs2_din);
    102       1.28      fvdl 		else
    103       1.28      fvdl #endif
    104       1.28      fvdl 		*ip->i_din.ffs2_din = *dp2;
    105       1.28      fvdl 
    106       1.28      fvdl 		ip->i_mode = ip->i_ffs2_mode;
    107       1.28      fvdl 		ip->i_nlink = ip->i_ffs2_nlink;
    108       1.28      fvdl 		ip->i_size = ip->i_ffs2_size;
    109       1.28      fvdl 		ip->i_flags = ip->i_ffs2_flags;
    110       1.28      fvdl 		ip->i_gen = ip->i_ffs2_gen;
    111       1.28      fvdl 		ip->i_uid = ip->i_ffs2_uid;
    112       1.28      fvdl 		ip->i_gid = ip->i_ffs2_gid;
    113       1.28      fvdl 	}
    114       1.28      fvdl }
    115       1.28      fvdl 
    116       1.45   hannken int
    117       1.45   hannken ffs_getblk(struct vnode *vp, daddr_t lblkno, daddr_t blkno, int size,
    118       1.45   hannken     bool clearbuf, buf_t **bpp)
    119       1.45   hannken {
    120       1.45   hannken 	int error = 0;
    121       1.45   hannken 
    122       1.45   hannken 	KASSERT(blkno >= 0 || blkno == FFS_NOBLK);
    123       1.45   hannken 
    124       1.45   hannken 	if ((*bpp = getblk(vp, lblkno, size, 0, 0)) == NULL)
    125       1.45   hannken 		return ENOMEM;
    126       1.45   hannken 	if (blkno != FFS_NOBLK)
    127       1.45   hannken 		(*bpp)->b_blkno = blkno;
    128       1.45   hannken 	if (clearbuf)
    129       1.45   hannken 		clrbuf(*bpp);
    130       1.45   hannken 	if ((*bpp)->b_blkno >= 0 && (error = fscow_run(*bpp, false)) != 0)
    131       1.45   hannken 		brelse(*bpp, BC_INVAL);
    132       1.45   hannken 	return error;
    133       1.45   hannken }
    134       1.45   hannken 
    135       1.20     lukem #endif	/* _KERNEL */
    136        1.1   mycroft 
    137        1.1   mycroft /*
    138       1.33     perry  * Update the frsum fields to reflect addition or deletion
    139        1.1   mycroft  * of some frags.
    140        1.1   mycroft  */
    141        1.1   mycroft void
    142       1.34   thorpej ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt,
    143       1.43  christos     int needswap)
    144        1.1   mycroft {
    145        1.1   mycroft 	int inblk;
    146       1.15  augustss 	int field, subfield;
    147       1.15  augustss 	int siz, pos;
    148        1.1   mycroft 
    149        1.1   mycroft 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
    150        1.1   mycroft 	fragmap <<= 1;
    151        1.1   mycroft 	for (siz = 1; siz < fs->fs_frag; siz++) {
    152       1.22   mycroft 		if ((inblk & (1 << (siz + (fs->fs_frag & (NBBY - 1))))) == 0)
    153        1.1   mycroft 			continue;
    154        1.1   mycroft 		field = around[siz];
    155        1.1   mycroft 		subfield = inside[siz];
    156        1.1   mycroft 		for (pos = siz; pos <= fs->fs_frag; pos++) {
    157        1.1   mycroft 			if ((fragmap & field) == subfield) {
    158       1.11    bouyer 				fraglist[siz] = ufs_rw32(
    159       1.12    kleink 				    ufs_rw32(fraglist[siz], needswap) + cnt,
    160       1.12    kleink 				    needswap);
    161        1.1   mycroft 				pos += siz;
    162        1.1   mycroft 				field <<= siz;
    163        1.1   mycroft 				subfield <<= siz;
    164        1.1   mycroft 			}
    165        1.1   mycroft 			field <<= 1;
    166        1.1   mycroft 			subfield <<= 1;
    167        1.1   mycroft 		}
    168        1.1   mycroft 	}
    169        1.1   mycroft }
    170        1.1   mycroft 
    171        1.1   mycroft /*
    172        1.1   mycroft  * block operations
    173        1.1   mycroft  *
    174        1.1   mycroft  * check if a block is available
    175       1.31       dbj  *  returns true if all the correponding bits in the free map are 1
    176       1.33     perry  *  returns false if any corresponding bit in the free map is 0
    177        1.1   mycroft  */
    178        1.1   mycroft int
    179       1.34   thorpej ffs_isblock(struct fs *fs, u_char *cp, int32_t h)
    180        1.1   mycroft {
    181       1.16     lukem 	u_char mask;
    182        1.1   mycroft 
    183       1.22   mycroft 	switch ((int)fs->fs_fragshift) {
    184       1.22   mycroft 	case 3:
    185        1.1   mycroft 		return (cp[h] == 0xff);
    186       1.22   mycroft 	case 2:
    187        1.1   mycroft 		mask = 0x0f << ((h & 0x1) << 2);
    188        1.1   mycroft 		return ((cp[h >> 1] & mask) == mask);
    189       1.22   mycroft 	case 1:
    190        1.1   mycroft 		mask = 0x03 << ((h & 0x3) << 1);
    191        1.1   mycroft 		return ((cp[h >> 2] & mask) == mask);
    192       1.22   mycroft 	case 0:
    193        1.1   mycroft 		mask = 0x01 << (h & 0x7);
    194        1.1   mycroft 		return ((cp[h >> 3] & mask) == mask);
    195        1.1   mycroft 	default:
    196       1.22   mycroft 		panic("ffs_isblock: unknown fs_fragshift %d",
    197       1.22   mycroft 		    (int)fs->fs_fragshift);
    198       1.14      fvdl 	}
    199       1.14      fvdl }
    200       1.14      fvdl 
    201       1.14      fvdl /*
    202       1.31       dbj  * check if a block is completely allocated
    203       1.31       dbj  *  returns true if all the corresponding bits in the free map are 0
    204       1.31       dbj  *  returns false if any corresponding bit in the free map is 1
    205       1.14      fvdl  */
    206       1.14      fvdl int
    207       1.34   thorpej ffs_isfreeblock(struct fs *fs, u_char *cp, int32_t h)
    208       1.14      fvdl {
    209       1.14      fvdl 
    210       1.22   mycroft 	switch ((int)fs->fs_fragshift) {
    211       1.22   mycroft 	case 3:
    212       1.14      fvdl 		return (cp[h] == 0);
    213       1.22   mycroft 	case 2:
    214       1.14      fvdl 		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
    215       1.22   mycroft 	case 1:
    216       1.14      fvdl 		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
    217       1.22   mycroft 	case 0:
    218       1.14      fvdl 		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
    219       1.14      fvdl 	default:
    220       1.22   mycroft 		panic("ffs_isfreeblock: unknown fs_fragshift %d",
    221       1.22   mycroft 		    (int)fs->fs_fragshift);
    222        1.1   mycroft 	}
    223        1.1   mycroft }
    224        1.1   mycroft 
    225        1.1   mycroft /*
    226        1.1   mycroft  * take a block out of the map
    227        1.1   mycroft  */
    228        1.1   mycroft void
    229       1.34   thorpej ffs_clrblock(struct fs *fs, u_char *cp, int32_t h)
    230        1.1   mycroft {
    231        1.1   mycroft 
    232       1.22   mycroft 	switch ((int)fs->fs_fragshift) {
    233       1.22   mycroft 	case 3:
    234        1.1   mycroft 		cp[h] = 0;
    235        1.1   mycroft 		return;
    236       1.22   mycroft 	case 2:
    237        1.1   mycroft 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
    238        1.1   mycroft 		return;
    239       1.22   mycroft 	case 1:
    240        1.1   mycroft 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
    241        1.1   mycroft 		return;
    242       1.22   mycroft 	case 0:
    243        1.1   mycroft 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
    244        1.1   mycroft 		return;
    245        1.1   mycroft 	default:
    246       1.22   mycroft 		panic("ffs_clrblock: unknown fs_fragshift %d",
    247       1.22   mycroft 		    (int)fs->fs_fragshift);
    248        1.1   mycroft 	}
    249        1.1   mycroft }
    250        1.1   mycroft 
    251        1.1   mycroft /*
    252        1.1   mycroft  * put a block into the map
    253        1.1   mycroft  */
    254        1.1   mycroft void
    255       1.34   thorpej ffs_setblock(struct fs *fs, u_char *cp, int32_t h)
    256        1.1   mycroft {
    257        1.1   mycroft 
    258       1.22   mycroft 	switch ((int)fs->fs_fragshift) {
    259       1.22   mycroft 	case 3:
    260        1.1   mycroft 		cp[h] = 0xff;
    261        1.1   mycroft 		return;
    262       1.22   mycroft 	case 2:
    263        1.1   mycroft 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
    264        1.1   mycroft 		return;
    265       1.22   mycroft 	case 1:
    266        1.1   mycroft 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
    267        1.1   mycroft 		return;
    268       1.22   mycroft 	case 0:
    269        1.1   mycroft 		cp[h >> 3] |= (0x01 << (h & 0x7));
    270        1.1   mycroft 		return;
    271        1.1   mycroft 	default:
    272       1.22   mycroft 		panic("ffs_setblock: unknown fs_fragshift %d",
    273       1.22   mycroft 		    (int)fs->fs_fragshift);
    274        1.1   mycroft 	}
    275        1.1   mycroft }
    276  1.45.20.1     rmind 
    277  1.45.20.1     rmind /*
    278  1.45.20.1     rmind  * Update the cluster map because of an allocation or free.
    279  1.45.20.1     rmind  *
    280  1.45.20.1     rmind  * Cnt == 1 means free; cnt == -1 means allocating.
    281  1.45.20.1     rmind  */
    282  1.45.20.1     rmind void
    283  1.45.20.1     rmind ffs_clusteracct(struct fs *fs, struct cg *cgp, int32_t blkno, int cnt)
    284  1.45.20.1     rmind {
    285  1.45.20.1     rmind 	int32_t *sump;
    286  1.45.20.1     rmind 	int32_t *lp;
    287  1.45.20.1     rmind 	u_char *freemapp, *mapp;
    288  1.45.20.1     rmind 	int i, start, end, forw, back, map, bit;
    289  1.45.20.1     rmind #ifdef FFS_EI
    290  1.45.20.1     rmind 	const int needswap = UFS_FSNEEDSWAP(fs);
    291  1.45.20.1     rmind #endif
    292  1.45.20.1     rmind 
    293  1.45.20.1     rmind 	/* KASSERT(mutex_owned(&ump->um_lock)); */
    294  1.45.20.1     rmind 
    295  1.45.20.1     rmind 	if (fs->fs_contigsumsize <= 0)
    296  1.45.20.1     rmind 		return;
    297  1.45.20.1     rmind 	freemapp = cg_clustersfree(cgp, needswap);
    298  1.45.20.1     rmind 	sump = cg_clustersum(cgp, needswap);
    299  1.45.20.1     rmind 	/*
    300  1.45.20.1     rmind 	 * Allocate or clear the actual block.
    301  1.45.20.1     rmind 	 */
    302  1.45.20.1     rmind 	if (cnt > 0)
    303  1.45.20.1     rmind 		setbit(freemapp, blkno);
    304  1.45.20.1     rmind 	else
    305  1.45.20.1     rmind 		clrbit(freemapp, blkno);
    306  1.45.20.1     rmind 	/*
    307  1.45.20.1     rmind 	 * Find the size of the cluster going forward.
    308  1.45.20.1     rmind 	 */
    309  1.45.20.1     rmind 	start = blkno + 1;
    310  1.45.20.1     rmind 	end = start + fs->fs_contigsumsize;
    311  1.45.20.1     rmind 	if (end >= ufs_rw32(cgp->cg_nclusterblks, needswap))
    312  1.45.20.1     rmind 		end = ufs_rw32(cgp->cg_nclusterblks, needswap);
    313  1.45.20.1     rmind 	mapp = &freemapp[start / NBBY];
    314  1.45.20.1     rmind 	map = *mapp++;
    315  1.45.20.1     rmind 	bit = 1 << (start % NBBY);
    316  1.45.20.1     rmind 	for (i = start; i < end; i++) {
    317  1.45.20.1     rmind 		if ((map & bit) == 0)
    318  1.45.20.1     rmind 			break;
    319  1.45.20.1     rmind 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
    320  1.45.20.1     rmind 			bit <<= 1;
    321  1.45.20.1     rmind 		} else {
    322  1.45.20.1     rmind 			map = *mapp++;
    323  1.45.20.1     rmind 			bit = 1;
    324  1.45.20.1     rmind 		}
    325  1.45.20.1     rmind 	}
    326  1.45.20.1     rmind 	forw = i - start;
    327  1.45.20.1     rmind 	/*
    328  1.45.20.1     rmind 	 * Find the size of the cluster going backward.
    329  1.45.20.1     rmind 	 */
    330  1.45.20.1     rmind 	start = blkno - 1;
    331  1.45.20.1     rmind 	end = start - fs->fs_contigsumsize;
    332  1.45.20.1     rmind 	if (end < 0)
    333  1.45.20.1     rmind 		end = -1;
    334  1.45.20.1     rmind 	mapp = &freemapp[start / NBBY];
    335  1.45.20.1     rmind 	map = *mapp--;
    336  1.45.20.1     rmind 	bit = 1 << (start % NBBY);
    337  1.45.20.1     rmind 	for (i = start; i > end; i--) {
    338  1.45.20.1     rmind 		if ((map & bit) == 0)
    339  1.45.20.1     rmind 			break;
    340  1.45.20.1     rmind 		if ((i & (NBBY - 1)) != 0) {
    341  1.45.20.1     rmind 			bit >>= 1;
    342  1.45.20.1     rmind 		} else {
    343  1.45.20.1     rmind 			map = *mapp--;
    344  1.45.20.1     rmind 			bit = 1 << (NBBY - 1);
    345  1.45.20.1     rmind 		}
    346  1.45.20.1     rmind 	}
    347  1.45.20.1     rmind 	back = start - i;
    348  1.45.20.1     rmind 	/*
    349  1.45.20.1     rmind 	 * Account for old cluster and the possibly new forward and
    350  1.45.20.1     rmind 	 * back clusters.
    351  1.45.20.1     rmind 	 */
    352  1.45.20.1     rmind 	i = back + forw + 1;
    353  1.45.20.1     rmind 	if (i > fs->fs_contigsumsize)
    354  1.45.20.1     rmind 		i = fs->fs_contigsumsize;
    355  1.45.20.1     rmind 	ufs_add32(sump[i], cnt, needswap);
    356  1.45.20.1     rmind 	if (back > 0)
    357  1.45.20.1     rmind 		ufs_add32(sump[back], -cnt, needswap);
    358  1.45.20.1     rmind 	if (forw > 0)
    359  1.45.20.1     rmind 		ufs_add32(sump[forw], -cnt, needswap);
    360  1.45.20.1     rmind 
    361  1.45.20.1     rmind 	/*
    362  1.45.20.1     rmind 	 * Update cluster summary information.
    363  1.45.20.1     rmind 	 */
    364  1.45.20.1     rmind 	lp = &sump[fs->fs_contigsumsize];
    365  1.45.20.1     rmind 	for (i = fs->fs_contigsumsize; i > 0; i--)
    366  1.45.20.1     rmind 		if (ufs_rw32(*lp--, needswap) > 0)
    367  1.45.20.1     rmind 			break;
    368  1.45.20.1     rmind #if defined(_KERNEL)
    369  1.45.20.1     rmind 	fs->fs_maxcluster[ufs_rw32(cgp->cg_cgx, needswap)] = i;
    370  1.45.20.1     rmind #endif
    371  1.45.20.1     rmind }
    372