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