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