1 /* $NetBSD: ffs_balloc.c,v 1.13 1998/10/27 21:32:58 mycroft 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_extern.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, flags) 73 register struct inode *ip; 74 register ufs_daddr_t lbn; 75 int size; 76 struct ucred *cred; 77 struct buf **bpp; 78 int flags; 79 { 80 register struct fs *fs; 81 register ufs_daddr_t nb; 82 struct buf *bp, *nbp; 83 struct vnode *vp = ITOV(ip); 84 struct indir indirs[NIADDR + 2]; 85 ufs_daddr_t newb, *bap, pref; 86 int deallocated, osize, nsize, num, i, error; 87 ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1]; 88 89 *bpp = NULL; 90 if (lbn < 0) 91 return (EFBIG); 92 fs = ip->i_fs; 93 94 /* 95 * If the next write will extend the file into a new block, 96 * and the file is currently composed of a fragment 97 * this fragment has to be extended to be a full block. 98 */ 99 nb = lblkno(fs, ip->i_ffs_size); 100 if (nb < NDADDR && nb < lbn) { 101 osize = blksize(fs, ip, nb); 102 if (osize < fs->fs_bsize && osize > 0) { 103 error = ffs_realloccg(ip, nb, 104 ffs_blkpref(ip, nb, (int)nb, &ip->i_ffs_db[0]), 105 osize, (int)fs->fs_bsize, cred, &bp); 106 if (error) 107 return (error); 108 ip->i_ffs_size = (nb + 1) * fs->fs_bsize; 109 #if defined(UVM) 110 uvm_vnp_setsize(vp, ip->i_ffs_size); 111 #else 112 vnode_pager_setsize(vp, ip->i_ffs_size); 113 #endif 114 ip->i_ffs_db[nb] = ufs_rw32(dbtofsb(fs, bp->b_blkno), 115 UFS_MPNEEDSWAP(vp->v_mount)); 116 ip->i_flag |= IN_CHANGE | IN_UPDATE; 117 if (flags & B_SYNC) 118 bwrite(bp); 119 else 120 bawrite(bp); 121 } 122 } 123 /* 124 * The first NDADDR blocks are direct blocks 125 */ 126 if (lbn < NDADDR) { 127 nb = ufs_rw32(ip->i_ffs_db[lbn], UFS_MPNEEDSWAP(vp->v_mount)); 128 if (nb != 0 && ip->i_ffs_size >= (lbn + 1) * fs->fs_bsize) { 129 error = bread(vp, lbn, fs->fs_bsize, NOCRED, &bp); 130 if (error) { 131 brelse(bp); 132 return (error); 133 } 134 *bpp = bp; 135 return (0); 136 } 137 if (nb != 0) { 138 /* 139 * Consider need to reallocate a fragment. 140 */ 141 osize = fragroundup(fs, blkoff(fs, ip->i_ffs_size)); 142 nsize = fragroundup(fs, size); 143 if (nsize <= osize) { 144 error = bread(vp, lbn, osize, NOCRED, &bp); 145 if (error) { 146 brelse(bp); 147 return (error); 148 } 149 } else { 150 error = ffs_realloccg(ip, lbn, 151 ffs_blkpref(ip, lbn, (int)lbn, 152 &ip->i_ffs_db[0]), osize, nsize, cred, 153 &bp); 154 if (error) 155 return (error); 156 } 157 } else { 158 if (ip->i_ffs_size < (lbn + 1) * fs->fs_bsize) 159 nsize = fragroundup(fs, size); 160 else 161 nsize = fs->fs_bsize; 162 error = ffs_alloc(ip, lbn, 163 ffs_blkpref(ip, lbn, (int)lbn, &ip->i_ffs_db[0]), 164 nsize, cred, &newb); 165 if (error) 166 return (error); 167 bp = getblk(vp, lbn, nsize, 0, 0); 168 bp->b_blkno = fsbtodb(fs, newb); 169 if (flags & B_CLRBUF) 170 clrbuf(bp); 171 } 172 ip->i_ffs_db[lbn] = ufs_rw32(dbtofsb(fs, bp->b_blkno), 173 UFS_MPNEEDSWAP(vp->v_mount)); 174 ip->i_flag |= IN_CHANGE | IN_UPDATE; 175 *bpp = bp; 176 return (0); 177 } 178 /* 179 * Determine the number of levels of indirection. 180 */ 181 pref = 0; 182 if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0) 183 return(error); 184 #ifdef DIAGNOSTIC 185 if (num < 1) 186 panic ("ffs_balloc: ufs_bmaparray returned indirect block\n"); 187 #endif 188 /* 189 * Fetch the first indirect block allocating if necessary. 190 */ 191 --num; 192 nb = ufs_rw32(ip->i_ffs_ib[indirs[0].in_off], 193 UFS_MPNEEDSWAP(vp->v_mount)); 194 allocib = NULL; 195 allocblk = allociblk; 196 if (nb == 0) { 197 pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0); 198 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, 199 cred, &newb); 200 if (error) 201 return (error); 202 nb = newb; 203 *allocblk++ = nb; 204 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0); 205 bp->b_blkno = fsbtodb(fs, nb); 206 clrbuf(bp); 207 /* 208 * Write synchronously so that indirect blocks 209 * never point at garbage. 210 */ 211 if ((error = bwrite(bp)) != 0) 212 goto fail; 213 allocib = &ip->i_ffs_ib[indirs[0].in_off]; 214 *allocib = ufs_rw32(nb, UFS_MPNEEDSWAP(vp->v_mount)); 215 ip->i_flag |= IN_CHANGE | IN_UPDATE; 216 } 217 /* 218 * Fetch through the indirect blocks, allocating as necessary. 219 */ 220 for (i = 1;;) { 221 error = bread(vp, 222 indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp); 223 if (error) { 224 brelse(bp); 225 goto fail; 226 } 227 bap = (ufs_daddr_t *)bp->b_data; 228 nb = ufs_rw32(bap[indirs[i].in_off], 229 UFS_MPNEEDSWAP(vp->v_mount)); 230 if (i == num) 231 break; 232 i += 1; 233 if (nb != 0) { 234 brelse(bp); 235 continue; 236 } 237 if (pref == 0) 238 pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0); 239 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred, 240 &newb); 241 if (error) { 242 brelse(bp); 243 goto fail; 244 } 245 nb = newb; 246 *allocblk++ = nb; 247 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0); 248 nbp->b_blkno = fsbtodb(fs, nb); 249 clrbuf(nbp); 250 /* 251 * Write synchronously so that indirect blocks 252 * never point at garbage. 253 */ 254 if ((error = bwrite(nbp)) != 0) { 255 brelse(bp); 256 goto fail; 257 } 258 bap[indirs[i - 1].in_off] = ufs_rw32(nb, 259 UFS_MPNEEDSWAP(vp->v_mount)); 260 /* 261 * If required, write synchronously, otherwise use 262 * delayed write. 263 */ 264 if (flags & B_SYNC) { 265 bwrite(bp); 266 } else { 267 bdwrite(bp); 268 } 269 } 270 /* 271 * Get the data block, allocating if necessary. 272 */ 273 if (nb == 0) { 274 pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]); 275 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred, 276 &newb); 277 if (error) { 278 brelse(bp); 279 goto fail; 280 } 281 nb = newb; 282 *allocblk++ = nb; 283 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0); 284 nbp->b_blkno = fsbtodb(fs, nb); 285 if (flags & B_CLRBUF) 286 clrbuf(nbp); 287 bap[indirs[i].in_off] = ufs_rw32(nb, 288 UFS_MPNEEDSWAP(vp->v_mount)); 289 /* 290 * If required, write synchronously, otherwise use 291 * delayed write. 292 */ 293 if (flags & B_SYNC) { 294 bwrite(bp); 295 } else { 296 bdwrite(bp); 297 } 298 *bpp = nbp; 299 return (0); 300 } 301 brelse(bp); 302 if (flags & B_CLRBUF) { 303 error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp); 304 if (error) { 305 brelse(nbp); 306 goto fail; 307 } 308 } else { 309 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0); 310 nbp->b_blkno = fsbtodb(fs, nb); 311 } 312 *bpp = nbp; 313 return (0); 314 fail: 315 /* 316 * If we have failed part way through block allocation, we 317 * have to deallocate any indirect blocks that we have allocated. 318 */ 319 for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) { 320 ffs_blkfree(ip, *blkp, fs->fs_bsize); 321 deallocated += fs->fs_bsize; 322 } 323 if (allocib != NULL) 324 *allocib = 0; 325 if (deallocated) { 326 #ifdef QUOTA 327 /* 328 * Restore user's disk quota because allocation failed. 329 */ 330 (void)chkdq(ip, (long)-btodb(deallocated), cred, FORCE); 331 #endif 332 ip->i_ffs_blocks -= btodb(deallocated); 333 ip->i_flag |= IN_CHANGE | IN_UPDATE; 334 } 335 return (error); 336 } 337