1 /* $NetBSD: lfs_alloc.c,v 1.72 2003/09/23 05:26:49 yamt Exp $ */ 2 3 /*- 4 * Copyright (c) 1999, 2000, 2001, 2002, 2003 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 /* 39 * Copyright (c) 1991, 1993 40 * The Regents of the University of California. All rights reserved. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 * 66 * @(#)lfs_alloc.c 8.4 (Berkeley) 1/4/94 67 */ 68 69 #include <sys/cdefs.h> 70 __KERNEL_RCSID(0, "$NetBSD: lfs_alloc.c,v 1.72 2003/09/23 05:26:49 yamt Exp $"); 71 72 #if defined(_KERNEL_OPT) 73 #include "opt_quota.h" 74 #endif 75 76 #include <sys/param.h> 77 #include <sys/systm.h> 78 #include <sys/kernel.h> 79 #include <sys/buf.h> 80 #include <sys/lock.h> 81 #include <sys/vnode.h> 82 #include <sys/syslog.h> 83 #include <sys/mount.h> 84 #include <sys/pool.h> 85 #include <sys/proc.h> 86 87 #include <ufs/ufs/quota.h> 88 #include <ufs/ufs/inode.h> 89 #include <ufs/ufs/ufsmount.h> 90 #include <ufs/ufs/ufs_extern.h> 91 92 #include <ufs/lfs/lfs.h> 93 #include <ufs/lfs/lfs_extern.h> 94 95 extern struct lock ufs_hashlock; 96 97 static int extend_ifile(struct lfs *, struct ucred *); 98 static int lfs_ialloc(struct lfs *, struct vnode *, ino_t, int, 99 struct vnode **); 100 101 /* 102 * Allocate a particular inode with a particular version number, freeing 103 * any previous versions of this inode that may have gone before. 104 * Used by the roll-forward code. 105 * 106 * XXX this function does not have appropriate locking to be used on a live fs; 107 * XXX but something similar could probably be used for an "undelete" call. 108 * 109 * Called with the Ifile inode locked. 110 */ 111 int 112 lfs_rf_valloc(struct lfs *fs, ino_t ino, int version, struct proc *p, 113 struct vnode **vpp) 114 { 115 IFILE *ifp; 116 struct buf *bp, *cbp; 117 struct vnode *vp; 118 struct inode *ip; 119 ino_t tino, oldnext; 120 int error; 121 CLEANERINFO *cip; 122 123 /* 124 * First, just try a vget. If the version number is the one we want, 125 * we don't have to do anything else. If the version number is wrong, 126 * take appropriate action. 127 */ 128 error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp); 129 if (error == 0) { 130 /* printf("lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp); */ 131 132 *vpp = vp; 133 ip = VTOI(vp); 134 if (ip->i_gen == version) 135 return 0; 136 else if (ip->i_gen < version) { 137 VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, p); 138 ip->i_gen = ip->i_ffs1_gen = version; 139 LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED | IN_UPDATE); 140 return 0; 141 } else { 142 /* printf("ino %d: asked for version %d but got %d\n", 143 ino, version, ip->i_ffs1_gen); */ 144 vput(vp); 145 *vpp = NULLVP; 146 return EEXIST; 147 } 148 } 149 150 /* 151 * The inode is not in use. Find it on the free list. 152 */ 153 /* If the Ifile is too short to contain this inum, extend it */ 154 while (VTOI(fs->lfs_ivnode)->i_size <= (ino / 155 fs->lfs_ifpb + fs->lfs_cleansz + fs->lfs_segtabsz) 156 << fs->lfs_bshift) { 157 extend_ifile(fs, NOCRED); 158 } 159 160 LFS_IENTRY(ifp, fs, ino, bp); 161 oldnext = ifp->if_nextfree; 162 ifp->if_version = version; 163 brelse(bp); 164 165 LFS_GET_HEADFREE(fs, cip, cbp, &ino); 166 if (ino) { 167 LFS_PUT_HEADFREE(fs, cip, cbp, oldnext); 168 } else { 169 tino = ino; 170 while (1) { 171 LFS_IENTRY(ifp, fs, tino, bp); 172 if (ifp->if_nextfree == ino || 173 ifp->if_nextfree == LFS_UNUSED_INUM) 174 break; 175 tino = ifp->if_nextfree; 176 brelse(bp); 177 } 178 if (ifp->if_nextfree == LFS_UNUSED_INUM) { 179 brelse(bp); 180 return ENOENT; 181 } 182 ifp->if_nextfree = oldnext; 183 LFS_BWRITE_LOG(bp); 184 } 185 186 error = lfs_ialloc(fs, fs->lfs_ivnode, ino, version, &vp); 187 if (error == 0) { 188 /* 189 * Make it VREG so we can put blocks on it. We will change 190 * this later if it turns out to be some other kind of file. 191 */ 192 ip = VTOI(vp); 193 ip->i_mode = ip->i_ffs1_mode = IFREG; 194 ip->i_nlink = ip->i_ffs1_nlink = 1; 195 ip->i_ffs_effnlink = 1; 196 ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp); 197 ip = VTOI(vp); 198 199 /* printf("lfs_rf_valloc: ino %d vp %p\n", ino, vp); */ 200 201 /* The dirop-nature of this vnode is past */ 202 lfs_unmark_vnode(vp); 203 (void)lfs_vunref(vp); 204 --lfs_dirvcount; 205 vp->v_flag &= ~VDIROP; 206 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain); 207 } 208 *vpp = vp; 209 return error; 210 } 211 212 /* 213 * Add a new block to the Ifile, to accommodate future file creations. 214 * Called with the segment lock held. 215 */ 216 static int 217 extend_ifile(struct lfs *fs, struct ucred *cred) 218 { 219 struct vnode *vp; 220 struct inode *ip; 221 IFILE *ifp; 222 IFILE_V1 *ifp_v1; 223 struct buf *bp, *cbp; 224 int error; 225 daddr_t i, blkno, max; 226 ino_t oldlast; 227 CLEANERINFO *cip; 228 229 vp = fs->lfs_ivnode; 230 ip = VTOI(vp); 231 blkno = lblkno(fs, ip->i_size); 232 if ((error = VOP_BALLOC(vp, ip->i_size, fs->lfs_bsize, cred, 0, 233 &bp)) != 0) { 234 return (error); 235 } 236 ip->i_size += fs->lfs_bsize; 237 ip->i_ffs1_size = ip->i_size; 238 uvm_vnp_setsize(vp, ip->i_size); 239 240 i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) * 241 fs->lfs_ifpb; 242 LFS_GET_HEADFREE(fs, cip, cbp, &oldlast); 243 LFS_PUT_HEADFREE(fs, cip, cbp, i); 244 #ifdef DIAGNOSTIC 245 if (fs->lfs_freehd == LFS_UNUSED_INUM) 246 panic("inode 0 allocated [2]"); 247 #endif /* DIAGNOSTIC */ 248 max = i + fs->lfs_ifpb; 249 /* printf("extend_ifile: new block ino %d--%d\n", i, max - 1); */ 250 251 if (fs->lfs_version == 1) { 252 for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < max; ++ifp_v1) { 253 ifp_v1->if_version = 1; 254 ifp_v1->if_daddr = LFS_UNUSED_DADDR; 255 ifp_v1->if_nextfree = ++i; 256 } 257 ifp_v1--; 258 ifp_v1->if_nextfree = oldlast; 259 } else { 260 for (ifp = (IFILE *)bp->b_data; i < max; ++ifp) { 261 ifp->if_version = 1; 262 ifp->if_daddr = LFS_UNUSED_DADDR; 263 ifp->if_nextfree = ++i; 264 } 265 ifp--; 266 ifp->if_nextfree = oldlast; 267 } 268 LFS_PUT_TAILFREE(fs, cip, cbp, max - 1); 269 270 (void) LFS_BWRITE_LOG(bp); /* Ifile */ 271 272 return 0; 273 } 274 275 /* Allocate a new inode. */ 276 /* ARGSUSED */ 277 /* VOP_BWRITE 2i times */ 278 int 279 lfs_valloc(void *v) 280 { 281 struct vop_valloc_args /* { 282 struct vnode *a_pvp; 283 int a_mode; 284 struct ucred *a_cred; 285 struct vnode **a_vpp; 286 } */ *ap = v; 287 struct lfs *fs; 288 struct buf *bp, *cbp; 289 struct ifile *ifp; 290 ino_t new_ino; 291 int error; 292 int new_gen; 293 CLEANERINFO *cip; 294 295 fs = VTOI(ap->a_pvp)->i_lfs; 296 if (fs->lfs_ronly) 297 return EROFS; 298 *ap->a_vpp = NULL; 299 300 lfs_seglock(fs, SEGM_PROT); 301 302 /* Get the head of the freelist. */ 303 LFS_GET_HEADFREE(fs, cip, cbp, &new_ino); 304 305 #ifdef DIAGNOSTIC 306 if (new_ino == LFS_UNUSED_INUM) { 307 #ifdef DEBUG 308 lfs_dump_super(fs); 309 #endif /* DEBUG */ 310 panic("inode 0 allocated [1]"); 311 } 312 #endif /* DIAGNOSTIC */ 313 #ifdef ALLOCPRINT 314 printf("lfs_valloc: allocate inode %d\n", new_ino); 315 #endif 316 317 /* 318 * Remove the inode from the free list and write the new start 319 * of the free list into the superblock. 320 */ 321 LFS_IENTRY(ifp, fs, new_ino, bp); 322 if (ifp->if_daddr != LFS_UNUSED_DADDR) 323 panic("lfs_valloc: inuse inode %d on the free list", new_ino); 324 LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree); 325 /* printf("lfs_valloc: headfree %d -> %d\n", new_ino, ifp->if_nextfree); */ 326 327 new_gen = ifp->if_version; /* version was updated by vfree */ 328 brelse(bp); 329 330 /* Extend IFILE so that the next lfs_valloc will succeed. */ 331 if (fs->lfs_freehd == LFS_UNUSED_INUM) { 332 if ((error = extend_ifile(fs, ap->a_cred)) != 0) { 333 LFS_PUT_HEADFREE(fs, cip, cbp, new_ino); 334 lfs_segunlock(fs); 335 return error; 336 } 337 } 338 #ifdef DIAGNOSTIC 339 if (fs->lfs_freehd == LFS_UNUSED_INUM) 340 panic("inode 0 allocated [3]"); 341 #endif /* DIAGNOSTIC */ 342 343 lfs_segunlock(fs); 344 345 return lfs_ialloc(fs, ap->a_pvp, new_ino, new_gen, ap->a_vpp); 346 } 347 348 /* 349 * Finish allocating a new inode, given an inode and generation number. 350 */ 351 static int 352 lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen, 353 struct vnode **vpp) 354 { 355 struct inode *ip; 356 struct vnode *vp; 357 IFILE *ifp; 358 struct buf *bp, *cbp; 359 int error; 360 CLEANERINFO *cip; 361 362 error = getnewvnode(VT_LFS, pvp->v_mount, lfs_vnodeop_p, &vp); 363 /* printf("lfs_ialloc: ino %d vp %p error %d\n", new_ino, vp, error);*/ 364 if (error) 365 goto errout; 366 367 lockmgr(&ufs_hashlock, LK_EXCLUSIVE, 0); 368 /* Create an inode to associate with the vnode. */ 369 lfs_vcreate(pvp->v_mount, new_ino, vp); 370 371 ip = VTOI(vp); 372 LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED); 373 /* on-disk structure has been zeroed out by lfs_vcreate */ 374 ip->i_din.ffs1_din->di_inumber = new_ino; 375 376 /* Set a new generation number for this inode. */ 377 if (new_gen) { 378 ip->i_gen = new_gen; 379 ip->i_ffs1_gen = new_gen; 380 } 381 382 /* Insert into the inode hash table. */ 383 ufs_ihashins(ip); 384 lockmgr(&ufs_hashlock, LK_RELEASE, 0); 385 386 ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp); 387 ip = VTOI(vp); 388 /* printf("lfs_ialloc[2]: ino %d vp %p\n", new_ino, vp);*/ 389 390 memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize)); 391 392 uvm_vnp_setsize(vp, 0); 393 *vpp = vp; 394 lfs_mark_vnode(vp); 395 genfs_node_init(vp, &lfs_genfsops); 396 VREF(ip->i_devvp); 397 /* Set superblock modified bit and increment file count. */ 398 fs->lfs_fmod = 1; 399 ++fs->lfs_nfiles; 400 return (0); 401 402 errout: 403 /* 404 * Put the new inum back on the free list. 405 */ 406 lfs_seglock(fs, SEGM_PROT); 407 LFS_IENTRY(ifp, fs, new_ino, bp); 408 ifp->if_daddr = LFS_UNUSED_DADDR; 409 LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree)); 410 LFS_PUT_HEADFREE(fs, cip, cbp, new_ino); 411 (void) LFS_BWRITE_LOG(bp); /* Ifile */ 412 lfs_segunlock(fs); 413 414 *vpp = NULLVP; 415 return (error); 416 } 417 418 /* Create a new vnode/inode pair and initialize what fields we can. */ 419 void 420 lfs_vcreate(struct mount *mp, ino_t ino, struct vnode *vp) 421 { 422 struct inode *ip; 423 struct ufs1_dinode *dp; 424 struct ufsmount *ump; 425 #ifdef QUOTA 426 int i; 427 #endif 428 429 /* Get a pointer to the private mount structure. */ 430 ump = VFSTOUFS(mp); 431 432 /* Initialize the inode. */ 433 ip = pool_get(&lfs_inode_pool, PR_WAITOK); 434 memset(ip, 0, sizeof(*ip)); 435 dp = pool_get(&lfs_dinode_pool, PR_WAITOK); 436 memset(dp, 0, sizeof(*dp)); 437 ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK); 438 vp->v_data = ip; 439 ip->i_din.ffs1_din = dp; 440 ip->i_ump = ump; 441 ip->i_vnode = vp; 442 ip->i_devvp = ump->um_devvp; 443 ip->i_dev = ump->um_dev; 444 ip->i_number = dp->di_inumber = ino; 445 ip->i_lfs = ump->um_lfs; 446 ip->i_lfs_effnblks = 0; 447 #ifdef QUOTA 448 for (i = 0; i < MAXQUOTAS; i++) 449 ip->i_dquot[i] = NODQUOT; 450 #endif 451 #ifdef DEBUG_LFS_VNLOCK 452 if (ino == LFS_IFILE_INUM) 453 vp->v_vnlock->lk_wmesg = "inlock"; 454 #endif 455 } 456 457 /* Free an inode. */ 458 /* ARGUSED */ 459 /* VOP_BWRITE 2i times */ 460 int 461 lfs_vfree(void *v) 462 { 463 struct vop_vfree_args /* { 464 struct vnode *a_pvp; 465 ino_t a_ino; 466 int a_mode; 467 } */ *ap = v; 468 SEGUSE *sup; 469 CLEANERINFO *cip; 470 struct buf *cbp, *bp; 471 struct ifile *ifp; 472 struct inode *ip; 473 struct vnode *vp; 474 struct lfs *fs; 475 daddr_t old_iaddr; 476 ino_t ino, otail; 477 int s; 478 479 /* Get the inode number and file system. */ 480 vp = ap->a_pvp; 481 ip = VTOI(vp); 482 fs = ip->i_lfs; 483 ino = ip->i_number; 484 485 /* Drain of pending writes */ 486 s = splbio(); 487 if (fs->lfs_version > 1 && WRITEINPROG(vp)) 488 tsleep(vp, (PRIBIO+1), "lfs_vfree", 0); 489 splx(s); 490 491 lfs_seglock(fs, SEGM_PROT); 492 493 lfs_unmark_vnode(vp); 494 if (vp->v_flag & VDIROP) { 495 --lfs_dirvcount; 496 vp->v_flag &= ~VDIROP; 497 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain); 498 wakeup(&lfs_dirvcount); 499 lfs_vunref(vp); 500 } 501 502 LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED); 503 ip->i_flag &= ~IN_ALLMOD; 504 505 /* 506 * Set the ifile's inode entry to unused, increment its version number 507 * and link it onto the free chain. 508 */ 509 LFS_IENTRY(ifp, fs, ino, bp); 510 old_iaddr = ifp->if_daddr; 511 ifp->if_daddr = LFS_UNUSED_DADDR; 512 ++ifp->if_version; 513 if (fs->lfs_version == 1) { 514 LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree)); 515 LFS_PUT_HEADFREE(fs, cip, cbp, ino); 516 (void) LFS_BWRITE_LOG(bp); /* Ifile */ 517 } else { 518 ifp->if_nextfree = LFS_UNUSED_INUM; 519 /* 520 * XXX Writing the freed node here means that it might not 521 * XXX make it into the free list in the event of a crash 522 * XXX (the ifile could be written before the rest of this 523 * XXX completes). 524 */ 525 (void) LFS_BWRITE_LOG(bp); /* Ifile */ 526 LFS_GET_TAILFREE(fs, cip, cbp, &otail); 527 LFS_IENTRY(ifp, fs, otail, bp); 528 ifp->if_nextfree = ino; 529 LFS_BWRITE_LOG(bp); 530 LFS_PUT_TAILFREE(fs, cip, cbp, ino); 531 /* printf("lfs_vfree: tailfree %d -> %d\n", otail, ino); */ 532 } 533 #ifdef DIAGNOSTIC 534 if (ino == LFS_UNUSED_INUM) { 535 panic("inode 0 freed"); 536 } 537 #endif /* DIAGNOSTIC */ 538 if (old_iaddr != LFS_UNUSED_DADDR) { 539 LFS_SEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp); 540 #ifdef DIAGNOSTIC 541 if (sup->su_nbytes < sizeof (struct ufs1_dinode)) { 542 printf("lfs_vfree: negative byte count" 543 " (segment %" PRIu32 " short by %d)\n", 544 dtosn(fs, old_iaddr), 545 (int)sizeof (struct ufs1_dinode) - 546 sup->su_nbytes); 547 panic("lfs_vfree: negative byte count"); 548 sup->su_nbytes = sizeof (struct ufs1_dinode); 549 } 550 #endif 551 sup->su_nbytes -= sizeof (struct ufs1_dinode); 552 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp); /* Ifile */ 553 } 554 555 /* Set superblock modified bit and decrement file count. */ 556 fs->lfs_fmod = 1; 557 --fs->lfs_nfiles; 558 559 lfs_segunlock(fs); 560 561 return (0); 562 } 563