ext2_alloc.c revision 13260
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 * @(#)ext2_alloc.c 8.8 (Berkeley) 2/21/94 40 */ 41 42#if !defined(__FreeBSD__) 43#include "quota.h" 44#include "diagnostic.h" 45#else 46#include "opt_quota.h" 47#endif 48 49#include <sys/param.h> 50#include <sys/systm.h> 51#include <sys/buf.h> 52#include <sys/proc.h> 53#include <sys/vnode.h> 54#include <sys/stat.h> 55#include <sys/mount.h> 56#include <sys/kernel.h> 57#include <sys/syslog.h> 58 59#include <vm/vm.h> 60 61#include <ufs/ufs/quota.h> 62#include <ufs/ufs/inode.h> 63 64#include <gnu/ext2fs/ext2_fs.h> 65#include <gnu/ext2fs/ext2_fs_sb.h> 66#include <gnu/ext2fs/fs.h> 67#include <gnu/ext2fs/ext2_extern.h> 68 69extern u_long nextgennumber; 70 71static void ext2_fserr __P((struct ext2_sb_info *, u_int, char *)); 72 73/* 74 * Linux calls this functions at the following locations: 75 * (1) the inode is freed 76 * (2) a preallocation miss occurs 77 * (3) truncate is called 78 * (4) release_file is called and f_mode & 2 79 * 80 * I call it in ext2_inactive, ext2_truncate, ext2_vfree and in (2) 81 * the call in vfree might be redundant 82 */ 83void ext2_discard_prealloc (struct inode * ip) 84{ 85#ifdef EXT2_PREALLOCATE 86 if (ip->i_prealloc_count) { 87 int i = ip->i_prealloc_count; 88 ip->i_prealloc_count = 0; 89 ext2_free_blocks (ITOV(ip)->v_mount, 90 ip->i_prealloc_block, 91 i); 92 } 93#endif 94} 95 96/* 97 * Allocate a block in the file system. 98 * 99 * this takes the framework from ffs_alloc. To implement the 100 * actual allocation, it calls ext2_new_block, the ported version 101 * of the same Linux routine. 102 * 103 * we note that this is always called in connection with ext2_blkpref 104 * 105 * preallocation is done as Linux does it 106 */ 107int 108ext2_alloc(ip, lbn, bpref, size, cred, bnp) 109 register struct inode *ip; 110 daddr_t lbn, bpref; 111 int size; 112 struct ucred *cred; 113 daddr_t *bnp; 114{ 115 register struct ext2_sb_info *fs; 116 daddr_t bno; 117#if QUOTA 118 int error; 119#endif 120 121 *bnp = 0; 122 fs = ip->i_e2fs; 123#if DIAGNOSTIC 124 if ((u_int)size > fs->s_blocksize || blkoff(fs, size) != 0) { 125 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 126 ip->i_dev, fs->s_blocksize, size, fs->fs_fsmnt); 127 panic("ext2_alloc: bad size"); 128 } 129 if (cred == NOCRED) 130 panic("ext2_alloc: missing credential\n"); 131#endif /* DIAGNOSTIC */ 132 if (size == fs->s_blocksize && fs->s_es->s_free_blocks_count == 0) 133 goto nospace; 134 if (cred->cr_uid != 0 && 135 fs->s_es->s_free_blocks_count < fs->s_es->s_r_blocks_count) 136 goto nospace; 137#if QUOTA 138 if (error = chkdq(ip, (long)btodb(size), cred, 0)) 139 return (error); 140#endif 141 if (bpref >= fs->s_es->s_blocks_count) 142 bpref = 0; 143 /* call the Linux code */ 144#ifdef EXT2_PREALLOCATE 145 /* To have a preallocation hit, we must 146 * - have at least one block preallocated 147 * - and our preferred block must have that block number or one below 148 */ 149 if (ip->i_prealloc_count && 150 (bpref == ip->i_prealloc_block || 151 bpref + 1 == ip->i_prealloc_block)) 152 { 153 bno = ip->i_prealloc_block++; 154 ip->i_prealloc_count--; 155 /* ext2_debug ("preallocation hit (%lu/%lu).\n", 156 ++alloc_hits, ++alloc_attempts); */ 157 158 /* Linux gets, clears, and releases the buffer at this 159 point - we don't have to that; we leave it to the caller 160 */ 161 } else { 162 ext2_discard_prealloc (ip); 163 /* ext2_debug ("preallocation miss (%lu/%lu).\n", 164 alloc_hits, ++alloc_attempts); */ 165 if (S_ISREG(ip->i_mode)) 166 bno = ext2_new_block 167 (ITOV(ip)->v_mount, bpref, 168 &ip->i_prealloc_count, 169 &ip->i_prealloc_block); 170 else 171 bno = (daddr_t)ext2_new_block(ITOV(ip)->v_mount, 172 bpref, 0, 0); 173 } 174#else 175 bno = (daddr_t)ext2_new_block(ITOV(ip)->v_mount, bpref, 0, 0); 176#endif 177 178 if (bno > 0) { 179 /* set next_alloc fields as done in block_getblk */ 180 ip->i_next_alloc_block = lbn; 181 ip->i_next_alloc_goal = bno; 182 183 ip->i_blocks += btodb(size); 184 ip->i_flag |= IN_CHANGE | IN_UPDATE; 185 *bnp = bno; 186 return (0); 187 } 188#if QUOTA 189 /* 190 * Restore user's disk quota because allocation failed. 191 */ 192 (void) chkdq(ip, (long)-btodb(size), cred, FORCE); 193#endif 194nospace: 195 ext2_fserr(fs, cred->cr_uid, "file system full"); 196 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 197 return (ENOSPC); 198} 199 200/* 201 * Reallocate a sequence of blocks into a contiguous sequence of blocks. 202 * 203 * The vnode and an array of buffer pointers for a range of sequential 204 * logical blocks to be made contiguous is given. The allocator attempts 205 * to find a range of sequential blocks starting as close as possible to 206 * an fs_rotdelay offset from the end of the allocation for the logical 207 * block immediately preceeding the current range. If successful, the 208 * physical block numbers in the buffer pointers and in the inode are 209 * changed to reflect the new allocation. If unsuccessful, the allocation 210 * is left unchanged. The success in doing the reallocation is returned. 211 * Note that the error return is not reflected back to the user. Rather 212 * the previous block allocation will be used. 213 */ 214#include <sys/sysctl.h> 215static int doasyncfree = 1; 216#ifdef OPT_DEBUG 217SYSCTL_INT(_debug, 14, doasyncfree, CTLFLAG_RW, &doasyncfree, 0, ""); 218#endif /* OPT_DEBUG */ 219int 220ext2_reallocblks(ap) 221 struct vop_reallocblks_args /* { 222 struct vnode *a_vp; 223 struct cluster_save *a_buflist; 224 } */ *ap; 225{ 226#ifndef FANCY_REALLOC 227/* printf("ext2_reallocblks not implemented\n"); */ 228return ENOSPC; 229#else 230 231 struct ext2_sb_info *fs; 232 struct inode *ip; 233 struct vnode *vp; 234 struct buf *sbp, *ebp; 235 daddr_t *bap, *sbap, *ebap; 236 struct cluster_save *buflist; 237 daddr_t start_lbn, end_lbn, soff, eoff, newblk, blkno; 238 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp; 239 int i, len, start_lvl, end_lvl, pref, ssize; 240 241 vp = ap->a_vp; 242 ip = VTOI(vp); 243 fs = ip->i_e2fs; 244#ifdef UNKLAR 245 if (fs->fs_contigsumsize <= 0) 246 return (ENOSPC); 247#endif 248 buflist = ap->a_buflist; 249 len = buflist->bs_nchildren; 250 start_lbn = buflist->bs_children[0]->b_lblkno; 251 end_lbn = start_lbn + len - 1; 252#if DIAGNOSTIC 253 for (i = 1; i < len; i++) 254 if (buflist->bs_children[i]->b_lblkno != start_lbn + i) 255 panic("ext2_reallocblks: non-cluster"); 256#endif 257 /* 258 * If the latest allocation is in a new cylinder group, assume that 259 * the filesystem has decided to move and do not force it back to 260 * the previous cylinder group. 261 */ 262 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) != 263 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno))) 264 return (ENOSPC); 265 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) || 266 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl)) 267 return (ENOSPC); 268 /* 269 * Get the starting offset and block map for the first block. 270 */ 271 if (start_lvl == 0) { 272 sbap = &ip->i_db[0]; 273 soff = start_lbn; 274 } else { 275 idp = &start_ap[start_lvl - 1]; 276 if (bread(vp, idp->in_lbn, (int)fs->s_blocksize, NOCRED, &sbp)) { 277 brelse(sbp); 278 return (ENOSPC); 279 } 280 sbap = (daddr_t *)sbp->b_data; 281 soff = idp->in_off; 282 } 283 /* 284 * Find the preferred location for the cluster. 285 */ 286 pref = ext2_blkpref(ip, start_lbn, soff, sbap); 287 /* 288 * If the block range spans two block maps, get the second map. 289 */ 290 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) { 291 ssize = len; 292 } else { 293#if DIAGNOSTIC 294 if (start_ap[start_lvl-1].in_lbn == idp->in_lbn) 295 panic("ext2_reallocblk: start == end"); 296#endif 297 ssize = len - (idp->in_off + 1); 298 if (bread(vp, idp->in_lbn, (int)fs->s_blocksize, NOCRED, &ebp)) 299 goto fail; 300 ebap = (daddr_t *)ebp->b_data; 301 } 302 /* 303 * Search the block map looking for an allocation of the desired size. 304 */ 305 if ((newblk = (daddr_t)ext2_hashalloc(ip, dtog(fs, pref), (long)pref, 306 len, (u_long (*)())ext2_clusteralloc)) == 0) 307 goto fail; 308 /* 309 * We have found a new contiguous block. 310 * 311 * First we have to replace the old block pointers with the new 312 * block pointers in the inode and indirect blocks associated 313 * with the file. 314 */ 315 blkno = newblk; 316 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->s_frags_per_block) { 317 if (i == ssize) 318 bap = ebap; 319#if DIAGNOSTIC 320 if (buflist->bs_children[i]->b_blkno != fsbtodb(fs, *bap)) 321 panic("ext2_reallocblks: alloc mismatch"); 322#endif 323 *bap++ = blkno; 324 } 325 /* 326 * Next we must write out the modified inode and indirect blocks. 327 * For strict correctness, the writes should be synchronous since 328 * the old block values may have been written to disk. In practise 329 * they are almost never written, but if we are concerned about 330 * strict correctness, the `doasyncfree' flag should be set to zero. 331 * 332 * The test on `doasyncfree' should be changed to test a flag 333 * that shows whether the associated buffers and inodes have 334 * been written. The flag should be set when the cluster is 335 * started and cleared whenever the buffer or inode is flushed. 336 * We can then check below to see if it is set, and do the 337 * synchronous write only when it has been cleared. 338 */ 339 if (sbap != &ip->i_db[0]) { 340 if (doasyncfree) 341 bdwrite(sbp); 342 else 343 bwrite(sbp); 344 } else { 345#if !defined(__FreeBSD__) 346 struct timeval time; 347 get_time(&time); 348#endif 349 ip->i_flag |= IN_CHANGE | IN_UPDATE; 350 if (!doasyncfree) 351 VOP_UPDATE(vp, &time, &time, MNT_WAIT); 352 } 353 if (ssize < len) 354 if (doasyncfree) 355 bdwrite(ebp); 356 else 357 bwrite(ebp); 358 /* 359 * Last, free the old blocks and assign the new blocks to the buffers. 360 */ 361 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->s_frags_per_block) { 362 ext2_blkfree(ip, dbtofsb(fs, buflist->bs_children[i]->b_blkno), 363 fs->s_blocksize); 364 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno); 365 } 366 return (0); 367 368fail: 369 if (ssize < len) 370 brelse(ebp); 371 if (sbap != &ip->i_db[0]) 372 brelse(sbp); 373 return (ENOSPC); 374 375#endif /* FANCY_REALLOC */ 376} 377 378/* 379 * Allocate an inode in the file system. 380 * 381 * we leave the actual allocation strategy to the (modified) 382 * ext2_new_inode(), to make sure we get the policies right 383 */ 384int 385ext2_valloc(ap) 386 struct vop_valloc_args /* { 387 struct vnode *a_pvp; 388 int a_mode; 389 struct ucred *a_cred; 390 struct vnode **a_vpp; 391 } */ *ap; 392{ 393 register struct vnode *pvp = ap->a_pvp; 394 register struct inode *pip; 395 register struct ext2_sb_info *fs; 396 register struct inode *ip; 397 mode_t mode = ap->a_mode; 398 ino_t ino; 399 int i, error; 400#if !defined(__FreeBSD__) 401 struct timeval time; 402#endif 403 404 *ap->a_vpp = NULL; 405 pip = VTOI(pvp); 406 fs = pip->i_e2fs; 407 if (fs->s_es->s_free_inodes_count == 0) 408 goto noinodes; 409 410 /* call the Linux routine - it returns the inode number only */ 411 ino = ext2_new_inode(pip, mode); 412 413 if (ino == 0) 414 goto noinodes; 415 error = VFS_VGET(pvp->v_mount, ino, ap->a_vpp); 416 if (error) { 417 VOP_VFREE(pvp, ino, mode); 418 return (error); 419 } 420 ip = VTOI(*ap->a_vpp); 421 422 /* 423 the question is whether using VGET was such good idea at all - 424 Linux doesn't read the old inode in when it's allocating a 425 new one. I will set at least i_size & i_blocks the zero. 426 */ 427 ip->i_mode = 0; 428 ip->i_size = 0; 429 ip->i_blocks = 0; 430 ip->i_flags = 0; 431 /* now we want to make sure that the block pointers are zeroed out */ 432 for(i = 0; i < EXT2_NDIR_BLOCKS; i++) 433 ip->i_db[i] = 0; 434 435 /* 436 * Set up a new generation number for this inode. 437 * XXX check if this makes sense in ext2 438 */ 439#if !defined(__FreeBSD__) 440 get_time(&time); 441#endif 442 if (++nextgennumber < (u_long)time.tv_sec) 443 nextgennumber = time.tv_sec; 444 ip->i_gen = nextgennumber; 445/* 446printf("ext2_valloc: allocated inode %d\n", ino); 447*/ 448 return (0); 449noinodes: 450 ext2_fserr(fs, ap->a_cred->cr_uid, "out of inodes"); 451 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 452 return (ENOSPC); 453} 454 455/* 456 * Select the desired position for the next block in a file. 457 * 458 * we try to mimic what Remy does in inode_getblk/block_getblk 459 * 460 * we note: blocknr == 0 means that we're about to allocate either 461 * a direct block or a pointer block at the first level of indirection 462 * (In other words, stuff that will go in i_db[] or i_ib[]) 463 * 464 * blocknr != 0 means that we're allocating a block that is none 465 * of the above. Then, blocknr tells us the number of the block 466 * that will hold the pointer 467 */ 468daddr_t 469ext2_blkpref(ip, lbn, indx, bap, blocknr) 470 struct inode *ip; 471 daddr_t lbn; 472 int indx; 473 daddr_t *bap; 474 daddr_t blocknr; 475{ 476 int tmp; 477 478 /* if the next block is actually what we thought it is, 479 then set the goal to what we thought it should be 480 */ 481 if(ip->i_next_alloc_block == lbn) 482 return ip->i_next_alloc_goal; 483 484 /* now check whether we were provided with an array that basically 485 tells us previous blocks to which we want to stay closeby 486 */ 487 if(bap) 488 for (tmp = indx - 1; tmp >= 0; tmp--) 489 if (bap[tmp]) 490 return bap[tmp]; 491 492 /* else let's fall back to the blocknr, or, if there is none, 493 follow the rule that a block should be allocated near it's inode 494 */ 495 return blocknr ? blocknr : 496 (daddr_t)(ip->i_block_group * 497 EXT2_BLOCKS_PER_GROUP(ip->i_e2fs)) + 498 ip->i_e2fs->s_es->s_first_data_block; 499} 500 501/* 502 * Free a block or fragment. 503 * 504 * pass on to the Linux code 505 */ 506void 507ext2_blkfree(ip, bno, size) 508 register struct inode *ip; 509 daddr_t bno; 510 long size; 511{ 512 register struct ext2_sb_info *fs; 513 514 fs = ip->i_e2fs; 515 /* 516 * call Linux code with mount *, block number, count 517 */ 518 ext2_free_blocks(ITOV(ip)->v_mount, bno, size / fs->s_frag_size); 519} 520 521/* 522 * Free an inode. 523 * 524 * the maintenance of the actual bitmaps is again up to the linux code 525 */ 526int 527ext2_vfree(ap) 528 struct vop_vfree_args /* { 529 struct vnode *a_pvp; 530 ino_t a_ino; 531 int a_mode; 532 } */ *ap; 533{ 534 register struct ext2_sb_info *fs; 535 register struct inode *pip; 536 ino_t ino = ap->a_ino; 537 int mode; 538 539 pip = VTOI(ap->a_pvp); 540 fs = pip->i_e2fs; 541 if ((u_int)ino >= fs->s_inodes_per_group * fs->s_groups_count) 542 panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n", 543 pip->i_dev, ino, fs->fs_fsmnt); 544 545/* ext2_debug("ext2_vfree (%d, %d) called\n", pip->i_number, ap->a_mode); 546 */ 547 ext2_discard_prealloc(pip); 548 549 /* we need to make sure that ext2_free_inode can adjust the 550 used_dir_counts in the group summary information - I'd 551 really like to know what the rationale behind this 552 'set i_mode to zero to denote an unused inode' is 553 */ 554 mode = pip->i_mode; 555 pip->i_mode = ap->a_mode; 556 ext2_free_inode(pip); 557 pip->i_mode = mode; 558 return (0); 559} 560 561/* 562 * Fserr prints the name of a file system with an error diagnostic. 563 * 564 * The form of the error message is: 565 * fs: error message 566 */ 567static void 568ext2_fserr(fs, uid, cp) 569 struct ext2_sb_info *fs; 570 u_int uid; 571 char *cp; 572{ 573 574 log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp); 575} 576