Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.119.6.2
      1  1.119.6.2     skrll /*	$NetBSD: lfs_alloc.c,v 1.119.6.2 2015/09/22 12:06:17 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.2     skrll __KERNEL_RCSID(0, "$NetBSD: lfs_alloc.c,v 1.119.6.2 2015/09/22 12:06:17 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.2     skrll 		  lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) * fs->lfs_ifpb;
    331       1.87  perseant 	for (ino = maxino - 1; ino > LFS_UNUSED_INUM; --ino) {
    332       1.87  perseant 		if (ISSET_BITMAP_FREE(fs, ino) == 0)
    333       1.87  perseant 			break;
    334       1.87  perseant 	}
    335       1.87  perseant 	return ino;
    336       1.87  perseant }
    337       1.87  perseant #endif
    338       1.87  perseant 
    339       1.87  perseant /*
    340       1.87  perseant  * Find the previous (next lowest numbered) free inode, if any.
    341       1.87  perseant  * If there is none, return LFS_UNUSED_INUM.
    342       1.87  perseant  */
    343       1.87  perseant static inline ino_t
    344       1.87  perseant lfs_freelist_prev(struct lfs *fs, ino_t ino)
    345       1.87  perseant {
    346       1.88  perseant 	ino_t tino, bound, bb, freehdbb;
    347       1.88  perseant 
    348  1.119.6.2     skrll 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM)	 /* No free inodes at all */
    349       1.88  perseant 		return LFS_UNUSED_INUM;
    350       1.88  perseant 
    351       1.88  perseant 	/* Search our own word first */
    352       1.88  perseant 	bound = ino & ~BMMASK;
    353       1.89  perseant 	for (tino = ino - 1; tino >= bound && tino > LFS_UNUSED_INUM; tino--)
    354       1.88  perseant 		if (ISSET_BITMAP_FREE(fs, tino))
    355       1.88  perseant 			return tino;
    356       1.88  perseant 	/* If there are no lower words to search, just return */
    357       1.88  perseant 	if (ino >> BMSHIFT == 0)
    358       1.88  perseant 		return LFS_UNUSED_INUM;
    359       1.88  perseant 
    360       1.88  perseant 	/*
    361       1.88  perseant 	 * Find a word with a free inode in it.  We have to be a bit
    362       1.88  perseant 	 * careful here since ino_t is unsigned.
    363       1.88  perseant 	 */
    364  1.119.6.2     skrll 	freehdbb = (lfs_sb_getfreehd(fs) >> BMSHIFT);
    365       1.88  perseant 	for (bb = (ino >> BMSHIFT) - 1; bb >= freehdbb && bb > 0; --bb)
    366       1.88  perseant 		if (fs->lfs_ino_bitmap[bb])
    367       1.88  perseant 			break;
    368       1.88  perseant 	if (fs->lfs_ino_bitmap[bb] == 0)
    369       1.88  perseant 		return LFS_UNUSED_INUM;
    370       1.88  perseant 
    371       1.88  perseant 	/* Search the word we found */
    372       1.89  perseant 	for (tino = (bb << BMSHIFT) | BMMASK; tino >= (bb << BMSHIFT) &&
    373       1.89  perseant 	     tino > LFS_UNUSED_INUM; tino--)
    374       1.88  perseant 		if (ISSET_BITMAP_FREE(fs, tino))
    375       1.88  perseant 			break;
    376       1.87  perseant 
    377       1.87  perseant 	if (tino <= LFS_IFILE_INUM)
    378       1.87  perseant 		tino = LFS_UNUSED_INUM;
    379       1.87  perseant 
    380       1.87  perseant 	return tino;
    381       1.87  perseant }
    382       1.87  perseant 
    383        1.1   mycroft /* Free an inode. */
    384        1.1   mycroft /* ARGUSED */
    385       1.43  perseant /* VOP_BWRITE 2i times */
    386        1.1   mycroft int
    387       1.99  christos lfs_vfree(struct vnode *vp, ino_t ino, int mode)
    388        1.3  christos {
    389        1.1   mycroft 	SEGUSE *sup;
    390       1.48  perseant 	CLEANERINFO *cip;
    391       1.48  perseant 	struct buf *cbp, *bp;
    392  1.119.6.2     skrll 	IFILE *ifp;
    393        1.1   mycroft 	struct inode *ip;
    394        1.1   mycroft 	struct lfs *fs;
    395       1.59      fvdl 	daddr_t old_iaddr;
    396       1.85      yamt 	ino_t otail;
    397       1.75     perry 
    398        1.1   mycroft 	/* Get the inode number and file system. */
    399       1.30  perseant 	ip = VTOI(vp);
    400        1.1   mycroft 	fs = ip->i_lfs;
    401        1.1   mycroft 	ino = ip->i_number;
    402       1.34  perseant 
    403       1.78  perseant 	ASSERT_NO_SEGLOCK(fs);
    404       1.88  perseant 	DLOG((DLOG_ALLOC, "lfs_vfree: free ino %lld\n", (long long)ino));
    405       1.78  perseant 
    406       1.48  perseant 	/* Drain of pending writes */
    407      1.111     rmind 	mutex_enter(vp->v_interlock);
    408  1.119.6.2     skrll 	while (lfs_sb_getversion(fs) > 1 && WRITEINPROG(vp)) {
    409      1.111     rmind 		cv_wait(&vp->v_cv, vp->v_interlock);
    410      1.105        ad 	}
    411      1.111     rmind 	mutex_exit(vp->v_interlock);
    412       1.48  perseant 
    413       1.63  perseant 	lfs_seglock(fs, SEGM_PROT);
    414       1.75     perry 
    415       1.72      yamt 	lfs_unmark_vnode(vp);
    416      1.105        ad 	mutex_enter(&lfs_lock);
    417      1.103        ad 	if (vp->v_uflag & VU_DIROP) {
    418      1.103        ad 		vp->v_uflag &= ~VU_DIROP;
    419       1.30  perseant 		--lfs_dirvcount;
    420       1.92  perseant 		--fs->lfs_dirvcount;
    421       1.63  perseant 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    422       1.92  perseant 		wakeup(&fs->lfs_dirvcount);
    423       1.30  perseant 		wakeup(&lfs_dirvcount);
    424      1.105        ad 		mutex_exit(&lfs_lock);
    425  1.119.6.1     skrll 		vrele(vp);
    426       1.90  perseant 
    427       1.90  perseant 		/*
    428       1.90  perseant 		 * If this inode is not going to be written any more, any
    429       1.90  perseant 		 * segment accounting left over from its truncation needs
    430       1.90  perseant 		 * to occur at the end of the next dirops flush.  Attach
    431       1.90  perseant 		 * them to the fs-wide list for that purpose.
    432       1.90  perseant 		 */
    433       1.90  perseant 		if (LIST_FIRST(&ip->i_lfs_segdhd) != NULL) {
    434       1.90  perseant 			struct segdelta *sd;
    435  1.119.6.2     skrll 
    436       1.90  perseant 			while((sd = LIST_FIRST(&ip->i_lfs_segdhd)) != NULL) {
    437       1.90  perseant 				LIST_REMOVE(sd, list);
    438       1.90  perseant 				LIST_INSERT_HEAD(&fs->lfs_segdhd, sd, list);
    439       1.90  perseant 			}
    440       1.90  perseant 		}
    441       1.90  perseant 	} else {
    442       1.90  perseant 		/*
    443       1.90  perseant 		 * If it's not a dirop, we can finalize right away.
    444       1.90  perseant 		 */
    445      1.105        ad 		mutex_exit(&lfs_lock);
    446       1.90  perseant 		lfs_finalize_ino_seguse(fs, ip);
    447       1.38  perseant 	}
    448       1.30  perseant 
    449      1.105        ad 	mutex_enter(&lfs_lock);
    450       1.42  perseant 	LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
    451      1.105        ad 	mutex_exit(&lfs_lock);
    452       1.33  perseant 	ip->i_flag &= ~IN_ALLMOD;
    453       1.93  perseant 	ip->i_lfs_iflags |= LFSI_DELETED;
    454  1.119.6.2     skrll 
    455        1.1   mycroft 	/*
    456        1.1   mycroft 	 * Set the ifile's inode entry to unused, increment its version number
    457       1.48  perseant 	 * and link it onto the free chain.
    458        1.1   mycroft 	 */
    459       1.87  perseant 	SET_BITMAP_FREE(fs, ino);
    460        1.1   mycroft 	LFS_IENTRY(ifp, fs, ino, bp);
    461  1.119.6.2     skrll 	old_iaddr = lfs_if_getdaddr(fs, ifp);
    462  1.119.6.2     skrll 	lfs_if_setdaddr(fs, ifp, LFS_UNUSED_DADDR);
    463  1.119.6.2     skrll 	lfs_if_setversion(fs, ifp, lfs_if_getversion(fs, ifp) + 1);
    464  1.119.6.2     skrll 	if (lfs_sb_getversion(fs) == 1) {
    465  1.119.6.2     skrll 		ino_t nextfree;
    466  1.119.6.2     skrll 
    467  1.119.6.2     skrll 		LFS_GET_HEADFREE(fs, cip, cbp, &nextfree);
    468  1.119.6.2     skrll 		lfs_if_setnextfree(fs, ifp, nextfree);
    469       1.48  perseant 		LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    470       1.56  perseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    471       1.48  perseant 	} else {
    472       1.87  perseant 		ino_t tino, onf;
    473       1.87  perseant 
    474  1.119.6.2     skrll 		lfs_if_setnextfree(fs, ifp, LFS_UNUSED_INUM);
    475       1.56  perseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    476       1.87  perseant 
    477       1.87  perseant 		tino = lfs_freelist_prev(fs, ino);
    478       1.87  perseant 		if (tino == LFS_UNUSED_INUM) {
    479  1.119.6.2     skrll 			ino_t nextfree;
    480  1.119.6.2     skrll 
    481       1.87  perseant 			/* Nothing free below us, put us on the head */
    482       1.87  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    483  1.119.6.2     skrll 			LFS_GET_HEADFREE(fs, cip, cbp, &nextfree);
    484  1.119.6.2     skrll 			lfs_if_setnextfree(fs, ifp, nextfree);
    485       1.87  perseant 			LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    486       1.88  perseant 			DLOG((DLOG_ALLOC, "lfs_vfree: headfree %lld -> %lld\n",
    487  1.119.6.2     skrll 			     (long long)nextfree, (long long)ino));
    488       1.87  perseant 			LFS_BWRITE_LOG(bp); /* Ifile */
    489       1.87  perseant 
    490       1.87  perseant 			/* If the list was empty, set tail too */
    491       1.87  perseant 			LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    492       1.87  perseant 			if (otail == LFS_UNUSED_INUM) {
    493       1.87  perseant 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    494       1.87  perseant 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    495       1.87  perseant 				      "-> %lld\n", (long long)otail,
    496       1.87  perseant 				      (long long)ino));
    497       1.87  perseant 			}
    498       1.87  perseant 		} else {
    499       1.87  perseant 			/*
    500       1.87  perseant 			 * Insert this inode into the list after tino.
    501       1.87  perseant 			 * We hold the segment lock so we don't have to
    502       1.87  perseant 			 * worry about blocks being written out of order.
    503       1.87  perseant 			 */
    504       1.87  perseant 			DLOG((DLOG_ALLOC, "lfs_vfree: insert ino %lld "
    505       1.87  perseant 			      " after %lld\n", ino, tino));
    506       1.87  perseant 
    507       1.87  perseant 			LFS_IENTRY(ifp, fs, tino, bp);
    508  1.119.6.2     skrll 			onf = lfs_if_getnextfree(fs, ifp);
    509  1.119.6.2     skrll 			lfs_if_setnextfree(fs, ifp, ino);
    510       1.87  perseant 			LFS_BWRITE_LOG(bp);	/* Ifile */
    511       1.87  perseant 
    512       1.87  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    513  1.119.6.2     skrll 			lfs_if_setnextfree(fs, ifp, onf);
    514       1.87  perseant 			LFS_BWRITE_LOG(bp);	/* Ifile */
    515       1.87  perseant 
    516       1.87  perseant 			/* If we're last, put us on the tail */
    517       1.87  perseant 			if (onf == LFS_UNUSED_INUM) {
    518       1.87  perseant 				LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    519       1.87  perseant 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    520       1.87  perseant 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    521       1.87  perseant 				      "-> %lld\n", (long long)otail,
    522       1.87  perseant 				      (long long)ino));
    523       1.87  perseant 			}
    524       1.87  perseant 		}
    525       1.48  perseant 	}
    526       1.17  perseant #ifdef DIAGNOSTIC
    527       1.53       chs 	if (ino == LFS_UNUSED_INUM) {
    528       1.17  perseant 		panic("inode 0 freed");
    529       1.17  perseant 	}
    530       1.17  perseant #endif /* DIAGNOSTIC */
    531        1.1   mycroft 	if (old_iaddr != LFS_UNUSED_DADDR) {
    532      1.117  christos 		LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, old_iaddr), bp);
    533       1.22  perseant #ifdef DIAGNOSTIC
    534  1.119.6.2     skrll 		if (sup->su_nbytes < DINOSIZE(fs)) {
    535       1.43  perseant 			printf("lfs_vfree: negative byte count"
    536       1.62      yamt 			       " (segment %" PRIu32 " short by %d)\n",
    537      1.117  christos 			       lfs_dtosn(fs, old_iaddr),
    538  1.119.6.2     skrll 			       (int)DINOSIZE(fs) -
    539       1.66      fvdl 				    sup->su_nbytes);
    540       1.22  perseant 			panic("lfs_vfree: negative byte count");
    541  1.119.6.2     skrll 			sup->su_nbytes = DINOSIZE(fs);
    542       1.17  perseant 		}
    543       1.22  perseant #endif
    544  1.119.6.2     skrll 		sup->su_nbytes -= DINOSIZE(fs);
    545      1.117  christos 		LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, old_iaddr), bp); /* Ifile */
    546        1.1   mycroft 	}
    547       1.75     perry 
    548        1.1   mycroft 	/* Set superblock modified bit and decrement file count. */
    549      1.105        ad 	mutex_enter(&lfs_lock);
    550        1.1   mycroft 	fs->lfs_fmod = 1;
    551      1.105        ad 	mutex_exit(&lfs_lock);
    552  1.119.6.2     skrll 	lfs_sb_subnfiles(fs, 1);
    553       1.75     perry 
    554       1.56  perseant 	lfs_segunlock(fs);
    555       1.63  perseant 
    556        1.1   mycroft 	return (0);
    557        1.1   mycroft }
    558       1.87  perseant 
    559       1.87  perseant /*
    560       1.87  perseant  * Sort the freelist and set up the free-inode bitmap.
    561       1.87  perseant  * To be called by lfs_mountfs().
    562       1.87  perseant  */
    563       1.87  perseant void
    564       1.87  perseant lfs_order_freelist(struct lfs *fs)
    565       1.87  perseant {
    566       1.87  perseant 	CLEANERINFO *cip;
    567       1.87  perseant 	IFILE *ifp = NULL;
    568       1.87  perseant 	struct buf *bp;
    569       1.87  perseant 	ino_t ino, firstino, lastino, maxino;
    570       1.97  perseant #ifdef notyet
    571       1.97  perseant 	struct vnode *vp;
    572       1.97  perseant #endif
    573  1.119.6.2     skrll 
    574       1.95  perseant 	ASSERT_NO_SEGLOCK(fs);
    575       1.95  perseant 	lfs_seglock(fs, SEGM_PROT);
    576       1.95  perseant 
    577  1.119.6.2     skrll 	maxino = ((fs->lfs_ivnode->v_size >> lfs_sb_getbshift(fs)) -
    578  1.119.6.2     skrll 		  lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) * lfs_sb_getifpb(fs);
    579  1.119.6.2     skrll 	fs->lfs_ino_bitmap =
    580       1.88  perseant 		malloc(((maxino + BMMASK) >> BMSHIFT) * sizeof(lfs_bm_t),
    581       1.88  perseant 		       M_SEGMENT, M_WAITOK | M_ZERO);
    582       1.88  perseant 	KASSERT(fs->lfs_ino_bitmap != NULL);
    583       1.87  perseant 
    584       1.87  perseant 	firstino = lastino = LFS_UNUSED_INUM;
    585       1.87  perseant 	for (ino = 0; ino < maxino; ino++) {
    586  1.119.6.2     skrll 		if (ino % lfs_sb_getifpb(fs) == 0)
    587       1.87  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    588       1.87  perseant 		else
    589  1.119.6.2     skrll 			LFS_IENTRY_NEXT(ifp, fs);
    590       1.87  perseant 
    591       1.87  perseant 		/* Don't put zero or ifile on the free list */
    592       1.87  perseant 		if (ino == LFS_UNUSED_INUM || ino == LFS_IFILE_INUM)
    593       1.87  perseant 			continue;
    594       1.87  perseant 
    595       1.97  perseant #ifdef notyet
    596       1.97  perseant 		/* Address orphaned files */
    597  1.119.6.2     skrll 		if (lfs_if_getnextfree(fs, ifp) == LFS_ORPHAN_NEXTFREE &&
    598       1.97  perseant 		    VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp) == 0) {
    599  1.119.6.2     skrll 			unsigned segno;
    600  1.119.6.2     skrll 
    601  1.119.6.2     skrll 			segno = lfs_dtosn(fs, lfs_if_getdaddr(fs, ifp));
    602      1.104        he 			lfs_truncate(vp, 0, 0, NOCRED);
    603       1.97  perseant 			vput(vp);
    604  1.119.6.2     skrll 			LFS_SEGENTRY(sup, fs, segno, bp);
    605  1.119.6.2     skrll 			KASSERT(sup->su_nbytes >= LFS_DINODE1_SIZE);
    606  1.119.6.2     skrll 			sup->su_nbytes -= LFS_DINODE1_SIZE;
    607  1.119.6.2     skrll 			LFS_WRITESEGENTRY(sup, fs, segno, bp);
    608       1.97  perseant 
    609       1.97  perseant 			/* Set up to fall through to next section */
    610  1.119.6.2     skrll 			lfs_if_setdaddr(fs, ifp, LFS_UNUSED_DADDR);
    611       1.97  perseant 			LFS_BWRITE_LOG(bp);
    612       1.97  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    613       1.97  perseant 		}
    614       1.97  perseant #endif
    615       1.97  perseant 
    616  1.119.6.2     skrll 		if (lfs_if_getdaddr(fs, ifp) == LFS_UNUSED_DADDR) {
    617       1.87  perseant 			if (firstino == LFS_UNUSED_INUM)
    618       1.87  perseant 				firstino = ino;
    619       1.87  perseant 			else {
    620      1.102        ad 				brelse(bp, 0);
    621       1.87  perseant 
    622       1.87  perseant 				LFS_IENTRY(ifp, fs, lastino, bp);
    623  1.119.6.2     skrll 				lfs_if_setnextfree(fs, ifp, ino);
    624       1.87  perseant 				LFS_BWRITE_LOG(bp);
    625  1.119.6.2     skrll 
    626       1.87  perseant 				LFS_IENTRY(ifp, fs, ino, bp);
    627       1.87  perseant 			}
    628       1.87  perseant 			lastino = ino;
    629       1.87  perseant 
    630       1.87  perseant 			SET_BITMAP_FREE(fs, ino);
    631       1.87  perseant 		}
    632       1.87  perseant 
    633  1.119.6.2     skrll 		if ((ino + 1) % lfs_sb_getifpb(fs) == 0)
    634      1.102        ad 			brelse(bp, 0);
    635       1.87  perseant 	}
    636       1.87  perseant 
    637       1.87  perseant 	LFS_PUT_HEADFREE(fs, cip, bp, firstino);
    638       1.87  perseant 	LFS_PUT_TAILFREE(fs, cip, bp, lastino);
    639       1.95  perseant 
    640       1.95  perseant 	lfs_segunlock(fs);
    641       1.87  perseant }
    642       1.97  perseant 
    643       1.97  perseant void
    644       1.97  perseant lfs_orphan(struct lfs *fs, ino_t ino)
    645       1.97  perseant {
    646       1.97  perseant 	IFILE *ifp;
    647       1.97  perseant 	struct buf *bp;
    648       1.97  perseant 
    649       1.97  perseant 	LFS_IENTRY(ifp, fs, ino, bp);
    650  1.119.6.2     skrll 	lfs_if_setnextfree(fs, ifp, LFS_ORPHAN_NEXTFREE);
    651       1.97  perseant 	LFS_BWRITE_LOG(bp);
    652       1.97  perseant }
    653