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