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