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