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