Home | History | Annotate | Line # | Download | only in ffs
ffs_balloc.c revision 1.14.4.3
      1 /*	$NetBSD: ffs_balloc.c,v 1.14.4.3 1999/07/06 14:52:08 chs Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)ffs_balloc.c	8.8 (Berkeley) 6/16/95
     36  */
     37 
     38 #if defined(_KERNEL) && !defined(_LKM)
     39 #include "opt_quota.h"
     40 #endif
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/buf.h>
     45 #include <sys/proc.h>
     46 #include <sys/file.h>
     47 #include <sys/vnode.h>
     48 #include <sys/mount.h>
     49 
     50 #include <vm/vm.h>
     51 #include <uvm/uvm_extern.h>
     52 #include <uvm/uvm.h>
     53 
     54 #include <ufs/ufs/quota.h>
     55 #include <ufs/ufs/ufsmount.h>
     56 #include <ufs/ufs/inode.h>
     57 #include <ufs/ufs/ufs_extern.h>
     58 #include <ufs/ufs/ufs_bswap.h>
     59 
     60 #include <ufs/ffs/fs.h>
     61 #include <ufs/ffs/ffs_extern.h>
     62 
     63 /*
     64  * Balloc defines the structure of file system storage
     65  * by allocating the physical blocks on a device given
     66  * the inode and the logical block number in a file.
     67  */
     68 int
     69 ffs_balloc(v)
     70 	void *v;
     71 {
     72 	struct vop_balloc_args /* {
     73 		struct vnode *a_vp;
     74 		off_t a_offset;
     75 		int a_size;
     76 		struct ucred *a_cred;
     77 		int a_flags;
     78 		struct buf **a_bpp;
     79 	} */ *ap = v;
     80 
     81 	struct vnode *vp = ap->a_vp;
     82 	struct inode *ip = VTOI(vp);
     83 	struct fs *fs = ip->i_fs;
     84 	ufs_daddr_t lbn = lblkno(fs, ap->a_offset);
     85 	int size = ap->a_size;
     86 	struct ucred *cred = ap->a_cred;
     87 	int flags = ap->a_flags;
     88 	struct buf **bpp = ap->a_bpp;
     89 
     90 	ufs_daddr_t nb;
     91 	struct buf *bp, *nbp;
     92 	struct indir indirs[NIADDR + 2];
     93 	ufs_daddr_t newb, *bap, pref;
     94 	int deallocated, osize, nsize, num, i, error;
     95 	ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
     96 	UVMHIST_FUNC("ffs_balloc"); UVMHIST_CALLED(ubchist);
     97 
     98 	UVMHIST_LOG(ubchist, "vp %p off 0x%x size 0x%x",
     99 		    vp, (int)ap->a_offset, ap->a_size,0);
    100 
    101 	if (bpp != NULL) {
    102 		*bpp = NULL;
    103 	}
    104 
    105 	if (lbn < 0)
    106 		return (EFBIG);
    107 	fs = ip->i_fs;
    108 
    109 	/*
    110 	 * If the file currently ends with a fragment and
    111 	 * the block we're allocating now is after the current EOF,
    112 	 * this fragment has to be extended to be a full block.
    113 	 */
    114 	nb = lblkno(fs, ip->i_ffs_size);
    115 	if (nb < NDADDR && nb < lbn) {
    116 		osize = blksize(fs, ip, nb);
    117 		if (osize < fs->fs_bsize && osize > 0) {
    118 			error = ffs_realloccg(ip, nb,
    119 				ffs_blkpref(ip, nb, (int)nb, &ip->i_ffs_db[0]),
    120 				osize, (int)fs->fs_bsize, cred, bpp, &newb);
    121 			if (error)
    122 				return (error);
    123 			ip->i_ffs_size = lblktosize(fs, nb + 1);
    124 			uvm_vnp_setsize(vp, ip->i_ffs_size);
    125 			ip->i_ffs_db[nb] = ufs_rw32(newb,
    126 			    UFS_MPNEEDSWAP(vp->v_mount));
    127 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    128 
    129 			if (bpp) {
    130 				if (flags & B_SYNC)
    131 					bwrite(*bpp);
    132 				else
    133 					bawrite(*bpp);
    134 			}
    135 			else {
    136 				/*
    137 				 * XXX the data in the frag might be
    138 				 * moving to a new disk location.
    139 				 * we need to flush pages to the
    140 				 * new disk locations.
    141 				 * XXX we could do this in realloccg
    142 				 * except for the sync flag.
    143 				 */
    144 				(vp->v_uvm.u_obj.pgops->pgo_flush)
    145 					(&vp->v_uvm.u_obj, lblktosize(fs, nb),
    146 					 lblktosize(fs, nb + 1),
    147 					 flags & B_SYNC ? PGO_SYNCIO : 0);
    148 			}
    149 		}
    150 	}
    151 	/*
    152 	 * The first NDADDR blocks are direct blocks
    153 	 */
    154 	if (lbn < NDADDR) {
    155 
    156 		nb = ufs_rw32(ip->i_ffs_db[lbn], UFS_MPNEEDSWAP(vp->v_mount));
    157 		if (nb != 0 && ip->i_ffs_size >= lblktosize(fs, lbn + 1)) {
    158 
    159 			/*
    160 			 * the block is an already-allocated direct block
    161 			 * and the file already extends past this block,
    162 			 * thus this must be a whole block.
    163 			 * just read the block (if requested).
    164 			 */
    165 
    166 			if (bpp != NULL) {
    167 				error = bread(vp, lbn, fs->fs_bsize, NOCRED,
    168 					      &bp);
    169 				if (error) {
    170 					brelse(bp);
    171 					return (error);
    172 				}
    173 				*bpp = bp;
    174 			}
    175 			return (0);
    176 		}
    177 		if (nb != 0) {
    178 
    179 			/*
    180 			 * Consider need to reallocate a fragment.
    181 			 */
    182 
    183 			osize = fragroundup(fs, blkoff(fs, ip->i_ffs_size));
    184 			nsize = fragroundup(fs, size);
    185 			if (nsize <= osize) {
    186 
    187 				/*
    188 				 * the existing block is already
    189 				 * at least as big as we want.
    190 				 * just read the block (if requested).
    191 				 */
    192 
    193 				if (bpp != NULL) {
    194 					error = bread(vp, lbn, osize, NOCRED,
    195 						      &bp);
    196 					if (error) {
    197 						brelse(bp);
    198 						return (error);
    199 					}
    200 					*bpp = bp;
    201 				}
    202 				return 0;
    203 			} else {
    204 
    205 				/*
    206 				 * the existing block is smaller than we want,
    207 				 * grow it.
    208 				 */
    209 
    210 				error = ffs_realloccg(ip, lbn,
    211 				    ffs_blkpref(ip, lbn, (int)lbn,
    212 					&ip->i_ffs_db[0]), osize, nsize, cred,
    213 					bpp, &newb);
    214 				if (error)
    215 					return (error);
    216 				if (vp->v_type == VREG) {
    217 					uvm_vnp_zerorange(vp,
    218 							  lblktosize(fs, lbn) +
    219 							  osize, nsize - osize);
    220 				}
    221 			}
    222 		} else {
    223 
    224 			/*
    225 			 * the block was not previously allocated,
    226 			 * allocate a new block or fragment.
    227 			 */
    228 
    229 			if (ip->i_ffs_size < lblktosize(fs, lbn + 1))
    230 				nsize = fragroundup(fs, size);
    231 			else
    232 				nsize = fs->fs_bsize;
    233 			error = ffs_alloc(ip, lbn,
    234 			    ffs_blkpref(ip, lbn, (int)lbn, &ip->i_ffs_db[0]),
    235 				nsize, cred, &newb);
    236 			if (error)
    237 				return (error);
    238 			if (bpp != NULL) {
    239 				bp = getblk(vp, lbn, nsize, 0, 0);
    240 				bp->b_blkno = fsbtodb(fs, newb);
    241 				if (flags & B_CLRBUF)
    242 					clrbuf(bp);
    243 				*bpp = bp;
    244 			}
    245 			if (vp->v_type == VREG) {
    246 				uvm_vnp_zerorange(vp, lblktosize(fs, lbn),
    247 						  nsize);
    248 			}
    249 		}
    250 		ip->i_ffs_db[lbn] = ufs_rw32(newb, UFS_MPNEEDSWAP(vp->v_mount));
    251 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    252 		return (0);
    253 	}
    254 
    255 	/*
    256 	 * Determine the number of levels of indirection.
    257 	 */
    258 
    259 	pref = 0;
    260 	if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
    261 		return(error);
    262 #ifdef DIAGNOSTIC
    263 	if (num < 1)
    264 		panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
    265 #endif
    266 	/*
    267 	 * Fetch the first indirect block allocating if necessary.
    268 	 */
    269 	--num;
    270 	nb = ufs_rw32(ip->i_ffs_ib[indirs[0].in_off],
    271 	    UFS_MPNEEDSWAP(vp->v_mount));
    272 	allocib = NULL;
    273 	allocblk = allociblk;
    274 	if (nb == 0) {
    275 		pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    276 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
    277 			cred, &newb);
    278 		if (error)
    279 			return (error);
    280 		nb = newb;
    281 		*allocblk++ = nb;
    282 		bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
    283 		bp->b_blkno = fsbtodb(fs, nb);
    284 		clrbuf(bp);
    285 		/*
    286 		 * Write synchronously so that indirect blocks
    287 		 * never point at garbage.
    288 		 */
    289 		if ((error = bwrite(bp)) != 0)
    290 			goto fail;
    291 		allocib = &ip->i_ffs_ib[indirs[0].in_off];
    292 		*allocib = ufs_rw32(nb, UFS_MPNEEDSWAP(vp->v_mount));
    293 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    294 	}
    295 	/*
    296 	 * Fetch through the indirect blocks, allocating as necessary.
    297 	 */
    298 	for (i = 1;;) {
    299 		error = bread(vp,
    300 		    indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
    301 		if (error) {
    302 			brelse(bp);
    303 			goto fail;
    304 		}
    305 		bap = (ufs_daddr_t *)bp->b_data;
    306 		nb = ufs_rw32(bap[indirs[i].in_off],
    307 		    UFS_MPNEEDSWAP(vp->v_mount));
    308 		if (i == num)
    309 			break;
    310 		i += 1;
    311 		if (nb != 0) {
    312 			brelse(bp);
    313 			continue;
    314 		}
    315 		if (pref == 0)
    316 			pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    317 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    318 				  &newb);
    319 		if (error) {
    320 			brelse(bp);
    321 			goto fail;
    322 		}
    323 		nb = newb;
    324 		*allocblk++ = nb;
    325 		nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
    326 		nbp->b_blkno = fsbtodb(fs, nb);
    327 		clrbuf(nbp);
    328 		/*
    329 		 * Write synchronously so that indirect blocks
    330 		 * never point at garbage.
    331 		 */
    332 		if ((error = bwrite(nbp)) != 0) {
    333 			brelse(bp);
    334 			goto fail;
    335 		}
    336 		bap[indirs[i - 1].in_off] = ufs_rw32(nb,
    337 		    UFS_MPNEEDSWAP(vp->v_mount));
    338 		/*
    339 		 * If required, write synchronously, otherwise use
    340 		 * delayed write.
    341 		 */
    342 		if (flags & B_SYNC) {
    343 			bwrite(bp);
    344 		} else {
    345 			bdwrite(bp);
    346 		}
    347 	}
    348 	/*
    349 	 * Get the data block, allocating if necessary.
    350 	 */
    351 	if (nb == 0) {
    352 		pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
    353 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    354 				  &newb);
    355 		if (error) {
    356 			brelse(bp);
    357 			goto fail;
    358 		}
    359 		nb = newb;
    360 		*allocblk++ = nb;
    361 		bap[indirs[i].in_off] = ufs_rw32(nb,
    362 		    UFS_MPNEEDSWAP(vp->v_mount));
    363 		/*
    364 		 * If required, write synchronously, otherwise use
    365 		 * delayed write.
    366 		 */
    367 		if (flags & B_SYNC) {
    368 			bwrite(bp);
    369 		} else {
    370 			bdwrite(bp);
    371 		}
    372 		if (bpp != NULL) {
    373 			nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
    374 			nbp->b_blkno = fsbtodb(fs, nb);
    375 			if (flags & B_CLRBUF)
    376 				clrbuf(nbp);
    377 			*bpp = nbp;
    378 		}
    379 		if (vp->v_type == VREG) {
    380 			uvm_vnp_zerorange(vp, lblktosize(fs, lbn), ap->a_size);
    381 		}
    382 		return (0);
    383 	}
    384 
    385 	brelse(bp);
    386 
    387 	if (bpp != NULL) {
    388 		if (flags & B_CLRBUF) {
    389 			error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
    390 			if (error) {
    391 				brelse(nbp);
    392 				goto fail;
    393 			}
    394 		} else {
    395 			nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
    396 			nbp->b_blkno = fsbtodb(fs, nb);
    397 			clrbuf(nbp);
    398 		}
    399 		*bpp = nbp;
    400 	}
    401 	return (0);
    402 fail:
    403 	/*
    404 	 * If we have failed part way through block allocation, we
    405 	 * have to deallocate any indirect blocks that we have allocated.
    406 	 */
    407 	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
    408 		ffs_blkfree(ip, *blkp, fs->fs_bsize);
    409 		deallocated += fs->fs_bsize;
    410 	}
    411 	if (allocib != NULL)
    412 		*allocib = 0;
    413 	if (deallocated) {
    414 #ifdef QUOTA
    415 		/*
    416 		 * Restore user's disk quota because allocation failed.
    417 		 */
    418 		(void)chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
    419 #endif
    420 		ip->i_ffs_blocks -= btodb(deallocated);
    421 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    422 	}
    423 	return (error);
    424 }
    425