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