ffs_balloc.c revision 55799
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.8 (Berkeley) 6/16/95 34 * $FreeBSD: head/sys/ufs/ffs/ffs_balloc.c 55799 2000-01-11 08:27:00Z mckusick $ 35 */ 36 37#include <sys/param.h> 38#include <sys/systm.h> 39#include <sys/buf.h> 40#include <sys/lock.h> 41#include <sys/mount.h> 42#include <sys/vnode.h> 43 44#include <ufs/ufs/quota.h> 45#include <ufs/ufs/inode.h> 46#include <ufs/ufs/ufs_extern.h> 47 48#include <ufs/ffs/fs.h> 49#include <ufs/ffs/ffs_extern.h> 50 51/* 52 * Balloc defines the structure of file system storage 53 * by allocating the physical blocks on a device given 54 * the inode and the logical block number in a file. 55 */ 56int 57ffs_balloc(ap) 58 struct vop_balloc_args /* { 59 struct vnode *a_vp; 60 ufs_daddr_t a_lbn; 61 int a_size; 62 struct ucred *a_cred; 63 int a_flags; 64 struct buf *a_bpp; 65 } */ *ap; 66{ 67 struct inode *ip; 68 ufs_daddr_t lbn; 69 int size; 70 struct ucred *cred; 71 int flags; 72 struct fs *fs; 73 ufs_daddr_t nb; 74 struct buf *bp, *nbp; 75 struct vnode *vp; 76 struct indir indirs[NIADDR + 2]; 77 ufs_daddr_t newb, *bap, pref; 78 int deallocated, osize, nsize, num, i, error; 79 ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1]; 80 struct proc *p = curproc; /* XXX */ 81 82 vp = ap->a_vp; 83 ip = VTOI(vp); 84 fs = ip->i_fs; 85 lbn = lblkno(fs, ap->a_startoffset); 86 size = blkoff(fs, ap->a_startoffset) + ap->a_size; 87 if (size > fs->fs_bsize) 88 panic("ffs_balloc: blk too big"); 89 *ap->a_bpp = NULL; 90 if (lbn < 0) 91 return (EFBIG); 92 cred = ap->a_cred; 93 flags = ap->a_flags; 94 95 /* 96 * If the next write will extend the file into a new block, 97 * and the file is currently composed of a fragment 98 * this fragment has to be extended to be a full block. 99 */ 100 nb = lblkno(fs, ip->i_size); 101 if (nb < NDADDR && nb < lbn) { 102 osize = blksize(fs, ip, nb); 103 if (osize < fs->fs_bsize && osize > 0) { 104 error = ffs_realloccg(ip, nb, 105 ffs_blkpref(ip, nb, (int)nb, &ip->i_db[0]), 106 osize, (int)fs->fs_bsize, cred, &bp); 107 if (error) 108 return (error); 109 if (DOINGSOFTDEP(vp)) 110 softdep_setup_allocdirect(ip, nb, 111 dbtofsb(fs, bp->b_blkno), ip->i_db[nb], 112 fs->fs_bsize, osize, bp); 113 ip->i_size = smalllblktosize(fs, nb + 1); 114 ip->i_db[nb] = dbtofsb(fs, bp->b_blkno); 115 ip->i_flag |= IN_CHANGE | IN_UPDATE; 116 if (flags & B_SYNC) 117 bwrite(bp); 118 else 119 bawrite(bp); 120 } 121 } 122 /* 123 * The first NDADDR blocks are direct blocks 124 */ 125 if (lbn < NDADDR) { 126 nb = ip->i_db[lbn]; 127 if (nb != 0 && ip->i_size >= smalllblktosize(fs, lbn + 1)) { 128 error = bread(vp, lbn, fs->fs_bsize, NOCRED, &bp); 129 if (error) { 130 brelse(bp); 131 return (error); 132 } 133 bp->b_blkno = fsbtodb(fs, nb); 134 *ap->a_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_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 bp->b_blkno = fsbtodb(fs, nb); 150 } else { 151 error = ffs_realloccg(ip, lbn, 152 ffs_blkpref(ip, lbn, (int)lbn, 153 &ip->i_db[0]), osize, nsize, cred, &bp); 154 if (error) 155 return (error); 156 if (DOINGSOFTDEP(vp)) 157 softdep_setup_allocdirect(ip, lbn, 158 dbtofsb(fs, bp->b_blkno), nb, 159 nsize, osize, bp); 160 } 161 } else { 162 if (ip->i_size < smalllblktosize(fs, lbn + 1)) 163 nsize = fragroundup(fs, size); 164 else 165 nsize = fs->fs_bsize; 166 error = ffs_alloc(ip, lbn, 167 ffs_blkpref(ip, lbn, (int)lbn, &ip->i_db[0]), 168 nsize, cred, &newb); 169 if (error) 170 return (error); 171 bp = getblk(vp, lbn, nsize, 0, 0); 172 bp->b_blkno = fsbtodb(fs, newb); 173 if (flags & B_CLRBUF) 174 vfs_bio_clrbuf(bp); 175 if (DOINGSOFTDEP(vp)) 176 softdep_setup_allocdirect(ip, lbn, newb, 0, 177 nsize, 0, bp); 178 } 179 ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno); 180 ip->i_flag |= IN_CHANGE | IN_UPDATE; 181 *ap->a_bpp = bp; 182 return (0); 183 } 184 /* 185 * Determine the number of levels of indirection. 186 */ 187 pref = 0; 188 if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0) 189 return(error); 190#ifdef DIAGNOSTIC 191 if (num < 1) 192 panic ("ffs_balloc: ufs_bmaparray returned indirect block"); 193#endif 194 /* 195 * Fetch the first indirect block allocating if necessary. 196 */ 197 --num; 198 nb = ip->i_ib[indirs[0].in_off]; 199 allocib = NULL; 200 allocblk = allociblk; 201 if (nb == 0) { 202 pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0); 203 if ((error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, 204 cred, &newb)) != 0) 205 return (error); 206 nb = newb; 207 *allocblk++ = nb; 208 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0); 209 bp->b_blkno = fsbtodb(fs, nb); 210 vfs_bio_clrbuf(bp); 211 if (DOINGSOFTDEP(vp)) { 212 softdep_setup_allocdirect(ip, NDADDR + indirs[0].in_off, 213 newb, 0, fs->fs_bsize, 0, bp); 214 bdwrite(bp); 215 } else { 216 /* 217 * Write synchronously so that indirect blocks 218 * never point at garbage. 219 */ 220 if (DOINGASYNC(vp)) 221 bdwrite(bp); 222 else if ((error = bwrite(bp)) != 0) 223 goto fail; 224 } 225 allocib = &ip->i_ib[indirs[0].in_off]; 226 *allocib = nb; 227 ip->i_flag |= IN_CHANGE | IN_UPDATE; 228 } 229 /* 230 * Fetch through the indirect blocks, allocating as necessary. 231 */ 232 for (i = 1;;) { 233 error = bread(vp, 234 indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp); 235 if (error) { 236 brelse(bp); 237 goto fail; 238 } 239 bap = (ufs_daddr_t *)bp->b_data; 240 nb = bap[indirs[i].in_off]; 241 if (i == num) 242 break; 243 i += 1; 244 if (nb != 0) { 245 bqrelse(bp); 246 continue; 247 } 248 if (pref == 0) 249 pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0); 250 if ((error = 251 ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred, &newb)) != 0) { 252 brelse(bp); 253 goto fail; 254 } 255 nb = newb; 256 *allocblk++ = nb; 257 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0); 258 nbp->b_blkno = fsbtodb(fs, nb); 259 vfs_bio_clrbuf(nbp); 260 if (DOINGSOFTDEP(vp)) { 261 softdep_setup_allocindir_meta(nbp, ip, bp, 262 indirs[i - 1].in_off, nb); 263 bdwrite(nbp); 264 } else { 265 /* 266 * Write synchronously so that indirect blocks 267 * never point at garbage. 268 */ 269 if ((error = bwrite(nbp)) != 0) { 270 brelse(bp); 271 goto fail; 272 } 273 } 274 bap[indirs[i - 1].in_off] = nb; 275 /* 276 * If required, write synchronously, otherwise use 277 * delayed write. 278 */ 279 if (flags & B_SYNC) { 280 bwrite(bp); 281 } else { 282 if (bp->b_bufsize == fs->fs_bsize) 283 bp->b_flags |= B_CLUSTEROK; 284 bdwrite(bp); 285 } 286 } 287 /* 288 * Get the data block, allocating if necessary. 289 */ 290 if (nb == 0) { 291 pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]); 292 error = ffs_alloc(ip, 293 lbn, pref, (int)fs->fs_bsize, cred, &newb); 294 if (error) { 295 brelse(bp); 296 goto fail; 297 } 298 nb = newb; 299 *allocblk++ = nb; 300 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0); 301 nbp->b_blkno = fsbtodb(fs, nb); 302 if (flags & B_CLRBUF) 303 vfs_bio_clrbuf(nbp); 304 if (DOINGSOFTDEP(vp)) 305 softdep_setup_allocindir_page(ip, lbn, bp, 306 indirs[i].in_off, nb, 0, nbp); 307 bap[indirs[i].in_off] = nb; 308 /* 309 * If required, write synchronously, otherwise use 310 * delayed write. 311 */ 312 if (flags & B_SYNC) { 313 bwrite(bp); 314 } else { 315 if (bp->b_bufsize == fs->fs_bsize) 316 bp->b_flags |= B_CLUSTEROK; 317 bdwrite(bp); 318 } 319 *ap->a_bpp = nbp; 320 return (0); 321 } 322 brelse(bp); 323 if (flags & B_CLRBUF) { 324 error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp); 325 if (error) { 326 brelse(nbp); 327 goto fail; 328 } 329 } else { 330 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0); 331 nbp->b_blkno = fsbtodb(fs, nb); 332 } 333 *ap->a_bpp = nbp; 334 return (0); 335fail: 336 /* 337 * If we have failed part way through block allocation, we 338 * have to deallocate any indirect blocks that we have allocated. 339 * We have to fsync the file before we start to get rid of all 340 * of its dependencies so that we do not leave them dangling. 341 * We have to sync it at the end so that the soft updates code 342 * does not find any untracked changes. Although this is really 343 * slow, running out of disk space is not expected to be a common 344 * occurence. The error return from fsync is ignored as we already 345 * have an error to return to the user. 346 */ 347 (void) VOP_FSYNC(vp, cred, MNT_WAIT, p); 348 for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) { 349 ffs_blkfree(ip, *blkp, fs->fs_bsize); 350 deallocated += fs->fs_bsize; 351 } 352 if (allocib != NULL) 353 *allocib = 0; 354 if (deallocated) { 355#ifdef QUOTA 356 /* 357 * Restore user's disk quota because allocation failed. 358 */ 359 (void) chkdq(ip, (long)-btodb(deallocated), cred, FORCE); 360#endif 361 ip->i_blocks -= btodb(deallocated); 362 ip->i_flag |= IN_CHANGE | IN_UPDATE; 363 } 364 (void) VOP_FSYNC(vp, cred, MNT_WAIT, p); 365 return (error); 366} 367