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