Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.119.6.3
      1  1.119.6.3     skrll /*	$NetBSD: lfs_alloc.c,v 1.119.6.3 2015/12/27 12:10:19 skrll Exp $	*/
      2        1.2       cgd 
      3       1.17  perseant /*-
      4      1.100        ad  * Copyright (c) 1999, 2000, 2001, 2002, 2003, 2007 The NetBSD Foundation, Inc.
      5       1.17  perseant  * All rights reserved.
      6       1.17  perseant  *
      7       1.17  perseant  * This code is derived from software contributed to The NetBSD Foundation
      8       1.17  perseant  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9       1.17  perseant  *
     10       1.17  perseant  * Redistribution and use in source and binary forms, with or without
     11       1.17  perseant  * modification, are permitted provided that the following conditions
     12       1.17  perseant  * are met:
     13       1.17  perseant  * 1. Redistributions of source code must retain the above copyright
     14       1.17  perseant  *    notice, this list of conditions and the following disclaimer.
     15       1.17  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.17  perseant  *    notice, this list of conditions and the following disclaimer in the
     17       1.17  perseant  *    documentation and/or other materials provided with the distribution.
     18       1.17  perseant  *
     19       1.17  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.17  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.17  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.17  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.17  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.17  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.17  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.17  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.17  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.17  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.17  perseant  * POSSIBILITY OF SUCH DAMAGE.
     30       1.17  perseant  */
     31        1.1   mycroft /*
     32        1.1   mycroft  * Copyright (c) 1991, 1993
     33        1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
     34        1.1   mycroft  *
     35        1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     36        1.1   mycroft  * modification, are permitted provided that the following conditions
     37        1.1   mycroft  * are met:
     38        1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     39        1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     40        1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     41        1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     42        1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     43       1.71       agc  * 3. Neither the name of the University nor the names of its contributors
     44        1.1   mycroft  *    may be used to endorse or promote products derived from this software
     45        1.1   mycroft  *    without specific prior written permission.
     46        1.1   mycroft  *
     47        1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     48        1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     49        1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     50        1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     51        1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     52        1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     53        1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     54        1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     55        1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     56        1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     57        1.1   mycroft  * SUCH DAMAGE.
     58        1.1   mycroft  *
     59        1.2       cgd  *	@(#)lfs_alloc.c	8.4 (Berkeley) 1/4/94
     60        1.1   mycroft  */
     61       1.52     lukem 
     62       1.52     lukem #include <sys/cdefs.h>
     63  1.119.6.3     skrll __KERNEL_RCSID(0, "$NetBSD: lfs_alloc.c,v 1.119.6.3 2015/12/27 12:10:19 skrll Exp $");
     64       1.12    scottr 
     65       1.47       mrg #if defined(_KERNEL_OPT)
     66       1.12    scottr #include "opt_quota.h"
     67       1.13    scottr #endif
     68        1.1   mycroft 
     69        1.1   mycroft #include <sys/param.h>
     70        1.3  christos #include <sys/systm.h>
     71        1.1   mycroft #include <sys/kernel.h>
     72        1.1   mycroft #include <sys/buf.h>
     73       1.56  perseant #include <sys/lock.h>
     74        1.1   mycroft #include <sys/vnode.h>
     75        1.1   mycroft #include <sys/syslog.h>
     76        1.1   mycroft #include <sys/mount.h>
     77       1.87  perseant #include <sys/malloc.h>
     78       1.15   thorpej #include <sys/pool.h>
     79       1.50       chs #include <sys/proc.h>
     80       1.81  perseant #include <sys/tree.h>
     81       1.94      elad #include <sys/kauth.h>
     82        1.1   mycroft 
     83      1.114  dholland #include <ufs/lfs/ulfs_quotacommon.h>
     84      1.114  dholland #include <ufs/lfs/ulfs_inode.h>
     85      1.114  dholland #include <ufs/lfs/ulfsmount.h>
     86      1.114  dholland #include <ufs/lfs/ulfs_extern.h>
     87        1.1   mycroft 
     88        1.1   mycroft #include <ufs/lfs/lfs.h>
     89  1.119.6.2     skrll #include <ufs/lfs/lfs_accessors.h>
     90        1.1   mycroft #include <ufs/lfs/lfs_extern.h>
     91      1.118  dholland #include <ufs/lfs/lfs_kernel.h>
     92        1.1   mycroft 
     93       1.87  perseant /* Constants for inode free bitmap */
     94       1.88  perseant #define BMSHIFT 5	/* 2 ** 5 = 32 */
     95       1.88  perseant #define BMMASK  ((1 << BMSHIFT) - 1)
     96       1.88  perseant #define SET_BITMAP_FREE(F, I) do { \
     97       1.88  perseant 	DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d set\n", (int)(I), 	\
     98       1.88  perseant 	     (int)((I) >> BMSHIFT), (int)((I) & BMMASK)));		\
     99       1.88  perseant 	(F)->lfs_ino_bitmap[(I) >> BMSHIFT] |= (1 << ((I) & BMMASK));	\
    100       1.88  perseant } while (0)
    101       1.88  perseant #define CLR_BITMAP_FREE(F, I) do { \
    102       1.88  perseant 	DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d clr\n", (int)(I), 	\
    103       1.88  perseant 	     (int)((I) >> BMSHIFT), (int)((I) & BMMASK)));		\
    104       1.88  perseant 	(F)->lfs_ino_bitmap[(I) >> BMSHIFT] &= ~(1 << ((I) & BMMASK));	\
    105       1.88  perseant } while(0)
    106       1.88  perseant 
    107       1.87  perseant #define ISSET_BITMAP_FREE(F, I) \
    108       1.88  perseant 	((F)->lfs_ino_bitmap[(I) >> BMSHIFT] & (1 << ((I) & BMMASK)))
    109       1.87  perseant 
    110       1.44  perseant /*
    111       1.65  perseant  * Add a new block to the Ifile, to accommodate future file creations.
    112       1.65  perseant  * Called with the segment lock held.
    113       1.56  perseant  */
    114       1.96  perseant int
    115       1.96  perseant lfs_extend_ifile(struct lfs *fs, kauth_cred_t cred)
    116       1.44  perseant {
    117       1.44  perseant 	struct vnode *vp;
    118       1.44  perseant 	struct inode *ip;
    119  1.119.6.2     skrll 	IFILE64 *ifp64;
    120  1.119.6.2     skrll 	IFILE32 *ifp32;
    121       1.48  perseant 	IFILE_V1 *ifp_v1;
    122       1.48  perseant 	struct buf *bp, *cbp;
    123       1.44  perseant 	int error;
    124       1.83  christos 	daddr_t i, blkno, xmax;
    125       1.87  perseant 	ino_t oldlast, maxino;
    126       1.48  perseant 	CLEANERINFO *cip;
    127       1.44  perseant 
    128       1.78  perseant 	ASSERT_SEGLOCK(fs);
    129       1.78  perseant 
    130       1.44  perseant 	vp = fs->lfs_ivnode;
    131       1.44  perseant 	ip = VTOI(vp);
    132      1.117  christos 	blkno = lfs_lblkno(fs, ip->i_size);
    133  1.119.6.2     skrll 	if ((error = lfs_balloc(vp, ip->i_size, lfs_sb_getbsize(fs), cred, 0,
    134       1.44  perseant 				&bp)) != 0) {
    135       1.44  perseant 		return (error);
    136       1.44  perseant 	}
    137  1.119.6.2     skrll 	ip->i_size += lfs_sb_getbsize(fs);
    138  1.119.6.2     skrll 	lfs_dino_setsize(fs, ip->i_din, ip->i_size);
    139       1.66      fvdl 	uvm_vnp_setsize(vp, ip->i_size);
    140       1.75     perry 
    141  1.119.6.2     skrll 	maxino = ((ip->i_size >> lfs_sb_getbshift(fs)) - lfs_sb_getcleansz(fs) -
    142  1.119.6.2     skrll 		  lfs_sb_getsegtabsz(fs)) * lfs_sb_getifpb(fs);
    143       1.88  perseant 	fs->lfs_ino_bitmap = (lfs_bm_t *)
    144       1.88  perseant 		realloc(fs->lfs_ino_bitmap, ((maxino + BMMASK) >> BMSHIFT) *
    145       1.88  perseant 			sizeof(lfs_bm_t), M_SEGMENT, M_WAITOK);
    146       1.88  perseant 	KASSERT(fs->lfs_ino_bitmap != NULL);
    147       1.87  perseant 
    148  1.119.6.2     skrll 	i = (blkno - lfs_sb_getsegtabsz(fs) - lfs_sb_getcleansz(fs)) *
    149  1.119.6.2     skrll 		lfs_sb_getifpb(fs);
    150       1.87  perseant 
    151       1.87  perseant 	/*
    152       1.87  perseant 	 * We insert the new inodes at the head of the free list.
    153       1.87  perseant 	 * Under normal circumstances, the free list is empty here,
    154       1.87  perseant 	 * so we are also incidentally placing them at the end (which
    155       1.87  perseant 	 * we must do if we are to keep them in order).
    156       1.87  perseant 	 */
    157       1.48  perseant 	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
    158       1.48  perseant 	LFS_PUT_HEADFREE(fs, cip, cbp, i);
    159       1.44  perseant #ifdef DIAGNOSTIC
    160  1.119.6.2     skrll 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM)
    161       1.44  perseant 		panic("inode 0 allocated [2]");
    162       1.44  perseant #endif /* DIAGNOSTIC */
    163  1.119.6.2     skrll 	xmax = i + lfs_sb_getifpb(fs);
    164       1.48  perseant 
    165  1.119.6.2     skrll 	if (fs->lfs_is64) {
    166  1.119.6.2     skrll 		for (ifp64 = (IFILE64 *)bp->b_data; i < xmax; ++ifp64) {
    167  1.119.6.2     skrll 			SET_BITMAP_FREE(fs, i);
    168  1.119.6.2     skrll 			ifp64->if_version = 1;
    169  1.119.6.2     skrll 			ifp64->if_daddr = LFS_UNUSED_DADDR;
    170  1.119.6.2     skrll 			ifp64->if_nextfree = ++i;
    171  1.119.6.2     skrll 		}
    172  1.119.6.2     skrll 		ifp64--;
    173  1.119.6.2     skrll 		ifp64->if_nextfree = oldlast;
    174  1.119.6.2     skrll 	} else if (lfs_sb_getversion(fs) > 1) {
    175  1.119.6.2     skrll 		for (ifp32 = (IFILE32 *)bp->b_data; i < xmax; ++ifp32) {
    176  1.119.6.2     skrll 			SET_BITMAP_FREE(fs, i);
    177  1.119.6.2     skrll 			ifp32->if_version = 1;
    178  1.119.6.2     skrll 			ifp32->if_daddr = LFS_UNUSED_DADDR;
    179  1.119.6.2     skrll 			ifp32->if_nextfree = ++i;
    180  1.119.6.2     skrll 		}
    181  1.119.6.2     skrll 		ifp32--;
    182  1.119.6.2     skrll 		ifp32->if_nextfree = oldlast;
    183  1.119.6.2     skrll 	} else {
    184       1.83  christos 		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < xmax; ++ifp_v1) {
    185       1.89  perseant 			SET_BITMAP_FREE(fs, i);
    186       1.48  perseant 			ifp_v1->if_version = 1;
    187       1.48  perseant 			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
    188       1.48  perseant 			ifp_v1->if_nextfree = ++i;
    189       1.48  perseant 		}
    190       1.48  perseant 		ifp_v1--;
    191       1.48  perseant 		ifp_v1->if_nextfree = oldlast;
    192       1.44  perseant 	}
    193       1.83  christos 	LFS_PUT_TAILFREE(fs, cip, cbp, xmax - 1);
    194       1.48  perseant 
    195       1.56  perseant 	(void) LFS_BWRITE_LOG(bp); /* Ifile */
    196       1.44  perseant 
    197       1.44  perseant 	return 0;
    198       1.44  perseant }
    199       1.44  perseant 
    200        1.1   mycroft /* Allocate a new inode. */
    201        1.1   mycroft /* ARGSUSED */
    202       1.43  perseant /* VOP_BWRITE 2i times */
    203        1.1   mycroft int
    204       1.99  christos lfs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred,
    205  1.119.6.1     skrll     ino_t *ino, int *gen)
    206        1.3  christos {
    207        1.1   mycroft 	struct lfs *fs;
    208       1.48  perseant 	struct buf *bp, *cbp;
    209  1.119.6.2     skrll 	IFILE *ifp;
    210        1.1   mycroft 	int error;
    211       1.48  perseant 	CLEANERINFO *cip;
    212        1.1   mycroft 
    213       1.85      yamt 	fs = VTOI(pvp)->i_lfs;
    214       1.38  perseant 	if (fs->lfs_ronly)
    215       1.38  perseant 		return EROFS;
    216       1.75     perry 
    217       1.78  perseant 	ASSERT_NO_SEGLOCK(fs);
    218       1.78  perseant 
    219       1.56  perseant 	lfs_seglock(fs, SEGM_PROT);
    220       1.17  perseant 
    221        1.1   mycroft 	/* Get the head of the freelist. */
    222  1.119.6.1     skrll 	LFS_GET_HEADFREE(fs, cip, cbp, ino);
    223  1.119.6.1     skrll 	KASSERT(*ino != LFS_UNUSED_INUM && *ino != LFS_IFILE_INUM);
    224       1.48  perseant 
    225  1.119.6.1     skrll 	DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %" PRId64 "\n",
    226  1.119.6.1     skrll 	     *ino));
    227       1.75     perry 
    228        1.1   mycroft 	/*
    229        1.1   mycroft 	 * Remove the inode from the free list and write the new start
    230        1.1   mycroft 	 * of the free list into the superblock.
    231        1.1   mycroft 	 */
    232  1.119.6.1     skrll 	CLR_BITMAP_FREE(fs, *ino);
    233  1.119.6.1     skrll 	LFS_IENTRY(ifp, fs, *ino, bp);
    234  1.119.6.2     skrll 	if (lfs_if_getdaddr(fs, ifp) != LFS_UNUSED_DADDR)
    235  1.119.6.1     skrll 		panic("lfs_valloc: inuse inode %" PRId64 " on the free list",
    236  1.119.6.1     skrll 		    *ino);
    237  1.119.6.2     skrll 	LFS_PUT_HEADFREE(fs, cip, cbp, lfs_if_getnextfree(fs, ifp));
    238  1.119.6.2     skrll 	DLOG((DLOG_ALLOC, "lfs_valloc: headfree %" PRId64 " -> %ju\n",
    239  1.119.6.2     skrll 	     *ino, (uintmax_t)lfs_if_getnextfree(fs, ifp)));
    240       1.48  perseant 
    241  1.119.6.2     skrll 	/* version was updated by vfree */
    242  1.119.6.2     skrll 	*gen = lfs_if_getversion(fs, ifp);
    243      1.102        ad 	brelse(bp, 0);
    244       1.30  perseant 
    245        1.1   mycroft 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    246  1.119.6.2     skrll 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM) {
    247       1.96  perseant 		if ((error = lfs_extend_ifile(fs, cred)) != 0) {
    248  1.119.6.1     skrll 			LFS_PUT_HEADFREE(fs, cip, cbp, *ino);
    249       1.56  perseant 			lfs_segunlock(fs);
    250       1.44  perseant 			return error;
    251        1.1   mycroft 		}
    252        1.1   mycroft 	}
    253       1.17  perseant #ifdef DIAGNOSTIC
    254  1.119.6.2     skrll 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM)
    255       1.17  perseant 		panic("inode 0 allocated [3]");
    256       1.17  perseant #endif /* DIAGNOSTIC */
    257       1.48  perseant 
    258       1.78  perseant 	/* Set superblock modified bit and increment file count. */
    259      1.105        ad 	mutex_enter(&lfs_lock);
    260       1.78  perseant 	fs->lfs_fmod = 1;
    261      1.105        ad 	mutex_exit(&lfs_lock);
    262  1.119.6.2     skrll 	lfs_sb_addnfiles(fs, 1);
    263       1.78  perseant 
    264       1.56  perseant 	lfs_segunlock(fs);
    265       1.63  perseant 
    266  1.119.6.1     skrll 	return 0;
    267       1.44  perseant }
    268       1.44  perseant 
    269       1.65  perseant /*
    270  1.119.6.1     skrll  * Allocate a new inode with given inode number and version.
    271       1.65  perseant  */
    272       1.96  perseant int
    273  1.119.6.1     skrll lfs_valloc_fixed(struct lfs *fs, ino_t ino, int vers)
    274       1.44  perseant {
    275  1.119.6.1     skrll 	IFILE *ifp;
    276  1.119.6.1     skrll 	struct buf *bp, *cbp;
    277  1.119.6.2     skrll 	ino_t headino, thisino, oldnext;
    278  1.119.6.1     skrll 	CLEANERINFO *cip;
    279       1.79  perseant 
    280  1.119.6.1     skrll 	/* If the Ifile is too short to contain this inum, extend it */
    281  1.119.6.1     skrll 	while (VTOI(fs->lfs_ivnode)->i_size <= (ino /
    282  1.119.6.2     skrll 		lfs_sb_getifpb(fs) + lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs))
    283  1.119.6.2     skrll 		<< lfs_sb_getbshift(fs)) {
    284  1.119.6.1     skrll 		lfs_extend_ifile(fs, NOCRED);
    285       1.66      fvdl 	}
    286       1.51       chs 
    287  1.119.6.1     skrll 	LFS_IENTRY(ifp, fs, ino, bp);
    288  1.119.6.2     skrll 	oldnext = lfs_if_getnextfree(fs, ifp);
    289  1.119.6.2     skrll 	lfs_if_setversion(fs, ifp, vers);
    290  1.119.6.1     skrll 	brelse(bp, 0);
    291       1.75     perry 
    292  1.119.6.2     skrll 	LFS_GET_HEADFREE(fs, cip, cbp, &headino);
    293  1.119.6.2     skrll 	if (headino == ino) {
    294  1.119.6.1     skrll 		LFS_PUT_HEADFREE(fs, cip, cbp, oldnext);
    295  1.119.6.1     skrll 	} else {
    296  1.119.6.2     skrll 		ino_t nextfree;
    297  1.119.6.2     skrll 
    298  1.119.6.2     skrll 		thisino = headino;
    299  1.119.6.1     skrll 		while (1) {
    300  1.119.6.2     skrll 			LFS_IENTRY(ifp, fs, thisino, bp);
    301  1.119.6.2     skrll 			nextfree = lfs_if_getnextfree(fs, ifp);
    302  1.119.6.2     skrll 			if (nextfree == ino ||
    303  1.119.6.2     skrll 			    nextfree == LFS_UNUSED_INUM)
    304  1.119.6.1     skrll 				break;
    305  1.119.6.2     skrll 			thisino = nextfree;
    306  1.119.6.1     skrll 			brelse(bp, 0);
    307  1.119.6.1     skrll 		}
    308  1.119.6.2     skrll 		if (nextfree == LFS_UNUSED_INUM) {
    309  1.119.6.1     skrll 			brelse(bp, 0);
    310  1.119.6.1     skrll 			return ENOENT;
    311  1.119.6.1     skrll 		}
    312  1.119.6.2     skrll 		lfs_if_setnextfree(fs, ifp, oldnext);
    313  1.119.6.1     skrll 		LFS_BWRITE_LOG(bp);
    314  1.119.6.1     skrll 	}
    315       1.78  perseant 
    316  1.119.6.1     skrll 	return 0;
    317        1.1   mycroft }
    318        1.1   mycroft 
    319       1.87  perseant #if 0
    320       1.87  perseant /*
    321       1.87  perseant  * Find the highest-numbered allocated inode.
    322       1.87  perseant  * This will be used to shrink the Ifile.
    323       1.87  perseant  */
    324       1.87  perseant static inline ino_t
    325       1.87  perseant lfs_last_alloc_ino(struct lfs *fs)
    326       1.87  perseant {
    327       1.87  perseant 	ino_t ino, maxino;
    328       1.87  perseant 
    329  1.119.6.2     skrll 	maxino = ((fs->lfs_ivnode->v_size >> lfs_sb_getbshift(fs)) -
    330  1.119.6.3     skrll 		  lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) *
    331  1.119.6.3     skrll 		lfs_sb_getifpb(fs);
    332       1.87  perseant 	for (ino = maxino - 1; ino > LFS_UNUSED_INUM; --ino) {
    333       1.87  perseant 		if (ISSET_BITMAP_FREE(fs, ino) == 0)
    334       1.87  perseant 			break;
    335       1.87  perseant 	}
    336       1.87  perseant 	return ino;
    337       1.87  perseant }
    338       1.87  perseant #endif
    339       1.87  perseant 
    340       1.87  perseant /*
    341       1.87  perseant  * Find the previous (next lowest numbered) free inode, if any.
    342       1.87  perseant  * If there is none, return LFS_UNUSED_INUM.
    343       1.87  perseant  */
    344       1.87  perseant static inline ino_t
    345       1.87  perseant lfs_freelist_prev(struct lfs *fs, ino_t ino)
    346       1.87  perseant {
    347       1.88  perseant 	ino_t tino, bound, bb, freehdbb;
    348       1.88  perseant 
    349  1.119.6.2     skrll 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM)	 /* No free inodes at all */
    350       1.88  perseant 		return LFS_UNUSED_INUM;
    351       1.88  perseant 
    352       1.88  perseant 	/* Search our own word first */
    353       1.88  perseant 	bound = ino & ~BMMASK;
    354       1.89  perseant 	for (tino = ino - 1; tino >= bound && tino > LFS_UNUSED_INUM; tino--)
    355       1.88  perseant 		if (ISSET_BITMAP_FREE(fs, tino))
    356       1.88  perseant 			return tino;
    357       1.88  perseant 	/* If there are no lower words to search, just return */
    358       1.88  perseant 	if (ino >> BMSHIFT == 0)
    359       1.88  perseant 		return LFS_UNUSED_INUM;
    360       1.88  perseant 
    361       1.88  perseant 	/*
    362       1.88  perseant 	 * Find a word with a free inode in it.  We have to be a bit
    363       1.88  perseant 	 * careful here since ino_t is unsigned.
    364       1.88  perseant 	 */
    365  1.119.6.2     skrll 	freehdbb = (lfs_sb_getfreehd(fs) >> BMSHIFT);
    366       1.88  perseant 	for (bb = (ino >> BMSHIFT) - 1; bb >= freehdbb && bb > 0; --bb)
    367       1.88  perseant 		if (fs->lfs_ino_bitmap[bb])
    368       1.88  perseant 			break;
    369       1.88  perseant 	if (fs->lfs_ino_bitmap[bb] == 0)
    370       1.88  perseant 		return LFS_UNUSED_INUM;
    371       1.88  perseant 
    372       1.88  perseant 	/* Search the word we found */
    373       1.89  perseant 	for (tino = (bb << BMSHIFT) | BMMASK; tino >= (bb << BMSHIFT) &&
    374       1.89  perseant 	     tino > LFS_UNUSED_INUM; tino--)
    375       1.88  perseant 		if (ISSET_BITMAP_FREE(fs, tino))
    376       1.88  perseant 			break;
    377       1.87  perseant 
    378       1.87  perseant 	if (tino <= LFS_IFILE_INUM)
    379       1.87  perseant 		tino = LFS_UNUSED_INUM;
    380       1.87  perseant 
    381       1.87  perseant 	return tino;
    382       1.87  perseant }
    383       1.87  perseant 
    384        1.1   mycroft /* Free an inode. */
    385        1.1   mycroft /* ARGUSED */
    386       1.43  perseant /* VOP_BWRITE 2i times */
    387        1.1   mycroft int
    388       1.99  christos lfs_vfree(struct vnode *vp, ino_t ino, int mode)
    389        1.3  christos {
    390        1.1   mycroft 	SEGUSE *sup;
    391       1.48  perseant 	CLEANERINFO *cip;
    392       1.48  perseant 	struct buf *cbp, *bp;
    393  1.119.6.2     skrll 	IFILE *ifp;
    394        1.1   mycroft 	struct inode *ip;
    395        1.1   mycroft 	struct lfs *fs;
    396       1.59      fvdl 	daddr_t old_iaddr;
    397       1.85      yamt 	ino_t otail;
    398       1.75     perry 
    399        1.1   mycroft 	/* Get the inode number and file system. */
    400       1.30  perseant 	ip = VTOI(vp);
    401        1.1   mycroft 	fs = ip->i_lfs;
    402        1.1   mycroft 	ino = ip->i_number;
    403       1.34  perseant 
    404       1.78  perseant 	ASSERT_NO_SEGLOCK(fs);
    405       1.88  perseant 	DLOG((DLOG_ALLOC, "lfs_vfree: free ino %lld\n", (long long)ino));
    406       1.78  perseant 
    407       1.48  perseant 	/* Drain of pending writes */
    408      1.111     rmind 	mutex_enter(vp->v_interlock);
    409  1.119.6.2     skrll 	while (lfs_sb_getversion(fs) > 1 && WRITEINPROG(vp)) {
    410      1.111     rmind 		cv_wait(&vp->v_cv, vp->v_interlock);
    411      1.105        ad 	}
    412      1.111     rmind 	mutex_exit(vp->v_interlock);
    413       1.48  perseant 
    414       1.63  perseant 	lfs_seglock(fs, SEGM_PROT);
    415       1.75     perry 
    416       1.72      yamt 	lfs_unmark_vnode(vp);
    417      1.105        ad 	mutex_enter(&lfs_lock);
    418      1.103        ad 	if (vp->v_uflag & VU_DIROP) {
    419      1.103        ad 		vp->v_uflag &= ~VU_DIROP;
    420       1.30  perseant 		--lfs_dirvcount;
    421       1.92  perseant 		--fs->lfs_dirvcount;
    422       1.63  perseant 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    423       1.92  perseant 		wakeup(&fs->lfs_dirvcount);
    424       1.30  perseant 		wakeup(&lfs_dirvcount);
    425      1.105        ad 		mutex_exit(&lfs_lock);
    426  1.119.6.1     skrll 		vrele(vp);
    427       1.90  perseant 
    428       1.90  perseant 		/*
    429       1.90  perseant 		 * If this inode is not going to be written any more, any
    430       1.90  perseant 		 * segment accounting left over from its truncation needs
    431       1.90  perseant 		 * to occur at the end of the next dirops flush.  Attach
    432       1.90  perseant 		 * them to the fs-wide list for that purpose.
    433       1.90  perseant 		 */
    434       1.90  perseant 		if (LIST_FIRST(&ip->i_lfs_segdhd) != NULL) {
    435       1.90  perseant 			struct segdelta *sd;
    436  1.119.6.2     skrll 
    437       1.90  perseant 			while((sd = LIST_FIRST(&ip->i_lfs_segdhd)) != NULL) {
    438       1.90  perseant 				LIST_REMOVE(sd, list);
    439       1.90  perseant 				LIST_INSERT_HEAD(&fs->lfs_segdhd, sd, list);
    440       1.90  perseant 			}
    441       1.90  perseant 		}
    442       1.90  perseant 	} else {
    443       1.90  perseant 		/*
    444       1.90  perseant 		 * If it's not a dirop, we can finalize right away.
    445       1.90  perseant 		 */
    446      1.105        ad 		mutex_exit(&lfs_lock);
    447       1.90  perseant 		lfs_finalize_ino_seguse(fs, ip);
    448       1.38  perseant 	}
    449       1.30  perseant 
    450      1.105        ad 	mutex_enter(&lfs_lock);
    451       1.42  perseant 	LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
    452      1.105        ad 	mutex_exit(&lfs_lock);
    453       1.33  perseant 	ip->i_flag &= ~IN_ALLMOD;
    454       1.93  perseant 	ip->i_lfs_iflags |= LFSI_DELETED;
    455  1.119.6.2     skrll 
    456        1.1   mycroft 	/*
    457        1.1   mycroft 	 * Set the ifile's inode entry to unused, increment its version number
    458       1.48  perseant 	 * and link it onto the free chain.
    459        1.1   mycroft 	 */
    460       1.87  perseant 	SET_BITMAP_FREE(fs, ino);
    461        1.1   mycroft 	LFS_IENTRY(ifp, fs, ino, bp);
    462  1.119.6.2     skrll 	old_iaddr = lfs_if_getdaddr(fs, ifp);
    463  1.119.6.2     skrll 	lfs_if_setdaddr(fs, ifp, LFS_UNUSED_DADDR);
    464  1.119.6.2     skrll 	lfs_if_setversion(fs, ifp, lfs_if_getversion(fs, ifp) + 1);
    465  1.119.6.2     skrll 	if (lfs_sb_getversion(fs) == 1) {
    466  1.119.6.2     skrll 		ino_t nextfree;
    467  1.119.6.2     skrll 
    468  1.119.6.2     skrll 		LFS_GET_HEADFREE(fs, cip, cbp, &nextfree);
    469  1.119.6.2     skrll 		lfs_if_setnextfree(fs, ifp, nextfree);
    470       1.48  perseant 		LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    471       1.56  perseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    472       1.48  perseant 	} else {
    473       1.87  perseant 		ino_t tino, onf;
    474       1.87  perseant 
    475  1.119.6.2     skrll 		lfs_if_setnextfree(fs, ifp, LFS_UNUSED_INUM);
    476       1.56  perseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    477       1.87  perseant 
    478       1.87  perseant 		tino = lfs_freelist_prev(fs, ino);
    479       1.87  perseant 		if (tino == LFS_UNUSED_INUM) {
    480  1.119.6.2     skrll 			ino_t nextfree;
    481  1.119.6.2     skrll 
    482       1.87  perseant 			/* Nothing free below us, put us on the head */
    483       1.87  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    484  1.119.6.2     skrll 			LFS_GET_HEADFREE(fs, cip, cbp, &nextfree);
    485  1.119.6.2     skrll 			lfs_if_setnextfree(fs, ifp, nextfree);
    486       1.87  perseant 			LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    487       1.88  perseant 			DLOG((DLOG_ALLOC, "lfs_vfree: headfree %lld -> %lld\n",
    488  1.119.6.2     skrll 			     (long long)nextfree, (long long)ino));
    489       1.87  perseant 			LFS_BWRITE_LOG(bp); /* Ifile */
    490       1.87  perseant 
    491       1.87  perseant 			/* If the list was empty, set tail too */
    492       1.87  perseant 			LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    493       1.87  perseant 			if (otail == LFS_UNUSED_INUM) {
    494       1.87  perseant 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    495       1.87  perseant 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    496       1.87  perseant 				      "-> %lld\n", (long long)otail,
    497       1.87  perseant 				      (long long)ino));
    498       1.87  perseant 			}
    499       1.87  perseant 		} else {
    500       1.87  perseant 			/*
    501       1.87  perseant 			 * Insert this inode into the list after tino.
    502       1.87  perseant 			 * We hold the segment lock so we don't have to
    503       1.87  perseant 			 * worry about blocks being written out of order.
    504       1.87  perseant 			 */
    505       1.87  perseant 			DLOG((DLOG_ALLOC, "lfs_vfree: insert ino %lld "
    506       1.87  perseant 			      " after %lld\n", ino, tino));
    507       1.87  perseant 
    508       1.87  perseant 			LFS_IENTRY(ifp, fs, tino, bp);
    509  1.119.6.2     skrll 			onf = lfs_if_getnextfree(fs, ifp);
    510  1.119.6.2     skrll 			lfs_if_setnextfree(fs, ifp, ino);
    511       1.87  perseant 			LFS_BWRITE_LOG(bp);	/* Ifile */
    512       1.87  perseant 
    513       1.87  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    514  1.119.6.2     skrll 			lfs_if_setnextfree(fs, ifp, onf);
    515       1.87  perseant 			LFS_BWRITE_LOG(bp);	/* Ifile */
    516       1.87  perseant 
    517       1.87  perseant 			/* If we're last, put us on the tail */
    518       1.87  perseant 			if (onf == LFS_UNUSED_INUM) {
    519       1.87  perseant 				LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    520       1.87  perseant 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    521       1.87  perseant 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    522       1.87  perseant 				      "-> %lld\n", (long long)otail,
    523       1.87  perseant 				      (long long)ino));
    524       1.87  perseant 			}
    525       1.87  perseant 		}
    526       1.48  perseant 	}
    527       1.17  perseant #ifdef DIAGNOSTIC
    528       1.53       chs 	if (ino == LFS_UNUSED_INUM) {
    529       1.17  perseant 		panic("inode 0 freed");
    530       1.17  perseant 	}
    531       1.17  perseant #endif /* DIAGNOSTIC */
    532        1.1   mycroft 	if (old_iaddr != LFS_UNUSED_DADDR) {
    533      1.117  christos 		LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, old_iaddr), bp);
    534       1.22  perseant #ifdef DIAGNOSTIC
    535  1.119.6.2     skrll 		if (sup->su_nbytes < DINOSIZE(fs)) {
    536       1.43  perseant 			printf("lfs_vfree: negative byte count"
    537       1.62      yamt 			       " (segment %" PRIu32 " short by %d)\n",
    538      1.117  christos 			       lfs_dtosn(fs, old_iaddr),
    539  1.119.6.2     skrll 			       (int)DINOSIZE(fs) -
    540       1.66      fvdl 				    sup->su_nbytes);
    541       1.22  perseant 			panic("lfs_vfree: negative byte count");
    542  1.119.6.2     skrll 			sup->su_nbytes = DINOSIZE(fs);
    543       1.17  perseant 		}
    544       1.22  perseant #endif
    545  1.119.6.2     skrll 		sup->su_nbytes -= DINOSIZE(fs);
    546      1.117  christos 		LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, old_iaddr), bp); /* Ifile */
    547        1.1   mycroft 	}
    548       1.75     perry 
    549        1.1   mycroft 	/* Set superblock modified bit and decrement file count. */
    550      1.105        ad 	mutex_enter(&lfs_lock);
    551        1.1   mycroft 	fs->lfs_fmod = 1;
    552      1.105        ad 	mutex_exit(&lfs_lock);
    553  1.119.6.2     skrll 	lfs_sb_subnfiles(fs, 1);
    554       1.75     perry 
    555       1.56  perseant 	lfs_segunlock(fs);
    556       1.63  perseant 
    557        1.1   mycroft 	return (0);
    558        1.1   mycroft }
    559       1.87  perseant 
    560       1.87  perseant /*
    561       1.87  perseant  * Sort the freelist and set up the free-inode bitmap.
    562       1.87  perseant  * To be called by lfs_mountfs().
    563       1.87  perseant  */
    564       1.87  perseant void
    565       1.87  perseant lfs_order_freelist(struct lfs *fs)
    566       1.87  perseant {
    567       1.87  perseant 	CLEANERINFO *cip;
    568       1.87  perseant 	IFILE *ifp = NULL;
    569       1.87  perseant 	struct buf *bp;
    570       1.87  perseant 	ino_t ino, firstino, lastino, maxino;
    571       1.97  perseant #ifdef notyet
    572       1.97  perseant 	struct vnode *vp;
    573       1.97  perseant #endif
    574  1.119.6.2     skrll 
    575       1.95  perseant 	ASSERT_NO_SEGLOCK(fs);
    576       1.95  perseant 	lfs_seglock(fs, SEGM_PROT);
    577       1.95  perseant 
    578  1.119.6.2     skrll 	maxino = ((fs->lfs_ivnode->v_size >> lfs_sb_getbshift(fs)) -
    579  1.119.6.2     skrll 		  lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) * lfs_sb_getifpb(fs);
    580  1.119.6.2     skrll 	fs->lfs_ino_bitmap =
    581       1.88  perseant 		malloc(((maxino + BMMASK) >> BMSHIFT) * sizeof(lfs_bm_t),
    582       1.88  perseant 		       M_SEGMENT, M_WAITOK | M_ZERO);
    583       1.88  perseant 	KASSERT(fs->lfs_ino_bitmap != NULL);
    584       1.87  perseant 
    585       1.87  perseant 	firstino = lastino = LFS_UNUSED_INUM;
    586       1.87  perseant 	for (ino = 0; ino < maxino; ino++) {
    587  1.119.6.2     skrll 		if (ino % lfs_sb_getifpb(fs) == 0)
    588       1.87  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    589       1.87  perseant 		else
    590  1.119.6.2     skrll 			LFS_IENTRY_NEXT(ifp, fs);
    591       1.87  perseant 
    592       1.87  perseant 		/* Don't put zero or ifile on the free list */
    593       1.87  perseant 		if (ino == LFS_UNUSED_INUM || ino == LFS_IFILE_INUM)
    594       1.87  perseant 			continue;
    595       1.87  perseant 
    596       1.97  perseant #ifdef notyet
    597       1.97  perseant 		/* Address orphaned files */
    598  1.119.6.2     skrll 		if (lfs_if_getnextfree(fs, ifp) == LFS_ORPHAN_NEXTFREE &&
    599       1.97  perseant 		    VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp) == 0) {
    600  1.119.6.2     skrll 			unsigned segno;
    601  1.119.6.2     skrll 
    602  1.119.6.2     skrll 			segno = lfs_dtosn(fs, lfs_if_getdaddr(fs, ifp));
    603      1.104        he 			lfs_truncate(vp, 0, 0, NOCRED);
    604       1.97  perseant 			vput(vp);
    605  1.119.6.2     skrll 			LFS_SEGENTRY(sup, fs, segno, bp);
    606  1.119.6.3     skrll 			KASSERT(sup->su_nbytes >= DINOSIZE(fs));
    607  1.119.6.3     skrll 			sup->su_nbytes -= DINOSIZE(fs);
    608  1.119.6.2     skrll 			LFS_WRITESEGENTRY(sup, fs, segno, bp);
    609       1.97  perseant 
    610       1.97  perseant 			/* Set up to fall through to next section */
    611  1.119.6.2     skrll 			lfs_if_setdaddr(fs, ifp, LFS_UNUSED_DADDR);
    612       1.97  perseant 			LFS_BWRITE_LOG(bp);
    613       1.97  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    614       1.97  perseant 		}
    615       1.97  perseant #endif
    616       1.97  perseant 
    617  1.119.6.2     skrll 		if (lfs_if_getdaddr(fs, ifp) == LFS_UNUSED_DADDR) {
    618       1.87  perseant 			if (firstino == LFS_UNUSED_INUM)
    619       1.87  perseant 				firstino = ino;
    620       1.87  perseant 			else {
    621      1.102        ad 				brelse(bp, 0);
    622       1.87  perseant 
    623       1.87  perseant 				LFS_IENTRY(ifp, fs, lastino, bp);
    624  1.119.6.2     skrll 				lfs_if_setnextfree(fs, ifp, ino);
    625       1.87  perseant 				LFS_BWRITE_LOG(bp);
    626  1.119.6.2     skrll 
    627       1.87  perseant 				LFS_IENTRY(ifp, fs, ino, bp);
    628       1.87  perseant 			}
    629       1.87  perseant 			lastino = ino;
    630       1.87  perseant 
    631       1.87  perseant 			SET_BITMAP_FREE(fs, ino);
    632       1.87  perseant 		}
    633       1.87  perseant 
    634  1.119.6.2     skrll 		if ((ino + 1) % lfs_sb_getifpb(fs) == 0)
    635      1.102        ad 			brelse(bp, 0);
    636       1.87  perseant 	}
    637       1.87  perseant 
    638       1.87  perseant 	LFS_PUT_HEADFREE(fs, cip, bp, firstino);
    639       1.87  perseant 	LFS_PUT_TAILFREE(fs, cip, bp, lastino);
    640       1.95  perseant 
    641       1.95  perseant 	lfs_segunlock(fs);
    642       1.87  perseant }
    643       1.97  perseant 
    644       1.97  perseant void
    645       1.97  perseant lfs_orphan(struct lfs *fs, ino_t ino)
    646       1.97  perseant {
    647       1.97  perseant 	IFILE *ifp;
    648       1.97  perseant 	struct buf *bp;
    649       1.97  perseant 
    650       1.97  perseant 	LFS_IENTRY(ifp, fs, ino, bp);
    651  1.119.6.2     skrll 	lfs_if_setnextfree(fs, ifp, LFS_ORPHAN_NEXTFREE);
    652       1.97  perseant 	LFS_BWRITE_LOG(bp);
    653       1.97  perseant }
    654