Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.18.2.5
      1 /*	$NetBSD: lfs_alloc.c,v 1.18.2.5 1999/12/17 23:53:41 he Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by the NetBSD
     21  *      Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 /*
     39  * Copyright (c) 1991, 1993
     40  *	The Regents of the University of California.  All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by the University of
     53  *	California, Berkeley and its contributors.
     54  * 4. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)lfs_alloc.c	8.4 (Berkeley) 1/4/94
     71  */
     72 
     73 #if defined(_KERNEL) && !defined(_LKM)
     74 #include "opt_quota.h"
     75 #endif
     76 
     77 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/kernel.h>
     80 #include <sys/buf.h>
     81 #include <sys/vnode.h>
     82 #include <sys/syslog.h>
     83 #include <sys/mount.h>
     84 #include <sys/malloc.h>
     85 #include <sys/pool.h>
     86 
     87 #include <vm/vm.h>
     88 
     89 #include <ufs/ufs/quota.h>
     90 #include <ufs/ufs/inode.h>
     91 #include <ufs/ufs/ufsmount.h>
     92 #include <ufs/ufs/ufs_extern.h>
     93 
     94 #include <ufs/lfs/lfs.h>
     95 #include <ufs/lfs/lfs_extern.h>
     96 
     97 extern struct lock ufs_hashlock;
     98 
     99 /* Allocate a new inode. */
    100 /* ARGSUSED */
    101 int
    102 lfs_valloc(v)
    103 	void *v;
    104 {
    105 	struct vop_valloc_args /* {
    106 				  struct vnode *a_pvp;
    107 				  int a_mode;
    108 				  struct ucred *a_cred;
    109 				  struct vnode **a_vpp;
    110 				  } */ *ap = v;
    111 	struct lfs *fs;
    112 	struct buf *bp;
    113 	struct ifile *ifp;
    114 	struct inode *ip;
    115 	struct vnode *vp;
    116 	ufs_daddr_t blkno;
    117 	ino_t new_ino;
    118 	u_long i, max;
    119 	int error;
    120 	extern int lfs_dirvcount;
    121 
    122 	fs = VTOI(ap->a_pvp)->i_lfs;
    123 
    124 	/*
    125 	 * Prevent a race getting lfs_free.  We use
    126 	 * the ufs_hashlock here because we need that anyway for
    127 	 * the hash insertion later.
    128 	 */
    129 	lockmgr(&ufs_hashlock, LK_EXCLUSIVE, 0);
    130 
    131 	/* Get the head of the freelist. */
    132 	new_ino = fs->lfs_free;
    133 #ifdef DIAGNOSTIC
    134 	if(new_ino == LFS_UNUSED_INUM) {
    135 #ifdef DEBUG
    136 		lfs_dump_super(fs);
    137 #endif /* DEBUG */
    138 		panic("inode 0 allocated [1]");
    139 	}
    140 #endif /* DIAGNOSTIC */
    141 #ifdef ALLOCPRINT
    142 	printf("lfs_ialloc: allocate inode %d\n", new_ino);
    143 #endif
    144 
    145 	/*
    146 	 * Remove the inode from the free list and write the new start
    147 	 * of the free list into the superblock.
    148 	 */
    149 	LFS_IENTRY(ifp, fs, new_ino, bp);
    150 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
    151 		panic("lfs_ialloc: inuse inode %d on the free list", new_ino);
    152 	fs->lfs_free = ifp->if_nextfree;
    153 	brelse(bp);
    154 
    155 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    156 	if (fs->lfs_free == LFS_UNUSED_INUM) {
    157 		vp = fs->lfs_ivnode;
    158 		VOP_LOCK(vp,LK_EXCLUSIVE);
    159 		ip = VTOI(vp);
    160 		blkno = lblkno(fs, ip->i_ffs_size);
    161 		lfs_balloc(vp, 0, fs->lfs_bsize, blkno, &bp);
    162 		ip->i_ffs_size += fs->lfs_bsize;
    163 		uvm_vnp_setsize(vp, ip->i_ffs_size);
    164 		(void)uvm_vnp_uncache(vp);
    165 
    166 		i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
    167 			fs->lfs_ifpb;
    168 		fs->lfs_free = i;
    169 #ifdef DIAGNOSTIC
    170 		if(fs->lfs_free == LFS_UNUSED_INUM)
    171 			panic("inode 0 allocated [2]");
    172 #endif /* DIAGNOSTIC */
    173 		max = i + fs->lfs_ifpb;
    174 		for (ifp = (struct ifile *)bp->b_data; i < max; ++ifp) {
    175 			ifp->if_version = 1;
    176 			ifp->if_daddr = LFS_UNUSED_DADDR;
    177 			ifp->if_nextfree = ++i;
    178 		}
    179 		ifp--;
    180 		ifp->if_nextfree = LFS_UNUSED_INUM;
    181 		VOP_UNLOCK(vp,0);
    182 		if ((error = VOP_BWRITE(bp)) != 0) {
    183 			lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    184 			return (error);
    185 		}
    186 	}
    187 
    188 #ifdef DIAGNOSTIC
    189 	if(fs->lfs_free == LFS_UNUSED_INUM)
    190 		panic("inode 0 allocated [3]");
    191 #endif /* DIAGNOSTIC */
    192 
    193 	/* Create a vnode to associate with the inode. */
    194 	if ((error = lfs_vcreate(ap->a_pvp->v_mount, new_ino, &vp)) != 0) {
    195 		lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    196 		return (error);
    197 	}
    198 
    199 	ip = VTOI(vp);
    200 	/* Zero out the direct and indirect block addresses. */
    201 	bzero(&ip->i_din, sizeof(ip->i_din));
    202 	ip->i_din.ffs_din.di_inumber = new_ino;
    203 
    204 	/* Set a new generation number for this inode. */
    205 	ip->i_ffs_gen++;
    206 
    207 	/* Insert into the inode hash table. */
    208 	ufs_ihashins(ip);
    209 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    210 
    211 	error = ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
    212 	if (error) {
    213 		vput(vp);
    214 		*ap->a_vpp = NULL;
    215 		return (error);
    216 	}
    217 
    218 	*ap->a_vpp = vp;
    219 	if(!(vp->v_flag & VDIROP)) {
    220 		lfs_vref(vp);
    221 		++lfs_dirvcount;
    222 	}
    223 	vp->v_flag |= VDIROP;
    224 	VREF(ip->i_devvp);
    225 
    226 	/* Set superblock modified bit and increment file count. */
    227 	fs->lfs_fmod = 1;
    228 	++fs->lfs_nfiles;
    229 	return (0);
    230 }
    231 
    232 /* Create a new vnode/inode pair and initialize what fields we can. */
    233 int
    234 lfs_vcreate(mp, ino, vpp)
    235 	struct mount *mp;
    236 	ino_t ino;
    237 	struct vnode **vpp;
    238 {
    239 	extern int (**lfs_vnodeop_p) __P((void *));
    240 	struct inode *ip;
    241 	struct ufsmount *ump;
    242 	int error;
    243 #ifdef QUOTA
    244 	int i;
    245 #endif
    246 
    247 	/* Create the vnode. */
    248 	if ((error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, vpp)) != 0) {
    249 		*vpp = NULL;
    250 		return (error);
    251 	}
    252 
    253 	/* Get a pointer to the private mount structure. */
    254 	ump = VFSTOUFS(mp);
    255 
    256 	/* Initialize the inode. */
    257 	ip = pool_get(&lfs_inode_pool, PR_WAITOK);
    258 	lockinit(&ip->i_lock, PINOD, "lfsinode", 0, 0);
    259 	(*vpp)->v_data = ip;
    260 	ip->i_vnode = *vpp;
    261 	ip->i_devvp = ump->um_devvp;
    262 	ip->i_flag = IN_MODIFIED;
    263 	ip->i_dev = ump->um_dev;
    264 	ip->i_number = ip->i_din.ffs_din.di_inumber = ino;
    265 	ip->i_lfs = ump->um_lfs;
    266 #ifdef QUOTA
    267 	for (i = 0; i < MAXQUOTAS; i++)
    268 		ip->i_dquot[i] = NODQUOT;
    269 #endif
    270 	ip->i_lockf = 0;
    271 	ip->i_diroff = 0;
    272 	ip->i_ffs_mode = 0;
    273 	ip->i_ffs_size = 0;
    274 	ip->i_ffs_blocks = 0;
    275 	++ump->um_lfs->lfs_uinodes;
    276 	return (0);
    277 }
    278 
    279 /* Free an inode. */
    280 /* ARGUSED */
    281 int
    282 lfs_vfree(v)
    283 	void *v;
    284 {
    285 	struct vop_vfree_args /* {
    286 				 struct vnode *a_pvp;
    287 				 ino_t a_ino;
    288 				 int a_mode;
    289 				 } */ *ap = v;
    290 	SEGUSE *sup;
    291 	struct buf *bp;
    292 	struct ifile *ifp;
    293 	struct inode *ip;
    294 	struct lfs *fs;
    295 	ufs_daddr_t old_iaddr;
    296 	ino_t ino;
    297 	int already_locked;
    298 
    299 	/* Get the inode number and file system. */
    300 	ip = VTOI(ap->a_pvp);
    301 	fs = ip->i_lfs;
    302 	ino = ip->i_number;
    303 
    304 	/* If we already hold ufs_hashlock, don't panic, just do it anyway */
    305 	already_locked = lockstatus(&ufs_hashlock) && ufs_hashlock.lk_lockholder == curproc->p_pid;
    306 	while(WRITEINPROG(ap->a_pvp)
    307 	      || fs->lfs_seglock
    308 	      || (!already_locked && lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0)))
    309 	{
    310 		if (WRITEINPROG(ap->a_pvp)) {
    311 			tsleep(ap->a_pvp, (PRIBIO+1), "lfs_vfree", 0);
    312 		}
    313 		if (fs->lfs_seglock) {
    314 			if (fs->lfs_lockpid == curproc->p_pid) {
    315 				break;
    316 			} else {
    317 				tsleep(&fs->lfs_seglock, PRIBIO + 1, "lfs_vfr1", 0);
    318 			}
    319 		}
    320 	}
    321 
    322 	if (ip->i_flag & IN_CLEANING) {
    323 		--fs->lfs_uinodes;
    324 		ip->i_flag &= ~IN_CLEANING;
    325 	}
    326 	if (ip->i_flag & IN_MODIFIED) {
    327 		--fs->lfs_uinodes;
    328 		ip->i_flag &=
    329 			~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
    330 	}
    331 #ifdef DEBUG_LFS
    332 	if((int32_t)fs->lfs_uinodes<0) {
    333 		printf("U1");
    334 		fs->lfs_uinodes=0;
    335 	}
    336 #endif
    337 	/*
    338 	 * Set the ifile's inode entry to unused, increment its version number
    339 	 * and link it into the free chain.
    340 	 */
    341 	LFS_IENTRY(ifp, fs, ino, bp);
    342 	old_iaddr = ifp->if_daddr;
    343 	ifp->if_daddr = LFS_UNUSED_DADDR;
    344 	++ifp->if_version;
    345 	ifp->if_nextfree = fs->lfs_free;
    346 	fs->lfs_free = ino;
    347 	(void) VOP_BWRITE(bp);
    348 #ifdef DIAGNOSTIC
    349 	if(fs->lfs_free == LFS_UNUSED_INUM) {
    350 		panic("inode 0 freed");
    351 	}
    352 #endif /* DIAGNOSTIC */
    353 	if (old_iaddr != LFS_UNUSED_DADDR) {
    354 		LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp);
    355 #ifdef DIAGNOSTIC
    356 		if (sup->su_nbytes < DINODE_SIZE) {
    357 			printf("lfs_vfree: negative byte count (segment %d short by %d)\n", datosn(fs, old_iaddr), (int)DINODE_SIZE - sup->su_nbytes);
    358 			panic("lfs_vfree: negative byte count");
    359 			sup->su_nbytes = DINODE_SIZE;
    360 		}
    361 #endif
    362 		sup->su_nbytes -= DINODE_SIZE;
    363 		(void) VOP_BWRITE(bp);
    364 	}
    365 	if(!already_locked)
    366 		lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    367 
    368 	/* Set superblock modified bit and decrement file count. */
    369 	fs->lfs_fmod = 1;
    370 	--fs->lfs_nfiles;
    371 
    372 	return (0);
    373 }
    374