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