Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.112.2.3
      1  1.112.2.2       tls /*	$NetBSD: lfs_alloc.c,v 1.112.2.3 2014/08/20 00:04:44 tls 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.112.2.2       tls __KERNEL_RCSID(0, "$NetBSD: lfs_alloc.c,v 1.112.2.3 2014/08/20 00:04:44 tls 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.112.2.2       tls #include <ufs/lfs/ulfs_quotacommon.h>
     84  1.112.2.2       tls #include <ufs/lfs/ulfs_inode.h>
     85  1.112.2.2       tls #include <ufs/lfs/ulfsmount.h>
     86  1.112.2.2       tls #include <ufs/lfs/ulfs_extern.h>
     87        1.1   mycroft 
     88        1.1   mycroft #include <ufs/lfs/lfs.h>
     89        1.1   mycroft #include <ufs/lfs/lfs_extern.h>
     90  1.112.2.3       tls #include <ufs/lfs/lfs_kernel.h>
     91        1.1   mycroft 
     92       1.87  perseant /* Constants for inode free bitmap */
     93       1.88  perseant #define BMSHIFT 5	/* 2 ** 5 = 32 */
     94       1.88  perseant #define BMMASK  ((1 << BMSHIFT) - 1)
     95       1.88  perseant #define SET_BITMAP_FREE(F, I) do { \
     96       1.88  perseant 	DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d set\n", (int)(I), 	\
     97       1.88  perseant 	     (int)((I) >> BMSHIFT), (int)((I) & BMMASK)));		\
     98       1.88  perseant 	(F)->lfs_ino_bitmap[(I) >> BMSHIFT] |= (1 << ((I) & BMMASK));	\
     99       1.88  perseant } while (0)
    100       1.88  perseant #define CLR_BITMAP_FREE(F, I) do { \
    101       1.88  perseant 	DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d clr\n", (int)(I), 	\
    102       1.88  perseant 	     (int)((I) >> BMSHIFT), (int)((I) & BMMASK)));		\
    103       1.88  perseant 	(F)->lfs_ino_bitmap[(I) >> BMSHIFT] &= ~(1 << ((I) & BMMASK));	\
    104       1.88  perseant } while(0)
    105       1.88  perseant 
    106       1.87  perseant #define ISSET_BITMAP_FREE(F, I) \
    107       1.88  perseant 	((F)->lfs_ino_bitmap[(I) >> BMSHIFT] & (1 << ((I) & BMMASK)))
    108       1.87  perseant 
    109       1.44  perseant /*
    110       1.65  perseant  * Add a new block to the Ifile, to accommodate future file creations.
    111       1.65  perseant  * Called with the segment lock held.
    112       1.56  perseant  */
    113       1.96  perseant int
    114       1.96  perseant lfs_extend_ifile(struct lfs *fs, kauth_cred_t cred)
    115       1.44  perseant {
    116       1.44  perseant 	struct vnode *vp;
    117       1.44  perseant 	struct inode *ip;
    118       1.44  perseant 	IFILE *ifp;
    119       1.48  perseant 	IFILE_V1 *ifp_v1;
    120       1.48  perseant 	struct buf *bp, *cbp;
    121       1.44  perseant 	int error;
    122       1.83  christos 	daddr_t i, blkno, xmax;
    123       1.87  perseant 	ino_t oldlast, maxino;
    124       1.48  perseant 	CLEANERINFO *cip;
    125       1.44  perseant 
    126       1.78  perseant 	ASSERT_SEGLOCK(fs);
    127       1.78  perseant 
    128       1.44  perseant 	vp = fs->lfs_ivnode;
    129       1.44  perseant 	ip = VTOI(vp);
    130  1.112.2.2       tls 	blkno = lfs_lblkno(fs, ip->i_size);
    131       1.85      yamt 	if ((error = lfs_balloc(vp, ip->i_size, fs->lfs_bsize, cred, 0,
    132       1.44  perseant 				&bp)) != 0) {
    133       1.44  perseant 		return (error);
    134       1.44  perseant 	}
    135       1.66      fvdl 	ip->i_size += fs->lfs_bsize;
    136       1.66      fvdl 	ip->i_ffs1_size = ip->i_size;
    137       1.66      fvdl 	uvm_vnp_setsize(vp, ip->i_size);
    138       1.75     perry 
    139       1.88  perseant 	maxino = ((ip->i_size >> fs->lfs_bshift) - fs->lfs_cleansz -
    140       1.88  perseant 		  fs->lfs_segtabsz) * fs->lfs_ifpb;
    141       1.88  perseant 	fs->lfs_ino_bitmap = (lfs_bm_t *)
    142       1.88  perseant 		realloc(fs->lfs_ino_bitmap, ((maxino + BMMASK) >> BMSHIFT) *
    143       1.88  perseant 			sizeof(lfs_bm_t), M_SEGMENT, M_WAITOK);
    144       1.88  perseant 	KASSERT(fs->lfs_ino_bitmap != NULL);
    145       1.87  perseant 
    146       1.44  perseant 	i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
    147       1.44  perseant 		fs->lfs_ifpb;
    148       1.87  perseant 
    149       1.87  perseant 	/*
    150       1.87  perseant 	 * We insert the new inodes at the head of the free list.
    151       1.87  perseant 	 * Under normal circumstances, the free list is empty here,
    152       1.87  perseant 	 * so we are also incidentally placing them at the end (which
    153       1.87  perseant 	 * we must do if we are to keep them in order).
    154       1.87  perseant 	 */
    155       1.48  perseant 	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
    156       1.48  perseant 	LFS_PUT_HEADFREE(fs, cip, cbp, i);
    157       1.44  perseant #ifdef DIAGNOSTIC
    158       1.63  perseant 	if (fs->lfs_freehd == LFS_UNUSED_INUM)
    159       1.44  perseant 		panic("inode 0 allocated [2]");
    160       1.44  perseant #endif /* DIAGNOSTIC */
    161       1.83  christos 	xmax = i + fs->lfs_ifpb;
    162       1.48  perseant 
    163       1.53       chs 	if (fs->lfs_version == 1) {
    164       1.83  christos 		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < xmax; ++ifp_v1) {
    165       1.89  perseant 			SET_BITMAP_FREE(fs, i);
    166       1.48  perseant 			ifp_v1->if_version = 1;
    167       1.48  perseant 			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
    168       1.48  perseant 			ifp_v1->if_nextfree = ++i;
    169       1.48  perseant 		}
    170       1.48  perseant 		ifp_v1--;
    171       1.48  perseant 		ifp_v1->if_nextfree = oldlast;
    172       1.48  perseant 	} else {
    173       1.83  christos 		for (ifp = (IFILE *)bp->b_data; i < xmax; ++ifp) {
    174       1.89  perseant 			SET_BITMAP_FREE(fs, i);
    175       1.48  perseant 			ifp->if_version = 1;
    176       1.48  perseant 			ifp->if_daddr = LFS_UNUSED_DADDR;
    177       1.48  perseant 			ifp->if_nextfree = ++i;
    178       1.48  perseant 		}
    179       1.48  perseant 		ifp--;
    180       1.48  perseant 		ifp->if_nextfree = oldlast;
    181       1.44  perseant 	}
    182       1.83  christos 	LFS_PUT_TAILFREE(fs, cip, cbp, xmax - 1);
    183       1.48  perseant 
    184       1.56  perseant 	(void) LFS_BWRITE_LOG(bp); /* Ifile */
    185       1.44  perseant 
    186       1.44  perseant 	return 0;
    187       1.44  perseant }
    188       1.44  perseant 
    189        1.1   mycroft /* Allocate a new inode. */
    190        1.1   mycroft /* ARGSUSED */
    191       1.43  perseant /* VOP_BWRITE 2i times */
    192        1.1   mycroft int
    193       1.99  christos lfs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred,
    194       1.98  christos     struct vnode **vpp)
    195        1.3  christos {
    196        1.1   mycroft 	struct lfs *fs;
    197       1.48  perseant 	struct buf *bp, *cbp;
    198        1.1   mycroft 	struct ifile *ifp;
    199        1.1   mycroft 	ino_t new_ino;
    200        1.1   mycroft 	int error;
    201       1.36  perseant 	int new_gen;
    202       1.48  perseant 	CLEANERINFO *cip;
    203        1.1   mycroft 
    204       1.85      yamt 	fs = VTOI(pvp)->i_lfs;
    205       1.38  perseant 	if (fs->lfs_ronly)
    206       1.38  perseant 		return EROFS;
    207       1.75     perry 
    208       1.78  perseant 	ASSERT_NO_SEGLOCK(fs);
    209       1.78  perseant 
    210       1.56  perseant 	lfs_seglock(fs, SEGM_PROT);
    211       1.17  perseant 
    212        1.1   mycroft 	/* Get the head of the freelist. */
    213       1.48  perseant 	LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
    214       1.87  perseant 	KASSERT(new_ino != LFS_UNUSED_INUM && new_ino != LFS_IFILE_INUM);
    215       1.48  perseant 
    216       1.87  perseant 	DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %lld\n",
    217       1.87  perseant 	     (long long)new_ino));
    218       1.75     perry 
    219        1.1   mycroft 	/*
    220        1.1   mycroft 	 * Remove the inode from the free list and write the new start
    221        1.1   mycroft 	 * of the free list into the superblock.
    222        1.1   mycroft 	 */
    223       1.87  perseant 	CLR_BITMAP_FREE(fs, new_ino);
    224        1.1   mycroft 	LFS_IENTRY(ifp, fs, new_ino, bp);
    225        1.1   mycroft 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
    226       1.84  christos 		panic("lfs_valloc: inuse inode %llu on the free list",
    227       1.84  christos 		    (unsigned long long)new_ino);
    228       1.48  perseant 	LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
    229       1.87  perseant 	DLOG((DLOG_ALLOC, "lfs_valloc: headfree %lld -> %lld\n",
    230       1.87  perseant 	     (long long)new_ino, (long long)ifp->if_nextfree));
    231       1.48  perseant 
    232       1.37  perseant 	new_gen = ifp->if_version; /* version was updated by vfree */
    233      1.102        ad 	brelse(bp, 0);
    234       1.30  perseant 
    235        1.1   mycroft 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    236       1.63  perseant 	if (fs->lfs_freehd == LFS_UNUSED_INUM) {
    237       1.96  perseant 		if ((error = lfs_extend_ifile(fs, cred)) != 0) {
    238       1.48  perseant 			LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
    239       1.56  perseant 			lfs_segunlock(fs);
    240       1.44  perseant 			return error;
    241        1.1   mycroft 		}
    242        1.1   mycroft 	}
    243       1.17  perseant #ifdef DIAGNOSTIC
    244       1.63  perseant 	if (fs->lfs_freehd == LFS_UNUSED_INUM)
    245       1.17  perseant 		panic("inode 0 allocated [3]");
    246       1.17  perseant #endif /* DIAGNOSTIC */
    247       1.48  perseant 
    248       1.78  perseant 	/* Set superblock modified bit and increment file count. */
    249      1.105        ad 	mutex_enter(&lfs_lock);
    250       1.78  perseant 	fs->lfs_fmod = 1;
    251      1.105        ad 	mutex_exit(&lfs_lock);
    252       1.78  perseant 	++fs->lfs_nfiles;
    253       1.78  perseant 
    254       1.56  perseant 	lfs_segunlock(fs);
    255       1.63  perseant 
    256       1.85      yamt 	return lfs_ialloc(fs, pvp, new_ino, new_gen, vpp);
    257       1.44  perseant }
    258       1.44  perseant 
    259       1.65  perseant /*
    260       1.65  perseant  * Finish allocating a new inode, given an inode and generation number.
    261       1.65  perseant  */
    262       1.96  perseant int
    263       1.44  perseant lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen,
    264       1.44  perseant 	   struct vnode **vpp)
    265       1.44  perseant {
    266       1.44  perseant 	struct inode *ip;
    267       1.44  perseant 	struct vnode *vp;
    268       1.40      fvdl 
    269       1.78  perseant 	ASSERT_NO_SEGLOCK(fs);
    270       1.78  perseant 
    271       1.77  perseant 	vp = *vpp;
    272  1.112.2.2       tls 	mutex_enter(&ulfs_hashlock);
    273       1.40      fvdl 	/* Create an inode to associate with the vnode. */
    274       1.44  perseant 	lfs_vcreate(pvp->v_mount, new_ino, vp);
    275       1.51       chs 
    276        1.1   mycroft 	ip = VTOI(vp);
    277      1.105        ad 	mutex_enter(&lfs_lock);
    278       1.73   mycroft 	LFS_SET_UINO(ip, IN_CHANGE);
    279      1.105        ad 	mutex_exit(&lfs_lock);
    280       1.66      fvdl 	/* on-disk structure has been zeroed out by lfs_vcreate */
    281       1.66      fvdl 	ip->i_din.ffs1_din->di_inumber = new_ino;
    282       1.51       chs 
    283       1.79  perseant 	/* Note no blocks yet */
    284       1.79  perseant 	ip->i_lfs_hiblk = -1;
    285       1.79  perseant 
    286        1.1   mycroft 	/* Set a new generation number for this inode. */
    287       1.66      fvdl 	if (new_gen) {
    288       1.66      fvdl 		ip->i_gen = new_gen;
    289       1.66      fvdl 		ip->i_ffs1_gen = new_gen;
    290       1.66      fvdl 	}
    291       1.51       chs 
    292        1.1   mycroft 	/* Insert into the inode hash table. */
    293  1.112.2.2       tls 	ulfs_ihashins(ip);
    294  1.112.2.2       tls 	mutex_exit(&ulfs_hashlock);
    295       1.44  perseant 
    296  1.112.2.2       tls 	ulfs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, vpp);
    297       1.77  perseant 	vp = *vpp;
    298       1.44  perseant 	ip = VTOI(vp);
    299       1.57      yamt 
    300  1.112.2.2       tls 	memset(ip->i_lfs_fragsize, 0, ULFS_NDADDR * sizeof(*ip->i_lfs_fragsize));
    301       1.51       chs 
    302       1.51       chs 	uvm_vnp_setsize(vp, 0);
    303       1.72      yamt 	lfs_mark_vnode(vp);
    304       1.54       chs 	genfs_node_init(vp, &lfs_genfsops);
    305      1.109     pooka 	vref(ip->i_devvp);
    306        1.1   mycroft 	return (0);
    307        1.1   mycroft }
    308        1.1   mycroft 
    309        1.1   mycroft /* Create a new vnode/inode pair and initialize what fields we can. */
    310       1.40      fvdl void
    311       1.48  perseant lfs_vcreate(struct mount *mp, ino_t ino, struct vnode *vp)
    312        1.1   mycroft {
    313        1.1   mycroft 	struct inode *ip;
    314  1.112.2.2       tls 	struct ulfs1_dinode *dp;
    315  1.112.2.2       tls 	struct ulfsmount *ump;
    316       1.75     perry 
    317        1.1   mycroft 	/* Get a pointer to the private mount structure. */
    318  1.112.2.2       tls 	ump = VFSTOULFS(mp);
    319       1.75     perry 
    320       1.78  perseant 	ASSERT_NO_SEGLOCK(ump->um_lfs);
    321       1.78  perseant 
    322        1.1   mycroft 	/* Initialize the inode. */
    323       1.15   thorpej 	ip = pool_get(&lfs_inode_pool, PR_WAITOK);
    324       1.66      fvdl 	memset(ip, 0, sizeof(*ip));
    325       1.66      fvdl 	dp = pool_get(&lfs_dinode_pool, PR_WAITOK);
    326       1.66      fvdl 	memset(dp, 0, sizeof(*dp));
    327       1.63  perseant 	ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK);
    328       1.74  perseant 	memset(ip->inode_ext.lfs, 0, sizeof(*ip->inode_ext.lfs));
    329       1.40      fvdl 	vp->v_data = ip;
    330       1.66      fvdl 	ip->i_din.ffs1_din = dp;
    331       1.66      fvdl 	ip->i_ump = ump;
    332       1.40      fvdl 	ip->i_vnode = vp;
    333        1.1   mycroft 	ip->i_devvp = ump->um_devvp;
    334        1.1   mycroft 	ip->i_dev = ump->um_dev;
    335       1.66      fvdl 	ip->i_number = dp->di_inumber = ino;
    336        1.1   mycroft 	ip->i_lfs = ump->um_lfs;
    337       1.66      fvdl 	ip->i_lfs_effnblks = 0;
    338       1.81  perseant 	SPLAY_INIT(&ip->i_lfs_lbtree);
    339       1.82  perseant 	ip->i_lfs_nbtree = 0;
    340       1.91  perseant 	LIST_INIT(&ip->i_lfs_segdhd);
    341  1.112.2.3       tls #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    342  1.112.2.2       tls 	ulfsquota_init(ip);
    343        1.1   mycroft #endif
    344        1.1   mycroft }
    345        1.1   mycroft 
    346       1.87  perseant #if 0
    347       1.87  perseant /*
    348       1.87  perseant  * Find the highest-numbered allocated inode.
    349       1.87  perseant  * This will be used to shrink the Ifile.
    350       1.87  perseant  */
    351       1.87  perseant static inline ino_t
    352       1.87  perseant lfs_last_alloc_ino(struct lfs *fs)
    353       1.87  perseant {
    354       1.87  perseant 	ino_t ino, maxino;
    355       1.87  perseant 
    356       1.87  perseant 	maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
    357       1.87  perseant 		  fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
    358       1.87  perseant 	for (ino = maxino - 1; ino > LFS_UNUSED_INUM; --ino) {
    359       1.87  perseant 		if (ISSET_BITMAP_FREE(fs, ino) == 0)
    360       1.87  perseant 			break;
    361       1.87  perseant 	}
    362       1.87  perseant 	return ino;
    363       1.87  perseant }
    364       1.87  perseant #endif
    365       1.87  perseant 
    366       1.87  perseant /*
    367       1.87  perseant  * Find the previous (next lowest numbered) free inode, if any.
    368       1.87  perseant  * If there is none, return LFS_UNUSED_INUM.
    369       1.87  perseant  */
    370       1.87  perseant static inline ino_t
    371       1.87  perseant lfs_freelist_prev(struct lfs *fs, ino_t ino)
    372       1.87  perseant {
    373       1.88  perseant 	ino_t tino, bound, bb, freehdbb;
    374       1.88  perseant 
    375       1.88  perseant 	if (fs->lfs_freehd == LFS_UNUSED_INUM)	 /* No free inodes at all */
    376       1.88  perseant 		return LFS_UNUSED_INUM;
    377       1.88  perseant 
    378       1.88  perseant 	/* Search our own word first */
    379       1.88  perseant 	bound = ino & ~BMMASK;
    380       1.89  perseant 	for (tino = ino - 1; tino >= bound && tino > LFS_UNUSED_INUM; tino--)
    381       1.88  perseant 		if (ISSET_BITMAP_FREE(fs, tino))
    382       1.88  perseant 			return tino;
    383       1.88  perseant 	/* If there are no lower words to search, just return */
    384       1.88  perseant 	if (ino >> BMSHIFT == 0)
    385       1.88  perseant 		return LFS_UNUSED_INUM;
    386       1.88  perseant 
    387       1.88  perseant 	/*
    388       1.88  perseant 	 * Find a word with a free inode in it.  We have to be a bit
    389       1.88  perseant 	 * careful here since ino_t is unsigned.
    390       1.88  perseant 	 */
    391       1.88  perseant 	freehdbb = (fs->lfs_freehd >> BMSHIFT);
    392       1.88  perseant 	for (bb = (ino >> BMSHIFT) - 1; bb >= freehdbb && bb > 0; --bb)
    393       1.88  perseant 		if (fs->lfs_ino_bitmap[bb])
    394       1.88  perseant 			break;
    395       1.88  perseant 	if (fs->lfs_ino_bitmap[bb] == 0)
    396       1.88  perseant 		return LFS_UNUSED_INUM;
    397       1.88  perseant 
    398       1.88  perseant 	/* Search the word we found */
    399       1.89  perseant 	for (tino = (bb << BMSHIFT) | BMMASK; tino >= (bb << BMSHIFT) &&
    400       1.89  perseant 	     tino > LFS_UNUSED_INUM; tino--)
    401       1.88  perseant 		if (ISSET_BITMAP_FREE(fs, tino))
    402       1.88  perseant 			break;
    403       1.87  perseant 
    404       1.87  perseant 	if (tino <= LFS_IFILE_INUM)
    405       1.87  perseant 		tino = LFS_UNUSED_INUM;
    406       1.87  perseant 
    407       1.87  perseant 	return tino;
    408       1.87  perseant }
    409       1.87  perseant 
    410        1.1   mycroft /* Free an inode. */
    411        1.1   mycroft /* ARGUSED */
    412       1.43  perseant /* VOP_BWRITE 2i times */
    413        1.1   mycroft int
    414       1.99  christos lfs_vfree(struct vnode *vp, ino_t ino, int mode)
    415        1.3  christos {
    416        1.1   mycroft 	SEGUSE *sup;
    417       1.48  perseant 	CLEANERINFO *cip;
    418       1.48  perseant 	struct buf *cbp, *bp;
    419        1.1   mycroft 	struct ifile *ifp;
    420        1.1   mycroft 	struct inode *ip;
    421        1.1   mycroft 	struct lfs *fs;
    422       1.59      fvdl 	daddr_t old_iaddr;
    423       1.85      yamt 	ino_t otail;
    424       1.75     perry 
    425        1.1   mycroft 	/* Get the inode number and file system. */
    426       1.30  perseant 	ip = VTOI(vp);
    427        1.1   mycroft 	fs = ip->i_lfs;
    428        1.1   mycroft 	ino = ip->i_number;
    429       1.34  perseant 
    430       1.78  perseant 	ASSERT_NO_SEGLOCK(fs);
    431       1.88  perseant 	DLOG((DLOG_ALLOC, "lfs_vfree: free ino %lld\n", (long long)ino));
    432       1.78  perseant 
    433       1.48  perseant 	/* Drain of pending writes */
    434      1.111     rmind 	mutex_enter(vp->v_interlock);
    435      1.105        ad 	while (fs->lfs_version > 1 && WRITEINPROG(vp)) {
    436      1.111     rmind 		cv_wait(&vp->v_cv, vp->v_interlock);
    437      1.105        ad 	}
    438      1.111     rmind 	mutex_exit(vp->v_interlock);
    439       1.48  perseant 
    440       1.63  perseant 	lfs_seglock(fs, SEGM_PROT);
    441       1.75     perry 
    442       1.72      yamt 	lfs_unmark_vnode(vp);
    443      1.105        ad 	mutex_enter(&lfs_lock);
    444      1.103        ad 	if (vp->v_uflag & VU_DIROP) {
    445      1.103        ad 		vp->v_uflag &= ~VU_DIROP;
    446       1.30  perseant 		--lfs_dirvcount;
    447       1.92  perseant 		--fs->lfs_dirvcount;
    448       1.63  perseant 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    449       1.92  perseant 		wakeup(&fs->lfs_dirvcount);
    450       1.30  perseant 		wakeup(&lfs_dirvcount);
    451      1.105        ad 		mutex_exit(&lfs_lock);
    452       1.30  perseant 		lfs_vunref(vp);
    453       1.90  perseant 
    454       1.90  perseant 		/*
    455       1.90  perseant 		 * If this inode is not going to be written any more, any
    456       1.90  perseant 		 * segment accounting left over from its truncation needs
    457       1.90  perseant 		 * to occur at the end of the next dirops flush.  Attach
    458       1.90  perseant 		 * them to the fs-wide list for that purpose.
    459       1.90  perseant 		 */
    460       1.90  perseant 		if (LIST_FIRST(&ip->i_lfs_segdhd) != NULL) {
    461       1.90  perseant 			struct segdelta *sd;
    462       1.90  perseant 
    463       1.90  perseant 			while((sd = LIST_FIRST(&ip->i_lfs_segdhd)) != NULL) {
    464       1.90  perseant 				LIST_REMOVE(sd, list);
    465       1.90  perseant 				LIST_INSERT_HEAD(&fs->lfs_segdhd, sd, list);
    466       1.90  perseant 			}
    467       1.90  perseant 		}
    468       1.90  perseant 	} else {
    469       1.90  perseant 		/*
    470       1.90  perseant 		 * If it's not a dirop, we can finalize right away.
    471       1.90  perseant 		 */
    472      1.105        ad 		mutex_exit(&lfs_lock);
    473       1.90  perseant 		lfs_finalize_ino_seguse(fs, ip);
    474       1.38  perseant 	}
    475       1.30  perseant 
    476      1.105        ad 	mutex_enter(&lfs_lock);
    477       1.42  perseant 	LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
    478      1.105        ad 	mutex_exit(&lfs_lock);
    479       1.33  perseant 	ip->i_flag &= ~IN_ALLMOD;
    480       1.93  perseant 	ip->i_lfs_iflags |= LFSI_DELETED;
    481       1.93  perseant 
    482        1.1   mycroft 	/*
    483        1.1   mycroft 	 * Set the ifile's inode entry to unused, increment its version number
    484       1.48  perseant 	 * and link it onto the free chain.
    485        1.1   mycroft 	 */
    486       1.87  perseant 	SET_BITMAP_FREE(fs, ino);
    487        1.1   mycroft 	LFS_IENTRY(ifp, fs, ino, bp);
    488        1.1   mycroft 	old_iaddr = ifp->if_daddr;
    489        1.1   mycroft 	ifp->if_daddr = LFS_UNUSED_DADDR;
    490        1.1   mycroft 	++ifp->if_version;
    491       1.48  perseant 	if (fs->lfs_version == 1) {
    492       1.48  perseant 		LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    493       1.48  perseant 		LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    494       1.56  perseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    495       1.48  perseant 	} else {
    496       1.87  perseant 		ino_t tino, onf;
    497       1.87  perseant 
    498       1.48  perseant 		ifp->if_nextfree = LFS_UNUSED_INUM;
    499       1.56  perseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    500       1.87  perseant 
    501       1.87  perseant 		tino = lfs_freelist_prev(fs, ino);
    502       1.87  perseant 		if (tino == LFS_UNUSED_INUM) {
    503       1.87  perseant 			/* Nothing free below us, put us on the head */
    504       1.87  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    505       1.87  perseant 			LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    506       1.87  perseant 			LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    507       1.88  perseant 			DLOG((DLOG_ALLOC, "lfs_vfree: headfree %lld -> %lld\n",
    508       1.87  perseant 			     (long long)ifp->if_nextfree, (long long)ino));
    509       1.87  perseant 			LFS_BWRITE_LOG(bp); /* Ifile */
    510       1.87  perseant 
    511       1.87  perseant 			/* If the list was empty, set tail too */
    512       1.87  perseant 			LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    513       1.87  perseant 			if (otail == LFS_UNUSED_INUM) {
    514       1.87  perseant 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    515       1.87  perseant 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    516       1.87  perseant 				      "-> %lld\n", (long long)otail,
    517       1.87  perseant 				      (long long)ino));
    518       1.87  perseant 			}
    519       1.87  perseant 		} else {
    520       1.87  perseant 			/*
    521       1.87  perseant 			 * Insert this inode into the list after tino.
    522       1.87  perseant 			 * We hold the segment lock so we don't have to
    523       1.87  perseant 			 * worry about blocks being written out of order.
    524       1.87  perseant 			 */
    525       1.87  perseant 			DLOG((DLOG_ALLOC, "lfs_vfree: insert ino %lld "
    526       1.87  perseant 			      " after %lld\n", ino, tino));
    527       1.87  perseant 
    528       1.87  perseant 			LFS_IENTRY(ifp, fs, tino, bp);
    529       1.87  perseant 			onf = ifp->if_nextfree;
    530       1.87  perseant 			ifp->if_nextfree = ino;
    531       1.87  perseant 			LFS_BWRITE_LOG(bp);	/* Ifile */
    532       1.87  perseant 
    533       1.87  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    534       1.87  perseant 			ifp->if_nextfree = onf;
    535       1.87  perseant 			LFS_BWRITE_LOG(bp);	/* Ifile */
    536       1.87  perseant 
    537       1.87  perseant 			/* If we're last, put us on the tail */
    538       1.87  perseant 			if (onf == LFS_UNUSED_INUM) {
    539       1.87  perseant 				LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    540       1.87  perseant 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    541       1.87  perseant 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    542       1.87  perseant 				      "-> %lld\n", (long long)otail,
    543       1.87  perseant 				      (long long)ino));
    544       1.87  perseant 			}
    545       1.87  perseant 		}
    546       1.48  perseant 	}
    547       1.17  perseant #ifdef DIAGNOSTIC
    548       1.53       chs 	if (ino == LFS_UNUSED_INUM) {
    549       1.17  perseant 		panic("inode 0 freed");
    550       1.17  perseant 	}
    551       1.17  perseant #endif /* DIAGNOSTIC */
    552        1.1   mycroft 	if (old_iaddr != LFS_UNUSED_DADDR) {
    553  1.112.2.2       tls 		LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, old_iaddr), bp);
    554       1.22  perseant #ifdef DIAGNOSTIC
    555  1.112.2.2       tls 		if (sup->su_nbytes < sizeof (struct ulfs1_dinode)) {
    556       1.43  perseant 			printf("lfs_vfree: negative byte count"
    557       1.62      yamt 			       " (segment %" PRIu32 " short by %d)\n",
    558  1.112.2.2       tls 			       lfs_dtosn(fs, old_iaddr),
    559  1.112.2.2       tls 			       (int)sizeof (struct ulfs1_dinode) -
    560       1.66      fvdl 				    sup->su_nbytes);
    561       1.22  perseant 			panic("lfs_vfree: negative byte count");
    562  1.112.2.2       tls 			sup->su_nbytes = sizeof (struct ulfs1_dinode);
    563       1.17  perseant 		}
    564       1.22  perseant #endif
    565  1.112.2.2       tls 		sup->su_nbytes -= sizeof (struct ulfs1_dinode);
    566  1.112.2.2       tls 		LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, old_iaddr), bp); /* Ifile */
    567        1.1   mycroft 	}
    568       1.75     perry 
    569        1.1   mycroft 	/* Set superblock modified bit and decrement file count. */
    570      1.105        ad 	mutex_enter(&lfs_lock);
    571        1.1   mycroft 	fs->lfs_fmod = 1;
    572      1.105        ad 	mutex_exit(&lfs_lock);
    573        1.1   mycroft 	--fs->lfs_nfiles;
    574       1.75     perry 
    575       1.56  perseant 	lfs_segunlock(fs);
    576       1.63  perseant 
    577        1.1   mycroft 	return (0);
    578        1.1   mycroft }
    579       1.87  perseant 
    580       1.87  perseant /*
    581       1.87  perseant  * Sort the freelist and set up the free-inode bitmap.
    582       1.87  perseant  * To be called by lfs_mountfs().
    583       1.87  perseant  */
    584       1.87  perseant void
    585       1.87  perseant lfs_order_freelist(struct lfs *fs)
    586       1.87  perseant {
    587       1.87  perseant 	CLEANERINFO *cip;
    588       1.87  perseant 	IFILE *ifp = NULL;
    589       1.87  perseant 	struct buf *bp;
    590       1.87  perseant 	ino_t ino, firstino, lastino, maxino;
    591       1.97  perseant #ifdef notyet
    592       1.97  perseant 	struct vnode *vp;
    593       1.97  perseant #endif
    594       1.87  perseant 
    595       1.95  perseant 	ASSERT_NO_SEGLOCK(fs);
    596       1.95  perseant 	lfs_seglock(fs, SEGM_PROT);
    597       1.95  perseant 
    598       1.87  perseant 	maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
    599       1.87  perseant 		  fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
    600       1.88  perseant 	fs->lfs_ino_bitmap = (lfs_bm_t *)
    601       1.88  perseant 		malloc(((maxino + BMMASK) >> BMSHIFT) * sizeof(lfs_bm_t),
    602       1.88  perseant 		       M_SEGMENT, M_WAITOK | M_ZERO);
    603       1.88  perseant 	KASSERT(fs->lfs_ino_bitmap != NULL);
    604       1.87  perseant 
    605       1.87  perseant 	firstino = lastino = LFS_UNUSED_INUM;
    606       1.87  perseant 	for (ino = 0; ino < maxino; ino++) {
    607       1.87  perseant 		if (ino % fs->lfs_ifpb == 0)
    608       1.87  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    609       1.87  perseant 		else
    610       1.87  perseant 			++ifp;
    611       1.87  perseant 
    612       1.87  perseant 		/* Don't put zero or ifile on the free list */
    613       1.87  perseant 		if (ino == LFS_UNUSED_INUM || ino == LFS_IFILE_INUM)
    614       1.87  perseant 			continue;
    615       1.87  perseant 
    616       1.97  perseant #ifdef notyet
    617       1.97  perseant 		/* Address orphaned files */
    618       1.97  perseant 		if (ifp->if_nextfree == LFS_ORPHAN_NEXTFREE &&
    619       1.97  perseant 		    VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp) == 0) {
    620      1.104        he 			lfs_truncate(vp, 0, 0, NOCRED);
    621       1.97  perseant 			vput(vp);
    622  1.112.2.2       tls 			LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, ifp->if_daddr), bp);
    623       1.97  perseant 			KASSERT(sup->su_nbytes >= DINODE1_SIZE);
    624       1.97  perseant 			sup->su_nbytes -= DINODE1_SIZE;
    625  1.112.2.2       tls 			LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, ifp->if_daddr), bp);
    626       1.97  perseant 
    627       1.97  perseant 			/* Set up to fall through to next section */
    628       1.97  perseant 			ifp->if_daddr = LFS_UNUSED_DADDR;
    629       1.97  perseant 			LFS_BWRITE_LOG(bp);
    630       1.97  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    631       1.97  perseant 		}
    632       1.97  perseant #endif
    633       1.97  perseant 
    634       1.87  perseant 		if (ifp->if_daddr == LFS_UNUSED_DADDR) {
    635       1.87  perseant 			if (firstino == LFS_UNUSED_INUM)
    636       1.87  perseant 				firstino = ino;
    637       1.87  perseant 			else {
    638      1.102        ad 				brelse(bp, 0);
    639       1.87  perseant 
    640       1.87  perseant 				LFS_IENTRY(ifp, fs, lastino, bp);
    641       1.87  perseant 				ifp->if_nextfree = ino;
    642       1.87  perseant 				LFS_BWRITE_LOG(bp);
    643       1.87  perseant 
    644       1.87  perseant 				LFS_IENTRY(ifp, fs, ino, bp);
    645       1.87  perseant 			}
    646       1.87  perseant 			lastino = ino;
    647       1.87  perseant 
    648       1.87  perseant 			SET_BITMAP_FREE(fs, ino);
    649       1.87  perseant 		}
    650       1.87  perseant 
    651       1.87  perseant 		if ((ino + 1) % fs->lfs_ifpb == 0)
    652      1.102        ad 			brelse(bp, 0);
    653       1.87  perseant 	}
    654       1.87  perseant 
    655       1.87  perseant 	LFS_PUT_HEADFREE(fs, cip, bp, firstino);
    656       1.87  perseant 	LFS_PUT_TAILFREE(fs, cip, bp, lastino);
    657       1.95  perseant 
    658       1.95  perseant 	lfs_segunlock(fs);
    659       1.87  perseant }
    660       1.97  perseant 
    661       1.97  perseant void
    662       1.97  perseant lfs_orphan(struct lfs *fs, ino_t ino)
    663       1.97  perseant {
    664       1.97  perseant 	IFILE *ifp;
    665       1.97  perseant 	struct buf *bp;
    666       1.97  perseant 
    667       1.97  perseant 	LFS_IENTRY(ifp, fs, ino, bp);
    668       1.97  perseant 	ifp->if_nextfree = LFS_ORPHAN_NEXTFREE;
    669       1.97  perseant 	LFS_BWRITE_LOG(bp);
    670       1.97  perseant }
    671