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