Home | History | Annotate | Line # | Download | only in lfs
lfs_balloc.c revision 1.49
      1 /*	$NetBSD: lfs_balloc.c,v 1.49 2005/02/26 05:40:42 perseant Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2002, 2003 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) 1989, 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. Neither the name of the University nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  *
     66  *	@(#)lfs_balloc.c	8.4 (Berkeley) 5/8/95
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: lfs_balloc.c,v 1.49 2005/02/26 05:40:42 perseant Exp $");
     71 
     72 #if defined(_KERNEL_OPT)
     73 #include "opt_quota.h"
     74 #endif
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/buf.h>
     79 #include <sys/proc.h>
     80 #include <sys/vnode.h>
     81 #include <sys/mount.h>
     82 #include <sys/resourcevar.h>
     83 #include <sys/trace.h>
     84 #include <sys/malloc.h>
     85 
     86 #include <miscfs/specfs/specdev.h>
     87 
     88 #include <ufs/ufs/quota.h>
     89 #include <ufs/ufs/inode.h>
     90 #include <ufs/ufs/ufsmount.h>
     91 #include <ufs/ufs/ufs_extern.h>
     92 
     93 #include <ufs/lfs/lfs.h>
     94 #include <ufs/lfs/lfs_extern.h>
     95 
     96 #include <uvm/uvm.h>
     97 
     98 int lfs_fragextend(struct vnode *, int, int, daddr_t, struct buf **, struct ucred *);
     99 
    100 u_int64_t locked_fakequeue_count;
    101 
    102 /*
    103  * Allocate a block, and to inode and filesystem block accounting for it
    104  * and for any indirect blocks the may need to be created in order for
    105  * this block to be created.
    106  *
    107  * Blocks which have never been accounted for (i.e., which "do not exist")
    108  * have disk address 0, which is translated by ufs_bmap to the special value
    109  * UNASSIGNED == -1, as in the historical UFS.
    110  *
    111  * Blocks which have been accounted for but which have not yet been written
    112  * to disk are given the new special disk address UNWRITTEN == -2, so that
    113  * they can be differentiated from completely new blocks.
    114  */
    115 /* VOP_BWRITE NIADDR+2 times */
    116 int
    117 lfs_balloc(void *v)
    118 {
    119 	struct vop_balloc_args /* {
    120 		struct vnode *a_vp;
    121 		off_t a_startoffset;
    122 		int a_size;
    123 		struct ucred *a_cred;
    124 		int a_flags;
    125 		struct buf *a_bpp;
    126 	} */ *ap = v;
    127 	struct vnode *vp;
    128 	int offset;
    129 	u_long iosize;
    130 	daddr_t daddr, idaddr;
    131 	struct buf *ibp, *bp, **bpp;
    132 	struct inode *ip;
    133 	struct lfs *fs;
    134 	struct indir indirs[NIADDR+2], *idp;
    135 	daddr_t	lbn, lastblock;
    136 	int bb, bcount;
    137 	int error, frags, i, nsize, osize, num;
    138 
    139 	vp = ap->a_vp;
    140 	ip = VTOI(vp);
    141 	fs = ip->i_lfs;
    142 	offset = blkoff(fs, ap->a_startoffset);
    143 	iosize = ap->a_size;
    144 	KASSERT(iosize <= fs->lfs_bsize);
    145 	lbn = lblkno(fs, ap->a_startoffset);
    146 	/* (void)lfs_check(vp, lbn, 0); */
    147 	bpp = ap->a_bpp;
    148 
    149 	/*
    150 	 * Three cases: it's a block beyond the end of file, it's a block in
    151 	 * the file that may or may not have been assigned a disk address or
    152 	 * we're writing an entire block.
    153 	 *
    154 	 * Note, if the daddr is UNWRITTEN, the block already exists in
    155 	 * the cache (it was read or written earlier).	If so, make sure
    156 	 * we don't count it as a new block or zero out its contents. If
    157 	 * it did not, make sure we allocate any necessary indirect
    158 	 * blocks.
    159 	 *
    160 	 * If we are writing a block beyond the end of the file, we need to
    161 	 * check if the old last block was a fragment.	If it was, we need
    162 	 * to rewrite it.
    163 	 */
    164 
    165 	if (bpp)
    166 		*bpp = NULL;
    167 
    168 	/* Bomb out immediately if there's no space left */
    169 	if (fs->lfs_bfree <= 0)
    170 		return ENOSPC;
    171 
    172 	/* Check for block beyond end of file and fragment extension needed. */
    173 	lastblock = lblkno(fs, ip->i_size);
    174 	if (lastblock < NDADDR && lastblock < lbn) {
    175 		osize = blksize(fs, ip, lastblock);
    176 		if (osize < fs->lfs_bsize && osize > 0) {
    177 			if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
    178 						    lastblock,
    179 						    (bpp ? &bp : NULL),
    180 						    ap->a_cred)))
    181 				return (error);
    182 			ip->i_ffs1_size = ip->i_size =
    183 			    (lastblock + 1) * fs->lfs_bsize;
    184 			uvm_vnp_setsize(vp, ip->i_size);
    185 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    186 			if (bpp)
    187 				(void) VOP_BWRITE(bp);
    188 		}
    189 	}
    190 
    191 	/*
    192 	 * If the block we are writing is a direct block, it's the last
    193 	 * block in the file, and offset + iosize is less than a full
    194 	 * block, we can write one or more fragments.  There are two cases:
    195 	 * the block is brand new and we should allocate it the correct
    196 	 * size or it already exists and contains some fragments and
    197 	 * may need to extend it.
    198 	 */
    199 	if (lbn < NDADDR && lblkno(fs, ip->i_size) <= lbn) {
    200 		osize = blksize(fs, ip, lbn);
    201 		nsize = fragroundup(fs, offset + iosize);
    202 		if (lblktosize(fs, lbn) >= ip->i_size) {
    203 			/* Brand new block or fragment */
    204 			frags = numfrags(fs, nsize);
    205 			bb = fragstofsb(fs, frags);
    206 			if (bpp) {
    207 				*ap->a_bpp = bp = getblk(vp, lbn, nsize, 0, 0);
    208 				bp->b_blkno = UNWRITTEN;
    209 				if (ap->a_flags & B_CLRBUF)
    210 					clrbuf(bp);
    211 			}
    212 			ip->i_lfs_effnblks += bb;
    213 			ip->i_lfs->lfs_bfree -= bb;
    214 			ip->i_ffs1_db[lbn] = UNWRITTEN;
    215 		} else {
    216 			if (nsize <= osize) {
    217 				/* No need to extend */
    218 				if (bpp && (error = bread(vp, lbn, osize, NOCRED, &bp)))
    219 					return error;
    220 			} else {
    221 				/* Extend existing block */
    222 				if ((error =
    223 				     lfs_fragextend(vp, osize, nsize, lbn,
    224 						    (bpp ? &bp : NULL),
    225 						    ap->a_cred)))
    226 					return error;
    227 			}
    228 			if (bpp)
    229 				*bpp = bp;
    230 		}
    231 		return 0;
    232 	}
    233 
    234 	error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL, NULL);
    235 	if (error)
    236 		return (error);
    237 
    238 	daddr = (daddr_t)((int32_t)daddr); /* XXX ondisk32 */
    239 	KASSERT(daddr <= LFS_MAX_DADDR);
    240 
    241 	/*
    242 	 * Do byte accounting all at once, so we can gracefully fail *before*
    243 	 * we start assigning blocks.
    244 	 */
    245 	bb = VFSTOUFS(vp->v_mount)->um_seqinc;
    246 	bcount = 0;
    247 	if (daddr == UNASSIGNED) {
    248 		bcount = bb;
    249 	}
    250 	for (i = 1; i < num; ++i) {
    251 		if (!indirs[i].in_exists) {
    252 			bcount += bb;
    253 		}
    254 	}
    255 	if (ISSPACE(fs, bcount, ap->a_cred)) {
    256 		ip->i_lfs->lfs_bfree -= bcount;
    257 		ip->i_lfs_effnblks += bcount;
    258 	} else {
    259 		return ENOSPC;
    260 	}
    261 
    262 	if (daddr == UNASSIGNED) {
    263 		if (num > 0 && ip->i_ffs1_ib[indirs[0].in_off] == 0) {
    264 			ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
    265 		}
    266 
    267 		/*
    268 		 * Create new indirect blocks if necessary
    269 		 */
    270 		if (num > 1) {
    271 			idaddr = ip->i_ffs1_ib[indirs[0].in_off];
    272 			for (i = 1; i < num; ++i) {
    273 				ibp = getblk(vp, indirs[i].in_lbn,
    274 				    fs->lfs_bsize, 0,0);
    275 				if (!indirs[i].in_exists) {
    276 					clrbuf(ibp);
    277 					ibp->b_blkno = UNWRITTEN;
    278 				} else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) {
    279 					ibp->b_blkno = fsbtodb(fs, idaddr);
    280 					ibp->b_flags |= B_READ;
    281 					VOP_STRATEGY(vp, ibp);
    282 					biowait(ibp);
    283 				}
    284 				/*
    285 				 * This block exists, but the next one may not.
    286 				 * If that is the case mark it UNWRITTEN to keep
    287 				 * the accounting straight.
    288 				 */
    289 				/* XXX ondisk32 */
    290 				if (((int32_t *)ibp->b_data)[indirs[i].in_off] == 0)
    291 					((int32_t *)ibp->b_data)[indirs[i].in_off] =
    292 						UNWRITTEN;
    293 				/* XXX ondisk32 */
    294 				idaddr = ((int32_t *)ibp->b_data)[indirs[i].in_off];
    295 				if ((error = VOP_BWRITE(ibp)))
    296 					return error;
    297 			}
    298 		}
    299 	}
    300 
    301 
    302 	/*
    303 	 * Get the existing block from the cache, if requested.
    304 	 */
    305 	frags = fsbtofrags(fs, bb);
    306 	if (bpp)
    307 		*bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
    308 
    309 	/*
    310 	 * Do accounting on blocks that represent pages.
    311 	 */
    312 	if (!bpp)
    313 		lfs_register_block(vp, lbn);
    314 
    315 	/*
    316 	 * The block we are writing may be a brand new block
    317 	 * in which case we need to do accounting.
    318 	 *
    319 	 * We can tell a truly new block because ufs_bmaparray will say
    320 	 * it is UNASSIGNED.  Once we allocate it we will assign it the
    321 	 * disk address UNWRITTEN.
    322 	 */
    323 	if (daddr == UNASSIGNED) {
    324 		if (bpp) {
    325 			if (ap->a_flags & B_CLRBUF)
    326 				clrbuf(bp);
    327 
    328 			/* Note the new address */
    329 			bp->b_blkno = UNWRITTEN;
    330 		}
    331 
    332 		switch (num) {
    333 		    case 0:
    334 			ip->i_ffs1_db[lbn] = UNWRITTEN;
    335 			break;
    336 		    case 1:
    337 			ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
    338 			break;
    339 		    default:
    340 			idp = &indirs[num - 1];
    341 			if (bread(vp, idp->in_lbn, fs->lfs_bsize, NOCRED,
    342 				  &ibp))
    343 				panic("lfs_balloc: bread bno %lld",
    344 				    (long long)idp->in_lbn);
    345 			/* XXX ondisk32 */
    346 			((int32_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
    347 			VOP_BWRITE(ibp);
    348 		}
    349 	} else if (bpp && !(bp->b_flags & (B_DONE|B_DELWRI))) {
    350 		/*
    351 		 * Not a brand new block, also not in the cache;
    352 		 * read it in from disk.
    353 		 */
    354 		if (iosize == fs->lfs_bsize)
    355 			/* Optimization: I/O is unnecessary. */
    356 			bp->b_blkno = daddr;
    357 		else {
    358 			/*
    359 			 * We need to read the block to preserve the
    360 			 * existing bytes.
    361 			 */
    362 			bp->b_blkno = daddr;
    363 			bp->b_flags |= B_READ;
    364 			VOP_STRATEGY(vp, bp);
    365 			return (biowait(bp));
    366 		}
    367 	}
    368 
    369 	return (0);
    370 }
    371 
    372 /* VOP_BWRITE 1 time */
    373 int
    374 lfs_fragextend(struct vnode *vp, int osize, int nsize, daddr_t lbn, struct buf **bpp, struct ucred *cred)
    375 {
    376 	struct inode *ip;
    377 	struct lfs *fs;
    378 	long bb;
    379 	int error;
    380 	extern long locked_queue_bytes;
    381 	size_t obufsize;
    382 
    383 	ip = VTOI(vp);
    384 	fs = ip->i_lfs;
    385 	bb = (long)fragstofsb(fs, numfrags(fs, nsize - osize));
    386 	error = 0;
    387 
    388 	/*
    389 	 * Get the seglock so we don't enlarge blocks while a segment
    390 	 * is being written.  If we're called with bpp==NULL, though,
    391 	 * we are only pretending to change a buffer, so we don't have to
    392 	 * lock.
    393 	 */
    394     top:
    395 	if (bpp) {
    396 		lockmgr(&fs->lfs_fraglock, LK_SHARED, 0);
    397 		LFS_DEBUG_COUNTLOCKED("frag");
    398 	}
    399 
    400 	if (!ISSPACE(fs, bb, cred)) {
    401 		error = ENOSPC;
    402 		goto out;
    403 	}
    404 
    405 	/*
    406 	 * If we are not asked to actually return the block, all we need
    407 	 * to do is allocate space for it.  UBC will handle dirtying the
    408 	 * appropriate things and making sure it all goes to disk.
    409 	 * Don't bother to read in that case.
    410 	 */
    411 	if (bpp && (error = bread(vp, lbn, osize, NOCRED, bpp))) {
    412 		brelse(*bpp);
    413 		goto out;
    414 	}
    415 #ifdef QUOTA
    416 	if ((error = chkdq(ip, bb, cred, 0))) {
    417 		if (bpp)
    418 			brelse(*bpp);
    419 		goto out;
    420 	}
    421 #endif
    422 	/*
    423 	 * Adjust accounting for lfs_avail.  If there's not enough room,
    424 	 * we will have to wait for the cleaner, which we can't do while
    425 	 * holding a block busy or while holding the seglock.  In that case,
    426 	 * release both and start over after waiting.
    427 	 */
    428 
    429 	if (bpp && ((*bpp)->b_flags & B_DELWRI)) {
    430 		if (!lfs_fits(fs, bb)) {
    431 			if (bpp)
    432 				brelse(*bpp);
    433 #ifdef QUOTA
    434 			chkdq(ip, -bb, cred, 0);
    435 #endif
    436 			lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    437 			lfs_availwait(fs, bb);
    438 			goto top;
    439 		}
    440 		fs->lfs_avail -= bb;
    441 	}
    442 
    443 	fs->lfs_bfree -= bb;
    444 	ip->i_lfs_effnblks += bb;
    445 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    446 
    447 	if (bpp) {
    448 		obufsize = (*bpp)->b_bufsize;
    449 		allocbuf(*bpp, nsize, 1);
    450 
    451 		/* Adjust locked-list accounting */
    452 		if (((*bpp)->b_flags & (B_LOCKED | B_CALL)) == B_LOCKED)
    453 			locked_queue_bytes += (*bpp)->b_bufsize - obufsize;
    454 
    455 		bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
    456 	}
    457 
    458     out:
    459 	if (bpp) {
    460 		lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    461 	}
    462 	return (error);
    463 }
    464 
    465 /*
    466  * Record this lbn as being "write pending".  We used to have this information
    467  * on the buffer headers, but since pages don't have buffer headers we
    468  * record it here instead.
    469  */
    470 
    471 void
    472 lfs_register_block(struct vnode *vp, daddr_t lbn)
    473 {
    474 	struct lfs *fs;
    475 	struct inode *ip;
    476 	struct lbnentry *lbp;
    477 	int hash;
    478 
    479 	/* Don't count metadata */
    480 	if (lbn < 0 || vp->v_type != VREG || VTOI(vp)->i_number == LFS_IFILE_INUM)
    481 		return;
    482 
    483 	ip = VTOI(vp);
    484 	fs = ip->i_lfs;
    485 
    486 	/* If no space, wait for the cleaner */
    487 	lfs_availwait(fs, btofsb(fs, 1 << fs->lfs_bshift));
    488 
    489 	hash = lbn % LFS_BLIST_HASH_WIDTH;
    490 	LIST_FOREACH(lbp, &(ip->i_lfs_blist[hash]), entry) {
    491 		if (lbp->lbn == lbn)
    492 			return;
    493 	}
    494 
    495 	lbp = (struct lbnentry *)pool_get(&lfs_lbnentry_pool, PR_WAITOK);
    496 	lbp->lbn = lbn;
    497 	LIST_INSERT_HEAD(&(ip->i_lfs_blist[hash]), lbp, entry);
    498 	fs->lfs_favail += btofsb(fs, (1 << fs->lfs_bshift));
    499 	++locked_fakequeue_count;
    500 }
    501 
    502 static void
    503 lfs_do_deregister(struct lfs *fs, struct lbnentry *lbp)
    504 {
    505 	LIST_REMOVE(lbp, entry);
    506 	pool_put(&lfs_lbnentry_pool, lbp);
    507 	if (fs->lfs_favail > btofsb(fs, (1 << fs->lfs_bshift)))
    508 		fs->lfs_favail -= btofsb(fs, (1 << fs->lfs_bshift));
    509 	if (locked_fakequeue_count > 0)
    510 		--locked_fakequeue_count;
    511 }
    512 
    513 void
    514 lfs_deregister_block(struct vnode *vp, daddr_t lbn)
    515 {
    516 	struct lfs *fs;
    517 	struct inode *ip;
    518 	struct lbnentry *lbp;
    519 	int hash;
    520 
    521 	/* Don't count metadata */
    522 	if (lbn < 0 || vp->v_type != VREG || VTOI(vp)->i_number == LFS_IFILE_INUM)
    523 		return;
    524 
    525 	ip = VTOI(vp);
    526 	fs = ip->i_lfs;
    527 	hash = lbn % LFS_BLIST_HASH_WIDTH;
    528 	LIST_FOREACH(lbp, &(ip->i_lfs_blist[hash]), entry) {
    529 		if (lbp->lbn == lbn)
    530 			break;
    531 	}
    532 	if (lbp == NULL)
    533 		return;
    534 
    535 	lfs_do_deregister(fs, lbp);
    536 }
    537 
    538 void
    539 lfs_deregister_all(struct vnode *vp)
    540 {
    541 	struct lbnentry *lbp;
    542 	struct lfs *fs;
    543 	struct inode *ip;
    544 	int i;
    545 
    546 	ip = VTOI(vp);
    547 	fs = ip->i_lfs;
    548 	for (i = 0; i < LFS_BLIST_HASH_WIDTH; i++)
    549 		while((lbp = LIST_FIRST(&(ip->i_lfs_blist[i]))) != NULL)
    550 			lfs_do_deregister(fs, lbp);
    551 }
    552