Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.132
      1  1.132  dholland /*	$NetBSD: lfs_alloc.c,v 1.132 2016/08/07 00:12:48 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.132  dholland __KERNEL_RCSID(0, "$NetBSD: lfs_alloc.c,v 1.132 2016/08/07 00:12:48 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.126  dholland 	IFILE64 *ifp64;
    120  1.126  dholland 	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.132  dholland 	/* XXX should check or assert that we aren't readonly. */
    131  1.132  dholland 
    132  1.132  dholland 	/*
    133  1.132  dholland 	 * Get a block and extend the ifile inode. Leave the buffer for
    134  1.132  dholland 	 * the block in bp.
    135  1.132  dholland 	 */
    136  1.132  dholland 
    137   1.44  perseant 	vp = fs->lfs_ivnode;
    138   1.44  perseant 	ip = VTOI(vp);
    139  1.117  christos 	blkno = lfs_lblkno(fs, ip->i_size);
    140  1.122  dholland 	if ((error = lfs_balloc(vp, ip->i_size, lfs_sb_getbsize(fs), cred, 0,
    141   1.44  perseant 				&bp)) != 0) {
    142   1.44  perseant 		return (error);
    143   1.44  perseant 	}
    144  1.122  dholland 	ip->i_size += lfs_sb_getbsize(fs);
    145  1.129  dholland 	lfs_dino_setsize(fs, ip->i_din, ip->i_size);
    146   1.66      fvdl 	uvm_vnp_setsize(vp, ip->i_size);
    147   1.75     perry 
    148  1.132  dholland 	/*
    149  1.132  dholland 	 * Compute the new number of inodes, and reallocate the in-memory
    150  1.132  dholland 	 * inode freemap.
    151  1.132  dholland 	 */
    152  1.132  dholland 
    153  1.123  dholland 	maxino = ((ip->i_size >> lfs_sb_getbshift(fs)) - lfs_sb_getcleansz(fs) -
    154  1.122  dholland 		  lfs_sb_getsegtabsz(fs)) * lfs_sb_getifpb(fs);
    155   1.88  perseant 	fs->lfs_ino_bitmap = (lfs_bm_t *)
    156   1.88  perseant 		realloc(fs->lfs_ino_bitmap, ((maxino + BMMASK) >> BMSHIFT) *
    157   1.88  perseant 			sizeof(lfs_bm_t), M_SEGMENT, M_WAITOK);
    158   1.88  perseant 	KASSERT(fs->lfs_ino_bitmap != NULL);
    159   1.87  perseant 
    160  1.132  dholland 	/* first new inode number */
    161  1.122  dholland 	i = (blkno - lfs_sb_getsegtabsz(fs) - lfs_sb_getcleansz(fs)) *
    162  1.122  dholland 		lfs_sb_getifpb(fs);
    163   1.87  perseant 
    164   1.87  perseant 	/*
    165   1.87  perseant 	 * We insert the new inodes at the head of the free list.
    166   1.87  perseant 	 * Under normal circumstances, the free list is empty here,
    167   1.87  perseant 	 * so we are also incidentally placing them at the end (which
    168   1.87  perseant 	 * we must do if we are to keep them in order).
    169   1.87  perseant 	 */
    170   1.48  perseant 	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
    171   1.48  perseant 	LFS_PUT_HEADFREE(fs, cip, cbp, i);
    172   1.44  perseant #ifdef DIAGNOSTIC
    173  1.122  dholland 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM)
    174   1.44  perseant 		panic("inode 0 allocated [2]");
    175   1.44  perseant #endif /* DIAGNOSTIC */
    176  1.132  dholland 
    177  1.132  dholland 	/* inode number to stop at (XXX: why *x*max?) */
    178  1.122  dholland 	xmax = i + lfs_sb_getifpb(fs);
    179   1.48  perseant 
    180  1.132  dholland 	/*
    181  1.132  dholland 	 * Initialize the ifile block.
    182  1.132  dholland 	 *
    183  1.132  dholland 	 * XXX: these loops should be restructured to use the accessor
    184  1.132  dholland 	 * functions instead of using cutpaste polymorphism.
    185  1.132  dholland 	 */
    186  1.132  dholland 
    187  1.126  dholland 	if (fs->lfs_is64) {
    188  1.126  dholland 		for (ifp64 = (IFILE64 *)bp->b_data; i < xmax; ++ifp64) {
    189  1.126  dholland 			SET_BITMAP_FREE(fs, i);
    190  1.126  dholland 			ifp64->if_version = 1;
    191  1.126  dholland 			ifp64->if_daddr = LFS_UNUSED_DADDR;
    192  1.126  dholland 			ifp64->if_nextfree = ++i;
    193  1.126  dholland 		}
    194  1.126  dholland 		ifp64--;
    195  1.126  dholland 		ifp64->if_nextfree = oldlast;
    196  1.126  dholland 	} else if (lfs_sb_getversion(fs) > 1) {
    197  1.126  dholland 		for (ifp32 = (IFILE32 *)bp->b_data; i < xmax; ++ifp32) {
    198  1.126  dholland 			SET_BITMAP_FREE(fs, i);
    199  1.126  dholland 			ifp32->if_version = 1;
    200  1.126  dholland 			ifp32->if_daddr = LFS_UNUSED_DADDR;
    201  1.126  dholland 			ifp32->if_nextfree = ++i;
    202  1.126  dholland 		}
    203  1.126  dholland 		ifp32--;
    204  1.126  dholland 		ifp32->if_nextfree = oldlast;
    205  1.126  dholland 	} else {
    206   1.83  christos 		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < xmax; ++ifp_v1) {
    207   1.89  perseant 			SET_BITMAP_FREE(fs, i);
    208   1.48  perseant 			ifp_v1->if_version = 1;
    209   1.48  perseant 			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
    210   1.48  perseant 			ifp_v1->if_nextfree = ++i;
    211   1.48  perseant 		}
    212   1.48  perseant 		ifp_v1--;
    213   1.48  perseant 		ifp_v1->if_nextfree = oldlast;
    214   1.44  perseant 	}
    215   1.83  christos 	LFS_PUT_TAILFREE(fs, cip, cbp, xmax - 1);
    216   1.48  perseant 
    217  1.132  dholland 	/*
    218  1.132  dholland 	 * Write out the new block.
    219  1.132  dholland 	 */
    220  1.132  dholland 
    221   1.56  perseant 	(void) LFS_BWRITE_LOG(bp); /* Ifile */
    222   1.44  perseant 
    223   1.44  perseant 	return 0;
    224   1.44  perseant }
    225   1.44  perseant 
    226  1.132  dholland /*
    227  1.132  dholland  * Allocate an inode for a new file.
    228  1.132  dholland  *
    229  1.132  dholland  * Takes the segment lock. Also (while holding it) takes lfs_lock
    230  1.132  dholland  * to frob fs->lfs_fmod.
    231  1.132  dholland  *
    232  1.132  dholland  * XXX: the mode argument is unused; should just get rid of it.
    233  1.132  dholland  */
    234    1.1   mycroft /* ARGSUSED */
    235   1.43  perseant /* VOP_BWRITE 2i times */
    236    1.1   mycroft int
    237   1.99  christos lfs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred,
    238  1.120   hannken     ino_t *ino, int *gen)
    239    1.3  christos {
    240    1.1   mycroft 	struct lfs *fs;
    241   1.48  perseant 	struct buf *bp, *cbp;
    242  1.126  dholland 	IFILE *ifp;
    243    1.1   mycroft 	int error;
    244   1.48  perseant 	CLEANERINFO *cip;
    245    1.1   mycroft 
    246   1.85      yamt 	fs = VTOI(pvp)->i_lfs;
    247   1.38  perseant 	if (fs->lfs_ronly)
    248   1.38  perseant 		return EROFS;
    249   1.75     perry 
    250   1.78  perseant 	ASSERT_NO_SEGLOCK(fs);
    251   1.78  perseant 
    252   1.56  perseant 	lfs_seglock(fs, SEGM_PROT);
    253   1.17  perseant 
    254    1.1   mycroft 	/* Get the head of the freelist. */
    255  1.120   hannken 	LFS_GET_HEADFREE(fs, cip, cbp, ino);
    256  1.132  dholland 
    257  1.132  dholland 	/* paranoia */
    258  1.120   hannken 	KASSERT(*ino != LFS_UNUSED_INUM && *ino != LFS_IFILE_INUM);
    259  1.120   hannken 	DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %" PRId64 "\n",
    260  1.120   hannken 	     *ino));
    261   1.75     perry 
    262  1.132  dholland 	/* Update the in-memory inode freemap */
    263  1.132  dholland 	CLR_BITMAP_FREE(fs, *ino);
    264  1.132  dholland 
    265    1.1   mycroft 	/*
    266  1.132  dholland 	 * Fetch the ifile entry and make sure the inode is really
    267  1.132  dholland 	 * free.
    268    1.1   mycroft 	 */
    269  1.120   hannken 	LFS_IENTRY(ifp, fs, *ino, bp);
    270  1.126  dholland 	if (lfs_if_getdaddr(fs, ifp) != LFS_UNUSED_DADDR)
    271  1.120   hannken 		panic("lfs_valloc: inuse inode %" PRId64 " on the free list",
    272  1.120   hannken 		    *ino);
    273  1.132  dholland 
    274  1.132  dholland 	/* Update the inode freelist head in the superblock. */
    275  1.126  dholland 	LFS_PUT_HEADFREE(fs, cip, cbp, lfs_if_getnextfree(fs, ifp));
    276  1.126  dholland 	DLOG((DLOG_ALLOC, "lfs_valloc: headfree %" PRId64 " -> %ju\n",
    277  1.126  dholland 	     *ino, (uintmax_t)lfs_if_getnextfree(fs, ifp)));
    278   1.48  perseant 
    279  1.132  dholland 	/*
    280  1.132  dholland 	 * Retrieve the version number from the ifile entry. It was
    281  1.132  dholland 	 * bumped by vfree, so don't bump it again.
    282  1.132  dholland 	 */
    283  1.126  dholland 	*gen = lfs_if_getversion(fs, ifp);
    284  1.132  dholland 
    285  1.132  dholland 	/* Done with ifile entry */
    286  1.102        ad 	brelse(bp, 0);
    287   1.30  perseant 
    288  1.122  dholland 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM) {
    289  1.132  dholland 		/*
    290  1.132  dholland 		 * No more inodes; extend the ifile so that the next
    291  1.132  dholland 		 * lfs_valloc will succeed.
    292  1.132  dholland 		 */
    293   1.96  perseant 		if ((error = lfs_extend_ifile(fs, cred)) != 0) {
    294  1.132  dholland 			/* restore the freelist */
    295  1.120   hannken 			LFS_PUT_HEADFREE(fs, cip, cbp, *ino);
    296  1.132  dholland 
    297  1.132  dholland 			/* unlock and return */
    298   1.56  perseant 			lfs_segunlock(fs);
    299   1.44  perseant 			return error;
    300    1.1   mycroft 		}
    301    1.1   mycroft 	}
    302   1.17  perseant #ifdef DIAGNOSTIC
    303  1.122  dholland 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM)
    304   1.17  perseant 		panic("inode 0 allocated [3]");
    305   1.17  perseant #endif /* DIAGNOSTIC */
    306   1.48  perseant 
    307  1.132  dholland 	/* Set superblock modified bit */
    308  1.105        ad 	mutex_enter(&lfs_lock);
    309   1.78  perseant 	fs->lfs_fmod = 1;
    310  1.105        ad 	mutex_exit(&lfs_lock);
    311  1.132  dholland 
    312  1.132  dholland 	/* increment file count */
    313  1.122  dholland 	lfs_sb_addnfiles(fs, 1);
    314   1.78  perseant 
    315  1.132  dholland 	/* done */
    316   1.56  perseant 	lfs_segunlock(fs);
    317  1.120   hannken 	return 0;
    318   1.44  perseant }
    319   1.44  perseant 
    320   1.65  perseant /*
    321  1.132  dholland  * Allocate an inode for a new file, with given inode number and
    322  1.132  dholland  * version.
    323  1.132  dholland  *
    324  1.132  dholland  * Called in the same context as lfs_valloc and therefore shares the
    325  1.132  dholland  * same locking assumptions.
    326  1.132  dholland  *
    327  1.132  dholland  * XXX: WHICH MEANS IT OUGHT TO TAKE THE SEGLOCK WHILE FROBBING THIS
    328  1.132  dholland  * XXX: STUFF. REALLY.
    329   1.65  perseant  */
    330   1.96  perseant int
    331  1.120   hannken lfs_valloc_fixed(struct lfs *fs, ino_t ino, int vers)
    332   1.44  perseant {
    333  1.120   hannken 	IFILE *ifp;
    334  1.120   hannken 	struct buf *bp, *cbp;
    335  1.130  dholland 	ino_t headino, thisino, oldnext;
    336  1.120   hannken 	CLEANERINFO *cip;
    337   1.40      fvdl 
    338  1.132  dholland 	/* XXX: check for readonly */
    339  1.132  dholland 	/* XXX: assert no seglock */
    340  1.132  dholland 	/* XXX: should take seglock (as noted above) */
    341  1.132  dholland 
    342  1.132  dholland 	/*
    343  1.132  dholland 	 * If the ifile is too short to contain this inum, extend it.
    344  1.132  dholland 	 *
    345  1.132  dholland 	 * XXX: lfs_extend_ifile should take a size instead of always
    346  1.132  dholland 	 * doing just one block at time.
    347  1.132  dholland 	 */
    348  1.120   hannken 	while (VTOI(fs->lfs_ivnode)->i_size <= (ino /
    349  1.122  dholland 		lfs_sb_getifpb(fs) + lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs))
    350  1.123  dholland 		<< lfs_sb_getbshift(fs)) {
    351  1.120   hannken 		lfs_extend_ifile(fs, NOCRED);
    352  1.120   hannken 	}
    353   1.78  perseant 
    354  1.132  dholland 	/*
    355  1.132  dholland 	 * fetch the ifile entry; get the inode freelist next pointer,
    356  1.132  dholland 	 * and set the version as directed.
    357  1.132  dholland 	 */
    358  1.120   hannken 	LFS_IENTRY(ifp, fs, ino, bp);
    359  1.126  dholland 	oldnext = lfs_if_getnextfree(fs, ifp);
    360  1.126  dholland 	lfs_if_setversion(fs, ifp, vers);
    361  1.120   hannken 	brelse(bp, 0);
    362   1.51       chs 
    363  1.132  dholland 	/* Get head of inode freelist */
    364  1.130  dholland 	LFS_GET_HEADFREE(fs, cip, cbp, &headino);
    365  1.130  dholland 	if (headino == ino) {
    366  1.132  dholland 		/* Easy case: the inode we wanted was at the head */
    367  1.120   hannken 		LFS_PUT_HEADFREE(fs, cip, cbp, oldnext);
    368  1.120   hannken 	} else {
    369  1.126  dholland 		ino_t nextfree;
    370  1.126  dholland 
    371  1.132  dholland 		/* Have to find the desired inode in the freelist... */
    372  1.132  dholland 
    373  1.130  dholland 		thisino = headino;
    374  1.120   hannken 		while (1) {
    375  1.132  dholland 			/* read this ifile entry */
    376  1.130  dholland 			LFS_IENTRY(ifp, fs, thisino, bp);
    377  1.126  dholland 			nextfree = lfs_if_getnextfree(fs, ifp);
    378  1.132  dholland 			/* stop if we find it or we hit the end */
    379  1.126  dholland 			if (nextfree == ino ||
    380  1.126  dholland 			    nextfree == LFS_UNUSED_INUM)
    381  1.120   hannken 				break;
    382  1.132  dholland 			/* nope, keep going... */
    383  1.130  dholland 			thisino = nextfree;
    384  1.120   hannken 			brelse(bp, 0);
    385  1.120   hannken 		}
    386  1.126  dholland 		if (nextfree == LFS_UNUSED_INUM) {
    387  1.132  dholland 			/* hit the end -- this inode is not available */
    388  1.120   hannken 			brelse(bp, 0);
    389  1.132  dholland 			/* XXX release seglock (see above) */
    390  1.120   hannken 			return ENOENT;
    391  1.120   hannken 		}
    392  1.132  dholland 		/* found it; update the next pointer */
    393  1.126  dholland 		lfs_if_setnextfree(fs, ifp, oldnext);
    394  1.132  dholland 		/* write the ifile block */
    395  1.120   hannken 		LFS_BWRITE_LOG(bp);
    396   1.66      fvdl 	}
    397   1.51       chs 
    398  1.132  dholland 	/* done */
    399  1.132  dholland 	/* XXX release seglock (see above) */
    400  1.120   hannken 	return 0;
    401    1.1   mycroft }
    402    1.1   mycroft 
    403   1.87  perseant #if 0
    404   1.87  perseant /*
    405   1.87  perseant  * Find the highest-numbered allocated inode.
    406   1.87  perseant  * This will be used to shrink the Ifile.
    407   1.87  perseant  */
    408   1.87  perseant static inline ino_t
    409   1.87  perseant lfs_last_alloc_ino(struct lfs *fs)
    410   1.87  perseant {
    411   1.87  perseant 	ino_t ino, maxino;
    412   1.87  perseant 
    413  1.123  dholland 	maxino = ((fs->lfs_ivnode->v_size >> lfs_sb_getbshift(fs)) -
    414  1.131  dholland 		  lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) *
    415  1.131  dholland 		lfs_sb_getifpb(fs);
    416   1.87  perseant 	for (ino = maxino - 1; ino > LFS_UNUSED_INUM; --ino) {
    417   1.87  perseant 		if (ISSET_BITMAP_FREE(fs, ino) == 0)
    418   1.87  perseant 			break;
    419   1.87  perseant 	}
    420   1.87  perseant 	return ino;
    421   1.87  perseant }
    422   1.87  perseant #endif
    423   1.87  perseant 
    424   1.87  perseant /*
    425   1.87  perseant  * Find the previous (next lowest numbered) free inode, if any.
    426   1.87  perseant  * If there is none, return LFS_UNUSED_INUM.
    427  1.132  dholland  *
    428  1.132  dholland  * XXX: locking?
    429   1.87  perseant  */
    430   1.87  perseant static inline ino_t
    431   1.87  perseant lfs_freelist_prev(struct lfs *fs, ino_t ino)
    432   1.87  perseant {
    433   1.88  perseant 	ino_t tino, bound, bb, freehdbb;
    434   1.88  perseant 
    435  1.132  dholland 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM) {
    436  1.132  dholland 		/* No free inodes at all */
    437   1.88  perseant 		return LFS_UNUSED_INUM;
    438  1.132  dholland 	}
    439   1.88  perseant 
    440   1.88  perseant 	/* Search our own word first */
    441   1.88  perseant 	bound = ino & ~BMMASK;
    442   1.89  perseant 	for (tino = ino - 1; tino >= bound && tino > LFS_UNUSED_INUM; tino--)
    443   1.88  perseant 		if (ISSET_BITMAP_FREE(fs, tino))
    444   1.88  perseant 			return tino;
    445   1.88  perseant 	/* If there are no lower words to search, just return */
    446   1.88  perseant 	if (ino >> BMSHIFT == 0)
    447   1.88  perseant 		return LFS_UNUSED_INUM;
    448   1.88  perseant 
    449   1.88  perseant 	/*
    450   1.88  perseant 	 * Find a word with a free inode in it.  We have to be a bit
    451   1.88  perseant 	 * careful here since ino_t is unsigned.
    452   1.88  perseant 	 */
    453  1.122  dholland 	freehdbb = (lfs_sb_getfreehd(fs) >> BMSHIFT);
    454   1.88  perseant 	for (bb = (ino >> BMSHIFT) - 1; bb >= freehdbb && bb > 0; --bb)
    455   1.88  perseant 		if (fs->lfs_ino_bitmap[bb])
    456   1.88  perseant 			break;
    457   1.88  perseant 	if (fs->lfs_ino_bitmap[bb] == 0)
    458   1.88  perseant 		return LFS_UNUSED_INUM;
    459   1.88  perseant 
    460   1.88  perseant 	/* Search the word we found */
    461   1.89  perseant 	for (tino = (bb << BMSHIFT) | BMMASK; tino >= (bb << BMSHIFT) &&
    462   1.89  perseant 	     tino > LFS_UNUSED_INUM; tino--)
    463   1.88  perseant 		if (ISSET_BITMAP_FREE(fs, tino))
    464   1.88  perseant 			break;
    465   1.87  perseant 
    466  1.132  dholland 	/* Avoid returning reserved inode numbers */
    467   1.87  perseant 	if (tino <= LFS_IFILE_INUM)
    468   1.87  perseant 		tino = LFS_UNUSED_INUM;
    469   1.87  perseant 
    470   1.87  perseant 	return tino;
    471   1.87  perseant }
    472   1.87  perseant 
    473  1.132  dholland /*
    474  1.132  dholland  * Free an inode.
    475  1.132  dholland  *
    476  1.132  dholland  * Takes lfs_seglock. Also (independently) takes vp->v_interlock.
    477  1.132  dholland  */
    478    1.1   mycroft /* ARGUSED */
    479   1.43  perseant /* VOP_BWRITE 2i times */
    480    1.1   mycroft int
    481   1.99  christos lfs_vfree(struct vnode *vp, ino_t ino, int mode)
    482    1.3  christos {
    483    1.1   mycroft 	SEGUSE *sup;
    484   1.48  perseant 	CLEANERINFO *cip;
    485   1.48  perseant 	struct buf *cbp, *bp;
    486  1.126  dholland 	IFILE *ifp;
    487    1.1   mycroft 	struct inode *ip;
    488    1.1   mycroft 	struct lfs *fs;
    489   1.59      fvdl 	daddr_t old_iaddr;
    490   1.85      yamt 	ino_t otail;
    491   1.75     perry 
    492    1.1   mycroft 	/* Get the inode number and file system. */
    493   1.30  perseant 	ip = VTOI(vp);
    494    1.1   mycroft 	fs = ip->i_lfs;
    495    1.1   mycroft 	ino = ip->i_number;
    496   1.34  perseant 
    497  1.132  dholland 	/* XXX: assert not readonly */
    498  1.132  dholland 
    499   1.78  perseant 	ASSERT_NO_SEGLOCK(fs);
    500   1.88  perseant 	DLOG((DLOG_ALLOC, "lfs_vfree: free ino %lld\n", (long long)ino));
    501   1.78  perseant 
    502   1.48  perseant 	/* Drain of pending writes */
    503  1.111     rmind 	mutex_enter(vp->v_interlock);
    504  1.125  dholland 	while (lfs_sb_getversion(fs) > 1 && WRITEINPROG(vp)) {
    505  1.111     rmind 		cv_wait(&vp->v_cv, vp->v_interlock);
    506  1.105        ad 	}
    507  1.111     rmind 	mutex_exit(vp->v_interlock);
    508   1.48  perseant 
    509   1.63  perseant 	lfs_seglock(fs, SEGM_PROT);
    510   1.75     perry 
    511  1.132  dholland 	/*
    512  1.132  dholland 	 * If the inode was in a dirop, it isn't now.
    513  1.132  dholland 	 *
    514  1.132  dholland 	 * XXX: why are (v_uflag & VU_DIROP) and (ip->i_flag & IN_ADIROP)
    515  1.132  dholland 	 * not updated together in one function? (and why do both exist,
    516  1.132  dholland 	 * anyway?)
    517  1.132  dholland 	 */
    518   1.72      yamt 	lfs_unmark_vnode(vp);
    519  1.132  dholland 
    520  1.105        ad 	mutex_enter(&lfs_lock);
    521  1.103        ad 	if (vp->v_uflag & VU_DIROP) {
    522  1.103        ad 		vp->v_uflag &= ~VU_DIROP;
    523   1.30  perseant 		--lfs_dirvcount;
    524   1.92  perseant 		--fs->lfs_dirvcount;
    525   1.63  perseant 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    526   1.92  perseant 		wakeup(&fs->lfs_dirvcount);
    527   1.30  perseant 		wakeup(&lfs_dirvcount);
    528  1.105        ad 		mutex_exit(&lfs_lock);
    529  1.120   hannken 		vrele(vp);
    530   1.90  perseant 
    531   1.90  perseant 		/*
    532   1.90  perseant 		 * If this inode is not going to be written any more, any
    533   1.90  perseant 		 * segment accounting left over from its truncation needs
    534   1.90  perseant 		 * to occur at the end of the next dirops flush.  Attach
    535   1.90  perseant 		 * them to the fs-wide list for that purpose.
    536   1.90  perseant 		 */
    537   1.90  perseant 		if (LIST_FIRST(&ip->i_lfs_segdhd) != NULL) {
    538   1.90  perseant 			struct segdelta *sd;
    539   1.90  perseant 
    540   1.90  perseant 			while((sd = LIST_FIRST(&ip->i_lfs_segdhd)) != NULL) {
    541   1.90  perseant 				LIST_REMOVE(sd, list);
    542   1.90  perseant 				LIST_INSERT_HEAD(&fs->lfs_segdhd, sd, list);
    543   1.90  perseant 			}
    544   1.90  perseant 		}
    545   1.90  perseant 	} else {
    546   1.90  perseant 		/*
    547   1.90  perseant 		 * If it's not a dirop, we can finalize right away.
    548   1.90  perseant 		 */
    549  1.105        ad 		mutex_exit(&lfs_lock);
    550   1.90  perseant 		lfs_finalize_ino_seguse(fs, ip);
    551   1.38  perseant 	}
    552   1.30  perseant 
    553  1.132  dholland 	/* it is no longer an unwritten inode, so update the counts */
    554  1.105        ad 	mutex_enter(&lfs_lock);
    555   1.42  perseant 	LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
    556  1.105        ad 	mutex_exit(&lfs_lock);
    557  1.132  dholland 
    558  1.132  dholland 	/* Turn off all inode modification flags */
    559   1.33  perseant 	ip->i_flag &= ~IN_ALLMOD;
    560  1.132  dholland 
    561  1.132  dholland 	/* Mark it deleted */
    562   1.93  perseant 	ip->i_lfs_iflags |= LFSI_DELETED;
    563   1.93  perseant 
    564  1.132  dholland 	/* Mark it free in the in-memory inode freemap */
    565  1.132  dholland 	SET_BITMAP_FREE(fs, ino);
    566  1.132  dholland 
    567    1.1   mycroft 	/*
    568    1.1   mycroft 	 * Set the ifile's inode entry to unused, increment its version number
    569   1.48  perseant 	 * and link it onto the free chain.
    570    1.1   mycroft 	 */
    571  1.132  dholland 
    572  1.132  dholland 	/* fetch the ifile entry */
    573    1.1   mycroft 	LFS_IENTRY(ifp, fs, ino, bp);
    574  1.132  dholland 
    575  1.132  dholland 	/* update the on-disk address (to "nowhere") */
    576  1.126  dholland 	old_iaddr = lfs_if_getdaddr(fs, ifp);
    577  1.126  dholland 	lfs_if_setdaddr(fs, ifp, LFS_UNUSED_DADDR);
    578  1.132  dholland 
    579  1.132  dholland 	/* bump the version */
    580  1.126  dholland 	lfs_if_setversion(fs, ifp, lfs_if_getversion(fs, ifp) + 1);
    581  1.132  dholland 
    582  1.125  dholland 	if (lfs_sb_getversion(fs) == 1) {
    583  1.126  dholland 		ino_t nextfree;
    584  1.126  dholland 
    585  1.132  dholland 		/* insert on freelist */
    586  1.126  dholland 		LFS_GET_HEADFREE(fs, cip, cbp, &nextfree);
    587  1.126  dholland 		lfs_if_setnextfree(fs, ifp, nextfree);
    588   1.48  perseant 		LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    589  1.132  dholland 
    590  1.132  dholland 		/* write the ifile block */
    591   1.56  perseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    592   1.48  perseant 	} else {
    593   1.87  perseant 		ino_t tino, onf;
    594   1.87  perseant 
    595  1.132  dholland 		/*
    596  1.132  dholland 		 * Clear the freelist next pointer and write the ifile
    597  1.132  dholland 		 * block. XXX: why? I'm sure there must be a reason but
    598  1.132  dholland 		 * it seems both silly and dangerous.
    599  1.132  dholland 		 */
    600  1.126  dholland 		lfs_if_setnextfree(fs, ifp, LFS_UNUSED_INUM);
    601   1.56  perseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    602   1.87  perseant 
    603  1.132  dholland 		/*
    604  1.132  dholland 		 * Insert on freelist in order.
    605  1.132  dholland 		 */
    606  1.132  dholland 
    607  1.132  dholland 		/* Find the next lower (by number) free inode */
    608   1.87  perseant 		tino = lfs_freelist_prev(fs, ino);
    609  1.132  dholland 
    610   1.87  perseant 		if (tino == LFS_UNUSED_INUM) {
    611  1.126  dholland 			ino_t nextfree;
    612  1.126  dholland 
    613  1.132  dholland 			/*
    614  1.132  dholland 			 * There isn't one; put us on the freelist head.
    615  1.132  dholland 			 */
    616  1.132  dholland 
    617  1.132  dholland 			/* reload the ifile block */
    618   1.87  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    619  1.132  dholland 			/* update the list */
    620  1.126  dholland 			LFS_GET_HEADFREE(fs, cip, cbp, &nextfree);
    621  1.126  dholland 			lfs_if_setnextfree(fs, ifp, nextfree);
    622   1.87  perseant 			LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    623   1.88  perseant 			DLOG((DLOG_ALLOC, "lfs_vfree: headfree %lld -> %lld\n",
    624  1.126  dholland 			     (long long)nextfree, (long long)ino));
    625  1.132  dholland 			/* write the ifile block */
    626   1.87  perseant 			LFS_BWRITE_LOG(bp); /* Ifile */
    627   1.87  perseant 
    628   1.87  perseant 			/* If the list was empty, set tail too */
    629   1.87  perseant 			LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    630   1.87  perseant 			if (otail == LFS_UNUSED_INUM) {
    631   1.87  perseant 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    632   1.87  perseant 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    633   1.87  perseant 				      "-> %lld\n", (long long)otail,
    634   1.87  perseant 				      (long long)ino));
    635   1.87  perseant 			}
    636   1.87  perseant 		} else {
    637   1.87  perseant 			/*
    638   1.87  perseant 			 * Insert this inode into the list after tino.
    639   1.87  perseant 			 * We hold the segment lock so we don't have to
    640   1.87  perseant 			 * worry about blocks being written out of order.
    641   1.87  perseant 			 */
    642  1.132  dholland 
    643   1.87  perseant 			DLOG((DLOG_ALLOC, "lfs_vfree: insert ino %lld "
    644   1.87  perseant 			      " after %lld\n", ino, tino));
    645   1.87  perseant 
    646  1.132  dholland 			/* load the previous inode's ifile block */
    647   1.87  perseant 			LFS_IENTRY(ifp, fs, tino, bp);
    648  1.132  dholland 			/* update the list pointer */
    649  1.126  dholland 			onf = lfs_if_getnextfree(fs, ifp);
    650  1.126  dholland 			lfs_if_setnextfree(fs, ifp, ino);
    651  1.132  dholland 			/* write the block */
    652   1.87  perseant 			LFS_BWRITE_LOG(bp);	/* Ifile */
    653   1.87  perseant 
    654  1.132  dholland 			/* load this inode's ifile block */
    655   1.87  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    656  1.132  dholland 			/* update the list pointer */
    657  1.126  dholland 			lfs_if_setnextfree(fs, ifp, onf);
    658  1.132  dholland 			/* write the block */
    659   1.87  perseant 			LFS_BWRITE_LOG(bp);	/* Ifile */
    660   1.87  perseant 
    661   1.87  perseant 			/* If we're last, put us on the tail */
    662   1.87  perseant 			if (onf == LFS_UNUSED_INUM) {
    663   1.87  perseant 				LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    664   1.87  perseant 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    665   1.87  perseant 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    666   1.87  perseant 				      "-> %lld\n", (long long)otail,
    667   1.87  perseant 				      (long long)ino));
    668   1.87  perseant 			}
    669   1.87  perseant 		}
    670   1.48  perseant 	}
    671   1.17  perseant #ifdef DIAGNOSTIC
    672  1.132  dholland 	/* XXX: shouldn't this check be further up *before* we trash the fs? */
    673   1.53       chs 	if (ino == LFS_UNUSED_INUM) {
    674   1.17  perseant 		panic("inode 0 freed");
    675   1.17  perseant 	}
    676   1.17  perseant #endif /* DIAGNOSTIC */
    677  1.132  dholland 
    678  1.132  dholland 	/*
    679  1.132  dholland 	 * Update the segment summary for the segment where the on-disk
    680  1.132  dholland 	 * copy used to be.
    681  1.132  dholland 	 */
    682    1.1   mycroft 	if (old_iaddr != LFS_UNUSED_DADDR) {
    683  1.132  dholland 		/* load it */
    684  1.117  christos 		LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, old_iaddr), bp);
    685   1.22  perseant #ifdef DIAGNOSTIC
    686  1.132  dholland 		/* the number of bytes in the segment should not become < 0 */
    687  1.127  dholland 		if (sup->su_nbytes < DINOSIZE(fs)) {
    688   1.43  perseant 			printf("lfs_vfree: negative byte count"
    689   1.62      yamt 			       " (segment %" PRIu32 " short by %d)\n",
    690  1.117  christos 			       lfs_dtosn(fs, old_iaddr),
    691  1.127  dholland 			       (int)DINOSIZE(fs) -
    692   1.66      fvdl 				    sup->su_nbytes);
    693   1.22  perseant 			panic("lfs_vfree: negative byte count");
    694  1.127  dholland 			sup->su_nbytes = DINOSIZE(fs);
    695   1.17  perseant 		}
    696   1.22  perseant #endif
    697  1.132  dholland 		/* update the number of bytes in the segment */
    698  1.127  dholland 		sup->su_nbytes -= DINOSIZE(fs);
    699  1.132  dholland 		/* write the segment entry */
    700  1.117  christos 		LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, old_iaddr), bp); /* Ifile */
    701    1.1   mycroft 	}
    702   1.75     perry 
    703  1.132  dholland 	/* Set superblock modified bit. */
    704  1.105        ad 	mutex_enter(&lfs_lock);
    705    1.1   mycroft 	fs->lfs_fmod = 1;
    706  1.105        ad 	mutex_exit(&lfs_lock);
    707  1.132  dholland 
    708  1.132  dholland 	/* Decrement file count. */
    709  1.122  dholland 	lfs_sb_subnfiles(fs, 1);
    710   1.75     perry 
    711   1.56  perseant 	lfs_segunlock(fs);
    712   1.63  perseant 
    713    1.1   mycroft 	return (0);
    714    1.1   mycroft }
    715   1.87  perseant 
    716   1.87  perseant /*
    717   1.87  perseant  * Sort the freelist and set up the free-inode bitmap.
    718   1.87  perseant  * To be called by lfs_mountfs().
    719  1.132  dholland  *
    720  1.132  dholland  * Takes the segmenet lock.
    721   1.87  perseant  */
    722   1.87  perseant void
    723   1.87  perseant lfs_order_freelist(struct lfs *fs)
    724   1.87  perseant {
    725   1.87  perseant 	CLEANERINFO *cip;
    726   1.87  perseant 	IFILE *ifp = NULL;
    727   1.87  perseant 	struct buf *bp;
    728   1.87  perseant 	ino_t ino, firstino, lastino, maxino;
    729   1.97  perseant #ifdef notyet
    730   1.97  perseant 	struct vnode *vp;
    731   1.97  perseant #endif
    732   1.87  perseant 
    733   1.95  perseant 	ASSERT_NO_SEGLOCK(fs);
    734   1.95  perseant 	lfs_seglock(fs, SEGM_PROT);
    735   1.95  perseant 
    736  1.132  dholland 	/* largest inode on fs */
    737  1.123  dholland 	maxino = ((fs->lfs_ivnode->v_size >> lfs_sb_getbshift(fs)) -
    738  1.122  dholland 		  lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) * lfs_sb_getifpb(fs);
    739  1.132  dholland 
    740  1.132  dholland 	/* allocate the in-memory inode freemap */
    741  1.132  dholland 	/* XXX: assert that fs->lfs_ino_bitmap is null here */
    742  1.121  dholland 	fs->lfs_ino_bitmap =
    743   1.88  perseant 		malloc(((maxino + BMMASK) >> BMSHIFT) * sizeof(lfs_bm_t),
    744   1.88  perseant 		       M_SEGMENT, M_WAITOK | M_ZERO);
    745   1.88  perseant 	KASSERT(fs->lfs_ino_bitmap != NULL);
    746   1.87  perseant 
    747  1.132  dholland 	/*
    748  1.132  dholland 	 * Scan the ifile.
    749  1.132  dholland 	 */
    750  1.132  dholland 
    751   1.87  perseant 	firstino = lastino = LFS_UNUSED_INUM;
    752   1.87  perseant 	for (ino = 0; ino < maxino; ino++) {
    753  1.132  dholland 		/* Load this inode's ifile entry. */
    754  1.122  dholland 		if (ino % lfs_sb_getifpb(fs) == 0)
    755   1.87  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    756   1.87  perseant 		else
    757  1.128   mlelstv 			LFS_IENTRY_NEXT(ifp, fs);
    758   1.87  perseant 
    759   1.87  perseant 		/* Don't put zero or ifile on the free list */
    760   1.87  perseant 		if (ino == LFS_UNUSED_INUM || ino == LFS_IFILE_INUM)
    761   1.87  perseant 			continue;
    762   1.87  perseant 
    763   1.97  perseant #ifdef notyet
    764  1.132  dholland 		/*
    765  1.132  dholland 		 * Address orphaned files.
    766  1.132  dholland 		 *
    767  1.132  dholland 		 * The idea of this is to free inodes belonging to
    768  1.132  dholland 		 * files that were unlinked but not reclaimed, I guess
    769  1.132  dholland 		 * because if we're going to scan the whole ifile
    770  1.132  dholland 		 * anyway it costs very little to do this. I don't
    771  1.132  dholland 		 * immediately see any reason this should be disabled,
    772  1.132  dholland 		 * but presumably it doesn't work... not sure what
    773  1.132  dholland 		 * happens to such files currently. -- dholland 20160806
    774  1.132  dholland 		 */
    775  1.126  dholland 		if (lfs_if_getnextfree(fs, ifp) == LFS_ORPHAN_NEXTFREE &&
    776   1.97  perseant 		    VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp) == 0) {
    777  1.126  dholland 			unsigned segno;
    778  1.126  dholland 
    779  1.132  dholland 			/* get the segment the inode in on disk  */
    780  1.126  dholland 			segno = lfs_dtosn(fs, lfs_if_getdaddr(fs, ifp));
    781  1.132  dholland 
    782  1.132  dholland 			/* truncate the inode */
    783  1.104        he 			lfs_truncate(vp, 0, 0, NOCRED);
    784   1.97  perseant 			vput(vp);
    785  1.132  dholland 
    786  1.132  dholland 			/* load the segment summary */
    787  1.126  dholland 			LFS_SEGENTRY(sup, fs, segno, bp);
    788  1.132  dholland 			/* update the number of bytes in the segment */
    789  1.131  dholland 			KASSERT(sup->su_nbytes >= DINOSIZE(fs));
    790  1.131  dholland 			sup->su_nbytes -= DINOSIZE(fs);
    791  1.132  dholland 			/* write the segment summary */
    792  1.126  dholland 			LFS_WRITESEGENTRY(sup, fs, segno, bp);
    793   1.97  perseant 
    794  1.132  dholland 			/* Drop the on-disk address */
    795  1.126  dholland 			lfs_if_setdaddr(fs, ifp, LFS_UNUSED_DADDR);
    796  1.132  dholland 			/* write the ifile entry */
    797   1.97  perseant 			LFS_BWRITE_LOG(bp);
    798  1.132  dholland 
    799  1.132  dholland 			/*
    800  1.132  dholland 			 * and reload it (XXX: why? I guess
    801  1.132  dholland 			 * LFS_BWRITE_LOG drops it...)
    802  1.132  dholland 			 */
    803   1.97  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    804  1.132  dholland 
    805  1.132  dholland 			/* Fall through to next if block */
    806   1.97  perseant 		}
    807   1.97  perseant #endif
    808   1.97  perseant 
    809  1.126  dholland 		if (lfs_if_getdaddr(fs, ifp) == LFS_UNUSED_DADDR) {
    810  1.132  dholland 
    811  1.132  dholland 			/*
    812  1.132  dholland 			 * This inode is free. Put it on the free list.
    813  1.132  dholland 			 */
    814  1.132  dholland 
    815  1.132  dholland 			if (firstino == LFS_UNUSED_INUM) {
    816  1.132  dholland 				/* XXX: assert lastino == LFS_UNUSED_INUM? */
    817  1.132  dholland 				/* remember the first free inode */
    818   1.87  perseant 				firstino = ino;
    819  1.132  dholland 			} else {
    820  1.132  dholland 				/* release this inode's ifile entry */
    821  1.102        ad 				brelse(bp, 0);
    822   1.87  perseant 
    823  1.132  dholland 				/* XXX: assert lastino != LFS_UNUSED_INUM? */
    824  1.132  dholland 
    825  1.132  dholland 				/* load lastino's ifile entry */
    826   1.87  perseant 				LFS_IENTRY(ifp, fs, lastino, bp);
    827  1.132  dholland 				/* set the list pointer */
    828  1.126  dholland 				lfs_if_setnextfree(fs, ifp, ino);
    829  1.132  dholland 				/* write the block */
    830   1.87  perseant 				LFS_BWRITE_LOG(bp);
    831  1.132  dholland 
    832  1.132  dholland 				/* reload this inode's ifile entry */
    833   1.87  perseant 				LFS_IENTRY(ifp, fs, ino, bp);
    834   1.87  perseant 			}
    835  1.132  dholland 			/* remember the last free inode seen so far */
    836   1.87  perseant 			lastino = ino;
    837   1.87  perseant 
    838  1.132  dholland 			/* Mark this inode free in the in-memory freemap */
    839   1.87  perseant 			SET_BITMAP_FREE(fs, ino);
    840   1.87  perseant 		}
    841   1.87  perseant 
    842  1.132  dholland 		/* If moving to the next ifile block, release the buffer. */
    843  1.122  dholland 		if ((ino + 1) % lfs_sb_getifpb(fs) == 0)
    844  1.102        ad 			brelse(bp, 0);
    845   1.87  perseant 	}
    846   1.87  perseant 
    847  1.132  dholland 	/* Write the freelist head and tail pointers */
    848  1.132  dholland 	/* XXX: do we need to mark the superblock dirty? */
    849   1.87  perseant 	LFS_PUT_HEADFREE(fs, cip, bp, firstino);
    850   1.87  perseant 	LFS_PUT_TAILFREE(fs, cip, bp, lastino);
    851   1.95  perseant 
    852  1.132  dholland 	/* done */
    853   1.95  perseant 	lfs_segunlock(fs);
    854   1.87  perseant }
    855   1.97  perseant 
    856  1.132  dholland /*
    857  1.132  dholland  * Mark a file orphaned (unlinked but not yet reclaimed) by inode
    858  1.132  dholland  * number. Do this with a magic freelist next pointer.
    859  1.132  dholland  *
    860  1.132  dholland  * XXX: howzabout some locking?
    861  1.132  dholland  */
    862   1.97  perseant void
    863   1.97  perseant lfs_orphan(struct lfs *fs, ino_t ino)
    864   1.97  perseant {
    865   1.97  perseant 	IFILE *ifp;
    866   1.97  perseant 	struct buf *bp;
    867   1.97  perseant 
    868   1.97  perseant 	LFS_IENTRY(ifp, fs, ino, bp);
    869  1.126  dholland 	lfs_if_setnextfree(fs, ifp, LFS_ORPHAN_NEXTFREE);
    870   1.97  perseant 	LFS_BWRITE_LOG(bp);
    871   1.97  perseant }
    872