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 */ 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/buf.h> 39 #include <sys/proc.h> 40 #include <sys/file.h> 41 #include <sys/vnode.h> 42 43 #include <vm/vm.h> 44 45 #include <ufs/ufs/quota.h> 46 #include <ufs/ufs/inode.h> 47 #include <ufs/ufs/ufs_extern.h> 48 49 #include <ufs/ffs/fs.h> 50 #include <ufs/ffs/ffs_extern.h> 51 52 /* 53 * Balloc defines the structure of file system storage 54 * by allocating the physical blocks on a device given 55 * the inode and the logical block number in a file. 56 */ 57 int 58 ffs_balloc(ip, bn, size, cred, bpp, flags) 59 register struct inode *ip; 60 register daddr_t bn; 61 int size; 62 struct ucred *cred; 63 struct buf **bpp; 64 int flags; 65 { 66 register struct fs *fs; 67 register daddr_t nb; 68 struct buf *bp, *nbp; 69 struct vnode *vp = ITOV(ip); 70 struct indir indirs[NIADDR + 2]; 71 daddr_t newb, lbn, *bap, pref; 72 int osize, nsize, num, i, error; 73 74 *bpp = NULL; 75 if (bn < 0) 76 return (EFBIG); 77 fs = ip->i_fs; 78 lbn = bn; 79 80 /* 81 * If the next write will extend the file into a new block, 82 * and the file is currently composed of a fragment 83 * this fragment has to be extended to be a full block. 84 */ 85 nb = lblkno(fs, ip->i_size); 86 if (nb < NDADDR && nb < bn) { 87 osize = blksize(fs, ip, nb); 88 if (osize < fs->fs_bsize && osize > 0) { 89 error = ffs_realloccg(ip, nb, 90 ffs_blkpref(ip, nb, (int)nb, &ip->i_db[0]), 91 osize, (int)fs->fs_bsize, cred, &bp); 92 if (error) 93 return (error); 94 ip->i_size = (nb + 1) * fs->fs_bsize; 95 vnode_pager_setsize(vp, (u_long)ip->i_size); 96 ip->i_db[nb] = dbtofsb(fs, bp->b_blkno); 97 ip->i_flag |= IN_CHANGE | IN_UPDATE; 98 if (flags & B_SYNC) 99 bwrite(bp); 100 else 101 bawrite(bp); 102 } 103 } 104 /* 105 * The first NDADDR blocks are direct blocks 106 */ 107 if (bn < NDADDR) { 108 nb = ip->i_db[bn]; 109 if (nb != 0 && ip->i_size >= (bn + 1) * fs->fs_bsize) { 110 error = bread(vp, bn, fs->fs_bsize, NOCRED, &bp); 111 if (error) { 112 brelse(bp); 113 return (error); 114 } 115 *bpp = bp; 116 return (0); 117 } 118 if (nb != 0) { 119 /* 120 * Consider need to reallocate a fragment. 121 */ 122 osize = fragroundup(fs, blkoff(fs, ip->i_size)); 123 nsize = fragroundup(fs, size); 124 if (nsize <= osize) { 125 error = bread(vp, bn, osize, NOCRED, &bp); 126 if (error) { 127 brelse(bp); 128 return (error); 129 } 130 } else { 131 error = ffs_realloccg(ip, bn, 132 ffs_blkpref(ip, bn, (int)bn, &ip->i_db[0]), 133 osize, nsize, cred, &bp); 134 if (error) 135 return (error); 136 } 137 } else { 138 if (ip->i_size < (bn + 1) * fs->fs_bsize) 139 nsize = fragroundup(fs, size); 140 else 141 nsize = fs->fs_bsize; 142 error = ffs_alloc(ip, bn, 143 ffs_blkpref(ip, bn, (int)bn, &ip->i_db[0]), 144 nsize, cred, &newb); 145 if (error) 146 return (error); 147 bp = getblk(vp, bn, nsize, 0, 0); 148 bp->b_blkno = fsbtodb(fs, newb); 149 if (flags & B_CLRBUF) 150 clrbuf(bp); 151 } 152 ip->i_db[bn] = dbtofsb(fs, bp->b_blkno); 153 ip->i_flag |= IN_CHANGE | IN_UPDATE; 154 *bpp = bp; 155 return (0); 156 } 157 /* 158 * Determine the number of levels of indirection. 159 */ 160 pref = 0; 161 if (error = ufs_getlbns(vp, bn, indirs, &num)) 162 return(error); 163 #ifdef DIAGNOSTIC 164 if (num < 1) 165 panic ("ffs_balloc: ufs_bmaparray returned indirect block\n"); 166 #endif 167 /* 168 * Fetch the first indirect block allocating if necessary. 169 */ 170 --num; 171 nb = ip->i_ib[indirs[0].in_off]; 172 if (nb == 0) { 173 pref = ffs_blkpref(ip, lbn, 0, (daddr_t *)0); 174 if (error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, 175 cred, &newb)) 176 return (error); 177 nb = newb; 178 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0); 179 bp->b_blkno = fsbtodb(fs, newb); 180 clrbuf(bp); 181 /* 182 * Write synchronously so that indirect blocks 183 * never point at garbage. 184 */ 185 if (error = bwrite(bp)) { 186 ffs_blkfree(ip, nb, fs->fs_bsize); 187 return (error); 188 } 189 ip->i_ib[indirs[0].in_off] = newb; 190 ip->i_flag |= IN_CHANGE | IN_UPDATE; 191 } 192 /* 193 * Fetch through the indirect blocks, allocating as necessary. 194 */ 195 for (i = 1;;) { 196 error = bread(vp, 197 indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp); 198 if (error) { 199 brelse(bp); 200 return (error); 201 } 202 bap = (daddr_t *)bp->b_data; 203 nb = bap[indirs[i].in_off]; 204 if (i == num) 205 break; 206 i += 1; 207 if (nb != 0) { 208 brelse(bp); 209 continue; 210 } 211 if (pref == 0) 212 pref = ffs_blkpref(ip, lbn, 0, (daddr_t *)0); 213 if (error = 214 ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred, &newb)) { 215 brelse(bp); 216 return (error); 217 } 218 nb = newb; 219 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0); 220 nbp->b_blkno = fsbtodb(fs, nb); 221 clrbuf(nbp); 222 /* 223 * Write synchronously so that indirect blocks 224 * never point at garbage. 225 */ 226 if (error = bwrite(nbp)) { 227 ffs_blkfree(ip, nb, fs->fs_bsize); 228 brelse(bp); 229 return (error); 230 } 231 bap[indirs[i - 1].in_off] = nb; 232 /* 233 * If required, write synchronously, otherwise use 234 * delayed write. 235 */ 236 if (flags & B_SYNC) { 237 bwrite(bp); 238 } else { 239 bdwrite(bp); 240 } 241 } 242 /* 243 * Get the data block, allocating if necessary. 244 */ 245 if (nb == 0) { 246 pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]); 247 if (error = ffs_alloc(ip, 248 lbn, pref, (int)fs->fs_bsize, cred, &newb)) { 249 brelse(bp); 250 return (error); 251 } 252 nb = newb; 253 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0); 254 nbp->b_blkno = fsbtodb(fs, nb); 255 if (flags & B_CLRBUF) 256 clrbuf(nbp); 257 bap[indirs[i].in_off] = nb; 258 /* 259 * If required, write synchronously, otherwise use 260 * delayed write. 261 */ 262 if (flags & B_SYNC) { 263 bwrite(bp); 264 } else { 265 bdwrite(bp); 266 } 267 *bpp = nbp; 268 return (0); 269 } 270 brelse(bp); 271 if (flags & B_CLRBUF) { 272 error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp); 273 if (error) { 274 brelse(nbp); 275 return (error); 276 } 277 } else { 278 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0); 279 nbp->b_blkno = fsbtodb(fs, nb); 280 } 281 *bpp = nbp; 282 return (0); 283 } 284