Home | History | Annotate | Line # | Download | only in lfs
lfs_balloc.c revision 1.14
      1 /*	$NetBSD: lfs_balloc.c,v 1.14 1999/11/15 18:49:14 fvdl 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 int
    103 lfs_balloc(v)
    104 	void *v;
    105 {
    106 	struct vop_balloc_args /* {
    107 		struct vnode *a_vp;
    108 		off_t a_startoffset;
    109 		int a_size;
    110 		struct ucred *a_cred;
    111 		int a_flags;
    112 		struct buf *a_bpp;
    113 	} */ *ap = v;
    114 	struct vnode *vp;
    115 	int offset;
    116 	u_long iosize;
    117 	daddr_t lbn;
    118 	struct buf *ibp, *bp;
    119 	struct inode *ip;
    120 	struct lfs *fs;
    121 	struct indir indirs[NIADDR+2];
    122 	ufs_daddr_t	daddr, lastblock;
    123 	int bb;		/* number of disk blocks in a block disk blocks */
    124 	int error, frags, i, nsize, osize, num;
    125 
    126 	vp = ap->a_vp;
    127 	ip = VTOI(vp);
    128 	fs = ip->i_lfs;
    129 	offset = blkoff(fs, ap->a_startoffset);
    130 	iosize = ap->a_size;
    131 	lbn = lblkno(fs, ap->a_startoffset);
    132 	(void)lfs_check(vp, lbn, 0);
    133 
    134 #ifdef DEBUG
    135 	if(!VOP_ISLOCKED(vp)) {
    136 		printf("lfs_balloc: warning: ino %d not locked\n",ip->i_number);
    137 	}
    138 #endif
    139 
    140 	/*
    141 	 * Three cases: it's a block beyond the end of file, it's a block in
    142 	 * the file that may or may not have been assigned a disk address or
    143 	 * we're writing an entire block.  Note, if the daddr is unassigned,
    144 	 * the block might still have existed in the cache (if it was read
    145 	 * or written earlier).	 If it did, make sure we don't count it as a
    146 	 * new block or zero out its contents.	If it did not, make sure
    147 	 * we allocate any necessary indirect blocks.
    148 	 * If we are writing a block beyond the end of the file, we need to
    149 	 * check if the old last block was a fragment.	If it was, we need
    150 	 * to rewrite it.
    151 	 */
    152 
    153 	*ap->a_bpp = NULL;
    154 	error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL );
    155 	if (error)
    156 		return (error);
    157 
    158 	/* Check for block beyond end of file and fragment extension needed. */
    159 	lastblock = lblkno(fs, ip->i_ffs_size);
    160 	if (lastblock < NDADDR && lastblock < lbn) {
    161 		osize = blksize(fs, ip, lastblock);
    162 		if (osize < fs->lfs_bsize && osize > 0) {
    163 			if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
    164 						    lastblock, &bp)))
    165 				return(error);
    166 			ip->i_ffs_size = (lastblock + 1) * fs->lfs_bsize;
    167 			uvm_vnp_setsize(vp, ip->i_ffs_size);
    168 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    169 			VOP_BWRITE(bp);
    170 		}
    171 	}
    172 
    173 	bb = VFSTOUFS(vp->v_mount)->um_seqinc;
    174 	if (daddr == UNASSIGNED)
    175 		/* May need to allocate indirect blocks */
    176 		for (i = 1; i < num; ++i)
    177 			if (!indirs[i].in_exists) {
    178 				ibp = getblk(vp, indirs[i].in_lbn, fs->lfs_bsize,
    179 					     0, 0);
    180 				if ((ibp->b_flags & (B_DONE | B_DELWRI)))
    181 					panic ("Indirect block should not exist");
    182 
    183 				if (!ISSPACE(fs, bb, curproc->p_ucred)){
    184 					ibp->b_flags |= B_INVAL;
    185 					brelse(ibp);
    186 					return(ENOSPC);
    187 				} else {
    188 					ip->i_ffs_blocks += bb;
    189 					ip->i_lfs->lfs_bfree -= bb;
    190 					clrbuf(ibp);
    191 					if ((error = VOP_BWRITE(ibp)))
    192 						return(error);
    193 				}
    194 			}
    195 
    196 	/*
    197 	 * If the block we are writing is a direct block, it's the last
    198 	 * block in the file, and offset + iosize is less than a full
    199 	 * block, we can write one or more fragments.  There are two cases:
    200 	 * the block is brand new and we should allocate it the correct
    201 	 * size or it already exists and contains some fragments and
    202 	 * may need to extend it.
    203 	 */
    204 	if (lbn < NDADDR && lblkno(fs, ip->i_ffs_size) <= lbn) {
    205 		osize = blksize(fs, ip, lbn);
    206 		nsize = fragroundup(fs, offset + iosize);
    207 		frags = numfrags(fs, nsize);
    208 		bb = fragstodb(fs, frags);
    209 		if (lblktosize(fs, lbn) >= ip->i_ffs_size)
    210 			/* Brand new block or fragment */
    211 			*ap->a_bpp = bp = getblk(vp, lbn, nsize, 0, 0);
    212 		else {
    213 			if (nsize <= osize) {
    214 				/* No need to extend */
    215 				/* XXX KS - Are we wasting space? */
    216 				if ((error = bread(vp, lbn, osize, NOCRED, &bp)))
    217 					return error;
    218 			} else {
    219 				/* Extend existing block */
    220 				if ((error =
    221 				     lfs_fragextend(vp, osize, nsize, lbn, &bp)))
    222 					return(error);
    223 			}
    224 			*ap->a_bpp = bp;
    225 		}
    226 	} else {
    227 		/*
    228 		 * Get the existing block from the cache either because the
    229 		 * block 1) is not a direct block or 2) is not the last
    230 		 * block in the file.
    231 		 */
    232 		frags = dbtofrags(fs, bb);
    233 		*ap->a_bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
    234 	}
    235 
    236 	/*
    237 	 * The block we are writing may be a brand new block
    238 	 * in which case we need to do accounting (i.e. check
    239 	 * for free space and update the inode number of blocks.
    240 	 */
    241 	if (!(bp->b_flags & (B_DONE | B_DELWRI))) {
    242 		if (daddr == UNASSIGNED)
    243 			if (!ISSPACE(fs, bb, curproc->p_ucred)) {
    244 				bp->b_flags |= B_INVAL;
    245 				brelse(bp);
    246 				return(ENOSPC);
    247 			} else {
    248 				ip->i_ffs_blocks += bb;
    249 				ip->i_lfs->lfs_bfree -= bb;
    250 				if (iosize != fs->lfs_bsize)
    251 					clrbuf(bp);
    252 			}
    253 		else if (iosize == fs->lfs_bsize)
    254 			/* Optimization: I/O is unnecessary. */
    255 			bp->b_blkno = daddr;
    256 		else  {
    257 			/*
    258 			 * We need to read the block to preserve the
    259 			 * existing bytes.
    260 			 */
    261 			bp->b_blkno = daddr;
    262 			bp->b_flags |= B_READ;
    263 			VOP_STRATEGY(bp);
    264 			return(biowait(bp));
    265 		}
    266 	}
    267 
    268 	return (0);
    269 }
    270 
    271 int
    272 lfs_fragextend(vp, osize, nsize, lbn, bpp)
    273 	struct vnode *vp;
    274 	int osize;
    275 	int nsize;
    276 	ufs_daddr_t lbn;
    277 	struct buf **bpp;
    278 {
    279 	struct inode *ip;
    280 	struct lfs *fs;
    281 	long bb;
    282 	int error;
    283 	extern long locked_queue_bytes;
    284 	struct buf *ibp;
    285 	SEGUSE *sup;
    286 
    287 	ip = VTOI(vp);
    288 	fs = ip->i_lfs;
    289 
    290 	bb = (long)fragstodb(fs, numfrags(fs, nsize - osize));
    291  top:
    292 	if (!ISSPACE(fs, bb, curproc->p_ucred)) {
    293 		return(ENOSPC);
    294 	}
    295 	if ((error = bread(vp, lbn, osize, NOCRED, bpp))) {
    296 		brelse(*bpp);
    297 		return(error);
    298 	}
    299 
    300 	/*
    301  	 * Fix the allocation for this fragment so that it looks like the
    302          * source segment contained a block of the new size.  This overcounts;
    303 	 * but the overcount only lasts until the block in question
    304 	 * is written, so the on-disk live bytes count is always correct.
    305 	 */
    306 	LFS_SEGENTRY(sup, fs, datosn(fs,(*bpp)->b_blkno), ibp);
    307 	sup->su_nbytes += (nsize-osize);
    308 	VOP_BWRITE(ibp);
    309 
    310 #ifdef QUOTA
    311 	if ((error = chkdq(ip, bb, curproc->p_ucred, 0))) {
    312 		brelse(*bpp);
    313 		return (error);
    314 	}
    315 #endif
    316 	/*
    317 	 * XXX - KS - Don't change size while we're gathered, as we could
    318 	 * then overlap another buffer in lfs_writeseg.
    319 	 */
    320 	if((*bpp)->b_flags & B_GATHERED) {
    321 		(*bpp)->b_flags |= B_NEEDCOMMIT; /* XXX KS - what flag to use? */
    322 		brelse(*bpp);
    323 		tsleep(*bpp, (PRIBIO+1), "lfs_fragextend", 0);
    324 		goto top;
    325 	}
    326 	ip->i_ffs_blocks += bb;
    327 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    328 	fs->lfs_bfree -= fragstodb(fs, numfrags(fs, (nsize - osize)));
    329 	if((*bpp)->b_flags & B_LOCKED)
    330 		locked_queue_bytes += (nsize - osize);
    331 	allocbuf(*bpp, nsize);
    332 	bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
    333 
    334 	return(0);
    335 }
    336