Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.131.2.1
      1  1.131.2.1  pgoyette /*	$NetBSD: lfs_alloc.c,v 1.131.2.1 2017/03/20 06:57:54 pgoyette 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.131.2.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: lfs_alloc.c,v 1.131.2.1 2017/03/20 06:57:54 pgoyette 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.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.122  dholland 	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.122  dholland 	ip->i_size += lfs_sb_getbsize(fs);
    138      1.129  dholland 	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.123  dholland 	maxino = ((ip->i_size >> lfs_sb_getbshift(fs)) - lfs_sb_getcleansz(fs) -
    142      1.122  dholland 		  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.122  dholland 	i = (blkno - lfs_sb_getsegtabsz(fs) - lfs_sb_getcleansz(fs)) *
    149      1.122  dholland 		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.131.2.1  pgoyette 	KASSERTMSG((lfs_sb_getfreehd(fs) != LFS_UNUSED_INUM),
    160  1.131.2.1  pgoyette 	    "inode 0 allocated [2]");
    161  1.131.2.1  pgoyette 
    162  1.131.2.1  pgoyette 	/* inode number to stop at (XXX: why *x*max?) */
    163      1.122  dholland 	xmax = i + lfs_sb_getifpb(fs);
    164       1.48  perseant 
    165      1.126  dholland 	if (fs->lfs_is64) {
    166      1.126  dholland 		for (ifp64 = (IFILE64 *)bp->b_data; i < xmax; ++ifp64) {
    167      1.126  dholland 			SET_BITMAP_FREE(fs, i);
    168      1.126  dholland 			ifp64->if_version = 1;
    169      1.126  dholland 			ifp64->if_daddr = LFS_UNUSED_DADDR;
    170      1.126  dholland 			ifp64->if_nextfree = ++i;
    171      1.126  dholland 		}
    172      1.126  dholland 		ifp64--;
    173      1.126  dholland 		ifp64->if_nextfree = oldlast;
    174      1.126  dholland 	} else if (lfs_sb_getversion(fs) > 1) {
    175      1.126  dholland 		for (ifp32 = (IFILE32 *)bp->b_data; i < xmax; ++ifp32) {
    176      1.126  dholland 			SET_BITMAP_FREE(fs, i);
    177      1.126  dholland 			ifp32->if_version = 1;
    178      1.126  dholland 			ifp32->if_daddr = LFS_UNUSED_DADDR;
    179      1.126  dholland 			ifp32->if_nextfree = ++i;
    180      1.126  dholland 		}
    181      1.126  dholland 		ifp32--;
    182      1.126  dholland 		ifp32->if_nextfree = oldlast;
    183      1.126  dholland 	} 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.120   hannken     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.126  dholland 	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.120   hannken 	LFS_GET_HEADFREE(fs, cip, cbp, ino);
    223      1.120   hannken 	KASSERT(*ino != LFS_UNUSED_INUM && *ino != LFS_IFILE_INUM);
    224       1.48  perseant 
    225      1.120   hannken 	DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %" PRId64 "\n",
    226      1.120   hannken 	     *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.120   hannken 	CLR_BITMAP_FREE(fs, *ino);
    233      1.120   hannken 	LFS_IENTRY(ifp, fs, *ino, bp);
    234      1.126  dholland 	if (lfs_if_getdaddr(fs, ifp) != LFS_UNUSED_DADDR)
    235      1.120   hannken 		panic("lfs_valloc: inuse inode %" PRId64 " on the free list",
    236      1.120   hannken 		    *ino);
    237      1.126  dholland 	LFS_PUT_HEADFREE(fs, cip, cbp, lfs_if_getnextfree(fs, ifp));
    238      1.126  dholland 	DLOG((DLOG_ALLOC, "lfs_valloc: headfree %" PRId64 " -> %ju\n",
    239      1.126  dholland 	     *ino, (uintmax_t)lfs_if_getnextfree(fs, ifp)));
    240       1.48  perseant 
    241      1.126  dholland 	/* version was updated by vfree */
    242      1.126  dholland 	*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.122  dholland 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM) {
    247       1.96  perseant 		if ((error = lfs_extend_ifile(fs, cred)) != 0) {
    248      1.120   hannken 			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.131.2.1  pgoyette 	KASSERTMSG((lfs_sb_getfreehd(fs) != LFS_UNUSED_INUM),
    254  1.131.2.1  pgoyette 	    "inode 0 allocated [3]");
    255       1.48  perseant 
    256       1.78  perseant 	/* Set superblock modified bit and increment file count. */
    257      1.105        ad 	mutex_enter(&lfs_lock);
    258       1.78  perseant 	fs->lfs_fmod = 1;
    259      1.105        ad 	mutex_exit(&lfs_lock);
    260      1.122  dholland 	lfs_sb_addnfiles(fs, 1);
    261       1.78  perseant 
    262       1.56  perseant 	lfs_segunlock(fs);
    263       1.63  perseant 
    264      1.120   hannken 	return 0;
    265       1.44  perseant }
    266       1.44  perseant 
    267       1.65  perseant /*
    268  1.131.2.1  pgoyette  * Allocate an inode for a new file, with given inode number and
    269  1.131.2.1  pgoyette  * version.
    270  1.131.2.1  pgoyette  *
    271  1.131.2.1  pgoyette  * Called in the same context as lfs_valloc and therefore shares the
    272  1.131.2.1  pgoyette  * same locking assumptions.
    273       1.65  perseant  */
    274       1.96  perseant int
    275      1.120   hannken lfs_valloc_fixed(struct lfs *fs, ino_t ino, int vers)
    276       1.44  perseant {
    277      1.120   hannken 	IFILE *ifp;
    278      1.120   hannken 	struct buf *bp, *cbp;
    279      1.130  dholland 	ino_t headino, thisino, oldnext;
    280      1.120   hannken 	CLEANERINFO *cip;
    281       1.40      fvdl 
    282  1.131.2.1  pgoyette 	if (fs->lfs_ronly)
    283  1.131.2.1  pgoyette 		return EROFS;
    284  1.131.2.1  pgoyette 
    285  1.131.2.1  pgoyette 	ASSERT_NO_SEGLOCK(fs);
    286  1.131.2.1  pgoyette 
    287  1.131.2.1  pgoyette 	lfs_seglock(fs, SEGM_PROT);
    288  1.131.2.1  pgoyette 
    289  1.131.2.1  pgoyette 	/*
    290  1.131.2.1  pgoyette 	 * If the ifile is too short to contain this inum, extend it.
    291  1.131.2.1  pgoyette 	 *
    292  1.131.2.1  pgoyette 	 * XXX: lfs_extend_ifile should take a size instead of always
    293  1.131.2.1  pgoyette 	 * doing just one block at time.
    294  1.131.2.1  pgoyette 	 */
    295      1.120   hannken 	while (VTOI(fs->lfs_ivnode)->i_size <= (ino /
    296      1.122  dholland 		lfs_sb_getifpb(fs) + lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs))
    297      1.123  dholland 		<< lfs_sb_getbshift(fs)) {
    298      1.120   hannken 		lfs_extend_ifile(fs, NOCRED);
    299      1.120   hannken 	}
    300       1.78  perseant 
    301      1.120   hannken 	LFS_IENTRY(ifp, fs, ino, bp);
    302      1.126  dholland 	oldnext = lfs_if_getnextfree(fs, ifp);
    303      1.126  dholland 	lfs_if_setversion(fs, ifp, vers);
    304      1.120   hannken 	brelse(bp, 0);
    305       1.51       chs 
    306      1.130  dholland 	LFS_GET_HEADFREE(fs, cip, cbp, &headino);
    307      1.130  dholland 	if (headino == ino) {
    308      1.120   hannken 		LFS_PUT_HEADFREE(fs, cip, cbp, oldnext);
    309      1.120   hannken 	} else {
    310      1.126  dholland 		ino_t nextfree;
    311      1.126  dholland 
    312      1.130  dholland 		thisino = headino;
    313      1.120   hannken 		while (1) {
    314      1.130  dholland 			LFS_IENTRY(ifp, fs, thisino, bp);
    315      1.126  dholland 			nextfree = lfs_if_getnextfree(fs, ifp);
    316      1.126  dholland 			if (nextfree == ino ||
    317      1.126  dholland 			    nextfree == LFS_UNUSED_INUM)
    318      1.120   hannken 				break;
    319      1.130  dholland 			thisino = nextfree;
    320      1.120   hannken 			brelse(bp, 0);
    321      1.120   hannken 		}
    322      1.126  dholland 		if (nextfree == LFS_UNUSED_INUM) {
    323      1.120   hannken 			brelse(bp, 0);
    324  1.131.2.1  pgoyette 			lfs_segunlock(fs);
    325      1.120   hannken 			return ENOENT;
    326      1.120   hannken 		}
    327      1.126  dholland 		lfs_if_setnextfree(fs, ifp, oldnext);
    328      1.120   hannken 		LFS_BWRITE_LOG(bp);
    329       1.66      fvdl 	}
    330       1.51       chs 
    331  1.131.2.1  pgoyette 	/* done */
    332  1.131.2.1  pgoyette 	lfs_segunlock(fs);
    333      1.120   hannken 	return 0;
    334        1.1   mycroft }
    335        1.1   mycroft 
    336       1.87  perseant #if 0
    337       1.87  perseant /*
    338       1.87  perseant  * Find the highest-numbered allocated inode.
    339       1.87  perseant  * This will be used to shrink the Ifile.
    340       1.87  perseant  */
    341       1.87  perseant static inline ino_t
    342       1.87  perseant lfs_last_alloc_ino(struct lfs *fs)
    343       1.87  perseant {
    344       1.87  perseant 	ino_t ino, maxino;
    345       1.87  perseant 
    346      1.123  dholland 	maxino = ((fs->lfs_ivnode->v_size >> lfs_sb_getbshift(fs)) -
    347      1.131  dholland 		  lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) *
    348      1.131  dholland 		lfs_sb_getifpb(fs);
    349       1.87  perseant 	for (ino = maxino - 1; ino > LFS_UNUSED_INUM; --ino) {
    350       1.87  perseant 		if (ISSET_BITMAP_FREE(fs, ino) == 0)
    351       1.87  perseant 			break;
    352       1.87  perseant 	}
    353       1.87  perseant 	return ino;
    354       1.87  perseant }
    355       1.87  perseant #endif
    356       1.87  perseant 
    357       1.87  perseant /*
    358       1.87  perseant  * Find the previous (next lowest numbered) free inode, if any.
    359       1.87  perseant  * If there is none, return LFS_UNUSED_INUM.
    360       1.87  perseant  */
    361       1.87  perseant static inline ino_t
    362       1.87  perseant lfs_freelist_prev(struct lfs *fs, ino_t ino)
    363       1.87  perseant {
    364       1.88  perseant 	ino_t tino, bound, bb, freehdbb;
    365       1.88  perseant 
    366      1.122  dholland 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM)	 /* No free inodes at all */
    367       1.88  perseant 		return LFS_UNUSED_INUM;
    368       1.88  perseant 
    369       1.88  perseant 	/* Search our own word first */
    370       1.88  perseant 	bound = ino & ~BMMASK;
    371       1.89  perseant 	for (tino = ino - 1; tino >= bound && tino > LFS_UNUSED_INUM; tino--)
    372       1.88  perseant 		if (ISSET_BITMAP_FREE(fs, tino))
    373       1.88  perseant 			return tino;
    374       1.88  perseant 	/* If there are no lower words to search, just return */
    375       1.88  perseant 	if (ino >> BMSHIFT == 0)
    376       1.88  perseant 		return LFS_UNUSED_INUM;
    377       1.88  perseant 
    378       1.88  perseant 	/*
    379       1.88  perseant 	 * Find a word with a free inode in it.  We have to be a bit
    380       1.88  perseant 	 * careful here since ino_t is unsigned.
    381       1.88  perseant 	 */
    382      1.122  dholland 	freehdbb = (lfs_sb_getfreehd(fs) >> BMSHIFT);
    383       1.88  perseant 	for (bb = (ino >> BMSHIFT) - 1; bb >= freehdbb && bb > 0; --bb)
    384       1.88  perseant 		if (fs->lfs_ino_bitmap[bb])
    385       1.88  perseant 			break;
    386       1.88  perseant 	if (fs->lfs_ino_bitmap[bb] == 0)
    387       1.88  perseant 		return LFS_UNUSED_INUM;
    388       1.88  perseant 
    389       1.88  perseant 	/* Search the word we found */
    390       1.89  perseant 	for (tino = (bb << BMSHIFT) | BMMASK; tino >= (bb << BMSHIFT) &&
    391       1.89  perseant 	     tino > LFS_UNUSED_INUM; tino--)
    392       1.88  perseant 		if (ISSET_BITMAP_FREE(fs, tino))
    393       1.88  perseant 			break;
    394       1.87  perseant 
    395       1.87  perseant 	if (tino <= LFS_IFILE_INUM)
    396       1.87  perseant 		tino = LFS_UNUSED_INUM;
    397       1.87  perseant 
    398       1.87  perseant 	return tino;
    399       1.87  perseant }
    400       1.87  perseant 
    401        1.1   mycroft /* Free an inode. */
    402        1.1   mycroft /* ARGUSED */
    403       1.43  perseant /* VOP_BWRITE 2i times */
    404        1.1   mycroft int
    405       1.99  christos lfs_vfree(struct vnode *vp, ino_t ino, int mode)
    406        1.3  christos {
    407        1.1   mycroft 	SEGUSE *sup;
    408       1.48  perseant 	CLEANERINFO *cip;
    409       1.48  perseant 	struct buf *cbp, *bp;
    410      1.126  dholland 	IFILE *ifp;
    411        1.1   mycroft 	struct inode *ip;
    412        1.1   mycroft 	struct lfs *fs;
    413       1.59      fvdl 	daddr_t old_iaddr;
    414       1.85      yamt 	ino_t otail;
    415       1.75     perry 
    416        1.1   mycroft 	/* Get the inode number and file system. */
    417       1.30  perseant 	ip = VTOI(vp);
    418        1.1   mycroft 	fs = ip->i_lfs;
    419        1.1   mycroft 	ino = ip->i_number;
    420       1.34  perseant 
    421       1.78  perseant 	ASSERT_NO_SEGLOCK(fs);
    422       1.88  perseant 	DLOG((DLOG_ALLOC, "lfs_vfree: free ino %lld\n", (long long)ino));
    423       1.78  perseant 
    424       1.48  perseant 	/* Drain of pending writes */
    425      1.111     rmind 	mutex_enter(vp->v_interlock);
    426      1.125  dholland 	while (lfs_sb_getversion(fs) > 1 && WRITEINPROG(vp)) {
    427      1.111     rmind 		cv_wait(&vp->v_cv, vp->v_interlock);
    428      1.105        ad 	}
    429      1.111     rmind 	mutex_exit(vp->v_interlock);
    430       1.48  perseant 
    431       1.63  perseant 	lfs_seglock(fs, SEGM_PROT);
    432       1.75     perry 
    433       1.72      yamt 	lfs_unmark_vnode(vp);
    434      1.105        ad 	mutex_enter(&lfs_lock);
    435      1.103        ad 	if (vp->v_uflag & VU_DIROP) {
    436      1.103        ad 		vp->v_uflag &= ~VU_DIROP;
    437       1.30  perseant 		--lfs_dirvcount;
    438       1.92  perseant 		--fs->lfs_dirvcount;
    439       1.63  perseant 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    440       1.92  perseant 		wakeup(&fs->lfs_dirvcount);
    441       1.30  perseant 		wakeup(&lfs_dirvcount);
    442      1.105        ad 		mutex_exit(&lfs_lock);
    443      1.120   hannken 		vrele(vp);
    444       1.90  perseant 
    445       1.90  perseant 		/*
    446       1.90  perseant 		 * If this inode is not going to be written any more, any
    447       1.90  perseant 		 * segment accounting left over from its truncation needs
    448       1.90  perseant 		 * to occur at the end of the next dirops flush.  Attach
    449       1.90  perseant 		 * them to the fs-wide list for that purpose.
    450       1.90  perseant 		 */
    451       1.90  perseant 		if (LIST_FIRST(&ip->i_lfs_segdhd) != NULL) {
    452       1.90  perseant 			struct segdelta *sd;
    453       1.90  perseant 
    454       1.90  perseant 			while((sd = LIST_FIRST(&ip->i_lfs_segdhd)) != NULL) {
    455       1.90  perseant 				LIST_REMOVE(sd, list);
    456       1.90  perseant 				LIST_INSERT_HEAD(&fs->lfs_segdhd, sd, list);
    457       1.90  perseant 			}
    458       1.90  perseant 		}
    459       1.90  perseant 	} else {
    460       1.90  perseant 		/*
    461       1.90  perseant 		 * If it's not a dirop, we can finalize right away.
    462       1.90  perseant 		 */
    463      1.105        ad 		mutex_exit(&lfs_lock);
    464       1.90  perseant 		lfs_finalize_ino_seguse(fs, ip);
    465       1.38  perseant 	}
    466       1.30  perseant 
    467      1.105        ad 	mutex_enter(&lfs_lock);
    468       1.42  perseant 	LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
    469      1.105        ad 	mutex_exit(&lfs_lock);
    470       1.33  perseant 	ip->i_flag &= ~IN_ALLMOD;
    471       1.93  perseant 	ip->i_lfs_iflags |= LFSI_DELETED;
    472       1.93  perseant 
    473        1.1   mycroft 	/*
    474        1.1   mycroft 	 * Set the ifile's inode entry to unused, increment its version number
    475       1.48  perseant 	 * and link it onto the free chain.
    476        1.1   mycroft 	 */
    477       1.87  perseant 	SET_BITMAP_FREE(fs, ino);
    478        1.1   mycroft 	LFS_IENTRY(ifp, fs, ino, bp);
    479      1.126  dholland 	old_iaddr = lfs_if_getdaddr(fs, ifp);
    480      1.126  dholland 	lfs_if_setdaddr(fs, ifp, LFS_UNUSED_DADDR);
    481      1.126  dholland 	lfs_if_setversion(fs, ifp, lfs_if_getversion(fs, ifp) + 1);
    482      1.125  dholland 	if (lfs_sb_getversion(fs) == 1) {
    483      1.126  dholland 		ino_t nextfree;
    484      1.126  dholland 
    485      1.126  dholland 		LFS_GET_HEADFREE(fs, cip, cbp, &nextfree);
    486      1.126  dholland 		lfs_if_setnextfree(fs, ifp, nextfree);
    487       1.48  perseant 		LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    488       1.56  perseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    489       1.48  perseant 	} else {
    490       1.87  perseant 		ino_t tino, onf;
    491       1.87  perseant 
    492      1.126  dholland 		lfs_if_setnextfree(fs, ifp, LFS_UNUSED_INUM);
    493       1.56  perseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    494       1.87  perseant 
    495       1.87  perseant 		tino = lfs_freelist_prev(fs, ino);
    496       1.87  perseant 		if (tino == LFS_UNUSED_INUM) {
    497      1.126  dholland 			ino_t nextfree;
    498      1.126  dholland 
    499       1.87  perseant 			/* Nothing free below us, put us on the head */
    500       1.87  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    501      1.126  dholland 			LFS_GET_HEADFREE(fs, cip, cbp, &nextfree);
    502      1.126  dholland 			lfs_if_setnextfree(fs, ifp, nextfree);
    503       1.87  perseant 			LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    504       1.88  perseant 			DLOG((DLOG_ALLOC, "lfs_vfree: headfree %lld -> %lld\n",
    505      1.126  dholland 			     (long long)nextfree, (long long)ino));
    506       1.87  perseant 			LFS_BWRITE_LOG(bp); /* Ifile */
    507       1.87  perseant 
    508       1.87  perseant 			/* If the list was empty, set tail too */
    509       1.87  perseant 			LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    510       1.87  perseant 			if (otail == LFS_UNUSED_INUM) {
    511       1.87  perseant 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    512       1.87  perseant 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    513       1.87  perseant 				      "-> %lld\n", (long long)otail,
    514       1.87  perseant 				      (long long)ino));
    515       1.87  perseant 			}
    516       1.87  perseant 		} else {
    517       1.87  perseant 			/*
    518       1.87  perseant 			 * Insert this inode into the list after tino.
    519       1.87  perseant 			 * We hold the segment lock so we don't have to
    520       1.87  perseant 			 * worry about blocks being written out of order.
    521       1.87  perseant 			 */
    522       1.87  perseant 			DLOG((DLOG_ALLOC, "lfs_vfree: insert ino %lld "
    523       1.87  perseant 			      " after %lld\n", ino, tino));
    524       1.87  perseant 
    525       1.87  perseant 			LFS_IENTRY(ifp, fs, tino, bp);
    526      1.126  dholland 			onf = lfs_if_getnextfree(fs, ifp);
    527      1.126  dholland 			lfs_if_setnextfree(fs, ifp, ino);
    528       1.87  perseant 			LFS_BWRITE_LOG(bp);	/* Ifile */
    529       1.87  perseant 
    530       1.87  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    531      1.126  dholland 			lfs_if_setnextfree(fs, ifp, onf);
    532       1.87  perseant 			LFS_BWRITE_LOG(bp);	/* Ifile */
    533       1.87  perseant 
    534       1.87  perseant 			/* If we're last, put us on the tail */
    535       1.87  perseant 			if (onf == LFS_UNUSED_INUM) {
    536       1.87  perseant 				LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    537       1.87  perseant 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    538       1.87  perseant 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    539       1.87  perseant 				      "-> %lld\n", (long long)otail,
    540       1.87  perseant 				      (long long)ino));
    541       1.87  perseant 			}
    542       1.87  perseant 		}
    543       1.48  perseant 	}
    544  1.131.2.1  pgoyette 	/* XXX: shouldn't this check be further up *before* we trash the fs? */
    545  1.131.2.1  pgoyette 	KASSERTMSG((ino != LFS_UNUSED_INUM), "inode 0 freed");
    546  1.131.2.1  pgoyette 
    547  1.131.2.1  pgoyette 	/*
    548  1.131.2.1  pgoyette 	 * Update the segment summary for the segment where the on-disk
    549  1.131.2.1  pgoyette 	 * copy used to be.
    550  1.131.2.1  pgoyette 	 */
    551        1.1   mycroft 	if (old_iaddr != LFS_UNUSED_DADDR) {
    552      1.117  christos 		LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, old_iaddr), bp);
    553  1.131.2.1  pgoyette 		/* the number of bytes in the segment should not become < 0 */
    554  1.131.2.1  pgoyette 		KASSERTMSG((sup->su_nbytes >= DINOSIZE(fs)),
    555  1.131.2.1  pgoyette 		    "lfs_vfree: negative byte count"
    556  1.131.2.1  pgoyette 		    " (segment %" PRIu32 " short by %d)\n",
    557  1.131.2.1  pgoyette 		    lfs_dtosn(fs, old_iaddr),
    558  1.131.2.1  pgoyette 		    (int)DINOSIZE(fs) - sup->su_nbytes);
    559  1.131.2.1  pgoyette 		/* update the number of bytes in the segment */
    560      1.127  dholland 		sup->su_nbytes -= DINOSIZE(fs);
    561      1.117  christos 		LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, old_iaddr), bp); /* Ifile */
    562        1.1   mycroft 	}
    563       1.75     perry 
    564        1.1   mycroft 	/* Set superblock modified bit and decrement file count. */
    565      1.105        ad 	mutex_enter(&lfs_lock);
    566        1.1   mycroft 	fs->lfs_fmod = 1;
    567      1.105        ad 	mutex_exit(&lfs_lock);
    568      1.122  dholland 	lfs_sb_subnfiles(fs, 1);
    569       1.75     perry 
    570       1.56  perseant 	lfs_segunlock(fs);
    571       1.63  perseant 
    572        1.1   mycroft 	return (0);
    573        1.1   mycroft }
    574       1.87  perseant 
    575       1.87  perseant /*
    576       1.87  perseant  * Sort the freelist and set up the free-inode bitmap.
    577       1.87  perseant  * To be called by lfs_mountfs().
    578       1.87  perseant  */
    579       1.87  perseant void
    580       1.87  perseant lfs_order_freelist(struct lfs *fs)
    581       1.87  perseant {
    582       1.87  perseant 	CLEANERINFO *cip;
    583       1.87  perseant 	IFILE *ifp = NULL;
    584       1.87  perseant 	struct buf *bp;
    585       1.87  perseant 	ino_t ino, firstino, lastino, maxino;
    586       1.97  perseant #ifdef notyet
    587       1.97  perseant 	struct vnode *vp;
    588       1.97  perseant #endif
    589       1.87  perseant 
    590       1.95  perseant 	ASSERT_NO_SEGLOCK(fs);
    591       1.95  perseant 	lfs_seglock(fs, SEGM_PROT);
    592       1.95  perseant 
    593      1.123  dholland 	maxino = ((fs->lfs_ivnode->v_size >> lfs_sb_getbshift(fs)) -
    594      1.122  dholland 		  lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) * lfs_sb_getifpb(fs);
    595      1.121  dholland 	fs->lfs_ino_bitmap =
    596       1.88  perseant 		malloc(((maxino + BMMASK) >> BMSHIFT) * sizeof(lfs_bm_t),
    597       1.88  perseant 		       M_SEGMENT, M_WAITOK | M_ZERO);
    598       1.88  perseant 	KASSERT(fs->lfs_ino_bitmap != NULL);
    599       1.87  perseant 
    600       1.87  perseant 	firstino = lastino = LFS_UNUSED_INUM;
    601       1.87  perseant 	for (ino = 0; ino < maxino; ino++) {
    602      1.122  dholland 		if (ino % lfs_sb_getifpb(fs) == 0)
    603       1.87  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    604       1.87  perseant 		else
    605      1.128   mlelstv 			LFS_IENTRY_NEXT(ifp, fs);
    606       1.87  perseant 
    607       1.87  perseant 		/* Don't put zero or ifile on the free list */
    608       1.87  perseant 		if (ino == LFS_UNUSED_INUM || ino == LFS_IFILE_INUM)
    609       1.87  perseant 			continue;
    610       1.87  perseant 
    611       1.97  perseant #ifdef notyet
    612       1.97  perseant 		/* Address orphaned files */
    613      1.126  dholland 		if (lfs_if_getnextfree(fs, ifp) == LFS_ORPHAN_NEXTFREE &&
    614       1.97  perseant 		    VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp) == 0) {
    615      1.126  dholland 			unsigned segno;
    616      1.126  dholland 
    617      1.126  dholland 			segno = lfs_dtosn(fs, lfs_if_getdaddr(fs, ifp));
    618      1.104        he 			lfs_truncate(vp, 0, 0, NOCRED);
    619       1.97  perseant 			vput(vp);
    620      1.126  dholland 			LFS_SEGENTRY(sup, fs, segno, bp);
    621      1.131  dholland 			KASSERT(sup->su_nbytes >= DINOSIZE(fs));
    622      1.131  dholland 			sup->su_nbytes -= DINOSIZE(fs);
    623      1.126  dholland 			LFS_WRITESEGENTRY(sup, fs, segno, bp);
    624       1.97  perseant 
    625       1.97  perseant 			/* Set up to fall through to next section */
    626      1.126  dholland 			lfs_if_setdaddr(fs, ifp, LFS_UNUSED_DADDR);
    627       1.97  perseant 			LFS_BWRITE_LOG(bp);
    628       1.97  perseant 			LFS_IENTRY(ifp, fs, ino, bp);
    629       1.97  perseant 		}
    630       1.97  perseant #endif
    631       1.97  perseant 
    632      1.126  dholland 		if (lfs_if_getdaddr(fs, ifp) == LFS_UNUSED_DADDR) {
    633       1.87  perseant 			if (firstino == LFS_UNUSED_INUM)
    634       1.87  perseant 				firstino = ino;
    635       1.87  perseant 			else {
    636      1.102        ad 				brelse(bp, 0);
    637       1.87  perseant 
    638       1.87  perseant 				LFS_IENTRY(ifp, fs, lastino, bp);
    639      1.126  dholland 				lfs_if_setnextfree(fs, ifp, ino);
    640       1.87  perseant 				LFS_BWRITE_LOG(bp);
    641       1.87  perseant 
    642       1.87  perseant 				LFS_IENTRY(ifp, fs, ino, bp);
    643       1.87  perseant 			}
    644       1.87  perseant 			lastino = ino;
    645       1.87  perseant 
    646       1.87  perseant 			SET_BITMAP_FREE(fs, ino);
    647       1.87  perseant 		}
    648       1.87  perseant 
    649      1.122  dholland 		if ((ino + 1) % lfs_sb_getifpb(fs) == 0)
    650      1.102        ad 			brelse(bp, 0);
    651       1.87  perseant 	}
    652       1.87  perseant 
    653       1.87  perseant 	LFS_PUT_HEADFREE(fs, cip, bp, firstino);
    654       1.87  perseant 	LFS_PUT_TAILFREE(fs, cip, bp, lastino);
    655       1.95  perseant 
    656       1.95  perseant 	lfs_segunlock(fs);
    657       1.87  perseant }
    658       1.97  perseant 
    659       1.97  perseant void
    660       1.97  perseant lfs_orphan(struct lfs *fs, ino_t ino)
    661       1.97  perseant {
    662       1.97  perseant 	IFILE *ifp;
    663       1.97  perseant 	struct buf *bp;
    664       1.97  perseant 
    665       1.97  perseant 	LFS_IENTRY(ifp, fs, ino, bp);
    666      1.126  dholland 	lfs_if_setnextfree(fs, ifp, LFS_ORPHAN_NEXTFREE);
    667       1.97  perseant 	LFS_BWRITE_LOG(bp);
    668       1.97  perseant }
    669