1 /* $NetBSD: lfs_alloc.c,v 1.119 2013/07/28 01:25:05 dholland 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.119 2013/07/28 01:25:05 dholland 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/lfs/ulfs_quotacommon.h> 84 #include <ufs/lfs/ulfs_inode.h> 85 #include <ufs/lfs/ulfsmount.h> 86 #include <ufs/lfs/ulfs_extern.h> 87 88 #include <ufs/lfs/lfs.h> 89 #include <ufs/lfs/lfs_extern.h> 90 #include <ufs/lfs/lfs_kernel.h> 91 92 /* Constants for inode free bitmap */ 93 #define BMSHIFT 5 /* 2 ** 5 = 32 */ 94 #define BMMASK ((1 << BMSHIFT) - 1) 95 #define SET_BITMAP_FREE(F, I) do { \ 96 DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d set\n", (int)(I), \ 97 (int)((I) >> BMSHIFT), (int)((I) & BMMASK))); \ 98 (F)->lfs_ino_bitmap[(I) >> BMSHIFT] |= (1 << ((I) & BMMASK)); \ 99 } while (0) 100 #define CLR_BITMAP_FREE(F, I) do { \ 101 DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d clr\n", (int)(I), \ 102 (int)((I) >> BMSHIFT), (int)((I) & BMMASK))); \ 103 (F)->lfs_ino_bitmap[(I) >> BMSHIFT] &= ~(1 << ((I) & BMMASK)); \ 104 } while(0) 105 106 #define ISSET_BITMAP_FREE(F, I) \ 107 ((F)->lfs_ino_bitmap[(I) >> BMSHIFT] & (1 << ((I) & BMMASK))) 108 109 /* 110 * Add a new block to the Ifile, to accommodate future file creations. 111 * Called with the segment lock held. 112 */ 113 int 114 lfs_extend_ifile(struct lfs *fs, kauth_cred_t cred) 115 { 116 struct vnode *vp; 117 struct inode *ip; 118 IFILE *ifp; 119 IFILE_V1 *ifp_v1; 120 struct buf *bp, *cbp; 121 int error; 122 daddr_t i, blkno, xmax; 123 ino_t oldlast, maxino; 124 CLEANERINFO *cip; 125 126 ASSERT_SEGLOCK(fs); 127 128 vp = fs->lfs_ivnode; 129 ip = VTOI(vp); 130 blkno = lfs_lblkno(fs, ip->i_size); 131 if ((error = lfs_balloc(vp, ip->i_size, fs->lfs_bsize, cred, 0, 132 &bp)) != 0) { 133 return (error); 134 } 135 ip->i_size += fs->lfs_bsize; 136 ip->i_ffs1_size = ip->i_size; 137 uvm_vnp_setsize(vp, ip->i_size); 138 139 maxino = ((ip->i_size >> fs->lfs_bshift) - fs->lfs_cleansz - 140 fs->lfs_segtabsz) * fs->lfs_ifpb; 141 fs->lfs_ino_bitmap = (lfs_bm_t *) 142 realloc(fs->lfs_ino_bitmap, ((maxino + BMMASK) >> BMSHIFT) * 143 sizeof(lfs_bm_t), M_SEGMENT, M_WAITOK); 144 KASSERT(fs->lfs_ino_bitmap != NULL); 145 146 i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) * 147 fs->lfs_ifpb; 148 149 /* 150 * We insert the new inodes at the head of the free list. 151 * Under normal circumstances, the free list is empty here, 152 * so we are also incidentally placing them at the end (which 153 * we must do if we are to keep them in order). 154 */ 155 LFS_GET_HEADFREE(fs, cip, cbp, &oldlast); 156 LFS_PUT_HEADFREE(fs, cip, cbp, i); 157 #ifdef DIAGNOSTIC 158 if (fs->lfs_freehd == LFS_UNUSED_INUM) 159 panic("inode 0 allocated [2]"); 160 #endif /* DIAGNOSTIC */ 161 xmax = i + fs->lfs_ifpb; 162 163 if (fs->lfs_version == 1) { 164 for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < xmax; ++ifp_v1) { 165 SET_BITMAP_FREE(fs, i); 166 ifp_v1->if_version = 1; 167 ifp_v1->if_daddr = LFS_UNUSED_DADDR; 168 ifp_v1->if_nextfree = ++i; 169 } 170 ifp_v1--; 171 ifp_v1->if_nextfree = oldlast; 172 } else { 173 for (ifp = (IFILE *)bp->b_data; i < xmax; ++ifp) { 174 SET_BITMAP_FREE(fs, i); 175 ifp->if_version = 1; 176 ifp->if_daddr = LFS_UNUSED_DADDR; 177 ifp->if_nextfree = ++i; 178 } 179 ifp--; 180 ifp->if_nextfree = oldlast; 181 } 182 LFS_PUT_TAILFREE(fs, cip, cbp, xmax - 1); 183 184 (void) LFS_BWRITE_LOG(bp); /* Ifile */ 185 186 return 0; 187 } 188 189 /* Allocate a new inode. */ 190 /* ARGSUSED */ 191 /* VOP_BWRITE 2i times */ 192 int 193 lfs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred, 194 struct vnode **vpp) 195 { 196 struct lfs *fs; 197 struct buf *bp, *cbp; 198 struct ifile *ifp; 199 ino_t new_ino; 200 int error; 201 int new_gen; 202 CLEANERINFO *cip; 203 204 fs = VTOI(pvp)->i_lfs; 205 if (fs->lfs_ronly) 206 return EROFS; 207 208 ASSERT_NO_SEGLOCK(fs); 209 210 lfs_seglock(fs, SEGM_PROT); 211 212 /* Get the head of the freelist. */ 213 LFS_GET_HEADFREE(fs, cip, cbp, &new_ino); 214 KASSERT(new_ino != LFS_UNUSED_INUM && new_ino != LFS_IFILE_INUM); 215 216 DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %lld\n", 217 (long long)new_ino)); 218 219 /* 220 * Remove the inode from the free list and write the new start 221 * of the free list into the superblock. 222 */ 223 CLR_BITMAP_FREE(fs, new_ino); 224 LFS_IENTRY(ifp, fs, new_ino, bp); 225 if (ifp->if_daddr != LFS_UNUSED_DADDR) 226 panic("lfs_valloc: inuse inode %llu on the free list", 227 (unsigned long long)new_ino); 228 LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree); 229 DLOG((DLOG_ALLOC, "lfs_valloc: headfree %lld -> %lld\n", 230 (long long)new_ino, (long long)ifp->if_nextfree)); 231 232 new_gen = ifp->if_version; /* version was updated by vfree */ 233 brelse(bp, 0); 234 235 /* Extend IFILE so that the next lfs_valloc will succeed. */ 236 if (fs->lfs_freehd == LFS_UNUSED_INUM) { 237 if ((error = lfs_extend_ifile(fs, cred)) != 0) { 238 LFS_PUT_HEADFREE(fs, cip, cbp, new_ino); 239 lfs_segunlock(fs); 240 return error; 241 } 242 } 243 #ifdef DIAGNOSTIC 244 if (fs->lfs_freehd == LFS_UNUSED_INUM) 245 panic("inode 0 allocated [3]"); 246 #endif /* DIAGNOSTIC */ 247 248 /* Set superblock modified bit and increment file count. */ 249 mutex_enter(&lfs_lock); 250 fs->lfs_fmod = 1; 251 mutex_exit(&lfs_lock); 252 ++fs->lfs_nfiles; 253 254 lfs_segunlock(fs); 255 256 return lfs_ialloc(fs, pvp, new_ino, new_gen, vpp); 257 } 258 259 /* 260 * Finish allocating a new inode, given an inode and generation number. 261 */ 262 int 263 lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen, 264 struct vnode **vpp) 265 { 266 struct inode *ip; 267 struct vnode *vp; 268 269 ASSERT_NO_SEGLOCK(fs); 270 271 vp = *vpp; 272 mutex_enter(&ulfs_hashlock); 273 /* Create an inode to associate with the vnode. */ 274 lfs_vcreate(pvp->v_mount, new_ino, vp); 275 276 ip = VTOI(vp); 277 mutex_enter(&lfs_lock); 278 LFS_SET_UINO(ip, IN_CHANGE); 279 mutex_exit(&lfs_lock); 280 /* on-disk structure has been zeroed out by lfs_vcreate */ 281 ip->i_din.ffs1_din->di_inumber = new_ino; 282 283 /* Note no blocks yet */ 284 ip->i_lfs_hiblk = -1; 285 286 /* Set a new generation number for this inode. */ 287 if (new_gen) { 288 ip->i_gen = new_gen; 289 ip->i_ffs1_gen = new_gen; 290 } 291 292 /* Insert into the inode hash table. */ 293 ulfs_ihashins(ip); 294 mutex_exit(&ulfs_hashlock); 295 296 ulfs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, vpp); 297 vp = *vpp; 298 ip = VTOI(vp); 299 300 memset(ip->i_lfs_fragsize, 0, ULFS_NDADDR * sizeof(*ip->i_lfs_fragsize)); 301 302 uvm_vnp_setsize(vp, 0); 303 lfs_mark_vnode(vp); 304 genfs_node_init(vp, &lfs_genfsops); 305 vref(ip->i_devvp); 306 return (0); 307 } 308 309 /* Create a new vnode/inode pair and initialize what fields we can. */ 310 void 311 lfs_vcreate(struct mount *mp, ino_t ino, struct vnode *vp) 312 { 313 struct inode *ip; 314 struct ulfs1_dinode *dp; 315 struct ulfsmount *ump; 316 317 /* Get a pointer to the private mount structure. */ 318 ump = VFSTOULFS(mp); 319 320 ASSERT_NO_SEGLOCK(ump->um_lfs); 321 322 /* Initialize the inode. */ 323 ip = pool_get(&lfs_inode_pool, PR_WAITOK); 324 memset(ip, 0, sizeof(*ip)); 325 dp = pool_get(&lfs_dinode_pool, PR_WAITOK); 326 memset(dp, 0, sizeof(*dp)); 327 ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK); 328 memset(ip->inode_ext.lfs, 0, sizeof(*ip->inode_ext.lfs)); 329 vp->v_data = ip; 330 ip->i_din.ffs1_din = dp; 331 ip->i_ump = ump; 332 ip->i_vnode = vp; 333 ip->i_devvp = ump->um_devvp; 334 ip->i_dev = ump->um_dev; 335 ip->i_number = dp->di_inumber = ino; 336 ip->i_lfs = ump->um_lfs; 337 ip->i_lfs_effnblks = 0; 338 SPLAY_INIT(&ip->i_lfs_lbtree); 339 ip->i_lfs_nbtree = 0; 340 LIST_INIT(&ip->i_lfs_segdhd); 341 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2) 342 ulfsquota_init(ip); 343 #endif 344 } 345 346 #if 0 347 /* 348 * Find the highest-numbered allocated inode. 349 * This will be used to shrink the Ifile. 350 */ 351 static inline ino_t 352 lfs_last_alloc_ino(struct lfs *fs) 353 { 354 ino_t ino, maxino; 355 356 maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) - 357 fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb; 358 for (ino = maxino - 1; ino > LFS_UNUSED_INUM; --ino) { 359 if (ISSET_BITMAP_FREE(fs, ino) == 0) 360 break; 361 } 362 return ino; 363 } 364 #endif 365 366 /* 367 * Find the previous (next lowest numbered) free inode, if any. 368 * If there is none, return LFS_UNUSED_INUM. 369 */ 370 static inline ino_t 371 lfs_freelist_prev(struct lfs *fs, ino_t ino) 372 { 373 ino_t tino, bound, bb, freehdbb; 374 375 if (fs->lfs_freehd == LFS_UNUSED_INUM) /* No free inodes at all */ 376 return LFS_UNUSED_INUM; 377 378 /* Search our own word first */ 379 bound = ino & ~BMMASK; 380 for (tino = ino - 1; tino >= bound && tino > LFS_UNUSED_INUM; tino--) 381 if (ISSET_BITMAP_FREE(fs, tino)) 382 return tino; 383 /* If there are no lower words to search, just return */ 384 if (ino >> BMSHIFT == 0) 385 return LFS_UNUSED_INUM; 386 387 /* 388 * Find a word with a free inode in it. We have to be a bit 389 * careful here since ino_t is unsigned. 390 */ 391 freehdbb = (fs->lfs_freehd >> BMSHIFT); 392 for (bb = (ino >> BMSHIFT) - 1; bb >= freehdbb && bb > 0; --bb) 393 if (fs->lfs_ino_bitmap[bb]) 394 break; 395 if (fs->lfs_ino_bitmap[bb] == 0) 396 return LFS_UNUSED_INUM; 397 398 /* Search the word we found */ 399 for (tino = (bb << BMSHIFT) | BMMASK; tino >= (bb << BMSHIFT) && 400 tino > LFS_UNUSED_INUM; tino--) 401 if (ISSET_BITMAP_FREE(fs, tino)) 402 break; 403 404 if (tino <= LFS_IFILE_INUM) 405 tino = LFS_UNUSED_INUM; 406 407 return tino; 408 } 409 410 /* Free an inode. */ 411 /* ARGUSED */ 412 /* VOP_BWRITE 2i times */ 413 int 414 lfs_vfree(struct vnode *vp, ino_t ino, int mode) 415 { 416 SEGUSE *sup; 417 CLEANERINFO *cip; 418 struct buf *cbp, *bp; 419 struct ifile *ifp; 420 struct inode *ip; 421 struct lfs *fs; 422 daddr_t old_iaddr; 423 ino_t otail; 424 425 /* Get the inode number and file system. */ 426 ip = VTOI(vp); 427 fs = ip->i_lfs; 428 ino = ip->i_number; 429 430 ASSERT_NO_SEGLOCK(fs); 431 DLOG((DLOG_ALLOC, "lfs_vfree: free ino %lld\n", (long long)ino)); 432 433 /* Drain of pending writes */ 434 mutex_enter(vp->v_interlock); 435 while (fs->lfs_version > 1 && WRITEINPROG(vp)) { 436 cv_wait(&vp->v_cv, vp->v_interlock); 437 } 438 mutex_exit(vp->v_interlock); 439 440 lfs_seglock(fs, SEGM_PROT); 441 442 lfs_unmark_vnode(vp); 443 mutex_enter(&lfs_lock); 444 if (vp->v_uflag & VU_DIROP) { 445 vp->v_uflag &= ~VU_DIROP; 446 --lfs_dirvcount; 447 --fs->lfs_dirvcount; 448 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain); 449 wakeup(&fs->lfs_dirvcount); 450 wakeup(&lfs_dirvcount); 451 mutex_exit(&lfs_lock); 452 lfs_vunref(vp); 453 454 /* 455 * If this inode is not going to be written any more, any 456 * segment accounting left over from its truncation needs 457 * to occur at the end of the next dirops flush. Attach 458 * them to the fs-wide list for that purpose. 459 */ 460 if (LIST_FIRST(&ip->i_lfs_segdhd) != NULL) { 461 struct segdelta *sd; 462 463 while((sd = LIST_FIRST(&ip->i_lfs_segdhd)) != NULL) { 464 LIST_REMOVE(sd, list); 465 LIST_INSERT_HEAD(&fs->lfs_segdhd, sd, list); 466 } 467 } 468 } else { 469 /* 470 * If it's not a dirop, we can finalize right away. 471 */ 472 mutex_exit(&lfs_lock); 473 lfs_finalize_ino_seguse(fs, ip); 474 } 475 476 mutex_enter(&lfs_lock); 477 LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED); 478 mutex_exit(&lfs_lock); 479 ip->i_flag &= ~IN_ALLMOD; 480 ip->i_lfs_iflags |= LFSI_DELETED; 481 482 /* 483 * Set the ifile's inode entry to unused, increment its version number 484 * and link it onto the free chain. 485 */ 486 SET_BITMAP_FREE(fs, ino); 487 LFS_IENTRY(ifp, fs, ino, bp); 488 old_iaddr = ifp->if_daddr; 489 ifp->if_daddr = LFS_UNUSED_DADDR; 490 ++ifp->if_version; 491 if (fs->lfs_version == 1) { 492 LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree)); 493 LFS_PUT_HEADFREE(fs, cip, cbp, ino); 494 (void) LFS_BWRITE_LOG(bp); /* Ifile */ 495 } else { 496 ino_t tino, onf; 497 498 ifp->if_nextfree = LFS_UNUSED_INUM; 499 (void) LFS_BWRITE_LOG(bp); /* Ifile */ 500 501 tino = lfs_freelist_prev(fs, ino); 502 if (tino == LFS_UNUSED_INUM) { 503 /* Nothing free below us, put us on the head */ 504 LFS_IENTRY(ifp, fs, ino, bp); 505 LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree)); 506 LFS_PUT_HEADFREE(fs, cip, cbp, ino); 507 DLOG((DLOG_ALLOC, "lfs_vfree: headfree %lld -> %lld\n", 508 (long long)ifp->if_nextfree, (long long)ino)); 509 LFS_BWRITE_LOG(bp); /* Ifile */ 510 511 /* If the list was empty, set tail too */ 512 LFS_GET_TAILFREE(fs, cip, cbp, &otail); 513 if (otail == LFS_UNUSED_INUM) { 514 LFS_PUT_TAILFREE(fs, cip, cbp, ino); 515 DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld " 516 "-> %lld\n", (long long)otail, 517 (long long)ino)); 518 } 519 } else { 520 /* 521 * Insert this inode into the list after tino. 522 * We hold the segment lock so we don't have to 523 * worry about blocks being written out of order. 524 */ 525 DLOG((DLOG_ALLOC, "lfs_vfree: insert ino %lld " 526 " after %lld\n", ino, tino)); 527 528 LFS_IENTRY(ifp, fs, tino, bp); 529 onf = ifp->if_nextfree; 530 ifp->if_nextfree = ino; 531 LFS_BWRITE_LOG(bp); /* Ifile */ 532 533 LFS_IENTRY(ifp, fs, ino, bp); 534 ifp->if_nextfree = onf; 535 LFS_BWRITE_LOG(bp); /* Ifile */ 536 537 /* If we're last, put us on the tail */ 538 if (onf == LFS_UNUSED_INUM) { 539 LFS_GET_TAILFREE(fs, cip, cbp, &otail); 540 LFS_PUT_TAILFREE(fs, cip, cbp, ino); 541 DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld " 542 "-> %lld\n", (long long)otail, 543 (long long)ino)); 544 } 545 } 546 } 547 #ifdef DIAGNOSTIC 548 if (ino == LFS_UNUSED_INUM) { 549 panic("inode 0 freed"); 550 } 551 #endif /* DIAGNOSTIC */ 552 if (old_iaddr != LFS_UNUSED_DADDR) { 553 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, old_iaddr), bp); 554 #ifdef DIAGNOSTIC 555 if (sup->su_nbytes < sizeof (struct ulfs1_dinode)) { 556 printf("lfs_vfree: negative byte count" 557 " (segment %" PRIu32 " short by %d)\n", 558 lfs_dtosn(fs, old_iaddr), 559 (int)sizeof (struct ulfs1_dinode) - 560 sup->su_nbytes); 561 panic("lfs_vfree: negative byte count"); 562 sup->su_nbytes = sizeof (struct ulfs1_dinode); 563 } 564 #endif 565 sup->su_nbytes -= sizeof (struct ulfs1_dinode); 566 LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, old_iaddr), bp); /* Ifile */ 567 } 568 569 /* Set superblock modified bit and decrement file count. */ 570 mutex_enter(&lfs_lock); 571 fs->lfs_fmod = 1; 572 mutex_exit(&lfs_lock); 573 --fs->lfs_nfiles; 574 575 lfs_segunlock(fs); 576 577 return (0); 578 } 579 580 /* 581 * Sort the freelist and set up the free-inode bitmap. 582 * To be called by lfs_mountfs(). 583 */ 584 void 585 lfs_order_freelist(struct lfs *fs) 586 { 587 CLEANERINFO *cip; 588 IFILE *ifp = NULL; 589 struct buf *bp; 590 ino_t ino, firstino, lastino, maxino; 591 #ifdef notyet 592 struct vnode *vp; 593 #endif 594 595 ASSERT_NO_SEGLOCK(fs); 596 lfs_seglock(fs, SEGM_PROT); 597 598 maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) - 599 fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb; 600 fs->lfs_ino_bitmap = (lfs_bm_t *) 601 malloc(((maxino + BMMASK) >> BMSHIFT) * sizeof(lfs_bm_t), 602 M_SEGMENT, M_WAITOK | M_ZERO); 603 KASSERT(fs->lfs_ino_bitmap != NULL); 604 605 firstino = lastino = LFS_UNUSED_INUM; 606 for (ino = 0; ino < maxino; ino++) { 607 if (ino % fs->lfs_ifpb == 0) 608 LFS_IENTRY(ifp, fs, ino, bp); 609 else 610 ++ifp; 611 612 /* Don't put zero or ifile on the free list */ 613 if (ino == LFS_UNUSED_INUM || ino == LFS_IFILE_INUM) 614 continue; 615 616 #ifdef notyet 617 /* Address orphaned files */ 618 if (ifp->if_nextfree == LFS_ORPHAN_NEXTFREE && 619 VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp) == 0) { 620 lfs_truncate(vp, 0, 0, NOCRED); 621 vput(vp); 622 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, ifp->if_daddr), bp); 623 KASSERT(sup->su_nbytes >= DINODE1_SIZE); 624 sup->su_nbytes -= DINODE1_SIZE; 625 LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, ifp->if_daddr), bp); 626 627 /* Set up to fall through to next section */ 628 ifp->if_daddr = LFS_UNUSED_DADDR; 629 LFS_BWRITE_LOG(bp); 630 LFS_IENTRY(ifp, fs, ino, bp); 631 } 632 #endif 633 634 if (ifp->if_daddr == LFS_UNUSED_DADDR) { 635 if (firstino == LFS_UNUSED_INUM) 636 firstino = ino; 637 else { 638 brelse(bp, 0); 639 640 LFS_IENTRY(ifp, fs, lastino, bp); 641 ifp->if_nextfree = ino; 642 LFS_BWRITE_LOG(bp); 643 644 LFS_IENTRY(ifp, fs, ino, bp); 645 } 646 lastino = ino; 647 648 SET_BITMAP_FREE(fs, ino); 649 } 650 651 if ((ino + 1) % fs->lfs_ifpb == 0) 652 brelse(bp, 0); 653 } 654 655 LFS_PUT_HEADFREE(fs, cip, bp, firstino); 656 LFS_PUT_TAILFREE(fs, cip, bp, lastino); 657 658 lfs_segunlock(fs); 659 } 660 661 void 662 lfs_orphan(struct lfs *fs, ino_t ino) 663 { 664 IFILE *ifp; 665 struct buf *bp; 666 667 LFS_IENTRY(ifp, fs, ino, bp); 668 ifp->if_nextfree = LFS_ORPHAN_NEXTFREE; 669 LFS_BWRITE_LOG(bp); 670 } 671