1 /* $NetBSD: lfs_inode.c,v 1.136 2013/10/17 21:01:08 christos 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.136 2013/10/17 21:01:08 christos 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 631 ASSERT_SEGLOCK(fs); 632 if (lastseg < 0 || num == 0) 633 return 0; 634 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 *= LFS_NINDIR(fs); 723 last = lastbn; 724 if (lastbn > 0) 725 last /= factor; 726 nblocks = lfs_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 = LFS_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)(LFS_NINDIR(fs) - (last + 1)) * sizeof (int32_t)); 763 error = VOP_BWRITE(bp->b_vp, bp); 764 if (error) 765 allerror = error; 766 bap = copy; 767 } 768 769 /* 770 * Recursively free totally unused blocks. 771 */ 772 for (i = LFS_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 += lfs_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 += lfs_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 += lfs_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