1 /* $NetBSD: lfs_alloc.c,v 1.112 2012/02/16 02:47:55 perseant Exp $ */ 2 3 /*- 4 * Copyright (c) 1999, 2000, 2001, 2002, 2003, 2007 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Konrad E. Schroder <perseant@hhhh.org>. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 /* 32 * Copyright (c) 1991, 1993 33 * The Regents of the University of California. All rights reserved. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. Neither the name of the University nor the names of its contributors 44 * may be used to endorse or promote products derived from this software 45 * without specific prior written permission. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 57 * SUCH DAMAGE. 58 * 59 * @(#)lfs_alloc.c 8.4 (Berkeley) 1/4/94 60 */ 61 62 #include <sys/cdefs.h> 63 __KERNEL_RCSID(0, "$NetBSD: lfs_alloc.c,v 1.112 2012/02/16 02:47:55 perseant Exp $"); 64 65 #if defined(_KERNEL_OPT) 66 #include "opt_quota.h" 67 #endif 68 69 #include <sys/param.h> 70 #include <sys/systm.h> 71 #include <sys/kernel.h> 72 #include <sys/buf.h> 73 #include <sys/lock.h> 74 #include <sys/vnode.h> 75 #include <sys/syslog.h> 76 #include <sys/mount.h> 77 #include <sys/malloc.h> 78 #include <sys/pool.h> 79 #include <sys/proc.h> 80 #include <sys/tree.h> 81 #include <sys/kauth.h> 82 83 #include <ufs/ufs/quota.h> 84 #include <ufs/ufs/inode.h> 85 #include <ufs/ufs/ufsmount.h> 86 #include <ufs/ufs/ufs_extern.h> 87 88 #include <ufs/lfs/lfs.h> 89 #include <ufs/lfs/lfs_extern.h> 90 91 /* Constants for inode free bitmap */ 92 #define BMSHIFT 5 /* 2 ** 5 = 32 */ 93 #define BMMASK ((1 << BMSHIFT) - 1) 94 #define SET_BITMAP_FREE(F, I) do { \ 95 DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d set\n", (int)(I), \ 96 (int)((I) >> BMSHIFT), (int)((I) & BMMASK))); \ 97 (F)->lfs_ino_bitmap[(I) >> BMSHIFT] |= (1 << ((I) & BMMASK)); \ 98 } while (0) 99 #define CLR_BITMAP_FREE(F, I) do { \ 100 DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d clr\n", (int)(I), \ 101 (int)((I) >> BMSHIFT), (int)((I) & BMMASK))); \ 102 (F)->lfs_ino_bitmap[(I) >> BMSHIFT] &= ~(1 << ((I) & BMMASK)); \ 103 } while(0) 104 105 #define ISSET_BITMAP_FREE(F, I) \ 106 ((F)->lfs_ino_bitmap[(I) >> BMSHIFT] & (1 << ((I) & BMMASK))) 107 108 /* 109 * Add a new block to the Ifile, to accommodate future file creations. 110 * Called with the segment lock held. 111 */ 112 int 113 lfs_extend_ifile(struct lfs *fs, kauth_cred_t cred) 114 { 115 struct vnode *vp; 116 struct inode *ip; 117 IFILE *ifp; 118 IFILE_V1 *ifp_v1; 119 struct buf *bp, *cbp; 120 int error; 121 daddr_t i, blkno, xmax; 122 ino_t oldlast, maxino; 123 CLEANERINFO *cip; 124 125 ASSERT_SEGLOCK(fs); 126 127 vp = fs->lfs_ivnode; 128 ip = VTOI(vp); 129 blkno = lblkno(fs, ip->i_size); 130 if ((error = lfs_balloc(vp, ip->i_size, fs->lfs_bsize, cred, 0, 131 &bp)) != 0) { 132 return (error); 133 } 134 ip->i_size += fs->lfs_bsize; 135 ip->i_ffs1_size = ip->i_size; 136 uvm_vnp_setsize(vp, ip->i_size); 137 138 maxino = ((ip->i_size >> fs->lfs_bshift) - fs->lfs_cleansz - 139 fs->lfs_segtabsz) * fs->lfs_ifpb; 140 fs->lfs_ino_bitmap = (lfs_bm_t *) 141 realloc(fs->lfs_ino_bitmap, ((maxino + BMMASK) >> BMSHIFT) * 142 sizeof(lfs_bm_t), M_SEGMENT, M_WAITOK); 143 KASSERT(fs->lfs_ino_bitmap != NULL); 144 145 i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) * 146 fs->lfs_ifpb; 147 148 /* 149 * We insert the new inodes at the head of the free list. 150 * Under normal circumstances, the free list is empty here, 151 * so we are also incidentally placing them at the end (which 152 * we must do if we are to keep them in order). 153 */ 154 LFS_GET_HEADFREE(fs, cip, cbp, &oldlast); 155 LFS_PUT_HEADFREE(fs, cip, cbp, i); 156 #ifdef DIAGNOSTIC 157 if (fs->lfs_freehd == LFS_UNUSED_INUM) 158 panic("inode 0 allocated [2]"); 159 #endif /* DIAGNOSTIC */ 160 xmax = i + fs->lfs_ifpb; 161 162 if (fs->lfs_version == 1) { 163 for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < xmax; ++ifp_v1) { 164 SET_BITMAP_FREE(fs, i); 165 ifp_v1->if_version = 1; 166 ifp_v1->if_daddr = LFS_UNUSED_DADDR; 167 ifp_v1->if_nextfree = ++i; 168 } 169 ifp_v1--; 170 ifp_v1->if_nextfree = oldlast; 171 } else { 172 for (ifp = (IFILE *)bp->b_data; i < xmax; ++ifp) { 173 SET_BITMAP_FREE(fs, i); 174 ifp->if_version = 1; 175 ifp->if_daddr = LFS_UNUSED_DADDR; 176 ifp->if_nextfree = ++i; 177 } 178 ifp--; 179 ifp->if_nextfree = oldlast; 180 } 181 LFS_PUT_TAILFREE(fs, cip, cbp, xmax - 1); 182 183 (void) LFS_BWRITE_LOG(bp); /* Ifile */ 184 185 return 0; 186 } 187 188 /* Allocate a new inode. */ 189 /* ARGSUSED */ 190 /* VOP_BWRITE 2i times */ 191 int 192 lfs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred, 193 struct vnode **vpp) 194 { 195 struct lfs *fs; 196 struct buf *bp, *cbp; 197 struct ifile *ifp; 198 ino_t new_ino; 199 int error; 200 int new_gen; 201 CLEANERINFO *cip; 202 203 fs = VTOI(pvp)->i_lfs; 204 if (fs->lfs_ronly) 205 return EROFS; 206 207 ASSERT_NO_SEGLOCK(fs); 208 209 lfs_seglock(fs, SEGM_PROT); 210 211 /* Get the head of the freelist. */ 212 LFS_GET_HEADFREE(fs, cip, cbp, &new_ino); 213 KASSERT(new_ino != LFS_UNUSED_INUM && new_ino != LFS_IFILE_INUM); 214 215 DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %lld\n", 216 (long long)new_ino)); 217 218 /* 219 * Remove the inode from the free list and write the new start 220 * of the free list into the superblock. 221 */ 222 CLR_BITMAP_FREE(fs, new_ino); 223 LFS_IENTRY(ifp, fs, new_ino, bp); 224 if (ifp->if_daddr != LFS_UNUSED_DADDR) 225 panic("lfs_valloc: inuse inode %llu on the free list", 226 (unsigned long long)new_ino); 227 LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree); 228 DLOG((DLOG_ALLOC, "lfs_valloc: headfree %lld -> %lld\n", 229 (long long)new_ino, (long long)ifp->if_nextfree)); 230 231 new_gen = ifp->if_version; /* version was updated by vfree */ 232 brelse(bp, 0); 233 234 /* Extend IFILE so that the next lfs_valloc will succeed. */ 235 if (fs->lfs_freehd == LFS_UNUSED_INUM) { 236 if ((error = lfs_extend_ifile(fs, cred)) != 0) { 237 LFS_PUT_HEADFREE(fs, cip, cbp, new_ino); 238 lfs_segunlock(fs); 239 return error; 240 } 241 } 242 #ifdef DIAGNOSTIC 243 if (fs->lfs_freehd == LFS_UNUSED_INUM) 244 panic("inode 0 allocated [3]"); 245 #endif /* DIAGNOSTIC */ 246 247 /* Set superblock modified bit and increment file count. */ 248 mutex_enter(&lfs_lock); 249 fs->lfs_fmod = 1; 250 mutex_exit(&lfs_lock); 251 ++fs->lfs_nfiles; 252 253 lfs_segunlock(fs); 254 255 return lfs_ialloc(fs, pvp, new_ino, new_gen, vpp); 256 } 257 258 /* 259 * Finish allocating a new inode, given an inode and generation number. 260 */ 261 int 262 lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen, 263 struct vnode **vpp) 264 { 265 struct inode *ip; 266 struct vnode *vp; 267 268 ASSERT_NO_SEGLOCK(fs); 269 270 vp = *vpp; 271 mutex_enter(&ufs_hashlock); 272 /* Create an inode to associate with the vnode. */ 273 lfs_vcreate(pvp->v_mount, new_ino, vp); 274 275 ip = VTOI(vp); 276 mutex_enter(&lfs_lock); 277 LFS_SET_UINO(ip, IN_CHANGE); 278 mutex_exit(&lfs_lock); 279 /* on-disk structure has been zeroed out by lfs_vcreate */ 280 ip->i_din.ffs1_din->di_inumber = new_ino; 281 282 /* Note no blocks yet */ 283 ip->i_lfs_hiblk = -1; 284 285 /* Set a new generation number for this inode. */ 286 if (new_gen) { 287 ip->i_gen = new_gen; 288 ip->i_ffs1_gen = new_gen; 289 } 290 291 /* Insert into the inode hash table. */ 292 ufs_ihashins(ip); 293 mutex_exit(&ufs_hashlock); 294 295 ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, vpp); 296 vp = *vpp; 297 ip = VTOI(vp); 298 299 memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize)); 300 301 uvm_vnp_setsize(vp, 0); 302 lfs_mark_vnode(vp); 303 genfs_node_init(vp, &lfs_genfsops); 304 vref(ip->i_devvp); 305 return (0); 306 } 307 308 /* Create a new vnode/inode pair and initialize what fields we can. */ 309 void 310 lfs_vcreate(struct mount *mp, ino_t ino, struct vnode *vp) 311 { 312 struct inode *ip; 313 struct ufs1_dinode *dp; 314 struct ufsmount *ump; 315 316 /* Get a pointer to the private mount structure. */ 317 ump = VFSTOUFS(mp); 318 319 ASSERT_NO_SEGLOCK(ump->um_lfs); 320 321 /* Initialize the inode. */ 322 ip = pool_get(&lfs_inode_pool, PR_WAITOK); 323 memset(ip, 0, sizeof(*ip)); 324 dp = pool_get(&lfs_dinode_pool, PR_WAITOK); 325 memset(dp, 0, sizeof(*dp)); 326 ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK); 327 memset(ip->inode_ext.lfs, 0, sizeof(*ip->inode_ext.lfs)); 328 vp->v_data = ip; 329 ip->i_din.ffs1_din = dp; 330 ip->i_ump = ump; 331 ip->i_vnode = vp; 332 ip->i_devvp = ump->um_devvp; 333 ip->i_dev = ump->um_dev; 334 ip->i_number = dp->di_inumber = ino; 335 ip->i_lfs = ump->um_lfs; 336 ip->i_lfs_effnblks = 0; 337 SPLAY_INIT(&ip->i_lfs_lbtree); 338 ip->i_lfs_nbtree = 0; 339 LIST_INIT(&ip->i_lfs_segdhd); 340 #ifdef QUOTA 341 ufsquota_init(ip); 342 #endif 343 } 344 345 #if 0 346 /* 347 * Find the highest-numbered allocated inode. 348 * This will be used to shrink the Ifile. 349 */ 350 static inline ino_t 351 lfs_last_alloc_ino(struct lfs *fs) 352 { 353 ino_t ino, maxino; 354 355 maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) - 356 fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb; 357 for (ino = maxino - 1; ino > LFS_UNUSED_INUM; --ino) { 358 if (ISSET_BITMAP_FREE(fs, ino) == 0) 359 break; 360 } 361 return ino; 362 } 363 #endif 364 365 /* 366 * Find the previous (next lowest numbered) free inode, if any. 367 * If there is none, return LFS_UNUSED_INUM. 368 */ 369 static inline ino_t 370 lfs_freelist_prev(struct lfs *fs, ino_t ino) 371 { 372 ino_t tino, bound, bb, freehdbb; 373 374 if (fs->lfs_freehd == LFS_UNUSED_INUM) /* No free inodes at all */ 375 return LFS_UNUSED_INUM; 376 377 /* Search our own word first */ 378 bound = ino & ~BMMASK; 379 for (tino = ino - 1; tino >= bound && tino > LFS_UNUSED_INUM; tino--) 380 if (ISSET_BITMAP_FREE(fs, tino)) 381 return tino; 382 /* If there are no lower words to search, just return */ 383 if (ino >> BMSHIFT == 0) 384 return LFS_UNUSED_INUM; 385 386 /* 387 * Find a word with a free inode in it. We have to be a bit 388 * careful here since ino_t is unsigned. 389 */ 390 freehdbb = (fs->lfs_freehd >> BMSHIFT); 391 for (bb = (ino >> BMSHIFT) - 1; bb >= freehdbb && bb > 0; --bb) 392 if (fs->lfs_ino_bitmap[bb]) 393 break; 394 if (fs->lfs_ino_bitmap[bb] == 0) 395 return LFS_UNUSED_INUM; 396 397 /* Search the word we found */ 398 for (tino = (bb << BMSHIFT) | BMMASK; tino >= (bb << BMSHIFT) && 399 tino > LFS_UNUSED_INUM; tino--) 400 if (ISSET_BITMAP_FREE(fs, tino)) 401 break; 402 403 if (tino <= LFS_IFILE_INUM) 404 tino = LFS_UNUSED_INUM; 405 406 return tino; 407 } 408 409 /* Free an inode. */ 410 /* ARGUSED */ 411 /* VOP_BWRITE 2i times */ 412 int 413 lfs_vfree(struct vnode *vp, ino_t ino, int mode) 414 { 415 SEGUSE *sup; 416 CLEANERINFO *cip; 417 struct buf *cbp, *bp; 418 struct ifile *ifp; 419 struct inode *ip; 420 struct lfs *fs; 421 daddr_t old_iaddr; 422 ino_t otail; 423 424 /* Get the inode number and file system. */ 425 ip = VTOI(vp); 426 fs = ip->i_lfs; 427 ino = ip->i_number; 428 429 ASSERT_NO_SEGLOCK(fs); 430 DLOG((DLOG_ALLOC, "lfs_vfree: free ino %lld\n", (long long)ino)); 431 432 /* Drain of pending writes */ 433 mutex_enter(vp->v_interlock); 434 while (fs->lfs_version > 1 && WRITEINPROG(vp)) { 435 cv_wait(&vp->v_cv, vp->v_interlock); 436 } 437 mutex_exit(vp->v_interlock); 438 439 lfs_seglock(fs, SEGM_PROT); 440 441 lfs_unmark_vnode(vp); 442 mutex_enter(&lfs_lock); 443 if (vp->v_uflag & VU_DIROP) { 444 vp->v_uflag &= ~VU_DIROP; 445 --lfs_dirvcount; 446 --fs->lfs_dirvcount; 447 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain); 448 wakeup(&fs->lfs_dirvcount); 449 wakeup(&lfs_dirvcount); 450 mutex_exit(&lfs_lock); 451 lfs_vunref(vp); 452 453 /* 454 * If this inode is not going to be written any more, any 455 * segment accounting left over from its truncation needs 456 * to occur at the end of the next dirops flush. Attach 457 * them to the fs-wide list for that purpose. 458 */ 459 if (LIST_FIRST(&ip->i_lfs_segdhd) != NULL) { 460 struct segdelta *sd; 461 462 while((sd = LIST_FIRST(&ip->i_lfs_segdhd)) != NULL) { 463 LIST_REMOVE(sd, list); 464 LIST_INSERT_HEAD(&fs->lfs_segdhd, sd, list); 465 } 466 } 467 } else { 468 /* 469 * If it's not a dirop, we can finalize right away. 470 */ 471 mutex_exit(&lfs_lock); 472 lfs_finalize_ino_seguse(fs, ip); 473 } 474 475 mutex_enter(&lfs_lock); 476 LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED); 477 mutex_exit(&lfs_lock); 478 ip->i_flag &= ~IN_ALLMOD; 479 ip->i_lfs_iflags |= LFSI_DELETED; 480 481 /* 482 * Set the ifile's inode entry to unused, increment its version number 483 * and link it onto the free chain. 484 */ 485 SET_BITMAP_FREE(fs, ino); 486 LFS_IENTRY(ifp, fs, ino, bp); 487 old_iaddr = ifp->if_daddr; 488 ifp->if_daddr = LFS_UNUSED_DADDR; 489 ++ifp->if_version; 490 if (fs->lfs_version == 1) { 491 LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree)); 492 LFS_PUT_HEADFREE(fs, cip, cbp, ino); 493 (void) LFS_BWRITE_LOG(bp); /* Ifile */ 494 } else { 495 ino_t tino, onf; 496 497 ifp->if_nextfree = LFS_UNUSED_INUM; 498 (void) LFS_BWRITE_LOG(bp); /* Ifile */ 499 500 tino = lfs_freelist_prev(fs, ino); 501 if (tino == LFS_UNUSED_INUM) { 502 /* Nothing free below us, put us on the head */ 503 LFS_IENTRY(ifp, fs, ino, bp); 504 LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree)); 505 LFS_PUT_HEADFREE(fs, cip, cbp, ino); 506 DLOG((DLOG_ALLOC, "lfs_vfree: headfree %lld -> %lld\n", 507 (long long)ifp->if_nextfree, (long long)ino)); 508 LFS_BWRITE_LOG(bp); /* Ifile */ 509 510 /* If the list was empty, set tail too */ 511 LFS_GET_TAILFREE(fs, cip, cbp, &otail); 512 if (otail == LFS_UNUSED_INUM) { 513 LFS_PUT_TAILFREE(fs, cip, cbp, ino); 514 DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld " 515 "-> %lld\n", (long long)otail, 516 (long long)ino)); 517 } 518 } else { 519 /* 520 * Insert this inode into the list after tino. 521 * We hold the segment lock so we don't have to 522 * worry about blocks being written out of order. 523 */ 524 DLOG((DLOG_ALLOC, "lfs_vfree: insert ino %lld " 525 " after %lld\n", ino, tino)); 526 527 LFS_IENTRY(ifp, fs, tino, bp); 528 onf = ifp->if_nextfree; 529 ifp->if_nextfree = ino; 530 LFS_BWRITE_LOG(bp); /* Ifile */ 531 532 LFS_IENTRY(ifp, fs, ino, bp); 533 ifp->if_nextfree = onf; 534 LFS_BWRITE_LOG(bp); /* Ifile */ 535 536 /* If we're last, put us on the tail */ 537 if (onf == LFS_UNUSED_INUM) { 538 LFS_GET_TAILFREE(fs, cip, cbp, &otail); 539 LFS_PUT_TAILFREE(fs, cip, cbp, ino); 540 DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld " 541 "-> %lld\n", (long long)otail, 542 (long long)ino)); 543 } 544 } 545 } 546 #ifdef DIAGNOSTIC 547 if (ino == LFS_UNUSED_INUM) { 548 panic("inode 0 freed"); 549 } 550 #endif /* DIAGNOSTIC */ 551 if (old_iaddr != LFS_UNUSED_DADDR) { 552 LFS_SEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp); 553 #ifdef DIAGNOSTIC 554 if (sup->su_nbytes < sizeof (struct ufs1_dinode)) { 555 printf("lfs_vfree: negative byte count" 556 " (segment %" PRIu32 " short by %d)\n", 557 dtosn(fs, old_iaddr), 558 (int)sizeof (struct ufs1_dinode) - 559 sup->su_nbytes); 560 panic("lfs_vfree: negative byte count"); 561 sup->su_nbytes = sizeof (struct ufs1_dinode); 562 } 563 #endif 564 sup->su_nbytes -= sizeof (struct ufs1_dinode); 565 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp); /* Ifile */ 566 } 567 568 /* Set superblock modified bit and decrement file count. */ 569 mutex_enter(&lfs_lock); 570 fs->lfs_fmod = 1; 571 mutex_exit(&lfs_lock); 572 --fs->lfs_nfiles; 573 574 lfs_segunlock(fs); 575 576 return (0); 577 } 578 579 /* 580 * Sort the freelist and set up the free-inode bitmap. 581 * To be called by lfs_mountfs(). 582 */ 583 void 584 lfs_order_freelist(struct lfs *fs) 585 { 586 CLEANERINFO *cip; 587 IFILE *ifp = NULL; 588 struct buf *bp; 589 ino_t ino, firstino, lastino, maxino; 590 #ifdef notyet 591 struct vnode *vp; 592 #endif 593 594 ASSERT_NO_SEGLOCK(fs); 595 lfs_seglock(fs, SEGM_PROT); 596 597 maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) - 598 fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb; 599 fs->lfs_ino_bitmap = (lfs_bm_t *) 600 malloc(((maxino + BMMASK) >> BMSHIFT) * sizeof(lfs_bm_t), 601 M_SEGMENT, M_WAITOK | M_ZERO); 602 KASSERT(fs->lfs_ino_bitmap != NULL); 603 604 firstino = lastino = LFS_UNUSED_INUM; 605 for (ino = 0; ino < maxino; ino++) { 606 if (ino % fs->lfs_ifpb == 0) 607 LFS_IENTRY(ifp, fs, ino, bp); 608 else 609 ++ifp; 610 611 /* Don't put zero or ifile on the free list */ 612 if (ino == LFS_UNUSED_INUM || ino == LFS_IFILE_INUM) 613 continue; 614 615 #ifdef notyet 616 /* Address orphaned files */ 617 if (ifp->if_nextfree == LFS_ORPHAN_NEXTFREE && 618 VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp) == 0) { 619 lfs_truncate(vp, 0, 0, NOCRED); 620 vput(vp); 621 LFS_SEGENTRY(sup, fs, dtosn(fs, ifp->if_daddr), bp); 622 KASSERT(sup->su_nbytes >= DINODE1_SIZE); 623 sup->su_nbytes -= DINODE1_SIZE; 624 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, ifp->if_daddr), bp); 625 626 /* Set up to fall through to next section */ 627 ifp->if_daddr = LFS_UNUSED_DADDR; 628 LFS_BWRITE_LOG(bp); 629 LFS_IENTRY(ifp, fs, ino, bp); 630 } 631 #endif 632 633 if (ifp->if_daddr == LFS_UNUSED_DADDR) { 634 if (firstino == LFS_UNUSED_INUM) 635 firstino = ino; 636 else { 637 brelse(bp, 0); 638 639 LFS_IENTRY(ifp, fs, lastino, bp); 640 ifp->if_nextfree = ino; 641 LFS_BWRITE_LOG(bp); 642 643 LFS_IENTRY(ifp, fs, ino, bp); 644 } 645 lastino = ino; 646 647 SET_BITMAP_FREE(fs, ino); 648 } 649 650 if ((ino + 1) % fs->lfs_ifpb == 0) 651 brelse(bp, 0); 652 } 653 654 LFS_PUT_HEADFREE(fs, cip, bp, firstino); 655 LFS_PUT_TAILFREE(fs, cip, bp, lastino); 656 657 lfs_segunlock(fs); 658 } 659 660 void 661 lfs_orphan(struct lfs *fs, ino_t ino) 662 { 663 IFILE *ifp; 664 struct buf *bp; 665 666 LFS_IENTRY(ifp, fs, ino, bp); 667 ifp->if_nextfree = LFS_ORPHAN_NEXTFREE; 668 LFS_BWRITE_LOG(bp); 669 } 670