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