Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_alloc.c revision 1.45.10.2
      1  1.45.10.2     skrll /*	$NetBSD: ext2fs_alloc.c,v 1.45.10.2 2016/10/05 20:56:11 skrll 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  *
     47       1.22    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48       1.22    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49       1.22    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50       1.22    bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     51       1.22    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52       1.22    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53       1.22    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54       1.22    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55       1.22    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56       1.22    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57        1.1    bouyer  *
     58        1.1    bouyer  *	@(#)ffs_alloc.c	8.11 (Berkeley) 10/27/94
     59        1.1    bouyer  *  Modified for ext2fs by Manuel Bouyer.
     60        1.1    bouyer  */
     61       1.13     lukem 
     62       1.13     lukem #include <sys/cdefs.h>
     63  1.45.10.2     skrll __KERNEL_RCSID(0, "$NetBSD: ext2fs_alloc.c,v 1.45.10.2 2016/10/05 20:56:11 skrll Exp $");
     64        1.1    bouyer 
     65        1.1    bouyer #include <sys/param.h>
     66        1.1    bouyer #include <sys/systm.h>
     67        1.1    bouyer #include <sys/buf.h>
     68        1.1    bouyer #include <sys/proc.h>
     69        1.1    bouyer #include <sys/vnode.h>
     70        1.1    bouyer #include <sys/mount.h>
     71        1.1    bouyer #include <sys/kernel.h>
     72        1.1    bouyer #include <sys/syslog.h>
     73       1.29      elad #include <sys/kauth.h>
     74        1.1    bouyer 
     75  1.45.10.2     skrll #include <lib/libkern/crc16.h>
     76  1.45.10.2     skrll 
     77        1.1    bouyer #include <ufs/ufs/inode.h>
     78        1.1    bouyer #include <ufs/ufs/ufs_extern.h>
     79       1.27      yamt #include <ufs/ufs/ufsmount.h>
     80        1.1    bouyer 
     81        1.1    bouyer #include <ufs/ext2fs/ext2fs.h>
     82        1.1    bouyer #include <ufs/ext2fs/ext2fs_extern.h>
     83        1.1    bouyer 
     84        1.1    bouyer u_long ext2gennumber;
     85        1.1    bouyer 
     86       1.26   xtraeme static daddr_t	ext2fs_alloccg(struct inode *, int, daddr_t, int);
     87       1.26   xtraeme static u_long	ext2fs_dirpref(struct m_ext2fs *);
     88       1.26   xtraeme static void	ext2fs_fserr(struct m_ext2fs *, u_int, const char *);
     89       1.26   xtraeme static u_long	ext2fs_hashalloc(struct inode *, int, long, int,
     90       1.40   tsutsui 		    daddr_t (*)(struct inode *, int, daddr_t, int));
     91       1.26   xtraeme static daddr_t	ext2fs_nodealloccg(struct inode *, int, daddr_t, int);
     92       1.26   xtraeme static daddr_t	ext2fs_mapsearch(struct m_ext2fs *, char *, daddr_t);
     93  1.45.10.2     skrll static __inline void	ext2fs_cg_update(struct m_ext2fs *, int, struct ext2_gd *, int, int, int, daddr_t);
     94  1.45.10.2     skrll static uint16_t 	ext2fs_cg_get_csum(struct m_ext2fs *, int, struct ext2_gd *);
     95  1.45.10.2     skrll static void		ext2fs_init_bb(struct m_ext2fs *, int, struct ext2_gd *, char *);
     96        1.1    bouyer 
     97        1.1    bouyer /*
     98        1.1    bouyer  * Allocate a block in the file system.
     99       1.23     perry  *
    100        1.1    bouyer  * A preference may be optionally specified. If a preference is given
    101        1.1    bouyer  * the following hierarchy is used to allocate a block:
    102        1.1    bouyer  *   1) allocate the requested block.
    103        1.1    bouyer  *   2) allocate a rotationally optimal block in the same cylinder.
    104        1.1    bouyer  *   3) allocate a block in the same cylinder group.
    105        1.1    bouyer  *   4) quadradically rehash into other cylinder groups, until an
    106        1.1    bouyer  *	  available block is located.
    107       1.11       wiz  * If no block preference is given the following hierarchy is used
    108        1.1    bouyer  * to allocate a block:
    109        1.1    bouyer  *   1) allocate a block in the cylinder group that contains the
    110        1.1    bouyer  *	  inode for the file.
    111        1.1    bouyer  *   2) quadradically rehash into other cylinder groups, until an
    112        1.1    bouyer  *	  available block is located.
    113        1.1    bouyer  */
    114        1.1    bouyer int
    115       1.32  christos ext2fs_alloc(struct inode *ip, daddr_t lbn, daddr_t bpref,
    116       1.31  christos     kauth_cred_t cred, daddr_t *bnp)
    117        1.1    bouyer {
    118        1.7  augustss 	struct m_ext2fs *fs;
    119       1.15      fvdl 	daddr_t bno;
    120        1.1    bouyer 	int cg;
    121       1.23     perry 
    122        1.1    bouyer 	*bnp = 0;
    123        1.1    bouyer 	fs = ip->i_e2fs;
    124        1.1    bouyer #ifdef DIAGNOSTIC
    125        1.1    bouyer 	if (cred == NOCRED)
    126       1.14    provos 		panic("ext2fs_alloc: missing credential");
    127        1.1    bouyer #endif /* DIAGNOSTIC */
    128        1.1    bouyer 	if (fs->e2fs.e2fs_fbcount == 0)
    129        1.1    bouyer 		goto nospace;
    130       1.39      elad 	if (kauth_authorize_system(cred, KAUTH_SYSTEM_FS_RESERVEDSPACE, 0, NULL,
    131       1.39      elad 	    NULL, NULL) != 0 &&
    132       1.34      elad 	    freespace(fs) <= 0)
    133        1.1    bouyer 		goto nospace;
    134        1.1    bouyer 	if (bpref >= fs->e2fs.e2fs_bcount)
    135        1.1    bouyer 		bpref = 0;
    136        1.1    bouyer 	if (bpref == 0)
    137        1.1    bouyer 		cg = ino_to_cg(fs, ip->i_number);
    138        1.1    bouyer 	else
    139        1.1    bouyer 		cg = dtog(fs, bpref);
    140       1.15      fvdl 	bno = (daddr_t)ext2fs_hashalloc(ip, cg, bpref, fs->e2fs_bsize,
    141       1.40   tsutsui 	    ext2fs_alloccg);
    142        1.1    bouyer 	if (bno > 0) {
    143       1.43  jakllsch 		ext2fs_setnblock(ip, ext2fs_nblock(ip) + btodb(fs->e2fs_bsize));
    144        1.1    bouyer 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    145        1.1    bouyer 		*bnp = bno;
    146  1.45.10.2     skrll 		return 0;
    147        1.1    bouyer 	}
    148        1.1    bouyer nospace:
    149       1.29      elad 	ext2fs_fserr(fs, kauth_cred_geteuid(cred), "file system full");
    150        1.1    bouyer 	uprintf("\n%s: write failed, file system is full\n", fs->e2fs_fsmnt);
    151  1.45.10.2     skrll 	return ENOSPC;
    152        1.1    bouyer }
    153        1.1    bouyer 
    154        1.1    bouyer /*
    155        1.1    bouyer  * Allocate an inode in the file system.
    156       1.23     perry  *
    157        1.1    bouyer  * If allocating a directory, use ext2fs_dirpref to select the inode.
    158        1.1    bouyer  * If allocating in a directory, the following hierarchy is followed:
    159        1.1    bouyer  *   1) allocate the preferred inode.
    160        1.1    bouyer  *   2) allocate an inode in the same cylinder group.
    161        1.1    bouyer  *   3) quadradically rehash into other cylinder groups, until an
    162        1.1    bouyer  *	  available inode is located.
    163       1.11       wiz  * If no inode preference is given the following hierarchy is used
    164        1.1    bouyer  * to allocate an inode:
    165        1.1    bouyer  *   1) allocate an inode in cylinder group 0.
    166        1.1    bouyer  *   2) quadradically rehash into other cylinder groups, until an
    167        1.1    bouyer  *	  available inode is located.
    168        1.1    bouyer  */
    169        1.1    bouyer int
    170       1.29      elad ext2fs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred,
    171       1.27      yamt     struct vnode **vpp)
    172        1.1    bouyer {
    173        1.7  augustss 	struct inode *pip;
    174        1.7  augustss 	struct m_ext2fs *fs;
    175        1.7  augustss 	struct inode *ip;
    176        1.1    bouyer 	ino_t ino, ipref;
    177        1.1    bouyer 	int cg, error;
    178       1.23     perry 
    179       1.27      yamt 	*vpp = NULL;
    180        1.1    bouyer 	pip = VTOI(pvp);
    181        1.1    bouyer 	fs = pip->i_e2fs;
    182        1.1    bouyer 	if (fs->e2fs.e2fs_ficount == 0)
    183        1.1    bouyer 		goto noinodes;
    184        1.1    bouyer 
    185        1.1    bouyer 	if ((mode & IFMT) == IFDIR)
    186        1.1    bouyer 		cg = ext2fs_dirpref(fs);
    187        1.1    bouyer 	else
    188        1.1    bouyer 		cg = ino_to_cg(fs, pip->i_number);
    189        1.1    bouyer 	ipref = cg * fs->e2fs.e2fs_ipg + 1;
    190        1.1    bouyer 	ino = (ino_t)ext2fs_hashalloc(pip, cg, (long)ipref, mode, ext2fs_nodealloccg);
    191        1.1    bouyer 	if (ino == 0)
    192        1.1    bouyer 		goto noinodes;
    193       1.27      yamt 	error = VFS_VGET(pvp->v_mount, ino, vpp);
    194        1.1    bouyer 	if (error) {
    195       1.27      yamt 		ext2fs_vfree(pvp, ino, mode);
    196  1.45.10.2     skrll 		return error;
    197        1.1    bouyer 	}
    198       1.27      yamt 	ip = VTOI(*vpp);
    199  1.45.10.2     skrll 
    200  1.45.10.2     skrll 	KASSERT(!E2FS_HAS_GD_CSUM(fs) || (fs->e2fs_gd[ino_to_cg(fs, ino)].ext2bgd_flags & h2fs16(E2FS_BG_INODE_ZEROED)) != 0);
    201  1.45.10.2     skrll 
    202  1.45.10.2     skrll 	/* check for already used inode; makes sense only for ZEROED itable */
    203  1.45.10.2     skrll 	if (__predict_false(ip->i_e2fs_mode && ip->i_e2fs_nlink != 0)) {
    204       1.25  christos 		printf("mode = 0%o, nlinks %d, inum = %llu, fs = %s\n",
    205       1.25  christos 		    ip->i_e2fs_mode, ip->i_e2fs_nlink,
    206       1.25  christos 		    (unsigned long long)ip->i_number, fs->e2fs_fsmnt);
    207        1.1    bouyer 		panic("ext2fs_valloc: dup alloc");
    208        1.1    bouyer 	}
    209        1.1    bouyer 
    210  1.45.10.2     skrll 	memset(ip->i_din.e2fs_din, 0, EXT2_DINODE_SIZE(fs));
    211        1.1    bouyer 
    212        1.1    bouyer 	/*
    213        1.1    bouyer 	 * Set up a new generation number for this inode.
    214        1.1    bouyer 	 */
    215       1.30    kardel 	if (++ext2gennumber < time_second)
    216       1.30    kardel 		ext2gennumber = time_second;
    217        1.1    bouyer 	ip->i_e2fs_gen = ext2gennumber;
    218  1.45.10.2     skrll 	return 0;
    219        1.1    bouyer noinodes:
    220       1.29      elad 	ext2fs_fserr(fs, kauth_cred_geteuid(cred), "out of inodes");
    221        1.1    bouyer 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->e2fs_fsmnt);
    222  1.45.10.2     skrll 	return ENOSPC;
    223        1.1    bouyer }
    224        1.1    bouyer 
    225        1.1    bouyer /*
    226        1.1    bouyer  * Find a cylinder to place a directory.
    227        1.1    bouyer  *
    228        1.1    bouyer  * The policy implemented by this algorithm is to select from
    229        1.1    bouyer  * among those cylinder groups with above the average number of
    230        1.1    bouyer  * free inodes, the one with the smallest number of directories.
    231        1.1    bouyer  */
    232        1.1    bouyer static u_long
    233       1.26   xtraeme ext2fs_dirpref(struct m_ext2fs *fs)
    234        1.1    bouyer {
    235        1.1    bouyer 	int cg, maxspace, mincg, avgifree;
    236        1.1    bouyer 
    237        1.1    bouyer 	avgifree = fs->e2fs.e2fs_ficount / fs->e2fs_ncg;
    238        1.1    bouyer 	maxspace = 0;
    239        1.1    bouyer 	mincg = -1;
    240        1.1    bouyer 	for (cg = 0; cg < fs->e2fs_ncg; cg++)
    241  1.45.10.2     skrll 		if (fs2h16(fs->e2fs_gd[cg].ext2bgd_nifree) >= avgifree) {
    242  1.45.10.2     skrll 			if (mincg == -1 || fs2h16(fs->e2fs_gd[cg].ext2bgd_nbfree) > maxspace) {
    243        1.1    bouyer 				mincg = cg;
    244  1.45.10.2     skrll 				maxspace = fs2h16(fs->e2fs_gd[cg].ext2bgd_nbfree);
    245        1.1    bouyer 			}
    246        1.1    bouyer 		}
    247        1.1    bouyer 	return mincg;
    248        1.1    bouyer }
    249        1.1    bouyer 
    250        1.1    bouyer /*
    251        1.1    bouyer  * Select the desired position for the next block in a file.  The file is
    252        1.1    bouyer  * logically divided into sections. The first section is composed of the
    253        1.1    bouyer  * direct blocks. Each additional section contains fs_maxbpg blocks.
    254       1.23     perry  *
    255        1.1    bouyer  * If no blocks have been allocated in the first section, the policy is to
    256        1.1    bouyer  * request a block in the same cylinder group as the inode that describes
    257        1.1    bouyer  * the file. Otherwise, the policy is to try to allocate the blocks
    258       1.10    toshii  * contigously. The two fields of the ext2 inode extension (see
    259        1.1    bouyer  * ufs/ufs/inode.h) help this.
    260        1.1    bouyer  */
    261       1.15      fvdl daddr_t
    262       1.26   xtraeme ext2fs_blkpref(struct inode *ip, daddr_t lbn, int indx,
    263       1.26   xtraeme 		int32_t *bap /* XXX ondisk32 */)
    264        1.1    bouyer {
    265        1.7  augustss 	struct m_ext2fs *fs;
    266        1.7  augustss 	int cg, i;
    267        1.1    bouyer 
    268        1.1    bouyer 	fs = ip->i_e2fs;
    269        1.1    bouyer 	/*
    270        1.1    bouyer 	 * if we are doing contigous lbn allocation, try to alloc blocks
    271        1.1    bouyer 	 * contigously on disk
    272        1.1    bouyer 	 */
    273        1.1    bouyer 
    274        1.1    bouyer 	if ( ip->i_e2fs_last_blk && lbn == ip->i_e2fs_last_lblk + 1) {
    275        1.1    bouyer 		return ip->i_e2fs_last_blk + 1;
    276        1.1    bouyer 	}
    277        1.1    bouyer 
    278        1.1    bouyer 	/*
    279        1.1    bouyer 	 * bap, if provided, gives us a list of blocks to which we want to
    280        1.1    bouyer 	 * stay close
    281        1.1    bouyer 	 */
    282        1.1    bouyer 
    283        1.1    bouyer 	if (bap) {
    284        1.1    bouyer 		for (i = indx; i >= 0 ; i--) {
    285        1.1    bouyer 			if (bap[i]) {
    286        1.2    bouyer 				return fs2h32(bap[i]) + 1;
    287        1.1    bouyer 			}
    288        1.1    bouyer 		}
    289        1.1    bouyer 	}
    290        1.1    bouyer 
    291        1.1    bouyer 	/* fall back to the first block of the cylinder containing the inode */
    292        1.1    bouyer 
    293        1.1    bouyer 	cg = ino_to_cg(fs, ip->i_number);
    294        1.1    bouyer 	return fs->e2fs.e2fs_bpg * cg + fs->e2fs.e2fs_first_dblock + 1;
    295        1.1    bouyer }
    296        1.1    bouyer 
    297        1.1    bouyer /*
    298        1.1    bouyer  * Implement the cylinder overflow algorithm.
    299        1.1    bouyer  *
    300        1.1    bouyer  * The policy implemented by this algorithm is:
    301        1.1    bouyer  *   1) allocate the block in its requested cylinder group.
    302        1.1    bouyer  *   2) quadradically rehash on the cylinder group number.
    303        1.1    bouyer  *   3) brute force search for a free block.
    304        1.1    bouyer  */
    305        1.1    bouyer static u_long
    306       1.26   xtraeme ext2fs_hashalloc(struct inode *ip, int cg, long pref, int size,
    307       1.26   xtraeme 		daddr_t (*allocator)(struct inode *, int, daddr_t, int))
    308        1.1    bouyer {
    309        1.7  augustss 	struct m_ext2fs *fs;
    310        1.1    bouyer 	long result;
    311        1.1    bouyer 	int i, icg = cg;
    312        1.1    bouyer 
    313        1.1    bouyer 	fs = ip->i_e2fs;
    314        1.1    bouyer 	/*
    315        1.1    bouyer 	 * 1: preferred cylinder group
    316        1.1    bouyer 	 */
    317        1.1    bouyer 	result = (*allocator)(ip, cg, pref, size);
    318        1.1    bouyer 	if (result)
    319  1.45.10.2     skrll 		return result;
    320        1.1    bouyer 	/*
    321        1.1    bouyer 	 * 2: quadratic rehash
    322        1.1    bouyer 	 */
    323        1.1    bouyer 	for (i = 1; i < fs->e2fs_ncg; i *= 2) {
    324        1.1    bouyer 		cg += i;
    325        1.1    bouyer 		if (cg >= fs->e2fs_ncg)
    326        1.1    bouyer 			cg -= fs->e2fs_ncg;
    327        1.1    bouyer 		result = (*allocator)(ip, cg, 0, size);
    328        1.1    bouyer 		if (result)
    329  1.45.10.2     skrll 			return result;
    330        1.1    bouyer 	}
    331        1.1    bouyer 	/*
    332        1.1    bouyer 	 * 3: brute force search
    333        1.1    bouyer 	 * Note that we start at i == 2, since 0 was checked initially,
    334        1.1    bouyer 	 * and 1 is always checked in the quadratic rehash.
    335        1.1    bouyer 	 */
    336        1.1    bouyer 	cg = (icg + 2) % fs->e2fs_ncg;
    337        1.1    bouyer 	for (i = 2; i < fs->e2fs_ncg; i++) {
    338        1.1    bouyer 		result = (*allocator)(ip, cg, 0, size);
    339        1.1    bouyer 		if (result)
    340  1.45.10.2     skrll 			return result;
    341        1.1    bouyer 		cg++;
    342        1.1    bouyer 		if (cg == fs->e2fs_ncg)
    343        1.1    bouyer 			cg = 0;
    344        1.1    bouyer 	}
    345  1.45.10.2     skrll 	return 0;
    346        1.1    bouyer }
    347        1.1    bouyer 
    348        1.1    bouyer /*
    349        1.1    bouyer  * Determine whether a block can be allocated.
    350        1.1    bouyer  *
    351        1.1    bouyer  * Check to see if a block of the appropriate size is available,
    352        1.1    bouyer  * and if it is, allocate it.
    353        1.1    bouyer  */
    354        1.1    bouyer 
    355       1.15      fvdl static daddr_t
    356       1.32  christos ext2fs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size)
    357        1.1    bouyer {
    358        1.7  augustss 	struct m_ext2fs *fs;
    359        1.7  augustss 	char *bbp;
    360        1.1    bouyer 	struct buf *bp;
    361       1.15      fvdl 	/* XXX ondisk32 */
    362        1.1    bouyer 	int error, bno, start, end, loc;
    363        1.1    bouyer 
    364        1.1    bouyer 	fs = ip->i_e2fs;
    365        1.1    bouyer 	if (fs->e2fs_gd[cg].ext2bgd_nbfree == 0)
    366  1.45.10.2     skrll 		return 0;
    367       1.45  dholland 	error = bread(ip->i_devvp, EXT2_FSBTODB(fs,
    368  1.45.10.2     skrll 		fs2h32(fs->e2fs_gd[cg].ext2bgd_b_bitmap)),
    369  1.45.10.1     skrll 		(int)fs->e2fs_bsize, B_MODIFY, &bp);
    370        1.1    bouyer 	if (error) {
    371  1.45.10.2     skrll 		return 0;
    372        1.1    bouyer 	}
    373        1.1    bouyer 	bbp = (char *)bp->b_data;
    374        1.1    bouyer 
    375        1.1    bouyer 	if (dtog(fs, bpref) != cg)
    376        1.1    bouyer 		bpref = 0;
    377  1.45.10.2     skrll 
    378  1.45.10.2     skrll 	/* initialize block bitmap now if uninit */
    379  1.45.10.2     skrll 	if (__predict_false(E2FS_HAS_GD_CSUM(fs) &&
    380  1.45.10.2     skrll 	    (fs->e2fs_gd[cg].ext2bgd_flags & h2fs16(E2FS_BG_BLOCK_UNINIT)))) {
    381  1.45.10.2     skrll 		ext2fs_init_bb(fs, cg, &fs->e2fs_gd[cg], bbp);
    382  1.45.10.2     skrll 		fs->e2fs_gd[cg].ext2bgd_flags &= h2fs16(~E2FS_BG_BLOCK_UNINIT);
    383  1.45.10.2     skrll 	}
    384  1.45.10.2     skrll 
    385        1.1    bouyer 	if (bpref != 0) {
    386        1.1    bouyer 		bpref = dtogd(fs, bpref);
    387        1.1    bouyer 		/*
    388        1.1    bouyer 		 * if the requested block is available, use it
    389        1.1    bouyer 		 */
    390        1.1    bouyer 		if (isclr(bbp, bpref)) {
    391        1.1    bouyer 			bno = bpref;
    392        1.1    bouyer 			goto gotit;
    393        1.1    bouyer 		}
    394        1.1    bouyer 	}
    395        1.1    bouyer 	/*
    396        1.1    bouyer 	 * no blocks in the requested cylinder, so take next
    397        1.1    bouyer 	 * available one in this cylinder group.
    398        1.1    bouyer 	 * first try to get 8 contigous blocks, then fall back to a single
    399        1.1    bouyer 	 * block.
    400        1.1    bouyer 	 */
    401        1.1    bouyer 	if (bpref)
    402        1.1    bouyer 		start = dtogd(fs, bpref) / NBBY;
    403        1.1    bouyer 	else
    404        1.1    bouyer 		start = 0;
    405        1.1    bouyer 	end = howmany(fs->e2fs.e2fs_fpg, NBBY) - start;
    406        1.1    bouyer 	for (loc = start; loc < end; loc++) {
    407        1.1    bouyer 		if (bbp[loc] == 0) {
    408        1.1    bouyer 			bno = loc * NBBY;
    409        1.1    bouyer 			goto gotit;
    410        1.1    bouyer 		}
    411        1.1    bouyer 	}
    412        1.1    bouyer 	for (loc = 0; loc < start; loc++) {
    413        1.1    bouyer 		if (bbp[loc] == 0) {
    414        1.1    bouyer 			bno = loc * NBBY;
    415        1.1    bouyer 			goto gotit;
    416        1.1    bouyer 		}
    417        1.1    bouyer 	}
    418        1.1    bouyer 
    419        1.1    bouyer 	bno = ext2fs_mapsearch(fs, bbp, bpref);
    420  1.45.10.2     skrll #if 0
    421  1.45.10.2     skrll 	/*
    422  1.45.10.2     skrll 	 * XXX jdolecek mapsearch actually never fails, it panics instead.
    423  1.45.10.2     skrll 	 * If re-enabling, make sure to brele() before returning.
    424  1.45.10.2     skrll 	 */
    425        1.1    bouyer 	if (bno < 0)
    426  1.45.10.2     skrll 		return 0;
    427  1.45.10.2     skrll #endif
    428        1.1    bouyer gotit:
    429        1.1    bouyer #ifdef DIAGNOSTIC
    430       1.15      fvdl 	if (isset(bbp, (daddr_t)bno)) {
    431        1.1    bouyer 		printf("ext2fs_alloccgblk: cg=%d bno=%d fs=%s\n",
    432        1.1    bouyer 			cg, bno, fs->e2fs_fsmnt);
    433        1.2    bouyer 		panic("ext2fs_alloccg: dup alloc");
    434        1.1    bouyer 	}
    435        1.1    bouyer #endif
    436       1.15      fvdl 	setbit(bbp, (daddr_t)bno);
    437        1.1    bouyer 	fs->e2fs.e2fs_fbcount--;
    438  1.45.10.2     skrll 	ext2fs_cg_update(fs, cg, &fs->e2fs_gd[cg], -1, 0, 0, 0);
    439        1.1    bouyer 	fs->e2fs_fmod = 1;
    440        1.1    bouyer 	bdwrite(bp);
    441  1.45.10.2     skrll 	return cg * fs->e2fs.e2fs_fpg + fs->e2fs.e2fs_first_dblock + bno;
    442        1.1    bouyer }
    443        1.1    bouyer 
    444        1.1    bouyer /*
    445        1.1    bouyer  * Determine whether an inode can be allocated.
    446        1.1    bouyer  *
    447        1.1    bouyer  * Check to see if an inode is available, and if it is,
    448        1.1    bouyer  * allocate it using the following policy:
    449        1.1    bouyer  *   1) allocate the requested inode.
    450        1.1    bouyer  *   2) allocate the next available inode after the requested
    451        1.1    bouyer  *	  inode in the specified cylinder group.
    452        1.1    bouyer  */
    453       1.15      fvdl static daddr_t
    454       1.26   xtraeme ext2fs_nodealloccg(struct inode *ip, int cg, daddr_t ipref, int mode)
    455        1.1    bouyer {
    456        1.7  augustss 	struct m_ext2fs *fs;
    457        1.7  augustss 	char *ibp;
    458        1.1    bouyer 	struct buf *bp;
    459        1.1    bouyer 	int error, start, len, loc, map, i;
    460        1.1    bouyer 
    461        1.1    bouyer 	ipref--; /* to avoid a lot of (ipref -1) */
    462       1.33       chs 	if (ipref == -1)
    463       1.33       chs 		ipref = 0;
    464        1.1    bouyer 	fs = ip->i_e2fs;
    465        1.1    bouyer 	if (fs->e2fs_gd[cg].ext2bgd_nifree == 0)
    466  1.45.10.2     skrll 		return 0;
    467       1.45  dholland 	error = bread(ip->i_devvp, EXT2_FSBTODB(fs,
    468  1.45.10.2     skrll 		fs2h32(fs->e2fs_gd[cg].ext2bgd_i_bitmap)),
    469  1.45.10.1     skrll 		(int)fs->e2fs_bsize, B_MODIFY, &bp);
    470        1.1    bouyer 	if (error) {
    471  1.45.10.2     skrll 		return 0;
    472        1.1    bouyer 	}
    473        1.1    bouyer 	ibp = (char *)bp->b_data;
    474  1.45.10.2     skrll 
    475  1.45.10.2     skrll 	KASSERT(!E2FS_HAS_GD_CSUM(fs) || (fs->e2fs_gd[cg].ext2bgd_flags & h2fs16(E2FS_BG_INODE_ZEROED)) != 0);
    476  1.45.10.2     skrll 
    477  1.45.10.2     skrll 	/* initialize inode bitmap now if uninit */
    478  1.45.10.2     skrll 	if (__predict_false(E2FS_HAS_GD_CSUM(fs) &&
    479  1.45.10.2     skrll 	    (fs->e2fs_gd[cg].ext2bgd_flags & h2fs16(E2FS_BG_INODE_UNINIT)))) {
    480  1.45.10.2     skrll 		KASSERT(fs2h16(fs->e2fs_gd[cg].ext2bgd_nifree) == fs->e2fs.e2fs_ipg);
    481  1.45.10.2     skrll 		memset(ibp, 0, fs->e2fs_bsize);
    482  1.45.10.2     skrll 		fs->e2fs_gd[cg].ext2bgd_flags &= h2fs16(~E2FS_BG_INODE_UNINIT);
    483  1.45.10.2     skrll 	}
    484  1.45.10.2     skrll 
    485        1.1    bouyer 	if (ipref) {
    486        1.1    bouyer 		ipref %= fs->e2fs.e2fs_ipg;
    487        1.1    bouyer 		if (isclr(ibp, ipref))
    488        1.1    bouyer 			goto gotit;
    489        1.1    bouyer 	}
    490        1.1    bouyer 	start = ipref / NBBY;
    491        1.1    bouyer 	len = howmany(fs->e2fs.e2fs_ipg - ipref, NBBY);
    492        1.1    bouyer 	loc = skpc(0xff, len, &ibp[start]);
    493        1.1    bouyer 	if (loc == 0) {
    494        1.1    bouyer 		len = start + 1;
    495        1.1    bouyer 		start = 0;
    496        1.1    bouyer 		loc = skpc(0xff, len, &ibp[0]);
    497        1.1    bouyer 		if (loc == 0) {
    498       1.15      fvdl 			printf("cg = %d, ipref = %lld, fs = %s\n",
    499       1.15      fvdl 				cg, (long long)ipref, fs->e2fs_fsmnt);
    500        1.1    bouyer 			panic("ext2fs_nodealloccg: map corrupted");
    501        1.1    bouyer 			/* NOTREACHED */
    502        1.1    bouyer 		}
    503        1.1    bouyer 	}
    504        1.1    bouyer 	i = start + len - loc;
    505       1.42     rmind 	map = ibp[i] ^ 0xff;
    506       1.42     rmind 	if (map == 0) {
    507       1.42     rmind 		printf("fs = %s\n", fs->e2fs_fsmnt);
    508  1.45.10.2     skrll 		panic("ext2fs_nodealloccg: inode not in map");
    509        1.1    bouyer 	}
    510       1.42     rmind 	ipref = i * NBBY + ffs(map) - 1;
    511        1.1    bouyer gotit:
    512        1.1    bouyer 	setbit(ibp, ipref);
    513        1.1    bouyer 	fs->e2fs.e2fs_ficount--;
    514  1.45.10.2     skrll 	ext2fs_cg_update(fs, cg, &fs->e2fs_gd[cg],
    515  1.45.10.2     skrll 		0, -1, ((mode & IFMT) == IFDIR) ? 1 : 0, ipref);
    516        1.1    bouyer 	fs->e2fs_fmod = 1;
    517        1.1    bouyer 	bdwrite(bp);
    518  1.45.10.2     skrll 	return cg * fs->e2fs.e2fs_ipg + ipref + 1;
    519        1.1    bouyer }
    520        1.1    bouyer 
    521        1.1    bouyer /*
    522        1.1    bouyer  * Free a block.
    523        1.1    bouyer  *
    524        1.1    bouyer  * The specified block is placed back in the
    525        1.1    bouyer  * free map.
    526        1.1    bouyer  */
    527        1.1    bouyer void
    528       1.26   xtraeme ext2fs_blkfree(struct inode *ip, daddr_t bno)
    529        1.1    bouyer {
    530        1.7  augustss 	struct m_ext2fs *fs;
    531        1.7  augustss 	char *bbp;
    532        1.1    bouyer 	struct buf *bp;
    533        1.1    bouyer 	int error, cg;
    534        1.1    bouyer 
    535        1.1    bouyer 	fs = ip->i_e2fs;
    536        1.1    bouyer 	cg = dtog(fs, bno);
    537  1.45.10.2     skrll 
    538  1.45.10.2     skrll 	KASSERT(!E2FS_HAS_GD_CSUM(fs) || (fs->e2fs_gd[cg].ext2bgd_flags & h2fs16(E2FS_BG_BLOCK_UNINIT)) == 0);
    539  1.45.10.2     skrll 
    540        1.1    bouyer 	if ((u_int)bno >= fs->e2fs.e2fs_bcount) {
    541       1.25  christos 		printf("bad block %lld, ino %llu\n", (long long)bno,
    542       1.25  christos 		    (unsigned long long)ip->i_number);
    543       1.37       mrg 		ext2fs_fserr(fs, ip->i_uid, "bad block");
    544        1.1    bouyer 		return;
    545        1.1    bouyer 	}
    546        1.6    bouyer 	error = bread(ip->i_devvp,
    547  1.45.10.2     skrll 		EXT2_FSBTODB(fs, fs2h32(fs->e2fs_gd[cg].ext2bgd_b_bitmap)),
    548  1.45.10.1     skrll 		(int)fs->e2fs_bsize, B_MODIFY, &bp);
    549        1.1    bouyer 	if (error) {
    550        1.1    bouyer 		return;
    551        1.1    bouyer 	}
    552        1.1    bouyer 	bbp = (char *)bp->b_data;
    553        1.1    bouyer 	bno = dtogd(fs, bno);
    554        1.1    bouyer 	if (isclr(bbp, bno)) {
    555       1.38  christos 		printf("dev = 0x%llx, block = %lld, fs = %s\n",
    556       1.38  christos 		    (unsigned long long)ip->i_dev, (long long)bno,
    557       1.38  christos 		    fs->e2fs_fsmnt);
    558        1.1    bouyer 		panic("blkfree: freeing free block");
    559        1.1    bouyer 	}
    560        1.1    bouyer 	clrbit(bbp, bno);
    561        1.1    bouyer 	fs->e2fs.e2fs_fbcount++;
    562  1.45.10.2     skrll 	ext2fs_cg_update(fs, cg, &fs->e2fs_gd[cg], 1, 0, 0, 0);
    563        1.1    bouyer 	fs->e2fs_fmod = 1;
    564        1.1    bouyer 	bdwrite(bp);
    565        1.1    bouyer }
    566        1.1    bouyer 
    567        1.1    bouyer /*
    568        1.1    bouyer  * Free an inode.
    569        1.1    bouyer  *
    570        1.1    bouyer  * The specified inode is placed back in the free map.
    571        1.1    bouyer  */
    572        1.1    bouyer int
    573       1.27      yamt ext2fs_vfree(struct vnode *pvp, ino_t ino, int mode)
    574        1.1    bouyer {
    575        1.7  augustss 	struct m_ext2fs *fs;
    576        1.7  augustss 	char *ibp;
    577        1.7  augustss 	struct inode *pip;
    578        1.1    bouyer 	struct buf *bp;
    579        1.1    bouyer 	int error, cg;
    580        1.1    bouyer 
    581       1.27      yamt 	pip = VTOI(pvp);
    582        1.1    bouyer 	fs = pip->i_e2fs;
    583  1.45.10.2     skrll 
    584       1.33       chs 	if ((u_int)ino > fs->e2fs.e2fs_icount || (u_int)ino < EXT2_FIRSTINO)
    585       1.38  christos 		panic("ifree: range: dev = 0x%llx, ino = %llu, fs = %s",
    586       1.38  christos 		    (unsigned long long)pip->i_dev, (unsigned long long)ino,
    587       1.38  christos 		    fs->e2fs_fsmnt);
    588  1.45.10.2     skrll 
    589        1.1    bouyer 	cg = ino_to_cg(fs, ino);
    590  1.45.10.2     skrll 
    591  1.45.10.2     skrll 	KASSERT(!E2FS_HAS_GD_CSUM(fs) || (fs->e2fs_gd[cg].ext2bgd_flags & h2fs16(E2FS_BG_INODE_UNINIT)) == 0);
    592  1.45.10.2     skrll 
    593        1.6    bouyer 	error = bread(pip->i_devvp,
    594  1.45.10.2     skrll 		EXT2_FSBTODB(fs, fs2h32(fs->e2fs_gd[cg].ext2bgd_i_bitmap)),
    595  1.45.10.1     skrll 		(int)fs->e2fs_bsize, B_MODIFY, &bp);
    596        1.1    bouyer 	if (error) {
    597  1.45.10.2     skrll 		return 0;
    598        1.1    bouyer 	}
    599        1.1    bouyer 	ibp = (char *)bp->b_data;
    600        1.1    bouyer 	ino = (ino - 1) % fs->e2fs.e2fs_ipg;
    601        1.1    bouyer 	if (isclr(ibp, ino)) {
    602       1.38  christos 		printf("dev = 0x%llx, ino = %llu, fs = %s\n",
    603       1.38  christos 		    (unsigned long long)pip->i_dev,
    604       1.38  christos 		    (unsigned long long)ino, fs->e2fs_fsmnt);
    605        1.1    bouyer 		if (fs->e2fs_ronly == 0)
    606        1.1    bouyer 			panic("ifree: freeing free inode");
    607        1.1    bouyer 	}
    608        1.1    bouyer 	clrbit(ibp, ino);
    609        1.1    bouyer 	fs->e2fs.e2fs_ficount++;
    610  1.45.10.2     skrll 	ext2fs_cg_update(fs, cg, &fs->e2fs_gd[cg],
    611  1.45.10.2     skrll 		0, 1, ((mode & IFMT) == IFDIR) ? -1 : 0, 0);
    612        1.1    bouyer 	fs->e2fs_fmod = 1;
    613        1.1    bouyer 	bdwrite(bp);
    614  1.45.10.2     skrll 	return 0;
    615        1.1    bouyer }
    616        1.1    bouyer 
    617        1.1    bouyer /*
    618        1.1    bouyer  * Find a block in the specified cylinder group.
    619        1.1    bouyer  *
    620        1.1    bouyer  * It is a panic if a request is made to find a block if none are
    621        1.1    bouyer  * available.
    622        1.1    bouyer  */
    623        1.1    bouyer 
    624       1.15      fvdl static daddr_t
    625       1.26   xtraeme ext2fs_mapsearch(struct m_ext2fs *fs, char *bbp, daddr_t bpref)
    626        1.1    bouyer {
    627        1.1    bouyer 	int start, len, loc, i, map;
    628        1.1    bouyer 
    629        1.1    bouyer 	/*
    630        1.1    bouyer 	 * find the fragment by searching through the free block
    631        1.1    bouyer 	 * map for an appropriate bit pattern
    632        1.1    bouyer 	 */
    633        1.1    bouyer 	if (bpref)
    634        1.1    bouyer 		start = dtogd(fs, bpref) / NBBY;
    635        1.1    bouyer 	else
    636        1.1    bouyer 		start = 0;
    637        1.1    bouyer 	len = howmany(fs->e2fs.e2fs_fpg, NBBY) - start;
    638        1.1    bouyer 	loc = skpc(0xff, len, &bbp[start]);
    639        1.1    bouyer 	if (loc == 0) {
    640        1.1    bouyer 		len = start + 1;
    641        1.1    bouyer 		start = 0;
    642        1.1    bouyer 		loc = skpc(0xff, len, &bbp[start]);
    643        1.1    bouyer 		if (loc == 0) {
    644        1.1    bouyer 			printf("start = %d, len = %d, fs = %s\n",
    645        1.1    bouyer 				start, len, fs->e2fs_fsmnt);
    646        1.1    bouyer 			panic("ext2fs_alloccg: map corrupted");
    647        1.1    bouyer 			/* NOTREACHED */
    648        1.1    bouyer 		}
    649        1.1    bouyer 	}
    650        1.1    bouyer 	i = start + len - loc;
    651       1.42     rmind 	map = bbp[i] ^ 0xff;
    652       1.42     rmind 	if (map == 0) {
    653       1.42     rmind 		printf("fs = %s\n", fs->e2fs_fsmnt);
    654       1.42     rmind 		panic("ext2fs_mapsearch: block not in map");
    655       1.42     rmind 	}
    656       1.42     rmind 	return i * NBBY + ffs(map) - 1;
    657        1.1    bouyer }
    658        1.1    bouyer 
    659        1.1    bouyer /*
    660        1.1    bouyer  * Fserr prints the name of a file system with an error diagnostic.
    661       1.23     perry  *
    662        1.1    bouyer  * The form of the error message is:
    663        1.1    bouyer  *	fs: error message
    664        1.1    bouyer  */
    665        1.1    bouyer static void
    666       1.26   xtraeme ext2fs_fserr(struct m_ext2fs *fs, u_int uid, const char *cp)
    667        1.1    bouyer {
    668        1.1    bouyer 
    669        1.1    bouyer 	log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->e2fs_fsmnt, cp);
    670        1.1    bouyer }
    671  1.45.10.2     skrll 
    672  1.45.10.2     skrll static __inline void
    673  1.45.10.2     skrll ext2fs_cg_update(struct m_ext2fs *fs, int cg, struct ext2_gd *gd, int nbfree, int nifree, int ndirs, daddr_t ioff)
    674  1.45.10.2     skrll {
    675  1.45.10.2     skrll 	/* XXX disk32 */
    676  1.45.10.2     skrll 	if (nifree) {
    677  1.45.10.2     skrll 		gd->ext2bgd_nifree = h2fs16(fs2h16(gd->ext2bgd_nifree) + nifree);
    678  1.45.10.2     skrll 		/*
    679  1.45.10.2     skrll 		 * If we allocated inode on bigger offset than what was
    680  1.45.10.2     skrll 		 * ever used before, bump the itable_unused count. This
    681  1.45.10.2     skrll 		 * member only ever grows, and is used only for initialization
    682  1.45.10.2     skrll 		 * !INODE_ZEROED groups with used inodes. Of course, by the
    683  1.45.10.2     skrll 		 * time we get here the itables are already zeroed, but
    684  1.45.10.2     skrll 		 * e2fstools fsck.ext4 still checks this.
    685  1.45.10.2     skrll 		 */
    686  1.45.10.2     skrll 		if (E2FS_HAS_GD_CSUM(fs) && nifree < 0 && (ioff+1) >= (fs->e2fs.e2fs_ipg - fs2h16(gd->ext2bgd_itable_unused_lo))) {
    687  1.45.10.2     skrll 			gd->ext2bgd_itable_unused_lo = h2fs16(fs->e2fs.e2fs_ipg - (ioff + 1));
    688  1.45.10.2     skrll 		}
    689  1.45.10.2     skrll 
    690  1.45.10.2     skrll 		KASSERT(!E2FS_HAS_GD_CSUM(fs) || gd->ext2bgd_itable_unused_lo <= gd->ext2bgd_nifree);
    691  1.45.10.2     skrll 	}
    692  1.45.10.2     skrll 
    693  1.45.10.2     skrll 
    694  1.45.10.2     skrll 	if (nbfree)
    695  1.45.10.2     skrll 		gd->ext2bgd_nbfree = h2fs16(fs2h16(gd->ext2bgd_nbfree) + nbfree);
    696  1.45.10.2     skrll 
    697  1.45.10.2     skrll 	if (ndirs)
    698  1.45.10.2     skrll 		gd->ext2bgd_ndirs = h2fs16(fs2h16(gd->ext2bgd_ndirs) + ndirs);
    699  1.45.10.2     skrll 
    700  1.45.10.2     skrll 	if (E2FS_HAS_GD_CSUM(fs))
    701  1.45.10.2     skrll 		gd->ext2bgd_checksum = ext2fs_cg_get_csum(fs, cg, gd);
    702  1.45.10.2     skrll }
    703  1.45.10.2     skrll 
    704  1.45.10.2     skrll /*
    705  1.45.10.2     skrll  * Compute group description csum. Structure data must be LE (not host).
    706  1.45.10.2     skrll  * Returned as LE (disk encoding).
    707  1.45.10.2     skrll  */
    708  1.45.10.2     skrll static uint16_t
    709  1.45.10.2     skrll ext2fs_cg_get_csum(struct m_ext2fs *fs, int cg, struct ext2_gd *gd)
    710  1.45.10.2     skrll {
    711  1.45.10.2     skrll 	uint16_t crc;
    712  1.45.10.2     skrll 	uint32_t cg_bswapped = h2fs32((uint32_t)cg);
    713  1.45.10.2     skrll 	size_t off;
    714  1.45.10.2     skrll 
    715  1.45.10.2     skrll 	if (!EXT2F_HAS_ROCOMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM))
    716  1.45.10.2     skrll 		return 0;
    717  1.45.10.2     skrll 
    718  1.45.10.2     skrll 	off = offsetof(struct ext2_gd, ext2bgd_checksum);
    719  1.45.10.2     skrll 
    720  1.45.10.2     skrll 	crc = crc16(~0, (uint8_t *)fs->e2fs.e2fs_uuid, sizeof(fs->e2fs.e2fs_uuid));
    721  1.45.10.2     skrll 	crc = crc16(crc, (uint8_t *)&cg_bswapped, sizeof(cg_bswapped));
    722  1.45.10.2     skrll 	crc = crc16(crc, (uint8_t *)gd, off);
    723  1.45.10.2     skrll 	/* XXX ondisk32 */
    724  1.45.10.2     skrll 
    725  1.45.10.2     skrll 	return h2fs16(crc);
    726  1.45.10.2     skrll }
    727  1.45.10.2     skrll 
    728  1.45.10.2     skrll static void
    729  1.45.10.2     skrll ext2fs_init_bb(struct m_ext2fs *fs, int cg, struct ext2_gd *gd, char *bbp)
    730  1.45.10.2     skrll {
    731  1.45.10.2     skrll 	int i;
    732  1.45.10.2     skrll 
    733  1.45.10.2     skrll 	memset(bbp, 0, fs->e2fs_bsize);
    734  1.45.10.2     skrll 
    735  1.45.10.2     skrll 	/*
    736  1.45.10.2     skrll 	 * No block was ever allocated on this cg before, so the only used
    737  1.45.10.2     skrll 	 * blocks are metadata blocks on start of the group. We could optimize
    738  1.45.10.2     skrll 	 * this to set by bytes, but since this is done once per the group
    739  1.45.10.2     skrll 	 * in lifetime of filesystem, it really is not worth it.
    740  1.45.10.2     skrll 	 */
    741  1.45.10.2     skrll 	for(i=0; i < fs->e2fs.e2fs_bpg - fs2h16(gd->ext2bgd_nbfree); i++)
    742  1.45.10.2     skrll 		setbit(bbp, i);
    743  1.45.10.2     skrll }
    744  1.45.10.2     skrll 
    745  1.45.10.2     skrll /*
    746  1.45.10.2     skrll  * Verify csum and initialize itable if not done already
    747  1.45.10.2     skrll  */
    748  1.45.10.2     skrll int
    749  1.45.10.2     skrll ext2fs_cg_verify_and_initialize(struct vnode *devvp, struct m_ext2fs *fs, int ronly)
    750  1.45.10.2     skrll {
    751  1.45.10.2     skrll 	/* XXX disk32 */
    752  1.45.10.2     skrll 	struct ext2_gd *gd;
    753  1.45.10.2     skrll 	ino_t ioff;
    754  1.45.10.2     skrll 	size_t boff;
    755  1.45.10.2     skrll 	struct buf *bp;
    756  1.45.10.2     skrll 	int cg, i, error;
    757  1.45.10.2     skrll 
    758  1.45.10.2     skrll 	if (!E2FS_HAS_GD_CSUM(fs))
    759  1.45.10.2     skrll 		return 0;
    760  1.45.10.2     skrll 
    761  1.45.10.2     skrll 	for(cg=0; cg < fs->e2fs_ncg; cg++) {
    762  1.45.10.2     skrll 		gd = &fs->e2fs_gd[cg];
    763  1.45.10.2     skrll 
    764  1.45.10.2     skrll 		/* Verify checksum */
    765  1.45.10.2     skrll 		if (gd->ext2bgd_checksum != ext2fs_cg_get_csum(fs, cg, gd)) {
    766  1.45.10.2     skrll 			printf("ext2fs_cg_verify_and_initialize: group %d invalid csum\n", cg);
    767  1.45.10.2     skrll 			return EINVAL;
    768  1.45.10.2     skrll 		}
    769  1.45.10.2     skrll 
    770  1.45.10.2     skrll 		/* if mounting read-write, zero itable if not already done */
    771  1.45.10.2     skrll 		if (ronly || (gd->ext2bgd_flags & h2fs16(E2FS_BG_INODE_ZEROED)) != 0)
    772  1.45.10.2     skrll 			continue;
    773  1.45.10.2     skrll 
    774  1.45.10.2     skrll 		/*
    775  1.45.10.2     skrll 		 * We are skipping already used inodes, zero rest of itable
    776  1.45.10.2     skrll 		 * blocks. First block to zero could be only partial wipe, all
    777  1.45.10.2     skrll 		 * others are wiped completely. This might take a while,
    778  1.45.10.2     skrll 		 * there could be many inode table blocks. We use
    779  1.45.10.2     skrll 		 * delayed writes, so this shouldn't block for very
    780  1.45.10.2     skrll 		 * long.
    781  1.45.10.2     skrll 		 */
    782  1.45.10.2     skrll 		ioff = fs->e2fs.e2fs_ipg - fs2h16(gd->ext2bgd_itable_unused_lo);
    783  1.45.10.2     skrll 		boff = (ioff % fs->e2fs_ipb) * EXT2_DINODE_SIZE(fs);
    784  1.45.10.2     skrll 
    785  1.45.10.2     skrll 		for(i = ioff / fs->e2fs_ipb; i < fs->e2fs_itpg; i++) {
    786  1.45.10.2     skrll 			if (boff) {
    787  1.45.10.2     skrll 				/* partial wipe, must read old data */
    788  1.45.10.2     skrll 				error = bread(devvp,
    789  1.45.10.2     skrll 					EXT2_FSBTODB(fs, fs2h32(gd->ext2bgd_i_tables) + i),
    790  1.45.10.2     skrll 					(int)fs->e2fs_bsize, B_MODIFY, &bp);
    791  1.45.10.2     skrll 				if (error) {
    792  1.45.10.2     skrll 					printf("ext2fs_cg_verify_and_initialize: can't read itable block");
    793  1.45.10.2     skrll 					return error;
    794  1.45.10.2     skrll 				}
    795  1.45.10.2     skrll 				memset((char *)bp->b_data + boff, 0, fs->e2fs_bsize - boff);
    796  1.45.10.2     skrll 				boff = 0;
    797  1.45.10.2     skrll 			} else {
    798  1.45.10.2     skrll 				/*
    799  1.45.10.2     skrll 				 * Complete wipe, don't need to read data. This
    800  1.45.10.2     skrll 				 * assumes nothing else is changing the data.
    801  1.45.10.2     skrll 				 */
    802  1.45.10.2     skrll 				bp = getblk(devvp,
    803  1.45.10.2     skrll 					EXT2_FSBTODB(fs, fs2h32(gd->ext2bgd_i_tables) + i),
    804  1.45.10.2     skrll 					(int)fs->e2fs_bsize, 0, 0);
    805  1.45.10.2     skrll 				clrbuf(bp);
    806  1.45.10.2     skrll 			}
    807  1.45.10.2     skrll 
    808  1.45.10.2     skrll 			bdwrite(bp);
    809  1.45.10.2     skrll 		}
    810  1.45.10.2     skrll 
    811  1.45.10.2     skrll 		gd->ext2bgd_flags |= h2fs16(E2FS_BG_INODE_ZEROED);
    812  1.45.10.2     skrll 		gd->ext2bgd_checksum = ext2fs_cg_get_csum(fs, cg, gd);
    813  1.45.10.2     skrll 		fs->e2fs_fmod = 1;
    814  1.45.10.2     skrll 	}
    815  1.45.10.2     skrll 
    816  1.45.10.2     skrll 	return 0;
    817  1.45.10.2     skrll }
    818