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 * 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.4 (Berkeley) 9/23/93
| 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 * 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.4 (Berkeley) 9/23/93
|
52/* 53 * Balloc defines the structure of filesystem storage 54 * by allocating the physical blocks on a device given 55 * the inode and the logical block number in a file. 56 */ 57int 58ext2_balloc(struct inode *ip, int32_t lbn, int size, struct ucred *cred, 59 struct buf **bpp, int flags) 60{ 61 struct m_ext2fs *fs; 62 struct ext2mount *ump; 63 struct buf *bp, *nbp; 64 struct vnode *vp = ITOV(ip); 65 struct indir indirs[NIADDR + 2]; 66 uint32_t nb, newb; 67 int32_t *bap, pref; 68 int osize, nsize, num, i, error; 69 70 *bpp = NULL; 71 if (lbn < 0) 72 return (EFBIG); 73 fs = ip->i_e2fs; 74 ump = ip->i_ump; 75 76 /* 77 * check if this is a sequential block allocation. 78 * If so, increment next_alloc fields to allow ext2_blkpref 79 * to make a good guess 80 */ 81 if (lbn == ip->i_next_alloc_block + 1) { 82 ip->i_next_alloc_block++; 83 ip->i_next_alloc_goal++; 84 } 85 86 /* 87 * The first NDADDR blocks are direct blocks 88 */ 89 if (lbn < NDADDR) { 90 nb = ip->i_db[lbn]; 91 /* no new block is to be allocated, and no need to expand 92 the file */ 93 if (nb != 0 && ip->i_size >= (lbn + 1) * fs->e2fs_bsize) { 94 error = bread(vp, lbn, fs->e2fs_bsize, NOCRED, &bp); 95 if (error) { 96 brelse(bp); 97 return (error); 98 } 99 bp->b_blkno = fsbtodb(fs, nb); 100 *bpp = bp; 101 return (0); 102 } 103 if (nb != 0) { 104 /* 105 * Consider need to reallocate a fragment. 106 */ 107 osize = fragroundup(fs, blkoff(fs, ip->i_size)); 108 nsize = fragroundup(fs, size); 109 if (nsize <= osize) { 110 error = bread(vp, lbn, osize, NOCRED, &bp); 111 if (error) { 112 brelse(bp); 113 return (error); 114 } 115 bp->b_blkno = fsbtodb(fs, nb); 116 } else { 117 /* Godmar thinks: this shouldn't happen w/o fragments */ 118 printf("nsize %d(%d) > osize %d(%d) nb %d\n", 119 (int)nsize, (int)size, (int)osize, 120 (int)ip->i_size, (int)nb); 121 panic( 122 "ext2_balloc: Something is terribly wrong"); 123/* 124 * please note there haven't been any changes from here on - 125 * FFS seems to work. 126 */ 127 } 128 } else { 129 if (ip->i_size < (lbn + 1) * fs->e2fs_bsize) 130 nsize = fragroundup(fs, size); 131 else 132 nsize = fs->e2fs_bsize; 133 EXT2_LOCK(ump); 134 error = ext2_alloc(ip, lbn, 135 ext2_blkpref(ip, lbn, (int)lbn, &ip->i_db[0], 0), 136 nsize, cred, &newb); 137 if (error) 138 return (error); 139 bp = getblk(vp, lbn, nsize, 0, 0, 0); 140 bp->b_blkno = fsbtodb(fs, newb); 141 if (flags & BA_CLRBUF) 142 vfs_bio_clrbuf(bp); 143 } 144 ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno); 145 ip->i_flag |= IN_CHANGE | IN_UPDATE; 146 *bpp = bp; 147 return (0); 148 } 149 /* 150 * Determine the number of levels of indirection. 151 */ 152 pref = 0; 153 if ((error = ext2_getlbns(vp, lbn, indirs, &num)) != 0) 154 return (error); 155#ifdef INVARIANTS 156 if (num < 1) 157 panic ("ext2_balloc: ext2_getlbns returned indirect block"); 158#endif 159 /* 160 * Fetch the first indirect block allocating if necessary. 161 */ 162 --num; 163 nb = ip->i_ib[indirs[0].in_off]; 164 if (nb == 0) { 165 EXT2_LOCK(ump); 166 pref = ext2_blkpref(ip, lbn, indirs[0].in_off + 167 EXT2_NDIR_BLOCKS, &ip->i_db[0], 0); 168 if ((error = ext2_alloc(ip, lbn, pref, fs->e2fs_bsize, cred, 169 &newb))) 170 return (error); 171 nb = newb; 172 bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0, 0); 173 bp->b_blkno = fsbtodb(fs, newb); 174 vfs_bio_clrbuf(bp); 175 /* 176 * Write synchronously so that indirect blocks 177 * never point at garbage. 178 */ 179 if ((error = bwrite(bp)) != 0) { 180 ext2_blkfree(ip, nb, fs->e2fs_bsize); 181 return (error); 182 } 183 ip->i_ib[indirs[0].in_off] = newb; 184 ip->i_flag |= IN_CHANGE | IN_UPDATE; 185 } 186 /* 187 * Fetch through the indirect blocks, allocating as necessary. 188 */ 189 for (i = 1;;) { 190 error = bread(vp, 191 indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, &bp); 192 if (error) { 193 brelse(bp); 194 return (error); 195 } 196 bap = (int32_t *)bp->b_data; 197 nb = bap[indirs[i].in_off]; 198 if (i == num) 199 break; 200 i += 1; 201 if (nb != 0) { 202 bqrelse(bp); 203 continue; 204 } 205 EXT2_LOCK(ump); 206 if (pref == 0) 207 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, bap, 208 bp->b_lblkno); 209 error = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newb); 210 if (error) { 211 brelse(bp); 212 return (error); 213 } 214 nb = newb; 215 nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0, 0); 216 nbp->b_blkno = fsbtodb(fs, nb); 217 vfs_bio_clrbuf(nbp); 218 /* 219 * Write synchronously so that indirect blocks 220 * never point at garbage. 221 */ 222 if ((error = bwrite(nbp)) != 0) { 223 ext2_blkfree(ip, nb, fs->e2fs_bsize); 224 EXT2_UNLOCK(ump); 225 brelse(bp); 226 return (error); 227 } 228 bap[indirs[i - 1].in_off] = nb; 229 /* 230 * If required, write synchronously, otherwise use 231 * delayed write. 232 */ 233 if (flags & IO_SYNC) { 234 bwrite(bp); 235 } else { 236 if (bp->b_bufsize == fs->e2fs_bsize) 237 bp->b_flags |= B_CLUSTEROK; 238 bdwrite(bp); 239 } 240 } 241 /* 242 * Get the data block, allocating if necessary. 243 */ 244 if (nb == 0) { 245 EXT2_LOCK(ump); 246 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, &bap[0], 247 bp->b_lblkno); 248 if ((error = ext2_alloc(ip, 249 lbn, pref, (int)fs->e2fs_bsize, cred, &newb)) != 0) { 250 brelse(bp); 251 return (error); 252 } 253 nb = newb; 254 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0); 255 nbp->b_blkno = fsbtodb(fs, nb); 256 if (flags & BA_CLRBUF) 257 vfs_bio_clrbuf(nbp); 258 bap[indirs[i].in_off] = nb; 259 /* 260 * If required, write synchronously, otherwise use 261 * delayed write. 262 */ 263 if (flags & IO_SYNC) { 264 bwrite(bp); 265 } else { 266 if (bp->b_bufsize == fs->e2fs_bsize) 267 bp->b_flags |= B_CLUSTEROK; 268 bdwrite(bp); 269 } 270 *bpp = nbp; 271 return (0); 272 } 273 brelse(bp); 274 if (flags & BA_CLRBUF) { 275 int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT; 276 if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) { 277 error = cluster_read(vp, ip->i_size, lbn, 278 (int)fs->e2fs_bsize, NOCRED, 279 MAXBSIZE, seqcount, 0, &nbp); 280 } else { 281 error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED, &nbp); 282 } 283 if (error) { 284 brelse(nbp); 285 return (error); 286 } 287 } else { 288 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0); 289 nbp->b_blkno = fsbtodb(fs, nb); 290 } 291 *bpp = nbp; 292 return (0); 293} 294
| 54/* 55 * Balloc defines the structure of filesystem storage 56 * by allocating the physical blocks on a device given 57 * the inode and the logical block number in a file. 58 */ 59int 60ext2_balloc(struct inode *ip, int32_t lbn, int size, struct ucred *cred, 61 struct buf **bpp, int flags) 62{ 63 struct m_ext2fs *fs; 64 struct ext2mount *ump; 65 struct buf *bp, *nbp; 66 struct vnode *vp = ITOV(ip); 67 struct indir indirs[NIADDR + 2]; 68 uint32_t nb, newb; 69 int32_t *bap, pref; 70 int osize, nsize, num, i, error; 71 72 *bpp = NULL; 73 if (lbn < 0) 74 return (EFBIG); 75 fs = ip->i_e2fs; 76 ump = ip->i_ump; 77 78 /* 79 * check if this is a sequential block allocation. 80 * If so, increment next_alloc fields to allow ext2_blkpref 81 * to make a good guess 82 */ 83 if (lbn == ip->i_next_alloc_block + 1) { 84 ip->i_next_alloc_block++; 85 ip->i_next_alloc_goal++; 86 } 87 88 /* 89 * The first NDADDR blocks are direct blocks 90 */ 91 if (lbn < NDADDR) { 92 nb = ip->i_db[lbn]; 93 /* no new block is to be allocated, and no need to expand 94 the file */ 95 if (nb != 0 && ip->i_size >= (lbn + 1) * fs->e2fs_bsize) { 96 error = bread(vp, lbn, fs->e2fs_bsize, NOCRED, &bp); 97 if (error) { 98 brelse(bp); 99 return (error); 100 } 101 bp->b_blkno = fsbtodb(fs, nb); 102 *bpp = bp; 103 return (0); 104 } 105 if (nb != 0) { 106 /* 107 * Consider need to reallocate a fragment. 108 */ 109 osize = fragroundup(fs, blkoff(fs, ip->i_size)); 110 nsize = fragroundup(fs, size); 111 if (nsize <= osize) { 112 error = bread(vp, lbn, osize, NOCRED, &bp); 113 if (error) { 114 brelse(bp); 115 return (error); 116 } 117 bp->b_blkno = fsbtodb(fs, nb); 118 } else { 119 /* Godmar thinks: this shouldn't happen w/o fragments */ 120 printf("nsize %d(%d) > osize %d(%d) nb %d\n", 121 (int)nsize, (int)size, (int)osize, 122 (int)ip->i_size, (int)nb); 123 panic( 124 "ext2_balloc: Something is terribly wrong"); 125/* 126 * please note there haven't been any changes from here on - 127 * FFS seems to work. 128 */ 129 } 130 } else { 131 if (ip->i_size < (lbn + 1) * fs->e2fs_bsize) 132 nsize = fragroundup(fs, size); 133 else 134 nsize = fs->e2fs_bsize; 135 EXT2_LOCK(ump); 136 error = ext2_alloc(ip, lbn, 137 ext2_blkpref(ip, lbn, (int)lbn, &ip->i_db[0], 0), 138 nsize, cred, &newb); 139 if (error) 140 return (error); 141 bp = getblk(vp, lbn, nsize, 0, 0, 0); 142 bp->b_blkno = fsbtodb(fs, newb); 143 if (flags & BA_CLRBUF) 144 vfs_bio_clrbuf(bp); 145 } 146 ip->i_db[lbn] = dbtofsb(fs, bp->b_blkno); 147 ip->i_flag |= IN_CHANGE | IN_UPDATE; 148 *bpp = bp; 149 return (0); 150 } 151 /* 152 * Determine the number of levels of indirection. 153 */ 154 pref = 0; 155 if ((error = ext2_getlbns(vp, lbn, indirs, &num)) != 0) 156 return (error); 157#ifdef INVARIANTS 158 if (num < 1) 159 panic ("ext2_balloc: ext2_getlbns returned indirect block"); 160#endif 161 /* 162 * Fetch the first indirect block allocating if necessary. 163 */ 164 --num; 165 nb = ip->i_ib[indirs[0].in_off]; 166 if (nb == 0) { 167 EXT2_LOCK(ump); 168 pref = ext2_blkpref(ip, lbn, indirs[0].in_off + 169 EXT2_NDIR_BLOCKS, &ip->i_db[0], 0); 170 if ((error = ext2_alloc(ip, lbn, pref, fs->e2fs_bsize, cred, 171 &newb))) 172 return (error); 173 nb = newb; 174 bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0, 0); 175 bp->b_blkno = fsbtodb(fs, newb); 176 vfs_bio_clrbuf(bp); 177 /* 178 * Write synchronously so that indirect blocks 179 * never point at garbage. 180 */ 181 if ((error = bwrite(bp)) != 0) { 182 ext2_blkfree(ip, nb, fs->e2fs_bsize); 183 return (error); 184 } 185 ip->i_ib[indirs[0].in_off] = newb; 186 ip->i_flag |= IN_CHANGE | IN_UPDATE; 187 } 188 /* 189 * Fetch through the indirect blocks, allocating as necessary. 190 */ 191 for (i = 1;;) { 192 error = bread(vp, 193 indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, &bp); 194 if (error) { 195 brelse(bp); 196 return (error); 197 } 198 bap = (int32_t *)bp->b_data; 199 nb = bap[indirs[i].in_off]; 200 if (i == num) 201 break; 202 i += 1; 203 if (nb != 0) { 204 bqrelse(bp); 205 continue; 206 } 207 EXT2_LOCK(ump); 208 if (pref == 0) 209 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, bap, 210 bp->b_lblkno); 211 error = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newb); 212 if (error) { 213 brelse(bp); 214 return (error); 215 } 216 nb = newb; 217 nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0, 0); 218 nbp->b_blkno = fsbtodb(fs, nb); 219 vfs_bio_clrbuf(nbp); 220 /* 221 * Write synchronously so that indirect blocks 222 * never point at garbage. 223 */ 224 if ((error = bwrite(nbp)) != 0) { 225 ext2_blkfree(ip, nb, fs->e2fs_bsize); 226 EXT2_UNLOCK(ump); 227 brelse(bp); 228 return (error); 229 } 230 bap[indirs[i - 1].in_off] = nb; 231 /* 232 * If required, write synchronously, otherwise use 233 * delayed write. 234 */ 235 if (flags & IO_SYNC) { 236 bwrite(bp); 237 } else { 238 if (bp->b_bufsize == fs->e2fs_bsize) 239 bp->b_flags |= B_CLUSTEROK; 240 bdwrite(bp); 241 } 242 } 243 /* 244 * Get the data block, allocating if necessary. 245 */ 246 if (nb == 0) { 247 EXT2_LOCK(ump); 248 pref = ext2_blkpref(ip, lbn, indirs[i].in_off, &bap[0], 249 bp->b_lblkno); 250 if ((error = ext2_alloc(ip, 251 lbn, pref, (int)fs->e2fs_bsize, cred, &newb)) != 0) { 252 brelse(bp); 253 return (error); 254 } 255 nb = newb; 256 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0); 257 nbp->b_blkno = fsbtodb(fs, nb); 258 if (flags & BA_CLRBUF) 259 vfs_bio_clrbuf(nbp); 260 bap[indirs[i].in_off] = nb; 261 /* 262 * If required, write synchronously, otherwise use 263 * delayed write. 264 */ 265 if (flags & IO_SYNC) { 266 bwrite(bp); 267 } else { 268 if (bp->b_bufsize == fs->e2fs_bsize) 269 bp->b_flags |= B_CLUSTEROK; 270 bdwrite(bp); 271 } 272 *bpp = nbp; 273 return (0); 274 } 275 brelse(bp); 276 if (flags & BA_CLRBUF) { 277 int seqcount = (flags & BA_SEQMASK) >> BA_SEQSHIFT; 278 if (seqcount && (vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) { 279 error = cluster_read(vp, ip->i_size, lbn, 280 (int)fs->e2fs_bsize, NOCRED, 281 MAXBSIZE, seqcount, 0, &nbp); 282 } else { 283 error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED, &nbp); 284 } 285 if (error) { 286 brelse(nbp); 287 return (error); 288 } 289 } else { 290 nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0, 0); 291 nbp->b_blkno = fsbtodb(fs, nb); 292 } 293 *bpp = nbp; 294 return (0); 295} 296
|