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