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