Home | History | Annotate | Line # | Download | only in ffs
ffs_balloc.c revision 1.13.2.3
      1 /*	$NetBSD: ffs_balloc.c,v 1.13.2.3 1999/04/09 04:35:33 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 				ip->i_ffs_db[lbn] = ufs_rw32(newb,
    215 					UFS_MPNEEDSWAP(vp->v_mount));
    216 				ip->i_flag |= IN_CHANGE | IN_UPDATE;
    217 			}
    218 		} else {
    219 
    220 			/*
    221 			 * the block was not previously allocated,
    222 			 * allocate a new block or fragment.
    223 			 */
    224 
    225 			if (ip->i_ffs_size < lblktosize(fs, lbn + 1))
    226 				nsize = fragroundup(fs, size);
    227 			else
    228 				nsize = fs->fs_bsize;
    229 			error = ffs_alloc(ip, lbn,
    230 			    ffs_blkpref(ip, lbn, (int)lbn, &ip->i_ffs_db[0]),
    231 				nsize, cred, &newb);
    232 			if (error)
    233 				return (error);
    234 
    235 			ip->i_ffs_db[lbn] = ufs_rw32(newb,
    236 				UFS_MPNEEDSWAP(vp->v_mount));
    237 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
    238 
    239 			if (bpp != NULL) {
    240 				bp = getblk(vp, lbn, nsize, 0, 0);
    241 				bp->b_blkno = fsbtodb(fs, newb);
    242 				if (flags & B_CLRBUF)
    243 					clrbuf(bp);
    244 				*bpp = bp;
    245 			}
    246 			if (blknop != NULL) {
    247 				*blknop = fsbtodb(fs, newb);
    248 			}
    249 			if (alloced != NULL) {
    250 				*alloced = TRUE;
    251 			}
    252 		}
    253 		return (0);
    254 	}
    255 	/*
    256 	 * Determine the number of levels of indirection.
    257 	 */
    258 	pref = 0;
    259 	if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
    260 		return(error);
    261 #ifdef DIAGNOSTIC
    262 	if (num < 1)
    263 		panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
    264 #endif
    265 	/*
    266 	 * Fetch the first indirect block allocating if necessary.
    267 	 */
    268 	--num;
    269 	nb = ufs_rw32(ip->i_ffs_ib[indirs[0].in_off],
    270 	    UFS_MPNEEDSWAP(vp->v_mount));
    271 	allocib = NULL;
    272 	allocblk = allociblk;
    273 	if (nb == 0) {
    274 		pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    275 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
    276 			cred, &newb);
    277 		if (error)
    278 			return (error);
    279 		nb = newb;
    280 		*allocblk++ = nb;
    281 		bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
    282 		bp->b_blkno = fsbtodb(fs, nb);
    283 		clrbuf(bp);
    284 		/*
    285 		 * Write synchronously so that indirect blocks
    286 		 * never point at garbage.
    287 		 */
    288 		if ((error = bwrite(bp)) != 0)
    289 			goto fail;
    290 		allocib = &ip->i_ffs_ib[indirs[0].in_off];
    291 		*allocib = ufs_rw32(nb, UFS_MPNEEDSWAP(vp->v_mount));
    292 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    293 	}
    294 	/*
    295 	 * Fetch through the indirect blocks, allocating as necessary.
    296 	 */
    297 	for (i = 1;;) {
    298 		error = bread(vp,
    299 		    indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
    300 		if (error) {
    301 			brelse(bp);
    302 			goto fail;
    303 		}
    304 		bap = (ufs_daddr_t *)bp->b_data;
    305 		nb = ufs_rw32(bap[indirs[i].in_off],
    306 		    UFS_MPNEEDSWAP(vp->v_mount));
    307 		if (i == num)
    308 			break;
    309 		i += 1;
    310 		if (nb != 0) {
    311 			brelse(bp);
    312 			continue;
    313 		}
    314 		if (pref == 0)
    315 			pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
    316 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    317 				  &newb);
    318 		if (error) {
    319 			brelse(bp);
    320 			goto fail;
    321 		}
    322 		nb = newb;
    323 		*allocblk++ = nb;
    324 		nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
    325 		nbp->b_blkno = fsbtodb(fs, nb);
    326 		clrbuf(nbp);
    327 		/*
    328 		 * Write synchronously so that indirect blocks
    329 		 * never point at garbage.
    330 		 */
    331 		if ((error = bwrite(nbp)) != 0) {
    332 			brelse(bp);
    333 			goto fail;
    334 		}
    335 		bap[indirs[i - 1].in_off] = ufs_rw32(nb,
    336 		    UFS_MPNEEDSWAP(vp->v_mount));
    337 		/*
    338 		 * If required, write synchronously, otherwise use
    339 		 * delayed write.
    340 		 */
    341 		if (flags & B_SYNC) {
    342 			bwrite(bp);
    343 		} else {
    344 			bdwrite(bp);
    345 		}
    346 	}
    347 	/*
    348 	 * Get the data block, allocating if necessary.
    349 	 */
    350 	if (nb == 0) {
    351 		pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
    352 		error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
    353 				  &newb);
    354 		if (error) {
    355 			brelse(bp);
    356 			goto fail;
    357 		}
    358 		nb = newb;
    359 		*allocblk++ = nb;
    360 		bap[indirs[i].in_off] = ufs_rw32(nb,
    361 		    UFS_MPNEEDSWAP(vp->v_mount));
    362 		/*
    363 		 * If required, write synchronously, otherwise use
    364 		 * delayed write.
    365 		 */
    366 		if (flags & B_SYNC) {
    367 			bwrite(bp);
    368 		} else {
    369 			bdwrite(bp);
    370 		}
    371 		if (bpp != NULL) {
    372 			nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
    373 			nbp->b_blkno = fsbtodb(fs, nb);
    374 			if (flags & B_CLRBUF)
    375 				clrbuf(nbp);
    376 			*bpp = nbp;
    377 		}
    378 		if (blknop != NULL) {
    379 			*blknop = fsbtodb(fs, nb);
    380 		}
    381 		if (alloced != NULL) {
    382 			*alloced = TRUE;
    383 		}
    384 		return (0);
    385 	}
    386 
    387 	brelse(bp);
    388 
    389 	if (bpp != NULL) {
    390 		if (flags & B_CLRBUF) {
    391 			error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
    392 			if (error) {
    393 				brelse(nbp);
    394 				goto fail;
    395 			}
    396 		} else {
    397 			nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
    398 			nbp->b_blkno = fsbtodb(fs, nb);
    399 			clrbuf(nbp);
    400 		}
    401 		*bpp = nbp;
    402 	}
    403 	if (blknop != NULL) {
    404 		*blknop = fsbtodb(fs, nb);
    405 	}
    406 	return (0);
    407 fail:
    408 	/*
    409 	 * If we have failed part way through block allocation, we
    410 	 * have to deallocate any indirect blocks that we have allocated.
    411 	 */
    412 	for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
    413 		ffs_blkfree(ip, *blkp, fs->fs_bsize);
    414 		deallocated += fs->fs_bsize;
    415 	}
    416 	if (allocib != NULL)
    417 		*allocib = 0;
    418 	if (deallocated) {
    419 #ifdef QUOTA
    420 		/*
    421 		 * Restore user's disk quota because allocation failed.
    422 		 */
    423 		(void)chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
    424 #endif
    425 		ip->i_ffs_blocks -= btodb(deallocated);
    426 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    427 	}
    428 	return (error);
    429 }
    430 
    431 
    432 
    433 int
    434 ffs_balloc_range(ip, off, len, cred, flags)
    435 	struct inode *ip;
    436 	off_t off;
    437 	off_t len;
    438 	struct ucred *cred;
    439 	int flags;
    440 {
    441 	struct fs *fs = ip->i_fs;
    442 	int lbn, bsize, delta, error;
    443 	daddr_t blkno;
    444 	boolean_t alloced;
    445 	off_t pagestart, pageend;
    446 
    447 	/*
    448 	 * pagestart and pageend describe the range of pages that are
    449 	 * completely covered by the range of blocks being allocated.
    450 	 */
    451 
    452 	pagestart = round_page(off);
    453 	pageend = trunc_page(off + len);
    454 
    455 	while (len > 0) {
    456 		lbn = lblkno(fs, off);
    457 		bsize = min(fs->fs_bsize, blkoff(fs, off) + len);
    458 
    459 
    460 		if ((error = ffs_balloc(ip, lbn, bsize, cred, NULL, &blkno,
    461 					flags, &alloced))) {
    462 			return error;
    463 		}
    464 
    465 		/*
    466 		 * if the block was freshly allocated then we can
    467 		 * allocate the pages now and set their blknos.
    468 		 */
    469 
    470 		if (alloced) {
    471 			uvm_vnp_setpageblknos(ITOV(ip), off, len, blkno,
    472 					      UFP_ALL, (off < pagestart ||
    473 							off + len > pageend));
    474 		}
    475 
    476 		delta = fs->fs_bsize - blkoff(fs, off);
    477 		len -= delta;
    478 		off += delta;
    479 	}
    480 	return 0;
    481 }
    482