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