Home | History | Annotate | Line # | Download | only in ffs
ffs_balloc.c revision 1.13.2.4
      1 /*	$NetBSD: ffs_balloc.c,v 1.13.2.4 1999/04/29 05:32:46 chs 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.8 (Berkeley) 6/16/95
     36  */
     37 
     38 #if defined(_KERNEL) && !defined(_LKM)
     39 #include "opt_quota.h"
     40 #include "opt_uvm.h"
     41 #endif
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/buf.h>
     46 #include <sys/proc.h>
     47 #include <sys/file.h>
     48 #include <sys/vnode.h>
     49 #include <sys/mount.h>
     50 
     51 #include <vm/vm.h>
     52 
     53 #if defined(UVM)
     54 #include <uvm/uvm.h>
     55 #endif
     56 
     57 #include <ufs/ufs/quota.h>
     58 #include <ufs/ufs/ufsmount.h>
     59 #include <ufs/ufs/inode.h>
     60 #include <ufs/ufs/ufs_extern.h>
     61 #include <ufs/ufs/ufs_bswap.h>
     62 
     63 #include <ufs/ffs/fs.h>
     64 #include <ufs/ffs/ffs_extern.h>
     65 
     66 /*
     67  * Balloc defines the structure of file system storage
     68  * by allocating the physical blocks on a device given
     69  * the inode and the logical block number in a file.
     70  */
     71 int
     72 ffs_balloc(ip, lbn, size, cred, bpp, blknop, flags, alloced)
     73 	struct inode *ip;
     74 	ufs_daddr_t lbn;
     75 	int size;
     76 	struct ucred *cred;
     77 	struct buf **bpp;
     78 	daddr_t *blknop;
     79 	int flags;
     80 	boolean_t *alloced;
     81 {
     82 	struct fs *fs;
     83 	ufs_daddr_t nb;
     84 	struct buf *bp, *nbp;
     85 	struct vnode *vp = ITOV(ip);
     86 	struct indir indirs[NIADDR + 2];
     87 	ufs_daddr_t newb, *bap, pref;
     88 	int deallocated, osize, nsize, num, i, error;
     89 	ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
     90 
     91 	if (bpp != NULL) {
     92 		*bpp = NULL;
     93 	}
     94 	if (blknop != NULL) {
     95 		*blknop = (daddr_t)-1;
     96 	}
     97 	if (alloced != NULL) {
     98 		*alloced = FALSE;
     99 	}
    100 
    101 	if (lbn < 0)
    102 		return (EFBIG);
    103 	fs = ip->i_fs;
    104 
    105 	/*
    106 	 * If the file currently ends with a fragment and
    107 	 * the block we're allocating now is after the current EOF,
    108 	 * this fragment has to be extended to be a full block.
    109 	 */
    110 	nb = lblkno(fs, ip->i_ffs_size);
    111 	if (nb < NDADDR && nb < lbn) {
    112 		osize = blksize(fs, ip, nb);
    113 		if (osize < fs->fs_bsize && osize > 0) {
    114 			error = ffs_realloccg(ip, nb,
    115 				ffs_blkpref(ip, nb, (int)nb, &ip->i_ffs_db[0]),
    116 				osize, (int)fs->fs_bsize, cred, bpp, &newb);
    117 			if (error)
    118 				return (error);
    119 			ip->i_ffs_size = lblktosize(fs, nb + 1);
    120 #if defined(UVM)
    121 			uvm_vnp_setsize(vp, ip->i_ffs_size);
    122 #else
    123 			vnode_pager_setsize(vp, ip->i_ffs_size);
    124 #endif
    125 			ip->i_ffs_db[nb] = ufs_rw32(newb,
    126 			    UFS_MPNEEDSWAP(vp->v_mount));
    127 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    128 
    129 			if (bpp) {
    130 				if (flags & B_SYNC)
    131 					bwrite(*bpp);
    132 				else
    133 					bawrite(*bpp);
    134 			}
    135 			else {
    136 				/*
    137 				 * XXX the data in the frag might be
    138 				 * moving to a new disk location.
    139 				 * we need to flush pages to the
    140 				 * new disk locations.
    141 				 * XXX we could do this in realloccg
    142 				 * except for the sync flag.
    143 				 */
    144 				(vp->v_uvm.u_obj.pgops->pgo_flush)
    145 					(&vp->v_uvm.u_obj, lblktosize(fs, nb),
    146 					 lblktosize(fs, nb + 1),
    147 					 flags & B_SYNC ? PGO_SYNCIO : 0);
    148 			}
    149 		}
    150 	}
    151 	/*
    152 	 * The first NDADDR blocks are direct blocks
    153 	 */
    154 	if (lbn < NDADDR) {
    155 
    156 		nb = ufs_rw32(ip->i_ffs_db[lbn], UFS_MPNEEDSWAP(vp->v_mount));
    157 		if (nb != 0 && ip->i_ffs_size >= lblktosize(fs, lbn + 1)) {
    158 
    159 			/*
    160 			 * the block is an already-allocated direct block
    161 			 * and the file already extends past this block,
    162 			 * thus this must be a whole block.
    163 			 * just read the block (if requested).
    164 			 */
    165 
    166 			if (bpp != NULL) {
    167 				error = bread(vp, lbn, fs->fs_bsize, NOCRED,
    168 					      &bp);
    169 				if (error) {
    170 					brelse(bp);
    171 					return (error);
    172 				}
    173 				*bpp = bp;
    174 			}
    175 			return (0);
    176 		}
    177 		if (nb != 0) {
    178 			/*
    179 			 * Consider need to reallocate a fragment.
    180 			 */
    181 			osize = fragroundup(fs, blkoff(fs, ip->i_ffs_size));
    182 			nsize = fragroundup(fs, size);
    183 			if (nsize <= osize) {
    184 
    185 				/*
    186 				 * the existing block is already
    187 				 * at least as big as we want.
    188 				 * just read the block (if requested).
    189 				 */
    190 
    191 				if (bpp != NULL) {
    192 					error = bread(vp, lbn, osize, NOCRED,
    193 						      &bp);
    194 					if (error) {
    195 						brelse(bp);
    196 						return (error);
    197 					}
    198 					*bpp = bp;
    199 				}
    200 				return 0;
    201 			} else {
    202 
    203 				/*
    204 				 * the existing block is smaller than we want,
    205 				 * grow it.
    206 				 */
    207 
    208 				error = ffs_realloccg(ip, lbn,
    209 				    ffs_blkpref(ip, lbn, (int)lbn,
    210 					&ip->i_ffs_db[0]), osize, nsize, cred,
    211 					bpp, &newb);
    212 				if (error)
    213 					return (error);
    214 			}
    215 		} else {
    216 
    217 			/*
    218 			 * the block was not previously allocated,
    219 			 * allocate a new block or fragment.
    220 			 */
    221 
    222 			if (ip->i_ffs_size < lblktosize(fs, lbn + 1))
    223 				nsize = fragroundup(fs, size);
    224 			else
    225 				nsize = fs->fs_bsize;
    226 			error = ffs_alloc(ip, lbn,
    227 			    ffs_blkpref(ip, lbn, (int)lbn, &ip->i_ffs_db[0]),
    228 				nsize, cred, &newb);
    229 			if (error)
    230 				return (error);
    231 			if (bpp != NULL) {
    232 				bp = getblk(vp, lbn, nsize, 0, 0);
    233 				bp->b_blkno = fsbtodb(fs, newb);
    234 				if (flags & B_CLRBUF)
    235 					clrbuf(bp);
    236 				*bpp = bp;
    237 			}
    238 		}
    239 		ip->i_ffs_db[lbn] = ufs_rw32(newb, UFS_MPNEEDSWAP(vp->v_mount));
    240 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    241 
    242 		if (blknop != NULL) {
    243 			*blknop = fsbtodb(fs, newb);
    244 		}
    245 		if (alloced != NULL) {
    246 			*alloced = TRUE;
    247 		}
    248 		return (0);
    249 	}
    250 	/*
    251 	 * Determine the number of levels of indirection.
    252 	 */
    253 	pref = 0;
    254 	if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
    255 		return(error);
    256 #ifdef DIAGNOSTIC
    257 	if (num < 1)
    258 		panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
    259 #endif
    260 	/*
    261 	 * Fetch the first indirect block allocating if necessary.
    262 	 */
    263 	--num;
    264 	nb = ufs_rw32(ip->i_ffs_ib[indirs[0].in_off],
    265 	    UFS_MPNEEDSWAP(vp->v_mount));
    266 	allocib = NULL;
    267 	allocblk = allociblk;
    268 	if (nb == 0) {
    269 		pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    270 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
    271 			cred, &newb);
    272 		if (error)
    273 			return (error);
    274 		nb = newb;
    275 		*allocblk++ = nb;
    276 		bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
    277 		bp->b_blkno = fsbtodb(fs, nb);
    278 		clrbuf(bp);
    279 		/*
    280 		 * Write synchronously so that indirect blocks
    281 		 * never point at garbage.
    282 		 */
    283 		if ((error = bwrite(bp)) != 0)
    284 			goto fail;
    285 		allocib = &ip->i_ffs_ib[indirs[0].in_off];
    286 		*allocib = ufs_rw32(nb, UFS_MPNEEDSWAP(vp->v_mount));
    287 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    288 	}
    289 	/*
    290 	 * Fetch through the indirect blocks, allocating as necessary.
    291 	 */
    292 	for (i = 1;;) {
    293 		error = bread(vp,
    294 		    indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
    295 		if (error) {
    296 			brelse(bp);
    297 			goto fail;
    298 		}
    299 		bap = (ufs_daddr_t *)bp->b_data;
    300 		nb = ufs_rw32(bap[indirs[i].in_off],
    301 		    UFS_MPNEEDSWAP(vp->v_mount));
    302 		if (i == num)
    303 			break;
    304 		i += 1;
    305 		if (nb != 0) {
    306 			brelse(bp);
    307 			continue;
    308 		}
    309 		if (pref == 0)
    310 			pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    311 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    312 				  &newb);
    313 		if (error) {
    314 			brelse(bp);
    315 			goto fail;
    316 		}
    317 		nb = newb;
    318 		*allocblk++ = nb;
    319 		nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
    320 		nbp->b_blkno = fsbtodb(fs, nb);
    321 		clrbuf(nbp);
    322 		/*
    323 		 * Write synchronously so that indirect blocks
    324 		 * never point at garbage.
    325 		 */
    326 		if ((error = bwrite(nbp)) != 0) {
    327 			brelse(bp);
    328 			goto fail;
    329 		}
    330 		bap[indirs[i - 1].in_off] = ufs_rw32(nb,
    331 		    UFS_MPNEEDSWAP(vp->v_mount));
    332 		/*
    333 		 * If required, write synchronously, otherwise use
    334 		 * delayed write.
    335 		 */
    336 		if (flags & B_SYNC) {
    337 			bwrite(bp);
    338 		} else {
    339 			bdwrite(bp);
    340 		}
    341 	}
    342 	/*
    343 	 * Get the data block, allocating if necessary.
    344 	 */
    345 	if (nb == 0) {
    346 		pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
    347 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    348 				  &newb);
    349 		if (error) {
    350 			brelse(bp);
    351 			goto fail;
    352 		}
    353 		nb = newb;
    354 		*allocblk++ = nb;
    355 		bap[indirs[i].in_off] = ufs_rw32(nb,
    356 		    UFS_MPNEEDSWAP(vp->v_mount));
    357 		/*
    358 		 * If required, write synchronously, otherwise use
    359 		 * delayed write.
    360 		 */
    361 		if (flags & B_SYNC) {
    362 			bwrite(bp);
    363 		} else {
    364 			bdwrite(bp);
    365 		}
    366 		if (bpp != NULL) {
    367 			nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
    368 			nbp->b_blkno = fsbtodb(fs, nb);
    369 			if (flags & B_CLRBUF)
    370 				clrbuf(nbp);
    371 			*bpp = nbp;
    372 		}
    373 		if (blknop != NULL) {
    374 			*blknop = fsbtodb(fs, nb);
    375 		}
    376 		if (alloced != NULL) {
    377 			*alloced = TRUE;
    378 		}
    379 		return (0);
    380 	}
    381 
    382 	brelse(bp);
    383 
    384 	if (bpp != NULL) {
    385 		if (flags & B_CLRBUF) {
    386 			error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
    387 			if (error) {
    388 				brelse(nbp);
    389 				goto fail;
    390 			}
    391 		} else {
    392 			nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
    393 			nbp->b_blkno = fsbtodb(fs, nb);
    394 			clrbuf(nbp);
    395 		}
    396 		*bpp = nbp;
    397 	}
    398 	if (blknop != NULL) {
    399 		*blknop = fsbtodb(fs, nb);
    400 	}
    401 	return (0);
    402 fail:
    403 	/*
    404 	 * If we have failed part way through block allocation, we
    405 	 * have to deallocate any indirect blocks that we have allocated.
    406 	 */
    407 	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
    408 		ffs_blkfree(ip, *blkp, fs->fs_bsize);
    409 		deallocated += fs->fs_bsize;
    410 	}
    411 	if (allocib != NULL)
    412 		*allocib = 0;
    413 	if (deallocated) {
    414 #ifdef QUOTA
    415 		/*
    416 		 * Restore user's disk quota because allocation failed.
    417 		 */
    418 		(void)chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
    419 #endif
    420 		ip->i_ffs_blocks -= btodb(deallocated);
    421 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    422 	}
    423 	return (error);
    424 }
    425 
    426 
    427 
    428 int
    429 ffs_balloc_range(ip, off, len, cred, flags)
    430 	struct inode *ip;
    431 	off_t off;
    432 	off_t len;
    433 	struct ucred *cred;
    434 	int flags;
    435 {
    436 	struct fs *fs = ip->i_fs;
    437 	int lbn, bsize, delta, error;
    438 	daddr_t blkno;
    439 	boolean_t alloced;
    440 	off_t pagestart, pageend;
    441 
    442 	/*
    443 	 * pagestart and pageend describe the range of pages that are
    444 	 * completely covered by the range of blocks being allocated.
    445 	 */
    446 
    447 	pagestart = round_page(off);
    448 	pageend = trunc_page(off + len);
    449 
    450 	while (len > 0) {
    451 		lbn = lblkno(fs, off);
    452 		bsize = min(fs->fs_bsize, blkoff(fs, off) + len);
    453 
    454 
    455 		if ((error = ffs_balloc(ip, lbn, bsize, cred, NULL, &blkno,
    456 					flags, &alloced))) {
    457 			return error;
    458 		}
    459 
    460 		/*
    461 		 * bump file size now.
    462 		 * ffs_balloc() needs to know in the case where we loop here.
    463 		 */
    464 
    465 		if (ip->i_ffs_size < lblktosize(fs, lbn) + bsize) {
    466 			ip->i_ffs_size = lblktosize(fs, lbn) + bsize;
    467 			uvm_vnp_setsize(ip->i_vnode, ip->i_ffs_size);
    468 		}
    469 
    470 		/*
    471 		 * if the block was freshly allocated then we can
    472 		 * allocate the pages now and set their blknos.
    473 		 */
    474 
    475 		if (alloced) {
    476 			uvm_vnp_setpageblknos(ITOV(ip), off, len, blkno,
    477 					      UFP_ALL, (off < pagestart ||
    478 							off + len > pageend));
    479 		}
    480 
    481 		delta = fs->fs_bsize - blkoff(fs, off);
    482 		len -= delta;
    483 		off += delta;
    484 	}
    485 	return 0;
    486 }
    487