Home | History | Annotate | Line # | Download | only in ffs
ffs_balloc.c revision 1.1.1.2
      1 /*
      2  * Copyright (c) 1982, 1986, 1989, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  *	@(#)ffs_balloc.c	8.8 (Berkeley) 6/16/95
     34  */
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/buf.h>
     39 #include <sys/proc.h>
     40 #include <sys/file.h>
     41 #include <sys/vnode.h>
     42 
     43 #include <vm/vm.h>
     44 
     45 #include <ufs/ufs/quota.h>
     46 #include <ufs/ufs/inode.h>
     47 #include <ufs/ufs/ufs_extern.h>
     48 
     49 #include <ufs/ffs/fs.h>
     50 #include <ufs/ffs/ffs_extern.h>
     51 
     52 /*
     53  * Balloc defines the structure of file system storage
     54  * by allocating the physical blocks on a device given
     55  * the inode and the logical block number in a file.
     56  */
     57 ffs_balloc(ip, lbn, size, cred, bpp, flags)
     58 	register struct inode *ip;
     59 	register ufs_daddr_t lbn;
     60 	int size;
     61 	struct ucred *cred;
     62 	struct buf **bpp;
     63 	int flags;
     64 {
     65 	register struct fs *fs;
     66 	register ufs_daddr_t nb;
     67 	struct buf *bp, *nbp;
     68 	struct vnode *vp = ITOV(ip);
     69 	struct indir indirs[NIADDR + 2];
     70 	ufs_daddr_t newb, *bap, pref;
     71 	int deallocated, osize, nsize, num, i, error;
     72 	ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
     73 
     74 	*bpp = NULL;
     75 	if (lbn < 0)
     76 		return (EFBIG);
     77 	fs = ip->i_fs;
     78 
     79 	/*
     80 	 * If the next write will extend the file into a new block,
     81 	 * and the file is currently composed of a fragment
     82 	 * this fragment has to be extended to be a full block.
     83 	 */
     84 	nb = lblkno(fs, ip->i_size);
     85 	if (nb < NDADDR && nb < lbn) {
     86 		osize = blksize(fs, ip, nb);
     87 		if (osize < fs->fs_bsize && osize > 0) {
     88 			error = ffs_realloccg(ip, nb,
     89 				ffs_blkpref(ip, nb, (int)nb, &ip->i_db[0]),
     90 				osize, (int)fs->fs_bsize, cred, &bp);
     91 			if (error)
     92 				return (error);
     93 			ip->i_size = (nb + 1) * fs->fs_bsize;
     94 			vnode_pager_setsize(vp, (u_long)ip->i_size);
     95 			ip->i_db[nb] = dbtofsb(fs, bp->b_blkno);
     96 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
     97 			if (flags & B_SYNC)
     98 				bwrite(bp);
     99 			else
    100 				bawrite(bp);
    101 		}
    102 	}
    103 	/*
    104 	 * The first NDADDR blocks are direct blocks
    105 	 */
    106 	if (lbn < NDADDR) {
    107 		nb = ip->i_db[lbn];
    108 		if (nb != 0 && ip->i_size >= (lbn + 1) * fs->fs_bsize) {
    109 			error = bread(vp, lbn, fs->fs_bsize, NOCRED, &bp);
    110 			if (error) {
    111 				brelse(bp);
    112 				return (error);
    113 			}
    114 			*bpp = bp;
    115 			return (0);
    116 		}
    117 		if (nb != 0) {
    118 			/*
    119 			 * Consider need to reallocate a fragment.
    120 			 */
    121 			osize = fragroundup(fs, blkoff(fs, ip->i_size));
    122 			nsize = fragroundup(fs, size);
    123 			if (nsize <= osize) {
    124 				error = bread(vp, lbn, osize, NOCRED, &bp);
    125 				if (error) {
    126 					brelse(bp);
    127 					return (error);
    128 				}
    129 			} else {
    130 				error = ffs_realloccg(ip, lbn,
    131 				    ffs_blkpref(ip, lbn, (int)lbn,
    132 					&ip->i_db[0]), osize, nsize, cred, &bp);
    133 				if (error)
    134 					return (error);
    135 			}
    136 		} else {
    137 			if (ip->i_size < (lbn + 1) * fs->fs_bsize)
    138 				nsize = fragroundup(fs, size);
    139 			else
    140 				nsize = fs->fs_bsize;
    141 			error = ffs_alloc(ip, lbn,
    142 			    ffs_blkpref(ip, lbn, (int)lbn, &ip->i_db[0]),
    143 			    nsize, cred, &newb);
    144 			if (error)
    145 				return (error);
    146 			bp = getblk(vp, lbn, nsize, 0, 0);
    147 			bp->b_blkno = fsbtodb(fs, newb);
    148 			if (flags & B_CLRBUF)
    149 				clrbuf(bp);
    150 		}
    151 		ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno);
    152 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    153 		*bpp = bp;
    154 		return (0);
    155 	}
    156 	/*
    157 	 * Determine the number of levels of indirection.
    158 	 */
    159 	pref = 0;
    160 	if (error = ufs_getlbns(vp, lbn, indirs, &num))
    161 		return(error);
    162 #ifdef DIAGNOSTIC
    163 	if (num < 1)
    164 		panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
    165 #endif
    166 	/*
    167 	 * Fetch the first indirect block allocating if necessary.
    168 	 */
    169 	--num;
    170 	nb = ip->i_ib[indirs[0].in_off];
    171 	allocib = NULL;
    172 	allocblk = allociblk;
    173 	if (nb == 0) {
    174 		pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    175 	        if (error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
    176 		    cred, &newb))
    177 			return (error);
    178 		nb = newb;
    179 		*allocblk++ = nb;
    180 		bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
    181 		bp->b_blkno = fsbtodb(fs, nb);
    182 		clrbuf(bp);
    183 		/*
    184 		 * Write synchronously so that indirect blocks
    185 		 * never point at garbage.
    186 		 */
    187 		if (error = bwrite(bp))
    188 			goto fail;
    189 		allocib = &ip->i_ib[indirs[0].in_off];
    190 		*allocib = nb;
    191 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    192 	}
    193 	/*
    194 	 * Fetch through the indirect blocks, allocating as necessary.
    195 	 */
    196 	for (i = 1;;) {
    197 		error = bread(vp,
    198 		    indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
    199 		if (error) {
    200 			brelse(bp);
    201 			goto fail;
    202 		}
    203 		bap = (ufs_daddr_t *)bp->b_data;
    204 		nb = bap[indirs[i].in_off];
    205 		if (i == num)
    206 			break;
    207 		i += 1;
    208 		if (nb != 0) {
    209 			brelse(bp);
    210 			continue;
    211 		}
    212 		if (pref == 0)
    213 			pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    214 		if (error =
    215 		    ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred, &newb)) {
    216 			brelse(bp);
    217 			goto fail;
    218 		}
    219 		nb = newb;
    220 		*allocblk++ = nb;
    221 		nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
    222 		nbp->b_blkno = fsbtodb(fs, nb);
    223 		clrbuf(nbp);
    224 		/*
    225 		 * Write synchronously so that indirect blocks
    226 		 * never point at garbage.
    227 		 */
    228 		if (error = bwrite(nbp)) {
    229 			brelse(bp);
    230 			goto fail;
    231 		}
    232 		bap[indirs[i - 1].in_off] = nb;
    233 		/*
    234 		 * If required, write synchronously, otherwise use
    235 		 * delayed write.
    236 		 */
    237 		if (flags & B_SYNC) {
    238 			bwrite(bp);
    239 		} else {
    240 			bdwrite(bp);
    241 		}
    242 	}
    243 	/*
    244 	 * Get the data block, allocating if necessary.
    245 	 */
    246 	if (nb == 0) {
    247 		pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
    248 		if (error = ffs_alloc(ip,
    249 		    lbn, pref, (int)fs->fs_bsize, cred, &newb)) {
    250 			brelse(bp);
    251 			goto fail;
    252 		}
    253 		nb = newb;
    254 		*allocblk++ = nb;
    255 		nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
    256 		nbp->b_blkno = fsbtodb(fs, nb);
    257 		if (flags & B_CLRBUF)
    258 			clrbuf(nbp);
    259 		bap[indirs[i].in_off] = nb;
    260 		/*
    261 		 * If required, write synchronously, otherwise use
    262 		 * delayed write.
    263 		 */
    264 		if (flags & B_SYNC) {
    265 			bwrite(bp);
    266 		} else {
    267 			bdwrite(bp);
    268 		}
    269 		*bpp = nbp;
    270 		return (0);
    271 	}
    272 	brelse(bp);
    273 	if (flags & B_CLRBUF) {
    274 		error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
    275 		if (error) {
    276 			brelse(nbp);
    277 			goto fail;
    278 		}
    279 	} else {
    280 		nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
    281 		nbp->b_blkno = fsbtodb(fs, nb);
    282 	}
    283 	*bpp = nbp;
    284 	return (0);
    285 fail:
    286 	/*
    287 	 * If we have failed part way through block allocation, we
    288 	 * have to deallocate any indirect blocks that we have allocated.
    289 	 */
    290 	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
    291 		ffs_blkfree(ip, *blkp, fs->fs_bsize);
    292 		deallocated += fs->fs_bsize;
    293 	}
    294 	if (allocib != NULL)
    295 		*allocib = 0;
    296 	if (deallocated) {
    297 #ifdef QUOTA
    298 		/*
    299 		 * Restore user's disk quota because allocation failed.
    300 		 */
    301 		(void) chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
    302 #endif
    303 		ip->i_blocks -= btodb(deallocated);
    304 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    305 	}
    306 	return (error);
    307 }
    308