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