Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.125
      1  1.125  dholland /*	$NetBSD: lfs_alloc.c,v 1.125 2015/08/02 18:14:16 dholland 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.125  dholland __KERNEL_RCSID(0, "$NetBSD: lfs_alloc.c,v 1.125 2015/08/02 18:14:16 dholland 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.124  dholland #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.44  perseant 	IFILE *ifp;
    120   1.48  perseant 	IFILE_V1 *ifp_v1;
    121   1.48  perseant 	struct buf *bp, *cbp;
    122   1.44  perseant 	int error;
    123   1.83  christos 	daddr_t i, blkno, xmax;
    124   1.87  perseant 	ino_t oldlast, maxino;
    125   1.48  perseant 	CLEANERINFO *cip;
    126   1.44  perseant 
    127   1.78  perseant 	ASSERT_SEGLOCK(fs);
    128   1.78  perseant 
    129   1.44  perseant 	vp = fs->lfs_ivnode;
    130   1.44  perseant 	ip = VTOI(vp);
    131  1.117  christos 	blkno = lfs_lblkno(fs, ip->i_size);
    132  1.122  dholland 	if ((error = lfs_balloc(vp, ip->i_size, lfs_sb_getbsize(fs), cred, 0,
    133   1.44  perseant 				&bp)) != 0) {
    134   1.44  perseant 		return (error);
    135   1.44  perseant 	}
    136  1.122  dholland 	ip->i_size += lfs_sb_getbsize(fs);
    137   1.66      fvdl 	ip->i_ffs1_size = ip->i_size;
    138   1.66      fvdl 	uvm_vnp_setsize(vp, ip->i_size);
    139   1.75     perry 
    140  1.123  dholland 	maxino = ((ip->i_size >> lfs_sb_getbshift(fs)) - lfs_sb_getcleansz(fs) -
    141  1.122  dholland 		  lfs_sb_getsegtabsz(fs)) * lfs_sb_getifpb(fs);
    142   1.88  perseant 	fs->lfs_ino_bitmap = (lfs_bm_t *)
    143   1.88  perseant 		realloc(fs->lfs_ino_bitmap, ((maxino + BMMASK) >> BMSHIFT) *
    144   1.88  perseant 			sizeof(lfs_bm_t), M_SEGMENT, M_WAITOK);
    145   1.88  perseant 	KASSERT(fs->lfs_ino_bitmap != NULL);
    146   1.87  perseant 
    147  1.122  dholland 	i = (blkno - lfs_sb_getsegtabsz(fs) - lfs_sb_getcleansz(fs)) *
    148  1.122  dholland 		lfs_sb_getifpb(fs);
    149   1.87  perseant 
    150   1.87  perseant 	/*
    151   1.87  perseant 	 * We insert the new inodes at the head of the free list.
    152   1.87  perseant 	 * Under normal circumstances, the free list is empty here,
    153   1.87  perseant 	 * so we are also incidentally placing them at the end (which
    154   1.87  perseant 	 * we must do if we are to keep them in order).
    155   1.87  perseant 	 */
    156   1.48  perseant 	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
    157   1.48  perseant 	LFS_PUT_HEADFREE(fs, cip, cbp, i);
    158   1.44  perseant #ifdef DIAGNOSTIC
    159  1.122  dholland 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM)
    160   1.44  perseant 		panic("inode 0 allocated [2]");
    161   1.44  perseant #endif /* DIAGNOSTIC */
    162  1.122  dholland 	xmax = i + lfs_sb_getifpb(fs);
    163   1.48  perseant 
    164  1.125  dholland 	if (lfs_sb_getversion(fs) == 1) {
    165   1.83  christos 		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < xmax; ++ifp_v1) {
    166   1.89  perseant 			SET_BITMAP_FREE(fs, i);
    167   1.48  perseant 			ifp_v1->if_version = 1;
    168   1.48  perseant 			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
    169   1.48  perseant 			ifp_v1->if_nextfree = ++i;
    170   1.48  perseant 		}
    171   1.48  perseant 		ifp_v1--;
    172   1.48  perseant 		ifp_v1->if_nextfree = oldlast;
    173   1.48  perseant 	} else {
    174   1.83  christos 		for (ifp = (IFILE *)bp->b_data; i < xmax; ++ifp) {
    175   1.89  perseant 			SET_BITMAP_FREE(fs, i);
    176   1.48  perseant 			ifp->if_version = 1;
    177   1.48  perseant 			ifp->if_daddr = LFS_UNUSED_DADDR;
    178   1.48  perseant 			ifp->if_nextfree = ++i;
    179   1.48  perseant 		}
    180   1.48  perseant 		ifp--;
    181   1.48  perseant 		ifp->if_nextfree = oldlast;
    182   1.44  perseant 	}
    183   1.83  christos 	LFS_PUT_TAILFREE(fs, cip, cbp, xmax - 1);
    184   1.48  perseant 
    185   1.56  perseant 	(void) LFS_BWRITE_LOG(bp); /* Ifile */
    186   1.44  perseant 
    187   1.44  perseant 	return 0;
    188   1.44  perseant }
    189   1.44  perseant 
    190    1.1   mycroft /* Allocate a new inode. */
    191    1.1   mycroft /* ARGSUSED */
    192   1.43  perseant /* VOP_BWRITE 2i times */
    193    1.1   mycroft int
    194   1.99  christos lfs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred,
    195  1.120   hannken     ino_t *ino, int *gen)
    196    1.3  christos {
    197    1.1   mycroft 	struct lfs *fs;
    198   1.48  perseant 	struct buf *bp, *cbp;
    199    1.1   mycroft 	struct ifile *ifp;
    200    1.1   mycroft 	int error;
    201   1.48  perseant 	CLEANERINFO *cip;
    202    1.1   mycroft 
    203   1.85      yamt 	fs = VTOI(pvp)->i_lfs;
    204   1.38  perseant 	if (fs->lfs_ronly)
    205   1.38  perseant 		return EROFS;
    206   1.75     perry 
    207   1.78  perseant 	ASSERT_NO_SEGLOCK(fs);
    208   1.78  perseant 
    209   1.56  perseant 	lfs_seglock(fs, SEGM_PROT);
    210   1.17  perseant 
    211    1.1   mycroft 	/* Get the head of the freelist. */
    212  1.120   hannken 	LFS_GET_HEADFREE(fs, cip, cbp, ino);
    213  1.120   hannken 	KASSERT(*ino != LFS_UNUSED_INUM && *ino != LFS_IFILE_INUM);
    214   1.48  perseant 
    215  1.120   hannken 	DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %" PRId64 "\n",
    216  1.120   hannken 	     *ino));
    217   1.75     perry 
    218    1.1   mycroft 	/*
    219    1.1   mycroft 	 * Remove the inode from the free list and write the new start
    220    1.1   mycroft 	 * of the free list into the superblock.
    221    1.1   mycroft 	 */
    222  1.120   hannken 	CLR_BITMAP_FREE(fs, *ino);
    223  1.120   hannken 	LFS_IENTRY(ifp, fs, *ino, bp);
    224    1.1   mycroft 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
    225  1.120   hannken 		panic("lfs_valloc: inuse inode %" PRId64 " on the free list",
    226  1.120   hannken 		    *ino);
    227   1.48  perseant 	LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
    228  1.120   hannken 	DLOG((DLOG_ALLOC, "lfs_valloc: headfree %" PRId64 " -> %u\n",
    229  1.120   hannken 	     *ino, ifp->if_nextfree));
    230   1.48  perseant 
    231  1.120   hannken 	*gen = ifp->if_version; /* version was updated by vfree */
    232  1.102        ad 	brelse(bp, 0);
    233   1.30  perseant 
    234    1.1   mycroft 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    235  1.122  dholland 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM) {
    236   1.96  perseant 		if ((error = lfs_extend_ifile(fs, cred)) != 0) {
    237  1.120   hannken 			LFS_PUT_HEADFREE(fs, cip, cbp, *ino);
    238   1.56  perseant 			lfs_segunlock(fs);
    239   1.44  perseant 			return error;
    240    1.1   mycroft 		}
    241    1.1   mycroft 	}
    242   1.17  perseant #ifdef DIAGNOSTIC
    243  1.122  dholland 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM)
    244   1.17  perseant 		panic("inode 0 allocated [3]");
    245   1.17  perseant #endif /* DIAGNOSTIC */
    246   1.48  perseant 
    247   1.78  perseant 	/* Set superblock modified bit and increment file count. */
    248  1.105        ad 	mutex_enter(&lfs_lock);
    249   1.78  perseant 	fs->lfs_fmod = 1;
    250  1.105        ad 	mutex_exit(&lfs_lock);
    251  1.122  dholland 	lfs_sb_addnfiles(fs, 1);
    252   1.78  perseant 
    253   1.56  perseant 	lfs_segunlock(fs);
    254   1.63  perseant 
    255  1.120   hannken 	return 0;
    256   1.44  perseant }
    257   1.44  perseant 
    258   1.65  perseant /*
    259  1.120   hannken  * Allocate a new inode with given inode number and version.
    260   1.65  perseant  */
    261   1.96  perseant int
    262  1.120   hannken lfs_valloc_fixed(struct lfs *fs, ino_t ino, int vers)
    263   1.44  perseant {
    264  1.120   hannken 	IFILE *ifp;
    265  1.120   hannken 	struct buf *bp, *cbp;
    266  1.120   hannken 	ino_t tino, oldnext;
    267  1.120   hannken 	CLEANERINFO *cip;
    268   1.40      fvdl 
    269  1.120   hannken 	/* If the Ifile is too short to contain this inum, extend it */
    270  1.120   hannken 	while (VTOI(fs->lfs_ivnode)->i_size <= (ino /
    271  1.122  dholland 		lfs_sb_getifpb(fs) + lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs))
    272  1.123  dholland 		<< lfs_sb_getbshift(fs)) {
    273  1.120   hannken 		lfs_extend_ifile(fs, NOCRED);
    274  1.120   hannken 	}
    275   1.78  perseant 
    276  1.120   hannken 	LFS_IENTRY(ifp, fs, ino, bp);
    277  1.120   hannken 	oldnext = ifp->if_nextfree;
    278  1.120   hannken 	ifp->if_version = vers;
    279  1.120   hannken 	brelse(bp, 0);
    280   1.51       chs 
    281  1.120   hannken 	LFS_GET_HEADFREE(fs, cip, cbp, &ino);
    282  1.120   hannken 	if (ino) {
    283  1.120   hannken 		LFS_PUT_HEADFREE(fs, cip, cbp, oldnext);
    284  1.120   hannken 	} else {
    285  1.120   hannken 		tino = ino;
    286  1.120   hannken 		while (1) {
    287  1.120   hannken 			LFS_IENTRY(ifp, fs, tino, bp);
    288  1.120   hannken 			if (ifp->if_nextfree == ino ||
    289  1.120   hannken 			    ifp->if_nextfree == LFS_UNUSED_INUM)
    290  1.120   hannken 				break;
    291  1.120   hannken 			tino = ifp->if_nextfree;
    292  1.120   hannken 			brelse(bp, 0);
    293  1.120   hannken 		}
    294  1.120   hannken 		if (ifp->if_nextfree == LFS_UNUSED_INUM) {
    295  1.120   hannken 			brelse(bp, 0);
    296  1.120   hannken 			return ENOENT;
    297  1.120   hannken 		}
    298  1.120   hannken 		ifp->if_nextfree = oldnext;
    299  1.120   hannken 		LFS_BWRITE_LOG(bp);
    300   1.66      fvdl 	}
    301   1.51       chs 
    302  1.120   hannken 	return 0;
    303    1.1   mycroft }
    304    1.1   mycroft 
    305   1.87  perseant #if 0
    306   1.87  perseant /*
    307   1.87  perseant  * Find the highest-numbered allocated inode.
    308   1.87  perseant  * This will be used to shrink the Ifile.
    309   1.87  perseant  */
    310   1.87  perseant static inline ino_t
    311   1.87  perseant lfs_last_alloc_ino(struct lfs *fs)
    312   1.87  perseant {
    313   1.87  perseant 	ino_t ino, maxino;
    314   1.87  perseant 
    315  1.123  dholland 	maxino = ((fs->lfs_ivnode->v_size >> lfs_sb_getbshift(fs)) -
    316  1.122  dholland 		  lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) * fs->lfs_ifpb;
    317   1.87  perseant 	for (ino = maxino - 1; ino > LFS_UNUSED_INUM; --ino) {
    318   1.87  perseant 		if (ISSET_BITMAP_FREE(fs, ino) == 0)
    319   1.87  perseant 			break;
    320   1.87  perseant 	}
    321   1.87  perseant 	return ino;
    322   1.87  perseant }
    323   1.87  perseant #endif
    324   1.87  perseant 
    325   1.87  perseant /*
    326   1.87  perseant  * Find the previous (next lowest numbered) free inode, if any.
    327   1.87  perseant  * If there is none, return LFS_UNUSED_INUM.
    328   1.87  perseant  */
    329   1.87  perseant static inline ino_t
    330   1.87  perseant lfs_freelist_prev(struct lfs *fs, ino_t ino)
    331   1.87  perseant {
    332   1.88  perseant 	ino_t tino, bound, bb, freehdbb;
    333   1.88  perseant 
    334  1.122  dholland 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM)	 /* No free inodes at all */
    335   1.88  perseant 		return LFS_UNUSED_INUM;
    336   1.88  perseant 
    337   1.88  perseant 	/* Search our own word first */
    338   1.88  perseant 	bound = ino & ~BMMASK;
    339   1.89  perseant 	for (tino = ino - 1; tino >= bound && tino > LFS_UNUSED_INUM; tino--)
    340   1.88  perseant 		if (ISSET_BITMAP_FREE(fs, tino))
    341   1.88  perseant 			return tino;
    342   1.88  perseant 	/* If there are no lower words to search, just return */
    343   1.88  perseant 	if (ino >> BMSHIFT == 0)
    344   1.88  perseant 		return LFS_UNUSED_INUM;
    345   1.88  perseant 
    346   1.88  perseant 	/*
    347   1.88  perseant 	 * Find a word with a free inode in it.  We have to be a bit
    348   1.88  perseant 	 * careful here since ino_t is unsigned.
    349   1.88  perseant 	 */
    350  1.122  dholland 	freehdbb = (lfs_sb_getfreehd(fs) >> BMSHIFT);
    351   1.88  perseant 	for (bb = (ino >> BMSHIFT) - 1; bb >= freehdbb && bb > 0; --bb)
    352   1.88  perseant 		if (fs->lfs_ino_bitmap[bb])
    353   1.88  perseant 			break;
    354   1.88  perseant 	if (fs->lfs_ino_bitmap[bb] == 0)
    355   1.88  perseant 		return LFS_UNUSED_INUM;
    356   1.88  perseant 
    357   1.88  perseant 	/* Search the word we found */
    358   1.89  perseant 	for (tino = (bb << BMSHIFT) | BMMASK; tino >= (bb << BMSHIFT) &&
    359   1.89  perseant 	     tino > LFS_UNUSED_INUM; tino--)
    360   1.88  perseant 		if (ISSET_BITMAP_FREE(fs, tino))
    361   1.88  perseant 			break;
    362   1.87  perseant 
    363   1.87  perseant 	if (tino <= LFS_IFILE_INUM)
    364   1.87  perseant 		tino = LFS_UNUSED_INUM;
    365   1.87  perseant 
    366   1.87  perseant 	return tino;
    367   1.87  perseant }
    368   1.87  perseant 
    369    1.1   mycroft /* Free an inode. */
    370    1.1   mycroft /* ARGUSED */
    371   1.43  perseant /* VOP_BWRITE 2i times */
    372    1.1   mycroft int
    373   1.99  christos lfs_vfree(struct vnode *vp, ino_t ino, int mode)
    374    1.3  christos {
    375    1.1   mycroft 	SEGUSE *sup;
    376   1.48  perseant 	CLEANERINFO *cip;
    377   1.48  perseant 	struct buf *cbp, *bp;
    378    1.1   mycroft 	struct ifile *ifp;
    379    1.1   mycroft 	struct inode *ip;
    380    1.1   mycroft 	struct lfs *fs;
    381   1.59      fvdl 	daddr_t old_iaddr;
    382   1.85      yamt 	ino_t otail;
    383   1.75     perry 
    384    1.1   mycroft 	/* Get the inode number and file system. */
    385   1.30  perseant 	ip = VTOI(vp);
    386    1.1   mycroft 	fs = ip->i_lfs;
    387    1.1   mycroft 	ino = ip->i_number;
    388   1.34  perseant 
    389   1.78  perseant 	ASSERT_NO_SEGLOCK(fs);
    390   1.88  perseant 	DLOG((DLOG_ALLOC, "lfs_vfree: free ino %lld\n", (long long)ino));
    391   1.78  perseant 
    392   1.48  perseant 	/* Drain of pending writes */
    393  1.111     rmind 	mutex_enter(vp->v_interlock);
    394  1.125  dholland 	while (lfs_sb_getversion(fs) > 1 && WRITEINPROG(vp)) {
    395  1.111     rmind 		cv_wait(&vp->v_cv, vp->v_interlock);
    396  1.105        ad 	}
    397  1.111     rmind 	mutex_exit(vp->v_interlock);
    398   1.48  perseant 
    399   1.63  perseant 	lfs_seglock(fs, SEGM_PROT);
    400   1.75     perry 
    401   1.72      yamt 	lfs_unmark_vnode(vp);
    402  1.105        ad 	mutex_enter(&lfs_lock);
    403  1.103        ad 	if (vp->v_uflag & VU_DIROP) {
    404  1.103        ad 		vp->v_uflag &= ~VU_DIROP;
    405   1.30  perseant 		--lfs_dirvcount;
    406   1.92  perseant 		--fs->lfs_dirvcount;
    407   1.63  perseant 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    408   1.92  perseant 		wakeup(&fs->lfs_dirvcount);
    409   1.30  perseant 		wakeup(&lfs_dirvcount);
    410  1.105        ad 		mutex_exit(&lfs_lock);
    411  1.120   hannken 		vrele(vp);
    412   1.90  perseant 
    413   1.90  perseant 		/*
    414   1.90  perseant 		 * If this inode is not going to be written any more, any
    415   1.90  perseant 		 * segment accounting left over from its truncation needs
    416   1.90  perseant 		 * to occur at the end of the next dirops flush.  Attach
    417   1.90  perseant 		 * them to the fs-wide list for that purpose.
    418   1.90  perseant 		 */
    419   1.90  perseant 		if (LIST_FIRST(&ip->i_lfs_segdhd) != NULL) {
    420   1.90  perseant 			struct segdelta *sd;
    421   1.90  perseant 
    422   1.90  perseant 			while((sd = LIST_FIRST(&ip->i_lfs_segdhd)) != NULL) {
    423   1.90  perseant 				LIST_REMOVE(sd, list);
    424   1.90  perseant 				LIST_INSERT_HEAD(&fs->lfs_segdhd, sd, list);
    425   1.90  perseant 			}
    426   1.90  perseant 		}
    427   1.90  perseant 	} else {
    428   1.90  perseant 		/*
    429   1.90  perseant 		 * If it's not a dirop, we can finalize right away.
    430   1.90  perseant 		 */
    431  1.105        ad 		mutex_exit(&lfs_lock);
    432   1.90  perseant 		lfs_finalize_ino_seguse(fs, ip);
    433   1.38  perseant 	}
    434   1.30  perseant 
    435  1.105        ad 	mutex_enter(&lfs_lock);
    436   1.42  perseant 	LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
    437  1.105        ad 	mutex_exit(&lfs_lock);
    438   1.33  perseant 	ip->i_flag &= ~IN_ALLMOD;
    439   1.93  perseant 	ip->i_lfs_iflags |= LFSI_DELETED;
    440   1.93  perseant 
    441    1.1   mycroft 	/*
    442    1.1   mycroft 	 * Set the ifile's inode entry to unused, increment its version number
    443   1.48  perseant 	 * and link it onto the free chain.
    444    1.1   mycroft 	 */
    445   1.87  perseant 	SET_BITMAP_FREE(fs, ino);
    446    1.1   mycroft 	LFS_IENTRY(ifp, fs, ino, bp);
    447    1.1   mycroft 	old_iaddr = ifp->if_daddr;
    448    1.1   mycroft 	ifp->if_daddr = LFS_UNUSED_DADDR;
    449    1.1   mycroft 	++ifp->if_version;
    450  1.125  dholland 	if (lfs_sb_getversion(fs) == 1) {
    451   1.48  perseant 		LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    452   1.48  perseant 		LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    453   1.56  perseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    454   1.48  perseant 	} else {
    455   1.87  perseant 		ino_t tino, onf;
    456   1.87  perseant 
    457   1.48  perseant 		ifp->if_nextfree = LFS_UNUSED_INUM;
    458   1.56  perseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    459   1.87  perseant 
    460   1.87  perseant 		tino = lfs_freelist_prev(fs, ino);
    461   1.87  perseant 		if (tino == LFS_UNUSED_INUM) {
    462   1.87  perseant 			/* Nothing free below us, put us on the head */
    463   1.87  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    464   1.87  perseant 			LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    465   1.87  perseant 			LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    466   1.88  perseant 			DLOG((DLOG_ALLOC, "lfs_vfree: headfree %lld -> %lld\n",
    467   1.87  perseant 			     (long long)ifp->if_nextfree, (long long)ino));
    468   1.87  perseant 			LFS_BWRITE_LOG(bp); /* Ifile */
    469   1.87  perseant 
    470   1.87  perseant 			/* If the list was empty, set tail too */
    471   1.87  perseant 			LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    472   1.87  perseant 			if (otail == LFS_UNUSED_INUM) {
    473   1.87  perseant 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    474   1.87  perseant 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    475   1.87  perseant 				      "-> %lld\n", (long long)otail,
    476   1.87  perseant 				      (long long)ino));
    477   1.87  perseant 			}
    478   1.87  perseant 		} else {
    479   1.87  perseant 			/*
    480   1.87  perseant 			 * Insert this inode into the list after tino.
    481   1.87  perseant 			 * We hold the segment lock so we don't have to
    482   1.87  perseant 			 * worry about blocks being written out of order.
    483   1.87  perseant 			 */
    484   1.87  perseant 			DLOG((DLOG_ALLOC, "lfs_vfree: insert ino %lld "
    485   1.87  perseant 			      " after %lld\n", ino, tino));
    486   1.87  perseant 
    487   1.87  perseant 			LFS_IENTRY(ifp, fs, tino, bp);
    488   1.87  perseant 			onf = ifp->if_nextfree;
    489   1.87  perseant 			ifp->if_nextfree = ino;
    490   1.87  perseant 			LFS_BWRITE_LOG(bp);	/* Ifile */
    491   1.87  perseant 
    492   1.87  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    493   1.87  perseant 			ifp->if_nextfree = onf;
    494   1.87  perseant 			LFS_BWRITE_LOG(bp);	/* Ifile */
    495   1.87  perseant 
    496   1.87  perseant 			/* If we're last, put us on the tail */
    497   1.87  perseant 			if (onf == LFS_UNUSED_INUM) {
    498   1.87  perseant 				LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    499   1.87  perseant 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    500   1.87  perseant 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    501   1.87  perseant 				      "-> %lld\n", (long long)otail,
    502   1.87  perseant 				      (long long)ino));
    503   1.87  perseant 			}
    504   1.87  perseant 		}
    505   1.48  perseant 	}
    506   1.17  perseant #ifdef DIAGNOSTIC
    507   1.53       chs 	if (ino == LFS_UNUSED_INUM) {
    508   1.17  perseant 		panic("inode 0 freed");
    509   1.17  perseant 	}
    510   1.17  perseant #endif /* DIAGNOSTIC */
    511    1.1   mycroft 	if (old_iaddr != LFS_UNUSED_DADDR) {
    512  1.117  christos 		LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, old_iaddr), bp);
    513   1.22  perseant #ifdef DIAGNOSTIC
    514  1.116  dholland 		if (sup->su_nbytes < sizeof (struct ulfs1_dinode)) {
    515   1.43  perseant 			printf("lfs_vfree: negative byte count"
    516   1.62      yamt 			       " (segment %" PRIu32 " short by %d)\n",
    517  1.117  christos 			       lfs_dtosn(fs, old_iaddr),
    518  1.116  dholland 			       (int)sizeof (struct ulfs1_dinode) -
    519   1.66      fvdl 				    sup->su_nbytes);
    520   1.22  perseant 			panic("lfs_vfree: negative byte count");
    521  1.116  dholland 			sup->su_nbytes = sizeof (struct ulfs1_dinode);
    522   1.17  perseant 		}
    523   1.22  perseant #endif
    524  1.116  dholland 		sup->su_nbytes -= sizeof (struct ulfs1_dinode);
    525  1.117  christos 		LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, old_iaddr), bp); /* Ifile */
    526    1.1   mycroft 	}
    527   1.75     perry 
    528    1.1   mycroft 	/* Set superblock modified bit and decrement file count. */
    529  1.105        ad 	mutex_enter(&lfs_lock);
    530    1.1   mycroft 	fs->lfs_fmod = 1;
    531  1.105        ad 	mutex_exit(&lfs_lock);
    532  1.122  dholland 	lfs_sb_subnfiles(fs, 1);
    533   1.75     perry 
    534   1.56  perseant 	lfs_segunlock(fs);
    535   1.63  perseant 
    536    1.1   mycroft 	return (0);
    537    1.1   mycroft }
    538   1.87  perseant 
    539   1.87  perseant /*
    540   1.87  perseant  * Sort the freelist and set up the free-inode bitmap.
    541   1.87  perseant  * To be called by lfs_mountfs().
    542   1.87  perseant  */
    543   1.87  perseant void
    544   1.87  perseant lfs_order_freelist(struct lfs *fs)
    545   1.87  perseant {
    546   1.87  perseant 	CLEANERINFO *cip;
    547   1.87  perseant 	IFILE *ifp = NULL;
    548   1.87  perseant 	struct buf *bp;
    549   1.87  perseant 	ino_t ino, firstino, lastino, maxino;
    550   1.97  perseant #ifdef notyet
    551   1.97  perseant 	struct vnode *vp;
    552   1.97  perseant #endif
    553   1.87  perseant 
    554   1.95  perseant 	ASSERT_NO_SEGLOCK(fs);
    555   1.95  perseant 	lfs_seglock(fs, SEGM_PROT);
    556   1.95  perseant 
    557  1.123  dholland 	maxino = ((fs->lfs_ivnode->v_size >> lfs_sb_getbshift(fs)) -
    558  1.122  dholland 		  lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) * lfs_sb_getifpb(fs);
    559  1.121  dholland 	fs->lfs_ino_bitmap =
    560   1.88  perseant 		malloc(((maxino + BMMASK) >> BMSHIFT) * sizeof(lfs_bm_t),
    561   1.88  perseant 		       M_SEGMENT, M_WAITOK | M_ZERO);
    562   1.88  perseant 	KASSERT(fs->lfs_ino_bitmap != NULL);
    563   1.87  perseant 
    564   1.87  perseant 	firstino = lastino = LFS_UNUSED_INUM;
    565   1.87  perseant 	for (ino = 0; ino < maxino; ino++) {
    566  1.122  dholland 		if (ino % lfs_sb_getifpb(fs) == 0)
    567   1.87  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    568   1.87  perseant 		else
    569   1.87  perseant 			++ifp;
    570   1.87  perseant 
    571   1.87  perseant 		/* Don't put zero or ifile on the free list */
    572   1.87  perseant 		if (ino == LFS_UNUSED_INUM || ino == LFS_IFILE_INUM)
    573   1.87  perseant 			continue;
    574   1.87  perseant 
    575   1.97  perseant #ifdef notyet
    576   1.97  perseant 		/* Address orphaned files */
    577   1.97  perseant 		if (ifp->if_nextfree == LFS_ORPHAN_NEXTFREE &&
    578   1.97  perseant 		    VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp) == 0) {
    579  1.104        he 			lfs_truncate(vp, 0, 0, NOCRED);
    580   1.97  perseant 			vput(vp);
    581  1.117  christos 			LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, ifp->if_daddr), bp);
    582   1.97  perseant 			KASSERT(sup->su_nbytes >= DINODE1_SIZE);
    583   1.97  perseant 			sup->su_nbytes -= DINODE1_SIZE;
    584  1.117  christos 			LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, ifp->if_daddr), bp);
    585   1.97  perseant 
    586   1.97  perseant 			/* Set up to fall through to next section */
    587   1.97  perseant 			ifp->if_daddr = LFS_UNUSED_DADDR;
    588   1.97  perseant 			LFS_BWRITE_LOG(bp);
    589   1.97  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    590   1.97  perseant 		}
    591   1.97  perseant #endif
    592   1.97  perseant 
    593   1.87  perseant 		if (ifp->if_daddr == LFS_UNUSED_DADDR) {
    594   1.87  perseant 			if (firstino == LFS_UNUSED_INUM)
    595   1.87  perseant 				firstino = ino;
    596   1.87  perseant 			else {
    597  1.102        ad 				brelse(bp, 0);
    598   1.87  perseant 
    599   1.87  perseant 				LFS_IENTRY(ifp, fs, lastino, bp);
    600   1.87  perseant 				ifp->if_nextfree = ino;
    601   1.87  perseant 				LFS_BWRITE_LOG(bp);
    602   1.87  perseant 
    603   1.87  perseant 				LFS_IENTRY(ifp, fs, ino, bp);
    604   1.87  perseant 			}
    605   1.87  perseant 			lastino = ino;
    606   1.87  perseant 
    607   1.87  perseant 			SET_BITMAP_FREE(fs, ino);
    608   1.87  perseant 		}
    609   1.87  perseant 
    610  1.122  dholland 		if ((ino + 1) % lfs_sb_getifpb(fs) == 0)
    611  1.102        ad 			brelse(bp, 0);
    612   1.87  perseant 	}
    613   1.87  perseant 
    614   1.87  perseant 	LFS_PUT_HEADFREE(fs, cip, bp, firstino);
    615   1.87  perseant 	LFS_PUT_TAILFREE(fs, cip, bp, lastino);
    616   1.95  perseant 
    617   1.95  perseant 	lfs_segunlock(fs);
    618   1.87  perseant }
    619   1.97  perseant 
    620   1.97  perseant void
    621   1.97  perseant lfs_orphan(struct lfs *fs, ino_t ino)
    622   1.97  perseant {
    623   1.97  perseant 	IFILE *ifp;
    624   1.97  perseant 	struct buf *bp;
    625   1.97  perseant 
    626   1.97  perseant 	LFS_IENTRY(ifp, fs, ino, bp);
    627   1.97  perseant 	ifp->if_nextfree = LFS_ORPHAN_NEXTFREE;
    628   1.97  perseant 	LFS_BWRITE_LOG(bp);
    629   1.97  perseant }
    630