1 /* $NetBSD: lfs_inode.c,v 1.105 2006/05/14 21:32:45 elad 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) 1986, 1989, 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_inode.c 8.9 (Berkeley) 5/8/95 67 */ 68 69 #include <sys/cdefs.h> 70 __KERNEL_RCSID(0, "$NetBSD: lfs_inode.c,v 1.105 2006/05/14 21:32:45 elad 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/mount.h> 79 #include <sys/malloc.h> 80 #include <sys/proc.h> 81 #include <sys/file.h> 82 #include <sys/buf.h> 83 #include <sys/vnode.h> 84 #include <sys/kernel.h> 85 #include <sys/trace.h> 86 #include <sys/resourcevar.h> 87 #include <sys/kauth.h> 88 89 #include <ufs/ufs/quota.h> 90 #include <ufs/ufs/inode.h> 91 #include <ufs/ufs/ufsmount.h> 92 #include <ufs/ufs/ufs_extern.h> 93 94 #include <ufs/lfs/lfs.h> 95 #include <ufs/lfs/lfs_extern.h> 96 97 static int lfs_update_seguse(struct lfs *, struct inode *ip, long, size_t); 98 static int lfs_indirtrunc (struct inode *, daddr_t, daddr_t, 99 daddr_t, int, long *, long *, long *, size_t *, 100 struct lwp *); 101 static int lfs_blkfree (struct lfs *, struct inode *, daddr_t, size_t, long *, size_t *); 102 static int lfs_vtruncbuf(struct vnode *, daddr_t, int, int); 103 104 /* Search a block for a specific dinode. */ 105 struct ufs1_dinode * 106 lfs_ifind(struct lfs *fs, ino_t ino, struct buf *bp) 107 { 108 struct ufs1_dinode *dip = (struct ufs1_dinode *)bp->b_data; 109 struct ufs1_dinode *ldip, *fin; 110 111 ASSERT_NO_SEGLOCK(fs); 112 /* 113 * Read the inode block backwards, since later versions of the 114 * inode will supercede earlier ones. Though it is unlikely, it is 115 * possible that the same inode will appear in the same inode block. 116 */ 117 fin = dip + INOPB(fs); 118 for (ldip = fin - 1; ldip >= dip; --ldip) 119 if (ldip->di_inumber == ino) 120 return (ldip); 121 122 printf("searched %d entries\n", (int)(fin - dip)); 123 printf("offset is 0x%x (seg %d)\n", fs->lfs_offset, 124 dtosn(fs, fs->lfs_offset)); 125 printf("block is 0x%llx (seg %lld)\n", 126 (unsigned long long)dbtofsb(fs, bp->b_blkno), 127 (long long)dtosn(fs, dbtofsb(fs, bp->b_blkno))); 128 129 return NULL; 130 } 131 132 int 133 lfs_update(struct vnode *vp, const struct timespec *acc, 134 const struct timespec *mod, int updflags) 135 { 136 struct inode *ip; 137 struct lfs *fs = VFSTOUFS(vp->v_mount)->um_lfs; 138 int s; 139 int flags; 140 141 ASSERT_NO_SEGLOCK(fs); 142 if (vp->v_mount->mnt_flag & MNT_RDONLY) 143 return (0); 144 ip = VTOI(vp); 145 146 /* 147 * If we are called from vinvalbuf, and the file's blocks have 148 * already been scheduled for writing, but the writes have not 149 * yet completed, lfs_vflush will not be called, and vinvalbuf 150 * will cause a panic. So, we must wait until any pending write 151 * for our inode completes, if we are called with UPDATE_WAIT set. 152 */ 153 s = splbio(); 154 simple_lock(&vp->v_interlock); 155 while ((updflags & (UPDATE_WAIT|UPDATE_DIROP)) == UPDATE_WAIT && 156 WRITEINPROG(vp)) { 157 DLOG((DLOG_SEG, "lfs_update: sleeping on ino %d" 158 " (in progress)\n", ip->i_number)); 159 ltsleep(vp, (PRIBIO+1), "lfs_update", 0, &vp->v_interlock); 160 } 161 simple_unlock(&vp->v_interlock); 162 splx(s); 163 LFS_ITIMES(ip, acc, mod, NULL); 164 if (updflags & UPDATE_CLOSE) 165 flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED | IN_CLEANING); 166 else 167 flags = ip->i_flag & (IN_MODIFIED | IN_CLEANING); 168 if (flags == 0) 169 return (0); 170 171 /* If sync, push back the vnode and any dirty blocks it may have. */ 172 if ((updflags & (UPDATE_WAIT|UPDATE_DIROP)) == UPDATE_WAIT) { 173 /* Avoid flushing VDIROP. */ 174 simple_lock(&fs->lfs_interlock); 175 ++fs->lfs_diropwait; 176 while (vp->v_flag & VDIROP) { 177 DLOG((DLOG_DIROP, "lfs_update: sleeping on inode %d" 178 " (dirops)\n", ip->i_number)); 179 DLOG((DLOG_DIROP, "lfs_update: vflags 0x%x, iflags" 180 " 0x%x\n", vp->v_flag, ip->i_flag)); 181 if (fs->lfs_dirops == 0) 182 lfs_flush_fs(fs, SEGM_SYNC); 183 else 184 ltsleep(&fs->lfs_writer, PRIBIO+1, "lfs_fsync", 185 0, &fs->lfs_interlock); 186 /* XXX KS - by falling out here, are we writing the vn 187 twice? */ 188 } 189 --fs->lfs_diropwait; 190 simple_unlock(&fs->lfs_interlock); 191 return lfs_vflush(vp); 192 } 193 return 0; 194 } 195 196 #define SINGLE 0 /* index of single indirect block */ 197 #define DOUBLE 1 /* index of double indirect block */ 198 #define TRIPLE 2 /* index of triple indirect block */ 199 /* 200 * Truncate the inode oip to at most length size, freeing the 201 * disk blocks. 202 */ 203 /* VOP_BWRITE 1 + NIADDR + lfs_balloc == 2 + 2*NIADDR times */ 204 205 int 206 lfs_truncate(struct vnode *ovp, off_t length, int ioflag, 207 kauth_cred_t cred, struct lwp *l) 208 { 209 struct genfs_node *gp = VTOG(ovp); 210 daddr_t lastblock; 211 struct inode *oip = VTOI(ovp); 212 daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR]; 213 /* XXX ondisk32 */ 214 int32_t newblks[NDADDR + NIADDR]; 215 struct lfs *fs; 216 struct buf *bp; 217 int offset, size, level; 218 long count, rcount, blocksreleased = 0, real_released = 0; 219 int i, nblocks; 220 int aflags, error, allerror = 0; 221 off_t osize; 222 long lastseg; 223 size_t bc; 224 int obufsize, odb; 225 int usepc; 226 struct ufsmount *ump = oip->i_ump; 227 228 if (ovp->v_type == VCHR || ovp->v_type == VBLK || 229 ovp->v_type == VFIFO || ovp->v_type == VSOCK) { 230 KASSERT(oip->i_size == 0); 231 return 0; 232 } 233 234 if (length < 0) 235 return (EINVAL); 236 237 /* 238 * Just return and not update modification times. 239 */ 240 if (oip->i_size == length) 241 return (0); 242 243 if (ovp->v_type == VLNK && 244 (oip->i_size < ump->um_maxsymlinklen || 245 (ump->um_maxsymlinklen == 0 && 246 oip->i_ffs1_blocks == 0))) { 247 #ifdef DIAGNOSTIC 248 if (length != 0) 249 panic("lfs_truncate: partial truncate of symlink"); 250 #endif 251 memset((char *)SHORTLINK(oip), 0, (u_int)oip->i_size); 252 oip->i_size = oip->i_ffs1_size = 0; 253 oip->i_flag |= IN_CHANGE | IN_UPDATE; 254 return (lfs_update(ovp, NULL, NULL, 0)); 255 } 256 if (oip->i_size == length) { 257 oip->i_flag |= IN_CHANGE | IN_UPDATE; 258 return (lfs_update(ovp, NULL, NULL, 0)); 259 } 260 #ifdef QUOTA 261 if ((error = getinoquota(oip)) != 0) 262 return (error); 263 #endif 264 fs = oip->i_lfs; 265 lfs_imtime(fs); 266 osize = oip->i_size; 267 usepc = (ovp->v_type == VREG && ovp != fs->lfs_ivnode); 268 269 ASSERT_NO_SEGLOCK(fs); 270 /* 271 * Lengthen the size of the file. We must ensure that the 272 * last byte of the file is allocated. Since the smallest 273 * value of osize is 0, length will be at least 1. 274 */ 275 if (osize < length) { 276 if (length > ump->um_maxfilesize) 277 return (EFBIG); 278 aflags = B_CLRBUF; 279 if (ioflag & IO_SYNC) 280 aflags |= B_SYNC; 281 if (usepc) { 282 if (lblkno(fs, osize) < NDADDR && 283 lblkno(fs, osize) != lblkno(fs, length) && 284 blkroundup(fs, osize) != osize) { 285 off_t eob; 286 287 eob = blkroundup(fs, osize); 288 error = ufs_balloc_range(ovp, osize, 289 eob - osize, cred, aflags); 290 if (error) 291 return error; 292 if (ioflag & IO_SYNC) { 293 ovp->v_size = eob; 294 simple_lock(&ovp->v_interlock); 295 VOP_PUTPAGES(ovp, 296 trunc_page(osize & fs->lfs_bmask), 297 round_page(eob), 298 PGO_CLEANIT | PGO_SYNCIO); 299 } 300 } 301 error = ufs_balloc_range(ovp, length - 1, 1, cred, 302 aflags); 303 if (error) { 304 (void) lfs_truncate(ovp, osize, 305 ioflag & IO_SYNC, cred, l); 306 return error; 307 } 308 uvm_vnp_setsize(ovp, length); 309 oip->i_flag |= IN_CHANGE | IN_UPDATE; 310 KASSERT(ovp->v_size == oip->i_size); 311 oip->i_lfs_hiblk = lblkno(fs, oip->i_size + fs->lfs_bsize - 1) - 1; 312 return (lfs_update(ovp, NULL, NULL, 0)); 313 } else { 314 error = lfs_reserve(fs, ovp, NULL, 315 btofsb(fs, (NIADDR + 2) << fs->lfs_bshift)); 316 if (error) 317 return (error); 318 error = lfs_balloc(ovp, length - 1, 1, cred, 319 aflags, &bp); 320 lfs_reserve(fs, ovp, NULL, 321 -btofsb(fs, (NIADDR + 2) << fs->lfs_bshift)); 322 if (error) 323 return (error); 324 oip->i_ffs1_size = oip->i_size = length; 325 uvm_vnp_setsize(ovp, length); 326 (void) VOP_BWRITE(bp); 327 oip->i_flag |= IN_CHANGE | IN_UPDATE; 328 oip->i_lfs_hiblk = lblkno(fs, oip->i_size + fs->lfs_bsize - 1) - 1; 329 return (lfs_update(ovp, NULL, NULL, 0)); 330 } 331 } 332 333 if ((error = lfs_reserve(fs, ovp, NULL, 334 btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift))) != 0) 335 return (error); 336 337 /* 338 * Shorten the size of the file. If the file is not being 339 * truncated to a block boundary, the contents of the 340 * partial block following the end of the file must be 341 * zero'ed in case it ever becomes accessible again because 342 * of subsequent file growth. Directories however are not 343 * zero'ed as they should grow back initialized to empty. 344 */ 345 offset = blkoff(fs, length); 346 lastseg = -1; 347 bc = 0; 348 349 if (ovp != fs->lfs_ivnode) 350 lfs_seglock(fs, SEGM_PROT); 351 if (offset == 0) { 352 oip->i_size = oip->i_ffs1_size = length; 353 } else if (!usepc) { 354 lbn = lblkno(fs, length); 355 aflags = B_CLRBUF; 356 if (ioflag & IO_SYNC) 357 aflags |= B_SYNC; 358 error = lfs_balloc(ovp, length - 1, 1, cred, aflags, &bp); 359 if (error) { 360 lfs_reserve(fs, ovp, NULL, 361 -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift)); 362 goto errout; 363 } 364 obufsize = bp->b_bufsize; 365 odb = btofsb(fs, bp->b_bcount); 366 oip->i_size = oip->i_ffs1_size = length; 367 size = blksize(fs, oip, lbn); 368 if (ovp->v_type != VDIR) 369 memset((char *)bp->b_data + offset, 0, 370 (u_int)(size - offset)); 371 allocbuf(bp, size, 1); 372 if ((bp->b_flags & (B_LOCKED | B_CALL)) == B_LOCKED) { 373 simple_lock(&lfs_subsys_lock); 374 locked_queue_bytes -= obufsize - bp->b_bufsize; 375 simple_unlock(&lfs_subsys_lock); 376 } 377 if (bp->b_flags & B_DELWRI) 378 fs->lfs_avail += odb - btofsb(fs, size); 379 (void) VOP_BWRITE(bp); 380 } else { /* vp->v_type == VREG && length < osize && offset != 0 */ 381 /* 382 * When truncating a regular file down to a non-block-aligned 383 * size, we must zero the part of last block which is past 384 * the new EOF. We must synchronously flush the zeroed pages 385 * to disk since the new pages will be invalidated as soon 386 * as we inform the VM system of the new, smaller size. 387 * We must do this before acquiring the GLOCK, since fetching 388 * the pages will acquire the GLOCK internally. 389 * So there is a window where another thread could see a whole 390 * zeroed page past EOF, but that's life. 391 */ 392 daddr_t xlbn; 393 voff_t eoz; 394 395 aflags = ioflag & IO_SYNC ? B_SYNC : 0; 396 error = ufs_balloc_range(ovp, length - 1, 1, cred, aflags); 397 if (error) { 398 lfs_reserve(fs, ovp, NULL, 399 -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift)); 400 goto errout; 401 } 402 xlbn = lblkno(fs, length); 403 size = blksize(fs, oip, xlbn); 404 eoz = MIN(lblktosize(fs, xlbn) + size, osize); 405 uvm_vnp_zerorange(ovp, length, eoz - length); 406 if (round_page(eoz) > round_page(length)) { 407 simple_lock(&ovp->v_interlock); 408 error = VOP_PUTPAGES(ovp, round_page(length), 409 round_page(eoz), 410 PGO_CLEANIT | PGO_DEACTIVATE | 411 ((ioflag & IO_SYNC) ? PGO_SYNCIO : 0)); 412 if (error) { 413 lfs_reserve(fs, ovp, NULL, 414 -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift)); 415 goto errout; 416 } 417 } 418 } 419 420 lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL); 421 422 oip->i_size = oip->i_ffs1_size = length; 423 uvm_vnp_setsize(ovp, length); 424 /* 425 * Calculate index into inode's block list of 426 * last direct and indirect blocks (if any) 427 * which we want to keep. Lastblock is -1 when 428 * the file is truncated to 0. 429 */ 430 /* Avoid sign overflow - XXX assumes that off_t is a quad_t. */ 431 if (length > QUAD_MAX - fs->lfs_bsize) 432 lastblock = lblkno(fs, QUAD_MAX - fs->lfs_bsize); 433 else 434 lastblock = lblkno(fs, length + fs->lfs_bsize - 1) - 1; 435 lastiblock[SINGLE] = lastblock - NDADDR; 436 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 437 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 438 nblocks = btofsb(fs, fs->lfs_bsize); 439 /* 440 * Record changed file and block pointers before we start 441 * freeing blocks. lastiblock values are also normalized to -1 442 * for calls to lfs_indirtrunc below. 443 */ 444 memcpy((caddr_t)newblks, (caddr_t)&oip->i_ffs1_db[0], sizeof newblks); 445 for (level = TRIPLE; level >= SINGLE; level--) 446 if (lastiblock[level] < 0) { 447 newblks[NDADDR+level] = 0; 448 lastiblock[level] = -1; 449 } 450 for (i = NDADDR - 1; i > lastblock; i--) 451 newblks[i] = 0; 452 453 oip->i_size = oip->i_ffs1_size = osize; 454 error = lfs_vtruncbuf(ovp, lastblock + 1, 0, 0); 455 if (error && !allerror) 456 allerror = error; 457 458 /* 459 * Indirect blocks first. 460 */ 461 indir_lbn[SINGLE] = -NDADDR; 462 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1; 463 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1; 464 for (level = TRIPLE; level >= SINGLE; level--) { 465 bn = oip->i_ffs1_ib[level]; 466 if (bn != 0) { 467 error = lfs_indirtrunc(oip, indir_lbn[level], 468 bn, lastiblock[level], 469 level, &count, &rcount, 470 &lastseg, &bc, l); 471 if (error) 472 allerror = error; 473 real_released += rcount; 474 blocksreleased += count; 475 if (lastiblock[level] < 0) { 476 if (oip->i_ffs1_ib[level] > 0) 477 real_released += nblocks; 478 blocksreleased += nblocks; 479 oip->i_ffs1_ib[level] = 0; 480 lfs_blkfree(fs, oip, bn, fs->lfs_bsize, 481 &lastseg, &bc); 482 lfs_deregister_block(ovp, bn); 483 } 484 } 485 if (lastiblock[level] >= 0) 486 goto done; 487 } 488 489 /* 490 * All whole direct blocks or frags. 491 */ 492 for (i = NDADDR - 1; i > lastblock; i--) { 493 long bsize, obsize; 494 495 bn = oip->i_ffs1_db[i]; 496 if (bn == 0) 497 continue; 498 bsize = blksize(fs, oip, i); 499 if (oip->i_ffs1_db[i] > 0) { 500 /* Check for fragment size changes */ 501 obsize = oip->i_lfs_fragsize[i]; 502 real_released += btofsb(fs, obsize); 503 oip->i_lfs_fragsize[i] = 0; 504 } else 505 obsize = 0; 506 blocksreleased += btofsb(fs, bsize); 507 oip->i_ffs1_db[i] = 0; 508 lfs_blkfree(fs, oip, bn, obsize, &lastseg, &bc); 509 lfs_deregister_block(ovp, bn); 510 } 511 if (lastblock < 0) 512 goto done; 513 514 /* 515 * Finally, look for a change in size of the 516 * last direct block; release any frags. 517 */ 518 bn = oip->i_ffs1_db[lastblock]; 519 if (bn != 0) { 520 long oldspace, newspace; 521 #if 0 522 long olddspace; 523 #endif 524 525 /* 526 * Calculate amount of space we're giving 527 * back as old block size minus new block size. 528 */ 529 oldspace = blksize(fs, oip, lastblock); 530 #if 0 531 olddspace = oip->i_lfs_fragsize[lastblock]; 532 #endif 533 534 oip->i_size = oip->i_ffs1_size = length; 535 newspace = blksize(fs, oip, lastblock); 536 if (newspace == 0) 537 panic("itrunc: newspace"); 538 if (oldspace - newspace > 0) { 539 blocksreleased += btofsb(fs, oldspace - newspace); 540 } 541 #if 0 542 if (bn > 0 && olddspace - newspace > 0) { 543 /* No segment accounting here, just vnode */ 544 real_released += btofsb(fs, olddspace - newspace); 545 } 546 #endif 547 } 548 549 done: 550 /* Finish segment accounting corrections */ 551 lfs_update_seguse(fs, oip, lastseg, bc); 552 #ifdef DIAGNOSTIC 553 for (level = SINGLE; level <= TRIPLE; level++) 554 if ((newblks[NDADDR + level] == 0) != 555 ((oip->i_ffs1_ib[level]) == 0)) { 556 panic("lfs itrunc1"); 557 } 558 for (i = 0; i < NDADDR; i++) 559 if ((newblks[i] == 0) != (oip->i_ffs1_db[i] == 0)) { 560 panic("lfs itrunc2"); 561 } 562 if (length == 0 && 563 (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd))) 564 panic("lfs itrunc3"); 565 #endif /* DIAGNOSTIC */ 566 /* 567 * Put back the real size. 568 */ 569 oip->i_size = oip->i_ffs1_size = length; 570 oip->i_lfs_effnblks -= blocksreleased; 571 oip->i_ffs1_blocks -= real_released; 572 simple_lock(&fs->lfs_interlock); 573 fs->lfs_bfree += blocksreleased; 574 simple_unlock(&fs->lfs_interlock); 575 #ifdef DIAGNOSTIC 576 if (oip->i_size == 0 && 577 (oip->i_ffs1_blocks != 0 || oip->i_lfs_effnblks != 0)) { 578 printf("lfs_truncate: truncate to 0 but %d blks/%d effblks\n", 579 oip->i_ffs1_blocks, oip->i_lfs_effnblks); 580 panic("lfs_truncate: persistent blocks"); 581 } 582 #endif 583 584 /* 585 * If we truncated to zero, take us off the paging queue. 586 */ 587 simple_lock(&fs->lfs_interlock); 588 if (oip->i_size == 0 && oip->i_flags & IN_PAGING) { 589 oip->i_flags &= ~IN_PAGING; 590 TAILQ_REMOVE(&fs->lfs_pchainhd, oip, i_lfs_pchain); 591 } 592 simple_unlock(&fs->lfs_interlock); 593 594 oip->i_flag |= IN_CHANGE; 595 #ifdef QUOTA 596 (void) chkdq(oip, -blocksreleased, NOCRED, 0); 597 #endif 598 lfs_reserve(fs, ovp, NULL, 599 -btofsb(fs, (2 * NIADDR + 3) << fs->lfs_bshift)); 600 lockmgr(&gp->g_glock, LK_RELEASE, NULL); 601 errout: 602 oip->i_lfs_hiblk = lblkno(fs, oip->i_size + fs->lfs_bsize - 1) - 1; 603 if (ovp != fs->lfs_ivnode) 604 lfs_segunlock(fs); 605 return (allerror ? allerror : error); 606 } 607 608 /* Update segment and avail usage information when removing a block. */ 609 static int 610 lfs_blkfree(struct lfs *fs, struct inode *ip, daddr_t daddr, 611 size_t bsize, long *lastseg, size_t *num) 612 { 613 long seg; 614 int error = 0; 615 616 ASSERT_SEGLOCK(fs); 617 bsize = fragroundup(fs, bsize); 618 if (daddr > 0) { 619 if (*lastseg != (seg = dtosn(fs, daddr))) { 620 error = lfs_update_seguse(fs, ip, *lastseg, *num); 621 *num = bsize; 622 *lastseg = seg; 623 } else 624 *num += bsize; 625 } 626 627 return error; 628 } 629 630 /* Finish the accounting updates for a segment. */ 631 static int 632 lfs_update_seguse(struct lfs *fs, struct inode *ip, long lastseg, size_t num) 633 { 634 struct segdelta *sd; 635 struct vnode *vp; 636 637 ASSERT_SEGLOCK(fs); 638 if (lastseg < 0 || num == 0) 639 return 0; 640 641 vp = ITOV(ip); 642 LIST_FOREACH(sd, &ip->i_lfs_segdhd, list) 643 if (sd->segnum == lastseg) 644 break; 645 if (sd == NULL) { 646 sd = malloc(sizeof(*sd), M_SEGMENT, M_WAITOK); 647 sd->segnum = lastseg; 648 sd->num = 0; 649 LIST_INSERT_HEAD(&ip->i_lfs_segdhd, sd, list); 650 } 651 sd->num += num; 652 653 return 0; 654 } 655 656 static void 657 lfs_finalize_seguse(struct lfs *fs, void *v) 658 { 659 SEGUSE *sup; 660 struct buf *bp; 661 struct segdelta *sd; 662 LIST_HEAD(, segdelta) *hd = v; 663 664 ASSERT_SEGLOCK(fs); 665 while((sd = LIST_FIRST(hd)) != NULL) { 666 LIST_REMOVE(sd, list); 667 LFS_SEGENTRY(sup, fs, sd->segnum, bp); 668 if (sd->num > sup->su_nbytes) { 669 printf("lfs_finalize_seguse: segment %ld short by %ld\n", 670 sd->segnum, (long)(sd->num - sup->su_nbytes)); 671 panic("lfs_finalize_seguse: negative bytes"); 672 sup->su_nbytes = sd->num; 673 } 674 sup->su_nbytes -= sd->num; 675 LFS_WRITESEGENTRY(sup, fs, sd->segnum, bp); 676 free(sd, M_SEGMENT); 677 } 678 } 679 680 /* Finish the accounting updates for a segment. */ 681 void 682 lfs_finalize_ino_seguse(struct lfs *fs, struct inode *ip) 683 { 684 ASSERT_SEGLOCK(fs); 685 lfs_finalize_seguse(fs, &ip->i_lfs_segdhd); 686 } 687 688 /* Finish the accounting updates for a segment. */ 689 void 690 lfs_finalize_fs_seguse(struct lfs *fs) 691 { 692 ASSERT_SEGLOCK(fs); 693 lfs_finalize_seguse(fs, &fs->lfs_segdhd); 694 } 695 696 /* 697 * Release blocks associated with the inode ip and stored in the indirect 698 * block bn. Blocks are free'd in LIFO order up to (but not including) 699 * lastbn. If level is greater than SINGLE, the block is an indirect block 700 * and recursive calls to indirtrunc must be used to cleanse other indirect 701 * blocks. 702 * 703 * NB: triple indirect blocks are untested. 704 */ 705 static int 706 lfs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn, 707 daddr_t lastbn, int level, long *countp, 708 long *rcountp, long *lastsegp, size_t *bcp, struct lwp *l) 709 { 710 int i; 711 struct buf *bp; 712 struct lfs *fs = ip->i_lfs; 713 int32_t *bap; /* XXX ondisk32 */ 714 struct vnode *vp; 715 daddr_t nb, nlbn, last; 716 int32_t *copy = NULL; /* XXX ondisk32 */ 717 long blkcount, rblkcount, factor; 718 int nblocks, blocksreleased = 0, real_released = 0; 719 int error = 0, allerror = 0; 720 721 ASSERT_SEGLOCK(fs); 722 /* 723 * Calculate index in current block of last 724 * block to be kept. -1 indicates the entire 725 * block so we need not calculate the index. 726 */ 727 factor = 1; 728 for (i = SINGLE; i < level; i++) 729 factor *= NINDIR(fs); 730 last = lastbn; 731 if (lastbn > 0) 732 last /= factor; 733 nblocks = btofsb(fs, fs->lfs_bsize); 734 /* 735 * Get buffer of block pointers, zero those entries corresponding 736 * to blocks to be free'd, and update on disk copy first. Since 737 * double(triple) indirect before single(double) indirect, calls 738 * to bmap on these blocks will fail. However, we already have 739 * the on disk address, so we have to set the b_blkno field 740 * explicitly instead of letting bread do everything for us. 741 */ 742 vp = ITOV(ip); 743 bp = getblk(vp, lbn, (int)fs->lfs_bsize, 0, 0); 744 if (bp->b_flags & (B_DONE | B_DELWRI)) { 745 /* Braces must be here in case trace evaluates to nothing. */ 746 trace(TR_BREADHIT, pack(vp, fs->lfs_bsize), lbn); 747 } else { 748 trace(TR_BREADMISS, pack(vp, fs->lfs_bsize), lbn); 749 l->l_proc->p_stats->p_ru.ru_inblock++; /* pay for read */ 750 bp->b_flags |= B_READ; 751 if (bp->b_bcount > bp->b_bufsize) 752 panic("lfs_indirtrunc: bad buffer size"); 753 bp->b_blkno = fsbtodb(fs, dbn); 754 VOP_STRATEGY(vp, bp); 755 error = biowait(bp); 756 } 757 if (error) { 758 brelse(bp); 759 *countp = *rcountp = 0; 760 return (error); 761 } 762 763 bap = (int32_t *)bp->b_data; /* XXX ondisk32 */ 764 if (lastbn >= 0) { 765 copy = (int32_t *)lfs_malloc(fs, fs->lfs_bsize, LFS_NB_IBLOCK); 766 memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->lfs_bsize); 767 memset((caddr_t)&bap[last + 1], 0, 768 /* XXX ondisk32 */ 769 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (int32_t)); 770 error = VOP_BWRITE(bp); 771 if (error) 772 allerror = error; 773 bap = copy; 774 } 775 776 /* 777 * Recursively free totally unused blocks. 778 */ 779 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last; 780 i--, nlbn += factor) { 781 nb = bap[i]; 782 if (nb == 0) 783 continue; 784 if (level > SINGLE) { 785 error = lfs_indirtrunc(ip, nlbn, nb, 786 (daddr_t)-1, level - 1, 787 &blkcount, &rblkcount, 788 lastsegp, bcp, l); 789 if (error) 790 allerror = error; 791 blocksreleased += blkcount; 792 real_released += rblkcount; 793 } 794 lfs_blkfree(fs, ip, nb, fs->lfs_bsize, lastsegp, bcp); 795 if (bap[i] > 0) 796 real_released += nblocks; 797 blocksreleased += nblocks; 798 } 799 800 /* 801 * Recursively free last partial block. 802 */ 803 if (level > SINGLE && lastbn >= 0) { 804 last = lastbn % factor; 805 nb = bap[i]; 806 if (nb != 0) { 807 error = lfs_indirtrunc(ip, nlbn, nb, 808 last, level - 1, &blkcount, 809 &rblkcount, lastsegp, bcp, l); 810 if (error) 811 allerror = error; 812 real_released += rblkcount; 813 blocksreleased += blkcount; 814 } 815 } 816 817 if (copy != NULL) { 818 lfs_free(fs, copy, LFS_NB_IBLOCK); 819 } else { 820 if (bp->b_flags & B_DELWRI) { 821 LFS_UNLOCK_BUF(bp); 822 fs->lfs_avail += btofsb(fs, bp->b_bcount); 823 wakeup(&fs->lfs_avail); 824 } 825 bp->b_flags |= B_INVAL; 826 brelse(bp); 827 } 828 829 *countp = blocksreleased; 830 *rcountp = real_released; 831 return (allerror); 832 } 833 834 /* 835 * Destroy any in core blocks past the truncation length. 836 * Inlined from vtruncbuf, so that lfs_avail could be updated. 837 * We take the seglock to prevent cleaning from occurring while we are 838 * invalidating blocks. 839 */ 840 static int 841 lfs_vtruncbuf(struct vnode *vp, daddr_t lbn, int slpflag, int slptimeo) 842 { 843 struct buf *bp, *nbp; 844 int s, error; 845 struct lfs *fs; 846 voff_t off; 847 848 off = round_page((voff_t)lbn << vp->v_mount->mnt_fs_bshift); 849 simple_lock(&vp->v_interlock); 850 error = VOP_PUTPAGES(vp, off, 0, PGO_FREE | PGO_SYNCIO); 851 if (error) 852 return error; 853 854 fs = VTOI(vp)->i_lfs; 855 s = splbio(); 856 857 ASSERT_SEGLOCK(fs); 858 restart: 859 for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) { 860 nbp = LIST_NEXT(bp, b_vnbufs); 861 if (bp->b_lblkno < lbn) 862 continue; 863 simple_lock(&bp->b_interlock); 864 if (bp->b_flags & B_BUSY) { 865 bp->b_flags |= B_WANTED; 866 error = ltsleep(bp, slpflag | (PRIBIO + 1) | PNORELOCK, 867 "lfs_vtruncbuf", slptimeo, &bp->b_interlock); 868 if (error) { 869 splx(s); 870 return (error); 871 } 872 goto restart; 873 } 874 bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH; 875 if (bp->b_flags & B_DELWRI) { 876 bp->b_flags &= ~B_DELWRI; 877 fs->lfs_avail += btofsb(fs, bp->b_bcount); 878 wakeup(&fs->lfs_avail); 879 } 880 LFS_UNLOCK_BUF(bp); 881 simple_unlock(&bp->b_interlock); 882 brelse(bp); 883 } 884 885 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 886 nbp = LIST_NEXT(bp, b_vnbufs); 887 if (bp->b_lblkno < lbn) 888 continue; 889 simple_lock(&bp->b_interlock); 890 if (bp->b_flags & B_BUSY) { 891 bp->b_flags |= B_WANTED; 892 error = ltsleep(bp, slpflag | (PRIBIO + 1) | PNORELOCK, 893 "lfs_vtruncbuf", slptimeo, &bp->b_interlock); 894 if (error) { 895 splx(s); 896 return (error); 897 } 898 goto restart; 899 } 900 bp->b_flags |= B_BUSY | B_INVAL | B_VFLUSH; 901 if (bp->b_flags & B_DELWRI) { 902 bp->b_flags &= ~B_DELWRI; 903 fs->lfs_avail += btofsb(fs, bp->b_bcount); 904 wakeup(&fs->lfs_avail); 905 } 906 LFS_UNLOCK_BUF(bp); 907 simple_unlock(&bp->b_interlock); 908 brelse(bp); 909 } 910 911 splx(s); 912 913 return (0); 914 } 915 916