Home | History | Annotate | Line # | Download | only in ffs
ffs_balloc.c revision 1.14.4.6
      1 /*	$NetBSD: ffs_balloc.c,v 1.14.4.6 1999/08/06 12:50:04 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 
     69 int
     70 ffs_balloc(v)
     71 	void *v;
     72 {
     73 	struct vop_balloc_args /* {
     74 		struct vnode *a_vp;
     75 		off_t a_offset;
     76 		off_t a_length;
     77 		struct ucred *a_cred;
     78 		int a_flags;
     79 	} */ *ap = v;
     80 
     81 	off_t off, len;
     82 	struct vnode *vp = ap->a_vp;
     83 	struct inode *ip = VTOI(vp);
     84 	struct fs *fs = ip->i_fs;
     85 	int error, delta, bshift, bsize;
     86 
     87 	bshift = fs->fs_bshift;
     88 	bsize = 1 << bshift;
     89 
     90 	off = ap->a_offset;
     91 	len = ap->a_length;
     92 
     93 	delta = off & (bsize - 1);
     94 	off -= delta;
     95 	len += delta;
     96 
     97 	while (len > 0) {
     98 		bsize = min(bsize, len);
     99 
    100 		if ((error = ffs_balloc1(vp, lblkno(fs, off), bsize, ap->a_cred,
    101 					 ap->a_flags, NULL))) {
    102 			return error;
    103 		}
    104 
    105 		/*
    106 		 * increase file size now, VOP_BALLOC() requires that
    107 		 * EOF be up-to-date before each call.
    108 		 */
    109 
    110 		if (ip->i_ffs_size < off + bsize) {
    111 			ip->i_ffs_size = off + bsize;
    112 			if (vp->v_uvm.u_size < ip->i_ffs_size) {
    113 				uvm_vnp_setsize(vp, ip->i_ffs_size);
    114 			}
    115 		}
    116 
    117 		off += bsize;
    118 		len -= bsize;
    119 	}
    120 	return 0;
    121 }
    122 
    123 int
    124 ffs_balloc1(vp, lbn, size, cred, flags, bpp)
    125 	struct vnode *vp;
    126 	ufs_daddr_t lbn;
    127 	int size;
    128 	struct ucred *cred;
    129 	int flags;
    130 	struct buf **bpp;
    131 {
    132 	struct inode *ip = VTOI(vp);
    133 	struct fs *fs = ip->i_fs;
    134 	ufs_daddr_t nb;
    135 	struct buf *bp, *nbp;
    136 	struct indir indirs[NIADDR + 2];
    137 	ufs_daddr_t newb, *bap, pref;
    138 	int deallocated, osize, nsize, num, i, error;
    139 	ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
    140 	UVMHIST_FUNC("ffs_balloc"); UVMHIST_CALLED(ubchist);
    141 
    142 	UVMHIST_LOG(ubchist, "vp %p lbn 0x%x size 0x%x", vp, lbn, size,0);
    143 	if (bpp != NULL) {
    144 		*bpp = NULL;
    145 	}
    146 	if (lbn < 0)
    147 		return (EFBIG);
    148 	fs = ip->i_fs;
    149 
    150 	/*
    151 	 * If the file currently ends with a fragment and
    152 	 * the block we're allocating now is after the current EOF,
    153 	 * this fragment has to be extended to be a full block.
    154 	 */
    155 
    156 	nb = lblkno(fs, ip->i_ffs_size);
    157 	if (nb < NDADDR && nb < lbn) {
    158 		osize = blksize(fs, ip, nb);
    159 		if (osize < fs->fs_bsize && osize > 0) {
    160 			error = ffs_realloccg(ip, nb,
    161 				ffs_blkpref(ip, nb, (int)nb, &ip->i_ffs_db[0]),
    162 				osize, (int)fs->fs_bsize, cred, bpp, &newb);
    163 			if (error)
    164 				return (error);
    165 			ip->i_ffs_size = lblktosize(fs, nb + 1);
    166 			uvm_vnp_setsize(vp, ip->i_ffs_size);
    167 			ip->i_ffs_db[nb] = ufs_rw32(newb,
    168 			    UFS_MPNEEDSWAP(vp->v_mount));
    169 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    170 
    171 			if (bpp) {
    172 				if (flags & B_SYNC)
    173 					bwrite(*bpp);
    174 				else
    175 					bawrite(*bpp);
    176 			}
    177 			else {
    178 				/*
    179 				 * XXX the data in the frag might be
    180 				 * moving to a new disk location.
    181 				 * we need to flush pages to the
    182 				 * new disk locations.
    183 				 * XXX we could do this in realloccg
    184 				 * except for the sync flag.
    185 				 */
    186 				(vp->v_uvm.u_obj.pgops->pgo_flush)
    187 					(&vp->v_uvm.u_obj, lblktosize(fs, nb),
    188 					 lblktosize(fs, nb + 1),
    189 					 flags & B_SYNC ? PGO_SYNCIO : 0);
    190 			}
    191 		}
    192 	}
    193 	/*
    194 	 * The first NDADDR blocks are direct blocks
    195 	 */
    196 	if (lbn < NDADDR) {
    197 
    198 		nb = ufs_rw32(ip->i_ffs_db[lbn], UFS_MPNEEDSWAP(vp->v_mount));
    199 		if (nb != 0 && ip->i_ffs_size >= lblktosize(fs, lbn + 1)) {
    200 
    201 			/*
    202 			 * the block is an already-allocated direct block
    203 			 * and the file already extends past this block,
    204 			 * thus this must be a whole block.
    205 			 * just read the block (if requested).
    206 			 */
    207 
    208 			if (bpp != NULL) {
    209 				error = bread(vp, lbn, fs->fs_bsize, NOCRED,
    210 					      &bp);
    211 				if (error) {
    212 					brelse(bp);
    213 					return (error);
    214 				}
    215 				*bpp = bp;
    216 			}
    217 			return (0);
    218 		}
    219 		if (nb != 0) {
    220 
    221 			/*
    222 			 * Consider need to reallocate a fragment.
    223 			 */
    224 
    225 			osize = fragroundup(fs, blkoff(fs, ip->i_ffs_size));
    226 			nsize = fragroundup(fs, size);
    227 			if (nsize <= osize) {
    228 
    229 				/*
    230 				 * the existing block is already
    231 				 * at least as big as we want.
    232 				 * just read the block (if requested).
    233 				 */
    234 
    235 				if (bpp != NULL) {
    236 					error = bread(vp, lbn, osize, NOCRED,
    237 						      &bp);
    238 					if (error) {
    239 						brelse(bp);
    240 						return (error);
    241 					}
    242 					*bpp = bp;
    243 				}
    244 				return 0;
    245 			} else {
    246 
    247 				/*
    248 				 * the existing block is smaller than we want,
    249 				 * grow it.
    250 				 */
    251 
    252 				error = ffs_realloccg(ip, lbn,
    253 				    ffs_blkpref(ip, lbn, (int)lbn,
    254 					&ip->i_ffs_db[0]), osize, nsize, cred,
    255 					bpp, &newb);
    256 				if (error)
    257 					return (error);
    258 			}
    259 		} else {
    260 
    261 			/*
    262 			 * the block was not previously allocated,
    263 			 * allocate a new block or fragment.
    264 			 */
    265 
    266 			if (ip->i_ffs_size < lblktosize(fs, lbn + 1))
    267 				nsize = fragroundup(fs, size);
    268 			else
    269 				nsize = fs->fs_bsize;
    270 			error = ffs_alloc(ip, lbn,
    271 			    ffs_blkpref(ip, lbn, (int)lbn, &ip->i_ffs_db[0]),
    272 				nsize, cred, &newb);
    273 			if (error)
    274 				return (error);
    275 			if (bpp != NULL) {
    276 				bp = getblk(vp, lbn, nsize, 0, 0);
    277 				bp->b_blkno = fsbtodb(fs, newb);
    278 				if (flags & B_CLRBUF)
    279 					clrbuf(bp);
    280 				*bpp = bp;
    281 			}
    282 		}
    283 		ip->i_ffs_db[lbn] = ufs_rw32(newb, UFS_MPNEEDSWAP(vp->v_mount));
    284 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    285 		return (0);
    286 	}
    287 
    288 	/*
    289 	 * Determine the number of levels of indirection.
    290 	 */
    291 
    292 	pref = 0;
    293 	if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
    294 		return(error);
    295 #ifdef DIAGNOSTIC
    296 	if (num < 1)
    297 		panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
    298 #endif
    299 	/*
    300 	 * Fetch the first indirect block allocating if necessary.
    301 	 */
    302 	--num;
    303 	nb = ufs_rw32(ip->i_ffs_ib[indirs[0].in_off],
    304 	    UFS_MPNEEDSWAP(vp->v_mount));
    305 	allocib = NULL;
    306 	allocblk = allociblk;
    307 	if (nb == 0) {
    308 		pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    309 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
    310 			cred, &newb);
    311 		if (error)
    312 			return (error);
    313 		nb = newb;
    314 		*allocblk++ = nb;
    315 		bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
    316 		bp->b_blkno = fsbtodb(fs, nb);
    317 		clrbuf(bp);
    318 		/*
    319 		 * Write synchronously so that indirect blocks
    320 		 * never point at garbage.
    321 		 */
    322 		if ((error = bwrite(bp)) != 0)
    323 			goto fail;
    324 		allocib = &ip->i_ffs_ib[indirs[0].in_off];
    325 		*allocib = ufs_rw32(nb, UFS_MPNEEDSWAP(vp->v_mount));
    326 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    327 	}
    328 	/*
    329 	 * Fetch through the indirect blocks, allocating as necessary.
    330 	 */
    331 	for (i = 1;;) {
    332 		error = bread(vp,
    333 		    indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
    334 		if (error) {
    335 			brelse(bp);
    336 			goto fail;
    337 		}
    338 		bap = (ufs_daddr_t *)bp->b_data;
    339 		nb = ufs_rw32(bap[indirs[i].in_off],
    340 		    UFS_MPNEEDSWAP(vp->v_mount));
    341 		if (i == num)
    342 			break;
    343 		i += 1;
    344 		if (nb != 0) {
    345 			brelse(bp);
    346 			continue;
    347 		}
    348 		if (pref == 0)
    349 			pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    350 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    351 				  &newb);
    352 		if (error) {
    353 			brelse(bp);
    354 			goto fail;
    355 		}
    356 		nb = newb;
    357 		*allocblk++ = nb;
    358 		nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
    359 		nbp->b_blkno = fsbtodb(fs, nb);
    360 		clrbuf(nbp);
    361 		/*
    362 		 * Write synchronously so that indirect blocks
    363 		 * never point at garbage.
    364 		 */
    365 		if ((error = bwrite(nbp)) != 0) {
    366 			brelse(bp);
    367 			goto fail;
    368 		}
    369 		bap[indirs[i - 1].in_off] = ufs_rw32(nb,
    370 		    UFS_MPNEEDSWAP(vp->v_mount));
    371 		/*
    372 		 * If required, write synchronously, otherwise use
    373 		 * delayed write.
    374 		 */
    375 		if (flags & B_SYNC) {
    376 			bwrite(bp);
    377 		} else {
    378 			bdwrite(bp);
    379 		}
    380 	}
    381 	/*
    382 	 * Get the data block, allocating if necessary.
    383 	 */
    384 	if (nb == 0) {
    385 		pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
    386 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    387 				  &newb);
    388 		if (error) {
    389 			brelse(bp);
    390 			goto fail;
    391 		}
    392 		nb = newb;
    393 		*allocblk++ = nb;
    394 		bap[indirs[i].in_off] = ufs_rw32(nb,
    395 		    UFS_MPNEEDSWAP(vp->v_mount));
    396 		/*
    397 		 * If required, write synchronously, otherwise use
    398 		 * delayed write.
    399 		 */
    400 		if (flags & B_SYNC) {
    401 			bwrite(bp);
    402 		} else {
    403 			bdwrite(bp);
    404 		}
    405 		if (bpp != NULL) {
    406 			nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
    407 			nbp->b_blkno = fsbtodb(fs, nb);
    408 			if (flags & B_CLRBUF)
    409 				clrbuf(nbp);
    410 			*bpp = nbp;
    411 		}
    412 		return (0);
    413 	}
    414 
    415 	brelse(bp);
    416 
    417 	if (bpp != NULL) {
    418 		if (flags & B_CLRBUF) {
    419 			error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
    420 			if (error) {
    421 				brelse(nbp);
    422 				goto fail;
    423 			}
    424 		} else {
    425 			nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
    426 			nbp->b_blkno = fsbtodb(fs, nb);
    427 			clrbuf(nbp);
    428 		}
    429 		*bpp = nbp;
    430 	}
    431 	return (0);
    432 fail:
    433 	/*
    434 	 * If we have failed part way through block allocation, we
    435 	 * have to deallocate any indirect blocks that we have allocated.
    436 	 */
    437 	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
    438 		ffs_blkfree(ip, *blkp, fs->fs_bsize);
    439 		deallocated += fs->fs_bsize;
    440 	}
    441 	if (allocib != NULL)
    442 		*allocib = 0;
    443 	if (deallocated) {
    444 #ifdef QUOTA
    445 		/*
    446 		 * Restore user's disk quota because allocation failed.
    447 		 */
    448 		(void)chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
    449 #endif
    450 		ip->i_ffs_blocks -= btodb(deallocated);
    451 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    452 	}
    453 	return (error);
    454 }
    455