Home | History | Annotate | Line # | Download | only in lfs
lfs_balloc.c revision 1.11
      1 /*	$NetBSD: lfs_balloc.c,v 1.11 1999/03/10 00:20:00 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 #include "opt_uvm.h"
     76 #endif
     77 
     78 #include <sys/param.h>
     79 #include <sys/systm.h>
     80 #include <sys/buf.h>
     81 #include <sys/proc.h>
     82 #include <sys/vnode.h>
     83 #include <sys/mount.h>
     84 #include <sys/resourcevar.h>
     85 #include <sys/trace.h>
     86 
     87 #include <miscfs/specfs/specdev.h>
     88 
     89 #include <ufs/ufs/quota.h>
     90 #include <ufs/ufs/inode.h>
     91 #include <ufs/ufs/ufsmount.h>
     92 #include <ufs/ufs/ufs_extern.h>
     93 
     94 #include <ufs/lfs/lfs.h>
     95 #include <ufs/lfs/lfs_extern.h>
     96 
     97 #include <vm/vm.h>
     98 #include <vm/vm_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 #if defined(UVM)
    155 			uvm_vnp_setsize(vp, ip->i_ffs_size);
    156 #else
    157 			vnode_pager_setsize(vp, ip->i_ffs_size);
    158 #endif
    159 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    160 			VOP_BWRITE(bp);
    161 		}
    162 	}
    163 
    164 	bb = VFSTOUFS(vp->v_mount)->um_seqinc;
    165 	if (daddr == UNASSIGNED)
    166 		/* May need to allocate indirect blocks */
    167 		for (i = 1; i < num; ++i)
    168 			if (!indirs[i].in_exists) {
    169 				ibp = getblk(vp, indirs[i].in_lbn, fs->lfs_bsize,
    170 					     0, 0);
    171 				if ((ibp->b_flags & (B_DONE | B_DELWRI)))
    172 					panic ("Indirect block should not exist");
    173 
    174 				if (!ISSPACE(fs, bb, curproc->p_ucred)){
    175 					ibp->b_flags |= B_INVAL;
    176 					brelse(ibp);
    177 					return(ENOSPC);
    178 				} else {
    179 					ip->i_ffs_blocks += bb;
    180 					ip->i_lfs->lfs_bfree -= bb;
    181 					clrbuf(ibp);
    182 					if ((error = VOP_BWRITE(ibp)))
    183 						return(error);
    184 				}
    185 			}
    186 
    187 	/*
    188 	 * If the block we are writing is a direct block, it's the last
    189 	 * block in the file, and offset + iosize is less than a full
    190 	 * block, we can write one or more fragments.  There are two cases:
    191 	 * the block is brand new and we should allocate it the correct
    192 	 * size or it already exists and contains some fragments and
    193 	 * may need to extend it.
    194 	 */
    195 	if (lbn < NDADDR && lblkno(fs, ip->i_ffs_size) <= lbn) {
    196 		osize = blksize(fs, ip, lbn);
    197 		nsize = fragroundup(fs, offset + iosize);
    198 		frags = numfrags(fs, nsize);
    199 		bb = fragstodb(fs, frags);
    200 		if (lblktosize(fs, lbn) >= ip->i_ffs_size)
    201 			/* Brand new block or fragment */
    202 			*bpp = bp = getblk(vp, lbn, nsize, 0, 0);
    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 			*bpp = bp;
    216 		}
    217 	} else {
    218 		/*
    219 		 * Get the existing block from the cache either because the
    220 		 * block 1) is not a direct block or 2) is not the last
    221 		 * block in the file.
    222 		 */
    223 		frags = dbtofrags(fs, bb);
    224 		*bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
    225 	}
    226 
    227 	/*
    228 	 * The block we are writing may be a brand new block
    229 	 * in which case we need to do accounting (i.e. check
    230 	 * for free space and update the inode number of blocks.
    231 	 */
    232 	if (!(bp->b_flags & (B_DONE | B_DELWRI))) {
    233 		if (daddr == UNASSIGNED)
    234 			if (!ISSPACE(fs, bb, curproc->p_ucred)) {
    235 				bp->b_flags |= B_INVAL;
    236 				brelse(bp);
    237 				return(ENOSPC);
    238 			} else {
    239 				ip->i_ffs_blocks += bb;
    240 				ip->i_lfs->lfs_bfree -= bb;
    241 				if (iosize != fs->lfs_bsize)
    242 					clrbuf(bp);
    243 			}
    244 		else if (iosize == fs->lfs_bsize)
    245 			/* Optimization: I/O is unnecessary. */
    246 			bp->b_blkno = daddr;
    247 		else  {
    248 			/*
    249 			 * We need to read the block to preserve the
    250 			 * existing bytes.
    251 			 */
    252 			bp->b_blkno = daddr;
    253 			bp->b_flags |= B_READ;
    254 			VOP_STRATEGY(bp);
    255 			return(biowait(bp));
    256 		}
    257 	}
    258 
    259 	return (0);
    260 }
    261 
    262 int
    263 lfs_fragextend(vp, osize, nsize, lbn, bpp)
    264 	struct vnode *vp;
    265 	int osize;
    266 	int nsize;
    267 	ufs_daddr_t lbn;
    268 	struct buf **bpp;
    269 {
    270 	struct inode *ip;
    271 	struct lfs *fs;
    272 	long bb;
    273 	int error;
    274 	extern long locked_queue_bytes;
    275 
    276 	ip = VTOI(vp);
    277 	fs = ip->i_lfs;
    278 
    279 	bb = (long)fragstodb(fs, numfrags(fs, nsize - osize));
    280  top:
    281 	if (!ISSPACE(fs, bb, curproc->p_ucred)) {
    282 		return(ENOSPC);
    283 	}
    284 	if ((error = bread(vp, lbn, osize, NOCRED, bpp))) {
    285 		brelse(*bpp);
    286 		return(error);
    287 	}
    288 #ifdef QUOTA
    289 	if ((error = chkdq(ip, bb, curproc->p_ucred, 0))) {
    290 		brelse(*bpp);
    291 		return (error);
    292 	}
    293 #endif
    294 	/*
    295 	 * XXX - KS - Don't change size while we're gathered, as we could
    296 	 * then overlap another buffer in lfs_writeseg.
    297 	 */
    298 	if((*bpp)->b_flags & B_GATHERED) {
    299 		(*bpp)->b_flags |= B_NEEDCOMMIT; /* XXX KS - what flag to use? */
    300 		brelse(*bpp);
    301 		tsleep(*bpp, (PRIBIO+1), "lfs_fragextend", 0);
    302 		goto top;
    303 	}
    304 	ip->i_ffs_blocks += bb;
    305 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    306 	fs->lfs_bfree -= fragstodb(fs, numfrags(fs, (nsize - osize)));
    307 	if((*bpp)->b_flags & B_LOCKED)
    308 		locked_queue_bytes += (nsize - osize);
    309 	allocbuf(*bpp, nsize);
    310 	bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
    311 
    312 	return(0);
    313 }
    314