1 /*- 2 * modified for Lites 1.1 3 * 4 * Aug 1995, Godmar Back (gback@cs.utah.edu) 5 * University of Utah, Department of Computer Science 6 */ 7 /*- 8 * Copyright (c) 1982, 1986, 1989, 1993 9 * The Regents of the University of California. All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. 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.4 (Berkeley) 9/23/93 36 * $FreeBSD$ 37 */ 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/bio.h> 42 #include <sys/buf.h> 43 #include <sys/lock.h> 44 #include <sys/mount.h> 45 #include <sys/vnode.h> 46 47 #include <fs/ext2fs/fs.h> 48 #include <fs/ext2fs/inode.h> 49 #include <fs/ext2fs/ext2fs.h> 50 #include <fs/ext2fs/ext2_dinode.h> 51 #include <fs/ext2fs/ext2_extern.h> 52 #include <fs/ext2fs/ext2_mount.h> 53 54 static int 55 ext2_ext_balloc(struct inode *ip, uint32_t lbn, int size, 56 struct ucred *cred, struct buf **bpp, int flags) 57 { 58 struct m_ext2fs *fs; 59 struct buf *bp = NULL; 60 struct vnode *vp = ITOV(ip); 61 uint32_t nb; 62 int osize, nsize, blks, error, allocated; 63 64 fs = ip->i_e2fs; 65 blks = howmany(size, fs->e2fs_bsize); 66 67 error = ext4_ext_get_blocks(ip, lbn, blks, cred, NULL, &allocated, &nb); 68 if (error) 69 return (error); 70 71 if (allocated) { 72 if (ip->i_size < (lbn + 1) * fs->e2fs_bsize) 73 nsize = fragroundup(fs, size); 74 else 75 nsize = fs->e2fs_bsize; 76 77 bp = getblk(vp, lbn, nsize, 0, 0, 0); 78 if(!bp) 79 return (EIO); 80 81 bp->b_blkno = fsbtodb(fs, nb); 82 if (flags & BA_CLRBUF) 83 vfs_bio_clrbuf(bp); 84 } else { 85 if (ip->i_size >= (lbn + 1) * fs->e2fs_bsize) { 86 87 error = bread(vp, lbn, fs->e2fs_bsize, NOCRED, &bp); 88 if (error) { 89 brelse(bp); 90 return (error); 91 } 92 bp->b_blkno = fsbtodb(fs, nb); 93 *bpp = bp; 94 return (0); 95 } 96 97 /* 98 * Consider need to reallocate a fragment. 99 */ 100 osize = fragroundup(fs, blkoff(fs, ip->i_size)); 101 nsize = fragroundup(fs, size); 102 if (nsize <= osize) { 103 error = bread(vp, lbn, osize, NOCRED, &bp); 104 if (error) { 105 brelse(bp); 106 return (error); 107 } 108 bp->b_blkno = fsbtodb(fs, nb); 109 } else { 110 error = bread(vp, lbn, fs->e2fs_bsize, NOCRED, &bp); 111 if (error) { 112 brelse(bp); 113 return (error); 114 } 115 bp->b_blkno = fsbtodb(fs, nb); 116 } 117 } 118 119 *bpp = bp; 120 121 return (error); 122 } 123 124 /* 125 * Balloc defines the structure of filesystem storage 126 * by allocating the physical blocks on a device given 127 * the inode and the logical block number in a file. 128 */ 129 int 130 ext2_balloc(struct inode *ip, e2fs_lbn_t lbn, int size, struct ucred *cred, 131 struct buf **bpp, int flags) 132 { 133 struct m_ext2fs *fs; 134 struct ext2mount *ump; 135 struct buf *bp, *nbp; 136 struct vnode *vp = ITOV(ip); 137 struct indir indirs[EXT2_NIADDR + 2]; 138 e4fs_daddr_t nb, newb; 139 e2fs_daddr_t *bap, pref; 140 int osize, nsize, num, i, error; 141 142 *bpp = NULL; 143 if (lbn < 0) 144 return (EFBIG); 145 fs = ip->i_e2fs; 146 ump = ip->i_ump; 147 148 /* 149 * check if this is a sequential block allocation. 150 * If so, increment next_alloc fields to allow ext2_blkpref 151 * to make a good guess 152 */ 153 if (lbn == ip->i_next_alloc_block + 1) { 154 ip->i_next_alloc_block++; 155 ip->i_next_alloc_goal++; 156 } 157 158 if (ip->i_flag & IN_E4EXTENTS) 159 return (ext2_ext_balloc(ip, lbn, size, cred, bpp, flags)); 160 161 /* 162 * The first EXT2_NDADDR blocks are direct blocks 163 */ 164 if (lbn < EXT2_NDADDR) { 165 nb = ip->i_db[lbn]; 166 /* 167 * no new block is to be allocated, and no need to expand 168 * the file 169 */ 170 if (nb != 0 && ip->i_size >= (lbn + 1) * fs->e2fs_bsize) { 171 error = bread(vp, lbn, fs->e2fs_bsize, NOCRED, &bp); 172 if (error) { 173 brelse(bp); 174 return (error); 175 } 176 bp->b_blkno = fsbtodb(fs, nb); 177 *bpp = bp; 178 return (0); 179 } 180 if (nb != 0) { 181 /* 182 * Consider need to reallocate a fragment. 183 */ 184 osize = fragroundup(fs, blkoff(fs, ip->i_size)); 185 nsize = fragroundup(fs, size); 186 if (nsize <= osize) { 187 error = bread(vp, lbn, osize, NOCRED, &bp); 188 if (error) { 189 brelse(bp); 190 return (error); 191 } 192 bp->b_blkno = fsbtodb(fs, nb); 193 } else { 194 /* 195 * Godmar thinks: this shouldn't happen w/o 196 * fragments 197 */ 198 printf("nsize %d(%d) > osize %d(%d) nb %d\n", 199 (int)nsize, (int)size, (int)osize, 200 (int)ip->i_size, (int)nb); 201 panic( 202 "ext2_balloc: Something is terribly wrong"); 203 /* 204 * please note there haven't been any changes from here on - 205 * FFS seems to work. 206 */ 207 } 208 } else { 209 if (ip->i_size < (lbn + 1) * fs->e2fs_bsize) 210 nsize = fragroundup(fs, size); 211 else 212 nsize = fs->e2fs_bsize; 213 EXT2_LOCK(ump); 214 error = ext2_alloc(ip, lbn, 215 ext2_blkpref(ip, lbn, (int)lbn, &ip->i_db[0], 0), 216 nsize, cred, &newb); 217 if (error) 218 return (error); 219 bp = getblk(vp, lbn, nsize, 0, 0, 0); 220 bp->b_blkno = fsbtodb(fs, newb); 221 if (flags & BA_CLRBUF) 222 vfs_bio_clrbuf(bp); 223 } 224 ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno); 225 ip->i_flag |= IN_CHANGE | IN_UPDATE; 226 *bpp = bp; 227 return (0); 228 } 229 /* 230 * Determine the number of levels of indirection. 231 */ 232 pref = 0; 233 if ((error = ext2_getlbns(vp, lbn, indirs, &num)) != 0) 234 return (error); 235 #ifdef INVARIANTS 236 if (num < 1) 237 panic("ext2_balloc: ext2_getlbns returned indirect block"); 238 #endif 239 /* 240 * Fetch the first indirect block allocating if necessary. 241 */ 242 --num; 243 nb = ip->i_ib[indirs[0].in_off]; 244 if (nb == 0) { 245 EXT2_LOCK(ump); 246 pref = ext2_blkpref(ip, lbn, indirs[0].in_off + 247 EXT2_NDIR_BLOCKS, &ip->i_db[0], 0); 248 if ((error = ext2_alloc(ip, lbn, pref, fs->e2fs_bsize, cred, 249 &newb))) 250 return (error); 251 nb = newb; 252 bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0, 0); 253 bp->b_blkno = fsbtodb(fs, newb); 254 vfs_bio_clrbuf(bp); 255 /* 256 * Write synchronously so that indirect blocks 257 * never point at garbage. 258 */ 259 if ((error = bwrite(bp)) != 0) { 260 ext2_blkfree(ip, nb, fs->e2fs_bsize); 261 return (error); 262 } 263 ip->i_ib[indirs[0].in_off] = newb; 264 ip->i_flag |= IN_CHANGE | IN_UPDATE; 265 } 266 /* 267 * Fetch through the indirect blocks, allocating as necessary. 268 */ 269 for (i = 1;;) { 270 error = bread(vp, 271 indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, &bp); 272 if (error) { 273 brelse(bp); 274 return (error); 275 } 276 bap = (e2fs_daddr_t *)bp->b_data; 277 nb = bap[indirs[i].in_off]; 278 if (i == num) 279 break; 280 i += 1; 281 if (nb != 0) { 282 bqrelse(bp); 283 continue; 284 } 285 EXT2_LOCK(ump); 286 if (pref == 0) 287 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, bap, 288 bp->b_lblkno); 289 error = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newb); 290 if (error) { 291 brelse(bp); 292 return (error); 293 } 294 nb = newb; 295 nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0, 0); 296 nbp->b_blkno = fsbtodb(fs, nb); 297 vfs_bio_clrbuf(nbp); 298 /* 299 * Write synchronously so that indirect blocks 300 * never point at garbage. 301 */ 302 if ((error = bwrite(nbp)) != 0) { 303 ext2_blkfree(ip, nb, fs->e2fs_bsize); 304 EXT2_UNLOCK(ump); 305 brelse(bp); 306 return (error); 307 } 308 bap[indirs[i - 1].in_off] = nb; 309 /* 310 * If required, write synchronously, otherwise use 311 * delayed write. 312 */ 313 if (flags & IO_SYNC) { 314 bwrite(bp); 315 } else { 316 if (bp->b_bufsize == fs->e2fs_bsize) 317 bp->b_flags |= B_CLUSTEROK; 318 bdwrite(bp); 319 } 320 } 321 /* 322 * Get the data block, allocating if necessary. 323 */ 324 if (nb == 0) { 325 EXT2_LOCK(ump); 326 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, &bap[0], 327 bp->b_lblkno); 328 if ((error = ext2_alloc(ip, 329 lbn, pref, (int)fs->e2fs_bsize, cred, &newb)) != 0) { 330 brelse(bp); 331 return (error); 332 } 333 nb = newb; 334 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0); 335 nbp->b_blkno = fsbtodb(fs, nb); 336 if (flags & BA_CLRBUF) 337 vfs_bio_clrbuf(nbp); 338 bap[indirs[i].in_off] = nb; 339 /* 340 * If required, write synchronously, otherwise use 341 * delayed write. 342 */ 343 if (flags & IO_SYNC) { 344 bwrite(bp); 345 } else { 346 if (bp->b_bufsize == fs->e2fs_bsize) 347 bp->b_flags |= B_CLUSTEROK; 348 bdwrite(bp); 349 } 350 *bpp = nbp; 351 return (0); 352 } 353 brelse(bp); 354 if (flags & BA_CLRBUF) { 355 int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT; 356 357 if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) { 358 error = cluster_read(vp, ip->i_size, lbn, 359 (int)fs->e2fs_bsize, NOCRED, 360 MAXBSIZE, seqcount, 0, &nbp); 361 } else { 362 error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED, &nbp); 363 } 364 if (error) { 365 brelse(nbp); 366 return (error); 367 } 368 } else { 369 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0); 370 nbp->b_blkno = fsbtodb(fs, nb); 371 } 372 *bpp = nbp; 373 return (0); 374 } 375