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