Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.38
      1 /*	$NetBSD: lfs_alloc.c,v 1.38 2000/06/27 20:57:12 perseant Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000 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 int lfs_dirvcount;
     98 extern struct lock ufs_hashlock;
     99 
    100 /* Allocate a new inode. */
    101 /* ARGSUSED */
    102 int
    103 lfs_valloc(v)
    104 	void *v;
    105 {
    106 	struct vop_valloc_args /* {
    107 				  struct vnode *a_pvp;
    108 				  int a_mode;
    109 				  struct ucred *a_cred;
    110 				  struct vnode **a_vpp;
    111 				  } */ *ap = v;
    112 	struct lfs *fs;
    113 	struct buf *bp;
    114 	struct ifile *ifp;
    115 	struct inode *ip;
    116 	struct vnode *vp;
    117 	ufs_daddr_t blkno;
    118 	ino_t new_ino;
    119 	u_long i, max;
    120 	int error;
    121 	int new_gen;
    122 	extern int lfs_dirvcount;
    123 
    124 	fs = VTOI(ap->a_pvp)->i_lfs;
    125 	if (fs->lfs_ronly)
    126 		return EROFS;
    127 	*ap->a_vpp = NULL;
    128 
    129 	/*
    130 	 * Use lfs_seglock here, instead of fs->lfs_freelock, to ensure that
    131 	 * the free list is not changed in between the time that the ifile
    132 	 * blocks are written to disk and the time that the superblock is
    133 	 * written to disk.
    134 	 *
    135 	 * XXX this sucks.  We should instead encode the head of the free
    136 	 * list into the CLEANERINFO block of the Ifile.
    137 	 */
    138 	lfs_seglock(fs, SEGM_PROT);
    139 
    140 	/* Get the head of the freelist. */
    141 	new_ino = fs->lfs_free;
    142 #ifdef DIAGNOSTIC
    143 	if(new_ino == LFS_UNUSED_INUM) {
    144 #ifdef DEBUG
    145 		lfs_dump_super(fs);
    146 #endif /* DEBUG */
    147 		panic("inode 0 allocated [1]");
    148 	}
    149 #endif /* DIAGNOSTIC */
    150 #ifdef ALLOCPRINT
    151 	printf("lfs_ialloc: allocate inode %d\n", new_ino);
    152 #endif
    153 
    154 	/*
    155 	 * Remove the inode from the free list and write the new start
    156 	 * of the free list into the superblock.
    157 	 */
    158 	LFS_IENTRY(ifp, fs, new_ino, bp);
    159 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
    160 		panic("lfs_ialloc: inuse inode %d on the free list", new_ino);
    161 	fs->lfs_free = ifp->if_nextfree;
    162 	new_gen = ifp->if_version; /* version was updated by vfree */
    163 #ifdef LFS_DEBUG_NEXTFREE
    164 	ifp->if_nextfree = 0;
    165 	VOP_BWRITE(bp);
    166 #else
    167 	brelse(bp);
    168 #endif
    169 
    170 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    171 	if (fs->lfs_free == LFS_UNUSED_INUM) {
    172 		vp = fs->lfs_ivnode;
    173 		(void)lfs_vref(vp);
    174 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    175 		ip = VTOI(vp);
    176 		blkno = lblkno(fs, ip->i_ffs_size);
    177 		if ((error = VOP_BALLOC(vp, ip->i_ffs_size, fs->lfs_bsize,
    178 					ap->a_cred, 0, &bp)) != 0) {
    179 			VOP_UNLOCK(vp, 0);
    180 			lfs_segunlock(fs);
    181 			fs->lfs_free = new_ino;
    182 			return (error);
    183 		}
    184 		ip->i_ffs_size += fs->lfs_bsize;
    185 		uvm_vnp_setsize(vp, ip->i_ffs_size);
    186 		(void)uvm_vnp_uncache(vp);
    187 		VOP_UNLOCK(vp, 0);
    188 
    189 		i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
    190 			fs->lfs_ifpb;
    191 		fs->lfs_free = i;
    192 #ifdef DIAGNOSTIC
    193 		if(fs->lfs_free == LFS_UNUSED_INUM)
    194 			panic("inode 0 allocated [2]");
    195 #endif /* DIAGNOSTIC */
    196 		max = i + fs->lfs_ifpb;
    197 		for (ifp = (struct ifile *)bp->b_data; i < max; ++ifp) {
    198 			ifp->if_version = 1;
    199 			ifp->if_daddr = LFS_UNUSED_DADDR;
    200 			ifp->if_nextfree = ++i;
    201 		}
    202 		ifp--;
    203 		ifp->if_nextfree = LFS_UNUSED_INUM;
    204 		VOP_BWRITE(bp);
    205 		lfs_vunref(vp);
    206 	}
    207 #ifdef DIAGNOSTIC
    208 	if(fs->lfs_free == LFS_UNUSED_INUM)
    209 		panic("inode 0 allocated [3]");
    210 #endif /* DIAGNOSTIC */
    211 
    212 	lfs_segunlock(fs);
    213 
    214 	lockmgr(&ufs_hashlock, LK_EXCLUSIVE, 0);
    215 	/* Create a vnode to associate with the inode. */
    216 	if ((error = lfs_vcreate(ap->a_pvp->v_mount, new_ino, &vp)) != 0) {
    217 		lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    218 		goto errout;
    219 	}
    220 
    221 	ip = VTOI(vp);
    222 	/* Zero out the direct and indirect block addresses. */
    223 	bzero(&ip->i_din, sizeof(ip->i_din));
    224 	ip->i_din.ffs_din.di_inumber = new_ino;
    225 
    226 	/* Set a new generation number for this inode. */
    227 	ip->i_ffs_gen = new_gen;
    228 
    229 	/* Insert into the inode hash table. */
    230 	ufs_ihashins(ip);
    231 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    232 
    233 	error = ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
    234 	if (error) {
    235 		vput(vp);
    236 		goto errout;
    237 	}
    238 
    239 	*ap->a_vpp = vp;
    240 #if 1
    241 	if(!(vp->v_flag & VDIROP)) {
    242 		(void)lfs_vref(vp);
    243 		++lfs_dirvcount;
    244 	}
    245 	vp->v_flag |= VDIROP;
    246 
    247 	if(!(ip->i_flag & IN_ADIROP))
    248 		++fs->lfs_nadirop;
    249 	ip->i_flag |= IN_ADIROP;
    250 #endif
    251 	VREF(ip->i_devvp);
    252 	/* Set superblock modified bit and increment file count. */
    253 	fs->lfs_fmod = 1;
    254 	++fs->lfs_nfiles;
    255 	return (0);
    256 
    257     errout:
    258 	/*
    259 	 * Put the new inum back on the free list.
    260 	 */
    261 	LFS_IENTRY(ifp, fs, new_ino, bp);
    262 	ifp->if_daddr = LFS_UNUSED_DADDR;
    263 	ifp->if_nextfree = fs->lfs_free;
    264 	fs->lfs_free = new_ino;
    265 	VOP_BWRITE(bp);
    266 
    267 	return (error);
    268 }
    269 
    270 /* Create a new vnode/inode pair and initialize what fields we can. */
    271 int
    272 lfs_vcreate(mp, ino, vpp)
    273 	struct mount *mp;
    274 	ino_t ino;
    275 	struct vnode **vpp;
    276 {
    277 	extern int (**lfs_vnodeop_p) __P((void *));
    278 	struct inode *ip;
    279 	struct ufsmount *ump;
    280 	int error;
    281 #ifdef QUOTA
    282 	int i;
    283 #endif
    284 
    285 	/* Create the vnode. */
    286 	if ((error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, vpp)) != 0) {
    287 		*vpp = NULL;
    288 		return (error);
    289 	}
    290 
    291 	/* Get a pointer to the private mount structure. */
    292 	ump = VFSTOUFS(mp);
    293 
    294 	/* Initialize the inode. */
    295 	ip = pool_get(&lfs_inode_pool, PR_WAITOK);
    296 	(*vpp)->v_data = ip;
    297 	ip->i_vnode = *vpp;
    298 	ip->i_devvp = ump->um_devvp;
    299 	ip->i_flag = IN_MODIFIED;
    300 	ip->i_dev = ump->um_dev;
    301 	ip->i_number = ip->i_din.ffs_din.di_inumber = ino;
    302 	ip->i_lfs = ump->um_lfs;
    303 #ifdef QUOTA
    304 	for (i = 0; i < MAXQUOTAS; i++)
    305 		ip->i_dquot[i] = NODQUOT;
    306 #endif
    307 	ip->i_lockf = 0;
    308 	ip->i_diroff = 0;
    309 	ip->i_ffs_mode = 0;
    310 	ip->i_ffs_size = 0;
    311 	ip->i_ffs_blocks = 0;
    312 	++ump->um_lfs->lfs_uinodes;
    313 	return (0);
    314 }
    315 
    316 /* Free an inode. */
    317 /* ARGUSED */
    318 int
    319 lfs_vfree(v)
    320 	void *v;
    321 {
    322 	struct vop_vfree_args /* {
    323 				 struct vnode *a_pvp;
    324 				 ino_t a_ino;
    325 				 int a_mode;
    326 				 } */ *ap = v;
    327 	SEGUSE *sup;
    328 	struct buf *bp;
    329 	struct ifile *ifp;
    330 	struct inode *ip;
    331 	struct vnode *vp;
    332 	struct lfs *fs;
    333 	ufs_daddr_t old_iaddr;
    334 	ino_t ino;
    335 	extern int lfs_dirvcount;
    336 
    337 	/* Get the inode number and file system. */
    338 	vp = ap->a_pvp;
    339 	ip = VTOI(vp);
    340 	fs = ip->i_lfs;
    341 	ino = ip->i_number;
    342 
    343 #if 0
    344 	/*
    345 	 * Right now this is unnecessary since we take the seglock.
    346 	 * But if the seglock is no longer necessary (e.g. we put the
    347 	 * head of the free list into the Ifile) we will need to drain
    348 	 * this vnode of any pending writes.
    349 	 */
    350 	if (WRITEINPROG(vp))
    351 		tsleep(vp, (PRIBIO+1), "lfs_vfree", 0);
    352 #endif
    353 	lfs_seglock(fs, SEGM_PROT);
    354 
    355 	if(vp->v_flag & VDIROP) {
    356 		--lfs_dirvcount;
    357 		vp->v_flag &= ~VDIROP;
    358 		wakeup(&lfs_dirvcount);
    359 		lfs_vunref(vp);
    360 	}
    361 	if (ip->i_flag & IN_ADIROP) {
    362 		--fs->lfs_nadirop;
    363 		ip->i_flag &= ~IN_ADIROP;
    364 	}
    365 
    366 	if (ip->i_flag & IN_CLEANING) {
    367 		--fs->lfs_uinodes;
    368 	}
    369 	if (ip->i_flag & (IN_MODIFIED | IN_ACCESSED)) {
    370 		--fs->lfs_uinodes;
    371 	}
    372 	ip->i_flag &= ~IN_ALLMOD;
    373 #ifdef DEBUG_LFS
    374 	if((int32_t)fs->lfs_uinodes<0) {
    375 		printf("U1");
    376 		fs->lfs_uinodes=0;
    377 	}
    378 #endif
    379 	/*
    380 	 * Set the ifile's inode entry to unused, increment its version number
    381 	 * and link it into the free chain.
    382 	 */
    383 	LFS_IENTRY(ifp, fs, ino, bp);
    384 	old_iaddr = ifp->if_daddr;
    385 	ifp->if_daddr = LFS_UNUSED_DADDR;
    386 	++ifp->if_version;
    387 	ifp->if_nextfree = fs->lfs_free;
    388 	fs->lfs_free = ino;
    389 	(void) VOP_BWRITE(bp);
    390 #ifdef DIAGNOSTIC
    391 	if(fs->lfs_free == LFS_UNUSED_INUM) {
    392 		panic("inode 0 freed");
    393 	}
    394 #endif /* DIAGNOSTIC */
    395 	if (old_iaddr != LFS_UNUSED_DADDR) {
    396 		LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp);
    397 #ifdef DIAGNOSTIC
    398 		if (sup->su_nbytes < DINODE_SIZE) {
    399 			printf("lfs_vfree: negative byte count (segment %d short by %d)\n", datosn(fs, old_iaddr), (int)DINODE_SIZE - sup->su_nbytes);
    400 			panic("lfs_vfree: negative byte count");
    401 			sup->su_nbytes = DINODE_SIZE;
    402 		}
    403 #endif
    404 		sup->su_nbytes -= DINODE_SIZE;
    405 		(void) VOP_BWRITE(bp);
    406 	}
    407 
    408 	/* Set superblock modified bit and decrement file count. */
    409 	fs->lfs_fmod = 1;
    410 	--fs->lfs_nfiles;
    411 
    412 	lfs_segunlock(fs);
    413 	return (0);
    414 }
    415