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 * SPDX-License-Identifier: BSD-3-Clause 9 * 10 * Copyright (c) 1982, 1986, 1989, 1993 11 * The Regents of the University of California. All rights reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)ffs_balloc.c 8.4 (Berkeley) 9/23/93 38 * $FreeBSD$ 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/endian.h> 44 #include <sys/bio.h> 45 #include <sys/buf.h> 46 #include <sys/limits.h> 47 #include <sys/lock.h> 48 #include <sys/mount.h> 49 #include <sys/vnode.h> 50 51 #include <fs/ext2fs/fs.h> 52 #include <fs/ext2fs/inode.h> 53 #include <fs/ext2fs/ext2fs.h> 54 #include <fs/ext2fs/ext2_dinode.h> 55 #include <fs/ext2fs/ext2_extern.h> 56 #include <fs/ext2fs/ext2_mount.h> 57 58 static int 59 ext2_ext_balloc(struct inode *ip, uint32_t lbn, int size, 60 struct ucred *cred, struct buf **bpp, int flags) 61 { 62 struct m_ext2fs *fs; 63 struct buf *bp = NULL; 64 struct vnode *vp = ITOV(ip); 65 daddr_t newblk; 66 int blks, error, allocated; 67 68 fs = ip->i_e2fs; 69 blks = howmany(size, fs->e2fs_bsize); 70 71 error = ext4_ext_get_blocks(ip, lbn, blks, cred, NULL, &allocated, &newblk); 72 if (error) 73 return (error); 74 75 if (allocated) { 76 bp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0); 77 if(!bp) 78 return (EIO); 79 } else { 80 error = bread(vp, lbn, fs->e2fs_bsize, NOCRED, &bp); 81 if (error) { 82 return (error); 83 } 84 } 85 86 87 bp->b_blkno = fsbtodb(fs, newblk); 88 if (flags & BA_CLRBUF) 89 vfs_bio_clrbuf(bp); 90 91 *bpp = bp; 92 93 return (error); 94 } 95 96 /* 97 * Balloc defines the structure of filesystem storage 98 * by allocating the physical blocks on a device given 99 * the inode and the logical block number in a file. 100 */ 101 int 102 ext2_balloc(struct inode *ip, e2fs_lbn_t lbn, int size, struct ucred *cred, 103 struct buf **bpp, int flags) 104 { 105 struct m_ext2fs *fs; 106 struct ext2mount *ump; 107 struct buf *bp, *nbp; 108 struct vnode *vp = ITOV(ip); 109 struct indir indirs[EXT2_NIADDR + 2]; 110 e4fs_daddr_t nb, newb; 111 e2fs_daddr_t *bap, pref; 112 int num, i, error; 113 114 *bpp = NULL; 115 if (lbn < 0) 116 return (EFBIG); 117 fs = ip->i_e2fs; 118 ump = ip->i_ump; 119 120 /* 121 * check if this is a sequential block allocation. 122 * If so, increment next_alloc fields to allow ext2_blkpref 123 * to make a good guess 124 */ 125 if (lbn == ip->i_next_alloc_block + 1) { 126 ip->i_next_alloc_block++; 127 ip->i_next_alloc_goal++; 128 } 129 130 if (ip->i_flag & IN_E4EXTENTS) 131 return (ext2_ext_balloc(ip, lbn, size, cred, bpp, flags)); 132 133 /* 134 * The first EXT2_NDADDR blocks are direct blocks 135 */ 136 if (lbn < EXT2_NDADDR) { 137 nb = ip->i_db[lbn]; 138 /* 139 * no new block is to be allocated, and no need to expand 140 * the file 141 */ 142 if (nb != 0) { 143 error = bread(vp, lbn, fs->e2fs_bsize, NOCRED, &bp); 144 if (error) { 145 return (error); 146 } 147 bp->b_blkno = fsbtodb(fs, nb); 148 if (ip->i_size >= (lbn + 1) * fs->e2fs_bsize) { 149 *bpp = bp; 150 return (0); 151 } 152 } else { 153 EXT2_LOCK(ump); 154 error = ext2_alloc(ip, lbn, 155 ext2_blkpref(ip, lbn, (int)lbn, &ip->i_db[0], 0), 156 fs->e2fs_bsize, cred, &newb); 157 if (error) 158 return (error); 159 /* 160 * If the newly allocated block exceeds 32-bit limit, 161 * we can not use it in file block maps. 162 */ 163 if (newb > UINT_MAX) 164 return (EFBIG); 165 bp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0); 166 bp->b_blkno = fsbtodb(fs, newb); 167 if (flags & BA_CLRBUF) 168 vfs_bio_clrbuf(bp); 169 } 170 ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno); 171 ip->i_flag |= IN_CHANGE | IN_UPDATE; 172 *bpp = bp; 173 return (0); 174 } 175 /* 176 * Determine the number of levels of indirection. 177 */ 178 pref = 0; 179 if ((error = ext2_getlbns(vp, lbn, indirs, &num)) != 0) 180 return (error); 181 #ifdef INVARIANTS 182 if (num < 1) 183 panic("ext2_balloc: ext2_getlbns returned indirect block"); 184 #endif 185 /* 186 * Fetch the first indirect block allocating if necessary. 187 */ 188 --num; 189 nb = ip->i_ib[indirs[0].in_off]; 190 if (nb == 0) { 191 EXT2_LOCK(ump); 192 pref = ext2_blkpref(ip, lbn, indirs[0].in_off + 193 EXT2_NDIR_BLOCKS, &ip->i_db[0], 0); 194 if ((error = ext2_alloc(ip, lbn, pref, fs->e2fs_bsize, cred, 195 &newb))) 196 return (error); 197 if (newb > UINT_MAX) 198 return (EFBIG); 199 nb = newb; 200 bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0, 0); 201 bp->b_blkno = fsbtodb(fs, newb); 202 vfs_bio_clrbuf(bp); 203 /* 204 * Write synchronously so that indirect blocks 205 * never point at garbage. 206 */ 207 if ((error = bwrite(bp)) != 0) { 208 ext2_blkfree(ip, nb, fs->e2fs_bsize); 209 return (error); 210 } 211 ip->i_ib[indirs[0].in_off] = newb; 212 ip->i_flag |= IN_CHANGE | IN_UPDATE; 213 } 214 /* 215 * Fetch through the indirect blocks, allocating as necessary. 216 */ 217 for (i = 1;;) { 218 error = bread(vp, 219 indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, &bp); 220 if (error) { 221 return (error); 222 } 223 bap = (e2fs_daddr_t *)bp->b_data; 224 nb = le32toh(bap[indirs[i].in_off]); 225 if (i == num) 226 break; 227 i += 1; 228 if (nb != 0) { 229 bqrelse(bp); 230 continue; 231 } 232 EXT2_LOCK(ump); 233 if (pref == 0) 234 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, bap, 235 bp->b_lblkno); 236 error = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newb); 237 if (error) { 238 brelse(bp); 239 return (error); 240 } 241 if (newb > UINT_MAX) 242 return (EFBIG); 243 nb = newb; 244 nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0, 0); 245 nbp->b_blkno = fsbtodb(fs, nb); 246 vfs_bio_clrbuf(nbp); 247 /* 248 * Write synchronously so that indirect blocks 249 * never point at garbage. 250 */ 251 if ((error = bwrite(nbp)) != 0) { 252 ext2_blkfree(ip, nb, fs->e2fs_bsize); 253 brelse(bp); 254 return (error); 255 } 256 bap[indirs[i - 1].in_off] = htole32(nb); 257 /* 258 * If required, write synchronously, otherwise use 259 * delayed write. 260 */ 261 if (flags & IO_SYNC) { 262 bwrite(bp); 263 } else { 264 if (bp->b_bufsize == fs->e2fs_bsize) 265 bp->b_flags |= B_CLUSTEROK; 266 bdwrite(bp); 267 } 268 } 269 /* 270 * Get the data block, allocating if necessary. 271 */ 272 if (nb == 0) { 273 EXT2_LOCK(ump); 274 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, &bap[0], 275 bp->b_lblkno); 276 if ((error = ext2_alloc(ip, 277 lbn, pref, (int)fs->e2fs_bsize, cred, &newb)) != 0) { 278 brelse(bp); 279 return (error); 280 } 281 if (newb > UINT_MAX) 282 return (EFBIG); 283 nb = newb; 284 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0); 285 nbp->b_blkno = fsbtodb(fs, nb); 286 if (flags & BA_CLRBUF) 287 vfs_bio_clrbuf(nbp); 288 bap[indirs[i].in_off] = htole32(nb); 289 /* 290 * If required, write synchronously, otherwise use 291 * delayed write. 292 */ 293 if (flags & IO_SYNC) { 294 bwrite(bp); 295 } else { 296 if (bp->b_bufsize == fs->e2fs_bsize) 297 bp->b_flags |= B_CLUSTEROK; 298 bdwrite(bp); 299 } 300 *bpp = nbp; 301 return (0); 302 } 303 brelse(bp); 304 if (flags & BA_CLRBUF) { 305 int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT; 306 307 if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) { 308 error = cluster_read(vp, ip->i_size, lbn, 309 (int)fs->e2fs_bsize, NOCRED, 310 MAXBSIZE, seqcount, 0, &nbp); 311 } else { 312 error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED, &nbp); 313 } 314 if (error) { 315 brelse(nbp); 316 return (error); 317 } 318 } else { 319 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0); 320 nbp->b_blkno = fsbtodb(fs, nb); 321 } 322 *bpp = nbp; 323 return (0); 324 } 325