Home | History | Annotate | Line # | Download | only in lfs
lfs_balloc.c revision 1.39
      1 /*	$NetBSD: lfs_balloc.c,v 1.39 2003/03/15 06:58:50 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. 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_balloc.c	8.4 (Berkeley) 5/8/95
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 __KERNEL_RCSID(0, "$NetBSD: lfs_balloc.c,v 1.39 2003/03/15 06:58:50 perseant Exp $");
     75 
     76 #if defined(_KERNEL_OPT)
     77 #include "opt_quota.h"
     78 #endif
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/buf.h>
     83 #include <sys/proc.h>
     84 #include <sys/vnode.h>
     85 #include <sys/mount.h>
     86 #include <sys/resourcevar.h>
     87 #include <sys/trace.h>
     88 
     89 #include <miscfs/specfs/specdev.h>
     90 
     91 #include <ufs/ufs/quota.h>
     92 #include <ufs/ufs/inode.h>
     93 #include <ufs/ufs/ufsmount.h>
     94 #include <ufs/ufs/ufs_extern.h>
     95 
     96 #include <ufs/lfs/lfs.h>
     97 #include <ufs/lfs/lfs_extern.h>
     98 
     99 #include <uvm/uvm.h>
    100 
    101 int lfs_fragextend(struct vnode *, int, int, daddr_t, struct buf **, struct ucred *);
    102 
    103 /*
    104  * Allocate a block, and to inode and filesystem block accounting for it
    105  * and for any indirect blocks the may need to be created in order for
    106  * this block to be created.
    107  *
    108  * Blocks which have never been accounted for (i.e., which "do not exist")
    109  * have disk address 0, which is translated by ufs_bmap to the special value
    110  * UNASSIGNED == -1, as in the historical UFS.
    111  *
    112  * Blocks which have been accounted for but which have not yet been written
    113  * to disk are given the new special disk address UNWRITTEN == -2, so that
    114  * they can be differentiated from completely new blocks.
    115  */
    116 /* VOP_BWRITE NIADDR+2 times */
    117 int
    118 lfs_balloc(void *v)
    119 {
    120 	struct vop_balloc_args /* {
    121 		struct vnode *a_vp;
    122 		off_t a_startoffset;
    123 		int a_size;
    124 		struct ucred *a_cred;
    125 		int a_flags;
    126 		struct buf *a_bpp;
    127 	} */ *ap = v;
    128 	struct vnode *vp;
    129 	int offset;
    130 	u_long iosize;
    131 	daddr_t daddr, idaddr;
    132 	struct buf *ibp, *bp, **bpp;
    133 	struct inode *ip;
    134 	struct lfs *fs;
    135 	struct indir indirs[NIADDR+2], *idp;
    136 	daddr_t	lbn, lastblock;
    137 	int bb, bcount;
    138 	int error, frags, i, nsize, osize, num;
    139 
    140 	vp = ap->a_vp;
    141 	ip = VTOI(vp);
    142 	fs = ip->i_lfs;
    143 	offset = blkoff(fs, ap->a_startoffset);
    144 	iosize = ap->a_size;
    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 	/* Check for block beyond end of file and fragment extension needed. */
    169 	lastblock = lblkno(fs, ip->i_ffs_size);
    170 	if (lastblock < NDADDR && lastblock < lbn) {
    171 		osize = blksize(fs, ip, lastblock);
    172 		if (osize < fs->lfs_bsize && osize > 0) {
    173 			if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
    174 						    lastblock,
    175 						    (bpp ? &bp : NULL),
    176 						    ap->a_cred)))
    177 				return (error);
    178 			ip->i_ffs_size = (lastblock + 1) * fs->lfs_bsize;
    179 			uvm_vnp_setsize(vp, ip->i_ffs_size);
    180 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    181 			if (bpp)
    182 				(void) VOP_BWRITE(bp);
    183 		}
    184 	}
    185 
    186 	/*
    187 	 * If the block we are writing is a direct block, it's the last
    188 	 * block in the file, and offset + iosize is less than a full
    189 	 * block, we can write one or more fragments.  There are two cases:
    190 	 * the block is brand new and we should allocate it the correct
    191 	 * size or it already exists and contains some fragments and
    192 	 * may need to extend it.
    193 	 */
    194 	if (lbn < NDADDR && lblkno(fs, ip->i_ffs_size) <= lbn) {
    195 		osize = blksize(fs, ip, lbn);
    196 		nsize = fragroundup(fs, offset + iosize);
    197 		if (lblktosize(fs, lbn) >= ip->i_ffs_size) {
    198 			/* Brand new block or fragment */
    199 			frags = numfrags(fs, nsize);
    200 			bb = fragstofsb(fs, frags);
    201 			if (bpp) {
    202 				*ap->a_bpp = bp = getblk(vp, lbn, nsize, 0, 0);
    203 				bp->b_blkno = UNWRITTEN;
    204 				if (ap->a_flags & B_CLRBUF)
    205 					clrbuf(bp);
    206 			}
    207 			ip->i_lfs_effnblks += bb;
    208 			ip->i_lfs->lfs_bfree -= bb;
    209 			ip->i_ffs_db[lbn] = UNWRITTEN;
    210 		} else {
    211 			if (nsize <= osize) {
    212 				/* No need to extend */
    213 				if (bpp && (error = bread(vp, lbn, osize, NOCRED, &bp)))
    214 					return error;
    215 			} else {
    216 				/* Extend existing block */
    217 				if ((error =
    218 				     lfs_fragextend(vp, osize, nsize, lbn,
    219 						    (bpp ? &bp : NULL),
    220 						    ap->a_cred)))
    221 					return error;
    222 			}
    223 			if (bpp)
    224 				*bpp = bp;
    225 		}
    226 		return 0;
    227 	}
    228 
    229 	error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL );
    230 	if (error)
    231 		return (error);
    232 	/*
    233 	 * Do byte accounting all at once, so we can gracefully fail *before*
    234 	 * we start assigning blocks.
    235 	 */
    236 	bb = VFSTOUFS(vp->v_mount)->um_seqinc;
    237 	bcount = 0;
    238 	if (daddr == UNASSIGNED) {
    239 		bcount = bb;
    240 	}
    241 	for (i = 1; i < num; ++i) {
    242 		if (!indirs[i].in_exists) {
    243 			bcount += bb;
    244 		}
    245 	}
    246 	if (ISSPACE(fs, bcount, ap->a_cred)) {
    247 		ip->i_lfs->lfs_bfree -= bcount;
    248 		ip->i_lfs_effnblks += bcount;
    249 	} else {
    250 		return ENOSPC;
    251 	}
    252 
    253 	if (daddr == UNASSIGNED) {
    254 		if (num > 0 && ip->i_ffs_ib[indirs[0].in_off] == 0) {
    255 			ip->i_ffs_ib[indirs[0].in_off] = UNWRITTEN;
    256 		}
    257 
    258 		/*
    259 		 * Create new indirect blocks if necessary
    260 		 */
    261 		if (num > 1)
    262 			idaddr = ip->i_ffs_ib[indirs[0].in_off];
    263 		for (i = 1; i < num; ++i) {
    264 			ibp = getblk(vp, indirs[i].in_lbn, fs->lfs_bsize, 0,0);
    265 			if (!indirs[i].in_exists) {
    266 				clrbuf(ibp);
    267 				ibp->b_blkno = UNWRITTEN;
    268 			} else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) {
    269 				ibp->b_blkno = fsbtodb(fs, idaddr);
    270 				ibp->b_flags |= B_READ;
    271 				VOP_STRATEGY(ibp);
    272 				biowait(ibp);
    273 			}
    274 			/*
    275 			 * This block exists, but the next one may not.
    276 			 * If that is the case mark it UNWRITTEN to keep
    277 			 * the accounting straight.
    278 			 */
    279 			/* XXX ondisk32 */
    280 			if (((int32_t *)ibp->b_data)[indirs[i].in_off] == 0)
    281 				((int32_t *)ibp->b_data)[indirs[i].in_off] =
    282 					UNWRITTEN;
    283 			/* XXX ondisk32 */
    284 			idaddr = ((int32_t *)ibp->b_data)[indirs[i].in_off];
    285 			if ((error = VOP_BWRITE(ibp))) {
    286 				return error;
    287 			}
    288 		}
    289 	}
    290 
    291 
    292 	/*
    293 	 * Get the existing block from the cache, if requested.
    294 	 */
    295 	frags = fsbtofrags(fs, bb);
    296 	if (bpp)
    297 		*bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
    298 
    299 	/*
    300 	 * The block we are writing may be a brand new block
    301 	 * in which case we need to do accounting.
    302 	 *
    303 	 * We can tell a truly new block because ufs_bmaparray will say
    304 	 * it is UNASSIGNED.  Once we allocate it we will assign it the
    305 	 * disk address UNWRITTEN.
    306 	 */
    307 	if (daddr == UNASSIGNED) {
    308 		if (bpp) {
    309 			if (ap->a_flags & B_CLRBUF)
    310 				clrbuf(bp);
    311 
    312 			/* Note the new address */
    313 			bp->b_blkno = UNWRITTEN;
    314 		}
    315 
    316 		switch (num) {
    317 		    case 0:
    318 			ip->i_ffs_db[lbn] = UNWRITTEN;
    319 			break;
    320 		    case 1:
    321 			ip->i_ffs_ib[indirs[0].in_off] = UNWRITTEN;
    322 			break;
    323 		    default:
    324 			idp = &indirs[num - 1];
    325 			if (bread(vp, idp->in_lbn, fs->lfs_bsize, NOCRED,
    326 				  &ibp))
    327 				panic("lfs_balloc: bread bno %lld",
    328 				    (long long)idp->in_lbn);
    329 			/* XXX ondisk32 */
    330 			((int32_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
    331 			VOP_BWRITE(ibp);
    332 		}
    333 	} else if (bpp && !(bp->b_flags & (B_DONE|B_DELWRI))) {
    334 		/*
    335 		 * Not a brand new block, also not in the cache;
    336 		 * read it in from disk.
    337 		 */
    338 		if (iosize == fs->lfs_bsize)
    339 			/* Optimization: I/O is unnecessary. */
    340 			bp->b_blkno = daddr;
    341 		else {
    342 			/*
    343 			 * We need to read the block to preserve the
    344 			 * existing bytes.
    345 			 */
    346 			bp->b_blkno = daddr;
    347 			bp->b_flags |= B_READ;
    348 			VOP_STRATEGY(bp);
    349 			return (biowait(bp));
    350 		}
    351 	}
    352 
    353 	return (0);
    354 }
    355 
    356 /* VOP_BWRITE 1 time */
    357 int
    358 lfs_fragextend(struct vnode *vp, int osize, int nsize, daddr_t lbn, struct buf **bpp, struct ucred *cred)
    359 {
    360 	struct inode *ip;
    361 	struct lfs *fs;
    362 	long bb;
    363 	int error;
    364 	extern long locked_queue_bytes;
    365 	size_t obufsize;
    366 
    367 	ip = VTOI(vp);
    368 	fs = ip->i_lfs;
    369 	bb = (long)fragstofsb(fs, numfrags(fs, nsize - osize));
    370 	error = 0;
    371 
    372 	/*
    373 	 * Get the seglock so we don't enlarge blocks while a segment
    374 	 * is being written.  If we're called with bpp==NULL, though,
    375 	 * we are only pretending to change a buffer, so we don't have to
    376 	 * lock.
    377 	 */
    378     top:
    379 	if (bpp) {
    380 		lockmgr(&fs->lfs_fraglock, LK_SHARED, 0);
    381 	}
    382 
    383 	if (!ISSPACE(fs, bb, cred)) {
    384 		error = ENOSPC;
    385 		goto out;
    386 	}
    387 
    388 	/*
    389 	 * If we are not asked to actually return the block, all we need
    390 	 * to do is allocate space for it.  UBC will handle dirtying the
    391 	 * appropriate things and making sure it all goes to disk.
    392 	 * Don't bother to read in that case.
    393 	 */
    394 	if (bpp && (error = bread(vp, lbn, osize, NOCRED, bpp))) {
    395 		brelse(*bpp);
    396 		goto out;
    397 	}
    398 #ifdef QUOTA
    399 	if ((error = chkdq(ip, bb, cred, 0))) {
    400 		if (bpp)
    401 			brelse(*bpp);
    402 		goto out;
    403 	}
    404 #endif
    405 	/*
    406 	 * Adjust accounting for lfs_avail.  If there's not enough room,
    407 	 * we will have to wait for the cleaner, which we can't do while
    408 	 * holding a block busy or while holding the seglock.  In that case,
    409 	 * release both and start over after waiting.
    410 	 */
    411 
    412 	if (bpp && ((*bpp)->b_flags & B_DELWRI)) {
    413 		if (!lfs_fits(fs, bb)) {
    414 			if (bpp)
    415 				brelse(*bpp);
    416 #ifdef QUOTA
    417 			chkdq(ip, -bb, cred, 0);
    418 #endif
    419 			lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    420 			lfs_availwait(fs, bb);
    421 			goto top;
    422 		}
    423 		fs->lfs_avail -= bb;
    424 	}
    425 
    426 	fs->lfs_bfree -= bb;
    427 	ip->i_lfs_effnblks += bb;
    428 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    429 
    430 	if (bpp) {
    431 		LFS_DEBUG_COUNTLOCKED("frag1");
    432 
    433 		obufsize = (*bpp)->b_bufsize;
    434 		allocbuf(*bpp, nsize);
    435 
    436 		/* Adjust locked-list accounting */
    437 		if (((*bpp)->b_flags & (B_LOCKED | B_CALL)) == B_LOCKED)
    438 			locked_queue_bytes += (*bpp)->b_bufsize - obufsize;
    439 
    440 		LFS_DEBUG_COUNTLOCKED("frag2");
    441 
    442 		bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
    443 	}
    444 
    445     out:
    446 	if (bpp) {
    447 		lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
    448 	}
    449 	return (error);
    450 }
    451