Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_alloc.c revision 1.24.2.2
      1  1.24.2.2      yamt /*	$NetBSD: ext2fs_alloc.c,v 1.24.2.2 2006/12/30 20:51:00 yamt Exp $	*/
      2       1.1    bouyer 
      3       1.1    bouyer /*
      4       1.1    bouyer  * Copyright (c) 1982, 1986, 1989, 1993
      5       1.1    bouyer  *	The Regents of the University of California.  All rights reserved.
      6       1.1    bouyer  *
      7       1.1    bouyer  * Redistribution and use in source and binary forms, with or without
      8       1.1    bouyer  * modification, are permitted provided that the following conditions
      9       1.1    bouyer  * are met:
     10       1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     11       1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     12       1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     14       1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     15      1.20       agc  * 3. Neither the name of the University nor the names of its contributors
     16      1.20       agc  *    may be used to endorse or promote products derived from this software
     17      1.20       agc  *    without specific prior written permission.
     18      1.20       agc  *
     19      1.20       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20      1.20       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21      1.20       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22      1.20       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23      1.20       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24      1.20       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25      1.20       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26      1.20       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27      1.20       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28      1.20       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29      1.20       agc  * SUCH DAMAGE.
     30      1.20       agc  *
     31      1.20       agc  *	@(#)ffs_alloc.c	8.11 (Berkeley) 10/27/94
     32      1.20       agc  *  Modified for ext2fs by Manuel Bouyer.
     33      1.20       agc  */
     34      1.20       agc 
     35      1.20       agc /*
     36      1.20       agc  * Copyright (c) 1997 Manuel Bouyer.
     37      1.20       agc  *
     38      1.20       agc  * Redistribution and use in source and binary forms, with or without
     39      1.20       agc  * modification, are permitted provided that the following conditions
     40      1.20       agc  * are met:
     41      1.20       agc  * 1. Redistributions of source code must retain the above copyright
     42      1.20       agc  *    notice, this list of conditions and the following disclaimer.
     43      1.20       agc  * 2. Redistributions in binary form must reproduce the above copyright
     44      1.20       agc  *    notice, this list of conditions and the following disclaimer in the
     45      1.20       agc  *    documentation and/or other materials provided with the distribution.
     46       1.1    bouyer  * 3. All advertising materials mentioning features or use of this software
     47       1.1    bouyer  *    must display the following acknowledgement:
     48      1.21    bouyer  *	This product includes software developed by Manuel Bouyer.
     49      1.21    bouyer  * 4. The name of the author may not be used to endorse or promote products
     50      1.21    bouyer  *    derived from this software without specific prior written permission.
     51       1.1    bouyer  *
     52      1.22    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     53      1.22    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     54      1.22    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     55      1.22    bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     56      1.22    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     57      1.22    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     58      1.22    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     59      1.22    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     60      1.22    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     61      1.22    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     62       1.1    bouyer  *
     63       1.1    bouyer  *	@(#)ffs_alloc.c	8.11 (Berkeley) 10/27/94
     64       1.1    bouyer  *  Modified for ext2fs by Manuel Bouyer.
     65       1.1    bouyer  */
     66      1.13     lukem 
     67      1.13     lukem #include <sys/cdefs.h>
     68  1.24.2.2      yamt __KERNEL_RCSID(0, "$NetBSD: ext2fs_alloc.c,v 1.24.2.2 2006/12/30 20:51:00 yamt Exp $");
     69       1.1    bouyer 
     70       1.1    bouyer #include <sys/param.h>
     71       1.1    bouyer #include <sys/systm.h>
     72       1.1    bouyer #include <sys/buf.h>
     73       1.1    bouyer #include <sys/proc.h>
     74       1.1    bouyer #include <sys/vnode.h>
     75       1.1    bouyer #include <sys/mount.h>
     76       1.1    bouyer #include <sys/kernel.h>
     77       1.1    bouyer #include <sys/syslog.h>
     78  1.24.2.1      yamt #include <sys/kauth.h>
     79       1.1    bouyer 
     80       1.1    bouyer #include <ufs/ufs/inode.h>
     81       1.1    bouyer #include <ufs/ufs/ufs_extern.h>
     82  1.24.2.1      yamt #include <ufs/ufs/ufsmount.h>
     83       1.1    bouyer 
     84       1.1    bouyer #include <ufs/ext2fs/ext2fs.h>
     85       1.1    bouyer #include <ufs/ext2fs/ext2fs_extern.h>
     86       1.1    bouyer 
     87       1.1    bouyer u_long ext2gennumber;
     88       1.1    bouyer 
     89  1.24.2.1      yamt static daddr_t	ext2fs_alloccg(struct inode *, int, daddr_t, int);
     90  1.24.2.1      yamt static u_long	ext2fs_dirpref(struct m_ext2fs *);
     91  1.24.2.1      yamt static void	ext2fs_fserr(struct m_ext2fs *, u_int, const char *);
     92  1.24.2.1      yamt static u_long	ext2fs_hashalloc(struct inode *, int, long, int,
     93      1.15      fvdl 				   daddr_t (*)(struct inode *, int, daddr_t,
     94  1.24.2.1      yamt 						   int));
     95  1.24.2.1      yamt static daddr_t	ext2fs_nodealloccg(struct inode *, int, daddr_t, int);
     96  1.24.2.1      yamt static daddr_t	ext2fs_mapsearch(struct m_ext2fs *, char *, daddr_t);
     97       1.1    bouyer 
     98       1.1    bouyer /*
     99       1.1    bouyer  * Allocate a block in the file system.
    100      1.23     perry  *
    101       1.1    bouyer  * A preference may be optionally specified. If a preference is given
    102       1.1    bouyer  * the following hierarchy is used to allocate a block:
    103       1.1    bouyer  *   1) allocate the requested block.
    104       1.1    bouyer  *   2) allocate a rotationally optimal block in the same cylinder.
    105       1.1    bouyer  *   3) allocate a block in the same cylinder group.
    106       1.1    bouyer  *   4) quadradically rehash into other cylinder groups, until an
    107       1.1    bouyer  *	  available block is located.
    108      1.11       wiz  * If no block preference is given the following hierarchy is used
    109       1.1    bouyer  * to allocate a block:
    110       1.1    bouyer  *   1) allocate a block in the cylinder group that contains the
    111       1.1    bouyer  *	  inode for the file.
    112       1.1    bouyer  *   2) quadradically rehash into other cylinder groups, until an
    113       1.1    bouyer  *	  available block is located.
    114       1.1    bouyer  */
    115       1.1    bouyer int
    116  1.24.2.2      yamt ext2fs_alloc(struct inode *ip, daddr_t lbn, daddr_t bpref,
    117  1.24.2.2      yamt     kauth_cred_t cred, daddr_t *bnp)
    118       1.1    bouyer {
    119       1.7  augustss 	struct m_ext2fs *fs;
    120      1.15      fvdl 	daddr_t bno;
    121       1.1    bouyer 	int cg;
    122      1.23     perry 
    123       1.1    bouyer 	*bnp = 0;
    124       1.1    bouyer 	fs = ip->i_e2fs;
    125       1.1    bouyer #ifdef DIAGNOSTIC
    126       1.1    bouyer 	if (cred == NOCRED)
    127      1.14    provos 		panic("ext2fs_alloc: missing credential");
    128       1.1    bouyer #endif /* DIAGNOSTIC */
    129       1.1    bouyer 	if (fs->e2fs.e2fs_fbcount == 0)
    130       1.1    bouyer 		goto nospace;
    131  1.24.2.1      yamt 	if (kauth_cred_geteuid(cred) != 0 && freespace(fs) <= 0)
    132       1.1    bouyer 		goto nospace;
    133       1.1    bouyer 	if (bpref >= fs->e2fs.e2fs_bcount)
    134       1.1    bouyer 		bpref = 0;
    135       1.1    bouyer 	if (bpref == 0)
    136       1.1    bouyer 		cg = ino_to_cg(fs, ip->i_number);
    137       1.1    bouyer 	else
    138       1.1    bouyer 		cg = dtog(fs, bpref);
    139      1.15      fvdl 	bno = (daddr_t)ext2fs_hashalloc(ip, cg, bpref, fs->e2fs_bsize,
    140       1.1    bouyer 						 ext2fs_alloccg);
    141       1.1    bouyer 	if (bno > 0) {
    142       1.1    bouyer 		ip->i_e2fs_nblock += btodb(fs->e2fs_bsize);
    143       1.1    bouyer 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    144       1.1    bouyer 		*bnp = bno;
    145       1.1    bouyer 		return (0);
    146       1.1    bouyer 	}
    147       1.1    bouyer nospace:
    148  1.24.2.1      yamt 	ext2fs_fserr(fs, kauth_cred_geteuid(cred), "file system full");
    149       1.1    bouyer 	uprintf("\n%s: write failed, file system is full\n", fs->e2fs_fsmnt);
    150       1.1    bouyer 	return (ENOSPC);
    151       1.1    bouyer }
    152       1.1    bouyer 
    153       1.1    bouyer /*
    154       1.1    bouyer  * Allocate an inode in the file system.
    155      1.23     perry  *
    156       1.1    bouyer  * If allocating a directory, use ext2fs_dirpref to select the inode.
    157       1.1    bouyer  * If allocating in a directory, the following hierarchy is followed:
    158       1.1    bouyer  *   1) allocate the preferred inode.
    159       1.1    bouyer  *   2) allocate an inode in the same cylinder group.
    160       1.1    bouyer  *   3) quadradically rehash into other cylinder groups, until an
    161       1.1    bouyer  *	  available inode is located.
    162      1.11       wiz  * If no inode preference is given the following hierarchy is used
    163       1.1    bouyer  * to allocate an inode:
    164       1.1    bouyer  *   1) allocate an inode in cylinder group 0.
    165       1.1    bouyer  *   2) quadradically rehash into other cylinder groups, until an
    166       1.1    bouyer  *	  available inode is located.
    167       1.1    bouyer  */
    168       1.1    bouyer int
    169  1.24.2.1      yamt ext2fs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred,
    170  1.24.2.1      yamt     struct vnode **vpp)
    171       1.1    bouyer {
    172       1.7  augustss 	struct inode *pip;
    173       1.7  augustss 	struct m_ext2fs *fs;
    174       1.7  augustss 	struct inode *ip;
    175       1.1    bouyer 	ino_t ino, ipref;
    176       1.1    bouyer 	int cg, error;
    177      1.23     perry 
    178  1.24.2.1      yamt 	*vpp = NULL;
    179       1.1    bouyer 	pip = VTOI(pvp);
    180       1.1    bouyer 	fs = pip->i_e2fs;
    181       1.1    bouyer 	if (fs->e2fs.e2fs_ficount == 0)
    182       1.1    bouyer 		goto noinodes;
    183       1.1    bouyer 
    184       1.1    bouyer 	if ((mode & IFMT) == IFDIR)
    185       1.1    bouyer 		cg = ext2fs_dirpref(fs);
    186       1.1    bouyer 	else
    187       1.1    bouyer 		cg = ino_to_cg(fs, pip->i_number);
    188       1.1    bouyer 	ipref = cg * fs->e2fs.e2fs_ipg + 1;
    189       1.1    bouyer 	ino = (ino_t)ext2fs_hashalloc(pip, cg, (long)ipref, mode, ext2fs_nodealloccg);
    190       1.1    bouyer 	if (ino == 0)
    191       1.1    bouyer 		goto noinodes;
    192  1.24.2.1      yamt 	error = VFS_VGET(pvp->v_mount, ino, vpp);
    193       1.1    bouyer 	if (error) {
    194  1.24.2.1      yamt 		ext2fs_vfree(pvp, ino, mode);
    195       1.1    bouyer 		return (error);
    196       1.1    bouyer 	}
    197  1.24.2.1      yamt 	ip = VTOI(*vpp);
    198       1.1    bouyer 	if (ip->i_e2fs_mode && ip->i_e2fs_nlink != 0) {
    199  1.24.2.1      yamt 		printf("mode = 0%o, nlinks %d, inum = %llu, fs = %s\n",
    200  1.24.2.1      yamt 		    ip->i_e2fs_mode, ip->i_e2fs_nlink,
    201  1.24.2.1      yamt 		    (unsigned long long)ip->i_number, fs->e2fs_fsmnt);
    202       1.1    bouyer 		panic("ext2fs_valloc: dup alloc");
    203       1.1    bouyer 	}
    204       1.1    bouyer 
    205      1.16  gmcgarry 	memset(ip->i_din.e2fs_din, 0, sizeof(struct ext2fs_dinode));
    206       1.1    bouyer 
    207       1.1    bouyer 	/*
    208       1.1    bouyer 	 * Set up a new generation number for this inode.
    209       1.1    bouyer 	 */
    210  1.24.2.1      yamt 	if (++ext2gennumber < time_second)
    211  1.24.2.1      yamt 		ext2gennumber = time_second;
    212       1.1    bouyer 	ip->i_e2fs_gen = ext2gennumber;
    213       1.1    bouyer 	return (0);
    214       1.1    bouyer noinodes:
    215  1.24.2.1      yamt 	ext2fs_fserr(fs, kauth_cred_geteuid(cred), "out of inodes");
    216       1.1    bouyer 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->e2fs_fsmnt);
    217       1.1    bouyer 	return (ENOSPC);
    218       1.1    bouyer }
    219       1.1    bouyer 
    220       1.1    bouyer /*
    221       1.1    bouyer  * Find a cylinder to place a directory.
    222       1.1    bouyer  *
    223       1.1    bouyer  * The policy implemented by this algorithm is to select from
    224       1.1    bouyer  * among those cylinder groups with above the average number of
    225       1.1    bouyer  * free inodes, the one with the smallest number of directories.
    226       1.1    bouyer  */
    227       1.1    bouyer static u_long
    228  1.24.2.1      yamt ext2fs_dirpref(struct m_ext2fs *fs)
    229       1.1    bouyer {
    230       1.1    bouyer 	int cg, maxspace, mincg, avgifree;
    231       1.1    bouyer 
    232       1.1    bouyer 	avgifree = fs->e2fs.e2fs_ficount / fs->e2fs_ncg;
    233       1.1    bouyer 	maxspace = 0;
    234       1.1    bouyer 	mincg = -1;
    235       1.1    bouyer 	for (cg = 0; cg < fs->e2fs_ncg; cg++)
    236       1.1    bouyer 		if ( fs->e2fs_gd[cg].ext2bgd_nifree >= avgifree) {
    237       1.1    bouyer 			if (mincg == -1 || fs->e2fs_gd[cg].ext2bgd_nbfree > maxspace) {
    238       1.1    bouyer 				mincg = cg;
    239       1.1    bouyer 				maxspace = fs->e2fs_gd[cg].ext2bgd_nbfree;
    240       1.1    bouyer 			}
    241       1.1    bouyer 		}
    242       1.1    bouyer 	return mincg;
    243       1.1    bouyer }
    244       1.1    bouyer 
    245       1.1    bouyer /*
    246       1.1    bouyer  * Select the desired position for the next block in a file.  The file is
    247       1.1    bouyer  * logically divided into sections. The first section is composed of the
    248       1.1    bouyer  * direct blocks. Each additional section contains fs_maxbpg blocks.
    249      1.23     perry  *
    250       1.1    bouyer  * If no blocks have been allocated in the first section, the policy is to
    251       1.1    bouyer  * request a block in the same cylinder group as the inode that describes
    252       1.1    bouyer  * the file. Otherwise, the policy is to try to allocate the blocks
    253      1.10    toshii  * contigously. The two fields of the ext2 inode extension (see
    254       1.1    bouyer  * ufs/ufs/inode.h) help this.
    255       1.1    bouyer  */
    256      1.15      fvdl daddr_t
    257  1.24.2.1      yamt ext2fs_blkpref(struct inode *ip, daddr_t lbn, int indx,
    258  1.24.2.1      yamt 		int32_t *bap /* XXX ondisk32 */)
    259       1.1    bouyer {
    260       1.7  augustss 	struct m_ext2fs *fs;
    261       1.7  augustss 	int cg, i;
    262       1.1    bouyer 
    263       1.1    bouyer 	fs = ip->i_e2fs;
    264       1.1    bouyer 	/*
    265       1.1    bouyer 	 * if we are doing contigous lbn allocation, try to alloc blocks
    266       1.1    bouyer 	 * contigously on disk
    267       1.1    bouyer 	 */
    268       1.1    bouyer 
    269       1.1    bouyer 	if ( ip->i_e2fs_last_blk && lbn == ip->i_e2fs_last_lblk + 1) {
    270       1.1    bouyer 		return ip->i_e2fs_last_blk + 1;
    271       1.1    bouyer 	}
    272       1.1    bouyer 
    273       1.1    bouyer 	/*
    274       1.1    bouyer 	 * bap, if provided, gives us a list of blocks to which we want to
    275       1.1    bouyer 	 * stay close
    276       1.1    bouyer 	 */
    277       1.1    bouyer 
    278       1.1    bouyer 	if (bap) {
    279       1.1    bouyer 		for (i = indx; i >= 0 ; i--) {
    280       1.1    bouyer 			if (bap[i]) {
    281       1.2    bouyer 				return fs2h32(bap[i]) + 1;
    282       1.1    bouyer 			}
    283       1.1    bouyer 		}
    284       1.1    bouyer 	}
    285       1.1    bouyer 
    286       1.1    bouyer 	/* fall back to the first block of the cylinder containing the inode */
    287       1.1    bouyer 
    288       1.1    bouyer 	cg = ino_to_cg(fs, ip->i_number);
    289       1.1    bouyer 	return fs->e2fs.e2fs_bpg * cg + fs->e2fs.e2fs_first_dblock + 1;
    290       1.1    bouyer }
    291       1.1    bouyer 
    292       1.1    bouyer /*
    293       1.1    bouyer  * Implement the cylinder overflow algorithm.
    294       1.1    bouyer  *
    295       1.1    bouyer  * The policy implemented by this algorithm is:
    296       1.1    bouyer  *   1) allocate the block in its requested cylinder group.
    297       1.1    bouyer  *   2) quadradically rehash on the cylinder group number.
    298       1.1    bouyer  *   3) brute force search for a free block.
    299       1.1    bouyer  */
    300       1.1    bouyer static u_long
    301  1.24.2.1      yamt ext2fs_hashalloc(struct inode *ip, int cg, long pref, int size,
    302  1.24.2.1      yamt 		daddr_t (*allocator)(struct inode *, int, daddr_t, int))
    303       1.1    bouyer {
    304       1.7  augustss 	struct m_ext2fs *fs;
    305       1.1    bouyer 	long result;
    306       1.1    bouyer 	int i, icg = cg;
    307       1.1    bouyer 
    308       1.1    bouyer 	fs = ip->i_e2fs;
    309       1.1    bouyer 	/*
    310       1.1    bouyer 	 * 1: preferred cylinder group
    311       1.1    bouyer 	 */
    312       1.1    bouyer 	result = (*allocator)(ip, cg, pref, size);
    313       1.1    bouyer 	if (result)
    314       1.1    bouyer 		return (result);
    315       1.1    bouyer 	/*
    316       1.1    bouyer 	 * 2: quadratic rehash
    317       1.1    bouyer 	 */
    318       1.1    bouyer 	for (i = 1; i < fs->e2fs_ncg; i *= 2) {
    319       1.1    bouyer 		cg += i;
    320       1.1    bouyer 		if (cg >= fs->e2fs_ncg)
    321       1.1    bouyer 			cg -= fs->e2fs_ncg;
    322       1.1    bouyer 		result = (*allocator)(ip, cg, 0, size);
    323       1.1    bouyer 		if (result)
    324       1.1    bouyer 			return (result);
    325       1.1    bouyer 	}
    326       1.1    bouyer 	/*
    327       1.1    bouyer 	 * 3: brute force search
    328       1.1    bouyer 	 * Note that we start at i == 2, since 0 was checked initially,
    329       1.1    bouyer 	 * and 1 is always checked in the quadratic rehash.
    330       1.1    bouyer 	 */
    331       1.1    bouyer 	cg = (icg + 2) % fs->e2fs_ncg;
    332       1.1    bouyer 	for (i = 2; i < fs->e2fs_ncg; i++) {
    333       1.1    bouyer 		result = (*allocator)(ip, cg, 0, size);
    334       1.1    bouyer 		if (result)
    335       1.1    bouyer 			return (result);
    336       1.1    bouyer 		cg++;
    337       1.1    bouyer 		if (cg == fs->e2fs_ncg)
    338       1.1    bouyer 			cg = 0;
    339       1.1    bouyer 	}
    340       1.8   thorpej 	return (0);
    341       1.1    bouyer }
    342       1.1    bouyer 
    343       1.1    bouyer /*
    344       1.1    bouyer  * Determine whether a block can be allocated.
    345       1.1    bouyer  *
    346       1.1    bouyer  * Check to see if a block of the appropriate size is available,
    347       1.1    bouyer  * and if it is, allocate it.
    348       1.1    bouyer  */
    349       1.1    bouyer 
    350      1.15      fvdl static daddr_t
    351  1.24.2.1      yamt ext2fs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size)
    352       1.1    bouyer {
    353       1.7  augustss 	struct m_ext2fs *fs;
    354       1.7  augustss 	char *bbp;
    355       1.1    bouyer 	struct buf *bp;
    356      1.15      fvdl 	/* XXX ondisk32 */
    357       1.1    bouyer 	int error, bno, start, end, loc;
    358       1.1    bouyer 
    359       1.1    bouyer 	fs = ip->i_e2fs;
    360       1.1    bouyer 	if (fs->e2fs_gd[cg].ext2bgd_nbfree == 0)
    361       1.8   thorpej 		return (0);
    362       1.6    bouyer 	error = bread(ip->i_devvp, fsbtodb(fs,
    363       1.6    bouyer 		fs->e2fs_gd[cg].ext2bgd_b_bitmap),
    364       1.1    bouyer 		(int)fs->e2fs_bsize, NOCRED, &bp);
    365       1.1    bouyer 	if (error) {
    366       1.1    bouyer 		brelse(bp);
    367       1.8   thorpej 		return (0);
    368       1.1    bouyer 	}
    369       1.1    bouyer 	bbp = (char *)bp->b_data;
    370       1.1    bouyer 
    371       1.1    bouyer 	if (dtog(fs, bpref) != cg)
    372       1.1    bouyer 		bpref = 0;
    373       1.1    bouyer 	if (bpref != 0) {
    374       1.1    bouyer 		bpref = dtogd(fs, bpref);
    375       1.1    bouyer 		/*
    376       1.1    bouyer 		 * if the requested block is available, use it
    377       1.1    bouyer 		 */
    378       1.1    bouyer 		if (isclr(bbp, bpref)) {
    379       1.1    bouyer 			bno = bpref;
    380       1.1    bouyer 			goto gotit;
    381       1.1    bouyer 		}
    382       1.1    bouyer 	}
    383       1.1    bouyer 	/*
    384       1.1    bouyer 	 * no blocks in the requested cylinder, so take next
    385       1.1    bouyer 	 * available one in this cylinder group.
    386       1.1    bouyer 	 * first try to get 8 contigous blocks, then fall back to a single
    387       1.1    bouyer 	 * block.
    388       1.1    bouyer 	 */
    389       1.1    bouyer 	if (bpref)
    390       1.1    bouyer 		start = dtogd(fs, bpref) / NBBY;
    391       1.1    bouyer 	else
    392       1.1    bouyer 		start = 0;
    393       1.1    bouyer 	end = howmany(fs->e2fs.e2fs_fpg, NBBY) - start;
    394       1.1    bouyer 	for (loc = start; loc < end; loc++) {
    395       1.1    bouyer 		if (bbp[loc] == 0) {
    396       1.1    bouyer 			bno = loc * NBBY;
    397       1.1    bouyer 			goto gotit;
    398       1.1    bouyer 		}
    399       1.1    bouyer 	}
    400       1.1    bouyer 	for (loc = 0; loc < start; loc++) {
    401       1.1    bouyer 		if (bbp[loc] == 0) {
    402       1.1    bouyer 			bno = loc * NBBY;
    403       1.1    bouyer 			goto gotit;
    404       1.1    bouyer 		}
    405       1.1    bouyer 	}
    406       1.1    bouyer 
    407       1.1    bouyer 	bno = ext2fs_mapsearch(fs, bbp, bpref);
    408       1.1    bouyer 	if (bno < 0)
    409       1.8   thorpej 		return (0);
    410       1.1    bouyer gotit:
    411       1.1    bouyer #ifdef DIAGNOSTIC
    412      1.15      fvdl 	if (isset(bbp, (daddr_t)bno)) {
    413       1.1    bouyer 		printf("ext2fs_alloccgblk: cg=%d bno=%d fs=%s\n",
    414       1.1    bouyer 			cg, bno, fs->e2fs_fsmnt);
    415       1.2    bouyer 		panic("ext2fs_alloccg: dup alloc");
    416       1.1    bouyer 	}
    417       1.1    bouyer #endif
    418      1.15      fvdl 	setbit(bbp, (daddr_t)bno);
    419       1.1    bouyer 	fs->e2fs.e2fs_fbcount--;
    420       1.1    bouyer 	fs->e2fs_gd[cg].ext2bgd_nbfree--;
    421       1.1    bouyer 	fs->e2fs_fmod = 1;
    422       1.1    bouyer 	bdwrite(bp);
    423       1.1    bouyer 	return (cg * fs->e2fs.e2fs_fpg + fs->e2fs.e2fs_first_dblock + bno);
    424       1.1    bouyer }
    425       1.1    bouyer 
    426       1.1    bouyer /*
    427       1.1    bouyer  * Determine whether an inode can be allocated.
    428       1.1    bouyer  *
    429       1.1    bouyer  * Check to see if an inode is available, and if it is,
    430       1.1    bouyer  * allocate it using the following policy:
    431       1.1    bouyer  *   1) allocate the requested inode.
    432       1.1    bouyer  *   2) allocate the next available inode after the requested
    433       1.1    bouyer  *	  inode in the specified cylinder group.
    434       1.1    bouyer  */
    435      1.15      fvdl static daddr_t
    436  1.24.2.1      yamt ext2fs_nodealloccg(struct inode *ip, int cg, daddr_t ipref, int mode)
    437       1.1    bouyer {
    438       1.7  augustss 	struct m_ext2fs *fs;
    439       1.7  augustss 	char *ibp;
    440       1.1    bouyer 	struct buf *bp;
    441       1.1    bouyer 	int error, start, len, loc, map, i;
    442       1.1    bouyer 
    443       1.1    bouyer 	ipref--; /* to avoid a lot of (ipref -1) */
    444  1.24.2.2      yamt 	if (ipref == -1)
    445  1.24.2.2      yamt 		ipref = 0;
    446       1.1    bouyer 	fs = ip->i_e2fs;
    447       1.1    bouyer 	if (fs->e2fs_gd[cg].ext2bgd_nifree == 0)
    448       1.8   thorpej 		return (0);
    449       1.6    bouyer 	error = bread(ip->i_devvp, fsbtodb(fs,
    450       1.6    bouyer 		fs->e2fs_gd[cg].ext2bgd_i_bitmap),
    451       1.1    bouyer 		(int)fs->e2fs_bsize, NOCRED, &bp);
    452       1.1    bouyer 	if (error) {
    453       1.1    bouyer 		brelse(bp);
    454       1.8   thorpej 		return (0);
    455       1.1    bouyer 	}
    456       1.1    bouyer 	ibp = (char *)bp->b_data;
    457       1.1    bouyer 	if (ipref) {
    458       1.1    bouyer 		ipref %= fs->e2fs.e2fs_ipg;
    459       1.1    bouyer 		if (isclr(ibp, ipref))
    460       1.1    bouyer 			goto gotit;
    461       1.1    bouyer 	}
    462       1.1    bouyer 	start = ipref / NBBY;
    463       1.1    bouyer 	len = howmany(fs->e2fs.e2fs_ipg - ipref, NBBY);
    464       1.1    bouyer 	loc = skpc(0xff, len, &ibp[start]);
    465       1.1    bouyer 	if (loc == 0) {
    466       1.1    bouyer 		len = start + 1;
    467       1.1    bouyer 		start = 0;
    468       1.1    bouyer 		loc = skpc(0xff, len, &ibp[0]);
    469       1.1    bouyer 		if (loc == 0) {
    470      1.15      fvdl 			printf("cg = %d, ipref = %lld, fs = %s\n",
    471      1.15      fvdl 				cg, (long long)ipref, fs->e2fs_fsmnt);
    472       1.1    bouyer 			panic("ext2fs_nodealloccg: map corrupted");
    473       1.1    bouyer 			/* NOTREACHED */
    474       1.1    bouyer 		}
    475       1.1    bouyer 	}
    476       1.1    bouyer 	i = start + len - loc;
    477       1.1    bouyer 	map = ibp[i];
    478       1.1    bouyer 	ipref = i * NBBY;
    479       1.1    bouyer 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
    480       1.1    bouyer 		if ((map & i) == 0) {
    481       1.1    bouyer 			goto gotit;
    482       1.1    bouyer 		}
    483       1.1    bouyer 	}
    484       1.1    bouyer 	printf("fs = %s\n", fs->e2fs_fsmnt);
    485       1.1    bouyer 	panic("ext2fs_nodealloccg: block not in map");
    486       1.1    bouyer 	/* NOTREACHED */
    487       1.1    bouyer gotit:
    488       1.1    bouyer 	setbit(ibp, ipref);
    489       1.1    bouyer 	fs->e2fs.e2fs_ficount--;
    490       1.1    bouyer 	fs->e2fs_gd[cg].ext2bgd_nifree--;
    491       1.1    bouyer 	fs->e2fs_fmod = 1;
    492       1.1    bouyer 	if ((mode & IFMT) == IFDIR) {
    493       1.1    bouyer 		fs->e2fs_gd[cg].ext2bgd_ndirs++;
    494       1.1    bouyer 	}
    495       1.1    bouyer 	bdwrite(bp);
    496       1.1    bouyer 	return (cg * fs->e2fs.e2fs_ipg + ipref +1);
    497       1.1    bouyer }
    498       1.1    bouyer 
    499       1.1    bouyer /*
    500       1.1    bouyer  * Free a block.
    501       1.1    bouyer  *
    502       1.1    bouyer  * The specified block is placed back in the
    503       1.1    bouyer  * free map.
    504       1.1    bouyer  */
    505       1.1    bouyer void
    506  1.24.2.1      yamt ext2fs_blkfree(struct inode *ip, daddr_t bno)
    507       1.1    bouyer {
    508       1.7  augustss 	struct m_ext2fs *fs;
    509       1.7  augustss 	char *bbp;
    510       1.1    bouyer 	struct buf *bp;
    511       1.1    bouyer 	int error, cg;
    512       1.1    bouyer 
    513       1.1    bouyer 	fs = ip->i_e2fs;
    514       1.1    bouyer 	cg = dtog(fs, bno);
    515       1.1    bouyer 	if ((u_int)bno >= fs->e2fs.e2fs_bcount) {
    516  1.24.2.1      yamt 		printf("bad block %lld, ino %llu\n", (long long)bno,
    517  1.24.2.1      yamt 		    (unsigned long long)ip->i_number);
    518       1.1    bouyer 		ext2fs_fserr(fs, ip->i_e2fs_uid, "bad block");
    519       1.1    bouyer 		return;
    520       1.1    bouyer 	}
    521       1.6    bouyer 	error = bread(ip->i_devvp,
    522       1.6    bouyer 		fsbtodb(fs, fs->e2fs_gd[cg].ext2bgd_b_bitmap),
    523       1.1    bouyer 		(int)fs->e2fs_bsize, NOCRED, &bp);
    524       1.1    bouyer 	if (error) {
    525       1.1    bouyer 		brelse(bp);
    526       1.1    bouyer 		return;
    527       1.1    bouyer 	}
    528       1.1    bouyer 	bbp = (char *)bp->b_data;
    529       1.1    bouyer 	bno = dtogd(fs, bno);
    530       1.1    bouyer 	if (isclr(bbp, bno)) {
    531      1.15      fvdl 		printf("dev = 0x%x, block = %lld, fs = %s\n",
    532      1.15      fvdl 			ip->i_dev, (long long)bno, fs->e2fs_fsmnt);
    533       1.1    bouyer 		panic("blkfree: freeing free block");
    534       1.1    bouyer 	}
    535       1.1    bouyer 	clrbit(bbp, bno);
    536       1.1    bouyer 	fs->e2fs.e2fs_fbcount++;
    537       1.1    bouyer 	fs->e2fs_gd[cg].ext2bgd_nbfree++;
    538       1.1    bouyer 
    539       1.1    bouyer 	fs->e2fs_fmod = 1;
    540       1.1    bouyer 	bdwrite(bp);
    541       1.1    bouyer }
    542       1.1    bouyer 
    543       1.1    bouyer /*
    544       1.1    bouyer  * Free an inode.
    545       1.1    bouyer  *
    546       1.1    bouyer  * The specified inode is placed back in the free map.
    547       1.1    bouyer  */
    548       1.1    bouyer int
    549  1.24.2.1      yamt ext2fs_vfree(struct vnode *pvp, ino_t ino, int mode)
    550       1.1    bouyer {
    551       1.7  augustss 	struct m_ext2fs *fs;
    552       1.7  augustss 	char *ibp;
    553       1.7  augustss 	struct inode *pip;
    554       1.1    bouyer 	struct buf *bp;
    555       1.1    bouyer 	int error, cg;
    556       1.1    bouyer 
    557  1.24.2.1      yamt 	pip = VTOI(pvp);
    558       1.1    bouyer 	fs = pip->i_e2fs;
    559  1.24.2.2      yamt 	if ((u_int)ino > fs->e2fs.e2fs_icount || (u_int)ino < EXT2_FIRSTINO)
    560  1.24.2.1      yamt 		panic("ifree: range: dev = 0x%x, ino = %llu, fs = %s",
    561  1.24.2.1      yamt 			pip->i_dev, (unsigned long long)ino, fs->e2fs_fsmnt);
    562       1.1    bouyer 	cg = ino_to_cg(fs, ino);
    563       1.6    bouyer 	error = bread(pip->i_devvp,
    564       1.6    bouyer 		fsbtodb(fs, fs->e2fs_gd[cg].ext2bgd_i_bitmap),
    565       1.1    bouyer 		(int)fs->e2fs_bsize, NOCRED, &bp);
    566       1.1    bouyer 	if (error) {
    567       1.1    bouyer 		brelse(bp);
    568       1.1    bouyer 		return (0);
    569       1.1    bouyer 	}
    570       1.1    bouyer 	ibp = (char *)bp->b_data;
    571       1.1    bouyer 	ino = (ino - 1) % fs->e2fs.e2fs_ipg;
    572       1.1    bouyer 	if (isclr(ibp, ino)) {
    573  1.24.2.1      yamt 		printf("dev = 0x%x, ino = %llu, fs = %s\n",
    574  1.24.2.1      yamt 		    pip->i_dev, (unsigned long long)ino, fs->e2fs_fsmnt);
    575       1.1    bouyer 		if (fs->e2fs_ronly == 0)
    576       1.1    bouyer 			panic("ifree: freeing free inode");
    577       1.1    bouyer 	}
    578       1.1    bouyer 	clrbit(ibp, ino);
    579       1.1    bouyer 	fs->e2fs.e2fs_ficount++;
    580       1.1    bouyer 	fs->e2fs_gd[cg].ext2bgd_nifree++;
    581  1.24.2.1      yamt 	if ((mode & IFMT) == IFDIR) {
    582       1.1    bouyer 		fs->e2fs_gd[cg].ext2bgd_ndirs--;
    583       1.1    bouyer 	}
    584       1.1    bouyer 	fs->e2fs_fmod = 1;
    585       1.1    bouyer 	bdwrite(bp);
    586       1.1    bouyer 	return (0);
    587       1.1    bouyer }
    588       1.1    bouyer 
    589       1.1    bouyer /*
    590       1.1    bouyer  * Find a block in the specified cylinder group.
    591       1.1    bouyer  *
    592       1.1    bouyer  * It is a panic if a request is made to find a block if none are
    593       1.1    bouyer  * available.
    594       1.1    bouyer  */
    595       1.1    bouyer 
    596      1.15      fvdl static daddr_t
    597  1.24.2.1      yamt ext2fs_mapsearch(struct m_ext2fs *fs, char *bbp, daddr_t bpref)
    598       1.1    bouyer {
    599      1.15      fvdl 	daddr_t bno;
    600       1.1    bouyer 	int start, len, loc, i, map;
    601       1.1    bouyer 
    602       1.1    bouyer 	/*
    603       1.1    bouyer 	 * find the fragment by searching through the free block
    604       1.1    bouyer 	 * map for an appropriate bit pattern
    605       1.1    bouyer 	 */
    606       1.1    bouyer 	if (bpref)
    607       1.1    bouyer 		start = dtogd(fs, bpref) / NBBY;
    608       1.1    bouyer 	else
    609       1.1    bouyer 		start = 0;
    610       1.1    bouyer 	len = howmany(fs->e2fs.e2fs_fpg, NBBY) - start;
    611       1.1    bouyer 	loc = skpc(0xff, len, &bbp[start]);
    612       1.1    bouyer 	if (loc == 0) {
    613       1.1    bouyer 		len = start + 1;
    614       1.1    bouyer 		start = 0;
    615       1.1    bouyer 		loc = skpc(0xff, len, &bbp[start]);
    616       1.1    bouyer 		if (loc == 0) {
    617       1.1    bouyer 			printf("start = %d, len = %d, fs = %s\n",
    618       1.1    bouyer 				start, len, fs->e2fs_fsmnt);
    619       1.1    bouyer 			panic("ext2fs_alloccg: map corrupted");
    620       1.1    bouyer 			/* NOTREACHED */
    621       1.1    bouyer 		}
    622       1.1    bouyer 	}
    623       1.1    bouyer 	i = start + len - loc;
    624       1.1    bouyer 	map = bbp[i];
    625       1.1    bouyer 	bno = i * NBBY;
    626       1.1    bouyer 	for (i = 1; i < (1 << NBBY); i <<= 1, bno++) {
    627       1.1    bouyer 		if ((map & i) == 0)
    628       1.1    bouyer 			return (bno);
    629       1.1    bouyer 	}
    630       1.1    bouyer 	printf("fs = %s\n", fs->e2fs_fsmnt);
    631       1.1    bouyer 	panic("ext2fs_mapsearch: block not in map");
    632       1.1    bouyer 	/* NOTREACHED */
    633       1.1    bouyer }
    634       1.1    bouyer 
    635       1.1    bouyer /*
    636       1.1    bouyer  * Fserr prints the name of a file system with an error diagnostic.
    637      1.23     perry  *
    638       1.1    bouyer  * The form of the error message is:
    639       1.1    bouyer  *	fs: error message
    640       1.1    bouyer  */
    641       1.1    bouyer static void
    642  1.24.2.1      yamt ext2fs_fserr(struct m_ext2fs *fs, u_int uid, const char *cp)
    643       1.1    bouyer {
    644       1.1    bouyer 
    645       1.1    bouyer 	log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->e2fs_fsmnt, cp);
    646       1.1    bouyer }
    647