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