Home | History | Annotate | Line # | Download | only in lfs
lfs_balloc.c revision 1.19
      1 /*	$NetBSD: lfs_balloc.c,v 1.19 2000/06/27 20:57:12 perseant Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 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 #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/buf.h>
     80 #include <sys/proc.h>
     81 #include <sys/vnode.h>
     82 #include <sys/mount.h>
     83 #include <sys/resourcevar.h>
     84 #include <sys/trace.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 <vm/vm.h>
     97 
     98 #include <uvm/uvm_extern.h>
     99 
    100 int lfs_fragextend __P((struct vnode *, int, int, ufs_daddr_t, struct buf **));
    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 int
    116 lfs_balloc(v)
    117 	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;
    132 	struct inode *ip;
    133 	struct lfs *fs;
    134 	struct indir indirs[NIADDR+2], *idp;
    135 	ufs_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 	lbn = lblkno(fs, ap->a_startoffset);
    145 	(void)lfs_check(vp, lbn, 0);
    146 
    147 #ifdef DEBUG
    148 	if(!VOP_ISLOCKED(vp)) {
    149 		printf("lfs_balloc: warning: ino %d not locked\n",ip->i_number);
    150 	}
    151 #endif
    152 
    153 	/*
    154 	 * Three cases: it's a block beyond the end of file, it's a block in
    155 	 * the file that may or may not have been assigned a disk address or
    156 	 * we're writing an entire block.
    157 	 *
    158 	 * Note, if the daddr is UNWRITTEN, the block already exists in
    159 	 * the cache (it was read or written earlier).  If so, make sure
    160 	 * we don't count it as a new block or zero out its contents. If
    161 	 * it did not, make sure we allocate any necessary indirect
    162 	 * blocks.
    163 	 *
    164 	 * If we are writing a block beyond the end of the file, we need to
    165 	 * check if the old last block was a fragment.	If it was, we need
    166 	 * to rewrite it.
    167 	 */
    168 
    169 	*ap->a_bpp = NULL;
    170 
    171 	/* Check for block beyond end of file and fragment extension needed. */
    172 	lastblock = lblkno(fs, ip->i_ffs_size);
    173 	if (lastblock < NDADDR && lastblock < lbn) {
    174 		osize = blksize(fs, ip, lastblock);
    175 		if (osize < fs->lfs_bsize && osize > 0) {
    176 			if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
    177 						    lastblock, &bp)))
    178 				return(error);
    179 			ip->i_ffs_size = (lastblock + 1) * fs->lfs_bsize;
    180 			uvm_vnp_setsize(vp, ip->i_ffs_size);
    181 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    182 			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 = fragstodb(fs, frags);
    201 			*ap->a_bpp = bp = getblk(vp, lbn, nsize, 0, 0);
    202 			ip->i_ffs_blocks += bb;
    203 			ip->i_lfs->lfs_bfree -= bb;
    204 			ip->i_ffs_db[lbn] = bp->b_blkno = UNWRITTEN;
    205 		} else {
    206 			if (nsize <= osize) {
    207 				/* No need to extend */
    208 				if ((error = bread(vp, lbn, osize, NOCRED, &bp)))
    209 					return error;
    210 			} else {
    211 				/* Extend existing block */
    212 				if ((error =
    213 				     lfs_fragextend(vp, osize, nsize, lbn, &bp)))
    214 					return error;
    215 			}
    216 			*ap->a_bpp = bp;
    217 		}
    218 		return 0;
    219 	}
    220 
    221 	error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL );
    222 	if (error)
    223 		return (error);
    224 	/*
    225 	 * Do byte accounting all at once, so we can gracefully fail *before*
    226 	 * we start assigning blocks.
    227 	 */
    228 	bb = VFSTOUFS(vp->v_mount)->um_seqinc;
    229 	bcount = 0;
    230 	if (daddr == UNASSIGNED) {
    231 		bcount = bb;
    232 	}
    233 	for (i = 1; i < num; ++i) {
    234 		if (!indirs[i].in_exists) {
    235 			bcount += bb;
    236 		}
    237 	}
    238 	if (ISSPACE(fs, bcount, ap->a_cred)) {
    239 		ip->i_lfs->lfs_bfree -= bcount;
    240 		ip->i_ffs_blocks += bcount;
    241 	} else {
    242 		return ENOSPC;
    243 	}
    244 
    245 	if (daddr == UNASSIGNED) {
    246 		if (num > 0 && ip->i_ffs_ib[indirs[0].in_off] == 0) {
    247 			ip->i_ffs_ib[indirs[0].in_off] = UNWRITTEN;
    248 		}
    249 
    250 		/*
    251 		 * Create new indirect blocks if necessary
    252 		 */
    253 		if (num > 1)
    254 			idaddr = ip->i_ffs_ib[indirs[0].in_off];
    255 		for (i = 1; i < num; ++i) {
    256 			ibp = getblk(vp, indirs[i].in_lbn, fs->lfs_bsize, 0,0);
    257 			if (!indirs[i].in_exists) {
    258 				clrbuf(ibp);
    259 				ibp->b_blkno = UNWRITTEN;
    260 			} else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) {
    261 				ibp->b_blkno = idaddr;
    262 				ibp->b_flags |= B_READ;
    263 				VOP_STRATEGY(ibp);
    264 				biowait(ibp);
    265 			}
    266 			/*
    267 			 * This block exists, but the next one may not.
    268 			 * If that is the case mark it UNWRITTEN to keep
    269 			 * the accounting straight.
    270 			 */
    271 			if (((daddr_t *)ibp->b_data)[indirs[i].in_off]==0)
    272 				((daddr_t *)ibp->b_data)[indirs[i].in_off] =
    273 					UNWRITTEN;
    274 			idaddr = ((daddr_t *)ibp->b_data)[indirs[i].in_off];
    275 			if ((error = VOP_BWRITE(ibp))) {
    276 				return error;
    277 			}
    278 		}
    279 	}
    280 
    281 
    282 	/*
    283 	 * Get the existing block from the cache.
    284 	 */
    285 	frags = dbtofrags(fs, bb);
    286 	*ap->a_bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
    287 
    288 	/*
    289 	 * The block we are writing may be a brand new block
    290 	 * in which case we need to do accounting.
    291 	 *
    292 	 * We can tell a truly new block because ufs_bmaparray will say
    293 	 * it is UNASSIGNED.  Once we allocate it we will assign it the
    294 	 * disk address UNWRITTEN.
    295 	 */
    296 	if (daddr == UNASSIGNED) {
    297 		if (iosize != fs->lfs_bsize)
    298 			clrbuf(bp);
    299 
    300 		/* Note the new address */
    301 		bp->b_blkno = UNWRITTEN;
    302 
    303 		switch (num) {
    304 		    case 0:
    305 			ip->i_ffs_db[lbn] = UNWRITTEN;
    306 			break;
    307 		    case 1:
    308 			ip->i_ffs_ib[indirs[0].in_off] = UNWRITTEN;
    309 			break;
    310 		    default:
    311 			idp = &indirs[num - 1];
    312 			if (bread(vp, idp->in_lbn, fs->lfs_bsize, NOCRED,
    313 				  &ibp))
    314 				panic("lfs_balloc: bread bno %d", idp->in_lbn);
    315 			((ufs_daddr_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
    316 			VOP_BWRITE(ibp);
    317 		}
    318 	} else if (!(bp->b_flags & (B_DONE|B_DELWRI))) {
    319 		/*
    320 		 * Not a brand new block, also not in the cache;
    321 		 * read it in from disk.
    322 		 */
    323 		if (iosize == fs->lfs_bsize)
    324 			/* Optimization: I/O is unnecessary. */
    325 			bp->b_blkno = daddr;
    326 		else {
    327 			/*
    328 			 * We need to read the block to preserve the
    329 			 * existing bytes.
    330 			 */
    331 			bp->b_blkno = daddr;
    332 			bp->b_flags |= B_READ;
    333 			VOP_STRATEGY(bp);
    334 			return(biowait(bp));
    335 		}
    336 	}
    337 
    338 	return (0);
    339 }
    340 
    341 int
    342 lfs_fragextend(vp, osize, nsize, lbn, bpp)
    343 	struct vnode *vp;
    344 	int osize;
    345 	int nsize;
    346 	ufs_daddr_t lbn;
    347 	struct buf **bpp;
    348 {
    349 	struct inode *ip;
    350 	struct lfs *fs;
    351 	long bb;
    352 	int error;
    353 	extern long locked_queue_bytes;
    354 	struct buf *ibp;
    355 	SEGUSE *sup;
    356 
    357 	ip = VTOI(vp);
    358 	fs = ip->i_lfs;
    359 	bb = (long)fragstodb(fs, numfrags(fs, nsize - osize));
    360 	error = 0;
    361 
    362 	/*
    363 	 * Get the seglock so we don't enlarge blocks or change the segment
    364 	 * accounting information while a segment is being written.
    365 	 */
    366 	lfs_seglock(fs, SEGM_PROT);
    367 
    368 	if (!ISSPACE(fs, bb, curproc->p_ucred)) {
    369 		error = ENOSPC;
    370 		goto out;
    371 	}
    372 	if ((error = bread(vp, lbn, osize, NOCRED, bpp))) {
    373 		brelse(*bpp);
    374 		goto out;
    375 	}
    376 #ifdef QUOTA
    377 	if ((error = chkdq(ip, bb, curproc->p_ucred, 0))) {
    378 		brelse(*bpp);
    379 		goto out;
    380 	}
    381 #endif
    382 	/*
    383  	 * Fix the allocation for this fragment so that it looks like the
    384          * source segment contained a block of the new size.  This overcounts;
    385 	 * but the overcount only lasts until the block in question
    386 	 * is written, so the on-disk live bytes count is always correct.
    387 	 */
    388 	if ((*bpp)->b_blkno > 0) {
    389 		LFS_SEGENTRY(sup, fs, datosn(fs,(*bpp)->b_blkno), ibp);
    390 		sup->su_nbytes += (nsize-osize);
    391 		VOP_BWRITE(ibp);
    392 	}
    393 
    394 	fs->lfs_bfree -= bb;
    395 	ip->i_ffs_blocks += bb;
    396 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    397 	if((*bpp)->b_flags & B_LOCKED)
    398 		locked_queue_bytes += (nsize - osize);
    399 	allocbuf(*bpp, nsize);
    400 	bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
    401 
    402     out:
    403 	lfs_segunlock(fs);
    404 	return (error);
    405 }
    406