Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.47
      1 /*	$NetBSD: lfs_alloc.c,v 1.47 2001/05/30 11:57:18 mrg 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_OPT)
     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 <ufs/ufs/quota.h>
     88 #include <ufs/ufs/inode.h>
     89 #include <ufs/ufs/ufsmount.h>
     90 #include <ufs/ufs/ufs_extern.h>
     91 
     92 #include <ufs/lfs/lfs.h>
     93 #include <ufs/lfs/lfs_extern.h>
     94 
     95 extern int lfs_dirvcount;
     96 extern struct lock ufs_hashlock;
     97 
     98 static int extend_ifile(struct lfs *, struct ucred *);
     99 static int lfs_ialloc(struct lfs *, struct vnode *, ino_t, int, struct vnode **);
    100 
    101 /*
    102  * Allocate a particular inode with a particular version number, freeing
    103  * any previous versions of this inode that may have gone before.
    104  * Used by the roll-forward code.
    105  *
    106  * XXX this function does not have appropriate locking to be used on a live fs;
    107  * XXX but something similar could probably be used for an "undelete" call.
    108  */
    109 int
    110 lfs_rf_valloc(struct lfs *fs, ino_t ino, int version, struct proc *p,
    111 	      struct vnode **vpp)
    112 {
    113 	IFILE *ifp;
    114 	struct buf *bp;
    115 	struct vnode *vp;
    116 	struct inode *ip;
    117 	ino_t tino, oldnext;
    118 	int error;
    119 
    120 	/*
    121 	 * First, just try a vget. If the version number is the one we want,
    122 	 * we don't have to do anything else.  If the version number is wrong,
    123 	 * take appropriate action.
    124 	 */
    125 	error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp);
    126 	if (error == 0) {
    127 		/* printf("lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp); */
    128 
    129 		*vpp = vp;
    130 		ip = VTOI(vp);
    131 		if (ip->i_ffs_gen == version)
    132 			return 0;
    133 		else if (ip->i_ffs_gen < version) {
    134 			VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, p);
    135 			ip->i_ffs_gen = version;
    136 			LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED | IN_UPDATE);
    137 			return 0;
    138 		} else {
    139 			/* printf("ino %d: asked for version %d but got %d\n",
    140 			       ino, version, ip->i_ffs_gen); */
    141 			vput(vp);
    142 			*vpp = NULLVP;
    143 			return EEXIST;
    144 		}
    145 	}
    146 
    147 	/*
    148 	 * The inode is not in use.  Find it on the free list.
    149 	 */
    150 	/* If the Ifile is too short to contain this inum, extend it */
    151 	while (VTOI(fs->lfs_ivnode)->i_ffs_size <=
    152 	       dbtob(fsbtodb(fs, ino / fs->lfs_ifpb + fs->lfs_cleansz +
    153 			     fs->lfs_segtabsz))) {
    154 		extend_ifile(fs, NOCRED);
    155 	}
    156 
    157 	LFS_IENTRY(ifp, fs, ino, bp);
    158 	oldnext = ifp->if_nextfree;
    159 	ifp->if_version = version;
    160 	brelse(bp);
    161 
    162 	if (ino == fs->lfs_free) {
    163 		fs->lfs_free = oldnext;
    164 	} else {
    165 		tino = fs->lfs_free;
    166 		while(1) {
    167 			LFS_IENTRY(ifp, fs, tino, bp);
    168 			if (ifp->if_nextfree == ino ||
    169 			    ifp->if_nextfree == LFS_UNUSED_INUM)
    170 				break;
    171 			tino = ifp->if_nextfree;
    172 			brelse(bp);
    173 		}
    174 		if (ifp->if_nextfree == LFS_UNUSED_INUM) {
    175 			brelse(bp);
    176 			return ENOENT;
    177 		}
    178 		ifp->if_nextfree = oldnext;
    179 		VOP_BWRITE(bp);
    180 	}
    181 
    182 	error = lfs_ialloc(fs, fs->lfs_ivnode, ino, version, &vp);
    183 	if (error == 0) {
    184 		/*
    185 		 * Make it VREG so we can put blocks on it.  We will change
    186 		 * this later if it turns out to be some other kind of file.
    187 		 */
    188 		ip = VTOI(vp);
    189 		ip->i_ffs_mode = IFREG;
    190 		ip->i_ffs_nlink = 1;
    191 		ip->i_ffs_effnlink = 1;
    192 		ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
    193 		ip = VTOI(vp);
    194 
    195 		/* printf("lfs_rf_valloc: ino %d vp %p\n", ino, vp); */
    196 
    197 		/* The dirop-nature of this vnode is past */
    198 		(void)lfs_vunref(vp);
    199 		--lfs_dirvcount;
    200 		vp->v_flag &= ~VDIROP;
    201 		--fs->lfs_nadirop;
    202 		ip->i_flag &= ~IN_ADIROP;
    203 	}
    204 	*vpp = vp;
    205 	return error;
    206 }
    207 
    208 static int
    209 extend_ifile(struct lfs *fs, struct ucred *cred)
    210 {
    211 	struct vnode *vp;
    212 	struct inode *ip;
    213 	IFILE *ifp;
    214 	struct buf *bp;
    215 	int error;
    216 	ufs_daddr_t i, blkno, max;
    217 	ino_t oldlast;
    218 
    219 	vp = fs->lfs_ivnode;
    220 	(void)lfs_vref(vp);
    221 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    222 	ip = VTOI(vp);
    223 	blkno = lblkno(fs, ip->i_ffs_size);
    224 	if ((error = VOP_BALLOC(vp, ip->i_ffs_size, fs->lfs_bsize, cred, 0,
    225 				&bp)) != 0) {
    226 		VOP_UNLOCK(vp, 0);
    227 		lfs_vunref(vp);
    228 		return (error);
    229 	}
    230 	ip->i_ffs_size += fs->lfs_bsize;
    231 	uvm_vnp_setsize(vp, ip->i_ffs_size);
    232 	VOP_UNLOCK(vp, 0);
    233 
    234 	i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
    235 		fs->lfs_ifpb;
    236 	oldlast = fs->lfs_free;
    237 	fs->lfs_free = i;
    238 #ifdef DIAGNOSTIC
    239 	if(fs->lfs_free == LFS_UNUSED_INUM)
    240 		panic("inode 0 allocated [2]");
    241 #endif /* DIAGNOSTIC */
    242 	max = i + fs->lfs_ifpb;
    243 	/* printf("extend ifile for ino %d--%d\n", i, max); */
    244 	for (ifp = (struct ifile *)bp->b_data; i < max; ++ifp) {
    245 		ifp->if_version = 1;
    246 		ifp->if_daddr = LFS_UNUSED_DADDR;
    247 		ifp->if_nextfree = ++i;
    248 	}
    249 	ifp--;
    250 	ifp->if_nextfree = oldlast;
    251 	(void) VOP_BWRITE(bp); /* Ifile */
    252 	lfs_vunref(vp);
    253 
    254 	return 0;
    255 }
    256 
    257 /* Allocate a new inode. */
    258 /* ARGSUSED */
    259 /* VOP_BWRITE 2i times */
    260 int
    261 lfs_valloc(v)
    262 	void *v;
    263 {
    264 	struct vop_valloc_args /* {
    265 				  struct vnode *a_pvp;
    266 				  int a_mode;
    267 				  struct ucred *a_cred;
    268 				  struct vnode **a_vpp;
    269 				  } */ *ap = v;
    270 	struct lfs *fs;
    271 	struct buf *bp;
    272 	struct ifile *ifp;
    273 	ino_t new_ino;
    274 	int error;
    275 	int new_gen;
    276 
    277 	fs = VTOI(ap->a_pvp)->i_lfs;
    278 	if (fs->lfs_ronly)
    279 		return EROFS;
    280 	*ap->a_vpp = NULL;
    281 
    282 	/*
    283 	 * Use lfs_seglock here, instead of fs->lfs_freelock, to ensure that
    284 	 * the free list is not changed in between the time that the ifile
    285 	 * blocks are written to disk and the time that the superblock is
    286 	 * written to disk.
    287 	 *
    288 	 * XXX this sucks.  We should instead encode the head of the free
    289 	 * list into the CLEANERINFO block of the Ifile. [XXX v2]
    290 	 */
    291 	lfs_seglock(fs, SEGM_PROT);
    292 
    293 	/* Get the head of the freelist. */
    294 	new_ino = fs->lfs_free;
    295 #ifdef DIAGNOSTIC
    296 	if(new_ino == LFS_UNUSED_INUM) {
    297 #ifdef DEBUG
    298 		lfs_dump_super(fs);
    299 #endif /* DEBUG */
    300 		panic("inode 0 allocated [1]");
    301 	}
    302 #endif /* DIAGNOSTIC */
    303 #ifdef ALLOCPRINT
    304 	printf("lfs_valloc: allocate inode %d\n", new_ino);
    305 #endif
    306 
    307 	/*
    308 	 * Remove the inode from the free list and write the new start
    309 	 * of the free list into the superblock.
    310 	 */
    311 	LFS_IENTRY(ifp, fs, new_ino, bp);
    312 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
    313 		panic("lfs_valloc: inuse inode %d on the free list", new_ino);
    314 	fs->lfs_free = ifp->if_nextfree;
    315 	new_gen = ifp->if_version; /* version was updated by vfree */
    316 	brelse(bp);
    317 
    318 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    319 	if (fs->lfs_free == LFS_UNUSED_INUM) {
    320 		if ((error = extend_ifile(fs, ap->a_cred)) != 0) {
    321 			fs->lfs_free = new_ino;
    322 			lfs_segunlock(fs);
    323 			return error;
    324 		}
    325 	}
    326 #ifdef DIAGNOSTIC
    327 	if(fs->lfs_free == LFS_UNUSED_INUM)
    328 		panic("inode 0 allocated [3]");
    329 #endif /* DIAGNOSTIC */
    330 
    331 	lfs_segunlock(fs);
    332 
    333 	return lfs_ialloc(fs, ap->a_pvp, new_ino, new_gen, ap->a_vpp);
    334 }
    335 
    336 static int
    337 lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen,
    338 	   struct vnode **vpp)
    339 {
    340 	struct inode *ip;
    341 	struct vnode *vp;
    342 	IFILE *ifp;
    343 	struct buf *bp;
    344 	int error;
    345 
    346 	error = getnewvnode(VT_LFS, pvp->v_mount, lfs_vnodeop_p, &vp);
    347 	/* printf("lfs_ialloc: ino %d vp %p error %d\n", new_ino, vp, error);*/
    348 	if (error)
    349 		goto errout;
    350 
    351 	lockmgr(&ufs_hashlock, LK_EXCLUSIVE, 0);
    352 	/* Create an inode to associate with the vnode. */
    353 	lfs_vcreate(pvp->v_mount, new_ino, vp);
    354 
    355 	ip = VTOI(vp);
    356 	/* Zero out the direct and indirect block addresses. */
    357 	bzero(&ip->i_din, sizeof(ip->i_din));
    358 	ip->i_din.ffs_din.di_inumber = new_ino;
    359 
    360 	/* Set a new generation number for this inode. */
    361 	if (new_gen)
    362 		ip->i_ffs_gen = new_gen;
    363 
    364 	/* Insert into the inode hash table. */
    365 	ufs_ihashins(ip);
    366 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    367 
    368 	error = ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
    369 	ip = VTOI(vp);
    370 	if (error) {
    371 		vput(vp);
    372 		goto errout;
    373 	}
    374 	/* printf("lfs_ialloc[2]: ino %d vp %p\n", new_ino, vp);*/
    375 
    376 	*vpp = vp;
    377 #if 1
    378 	if(!(vp->v_flag & VDIROP)) {
    379 		(void)lfs_vref(vp);
    380 		++lfs_dirvcount;
    381 	}
    382 	vp->v_flag |= VDIROP;
    383 
    384 	if(!(ip->i_flag & IN_ADIROP))
    385 		++fs->lfs_nadirop;
    386 	ip->i_flag |= IN_ADIROP;
    387 #endif
    388 	VREF(ip->i_devvp);
    389 	/* Set superblock modified bit and increment file count. */
    390 	fs->lfs_fmod = 1;
    391 	++fs->lfs_nfiles;
    392 	return (0);
    393 
    394     errout:
    395 	/*
    396 	 * Put the new inum back on the free list.
    397 	 */
    398 	LFS_IENTRY(ifp, fs, new_ino, bp);
    399 	ifp->if_daddr = LFS_UNUSED_DADDR;
    400 	ifp->if_nextfree = fs->lfs_free;
    401 	fs->lfs_free = new_ino;
    402 	(void) VOP_BWRITE(bp); /* Ifile */
    403 
    404 	*vpp = NULLVP;
    405 	return (error);
    406 }
    407 
    408 /* Create a new vnode/inode pair and initialize what fields we can. */
    409 void
    410 lfs_vcreate(mp, ino, vp)
    411 	struct mount *mp;
    412 	ino_t ino;
    413 	struct vnode *vp;
    414 {
    415 	struct inode *ip;
    416 	struct ufsmount *ump;
    417 #ifdef QUOTA
    418 	int i;
    419 #endif
    420 
    421 	/* Get a pointer to the private mount structure. */
    422 	ump = VFSTOUFS(mp);
    423 
    424 	/* Initialize the inode. */
    425 	ip = pool_get(&lfs_inode_pool, PR_WAITOK);
    426 	vp->v_data = ip;
    427 	ip->i_vnode = vp;
    428 	ip->i_devvp = ump->um_devvp;
    429 	ip->i_dev = ump->um_dev;
    430 	ip->i_number = ip->i_din.ffs_din.di_inumber = ino;
    431 	ip->i_lfs = ump->um_lfs;
    432 #ifdef QUOTA
    433 	for (i = 0; i < MAXQUOTAS; i++)
    434 		ip->i_dquot[i] = NODQUOT;
    435 #endif
    436 	ip->i_lockf = 0;
    437 	ip->i_diroff = 0;
    438 	ip->i_ffs_mode = 0;
    439 	ip->i_ffs_size = 0;
    440 	ip->i_ffs_blocks = 0;
    441 	ip->i_lfs_effnblks = 0;
    442 	ip->i_flag = 0;
    443 	LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED);
    444 }
    445 
    446 /* Free an inode. */
    447 /* ARGUSED */
    448 /* VOP_BWRITE 2i times */
    449 int
    450 lfs_vfree(v)
    451 	void *v;
    452 {
    453 	struct vop_vfree_args /* {
    454 				 struct vnode *a_pvp;
    455 				 ino_t a_ino;
    456 				 int a_mode;
    457 				 } */ *ap = v;
    458 	SEGUSE *sup;
    459 	struct buf *bp;
    460 	struct ifile *ifp;
    461 	struct inode *ip;
    462 	struct vnode *vp;
    463 	struct lfs *fs;
    464 	ufs_daddr_t old_iaddr;
    465 	ino_t ino;
    466 	extern int lfs_dirvcount;
    467 
    468 	/* Get the inode number and file system. */
    469 	vp = ap->a_pvp;
    470 	ip = VTOI(vp);
    471 	fs = ip->i_lfs;
    472 	ino = ip->i_number;
    473 
    474 #if 0
    475 	/*
    476 	 * Right now this is unnecessary since we take the seglock.
    477 	 * But if the seglock is no longer necessary (e.g. we put the
    478 	 * head of the free list into the Ifile) we will need to drain
    479 	 * this vnode of any pending writes.
    480 	 */
    481 	if (WRITEINPROG(vp))
    482 		tsleep(vp, (PRIBIO+1), "lfs_vfree", 0);
    483 #endif
    484 	lfs_seglock(fs, SEGM_PROT);
    485 
    486 	if(vp->v_flag & VDIROP) {
    487 		--lfs_dirvcount;
    488 		vp->v_flag &= ~VDIROP;
    489 		wakeup(&lfs_dirvcount);
    490 		lfs_vunref(vp);
    491 	}
    492 	if (ip->i_flag & IN_ADIROP) {
    493 		--fs->lfs_nadirop;
    494 		ip->i_flag &= ~IN_ADIROP;
    495 	}
    496 
    497 	LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
    498 	ip->i_flag &= ~IN_ALLMOD;
    499 
    500 	/*
    501 	 * Set the ifile's inode entry to unused, increment its version number
    502 	 * and link it into the free chain.
    503 	 */
    504 	LFS_IENTRY(ifp, fs, ino, bp);
    505 	old_iaddr = ifp->if_daddr;
    506 	ifp->if_daddr = LFS_UNUSED_DADDR;
    507 	++ifp->if_version;
    508 	ifp->if_nextfree = fs->lfs_free;
    509 	fs->lfs_free = ino;
    510 	(void) VOP_BWRITE(bp); /* Ifile */
    511 #ifdef DIAGNOSTIC
    512 	if(fs->lfs_free == LFS_UNUSED_INUM) {
    513 		panic("inode 0 freed");
    514 	}
    515 #endif /* DIAGNOSTIC */
    516 	if (old_iaddr != LFS_UNUSED_DADDR) {
    517 		LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp);
    518 #ifdef DIAGNOSTIC
    519 		if (sup->su_nbytes < DINODE_SIZE) {
    520 			printf("lfs_vfree: negative byte count"
    521 			       " (segment %d short by %d)\n",
    522 			       datosn(fs, old_iaddr),
    523 			       (int)DINODE_SIZE - sup->su_nbytes);
    524 			panic("lfs_vfree: negative byte count");
    525 			sup->su_nbytes = DINODE_SIZE;
    526 		}
    527 #endif
    528 		sup->su_nbytes -= DINODE_SIZE;
    529 		(void) VOP_BWRITE(bp); /* Ifile */
    530 	}
    531 
    532 	/* Set superblock modified bit and decrement file count. */
    533 	fs->lfs_fmod = 1;
    534 	--fs->lfs_nfiles;
    535 
    536 	lfs_segunlock(fs);
    537 	return (0);
    538 }
    539