|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. ! 3: * ! 4: * @APPLE_LICENSE_HEADER_START@ ! 5: * ! 6: * The contents of this file constitute Original Code as defined in and ! 7: * are subject to the Apple Public Source License Version 1.1 (the ! 8: * "License"). You may not use this file except in compliance with the ! 9: * License. Please obtain a copy of the License at ! 10: * http://www.apple.com/publicsource and read it before using this file. ! 11: * ! 12: * This Original Code and all software distributed under the License are ! 13: * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER ! 14: * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, ! 15: * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, ! 16: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the ! 17: * License for the specific language governing rights and limitations ! 18: * under the License. ! 19: * ! 20: * @APPLE_LICENSE_HEADER_END@ ! 21: */ ! 22: /* $NetBSD: lfs_balloc.c,v 1.2 1994/06/29 06:46:49 cgd Exp $ */ ! 23: ! 24: /* ! 25: * Copyright (c) 1989, 1991, 1993 ! 26: * The Regents of the University of California. All rights reserved. ! 27: * ! 28: * Redistribution and use in source and binary forms, with or without ! 29: * modification, are permitted provided that the following conditions ! 30: * are met: ! 31: * 1. Redistributions of source code must retain the above copyright ! 32: * notice, this list of conditions and the following disclaimer. ! 33: * 2. Redistributions in binary form must reproduce the above copyright ! 34: * notice, this list of conditions and the following disclaimer in the ! 35: * documentation and/or other materials provided with the distribution. ! 36: * 3. All advertising materials mentioning features or use of this software ! 37: * must display the following acknowledgement: ! 38: * This product includes software developed by the University of ! 39: * California, Berkeley and its contributors. ! 40: * 4. Neither the name of the University nor the names of its contributors ! 41: * may be used to endorse or promote products derived from this software ! 42: * without specific prior written permission. ! 43: * ! 44: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ! 45: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ! 46: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ! 47: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE ! 48: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ! 49: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ! 50: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ! 51: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT ! 52: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY ! 53: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ! 54: * SUCH DAMAGE. ! 55: * ! 56: * @(#)lfs_balloc.c 8.1 (Berkeley) 6/11/93 ! 57: */ ! 58: #include <sys/param.h> ! 59: #include <sys/buf.h> ! 60: #include <sys/proc.h> ! 61: #include <sys/vnode.h> ! 62: #include <sys/mount.h> ! 63: #include <sys/resourcevar.h> ! 64: #include <sys/trace.h> ! 65: ! 66: #include <miscfs/specfs/specdev.h> ! 67: ! 68: #include <ufs/ufs/quota.h> ! 69: #include <ufs/ufs/inode.h> ! 70: #include <ufs/ufs/ufsmount.h> ! 71: ! 72: #include <ufs/lfs/lfs.h> ! 73: #include <ufs/lfs/lfs_extern.h> ! 74: ! 75: int ! 76: lfs_balloc(vp, iosize, lbn, bpp) ! 77: struct vnode *vp; ! 78: u_long iosize; ! 79: daddr_t lbn; ! 80: struct buf **bpp; ! 81: { ! 82: struct buf *ibp, *bp; ! 83: struct inode *ip; ! 84: struct lfs *fs; ! 85: struct indir indirs[NIADDR+2]; ! 86: daddr_t daddr; ! 87: int bb, error, i, num; ! 88: ! 89: ip = VTOI(vp); ! 90: fs = ip->i_lfs; ! 91: ! 92: /* ! 93: * Three cases: it's a block beyond the end of file, it's a block in ! 94: * the file that may or may not have been assigned a disk address or ! 95: * we're writing an entire block. Note, if the daddr is unassigned, ! 96: * the block might still have existed in the cache (if it was read ! 97: * or written earlier). If it did, make sure we don't count it as a ! 98: * new block or zero out its contents. If it did not, make sure ! 99: * we allocate any necessary indirect blocks. ! 100: */ ! 101: ! 102: *bpp = NULL; ! 103: if (error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL )) ! 104: return (error); ! 105: ! 106: *bpp = bp = getblk(vp, lbn, fs->lfs_bsize, 0, 0); ! 107: bb = VFSTOUFS(vp->v_mount)->um_seqinc; ! 108: if (daddr == UNASSIGNED) ! 109: /* May need to allocate indirect blocks */ ! 110: for (i = 1; i < num; ++i) ! 111: if (!indirs[i].in_exists) { ! 112: ibp = ! 113: getblk(vp, indirs[i].in_lbn, fs->lfs_bsize, ! 114: 0, 0); ! 115: if (!(ibp->b_flags & (B_DONE | B_DELWRI))) { ! 116: if (!ISSPACE(fs, bb, curproc->p_ucred)){ ! 117: ibp->b_flags |= B_INVAL; ! 118: brelse(ibp); ! 119: error = ENOSPC; ! 120: } else { ! 121: ip->i_blocks += bb; ! 122: ip->i_lfs->lfs_bfree -= bb; ! 123: clrbuf(ibp); ! 124: error = VOP_BWRITE(ibp); ! 125: } ! 126: } else ! 127: panic ("Indirect block should not exist"); ! 128: } ! 129: if (error) { ! 130: if (bp) ! 131: brelse(bp); ! 132: return(error); ! 133: } ! 134: ! 135: ! 136: /* Now, we may need to allocate the data block */ ! 137: if (!(bp->b_flags & (B_CACHE | B_DONE | B_DELWRI))) { ! 138: if (daddr == UNASSIGNED) ! 139: if (!ISSPACE(fs, bb, curproc->p_ucred)) { ! 140: bp->b_flags |= B_INVAL; ! 141: brelse(bp); ! 142: return(ENOSPC); ! 143: } else { ! 144: ip->i_blocks += bb; ! 145: ip->i_lfs->lfs_bfree -= bb; ! 146: if (iosize != fs->lfs_bsize) ! 147: clrbuf(bp); ! 148: } ! 149: else if (iosize == fs->lfs_bsize) ! 150: bp->b_blkno = daddr; /* Skip the I/O */ ! 151: else { ! 152: bp->b_blkno = daddr; ! 153: bp->b_flags |= B_READ; ! 154: VOP_STRATEGY(bp); ! 155: return(biowait(bp)); ! 156: } ! 157: } ! 158: return (error); ! 159: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.