| 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. All advertising materials mentioning features or use of this software |
| 20 | * must display the following acknowledgement: |
| 21 | * This product includes software developed by the University of |
| 22 | * California, Berkeley and its contributors. |
| 23 | * 4. Neither the name of the University nor the names of its contributors |
| 24 | * may be used to endorse or promote products derived from this software |
| 25 | * without specific prior written permission. |
| 26 | * |
| 27 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 28 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 30 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 33 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 34 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 35 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 36 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 37 | * SUCH DAMAGE. |
| 38 | * |
| 39 | * @(#)ffs_balloc.c 8.4 (Berkeley) 9/23/93 |
| 40 | * $FreeBSD: src/sys/gnu/ext2fs/ext2_balloc.c,v 1.9.2.1 2000/08/03 00:52:57 peter Exp $ |
| 41 | * $DragonFly: src/sys/vfs/gnu/ext2fs/ext2_balloc.c,v 1.10 2006/12/23 00:41:29 swildner Exp $ |
| 42 | */ |
| 43 | |
| 44 | #include <sys/param.h> |
| 45 | #include <sys/systm.h> |
| 46 | #include <sys/buf.h> |
| 47 | #include <sys/lock.h> |
| 48 | #include <sys/ucred.h> |
| 49 | #include <sys/vnode.h> |
| 50 | |
| 51 | #include "quota.h" |
| 52 | #include "inode.h" |
| 53 | #include "ext2_fs.h" |
| 54 | #include "ext2_fs_sb.h" |
| 55 | #include "fs.h" |
| 56 | #include "ext2_extern.h" |
| 57 | |
| 58 | /* |
| 59 | * Balloc defines the structure of file system storage |
| 60 | * by allocating the physical blocks on a device given |
| 61 | * the inode and the logical block number in a file. |
| 62 | */ |
| 63 | int |
| 64 | ext2_balloc(struct inode *ip, daddr_t bn, int size, struct ucred *cred, |
| 65 | struct buf **bpp, int flags) |
| 66 | { |
| 67 | struct ext2_sb_info *fs; |
| 68 | daddr_t nb; |
| 69 | struct buf *bp, *nbp; |
| 70 | struct vnode *vp = ITOV(ip); |
| 71 | struct indir indirs[NIADDR + 2]; |
| 72 | daddr_t newb, lbn, *bap, pref; |
| 73 | int osize, nsize, num, i, error; |
| 74 | /* |
| 75 | ext2_debug("ext2_balloc called (%d, %d, %d)\n", |
| 76 | ip->i_number, (int)bn, (int)size); |
| 77 | */ |
| 78 | *bpp = NULL; |
| 79 | if (bn < 0) |
| 80 | return (EFBIG); |
| 81 | fs = ip->i_e2fs; |
| 82 | lbn = bn; |
| 83 | |
| 84 | /* |
| 85 | * check if this is a sequential block allocation. |
| 86 | * If so, increment next_alloc fields to allow ext2_blkpref |
| 87 | * to make a good guess |
| 88 | */ |
| 89 | if (lbn == ip->i_next_alloc_block + 1) { |
| 90 | ip->i_next_alloc_block++; |
| 91 | ip->i_next_alloc_goal++; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * The first NDADDR blocks are direct blocks |
| 96 | */ |
| 97 | if (bn < NDADDR) { |
| 98 | nb = ip->i_db[bn]; |
| 99 | /* no new block is to be allocated, and no need to expand |
| 100 | the file */ |
| 101 | if (nb != 0 && ip->i_size >= (bn + 1) * fs->s_blocksize) { |
| 102 | error = bread(vp, lblktodoff(fs, bn), |
| 103 | fs->s_blocksize, &bp); |
| 104 | if (error) { |
| 105 | brelse(bp); |
| 106 | return (error); |
| 107 | } |
| 108 | *bpp = bp; |
| 109 | return (0); |
| 110 | } |
| 111 | if (nb != 0) { |
| 112 | /* |
| 113 | * Consider need to reallocate a fragment. |
| 114 | */ |
| 115 | osize = fragroundup(fs, blkoff(fs, ip->i_size)); |
| 116 | nsize = fragroundup(fs, size); |
| 117 | if (nsize <= osize) { |
| 118 | error = bread(vp, lblktodoff(fs, bn), |
| 119 | osize, &bp); |
| 120 | if (error) { |
| 121 | brelse(bp); |
| 122 | return (error); |
| 123 | } |
| 124 | } else { |
| 125 | /* Godmar thinks: this shouldn't happen w/o fragments */ |
| 126 | kprintf("nsize %d(%d) > osize %d(%d) nb %d\n", |
| 127 | (int)nsize, (int)size, (int)osize, |
| 128 | (int)ip->i_size, (int)nb); |
| 129 | panic( |
| 130 | "ext2_balloc: Something is terribly wrong"); |
| 131 | /* |
| 132 | * please note there haven't been any changes from here on - |
| 133 | * FFS seems to work. |
| 134 | */ |
| 135 | } |
| 136 | } else { |
| 137 | if (ip->i_size < (bn + 1) * fs->s_blocksize) |
| 138 | nsize = fragroundup(fs, size); |
| 139 | else |
| 140 | nsize = fs->s_blocksize; |
| 141 | error = ext2_alloc(ip, bn, |
| 142 | ext2_blkpref(ip, bn, (int)bn, &ip->i_db[0], 0), |
| 143 | nsize, cred, &newb); |
| 144 | if (error) |
| 145 | return (error); |
| 146 | bp = getblk(vp, lblktodoff(fs, bn), nsize, 0, 0); |
| 147 | bp->b_bio2.bio_offset = fsbtodoff(fs, newb); |
| 148 | if (flags & B_CLRBUF) |
| 149 | vfs_bio_clrbuf(bp); |
| 150 | } |
| 151 | ip->i_db[bn] = dofftofsb(fs, bp->b_bio2.bio_offset); |
| 152 | ip->i_flag |= IN_CHANGE | IN_UPDATE; |
| 153 | *bpp = bp; |
| 154 | return (0); |
| 155 | } |
| 156 | /* |
| 157 | * Determine the number of levels of indirection. |
| 158 | */ |
| 159 | pref = 0; |
| 160 | if ((error = ext2_getlbns(vp, bn, indirs, &num)) != 0) |
| 161 | return(error); |
| 162 | #if DIAGNOSTIC |
| 163 | if (num < 1) |
| 164 | panic ("ext2_balloc: ext2_bmaparray returned indirect block"); |
| 165 | #endif |
| 166 | /* |
| 167 | * Fetch the first indirect block allocating if necessary. |
| 168 | */ |
| 169 | --num; |
| 170 | nb = ip->i_ib[indirs[0].in_off]; |
| 171 | if (nb == 0) { |
| 172 | #if 0 |
| 173 | pref = ext2_blkpref(ip, lbn, 0, NULL, 0); |
| 174 | #else |
| 175 | /* see the comment by ext2_blkpref. What we do here is |
| 176 | to pretend that it'd be good for a block holding indirect |
| 177 | pointers to be allocated near its predecessor in terms |
| 178 | of indirection, or the last direct block. |
| 179 | We shamelessly exploit the fact that i_ib immediately |
| 180 | follows i_db. |
| 181 | Godmar thinks it make sense to allocate i_ib[0] immediately |
| 182 | after i_db[11], but it's not utterly clear whether this also |
| 183 | applies to i_ib[1] and i_ib[0] |
| 184 | */ |
| 185 | |
| 186 | pref = ext2_blkpref(ip, lbn, indirs[0].in_off + |
| 187 | EXT2_NDIR_BLOCKS, &ip->i_db[0], 0); |
| 188 | #endif |
| 189 | if ((error = ext2_alloc(ip, lbn, pref, (int)fs->s_blocksize, |
| 190 | cred, &newb)) != 0) |
| 191 | return (error); |
| 192 | nb = newb; |
| 193 | bp = getblk(vp, lblktodoff(fs, indirs[1].in_lbn), |
| 194 | fs->s_blocksize, 0, 0); |
| 195 | bp->b_bio2.bio_offset = fsbtodoff(fs, newb); |
| 196 | vfs_bio_clrbuf(bp); |
| 197 | /* |
| 198 | * Write synchronously so that indirect blocks |
| 199 | * never point at garbage. |
| 200 | */ |
| 201 | if ((error = bwrite(bp)) != 0) { |
| 202 | ext2_blkfree(ip, nb, fs->s_blocksize); |
| 203 | return (error); |
| 204 | } |
| 205 | ip->i_ib[indirs[0].in_off] = newb; |
| 206 | ip->i_flag |= IN_CHANGE | IN_UPDATE; |
| 207 | } |
| 208 | /* |
| 209 | * Fetch through the indirect blocks, allocating as necessary. |
| 210 | */ |
| 211 | for (i = 1;;) { |
| 212 | error = bread(vp, |
| 213 | lblktodoff(fs, indirs[i].in_lbn), |
| 214 | (int)fs->s_blocksize, &bp); |
| 215 | if (error) { |
| 216 | brelse(bp); |
| 217 | return (error); |
| 218 | } |
| 219 | bap = (daddr_t *)bp->b_data; |
| 220 | nb = bap[indirs[i].in_off]; |
| 221 | if (i == num) |
| 222 | break; |
| 223 | i += 1; |
| 224 | if (nb != 0) { |
| 225 | brelse(bp); |
| 226 | continue; |
| 227 | } |
| 228 | if (pref == 0) |
| 229 | #if 1 |
| 230 | /* see the comment above and by ext2_blkpref |
| 231 | * I think this implements Linux policy, but |
| 232 | * does it really make sense to allocate to |
| 233 | * block containing pointers together ? |
| 234 | * Also, will it ever succeed ? |
| 235 | */ |
| 236 | pref = ext2_blkpref(ip, lbn, indirs[i].in_off, bap, |
| 237 | lblkno(fs, bp->b_loffset)); |
| 238 | #else |
| 239 | pref = ext2_blkpref(ip, lbn, 0, NULL, 0); |
| 240 | #endif |
| 241 | if ((error = |
| 242 | ext2_alloc(ip, lbn, pref, (int)fs->s_blocksize, cred, &newb)) != 0) { |
| 243 | brelse(bp); |
| 244 | return (error); |
| 245 | } |
| 246 | nb = newb; |
| 247 | nbp = getblk(vp, lblktodoff(fs, indirs[i].in_lbn), |
| 248 | fs->s_blocksize, 0, 0); |
| 249 | nbp->b_bio2.bio_offset = fsbtodoff(fs, nb); |
| 250 | vfs_bio_clrbuf(nbp); |
| 251 | /* |
| 252 | * Write synchronously so that indirect blocks |
| 253 | * never point at garbage. |
| 254 | */ |
| 255 | if ((error = bwrite(nbp)) != 0) { |
| 256 | ext2_blkfree(ip, nb, fs->s_blocksize); |
| 257 | brelse(bp); |
| 258 | return (error); |
| 259 | } |
| 260 | bap[indirs[i - 1].in_off] = nb; |
| 261 | /* |
| 262 | * If required, write synchronously, otherwise use |
| 263 | * delayed write. |
| 264 | */ |
| 265 | if (flags & B_SYNC) { |
| 266 | bwrite(bp); |
| 267 | } else { |
| 268 | bdwrite(bp); |
| 269 | } |
| 270 | } |
| 271 | /* |
| 272 | * Get the data block, allocating if necessary. |
| 273 | */ |
| 274 | if (nb == 0) { |
| 275 | pref = ext2_blkpref(ip, lbn, indirs[i].in_off, &bap[0], |
| 276 | lblkno(fs, bp->b_loffset)); |
| 277 | if ((error = ext2_alloc(ip, |
| 278 | lbn, pref, (int)fs->s_blocksize, cred, &newb)) != 0) { |
| 279 | brelse(bp); |
| 280 | return (error); |
| 281 | } |
| 282 | nb = newb; |
| 283 | nbp = getblk(vp, lblktodoff(fs, lbn), fs->s_blocksize, 0, 0); |
| 284 | nbp->b_bio2.bio_offset = fsbtodoff(fs, nb); |
| 285 | if (flags & B_CLRBUF) |
| 286 | vfs_bio_clrbuf(nbp); |
| 287 | bap[indirs[i].in_off] = nb; |
| 288 | /* |
| 289 | * If required, write synchronously, otherwise use |
| 290 | * delayed write. |
| 291 | */ |
| 292 | if (flags & B_SYNC) { |
| 293 | bwrite(bp); |
| 294 | } else { |
| 295 | bdwrite(bp); |
| 296 | } |
| 297 | *bpp = nbp; |
| 298 | return (0); |
| 299 | } |
| 300 | brelse(bp); |
| 301 | if (flags & B_CLRBUF) { |
| 302 | error = bread(vp, lblktodoff(fs, lbn), |
| 303 | (int)fs->s_blocksize, &nbp); |
| 304 | if (error) { |
| 305 | brelse(nbp); |
| 306 | return (error); |
| 307 | } |
| 308 | } else { |
| 309 | nbp = getblk(vp, lblktodoff(fs, lbn), fs->s_blocksize, 0, 0); |
| 310 | nbp->b_bio2.bio_offset = fsbtodoff(fs, nb); |
| 311 | } |
| 312 | *bpp = nbp; |
| 313 | return (0); |
| 314 | } |