1 /* $NetBSD: lfs_inode.c,v 1.135 2013/07/28 01:25:06 dholland 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.135 2013/07/28 01:25:06 dholland 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/lfs/ulfs_quotacommon.h> 83 #include <ufs/lfs/ulfs_inode.h> 84 #include <ufs/lfs/ulfsmount.h> 85 #include <ufs/lfs/ulfs_extern.h> 86 87 #include <ufs/lfs/lfs.h> 88 #include <ufs/lfs/lfs_extern.h> 89 #include <ufs/lfs/lfs_kernel.h> 90 91 static int lfs_update_seguse(struct lfs *, struct inode *ip, long, size_t); 92 static int lfs_indirtrunc (struct inode *, daddr_t, daddr_t, 93 daddr_t, int, long *, long *, long *, size_t *); 94 static int lfs_blkfree (struct lfs *, struct inode *, daddr_t, size_t, long *, size_t *); 95 static int lfs_vtruncbuf(struct vnode *, daddr_t, bool, int); 96 97 /* Search a block for a specific dinode. */ 98 struct ulfs1_dinode * 99 lfs_ifind(struct lfs *fs, ino_t ino, struct buf *bp) 100 { 101 struct ulfs1_dinode *dip = (struct ulfs1_dinode *)bp->b_data; 102 struct ulfs1_dinode *ldip, *fin; 103 104 ASSERT_NO_SEGLOCK(fs); 105 /* 106 * Read the inode block backwards, since later versions of the 107 * inode will supercede earlier ones. Though it is unlikely, it is 108 * possible that the same inode will appear in the same inode block. 109 */ 110 fin = dip + LFS_INOPB(fs); 111 for (ldip = fin - 1; ldip >= dip; --ldip) 112 if (ldip->di_inumber == ino) 113 return (ldip); 114 115 printf("searched %d entries\n", (int)(fin - dip)); 116 printf("offset is 0x%x (seg %d)\n", fs->lfs_offset, 117 lfs_dtosn(fs, fs->lfs_offset)); 118 printf("block is 0x%llx (seg %lld)\n", 119 (unsigned long long)LFS_DBTOFSB(fs, bp->b_blkno), 120 (long long)lfs_dtosn(fs, LFS_DBTOFSB(fs, bp->b_blkno))); 121 122 return NULL; 123 } 124 125 int 126 lfs_update(struct vnode *vp, const struct timespec *acc, 127 const struct timespec *mod, int updflags) 128 { 129 struct inode *ip; 130 struct lfs *fs = VFSTOULFS(vp->v_mount)->um_lfs; 131 int flags; 132 133 ASSERT_NO_SEGLOCK(fs); 134 if (vp->v_mount->mnt_flag & MNT_RDONLY) 135 return (0); 136 ip = VTOI(vp); 137 138 /* 139 * If we are called from vinvalbuf, and the file's blocks have 140 * already been scheduled for writing, but the writes have not 141 * yet completed, lfs_vflush will not be called, and vinvalbuf 142 * will cause a panic. So, we must wait until any pending write 143 * for our inode completes, if we are called with UPDATE_WAIT set. 144 */ 145 mutex_enter(vp->v_interlock); 146 while ((updflags & (UPDATE_WAIT|UPDATE_DIROP)) == UPDATE_WAIT && 147 WRITEINPROG(vp)) { 148 DLOG((DLOG_SEG, "lfs_update: sleeping on ino %d" 149 " (in progress)\n", ip->i_number)); 150 cv_wait(&vp->v_cv, vp->v_interlock); 151 } 152 mutex_exit(vp->v_interlock); 153 LFS_ITIMES(ip, acc, mod, NULL); 154 if (updflags & UPDATE_CLOSE) 155 flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED | IN_CLEANING); 156 else 157 flags = ip->i_flag & (IN_MODIFIED | IN_CLEANING); 158 if (flags == 0) 159 return (0); 160 161 /* If sync, push back the vnode and any dirty blocks it may have. */ 162 if ((updflags & (UPDATE_WAIT|UPDATE_DIROP)) == UPDATE_WAIT) { 163 /* Avoid flushing VU_DIROP. */ 164 mutex_enter(&lfs_lock); 165 ++fs->lfs_diropwait; 166 while (vp->v_uflag & VU_DIROP) { 167 DLOG((DLOG_DIROP, "lfs_update: sleeping on inode %d" 168 " (dirops)\n", ip->i_number)); 169 DLOG((DLOG_DIROP, "lfs_update: vflags 0x%x, iflags" 170 " 0x%x\n", 171 vp->v_iflag | vp->v_vflag | vp->v_uflag, 172 ip->i_flag)); 173 if (fs->lfs_dirops == 0) 174 lfs_flush_fs(fs, SEGM_SYNC); 175 else 176 mtsleep(&fs->lfs_writer, PRIBIO+1, "lfs_fsync", 177 0, &lfs_lock); 178 /* XXX KS - by falling out here, are we writing the vn 179 twice? */ 180 } 181 --fs->lfs_diropwait; 182 mutex_exit(&lfs_lock); 183 return lfs_vflush(vp); 184 } 185 return 0; 186 } 187 188 #define SINGLE 0 /* index of single indirect block */ 189 #define DOUBLE 1 /* index of double indirect block */ 190 #define TRIPLE 2 /* index of triple indirect block */ 191 /* 192 * Truncate the inode oip to at most length size, freeing the 193 * disk blocks. 194 */ 195 /* VOP_BWRITE 1 + ULFS_NIADDR + lfs_balloc == 2 + 2*ULFS_NIADDR times */ 196 197 int 198 lfs_truncate(struct vnode *ovp, off_t length, int ioflag, kauth_cred_t cred) 199 { 200 daddr_t lastblock; 201 struct inode *oip = VTOI(ovp); 202 daddr_t bn, lbn, lastiblock[ULFS_NIADDR], indir_lbn[ULFS_NIADDR]; 203 /* XXX ondisk32 */ 204 int32_t newblks[ULFS_NDADDR + ULFS_NIADDR]; 205 struct lfs *fs; 206 struct buf *bp; 207 int offset, size, level; 208 long count, rcount, blocksreleased = 0, real_released = 0; 209 int i, nblocks; 210 int aflags, error, allerror = 0; 211 off_t osize; 212 long lastseg; 213 size_t bc; 214 int obufsize, odb; 215 int usepc; 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 fs = oip->i_lfs; 236 237 if (ovp->v_type == VLNK && 238 (oip->i_size < fs->um_maxsymlinklen || 239 (fs->um_maxsymlinklen == 0 && 240 oip->i_ffs1_blocks == 0))) { 241 #ifdef DIAGNOSTIC 242 if (length != 0) 243 panic("lfs_truncate: partial truncate of symlink"); 244 #endif 245 memset((char *)SHORTLINK(oip), 0, (u_int)oip->i_size); 246 oip->i_size = oip->i_ffs1_size = 0; 247 oip->i_flag |= IN_CHANGE | IN_UPDATE; 248 return (lfs_update(ovp, NULL, NULL, 0)); 249 } 250 if (oip->i_size == length) { 251 oip->i_flag |= IN_CHANGE | IN_UPDATE; 252 return (lfs_update(ovp, NULL, NULL, 0)); 253 } 254 lfs_imtime(fs); 255 osize = oip->i_size; 256 usepc = (ovp->v_type == VREG && ovp != fs->lfs_ivnode); 257 258 ASSERT_NO_SEGLOCK(fs); 259 /* 260 * Lengthen the size of the file. We must ensure that the 261 * last byte of the file is allocated. Since the smallest 262 * value of osize is 0, length will be at least 1. 263 */ 264 if (osize < length) { 265 if (length > fs->um_maxfilesize) 266 return (EFBIG); 267 aflags = B_CLRBUF; 268 if (ioflag & IO_SYNC) 269 aflags |= B_SYNC; 270 if (usepc) { 271 if (lfs_lblkno(fs, osize) < ULFS_NDADDR && 272 lfs_lblkno(fs, osize) != lfs_lblkno(fs, length) && 273 lfs_blkroundup(fs, osize) != osize) { 274 off_t eob; 275 276 eob = lfs_blkroundup(fs, osize); 277 uvm_vnp_setwritesize(ovp, eob); 278 error = ulfs_balloc_range(ovp, osize, 279 eob - osize, cred, aflags); 280 if (error) { 281 (void) lfs_truncate(ovp, osize, 282 ioflag & IO_SYNC, cred); 283 return error; 284 } 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 = ulfs_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 = lfs_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 lfs_btofsb(fs, (ULFS_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 -lfs_btofsb(fs, (ULFS_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->b_vp, bp); 320 oip->i_flag |= IN_CHANGE | IN_UPDATE; 321 oip->i_lfs_hiblk = lfs_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 lfs_btofsb(fs, (2 * ULFS_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 = lfs_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 = lfs_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 -lfs_btofsb(fs, (2 * ULFS_NIADDR + 3) << fs->lfs_bshift)); 355 goto errout; 356 } 357 obufsize = bp->b_bufsize; 358 odb = lfs_btofsb(fs, bp->b_bcount); 359 oip->i_size = oip->i_ffs1_size = length; 360 size = lfs_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 - lfs_btofsb(fs, size); 372 (void) VOP_BWRITE(bp->b_vp, 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 = ulfs_balloc_range(ovp, length - 1, 1, cred, aflags); 390 if (error) { 391 lfs_reserve(fs, ovp, NULL, 392 -lfs_btofsb(fs, (2 * ULFS_NIADDR + 3) << fs->lfs_bshift)); 393 goto errout; 394 } 395 xlbn = lfs_lblkno(fs, length); 396 size = lfs_blksize(fs, oip, xlbn); 397 eoz = MIN(lfs_lblktosize(fs, xlbn) + size, osize); 398 ubc_zerorange(&ovp->v_uobj, length, eoz - length, 399 UBC_UNMAP_FLAG(ovp)); 400 if (round_page(eoz) > round_page(length)) { 401 mutex_enter(ovp->v_interlock); 402 error = VOP_PUTPAGES(ovp, round_page(length), 403 round_page(eoz), 404 PGO_CLEANIT | PGO_DEACTIVATE | 405 ((ioflag & IO_SYNC) ? PGO_SYNCIO : 0)); 406 if (error) { 407 lfs_reserve(fs, ovp, NULL, 408 -lfs_btofsb(fs, (2 * ULFS_NIADDR + 3) << fs->lfs_bshift)); 409 goto errout; 410 } 411 } 412 } 413 414 genfs_node_wrlock(ovp); 415 416 oip->i_size = oip->i_ffs1_size = length; 417 uvm_vnp_setsize(ovp, length); 418 419 /* 420 * Calculate index into inode's block list of 421 * last direct and indirect blocks (if any) 422 * which we want to keep. Lastblock is -1 when 423 * the file is truncated to 0. 424 */ 425 /* Avoid sign overflow - XXX assumes that off_t is a quad_t. */ 426 if (length > QUAD_MAX - fs->lfs_bsize) 427 lastblock = lfs_lblkno(fs, QUAD_MAX - fs->lfs_bsize); 428 else 429 lastblock = lfs_lblkno(fs, length + fs->lfs_bsize - 1) - 1; 430 lastiblock[SINGLE] = lastblock - ULFS_NDADDR; 431 lastiblock[DOUBLE] = lastiblock[SINGLE] - LFS_NINDIR(fs); 432 lastiblock[TRIPLE] = lastiblock[DOUBLE] - LFS_NINDIR(fs) * LFS_NINDIR(fs); 433 nblocks = lfs_btofsb(fs, fs->lfs_bsize); 434 /* 435 * Record changed file and block pointers before we start 436 * freeing blocks. lastiblock values are also normalized to -1 437 * for calls to lfs_indirtrunc below. 438 */ 439 memcpy((void *)newblks, (void *)&oip->i_ffs1_db[0], sizeof newblks); 440 for (level = TRIPLE; level >= SINGLE; level--) 441 if (lastiblock[level] < 0) { 442 newblks[ULFS_NDADDR+level] = 0; 443 lastiblock[level] = -1; 444 } 445 for (i = ULFS_NDADDR - 1; i > lastblock; i--) 446 newblks[i] = 0; 447 448 oip->i_size = oip->i_ffs1_size = osize; 449 error = lfs_vtruncbuf(ovp, lastblock + 1, false, 0); 450 if (error && !allerror) 451 allerror = error; 452 453 /* 454 * Indirect blocks first. 455 */ 456 indir_lbn[SINGLE] = -ULFS_NDADDR; 457 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - LFS_NINDIR(fs) - 1; 458 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - LFS_NINDIR(fs) * LFS_NINDIR(fs) - 1; 459 for (level = TRIPLE; level >= SINGLE; level--) { 460 bn = oip->i_ffs1_ib[level]; 461 if (bn != 0) { 462 error = lfs_indirtrunc(oip, indir_lbn[level], 463 bn, lastiblock[level], 464 level, &count, &rcount, 465 &lastseg, &bc); 466 if (error) 467 allerror = error; 468 real_released += rcount; 469 blocksreleased += count; 470 if (lastiblock[level] < 0) { 471 if (oip->i_ffs1_ib[level] > 0) 472 real_released += nblocks; 473 blocksreleased += nblocks; 474 oip->i_ffs1_ib[level] = 0; 475 lfs_blkfree(fs, oip, bn, fs->lfs_bsize, 476 &lastseg, &bc); 477 lfs_deregister_block(ovp, bn); 478 } 479 } 480 if (lastiblock[level] >= 0) 481 goto done; 482 } 483 484 /* 485 * All whole direct blocks or frags. 486 */ 487 for (i = ULFS_NDADDR - 1; i > lastblock; i--) { 488 long bsize, obsize; 489 490 bn = oip->i_ffs1_db[i]; 491 if (bn == 0) 492 continue; 493 bsize = lfs_blksize(fs, oip, i); 494 if (oip->i_ffs1_db[i] > 0) { 495 /* Check for fragment size changes */ 496 obsize = oip->i_lfs_fragsize[i]; 497 real_released += lfs_btofsb(fs, obsize); 498 oip->i_lfs_fragsize[i] = 0; 499 } else 500 obsize = 0; 501 blocksreleased += lfs_btofsb(fs, bsize); 502 oip->i_ffs1_db[i] = 0; 503 lfs_blkfree(fs, oip, bn, obsize, &lastseg, &bc); 504 lfs_deregister_block(ovp, bn); 505 } 506 if (lastblock < 0) 507 goto done; 508 509 /* 510 * Finally, look for a change in size of the 511 * last direct block; release any frags. 512 */ 513 bn = oip->i_ffs1_db[lastblock]; 514 if (bn != 0) { 515 long oldspace, newspace; 516 #if 0 517 long olddspace; 518 #endif 519 520 /* 521 * Calculate amount of space we're giving 522 * back as old block size minus new block size. 523 */ 524 oldspace = lfs_blksize(fs, oip, lastblock); 525 #if 0 526 olddspace = oip->i_lfs_fragsize[lastblock]; 527 #endif 528 529 oip->i_size = oip->i_ffs1_size = length; 530 newspace = lfs_blksize(fs, oip, lastblock); 531 if (newspace == 0) 532 panic("itrunc: newspace"); 533 if (oldspace - newspace > 0) { 534 blocksreleased += lfs_btofsb(fs, oldspace - newspace); 535 } 536 #if 0 537 if (bn > 0 && olddspace - newspace > 0) { 538 /* No segment accounting here, just vnode */ 539 real_released += lfs_btofsb(fs, olddspace - newspace); 540 } 541 #endif 542 } 543 544 done: 545 /* Finish segment accounting corrections */ 546 lfs_update_seguse(fs, oip, lastseg, bc); 547 #ifdef DIAGNOSTIC 548 for (level = SINGLE; level <= TRIPLE; level++) 549 if ((newblks[ULFS_NDADDR + level] == 0) != 550 ((oip->i_ffs1_ib[level]) == 0)) { 551 panic("lfs itrunc1"); 552 } 553 for (i = 0; i < ULFS_NDADDR; i++) 554 if ((newblks[i] == 0) != (oip->i_ffs1_db[i] == 0)) { 555 panic("lfs itrunc2"); 556 } 557 if (length == 0 && 558 (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd))) 559 panic("lfs itrunc3"); 560 #endif /* DIAGNOSTIC */ 561 /* 562 * Put back the real size. 563 */ 564 oip->i_size = oip->i_ffs1_size = length; 565 oip->i_lfs_effnblks -= blocksreleased; 566 oip->i_ffs1_blocks -= real_released; 567 mutex_enter(&lfs_lock); 568 fs->lfs_bfree += blocksreleased; 569 mutex_exit(&lfs_lock); 570 #ifdef DIAGNOSTIC 571 if (oip->i_size == 0 && 572 (oip->i_ffs1_blocks != 0 || oip->i_lfs_effnblks != 0)) { 573 printf("lfs_truncate: truncate to 0 but %d blks/%d effblks\n", 574 oip->i_ffs1_blocks, oip->i_lfs_effnblks); 575 panic("lfs_truncate: persistent blocks"); 576 } 577 #endif 578 579 /* 580 * If we truncated to zero, take us off the paging queue. 581 */ 582 mutex_enter(&lfs_lock); 583 if (oip->i_size == 0 && oip->i_flags & IN_PAGING) { 584 oip->i_flags &= ~IN_PAGING; 585 TAILQ_REMOVE(&fs->lfs_pchainhd, oip, i_lfs_pchain); 586 } 587 mutex_exit(&lfs_lock); 588 589 oip->i_flag |= IN_CHANGE; 590 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2) 591 (void) lfs_chkdq(oip, -blocksreleased, NOCRED, 0); 592 #endif 593 lfs_reserve(fs, ovp, NULL, 594 -lfs_btofsb(fs, (2 * ULFS_NIADDR + 3) << fs->lfs_bshift)); 595 genfs_node_unlock(ovp); 596 errout: 597 oip->i_lfs_hiblk = lfs_lblkno(fs, oip->i_size + fs->lfs_bsize - 1) - 1; 598 if (ovp != fs->lfs_ivnode) 599 lfs_segunlock(fs); 600 return (allerror ? allerror : error); 601 } 602 603 /* Update segment and avail usage information when removing a block. */ 604 static int 605 lfs_blkfree(struct lfs *fs, struct inode *ip, daddr_t daddr, 606 size_t bsize, long *lastseg, size_t *num) 607 { 608 long seg; 609 int error = 0; 610 611 ASSERT_SEGLOCK(fs); 612 bsize = lfs_fragroundup(fs, bsize); 613 if (daddr > 0) { 614 if (*lastseg != (seg = lfs_dtosn(fs, daddr))) { 615 error = lfs_update_seguse(fs, ip, *lastseg, *num); 616 *num = bsize; 617 *lastseg = seg; 618 } else 619 *num += bsize; 620 } 621 622 return error; 623 } 624 625 /* Finish the accounting updates for a segment. */ 626 static int 627 lfs_update_seguse(struct lfs *fs, struct inode *ip, long lastseg, size_t num) 628 { 629 struct segdelta *sd; 630 struct vnode *vp; 631 632 ASSERT_SEGLOCK(fs); 633 if (lastseg < 0 || num == 0) 634 return 0; 635 636 vp = ITOV(ip); 637 LIST_FOREACH(sd, &ip->i_lfs_segdhd, list) 638 if (sd->segnum == lastseg) 639 break; 640 if (sd == NULL) { 641 sd = malloc(sizeof(*sd), M_SEGMENT, M_WAITOK); 642 sd->segnum = lastseg; 643 sd->num = 0; 644 LIST_INSERT_HEAD(&ip->i_lfs_segdhd, sd, list); 645 } 646 sd->num += num; 647 648 return 0; 649 } 650 651 static void 652 lfs_finalize_seguse(struct lfs *fs, void *v) 653 { 654 SEGUSE *sup; 655 struct buf *bp; 656 struct segdelta *sd; 657 LIST_HEAD(, segdelta) *hd = v; 658 659 ASSERT_SEGLOCK(fs); 660 while((sd = LIST_FIRST(hd)) != NULL) { 661 LIST_REMOVE(sd, list); 662 LFS_SEGENTRY(sup, fs, sd->segnum, bp); 663 if (sd->num > sup->su_nbytes) { 664 printf("lfs_finalize_seguse: segment %ld short by %ld\n", 665 sd->segnum, (long)(sd->num - sup->su_nbytes)); 666 panic("lfs_finalize_seguse: negative bytes"); 667 sup->su_nbytes = sd->num; 668 } 669 sup->su_nbytes -= sd->num; 670 LFS_WRITESEGENTRY(sup, fs, sd->segnum, bp); 671 free(sd, M_SEGMENT); 672 } 673 } 674 675 /* Finish the accounting updates for a segment. */ 676 void 677 lfs_finalize_ino_seguse(struct lfs *fs, struct inode *ip) 678 { 679 ASSERT_SEGLOCK(fs); 680 lfs_finalize_seguse(fs, &ip->i_lfs_segdhd); 681 } 682 683 /* Finish the accounting updates for a segment. */ 684 void 685 lfs_finalize_fs_seguse(struct lfs *fs) 686 { 687 ASSERT_SEGLOCK(fs); 688 lfs_finalize_seguse(fs, &fs->lfs_segdhd); 689 } 690 691 /* 692 * Release blocks associated with the inode ip and stored in the indirect 693 * block bn. Blocks are free'd in LIFO order up to (but not including) 694 * lastbn. If level is greater than SINGLE, the block is an indirect block 695 * and recursive calls to indirtrunc must be used to cleanse other indirect 696 * blocks. 697 * 698 * NB: triple indirect blocks are untested. 699 */ 700 static int 701 lfs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn, 702 daddr_t lastbn, int level, long *countp, 703 long *rcountp, long *lastsegp, size_t *bcp) 704 { 705 int i; 706 struct buf *bp; 707 struct lfs *fs = ip->i_lfs; 708 int32_t *bap; /* XXX ondisk32 */ 709 struct vnode *vp; 710 daddr_t nb, nlbn, last; 711 int32_t *copy = NULL; /* XXX ondisk32 */ 712 long blkcount, rblkcount, factor; 713 int nblocks, blocksreleased = 0, real_released = 0; 714 int error = 0, allerror = 0; 715 716 ASSERT_SEGLOCK(fs); 717 /* 718 * Calculate index in current block of last 719 * block to be kept. -1 indicates the entire 720 * block so we need not calculate the index. 721 */ 722 factor = 1; 723 for (i = SINGLE; i < level; i++) 724 factor *= LFS_NINDIR(fs); 725 last = lastbn; 726 if (lastbn > 0) 727 last /= factor; 728 nblocks = lfs_btofsb(fs, fs->lfs_bsize); 729 /* 730 * Get buffer of block pointers, zero those entries corresponding 731 * to blocks to be free'd, and update on disk copy first. Since 732 * double(triple) indirect before single(double) indirect, calls 733 * to bmap on these blocks will fail. However, we already have 734 * the on disk address, so we have to set the b_blkno field 735 * explicitly instead of letting bread do everything for us. 736 */ 737 vp = ITOV(ip); 738 bp = getblk(vp, lbn, (int)fs->lfs_bsize, 0, 0); 739 if (bp->b_oflags & (BO_DONE | BO_DELWRI)) { 740 /* Braces must be here in case trace evaluates to nothing. */ 741 trace(TR_BREADHIT, pack(vp, fs->lfs_bsize), lbn); 742 } else { 743 trace(TR_BREADMISS, pack(vp, fs->lfs_bsize), lbn); 744 curlwp->l_ru.ru_inblock++; /* pay for read */ 745 bp->b_flags |= B_READ; 746 if (bp->b_bcount > bp->b_bufsize) 747 panic("lfs_indirtrunc: bad buffer size"); 748 bp->b_blkno = LFS_FSBTODB(fs, dbn); 749 VOP_STRATEGY(vp, bp); 750 error = biowait(bp); 751 } 752 if (error) { 753 brelse(bp, 0); 754 *countp = *rcountp = 0; 755 return (error); 756 } 757 758 bap = (int32_t *)bp->b_data; /* XXX ondisk32 */ 759 if (lastbn >= 0) { 760 copy = (int32_t *)lfs_malloc(fs, fs->lfs_bsize, LFS_NB_IBLOCK); 761 memcpy((void *)copy, (void *)bap, (u_int)fs->lfs_bsize); 762 memset((void *)&bap[last + 1], 0, 763 /* XXX ondisk32 */ 764 (u_int)(LFS_NINDIR(fs) - (last + 1)) * sizeof (int32_t)); 765 error = VOP_BWRITE(bp->b_vp, bp); 766 if (error) 767 allerror = error; 768 bap = copy; 769 } 770 771 /* 772 * Recursively free totally unused blocks. 773 */ 774 for (i = LFS_NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last; 775 i--, nlbn += factor) { 776 nb = bap[i]; 777 if (nb == 0) 778 continue; 779 if (level > SINGLE) { 780 error = lfs_indirtrunc(ip, nlbn, nb, 781 (daddr_t)-1, level - 1, 782 &blkcount, &rblkcount, 783 lastsegp, bcp); 784 if (error) 785 allerror = error; 786 blocksreleased += blkcount; 787 real_released += rblkcount; 788 } 789 lfs_blkfree(fs, ip, nb, fs->lfs_bsize, lastsegp, bcp); 790 if (bap[i] > 0) 791 real_released += nblocks; 792 blocksreleased += nblocks; 793 } 794 795 /* 796 * Recursively free last partial block. 797 */ 798 if (level > SINGLE && lastbn >= 0) { 799 last = lastbn % factor; 800 nb = bap[i]; 801 if (nb != 0) { 802 error = lfs_indirtrunc(ip, nlbn, nb, 803 last, level - 1, &blkcount, 804 &rblkcount, lastsegp, bcp); 805 if (error) 806 allerror = error; 807 real_released += rblkcount; 808 blocksreleased += blkcount; 809 } 810 } 811 812 if (copy != NULL) { 813 lfs_free(fs, copy, LFS_NB_IBLOCK); 814 } else { 815 mutex_enter(&bufcache_lock); 816 if (bp->b_oflags & BO_DELWRI) { 817 LFS_UNLOCK_BUF(bp); 818 fs->lfs_avail += lfs_btofsb(fs, bp->b_bcount); 819 wakeup(&fs->lfs_avail); 820 } 821 brelsel(bp, BC_INVAL); 822 mutex_exit(&bufcache_lock); 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, bool catch, int slptimeo) 838 { 839 struct buf *bp, *nbp; 840 int error; 841 struct lfs *fs; 842 voff_t off; 843 844 off = round_page((voff_t)lbn << vp->v_mount->mnt_fs_bshift); 845 mutex_enter(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 852 ASSERT_SEGLOCK(fs); 853 854 mutex_enter(&bufcache_lock); 855 restart: 856 for (bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) { 857 nbp = LIST_NEXT(bp, b_vnbufs); 858 if (bp->b_lblkno < lbn) 859 continue; 860 error = bbusy(bp, catch, slptimeo, NULL); 861 if (error == EPASSTHROUGH) 862 goto restart; 863 if (error != 0) { 864 mutex_exit(&bufcache_lock); 865 return (error); 866 } 867 mutex_enter(bp->b_objlock); 868 if (bp->b_oflags & BO_DELWRI) { 869 bp->b_oflags &= ~BO_DELWRI; 870 fs->lfs_avail += lfs_btofsb(fs, bp->b_bcount); 871 wakeup(&fs->lfs_avail); 872 } 873 mutex_exit(bp->b_objlock); 874 LFS_UNLOCK_BUF(bp); 875 brelsel(bp, BC_INVAL | BC_VFLUSH); 876 } 877 878 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 879 nbp = LIST_NEXT(bp, b_vnbufs); 880 if (bp->b_lblkno < lbn) 881 continue; 882 error = bbusy(bp, catch, slptimeo, NULL); 883 if (error == EPASSTHROUGH) 884 goto restart; 885 if (error != 0) { 886 mutex_exit(&bufcache_lock); 887 return (error); 888 } 889 mutex_enter(bp->b_objlock); 890 if (bp->b_oflags & BO_DELWRI) { 891 bp->b_oflags &= ~BO_DELWRI; 892 fs->lfs_avail += lfs_btofsb(fs, bp->b_bcount); 893 wakeup(&fs->lfs_avail); 894 } 895 mutex_exit(bp->b_objlock); 896 LFS_UNLOCK_BUF(bp); 897 brelsel(bp, BC_INVAL | BC_VFLUSH); 898 } 899 mutex_exit(&bufcache_lock); 900 901 return (0); 902 } 903 904