Home | History | Annotate | Line # | Download | only in ffs
ffs_balloc.c revision 1.14.14.1
      1 /*	$NetBSD: ffs_balloc.c,v 1.14.14.1 1999/12/21 23:20:07 wrstuden 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 
     52 #include <uvm/uvm_extern.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(ip, lbn, size, cred, bpp, flags)
     70 	register struct inode *ip;
     71 	register ufs_daddr_t lbn;
     72 	int size;
     73 	struct ucred *cred;
     74 	struct buf **bpp;
     75 	int flags;
     76 {
     77 	register struct fs *fs;
     78 	register ufs_daddr_t nb;
     79 	struct buf *bp, *nbp;
     80 	struct vnode *vp = ITOV(ip);
     81 	struct indir indirs[NIADDR + 2];
     82 	ufs_daddr_t newb, *bap, pref;
     83 	int deallocated, osize, nsize, num, i, error;
     84 	ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
     85 
     86 	*bpp = NULL;
     87 	if (lbn < 0)
     88 		return (EFBIG);
     89 	fs = ip->i_fs;
     90 
     91 	/*
     92 	 * If the next write will extend the file into a new block,
     93 	 * and the file is currently composed of a fragment
     94 	 * this fragment has to be extended to be a full block.
     95 	 */
     96 	nb = lblkno(fs, ip->i_ffs_size);
     97 	if (nb < NDADDR && nb < lbn) {
     98 		osize = blksize(fs, ip, nb);
     99 		if (osize < fs->fs_bsize && osize > 0) {
    100 			error = ffs_realloccg(ip, nb,
    101 				ffs_blkpref(ip, nb, (int)nb, &ip->i_ffs_db[0]),
    102 				osize, (int)fs->fs_bsize, cred, &bp);
    103 			if (error)
    104 				return (error);
    105 			ip->i_ffs_size = (nb + 1) * fs->fs_bsize;
    106 			uvm_vnp_setsize(vp, ip->i_ffs_size);
    107 			ip->i_ffs_db[nb] = ufs_rw32(dbtofsb(fs, bp->b_blkno),
    108 			    UFS_MPNEEDSWAP(vp->v_mount));
    109 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    110 			if (flags & B_SYNC)
    111 				bwrite(bp);
    112 			else
    113 				bawrite(bp);
    114 		}
    115 	}
    116 	/*
    117 	 * The first NDADDR blocks are direct blocks
    118 	 */
    119 	if (lbn < NDADDR) {
    120 		nb = ufs_rw32(ip->i_ffs_db[lbn], UFS_MPNEEDSWAP(vp->v_mount));
    121 		if (nb != 0 && ip->i_ffs_size >= (lbn + 1) * fs->fs_bsize) {
    122 			error = bread(vp, lbn, fs->fs_bsize, NOCRED, &bp);
    123 			if (error) {
    124 				brelse(bp);
    125 				return (error);
    126 			}
    127 			*bpp = bp;
    128 			return (0);
    129 		}
    130 		if (nb != 0) {
    131 			/*
    132 			 * Consider need to reallocate a fragment.
    133 			 */
    134 			osize = fragroundup(fs, blkoff(fs, ip->i_ffs_size));
    135 			nsize = fragroundup(fs, size);
    136 			if (nsize <= osize) {
    137 				error = bread(vp, lbn, osize, NOCRED, &bp);
    138 				if (error) {
    139 					brelse(bp);
    140 					return (error);
    141 				}
    142 			} else {
    143 				error = ffs_realloccg(ip, lbn,
    144 				    ffs_blkpref(ip, lbn, (int)lbn,
    145 					&ip->i_ffs_db[0]), osize, nsize, cred,
    146 					&bp);
    147 				if (error)
    148 					return (error);
    149 			}
    150 		} else {
    151 			if (ip->i_ffs_size < (lbn + 1) * fs->fs_bsize)
    152 				nsize = fragroundup(fs, size);
    153 			else
    154 				nsize = fs->fs_bsize;
    155 			error = ffs_alloc(ip, lbn,
    156 			    ffs_blkpref(ip, lbn, (int)lbn, &ip->i_ffs_db[0]),
    157 				nsize, cred, &newb);
    158 			if (error)
    159 				return (error);
    160 			bp = getblk(vp, lbn, nsize, 0, 0);
    161 			bp->b_blkno = fsbtodb(fs, newb);
    162 			if (flags & B_CLRBUF)
    163 				clrbuf(bp);
    164 		}
    165 		ip->i_ffs_db[lbn] = ufs_rw32(dbtofsb(fs, bp->b_blkno),
    166 		    UFS_MPNEEDSWAP(vp->v_mount));
    167 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    168 		*bpp = bp;
    169 		return (0);
    170 	}
    171 	/*
    172 	 * Determine the number of levels of indirection.
    173 	 */
    174 	pref = 0;
    175 	if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
    176 		return(error);
    177 #ifdef DIAGNOSTIC
    178 	if (num < 1)
    179 		panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
    180 #endif
    181 	/*
    182 	 * Fetch the first indirect block allocating if necessary.
    183 	 */
    184 	--num;
    185 	nb = ufs_rw32(ip->i_ffs_ib[indirs[0].in_off],
    186 	    UFS_MPNEEDSWAP(vp->v_mount));
    187 	allocib = NULL;
    188 	allocblk = allociblk;
    189 	if (nb == 0) {
    190 		pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    191 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
    192 			cred, &newb);
    193 		if (error)
    194 			return (error);
    195 		nb = newb;
    196 		*allocblk++ = nb;
    197 		bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
    198 		bp->b_blkno = fsbtodb(fs, nb);
    199 		clrbuf(bp);
    200 		/*
    201 		 * Write synchronously so that indirect blocks
    202 		 * never point at garbage.
    203 		 */
    204 		if ((error = bwrite(bp)) != 0)
    205 			goto fail;
    206 		allocib = &ip->i_ffs_ib[indirs[0].in_off];
    207 		*allocib = ufs_rw32(nb, UFS_MPNEEDSWAP(vp->v_mount));
    208 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    209 	}
    210 	/*
    211 	 * Fetch through the indirect blocks, allocating as necessary.
    212 	 */
    213 	for (i = 1;;) {
    214 		error = bread(vp,
    215 		    indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
    216 		if (error) {
    217 			brelse(bp);
    218 			goto fail;
    219 		}
    220 		bap = (ufs_daddr_t *)bp->b_data;
    221 		nb = ufs_rw32(bap[indirs[i].in_off],
    222 		    UFS_MPNEEDSWAP(vp->v_mount));
    223 		if (i == num)
    224 			break;
    225 		i += 1;
    226 		if (nb != 0) {
    227 			brelse(bp);
    228 			continue;
    229 		}
    230 		if (pref == 0)
    231 			pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    232 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    233 				  &newb);
    234 		if (error) {
    235 			brelse(bp);
    236 			goto fail;
    237 		}
    238 		nb = newb;
    239 		*allocblk++ = nb;
    240 		nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
    241 		nbp->b_blkno = fsbtodb(fs, nb);
    242 		clrbuf(nbp);
    243 		/*
    244 		 * Write synchronously so that indirect blocks
    245 		 * never point at garbage.
    246 		 */
    247 		if ((error = bwrite(nbp)) != 0) {
    248 			brelse(bp);
    249 			goto fail;
    250 		}
    251 		bap[indirs[i - 1].in_off] = ufs_rw32(nb,
    252 		    UFS_MPNEEDSWAP(vp->v_mount));
    253 		/*
    254 		 * If required, write synchronously, otherwise use
    255 		 * delayed write.
    256 		 */
    257 		if (flags & B_SYNC) {
    258 			bwrite(bp);
    259 		} else {
    260 			bdwrite(bp);
    261 		}
    262 	}
    263 	/*
    264 	 * Get the data block, allocating if necessary.
    265 	 */
    266 	if (nb == 0) {
    267 		pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
    268 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    269 				  &newb);
    270 		if (error) {
    271 			brelse(bp);
    272 			goto fail;
    273 		}
    274 		nb = newb;
    275 		*allocblk++ = nb;
    276 		nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
    277 		nbp->b_blkno = fsbtodb(fs, nb);
    278 		if (flags & B_CLRBUF)
    279 			clrbuf(nbp);
    280 		bap[indirs[i].in_off] = ufs_rw32(nb,
    281 		    UFS_MPNEEDSWAP(vp->v_mount));
    282 		/*
    283 		 * If required, write synchronously, otherwise use
    284 		 * delayed write.
    285 		 */
    286 		if (flags & B_SYNC) {
    287 			bwrite(bp);
    288 		} else {
    289 			bdwrite(bp);
    290 		}
    291 		*bpp = nbp;
    292 		return (0);
    293 	}
    294 	brelse(bp);
    295 	if (flags & B_CLRBUF) {
    296 		error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
    297 		if (error) {
    298 			brelse(nbp);
    299 			goto fail;
    300 		}
    301 	} else {
    302 		nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
    303 		nbp->b_blkno = fsbtodb(fs, nb);
    304 	}
    305 	*bpp = nbp;
    306 	return (0);
    307 fail:
    308 	/*
    309 	 * If we have failed part way through block allocation, we
    310 	 * have to deallocate any indirect blocks that we have allocated.
    311 	 */
    312 	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
    313 		ffs_blkfree(ip, *blkp, fs->fs_bsize);
    314 		deallocated += fs->fs_bsize;
    315 	}
    316 	if (allocib != NULL)
    317 		*allocib = 0;
    318 	if (deallocated) {
    319 #ifdef QUOTA
    320 		/*
    321 		 * Restore user's disk quota because allocation failed.
    322 		 */
    323 		(void)chkdq(ip, (long)-btodb(deallocated, UFS_BSHIFT), cred, FORCE);
    324 #endif
    325 		ip->i_ffs_blocks -= btodb(deallocated, UFS_BSHIFT);
    326 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    327 	}
    328 	return (error);
    329 }
    330