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