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