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